tripal_feature.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. // create the temp table we will use for loading GFF files
  51. tripal_cv_create_tripal_gff_temp();
  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. /**
  67. * Create a temporary table used for loading gff3 files
  68. *
  69. * @ingroup tripal_feature
  70. */
  71. function tripal_cv_create_tripal_gff_temp() {
  72. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  73. // we create it here using plain SQL because we want it to be in the chado schema but we
  74. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  75. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  76. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  77. if (!db_table_exists('chado.tripal_gff_temp')) {
  78. $sql = "
  79. CREATE TABLE {tripal_gff_temp} (
  80. feature_id integer NOT NULL,
  81. organism_id integer NOT NULL,
  82. uniquename text NOT NULL,
  83. type_name character varying(1024) NOT NULL,
  84. CONSTRAINT tripal_gff_temp_uq0 UNIQUE (feature_id),
  85. CONSTRAINT tripal_gff_temp_uq1 UNIQUE (uniquename, organism_id, type_name)
  86. );
  87. ";
  88. chado_query($sql);
  89. $sql = "CREATE INDEX tripal_gff_temp_idx0 ON {tripal_gff_temp} USING btree (feature_id)";
  90. chado_query($sql);
  91. $sql = "CREATE INDEX tripal_gff_temp_idx1 ON {tripal_gff_temp} USING btree (organism_id)";
  92. chado_query($sql);
  93. $sql = "CREATE INDEX tripal_gff_temp_idx2 ON {tripal_gff_temp} USING btree (uniquename)";
  94. chado_query($sql);
  95. }
  96. }
  97. /**
  98. * Implementation of hook_schema().
  99. *
  100. * @ingroup tripal_feature
  101. */
  102. function tripal_feature_schema() {
  103. $schema['chado_feature'] = array(
  104. 'fields' => array(
  105. 'vid' => array(
  106. 'type' => 'int',
  107. 'unsigned' => TRUE,
  108. 'not null' => TRUE,
  109. 'default' => 0
  110. ),
  111. 'nid' => array(
  112. 'type' => 'int',
  113. 'unsigned' => TRUE,
  114. 'not null' => TRUE,
  115. 'default' => 0
  116. ),
  117. 'feature_id' => array(
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. 'default' => 0
  121. ),
  122. 'sync_date' => array(
  123. 'type' => 'int',
  124. 'not null' => FALSE,
  125. 'description' => 'UNIX integer sync date/time'
  126. ),
  127. ),
  128. 'indexes' => array(
  129. 'chado_feature_idx1' => array('feature_id')
  130. ),
  131. 'unique keys' => array(
  132. 'chado_feature_uq1' => array('nid', 'vid'),
  133. 'chado_feature_uq2' => array('vid')
  134. ),
  135. 'primary key' => array('nid'),
  136. );
  137. return $schema;
  138. };
  139. /**
  140. * Creates a materialized view that stores the type & number of features per organism
  141. *
  142. * @ingroup tripal_feature
  143. */
  144. function tripal_feature_add_organism_count_mview() {
  145. $view_name = 'organism_feature_count';
  146. $comment = 'Stores the type and number of features per organism';
  147. $schema = array(
  148. 'description' => $comment,
  149. 'table' => $view_name,
  150. 'fields' => array(
  151. 'organism_id' => array(
  152. 'type' => 'int',
  153. 'not null' => TRUE,
  154. ),
  155. 'genus' => array(
  156. 'type' => 'varchar',
  157. 'length' => '255',
  158. 'not null' => TRUE,
  159. ),
  160. 'species' => array(
  161. 'type' => 'varchar',
  162. 'length' => '255',
  163. 'not null' => TRUE,
  164. ),
  165. 'common_name' => array(
  166. 'type' => 'varchar',
  167. 'length' => '255',
  168. 'not null' => FALSE,
  169. ),
  170. 'num_features' => array(
  171. 'type' => 'int',
  172. 'not null' => TRUE,
  173. ),
  174. 'cvterm_id' => array(
  175. 'type' => 'int',
  176. 'not null' => TRUE,
  177. ),
  178. 'feature_type' => array(
  179. 'type' => 'varchar',
  180. 'length' => '255',
  181. 'not null' => TRUE,
  182. ),
  183. ),
  184. 'indexes' => array(
  185. 'organism_feature_count_idx1' => array('organism_id'),
  186. 'organism_feature_count_idx2' => array('cvterm_id'),
  187. 'organism_feature_count_idx3' => array('feature_type'),
  188. ),
  189. );
  190. $sql = "
  191. SELECT
  192. O.organism_id, O.genus, O.species, O.common_name,
  193. count(F.feature_id) as num_features,
  194. CVT.cvterm_id, CVT.name as feature_type
  195. FROM organism O
  196. INNER JOIN feature F ON O.Organism_id = F.organism_id
  197. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  198. GROUP BY
  199. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  200. ";
  201. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  202. }
  203. /**
  204. * Add cvs related to publications
  205. *
  206. * @ingroup tripal_pub
  207. */
  208. function tripal_feature_add_cvs() {
  209. // Add cv for relationship types
  210. tripal_insert_cv(
  211. 'feature_relationship',
  212. 'Contains types of relationships between features.'
  213. );
  214. // the feature_property CV already exists... it comes with chado, but we need to
  215. // add it just in case it doesn't get added before the feature module is installed
  216. tripal_insert_cv(
  217. 'feature_property',
  218. 'Stores properties about features'
  219. );
  220. // the feature type vocabulary should be the sequence ontology, and even though
  221. // this ontology should get loaded we will create it here just so that we can
  222. // set the default vocabulary for the feature.type_id field
  223. tripal_insert_cv(
  224. 'sequence',
  225. 'The Sequence Ontology'
  226. );
  227. }
  228. /**
  229. * This is the required update for tripal_feature when upgrading from Drupal core API 6.x.
  230. * This update may take some time to complete.
  231. */
  232. function tripal_feature_update_7200() {
  233. // During the upgrade from D6 to D7 the vocabulary terms assigned to features were
  234. // copied to the field_data_taxonomyextra table rather than to the correct
  235. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  236. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Feature Type'")->fetchField();
  237. if ($vid) {
  238. try {
  239. // first move from the field_data_taxonomyextra table
  240. $sql = "
  241. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  242. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  243. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  244. FROM field_data_taxonomyextra
  245. WHERE bundle = 'chado_feature')
  246. ";
  247. db_query($sql);
  248. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";
  249. db_query($sql);
  250. // next move from the field_revision_taxonomyextra table
  251. $sql = "
  252. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  253. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  254. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  255. FROM field_revision_taxonomyextra
  256. WHERE bundle = 'chado_feature')
  257. ";
  258. db_query($sql);
  259. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";
  260. db_query($sql);
  261. }
  262. catch (\PDOException $e) {
  263. $error = $e->getMessage();
  264. throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
  265. }
  266. }
  267. // set the default feature property vocabulary
  268. try {
  269. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_property'")->fetchField();
  270. db_insert('tripal_cv_defaults')
  271. ->fields(array(
  272. 'table_name' => 'featureprop',
  273. 'field_name' => 'type_id',
  274. 'cv_id' => $cv_id
  275. ))
  276. ->execute();
  277. }
  278. catch (\PDOException $e) {
  279. $error = $e->getMessage();
  280. throw new DrupalUpdateException('Failed to set feature_property vocabulary as default: '. $error);
  281. }
  282. // add the feature_relationshp CV
  283. try {
  284. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_relationship'")->fetchField();
  285. if (!$cv_id) {
  286. // add the vocabulary
  287. $cv_id = db_insert('chado.cv')
  288. ->fields(array(
  289. 'name' => 'feature_relationship',
  290. 'definition' => 'Contains types of relationships between features.'
  291. ))
  292. ->execute();
  293. }
  294. // use the new feature_relationship CV we just added
  295. db_insert('tripal_cv_defaults')
  296. ->fields(array(
  297. 'table_name' => 'feature_relationship',
  298. 'field_name' => 'type_id',
  299. 'cv_id' => $cv_id
  300. ))
  301. ->execute();
  302. }
  303. catch (\PDOException $e) {
  304. $error = $e->getMessage();
  305. throw new DrupalUpdateException('Failed to add feature_relationship vocabulary: '. $error);
  306. }
  307. // set the feature_type as the 'sequence' ontology
  308. try {
  309. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'sequence'")->fetchField();
  310. if (!$cv_id) {
  311. // add the vocabulary
  312. $cv_id = db_insert('chado.cv')
  313. ->fields(array(
  314. 'name' => 'sequence',
  315. 'definition' => 'The Sequence Ontology.'
  316. ))
  317. ->execute();
  318. }
  319. db_insert('tripal_cv_defaults')
  320. ->fields(array(
  321. 'table_name' => 'feature',
  322. 'field_name' => 'type_id',
  323. 'cv_id' => $cv_id
  324. ))
  325. ->execute();
  326. }
  327. catch (\PDOException $e) {
  328. $error = $e->getMessage();
  329. throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);
  330. }
  331. }
  332. /**
  333. * Implementation of hook_update_dependencies(). It specifies a list of
  334. * other modules whose updates must be run prior to this one.
  335. */
  336. function tripal_feature_update_dependencies() {
  337. $dependencies = array();
  338. // the tripal_cv update 7200 must run prior to update 7200 of this module
  339. $dependencies['tripal_feature'][7200] = array(
  340. 'tripal_cv' => 7200
  341. );
  342. return $dependencies;
  343. }
  344. /**
  345. * Fixes an error with the materialized view installation
  346. *
  347. */
  348. function tripal_feature_update_7201() {
  349. try {
  350. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  351. // materialized view schema to the mviews table.
  352. // get the schema for the materialized view from the custom_tables table
  353. // as there is a copy there, but only if the schema is missing from the
  354. // materialized view table
  355. $view_name = 'organism_feature_count';
  356. $schema = db_select('tripal_mviews', 'tm')
  357. ->fields('tm', array('mv_schema'))
  358. ->condition('name', $view_name)
  359. ->execute()
  360. ->fetchField();
  361. if (!$schema or $schema == 'Array') {
  362. $schema = db_select('tripal_custom_tables', 'tct')
  363. ->fields('tct', array('schema'))
  364. ->condition('table_name', $view_name)
  365. ->execute()
  366. ->fetchField();
  367. $schema_str = var_export(unserialize($schema), TRUE);
  368. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  369. db_update('tripal_mviews')
  370. ->fields(array(
  371. 'mv_schema' => $schema_str
  372. ))
  373. ->condition('name', $view_name)
  374. ->execute();
  375. }
  376. }
  377. catch (\PDOException $e) {
  378. $error = $e->getMessage();
  379. throw new DrupalUpdateException('Failed to complete update' . $error);
  380. }
  381. }