annotate tools/mytools/intersectSig.py @ 3:33c067c3ae34

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