-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathplotEffQuintiles.py
298 lines (250 loc) · 10.5 KB
/
plotEffQuintiles.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# plot the quintiles of all scores against all dataset, like the figure in the Doench 2015 paper
import os, logging
logging.basicConfig(loglevel=logging.INFO)
from os.path import isfile, splitext, join
from annotateOffs import *
from collections import defaultdict
logging.basicConfig(loglevel=logging.INFO)
from scipy.stats import linregress
from scipy.stats import gaussian_kde
from numpy.random import normal
from numpy import arange
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['pdf.fonttype'] = 42
import matplotlib.pyplot as plt
import numpy as np
# sqlUcsc hgcentral -e 'select * from blatServers where db="mm9"'
blatServers = {
"hg19": ("blat4a", "17779"),
"danRer10" : ("blat4c", "17863"),
"mm9" : ("blat4c", "17779")
}
def extend23Mers(seqs, db):
""" extend 23mers to 30mers for eff score calculations, seqs is a dict id -> seq
return a dict seqId -> list of (seq, genomePositionString)
"""
# write 23mers to fa file
inNames = set()
ofh = open("/tmp/temp.fa", "w")
for seqId, seq in seqs.iteritems():
ofh.write(">%s\n%s\n" % (seqId, seq))
inNames.add(seqId)
ofh.close()
print "running BLAT, writing to /tmp/temp.bed"
blatServer, port = blatServers[db]
cmd = "gfClient %s.soe.ucsc.edu %s /gbdb/%s /tmp/temp.fa /tmp/temp.psl -minScore=20 -nohead -minIdentity=100 -maxIntron=0 -dots=1 ; pslToBed /tmp/temp.psl /tmp/temp.bed" % (blatServer, port, db)
os.system(cmd)
matches = defaultdict(list) # seqId -> list of (chrom, start, end, strand)
for line in open("/tmp/temp.bed"):
chrom, start, end, name, score, strand = line.split()[:6]
if "_hap" in chrom or "random" in chrom:
continue
##print int(end)-int(start)
if (int(end)-int(start))!=23:
#print "SKIP", line
continue
matches[name].append( (chrom, int(start), int(end), strand) )
notFoundNames = inNames - set(matches)
logging.warn("These sequences were not found with BLAT: %s" % ",".join(notFoundNames))
#assert( len(seqs) == len(matches) )
# write matches to temp.bed file
# SSC needs extension by +7 bp of the end position
# Doench needs extension -4 of the start and +3 of the end pos
print "Creating /tmp/tempExt.bed with extended matches"
ofh = open("/tmp/tempExt.bed", "w")
positions = []
for seqId, matchTuples in matches.iteritems():
if len(matchTuples)>1:
logging.error("Multiple matches for %s, will require manual selection" % seqId)
logging.error("%s" % matchTuples)
for matchTuple in matchTuples:
chrom, start, end, strand = matchTuple
if strand=="+":
start = start - 4
end = end + 7
else:
start = start - 7
end = end + 4
row = [chrom, str(start), str(end), seqId, "0", strand]
ofh.write("\t".join(row)+"\n")
positions.append( "%s:%d-%d:%s" % (chrom, start, end, strand))
ofh.close()
cmd = "twoBitToFa /gbdb/%s/%s.2bit -bed=/tmp/tempExt.bed /tmp/tempExt.fa" % (db, db)
os.system(cmd)
seqs = parseFastaAsList(open("/tmp/tempExt.fa"))
assert(len(seqs)==len(positions))
ret = defaultdict(list)
for seqData, pos in zip(seqs, positions):
seqId, seq = seqData
ret[seqId].append( (seq, pos) )
return ret
def extendTabAddContext(fname, db):
"""
add a column to a tab file with a seq column: new column is called seqExt with 34 mers
additional column is pos, with chrom:start-end:strand
"""
newFname = fname.replace(".tab", ".ext.tab")
if isfile(newFname):
logging.info("not recreating %s, already exists. Delete to recreate" % newFname)
return newFname
seqs = dict()
for row in iterTsvRows(fname):
print "XX row", row
seqs[row.guide] = row.seq
seqPosDict = extend23Mers(seqs, db)
ofh = open(newFname, "w")
ofh.write("\t".join(row._fields)+"\t")
ofh.write("extSeq\tposition\n")
for row in iterTsvRows(fname):
guideName = row.guide
for seqPos in seqPosDict[guideName]:
seq, pos = seqPos
ofh.write("\t".join(row)+"\t"+seq+"\t"+pos+"\n")
ofh.close()
print "Wrote result to %s" % ofh.name
return newFname
def addDoenchAndScs(fname):
" given tab file with extSeq column, return scs and doench scores "
seqs = []
for row in iterTsvRows(fname):
seqs.append(row.extSeq)
return calcEffScores(seqs)
#binCount = 5
binCount = 4
def violin_plot(ax,data,pos, bp=False):
'''
create violin plots on an axis
'''
dist = max(pos)-min(pos)
w = min(0.15*max(dist,1.0),0.5)
for d,p in zip(data,pos):
k = gaussian_kde(d) #calculates the kernel density
m = k.dataset.min() #lower bound of violin
M = k.dataset.max() #upper bound of violin
x = arange(m,M,(M-m)/100.) # support for violin
v = k.evaluate(x) #violin profile (density curve)
v = v/v.max()*w #scaling the violin to the available space
ax.fill_betweenx(x,p,v+p,facecolor='y',alpha=0.3)
ax.fill_betweenx(x,p,-v+p,facecolor='y',alpha=0.3)
if bp:
ax.boxplot(data,notch=1,positions=pos,vert=1)
def emptyLists(c):
" return a list of x empty lists "
ret = []
for i in range(0, c):
ret.append([])
return ret
def plotQuintiles(ax, scores, scoreType, extFname, title):
" create barplots "
xVals = []
yVals = []
for row in iterTsvRows(extFname):
y = float(row.modFreq)
x = scores[row.extSeq][scoreType]
xVals.append(x)
yVals.append(y)
xVals = convToRankPerc(xVals)
yVals = convToRankPerc(yVals)
# bin based on x value
scoreBins = emptyLists(binCount)
binSize = 1.0/binCount
for x, y in zip(xVals, yVals):
scoreBin = int(x/binSize)
print x, y, scoreBin
scoreBins[scoreBin].append(y)
#for i in range(0, binCount):
#print i, len(scoreBins[i]), scoreBins[i][:3]
binLabels = range(0, binCount)
#violin_plot(ax, scoreBins, binLabels)
# for each bin, bin again based on y-Value
scoreActBins = [] # list of lists. One list per score bin. In this list, one list per activity bin
for scoreBin, actVals in enumerate(scoreBins):
actBins = emptyLists(binCount)
for y in actVals:
actBin = int(y / binSize) # should this be round() instead?
actBins[actBin].append(y)
# convert bins to frequencies
actFreqs = []
for actBin in actBins:
if len(actVals)!=0:
actFreq = len(actBin)/float(len(actVals))
else:
actFreq = 0.0
actFreqs.append(actFreq)
scoreActBins.append(actFreqs)
print "actFreqs for bin", scoreBin, actFreqs
colors = [0.9, 0.75, 0.5, 0.3, 0.1]
colors = [str(x) for x in colors]
currentSums = [0]*binCount
for actBinIdx in reversed(range(0, binCount)):
# get all vals for a given activity bin, plotting works by y-bin
binVals = []
for scoreBins in scoreActBins:
binVals.append(scoreBins[actBinIdx])
color = colors[actBinIdx]
print "binVals for color", color, actBinIdx, binVals
ax.bar(binLabels, binVals, color=color, linewidth=0, bottom=currentSums)
currentSums = currentSums+np.array(binVals)
def extendTabAddScores(extFname, scores, scoreNames, outFname):
" add columns for efficiency scores and write to extFname "
#outFname = splitext(extFname)[0]+".scores.tab"
ofh = None
for row in iterTsvRows(extFname):
if ofh==None:
ofh = open(outFname, "w")
ofh.write("\t".join(row._fields))
ofh.write("\t")
ofh.write("\t".join(scoreNames))
ofh.write("\n")
ofh.write("\t".join(row)+"\t")
rowScores = []
for name in scoreNames:
rowScores.append(scores[row.extSeq][name])
ofh.write("\t".join([str(x) for x in rowScores]))
ofh.write("\n")
ofh.close()
print "wrote data to %s" % ofh.name
def plotDataset(datasetName, ax, db, title, addColLabels=False):
inFname = join("effData/"+datasetName+".tab")
extFname = extendTabAddContext(inFname, db)
scores = addDoenchAndScs(extFname)
extendTabAddScores(extFname, scores, ["ssc", "doench", "svm", "chariRaw"], "out/%s-compEffData.tsv" % datasetName)
#ax[0].set_ylabel(yLabel)
plotQuintiles(ax[0], scores, "svm", extFname, "SVM Score from Wang et al. 2014")
plotQuintiles(ax[1], scores, "doench", extFname, "Score from Doench et al. 2014")
plotQuintiles(ax[2], scores, "ssc", extFname, "SSC Score from Xu et al. 2015")
plotQuintiles(ax[3], scores, "chariRaw", extFname, "SVM Score from Chari et al. 2015")
if addColLabels:
ax[0].set_title("SVM score from Wang et al. 2014")
ax[1].set_title("Score from Doench et al. 2014")
ax[2].set_title("Score from Xu et al. 2015")
ax[3].set_title("Score from Chari et al. 2015")
# put the row desc into the left border
# http://stackoverflow.com/questions/25812255/row-and-column-headers-in-matplotlibs-subplots
ax[0].annotate(title, xy=(0, 0.5), xytext=(-ax[0].yaxis.labelpad - 5, 0), \
xycoords=ax[0].yaxis.label, textcoords='offset points', \
#textcoords='offset points', \
size='medium', ha='right', va='top')
def main():
#"XuData/modFreq.tab"
#extendTabAddContext("temp.tab")
plotFname = "out/effQuintiles.pdf"
fig, axArr = plt.subplots(7, 4, sharey="row", sharex="col")
fig.set_size_inches(20,30)
plotDataset("xu2015", axArr[0], "hg19", "Xu 2015, validation data\ntwo methods, FOX/AR sequencing, AAVS measured protein KO", addColLabels=True)
plotDataset("varshney2015", axArr[1], "danRer10", "Varshney 2015\nZebrafish, special injection method?")
plotDataset("gagnon2014", axArr[2], "danRer10", "Gagnon 2014, Zebrafish\nwhy so low?")
plotDataset("xu2015Train", axArr[3], "danRer10", "Xu 2015 training data\ntaken from\nwhich other paper?")
plotDataset("doench2014-S10-414Genes", axArr[4], "hg19", "Zhang 2014 (?)\n also used by Doench?")
plotDataset("doench2014-Hs", axArr[5], "hg19", "Doench 2014, human")
plotDataset("doench2014-Mm", axArr[6], "mm9", "Doench 2014, mouse")
fig.tight_layout()
fig.subplots_adjust(left=0.15, top=0.95)
#fig.set_title(title)
fig.savefig(plotFname, format = 'pdf')
fig.savefig(plotFname.replace(".pdf", ".png"))
print "wrote plot to %s, added .png" % plotFname
plt.close()
if __name__=="__main__":
main()