tripal_featuremap.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_cv_add_cv(
  108. 'featuremap_units',
  109. 'Contains map unit types for the unittype_id column of the featuremap table.'
  110. );
  111. tripal_cv_add_cv(
  112. 'featurepos_property',
  113. 'Contains terms map properties.'
  114. );
  115. tripal_cv_add_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_cv_add_cvterm(
  128. array(
  129. 'name' => 'cM',
  130. 'def' => 'Centimorgan units'
  131. ),
  132. 'featuremap_units', 0, 1, 'tripal'
  133. );
  134. tripal_cv_add_cvterm(
  135. array(
  136. 'name' => 'bp',
  137. 'def' => 'Base pairs units'
  138. ),
  139. 'featuremap_units', 0, 1, 'tripal'
  140. );
  141. tripal_cv_add_cvterm(
  142. array(
  143. 'name' => 'bin_unit',
  144. 'def' => 'The bin unit'
  145. ),
  146. 'featuremap_units', 0, 1, 'tripal'
  147. );
  148. tripal_cv_add_cvterm(
  149. array(
  150. 'name' => 'marker_order',
  151. 'def' => 'Units simply to define marker order.'
  152. ),
  153. 'featuremap_units', 0, 1, 'tripal'
  154. );
  155. tripal_cv_add_cvterm(
  156. array(
  157. 'name' => 'undefined',
  158. 'def' => 'A catch-all for an undefined unit type'
  159. ),
  160. 'featuremap_units', 0, 1, 'tripal'
  161. );
  162. // featurepos properties
  163. tripal_cv_add_cvterm(
  164. array(
  165. 'name' => 'start',
  166. 'def' => 'The start coordinate for a map feature.'
  167. ),
  168. 'featurepos_property', 0, 1, 'tripal'
  169. );
  170. tripal_cv_add_cvterm(
  171. array(
  172. 'name' => 'stop',
  173. 'def' => 'The end coordinate for a map feature'
  174. ),
  175. 'featurepos_property', 0, 1, 'tripal'
  176. );
  177. // add cvterms for map properties
  178. tripal_cv_add_cvterm(
  179. array(
  180. 'name' => 'Map Dbxref',
  181. 'def' => 'A unique identifer for the map in a remote database. The format is a database abbreviation and a unique accession separated by a colon. (e.g. Gramene:tsh1996a)'
  182. ),
  183. 'featuremap_property', 0, 1, 'tripal'
  184. );
  185. tripal_cv_add_cvterm(
  186. array(
  187. 'name' => 'Map Type',
  188. 'def' => 'The type of Map (e.g. QTL, Physical, etc.)'
  189. ),
  190. 'featuremap_property', 0, 1, 'tripal'
  191. );
  192. tripal_cv_add_cvterm(
  193. array(
  194. 'name' => 'Genome Group',
  195. 'def' => ''
  196. ),
  197. 'featuremap_property', 0, 1, 'tripal'
  198. );
  199. tripal_cv_add_cvterm(
  200. array(
  201. 'name' => 'URL',
  202. 'def' => 'A univeral resource locator (URL) reference where the publication can be found. For maps found online, this would be the web address for the map.'
  203. ),
  204. 'featuremap_property', 0, 1, 'tripal'
  205. );
  206. tripal_cv_add_cvterm(
  207. array(
  208. 'name' => 'Population Type',
  209. 'def' => 'A brief description of the population type used to generate the map (e.g. RIL, F2, BC1, etc).'
  210. ),
  211. 'featuremap_property', 0, 1, 'tripal'
  212. );
  213. tripal_cv_add_cvterm(
  214. array(
  215. 'name' => 'Population Size',
  216. 'def' => 'The size of the population used to construct the map.'
  217. ),
  218. 'featuremap_property', 0, 1, 'tripal'
  219. );
  220. tripal_cv_add_cvterm(
  221. array(
  222. 'name' => 'Methods',
  223. 'def' => 'A brief description of the methods used to construct the map.'
  224. ),
  225. 'featuremap_property', 0, 1, 'tripal'
  226. );
  227. tripal_cv_add_cvterm(
  228. array(
  229. 'name' => 'Software',
  230. 'def' => 'The software used to construct the map.'
  231. ),
  232. 'featuremap_property', 0, 1, 'tripal'
  233. );
  234. }
  235. /**
  236. * Add custom tables needed by the feature map module
  237. * - featuremapprop
  238. * - featuremap_dbxref
  239. * - featureposprop
  240. *
  241. * @ingroup tripal_featuremap
  242. */
  243. function tripal_featuremap_add_custom_tables(){
  244. // add the featuremaprop table to Chado
  245. $schema = array (
  246. 'table' => 'featuremapprop',
  247. 'fields' => array (
  248. 'featuremapprop_id' => array (
  249. 'type' => 'serial',
  250. 'not null' => true,
  251. ),
  252. 'featuremap_id' => array (
  253. 'type' => 'int',
  254. 'not null' => true,
  255. ),
  256. 'type_id' => array (
  257. 'type' => 'int',
  258. 'not null' => true,
  259. ),
  260. 'value' => array (
  261. 'type' => 'text',
  262. 'not null' => false,
  263. ),
  264. 'rank' => array (
  265. 'type' => 'int',
  266. 'not null' => true,
  267. 'default' => 0,
  268. ),
  269. ),
  270. 'primary key' => array (
  271. 0 => 'featuremapprop_id',
  272. ),
  273. 'unique keys' => array (
  274. 'featuremapprop_c1' => array (
  275. 0 => 'featuremap_id',
  276. 1 => 'type_id',
  277. 2 => 'rank',
  278. ),
  279. ),
  280. 'indexes' => array (
  281. 'featuremapprop_idx1' => array (
  282. 0 => 'featuremap_id',
  283. ),
  284. 'featuremapprop_idx2' => array (
  285. 0 => 'type_id',
  286. ),
  287. ),
  288. 'foreign keys' => array (
  289. 'cvterm' => array (
  290. 'table' => 'cvterm',
  291. 'columns' => array (
  292. 'type_id' => 'cvterm_id',
  293. ),
  294. ),
  295. 'featuremap' => array (
  296. 'table' => 'featuremap',
  297. 'columns' => array (
  298. 'featuremap_id' => 'featuremap_id',
  299. ),
  300. ),
  301. ),
  302. );
  303. chado_create_custom_table('featuremapprop', $schema, TRUE);
  304. // add the featuremap_dbxref table to Chado
  305. $schema = array (
  306. 'table' => 'featuremap_dbxref',
  307. 'fields' => array (
  308. 'featuremap_dbxref_id' => array (
  309. 'type' => 'serial',
  310. 'not null' => true,
  311. ),
  312. 'featuremap_id' => array (
  313. 'type' => 'int',
  314. 'not null' => true,
  315. ),
  316. 'dbxref_id' => array (
  317. 'type' => 'int',
  318. 'not null' => true,
  319. ),
  320. ),
  321. 'primary key' => array (
  322. 0 => 'featuremap_dbxref_id',
  323. ),
  324. 'unique keys' => array (
  325. 'featuremap_dbxref_c1' => array (
  326. 0 => 'featuremap_id',
  327. 1 => 'dbxref_id',
  328. ),
  329. ),
  330. 'indexes' => array (
  331. 'featuremap_dbxref_idx1' => array (
  332. 0 => 'featuremap_dbxref_id',
  333. ),
  334. 'featuremap_dbxref_idx2' => array (
  335. 0 => 'dbxref_id',
  336. ),
  337. ),
  338. 'foreign keys' => array (
  339. 'dbxref' => array (
  340. 'table' => 'dbxref',
  341. 'columns' => array (
  342. 'dbxref_id' => 'dbxref_id',
  343. ),
  344. ),
  345. 'featuremap' => array (
  346. 'table' => 'featuremap',
  347. 'columns' => array (
  348. 'featuremap_id' => 'featuremap_id',
  349. ),
  350. ),
  351. ),
  352. 'referring_tables' => NULL,
  353. );
  354. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  355. $schema = array (
  356. 'table' => 'featureposprop',
  357. 'fields' => array (
  358. 'featureposprop_id' => array (
  359. 'type' => 'serial',
  360. 'not null' => true,
  361. ),
  362. 'featurepos_id' => array (
  363. 'type' => 'int',
  364. 'not null' => true,
  365. ),
  366. 'type_id' => array (
  367. 'type' => 'int',
  368. 'not null' => true,
  369. ),
  370. 'value' => array (
  371. 'type' => 'text',
  372. 'not null' => false,
  373. ),
  374. 'rank' => array (
  375. 'type' => 'int',
  376. 'not null' => true,
  377. 'default' => 0,
  378. ),
  379. ),
  380. 'primary key' => array (
  381. 0 => 'featureposprop_id',
  382. ),
  383. 'unique keys' => array (
  384. 'featureposprop_id' => array (
  385. 0 => 'featurepos_id',
  386. 1 => 'type_id',
  387. 2 => 'rank',
  388. ),
  389. ),
  390. 'indexes' => array (
  391. 'featureposprop_c1' => array (
  392. 0 => 'featurepos_id',
  393. ),
  394. 'featureposprop_idx2' => array (
  395. 0 => 'type_id',
  396. ),
  397. ),
  398. 'foreign keys' => array (
  399. 'cvterm' => array (
  400. 'table' => 'cvterm',
  401. 'columns' => array (
  402. 'type_id' => 'cvterm_id',
  403. ),
  404. ),
  405. 'featurepos' => array (
  406. 'table' => 'featurepos',
  407. 'columns' => array (
  408. 'featurepos_id' => 'featurepos_id',
  409. ),
  410. ),
  411. ),
  412. );
  413. chado_create_custom_table('featureposprop', $schema, TRUE);
  414. }
  415. /**
  416. * This is the required update for tripal_featuremap when upgrading from Drupal core API 6.x.
  417. * This update may take some time to complete.
  418. */
  419. function tripal_featuremap_update_7200() {
  420. // We can't use the Tripal API during an upgrade from D6 to D7 Tripal because the tripal_core
  421. // module is disabled. So, we have to manually make database additions/changes to chado.
  422. // set the default vocabularies
  423. // featuremap_units
  424. try {
  425. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_units'")->fetchField();
  426. db_insert('tripal_cv_defaults')
  427. ->fields(array(
  428. 'table_name' => 'featuremap',
  429. 'field_name' => 'unittype_id',
  430. 'cv_id' => $cv_id
  431. ))
  432. ->execute();
  433. }
  434. catch (\PDOException $e) {
  435. $error = $e->getMessage();
  436. throw new DrupalUpdateException('Failed to set featuremap_units vocabulary as default: '. $error);
  437. }
  438. // featurepos_property
  439. try {
  440. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featurepos_property'")->fetchField();
  441. db_insert('tripal_cv_defaults')
  442. ->fields(array(
  443. 'table_name' => 'featureposprop',
  444. 'field_name' => 'type_id',
  445. 'cv_id' => $cv_id
  446. ))
  447. ->execute();
  448. }
  449. catch (\PDOException $e) {
  450. $error = $e->getMessage();
  451. throw new DrupalUpdateException('Failed to set featurepos_property vocabulary as default: '. $error);
  452. }
  453. // featuremap_property
  454. try {
  455. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_property'")->fetchField();
  456. db_insert('tripal_cv_defaults')
  457. ->fields(array(
  458. 'table_name' => 'featuremapprop',
  459. 'field_name' => 'type_id',
  460. 'cv_id' => $cv_id
  461. ))
  462. ->execute();
  463. }
  464. catch (\PDOException $e) {
  465. $error = $e->getMessage();
  466. throw new DrupalUpdateException('Failed to set featuremap_property vocabulary as default: '. $error);
  467. }
  468. }
  469. /**
  470. * Implementation of hook_update_dependencies(). It specifies a list of
  471. * other modules whose updates must be run prior to this one.
  472. */
  473. function tripal_featuremap_update_dependencies() {
  474. $dependencies = array();
  475. // the tripal_cv update 7200 must run prior to update 7200 of this module
  476. $dependencies['tripal_featuremap'][7200] = array(
  477. 'tripal_cv' => 7200
  478. );
  479. return $dependencies;
  480. }
  481. /**
  482. * Adds missing foreign key constraints
  483. *
  484. */
  485. function tripal_featuremap_update_7201() {
  486. // there was a bug in the function for creating a custom table that
  487. // kept foreign key constraints from being added. So, we need to add those
  488. // to keep from error messages appear, we will drop the FK if it already
  489. // exists and then re-add it.
  490. try {
  491. // featuremapprop table
  492. db_query('
  493. ALTER TABLE chado.featuremapprop
  494. DROP CONSTRAINT IF EXISTS featuremapprop_type_id_fkey CASCADE
  495. ');
  496. db_query('
  497. ALTER TABLE chado.featuremapprop
  498. DROP CONSTRAINT IF EXISTS featuremapprop_featuremap_id_fkey CASCADE
  499. ');
  500. db_query('
  501. ALTER TABLE chado.featuremapprop
  502. ADD CONSTRAINT featuremapprop_type_id_fkey
  503. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  504. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  505. ');
  506. db_query('
  507. ALTER TABLE chado.featuremapprop
  508. ADD CONSTRAINT featuremapprop_featuremap_id_fkey
  509. FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
  510. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  511. ');
  512. // featuremap_dbref table
  513. db_query('
  514. ALTER TABLE chado.featuremap_dbxref
  515. DROP CONSTRAINT IF EXISTS featuremap_dbxref_dbxref_id_fkey CASCADE
  516. ');
  517. db_query('
  518. ALTER TABLE chado.featuremap_dbxref
  519. DROP CONSTRAINT IF EXISTS featuremap_dbxref_featuremap_id_fkey CASCADE
  520. ');
  521. db_query('
  522. ALTER TABLE chado.featuremap_dbxref
  523. ADD CONSTRAINT featuremap_dbxref_dbxref_id_fkey
  524. FOREIGN KEY (dbxref_id) REFERENCES chado.dbxref (dbxref_id)
  525. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  526. ');
  527. db_query('
  528. ALTER TABLE chado.featuremap_dbxref
  529. ADD CONSTRAINT featuremap_dbxref_featuremap_id_fkey
  530. FOREIGN KEY (featuremap_id) REFERENCES chado.featuremap (featuremap_id)
  531. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  532. ');
  533. // featureposprop
  534. db_query('
  535. ALTER TABLE chado.featureposprop
  536. DROP CONSTRAINT IF EXISTS featureposprop_type_id_fkey CASCADE
  537. ');
  538. db_query('
  539. ALTER TABLE chado.featureposprop
  540. DROP CONSTRAINT IF EXISTS featureposprop_featurepos_id_fkey CASCADE
  541. ');
  542. db_query('
  543. ALTER TABLE chado.featureposprop
  544. ADD CONSTRAINT featureposprop_type_id_fkey
  545. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  546. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  547. ');
  548. db_query('
  549. ALTER TABLE chado.featureposprop
  550. ADD CONSTRAINT featureposprop_featurepos_id_fkey
  551. FOREIGN KEY (featurepos_id) REFERENCES chado.featurepos (featurepos_id)
  552. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  553. ');
  554. }
  555. catch (\PDOException $e) {
  556. $error = $e->getMessage();
  557. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  558. }
  559. }