tripal_feature.install 5.0 KB

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