tripal_feature.install 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Disable default views when module is disabled
  8. */
  9. function tripal_feature_disable() {
  10. // Disable all default views provided by this module
  11. require_once("tripal_feature.views_default.inc");
  12. $views = tripal_feature_views_default_views();
  13. foreach (array_keys($views) as $view_name) {
  14. tripal_views_admin_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  15. }
  16. }
  17. /**
  18. * Implementation of hook_requirements().
  19. */
  20. function tripal_feature_requirements($phase) {
  21. $requirements = array();
  22. if ($phase == 'install') {
  23. // make sure chado is installed
  24. if (!$GLOBALS["chado_is_installed"]) {
  25. $requirements ['tripal_feature'] = array(
  26. 'title' => "t ripal_feature",
  27. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  28. 'severity' => REQUIREMENT_ERROR,
  29. );
  30. }
  31. }
  32. return $requirements;
  33. }
  34. /**
  35. * Implementation of hook_install().
  36. *
  37. * @ingroup tripal_feature
  38. */
  39. function tripal_feature_install() {
  40. // create the module's data directory
  41. tripal_create_moddir('tripal_feature');
  42. // add the materialized view
  43. tripal_feature_add_organism_count_mview();
  44. // add a job to the job queue so this view gets updated automatically next
  45. // time the job facility is run
  46. if ($mview_id = tripal_mviews_get_mview_id('organism_feature_count')) {
  47. tripal_mviews_action('update', $mview_id);
  48. }
  49. // create the temp table we will use for loading GFF files
  50. tripal_cv_create_tripal_gff_temp();
  51. }
  52. /**
  53. * Implementation of hook_uninstall().
  54. *
  55. * @ingroup tripal_feature
  56. */
  57. function tripal_feature_uninstall() {
  58. // Drop the MView table if it exists
  59. // D7 @todo Fix tripal_mviews_get_mview_id()
  60. if ($mview_id = tripal_mviews_get_mview_id('organism_feature_count')) {
  61. tripal_mviews_action("delete", $mview_id);
  62. }
  63. }
  64. /**
  65. *
  66. */
  67. function tripal_cv_create_tripal_gff_temp() {
  68. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  69. // we create it here using plain SQL because we want it to be in the chado schema but we
  70. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  71. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  72. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  73. if (!db_table_exists('chado.tripal_gff_temp')) {
  74. $sql = "
  75. CREATE TABLE {tripal_gff_temp} (
  76. feature_id integer NOT NULL,
  77. organism_id integer NOT NULL,
  78. uniquename text NOT NULL,
  79. type_name character varying(1024) NOT NULL,
  80. CONSTRAINT tripal_gff_temp_uq0 UNIQUE (feature_id),
  81. CONSTRAINT tripal_gff_temp_uq1 UNIQUE (uniquename, organism_id, type_name)
  82. );
  83. ";
  84. chado_query($sql);
  85. $sql = "CREATE INDEX tripal_gff_temp_idx0 ON {tripal_gff_temp} USING btree (feature_id)";
  86. chado_query($sql);
  87. $sql = "CREATE INDEX tripal_gff_temp_idx1 ON {tripal_gff_temp} USING btree (organism_id)";
  88. chado_query($sql);
  89. $sql = "CREATE INDEX tripal_gff_temp_idx2 ON {tripal_gff_temp} USING btree (uniquename)";
  90. chado_query($sql);
  91. }
  92. }
  93. /**
  94. * Implementation of hook_schema().
  95. *
  96. * @ingroup tripal_feature
  97. */
  98. function tripal_feature_schema() {
  99. $schema['chado_feature'] = array(
  100. 'fields' => array(
  101. 'vid' => array(
  102. 'type' => 'int',
  103. 'unsigned' => TRUE,
  104. 'not null' => TRUE,
  105. 'default' => 0
  106. ),
  107. 'nid' => array(
  108. 'type' => 'int',
  109. 'unsigned' => TRUE,
  110. 'not null' => TRUE,
  111. 'default' => 0
  112. ),
  113. 'feature_id' => array(
  114. 'type' => 'int',
  115. 'not null' => TRUE,
  116. 'default' => 0
  117. ),
  118. 'sync_date' => array(
  119. 'type' => 'int',
  120. 'not null' => FALSE,
  121. 'description' => 'UNIX integer sync date/time'
  122. ),
  123. ),
  124. 'indexes' => array(
  125. 'chado_feature_idx1' => array('feature_id')
  126. ),
  127. 'unique keys' => array(
  128. 'chado_feature_uq1' => array('nid', 'vid'),
  129. 'chado_feature_uq2' => array('vid')
  130. ),
  131. 'primary key' => array('nid'),
  132. );
  133. return $schema;
  134. };
  135. /**
  136. *
  137. * @ingroup tripal_feature
  138. */
  139. function tripal_feature_add_organism_count_mview() {
  140. $view_name = 'organism_feature_count';
  141. $comment = 'Stores the type and number of features per organism';
  142. $schema = array(
  143. 'description' => $comment,
  144. 'table' => $view_name,
  145. 'fields' => array(
  146. 'organism_id' => array(
  147. 'type' => 'int',
  148. 'not null' => TRUE,
  149. ),
  150. 'genus' => array(
  151. 'type' => 'varchar',
  152. 'length' => '255',
  153. 'not null' => TRUE,
  154. ),
  155. 'species' => array(
  156. 'type' => 'varchar',
  157. 'length' => '255',
  158. 'not null' => TRUE,
  159. ),
  160. 'common_name' => array(
  161. 'type' => 'varchar',
  162. 'length' => '255',
  163. 'not null' => FALSE,
  164. ),
  165. 'num_features' => array(
  166. 'type' => 'int',
  167. 'not null' => TRUE,
  168. ),
  169. 'cvterm_id' => array(
  170. 'type' => 'int',
  171. 'not null' => TRUE,
  172. ),
  173. 'feature_type' => array(
  174. 'type' => 'varchar',
  175. 'length' => '255',
  176. 'not null' => TRUE,
  177. ),
  178. ),
  179. 'indexes' => array(
  180. 'organism_feature_count_idx1' => array('organism_id'),
  181. 'organism_feature_count_idx2' => array('cvterm_id'),
  182. 'organism_feature_count_idx3' => array('feature_type'),
  183. ),
  184. );
  185. $sql = "
  186. SELECT
  187. O.organism_id, O.genus, O.species, O.common_name,
  188. count(F.feature_id) as num_features,
  189. CVT.cvterm_id, CVT.name as feature_type
  190. FROM organism O
  191. INNER JOIN feature F ON O.Organism_id = F.organism_id
  192. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  193. GROUP BY
  194. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  195. ";
  196. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  197. }