tripal_feature.install 5.6 KB

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