0
|
1 import re
|
|
2 import argparse
|
|
3
|
|
4 parser = argparse.ArgumentParser()
|
4
|
5 parser.add_argument("--input", help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation")
|
|
6 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
|
0
|
7 parser.add_argument("--output", help="Output file")
|
|
8
|
|
9 args = parser.parse_args()
|
|
10
|
4
|
11 infile = args.input
|
|
12 genes = str(args.genes).split(",")
|
|
13 outfile = args.output
|
|
14
|
|
15 genedic = dict()
|
0
|
16
|
|
17 mutationdic = dict()
|
|
18 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
|
|
19 linecount = 0
|
|
20
|
4
|
21 IDIndex = 0
|
|
22 best_matchIndex = 0
|
|
23 fr1Index = 0
|
|
24 cdr1Index = 0
|
|
25 fr2Index = 0
|
|
26 cdr2Index = 0
|
|
27 fr3Index = 0
|
|
28 first=True
|
26
|
29 IDlist = []
|
|
30 mutationList = []
|
|
31
|
4
|
32 with open(infile, 'r') as i:
|
|
33 for line in i:
|
|
34 if first:
|
|
35 linesplt = line.split("\t")
|
|
36 IDIndex = linesplt.index("Sequence.ID")
|
|
37 best_matchIndex = linesplt.index("best_match")
|
|
38 fr1Index = linesplt.index("FR1.IMGT")
|
|
39 cdr1Index = linesplt.index("CDR1.IMGT")
|
|
40 fr2Index = linesplt.index("FR2.IMGT")
|
|
41 cdr2Index = linesplt.index("CDR2.IMGT")
|
|
42 fr3Index = linesplt.index("FR3.IMGT")
|
|
43 first = False
|
|
44 continue
|
0
|
45 linecount += 1
|
|
46 linesplt = line.split("\t")
|
4
|
47 ID = linesplt[IDIndex]
|
|
48 genedic[ID] = linesplt[best_matchIndex]
|
|
49 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x]
|
|
50 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
|
|
51 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
|
|
52 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
|
0
|
53 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
|
4
|
54 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
|
26
|
55
|
|
56 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
|
|
57
|
|
58 IDlist += [ID]
|
|
59
|
|
60
|
28
|
61 AA_mutation = [0] * (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
|
26
|
62
|
|
63 for mutation in mutationList:
|
|
64 if mutation[4]: #if non silent mutation
|
|
65 AA_mutation[int(mutation[4])] += 1
|
|
66
|
|
67 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt"
|
|
68 with open(aa_mutations_file, 'w') as o:
|
|
69 o.write(",".join([str(x) for x in AA_mutation]) + "\n")
|
0
|
70
|
|
71 if linecount == 0:
|
|
72 print "No data, exiting"
|
|
73 with open(outfile, 'w') as o:
|
4
|
74 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
|
|
75 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
|
|
76 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
|
|
77 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
|
0
|
78 import sys
|
|
79 sys.exit()
|
|
80
|
|
81 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
|
4
|
82 RGYWCount = {g: 0 for g in genes}
|
|
83 WRCYCount = {g: 0 for g in genes}
|
|
84 WACount = {g: 0 for g in genes}
|
|
85 TWCount = {g: 0 for g in genes}
|
0
|
86
|
4
|
87 IDIndex = 0
|
|
88 ataIndex = 0
|
|
89 tatIndex = 0
|
|
90 aggctatIndex = 0
|
|
91 atagcctIndex = 0
|
|
92 first = True
|
|
93 with open(infile, 'r') as i:
|
|
94 for line in i:
|
|
95 if first:
|
|
96 linesplt = line.split("\t")
|
|
97 ataIndex = linesplt.index("X.a.t.a")
|
|
98 tatIndex = linesplt.index("t.a.t.")
|
|
99 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
|
|
100 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
|
|
101 first = False
|
0
|
102 continue
|
4
|
103 linesplt = line.split("\t")
|
|
104 gene = linesplt[best_matchIndex]
|
|
105 ID = linesplt[IDIndex]
|
|
106 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
|
|
107 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
|
|
108 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
|
|
109 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
|
28
|
110 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0,0,0,0
|
|
111 for (x,y,z) in RGYW: #RGYWCount[ID] = sum([1 for (x,y,z) in RGYW if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
|
|
112 if not z or z == "CDR3":
|
|
113 continue
|
30
|
114 mutations_in_motif = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
115 in_other_motifs = sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in WRCY]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in WA]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in TW])
|
|
116 if mutations_in_motif > 0:
|
|
117 RGYWCount[ID] += 1.0 / in_other_motifs if in_other_motifs > 0 else 1
|
|
118 if in_other_motifs > 1:
|
|
119 print in_other_motifs, ID, "RGYW", x, y, ([(x,y,z) for (xother, yother, zother) in WRCY if ((x <= int(xother) <= y) and (x <= int(yother) <= y))] + [(x,y,z) for (xother, yother, zother) in WA if ((x <= int(xother) <= y) and (x <= int(yother) <= y))] + [(x,y,z) for (xother, yother, zother) in TW if ((x <= int(xother) <= y) and (x <= int(yother) <= y))])
|
28
|
120
|
|
121 for (x,y,z) in WRCY: #WRCYCount[ID] = sum([1 for (x,y,z) in WRCY if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
|
|
122 if not z or z == "CDR3":
|
|
123 continue
|
30
|
124 mutations_in_motif = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
125 in_other_motifs = sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in RGYW]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in WA]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in TW])
|
|
126 if mutations_in_motif > 0:
|
|
127 WRCYCount[ID] += 1.0 / in_other_motifs if in_other_motifs > 0 else 1
|
|
128 if in_other_motifs > 1:
|
|
129 print in_other_motifs, ID, "WRCY", x, y
|
28
|
130
|
|
131 for (x,y,z) in WA: #WACount[ID] = sum([1 for (x,y,z) in WA if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
|
|
132 if not z or z == "CDR3":
|
|
133 continue
|
30
|
134 mutations_in_motif = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
135 in_other_motifs = sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in RGYW]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in WRCY]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in TW])
|
|
136 if mutations_in_motif > 0:
|
|
137 WACount[ID] += 1.0 / in_other_motifs if in_other_motifs > 0 else 1
|
|
138 if in_other_motifs > 1:
|
|
139 print in_other_motifs, ID, "WA", x, y
|
28
|
140
|
|
141 for (x,y,z) in TW: #TWCount[ID] = sum([1 for (x,y,z) in TW if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
|
|
142 if not z or z == "CDR3":
|
|
143 continue
|
30
|
144 mutations_in_motif = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
145 in_other_motifs = sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in RGYW]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in WRCY]) + sum([((x <= int(xother) <= y) and (x <= int(yother) <= y)) for (xother, yother, zother) in WA])
|
|
146 if mutations_in_motif > 0:
|
|
147 TWCount[ID] += 1.0 / in_other_motifs if in_other_motifs > 0 else 1
|
|
148 if in_other_motifs > 1:
|
|
149 print in_other_motifs, ID, "TW", x, y
|
0
|
150
|
|
151
|
4
|
152 directory = outfile[:outfile.rfind("/") + 1]
|
22
|
153 value = 0
|
4
|
154 valuedic = dict()
|
|
155 for gene in genes:
|
|
156 with open(directory + gene + "_value.txt", 'r') as v:
|
|
157 valuedic[gene] = int(v.readlines()[0].rstrip())
|
|
158 with open(directory + "total_value.txt", 'r') as v:
|
|
159 valuedic["total"] = int(v.readlines()[0].rstrip())
|
|
160
|
|
161 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
|
|
162 arr = ["RGYW", "WRCY", "WA", "TW"]
|
|
163 with open(outfile, 'w') as o:
|
|
164 for typ in arr:
|
|
165 o.write(typ + " (%)")
|
|
166 curr = dic[typ]
|
|
167 for gene in genes:
|
|
168 geneMatcher = re.compile(".*" + gene + ".*")
|
|
169 if valuedic[gene] is 0:
|
|
170 o.write(",0,0,0")
|
|
171 else:
|
28
|
172 x = int(round(sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]])))
|
4
|
173 y = valuedic[gene]
|
|
174 z = str(round(x / float(valuedic[gene]) * 100, 1))
|
|
175 o.write("," + str(x) + "," + str(y) + "," + z)
|
|
176 #for total
|
28
|
177 x = int(round(sum([y for x,y in curr.iteritems()])))
|
4
|
178 y = valuedic["total"]
|
|
179 z = str(round(x / float(valuedic["total"]) * 100, 1))
|
|
180 o.write("," + str(x) + "," + str(y) + "," + z + "\n")
|
21
|
181
|
|
182
|
|
183 #for testing
|
|
184 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
|
|
185 first = True
|
|
186 with open(seq_motif_file, 'w') as o:
|
|
187 for ID in IDlist:
|
|
188 if first:
|
|
189 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
|
|
190 first = False
|
|
191 continue
|
|
192 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n")
|