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
|
21
|
75 IDlist = []
|
4
|
76 with open(infile, 'r') as i:
|
|
77 for line in i:
|
|
78 if first:
|
|
79 linesplt = line.split("\t")
|
|
80 ataIndex = linesplt.index("X.a.t.a")
|
|
81 tatIndex = linesplt.index("t.a.t.")
|
|
82 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
|
|
83 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
|
|
84 first = False
|
0
|
85 continue
|
4
|
86 linesplt = line.split("\t")
|
|
87 gene = linesplt[best_matchIndex]
|
|
88 ID = linesplt[IDIndex]
|
|
89 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
|
|
90 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
|
|
91 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
|
|
92 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
|
|
93 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]])])
|
|
94 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]])])
|
|
95 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]])])
|
|
96 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]])])
|
21
|
97 IDlist += [ID]
|
0
|
98
|
|
99
|
4
|
100 directory = outfile[:outfile.rfind("/") + 1]
|
22
|
101 value = 0
|
4
|
102 valuedic = dict()
|
|
103 for gene in genes:
|
|
104 with open(directory + gene + "_value.txt", 'r') as v:
|
|
105 valuedic[gene] = int(v.readlines()[0].rstrip())
|
|
106 with open(directory + "total_value.txt", 'r') as v:
|
|
107 valuedic["total"] = int(v.readlines()[0].rstrip())
|
|
108
|
|
109 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
|
|
110 arr = ["RGYW", "WRCY", "WA", "TW"]
|
|
111 with open(outfile, 'w') as o:
|
|
112 for typ in arr:
|
|
113 o.write(typ + " (%)")
|
|
114 curr = dic[typ]
|
|
115 for gene in genes:
|
|
116 geneMatcher = re.compile(".*" + gene + ".*")
|
|
117 if valuedic[gene] is 0:
|
|
118 o.write(",0,0,0")
|
|
119 else:
|
|
120 x = sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]])
|
|
121 y = valuedic[gene]
|
|
122 z = str(round(x / float(valuedic[gene]) * 100, 1))
|
|
123 o.write("," + str(x) + "," + str(y) + "," + z)
|
|
124 #for total
|
|
125 x = sum([y for x,y in curr.iteritems()])
|
|
126 y = valuedic["total"]
|
|
127 z = str(round(x / float(valuedic["total"]) * 100, 1))
|
|
128 o.write("," + str(x) + "," + str(y) + "," + z + "\n")
|
21
|
129
|
|
130
|
|
131 #for testing
|
|
132 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
|
|
133 first = True
|
|
134 with open(seq_motif_file, 'w') as o:
|
|
135 for ID in IDlist:
|
|
136 if first:
|
|
137 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
|
|
138 first = False
|
|
139 continue
|
|
140 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n")
|