forked from mandricigor/imrep
-
Notifications
You must be signed in to change notification settings - Fork 4
/
clonality.py
431 lines (341 loc) · 14.1 KB
/
clonality.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import argparse
import csv
import sys
from collections import Counter
from math import log as ln
import os
from os.path import basename
def p(n, N):
""" Relative abundance """
if n is 0:
return 0
else:
return (float(n)/N) * ln(float(n)/N)
def sdi(data):
N = sum(data.values())
return -sum(p(n, N) for n in data.values() if n is not 0)
def write_cdr3(fileName,dict):
file = open(fileName, "w")
for key,value in sorted(dict.items()):
file.write(key)
file.write("\n")
file.close()
def write_VJ(fileName,dict):
file = open(fileName, "w")
for key,value in sorted(dict.items()):
file.write(key[0]+"-"+key[1])
file.write("\n")
file.close()
#---------------
#main
ap = argparse.ArgumentParser()
ap.add_argument('inFile', help='output of ImRep')
ap.add_argument('outDir', help='directory to save the summary of the immune repertoire')
args = ap.parse_args()
if not os.path.exists(args.outDir):
os.makedirs(args.outDir)
#------
print ("Read CDR3 assembled by ImreP", args.inFile)
#HWI-ST1148:152:C3LK5ACXX:7:2301:14697:52825/1/1 CALPIFHSRIRRRPLRPILHSRIHKHYYNKHPHHYNLPRKIVVGIVF TRAV9,TRAV18,TRAV11 NA TRAJ4 TRAV9-2*01:4:1,TRAV9-1*01:4:1,TRAV11*01:3:0,TRAV18*01:4:1,TRAV6*01:4:1,TRAV16*01:4:1 NA 0 0
#CDR3_AA_Seq Read_count V_chains D_chains J_chains
#CALPISGTRASKLFGLAATRVSYQQGPVILDEDVFDLHLGSLIHIFLVIGLQGF 2 TRAV18,TRAV19,TRAV11 NA TRAJ25,TRAJ35
cdr3_IGH={}
cdr3_IGK={}
cdr3_IGL={}
cdr3_TCRA={}
cdr3_TCRB={}
cdr3_TCRD={}
cdr3_TCRG={}
recomb_IGH={}
recomb_IGK={}
recomb_IGL={}
recomb_TCRA={}
recomb_TCRB={}
recomb_TCRD={}
recomb_TCRG={}
#CATQHYTRPRSSCTF TRA 2 TRAV25,TRAV12,TRAV13 NA TRAJ48
base=os.path.splitext(basename(args.inFile))[0]
with open(args.inFile) as csvfile:
readCSV = csv.reader(csvfile)
next(readCSV)
for row in readCSV:
V=row[3]
J=row[5]
cdr3=row[0]
if cdr3[0]!="C": #some lines of imrep output have no CDR3
break
count=int(row[2])
if row!=[]:
#extract non-ambiguous recombinations
if V.count(';')==0 and J.count(';')==0:
if row[1]=="IGH":
if (V,J) not in set(recomb_IGH.keys()):
recomb_IGH[(V,J)]=0
recomb_IGH[(V, J)]+=count
else:
recomb_IGH[(V, J)] += count
elif row[1]=="IGK":
if (V,J) not in set(recomb_IGK.keys()):
recomb_IGK[(V,J)]=0
recomb_IGK[(V, J)]+=count
else:
recomb_IGK[(V, J)] += count
elif row[1]=="IGL":
if (V,J) not in set(recomb_IGL.keys()):
recomb_IGL[(V,J)]=0
recomb_IGL[(V, J)]+=count
else:
recomb_IGL[(V, J)] += count
elif row[1] == "TRA":
if (V, J) not in set(recomb_TCRA.keys()):
recomb_TCRA[(V, J)] = 0
recomb_TCRA[(V, J)] += count
else:
recomb_TCRA[(V, J)] += count
elif row[1] == "TRB":
if (V, J) not in set(recomb_TCRB.keys()):
recomb_TCRB[(V, J)] = 0
recomb_TCRB[(V, J)] += count
else:
recomb_TCRB[(V, J)] += count
elif row[1] == "TRG":
if (V, J) not in set(recomb_TCRG.keys()):
recomb_TCRG[(V, J)] = 0
recomb_TCRG[(V, J)] += count
else:
recomb_TCRG[(V, J)] += count
elif row[1] == "TRD":
if (V, J) not in set(recomb_TCRD.keys()):
recomb_TCRD[(V, J)] = 0
recomb_TCRD[(V, J)] += count
else:
recomb_TCRD[(V, J)] += count
else:
print ("ERROR : ",row, V,J)
sys.exit(1)
if "*" not in row[0]:
if row[1]=="IGH":
cdr3_IGH[cdr3]=count
elif row[1]=="IGK":
cdr3_IGK[cdr3]=count
elif row[1]=="IGL":
cdr3_IGL[cdr3]=count
elif row[1]=="TRA":
cdr3_TCRA[cdr3]=count
elif row[1]=="TRB":
cdr3_TCRB[cdr3]=count
elif row[1]=="TRG":
cdr3_TCRG[cdr3]=count
elif row[1]=="TRD":
cdr3_TCRD[cdr3]=count
else:
print ("ERROR : ",row)
sys.exit(1)
sample=os.path.split(os.path.dirname(args.outDir))[1]
#----------------
#CDR3s
fileSTAT=open(args.outDir+"/summary.cdr3.txt","w")
fileSTAT.write("SAMPLE,nIGH,nIGK,nIGL,nTCRA,nTCRB,nTCRD,nTCRG, loadIGH,loadIGK,loadIGL,loadTCRA,loadTCRB,loadTCRD,loadTCRG,alphaIGH,alphaIGK,alphaIGL,alphaTCRA,alphaTCRB,alphaTCRD,alphaTCRG")
fileSTAT.write("\n")
nIGH=str(len((cdr3_IGH)))
nIGK=str(len((cdr3_IGK)))
nIGL=str(len((cdr3_IGL)))
nTCRA=str(len((cdr3_TCRA)))
nTCRB=str(len((cdr3_TCRB)))
nTCRD=str(len((cdr3_TCRD)))
nTCRG=str(len((cdr3_TCRG)))
loadIGH=str(sum(cdr3_IGH.values()))
loadIGK=str(sum(cdr3_IGK.values()))
loadIGL=str(sum(cdr3_IGL.values()))
loadTCRA=str(sum(cdr3_TCRA.values()))
loadTCRB=str(sum(cdr3_TCRB.values()))
loadTCRD=str(sum(cdr3_TCRD.values()))
loadTCRG=str(sum(cdr3_TCRG.values()))
alphaIGH=str(sdi(Counter(cdr3_IGH)))
alphaIGK=str(sdi(Counter(cdr3_IGK)))
alphaIGL=str(sdi(Counter(cdr3_IGL)))
alphaTCRA=str(sdi(Counter(cdr3_TCRA)))
alphaTCRB=str(sdi(Counter(cdr3_TCRB)))
alphaTCRD=str(sdi(Counter(cdr3_TCRD)))
alphaTCRG=str(sdi(Counter(cdr3_TCRG)))
#prefix
fileSTAT.write(base+","+nIGH+","+nIGK+","+nIGL+","+nTCRA+","+nTCRB+","+nTCRD+","+nTCRG+","+loadIGH+","+loadIGK+","+loadIGL+","+loadTCRA+","+loadTCRB+","+loadTCRD+","+loadTCRG+","+alphaIGH+","+alphaIGK+","+alphaIGL+","+alphaTCRA+","+alphaTCRB+","+alphaTCRD+","+alphaTCRG)
fileSTAT.write("\n")
print ("Total number of IGH CDR3 is", len(set(cdr3_IGH)))
print ("Total number of IGK CDR3 is", len(set(cdr3_IGK)))
print ("Total number of IGL CDR3 is", len(set(cdr3_IGL)))
print ("Total number of TCRA CDR3 is", len(set(cdr3_TCRA)))
print ("Total number of TCRB CDR3 is", len(set(cdr3_TCRB)))
print ("Total number of TCRG CDR3 is", len(set(cdr3_TCRG)))
print ("Total number of TCRD CDR3 is", len(set(cdr3_TCRD)))
#save CDR3s
write_cdr3(args.outDir+"/IGH.cdr3."+sample+".csv",cdr3_IGH)
write_cdr3(args.outDir+"/IGK.cdr3."+sample+".csv",cdr3_IGK)
write_cdr3(args.outDir+"/IGL.cdr3."+sample+".csv",cdr3_IGL)
write_cdr3(args.outDir+"/TCRA.cdr3."+sample+".csv",cdr3_TCRA)
write_cdr3(args.outDir+"/TCRB.cdr3."+sample+".csv",cdr3_TCRA)
write_cdr3(args.outDir+"/TCRD.cdr3."+sample+".csv",cdr3_TCRD)
write_cdr3(args.outDir+"/TCRG.cdr3."+sample+".csv",cdr3_TCRG)
#save VJ
write_VJ(args.outDir+"/IGH.VJ."+sample+".csv",recomb_IGH)
write_VJ(args.outDir+"/IGK.VJ."+sample+".csv",recomb_IGK)
write_VJ(args.outDir+"/IGL.VJ."+sample+".csv",recomb_IGL)
write_VJ(args.outDir+"/TCRA.VJ."+sample+".csv",recomb_TCRA)
write_VJ(args.outDir+"/TCRB.VJ."+sample+".csv",recomb_TCRA)
write_VJ(args.outDir+"/TCRD.VJ."+sample+".csv",recomb_TCRD)
write_VJ(args.outDir+"/TCRG.VJ."+sample+".csv",recomb_TCRG)
#save CDR3s with FREQ
#IGH
fileIGH=open(args.outDir+"/IGH.cdr3.FREQ."+sample+".csv","w")
fileIGH.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_IGH.values())
for key, value in sorted(cdr3_IGH.items()):
fileIGH.write(key+","+str(value)+","+str(value/float(N)))
fileIGH.write("\n")
fileIGH.close()
#IGK
fileIGK=open(args.outDir+"/IGK.cdr3.FREQ."+sample+".csv","w")
fileIGK.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_IGK.values())
for key, value in sorted(cdr3_IGK.items()):
fileIGK.write(key+","+str(value)+","+str(value/float(N)))
fileIGK.write("\n")
fileIGK.close()
#IGL
fileIGL=open(args.outDir+"/IGL.cdr3.FREQ."+sample+".csv","w")
fileIGL.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_IGL.values())
for key, value in sorted(cdr3_IGL.items()):
fileIGL.write(key+","+str(value)+","+str(value/float(N)))
fileIGL.write("\n")
fileIGL.close()
#TCRA
fileTCRA=open(args.outDir+"/TCRA.cdr3.FREQ."+sample+".csv","w")
fileTCRA.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_TCRA.values())
for key, value in sorted(cdr3_TCRA.items()):
fileTCRA.write(key+","+str(value)+","+str(value/float(N)))
fileTCRA.write("\n")
fileTCRA.close()
#TCRB
fileTCRB=open(args.outDir+"/TCRB.cdr3.FREQ."+sample+".csv","w")
fileTCRB.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_TCRB.values())
for key, value in sorted(cdr3_TCRB.items()):
fileTCRB.write(key+","+str(value)+","+str(value/float(N)))
fileTCRB.write("\n")
fileTCRB.close()
#TCRG
fileTCRG=open(args.outDir+"/TCRG.cdr3.FREQ."+sample+".csv","w")
fileTCRG.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_TCRG.values())
for key, value in sorted(cdr3_TCRG.items()):
fileTCRG.write(key+","+str(value)+","+str(value/float(N)))
fileTCRG.write("\n")
fileTCRG.close()
#TCRD
fileTCRD=open(args.outDir+"/TCRD.cdr3.FREQ."+sample+".csv","w")
fileTCRD.write("CDR3,count,relative.frequency\n")
N = sum(cdr3_TCRD.values())
for key, value in sorted(cdr3_TCRD.items()):
fileTCRD.write(key+","+str(value)+","+str(value/float(N)))
fileTCRD.write("\n")
fileTCRD.close()
#------------------------------
#VJs with FREQ
fileSTAT = open(args.outDir + "/summary.VJ.txt", "w")
fileSTAT.write(
"SAMPLE,nIGH,nIGK,nIGL,nTCRA,nTCRB,nTCRD,nTCRG, loadIGH,loadIGK,loadIGL,loadTCRA,loadTCRB,loadTCRD,loadTCRG,alphaIGH,alphaIGK,alphaIGL,alphaTCRA,alphaTCRB,alphaTCRD,alphaTCRG")
fileSTAT.write("\n")
nIGH = str(len((recomb_IGH)))
nIGK = str(len((recomb_IGK)))
nIGL = str(len((recomb_IGL)))
nTCRA = str(len((recomb_TCRA)))
nTCRB = str(len((recomb_TCRB)))
nTCRD = str(len((recomb_TCRD)))
nTCRG = str(len((recomb_TCRG)))
loadIGH = str(sum(recomb_IGH.values()))
loadIGK = str(sum(recomb_IGK.values()))
loadIGL = str(sum(recomb_IGL.values()))
loadTCRA = str(sum(recomb_TCRA.values()))
loadTCRB = str(sum(recomb_TCRB.values()))
loadTCRD = str(sum(recomb_TCRD.values()))
loadTCRG = str(sum(recomb_TCRG.values()))
alphaIGH = str(sdi(Counter(recomb_IGH)))
alphaIGK = str(sdi(Counter(recomb_IGK)))
alphaIGL = str(sdi(Counter(recomb_IGL)))
alphaTCRA = str(sdi(Counter(recomb_TCRA)))
alphaTCRB = str(sdi(Counter(recomb_TCRB)))
alphaTCRD = str(sdi(Counter(recomb_TCRD)))
alphaTCRG = str(sdi(Counter(recomb_TCRG)))
fileSTAT.write(
base + "," + nIGH + "," + nIGK + "," + nIGL + "," + nTCRA + "," + nTCRB + "," + nTCRD + "," + nTCRG + "," + loadIGH + "," + loadIGK + "," + loadIGL + "," + loadTCRA + "," + loadTCRB + "," + loadTCRD + "," + loadTCRG + "," + alphaIGH + "," + alphaIGK + "," + alphaIGL + "," + alphaTCRA + "," + alphaTCRB + "," + alphaTCRD + "," + alphaTCRG)
fileSTAT.write("\n")
print ("Total number of IGH VJ recombinations is", len(set(recomb_IGH)))
print ("Total number of IGK VJ recombinations is", len(set(recomb_IGK)))
print ("Total number of IGL VJ recombinations is", len(set(recomb_IGL)))
print ("Total number of TCRA VJ recombinations is", len(set(recomb_TCRA)))
print ("Total number of TCRB VJ recombinations is", len(set(recomb_TCRB)))
print ("Total number of TCRG VJ recombinations is", len(set(recomb_TCRG)))
print ("Total number of TCRD VJ recombinations is", len(set(recomb_TCRD)))
# IGH
fileIGH = open(args.outDir + "/IGH.VJ.FREQ." + sample + ".csv", "w")
fileIGH.write("VJ,count,relative.frequency\n")
N = sum(recomb_IGH.values())
for key, value in sorted(recomb_IGH.items()):
fileIGH.write(key[0] + "-" + key[1]+ ","+ str(value) + "," + str(value / float(N)))
fileIGH.write("\n")
fileIGH.close()
# IGK
fileIGK = open(args.outDir + "/IGK.VJ.FREQ." + sample + ".csv", "w")
fileIGK.write("VJ,count,relative.frequency\n")
N = sum(recomb_IGK.values())
for key, value in sorted(recomb_IGK.items()):
fileIGK.write(key[0] + "-" + key[1] + "," + str(value) + "," + str(value / float(N)))
fileIGK.write("\n")
fileIGK.close()
# IGL
fileIGL = open(args.outDir + "/IGL.VJ.FREQ." + sample + ".csv", "w")
fileIGL.write("VJ,count,relative.frequency\n")
N = sum(recomb_IGL.values())
for key, value in sorted(recomb_IGL.items()):
fileIGL.write(key[0] + "-" + key[1] + "," + str(value) + "," + str(value / float(N)))
fileIGL.write("\n")
fileIGL.close()
# TCRA
fileTCRA = open(args.outDir + "/TCRA.VJ.FREQ." + sample + ".csv", "w")
fileTCRA.write("VJ,count,relative.frequency\n")
N = sum(recomb_TCRA.values())
for key, value in sorted(recomb_TCRA.items()):
fileTCRA.write(key[0] + "-" + key[1] + "," + str(value) + "," + str(value / float(N)))
fileTCRA.write("\n")
fileTCRA.close()
# TCRB
fileTCRB = open(args.outDir + "/TCRB.VJ.FREQ." + sample + ".csv", "w")
fileTCRB.write("VJ,count,relative.frequency\n")
N = sum(recomb_TCRB.values())
for key, value in sorted(recomb_TCRB.items()):
fileTCRB.write(key[0] + "-" + key[1] + "," + str(value) + "," + str(value / float(N)))
fileTCRB.write("\n")
fileTCRB.close()
# TCRG
fileTCRG = open(args.outDir + "/TCRG.VJ.FREQ." + sample + ".csv", "w")
fileTCRG.write("VJ,count,relative.frequency\n")
N = sum(recomb_TCRG.values())
for key, value in sorted(recomb_TCRG.items()):
fileTCRG.write(key[0] + "-" + key[1] + "," + str(value) + "," + str(value / float(N)))
fileTCRG.write("\n")
fileTCRG.close()
# TCRD
fileTCRD = open(args.outDir + "/TCRD.VJ.FREQ." + sample + ".csv", "w")
fileTCRD.write("VJ,count,relative.frequency\n")
N = sum(recomb_TCRD.values())
for key, value in sorted(recomb_TCRD.items()):
fileTCRD.write(key[0] + "-" + key[1] + "," + str(value) + "," + str(value / float(N)))
fileTCRD.write("\n")
fileTCRD.close()
print ("All results are summarized in ",args.outDir)
print ("Relative frequencies and counts of assembled CDR3s are saved in individuals files.")
print ("For example, relative frequencies and counts of IGH chain are saved in:")
print (args.outDir+"/IGH.cdr3.FREQ."+sample+".csv")
print ("Done!")