tripal_feature.install 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. *
  9. * @ingroup tripal_feature
  10. */
  11. function tripal_feature_install() {
  12. // create the module's data directory
  13. tripal_create_moddir('tripal_feature');
  14. // create the tables that correlate drupal nodes with chado
  15. // features, organisms, etc....
  16. drupal_install_schema('tripal_feature');
  17. // add the materialized view
  18. tripal_feature_add_organism_count_mview();
  19. }
  20. /**
  21. * Update for Drupal 6.x, Tripal 0.2b, Feature Module 0.2
  22. * This update adjusts the materialized view by adding a 'cvterm_id' column
  23. *
  24. * @ingroup tripal_feature
  25. */
  26. function tripal_feature_update_6000() {
  27. // recreate the materialized view
  28. tripal_feature_add_organism_count_mview();
  29. $ret = array(
  30. '#finished' => 1,
  31. );
  32. return $ret;
  33. }
  34. /**
  35. *
  36. * @ingroup tripal_feature
  37. */
  38. /*
  39. function tripal_feature_update_6300() {
  40. // add the relationship aggregator table to the database
  41. $schema = tripal_feature_get_schemas('tripal_feature_relagg');
  42. $ret = array();
  43. db_create_table($ret, 'tripal_feature_relagg', $schema['tripal_feature_relagg']);
  44. return $ret;
  45. } */
  46. /**
  47. *
  48. * @ingroup tripal_feature
  49. */
  50. function tripal_feature_add_organism_count_mview() {
  51. $view_name = 'organism_feature_count';
  52. // Drop the MView table if it exists
  53. $mview_id = tripal_mviews_get_mview_id($view_name);
  54. if ($mview_id) {
  55. tripal_mviews_action("delete", $mview_id);
  56. }
  57. // Create the MView
  58. tripal_add_mview(
  59. // view name
  60. $view_name,
  61. // tripal module name
  62. 'tripal_feature',
  63. // table name
  64. $view_name,
  65. // table schema definition
  66. 'organism_id integer, genus character varying(255), '.
  67. ' species character varying(255), '.
  68. ' common_name character varying(255), '.
  69. ' num_features integer, cvterm_id integer, '.
  70. ' feature_type character varying(255)',
  71. // columns for indexing
  72. 'organism_id,cvterm_id,feature_type',
  73. // SQL statement to populate the view
  74. 'SELECT O.organism_id, O.genus, O.species, O.common_name,
  75. count(F.feature_id) as num_features,
  76. CVT.cvterm_id, CVT.name as feature_type
  77. FROM Organism O
  78. INNER JOIN Feature F ON O.Organism_id = F.organism_id
  79. INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id
  80. GROUP BY O.Organism_id, O.genus, O.species, O.common_name,
  81. CVT.cvterm_id, CVT.name',
  82. // special index
  83. ''
  84. );
  85. // add a job to the job queue so this view gets updated automatically next
  86. // time the job facility is run
  87. $mview_id = tripal_mviews_get_mview_id($view_name);
  88. if ($mview_id) {
  89. tripal_mviews_action('update', $mview_id);
  90. }
  91. }
  92. /**
  93. * Implementation of hook_schema().
  94. *
  95. * @ingroup tripal_feature
  96. */
  97. function tripal_feature_schema() {
  98. $schema = tripal_feature_get_schemas();
  99. return $schema;
  100. }
  101. /**
  102. * Implementation of hook_uninstall().
  103. *
  104. * @ingroup tripal_feature
  105. */
  106. function tripal_feature_uninstall() {
  107. // Drop the MView table if it exists
  108. $mview_id = tripal_mviews_get_mview_id('organism_feature_count');
  109. if ($mview_id) {
  110. tripal_mviews_action("delete", $mview_id);
  111. }
  112. drupal_uninstall_schema('tripal_feature');
  113. // Get the list of nodes to remove
  114. $sql_feature_id = "SELECT nid, vid " .
  115. "FROM {node} " .
  116. "WHERE type='chado_feature'";
  117. $result = db_query($sql_feature_id);
  118. while ($node = db_fetch_object($result)) {
  119. node_delete($node->nid);
  120. }
  121. }
  122. /**
  123. * This function simply defines all tables needed for the module to work
  124. * correctly. By putting the table definitions in a separate function we
  125. * can easily provide the entire list for hook_install or individual
  126. * tables for an update.
  127. *
  128. * @ingroup tripal_feature
  129. */
  130. function tripal_feature_get_schemas($table = NULL) {
  131. $schema = array();
  132. if (!$table or strcmp($table, 'chado_feature')==0) {
  133. $schema['chado_feature'] = array(
  134. 'fields' => array(
  135. 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  136. 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  137. 'feature_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  138. 'sync_date' => array('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer sync date/time'),
  139. ),
  140. 'indexes' => array(
  141. 'feature_id' => array('feature_id')
  142. ),
  143. 'unique keys' => array(
  144. 'nid_vid' => array('nid', 'vid'),
  145. 'vid' => array('vid')
  146. ),
  147. 'primary key' => array('nid'),
  148. );
  149. }
  150. return $schema;
  151. };
  152. /**
  153. * Implementation of hook_requirements().
  154. */
  155. function tripal_feature_requirements($phase) {
  156. $requirements = array();
  157. if ($phase == 'install') {
  158. // make sure chado is installed
  159. if (!tripal_core_is_chado_installed()) {
  160. $requirements ['tripal_feature'] = array(
  161. 'title' => "tripal_feature",
  162. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  163. 'severity' => REQUIREMENT_ERROR,
  164. );
  165. }
  166. }
  167. return $requirements;
  168. }