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