annotate NGSrich_0.5.5/R/eval_details.R @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 #!/usr/bin/Rscript
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4 ##Input parameters########################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 xml=as.character(commandArgs()[6])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 bed=as.character(commandArgs()[7])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7 cov=as.character(commandArgs()[8])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 chrinfo=as.character(commandArgs()[9])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 outdir=as.character(commandArgs()[10])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 genome=as.character(commandArgs()[11])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11 ##########################################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 ##Read data###############################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 bedfile<-read.table(bed,header=FALSE,stringsAsFactors=FALSE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 wunknown<-which(bedfile$V4=="unknown")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 bedfile$V4[wunknown]<-paste("unknown",seq(1,length(wunknown),1),sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19 bedfile$V4<-gsub("/","-",bedfile$V4)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 covdata<-readLines(cov,warn=FALSE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 xmldata<-readLines(xml,warn=FALSE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23 nbrline<-xmldata[grep("<NumberReads>",xmldata)]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 rlenline<-xmldata[grep("<ReadLength>",xmldata)]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 nbreads<-as.numeric(strsplit(strsplit(nbrline,">")[[1]][2],"<")[[1]][1])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 rlength<-as.numeric(strsplit(strsplit(rlenline,">")[[1]][2],"<")[[1]][1])
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 expect<-0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 allchr<-levels(as.factor(bedfile$V1))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 if(chrinfo!="none"){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 cinfo<-read.table(chrinfo,header=FALSE,stringsAsFactors=FALSE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 gsize<-sum(as.numeric(cinfo[,2]))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 expect=(rlength*nbreads)/gsize
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 dir.create(paste(outdir,"/chromosomes",sep=""),showWarnings=FALSE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 for(chr in allchr){dir.create(paste(outdir,"/chromosomes/",chr,sep=""),showWarnings=FALSE)}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 tcov<-list()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 tstarts<-numeric(0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 tcov0<-character(0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 firstt=1
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 for(i in 1:length(covdata)){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 if(i%%10000==0){cat(i,"/",length(covdata),"\n")}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 if(nchar(covdata[i])>10){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 if(firstt==0){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 tcov[[tname]]<-as.numeric(tcov0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48 tcov0<-character(0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 firstt=0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 nameel<-strsplit(covdata[i],"\t")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 tname<-paste(nameel[[1]][1],":",nameel[[1]][2],"-",nameel[[1]][3],sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54 else{tcov0<-c(tcov0,covdata[i])}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56 ##########################################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 sortchr<-function(x){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 if(sum(substr(x,1,3)=="chr")==length(x)){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61 x0<-strsplit(x,"chr")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 xspl<-character(0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 for(i in 1:length(x0)){xspl<-c(xspl,x0[[i]][2])}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 xnum<-xspl[!is.na(as.numeric(xspl))]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 xchar<-setdiff(xspl,xnum)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 xsorted<-paste("chr",c(sort(as.numeric(xnum)),sort(xchar)),sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 return(xsorted)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 else{return(x)}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 ##Create gene index#######################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 allchr<-sortchr(levels(as.factor(bedfile$V1)))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 outfile=paste(outdir,"/chromosomes/chromosomes.html",sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 cat(file=outfile,paste(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 "<html>\n<head>\n<title>Index of Target Regions</title>\n",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 "<style type=\"text/css\">\n body{font-family:sans-serif;}\n h2,h3{color: darkblue;}\n a{color:darkblue;}\n table.output td{ padding: 4px; background-color: lightskyblue; border: 1px solid #000; border-color: darkblue; }\n</style>\n",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 "</head>\n\n",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 "<script language=\"JavaScript\">\n var questionClass=\"ChrList\";\n function collapseAll(){\n var allSections=document.getElementsByTagName(\"div\");\n for(i=0;i<allSections.length;i++){\n if(allSections[i].className==questionClass){\n allSections[i].style.display = \"none\";\n }}}\n function expand(name){\n collapseAll();\n var newStyle=\"\";\n if(document.getElementById(name).style.display!=\"block\"){newStyle=\"block\";}\n else{newStyle=\"none\";}\n document.getElementById(name).style.display=newStyle;\n }\n</script>\n\n",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 "<body onload=\"expand('",allchr[1],"')\">\n<h2>Show Details on Genewise Coverage</h2>",sep=""))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 for(chr in allchr){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 cat(file=outfile,paste("<a href=\"javascript:expand('",chr,"')\">",chr,"</a>\n",sep=""),append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 cat(file=outfile,"<br/><br/>\n\n",append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 for(chr in allchr){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 thischr<-bedfile[bedfile$V1==chr,]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 thischr<-thischr[order(thischr$V2),]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 allgenes<-intersect(thischr$V4,thischr$V4)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 cat(file=outfile,paste("<div class=\"ChrList\" id=\"",chr,"\"><table class=\"output\">\n<tr><td><b>Gene</b></td><td><b>Region</b></td><td><b>Link</b></td></tr>\n",sep=""),append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 for(gene in allgenes){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 genebed<-thischr[thischr$V4==gene,]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 firststart=genebed$V2[1]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 lastend<-genebed$V3[nrow(genebed)]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 tname=paste(chr,":",firststart,"-",lastend,sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 if(substr(gene,1,7)=="unknown"){genelab="unknown"}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 else{genelab=gene}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 cat(file=outfile,paste("<tr><td>",genelab,"</td><td>",tname,"</td><td><a href=\"",chr,"/",gene,"/index.html\">Show Details</a></td></tr>\n",sep=""),append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 cat(file=outfile,"</table>\n</div>\n",append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 cat(file=outfile,"</body></html>\n",append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 ##########################################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 ##Create genewise reports#################################################################################################################
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 firstgene=1
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 partest<-cv<-goftest<-numeric(0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 genebed<-split(bedfile,bedfile$V4)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 for(gene in names(genebed)){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 thisbed<-genebed[[gene]]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 chr=thisbed$V1[1]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 if(substr(gene,1,7)=="unknown"){genelab="unknown"}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 else{genelab=gene}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 ##Write HTML header
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 outfile=paste(outdir,"/chromosomes/",chr,"/",gene,"/index.html",sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 dir.create(paste(outdir,"/chromosomes/",chr,"/",gene,sep=""),showWarnings=FALSE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 cat(file=outfile,paste("<html>\n<head>\n<title>Enrichment Details</title>\n<style type=\"text/css\">\n body{font-family:sans-serif;}\n h2,h3{color: darkblue;}\n a{color:darkblue;}\n table.output td{ padding: 4px; background-color: lightskyblue; border: 1px solid #000; border-color: darkblue; } table.noborder td{padding: 0px; border: 0px solid #000; border-color: lightskyblue}\n</style>\n</head>\n\n<body>\n<h2>Enrichment Details for ",genelab,"</h2>\n<table class=\"output\">\n<tr><td><b>Target Region</b></td><td><b>Coverage</b></td><td><b>Percentage</b></td><td><b>Enrichment<br/>Factor</b></td><td><b>Target Histogram</b></td><td><b>External Link</b></td></tr>\n",sep=""))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 for(i in 1:nrow(thisbed)){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 start=thisbed$V2[i]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 end=thisbed$V3[i]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 region=seq(start,end,1)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 tname=paste(chr,":",start,"-",end,sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 if(is.element(tname,names(tcov))){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 basecov=tcov[[tname]]
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138 if(expect!=0){enrratio<-round(mean(basecov)/expect,1)}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 else{enrratio<-"unknown"}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 stddev=round(sd(basecov),2)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 ##Create plots
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 png(file=paste(outdir,"/chromosomes/",chr,"/",gene,"/h_",tname,".png",sep=""))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 plot(region,basecov,pch=".",xlab=paste("Physical Position [",chr,"]",sep=""),ylab="Coverage",col="tomato3")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 edgesx<-c(region,end,start)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 edgesy<-c(basecov,0,0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 polygon(edgesx,edgesy,col="tomato3",border=NA)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 lines(c(start,start),c(-1000,1000000))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 lines(c(end,end),c(-1000,1000000))
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 lines(c(start,end),c(mean(basecov),mean(basecov)),col="darkblue")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 lines(c(start,end),c(mean(basecov)-stddev,mean(basecov)-stddev),lty="dashed",col="darkblue")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 lines(c(start,end),c(mean(basecov)+stddev,mean(basecov)+stddev),lty="dashed",col="darkblue")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 dev.off()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 ##Write HTML table
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 ucsclink<-paste("http://www.genome.ucsc.edu/cgi-bin/hgTracks?&db=",genome,"&position=",chr,"%3A",start,"-",end,"&hgt.suggest=&pix=800&Submit=submit&hgsid=183341879",sep="")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 cat(file=outfile,paste("<tr><td><b>",tname,"</b><br/>(",end-start+1," bp)</td><td><table class=\"noborder\"><tr><td><b>Mean:</b></td><td>",thisbed[i,5],"</td></tr><tr><td><b>Std Dev:</b></td><td>",stddev,"</td></tr></table></td><td><table class=\"noborder\"><tr><td><b>1x:</b></td><td>",thisbed[i,6],"%</td></tr><tr><td><b>5x:</b></td><td>",thisbed[i,7],"%</td></tr><tr><td><b>10x:</b></td><td>",thisbed[i,8],"%</td></tr><tr><td><b>20x:</b></td><td>",thisbed[i,9],"%</td></tr><tr><td><b>30x:</b></td><td>",thisbed[i,10],"%</td></tr></table></td><td>",enrratio,"</td><td><a href=\"h_",tname,".png\"><img src=\"h_",tname,".png\" width=160></img></a></td><td><a href=\"",ucsclink,"\">Show in Genome Browser</td></tr>\n",sep=""),append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 cat(file=outfile,"</table>\n</body>\n</html>",append=TRUE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 ##########################################################################################################################################