tripal_example.install 18 KB

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