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