tripal_feature.install 4.4 KB

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