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