tripal_feature.install 6.2 KB

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