Debian Codegraph @

fastx-graph

raw symbols SCIP
1 #!/usr/bin/Rscript --vanilla
2
3 # Copyright (c) 2011 Expression Analysis / Gunjan Hariani, Erik Aronesty
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 #
23 # $Id: fastx-graph 525 2012-12-25 19:41:22Z earonesty $
24
25 if (!require(getopt)) {
26 write(c("Installing package on:",system("hostname",intern=T)),file=stderr())
27 install.packages('getopt',repos='http://R-Forge.R-project.org')
28 # will die if it fails at this point
29 library(getopt)
30 }
31
32 if (!require("Hmisc")) {
33 write(c("Installing package on:",system("hostname",intern=T)),file=stderr())
34 install.packages('Hmisc',repos='http://R-Forge.R-project.org')
35 # will die if it fails at this point
36 library("Hmisc")
37 }
38
39 spec <- matrix(c(
40 'input' , 'i', 1, "character", "file from fastq-stats -x (required)",
41 'gc' , 'G', 1, "character", "input gc content file (optional)",
42 'out' , 'o', 1, "character", "output filename (optional)",
43 'help' , 'h', 0, "logical", "this help"
44 ),ncol=5,byrow=T)
45
46 opt = getopt(spec);
47
48 if (!is.null(opt$help) || is.null(opt$input)) {
49 cat(paste(getopt(spec, usage=T, command="fastx-graph"),"\n"));
50 q();
51 }
52
53 in.file <- opt$input
54 gc.file <- opt$gc
55 out.file <- opt$out
56
57 fastx <- read.table(in.file,sep="\t",header=T,as.is=T)
58
59 # output is in, minus txt, plus png
60 if (is.null(out.file)) {
61 in.file <- gsub(".txt$","",in.file,perl=T)
62 out.file <- paste(in.file,".png",sep="")
63 }
64
65 # correct for bug in R if the file has a % sign in it
66 out.file <- gsub("%","%%",out.file)
67
68 png(out.file,width=1000, height=500)
69
70 par(mar=c(4,3,5,5),xaxs="i",yaxs="i",xpd=T)
71 plot(c(0,0),pch="",ylim=c(min(c(0,fastx$lW)),max(41,fastx$rW)),xlim=c(0,(nrow(fastx)+1)),
72 xlab="",ylab="",las=2,cex.axis=.85,cex.lab=.85,xaxt="n")
73
74 lims <- par("xaxp")[1:2]
75 major.ticks <- pretty(lims,n=par("xaxp")[3])
76 minor.tick(nx=(major.ticks[2]-major.ticks[1]),ny=10)
77
78 minor.ticks <- 1:nrow(fastx)
79 mtext("Cycle",side=3,line=3,cex=.85)
80 axis(1,at=major.ticks,cex.axis=.85,labels=F)
81 axis(3,at=major.ticks,cex.axis=.85,labels=F)
82 axis(3,at=minor.ticks,tcl=par("tcl")*0.5,labels=minor.ticks,cex.axis=.80,las=3)
83
84 colnames(fastx) <- c("column","count","min","max","sum","mean","Q1","med",
85 "Q3","IQR","lW","rW","A_count","C_count","G_count",
86 "T_count","N_count","Max_count")
87
88 for(i in 1:nrow(fastx)) {
89 par(new=T)
90 rect(fastx$column[i]-.35,fastx$Q1[i],fastx$column[i]+0.35,fastx$Q3[i],col="gray")
91 segments(fastx$column[i]-.35,fastx$med[i],fastx$column[i]+0.35,fastx$med[i],col="red",lwd=1.5)
92 segments(fastx$column[i],fastx$lW[i],fastx$column[i],fastx$Q1[i],lty="dashed")
93 segments(fastx$column[i],fastx$Q3[i],fastx$column[i],fastx$rW[i],lty="dashed")
94 segments(fastx$column[i]-.35,fastx$lW[i],fastx$column[i]+0.35,fastx$lW[i])
95 segments(fastx$column[i]-.35,fastx$rW[i],fastx$column[i]+0.35,fastx$rW[i])
96 }
97
98 # if theres a significant difference
99 tots<-fastx$N_count+fastx$T_count+fastx$G_count+fastx$C_count+fastx$A_count
100
101 if (min(tots) < (max(tots)*.98)) {
102 par(new=T)
103 plot(tots,col="gray",xaxt="n",yaxt="n",xlab="",ylab="",pch="+",ylim=c(min(tots),max(tots)))
104 lines(fastx$N_count+fastx$T_count+fastx$G_count+fastx$C_count+fastx$A_count,col="gray",xaxt="n",yaxt="n",xlab="",ylab="", lty=2)
105 }
106
107 par(new=T)
108 plot(fastx$column,fastx$A_count*100/fastx$count,col="red",type="l",xaxt="n",yaxt="n",xlab="",ylab="",
109 ylim=c(0,100),lwd=2)
110 par(new=T)
111 plot(fastx$column,fastx$C_count*100/fastx$count,col="blue",type="l",xaxt="n",yaxt="n",xlab="",ylab="",
112 ylim=c(0,100),lwd=2)
113 par(new=T)
114 plot(fastx$column,fastx$G_count*100/fastx$count,col="green",type="l",xaxt="n",yaxt="n",xlab="",ylab="",
115 ylim=c(0,100),lwd=2)
116 par(new=T)
117 plot(fastx$column,fastx$T_count*100/fastx$count,col="black",type="l",xaxt="n",yaxt="n",xlab="",ylab="",
118 ylim=c(0,100),lwd=2)
119 par(new=T)
120 barplot(fastx$N_count*100/fastx$count,col="orange",xaxt="n",yaxt="n",xlab="",ylab="",
121 ylim=c(0,100))
122
123 axis(4,at=seq(0,100,25),cex.axis=.85)
124 mtext("Pct Base Distribution",side=4,line=3,cex=.85)
125 mtext("Base Quality",side=2,line=2,cex=.85)
126 legend(0,-4,col=c("red","blue","green","black","orange"),lty=1,
127 legend=c("A","C","G","T","N"),cex=.70,horiz=T,lwd=2)
128
129 if(!is.null(gc.file)) {
130 tmp <- read.table(gc.file,sep="\t",header=F,as.is=T)
131 if (tmp[1,1] == 'pct-GC') {
132 # silly legacy format
133 GC <- read.table(gc.file,sep="\t",header=T,as.is=T,skip=3)
134 } else if (tmp[1,1] == 'pct_GC') {
135 GC <- read.table(gc.file,sep="\t",header=T,as.is=T,skip=1)
136 } else {
137 GC <- read.table(gc.file,sep="\t",header=T,as.is=T)
138 }
139 colnames(GC)=c("pct_gc", "count")
140 par(new=T)
141 par(fig=c(0.1,0.2,0.45,0.60))
142 par(mar=c(0,0,1,0))
143 plot(GC$pct_gc,GC$count,type="l",xaxt="n",yaxt="n",
144 main="%GC per read",cex.main=.90)
145 axis(1,seq(0,100,20),labels=seq(0,100,20),las=2,tck=-0.1,cex.axis=.75)
146 }
147
148
149 graphics.off()