comparison mutation_analysis.py @ 39:7377bf7e632d draft

Uploaded
author davidvanzessen
date Mon, 02 Nov 2015 04:52:26 -0500
parents ac9a4307861a
children 7304b91757a8
comparison
equal deleted inserted replaced
38:6b6dbbcc771d 39:7377bf7e632d
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("--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") 5 parser.add_argument("--input",
6 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") 7 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
7 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column") 8 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column")
8 parser.add_argument("--output", help="Output file") 9 parser.add_argument("--output", help="Output file")
9 10
10 args = parser.parse_args() 11 args = parser.parse_args()
11 12
12 infile = args.input 13 infile = args.input
13 genes = str(args.genes).split(",") 14 genes = str(args.genes).split(",")
14 print "includefr1 =", args.includefr1 15 print "includefr1 =", args.includefr1
15 include_fr1 = True if args.includefr1 == "yes" else False 16 include_fr1 = True if args.includefr1 == "yes" else False
16 outfile = args.output 17 outfile = args.output
17 18
18 genedic = dict() 19 genedic = dict()
19 20
20 mutationdic = dict() 21 mutationdic = dict()
21 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?") 22 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
26 fr1Index = 0 27 fr1Index = 0
27 cdr1Index = 0 28 cdr1Index = 0
28 fr2Index = 0 29 fr2Index = 0
29 cdr2Index = 0 30 cdr2Index = 0
30 fr3Index = 0 31 fr3Index = 0
31 first=True 32 first = True
32 IDlist = [] 33 IDlist = []
33 mutationList = [] 34 mutationList = []
34 35
35 with open(infile, 'r') as i: 36 with open(infile, 'r') as i:
36 for line in i: 37 for line in i:
37 if first: 38 if first:
38 linesplt = line.split("\t") 39 linesplt = line.split("\t")
39 IDIndex = linesplt.index("Sequence.ID") 40 IDIndex = linesplt.index("Sequence.ID")
40 best_matchIndex = linesplt.index("best_match") 41 best_matchIndex = linesplt.index("best_match")
41 fr1Index = linesplt.index("FR1.IMGT") 42 fr1Index = linesplt.index("FR1.IMGT")
42 cdr1Index = linesplt.index("CDR1.IMGT") 43 cdr1Index = linesplt.index("CDR1.IMGT")
43 fr2Index = linesplt.index("FR2.IMGT") 44 fr2Index = linesplt.index("FR2.IMGT")
44 cdr2Index = linesplt.index("CDR2.IMGT") 45 cdr2Index = linesplt.index("CDR2.IMGT")
45 fr3Index = linesplt.index("FR3.IMGT") 46 fr3Index = linesplt.index("FR3.IMGT")
46 first = False 47 first = False
47 continue 48 continue
48 linecount += 1 49 linecount += 1
49 linesplt = line.split("\t") 50 linesplt = line.split("\t")
50 ID = linesplt[IDIndex] 51 ID = linesplt[IDIndex]
51 genedic[ID] = linesplt[best_matchIndex] 52 genedic[ID] = linesplt[best_matchIndex]
52 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if include_fr1 else [] 53 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if
53 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] 54 x] if include_fr1 else []
54 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] 55 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
55 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] 56 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
56 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] 57 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
57 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] 58 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
58 59 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
59 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
60
61 IDlist += [ID]
62 60
61 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[
62 ID + "_CDR2"] + mutationdic[ID + "_FR3"]
63 63
64 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 64 IDlist += [ID]
65
66 AA_mutation = [0] * (int(max(mutationList, key=lambda i: int(i[4]) if i[4] else 0)[
67 4]) + 1) # [4] is the position of the AA mutation, None if silent
65 68
66 for mutation in mutationList: 69 for mutation in mutationList:
67 if mutation[4]: #if non silent mutation 70 if mutation[4]: # if non silent mutation
68 AA_mutation[int(mutation[4])] += 1 71 AA_mutation[int(mutation[4])] += 1
69 72
70 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt" 73 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt"
71 with open(aa_mutations_file, 'w') as o: 74 with open(aa_mutations_file, 'w') as o:
72 o.write(",".join([str(x) for x in AA_mutation]) + "\n") 75 o.write(",".join([str(x) for x in AA_mutation]) + "\n")
73 76
74 if linecount == 0: 77 if linecount == 0:
75 print "No data, exiting" 78 print "No data, exiting"
76 with open(outfile, 'w') as o: 79 with open(outfile, 'w') as o:
77 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) 80 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
78 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) 81 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
79 o.write("WA (%)," + ("0,0,0\n" * len(genes))) 82 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
80 o.write("TW (%)," + ("0,0,0\n" * len(genes))) 83 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
81 import sys 84 import sys
82 sys.exit() 85
86 sys.exit()
83 87
84 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") 88 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
85 RGYWCount = {g: 0 for g in genes} 89 RGYWCount = {g: 0 for g in genes}
86 WRCYCount = {g: 0 for g in genes} 90 WRCYCount = {g: 0 for g in genes}
87 WACount = {g: 0 for g in genes} 91 WACount = {g: 0 for g in genes}
92 tatIndex = 0 96 tatIndex = 0
93 aggctatIndex = 0 97 aggctatIndex = 0
94 atagcctIndex = 0 98 atagcctIndex = 0
95 first = True 99 first = True
96 with open(infile, 'r') as i: 100 with open(infile, 'r') as i:
97 for line in i: 101 for line in i:
98 if first: 102 if first:
99 linesplt = line.split("\t") 103 linesplt = line.split("\t")
100 ataIndex = linesplt.index("X.a.t.a") 104 ataIndex = linesplt.index("X.a.t.a")
101 tatIndex = linesplt.index("t.a.t.") 105 tatIndex = linesplt.index("t.a.t.")
102 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") 106 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
103 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") 107 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
104 first = False 108 first = False
105 continue 109 continue
106 linesplt = line.split("\t") 110 linesplt = line.split("\t")
107 gene = linesplt[best_matchIndex] 111 gene = linesplt[best_matchIndex]
108 ID = linesplt[IDIndex] 112 ID = linesplt[IDIndex]
109 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] 113 RGYW = [(int(x), int(y), z) for (x, y, z) in
110 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] 114 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
111 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] 115 WRCY = [(int(x), int(y), z) for (x, y, z) in
112 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] 116 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
113 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0,0,0,0 117 WA = [(int(x), int(y), z) for (x, y, z) in
114 118 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
115 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] 119 TW = [(int(x), int(y), z) for (x, y, z) in
116 for mutation in mutationList: 120 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
117 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation 121 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
118 mutation_in_RGYW = any([(start <= int(where) <= end) for (start,end,region) in RGYW]) 122
119 mutation_in_WRCY = any([(start <= int(where) <= end) for (start,end,region) in WRCY]) 123 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[
120 mutation_in_WA = any([(start <= int(where) <= end) for (start,end,region) in WA]) 124 ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
121 mutation_in_TW = any([(start <= int(where) <= end) for (start,end,region) in TW]) 125 for mutation in mutationList:
122 126 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
123 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) 127 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW])
124 128 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY])
125 if in_how_many_motifs > 0: 129 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA])
126 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs 130 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW])
127 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs 131
128 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs 132 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
129 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs 133
134 if in_how_many_motifs > 0:
135 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
136 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
137 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
138 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
130 139
131 directory = outfile[:outfile.rfind("/") + 1] 140 directory = outfile[:outfile.rfind("/") + 1]
132 value = 0 141 value = 0
133 valuedic = dict() 142 valuedic = dict()
134 for gene in genes: 143 for gene in genes:
135 with open(directory + gene + "_value.txt", 'r') as v: 144 with open(directory + gene + "_value.txt", 'r') as v:
136 valuedic[gene] = int(v.readlines()[0].rstrip()) 145 valuedic[gene] = int(v.readlines()[0].rstrip())
137 with open(directory + "total_value.txt", 'r') as v: 146 with open(directory + "total_value.txt", 'r') as v:
138 valuedic["total"] = int(v.readlines()[0].rstrip()) 147 valuedic["total"] = int(v.readlines()[0].rstrip())
139 148
140 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} 149 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
141 arr = ["RGYW", "WRCY", "WA", "TW"] 150 arr = ["RGYW", "WRCY", "WA", "TW"]
142 with open(outfile, 'w') as o: 151 with open(outfile, 'w') as o:
143 for typ in arr: 152 for typ in arr:
144 o.write(typ + " (%)") 153 o.write(typ + " (%)")
145 curr = dic[typ] 154 curr = dic[typ]
146 for gene in genes: 155 for gene in genes:
147 geneMatcher = re.compile(".*" + gene + ".*") 156 geneMatcher = re.compile(".*" + gene + ".*")
148 if valuedic[gene] is 0: 157 if valuedic[gene] is 0:
149 o.write(",0,0,0") 158 o.write(",0,0,0")
150 else: 159 else:
151 x = int(round(sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]]))) 160 x = int(round(sum([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]])))
152 y = valuedic[gene] 161 y = valuedic[gene]
153 z = str(round(x / float(valuedic[gene]) * 100, 1)) 162 z = str(round(x / float(valuedic[gene]) * 100, 1))
154 o.write("," + str(x) + "," + str(y) + "," + z) 163 o.write("," + str(x) + "," + str(y) + "," + z)
155 #for total 164 # for total
156 x = int(round(sum([y for x,y in curr.iteritems()]))) 165 x = int(round(sum([y for x, y in curr.iteritems()])))
157 y = valuedic["total"] 166 y = valuedic["total"]
158 z = str(round(x / float(valuedic["total"]) * 100, 1)) 167 z = str(round(x / float(valuedic["total"]) * 100, 1))
159 o.write("," + str(x) + "," + str(y) + "," + z + "\n") 168 o.write("," + str(x) + "," + str(y) + "," + z + "\n")
160 169
161 170
162 #for testing 171 # for testing
163 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" 172 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
164 first = True
165 with open(seq_motif_file, 'w') as o: 173 with open(seq_motif_file, 'w') as o:
166 for ID in IDlist: 174 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
167 if first: 175 for ID in IDlist:
168 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n") 176 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str(
169 first = False 177 round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n")
170 continue
171 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str(round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n")