tripal_analysis_unigene.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. tripal_analysis_unigene_add_stat_mview(); // Add statistics mview
  26. }
  27. /**
  28. *
  29. */
  30. function tripal_analysis_unigene_update_6001(){
  31. // we have some new cvterms to add
  32. tripal_analysis_unigene_add_cvterms();
  33. // remove the old unigene materialized view and add the new one.
  34. $mview = tripal_mviews_get_mview_id('unigene_mview');
  35. if($mview){
  36. tripal_mviews_action('delete',$mview);
  37. }
  38. tripal_analysis_unigene_add_organism_unigene_mview();
  39. tripal_analysis_unigene_add_stat_mview(); // Add statistics mview
  40. // next, add the cvterm 'analysis_unigene_name' to all existing
  41. // unigenes that are already in the database. This will allow the
  42. // materialized view to work.
  43. $sql = "SELECT N.nid,N.title,CN.analysis_id
  44. FROM {node} N
  45. INNER JOIN {chado_analysis} CN on CN.nid = N.nid
  46. WHERE type = 'chado_analysis_unigene'";
  47. $analyses = db_query($sql);
  48. while($analysis = db_fetch_object($analyses)){
  49. tripal_analysis_insert_property($analysis->analysis_id,'analysis_unigene_name',$analysis->title);
  50. }
  51. $ret = array(
  52. '#finished' => 1,
  53. );
  54. return $ret;
  55. }
  56. /**
  57. *
  58. */
  59. function tripal_analysis_unigene_add_cvterms(){
  60. tripal_add_cvterms('unigene_version','The version number for the unigene ".
  61. "(e.g. v1, v2, etc...) ');
  62. tripal_add_cvterms('analysis_unigene_name', 'The name for a unigene.');
  63. tripal_add_cvterms('analysis_unigene_num_contigs','The number of contigs in the unigene assembly');
  64. tripal_add_cvterms('analysis_unigene_num_singlets','The number of singlets remaining in the unigene assembly');
  65. tripal_add_cvterms('analysis_unigene_num_clusters','The number of clusters in the unigene assembly');
  66. tripal_add_cvterms('analysis_unigene_num_reads','The number of reads, after filtering, used as input for the assembly');
  67. tripal_add_cvterms('analysis_unigene_avg_length','The average contig length');
  68. tripal_add_cvterms('singlet',"Indicates the feature is a singlet in a ".
  69. "specific unigene version (e.g. v1, v2, etc...). The value specified ".
  70. "should match that of the unigene_version");
  71. // Add cveterm 'analysis_unigene_settings' for inserting into analysisprop table
  72. tripal_add_cvterms('analysis_unigene_settings', 'Settings of a unigene analysis');
  73. tripal_add_cvterms('singlet_in_analysis', 'Label the feature as a singlet of specified analysis. The value is the name of a unigene analysis. More importantly, the analysis_id of said analysis is inserted to the featureprop table as rank');
  74. }
  75. /**
  76. *
  77. */
  78. function tripal_analysis_unigene_add_organism_unigene_mview(){
  79. $view_name = 'organism_unigene_mview';
  80. // Drop the MView table if it exists
  81. $mview_id = tripal_mviews_get_mview_id($view_name);
  82. if($mview_id){
  83. tripal_mviews_action("delete",$mview_id);
  84. }
  85. tripal_add_mview(
  86. // view name
  87. $view_name,
  88. // tripal module name
  89. 'tripal_analysis_unigene',
  90. // table name
  91. $view_name,
  92. // table schema definition
  93. 'analysis_id integer, organism_id integer',
  94. // columns for indexing
  95. 'analysis_id, organism_id',
  96. // SQL statement to populate the view
  97. "SELECT DISTINCT A.analysis_id, O.organism_id ".
  98. "FROM {Analysis} A ".
  99. " INNER JOIN analysisprop AP ON AP.analysis_id = A.analysis_id ".
  100. " INNER JOIN cvterm CVT ON AP.type_id = CVT.cvterm_id ".
  101. " INNER JOIN cv CV ON CV.cv_id = CVT.cv_id ".
  102. " INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id ".
  103. " INNER JOIN feature F ON AF.feature_id = F.feature_id ".
  104. " INNER JOIN organism O ON F.organism_id = O.organism_id ".
  105. "WHERE CV.name = 'tripal' AND CVT.name='analysis_unigene_name'",
  106. // special index
  107. ''
  108. );
  109. // add a job to the job queue so this view gets updated automatically next
  110. // time the job facility is run
  111. $mview_id = tripal_mviews_get_mview_id($view_name);
  112. if($mview_id){
  113. tripal_mviews_action('update',$mview_id);
  114. }
  115. }
  116. /*******************************************************************************
  117. * Implementation of hook_uninstall().
  118. */
  119. function tripal_analysis_unigene_uninstall() {
  120. $mview = tripal_mviews_get_mview_id('unigene_mview');
  121. if($mview){
  122. tripal_mviews_action('delete',$mview);
  123. }
  124. $mview = tripal_mviews_get_mview_id('unigene_libraries_mview');
  125. if($mview){
  126. tripal_mviews_action('delete',$mview);
  127. }
  128. // Delete the settings from {tripal_analysis} table
  129. // Drupal complains when the user tries to uninstall tripal_analysis
  130. // and tripal_analysis_unigene at the same time. This is because Drupal drops
  131. // the {tripal_analysis} table before we can delete anything from it. Thus,
  132. // we perform a db_table_exists() check before the deletion
  133. tripal_analysis_unregister_child('tripal_analysis_unigene');
  134. // Delete module's variables from variables table.
  135. db_query("DELETE FROM {variable} WHERE name='%s'",
  136. 'tripal_analysis_unigene_setting');
  137. }
  138. /*******************************************************************************
  139. * Implementation of hook_requirements(). Make sure 'Tripal Core' and 'Tripal
  140. * Analysis' are enabled before installation
  141. */
  142. function tripal_analysis_unigene_requirements($phase) {
  143. $requirements = array();
  144. if ($phase == 'install') {
  145. if (!function_exists('tripal_create_moddir') || !function_exists('tripal_analysis_register_child')) {
  146. $requirements ['tripal_analysis_unigene'] = array(
  147. 'title' => "tripal_analysis_unigene",
  148. 'value' => "error. Some required modules are just being installed. Please try again.",
  149. 'severity' => REQUIREMENT_ERROR,
  150. );
  151. }
  152. }
  153. return $requirements;
  154. }
  155. /********************************************************************************
  156. * Add a materialized view for unigene basic stat
  157. */
  158. function tripal_analysis_unigene_add_stat_mview() {
  159. $view_name = 'unigene_basic_stats';
  160. // Drop the MView table if it exists
  161. $mview_id = tripal_mviews_get_mview_id($view_name);
  162. if($mview_id){
  163. tripal_mviews_action("delete",$mview_id);
  164. }
  165. $schema = " analysis_id integer,
  166. name character varying(255),
  167. unigene_no integer,
  168. unigene_min_length integer,
  169. unigene_max_length integer,
  170. unigene_avg_length integer,
  171. contig_no integer,
  172. contig_min_length integer,
  173. contig_max_length integer,
  174. contig_avg_length integer,
  175. singlet_no integer,
  176. singlet_min_length integer,
  177. singlet_max_length integer,
  178. singlet_avg_length integer
  179. ";
  180. $index = "analysis_id";
  181. $ana_type = tripal_get_cvterm_id('analysis_type');
  182. $singlet_type_id = tripal_get_cvterm_id('singlet_in_analysis');
  183. $contig_type = db_result(chado_query("SELECT cvterm_id FROM {cvterm} WHERE name = 'contig' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'sequence')")); //P.S. 'contig' is not a tripal term
  184. $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
  185. $sql = "SELECT
  186. A.analysis_id, name,
  187. coalesce(unigene_no, 0),
  188. coalesce(unigene_min, 0),
  189. coalesce(unigene_max, 0),
  190. coalesce(unigene_avg, 0),
  191. coalesce(contig_no, 0),
  192. coalesce(contig_min, 0),
  193. coalesce(contig_max, 0),
  194. coalesce(contig_avg, 0),
  195. coalesce(singlet_no, 0),
  196. coalesce(singlet_min, 0),
  197. coalesce(singlet_max, 0),
  198. coalesce(singlet_avg, 0)
  199. FROM analysis A
  200. INNER JOIN analysisprop AP ON A.analysis_id = AP.analysis_id
  201. LEFT JOIN (SELECT count (F.feature_id) AS unigene_no,
  202. min(F.seqlen) AS unigene_min,
  203. max(F.seqlen) AS unigene_max,
  204. round(avg(F.seqlen),0) AS unigene_avg,
  205. analysis_id
  206. FROM feature F
  207. INNER JOIN analysisfeature AF ON F.feature_id = AF.feature_id
  208. LEFT JOIN featureprop FP on F.feature_id = FP.feature_id
  209. WHERE F.type_id = $contig_type
  210. AND ((FP.type_id = $singlet_type_id AND FP.rank != AF.analysis_id) OR FP.type_id IS NULL)
  211. OR (FP.type_id = $singlet_type_id AND FP.rank = AF.analysis_id)
  212. GROUP BY analysis_id) Unigene ON Unigene.analysis_id = A.analysis_id
  213. LEFT JOIN (SELECT count (F.feature_id) AS contig_no,
  214. min(F.seqlen) AS contig_min,
  215. max(F.seqlen) AS contig_max,
  216. round(avg(F.seqlen),0) AS contig_avg,
  217. analysis_id
  218. FROM feature F
  219. INNER JOIN analysisfeature AF ON F.feature_id = AF.feature_id
  220. LEFT JOIN featureprop FP on F.feature_id = FP.feature_id
  221. WHERE F.type_id = $contig_type
  222. AND ((FP.type_id = $singlet_type_id AND FP.rank != AF.analysis_id) OR FP.type_id IS NULL)
  223. GROUP BY analysis_id) Contig ON Contig.analysis_id = A.analysis_id
  224. LEFT JOIN (SELECT count (F.feature_id) AS singlet_no,
  225. min(F.seqlen) AS singlet_min,
  226. max(F.seqlen) AS singlet_max,
  227. round(avg(F.seqlen),0) AS singlet_avg,
  228. analysis_id
  229. FROM featureprop FP
  230. INNER JOIN feature F ON F.feature_id = FP.feature_id
  231. INNER JOIN analysisfeature AF ON FP.feature_id = AF.feature_id
  232. WHERE FP.type_id = $singlet_type_id AND FP.rank = AF.analysis_id
  233. GROUP BY analysis_id) Singlet ON Singlet.analysis_id = A.analysis_id
  234. WHERE type_id = $ana_type AND value = 'tripal_analysis_unigene'";
  235. // Create the MView
  236. tripal_add_mview(
  237. // view name
  238. $view_name,
  239. // tripal module name
  240. ' tripal_analysis_unigene',
  241. // table name
  242. $view_name,
  243. // table schema definition
  244. $schema,
  245. // columns for indexing
  246. $index,
  247. // SQL statement to populate the view
  248. $sql,
  249. // special index
  250. ''
  251. );
  252. // add a job to the job queue so this view gets updated automatically next
  253. // time the job facility is run
  254. $mview_id = tripal_mviews_get_mview_id($view_name);
  255. if($mview_id){
  256. tripal_mviews_action('update',$mview_id);
  257. }
  258. }