tripal_feature.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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' => FALSE,
  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. 'size' => 'big',
  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. 'size' => 'big',
  257. 'type' => 'int',
  258. 'not null' => TRUE,
  259. ),
  260. 'feature_type' => array(
  261. 'type' => 'varchar',
  262. 'length' => '255',
  263. 'not null' => TRUE,
  264. ),
  265. ),
  266. 'indexes' => array(
  267. 'organism_feature_count_idx1' => array('organism_id'),
  268. 'organism_feature_count_idx2' => array('cvterm_id'),
  269. 'organism_feature_count_idx3' => array('feature_type'),
  270. ),
  271. );
  272. $sql = "
  273. SELECT
  274. O.organism_id, O.genus, O.species, O.common_name,
  275. count(F.feature_id) as num_features,
  276. CVT.cvterm_id, CVT.name as feature_type
  277. FROM organism O
  278. INNER JOIN feature F ON O.Organism_id = F.organism_id
  279. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  280. GROUP BY
  281. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  282. ";
  283. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  284. }
  285. /**
  286. * Add cvs related to publications
  287. *
  288. * @ingroup tripal_pub
  289. */
  290. function tripal_feature_add_cvs() {
  291. // Add cv for relationship types
  292. tripal_insert_cv(
  293. 'feature_relationship',
  294. 'Contains types of relationships between features.'
  295. );
  296. // the feature_property CV already exists... it comes with chado, but we need to
  297. // add it just in case it doesn't get added before the feature module is installed
  298. tripal_insert_cv(
  299. 'feature_property',
  300. 'Stores properties about features'
  301. );
  302. // the feature type vocabulary should be the sequence ontology, and even though
  303. // this ontology should get loaded we will create it here just so that we can
  304. // set the default vocabulary for the feature.type_id field
  305. tripal_insert_cv(
  306. 'sequence',
  307. 'The Sequence Ontology'
  308. );
  309. }
  310. /**
  311. * This is the required update for tripal_feature when upgrading from Drupal core API 6.x.
  312. * This update may take some time to complete.
  313. */
  314. function tripal_feature_update_7200() {
  315. // Make sure we have the full API loaded this will help during a
  316. // site upgrade when the tripal_core module is disabled.
  317. module_load_include('module', 'tripal_core', 'tripal_core');
  318. tripal_core_import_api();
  319. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  320. // During the upgrade from D6 to D7 the vocabulary terms assigned to features were
  321. // copied to the field_data_taxonomyextra table rather than to the correct
  322. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  323. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Feature Type'")->fetchField();
  324. if ($vid) {
  325. try {
  326. if (db_table_exists('field_data_taxonomyextra')) {
  327. // first move from the field_data_taxonomyextra table
  328. $sql = "
  329. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  330. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  331. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  332. FROM field_data_taxonomyextra
  333. WHERE bundle = 'chado_feature')
  334. ";
  335. db_query($sql);
  336. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";
  337. db_query($sql);
  338. // next move from the field_revision_taxonomyextra table
  339. $sql = "
  340. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  341. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  342. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  343. FROM field_revision_taxonomyextra
  344. WHERE bundle = 'chado_feature')
  345. ";
  346. db_query($sql);
  347. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";
  348. db_query($sql);
  349. }
  350. }
  351. catch (\PDOException $e) {
  352. $error = $e->getMessage();
  353. throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
  354. }
  355. }
  356. // set the default feature property vocabulary
  357. try {
  358. tripal_insert_cv(
  359. 'feature_relationship',
  360. 'Contains types of relationships between features.'
  361. );
  362. // the feature_property CV already exists... it comes with chado, but we need to
  363. // add it just in case it doesn't get added before the feature module is installed
  364. tripal_insert_cv(
  365. 'feature_property',
  366. 'Stores properties about features'
  367. );
  368. // the feature type vocabulary should be the sequence ontology, and even though
  369. // this ontology should get loaded we will create it here just so that we can
  370. // set the default vocabulary for the feature.type_id field
  371. tripal_insert_cv(
  372. 'sequence',
  373. 'The Sequence Ontology'
  374. );
  375. tripal_set_default_cv('feature', 'type_id', 'sequence');
  376. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  377. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  378. }
  379. catch (\PDOException $e) {
  380. $error = $e->getMessage();
  381. throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);
  382. }
  383. }
  384. /**
  385. * Implementation of hook_update_dependencies().
  386. *
  387. * It specifies a list of other modules whose updates must be run prior to
  388. * this one. It also ensures the the Tripal API is in scope for site
  389. * upgrades when tripal_core is disabled.
  390. */
  391. function tripal_feature_update_dependencies() {
  392. $dependencies = array();
  393. // the tripal_cv update 7200 must run prior to update 7200 of this module
  394. $dependencies['tripal_feature'][7200] = array(
  395. 'tripal_cv' => 7200
  396. );
  397. return $dependencies;
  398. }
  399. /**
  400. * Fixes an error with the materialized view installation
  401. *
  402. */
  403. function tripal_feature_update_7201() {
  404. // Make sure we have the full API loaded this will help during a
  405. // site upgrade when the tripal_core module is disabled.
  406. module_load_include('module', 'tripal_core', 'tripal_core');
  407. tripal_core_import_api();
  408. try {
  409. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  410. // materialized view schema to the mviews table.
  411. // get the schema for the materialized view from the custom_tables table
  412. // as there is a copy there, but only if the schema is missing from the
  413. // materialized view table
  414. $view_name = 'organism_feature_count';
  415. $schema = db_select('tripal_mviews', 'tm')
  416. ->fields('tm', array('mv_schema'))
  417. ->condition('name', $view_name)
  418. ->execute()
  419. ->fetchField();
  420. if (!$schema or $schema == 'Array') {
  421. $schema = db_select('tripal_custom_tables', 'tct')
  422. ->fields('tct', array('schema'))
  423. ->condition('table_name', $view_name)
  424. ->execute()
  425. ->fetchField();
  426. $schema_str = var_export(unserialize($schema), TRUE);
  427. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  428. db_update('tripal_mviews')
  429. ->fields(array(
  430. 'mv_schema' => $schema_str
  431. ))
  432. ->condition('name', $view_name)
  433. ->execute();
  434. }
  435. }
  436. catch (\PDOException $e) {
  437. $error = $e->getMessage();
  438. throw new DrupalUpdateException('Failed to complete update' . $error);
  439. }
  440. }
  441. /**
  442. * Adds the temporary tables used for loading GFF files.
  443. */
  444. function tripal_feature_update_7202() {
  445. // Make sure we have the full API loaded this will help during a
  446. // site upgrade when the tripal_core module is disabled.
  447. module_load_include('module', 'tripal_core', 'tripal_core');
  448. tripal_core_import_api();
  449. try {
  450. tripal_feature_add_tripal_gff_temp_table();
  451. tripal_feature_add_tripal_gffcds_temp_table();
  452. tripal_feature_add_tripal_gffprotein_temp_table();
  453. }
  454. catch (\PDOException $e) {
  455. $error = $e->getMessage();
  456. throw new DrupalUpdateException('Failed to complete update' . $error);
  457. }
  458. }
  459. /**
  460. * Removes the unique constraint on the tripal_gffcds_temp table.
  461. */
  462. function tripal_feature_update_7203() {
  463. // Make sure we have the full API loaded this will help during a
  464. // site upgrade when the tripal_core module is disabled.
  465. module_load_include('module', 'tripal_core', 'tripal_core');
  466. tripal_core_import_api();
  467. try {
  468. tripal_feature_add_tripal_gffcds_temp_table(FALSE);
  469. }
  470. catch (\PDOException $e) {
  471. $error = $e->getMessage();
  472. throw new DrupalUpdateException('Failed to complete update' . $error);
  473. }
  474. }