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
|
|
29 with open(infile, 'r') as i:
|
|
30 for line in i:
|
|
31 if first:
|
|
32 linesplt = line.split("\t")
|
|
33 IDIndex = linesplt.index("Sequence.ID")
|
|
34 best_matchIndex = linesplt.index("best_match")
|
|
35 fr1Index = linesplt.index("FR1.IMGT")
|
|
36 cdr1Index = linesplt.index("CDR1.IMGT")
|
|
37 fr2Index = linesplt.index("FR2.IMGT")
|
|
38 cdr2Index = linesplt.index("CDR2.IMGT")
|
|
39 fr3Index = linesplt.index("FR3.IMGT")
|
|
40 first = False
|
|
41 continue
|
0
|
42 linecount += 1
|
|
43 linesplt = line.split("\t")
|
4
|
44 ID = linesplt[IDIndex]
|
|
45 genedic[ID] = linesplt[best_matchIndex]
|
|
46 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x]
|
|
47 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
|
|
48 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
|
|
49 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
|
0
|
50 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
|
4
|
51 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
|
0
|
52
|
|
53 if linecount == 0:
|
|
54 print "No data, exiting"
|
|
55 with open(outfile, 'w') as o:
|
4
|
56 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
|
|
57 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
|
|
58 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
|
|
59 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
|
0
|
60 import sys
|
|
61 sys.exit()
|
|
62
|
|
63 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
|
4
|
64 RGYWCount = {g: 0 for g in genes}
|
|
65 WRCYCount = {g: 0 for g in genes}
|
|
66 WACount = {g: 0 for g in genes}
|
|
67 TWCount = {g: 0 for g in genes}
|
0
|
68
|
4
|
69 IDIndex = 0
|
|
70 ataIndex = 0
|
|
71 tatIndex = 0
|
|
72 aggctatIndex = 0
|
|
73 atagcctIndex = 0
|
|
74 first = True
|
|
75 with open(infile, 'r') as i:
|
|
76 for line in i:
|
|
77 if first:
|
|
78 linesplt = line.split("\t")
|
|
79 ataIndex = linesplt.index("X.a.t.a")
|
|
80 tatIndex = linesplt.index("t.a.t.")
|
|
81 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
|
|
82 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
|
|
83 first = False
|
0
|
84 continue
|
4
|
85 linesplt = line.split("\t")
|
|
86 gene = linesplt[best_matchIndex]
|
|
87 ID = linesplt[IDIndex]
|
|
88 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
|
|
89 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
|
|
90 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
|
|
91 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
|
|
92 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]])])
|
|
93 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]])])
|
|
94 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]])])
|
|
95 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]])])
|
0
|
96
|
|
97
|
4
|
98 directory = outfile[:outfile.rfind("/") + 1]
|
|
99 value = 0;
|
|
100 valuedic = dict()
|
|
101 for gene in genes:
|
|
102 with open(directory + gene + "_value.txt", 'r') as v:
|
|
103 valuedic[gene] = int(v.readlines()[0].rstrip())
|
|
104 with open(directory + "total_value.txt", 'r') as v:
|
|
105 valuedic["total"] = int(v.readlines()[0].rstrip())
|
|
106
|
|
107 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
|
|
108 arr = ["RGYW", "WRCY", "WA", "TW"]
|
|
109 with open(outfile, 'w') as o:
|
|
110 for typ in arr:
|
|
111 o.write(typ + " (%)")
|
|
112 curr = dic[typ]
|
|
113 for gene in genes:
|
|
114 geneMatcher = re.compile(".*" + gene + ".*")
|
|
115 if valuedic[gene] is 0:
|
|
116 o.write(",0,0,0")
|
|
117 else:
|
|
118 x = sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]])
|
|
119 y = valuedic[gene]
|
|
120 z = str(round(x / float(valuedic[gene]) * 100, 1))
|
|
121 o.write("," + str(x) + "," + str(y) + "," + z)
|
|
122 #for total
|
|
123 x = sum([y for x,y in curr.iteritems()])
|
|
124 y = valuedic["total"]
|
|
125 z = str(round(x / float(valuedic["total"]) * 100, 1))
|
|
126 o.write("," + str(x) + "," + str(y) + "," + z + "\n")
|