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