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