tripal_feature.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. // first move from the field_data_taxonomyextra table
  320. $sql = "
  321. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  322. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  323. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  324. FROM field_data_taxonomyextra
  325. WHERE bundle = 'chado_feature')
  326. ";
  327. db_query($sql);
  328. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";
  329. db_query($sql);
  330. // next move from the field_revision_taxonomyextra table
  331. $sql = "
  332. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  333. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  334. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  335. FROM field_revision_taxonomyextra
  336. WHERE bundle = 'chado_feature')
  337. ";
  338. db_query($sql);
  339. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";
  340. db_query($sql);
  341. }
  342. catch (\PDOException $e) {
  343. $error = $e->getMessage();
  344. throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
  345. }
  346. }
  347. // set the default feature property vocabulary
  348. try {
  349. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_property'")->fetchField();
  350. db_insert('tripal_cv_defaults')
  351. ->fields(array(
  352. 'table_name' => 'featureprop',
  353. 'field_name' => 'type_id',
  354. 'cv_id' => $cv_id
  355. ))
  356. ->execute();
  357. }
  358. catch (\PDOException $e) {
  359. $error = $e->getMessage();
  360. throw new DrupalUpdateException('Failed to set feature_property vocabulary as default: '. $error);
  361. }
  362. // add the feature_relationshp CV
  363. try {
  364. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_relationship'")->fetchField();
  365. if (!$cv_id) {
  366. // add the vocabulary
  367. $cv_id = db_insert('chado.cv')
  368. ->fields(array(
  369. 'name' => 'feature_relationship',
  370. 'definition' => 'Contains types of relationships between features.'
  371. ))
  372. ->execute();
  373. }
  374. // use the new feature_relationship CV we just added
  375. db_insert('tripal_cv_defaults')
  376. ->fields(array(
  377. 'table_name' => 'feature_relationship',
  378. 'field_name' => 'type_id',
  379. 'cv_id' => $cv_id
  380. ))
  381. ->execute();
  382. }
  383. catch (\PDOException $e) {
  384. $error = $e->getMessage();
  385. throw new DrupalUpdateException('Failed to add feature_relationship vocabulary: '. $error);
  386. }
  387. // set the feature_type as the 'sequence' ontology
  388. try {
  389. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'sequence'")->fetchField();
  390. if (!$cv_id) {
  391. // add the vocabulary
  392. $cv_id = db_insert('chado.cv')
  393. ->fields(array(
  394. 'name' => 'sequence',
  395. 'definition' => 'The Sequence Ontology.'
  396. ))
  397. ->execute();
  398. }
  399. db_insert('tripal_cv_defaults')
  400. ->fields(array(
  401. 'table_name' => 'feature',
  402. 'field_name' => 'type_id',
  403. 'cv_id' => $cv_id
  404. ))
  405. ->execute();
  406. }
  407. catch (\PDOException $e) {
  408. $error = $e->getMessage();
  409. throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);
  410. }
  411. }
  412. /**
  413. * Implementation of hook_update_dependencies(). It specifies a list of
  414. * other modules whose updates must be run prior to this one.
  415. */
  416. function tripal_feature_update_dependencies() {
  417. $dependencies = array();
  418. // the tripal_cv update 7200 must run prior to update 7200 of this module
  419. $dependencies['tripal_feature'][7200] = array(
  420. 'tripal_cv' => 7200
  421. );
  422. return $dependencies;
  423. }
  424. /**
  425. * Fixes an error with the materialized view installation
  426. *
  427. */
  428. function tripal_feature_update_7201() {
  429. try {
  430. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  431. // materialized view schema to the mviews table.
  432. // get the schema for the materialized view from the custom_tables table
  433. // as there is a copy there, but only if the schema is missing from the
  434. // materialized view table
  435. $view_name = 'organism_feature_count';
  436. $schema = db_select('tripal_mviews', 'tm')
  437. ->fields('tm', array('mv_schema'))
  438. ->condition('name', $view_name)
  439. ->execute()
  440. ->fetchField();
  441. if (!$schema or $schema == 'Array') {
  442. $schema = db_select('tripal_custom_tables', 'tct')
  443. ->fields('tct', array('schema'))
  444. ->condition('table_name', $view_name)
  445. ->execute()
  446. ->fetchField();
  447. $schema_str = var_export(unserialize($schema), TRUE);
  448. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  449. db_update('tripal_mviews')
  450. ->fields(array(
  451. 'mv_schema' => $schema_str
  452. ))
  453. ->condition('name', $view_name)
  454. ->execute();
  455. }
  456. }
  457. catch (\PDOException $e) {
  458. $error = $e->getMessage();
  459. throw new DrupalUpdateException('Failed to complete update' . $error);
  460. }
  461. }
  462. /**
  463. * Adds the temporary tables used for loading GFF files.
  464. */
  465. function tripal_feature_update_7202() {
  466. try {
  467. tripal_feature_add_tripal_gff_temp_table();
  468. tripal_feature_add_tripal_gffcds_temp_table();
  469. tripal_feature_add_tripal_gffprotein_temp_table();
  470. }
  471. catch (\PDOException $e) {
  472. $error = $e->getMessage();
  473. throw new DrupalUpdateException('Failed to complete update' . $error);
  474. }
  475. }
  476. /**
  477. * Removes the unique constraint on the tripal_gffcds_temp table.
  478. */
  479. function tripal_feature_update_7203() {
  480. try {
  481. tripal_feature_add_tripal_gffcds_temp_table(FALSE);
  482. }
  483. catch (\PDOException $e) {
  484. $error = $e->getMessage();
  485. throw new DrupalUpdateException('Failed to complete update' . $error);
  486. }
  487. }