0
|
1 from __future__ import division
|
|
2 from collections import defaultdict
|
|
3 import re
|
|
4 import argparse
|
|
5
|
|
6 parser = argparse.ArgumentParser()
|
|
7 parser.add_argument("--input",
|
|
8 help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation")
|
|
9 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
|
|
10 parser.add_argument("--includefr1", help="Should the mutation/nucleotides in the FR1 region be included?")
|
|
11 parser.add_argument("--output", help="Output file")
|
|
12
|
|
13 args = parser.parse_args()
|
|
14
|
|
15 infile = args.input
|
|
16 genes = str(args.genes).split(",")
|
|
17 print "includefr1 =", args.includefr1
|
|
18 include_fr1 = True if args.includefr1 == "yes" else False
|
|
19 outfile = args.output
|
|
20
|
|
21 genedic = dict()
|
|
22
|
|
23 mutationdic = dict()
|
|
24 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
|
|
25 NAMatchResult = (None, None, None, None, None, None, '')
|
|
26 linecount = 0
|
|
27
|
|
28 IDIndex = 0
|
|
29 best_matchIndex = 0
|
|
30 fr1Index = 0
|
|
31 cdr1Index = 0
|
|
32 fr2Index = 0
|
|
33 cdr2Index = 0
|
|
34 fr3Index = 0
|
|
35 first = True
|
|
36 IDlist = []
|
|
37 mutationList = []
|
|
38 mutationListByID = {}
|
|
39 cdr1LengthDic = {}
|
|
40 cdr2LengthDic = {}
|
|
41
|
|
42 with open(infile, 'r') as i:
|
|
43 for line in i:
|
|
44 if first:
|
|
45 linesplt = line.split("\t")
|
|
46 IDIndex = linesplt.index("Sequence.ID")
|
|
47 best_matchIndex = linesplt.index("best_match")
|
|
48 fr1Index = linesplt.index("FR1.IMGT")
|
|
49 cdr1Index = linesplt.index("CDR1.IMGT")
|
|
50 fr2Index = linesplt.index("FR2.IMGT")
|
|
51 cdr2Index = linesplt.index("CDR2.IMGT")
|
|
52 fr3Index = linesplt.index("FR3.IMGT")
|
|
53 cdr1LengthIndex = linesplt.index("CDR1.IMGT.length")
|
|
54 cdr2LengthIndex = linesplt.index("CDR2.IMGT.length")
|
|
55 first = False
|
|
56 continue
|
|
57 linecount += 1
|
|
58 linesplt = line.split("\t")
|
|
59 ID = linesplt[IDIndex]
|
|
60 genedic[ID] = linesplt[best_matchIndex]
|
|
61 try:
|
|
62 if linesplt[fr1Index] != "NA":
|
|
63 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if include_fr1 else []
|
|
64 else:
|
|
65 mutationdic[ID + "_FR1"] = []
|
|
66 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] if linesplt[cdr1Index] != "NA" else []
|
|
67 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] if linesplt[fr2Index] != "NA" else []
|
|
68 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] if linesplt[cdr2Index] != "NA" else []
|
|
69 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
|
|
70 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] if linesplt[fr3Index] != "NA" else []
|
|
71 except e:
|
|
72 print linesplt
|
|
73 print linecount
|
|
74 print e
|
|
75 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
|
|
76 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
|
|
77
|
|
78 cdr1Length = linesplt[cdr1LengthIndex]
|
|
79 cdr2Length = linesplt[cdr2LengthIndex]
|
|
80
|
|
81 cdr1LengthDic[ID] = int(cdr1Length) if cdr1Length != "X" else 0
|
|
82 cdr2LengthDic[ID] = int(cdr2Length) if cdr2Length != "X" else 0
|
|
83
|
|
84 IDlist += [ID]
|
|
85
|
|
86 AALength = (int(max(mutationList, key=lambda i: int(i[4]) if i[4] else 0)[4]) + 1) # [4] is the position of the AA mutation, None if silent
|
|
87 if AALength < 60:
|
|
88 AALength = 64
|
|
89
|
|
90 AA_mutation = [0] * AALength
|
|
91 AA_mutation_dic = {"ca": AA_mutation[:], "cg": AA_mutation[:], "cm": AA_mutation[:], "un": AA_mutation[:]}
|
|
92 AA_mutation_empty = AA_mutation[:]
|
|
93
|
|
94 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt"
|
|
95 with open(aa_mutations_by_id_file, 'w') as o:
|
|
96 o.write("ID\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
|
|
97 for ID in mutationListByID.keys():
|
|
98 AA_mutation_for_ID = AA_mutation_empty[:]
|
|
99 for mutation in mutationListByID[ID]:
|
|
100 if mutation[4]:
|
|
101 AA_mutation_position = int(mutation[4])
|
|
102 AA_mutation[AA_mutation_position] += 1
|
|
103 AA_mutation_for_ID[AA_mutation_position] += 1
|
|
104 clss = genedic[ID][:2]
|
|
105 AA_mutation_dic[clss][AA_mutation_position] += 1
|
|
106 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n")
|
|
107
|
|
108
|
|
109
|
|
110 #absent AA stuff
|
|
111 absentAACDR1Dic = defaultdict(list)
|
|
112 absentAACDR1Dic[5] = range(29,36)
|
|
113 absentAACDR1Dic[6] = range(29,35)
|
|
114 absentAACDR1Dic[7] = range(30,35)
|
|
115 absentAACDR1Dic[8] = range(30,34)
|
|
116 absentAACDR1Dic[9] = range(31,34)
|
|
117 absentAACDR1Dic[10] = range(31,33)
|
|
118 absentAACDR1Dic[11] = [32]
|
|
119
|
|
120 absentAACDR2Dic = defaultdict(list)
|
|
121 absentAACDR2Dic[0] = range(55,65)
|
|
122 absentAACDR2Dic[1] = range(56,65)
|
|
123 absentAACDR2Dic[2] = range(56,64)
|
|
124 absentAACDR2Dic[3] = range(57,64)
|
|
125 absentAACDR2Dic[4] = range(57,63)
|
|
126 absentAACDR2Dic[5] = range(58,63)
|
|
127 absentAACDR2Dic[6] = range(58,62)
|
|
128 absentAACDR2Dic[7] = range(59,62)
|
|
129 absentAACDR2Dic[8] = range(59,61)
|
|
130 absentAACDR2Dic[9] = [60]
|
|
131
|
|
132 absentAA = [len(IDlist)] * (AALength-1)
|
|
133 for k, cdr1Length in cdr1LengthDic.iteritems():
|
|
134 for c in absentAACDR1Dic[cdr1Length]:
|
|
135 absentAA[c] -= 1
|
|
136
|
|
137 for k, cdr2Length in cdr2LengthDic.iteritems():
|
|
138 for c in absentAACDR2Dic[cdr2Length]:
|
|
139 absentAA[c] -= 1
|
|
140
|
|
141
|
|
142 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt"
|
|
143 with open(aa_mutations_by_id_file, 'w') as o:
|
|
144 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
|
|
145 for ID in IDlist:
|
|
146 absentAAbyID = [1] * (AALength-1)
|
|
147 cdr1Length = cdr1LengthDic[ID]
|
|
148 for c in absentAACDR1Dic[cdr1Length]:
|
|
149 absentAAbyID[c] -= 1
|
|
150
|
|
151 cdr2Length = cdr2LengthDic[ID]
|
|
152 for c in absentAACDR2Dic[cdr2Length]:
|
|
153 absentAAbyID[c] -= 1
|
|
154 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n")
|
|
155
|
|
156 if linecount == 0:
|
|
157 print "No data, exiting"
|
|
158 with open(outfile, 'w') as o:
|
|
159 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
|
|
160 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
|
|
161 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
|
|
162 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
|
|
163 import sys
|
|
164
|
|
165 sys.exit()
|
|
166
|
|
167 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
|
|
168 RGYWCount = {}
|
|
169 WRCYCount = {}
|
|
170 WACount = {}
|
|
171 TWCount = {}
|
|
172
|
|
173 #IDIndex = 0
|
|
174 ataIndex = 0
|
|
175 tatIndex = 0
|
|
176 aggctatIndex = 0
|
|
177 atagcctIndex = 0
|
|
178 first = True
|
|
179 with open(infile, 'r') as i:
|
|
180 for line in i:
|
|
181 if first:
|
|
182 linesplt = line.split("\t")
|
|
183 ataIndex = linesplt.index("X.a.t.a")
|
|
184 tatIndex = linesplt.index("t.a.t.")
|
|
185 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
|
|
186 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
|
|
187 first = False
|
|
188 continue
|
|
189 linesplt = line.split("\t")
|
|
190 gene = linesplt[best_matchIndex]
|
|
191 ID = linesplt[IDIndex]
|
|
192 if ID == "ca2":
|
|
193 print linesplt
|
|
194 RGYW = [(int(x), int(y), z) for (x, y, z) in
|
|
195 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
|
|
196 WRCY = [(int(x), int(y), z) for (x, y, z) in
|
|
197 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
|
|
198 WA = [(int(x), int(y), z) for (x, y, z) in
|
|
199 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
|
|
200 TW = [(int(x), int(y), z) for (x, y, z) in
|
|
201 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
|
|
202 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
|
|
203
|
|
204 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[
|
|
205 ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
|
|
206 for mutation in mutationList:
|
|
207 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
|
|
208 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW])
|
|
209 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY])
|
|
210 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA])
|
|
211 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW])
|
|
212
|
|
213 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
|
|
214
|
|
215 if in_how_many_motifs > 0:
|
|
216 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
|
|
217 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
|
|
218 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
|
|
219 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
|
|
220
|
|
221
|
|
222 def mean(lst):
|
|
223 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0
|
|
224
|
|
225
|
|
226 def median(lst):
|
|
227 lst = sorted(lst)
|
|
228 l = len(lst)
|
|
229 if l == 0:
|
|
230 return 0
|
|
231 if l == 1:
|
|
232 return lst[0]
|
|
233
|
|
234 l = int(l / 2)
|
|
235
|
|
236 if len(lst) % 2 == 0:
|
|
237 return float(lst[l] + lst[(l - 1)]) / 2.0
|
|
238 else:
|
|
239 return lst[l]
|
|
240
|
|
241 funcs = {"mean": mean, "median": median, "sum": sum}
|
|
242
|
|
243 directory = outfile[:outfile.rfind("/") + 1]
|
|
244 value = 0
|
|
245 valuedic = dict()
|
|
246
|
|
247 for fname in funcs.keys():
|
|
248 for gene in genes:
|
|
249 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v:
|
|
250 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip())
|
|
251 with open(directory + "all_" + fname + "_value.txt", 'r') as v:
|
|
252 valuedic["total_" + fname] = float(v.readlines()[0].rstrip())
|
|
253
|
|
254
|
|
255 def get_xyz(lst, gene, f, fname):
|
|
256 x = int(round(f(lst)))
|
|
257 y = valuedic[gene + "_" + fname]
|
|
258 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0"
|
|
259 return (str(x), str(y), z)
|
|
260
|
|
261 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
|
|
262 arr = ["RGYW", "WRCY", "WA", "TW"]
|
|
263
|
|
264 geneMatchers = {gene: re.compile("^" + gene + ".*") for gene in genes}
|
|
265
|
|
266 for fname in funcs.keys():
|
|
267 func = funcs[fname]
|
|
268 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt"
|
|
269 with open(foutfile, 'w') as o:
|
|
270 for typ in arr:
|
|
271 o.write(typ + " (%)")
|
|
272 curr = dic[typ]
|
|
273 for gene in genes:
|
|
274 geneMatcher = geneMatchers[gene] #re.compile("^" + gene + ".*") #recompile every loop....
|
|
275 if valuedic[gene + "_" + fname] is 0:
|
|
276 o.write(",0,0,0")
|
|
277 else:
|
|
278 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]], gene, func, fname)
|
|
279 o.write("," + x + "," + y + "," + z)
|
|
280
|
|
281 x, y, z = get_xyz([y for x, y in curr.iteritems() if not genedic[x].startswith("unmatched")], "total", func, fname)
|
|
282 o.write("," + x + "," + y + "," + z + "\n")
|
|
283
|
|
284
|
|
285 # for testing
|
|
286 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
|
|
287 with open(seq_motif_file, 'w') as o:
|
|
288 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
|
|
289 for ID in IDlist:
|
|
290 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str(round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n")
|