tripal_feature.install 14 KB

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