tripal_feature.install 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Implementation of hook_requirements().
  8. */
  9. function tripal_feature_requirements($phase) {
  10. $requirements = array();
  11. if ($phase == 'install') {
  12. // make sure chado is installed
  13. if (!tripal_core_is_chado_installed()) {
  14. $requirements ['tripal_feature'] = array(
  15. 'title' => "tripal_feature",
  16. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  17. 'severity' => REQUIREMENT_ERROR,
  18. );
  19. }
  20. }
  21. return $requirements;
  22. }
  23. /**
  24. * Implementation of hook_install().
  25. *
  26. * @ingroup tripal_feature
  27. */
  28. function tripal_feature_install() {
  29. // create the module's data directory
  30. tripal_create_moddir('tripal_feature');
  31. // add the materialized view
  32. tripal_feature_add_organism_count_mview();
  33. // add a job to the job queue so this view gets updated automatically next
  34. // time the job facility is run
  35. if ($mview_id = tripal_mviews_get_mview_id('organism_feature_count')) {
  36. tripal_mviews_action('update', $mview_id);
  37. }
  38. }
  39. /**
  40. * Implementation of hook_uninstall().
  41. *
  42. * @ingroup tripal_feature
  43. */
  44. function tripal_feature_uninstall() {
  45. // Drop the MView table if it exists
  46. if ($mview_id = tripal_mviews_get_mview_id('organism_feature_count')) {
  47. tripal_mviews_action("delete", $mview_id);
  48. }
  49. }
  50. /**
  51. * Implementation of hook_schema().
  52. *
  53. * @ingroup tripal_feature
  54. */
  55. function tripal_feature_schema() {
  56. $schema['chado_feature'] = array(
  57. 'fields' => array(
  58. 'vid' => array(
  59. 'type' => 'int',
  60. 'unsigned' => TRUE,
  61. 'not null' => TRUE,
  62. 'default' => 0
  63. ),
  64. 'nid' => array(
  65. 'type' => 'int',
  66. 'unsigned' => TRUE,
  67. 'not null' => TRUE,
  68. 'default' => 0
  69. ),
  70. 'feature_id' => array(
  71. 'type' => 'int',
  72. 'not null' => TRUE,
  73. 'default' => 0
  74. ),
  75. 'sync_date' => array(
  76. 'type' => 'int',
  77. 'not null' => FALSE,
  78. 'description' => 'UNIX integer sync date/time'
  79. ),
  80. ),
  81. 'indexes' => array(
  82. 'chado_feature_idx1' => array('feature_id')
  83. ),
  84. 'unique keys' => array(
  85. 'chado_feature_uq1' => array('nid', 'vid'),
  86. 'chado_feature_uq2' => array('vid')
  87. ),
  88. 'primary key' => array('nid'),
  89. );
  90. return $schema;
  91. };
  92. /**
  93. *
  94. * @ingroup tripal_feature
  95. */
  96. function tripal_feature_add_organism_count_mview() {
  97. $view_name = 'organism_feature_count';
  98. $comment = 'Stores the type and number of features per organism';
  99. $schema = array(
  100. 'description' => $comment,
  101. 'table' => $view_name,
  102. 'fields' => array(
  103. 'organism_id' => array(
  104. 'type' => 'int',
  105. 'not null' => TRUE,
  106. ),
  107. 'genus' => array(
  108. 'type' => 'varchar',
  109. 'length' => '255',
  110. 'not null' => TRUE,
  111. ),
  112. 'species' => array(
  113. 'type' => 'varchar',
  114. 'length' => '255',
  115. 'not null' => TRUE,
  116. ),
  117. 'common_name' => array(
  118. 'type' => 'varchar',
  119. 'length' => '255',
  120. 'not null' => FALSE,
  121. ),
  122. 'num_features' => array(
  123. 'type' => 'int',
  124. 'not null' => TRUE,
  125. ),
  126. 'cvterm_id' => array(
  127. 'type' => 'int',
  128. 'not null' => TRUE,
  129. ),
  130. 'feature_type' => array(
  131. 'type' => 'varchar',
  132. 'length' => '255',
  133. 'not null' => TRUE,
  134. ),
  135. ),
  136. 'indexes' => array(
  137. 'organism_feature_count_idx1' => array('organism_id'),
  138. 'organism_feature_count_idx2' => array('cvterm_id'),
  139. 'organism_feature_count_idx3' => array('feature_type'),
  140. ),
  141. );
  142. $sql = "
  143. SELECT
  144. O.organism_id, O.genus, O.species, O.common_name,
  145. count(F.feature_id) as num_features,
  146. CVT.cvterm_id, CVT.name as feature_type
  147. FROM organism O
  148. INNER JOIN feature F ON O.Organism_id = F.organism_id
  149. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  150. GROUP BY
  151. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  152. ";
  153. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  154. }