Mercurial > repos > geert-vandeweyer > coverage_report
annotate CoverageReport.pl @ 25:6cb012c8497a draft
Added BED format check before collapsing regions.
author | geert-vandeweyer |
---|---|
date | Thu, 12 Feb 2015 09:54:03 -0500 |
parents | fd788f9db899 |
children | 859999cb135b |
rev | line source |
---|---|
1 | 1 #!/usr/bin/perl |
2 | |
3 # load modules | |
4 use Getopt::Std; | |
5 use File::Basename; | |
6 use Number::Format; | |
7 | |
8 # number format | |
9 my $de = new Number::Format(-thousands_sep =>',',-decimal_point => '.'); | |
10 | |
11 ########## | |
12 ## opts ## | |
13 ########## | |
14 ## input files | |
15 # b : path to input (b)am file | |
16 # t : path to input (t)arget regions in BED format | |
17 ## output files | |
18 # o : report pdf (o)utput file | |
19 # z : all plots and tables in tar.g(z) format | |
20 ## entries in the report | |
21 # r : Coverage per (r)egion (boolean) | |
22 # s : (s)ubregion coverage if average < specified (plots for positions along target region) (boolean) | |
23 # S : (S)ubregion coverage for ALL failed exons => use either s OR S or you will have double plots. | |
24 # A : (A)ll exons will be plotted. | |
25 # L : (L)ist failed exons instead of plotting | |
26 # m : (m)inimal Coverage threshold | |
27 # f : fraction of average as threshold | |
28 # n : sample (n)ame. | |
29 | |
30 | |
24
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
31 getopts('b:t:o:z:rsSALm:n:f:T', \%opts) ; |
1 | 32 |
33 # make output directory in (tmp) working dir | |
34 our $wd = "/tmp/Coverage.".int(rand(1000)); | |
35 while (-d $wd) { | |
36 $wd = "/tmp/Coverage.".int(rand(1000)); | |
24
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
37 |
1 | 38 } |
39 system("mkdir $wd"); | |
40 | |
41 ## variables | |
42 our %commandsrun = (); | |
43 | |
44 if (!exists($opts{'b'}) || !-e $opts{'b'}) { | |
45 die('Bam File not found'); | |
46 } | |
47 if (!exists($opts{'t'}) || !-e $opts{'t'}) { | |
48 die('Target File (BED) not found'); | |
49 } | |
50 | |
51 if (exists($opts{'m'})) { | |
52 $thresh = $opts{'m'}; | |
53 } | |
54 else { | |
55 $thresh = 40; | |
56 } | |
57 | |
58 if (exists($opts{'f'})) { | |
59 $frac = $opts{'f'}; | |
60 } | |
61 else { | |
62 $frac = 0.2; | |
63 } | |
64 | |
65 if (exists($opts{'o'})) { | |
66 $pdffile = $opts{'o'}; | |
67 } | |
68 else { | |
69 $pdffile = "$wd/CoverageReport.pdf"; | |
70 } | |
71 | |
72 if (exists($opts{'z'})) { | |
73 $tarfile = $opts{'z'}; | |
74 } | |
75 else { | |
76 $tarfile = "$wd/Results.tar.gz"; | |
77 } | |
78 | |
24
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
79 ## 0. Collapse overlapping target regions. |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
80 if (defined($opts{'T'})) { |
25
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
81 ## check BED format. Must have 6 cols if using this. |
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
82 my $head = `head -n 1 $opts{'t'}`; |
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
83 chomp; |
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
84 my @c = split(/\t/,$head); |
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
85 if (scalar(@c) < 6) { |
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
86 die("Targets BED file must be in 6-column format for collapsings. See tool documentation for more info.\n"); |
6cb012c8497a
Added BED format check before collapsing regions.
geert-vandeweyer
parents:
24
diff
changeset
|
87 } |
24
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
88 my $targets = $opts{'t'}; |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
89 my $tmptargets = "$wd/collapsedtargets.bed"; |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
90 system("sort -k1,1 -k2,2n $targets > $wd/sorted.targets.bed"); |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
91 system("bedtools merge -s -scores max -nms -i $wd/sorted.targets.bed > $tmptargets"); |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
92 $opts{'t'} = $tmptargets; |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
93 } |
fd788f9db899
Added (default) option to collapse repetitive bed files
geert-vandeweyer
parents:
22
diff
changeset
|
94 |
1 | 95 # 1. Global Summary => default |
96 &GlobalSummary($opts{'b'}, $opts{'t'}); | |
97 | |
98 # 2. Coverage per position | |
99 &SubRegionCoverage($opts{'b'}, $opts{'t'}); | |
100 our %filehash; | |
101 if (exists($opts{'s'}) || exists($opts{'S'}) || exists($opts{'A'}) || exists($opts{'L'})) { | |
102 system("mkdir $wd/SplitFiles"); | |
103 ## get position coverages | |
104 ## split input files | |
105 open IN, "$wd/Targets.Position.Coverage"; | |
106 my $fileidx = 0; | |
107 my $currreg = ''; | |
108 while (<IN>) { | |
109 my $line = $_; | |
110 chomp($line); | |
111 my @p = split(/\t/,$line); | |
112 my $reg = $p[0].'-'.$p[1].'-'.$p[2]; #.$p[3]; | |
113 my $ex = $p[3]; | |
114 if ($reg ne $currreg) { | |
115 ## new exon open new outfile | |
116 if ($currreg ne '') { | |
117 ## filehandle is open. close it | |
118 close OUT; | |
119 } | |
120 if (!exists($filehash{$reg})) { | |
121 $fileidx++; | |
122 $filehash{$reg}{'idx'} = $fileidx; | |
123 $filehash{$reg}{'exon'} = $ex; | |
124 open OUT, ">> $wd/SplitFiles/File_$fileidx.txt"; | |
125 $currreg = $reg; | |
126 } | |
127 else { | |
128 open OUT, ">> $wd/SplitFiles/File_".$filehash{$reg}{'idx'}.".txt"; | |
129 $currreg = $reg; | |
130 } | |
131 } | |
132 ## print the line to the open filehandle. | |
133 print OUT "$line\n"; | |
134 } | |
135 close OUT; | |
136 close IN; | |
137 | |
138 } | |
139 | |
140 ## sort output files according to targets file | |
141 if (exists($opts{'r'}) ) { | |
142 my %hash = (); | |
143 open IN, "$wd/Targets.Global.Coverage"; | |
144 while (<IN>) { | |
145 my @p = split(/\t/,$_) ; | |
146 $hash{$p[3]} = $_; | |
147 } | |
148 close IN; | |
149 open OUT, ">$wd/Targets.Global.Coverage"; | |
150 open IN, $opts{'t'}; | |
151 while (<IN>) { | |
152 my @p = split(/\t/,$_) ; | |
153 print OUT $hash{$p[3]}; | |
154 } | |
155 close IN; | |
156 close OUT; | |
157 } | |
158 | |
159 | |
160 #################################### | |
161 ## PROCESS RESULTS & CREATE PLOTS ## | |
162 #################################### | |
163 system("mkdir $wd/Report"); | |
164 | |
165 system("mkdir $wd/Rout"); | |
166 system("mkdir $wd/Plots"); | |
167 | |
168 $samplename = $opts{'n'}; | |
169 $samplename =~ s/_/\\_/g; | |
170 | |
171 # 0. Preamble | |
172 ## compose preamble | |
173 open OUT, ">$wd/Report/Report.tex"; | |
174 print OUT '\documentclass[a4paper,10pt]{article}'."\n"; | |
175 print OUT '\usepackage[left=2cm,top=1.5cm,right=1.5cm,bottom=2.5cm,nohead]{geometry}'."\n"; | |
176 print OUT '\usepackage{longtable}'."\n"; | |
177 print OUT '\usepackage[T1]{fontenc}'."\n"; | |
178 print OUT '\usepackage{fancyhdr}'."\n"; | |
179 print OUT '\usepackage[latin9]{inputenc}'."\n"; | |
180 print OUT '\usepackage{color}'."\n"; | |
181 print OUT '\usepackage[pdftex]{graphicx}'."\n"; | |
182 print OUT '\definecolor{grey}{RGB}{160,160,160}'."\n"; | |
183 print OUT '\definecolor{darkgrey}{RGB}{100,100,100}'."\n"; | |
184 print OUT '\definecolor{red}{RGB}{255,0,0}'."\n"; | |
185 print OUT '\definecolor{orange}{RGB}{238,118,0}'."\n"; | |
186 print OUT '\setlength\LTleft{0pt}'."\n"; | |
187 print OUT '\setlength\LTright{0pt}'."\n"; | |
188 print OUT '\begin{document}'."\n"; | |
189 print OUT '\pagestyle{fancy}'."\n"; | |
190 print OUT '\fancyhead{}'."\n"; | |
191 print OUT '\renewcommand{\footrulewidth}{0.4pt}'."\n"; | |
192 print OUT '\renewcommand{\headrulewidth}{0pt}'."\n"; | |
193 print OUT '\fancyfoot[R]{\today\hspace{2cm}\thepage\ of \pageref{endofdoc}}'."\n"; | |
194 print OUT '\fancyfoot[C]{}'."\n"; | |
195 print OUT '\fancyfoot[L]{Coverage Report for ``'.$samplename.'"}'."\n"; | |
196 print OUT '\let\oldsubsubsection=\subsubsection'."\n"; | |
197 print OUT '\renewcommand{\subsubsection}{%'."\n"; | |
198 print OUT ' \filbreak'."\n"; | |
199 print OUT ' \oldsubsubsection'."\n"; | |
200 print OUT '}'."\n"; | |
201 # main title | |
202 print OUT '\section*{Coverage Report for ``'.$samplename.'"}'."\n"; | |
203 close OUT; | |
204 | |
205 # 1. Summary Report | |
206 # Get samtools flagstat summary of BAM file | |
207 my $flagstat = `samtools flagstat $opts{'b'}`; | |
208 my @s = split(/\n/,$flagstat); | |
209 # Get number of reads mapped in total | |
210 ## updated on 2012-10-1 !! | |
211 $totalmapped = $s[2]; | |
212 $totalmapped =~ s/^(\d+)(\s.+)/$1/; | |
213 # count columns | |
214 my $head = `head -n 1 $wd/Targets.Global.Coverage`; | |
215 chomp($head); | |
216 my @cols = split(/\t/,$head); | |
217 my $nrcols = scalar(@cols); | |
218 my $covcol = $nrcols - 3; | |
219 # get min/max/median/average coverage => values | |
220 my $covs = `cut -f $covcol $wd/Targets.Global.Coverage`; | |
221 my @coverages = split(/\n/,$covs); | |
222 my ($eavg,$med,$min,$max,$first,$third,$ontarget) = arraystats(@coverages); | |
223 my $spec = sprintf("%.1f",($ontarget / $totalmapped)*100); | |
224 # get min/max/median/average coverage => boxplot in R | |
225 open OUT, ">$wd/Rout/boxplot.R"; | |
226 print OUT 'coverage <- read.table("../Targets.Global.Coverage",as.is=TRUE,sep="\t",header=FALSE)'."\n"; | |
227 print OUT 'coverage <- coverage[,'.$covcol.']'."\n"; | |
22
95062840f80f
Correction to png calls to use cairo instead of x11. thanks to Eric Enns for pointing this out.
geert-vandeweyer
parents:
12
diff
changeset
|
228 print OUT 'png(file="../Plots/CoverageBoxPlot.png", bg="white", width=240, height=480,type=c("cairo"))'."\n"; |
1 | 229 print OUT 'boxplot(coverage,range=1.5,main="Target Region Coverage")'."\n"; |
230 print OUT 'graphics.off()'."\n"; | |
231 close OUT; | |
12
86df3f847a72
Switched to R 3.0.2 from iuc, and moved bedtools to seperate tool_definition
geert-vandeweyer
parents:
11
diff
changeset
|
232 system("cd $wd/Rout && Rscript boxplot.R"); |
1 | 233 |
234 ## global nt coverage plot | |
235 ## use perl to make histogram (lower memory) | |
236 open IN, "$wd/Targets.Position.Coverage"; | |
237 my %dens; | |
238 my $counter = 0; | |
239 my $sum = 0; | |
240 while (<IN>) { | |
241 chomp(); | |
242 my @p = split(/\t/); | |
243 $sum += $p[-1]; | |
244 $counter++; | |
245 if (defined($dens{$p[-1]})) { | |
246 $dens{$p[-1]}++; | |
247 } | |
248 else { | |
249 $dens{$p[-1]} = 1; | |
250 } | |
251 } | |
252 $avg = $sum/$counter; | |
253 close IN; | |
254 open OUT, ">$wd/Rout/hist.txt"; | |
3 | 255 if (!defined($dens{'0'})) { |
256 $dens{'0'} = 0; | |
257 } | |
1 | 258 foreach (keys(%dens)) { |
259 print OUT "$_;$dens{$_}\n"; | |
260 } | |
261 close OUT; | |
262 open OUT, ">$wd/Rout/ntplot.R"; | |
263 # read coverage hist in R to plot | |
264 print OUT 'coverage <- read.table("hist.txt" , as.is = TRUE, header=FALSE,sep=";")'."\n"; | |
265 print OUT 'mincov <- '."$thresh \n"; | |
266 print OUT "avg <- round($avg)\n"; | |
267 print OUT "colnames(coverage) <- c('cov','count')\n"; | |
268 print OUT 'coverage$cov <- coverage$cov / avg'."\n"; | |
269 print OUT 'rep <- which(coverage$cov > 1)'."\n"; | |
270 print OUT 'coverage[coverage$cov > 1,1] <- 1'."\n"; | |
271 print OUT 'values <- coverage[coverage$cov < 1,]'."\n"; | |
272 print OUT 'values <- rbind(values,c(1,sum(coverage[coverage$cov == 1,"count"])))'."\n"; | |
273 print OUT 'values <- values[order(values$cov),]'."\n"; | |
274 print OUT 'prevcount <- 0'."\n"; | |
275 # make cumulative count data frame | |
276 print OUT 'for (i in rev(values$cov)) {'."\n"; | |
277 print OUT ' values[values$cov == i,"count"] <- prevcount + values[values$cov == i,"count"]'."\n"; | |
278 print OUT ' prevcount <- values[values$cov == i,"count"]'."\n"; | |
279 print OUT '}'."\n"; | |
280 print OUT 'values$count <- values$count / (values[values$cov == 0,"count"] / 100)'."\n"; | |
281 # get some values to plot lines. | |
282 print OUT 'mincov.x <- mincov/avg'."\n"; | |
283 print OUT 'if (mincov/avg <= 1) {'."\n"; | |
284 print OUT ' ii <- which(values$cov == mincov.x)'."\n"; | |
285 print OUT ' if (length(ii) == 1) {'."\n"; | |
286 print OUT ' mincov.y <- values[ii[1],"count"]'."\n"; | |
287 print OUT ' } else {'."\n"; | |
288 print OUT ' i1 <- max(which(values$cov < mincov.x))'."\n"; | |
289 print OUT ' i2 <- min(which(values$cov > mincov.x))'."\n"; | |
290 print OUT ' mincov.y <- ((values[i2,"count"] - values[i1,"count"])/(values[i2,"cov"] - values[i1,"cov"]))*(mincov.x - values[i1,"cov"]) + values[i1,"count"]'."\n"; | |
291 print OUT ' }'."\n"; | |
292 print OUT '}'."\n"; | |
293 # open output image and create plot | |
22
95062840f80f
Correction to png calls to use cairo instead of x11. thanks to Eric Enns for pointing this out.
geert-vandeweyer
parents:
12
diff
changeset
|
294 print OUT 'png(file="../Plots/CoverageNtPlot.png", bg="white", width=540, height=480,type=c("cairo"))'."\n"; |
1 | 295 print OUT 'par(xaxs="i",yaxs="i")'."\n"; |
296 print OUT 'plot(values$cov,values$count,ylim=c(0,100),pch=".",main="Cumulative Normalised Base-Coverage Plot",xlab="Normalizalised Coverage",ylab="Cumulative Nr. Of Bases")'."\n"; | |
297 print OUT 'lines(values$cov,values$count)'."\n"; | |
298 print OUT 'if (mincov.x <= 1) {'."\n"; | |
299 print OUT ' lines(c(mincov.x,mincov.x),c(0,mincov.y),lty=2,col="darkgreen")'."\n"; | |
300 print OUT ' lines(c(0,mincov.x),c(mincov.y,mincov.y),lty=2,col="darkgreen")'."\n"; | |
301 print OUT ' text(1,(95),pos=2,col="darkgreen",labels="Threshold: '.$thresh.'x")'."\n"; | |
302 print OUT ' text(1,(91),pos=2,col="darkgreen",labels=paste("%Bases: ",round(mincov.y,2),"%",sep=""))'."\n"; | |
303 print OUT '} else {'."\n"; | |
304 print OUT ' text(1,(95),pos=2,col="darkgreen",labels="Threshold ('.$thresh.'x) > Average")'."\n"; | |
305 print OUT ' text(1,(91),pos=2,col="darkgreen",labels="Plotting impossible")'."\n"; | |
306 print OUT '}'."\n"; | |
307 print OUT 'frac.x <- '."$frac\n"; | |
308 print OUT 'ii <- which(values$cov == frac.x)'."\n"; | |
309 print OUT 'if (length(ii) == 1) {'."\n"; | |
310 print OUT ' frac.y <- values[ii[1],"count"]'."\n"; | |
311 print OUT '} else {'."\n"; | |
312 print OUT ' i1 <- max(which(values$cov < frac.x))'."\n"; | |
313 print OUT ' i2 <- min(which(values$cov > frac.x))'."\n"; | |
314 print OUT ' frac.y <- ((values[i2,"count"] - values[i1,"count"])/(values[i2,"cov"] - values[i1,"cov"]))*(frac.x - values[i1,"cov"]) + values[i1,"count"]'."\n"; | |
315 print OUT '}'."\n"; | |
316 print OUT 'lines(c(frac.x,frac.x),c(0,frac.y),lty=2,col="red")'."\n"; | |
317 print OUT 'lines(c(0,frac.x),c(frac.y,frac.y),lty=2,col="red")'."\n"; | |
318 #iprint OUT 'text((frac.x+0.05),(frac.y - 2),pos=4,col="red",labels=paste(frac.x," x Avg.Cov : ",round(frac.x * avg,2),"x",sep="" ))'."\n"; | |
319 #print OUT 'text((frac.x+0.05),(frac.y-5),pos=4,col="red",labels=paste("%Bases: ",round(frac.y,2),"%",sep=""))'."\n"; | |
320 print OUT 'text(1,86,pos=2,col="red",labels=paste(frac.x," x Avg.Cov : ",round(frac.x * avg,2),"x",sep="" ))'."\n"; | |
321 print OUT 'text(1,82,pos=2,col="red",labels=paste("%Bases: ",round(frac.y,2),"%",sep=""))'."\n"; | |
322 | |
323 print OUT 'graphics.off()'."\n"; | |
324 | |
325 close OUT; | |
12
86df3f847a72
Switched to R 3.0.2 from iuc, and moved bedtools to seperate tool_definition
geert-vandeweyer
parents:
11
diff
changeset
|
326 system("cd $wd/Rout && Rscript ntplot.R"); |
1 | 327 ## PRINT TO .TEX FILE |
328 open OUT, ">>$wd/Report/Report.tex"; | |
329 # average coverage overviews | |
330 print OUT '\subsection*{Overall Summary}'."\n"; | |
331 print OUT '{\small '; | |
332 # left : boxplot | |
333 print OUT '\begin{minipage}{0.3\linewidth}\centering'."\n"; | |
334 print OUT '\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/CoverageBoxPlot.png}'."\n"; | |
335 print OUT '\end{minipage}'."\n"; | |
336 # right : cum.cov.plot | |
337 print OUT '\hspace{0.6cm}'."\n"; | |
338 print OUT '\begin{minipage}{0.65\linewidth}\centering'."\n"; | |
339 print OUT '\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/CoverageNtPlot.png}'."\n"; | |
340 print OUT '\end{minipage} \\\\'."\n"; | |
341 ## next line | |
342 print OUT '\begin{minipage}{0.48\linewidth}'."\n"; | |
343 print OUT '\vspace{-1.2em}'."\n"; | |
344 print OUT '\begin{tabular}{ll}'."\n"; | |
345 # bam statistics | |
346 print OUT '\multicolumn{2}{l}{\textbf{\underline{Samtools Flagstat Summary}}} \\\\'."\n"; | |
347 foreach (@s) { | |
348 $_ =~ m/^(\d+)\s(.+)$/; | |
349 my $one = $1; | |
350 my $two = $2; | |
351 $two =~ s/\s\+\s0\s//; | |
352 $two = ucfirst($two); | |
353 $one =~ s/%/\\%/g; | |
354 # remove '+ 0 ' from front | |
355 $two =~ s/\+\s0\s//; | |
356 # remove trailing from end | |
357 $two =~ s/(\s\+.*)|(:.*)/\)/; | |
358 $two =~ s/%/\\%/g; | |
359 $two =~ s/>=/\$\\ge\$/g; | |
360 $two = ucfirst($two); | |
361 print OUT '\textbf{'.$two.'} & '.$one.' \\\\'."\n"; | |
362 } | |
363 print OUT '\end{tabular}\end{minipage}'."\n"; | |
364 print OUT '\hspace{1.5cm}'."\n"; | |
365 # target coverage statistics | |
366 print OUT '\begin{minipage}{0.4\linewidth}'."\n"; | |
367 #print OUT '\vspace{-4.8em}'."\n"; | |
368 print OUT '\begin{tabular}{ll}'."\n"; | |
369 print OUT '\multicolumn{2}{l}{\textbf{\underline{Target Region Coverage}}} \\\\'."\n"; | |
370 print OUT '\textbf{Number of Target Regions} & '.scalar(@coverages).' \\\\'."\n"; | |
371 print OUT '\textbf{Minimal Region Coverage} & '.$min.' \\\\'."\n"; | |
372 print OUT '\textbf{25\% Region Coverage} & '.$first.' \\\\'. "\n"; | |
373 print OUT '\textbf{50\% (Median) Region Coverage} & '.$med.' \\\\'. "\n"; | |
374 print OUT '\textbf{75\% Region Coverage} & '.$third.' \\\\'. "\n"; | |
375 print OUT '\textbf{Maximal Region Coverage} & '.$max.' \\\\'. "\n"; | |
376 print OUT '\textbf{Average Region Coverage} & '.int($eavg).' \\\\'. "\n"; | |
377 print OUT '\textbf{Mapped On Target} & '.$spec.' \\\\'."\n"; | |
378 print OUT '\multicolumn{2}{l}{\textbf{\underline{Target Base Coverage }}} \\\\'."\n"; | |
379 print OUT '\textbf{Number of Target Bases} & '.$counter.' \\\\'."\n"; | |
380 print OUT '\textbf{Average Base Coverage} & '.int($avg).' \\\\'. "\n"; | |
381 print OUT '\textbf{Non-Covered Bases} & '.$dens{'0'}.' \\\\'."\n"; | |
382 #print OUT '\textbf{Bases Covered $ge$ '.$frac.'xAvg.Cov} & '. | |
383 print OUT '\end{tabular}\end{minipage}}'."\n"; | |
384 close OUT; | |
385 | |
386 # 2. GLOBAL COVERAGE OVERVIEW PER GENE | |
387 @failedexons; | |
388 @allexons; | |
389 @allregions; | |
390 @failedregions; | |
391 if (exists($opts{'r'}) || exists($opts{'s'}) || exists($opts{'S'})) { | |
392 # count columns | |
393 my $head = `head -n 1 $wd/Targets.Global.Coverage`; | |
394 chomp($head); | |
395 my @cols = split(/\t/,$head); | |
396 my $nrcols = scalar(@cols); | |
397 my $covcol = $nrcols - 3; | |
398 # Coverage Plots for each gene => barplots in R, table here. | |
399 open IN, "$wd/Targets.Global.Coverage"; | |
400 my $currgroup = ''; | |
401 my $startline = 0; | |
402 my $stopline = 0; | |
403 $linecounter = 0; | |
404 while (<IN>) { | |
405 $linecounter++; | |
406 chomp($_); | |
407 my @c = split(/\t/,$_); | |
408 push(@allregions,$c[0].'-'.$c[1].'-'.$c[2]); | |
409 my $group = $c[3]; | |
410 ## coverage failure? | |
411 if ($c[$nrcol-1] < 1 || $c[$covcol-1] < $thresh) { | |
412 push(@failedexons,$group); | |
413 push(@failedregions,$c[0].'-'.$c[1].'-'.$c[2]); | |
414 } | |
415 ## store exon | |
416 push(@allexons,$group); | |
417 ## extract and check gene | |
418 $group =~ s/^(\S+)[\|\s](.+)/$1/; | |
419 if ($group ne $currgroup ) { | |
420 if ($currgroup ne '') { | |
421 # new gene, make plot. | |
422 open OUT, ">$wd/Rout/barplot.R"; | |
423 print OUT 'coveragetable <- read.table("../Targets.Global.Coverage",as.is=TRUE,sep="\t",header=FALSE)'."\n"; | |
424 print OUT 'coverage <- coveragetable[c('.$startline.':'.$stopline.'),'.$covcol.']'."\n"; | |
425 print OUT 'entries <- coveragetable[c('.$startline.':'.$stopline.'),4]'."\n"; | |
426 print OUT 'entries <- sub("\\\\S+\\\\|","",entries,perl=TRUE)'."\n"; | |
427 print OUT 'coverage[coverage < 1] <- 1'."\n"; | |
428 print OUT 'colors <- c(rep("grey",length(coverage)))'."\n"; | |
429 # coverage not whole target region => orange | |
430 print OUT 'covperc <- coveragetable[c('.$startline.':'.$stopline.'),'.$nrcols.']'."\n"; | |
431 print OUT 'colors[covperc<1] <- "orange"'."\n"; | |
432 # coverage below threshold => red | |
433 print OUT 'colors[coverage<'.$thresh.'] <- "red"'."\n"; | |
434 | |
435 if ($stopline - $startline > 20) { | |
436 $scale = 2; | |
437 } | |
438 else { | |
439 $scale = 1; | |
440 } | |
441 my $width = 480 * $scale; | |
442 my $height = 240 * $scale; | |
22
95062840f80f
Correction to png calls to use cairo instead of x11. thanks to Eric Enns for pointing this out.
geert-vandeweyer
parents:
12
diff
changeset
|
443 print OUT 'png(file="../Plots/Coverage_'.$currgroup.'.png", bg="white", width='.$width.', height='.$height.',type=c("cairo"))'."\n"; |
1 | 444 print OUT 'ylim = c(0,max(max(log10(coverage),log10('.($thresh+20).'))))'."\n"; |
445 print OUT 'mp <- barplot(log10(coverage),col=colors,main="Exon Coverage for '.$currgroup.'",ylab="Log10(Coverage)",ylim=ylim)'."\n"; | |
446 print OUT 'text(mp, log10(coverage) + '.(0.4/$scale).',format(coverage),xpd = TRUE,srt=90)'."\n"; | |
447 print OUT 'text(mp,par("usr")[3]-0.05,labels=entries,srt=45,adj=1,xpd=TRUE)'."\n"; | |
448 print OUT 'abline(h=log10('.$thresh.'),lwd=4,col=rgb(255,0,0,100,maxColorValue=255))'."\n"; | |
449 print OUT 'graphics.off()'."\n"; | |
450 close OUT; | |
12
86df3f847a72
Switched to R 3.0.2 from iuc, and moved bedtools to seperate tool_definition
geert-vandeweyer
parents:
11
diff
changeset
|
451 system("cd $wd/Rout && Rscript barplot.R"); |
1 | 452 if ($scale == 1) { |
453 push(@small,'\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/Coverage_'.$currgroup.'.png}'); | |
454 } | |
455 else { | |
456 push(@large,'\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/Coverage_'.$currgroup.'.png}'); | |
457 } | |
458 | |
459 } | |
460 $currgroup = $group; | |
461 $startline = $linecounter; | |
462 } | |
463 $stopline = $linecounter; | |
464 } | |
465 close IN; | |
466 if ($currgroup ne '') { | |
467 # last gene, make plot. | |
468 open OUT, ">$wd/Rout/barplot.R"; | |
469 print OUT 'coveragetable <- read.table("../Targets.Global.Coverage",as.is=TRUE,sep="\t",header=FALSE)'."\n"; | |
470 print OUT 'coverage <- coveragetable[c('.$startline.':'.$stopline.'),'.$covcol.']'."\n"; | |
471 print OUT 'entries <- coveragetable[c('.$startline.':'.$stopline.'),4]'."\n"; | |
472 print OUT 'entries <- sub("\\\\S+\\\\|","",entries,perl=TRUE)'."\n"; | |
473 print OUT 'coverage[coverage < 1] <- 1'."\n"; | |
474 print OUT 'colors <- c(rep("grey",length(coverage)))'."\n"; | |
475 print OUT 'colors[coverage<'.$thresh.'] <- "red"'."\n"; | |
476 | |
477 if ($stopline - $startline > 20) { | |
478 $scale = 2; | |
479 } | |
480 else { | |
481 $scale = 1; | |
482 } | |
483 my $width = 480 * $scale; | |
484 my $height = 240 * $scale; | |
22
95062840f80f
Correction to png calls to use cairo instead of x11. thanks to Eric Enns for pointing this out.
geert-vandeweyer
parents:
12
diff
changeset
|
485 print OUT 'png(file="../Plots/Coverage_'.$currgroup.'.png", bg="white", width='.$width.', height='.$height.',type=c("cairo"))'."\n"; |
1 | 486 print OUT 'ylim = c(0,max(max(log10(coverage),log10('.($thresh+20).'))))'."\n"; |
487 print OUT 'mp <- barplot(log10(coverage),col=colors,main="Exon Coverage for '.$currgroup.'",ylab="Log10(Coverage)", ylim=ylim)'."\n"; | |
488 print OUT 'text(mp, log10(coverage) + log10(2),format(coverage),xpd = TRUE,srt=90)'."\n"; | |
489 print OUT 'text(mp,par("usr")[3]-0.1,labels=entries,srt=45,adj=1,xpd=TRUE)'."\n"; | |
490 print OUT 'abline(h=log10('.$thresh.'),lwd=4,col=rgb(255,0,0,100,maxColorValue=255))'."\n"; | |
491 print OUT 'graphics.off()'."\n"; | |
492 close OUT; | |
12
86df3f847a72
Switched to R 3.0.2 from iuc, and moved bedtools to seperate tool_definition
geert-vandeweyer
parents:
11
diff
changeset
|
493 system("cd $wd/Rout && Rscript barplot.R"); |
1 | 494 if ($scale == 1) { |
495 push(@small,'\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/Coverage_'.$currgroup.'.png}'); | |
496 } | |
497 else { | |
498 push(@large,'\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/Coverage_'.$currgroup.'.png}'); | |
499 } | |
500 } | |
501 ## print to TEX | |
502 open OUT, ">>$wd/Report/Report.tex"; | |
503 print OUT '\subsection*{Gene Summaries}'."\n"; | |
504 print OUT '\underline{Legend:} \\\\'."\n"; | |
505 print OUT '{\color{red}\textbf{RED:} Coverage did not reach set threshold of '.$thresh.'} \\\\'."\n"; | |
506 print OUT '{\color{orange}\textbf{ORANGE:} Coverage was incomplete for the exon. Overruled by red.} \\\\' ."\n"; | |
507 $col = 1; | |
508 foreach (@small) { | |
509 if ($col > 2) { | |
510 $col = 1; | |
511 print OUT "\n"; | |
512 } | |
513 print OUT '\begin{minipage}{0.5\linewidth}\centering'."\n"; | |
514 print OUT $_."\n"; | |
515 print OUT '\end{minipage}'."\n"; | |
516 $col++; | |
517 } | |
518 ## new line | |
519 if ($col == 2) { | |
520 print OUT '\\\\'." \n"; | |
521 } | |
522 foreach(@large) { | |
523 print OUT $_."\n"; | |
524 } | |
525 close OUT; | |
526 | |
527 } | |
528 | |
529 # 3. Detailed overview of failed exons (globally failed) | |
530 if (exists($opts{'s'})) { | |
531 # count columns | |
532 my $head = `head -n 1 $wd/Targets.Position.Coverage`; | |
533 chomp($head); | |
534 my @cols = split(/\t/,$head); | |
535 my $nrcols = scalar(@cols); | |
536 my $covcol = $nrcols; | |
537 my $poscol = $nrcols -1; | |
538 # tex section header | |
539 open TEX, ">>$wd/Report/Report.tex"; | |
540 print TEX '\subsection*{Failed Exon Plots}'."\n"; | |
541 $col = 1; | |
542 print TEX '\underline{NOTE:} Only exons with global coverage $<$'.$thresh.' or incomplete coverage were plotted \\\\'."\n"; | |
543 foreach(@failedregions) { | |
544 if ($col > 2) { | |
545 $col = 1; | |
546 print TEX "\n"; | |
547 } | |
548 # which exon | |
549 my $region = $_; | |
550 my $exon = $filehash{$region}{'exon'}; | |
551 # link exon to tmp file | |
552 my $exonfile = "$wd/SplitFiles/File_".$filehash{$region}{'idx'}.".txt"; | |
553 ## determine transcript orientation and location | |
554 my $firstline = `head -n 1 $exonfile`; | |
555 my @firstcols = split(/\t/,$firstline); | |
556 my $orient = $firstcols[5]; | |
557 my $genomicchr = $firstcols[0]; | |
558 my $genomicstart = $firstcols[1]; | |
559 my $genomicstop = $firstcols[2]; | |
560 if ($orient eq '+') { | |
561 $bps = $genomicstop - $genomicstart + 1; | |
562 $subtitle = "Region 0-$bps: $genomicchr:".$de->format_number($genomicstart)."+".$de->format_number($genomicstop); | |
563 } | |
564 else { | |
565 $bps = $genomicstop - $genomicstart + 1; | |
566 $subtitle = "Region 0-$bps: $genomicchr:".$de->format_number($genomicstart)."-".$de->format_number($genomicstop); | |
567 } | |
568 # print Rscript | |
569 open OUT, ">$wd/Rout/exonplot.R"; | |
570 print OUT 'coveragetable <- read.table("'.$exonfile.'",as.is=TRUE,sep="\t",header=FALSE)'."\n"; | |
571 print OUT 'coverage <- coveragetable[,'.$covcol.']'."\n"; | |
572 print OUT 'coverage[coverage < 1] <- 1'."\n"; | |
573 print OUT 'positions <- coveragetable[,'.$poscol.']'."\n"; | |
574 | |
575 my $width = 480 ; | |
576 my $height = 240 ; | |
577 my $exonstr = $exon; | |
578 $exonstr =~ s/\s/_/g; | |
579 $exon =~ s/_/ /g; | |
580 $exon =~ s/\|/ /g; | |
22
95062840f80f
Correction to png calls to use cairo instead of x11. thanks to Eric Enns for pointing this out.
geert-vandeweyer
parents:
12
diff
changeset
|
581 print OUT 'png(file="../Plots/Coverage_'.$exonstr.'.png", bg="white", width='.$width.', height='.$height.',type=c("cairo"))'."\n"; |
1 | 582 print OUT 'ylim = c(0,log10(max(max(coverage),'.($thresh+10).')))'."\n"; |
583 if ($orient eq '-') { | |
584 print OUT 'plot(positions,log10(coverage),type="n",main="Coverage for '.$exon.'",ylab="log10(Coverage)",ylim=ylim,xlab="Position",xlim=rev(range(positions)),sub="(Transcribed from minus strand)")'."\n"; | |
585 print OUT 'mtext("'.$subtitle.'")'."\n"; | |
586 } | |
587 else { | |
588 print OUT 'plot(positions,log10(coverage),type="n",main="Coverage for '.$exon.'",ylab="log10(Coverage)",ylim=ylim,xlab="Position",sub="(Transcribed from plus strand)")'."\n"; | |
589 print OUT 'mtext("'.$subtitle.'")'."\n"; | |
590 } | |
591 print OUT 'lines(positions,log10(coverage))'."\n"; | |
592 print OUT 'abline(h=log10('.$thresh.'),lwd=4,col=rgb(255,0,0,100,maxColorValue=255))'."\n"; | |
593 print OUT 'failedpos <- positions[coverage<'.$thresh.']'."\n"; | |
594 print OUT 'failedcov <- coverage[coverage<'.$thresh.']'."\n"; | |
595 print OUT 'points(failedpos,log10(failedcov),col="red",pch=19)'."\n"; | |
596 print OUT 'graphics.off()'."\n"; | |
597 close OUT; | |
598 # run R script | |
12
86df3f847a72
Switched to R 3.0.2 from iuc, and moved bedtools to seperate tool_definition
geert-vandeweyer
parents:
11
diff
changeset
|
599 system("cd $wd/Rout && Rscript exonplot.R"); |
1 | 600 # Add to .TEX |
601 print TEX '\begin{minipage}{0.5\linewidth}\centering'."\n"; | |
602 print TEX '\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/Coverage_'.$exonstr.'.png}'."\n"; | |
603 print TEX '\end{minipage}'."\n"; | |
604 $col++; | |
605 } | |
606 } | |
607 | |
608 ## plot failed (subregion) or all exons | |
609 if (exists($opts{'S'}) || exists($opts{'A'})) { | |
610 # count columns | |
611 my $head = `head -n 1 $wd/Targets.Position.Coverage`; | |
612 chomp($head); | |
613 my @cols = split(/\t/,$head); | |
614 my $nrcols = scalar(@cols); | |
615 my $covcol = $nrcols; | |
616 my $poscol = $nrcols -1; | |
617 # tex section header | |
618 open TEX, ">>$wd/Report/Report.tex"; | |
619 print TEX '\subsection*{Failed Exon Plots}'."\n"; | |
620 if (exists($opts{'S'})) { | |
621 print TEX '\underline{NOTE:} ALL exons were tested for local coverage $<$'.$thresh.' \\\\'."\n"; | |
622 } | |
623 elsif (exists($opts{'A'})) { | |
624 print TEX '\underline{NOTE:} ALL exons are plotted, regardless of coverage \\\\'."\n"; | |
625 } | |
626 $col = 1; | |
627 foreach(@allregions) { | |
628 if ($col > 2) { | |
629 $col = 1; | |
630 print TEX "\n"; | |
631 } | |
632 # which exon | |
633 my $region = $_; | |
634 my $exon = $filehash{$region}{'exon'}; | |
635 # grep exon to tmp file | |
636 my $exonfile = "$wd/SplitFiles/File_".$filehash{$region}{'idx'}.".txt"; | |
637 ## determine transcript orientation. | |
638 my $firstline = `head -n 1 $exonfile`; | |
639 my @firstcols = split(/\t/,$firstline); | |
640 my $orient = $firstcols[5]; | |
641 my $genomicchr = $firstcols[0]; | |
642 my $genomicstart = $firstcols[1]; | |
643 my $genomicstop = $firstcols[2]; | |
644 | |
645 if ($orient eq '+') { | |
646 $bps = $genomicstop - $genomicstart + 1; | |
647 $subtitle = "Region 0-$bps: $genomicchr:".$de->format_number($genomicstart)."+".$de->format_number($genomicstop); | |
648 | |
649 } | |
650 else { | |
651 $bps = $genomicstop - $genomicstart + 1; | |
652 $subtitle = "Region 0-$bps: $genomicchr:".$de->format_number($genomicstart)."-".$de->format_number($genomicstop); | |
653 | |
654 } | |
655 | |
656 # check if failed | |
657 if (exists($opts{'S'})) { | |
658 my $cs = `cut -f $covcol '$exonfile' `; | |
659 my @c = split(/\n/,$cs); | |
660 @c = sort { $a <=> $b } @c; | |
661 if ($c[0] >= $thresh) { | |
662 # lowest coverage > threshold => skip | |
663 next; | |
664 } | |
665 } | |
666 # print Rscript | |
667 open OUT, ">$wd/Rout/exonplot.R"; | |
668 print OUT 'coveragetable <- read.table("'.$exonfile.'",as.is=TRUE,sep="\t",header=FALSE)'."\n"; | |
669 print OUT 'coverage <- coveragetable[,'.$covcol.']'."\n"; | |
670 print OUT 'coverage[coverage < 1] <- 1'."\n"; | |
671 print OUT 'positions <- coveragetable[,'.$poscol.']'."\n"; | |
672 my $width = 480 ; | |
673 my $height = 240 ; | |
674 my $exonstr = $exon; | |
675 $exonstr =~ s/\s/_/g; | |
676 $exon =~ s/_/ /g; | |
677 $exon =~ s/\|/ /g; | |
22
95062840f80f
Correction to png calls to use cairo instead of x11. thanks to Eric Enns for pointing this out.
geert-vandeweyer
parents:
12
diff
changeset
|
678 print OUT 'png(file="../Plots/Coverage_'.$exonstr.'.png", bg="white", width='.$width.', height='.$height.',type=c("cairo"))'."\n"; |
1 | 679 print OUT 'ylim = c(0,log10(max(max(coverage),'.($thresh+10).')))'."\n"; |
680 if ($orient eq '-') { | |
681 print OUT 'plot(positions,log10(coverage),type="n",main="Coverage for '.$exon.'",ylab="log10(Coverage)",ylim=ylim,xlab="Position",xlim=rev(range(positions)),sub="(Transcribed from minus strand)")'."\n"; | |
682 print OUT 'mtext("'.$subtitle.'")'."\n"; | |
683 } | |
684 else { | |
685 print OUT 'plot(positions,log10(coverage),type="n",main="Coverage for '.$exon.'",ylab="log10(Coverage)",ylim=ylim,xlab="Position",sub="(Transcribed from plus strand)")'."\n"; | |
686 print OUT 'mtext("'.$subtitle.'")'."\n"; | |
687 } | |
688 | |
689 print OUT 'lines(positions,log10(coverage))'."\n"; | |
690 print OUT 'abline(h=log10('.$thresh.'),lwd=4,col=rgb(255,0,0,100,maxColorValue=255))'."\n"; | |
691 print OUT 'failedpos <- positions[coverage<'.$thresh.']'."\n"; | |
692 print OUT 'failedcov <- coverage[coverage<'.$thresh.']'."\n"; | |
693 print OUT 'points(failedpos,log10(failedcov),col="red",pch=19)'."\n"; | |
694 print OUT 'graphics.off()'."\n"; | |
695 close OUT; | |
696 # run R script | |
12
86df3f847a72
Switched to R 3.0.2 from iuc, and moved bedtools to seperate tool_definition
geert-vandeweyer
parents:
11
diff
changeset
|
697 system("cd $wd/Rout && Rscript exonplot.R"); |
1 | 698 # Add to .TEX |
699 print TEX '\begin{minipage}{0.5\linewidth}\centering'."\n"; | |
700 print TEX '\includegraphics[width=\textwidth,keepaspectratio=true]{../Plots/Coverage_'.$exonstr.'.png}'."\n"; | |
701 print TEX '\end{minipage}'."\n"; | |
702 $col++; | |
703 } | |
704 } | |
705 ## list failed exons | |
706 if (exists($opts{'L'})) { | |
707 # count columns | |
708 my $head = `head -n 1 $wd/Targets.Position.Coverage`; | |
709 chomp($head); | |
710 my @cols = split(/\t/,$head); | |
711 my $nrcols = scalar(@cols); | |
712 my $covcol = $nrcols; | |
713 my $poscol = $nrcols -1; | |
714 ## hash to print | |
715 # tex section header | |
716 open TEX, ">>$wd/Report/Report.tex"; | |
717 print TEX '\subsection*{List of Failed Exons}'."\n"; | |
718 print TEX '\underline{NOTE:} ALL exons were tested for local coverage $<$'.$thresh.' \\\\'."\n"; | |
719 print TEX '{\footnotesize\begin{longtable}[l]{@{\extracolsep{\fill}}llll}'."\n".'\hline'."\n"; | |
720 print TEX '\textbf{Target Name} & \textbf{Genomic Position} & \textbf{Avg.Coverage} & \textbf{Min.Coverage} \\\\'."\n".'\hline'."\n"; | |
721 print TEX '\endhead'."\n"; | |
722 print TEX '\hline '."\n".'\multicolumn{4}{r}{{\textsl{\footnotesize Continued on next page}}} \\\\ '."\n".'\hline' ."\n". '\endfoot' . "\n". '\endlastfoot' . "\n"; | |
723 | |
724 $col = 1; | |
725 open IN, "$wd/Targets.Global.Coverage"; | |
726 while (<IN>) { | |
727 chomp($_); | |
728 my @p = split(/\t/,$_); | |
729 my $region = $p[0].'-'.$p[1].'-'.$p[2]; | |
730 my $exon = $filehash{$region}{'exon'}; | |
731 # grep exon to tmp file | |
732 my $exonfile = "$wd/SplitFiles/File_".$filehash{$region}{'idx'}.".txt"; | |
733 ## determine transcript orientation. | |
734 my $firstline = `head -n 1 $exonfile`; | |
735 my @firstcols = split(/\t/,$firstline); | |
736 my $orient = $firstcols[5]; | |
737 my $genomicchr = $firstcols[0]; | |
738 my $genomicstart = $firstcols[1]; | |
739 my $genomicstop = $firstcols[2]; | |
740 | |
741 if ($orient eq '+') { | |
742 $bps = $genomicstop - $genomicstart + 1; | |
743 $subtitle = "$genomicchr:".$de->format_number($genomicstart)."+".$de->format_number($genomicstop); | |
744 | |
745 } | |
746 else { | |
747 $bps = $genomicstop - $genomicstart + 1; | |
748 $subtitle = "$genomicchr:".$de->format_number($genomicstart)."-".$de->format_number($genomicstop); | |
749 } | |
750 | |
751 # check if failed | |
752 my $cs = `cut -f $covcol '$exonfile' `; | |
753 my @c = split(/\n/,$cs); | |
754 my ($avg,$med,$min,$max,$first,$third,$ontarget) = arraystats(@c); | |
755 | |
756 if ($min >= $thresh) { | |
757 # lowest coverage > threshold => skip | |
758 next; | |
759 } | |
760 | |
761 # print to .tex table | |
762 if (length($exon) > 30) { | |
763 $exon = substr($exon,0,27) . '...'; | |
764 } | |
765 $exon =~ s/_/ /g; | |
766 $exon =~ s/\|/ /g; | |
767 | |
768 print TEX "$exon & $subtitle & ".int($avg)." & $min ".'\\\\'."\n"; | |
769 } | |
770 close IN; | |
771 print TEX '\hline'."\n"; | |
772 print TEX '\end{longtable}}'."\n"; | |
773 close TEX; | |
774 } | |
775 | |
776 | |
777 ## Close document | |
778 open OUT, ">>$wd/Report/Report.tex"; | |
779 print OUT '\label{endofdoc}'."\n"; | |
780 print OUT '\end{document}'."\n"; | |
781 close OUT; | |
782 system("cd $wd/Report && pdflatex Report.tex > /dev/null 2>&1 && pdflatex Report.tex > /dev/null 2>&1 "); | |
783 | |
784 ## mv report to output file | |
785 system("cp -f $wd/Report/Report.pdf '$pdffile'"); | |
786 ##create tar.gz file | |
787 system("mkdir $wd/Results"); | |
788 system("cp -Rf $wd/Plots $wd/Results/"); | |
789 system("cp -Rf $wd/Report/ $wd/Results/"); | |
790 if (-e "$wd/Targets.Global.Coverage") { | |
791 system("cp -Rf $wd/Targets.Global.Coverage $wd/Results/"); | |
792 } | |
793 if (-e "$wd/Targets.Position.Coverage") { | |
794 system("cp -Rf $wd/Targets.Position.Coverage $wd/Results/"); | |
795 } | |
796 | |
797 system("cd $wd && tar czf '$tarfile' Results/"); | |
798 ## clean up (galaxy stores outside wd) | |
799 system("rm -Rf $wd"); | |
800 ############### | |
801 ## FUNCTIONS ## | |
802 ############### | |
803 sub arraystats{ | |
804 my @array = @_; | |
805 my $count = scalar(@array); | |
806 @array = sort { $a <=> $b } @array; | |
807 # median | |
808 my $median = 0; | |
809 if ($count % 2) { | |
810 $median = $array[int($count/2)]; | |
811 } else { | |
812 $median = ($array[$count/2] + $array[$count/2 - 1]) / 2; | |
813 } | |
814 # average | |
815 my $sum = 0; | |
816 foreach (@array) { $sum += $_; } | |
817 my $average = $sum / $count; | |
818 # quantiles (rounded) | |
819 my $quart = int($count/4) ; | |
820 my $first = $array[$quart]; | |
821 my $third = $array[($quart*3)]; | |
822 my $min = $array[0]; | |
823 my $max = $array[($count-1)]; | |
824 return ($average,$median,$min,$max,$first,$third,$sum); | |
825 } | |
826 | |
827 sub GlobalSummary { | |
828 my ($bam,$targets) = @_; | |
829 | |
830 my $command = "cd $wd && coverageBed -abam $bam -b $targets > $wd/Targets.Global.Coverage"; | |
831 if (exists($commandsrun{$command})) { | |
832 return; | |
833 } | |
834 system($command); | |
835 $commandsrun{$command} = 1; | |
836 } | |
837 | |
838 sub CoveragePerRegion { | |
839 my ($bam,$targets) = @_; | |
840 my $command = "cd $wd && coverageBed -abam $bam -b $targets > $wd/Targets.Global.Coverage"; | |
841 if (exists($commandsrun{$command})) { | |
842 return; | |
843 } | |
844 system($command); | |
845 $commandsrun{$command} = 1; | |
846 } | |
847 | |
848 sub SubRegionCoverage { | |
849 my ($bam,$targets) = @_; | |
850 my $command = "cd $wd && coverageBed -abam $bam -b $targets -d > $wd/Targets.Position.Coverage"; | |
851 system($command); | |
852 $commandsrun{$command} = 1; | |
853 } | |
854 |