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
|
28
|
56 print mutationdic[ID + "_FR1"]
|
|
57
|
26
|
58 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
|
|
59
|
|
60 IDlist += [ID]
|
|
61
|
|
62
|
28
|
63 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
|
64
|
|
65 for mutation in mutationList:
|
|
66 if mutation[4]: #if non silent mutation
|
|
67 AA_mutation[int(mutation[4])] += 1
|
|
68
|
|
69 print AA_mutation
|
|
70
|
|
71 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt"
|
|
72 with open(aa_mutations_file, 'w') as o:
|
|
73 o.write(",".join([str(x) for x in AA_mutation]) + "\n")
|
0
|
74
|
|
75 if linecount == 0:
|
|
76 print "No data, exiting"
|
|
77 with open(outfile, 'w') as o:
|
4
|
78 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
|
|
79 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
|
|
80 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
|
|
81 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
|
0
|
82 import sys
|
|
83 sys.exit()
|
|
84
|
|
85 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
|
4
|
86 RGYWCount = {g: 0 for g in genes}
|
|
87 WRCYCount = {g: 0 for g in genes}
|
|
88 WACount = {g: 0 for g in genes}
|
|
89 TWCount = {g: 0 for g in genes}
|
0
|
90
|
4
|
91 IDIndex = 0
|
|
92 ataIndex = 0
|
|
93 tatIndex = 0
|
|
94 aggctatIndex = 0
|
|
95 atagcctIndex = 0
|
|
96 first = True
|
|
97 with open(infile, 'r') as i:
|
|
98 for line in i:
|
|
99 if first:
|
|
100 linesplt = line.split("\t")
|
|
101 ataIndex = linesplt.index("X.a.t.a")
|
|
102 tatIndex = linesplt.index("t.a.t.")
|
|
103 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
|
|
104 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
|
|
105 first = False
|
0
|
106 continue
|
4
|
107 linesplt = line.split("\t")
|
|
108 gene = linesplt[best_matchIndex]
|
|
109 ID = linesplt[IDIndex]
|
|
110 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
|
|
111 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
|
|
112 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
|
|
113 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
|
28
|
114 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0,0,0,0
|
|
115 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]])])
|
|
116 if not z or z == "CDR3":
|
|
117 continue
|
|
118 in_mutations = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
119 if in_mutations > 0:
|
|
120 RGYWCount[ID] += 1.0 / in_mutations
|
|
121
|
|
122 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]])])
|
|
123 if not z or z == "CDR3":
|
|
124 continue
|
|
125 in_mutations = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
126 if in_mutations > 0:
|
|
127 WRCYCount[ID] += 1.0 / in_mutations
|
|
128
|
|
129 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]])])
|
|
130 if not z or z == "CDR3":
|
|
131 continue
|
|
132 in_mutations = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
133 if in_mutations > 0:
|
|
134 WACount[ID] += 1.0 / in_mutations
|
|
135
|
|
136 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]])])
|
|
137 if not z or z == "CDR3":
|
|
138 continue
|
|
139 in_mutations = sum([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])
|
|
140 if in_mutations > 0:
|
|
141 TWCount[ID] += 1.0 / in_mutations
|
26
|
142
|
0
|
143
|
|
144
|
4
|
145 directory = outfile[:outfile.rfind("/") + 1]
|
22
|
146 value = 0
|
4
|
147 valuedic = dict()
|
|
148 for gene in genes:
|
|
149 with open(directory + gene + "_value.txt", 'r') as v:
|
|
150 valuedic[gene] = int(v.readlines()[0].rstrip())
|
|
151 with open(directory + "total_value.txt", 'r') as v:
|
|
152 valuedic["total"] = int(v.readlines()[0].rstrip())
|
|
153
|
|
154 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
|
|
155 arr = ["RGYW", "WRCY", "WA", "TW"]
|
|
156 with open(outfile, 'w') as o:
|
|
157 for typ in arr:
|
|
158 o.write(typ + " (%)")
|
|
159 curr = dic[typ]
|
|
160 for gene in genes:
|
|
161 geneMatcher = re.compile(".*" + gene + ".*")
|
|
162 if valuedic[gene] is 0:
|
|
163 o.write(",0,0,0")
|
|
164 else:
|
28
|
165 x = int(round(sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]])))
|
4
|
166 y = valuedic[gene]
|
|
167 z = str(round(x / float(valuedic[gene]) * 100, 1))
|
|
168 o.write("," + str(x) + "," + str(y) + "," + z)
|
|
169 #for total
|
28
|
170 x = int(round(sum([y for x,y in curr.iteritems()])))
|
4
|
171 y = valuedic["total"]
|
|
172 z = str(round(x / float(valuedic["total"]) * 100, 1))
|
|
173 o.write("," + str(x) + "," + str(y) + "," + z + "\n")
|
21
|
174
|
|
175
|
|
176 #for testing
|
|
177 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
|
|
178 first = True
|
|
179 with open(seq_motif_file, 'w') as o:
|
|
180 for ID in IDlist:
|
|
181 if first:
|
|
182 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
|
|
183 first = False
|
|
184 continue
|
|
185 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n")
|