comparison mutation_analysis.py @ 4:069419cccba4 draft

Uploaded
author davidvanzessen
date Mon, 22 Sep 2014 10:19:36 -0400
parents 74d2bc479bee
children c9f9623f1f76
comparison
equal deleted inserted replaced
3:a0b27058dcac 4:069419cccba4
1 import re 1 import re
2 import argparse 2 import argparse
3 3
4 parser = argparse.ArgumentParser() 4 parser = argparse.ArgumentParser()
5 parser.add_argument("--mutationfile", help="The '7_V-REGION-mutation-and-AA-change-table' file from the IMGT output") 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("--hotspotfile", help="The '10_V-REGION-mutation-hotspots' file from the IMGT output") 6 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
7 parser.add_argument("--output", help="Output file") 7 parser.add_argument("--output", help="Output file")
8 8
9 args = parser.parse_args() 9 args = parser.parse_args()
10 10
11 mutationfile = args.mutationfile #"test_VH-Ca_Cg_25nt/7_V-REGION-mutation-and-AA-change-table_test_VH-Ca_Cg_25nt_241013.txt" 11 infile = args.input
12 hotspotsfile = args.hotspotfile #"test_VH-Ca_Cg_25nt/10_V-REGION-mutation-hotspots_test_VH-Ca_Cg_25nt_241013.txt" 12 genes = str(args.genes).split(",")
13 outfile = args.output #"out.txt" 13 outfile = args.output
14
15 genedic = dict()
14 16
15 mutationdic = dict() 17 mutationdic = dict()
16 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?") 18 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
17 linecount = 0 19 linecount = 0
18 20
19 with open(mutationfile, 'r') as i: 21 IDIndex = 0
20 for line in i.readlines()[1:]: 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
21 linecount += 1 42 linecount += 1
22 linesplt = line.split("\t") 43 linesplt = line.split("\t")
23 if linesplt[2] != "productive": 44 ID = linesplt[IDIndex]
24 continue 45 genedic[ID] = linesplt[best_matchIndex]
25 ID = linesplt[1] 46 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x]
26 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[5].split("|") if x] 47 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
27 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[6].split("|") if x] 48 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
28 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[7].split("|") if x] 49 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
29 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[8].split("|") if x]
30 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] 50 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
31 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[9].split("|") if x] 51 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
32 52
33 if linecount == 0: 53 if linecount == 0:
34 print "No data, exiting" 54 print "No data, exiting"
35 with open(outfile, 'w') as o: 55 with open(outfile, 'w') as o:
36 o.write("RGYW (%),0,0,NA\n") 56 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
37 o.write("WRCY (%),0,0,NA\n") 57 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
38 o.write("WA (%),0,0,NA\n") 58 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
39 o.write("TW (%),0,0,NA\n") 59 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
40 import sys 60 import sys
41 sys.exit() 61 sys.exit()
42 62
43 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") 63 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
44 RGYWCount = 0 64 RGYWCount = {g: 0 for g in genes}
45 WRCYCount = 0 65 WRCYCount = {g: 0 for g in genes}
46 WACount = 0 66 WACount = {g: 0 for g in genes}
47 TWCount = 0 67 TWCount = {g: 0 for g in genes}
48 68
49 with open(hotspotsfile, 'r') as i: 69 IDIndex = 0
50 for line in i.readlines()[1:]: 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
84 continue
51 linesplt = line.split("\t") 85 linesplt = line.split("\t")
52 if linesplt[2] != "productive": 86 gene = linesplt[best_matchIndex]
53 continue 87 ID = linesplt[IDIndex]
54 ID = linesplt[1] 88 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
55 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[6].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]]
56 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[7].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]]
57 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[4].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]]
58 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[5].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]])])
59 RGYWCount += sum([1 for (x,y,z) in RGYW if 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]])])
60 WRCYCount += sum([1 for (x,y,z) in WRCY if 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]])])
61 WACount += sum([1 for (x,y,z) in WA if 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]])])
62 TWCount += sum([1 for (x,y,z) in TW if z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
63
64 path = outfile[:outfile.rfind("/") + 1] + "mutations.txt"
65 value = 0;
66 with open(path, 'r') as f:
67 value = f.readlines()[0].split(",")[1]
68 with open(outfile, 'w') as o:
69 if value != "0":
70 o.write("RGYW (%)," + str(RGYWCount) + "," + value + "," + str(round(RGYWCount / float(value) * 100, 1)) + "\n")
71 o.write("WRCY (%)," + str(WRCYCount) + "," + value + "," + str(round(WRCYCount / float(value) * 100, 1)) + "\n")
72 o.write("WA (%)," + str(WACount) + "," + value + "," + str(round(WACount / float(value) * 100, 1)) + "\n")
73 o.write("TW (%)," + str(TWCount) + "," + value + "," + str(round(TWCount / float(value) * 100, 1)) + "\n")
74 else:
75 o.write("RGYW (%)," + str(RGYWCount) + ",0,NA\n")
76 o.write("WRCY (%)," + str(WRCYCount) + ",0,NA\n")
77 o.write("WA (%)," + str(WACount) + ",0,NA\n")
78 o.write("TW (%)," + str(TWCount) + ",0,NA\n")
79 96
80 97
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")