tripal_feature.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the feature module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. *
  9. * Disable default views when module is disabled
  10. *
  11. * @ingroup tripal_feature
  12. */
  13. function tripal_feature_disable() {
  14. // Disable all default views provided by this module
  15. require_once("tripal_feature.views_default.inc");
  16. $views = tripal_feature_views_default_views();
  17. foreach (array_keys($views) as $view_name) {
  18. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  19. }
  20. }
  21. /**
  22. * Implements hook_requirements().
  23. *
  24. * @ingroup tripal_feature
  25. */
  26. function tripal_feature_requirements($phase) {
  27. $requirements = array();
  28. if ($phase == 'install') {
  29. // make sure chado is installed
  30. if (!$GLOBALS["chado_is_installed"]) {
  31. $requirements ['tripal_feature'] = array(
  32. 'title' => "t ripal_feature",
  33. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  34. 'severity' => REQUIREMENT_ERROR,
  35. );
  36. }
  37. }
  38. return $requirements;
  39. }
  40. /**
  41. * Implements hook_install().
  42. *
  43. * @ingroup tripal_feature
  44. */
  45. function tripal_feature_install() {
  46. // Add the feature properties vocabulary provided by Chado.
  47. $obo_path = '{tripal_feature}/files/feature_property.obo';
  48. $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  49. tripal_submit_obo_job(array('obo_id' => $obo_id));
  50. // Add the materialized view.
  51. tripal_feature_add_organism_count_mview();
  52. // Add the custom tables.
  53. tripal_feature_add_tripal_gff_temp_table();
  54. tripal_feature_add_tripal_gffcds_temp_table();
  55. tripal_feature_add_tripal_gffprotein_temp_table();
  56. // Add the vocabularies used by the feature module.
  57. tripal_feature_add_cvs();
  58. // Set the default vocabularies.
  59. tripal_set_default_cv('feature', 'type_id', 'sequence');
  60. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  61. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  62. }
  63. /**
  64. * Implements hook_uninstall().
  65. *
  66. * @ingroup tripal_feature
  67. */
  68. function tripal_feature_uninstall() {
  69. }
  70. function tripal_feature_add_tripal_gff_temp_table() {
  71. $schema = array(
  72. 'table' => 'tripal_gff_temp',
  73. 'fields' => array(
  74. 'feature_id' => array(
  75. 'type' => 'int',
  76. 'not null' => TRUE,
  77. ),
  78. 'organism_id' => array(
  79. 'type' => 'int',
  80. 'not null' => TRUE,
  81. ),
  82. 'uniquename' => array(
  83. 'type' => 'text',
  84. 'not null' => TRUE,
  85. ),
  86. 'type_name' => array(
  87. 'type' => 'varchar',
  88. 'length' => '1024',
  89. 'not null' => TRUE,
  90. ),
  91. ),
  92. 'indexes' => array(
  93. 'tripal_gff_temp_idx0' => array('feature_id'),
  94. 'tripal_gff_temp_idx0' => array('organism_id'),
  95. 'tripal_gff_temp_idx1' => array('uniquename'),
  96. ),
  97. 'unique keys' => array(
  98. 'tripal_gff_temp_uq0' => array('feature_id'),
  99. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  100. ),
  101. );
  102. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  103. }
  104. /**
  105. *
  106. */
  107. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  108. $schema = array(
  109. 'table' => 'tripal_gffcds_temp',
  110. 'fields' => array(
  111. 'feature_id' => array(
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. ),
  115. 'parent_id' => array(
  116. 'type' => 'int',
  117. 'not null' => TRUE,
  118. ),
  119. 'phase' => array(
  120. 'type' => 'int',
  121. 'not null' => TRUE,
  122. ),
  123. 'strand' => array(
  124. 'type' => 'int',
  125. 'not null' => TRUE,
  126. ),
  127. 'fmin' => array(
  128. 'type' => 'int',
  129. 'not null' => TRUE,
  130. ),
  131. 'fmax' => array(
  132. 'type' => 'int',
  133. 'not null' => TRUE,
  134. ),
  135. ),
  136. 'indexes' => array(
  137. 'tripal_gff_temp_idx0' => array('feature_id'),
  138. 'tripal_gff_temp_idx0' => array('parent_id'),
  139. ),
  140. );
  141. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  142. }
  143. /**
  144. *
  145. */
  146. function tripal_feature_add_tripal_gffprotein_temp_table() {
  147. $schema = array(
  148. 'table' => 'tripal_gffprotein_temp',
  149. 'fields' => array(
  150. 'feature_id' => array(
  151. 'type' => 'int',
  152. 'not null' => TRUE,
  153. ),
  154. 'parent_id' => array(
  155. 'type' => 'int',
  156. 'not null' => TRUE,
  157. ),
  158. 'fmin' => array(
  159. 'type' => 'int',
  160. 'not null' => TRUE,
  161. ),
  162. 'fmax' => array(
  163. 'type' => 'int',
  164. 'not null' => TRUE,
  165. ),
  166. ),
  167. 'indexes' => array(
  168. 'tripal_gff_temp_idx0' => array('feature_id'),
  169. 'tripal_gff_temp_idx0' => array('parent_id'),
  170. ),
  171. 'unique keys' => array(
  172. 'tripal_gff_temp_uq0' => array('feature_id'),
  173. ),
  174. );
  175. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  176. }
  177. /**
  178. * Implementation of hook_schema().
  179. *
  180. * @ingroup tripal_feature
  181. */
  182. function tripal_feature_schema() {
  183. $schema['chado_feature'] = array(
  184. 'fields' => array(
  185. 'vid' => array(
  186. 'type' => 'int',
  187. 'unsigned' => TRUE,
  188. 'not null' => TRUE,
  189. 'default' => 0
  190. ),
  191. 'nid' => array(
  192. 'type' => 'int',
  193. 'unsigned' => TRUE,
  194. 'not null' => TRUE,
  195. 'default' => 0
  196. ),
  197. 'feature_id' => array(
  198. 'type' => 'int',
  199. 'not null' => TRUE,
  200. 'default' => 0
  201. ),
  202. 'sync_date' => array(
  203. 'type' => 'int',
  204. 'not null' => FALSE,
  205. 'description' => 'UNIX integer sync date/time'
  206. ),
  207. ),
  208. 'indexes' => array(
  209. 'chado_feature_idx1' => array('feature_id')
  210. ),
  211. 'unique keys' => array(
  212. 'chado_feature_uq1' => array('nid', 'vid'),
  213. 'chado_feature_uq2' => array('vid')
  214. ),
  215. 'primary key' => array('nid'),
  216. );
  217. return $schema;
  218. };
  219. /**
  220. * Creates a materialized view that stores the type & number of features per organism
  221. *
  222. * @ingroup tripal_feature
  223. */
  224. function tripal_feature_add_organism_count_mview() {
  225. $view_name = 'organism_feature_count';
  226. $comment = 'Stores the type and number of features per organism';
  227. $schema = array(
  228. 'description' => $comment,
  229. 'table' => $view_name,
  230. 'fields' => array(
  231. 'organism_id' => array(
  232. 'type' => 'int',
  233. 'not null' => TRUE,
  234. ),
  235. 'genus' => array(
  236. 'type' => 'varchar',
  237. 'length' => '255',
  238. 'not null' => TRUE,
  239. ),
  240. 'species' => array(
  241. 'type' => 'varchar',
  242. 'length' => '255',
  243. 'not null' => TRUE,
  244. ),
  245. 'common_name' => array(
  246. 'type' => 'varchar',
  247. 'length' => '255',
  248. 'not null' => FALSE,
  249. ),
  250. 'num_features' => array(
  251. 'type' => 'int',
  252. 'not null' => TRUE,
  253. ),
  254. 'cvterm_id' => array(
  255. 'type' => 'int',
  256. 'not null' => TRUE,
  257. ),
  258. 'feature_type' => array(
  259. 'type' => 'varchar',
  260. 'length' => '255',
  261. 'not null' => TRUE,
  262. ),
  263. ),
  264. 'indexes' => array(
  265. 'organism_feature_count_idx1' => array('organism_id'),
  266. 'organism_feature_count_idx2' => array('cvterm_id'),
  267. 'organism_feature_count_idx3' => array('feature_type'),
  268. ),
  269. );
  270. $sql = "
  271. SELECT
  272. O.organism_id, O.genus, O.species, O.common_name,
  273. count(F.feature_id) as num_features,
  274. CVT.cvterm_id, CVT.name as feature_type
  275. FROM organism O
  276. INNER JOIN feature F ON O.Organism_id = F.organism_id
  277. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  278. GROUP BY
  279. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  280. ";
  281. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  282. }
  283. /**
  284. * Add cvs related to publications
  285. *
  286. * @ingroup tripal_pub
  287. */
  288. function tripal_feature_add_cvs() {
  289. // Add cv for relationship types
  290. tripal_insert_cv(
  291. 'feature_relationship',
  292. 'Contains types of relationships between features.'
  293. );
  294. // the feature_property CV already exists... it comes with chado, but we need to
  295. // add it just in case it doesn't get added before the feature module is installed
  296. tripal_insert_cv(
  297. 'feature_property',
  298. 'Stores properties about features'
  299. );
  300. // the feature type vocabulary should be the sequence ontology, and even though
  301. // this ontology should get loaded we will create it here just so that we can
  302. // set the default vocabulary for the feature.type_id field
  303. tripal_insert_cv(
  304. 'sequence',
  305. 'The Sequence Ontology'
  306. );
  307. }
  308. /**
  309. * This is the required update for tripal_feature when upgrading from Drupal core API 6.x.
  310. * This update may take some time to complete.
  311. */
  312. function tripal_feature_update_7200() {
  313. // During the upgrade from D6 to D7 the vocabulary terms assigned to features were
  314. // copied to the field_data_taxonomyextra table rather than to the correct
  315. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  316. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Feature Type'")->fetchField();
  317. if ($vid) {
  318. try {
  319. if (db_table_exists('field_data_taxonomyextra')) {
  320. // first move from the field_data_taxonomyextra table
  321. $sql = "
  322. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  323. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  324. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  325. FROM field_data_taxonomyextra
  326. WHERE bundle = 'chado_feature')
  327. ";
  328. db_query($sql);
  329. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";
  330. db_query($sql);
  331. // next move from the field_revision_taxonomyextra table
  332. $sql = "
  333. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  334. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  335. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  336. FROM field_revision_taxonomyextra
  337. WHERE bundle = 'chado_feature')
  338. ";
  339. db_query($sql);
  340. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";
  341. db_query($sql);
  342. }
  343. }
  344. catch (\PDOException $e) {
  345. $error = $e->getMessage();
  346. throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
  347. }
  348. }
  349. // set the default feature property vocabulary
  350. try {
  351. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_property'")->fetchField();
  352. $cdi = db_select('tripal_cv_defaults', 't')
  353. ->fields('t', array('cv_default_id'))
  354. ->condition('table_name', 'featureprop')
  355. ->condition('field_name', 'type_id')
  356. ->execute()
  357. ->fetchField();
  358. if (!$cdi) {
  359. db_insert('tripal_cv_defaults')
  360. ->fields(array(
  361. 'table_name' => 'featureprop',
  362. 'field_name' => 'type_id',
  363. 'cv_id' => $cv_id
  364. ))
  365. ->execute();
  366. }
  367. }
  368. catch (\PDOException $e) {
  369. $error = $e->getMessage();
  370. throw new DrupalUpdateException('Failed to set feature_property vocabulary as default: '. $error);
  371. }
  372. // add the feature_relationshp CV
  373. try {
  374. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_relationship'")->fetchField();
  375. if (!$cv_id) {
  376. // add the vocabulary
  377. $cv_id = db_insert('chado.cv')
  378. ->fields(array(
  379. 'name' => 'feature_relationship',
  380. 'definition' => 'Contains types of relationships between features.'
  381. ))
  382. ->execute();
  383. }
  384. // use the new featureprop CV we just added
  385. $cdi = db_select('tripal_cv_defaults', 't')
  386. ->fields('t', array('cv_default_id'))
  387. ->condition('table_name', 'feature_relationship')
  388. ->condition('field_name', 'type_id')
  389. ->execute()
  390. ->fetchField();
  391. if (!$cdi) {
  392. db_insert('tripal_cv_defaults')
  393. ->fields(array(
  394. 'table_name' => 'feature_relationship',
  395. 'field_name' => 'type_id',
  396. 'cv_id' => $cv_id
  397. ))
  398. ->execute();
  399. }
  400. }
  401. catch (\PDOException $e) {
  402. $error = $e->getMessage();
  403. throw new DrupalUpdateException('Failed to add feature_relationship vocabulary: '. $error);
  404. }
  405. // set the feature_type as the 'sequence' ontology
  406. try {
  407. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'sequence'")->fetchField();
  408. if (!$cv_id) {
  409. // add the vocabulary
  410. $cv_id = db_insert('chado.cv')
  411. ->fields(array(
  412. 'name' => 'sequence',
  413. 'definition' => 'The Sequence Ontology.'
  414. ))
  415. ->execute();
  416. }
  417. $cdi = db_select('tripal_cv_defaults', 't')
  418. ->fields('t', array('cv_default_id'))
  419. ->condition('table_name', 'feature')
  420. ->condition('field_name', 'type_id')
  421. ->execute()
  422. ->fetchField();
  423. if (!$cdi) {
  424. db_insert('tripal_cv_defaults')
  425. ->fields(array(
  426. 'table_name' => 'feature',
  427. 'field_name' => 'type_id',
  428. 'cv_id' => $cv_id
  429. ))
  430. ->execute();
  431. }
  432. }
  433. catch (\PDOException $e) {
  434. $error = $e->getMessage();
  435. throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);
  436. }
  437. }
  438. /**
  439. * Implementation of hook_update_dependencies(). It specifies a list of
  440. * other modules whose updates must be run prior to this one.
  441. */
  442. function tripal_feature_update_dependencies() {
  443. $dependencies = array();
  444. // the tripal_cv update 7200 must run prior to update 7200 of this module
  445. $dependencies['tripal_feature'][7200] = array(
  446. 'tripal_cv' => 7200
  447. );
  448. return $dependencies;
  449. }
  450. /**
  451. * Fixes an error with the materialized view installation
  452. *
  453. */
  454. function tripal_feature_update_7201() {
  455. try {
  456. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  457. // materialized view schema to the mviews table.
  458. // get the schema for the materialized view from the custom_tables table
  459. // as there is a copy there, but only if the schema is missing from the
  460. // materialized view table
  461. $view_name = 'organism_feature_count';
  462. $schema = db_select('tripal_mviews', 'tm')
  463. ->fields('tm', array('mv_schema'))
  464. ->condition('name', $view_name)
  465. ->execute()
  466. ->fetchField();
  467. if (!$schema or $schema == 'Array') {
  468. $schema = db_select('tripal_custom_tables', 'tct')
  469. ->fields('tct', array('schema'))
  470. ->condition('table_name', $view_name)
  471. ->execute()
  472. ->fetchField();
  473. $schema_str = var_export(unserialize($schema), TRUE);
  474. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  475. db_update('tripal_mviews')
  476. ->fields(array(
  477. 'mv_schema' => $schema_str
  478. ))
  479. ->condition('name', $view_name)
  480. ->execute();
  481. }
  482. }
  483. catch (\PDOException $e) {
  484. $error = $e->getMessage();
  485. throw new DrupalUpdateException('Failed to complete update' . $error);
  486. }
  487. }
  488. /**
  489. * Adds the temporary tables used for loading GFF files.
  490. */
  491. function tripal_feature_update_7202() {
  492. try {
  493. tripal_feature_add_tripal_gff_temp_table();
  494. tripal_feature_add_tripal_gffcds_temp_table();
  495. tripal_feature_add_tripal_gffprotein_temp_table();
  496. }
  497. catch (\PDOException $e) {
  498. $error = $e->getMessage();
  499. throw new DrupalUpdateException('Failed to complete update' . $error);
  500. }
  501. }
  502. /**
  503. * Removes the unique constraint on the tripal_gffcds_temp table.
  504. */
  505. function tripal_feature_update_7203() {
  506. try {
  507. tripal_feature_add_tripal_gffcds_temp_table(FALSE);
  508. }
  509. catch (\PDOException $e) {
  510. $error = $e->getMessage();
  511. throw new DrupalUpdateException('Failed to complete update' . $error);
  512. }
  513. }