-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmost_abundant_CDR3.py
351 lines (302 loc) · 11.8 KB
/
most_abundant_CDR3.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
#!/usr/bin/env python
#[email protected]:/srv/gsfs0/projects/snyder/slancast/repertoire/
#I want this program to find the change from 1 to 2 and then from 2 to 3 to see if more antibodies stick around from 2 to 3.
import sys
import numpy as np
from collections import Counter
print("###################")
print("###################")
print("###################")
print(sys.argv[4])
print("###################")
print("###################")
print("###################")
##################################################################################
# sys.argv[1] should be the timepoint that you want to count the CDR3 sequences
##################################################################################
filehandle = open(sys.argv[1], "r")
filestring = filehandle.read()
filelist = filestring.split("\n")
split = []
for entry in filelist:
entry=entry.split(",")
split.append(entry)
##################################################################################
# creating lists of total CDR and VD recombinations, and then finding the most common members
##################################################################################
CDR3 = []
VDJtotal = []
for entry in split:
if entry[0] != '*' and len(entry) > 12:
CDR3.append(entry[0])
VDJtotal.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
print("Total reads T1: ")
print(len(split))
count = Counter(CDR3)
most_common_CDR3 = [count.most_common()[0][0],count.most_common()[1][0],count.most_common()[2][0],count.most_common()[3][0]]
print("Most common T1 CDR3 seqs:")
print(most_common_CDR3)
print("T1 top three most common vdj recombinations:")
count_VDJ = Counter(VDJtotal)
print(count_VDJ.most_common()[0])
print(count_VDJ.most_common()[1])
print(count_VDJ.most_common()[2])
print(count_VDJ.most_common()[3])
print(count_VDJ.most_common()[4])
most_common_vdj = [count_VDJ.most_common()[0][0],count_VDJ.most_common()[1][0],count_VDJ.most_common()[2][0],count_VDJ.most_common()[3][0]]
##################################################################################
# finding the most common vdj sequences in the CDR3 sequences.
##################################################################################
output=[]
vdj1 = []
vdj2 = []
vdj3 = []
vdj4 = []
for entry in split:
if entry[0] in most_common_CDR3:
output.append(entry)
if most_common_CDR3[0] == entry[0]:
vdj1.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[1] == entry[0]:
vdj2.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[2] == entry[0]:
vdj3.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[3] == entry[0]:
vdj4.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
print("T1 counts of top 4 cdr3 seqs:")
print(len(vdj1))
print(len(vdj2))
print(len(vdj3))
print(len(vdj4))
count_vdj1 = Counter(vdj1)#Finding what are the most common vdj arrangements for each CDR3 sequence.
count_vdj2 = Counter(vdj2)
count_vdj3 = Counter(vdj3)
count_vdj4 = Counter(vdj4)
#saving first two most common vdj combinations for every CDR3 sequence
mc_vdj1=count_vdj1.most_common()[0][0]
smc_vdj1=count_vdj1.most_common()[1][0]
mc_vdj2=count_vdj2.most_common()[0][0]
smc_vdj2=count_vdj2.most_common()[1][0]
mc_vdj3=count_vdj3.most_common()[0][0]
smc_vdj3=count_vdj3.most_common()[1][0]
mc_vdj4=count_vdj4.most_common()[0][0]
smc_vdj4=count_vdj4.most_common()[1][0]
print("T2 top two vdj recombinations for each of the top 4 CDR3 sequences:")
print(mc_vdj1)
print(smc_vdj1)
print(mc_vdj2)
print(smc_vdj2)
print(mc_vdj3)
print(smc_vdj3)
print(mc_vdj4)
print(smc_vdj4)
#Don't need to save for now
#output = np.array(output)
#np.savetxt("./vdj_most_common_CDR3.csv", output, delimiter=",", fmt="%s")
##################################################################################
# opening T2 to find the overlap in CDR3 and VDJ sequences
##################################################################################
filehandle = open(sys.argv[2], "r")
filestring = filehandle.read()
filelist = filestring.split("\n")
split = []
for entry in filelist:
entry=entry.split(",")
split.append(entry)
print("Total reads T2: ")
print(len(split))
#In the second timepoint finding the sequences that correspond to each of the most common vdj sequence
#for each of the most common CDR3 sequences.
counter_mc_vdj1 = 0
counter_smc_vdj1 = 0
counter_mc_vdj2 = 0
counter_smc_vdj2 = 0
counter_mc_vdj3 = 0
counter_smc_vdj3 = 0
counter_mc_vdj4 = 0
counter_smc_vdj4 = 0
for entry in split:
if mc_vdj1 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj1 = counter_mc_vdj1 + 1
if smc_vdj1 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj1 = counter_smc_vdj1 + 1
if mc_vdj2 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj2 = counter_mc_vdj2 + 1
if smc_vdj2 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj2 = counter_smc_vdj2 + 1
if mc_vdj3 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj3 = counter_mc_vdj3 + 1
if smc_vdj3 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj3 = counter_smc_vdj3 + 1
if mc_vdj4 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj4 = counter_mc_vdj4 + 1
if smc_vdj4 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj4 = counter_smc_vdj4 + 1
print("Number of overlaps in T2 for the top two vdj recombinations for each of the top 4 CDR3 sequences:")
print(counter_mc_vdj1)
print(counter_smc_vdj1)
print(counter_mc_vdj2)
print(counter_smc_vdj2)
print(counter_mc_vdj3)
print(counter_smc_vdj3)
print(counter_mc_vdj4)
print(counter_smc_vdj4)
##########################################
# now this is going to redo it from 2 to 3
##########################################
##################################################################################
# creating lists of total CDR and VD recombinations, and then finding the most common members
##################################################################################
CDR3 = []
VDJtotal = []
for entry in split:
if entry[0] != '*' and len(entry) > 12:
CDR3.append(entry[0])
VDJtotal.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
count = Counter(CDR3)
most_common_CDR3 = [count.most_common()[0][0],count.most_common()[1][0],count.most_common()[2][0],count.most_common()[3][0]]
print("Most common T2 CDR3 seqs:")
print(most_common_CDR3)
print("T2 top five most common vdj recombinations:")
count_VDJ = Counter(VDJtotal)
print(count_VDJ.most_common()[0])
print(count_VDJ.most_common()[1])
print(count_VDJ.most_common()[2])
print(count_VDJ.most_common()[3])
print(count_VDJ.most_common()[4])
most_common_vdj = [count_VDJ.most_common()[0][0],count_VDJ.most_common()[1][0],count_VDJ.most_common()[2][0],count_VDJ.most_common()[3][0]]
##################################################################################
# finding the most common vdj sequences in the CDR3 sequences.
##################################################################################
output=[]
vdj1 = []
vdj2 = []
vdj3 = []
vdj4 = []
for entry in split:
if entry[0] in most_common_CDR3:
output.append(entry)
if most_common_CDR3[0] == entry[0]:
vdj1.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[1] == entry[0]:
vdj2.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[2] == entry[0]:
vdj3.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[3] == entry[0]:
vdj4.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
print("T2 counts of top 4 cdr3 seqs:")
print(len(vdj1))
print(len(vdj2))
print(len(vdj3))
print(len(vdj4))
count_vdj1 = Counter(vdj1)#Finding what are the most common vdj arrangements for each CDR3 sequence.
count_vdj2 = Counter(vdj2)
count_vdj3 = Counter(vdj3)
count_vdj4 = Counter(vdj4)
#saving first two most common vdj combinations for every CDR3 sequence
mc_vdj1=count_vdj1.most_common()[0][0]
smc_vdj1=count_vdj1.most_common()[1][0]
mc_vdj2=count_vdj2.most_common()[0][0]
smc_vdj2=count_vdj2.most_common()[1][0]
mc_vdj3=count_vdj3.most_common()[0][0]
smc_vdj3=count_vdj3.most_common()[1][0]
mc_vdj4=count_vdj4.most_common()[0][0]
smc_vdj4=count_vdj4.most_common()[1][0]
print("T2 top two vdj recombinations for each of the top 4 CDR3 sequences:")
print(mc_vdj1)
print(smc_vdj1)
print(mc_vdj2)
print(smc_vdj2)
print(mc_vdj3)
print(smc_vdj3)
print(mc_vdj4)
print(smc_vdj4)
##################################################################################
# opening T3 to find the overlap in CDR3 and VDJ sequences
##################################################################################
filehandle = open(sys.argv[3], "r")
filestring = filehandle.read()
filelist = filestring.split("\n")
split = []
for entry in filelist:
entry=entry.split(",")
split.append(entry)
#In the second timepoint finding the sequences that correspond to each of the most common vdj sequence
#for each of the most common CDR3 sequences.
counter_mc_vdj1 = 0
counter_smc_vdj1 = 0
counter_mc_vdj2 = 0
counter_smc_vdj2 = 0
counter_mc_vdj3 = 0
counter_smc_vdj3 = 0
counter_mc_vdj4 = 0
counter_smc_vdj4 = 0
for entry in split:
if mc_vdj1 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj1 = counter_mc_vdj1 + 1
if smc_vdj1 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj1 = counter_smc_vdj1 + 1
if mc_vdj2 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj2 = counter_mc_vdj2 + 1
if smc_vdj2 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj2 = counter_smc_vdj2 + 1
if mc_vdj3 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj3 = counter_mc_vdj3 + 1
if smc_vdj3 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj3 = counter_smc_vdj3 + 1
if mc_vdj4 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_mc_vdj4 = counter_mc_vdj4 + 1
if smc_vdj4 == str(entry[1]) + str(entry[6]) + str(entry[11]):
counter_smc_vdj4 = counter_smc_vdj4 + 1
print("Number of overlaps in T3 for the top two vdj recombinations for each of the top 4 CDR3 sequences:")
print(counter_mc_vdj1)
print(counter_smc_vdj1)
print(counter_mc_vdj2)
print(counter_smc_vdj2)
print(counter_mc_vdj3)
print(counter_smc_vdj3)
print(counter_mc_vdj4)
print(counter_smc_vdj4)
print("Total reads T3: ")
print(len(split))
CDR3 = []
VDJtotal = []
for entry in split:
if entry[0] != '*' and len(entry) > 12:
CDR3.append(entry[0])
VDJtotal.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
most_common_CDR3 = [count.most_common()[0][0],count.most_common()[1][0],count.most_common()[2][0],count.most_common()[3][0]]
print("Most common T3 CDR3 seqs:")
print(most_common_CDR3)
print("T3 top three most common vdj recombinations:")
count_VDJ = Counter(VDJtotal)
print(count_VDJ.most_common()[0])
print(count_VDJ.most_common()[1])
print(count_VDJ.most_common()[2])
print(count_VDJ.most_common()[3])
print(count_VDJ.most_common()[4])
most_common_vdj = [count_VDJ.most_common()[0][0],count_VDJ.most_common()[1][0],count_VDJ.most_common()[2][0],count_VDJ.most_common()[3][0]]
##################################################################################
# finding the most common vdj sequences in the CDR3 sequences.
##################################################################################
output=[]
vdj1 = []
vdj2 = []
vdj3 = []
vdj4 = []
for entry in split:
if entry[0] in most_common_CDR3:
output.append(entry)
if most_common_CDR3[0] == entry[0]:
vdj1.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[1] == entry[0]:
vdj2.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[2] == entry[0]:
vdj3.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
if most_common_CDR3[3] == entry[0]:
vdj4.append(str(entry[1]) + str(entry[6]) + str(entry[11]))
print("T3 counts of top 4 cdr3 seqs:")
print(len(vdj1))
print(len(vdj2))
print(len(vdj3))
print(len(vdj4))