tripal_feature.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_views_admin_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_cv_add_cv(
  211. 'feature_relationship',
  212. 'Contains types of relationships between features.'
  213. );
  214. // the feature_property CV already exists... it comes with chado, so no need to add it here
  215. // the feature type vocabulary should be the sequence ontology, and even though
  216. // this ontology should get loaded we will create it here just so that we can
  217. // set the default vocabulary for the feature.type_id field
  218. tripal_cv_add_cv(
  219. 'sequence',
  220. 'The Sequence Ontology'
  221. );
  222. }
  223. /**
  224. * This is the required update for tripal_feature when upgrading from Drupal core API 6.x.
  225. * This update may take some time to complete.
  226. */
  227. function tripal_feature_update_7200() {
  228. // During the upgrade from D6 to D7 the vocabulary terms assigned to features were
  229. // copied to the field_data_taxonomyextra table rather than to the correct
  230. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  231. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Feature Type'")->fetchField();
  232. if ($vid) {
  233. try {
  234. // first move from the field_data_taxonomyextra table
  235. $sql = "
  236. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  237. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  238. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  239. FROM field_data_taxonomyextra
  240. WHERE bundle = 'chado_feature')
  241. ";
  242. db_query($sql);
  243. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";
  244. db_query($sql);
  245. // next move from the field_revision_taxonomyextra table
  246. $sql = "
  247. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  248. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  249. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  250. FROM field_revision_taxonomyextra
  251. WHERE bundle = 'chado_feature')
  252. ";
  253. db_query($sql);
  254. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";
  255. db_query($sql);
  256. }
  257. catch (\PDOException $e) {
  258. $error = $e->getMessage();
  259. throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
  260. }
  261. }
  262. // set the default feature property vocabulary
  263. try {
  264. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_property'")->fetchField();
  265. db_insert('tripal_cv_defaults')
  266. ->fields(array(
  267. 'table_name' => 'featureprop',
  268. 'field_name' => 'type_id',
  269. 'cv_id' => $cv_id
  270. ))
  271. ->execute();
  272. }
  273. catch (\PDOException $e) {
  274. $error = $e->getMessage();
  275. throw new DrupalUpdateException('Failed to set feature_property vocabulary as default: '. $error);
  276. }
  277. // add the feature_relationshp CV
  278. try {
  279. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_relationship'")->fetchField();
  280. if (!$cv_id) {
  281. // add the vocabulary
  282. $cv_id = db_insert('chado.cv')
  283. ->fields(array(
  284. 'name' => 'feature_relationship',
  285. 'definition' => 'Contains types of relationships between features.'
  286. ))
  287. ->execute();
  288. }
  289. // use the new feature_relationship CV we just added
  290. db_insert('tripal_cv_defaults')
  291. ->fields(array(
  292. 'table_name' => 'feature_relationship',
  293. 'field_name' => 'type_id',
  294. 'cv_id' => $cv_id
  295. ))
  296. ->execute();
  297. }
  298. catch (\PDOException $e) {
  299. $error = $e->getMessage();
  300. throw new DrupalUpdateException('Failed to add feature_relationship vocabulary: '. $error);
  301. }
  302. // set the feature_type as the 'sequence' ontology
  303. try {
  304. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'sequence'")->fetchField();
  305. if (!$cv_id) {
  306. // add the vocabulary
  307. $cv_id = db_insert('chado.cv')
  308. ->fields(array(
  309. 'name' => 'sequence',
  310. 'definition' => 'The Sequence Ontology.'
  311. ))
  312. ->execute();
  313. }
  314. db_insert('tripal_cv_defaults')
  315. ->fields(array(
  316. 'table_name' => 'feature',
  317. 'field_name' => 'type_id',
  318. 'cv_id' => $cv_id
  319. ))
  320. ->execute();
  321. }
  322. catch (\PDOException $e) {
  323. $error = $e->getMessage();
  324. throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);
  325. }
  326. }
  327. /**
  328. * Implementation of hook_update_dependencies(). It specifies a list of
  329. * other modules whose updates must be run prior to this one.
  330. */
  331. function tripal_feature_update_dependencies() {
  332. $dependencies = array();
  333. // the tripal_cv update 7200 must run prior to update 7200 of this module
  334. $dependencies['tripal_feature'][7200] = array(
  335. 'tripal_cv' => 7200
  336. );
  337. return $dependencies;
  338. }