tripal_feature.install 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. }
  53. /**
  54. * Implements hook_uninstall().
  55. *
  56. * @ingroup tripal_feature
  57. */
  58. function tripal_feature_uninstall() {
  59. }
  60. /**
  61. * Create a temporary table used for loading gff3 files
  62. *
  63. * @ingroup tripal_feature
  64. */
  65. function tripal_cv_create_tripal_gff_temp() {
  66. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  67. // we create it here using plain SQL because we want it to be in the chado schema but we
  68. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  69. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  70. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  71. if (!db_table_exists('chado.tripal_gff_temp')) {
  72. $sql = "
  73. CREATE TABLE {tripal_gff_temp} (
  74. feature_id integer NOT NULL,
  75. organism_id integer NOT NULL,
  76. uniquename text NOT NULL,
  77. type_name character varying(1024) NOT NULL,
  78. CONSTRAINT tripal_gff_temp_uq0 UNIQUE (feature_id),
  79. CONSTRAINT tripal_gff_temp_uq1 UNIQUE (uniquename, organism_id, type_name)
  80. );
  81. ";
  82. chado_query($sql);
  83. $sql = "CREATE INDEX tripal_gff_temp_idx0 ON {tripal_gff_temp} USING btree (feature_id)";
  84. chado_query($sql);
  85. $sql = "CREATE INDEX tripal_gff_temp_idx1 ON {tripal_gff_temp} USING btree (organism_id)";
  86. chado_query($sql);
  87. $sql = "CREATE INDEX tripal_gff_temp_idx2 ON {tripal_gff_temp} USING btree (uniquename)";
  88. chado_query($sql);
  89. }
  90. }
  91. /**
  92. * Implementation of hook_schema().
  93. *
  94. * @ingroup tripal_feature
  95. */
  96. function tripal_feature_schema() {
  97. $schema['chado_feature'] = array(
  98. 'fields' => array(
  99. 'vid' => array(
  100. 'type' => 'int',
  101. 'unsigned' => TRUE,
  102. 'not null' => TRUE,
  103. 'default' => 0
  104. ),
  105. 'nid' => array(
  106. 'type' => 'int',
  107. 'unsigned' => TRUE,
  108. 'not null' => TRUE,
  109. 'default' => 0
  110. ),
  111. 'feature_id' => array(
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. 'default' => 0
  115. ),
  116. 'sync_date' => array(
  117. 'type' => 'int',
  118. 'not null' => FALSE,
  119. 'description' => 'UNIX integer sync date/time'
  120. ),
  121. ),
  122. 'indexes' => array(
  123. 'chado_feature_idx1' => array('feature_id')
  124. ),
  125. 'unique keys' => array(
  126. 'chado_feature_uq1' => array('nid', 'vid'),
  127. 'chado_feature_uq2' => array('vid')
  128. ),
  129. 'primary key' => array('nid'),
  130. );
  131. return $schema;
  132. };
  133. /**
  134. * Creates a materialized view that stores the type & number of features per organism
  135. *
  136. * @ingroup tripal_feature
  137. */
  138. function tripal_feature_add_organism_count_mview() {
  139. $view_name = 'organism_feature_count';
  140. $comment = 'Stores the type and number of features per organism';
  141. $schema = array(
  142. 'description' => $comment,
  143. 'table' => $view_name,
  144. 'fields' => array(
  145. 'organism_id' => array(
  146. 'type' => 'int',
  147. 'not null' => TRUE,
  148. ),
  149. 'genus' => array(
  150. 'type' => 'varchar',
  151. 'length' => '255',
  152. 'not null' => TRUE,
  153. ),
  154. 'species' => array(
  155. 'type' => 'varchar',
  156. 'length' => '255',
  157. 'not null' => TRUE,
  158. ),
  159. 'common_name' => array(
  160. 'type' => 'varchar',
  161. 'length' => '255',
  162. 'not null' => FALSE,
  163. ),
  164. 'num_features' => array(
  165. 'type' => 'int',
  166. 'not null' => TRUE,
  167. ),
  168. 'cvterm_id' => array(
  169. 'type' => 'int',
  170. 'not null' => TRUE,
  171. ),
  172. 'feature_type' => array(
  173. 'type' => 'varchar',
  174. 'length' => '255',
  175. 'not null' => TRUE,
  176. ),
  177. ),
  178. 'indexes' => array(
  179. 'organism_feature_count_idx1' => array('organism_id'),
  180. 'organism_feature_count_idx2' => array('cvterm_id'),
  181. 'organism_feature_count_idx3' => array('feature_type'),
  182. ),
  183. );
  184. $sql = "
  185. SELECT
  186. O.organism_id, O.genus, O.species, O.common_name,
  187. count(F.feature_id) as num_features,
  188. CVT.cvterm_id, CVT.name as feature_type
  189. FROM organism O
  190. INNER JOIN feature F ON O.Organism_id = F.organism_id
  191. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  192. GROUP BY
  193. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  194. ";
  195. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  196. }
  197. /**
  198. * This is the required update for tripal_feature when upgrading from Drupal core API 6.x.
  199. */
  200. function tripal_feature_update_7200() {
  201. // During the upgrade from D6 to D7 the vocabulary terms assigned to features were
  202. // copied to the field_data_taxonomyextra table rather than to the correct
  203. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  204. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Feature Type'")->fetchField();
  205. if ($vid) {
  206. try {
  207. // first move from the field_data_taxonomyextra table
  208. $sql = "
  209. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  210. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  211. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  212. FROM field_data_taxonomyextra
  213. WHERE bundle = 'chado_feature')
  214. ";
  215. db_query($sql);
  216. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";
  217. db_query($sql);
  218. // next move from the field_revision_taxonomyextra table
  219. $sql = "
  220. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  221. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  222. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  223. FROM field_revision_taxonomyextra
  224. WHERE bundle = 'chado_feature')
  225. ";
  226. db_query($sql);
  227. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";
  228. db_query($sql);
  229. }
  230. catch (\PDOException $e) {
  231. $error = $e->getMessage();
  232. throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
  233. }
  234. }
  235. }