tripal_feature.install 14 KB

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