annotate mytools/metaintv_ext.py @ 0:39217fa39ff2

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