tripal_feature.install 15 KB

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