-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcompSpecScoreVsOtCount.py
234 lines (193 loc) · 7.77 KB
/
compSpecScoreVsOtCount.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# compare guide specificity scores against ot counts
# based on compareMitCrisporSpecScore.py
# XX currently recalculating the MIT scores. Maybe use the originals (once the site is working again)
from annotateOffs import *
import matplotlib.pyplot as plt
from collections import defaultdict
from os.path import isfile
import pickle
import numpy as np
# save time-intensive scores between invocations
TMPFNAME = "/tmp/guideSpecScores.pickle"
# size expansion factor for bubbles
BUBBLEFAC = 200.0
# expansion factor for very small bubbles
SMALLFAC= 15.0
def parseOtCounts(fname):
" return a tuple of three dicts strongOtCount, weakOtCounts, offtargetSum, each is : guideName -> float "
strongOffs = defaultdict(int)
weakOffs = defaultdict(int)
otShareSum = defaultdict(float)
for row in iterTsvRows(fname):
rf = float(row.readFraction)
if rf>0.01:
strongOffs[row.name]+=1
#if rf>0.001:
weakOffs[row.name]+=1
otShareSum[row.name]+=rf
return strongOffs, weakOffs, otShareSum
def splitXyzVals(xVals, yVals, zVals, zCutoff):
""" split three lists into two sets of lists, depending on the yVal
stupid hack, numpy array would be one line.
"""
x1, y1, z1 = [], [], []
x2, y2, z2 = [], [], []
for x,y,z in zip(xVals, yVals, zVals):
if z <= zCutoff:
x2.append(x)
y2.append(y)
z2.append(z)
else:
x1.append(x)
y1.append(y)
z1.append(z)
return (x1, y1, np.array(z1)), (x2, y2, np.array(z2))
def makePlot(xVals, yVals, areas):
(highX, highY, highZ), (lowX, lowY, lowZ) = splitXyzVals(xVals, yVals, areas, 0.005)
#print lowX, lowY, lowZ, 200*lowZ
#edgecolor='none', \
plt.scatter(lowX, lowY, \
alpha=.7, \
marker="x", \
edgecolor="black", \
s=BUBBLEFAC*lowZ*SMALLFAC)
#print highX, highY, highZ
plt.scatter(highX, highY, \
alpha=.7, \
marker="o", \
s=BUBBLEFAC*highZ)
#plt.scatter(xVals, yVals, \
#alpha=.7, \
#marker="o", \
#s=BUBBLEFAC*np.array(areas))
plt.xticks(range(0, 101, 10))
plt.xlim(0,100)
plt.ylim(0,70)
# invisible markers, needed for legend
legPlots = []
for frac in [0.005, 0.01, 0.05, 0.10, 0.30, 0.5, 0.7, 0.9]:
fracChar = "o"
size = BUBBLEFAC*frac
if frac == 0.005:
fracChar = "x"
size *= SMALLFAC
#if frac == 0.005:
#fracChar = "x"
#size *= 10
legPlots.append(
plt.scatter([],[], s=size, edgecolors='blue', marker=fracChar, lw=1),
)
# add legend
leg1 = plt.legend(legPlots, ["0.1%", "0.5%", "1%", "5%", "10%", "30%", "50%", "70%", "90%"],
#loc='upper right',
bbox_to_anchor=(1.15, 1), loc=2, borderaxespad=0., \
ncol=1,
fontsize=10, scatterpoints=1, title="Sum of\noff-target\nmodification\nfrequencies")
plt.setp(leg1.get_title(),fontsize='small')
return leg1
def parseSpecScores(fname, cacheFname):
""" parse a file with (seq,specScore) and return a list 0,10 with the percentage for each bin
As this is somewhat slow, cache the result in a temp file.
"""
print "parsing", fname
if isfile(cacheFname):
print "reading score histogram from temp file %s" % cacheFname
return pickle.load(open(cacheFname))
hist = [0] * 10
totalCount = 0
for line in open(fname):
if "None" in line:
continue
score = int(line.rstrip("\n").split()[1])
if score==100:
score=99
binIdx = score/10
hist[binIdx]+=1
totalCount += 1
xVals = range(0, 100, 10)
yVals = [100*(float(x)/totalCount) for x in hist]
#print fname, xVals, yVals
pickle.dump((xVals, yVals), open(cacheFname, "w"))
return xVals, yVals
def main():
maxMismatches = 4
guideValidOts, guideSeqs = parseOfftargets("out/annotFiltOfftargets.tsv", maxMismatches, False, None)
# get sum of off-target frequencies
strongOtCounts, weakOtCounts, otShareSum = parseOtCounts("out/annotFiltOfftargets.tsv")
histXVals, histYVals = parseSpecScores("wholeGenome/specScores.tab", "/tmp/crisporCache.pickle")
mitHistXVals, mitHistYVals = parseSpecScores("seleniumMit/seqScores.txt", "/tmp/mitCache.pickle")
if not isfile(TMPFNAME):
crisporOffs = parseCrispor("crisporOfftargets", guideSeqs, maxMismatches)
mitOffs = parseMit("mitOfftargets", guideSeqs)
scoreCache = {}
else:
print "Not recalculating guide scores. Reading guide scores from %s" % TMPFNAME
scoreCache = pickle.load(open(TMPFNAME))
ofh = open("out/specScoreVsOtCount.tsv", "w")
headers = ["guide", "CRISPORSpecScore", "MITSpecScore", "strongOtCount", "weakOtCount"]
ofh.write("\t".join(headers)+"\n")
xValsCrispor = []
xValsMit = []
yValsWeak = []
yValsStrong = []
areas = [] # size of the dots in the plot, one per xVal
rows = []
for guideName, guideSeq in guideSeqs.iteritems():
if guideName in scoreCache:
mitScore, crisporScore = scoreCache[guideName]
else:
mitScore = calcMitGuideScore_offs(guideSeq, mitOffs[guideSeq])
crisporScore = calcMitGuideScore_offs(guideSeq, crisporOffs[guideSeq])
scoreCache[guideName] = (mitScore, crisporScore)
weakOtCount = weakOtCounts[guideName]
strongOtCount = strongOtCounts[guideName]
row = [guideName, crisporScore, mitScore, weakOtCount, strongOtCount]
xValsCrispor.append(crisporScore)
xValsMit.append(mitScore)
yValsWeak.append(weakOtCount)
yValsStrong.append(strongOtCount)
areas.append(otShareSum[guideName])
row = [str(x) for x in row]
rows.append(row)
rows.sort()
for row in rows:
ofh.write( "\t".join(row)+'\n')
ofh.close()
print "output written to %s" % ofh.name
pickle.dump(scoreCache, open(TMPFNAME, "w"))
plt.figure(figsize=(5,5))
#axy1 = plt.subplot(121)
leg1 = makePlot(xValsMit, yValsWeak, areas)
xlab = plt.xlabel("MIT Specificity Score")
plt.ylabel("Off-targets found per guide sequence", color="black")
ax1b = plt.twinx()
ax1b.bar(mitHistXVals, mitHistYVals, 10, edgecolor='white', color="lightblue" , alpha=0.4, lw=1)
ax1b.set_ylim(0,25)
ylab = ax1b.set_ylabel('Frequency of specificity in exons (unique 20mers)', color="grey")
#ax2 = plt.subplot(122, sharey=axy1)
#plt.setp( ax2.get_yticklabels(), visible=False)
#plt.ylim(0,60)
plotFname = "out/specScoreVsOtCount-MIT.pdf"
plt.savefig(plotFname, format = 'pdf', bbox_extra_artists=(leg1,xlab,ylab), bbox_inches='tight')
plt.savefig(plotFname.replace(".pdf", ".png"), bbox_extra_artists=(leg1,), bbox_inches='tight')
print "wrote plot to %s, added .png" % plotFname
plt.close()
# make the CRISPOR plot
plt.figure(figsize=(5,5))
leg1 = makePlot(xValsCrispor, yValsWeak, areas)
xlab = plt.xlabel("CRISPOR Specificity Score")
plt.ylabel("Off-targets found per guide sequence", color="black")
# add 2nd y axis and plot histogram
ax2b = plt.twinx()
ylab = ax2b.set_ylabel('Frequency of specificity in exons (unique 20mers)', color="grey")
ax2b.set_ylim(0,20)
ax2b.bar(histXVals, histYVals, 10, edgecolor='white', color="lightblue" , alpha=0.4, lw=1)
#plt.tight_layout()
#plt.tight_layout()
# plt.subplots_adjust(hspace=0) # doesn't work
plotFname = "out/specScoreVsOtCount-CRISPOR.pdf"
plt.savefig(plotFname, format = 'pdf', bbox_extra_artists=(leg1,xlab,ylab), bbox_inches='tight')
plt.savefig(plotFname.replace(".pdf", ".png"), bbox_extra_artists=(leg1,), bbox_inches='tight')
print "wrote plot to %s, added .png" % plotFname
plt.close()
main()