forked from mandricigor/imrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clonality.py
205 lines (144 loc) · 5.22 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
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)
ap = argparse.ArgumentParser()
ap.add_argument('inFile', help='Mapped reads in bam format')
ap.add_argument('outDir', help='Mapped reads in bam format')
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={}
base=os.path.splitext(basename(args.inFile))[0]
with open(args.inFile) as csvfile:
readCSV = csv.reader(csvfile, delimiter='\t')
next(readCSV)
for row in readCSV:
V=row[2]
J=row[4]
cdr3=row[0]
count=int(row[2])
if row!=[]:
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, V,J
sys.exit(1)
fileSTAT=open(args.outDir+"/SUMMARY_"+args.outDir+".txt","w")
fileSTAT.write("SAMPLE,nIGH,nIGK,nIGL,nTCRA,nTCRB,nTCRD,nTCRG, loadIGH,nIGK,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)))
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))
#IGH
fileIGH=open(args.outDir+"/IGH_cdr3_"+args.outDir+".txt","w")
#fileIGH.write("CDR3,nReads")
#fileIGH.write("\n")
for key in sorted(cdr3_IGH):
fileIGH.write(key)
fileIGH.write("\n")
#IGK
fileIGK=open(args.outDir+"/IGK_cdr3_"+args.outDir+".txt","w")
#fileIGK.write("CDR3,nReads")
#fileIGK.write("\n")
for key in sorted(cdr3_IGK):
fileIGK.write(key)
fileIGK.write("\n")
#IGL
fileIGL=open(args.outDir+"/IGL_cdr3_"+args.outDir+".txt","w")
#fileIGL.write("CDR3,nReads")
#fileIGL.write("\n")
for key in sorted(cdr3_IGL):
fileIGL.write(key)
fileIGL.write("\n")
#TCRA
fileTCRA=open(args.outDir+"/TCRA_cdr3_"+args.outDir+".txt","w")
#fileTCRA.write("CDR3,nReads")
#fileTCRA.write("\n")
for key in sorted(cdr3_TCRA):
fileTCRA.write(key)
fileTCRA.write("\n")
#TCRB
fileTCRB=open(args.outDir+"/TCRB_cdr3_"+args.outDir+".txt","w")
#fileTCRB.write("CDR3,nReads")
#fileTCRB.write("\n")
for key in sorted(cdr3_TCRB):
fileTCRB.write(key)
fileTCRB.write("\n")
#TCRG
fileTCRG=open(args.outDir+"/TCRG_cdr3_"+args.outDir+".txt","w")
#fileTCRG.write("CDR3,nReads")
#fileTCRG.write("\n")
for key in sorted(cdr3_TCRG):
fileTCRG.write(key)
fileTCRG.write("\n")
#TCRD
fileTCRD=open(args.outDir+"/TCRD_cdr3_"+args.outDir+".txt","w")
#fileTCRD.write("CDR3,nReads")
#fileTCRD.write("\n")
for key in sorted(cdr3_TCRD):
fileTCRD.write(key)
fileTCRD.write("\n")