-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKmerFreq.py
373 lines (317 loc) · 12.8 KB
/
KmerFreq.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from KmerKounter import identifier
from KmerKounter import kmercount
from KmerKounter import mink
from KmerKounter import maxk
from KmerKounter import kmer2hash
from KmerKounter import hash2kmer
from KmerKounter import revnuc
from KmerKounter import revComp
from KmerKounter import revcompwanted
from KmerKounter import startround
from KmerKounter import totaldict
from KmerKounter import numofruns
from HamDistFig import top6all
import matplotlib.pyplot as plt
from matplotlib.transforms import TransformedBbox, Bbox
import numpy as np
import math
def makexaxis():
xaxis = ([])
for i in range((startround-1), numofruns+(startround-1)):
xaxis.append(i)
return xaxis
#print(top6all)
def colours1():
global colours
colours = {}
for k in range(mink, maxk+1):
top6s = top6all[numofruns][k]
#consensusnum = kmercount[numofruns][k][top6s[0]]
colours[k] = list(kmercount[numofruns][k].keys())[6:66]
#colours[k] = [j[0] for j in colours[k]]
for i in top6s:
if i in colours[k]:
colours[k].remove(i)
return colours
colours1()
def makeyaxis1a(i, k):
yaxis1a = ([])
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
top6s = top6all[numofruns][k]
for l in range(1, numofruns+1):
total = totaldict[l][k]
if i not in top6s or colours[k]:
try:
num = kmercount[l][k][i]
except:
num = 0
if i != rkmer:
try:
rnum = kmercount[l][k][rkmer]
except:
rnum = 0
else:
rnum = 0
f = (num+rnum)/total
freq = (f/(1-f))
yaxis1a.append(math.log10(freq))
return yaxis1a
def makeyaxis1b(i, k):
yaxis1b = ([])
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
for l in range(1, numofruns+1):
total = totaldict[l][k]
if i in colours[k]:
try:
num = kmercount[l][k][i]
except:
num = 0
if i != rkmer:
try:
rnum = kmercount[l][k][rkmer]
except:
rnum = 0
else:
rnum = 0
f = (num+rnum)/total
freq = (f/(1-f))
yaxis1b.append(math.log10(freq))
return yaxis1b
def makeyaxis1c(i, k):
yaxis1c = ([])
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
top6s = top6all[numofruns][k]
for l in range(1, numofruns+1):
total = totaldict[l][k]
if i in top6s:
try:
num = kmercount[l][k][i]
except:
num = 0
if i != rkmer:
try:
rnum = kmercount[l][k][rkmer]
except:
rnum = 0
else:
rnum = 0
f = (num+rnum)/total
freq = (f/(1-f))
if freq == 0:
freq = 1
yaxis1c.append(math.log10(freq))
return yaxis1c
def makeyaxis2a(i, k):
yaxis2a = ([])
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
top6s = top6all[numofruns][k]
for l in range(1, numofruns+1):
total = totaldict[l][k]
if i not in top6s or colours[k]:
try:
num = kmercount[l][k][i]
except:
num = 0
try:
rnum = kmercount[l][k][rkmer]
except:
rnum = 0
yaxis2a.append((num+rnum)/total)
return yaxis2a
def makeyaxis2b(i, k):
yaxis2b = ([])
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
for l in range(1, numofruns+1):
total = totaldict[l][k]
if i in colours[k]:
try:
num = kmercount[l][k][i]
except:
num = 0
try:
rnum = kmercount[l][k][rkmer]
except:
rnum = 0
yaxis2b.append((num+rnum)/total)
return yaxis2b
def makeyaxis2c(i, k):
yaxis2c = ([])
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
top6s = top6all[numofruns][k]
for l in range(1, numofruns+1):
total = totaldict[l][k]
if i in top6s:
try:
num = kmercount[l][k][i]
except:
num = 0
try:
rnum = kmercount[l][k][rkmer]
except:
rnum = 0
yaxis2c.append((num+rnum)/total)
return yaxis2c
def makeyaxis3(k):
yaxis3 = ([])
for l in range(1, numofruns+1):
total = totaldict[l][k]
yaxis3.append(total)
return yaxis3
def grapher(k):
xaxis = makexaxis()
last = (len(xaxis)-1)
top6s = top6all[numofruns][k]
fig = plt.figure(figsize=(10,10))
grid = plt.GridSpec(2,3,wspace=0.4,hspace=0.3)
top = fig.add_subplot(grid[:-1,:])
top.set_xlabel("SELEX round")
top.set_ylabel("log(f/(1-f))")
top.set_title("Kmer frequency"+', K: '+str(k))
top.set_xlim([((startround-1)-0.5), (numofruns+startround)])
top.set_xticks(np.linspace((startround-1)-0.5, (numofruns+startround), num=((2*(numofruns+1))+2), endpoint=True))
top.spines['right'].set_visible(False)
top.spines['top'].set_visible(False)
bottom = fig.add_subplot(grid[-1,:-1])
bottom.set_xlabel("SELEX round")
bottom.set_ylabel("f = (kmer/total)")
bottom.set_title("Kmer frequency")
bottom.set_xlim([((startround-1)-0.5), (numofruns+(startround-1))])
bottom.set_xticks(range((startround-1),(numofruns+1)))
bottom.spines['right'].set_visible(False)
bottom.spines['top'].set_visible(False)
bar = fig.add_subplot(grid[-1,-1:])
bar.set_xlabel("SELEX round")
bar.set_ylabel("Total kmers")
bar.set_title("Kmer total distribution")
bar.set_xticks(makexaxis())
colourslist = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']
if revcompwanted == False:
for l in range(1, (numofruns+1)):
hashkeys = list(kmercount[l][k].keys())
for x in range(0, 66):
i = hashkeys[x]
num = kmercount[l][k][i]
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
try:
rnum = kmercount[l][k][rkmer]
if num >= rnum:
if i in top6s:
yaxis1c = makeyaxis1c(i, k)
top.plot(xaxis, yaxis1c, color=colourslist[(top6s.index(i)//2)], linewidth=2, marker="s", markevery=None, zorder=(66-x))
if i in colours[k]:
yaxis1b = makeyaxis1b(i, k)
top.plot(xaxis, yaxis1b, linewidth=1, marker=".", markevery=None, zorder=(66-x))
except:
continue
if k <= 5:
step = 1
if k > 5:
step = int(round(len(hashkeys)/1000, 0))
for x in range(66, len(hashkeys), step):
i = hashkeys[x]
num = kmercount[l][k][i]
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
try:
rnum = kmercount[l][k][rkmer]
if num >= rnum:
yaxis1a = makeyaxis1a(i, k)
top.plot(xaxis, yaxis1a, color = '0.75', linestyle='--', linewidth=0.5, marker="x", markevery=None, alpha=0.5, zorder=0)
except:
continue
for l in range(1, (numofruns+1)):
hashkeys = list(kmercount[l][k].keys())
for x in range(0, 66):
i = hashkeys[x]
num = kmercount[l][k][i]
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
try:
rnum = kmercount[l][k][rkmer]
if num > rnum:
if i in top6s:
yaxis2c = makeyaxis2c(i, k)
bottom.plot(xaxis, yaxis2c, color=colourslist[(top6s.index(i)//2)], linewidth=2, marker="s", markevery=None, zorder=(66-x))
if i in colours[k]:
yaxis2b = makeyaxis2b(i, k)
bottom.plot(xaxis, yaxis2b, linewidth=1, marker=".", markevery=None, zorder=66-x)
except:
continue
if k <= 5:
step = 1
if k > 5:
step = int(round(len(hashkeys)/1000, 0))
for x in range(66, len(hashkeys), step):
i = hashkeys[x]
num = kmercount[l][k][i]
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
try:
rnum = kmercount[l][k][rkmer]
if num > rnum:
yaxis2a = makeyaxis2a(i, k)
bottom.plot(xaxis, yaxis2a, color = '0.75', linestyle='--', linewidth=0.5, marker="x", markevery=None, alpha=0.8, zorder=0)
except:
continue
if revcompwanted == True:
for l in range(1, (numofruns+1)):
alreadytop = []
for i in kmercount[l][k]:
num = kmercount[l][k][i]
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
try:
rnum = kmercount[l][k][rkmer]
if num >= rnum:
alreadytop.append(rkmer)
if i not in alreadytop:
if i in top6s:
yaxis1c = makeyaxis1c(i, k)
top.plot(xaxis, yaxis1c, color=colourslist[(top6s.index(i)//2)], linewidth=2, marker="s", markevery=None, zorder=int(kmercount[l][k][i]))
if i in colours[k]:
yaxis1b = makeyaxis1b(i, k)
top.plot(xaxis, yaxis1b, linewidth=1, marker=".", markevery=None, zorder=int(kmercount[l][k][i]))
if i not in top6s or colours[k]:
yaxis1a = makeyaxis1a(i, k)
top.plot(xaxis, yaxis1a, color = '0.75', linestyle='--', linewidth=0.5, marker="x", markevery=None, alpha=0.5, zorder=0)
except:
continue
for l in range(1, numofruns+1):
alreadybottom = []
for i in kmercount[l][k]:
num = kmercount[l][k][i]
rkmer = kmer2hash(revComp(hash2kmer(i,k)))
try:
rnum = kmercount[l][k][rkmer]
if num >= rnum:
alreadybottom.append(rkmer)
if i not in alreadybottom:
if i in top6s:
yaxis2c = makeyaxis2c(i, k)
bottom.plot(xaxis, yaxis2c, color=colourslist[(top6s.index(i)//2)], linewidth=2, marker="s", markevery=None, zorder=int(kmercount[l][k][i]))
if i in colours[k]:
yaxis2b = makeyaxis2b(i, k)
bottom.plot(xaxis, yaxis2b, linewidth=1, marker=".", markevery=None, zorder=int(kmercount[l][k][i]))
if i not in top6s or colours[k]:
yaxis2a = makeyaxis2a(i, k)
bottom.plot(xaxis, yaxis2a, color = '0.75', linestyle='--', linewidth=0.5, marker="x", markevery=None, alpha=0.8, zorder=0)
except:
continue
ymint, ymaxt = top.get_ylim()
ypost = np.linspace(ymint, ymaxt, num=20, endpoint=True)
yminb, ymaxb = bottom.get_ylim()
yposb = np.linspace(yminb, ymaxb, num=20, endpoint=True)
top6labels = []
for n, i in enumerate(top6s[::2]):
p = (n//2)
yaxis1c = makeyaxis1c(i, k)
yaxis2c = makeyaxis2c(i, k)
top.annotate((str(n+1)), (xaxis[last], yaxis1c[last]), (xaxis[last]+0.2, ypost[-(n+2)]), size=10, fontname='monospace', weight='bold', arrowprops=dict(color=colourslist[n], shrink=0.05, width=0.05, headwidth=0.4), color=colourslist[n])
top6labels.append(str(n+1)+". "+str(hash2kmer(i,k))+" / "+revComp(str(hash2kmer(i,k))))
bottom.annotate((str(n+1)+". "+str(hash2kmer(i,k))), (xaxis[last], yaxis2c[last]), (xaxis[last]+0.3, yposb[-(n+2)]), size=10, fontname='monospace', weight='bold', color=colourslist[n], arrowprops=dict(color=colourslist[n], shrink=0.05, width=0.05, headwidth=0.4))
for p, i in enumerate(top6s[::2]):
dp = (p+3)
top.text(s=(str(top6labels[p])), x=(numofruns-0.5+(startround-1)), y=(ypost[-dp]), size=14, fontname='monospace', color=colourslist[p])
bar.bar(xaxis, makeyaxis3(k))
plt.savefig("figures/"+str(identifier)+"/kmer_frequency/kmerfreq_"+str(identifier)+"_"+str(k), dpi=600)
plt.close()
def multigrapher(mink, maxk):
for k in range(mink, maxk+1):
grapher(k)
multigrapher(mink, maxk)