tripal_featuremap.install 18 KB

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