annotate mytools/intersectSig.py @ 9:87eb5c5ddfe9

Uploaded
author xuebing
date Fri, 09 Mar 2012 20:01:43 -0500
parents f0dc65e7f6c0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
1 '''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
2 find overlap and test signifiance
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
3 '''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
4
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
5 import os,sys
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
6
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
7 def lineCount(filename):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
8 if os.stat(filename).st_size == 0:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
9 return 0
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
10 with open(filename) as f:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
11 for i, l in enumerate(f):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
12 pass
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
13 print i
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
14 return i+1
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
15
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
16 def intersect(fileA,fileB,outfile,fraction,reciprocal):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
17 # return fileA intervals that overlap with interval in fileB
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
18 cmd = 'intersectBed -a '+fileA+' -b '+fileB + ' -u -wa -f '+fraction +' '+ reciprocal + '>'+outfile
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
19 #print cmd
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
20 os.system(cmd)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
21
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
22 def shuffle(fileA,fileB,genomefile,fraction,reciprocal,N):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
23 # shuffle fileA N times, return the distribution of overlaps
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
24 nOverlap = []
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
25 for i in range(N):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
26 # shuffle fileA using shuffleBed
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
27 #cmd = 'shuffleBed -i '+fileA+' -g '+genomefile +'>fileA.shuffled'
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
28 # using random_interval.py
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
29 cmd = 'python /Users/xuebing/galaxy-dist/tools/mytools/random_interval.py '+fileA+' fileA.shuffled across '+genomefile
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
30 os.system(cmd)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
31 intersect('fileA.shuffled',fileB,'tmp',fraction,reciprocal)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
32 nOverlap.append(lineCount('tmp'))
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
33 os.system('rm tmp')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
34 os.system('rm fileA.shuffled')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
35 return nOverlap
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
36
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
37 def main():
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
38 fileA = sys.argv[1]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
39 fileB = sys.argv[2]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
40 outfile = sys.argv[3]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
41 outplot = sys.argv[4]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
42 outshuffle = sys.argv[5]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
43 N = int(sys.argv[6]) # times to shuffle
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
44 genomefile = sys.argv[7]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
45 fraction = sys.argv[8]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
46 if len(sys.argv) == 10:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
47 reciprocal = sys.argv[9] # can only be '-r'
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
48 else:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
49 reciprocal = ''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
50
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
51 #print sys.argv
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
52
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
53 # number of lines in input
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
54 nA = lineCount(fileA)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
55 nB = lineCount(fileB)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
56
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
57 # intersect on real data
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
58 intersect(fileA,fileB,outfile,fraction,reciprocal)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
59 # number of overlaps
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
60 nOverlapReal = lineCount(outfile)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
61
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
62 #print 'number of intervals in inputA that overlap with intervals in inputB:',nOverlapReal
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
63
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
64 # shuffle fileA to estimate background
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
65 nOverlapNull = shuffle(fileA,fileB,genomefile,fraction,reciprocal,N)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
66 out = open(outshuffle,'w')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
67 out.write("\t".join(map(str,nOverlapNull)))
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
68 out.close()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
69
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
70 # plot histogram
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
71 rscript = open('tmp.r','w')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
72 rscript.write("options(warn=-1)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
73 rscript.write("x0 <- "+str(nOverlapReal)+"\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
74 rscript.write("x <- c("+','.join(map(str,nOverlapNull))+")\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
75 rscript.write("library(MASS)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
76 rscript.write("pv <- min((1+sum(x>=x0))/length(x),(1+sum(x<=x0))/length(x))\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
77 rscript.write("title <- paste('actual:chance = ',x0,':',format(mean(x),digits=1,nsmall=1),' = ',format(x0/mean(x),digits=1,nsmall=2),', p-value < ',pv,sep='')\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
78 rscript.write("pdf('"+outplot+"')\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
79 rscript.write("library(grid)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
80 rscript.write("library(VennDiagram)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
81 rscript.write("venn <- venn.diagram(x=list(A=1:"+str(nA)+",B="+str(nA-nOverlapReal+1)+":"+str(nA+nB-nOverlapReal)+"),filename=NULL,fill=c('red','blue'),col='transparent',alpha=0.5,label.col='black',cex=3,lwd=0,fontfamily='serif',fontface='bold',cat.col = c('red', 'blue'),cat.cex=3,cat.fontfamily='serif',cat.fontface='bold')\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
82 rscript.write("grid.draw(venn)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
83 rscript.write("h <- hist(x,breaks=50,xlab='number of overlaps',ylab='frequency',main=title)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
84 rscript.write("plot(h$mids,h$counts,type='h',xlim=c(min(h$mids,x0),max(x0,h$mids)),ylim=c(0,max(h$counts)),xlab='number of overlaps',ylab='frequency',main=title)\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
85 rscript.write("points(x0,0,col='red')\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
86 rscript.write("dev.off()\n")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
87 rscript.close()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
88 os.system("R --vanilla < tmp.r")
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
89 os.system('rm tmp.r')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
90 main()