tripal_feature.install 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. function tripal_feature_update_6300() {
  39. // add the relationship aggregator table to the database
  40. $schema = tripal_feature_get_schemas('tripal_feature_relagg');
  41. $ret = array();
  42. db_create_table($ret, 'tripal_feature_relagg', $schema['tripal_feature_relagg']);
  43. return $ret;
  44. }
  45. /**
  46. *
  47. * @ingroup tripal_feature
  48. */
  49. function tripal_feature_add_organism_count_mview() {
  50. $view_name = 'organism_feature_count';
  51. // Drop the MView table if it exists
  52. $mview_id = tripal_mviews_get_mview_id($view_name);
  53. if ($mview_id) {
  54. tripal_mviews_action("delete", $mview_id);
  55. }
  56. // Create the MView
  57. tripal_add_mview(
  58. // view name
  59. $view_name,
  60. // tripal module name
  61. 'tripal_feature',
  62. // table name
  63. $view_name,
  64. // table schema definition
  65. 'organism_id integer, genus character varying(255), '.
  66. ' species character varying(255), '.
  67. ' common_name character varying(255), '.
  68. ' num_features integer, cvterm_id integer, '.
  69. ' feature_type character varying(255)',
  70. // columns for indexing
  71. 'organism_id,cvterm_id,feature_type',
  72. // SQL statement to populate the view
  73. 'SELECT O.organism_id, O.genus, O.species, O.common_name,
  74. count(F.feature_id) as num_features,
  75. CVT.cvterm_id, CVT.name as feature_type
  76. FROM {Organism} O
  77. INNER JOIN Feature F ON O.Organism_id = F.organism_id
  78. INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id
  79. GROUP BY O.Organism_id, O.genus, O.species, O.common_name,
  80. CVT.cvterm_id, CVT.name',
  81. // special index
  82. ''
  83. );
  84. // add a job to the job queue so this view gets updated automatically next
  85. // time the job facility is run
  86. $mview_id = tripal_mviews_get_mview_id($view_name);
  87. if ($mview_id) {
  88. tripal_mviews_action('update', $mview_id);
  89. }
  90. }
  91. /**
  92. * Implementation of hook_schema().
  93. *
  94. * @ingroup tripal_feature
  95. */
  96. function tripal_feature_schema() {
  97. $schema = tripal_feature_get_schemas();
  98. return $schema;
  99. }
  100. /**
  101. * Implementation of hook_uninstall().
  102. *
  103. * @ingroup tripal_feature
  104. */
  105. function tripal_feature_uninstall() {
  106. // Drop the MView table if it exists
  107. $mview_id = tripal_mviews_get_mview_id('organism_feature_count');
  108. if ($mview_id) {
  109. tripal_mviews_action("delete", $mview_id);
  110. }
  111. drupal_uninstall_schema('tripal_feature');
  112. // Get the list of nodes to remove
  113. $sql_feature_id = "SELECT nid, vid " .
  114. "FROM {node} " .
  115. "WHERE type='chado_feature'";
  116. $result = db_query($sql_feature_id);
  117. while ($node = db_fetch_object($result)) {
  118. node_delete($node->nid);
  119. }
  120. }
  121. /**
  122. * This function simply defines all tables needed for the module to work
  123. * correctly. By putting the table definitions in a separate function we
  124. * can easily provide the entire list for hook_install or individual
  125. * tables for an update.
  126. *
  127. * @ingroup tripal_feature
  128. */
  129. function tripal_feature_get_schemas($table = NULL) {
  130. $schema = array();
  131. if (!$table or strcmp($table, 'chado_feature')==0) {
  132. $schema['chado_feature'] = array(
  133. 'fields' => array(
  134. 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  135. 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  136. 'feature_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  137. 'sync_date' => array('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer sync date/time'),
  138. ),
  139. 'indexes' => array(
  140. 'feature_id' => array('feature_id')
  141. ),
  142. 'unique keys' => array(
  143. 'nid_vid' => array('nid', 'vid'),
  144. 'vid' => array('vid')
  145. ),
  146. 'primary key' => array('nid'),
  147. );
  148. }
  149. if (!$table or strcmp($table, 'tripal_feature_relagg')==0) {
  150. $schema['tripal_feature_relagg'] = array(
  151. 'fields' => array(
  152. 'type_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  153. 'rel_type_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  154. ),
  155. 'indexes' => array(
  156. 'type_id' => array('type_id')
  157. ),
  158. );
  159. }
  160. return $schema;
  161. };
  162. /**
  163. * Implementation of hook_requirements().
  164. */
  165. function tripal_feature_requirements($phase) {
  166. $requirements = array();
  167. if ($phase == 'install') {
  168. // make sure chado is installed
  169. $version = tripal_core_set_chado_version();
  170. if ($version == 'not installed') {
  171. $requirements ['tripal_feature'] = array(
  172. 'title' => "tripal_feature",
  173. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  174. 'severity' => REQUIREMENT_ERROR,
  175. );
  176. }
  177. }
  178. return $requirements;
  179. }