tripal_feature.install 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. }
  50. /**
  51. * Implementation of hook_uninstall().
  52. *
  53. * @ingroup tripal_feature
  54. */
  55. function tripal_feature_uninstall() {
  56. // Drop the MView table if it exists
  57. // D7 @todo Fix tripal_mviews_get_mview_id()
  58. /**
  59. if ($mview_id = tripal_mviews_get_mview_id('organism_feature_count')) {
  60. tripal_mviews_action("delete", $mview_id);
  61. }
  62. */
  63. }
  64. /**
  65. * Implementation of hook_schema().
  66. *
  67. * @ingroup tripal_feature
  68. */
  69. function tripal_feature_schema() {
  70. $schema['chado_feature'] = array(
  71. 'fields' => array(
  72. 'vid' => array(
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. 'default' => 0
  77. ),
  78. 'nid' => array(
  79. 'type' => 'int',
  80. 'unsigned' => TRUE,
  81. 'not null' => TRUE,
  82. 'default' => 0
  83. ),
  84. 'feature_id' => array(
  85. 'type' => 'int',
  86. 'not null' => TRUE,
  87. 'default' => 0
  88. ),
  89. 'sync_date' => array(
  90. 'type' => 'int',
  91. 'not null' => FALSE,
  92. 'description' => 'UNIX integer sync date/time'
  93. ),
  94. ),
  95. 'indexes' => array(
  96. 'chado_feature_idx1' => array('feature_id')
  97. ),
  98. 'unique keys' => array(
  99. 'chado_feature_uq1' => array('nid', 'vid'),
  100. 'chado_feature_uq2' => array('vid')
  101. ),
  102. 'primary key' => array('nid'),
  103. );
  104. return $schema;
  105. };
  106. /**
  107. *
  108. * @ingroup tripal_feature
  109. */
  110. function tripal_feature_add_organism_count_mview() {
  111. $view_name = 'organism_feature_count';
  112. $comment = 'Stores the type and number of features per organism';
  113. $schema = array(
  114. 'description' => $comment,
  115. 'table' => $view_name,
  116. 'fields' => array(
  117. 'organism_id' => array(
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. ),
  121. 'genus' => array(
  122. 'type' => 'varchar',
  123. 'length' => '255',
  124. 'not null' => TRUE,
  125. ),
  126. 'species' => array(
  127. 'type' => 'varchar',
  128. 'length' => '255',
  129. 'not null' => TRUE,
  130. ),
  131. 'common_name' => array(
  132. 'type' => 'varchar',
  133. 'length' => '255',
  134. 'not null' => FALSE,
  135. ),
  136. 'num_features' => array(
  137. 'type' => 'int',
  138. 'not null' => TRUE,
  139. ),
  140. 'cvterm_id' => array(
  141. 'type' => 'int',
  142. 'not null' => TRUE,
  143. ),
  144. 'feature_type' => array(
  145. 'type' => 'varchar',
  146. 'length' => '255',
  147. 'not null' => TRUE,
  148. ),
  149. ),
  150. 'indexes' => array(
  151. 'organism_feature_count_idx1' => array('organism_id'),
  152. 'organism_feature_count_idx2' => array('cvterm_id'),
  153. 'organism_feature_count_idx3' => array('feature_type'),
  154. ),
  155. );
  156. $sql = "
  157. SELECT
  158. O.organism_id, O.genus, O.species, O.common_name,
  159. count(F.feature_id) as num_features,
  160. CVT.cvterm_id, CVT.name as feature_type
  161. FROM organism O
  162. INNER JOIN feature F ON O.Organism_id = F.organism_id
  163. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  164. GROUP BY
  165. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  166. ";
  167. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  168. }