tripal_feature.install 6.1 KB

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