tripal_feature.install 14 KB

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