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")
|
31
|
7 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column")
|
0
|
8 parser.add_argument("--output", help="Output file")
|
|
9
|
|
10 args = parser.parse_args()
|
|
11
|
4
|
12 infile = args.input
|
|
13 genes = str(args.genes).split(",")
|
31
|
14 include_fr1 = True if args.includefr1 == "yes" else False
|
4
|
15 outfile = args.output
|
|
16
|
|
17 genedic = dict()
|
0
|
18
|
|
19 mutationdic = dict()
|
|
20 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
|
|
21 linecount = 0
|
|
22
|
4
|
23 IDIndex = 0
|
|
24 best_matchIndex = 0
|
|
25 fr1Index = 0
|
|
26 cdr1Index = 0
|
|
27 fr2Index = 0
|
|
28 cdr2Index = 0
|
|
29 fr3Index = 0
|
|
30 first=True
|
26
|
31 IDlist = []
|
|
32 mutationList = []
|
|
33
|
4
|
34 with open(infile, 'r') as i:
|
|
35 for line in i:
|
|
36 if first:
|
|
37 linesplt = line.split("\t")
|
|
38 IDIndex = linesplt.index("Sequence.ID")
|
|
39 best_matchIndex = linesplt.index("best_match")
|
|
40 fr1Index = linesplt.index("FR1.IMGT")
|
|
41 cdr1Index = linesplt.index("CDR1.IMGT")
|
|
42 fr2Index = linesplt.index("FR2.IMGT")
|
|
43 cdr2Index = linesplt.index("CDR2.IMGT")
|
|
44 fr3Index = linesplt.index("FR3.IMGT")
|
|
45 first = False
|
|
46 continue
|
0
|
47 linecount += 1
|
|
48 linesplt = line.split("\t")
|
4
|
49 ID = linesplt[IDIndex]
|
|
50 genedic[ID] = linesplt[best_matchIndex]
|
31
|
51 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if include_fr1 else []
|
4
|
52 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
|
|
53 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
|
|
54 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
|
0
|
55 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
|
4
|
56 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
|
26
|
57
|
|
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 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]]
|
28
|
112 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0,0,0,0
|
|
113
|
31
|
114 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
|
|
115 for mutation in mutationList:
|
|
116 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
|
|
117 mutation_in_RGYW = any([(start <= int(where) <= end) for (start,end,region) in RGYW])
|
|
118 mutation_in_WRCY = any([(start <= int(where) <= end) for (start,end,region) in WRCY])
|
|
119 mutation_in_WA = any([(start <= int(where) <= end) for (start,end,region) in WA])
|
|
120 mutation_in_TW = any([(start <= int(where) <= end) for (start,end,region) in TW])
|
|
121
|
|
122 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
|
|
123
|
|
124 if in_how_many_motifs > 0:
|
|
125 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
|
|
126 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
|
|
127 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
|
|
128 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
|
0
|
129
|
4
|
130 directory = outfile[:outfile.rfind("/") + 1]
|
22
|
131 value = 0
|
4
|
132 valuedic = dict()
|
|
133 for gene in genes:
|
|
134 with open(directory + gene + "_value.txt", 'r') as v:
|
|
135 valuedic[gene] = int(v.readlines()[0].rstrip())
|
|
136 with open(directory + "total_value.txt", 'r') as v:
|
|
137 valuedic["total"] = int(v.readlines()[0].rstrip())
|
|
138
|
|
139 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
|
|
140 arr = ["RGYW", "WRCY", "WA", "TW"]
|
|
141 with open(outfile, 'w') as o:
|
|
142 for typ in arr:
|
|
143 o.write(typ + " (%)")
|
|
144 curr = dic[typ]
|
|
145 for gene in genes:
|
|
146 geneMatcher = re.compile(".*" + gene + ".*")
|
|
147 if valuedic[gene] is 0:
|
|
148 o.write(",0,0,0")
|
|
149 else:
|
28
|
150 x = int(round(sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]])))
|
4
|
151 y = valuedic[gene]
|
|
152 z = str(round(x / float(valuedic[gene]) * 100, 1))
|
|
153 o.write("," + str(x) + "," + str(y) + "," + z)
|
|
154 #for total
|
28
|
155 x = int(round(sum([y for x,y in curr.iteritems()])))
|
4
|
156 y = valuedic["total"]
|
|
157 z = str(round(x / float(valuedic["total"]) * 100, 1))
|
|
158 o.write("," + str(x) + "," + str(y) + "," + z + "\n")
|
21
|
159
|
|
160
|
|
161 #for testing
|
|
162 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
|
|
163 first = True
|
|
164 with open(seq_motif_file, 'w') as o:
|
|
165 for ID in IDlist:
|
|
166 if first:
|
|
167 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
|
|
168 first = False
|
|
169 continue
|
31
|
170 o.write(ID + "\t" + str(int(RGYWCount[ID])) + "\t" + str(int(WRCYCount[ID])) + "\t" + str(int(WACount[ID])) + "\t" + str(int(TWCount[ID])) + "\n")
|