tripal_featuremap.install 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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', 'unittype_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. /**
  286. * Add custom tables needed by the feature map module
  287. * - featuremapprop
  288. * - featuremap_dbxref
  289. * - featureposprop
  290. *
  291. * @ingroup tripal_featuremap
  292. */
  293. function tripal_featuremap_add_custom_tables(){
  294. // add the featuremaprop table to Chado
  295. $schema = array (
  296. 'table' => 'featuremapprop',
  297. 'fields' => array (
  298. 'featuremapprop_id' => array (
  299. 'type' => 'serial',
  300. 'not null' => true,
  301. ),
  302. 'featuremap_id' => array (
  303. 'type' => 'int',
  304. 'not null' => true,
  305. ),
  306. 'type_id' => array (
  307. 'type' => 'int',
  308. 'not null' => true,
  309. ),
  310. 'value' => array (
  311. 'type' => 'text',
  312. 'not null' => false,
  313. ),
  314. 'rank' => array (
  315. 'type' => 'int',
  316. 'not null' => true,
  317. 'default' => 0,
  318. ),
  319. ),
  320. 'primary key' => array (
  321. 0 => 'featuremapprop_id',
  322. ),
  323. 'unique keys' => array (
  324. 'featuremapprop_c1' => array (
  325. 0 => 'featuremap_id',
  326. 1 => 'type_id',
  327. 2 => 'rank',
  328. ),
  329. ),
  330. 'indexes' => array (
  331. 'featuremapprop_idx1' => array (
  332. 0 => 'featuremap_id',
  333. ),
  334. 'featuremapprop_idx2' => array (
  335. 0 => 'type_id',
  336. ),
  337. ),
  338. 'foreign keys' => array (
  339. 'cvterm' => array (
  340. 'table' => 'cvterm',
  341. 'columns' => array (
  342. 'type_id' => 'cvterm_id',
  343. ),
  344. ),
  345. 'featuremap' => array (
  346. 'table' => 'featuremap',
  347. 'columns' => array (
  348. 'featuremap_id' => 'featuremap_id',
  349. ),
  350. ),
  351. ),
  352. );
  353. chado_create_custom_table('featuremapprop', $schema, TRUE);
  354. // add the featuremap_dbxref table to Chado
  355. $schema = array (
  356. 'table' => 'featuremap_dbxref',
  357. 'fields' => array (
  358. 'featuremap_dbxref_id' => array (
  359. 'type' => 'serial',
  360. 'not null' => true,
  361. ),
  362. 'featuremap_id' => array (
  363. 'type' => 'int',
  364. 'not null' => true,
  365. ),
  366. 'dbxref_id' => array (
  367. 'type' => 'int',
  368. 'not null' => true,
  369. ),
  370. ),
  371. 'primary key' => array (
  372. 0 => 'featuremap_dbxref_id',
  373. ),
  374. 'unique keys' => array (
  375. 'featuremap_dbxref_c1' => array (
  376. 0 => 'featuremap_id',
  377. 1 => 'dbxref_id',
  378. ),
  379. ),
  380. 'indexes' => array (
  381. 'featuremap_dbxref_idx1' => array (
  382. 0 => 'featuremap_dbxref_id',
  383. ),
  384. 'featuremap_dbxref_idx2' => array (
  385. 0 => 'dbxref_id',
  386. ),
  387. ),
  388. 'foreign keys' => array (
  389. 'dbxref' => array (
  390. 'table' => 'dbxref',
  391. 'columns' => array (
  392. 'dbxref_id' => 'dbxref_id',
  393. ),
  394. ),
  395. 'featuremap' => array (
  396. 'table' => 'featuremap',
  397. 'columns' => array (
  398. 'featuremap_id' => 'featuremap_id',
  399. ),
  400. ),
  401. ),
  402. 'referring_tables' => NULL,
  403. );
  404. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  405. $schema = array (
  406. 'table' => 'featureposprop',
  407. 'fields' => array (
  408. 'featureposprop_id' => array (
  409. 'type' => 'serial',
  410. 'not null' => true,
  411. ),
  412. 'featurepos_id' => array (
  413. 'type' => 'int',
  414. 'not null' => true,
  415. ),
  416. 'type_id' => array (
  417. 'type' => 'int',
  418. 'not null' => true,
  419. ),
  420. 'value' => array (
  421. 'type' => 'text',
  422. 'not null' => false,
  423. ),
  424. 'rank' => array (
  425. 'type' => 'int',
  426. 'not null' => true,
  427. 'default' => 0,
  428. ),
  429. ),
  430. 'primary key' => array (
  431. 0 => 'featureposprop_id',
  432. ),
  433. 'unique keys' => array (
  434. 'featureposprop_id' => array (
  435. 0 => 'featurepos_id',
  436. 1 => 'type_id',
  437. 2 => 'rank',
  438. ),
  439. ),
  440. 'indexes' => array (
  441. 'featureposprop_c1' => array (
  442. 0 => 'featurepos_id',
  443. ),
  444. 'featureposprop_idx2' => 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. 'featurepos' => array (
  456. 'table' => 'featurepos',
  457. 'columns' => array (
  458. 'featurepos_id' => 'featurepos_id',
  459. ),
  460. ),
  461. ),
  462. );
  463. chado_create_custom_table('featureposprop', $schema, TRUE);
  464. }
  465. /**
  466. * This is the required update for tripal_featuremap when upgrading from Drupal core API 6.x.
  467. * This update may take some time to complete.
  468. */
  469. function tripal_featuremap_update_7200() {
  470. // We can't use the Tripal API during an upgrade from D6 to D7 Tripal because the tripal_core
  471. // module is disabled. So, we have to manually make database additions/changes to chado.
  472. // set the default vocabularies
  473. // featuremap_units
  474. try {
  475. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_units'")->fetchField();
  476. db_insert('tripal_cv_defaults')
  477. ->fields(array(
  478. 'table_name' => 'featuremap',
  479. 'field_name' => 'unittype_id',
  480. 'cv_id' => $cv_id
  481. ))
  482. ->execute();
  483. }
  484. catch (\PDOException $e) {
  485. $error = $e->getMessage();
  486. throw new DrupalUpdateException('Failed to set featuremap_units vocabulary as default: '. $error);
  487. }
  488. // featurepos_property
  489. try {
  490. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featurepos_property'")->fetchField();
  491. db_insert('tripal_cv_defaults')
  492. ->fields(array(
  493. 'table_name' => 'featureposprop',
  494. 'field_name' => 'type_id',
  495. 'cv_id' => $cv_id
  496. ))
  497. ->execute();
  498. }
  499. catch (\PDOException $e) {
  500. $error = $e->getMessage();
  501. throw new DrupalUpdateException('Failed to set featurepos_property vocabulary as default: '. $error);
  502. }
  503. // featuremap_property
  504. try {
  505. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_property'")->fetchField();
  506. db_insert('tripal_cv_defaults')
  507. ->fields(array(
  508. 'table_name' => 'featuremapprop',
  509. 'field_name' => 'type_id',
  510. 'cv_id' => $cv_id
  511. ))
  512. ->execute();
  513. }
  514. catch (\PDOException $e) {
  515. $error = $e->getMessage();
  516. throw new DrupalUpdateException('Failed to set featuremap_property vocabulary as default: '. $error);
  517. }
  518. }
  519. /**
  520. * Implementation of hook_update_dependencies(). It specifies a list of
  521. * other modules whose updates must be run prior to this one.
  522. */
  523. function tripal_featuremap_update_dependencies() {
  524. $dependencies = array();
  525. // the tripal_cv update 7200 must run prior to update 7200 of this module
  526. $dependencies['tripal_featuremap'][7200] = array(
  527. 'tripal_cv' => 7200
  528. );
  529. return $dependencies;
  530. }
  531. /**
  532. * Adds missing foreign key constraints
  533. *
  534. */
  535. function tripal_featuremap_update_7201() {
  536. // there was a bug in the function for creating a custom table that
  537. // kept foreign key constraints from being added. So, we need to add those
  538. // to keep from error messages appear, we will drop the FK if it already
  539. // exists and then re-add it.
  540. try {
  541. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'featuremapprop_type_id_fkey'))->fetchField();
  542. if ($fkey_exists) {
  543. // featuremapprop table
  544. db_query('
  545. ALTER TABLE chado.featuremapprop
  546. DROP CONSTRAINT featuremapprop_type_id_fkey CASCADE
  547. ');
  548. db_query('
  549. ALTER TABLE chado.featuremapprop
  550. DROP CONSTRAINT featuremapprop_featuremap_id_fkey CASCADE
  551. ');
  552. }
  553. db_query('
  554. ALTER TABLE chado.featuremapprop
  555. ADD CONSTRAINT featuremapprop_type_id_fkey
  556. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  557. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  558. ');
  559. db_query('
  560. ALTER TABLE chado.featuremapprop
  561. ADD CONSTRAINT featuremapprop_featuremap_id_fkey
  562. FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
  563. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  564. ');
  565. // featuremap_dbref table
  566. if ($fkey_exists) {
  567. db_query('
  568. ALTER TABLE chado.featuremap_dbxref
  569. DROP CONSTRAINT featuremap_dbxref_dbxref_id_fkey CASCADE
  570. ');
  571. db_query('
  572. ALTER TABLE chado.featuremap_dbxref
  573. DROP CONSTRAINT featuremap_dbxref_featuremap_id_fkey CASCADE
  574. ');
  575. }
  576. db_query('
  577. ALTER TABLE chado.featuremap_dbxref
  578. ADD CONSTRAINT featuremap_dbxref_dbxref_id_fkey
  579. FOREIGN KEY (dbxref_id) REFERENCES chado.dbxref (dbxref_id)
  580. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  581. ');
  582. db_query('
  583. ALTER TABLE chado.featuremap_dbxref
  584. ADD CONSTRAINT featuremap_dbxref_featuremap_id_fkey
  585. FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
  586. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  587. ');
  588. // featureposprop
  589. if ($fkey_exists) {
  590. db_query('
  591. ALTER TABLE chado.featureposprop
  592. DROP CONSTRAINT featureposprop_type_id_fkey CASCADE
  593. ');
  594. db_query('
  595. ALTER TABLE chado.featureposprop
  596. DROP CONSTRAINT featureposprop_featurepos_id_fkey CASCADE
  597. ');
  598. }
  599. db_query('
  600. ALTER TABLE chado.featureposprop
  601. ADD CONSTRAINT featureposprop_type_id_fkey
  602. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  603. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  604. ');
  605. db_query('
  606. ALTER TABLE chado.featureposprop
  607. ADD CONSTRAINT featureposprop_featurepos_id_fkey
  608. FOREIGN KEY (featurepos_id) REFERENCES chado.featurepos (featurepos_id)
  609. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  610. ');
  611. }
  612. catch (\PDOException $e) {
  613. $error = $e->getMessage();
  614. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  615. }
  616. }
  617. /**
  618. * Fixes a typo when setting the default CV for the unittype_id field of the featuremap table.
  619. */
  620. function tripal_featuremap_update_7202() {
  621. try {
  622. tripal_set_default_cv('featuremap', 'unittype_id', 'featuremap_units');
  623. }
  624. catch (\PDOException $e) {
  625. $error = $e->getMessage();
  626. throw new DrupalUpdateException('Failed to update featurmap CV default: '. $error);
  627. }
  628. }