tripal_featuremap.install 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. /**
  3. * @file
  4. * Handles installation of the feature map module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_featuremap
  11. */
  12. function tripal_featuremap_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_featuremap.views_default.inc");
  15. $views = tripal_featuremap_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_featuremap
  24. */
  25. function tripal_featuremap_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_featuremap'] = array(
  31. 'title' => "tripal_featuremap",
  32. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_featuremap
  43. */
  44. function tripal_featuremap_install() {
  45. // create the module's data directory
  46. tripal_create_files_dir('tripal_featuremap');
  47. // add the featuremapprop table to Chado
  48. tripal_featuremap_add_custom_tables();
  49. // Add cvterms
  50. tripal_featuremap_add_cvs();
  51. tripal_featuremap_add_cvterms();
  52. // set the default vocabularies
  53. tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
  54. tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
  55. tripal_set_default_cv('featuremap', 'uinittype_id', 'featuremap_units');
  56. }
  57. /**
  58. * Implementation of hook_uninstall().
  59. *
  60. * @ingroup tripal_featuremap
  61. */
  62. function tripal_featuremap_uninstall() {
  63. }
  64. /**
  65. * Implementation of hook_schema().
  66. *
  67. * @ingroup tripal_featuremap
  68. */
  69. function tripal_featuremap_schema() {
  70. $schema['chado_featuremap'] = array(
  71. 'fields' => array(
  72. 'vid' => array(
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. 'default' => 0
  77. ),
  78. 'nid' => array(
  79. 'type' => 'int',
  80. 'unsigned' => TRUE,
  81. 'not null' => TRUE,
  82. 'default' => 0
  83. ),
  84. 'featuremap_id' => array(
  85. 'type' => 'int',
  86. 'not null' => TRUE,
  87. 'default' => 0
  88. )
  89. ),
  90. 'indexes' => array(
  91. 'featuremap_id' => array('featuremap_id')
  92. ),
  93. 'unique keys' => array(
  94. 'nid_vid' => array('nid', 'vid'),
  95. 'vid' => array('vid')
  96. ),
  97. 'primary key' => array('nid'),
  98. );
  99. return $schema;
  100. }
  101. /**
  102. * Add cvs needed by the featuremap module
  103. *
  104. * @ingroup tripal_featuremap
  105. */
  106. function tripal_featuremap_add_cvs() {
  107. tripal_insert_cv(
  108. 'featuremap_units',
  109. 'Contains map unit types for the unittype_id column of the featuremap table.'
  110. );
  111. tripal_insert_cv(
  112. 'featurepos_property',
  113. 'Contains terms map properties.'
  114. );
  115. tripal_insert_cv(
  116. 'featuremap_property',
  117. 'Contains positional types for the feature positions'
  118. );
  119. }
  120. /**
  121. * Add cv terms needed by the featuremap module
  122. *
  123. * @ingroup tripal_featuremap
  124. */
  125. function tripal_featuremap_add_cvterms() {
  126. // add cvterms for the map unit types
  127. tripal_insert_cvterm(
  128. array(
  129. 'name' => 'cM',
  130. 'definition' => 'Centimorgan units',
  131. 'cv_name' => 'featuremap_units',
  132. 'is_relationship' => 0,
  133. 'db_name' => 'tripal'
  134. ),
  135. array('update_existing' => TRUE)
  136. );
  137. tripal_insert_cvterm(
  138. array(
  139. 'name' => 'bp',
  140. 'definition' => 'Base pairs units',
  141. 'cv_name' => 'featuremap_units',
  142. 'is_relationship' => 0,
  143. 'db_name' => 'tripal'
  144. ),
  145. array('update_existing' => TRUE)
  146. );
  147. tripal_insert_cvterm(
  148. array(
  149. 'name' => 'bin_unit',
  150. 'definition' => 'The bin unit',
  151. 'cv_name' => 'featuremap_units',
  152. 'is_relationship' => 0,
  153. 'db_name' => 'tripal'
  154. ),
  155. array('update_existing' => TRUE)
  156. );
  157. tripal_insert_cvterm(
  158. array(
  159. 'name' => 'marker_order',
  160. 'definition' => 'Units simply to define marker order.',
  161. 'cv_name' => 'featuremap_units',
  162. 'is_relationship' => 0,
  163. 'db_name' => 'tripal'
  164. ),
  165. array('update_existing' => TRUE)
  166. );
  167. tripal_insert_cvterm(
  168. array(
  169. 'name' => 'undefined',
  170. 'definition' => 'A catch-all for an undefined unit type',
  171. 'cv_name' => 'featuremap_units',
  172. 'is_relationship' => 0,
  173. 'db_name' => 'tripal'
  174. ),
  175. array('update_existing' => TRUE)
  176. );
  177. // featurepos properties
  178. tripal_insert_cvterm(
  179. array(
  180. 'name' => 'start',
  181. 'definition' => 'The start coordinate for a map feature.',
  182. 'cv_name' => 'featurepos_property',
  183. 'is_relationship' => 0,
  184. 'db_name' => 'tripal'
  185. ),
  186. array('update_existing' => TRUE)
  187. );
  188. tripal_insert_cvterm(
  189. array(
  190. 'name' => 'stop',
  191. 'definition' => 'The end coordinate for a map feature',
  192. 'cv_name' => 'featurepos_property',
  193. 'is_relationship' => 0,
  194. 'db_name' => 'tripal'
  195. ),
  196. array('update_existing' => TRUE)
  197. );
  198. // add cvterms for map properties
  199. tripal_insert_cvterm(
  200. array(
  201. 'name' => 'Map Dbxref',
  202. 'definition' => 'A unique identifer for the map in a remote database. The '
  203. . 'format is a database abbreviation and a unique accession separated '
  204. . 'by a colon. (e.g. Gramene:tsh1996a)',
  205. 'cv_name' => 'featuremap_property',
  206. 'is_relationship' => 0,
  207. 'db_name' => 'tripal'
  208. ),
  209. array('update_existing' => TRUE)
  210. );
  211. tripal_insert_cvterm(
  212. array(
  213. 'name' => 'Map Type',
  214. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  215. 'cv_name' => 'featuremap_property',
  216. 'is_relationship' => 0,
  217. 'db_name' => 'tripal'
  218. ),
  219. array('update_existing' => TRUE)
  220. );
  221. tripal_insert_cvterm(
  222. array(
  223. 'name' => 'Genome Group',
  224. 'definition' => '',
  225. 'cv_name' => 'featuremap_property',
  226. 'is_relationship' => 0,
  227. 'db_name' => 'tripal'
  228. ),
  229. array('update_existing' => TRUE)
  230. );
  231. tripal_insert_cvterm(
  232. array(
  233. 'name' => 'URL',
  234. 'definition' => 'A univeral resource locator (URL) reference where the '
  235. . 'publication can be found. For maps found online, this would be '
  236. . 'the web address for the map.',
  237. 'cv_name' => 'featuremap_property',
  238. 'is_relationship' => 0,
  239. 'db_name' => 'tripal'
  240. ),
  241. array('update_existing' => TRUE)
  242. );
  243. tripal_insert_cvterm(
  244. array(
  245. 'name' => 'Population Type',
  246. 'definition' => 'A brief description of the population type used to generate '
  247. . 'the map (e.g. RIL, F2, BC1, etc).',
  248. 'cv_name' => 'featuremap_property',
  249. 'is_relationship' => 0,
  250. 'db_name' => 'tripal'
  251. ),
  252. array('update_existing' => TRUE)
  253. );
  254. tripal_insert_cvterm(
  255. array(
  256. 'name' => 'Population Size',
  257. 'definition' => 'The size of the population used to construct the map.',
  258. 'cv_name' => 'featuremap_property',
  259. 'is_relationship' => 0,
  260. 'db_name' => 'tripal'
  261. ),
  262. array('update_existing' => TRUE)
  263. );
  264. tripal_insert_cvterm(
  265. array(
  266. 'name' => 'Methods',
  267. 'definition' => 'A brief description of the methods used to construct the map.',
  268. 'cv_name' => 'featuremap_property',
  269. 'is_relationship' => 0,
  270. 'db_name' => 'tripal'
  271. ),
  272. array('update_existing' => TRUE)
  273. );
  274. tripal_insert_cvterm(
  275. array(
  276. 'name' => 'Software',
  277. 'definition' => 'The software used to construct the map.',
  278. 'cv_name' => 'featuremap_property',
  279. 'is_relationship' => 0,
  280. 'db_name' => 'tripal'
  281. ),
  282. array('update_existing' => TRUE)
  283. }
  284. /**
  285. * Add custom tables needed by the feature map module
  286. * - featuremapprop
  287. * - featuremap_dbxref
  288. * - featureposprop
  289. *
  290. * @ingroup tripal_featuremap
  291. */
  292. function tripal_featuremap_add_custom_tables(){
  293. // add the featuremaprop table to Chado
  294. $schema = array (
  295. 'table' => 'featuremapprop',
  296. 'fields' => array (
  297. 'featuremapprop_id' => array (
  298. 'type' => 'serial',
  299. 'not null' => true,
  300. ),
  301. 'featuremap_id' => array (
  302. 'type' => 'int',
  303. 'not null' => true,
  304. ),
  305. 'type_id' => array (
  306. 'type' => 'int',
  307. 'not null' => true,
  308. ),
  309. 'value' => array (
  310. 'type' => 'text',
  311. 'not null' => false,
  312. ),
  313. 'rank' => array (
  314. 'type' => 'int',
  315. 'not null' => true,
  316. 'default' => 0,
  317. ),
  318. ),
  319. 'primary key' => array (
  320. 0 => 'featuremapprop_id',
  321. ),
  322. 'unique keys' => array (
  323. 'featuremapprop_c1' => array (
  324. 0 => 'featuremap_id',
  325. 1 => 'type_id',
  326. 2 => 'rank',
  327. ),
  328. ),
  329. 'indexes' => array (
  330. 'featuremapprop_idx1' => array (
  331. 0 => 'featuremap_id',
  332. ),
  333. 'featuremapprop_idx2' => array (
  334. 0 => 'type_id',
  335. ),
  336. ),
  337. 'foreign keys' => array (
  338. 'cvterm' => array (
  339. 'table' => 'cvterm',
  340. 'columns' => array (
  341. 'type_id' => 'cvterm_id',
  342. ),
  343. ),
  344. 'featuremap' => array (
  345. 'table' => 'featuremap',
  346. 'columns' => array (
  347. 'featuremap_id' => 'featuremap_id',
  348. ),
  349. ),
  350. ),
  351. );
  352. chado_create_custom_table('featuremapprop', $schema, TRUE);
  353. // add the featuremap_dbxref table to Chado
  354. $schema = array (
  355. 'table' => 'featuremap_dbxref',
  356. 'fields' => array (
  357. 'featuremap_dbxref_id' => array (
  358. 'type' => 'serial',
  359. 'not null' => true,
  360. ),
  361. 'featuremap_id' => array (
  362. 'type' => 'int',
  363. 'not null' => true,
  364. ),
  365. 'dbxref_id' => array (
  366. 'type' => 'int',
  367. 'not null' => true,
  368. ),
  369. ),
  370. 'primary key' => array (
  371. 0 => 'featuremap_dbxref_id',
  372. ),
  373. 'unique keys' => array (
  374. 'featuremap_dbxref_c1' => array (
  375. 0 => 'featuremap_id',
  376. 1 => 'dbxref_id',
  377. ),
  378. ),
  379. 'indexes' => array (
  380. 'featuremap_dbxref_idx1' => array (
  381. 0 => 'featuremap_dbxref_id',
  382. ),
  383. 'featuremap_dbxref_idx2' => array (
  384. 0 => 'dbxref_id',
  385. ),
  386. ),
  387. 'foreign keys' => array (
  388. 'dbxref' => array (
  389. 'table' => 'dbxref',
  390. 'columns' => array (
  391. 'dbxref_id' => 'dbxref_id',
  392. ),
  393. ),
  394. 'featuremap' => array (
  395. 'table' => 'featuremap',
  396. 'columns' => array (
  397. 'featuremap_id' => 'featuremap_id',
  398. ),
  399. ),
  400. ),
  401. 'referring_tables' => NULL,
  402. );
  403. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  404. $schema = array (
  405. 'table' => 'featureposprop',
  406. 'fields' => array (
  407. 'featureposprop_id' => array (
  408. 'type' => 'serial',
  409. 'not null' => true,
  410. ),
  411. 'featurepos_id' => array (
  412. 'type' => 'int',
  413. 'not null' => true,
  414. ),
  415. 'type_id' => array (
  416. 'type' => 'int',
  417. 'not null' => true,
  418. ),
  419. 'value' => array (
  420. 'type' => 'text',
  421. 'not null' => false,
  422. ),
  423. 'rank' => array (
  424. 'type' => 'int',
  425. 'not null' => true,
  426. 'default' => 0,
  427. ),
  428. ),
  429. 'primary key' => array (
  430. 0 => 'featureposprop_id',
  431. ),
  432. 'unique keys' => array (
  433. 'featureposprop_id' => array (
  434. 0 => 'featurepos_id',
  435. 1 => 'type_id',
  436. 2 => 'rank',
  437. ),
  438. ),
  439. 'indexes' => array (
  440. 'featureposprop_c1' => array (
  441. 0 => 'featurepos_id',
  442. ),
  443. 'featureposprop_idx2' => array (
  444. 0 => 'type_id',
  445. ),
  446. ),
  447. 'foreign keys' => array (
  448. 'cvterm' => array (
  449. 'table' => 'cvterm',
  450. 'columns' => array (
  451. 'type_id' => 'cvterm_id',
  452. ),
  453. ),
  454. 'featurepos' => array (
  455. 'table' => 'featurepos',
  456. 'columns' => array (
  457. 'featurepos_id' => 'featurepos_id',
  458. ),
  459. ),
  460. ),
  461. );
  462. chado_create_custom_table('featureposprop', $schema, TRUE);
  463. }
  464. /**
  465. * This is the required update for tripal_featuremap when upgrading from Drupal core API 6.x.
  466. * This update may take some time to complete.
  467. */
  468. function tripal_featuremap_update_7200() {
  469. // We can't use the Tripal API during an upgrade from D6 to D7 Tripal because the tripal_core
  470. // module is disabled. So, we have to manually make database additions/changes to chado.
  471. // set the default vocabularies
  472. // featuremap_units
  473. try {
  474. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_units'")->fetchField();
  475. db_insert('tripal_cv_defaults')
  476. ->fields(array(
  477. 'table_name' => 'featuremap',
  478. 'field_name' => 'unittype_id',
  479. 'cv_id' => $cv_id
  480. ))
  481. ->execute();
  482. }
  483. catch (\PDOException $e) {
  484. $error = $e->getMessage();
  485. throw new DrupalUpdateException('Failed to set featuremap_units vocabulary as default: '. $error);
  486. }
  487. // featurepos_property
  488. try {
  489. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featurepos_property'")->fetchField();
  490. db_insert('tripal_cv_defaults')
  491. ->fields(array(
  492. 'table_name' => 'featureposprop',
  493. 'field_name' => 'type_id',
  494. 'cv_id' => $cv_id
  495. ))
  496. ->execute();
  497. }
  498. catch (\PDOException $e) {
  499. $error = $e->getMessage();
  500. throw new DrupalUpdateException('Failed to set featurepos_property vocabulary as default: '. $error);
  501. }
  502. // featuremap_property
  503. try {
  504. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_property'")->fetchField();
  505. db_insert('tripal_cv_defaults')
  506. ->fields(array(
  507. 'table_name' => 'featuremapprop',
  508. 'field_name' => 'type_id',
  509. 'cv_id' => $cv_id
  510. ))
  511. ->execute();
  512. }
  513. catch (\PDOException $e) {
  514. $error = $e->getMessage();
  515. throw new DrupalUpdateException('Failed to set featuremap_property vocabulary as default: '. $error);
  516. }
  517. }
  518. /**
  519. * Implementation of hook_update_dependencies(). It specifies a list of
  520. * other modules whose updates must be run prior to this one.
  521. */
  522. function tripal_featuremap_update_dependencies() {
  523. $dependencies = array();
  524. // the tripal_cv update 7200 must run prior to update 7200 of this module
  525. $dependencies['tripal_featuremap'][7200] = array(
  526. 'tripal_cv' => 7200
  527. );
  528. return $dependencies;
  529. }
  530. /**
  531. * Adds missing foreign key constraints
  532. *
  533. */
  534. function tripal_featuremap_update_7201() {
  535. // there was a bug in the function for creating a custom table that
  536. // kept foreign key constraints from being added. So, we need to add those
  537. // to keep from error messages appear, we will drop the FK if it already
  538. // exists and then re-add it.
  539. try {
  540. // featuremapprop table
  541. db_query('
  542. ALTER TABLE chado.featuremapprop
  543. DROP CONSTRAINT IF EXISTS featuremapprop_type_id_fkey CASCADE
  544. ');
  545. db_query('
  546. ALTER TABLE chado.featuremapprop
  547. DROP CONSTRAINT IF EXISTS featuremapprop_featuremap_id_fkey CASCADE
  548. ');
  549. db_query('
  550. ALTER TABLE chado.featuremapprop
  551. ADD CONSTRAINT featuremapprop_type_id_fkey
  552. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  553. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  554. ');
  555. db_query('
  556. ALTER TABLE chado.featuremapprop
  557. ADD CONSTRAINT featuremapprop_featuremap_id_fkey
  558. FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
  559. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  560. ');
  561. // featuremap_dbref table
  562. db_query('
  563. ALTER TABLE chado.featuremap_dbxref
  564. DROP CONSTRAINT IF EXISTS featuremap_dbxref_dbxref_id_fkey CASCADE
  565. ');
  566. db_query('
  567. ALTER TABLE chado.featuremap_dbxref
  568. DROP CONSTRAINT IF EXISTS featuremap_dbxref_featuremap_id_fkey CASCADE
  569. ');
  570. db_query('
  571. ALTER TABLE chado.featuremap_dbxref
  572. ADD CONSTRAINT featuremap_dbxref_dbxref_id_fkey
  573. FOREIGN KEY (dbxref_id) REFERENCES chado.dbxref (dbxref_id)
  574. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  575. ');
  576. db_query('
  577. ALTER TABLE chado.featuremap_dbxref
  578. ADD CONSTRAINT featuremap_dbxref_featuremap_id_fkey
  579. FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
  580. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  581. ');
  582. // featureposprop
  583. db_query('
  584. ALTER TABLE chado.featureposprop
  585. DROP CONSTRAINT IF EXISTS featureposprop_type_id_fkey CASCADE
  586. ');
  587. db_query('
  588. ALTER TABLE chado.featureposprop
  589. DROP CONSTRAINT IF EXISTS featureposprop_featurepos_id_fkey CASCADE
  590. ');
  591. db_query('
  592. ALTER TABLE chado.featureposprop
  593. ADD CONSTRAINT featureposprop_type_id_fkey
  594. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  595. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  596. ');
  597. db_query('
  598. ALTER TABLE chado.featureposprop
  599. ADD CONSTRAINT featureposprop_featurepos_id_fkey
  600. FOREIGN KEY (featurepos_id) REFERENCES chado.featurepos (featurepos_id)
  601. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  602. ');
  603. }
  604. catch (\PDOException $e) {
  605. $error = $e->getMessage();
  606. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  607. }
  608. }