annotate getGenomicScore.py @ 20:16ba480adf96

Uploaded
author xuebing
date Sat, 31 Mar 2012 08:31:22 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
1 import random,string,os,sys
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
2
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
3
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
4 def getScore(intvfile,outfile,summary_type,bwfilepath,nbin,strand,outplot,span):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
5 f = open(intvfile)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
6 tmpsh = "".join(random.sample(string.letters+string.digits, 8))
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
7 tmpout = "".join(random.sample(string.letters+string.digits, 8))
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
8 tmp = open(tmpsh,'w')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
9 if os.path.isdir(bwfilepath):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
10 for line in f:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
11 flds = line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
12 cmd = 'bigWigSummary -type='+summary_type+' '+bwfilepath+'/'+flds[0]+'.bw '+flds[0]+' '+flds[1]+' '+flds[2]+' '+nbin+' >> '+tmpout+' 2>>'+tmpout+'\n'
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
13 tmp.write(cmd)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
14 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
15 for line in f:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
16 flds = line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
17 cmd = 'bigWigSummary -type='+summary_type+' '+bwfilepath+' '+flds[0]+' '+flds[1]+' '+flds[2]+' '+nbin+' >> '+tmpout+' 2>>'+tmpout+'\n'
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
18 tmp.write(cmd)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
19 f.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
20 # remove blank lines
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
21 tmp.write("sed '/^$/d' "+tmpout+'>'+tmpout+".1\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
22 tmp.write("sed '/^Can/d' "+tmpout+".1 >"+tmpout+".2\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
23 # set n/a to 0
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
24 tmp.write("sed 's/n\/a/0/g' "+tmpout+".2 >"+tmpout+".3\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
25 # replace text with 0
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
26 zeros = ''.join(['0\t']*int(nbin))
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
27 tmp.write("sed 's/^[a-zA-Z]/"+zeros+"/' "+tmpout+".3 >"+tmpout+".4\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
28 # cut the first nbin columns
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
29 tmp.write("cut -f 1-"+nbin+" "+tmpout+".4 > "+tmpout+".5\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
30 tmp.write("paste "+intvfile+" "+tmpout+".5 >"+outfile+"\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
31 tmp.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
32 os.system('chmod +x '+tmpsh)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
33 os.system('./'+tmpsh)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
34 #os.system('rm '+tmpout+'*')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
35 #os.system('rm '+tmpsh)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
36
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
37 # strandness: need to reverse bins for - strand
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
38 if nbin > 1 and strand > 0:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
39 strand = strand - 1 # python is 0 based
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
40 os.system('mv '+outfile+' '+tmpout)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
41 f = open(tmpout)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
42 out = open(outfile,'w')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
43 for line in f:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
44 flds=line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
45 if flds[strand] == '+':
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
46 out.write(line)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
47 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
48 scores = flds[-int(nbin):]
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
49 scores.reverse()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
50 flds = flds[:-int(nbin)]+scores
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
51 out.write('\t'.join(flds)+'\n')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
52 os.system('rm '+tmpout)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
53 f.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
54 out.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
55 # plot
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
56 if int(nbin) > 1:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
57 rscript = open(tmpsh,"w")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
58 rscript.write("options(warn=-1)\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
59 rscript.write("x <- read.table('"+outfile+"',sep='\t')\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
60 rscript.write("x <- x[,(ncol(x)+1-"+nbin+"):ncol(x)]\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
61 rscript.write("pdf('"+outplot+"')\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
62 rscript.write("avg <- apply(x,2,mean)\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
63 rscript.write("err <- apply(x,2,sd)/sqrt(nrow(x))\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
64 rscript.write("ylim=c(min(avg-err),max(avg+err))\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
65 rscript.write("xticks <- seq(ncol(x))-(1+ncol(x))/2\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
66 if span >= 0.1:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
67 rscript.write("avg = loess(avg~xticks,span="+str(span)+")$fitted\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
68 rscript.write("err = loess(err~xticks,span="+str(span)+")$fitted\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
69 rscript.write("par(cex=1.5)\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
70 rscript.write("plot(xticks,avg,ylab='average conservation score',xlab='relative position (bin)',type='l',lwd=0,ylim=ylim)\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
71 rscript.write("polygon(c(xticks,rev(xticks)),c(avg+err,rev(avg-err)),col='slateblue1',border=NA)\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
72 rscript.write("lines(xticks,avg,type='l',lwd=1)\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
73 rscript.write("dev.off()\n")
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
74 rscript.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
75 os.system("R --vanilla < "+tmpsh)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
76 os.system("rm "+tmpsh)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
77
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
78 getScore(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5],int(sys.argv[6]),sys.argv[7],float(sys.argv[8]))