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