tripal_feature.install 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // create the temp table we will use for loading GFF files
  45. tripal_cv_create_tripal_gff_temp();
  46. }
  47. /**
  48. * Implementation of hook_uninstall().
  49. *
  50. * @ingroup tripal_feature
  51. */
  52. function tripal_feature_uninstall() {
  53. }
  54. /**
  55. *
  56. */
  57. function tripal_cv_create_tripal_gff_temp() {
  58. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  59. // we create it here using plain SQL because we want it to be in the chado schema but we
  60. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  61. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  62. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  63. if (!db_table_exists('chado.tripal_gff_temp')) {
  64. $sql = "
  65. CREATE TABLE {tripal_gff_temp} (
  66. feature_id integer NOT NULL,
  67. organism_id integer NOT NULL,
  68. uniquename text NOT NULL,
  69. type_name character varying(1024) NOT NULL,
  70. CONSTRAINT tripal_gff_temp_uq0 UNIQUE (feature_id),
  71. CONSTRAINT tripal_gff_temp_uq1 UNIQUE (uniquename, organism_id, type_name)
  72. );
  73. ";
  74. chado_query($sql);
  75. $sql = "CREATE INDEX tripal_gff_temp_idx0 ON {tripal_gff_temp} USING btree (feature_id)";
  76. chado_query($sql);
  77. $sql = "CREATE INDEX tripal_gff_temp_idx1 ON {tripal_gff_temp} USING btree (organism_id)";
  78. chado_query($sql);
  79. $sql = "CREATE INDEX tripal_gff_temp_idx2 ON {tripal_gff_temp} USING btree (uniquename)";
  80. chado_query($sql);
  81. }
  82. }
  83. /**
  84. * Implementation of hook_schema().
  85. *
  86. * @ingroup tripal_feature
  87. */
  88. function tripal_feature_schema() {
  89. $schema['chado_feature'] = array(
  90. 'fields' => array(
  91. 'vid' => array(
  92. 'type' => 'int',
  93. 'unsigned' => TRUE,
  94. 'not null' => TRUE,
  95. 'default' => 0
  96. ),
  97. 'nid' => array(
  98. 'type' => 'int',
  99. 'unsigned' => TRUE,
  100. 'not null' => TRUE,
  101. 'default' => 0
  102. ),
  103. 'feature_id' => array(
  104. 'type' => 'int',
  105. 'not null' => TRUE,
  106. 'default' => 0
  107. ),
  108. 'sync_date' => array(
  109. 'type' => 'int',
  110. 'not null' => FALSE,
  111. 'description' => 'UNIX integer sync date/time'
  112. ),
  113. ),
  114. 'indexes' => array(
  115. 'chado_feature_idx1' => array('feature_id')
  116. ),
  117. 'unique keys' => array(
  118. 'chado_feature_uq1' => array('nid', 'vid'),
  119. 'chado_feature_uq2' => array('vid')
  120. ),
  121. 'primary key' => array('nid'),
  122. );
  123. return $schema;
  124. };
  125. /**
  126. *
  127. * @ingroup tripal_feature
  128. */
  129. function tripal_feature_add_organism_count_mview() {
  130. $view_name = 'organism_feature_count';
  131. $comment = 'Stores the type and number of features per organism';
  132. $schema = array(
  133. 'description' => $comment,
  134. 'table' => $view_name,
  135. 'fields' => array(
  136. 'organism_id' => array(
  137. 'type' => 'int',
  138. 'not null' => TRUE,
  139. ),
  140. 'genus' => array(
  141. 'type' => 'varchar',
  142. 'length' => '255',
  143. 'not null' => TRUE,
  144. ),
  145. 'species' => array(
  146. 'type' => 'varchar',
  147. 'length' => '255',
  148. 'not null' => TRUE,
  149. ),
  150. 'common_name' => array(
  151. 'type' => 'varchar',
  152. 'length' => '255',
  153. 'not null' => FALSE,
  154. ),
  155. 'num_features' => array(
  156. 'type' => 'int',
  157. 'not null' => TRUE,
  158. ),
  159. 'cvterm_id' => array(
  160. 'type' => 'int',
  161. 'not null' => TRUE,
  162. ),
  163. 'feature_type' => array(
  164. 'type' => 'varchar',
  165. 'length' => '255',
  166. 'not null' => TRUE,
  167. ),
  168. ),
  169. 'indexes' => array(
  170. 'organism_feature_count_idx1' => array('organism_id'),
  171. 'organism_feature_count_idx2' => array('cvterm_id'),
  172. 'organism_feature_count_idx3' => array('feature_type'),
  173. ),
  174. );
  175. $sql = "
  176. SELECT
  177. O.organism_id, O.genus, O.species, O.common_name,
  178. count(F.feature_id) as num_features,
  179. CVT.cvterm_id, CVT.name as feature_type
  180. FROM organism O
  181. INNER JOIN feature F ON O.Organism_id = F.organism_id
  182. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  183. GROUP BY
  184. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  185. ";
  186. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  187. }