tripal_search_unigene.install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*******************************************************************************
  3. * Implementation of hook_install();
  4. */
  5. function tripal_search_unigene_install(){
  6. // create the module's data directory
  7. tripal_create_moddir('tripal_search_unigene');
  8. // Create a sequence as the primiry key for the unigene_for_search
  9. $sql = "DROP SEQUENCE IF EXISTS unigene_for_search_id; CREATE SEQUENCE unigene_for_search_id";
  10. chado_query($sql);
  11. $view_name = 'unigene_for_search';
  12. // Drop the MView table if it exists
  13. $mview_id = tripal_mviews_get_mview_id($view_name);
  14. if($mview_id){
  15. tripal_mviews_action("delete",$mview_id);
  16. }
  17. $schema = " unigene_for_search_id integer,
  18. feature_id integer,
  19. feature_type character varying(1024),
  20. feature_name character varying(1024),
  21. feature_uniquename text,
  22. feature_is_obsolete boolean,
  23. organism_common_name character varying(255),
  24. feature_seqlen integer,
  25. go_term character varying(1024),
  26. unigene character varying(255),
  27. blast_value text,
  28. kegg_value text,
  29. interpro_value text
  30. ";
  31. $index = "feature_id, go_term, unigene, blast_value, kegg_value, interpro_value";
  32. $singlet_type_id = tripal_get_cvterm_id('singlet_in_analysis');
  33. if ($singlet_type_id) {
  34. $singlet_condition = " AND F.feature_id NOT IN (SELECT feature_id from feature
  35. WHERE type_id = (select cvterm_id from cvterm where name = 'EST')
  36. AND feature_id NOT IN (SELECT F.feature_id FROM feature F
  37. INNER JOIN featureprop FP ON F.feature_id = FP.feature_id
  38. AND FP.type_id = (select cvterm_id from cvterm where name = 'singlet_in_analysis')))";
  39. }
  40. $EST_type = db_result(chado_query("SELECT cvterm_id FROM {cvterm} WHERE name = 'EST' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'sequence')")); //P.S. 'EST' is not a tripal term
  41. $sql = "SELECT nextval ('unigene_for_search_id') AS unigene_for_search_id, T.* FROM (
  42. SELECT F.feature_id AS feature_id,
  43. (SELECT name FROM cvterm WHERE F.type_id = cvterm_id) AS feature_type,
  44. F.name AS feature_name,
  45. F.uniquename AS feature_uniquename,
  46. F.is_obsolete AS feature_is_obsolete,
  47. O.common_name AS organism_common_name,
  48. F.seqlen AS feature_seqlen,
  49. C.name AS go_term,
  50. UNIGENE.name AS unigene_name,
  51. BLAST.value AS blast_value,
  52. KEGG.value AS KEGG_value,
  53. INTERPRO.value AS interpro_value
  54. FROM feature F
  55. LEFT JOIN organism O ON F.organism_id = O.organism_id
  56. LEFT JOIN feature_cvterm FC ON F.feature_id = FC.feature_id
  57. LEFT JOIN cvterm C ON FC.cvterm_id = C.cvterm_id
  58. LEFT JOIN (SELECT AF.feature_id, AFP.value FROM analysisfeatureprop AFP
  59. INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id
  60. WHERE type_id = (SELECT cvterm_id FROM cvterm
  61. WHERE name = 'analysis_blast_besthit_description'
  62. AND cv_id = (SELECT cv_id FROM cv WHERE name = 'tripal')
  63. ) GROUP BY feature_id, value
  64. ) BLAST ON BLAST.feature_id = F.feature_id
  65. LEFT JOIN (SELECT AF.feature_id, AFP.value FROM analysisfeatureprop AFP
  66. INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id
  67. WHERE type_id = (SELECT cvterm_id FROM cvterm
  68. WHERE name = 'analysis_kegg_output_keywords'
  69. AND cv_id = (SELECT cv_id FROM cv WHERE name = 'tripal')
  70. ) GROUP BY feature_id, value
  71. ) KEGG ON KEGG.feature_id = F.feature_id
  72. LEFT JOIN (SELECT AF.feature_id, AFP.value FROM analysisfeatureprop AFP
  73. INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id
  74. WHERE type_id = (SELECT cvterm_id FROM cvterm
  75. WHERE name = 'analysis_interpro_output_keywords'
  76. AND cv_id = (SELECT cv_id FROM cv WHERE name = 'tripal')
  77. ) GROUP BY feature_id, value
  78. ) INTERPRO ON INTERPRO.feature_id = F.feature_id
  79. LEFT JOIN (SELECT * FROM analysisfeature AF
  80. INNER JOIN analysis A ON A.analysis_id = AF.analysis_id
  81. INNER JOIN analysisprop AP ON AF.analysis_id = AP.analysis_id
  82. WHERE AP.type_id = (SELECT cvterm_id FROM cvterm
  83. WHERE name = 'analysis_type'
  84. AND cv_id = (SELECT cv_id FROM cv WHERE name = 'tripal')
  85. )
  86. AND value = 'tripal_analysis_unigene') UNIGENE ON F.feature_id = UNIGENE.feature_id
  87. WHERE F.type_id != (SELECT cvterm_id FROM cvterm WHERE name = 'EST_match') $singlet_condition
  88. ) T";
  89. // Create the MView
  90. tripal_add_mview(
  91. // view name
  92. $view_name,
  93. // tripal module name
  94. ' tripal_search_unigene',
  95. // table name
  96. $view_name,
  97. // table schema definition
  98. $schema,
  99. // columns for indexing
  100. $index,
  101. // SQL statement to populate the view
  102. $sql,
  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_search_unigene_uninstall(){
  117. $view_name = 'unigene_for_search';
  118. // Drop the MView table if it exists
  119. $mview_id = tripal_mviews_get_mview_id($view_name);
  120. if($mview_id){
  121. tripal_mviews_action("delete",$mview_id);
  122. }
  123. // Drop the sequence
  124. $sql = "DROP SEQUENCE IF EXISTS unigene_for_search_id";
  125. chado_query($sql);
  126. }
  127. /*******************************************************************************
  128. * Implementation of hook_requirements(). Make sure 'Tripal Core' is enabled
  129. * before installation
  130. */
  131. function tripal_search_unigene_requirements($phase) {
  132. $requirements = array();
  133. if ($phase == 'install') {
  134. if (!function_exists('tripal_create_moddir')) {
  135. $requirements ['tripal_search_unigene'] = array(
  136. 'title' => "tripal_search_unigene",
  137. 'value' => "error. Some required modules are just being installed. Please try again.",
  138. 'severity' => REQUIREMENT_ERROR,
  139. );
  140. }
  141. }
  142. return $requirements;
  143. }