tripal_example.install

Installation of the example module

File

tripal_example/tripal_example.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the example module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. *
  9. * Perform actions when the module is disabled by the site administrator
  10. *
  11. * @ingroup tripal_example
  12. */
  13. function tripal_example_disable() {
  14. // EXPLANATION: If you are using Drupal Views you want to ensure that any
  15. // default views that your module provides are disabled when the module is
  16. // disabled. Default views are specified in the
  17. // [module name].views.default.inc file. The following code will disable these
  18. // views. If your module does not create any default views you can remove the
  19. // following code.
  20. // Disable all default views provided by this module
  21. require_once("tripal_example.views_default.inc");
  22. $views = tripal_example_views_default_views();
  23. foreach (array_keys($views) as $view_name) {
  24. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  25. }
  26. }
  27. /**
  28. * Implements hook_requirements().
  29. *
  30. * Performs check to see if all required dependencies are met. Drupal will
  31. * automatically check for module dependencies but here you can check for other
  32. * requirements.
  33. *
  34. * @ingroup tripal_example
  35. */
  36. function tripal_example_requirements($phase) {
  37. $requirements = array();
  38. if ($phase == 'install') {
  39. // EXPLANATION: It is essential that Chado be installed for almost all
  40. // Tripal modules. Therefore, the following code checks to ensure Chado is
  41. // installed and available. If your module does not require that Chado be
  42. // installed, you can remove the following check.
  43. // make sure chado is installed
  44. if (!$GLOBALS["chado_is_installed"]) {
  45. $requirements ['tripal_example'] = array(
  46. 'title' => "tripal_example",
  47. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  48. 'severity' => REQUIREMENT_ERROR,
  49. );
  50. }
  51. }
  52. return $requirements;
  53. }
  54. /**
  55. * Implements hook_install().
  56. *
  57. * Performs actions when the modules is first installed.
  58. *
  59. * @ingroup tripal_example
  60. */
  61. function tripal_example_install() {
  62. // EXPLANATION: Here is a good place to add any materialized views, controlled
  63. // vocabularies CV, databases or CV terms needed by your module.
  64. // To keep this module code short, create functions to do each of those tasks
  65. // add any materialized view
  66. tripal_example_add_mviews();
  67. // add any external databases used by the example module.
  68. tripal_example_add_dbs();
  69. // add any controlled vocabularies used by the example module. You may need
  70. // to add a vocabulary if you to set it as default (see next lines of code).
  71. // For example, the Sequence Ontology (SO) is used by the feature module as
  72. // the default vocabulary for the feature type_id field. But, that vocabulary
  73. // does not yet exist in Chado until after the SO is loaded using the Tripal
  74. // OBO loader. But, we can add it here as a place-holder so that we can then
  75. // set it as a default vocabulary (see below).
  76. tripal_example_add_cvs();
  77. // add any controlled vocabulary terms
  78. tripal_example_add_cvterms();
  79. // EXPLANATION: Many tables in Chado have a 'type_id' column which allows for
  80. // association of controlled vocabularies to describe the record. Chado places
  81. // no restrictions on which vocabularies can be used, but Tripal can be
  82. // instructed to provide a default vocabulary for any given field. For
  83. // example, the feature.type_id column will typically use the Sequence
  84. // Ontology. In that case, we can use the tripal_set_default_cv() function to
  85. // specify the Sequence Ontology (sequence) as the default vocabulary.
  86. tripal_set_default_cv('example', 'type_id', 'example_type');
  87. tripal_set_default_cv('exampleprop', 'type_id', 'example_property');
  88. tripal_set_default_cv('example_relationship', 'type_id', 'example_relationship');
  89. // add any custom tables. For this case we will add an 'example' table to the
  90. // chado schema
  91. tripal_example_add_custom_tables();
  92. }
  93. /**
  94. * Implements hook_uninstall().
  95. *
  96. * Performs actions when the modules is uninstalled.
  97. *
  98. * @ingroup tripal_example
  99. */
  100. function tripal_example_uninstall() {
  101. }
  102. /**
  103. * Implementation of hook_schema().
  104. *
  105. * Provides a list of tables to be created inside of the Drupal schema (the
  106. * 'public' schema by default). It uses the Drupal Schema API array structure to
  107. * define the table, its indexes and constraints.
  108. *
  109. * Schema API documentation is here:
  110. * https://api.drupal.org/api/drupal/includes%21database%21schema.inc/group/schemaapi/7
  111. *
  112. * @ingroup tripal_example
  113. */
  114. function tripal_example_schema() {
  115. // EXPLANATION: If your module creates a node type for data in the Chado
  116. // database then you probably need to link Drupal nodes with a respective ID
  117. // in the Chado table. The following is an example array for a table that will
  118. // link the 'chado_example' node type (created by this example module) with a
  119. // record in the fake Chado example table. This table will link the 'nid' of
  120. // the node with the 'example_id' of the example record.
  121. $schema['chado_example'] = array(
  122. 'fields' => array(
  123. 'vid' => array(
  124. 'type' => 'int',
  125. 'unsigned' => TRUE,
  126. 'not null' => TRUE,
  127. 'default' => 0
  128. ),
  129. 'nid' => array(
  130. 'type' => 'int',
  131. 'unsigned' => TRUE,
  132. 'not null' => TRUE,
  133. 'default' => 0
  134. ),
  135. 'example_id' => array(
  136. 'type' => 'int',
  137. 'not null' => TRUE,
  138. 'default' => 0
  139. ),
  140. 'sync_date' => array(
  141. 'type' => 'int',
  142. 'not null' => FALSE,
  143. 'description' => 'UNIX integer sync date/time'
  144. ),
  145. ),
  146. 'indexes' => array(
  147. 'chado_example_idx1' => array('example_id')
  148. ),
  149. 'unique keys' => array(
  150. 'chado_example_uq1' => array('nid', 'vid'),
  151. 'chado_example_uq2' => array('vid')
  152. ),
  153. 'primary key' => array('nid'),
  154. );
  155. return $schema;
  156. };
  157. /**
  158. * Creates a materialized view that stores the type & number of examples per
  159. * organism.
  160. *
  161. * @ingroup tripal_example
  162. */
  163. function tripal_example_add_mviews() {
  164. // EXPLANATION: use the tripal_add_mview() function to add a materialized view
  165. // needed by your module. If you have more than one materialized view it is
  166. // best to create a single function for each one and call each function here.
  167. // Otherwise this function can become quite long.
  168. }
  169. /**
  170. * Add cvs related to publications
  171. *
  172. * @ingroup tripal_example
  173. */
  174. function tripal_example_add_dbs() {
  175. // EXPLANATION: use the tripal_insert_db() function to add any external
  176. // databases needed by your module. If the database already exists then the
  177. // function will gracefully return.
  178. tripal_insert_db(array(
  179. 'name' => 'example_db',
  180. 'description' => 'An example database.'
  181. ));
  182. }
  183. /**
  184. * Add cvs related to publications
  185. *
  186. * @ingroup tripal_example
  187. */
  188. function tripal_example_add_cvs() {
  189. // EXPLANATION: use the tripal_insert_cv() function to add any controlled
  190. // vocabularies needed by your module. If the vocabulary already exists then
  191. // the function will gracefully return. Chado conventions use a singular name
  192. // for CV names (not plural).
  193. tripal_insert_cv(
  194. 'example_property',
  195. 'Contains property terms for examples.'
  196. );
  197. tripal_insert_cv(
  198. 'example_type',
  199. 'Contains terms describing types of examples.'
  200. );
  201. tripal_insert_cv(
  202. 'example_relationship',
  203. 'Contains terms for describing relationship types between examples.'
  204. );
  205. }
  206. /**
  207. * Adds controlled vocabulary terms needed by this module.
  208. *
  209. * @ingroup tripal_example
  210. */
  211. function tripal_example_add_cvterms() {
  212. // EXPLANATION: for our test module to work we need to add some terms to our
  213. // example_type controlled vocabulary. Ideally we should have a full OBO file
  214. // for loading but sometimes we just have a small list that won't really
  215. // change so we can add those terms here.
  216. tripal_insert_cvterm(array(
  217. 'id' => 'test', // the term accession
  218. 'name' => 'Test type', // the human readable term name
  219. 'cv_name' => 'example_type', // the CV name this term belongs to.
  220. 'definition' => 'A test type for the example module.',
  221. 'db_name' => 'example_db', // the database in which the term is found.
  222. ));
  223. }
  224. /**
  225. * Add custom tables to Chado that are required by this module
  226. *
  227. * @ingroup tripal_example
  228. */
  229. function tripal_example_add_custom_tables() {
  230. // EXPLANATION: for this example module we will create a set of example tables
  231. // that mimic Chado tables. These tables are:
  232. //
  233. // 1) example (for storing the primary example records)
  234. // 2) exampleprop (for sorting properties about the example)
  235. // 3) example_relationship (for storing relationships about examples)
  236. // 4) example_dbxref (for storing cross-references about an example)
  237. //
  238. // To make the code easier to read, each table is created by a separate
  239. // function called here:
  240. tripal_example_add_example_table();
  241. tripal_example_add_exampleprop_table();
  242. tripal_example_add_example_relationship_table();
  243. tripal_example_add_example_dbxref_table();
  244. }
  245. /**
  246. * Adds the 'example' custom table to Chado.
  247. *
  248. * @ingroup tripal_example
  249. */
  250. function tripal_example_add_example_table() {
  251. // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
  252. // add the table using the chado_create_custom_table() function.
  253. $schema = array(
  254. 'table' => 'example',
  255. 'fields' => array(
  256. 'example_id' => array(
  257. 'type' => 'serial',
  258. 'not null' => true,
  259. ),
  260. 'uniquename' => array(
  261. 'type' => 'varchar',
  262. 'length' => '255',
  263. 'not null' => TRUE,
  264. ),
  265. 'type_id' => array(
  266. 'type' => 'int',
  267. 'not null' => true,
  268. ),
  269. 'organism_id' => array(
  270. 'type' => 'int',
  271. 'not null' => true,
  272. ),
  273. 'description' => array(
  274. 'type' => 'text',
  275. ),
  276. ),
  277. 'primary key' => array(
  278. 0 => 'example_id',
  279. ),
  280. 'unique keys' => array(
  281. 'example_uq1' => array(
  282. 0 => 'uniquename',
  283. 1 => 'type_id',
  284. 2 => 'organism_id',
  285. ),
  286. ),
  287. 'indexes' => array(
  288. 'example_idx1' => array(
  289. 0 => 'example_id',
  290. ),
  291. 'example_idx2' => array(
  292. 0 => 'uniquename',
  293. ),
  294. ),
  295. 'foreign keys' => array(
  296. 'cvterm' => array(
  297. 'table' => 'cvterm',
  298. 'columns' => array(
  299. 'type_id' => 'cvterm_id',
  300. ),
  301. ),
  302. 'organism' => array(
  303. 'table' => 'organism',
  304. 'columns' => array(
  305. 'organism_id' => 'organism_id',
  306. ),
  307. ),
  308. ),
  309. // EXPLANATION: the 'referring_tables' array is the list of tables that have
  310. // a foreign key relationships with this table. This information is required
  311. // for the Tripal API to be able to expand tables in templates.
  312. 'referring_tables' => array(
  313. 0 => 'example_relationship',
  314. 1 => 'exampleprop',
  315. 2 => 'example_dbxref',
  316. ),
  317. );
  318. chado_create_custom_table('example', $schema, TRUE);
  319. }
  320. /**
  321. * Adds the 'example_relationship' custom table to Chado.
  322. *
  323. * @ingroup tripal_example
  324. */
  325. function tripal_example_add_exampleprop_table() {
  326. // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
  327. // add the table using the chado_create_custom_table() function.
  328. // Add the exampleprop table
  329. $schema = array(
  330. 'table' => 'exampleprop',
  331. 'fields' => array(
  332. 'exampleprop_id' => array(
  333. 'type' => 'serial',
  334. 'not null' => TRUE,
  335. ),
  336. 'example_id' => array(
  337. 'type' => 'int',
  338. 'not null' => TRUE,
  339. ),
  340. 'type_id' => array(
  341. 'type' => 'int',
  342. 'not null' => TRUE,
  343. ),
  344. 'value' => array(
  345. 'type' => 'text',
  346. 'not null' => FALSE,
  347. ),
  348. 'rank' => array(
  349. 'type' => 'int',
  350. 'not null' => TRUE,
  351. ),
  352. ),
  353. 'primary key' => array(
  354. 0 => 'exampleprop_id',
  355. ),
  356. 'unique keys' => array(
  357. 'example_id_type_id_rank' => array(
  358. 0 => 'example_id',
  359. 1 => 'type_id',
  360. 2 => 'rank',
  361. ),
  362. ),
  363. 'foreign keys' => array(
  364. 'cvterm' => array(
  365. 'table' => 'cvterm',
  366. 'columns' => array(
  367. 'type_id' => 'cvterm_id',
  368. ),
  369. ),
  370. 'example' => array(
  371. 'table' => 'example',
  372. 'columns' => array(
  373. 'example_id' => 'example_id',
  374. ),
  375. ),
  376. ),
  377. );
  378. chado_create_custom_table('exampleprop', $schema, TRUE);
  379. }
  380. /**
  381. * Adds the 'example_relationship' custom table to Chado.
  382. *
  383. * @ingroup tripal_example
  384. */
  385. function tripal_example_add_example_relationship_table() {
  386. // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
  387. // add the table using the chado_create_custom_table() function.
  388. $schema = array(
  389. 'table' => 'example_relationship',
  390. 'fields' => array(
  391. 'example_relationship_id' => array(
  392. 'type' => 'serial',
  393. 'not null' => TRUE,
  394. ),
  395. 'subject_id' => array(
  396. 'type' => 'int',
  397. 'not null' => TRUE,
  398. ),
  399. 'object_id' => array(
  400. 'type' => 'int',
  401. 'not null' => TRUE,
  402. ),
  403. 'type_id' => array(
  404. 'type' => 'int',
  405. 'not null' => TRUE,
  406. ),
  407. 'value' => array(
  408. 'type' => 'text',
  409. 'not null' => FALSE,
  410. ),
  411. 'rank' => array(
  412. 'type' => 'int',
  413. 'not null' => TRUE,
  414. 'default' => 0,
  415. ),
  416. ),
  417. 'primary key' => array(
  418. 0 => 'example_relationship_id',
  419. ),
  420. 'unique keys' => array(
  421. 'example_relationship_c1' => array(
  422. 0 => 'subject_id',
  423. 1 => 'object_id',
  424. 2 => 'type_id',
  425. 3 => 'rank',
  426. ),
  427. ),
  428. 'indexes' => array(
  429. 'example_relationship_idx1' => array(
  430. 0 => 'subject_id',
  431. ),
  432. 'example_relationship_idx2' => array(
  433. 0 => 'object_id',
  434. ),
  435. 'example_relationship_idx3' => array(
  436. 0 => 'type_id',
  437. ),
  438. ),
  439. 'foreign keys' => array(
  440. 'cvterm' => array(
  441. 'table' => 'cvterm',
  442. 'columns' => array(
  443. 'type_id' => 'cvterm_id',
  444. ),
  445. ),
  446. 'example' => array(
  447. 'table' => 'example',
  448. 'columns' => array(
  449. 'subject_id' => 'example_id',
  450. 'object_id' => 'example_id',
  451. ),
  452. ),
  453. ),
  454. );
  455. chado_create_custom_table('example_relationship', $schema, TRUE);
  456. }
  457. /**
  458. * Adds the 'example_dbxref' custom table to Chado.
  459. *
  460. * @ingroup tripal_example
  461. */
  462. function tripal_example_add_example_dbxref_table() {
  463. // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
  464. // add the table using the chado_create_custom_table() function.
  465. $schema = array(
  466. 'table' => 'example_dbxref',
  467. 'fields' => array(
  468. 'example_dbxref_id' => array(
  469. 'type' => 'serial',
  470. 'not null' => TRUE,
  471. ),
  472. 'example_id' => array(
  473. 'type' => 'int',
  474. 'not null' => TRUE,
  475. ),
  476. 'dbxref_id' => array(
  477. 'type' => 'int',
  478. 'not null' => TRUE,
  479. ),
  480. 'is_current' => array(
  481. 'type' => 'int',
  482. 'size' => 'tiny',
  483. 'not null' => TRUE,
  484. 'default' => 1,
  485. ),
  486. ),
  487. 'primary key' => array(
  488. 0 => 'example_dbxref_id',
  489. ),
  490. 'unique keys' => array(
  491. 'example_dbxref_unq1' => array(
  492. 0 => 'example_id',
  493. 1 => 'dbxref_id',
  494. ),
  495. ),
  496. 'indexes' => array(
  497. 'example_dbxref_idx1' => array(
  498. 0 => 'example_id',
  499. ),
  500. 'example_dbxref_idx2' => array(
  501. 0 => 'dbxref_id',
  502. ),
  503. ),
  504. 'foreign keys' => array(
  505. 'dbxref' => array(
  506. 'table' => 'dbxref',
  507. 'columns' => array(
  508. 'dbxref_id' => 'dbxref_id',
  509. ),
  510. ),
  511. 'example' => array(
  512. 'table' => 'example',
  513. 'columns' => array(
  514. 'example_id' => 'example_id',
  515. ),
  516. ),
  517. ),
  518. );
  519. chado_create_custom_table('example_dbxref', $schema, TRUE);
  520. }
  521. /**
  522. * This is the required update for tripal_example.
  523. */
  524. function tripal_example_update_7200() {
  525. // EXPLANATION: as you create new releases of your module you may find that
  526. // tables your module created, or data may need to be adjusted. This function
  527. // allows you to do that. This function is executed using the
  528. // http://[your site]/update.php URL or using the drush command 'updatedb'.
  529. // This function should be named according to the instructions provided here:
  530. // https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7
  531. //
  532. // Make sure we have the full API loaded this will help during a
  533. // site upgrade when the tripal_core module is disabled.
  534. module_load_include('module', 'tripal_core', 'tripal_core');
  535. tripal_core_import_api();
  536. // it is good to wrap any database changes inside of a try catch block:
  537. try {
  538. // perform database changes
  539. }
  540. catch (\PDOException $e) {
  541. $error = $e->getMessage();
  542. throw new DrupalUpdateException('Could not apply updates: '. $error);
  543. }
  544. }
  545. /**
  546. * Implementation of hook_update_dependencies().
  547. *
  548. * It specifies a list of other modules whose updates must be run prior to
  549. * this one. It also ensures the the Tripal API is in scope for site
  550. * upgrades when tripal_core is disabled.
  551. */
  552. function tripal_example_update_dependencies() {
  553. $dependencies = array();
  554. // EXPLANATION: here we can specify which modules must be updated prior to
  555. // applying the updates in this module. This is useful because it prevents
  556. // updates from being executed out of order. The following example code shows
  557. // that the 'tripal_example' module update number 7200 must be executed after
  558. // the 'tripal_cv' module's 7200 update.
  559. $dependencies['tripal_example'][7200] = array(
  560. 'tripal_cv' => 7200
  561. );
  562. return $dependencies;
  563. }