tripal_analysis_unigene.install 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. //$Id:
  3. /*******************************************************************************
  4. * Implementation of hook_install().
  5. */
  6. function tripal_analysis_unigene_install() {
  7. // create the module's data directory
  8. tripal_create_moddir('tripal_analysis_unigene');
  9. // We need to register to tripal_analysis module so it can provide a control
  10. // for our unigene result. Basically the registration is done by inserting
  11. // required information into the {tripal_analysis} table.
  12. tripal_analysis_register_child('tripal_analysis_unigene');
  13. $previous_db = tripal_db_set_active('chado');
  14. if (db_table_exists('unigene_libraries_mview')) {
  15. $sql = "DROP TABLE unigene_libraries_mview";
  16. db_query($sql);
  17. }
  18. if (db_table_exists('unigene_mview')) {
  19. $sql = "DROP TABLE unigene_mview";
  20. db_query($sql);
  21. }
  22. tripal_db_set_active($previous_db);
  23. tripal_analysis_unigene_add_cvterms();
  24. tripal_analysis_unigene_add_organism_unigene_mview();
  25. }
  26. /**
  27. *
  28. */
  29. function tripal_analysis_unigene_update_6000(){
  30. // we have some new cvterms to add
  31. tripal_analysis_unigene_add_cvterms();
  32. // remove the old unigene materialized view and add the new one.
  33. $mview = tripal_mviews_get_mview_id('unigene_mview');
  34. if($mview){
  35. tripal_mviews_action('delete',$mview);
  36. }
  37. tripal_analysis_unigene_add_organism_unigene_mview();
  38. // next, add the cvterm 'analysis_unigene_name' to all existing
  39. // unigenes that are already in the database. This will allow the
  40. // materialized view to work.
  41. $sql = "SELECT N.nid,N.title,CN.analysis_id
  42. FROM {node} N
  43. INNER JOIN {chado_analysis} CN on CN.nid = N.nid
  44. WHERE type = 'chado_analysis_unigene'";
  45. $analyses = db_query($sql);
  46. while($analysis = db_fetch_object($analyses)){
  47. tripal_analysis_insert_property($analysis->analysis_id,'analysis_unigene_name',$analysis->title);
  48. }
  49. $ret = array(
  50. '#finished' => 1,
  51. );
  52. return $ret;
  53. }
  54. /**
  55. *
  56. */
  57. function tripal_analysis_unigene_add_cvterms(){
  58. tripal_add_cvterms('unigene_version','The version number for the unigene ".
  59. "(e.g. v1, v2, etc...) ');
  60. tripal_add_cvterms('analysis_unigene_name', 'The name for a unigene.');
  61. tripal_add_cvterms('analysis_unigene_num_contigs','The number of contigs in the unigene assembly');
  62. tripal_add_cvterms('analysis_unigene_num_singlets','The number of singlets remaining in the unigene assembly');
  63. tripal_add_cvterms('analysis_unigene_num_clusters','The number of clusters in the unigene assembly');
  64. tripal_add_cvterms('analysis_unigene_num_reads','The number of reads, after filtering, used as input for the assembly');
  65. tripal_add_cvterms('singlet',"Indicates the feature is a singlet in a ".
  66. "specific unigene version (e.g. v1, v2, etc...). The value specified ".
  67. "should match that of the unigene_version");
  68. // Add cveterm 'analysis_unigene_settings' for inserting into analysisprop table
  69. tripal_add_cvterms('analysis_unigene_settings', 'Settings of a unigene analysis');
  70. }
  71. /**
  72. *
  73. */
  74. function tripal_analysis_unigene_add_organism_unigene_mview(){
  75. $view_name = 'organism_unigene_mview';
  76. // Drop the MView table if it exists
  77. $mview_id = tripal_mviews_get_mview_id($view_name);
  78. if($mview_id){
  79. tripal_mviews_action("delete",$mview_id);
  80. }
  81. tripal_add_mview(
  82. // view name
  83. $view_name,
  84. // tripal module name
  85. 'tripal_analysis_unigene',
  86. // table name
  87. $view_name,
  88. // table schema definition
  89. 'analysis_id integer, organism_id integer',
  90. // columns for indexing
  91. 'analysis_id, organism_id',
  92. // SQL statement to populate the view
  93. "SELECT DISTINCT A.analysis_id, O.organism_id ".
  94. "FROM {Analysis} A ".
  95. " INNER JOIN analysisprop AP ON AP.analysis_id = A.analysis_id ".
  96. " INNER JOIN cvterm CVT ON AP.type_id = CVT.cvterm_id ".
  97. " INNER JOIN cv CV ON CV.cv_id = CVT.cv_id ".
  98. " INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id ".
  99. " INNER JOIN feature F ON AF.feature_id = F.feature_id ".
  100. " INNER JOIN organism O ON F.organism_id = O.organism_id ".
  101. "WHERE CV.name = 'tripal' AND CVT.name='analysis_unigene_name'",
  102. // special index
  103. ''
  104. );
  105. // add a job to the job queue so this view gets updated automatically next
  106. // time the job facility is run
  107. $mview_id = tripal_mviews_get_mview_id($view_name);
  108. if($mview_id){
  109. tripal_mviews_action('update',$mview_id);
  110. }
  111. }
  112. /*******************************************************************************
  113. * Implementation of hook_uninstall().
  114. */
  115. function tripal_analysis_unigene_uninstall() {
  116. $mview = tripal_mviews_get_mview_id('unigene_mview');
  117. if($mview){
  118. tripal_mviews_action('delete',$mview);
  119. }
  120. $mview = tripal_mviews_get_mview_id('unigene_libraries_mview');
  121. if($mview){
  122. tripal_mviews_action('delete',$mview);
  123. }
  124. // Delete the settings from {tripal_analysis} table
  125. // Drupal complains when the user tries to uninstall tripal_analysis
  126. // and tripal_analysis_unigene at the same time. This is because Drupal drops
  127. // the {tripal_analysis} table before we can delete anything from it. Thus,
  128. // we perform a db_table_exists() check before the deletion
  129. tripal_analysis_unregister_child('tripal_analysis_unigene');
  130. // Delete module's variables from variables table.
  131. db_query("DELETE FROM {variable} WHERE name='%s'",
  132. 'tripal_analysis_unigene_setting');
  133. }
  134. /*******************************************************************************
  135. * Implementation of hook_requirements(). Make sure 'Tripal Core' and 'Tripal
  136. * Analysis' are enabled before installation
  137. */
  138. function tripal_analysis_unigene_requirements($phase) {
  139. $requirements = array();
  140. if ($phase == 'install') {
  141. if (!function_exists('tripal_create_moddir') || !function_exists('tripal_analysis_register_child')) {
  142. $requirements ['tripal_analysis_unigene'] = array(
  143. 'title' => "tripal_analysis_unigene",
  144. 'value' => "error. Some required modules are just being installed. Please try again.",
  145. 'severity' => REQUIREMENT_ERROR,
  146. );
  147. }
  148. }
  149. return $requirements;
  150. }