tripal_feature.install 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 (!tripal_core_is_chado_installed()) {
  25. $requirements ['tripal_feature'] = array(
  26. 'title' => "tripal_feature",
  27. 'value' => "ERROR: Chado most 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. // add the custom tables to Chado
  50. tripal_feature_add_custom_tables();
  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. /**
  61. if ($mview_id = tripal_mviews_get_mview_id('organism_feature_count')) {
  62. tripal_mviews_action("delete", $mview_id);
  63. }
  64. */
  65. }
  66. /**
  67. * Implementation of hook_schema().
  68. *
  69. * @ingroup tripal_feature
  70. */
  71. function tripal_feature_schema() {
  72. $schema['chado_feature'] = array(
  73. 'fields' => array(
  74. 'vid' => array(
  75. 'type' => 'int',
  76. 'unsigned' => TRUE,
  77. 'not null' => TRUE,
  78. 'default' => 0
  79. ),
  80. 'nid' => array(
  81. 'type' => 'int',
  82. 'unsigned' => TRUE,
  83. 'not null' => TRUE,
  84. 'default' => 0
  85. ),
  86. 'feature_id' => array(
  87. 'type' => 'int',
  88. 'not null' => TRUE,
  89. 'default' => 0
  90. ),
  91. 'sync_date' => array(
  92. 'type' => 'int',
  93. 'not null' => FALSE,
  94. 'description' => 'UNIX integer sync date/time'
  95. ),
  96. ),
  97. 'indexes' => array(
  98. 'chado_feature_idx1' => array('feature_id')
  99. ),
  100. 'unique keys' => array(
  101. 'chado_feature_uq1' => array('nid', 'vid'),
  102. 'chado_feature_uq2' => array('vid')
  103. ),
  104. 'primary key' => array('nid'),
  105. );
  106. return $schema;
  107. };
  108. /**
  109. *
  110. * @ingroup tripal_feature
  111. */
  112. function tripal_feature_add_organism_count_mview() {
  113. $view_name = 'organism_feature_count';
  114. $comment = 'Stores the type and number of features per organism';
  115. $schema = array(
  116. 'description' => $comment,
  117. 'table' => $view_name,
  118. 'fields' => array(
  119. 'organism_id' => array(
  120. 'type' => 'int',
  121. 'not null' => TRUE,
  122. ),
  123. 'genus' => array(
  124. 'type' => 'varchar',
  125. 'length' => '255',
  126. 'not null' => TRUE,
  127. ),
  128. 'species' => array(
  129. 'type' => 'varchar',
  130. 'length' => '255',
  131. 'not null' => TRUE,
  132. ),
  133. 'common_name' => array(
  134. 'type' => 'varchar',
  135. 'length' => '255',
  136. 'not null' => FALSE,
  137. ),
  138. 'num_features' => array(
  139. 'type' => 'int',
  140. 'not null' => TRUE,
  141. ),
  142. 'cvterm_id' => array(
  143. 'type' => 'int',
  144. 'not null' => TRUE,
  145. ),
  146. 'feature_type' => array(
  147. 'type' => 'varchar',
  148. 'length' => '255',
  149. 'not null' => TRUE,
  150. ),
  151. ),
  152. 'indexes' => array(
  153. 'organism_feature_count_idx1' => array('organism_id'),
  154. 'organism_feature_count_idx2' => array('cvterm_id'),
  155. 'organism_feature_count_idx3' => array('feature_type'),
  156. ),
  157. );
  158. $sql = "
  159. SELECT
  160. O.organism_id, O.genus, O.species, O.common_name,
  161. count(F.feature_id) as num_features,
  162. CVT.cvterm_id, CVT.name as feature_type
  163. FROM organism O
  164. INNER JOIN feature F ON O.Organism_id = F.organism_id
  165. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  166. GROUP BY
  167. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  168. ";
  169. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  170. }
  171. /**
  172. *
  173. */
  174. function tripal_feature_add_custom_tables() {
  175. $schema['tripal_gff_temp'] = array(
  176. 'table' => 'tripal_gff_temp',
  177. 'fields' => array(
  178. 'feature_id' => array(
  179. 'type' => 'int',
  180. 'not null' => TRUE,
  181. ),
  182. 'organism_id' => array(
  183. 'type' => 'int',
  184. 'not null' => TRUE,
  185. ),
  186. 'uniquename' => array(
  187. 'type' => 'text',
  188. 'not null' => TRUE,
  189. ),
  190. 'type_name' => array(
  191. 'type' => 'varchar',
  192. 'length' => '1024',
  193. 'not null' => TRUE,
  194. ),
  195. ),
  196. 'indexes' => array(
  197. 'tripal_gff_temp_idx0' => array('feature_id'),
  198. 'tripal_gff_temp_idx0' => array('organism_id'),
  199. 'tripal_gff_temp_idx1' => array('uniquename'),
  200. ),
  201. 'unique keys' => array(
  202. 'tripal_gff_temp_uq0' => array('feature_id'),
  203. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  204. ),
  205. );
  206. tripal_core_create_custom_table('tripal_gff_temp', $schema, TRUE);
  207. }