tripal_example.install 18 KB

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