First complete Projects/Tasks build.
[htsworkflow.git] / htswanalysis / scripts / SummarizeProject2.pm
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use XML::Simple;
5 my $root_dir = shift;
6 my $library_info = shift;
7 my %libs; 
8 my %quest;
9 my $expxml = XML::Simple->new();
10 my $xml = $expxml->XMLin("Project.xml", ForceArray => [ qw(ComparePeakCalls CompareLibraries PeakCalling ProfileReads qPCR MotifFinding)]);
11 my @peak_calling = ();
12
13 my $project_name = $xml->{Name};
14 my $project_id= $xml->{ProjectId};
15
16 if(exists($xml->{PeakCalling})) {
17 for my $i (0..scalar(@{$xml->{PeakCalling}})-1) {
18   my $task = $xml->{PeakCalling}->[$i]->{TaskId};
19   my $name = $xml->{PeakCalling}->[$i]->{Name};
20   my $caller = $xml->{PeakCalling}->[$i]->{Caller};
21   my $genome = $xml->{PeakCalling}->[$i]->{Genome};
22   my $signal = $xml->{PeakCalling}->[$i]->{Signal}->{Library};
23   my $bg = $xml->{PeakCalling}->[$i]->{Background}->{Library};
24   $libs{$signal} = 0; $libs{$bg} = 0;
25   $name =~ s/\s/_/ig;
26   my $caller_dir = "../../Tasks/$task";
27   my %desc = (Name => $name, Dir => $caller_dir, Caller => $caller);
28   $desc{outfile} = "";
29   $desc{fasta} = "";
30   if($caller eq "QuEST") {
31     $desc{Summary} = `$root_dir/scripts/SummarizeQuEST.pm $caller_dir`;
32     $desc{outfile} = "$caller_dir/peak_caller.ChIP.out.bedgraph";
33     $desc{fasta} = "$caller_dir/peak_caller.ChIP.out.fasta";
34   } elsif($caller eq "WingPeaks") {
35     $desc{Summary} = `$root_dir/scripts/SummarizeWingPeaks.pm $caller_dir/$name.peaks`;
36     $desc{outfile} = "$caller_dir/$name.peaks.bed";
37     $desc{fasta} = "$caller_dir/$name.peaks.fasta";
38   } elsif($caller eq "MACS") {
39     my $peakfile = $caller_dir.'/'.$name.'_peaks.xls';
40     my $negpeakfile = $caller_dir.'/'.$name.'_negative_peaks.xls';
41     $desc{Summary} = `$root_dir/scripts/SummarizeMACS.pm $peakfile $negpeakfile`;
42     $desc{outfile} = "$caller_dir/".$name."_peaks.bed";
43     $desc{fasta} = "$caller_dir/".$name."_peaks.fasta";
44   }
45
46   if($genome eq "scer") { $genome = "sacCer1"; }
47   $desc{Genome} = $genome;
48   $desc{Task} = $task;
49   push @peak_calling, \%desc;
50 }
51 }
52
53 my $qPCR_Summary = "";
54 if(exists($xml->{qPCR})) {
55 $qPCR_Summary .= "<H2><EM>In-silico</EM> qPCR Summary</H2><BR><TABLE BORDER=1><TR><TD><EM>Library</EM></TD><TD><EM>Factor</EM></TD><TD><EM>Fold Enrichment</EM></TD></TR>\n";
56 for my $i (0..scalar(@{$xml->{qPCR}})-1) {
57   my $task = $xml->{qPCR}->[$i]->{TaskId}; 
58   my $name = $xml->{qPCR}->[$i]->{Name}; $name =~ s/\s/_/g;
59   my $genome = $xml->{qPCR}->[$i]->{Genome};
60   my $lib = $xml->{qPCR}->[$i]->{Library};
61   my $outfile = "../../Tasks/$task/$name.qPCR";
62
63   $libs{$lib} = 0;
64  
65   if( ! -e $outfile ) {  $qPCR_Summary .= "<TR BCOLOR=#FFBBBB><TD>$name</TD><TD>Processing...</TD></TR>\n"; } else {
66     my $summary_line = `$root_dir/scripts/Summarize_qPCR.pm $name $lib $outfile`;
67     if($summary_line eq "") { $qPCR_Summary .= "<TR BCOLOR=#FFBBBB><TD>$name</TD><TD>Processing...</TD></TR>\n"; }
68     else {
69       $qPCR_Summary .= $summary_line; 
70     }
71   }
72 }
73 $qPCR_Summary .= "</TABLE>\n";
74 }
75
76 my $profile_summary = "";
77 if(exists($xml->{ProfileReads})) {
78   $profile_summary = "<BR><H2>Read Profiles (<A HREF=http://genome.ucsc.edu/cgi-bin/hgGateway>Genome Browser</A>)</H2>\n";
79   $profile_summary .= "<TABLE BORDER=1><TR><TD><EM>Library</EM></TD><TD><EM>Wiggle file</EM></TD></TR>";
80   for my $i (0..scalar(@{$xml->{ProfileReads}})-1) {
81     my $task = $xml->{ProfileReads}->[$i]->{TaskId};
82     my $name = $xml->{ProfileReads}->[$i]->{Name};
83     my $genome = $xml->{ProfileReads}->[$i]->{Genome};
84     my $lib = $xml->{ProfileReads}->[$i]->{Library};
85     $libs{$lib} = 0;
86
87     if($genome eq "scer") { $genome = "sacCer1"; }
88     $profile_summary .= "<TR><TD>$name ($lib)</TD><TD><A HREF=../../Tasks/$task/$lib.wig.gz>$lib.wig.gz</A><BR><A HREF=http://genome.ucsc.edu/cgi-bin/hgTracks?db=$genome&position=chr1:1000-2000&hgt.customText=http://171.65.76.194/Tasks/$task/$lib.wig.gz TARGET=\"_blank\">View in UCSC Genome Browser</A></TD>\n";
89     $profile_summary .= "<TD><A HREF=../../Tasks/$task/$lib.profile.gif><IMG WIDTH=300 SRC=../../Tasks/$task/$lib.profile.gif></A></TD></TR>\n";
90   }
91   $profile_summary .= "</TABLE>\n";
92 }
93
94 my $library_comparisons = "";
95 if(exists($xml->{CompareLibraries})) {
96 $library_comparisons .= "<H2>Library Comparisons</H2>\n";
97 $library_comparisons .= "<TABLE BORDER=1>\n";
98 $library_comparisons .= "<TR><TD><EM>Factor</EM></TD><TD><EM>Library 1</EM></TD><TD><EM>Library 2</EM></TD><TD><EM>Correlation</EM></TD></TR>\n";
99 for my $i (0..scalar(@{$xml->{CompareLibraries}})-1) {
100   my $task = $xml->{CompareLibraries}->[$i]->{TaskId};
101   my $tf = $xml->{CompareLibraries}->[$i]->{TF};
102   my $genome = $xml->{CompareLibraries}->[$i]->{Genome};
103   my $features = "$root_dir/reference_data/".$genome."_uptream5k_downtream1k";
104   my $name1 = $xml->{CompareLibraries}->[$i]->{Library}->[0]->{Library};
105   my $name2 = $xml->{CompareLibraries}->[$i]->{Library}->[1]->{Library};
106   my $outfile = "../../Tasks/".$task.'/'.$name1."_".$name2.".compare";
107   $libs{$name1} = 0; $libs{$name2} = 0;
108   my $correlation;
109   if( !(-e $outfile) ) { $correlation = "In Progress..."; $color = "#FFBBBB"; } 
110   else {
111     if(-e $outfile) { $correlation = `cat $outfile | awk '{print \$8}'`; }
112     my $color; 
113     elsif($correlation > 0.9) { $color = "#BBFFBB"; }
114     elsif($correlation > 0.6) { $color = "#BBBBFF"; }
115     else { $color = "#FFBBBB"; }
116   }
117   $library_comparisons .= "<TR BGCOLOR=$color><TD>$tf</TD><TD>$name1</TD><TD>$name2</TD<TD>$correlation</TD></TR>\n";
118 }
119 $library_comparisons .= "</TABLE>\n";
120 }
121
122 my $peak_call_comparisons = "";
123 if(exists($xml->{ComparePeakCalls})) {
124   $peak_call_comparisons = "<H2>Peak Calling Comparisons</H2>\n";
125   $peak_call_comparisons .= "<TABLE BORDER=1><TR><TD><EM>Caller1</EM></TD><TD><EM>Set1</EM></TD><TD><EM>Caller2</EM></TD><TD><EM>Set2</EM></TD><TD>|Union|</TD><TD>|Intersect|</TD><TD>|Difference|</TD><TD>Jaccard</TD><TD>Correlation</TD></TR>\n";
126   for my $i (0..scalar(@{$xml->{ComparePeakCalls}})-1) {
127     my $task = $xml->{ComparePeakCalls}->[$i]->{TaskId};
128     my $caller1 = $xml->{ComparePeakCalls}->[$i]->{Caller1};
129     my $caller2 = $xml->{ComparePeakCalls}->[$i]->{Caller2};
130     my $genome = $xml->{ComparePeakCalls}->[$i]->{Genome};
131     my $set1 = $xml->{ComparePeakCalls}->[$i]->{Set1}; $set1 =~ s/\s/_/g;
132     my $set2 = $xml->{ComparePeakCalls}->[$i]->{Set2}; $set2 =~ s/\s/_/g;
133     my $outfile .= "../../Tasks/".$task.'/'.$set1."-".$caller1."_v_".$set2."-".$caller2.".compare";
134     my $correlation = -1;
135
136     if(! -e $outfile) {
137       my $color = "#FFBBBB";
138       $peak_call_comparisons .= "<TR BGCOLOR=$color><TD>$caller1</TD><TD>$set1</TD><TD>$caller2</TD><TD>$set2</TD><TD COLSPAN=5><B>Processing...</B></TD></TR>\n";
139     } else {
140       my $line = `cat $outfile`; chomp $line;
141       my($tf,$set1_id,$set2_id,$U,$I,$D,$J,$R) = split(/\t/,$line);
142       my $color = "#FFBBBB";
143       if(!defined($R)) { $peak_call_comparisons .= "<TR BGCOLOR=$color><TD>$caller1</TD><TD>$set1</TD><TD>$caller2</TD><TD>$set2</TD><TD COLSPAN=5><B>Processing...</B></TD></TR>\n"; } else {
144         $set1_id =~ /.+?\((\d+)\)$/; my $n1 = $1;
145         $set2_id =~ /.+?\((\d+)\)$/; my $n2 = $1;
146         if($R > 0.85) { $color = "#BBFFBB"; } elsif($R > 0.5) { $color="#BBBBFF"; } else { $color="#FFBBBB"; }
147         $peak_call_comparisons .= "<TR BGCOLOR=$color><TD>$caller1</TD><TD>$set1 ($n1)</TD><TD>$caller2</TD><TD>$set2 ($n2)</TD><TD>$U</TD><TD>$I</TD><TD>$D</TD><TD>$J</TD><TD>$R</TD></TR>\n";
148       }
149     }
150   }
151   $peak_call_comparisons .= "</TABLE>\n";
152 }
153
154 my $motif_finding = "";
155 if(exists($xml->{MotifFinding})) {
156   $motif_finding = "<H2>Motif Finding</H2>\n";
157   $motif_finding .= "<TABLE BORDER=1><TR><TD><EM>Caller</EM></TD><TD><EM>Set</EM></TD><TD><EM>Width</EM></TD><TD>Motifs</TD><TD><EM>Motif Information</EM></TD></TR>\n";
158   for my $i (0..scalar(@{$xml->{MotifFinding}})-1) {
159     my $task = $xml->{MotifFinding}->[$i]->{TaskId};
160     my $caller = $xml->{MotifFinding}->[$i]->{Caller};
161     my $genome = $xml->{MotifFinding}->[$i]->{Genome};
162     my $set = $xml->{MotifFinding}->[$i]->{Set}; $set =~ s/\s/_/g;
163     my $width = $xml->{MotifFinding}->[$i]->{Width};
164     my $outfile .= "../../Tasks/".$task.'/'.$set."-".$caller.".w".$width.".biop";
165
166     if(! -e $outfile) {
167       my $color = "#FFBBBB";
168       $motif_finding .= "<TR BGCOLOR=$color><TD>$caller</TD><TD>$set</TD><TD COLSPAN=3><B>Processing...</B></TD></TR>\n";
169     } else {
170       my $color = "#BBFFBB";
171       my $line = `cat $outfile | grep "Motif #"`; $line =~ s/\n/<BR>/g;
172       my $score = `cat $outfile | grep "MotifScore"`; $score=~ s/\n/<BR>/g;
173       $motif_finding .= "<TR BGCOLOR=$color><TD>$caller</TD><TD>$set</TD><TD>$width</TD><TD>$line</TD><TD>$score</TD></TR>\n"; 
174     }
175   }
176   $motif_finding .= "</TABLE>\n";
177 }
178
179 print "<HTML><HEAD><TITLE>$project_name Analysis Summary</TITLE></HEAD><BODY>\n";
180 print "<H1><CENTER>$project_name Analysis Summary</CENTER></H1>\n";
181 print SummarizeLibrary($library_info);
182 print $profile_summary;
183 print $library_comparisons;
184 print $peak_call_comparisons;
185 print $motif_finding;
186 print $qPCR_Summary;
187
188 if(exists($xml->{PeakCalling})) {
189 print "<H2>Peak Calling Summary</H2>";
190 print "<TABLE BORDER=1><TR><TD><EM>Experiment</EM></TD><TD><EM>Peak Caller</EM></TD><TD><EM>Summary</EM></TD></TR>\n";
191 my $row = 0;
192 for(@peak_calling) {
193   my %hash = %{$_};
194   $row++;
195   #my $color = $row % 2?"#CCCCFF":"#FFFFFF";
196   my $color = "#EEEEFF";
197   print "<TR BGCOLOR=$color><TD><B>$hash{Name}</B></TD>";
198   print "<TD>$hash{Caller}</TD><TD>$hash{Summary}</TD>\n";
199   print "<TD><A HREF=$hash{outfile}>BED file</A><BR><A HREF=http://genome.ucsc.edu/cgi-bin/hgTracks?db=$hash{Genome}&hgt.customText=http://171.65.76.194/Tasks/$hash{Task}/$hash{outfile}>View in Genome Browser</A></TD><TD><A HREF=$hash{fasta}>FASTA</A></TD>\n";
200   print "</TR>\n";
201 }
202 }
203 print "</TABLE>\n";
204 print "</BODY></HTML>\n";
205 sub SummarizeLibrary {
206   my $xml_file = shift;
207   my $xml = XMLin($xml_file, ForceArray => 1);
208   my $summary = "";
209   $summary .= "<H2>Libraries Used</H2><TABLE BORDER=1><TR><TD><EM>Library</EM></TD><TD><EM>Name</EM></TD><TD><EM>Num Lanes</EM></TD><TD><EM>Num Reads</EM></TD></TR>\n";
210   for my $i (0..scalar(@{$xml->{Library}})-1) {
211     my $lib = $xml->{Library}->[$i]->{Name};
212     next unless(scalar(keys %libs) == 0 || exists($libs{$lib}));
213     my $N = scalar(@{$xml->{Library}->[$i]->{Track}});
214     my($date,$fc,$lane,$desc);
215     my $num_reads = 0;
216     my $num_lanes = 0;
217     for my $t (0..$N-1) {
218       my $filename = $xml->{Library}->[$i]->{Track}->[$t]->{Filename};
219       $filename = `basename $filename`; 
220       $filename =~ /^(\d+)_(.+?)_s(\d+)_(.+?)_$lib.align/;
221       ($date,$fc,$lane,$desc) = ($1,$2,$3,$4);
222       $num_lanes += length($lane);
223       $num_reads += $xml->{Library}->[$i]->{Track}->[$t]->{Count};
224     }
225     my $bgcolor;
226     if($num_reads < 3000000) { $bgcolor = "FF3300"; }
227     elsif($num_reads < 5000000) { $bgcolor = "FFCC33"; }
228     elsif($num_reads < 10000000) { $bgcolor = "00CCFF"; }
229     else { $bgcolor = "66FF66"; }
230     
231     $summary .= sprintf("<TR BGCOLOR=#%s><TD>%s</TD><TD>%s</TD><TD>%d</TD><TD>%0.2fM</TD></TR>\n",$bgcolor,$lib,$desc,$num_lanes,$num_reads/1000000.0);
232   }
233   $summary .= "</TABLE>\n";
234   return $summary;
235 }
236