-
Notifications
You must be signed in to change notification settings - Fork 2
/
drawVenn.py
executable file
·156 lines (116 loc) · 5.97 KB
/
drawVenn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
#uses pygooglechart module via Google Chart API (http://code.google.com/apis/chart/)
# http://pygooglechart.slowchop.com/
#requires network connection?
from pygooglechart import VennChart
from types import *
from sys import *
from os.path import basename
import fisher
def twodigithex(I):
hexString=hex(I)
if hexString[0:2]!="0x":
print >> stderr,"wrong hex conversion from",I,"to",hexString,"abort"
exit(1)
hexString=hexString[2:]
if len(hexString)<2:
hexString="0"+hexString
return hexString.upper()
def drawTwoVenn(set1,set2,intersect,width,height,outfile,fisherU=None,title=None,legend=None,indicateNumberInLegend=False,colors=None,htmlReport=None):
chart=VennChart(width,height)
chart.add_data([set1,set2,0,intersect])
if title:
chart.set_title(title)
if legend:
legendOut=legend[:]
if indicateNumberInLegend:
legendOut[0]=legend[0]+" ("+str(set1)+")"
legendOut[1]=legend[1]+" ("+str(set2)+")"
chart.set_legend(legendOut)
if colors:
for i in range(0,len(colors)):
if type(colors[i]) is not StringType:
#probably a int tuple (4 or 3)
#print >> stderr,colors[i],"is not string"
if type(colors[i]) is ListType or type(colors[i]) is TupleType:
#print >> stderr,"is list or tuple"
hexCode=""
for color_component in colors[i]:
hexCode+=twodigithex(color_component)
colors[i]=hexCode
#print >> stderr,colors
chart.set_colours(colors)
chart.download(outfile)
if fisherU:
#build a contingency table 2x2
box1=intersect
box2=set2-intersect
box3=set2
box6=fisherU-set2
box7=set1
box8=fisherU-set1
box9=fisherU
box4=set1-intersect
box5=fisherU-set1-set2+intersect
fisher_pvalues=fisher.pvalue(box1,box2,box4,box5)
expectedIntersect=round(float(set1)*set2/fisherU)
if htmlReport:
if not title:
title=""
if not legend:
legend=["set1","set2"]
fout = open(htmlReport,"w")
print >> fout, '<html><head><title>%s</title></head><body><h1>%s</h1><table width=%d><tr><td colspan=2><img src="%s"></td></tr><tr bgcolor="#AAAAAA"><td>%s</td><td>%d</td></tr><tr><td>%s</td><td>%d</td></tr> <tr bgcolor="#AAAAAA"> <td>%s only</td><td>%d</td></tr> <tr><td>%s only</td><td>%d</td></tr> <tr bgcolor="#AAAAAA"> <td>%s ∩ %s</td><td>%d</td></tr> ' %(title,title,width,basename(outfile),legend[0],set1,legend[1],set2,legend[0],set1-intersect,legend[1],set2-intersect,legend[0],legend[1],intersect)
if fisherU:
print >> fout, '<tr><td>Universe</td><td>%d</td></tr> <tr bgcolor="#AAAAAA"> <td>E[%s ∩ %s]</td><td>%d</td></tr> <tr><td>dnrichment p-value</td><td>%g</td></tr> <tr bgcolor="#AAAAAA"> <td>depletion p-value</td><td>%g</td></tr>' %(fisherU,legend[0],legend[1],expectedIntersect,fisher_pvalues.right_tail,fisher_pvalues.left_tail)
print >> fout, '</body></html>'
fout.close()
def drawThreeVenn(set1,set2,set3,set1set2Intersect,set1set3Intersect,set2set3Intersect,allIntersect,width,height,outfile,title=None,legend=None,indicateNumberInLegend=False,colors=None,htmlReport=None):
chart=VennChart(width,height)
chart.add_data([set1,set2,set3,set1set2Intersect,set1set3Intersect,set2set3Intersect,allIntersect])
if title:
chart.set_title(title)
if legend:
legendOut=legend[:]
if indicateNumberInLegend:
legendOut[0]=legend[0]+" ("+str(set1)+")"
legendOut[1]=legend[1]+" ("+str(set2)+")"
legendOut[2]=legend[2]+" ("+str(set3)+")"
chart.set_legend(legendOut)
if colors:
for i in range(0,len(colors)):
if type(colors[i]) is not StringType:
#probably a int tuple (4 or 3)
#print >> stderr,colors[i],"is not string"
if type(colors[i]) is ListType or type(colors[i]) is TupleType:
#print >> stderr,"is list or tuple"
hexCode=""
for color_component in colors[i]:
hexCode+=twodigithex(color_component)
colors[i]=hexCode
#print >> stderr,colors
chart.set_colours(colors)
chart.download(outfile)
if htmlReport:
fout = open(htmlReport,"w")
set1only=set1-set1set2Intersect-set1set3Intersect+allIntersect
set2only=set2-set1set2Intersect-set2set3Intersect+allIntersect
set3only=set3-set1set3Intersect-set2set3Intersect+allIntersect
if not title:
title=""
if not legend:
legend=["set1","set2","set3"]
print >> fout, '<html><head><title>%s</title></head><body><h1>%s</h1><table width=%d><tr><td colspan=2><img src="%s"></td></tr><tr bgcolor="#AAAAAA"><td>%s</td><td>%d</td></tr><tr><td>%s</td><td>%d</td></tr> <tr bgcolor="#AAAAAA"> <td>%s</td><td>%d</td></tr> <tr> <td>%s only</td><td>%d</td></tr> <tr bgcolor="#AAAAAA"> <td>%s only</td><td>%d</td> </tr> <tr><td>%s only</td><td>%d</td> </tr> <tr bgcolor="#AAAAAA"> <td>%s ∩ %s</td><td>%d</td></tr> <tr> <td>%s ∩ %s</td><td>%d</td></tr> <tr bgcolor="#AAAAAA"> <td>%s ∩ %s</td><td>%d</td></tr> <tr> <td>%s ∩ %s ∩ %s </td><td>%d</td></tr> </body></html>' %(title,title,width,basename(outfile),legend[0],set1,legend[1],set2,legend[2],set3,legend[0],set1only,legend[1],set2only,legend[2],set3only,legend[0],legend[1],set1set2Intersect,legend[0],legend[2],set1set3Intersect,legend[1],legend[2],set2set3Intersect,legend[0],legend[1],legend[2],allIntersect)
fout.close()
def testTwoVenn():
drawTwoVenn(10,20,3,500,300,"testTwoVenn.png","Two venn testing",["A","B"],True,[[255,255,0],[0,255,255]],"testTwoVenn.htm")
def testThreeVenn():
drawThreeVenn(100,80,60,30,30,30,10,500,300,"testThreeVenn.png","Three venn testing",["A","B","C"],True,[[255,255,0],[0,255,255],[255,0,0]],"testThreeVenn.htm")
def printUsageAndExit(programName):
print >> stderr,programName,"--v2 set1 set2 set1AndSet2 [--out-png outfile] [--html-report htmlreport] [--l2 set1name set2name ] [--c2 set1color set2color ] [--int-num]"
print >> stderr,programName,"--v3 set1 set2 set3 set1AndSet2 set1AndSet3 set2AndSet3 set1AndSet2AndSet3 [--out-png outfile] [--html-report htmlreport] [--l3 set1name set2name set3name ] [--c3 set1color set2color set3color ] [--int-num]"
exit(1)
if __name__=='__main__':
#testTwoVenn()
#testThreeVenn()
pass