tripal_analysis_unigene.install 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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('analysis_unigene_avg_length','The average contig length');
  66. tripal_add_cvterms('singlet',"Indicates the feature is a singlet in a ".
  67. "specific unigene version (e.g. v1, v2, etc...). The value specified ".
  68. "should match that of the unigene_version");
  69. // Add cveterm 'analysis_unigene_settings' for inserting into analysisprop table
  70. tripal_add_cvterms('analysis_unigene_settings', 'Settings of a unigene analysis');
  71. }
  72. /**
  73. *
  74. */
  75. function tripal_analysis_unigene_add_organism_unigene_mview(){
  76. $view_name = 'organism_unigene_mview';
  77. // Drop the MView table if it exists
  78. $mview_id = tripal_mviews_get_mview_id($view_name);
  79. if($mview_id){
  80. tripal_mviews_action("delete",$mview_id);
  81. }
  82. tripal_add_mview(
  83. // view name
  84. $view_name,
  85. // tripal module name
  86. 'tripal_analysis_unigene',
  87. // table name
  88. $view_name,
  89. // table schema definition
  90. 'analysis_id integer, organism_id integer',
  91. // columns for indexing
  92. 'analysis_id, organism_id',
  93. // SQL statement to populate the view
  94. "SELECT DISTINCT A.analysis_id, O.organism_id ".
  95. "FROM {Analysis} A ".
  96. " INNER JOIN analysisprop AP ON AP.analysis_id = A.analysis_id ".
  97. " INNER JOIN cvterm CVT ON AP.type_id = CVT.cvterm_id ".
  98. " INNER JOIN cv CV ON CV.cv_id = CVT.cv_id ".
  99. " INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id ".
  100. " INNER JOIN feature F ON AF.feature_id = F.feature_id ".
  101. " INNER JOIN organism O ON F.organism_id = O.organism_id ".
  102. "WHERE CV.name = 'tripal' AND CVT.name='analysis_unigene_name'",
  103. // special index
  104. ''
  105. );
  106. // add a job to the job queue so this view gets updated automatically next
  107. // time the job facility is run
  108. $mview_id = tripal_mviews_get_mview_id($view_name);
  109. if($mview_id){
  110. tripal_mviews_action('update',$mview_id);
  111. }
  112. }
  113. /*******************************************************************************
  114. * Implementation of hook_uninstall().
  115. */
  116. function tripal_analysis_unigene_uninstall() {
  117. $mview = tripal_mviews_get_mview_id('unigene_mview');
  118. if($mview){
  119. tripal_mviews_action('delete',$mview);
  120. }
  121. $mview = tripal_mviews_get_mview_id('unigene_libraries_mview');
  122. if($mview){
  123. tripal_mviews_action('delete',$mview);
  124. }
  125. // Delete the settings from {tripal_analysis} table
  126. // Drupal complains when the user tries to uninstall tripal_analysis
  127. // and tripal_analysis_unigene at the same time. This is because Drupal drops
  128. // the {tripal_analysis} table before we can delete anything from it. Thus,
  129. // we perform a db_table_exists() check before the deletion
  130. tripal_analysis_unregister_child('tripal_analysis_unigene');
  131. // Delete module's variables from variables table.
  132. db_query("DELETE FROM {variable} WHERE name='%s'",
  133. 'tripal_analysis_unigene_setting');
  134. }
  135. /*******************************************************************************
  136. * Implementation of hook_requirements(). Make sure 'Tripal Core' and 'Tripal
  137. * Analysis' are enabled before installation
  138. */
  139. function tripal_analysis_unigene_requirements($phase) {
  140. $requirements = array();
  141. if ($phase == 'install') {
  142. if (!function_exists('tripal_create_moddir') || !function_exists('tripal_analysis_register_child')) {
  143. $requirements ['tripal_analysis_unigene'] = array(
  144. 'title' => "tripal_analysis_unigene",
  145. 'value' => "error. Some required modules are just being installed. Please try again.",
  146. 'severity' => REQUIREMENT_ERROR,
  147. );
  148. }
  149. }
  150. return $requirements;
  151. }