annotate tools/mytools/metaintv_ext.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 get binned score of intervals,allow extension
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import os,sys,numpy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import string, random
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from resize import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 from bx.bbi.bigwig_file import BigWigFile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 def binning(x,n):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 # make n bin of x
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 y = numpy.zeros(n,dtype=float)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 if len(x) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 return y
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 step = float(len(x))/n
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 for k in range(n):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 i = int(step*k)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 j = int(step*(k+1)) + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 y[k] = x[i:j].mean()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 #print i,j,k,y[k]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 return y
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 def getBinnedScore(bwfile,intvfile,nbin):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 get binned average and std
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 fbw = open(bwfile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 bw = BigWigFile(file=fbw)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 fin = open(intvfile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 avg = numpy.zeros(nbin)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 sqr = numpy.zeros(nbin)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 N = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 for line in fin:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 #print N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 #chrom,start,end,name,score,strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 #get the score at base resolution as an array
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 scores = bw.get_as_array(flds[0],int(flds[1]),int(flds[2]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 if scores == None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 print 'not found:\t',N,line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 N = N + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 #print line,scores
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 # reverse if on minus strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if flds[5] == '-':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 scores = scores[::-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 # no score = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 scores = numpy.nan_to_num(scores)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 # bin the data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 binned = binning(scores,nbin)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 avg = avg + binned
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 sqr = sqr + binned**2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 # compute avg and std
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 avg = avg / N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 err = ((sqr/N-avg**2)**0.5)/(N**0.5)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 return avg,err,N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 def getExtendedBinScore(bwfile,intvfile,nbins,exts):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 nbins: n1,n2,n3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 exts: l1,l2,l3,l4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 avg = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 err = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 tmpfile = "".join(random.sample(string.letters+string.digits, 8))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if exts[0]>0 or exts[1]>0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 print 'make left extension'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 resize(intvfile,tmpfile,'start-'+str(exts[0]),'start+'+str(exts[1]),'stranded')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 print 'compute binned average'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 avg,err,N = getBinnedScore(bwfile,tmpfile,nbins[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 print 'regions used:',N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 print 'make center region'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 resize(intvfile,tmpfile,'start+'+str(exts[1]),'end-'+str(exts[2]),'stranded')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 print 'compute binned average'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 avg1,err1,N = getBinnedScore(bwfile,tmpfile,nbins[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 print 'regions used:',N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 avg = numpy.concatenate((avg,avg1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 err = numpy.concatenate((err,err1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 if exts[2]>0 or exts[3]>0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 print 'make right region'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 resize(intvfile,tmpfile,'end-'+str(exts[2]),'end+'+str(exts[3]),'stranded')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 print 'compute binned average'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 avg2,err2,N = getBinnedScore(bwfile,tmpfile,nbins[2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 print 'regions used:',N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 avg = numpy.concatenate((avg,avg2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 err = numpy.concatenate((err,err2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 os.system('rm '+tmpfile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 return avg,err
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 prog,bwfile,intvfile,exts,nbins,outfile,outplot = sys.argv
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 nbins = numpy.fromstring(nbins,dtype=int,sep=',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 exts = numpy.fromstring(exts,dtype=int,sep=',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 avg, err = getExtendedBinScore(bwfile,intvfile,nbins,exts)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 print 'save data'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 out = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 numpy.savetxt(out, avg, fmt='%.6f', delimiter=' ', newline=' ')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 out.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 numpy.savetxt(out, err, fmt='%.6f', delimiter=' ', newline=' ')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 out.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 print 'plot'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 start = exts[0]*nbins[0]/(exts[0]+exts[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 end = nbins[0]+nbins[1]+exts[2]*nbins[2]/(exts[2]+exts[3])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 #print start,end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 rscript = open("tmp.r","w")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 rscript.write("options(warn=-1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 rscript.write("x <- read.table('"+outfile+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 rscript.write("pdf('"+outplot+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 rscript.write("avg <- x[1,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 rscript.write("err <- x[2,]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 #rscript.write("print(x)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 rscript.write("ylim=c(min(avg-err),max(avg+err))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 rscript.write("xticks <- seq(ncol(x))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 #rscript.write("print(xticks)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 rscript.write("plot(xticks,avg,xlab='',ylab='average coverage',type='l',lwd=0,ylim=ylim,xaxt='n')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 rscript.write("axis(1, at=c(min(xticks),"+str(start)+","+str(end)+",max(xticks)),labels=c(-"+str(exts[0])+",0,0,"+str(exts[3])+"), las=2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 rscript.write("polygon(c(xticks,rev(xticks)),c(avg+err,rev(avg-err)),col='lightgreen',border=NA)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 rscript.write("lines(xticks,avg,type='l',lwd=1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 rscript.write("lines(c(min(xticks),max(xticks)),c(0,0),lwd=2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 rscript.write("lines(c("+str(start)+","+str(end)+"),c(0,0),lwd=10)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 rscript.write("dev.off()\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 rscript.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 os.system("R --vanilla --slave < tmp.r")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 os.system("rm tmp.r")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128