Mercurial > repos > davidvanzessen > shm_csr
comparison shm_csr.py @ 47:64711f461c8e draft
Uploaded
author | davidvanzessen |
---|---|
date | Thu, 04 May 2017 07:43:09 -0400 |
parents | 77a7ac76c7b9 |
children | c5295dd10dfc |
comparison
equal
deleted
inserted
replaced
46:cfc9a442e59d | 47:64711f461c8e |
---|---|
1 from __future__ import division | 1 import argparse |
2 import logging | |
3 import sys | |
4 import os | |
5 import re | |
6 | |
2 from collections import defaultdict | 7 from collections import defaultdict |
3 import re | 8 |
4 import argparse | 9 def main(): |
5 | 10 parser = argparse.ArgumentParser() |
6 parser = argparse.ArgumentParser() | 11 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") |
7 parser.add_argument("--input", | 12 parser.add_argument("--genes", help="The genes available in the 'best_match' column") |
8 help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation") | 13 parser.add_argument("--empty_region_filter", help="Where does the sequence start?", choices=['leader', 'FR1', 'CDR1', 'FR2']) |
9 parser.add_argument("--genes", help="The genes available in the 'best_match' column") | 14 parser.add_argument("--output", help="Output file") |
10 parser.add_argument("--empty_region_filter", help="Where does the sequence start?", choices=['leader', 'FR1', 'CDR1', 'FR2']) | 15 |
11 parser.add_argument("--output", help="Output file") | 16 args = parser.parse_args() |
12 | 17 |
13 args = parser.parse_args() | 18 infile = args.input |
14 | 19 genes = str(args.genes).split(",") |
15 infile = args.input | 20 empty_region_filter = args.empty_region_filter |
16 genes = str(args.genes).split(",") | 21 outfile = args.output |
17 empty_region_filter = args.empty_region_filter | 22 |
18 outfile = args.output | 23 genedic = dict() |
19 | 24 |
20 genedic = dict() | 25 mutationdic = dict() |
21 | 26 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?") |
22 mutationdic = dict() | 27 NAMatchResult = (None, None, None, None, None, None, '') |
23 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?") | 28 geneMatchers = {gene: re.compile("^" + gene + ".*") for gene in genes} |
24 NAMatchResult = (None, None, None, None, None, None, '') | 29 linecount = 0 |
25 linecount = 0 | 30 |
26 | 31 IDIndex = 0 |
27 IDIndex = 0 | 32 best_matchIndex = 0 |
28 best_matchIndex = 0 | 33 fr1Index = 0 |
29 fr1Index = 0 | 34 cdr1Index = 0 |
30 cdr1Index = 0 | 35 fr2Index = 0 |
31 fr2Index = 0 | 36 cdr2Index = 0 |
32 cdr2Index = 0 | 37 fr3Index = 0 |
33 fr3Index = 0 | 38 first = True |
34 first = True | 39 IDlist = [] |
35 IDlist = [] | 40 mutationList = [] |
36 mutationList = [] | 41 mutationListByID = {} |
37 mutationListByID = {} | 42 cdr1LengthDic = {} |
38 cdr1LengthDic = {} | 43 cdr2LengthDic = {} |
39 cdr2LengthDic = {} | 44 |
40 | 45 fr1LengthDict = {} |
41 with open(infile, 'ru') as i: | 46 fr2LengthDict = {} |
42 for line in i: | 47 fr3LengthDict = {} |
43 if first: | 48 |
49 cdr1LengthIndex = 0 | |
50 cdr2LengthIndex = 0 | |
51 | |
52 fr1SeqIndex = 0 | |
53 fr2SeqIndex = 0 | |
54 fr3SeqIndex = 0 | |
55 | |
56 tandem_sum_by_class = defaultdict(int) | |
57 expected_tandem_sum_by_class = defaultdict(float) | |
58 | |
59 with open(infile, 'ru') as i: | |
60 for line in i: | |
61 if first: | |
62 linesplt = line.split("\t") | |
63 IDIndex = linesplt.index("Sequence.ID") | |
64 best_matchIndex = linesplt.index("best_match") | |
65 fr1Index = linesplt.index("FR1.IMGT") | |
66 cdr1Index = linesplt.index("CDR1.IMGT") | |
67 fr2Index = linesplt.index("FR2.IMGT") | |
68 cdr2Index = linesplt.index("CDR2.IMGT") | |
69 fr3Index = linesplt.index("FR3.IMGT") | |
70 cdr1LengthIndex = linesplt.index("CDR1.IMGT.seq") | |
71 cdr2LengthIndex = linesplt.index("CDR2.IMGT.seq") | |
72 fr1SeqIndex = linesplt.index("FR1.IMGT.seq") | |
73 fr2SeqIndex = linesplt.index("FR2.IMGT.seq") | |
74 fr3SeqIndex = linesplt.index("FR3.IMGT.seq") | |
75 first = False | |
76 continue | |
77 linecount += 1 | |
44 linesplt = line.split("\t") | 78 linesplt = line.split("\t") |
45 IDIndex = linesplt.index("Sequence.ID") | 79 ID = linesplt[IDIndex] |
46 best_matchIndex = linesplt.index("best_match") | 80 genedic[ID] = linesplt[best_matchIndex] |
47 fr1Index = linesplt.index("FR1.IMGT") | 81 try: |
48 cdr1Index = linesplt.index("CDR1.IMGT") | 82 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if (linesplt[fr1Index] != "NA" and empty_region_filter == "leader") else [] |
49 fr2Index = linesplt.index("FR2.IMGT") | 83 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] if (linesplt[cdr1Index] != "NA" and empty_region_filter in ["leader", "FR1"]) else [] |
50 cdr2Index = linesplt.index("CDR2.IMGT") | 84 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] if (linesplt[fr2Index] != "NA" and empty_region_filter in ["leader", "FR1", "CDR1"]) else [] |
51 fr3Index = linesplt.index("FR3.IMGT") | 85 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] if (linesplt[cdr2Index] != "NA") else [] |
52 cdr1LengthIndex = linesplt.index("CDR1.IMGT.length") | 86 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] |
53 cdr2LengthIndex = linesplt.index("CDR2.IMGT.length") | 87 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] if linesplt[fr3Index] != "NA" else [] |
54 first = False | 88 except Exception as e: |
55 continue | 89 print "Something went wrong while processing this line:" |
56 linecount += 1 | 90 print linesplt |
57 linesplt = line.split("\t") | 91 print linecount |
58 ID = linesplt[IDIndex] | 92 print e |
59 genedic[ID] = linesplt[best_matchIndex] | 93 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] |
60 try: | 94 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] |
61 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if (linesplt[fr1Index] != "NA" and empty_region_filter == "leader") else [] | 95 |
62 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] if (linesplt[cdr1Index] != "NA" and empty_region_filter in ["leader", "FR1"]) else [] | 96 cdr1Length = len(linesplt[cdr1LengthIndex]) |
63 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] if (linesplt[fr2Index] != "NA" and empty_region_filter in ["leader", "FR1", "CDR1"]) else [] | 97 cdr2Length = len(linesplt[cdr2LengthIndex]) |
64 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] if (linesplt[cdr2Index] != "NA") else [] | 98 |
65 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] | 99 #print linesplt[fr2SeqIndex] |
66 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] if linesplt[fr3Index] != "NA" else [] | 100 fr1Length = len(linesplt[fr1SeqIndex]) if empty_region_filter == "leader" else 0 |
67 except Exception as e: | 101 fr2Length = len(linesplt[fr2SeqIndex]) if empty_region_filter in ["leader", "FR1", "CDR1"] else 0 |
68 print "Something went wrong while processing this line:" | 102 fr3Length = len(linesplt[fr3SeqIndex]) |
69 print linesplt | 103 |
70 print linecount | 104 cdr1LengthDic[ID] = cdr1Length |
71 print e | 105 cdr2LengthDic[ID] = cdr2Length |
72 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | 106 |
73 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | 107 fr1LengthDict[ID] = fr1Length |
74 | 108 fr2LengthDict[ID] = fr2Length |
75 cdr1Length = linesplt[cdr1LengthIndex] | 109 fr3LengthDict[ID] = fr3Length |
76 cdr2Length = linesplt[cdr2LengthIndex] | 110 |
77 | 111 IDlist += [ID] |
78 cdr1LengthDic[ID] = int(cdr1Length) if cdr1Length != "X" else 0 | 112 |
79 cdr2LengthDic[ID] = int(cdr2Length) if cdr2Length != "X" else 0 | 113 |
114 #tandem mutation stuff | |
115 tandem_frequency = defaultdict(int) | |
116 mutation_frequency = defaultdict(int) | |
117 | |
118 tandem_file = os.path.join(os.path.dirname(outfile), "tandems_by_id.txt") | |
119 with open(tandem_file, 'w') as o: | |
120 highest_tandem_length = 0 | |
121 | |
122 o.write("Sequence.ID\tnumber_of_mutations\tnumber_of_tandems\tregion_length\texpected_tandems\tlongest_tandem\ttandems\n") | |
123 for ID in IDlist: | |
124 mutations = mutationListByID[ID] | |
125 if len(mutations) == 0: | |
126 continue | |
127 last_mut = max(mutations, key=lambda x: int(x[1])) | |
128 | |
129 last_mut_pos = int(last_mut[1]) | |
130 | |
131 mut_positions = [False] * (last_mut_pos + 1) | |
132 | |
133 for mutation in mutations: | |
134 frm, where, to, frmAA, whereAA, toAA, thing = mutation | |
135 where = int(where) | |
136 mut_positions[where] = True | |
137 | |
138 tandem_muts = [] | |
139 tandem_start = -1 | |
140 tandem_length = 0 | |
141 for i in range(len(mut_positions)): | |
142 if mut_positions[i]: | |
143 if tandem_start == -1: | |
144 tandem_start = i | |
145 tandem_length += 1 | |
146 #print "".join(["1" if x else "0" for x in mut_positions[:i+1]]) | |
147 else: | |
148 if tandem_length > 1: | |
149 tandem_muts.append((tandem_start, tandem_length)) | |
150 #print "{0}{1} {2}:{3}".format(" " * (i - tandem_length), "^" * tandem_length, tandem_start, tandem_length) | |
151 tandem_start = -1 | |
152 tandem_length = 0 | |
153 if tandem_length > 1: # if the sequence ends with a tandem mutation | |
154 tandem_muts.append((tandem_start, tandem_length)) | |
155 | |
156 if len(tandem_muts) > 0: | |
157 if highest_tandem_length < len(tandem_muts): | |
158 highest_tandem_length = len(tandem_muts) | |
159 | |
160 region_length = fr1LengthDict[ID] + cdr1LengthDic[ID] + fr2LengthDict[ID] + cdr2LengthDic[ID] + fr3LengthDict[ID] | |
161 longest_tandem = max(tandem_muts, key=lambda x: x[1]) if len(tandem_muts) else (0, 0) | |
162 num_mutations = len(mutations) | |
163 f_num_mutations = float(num_mutations) | |
164 num_tandem_muts = len(tandem_muts) | |
165 expected_tandem_muts = f_num_mutations * (f_num_mutations - 1.0) / float(region_length) | |
166 o.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\n".format(ID, | |
167 str(num_mutations), | |
168 str(num_tandem_muts), | |
169 str(region_length), | |
170 str(round(expected_tandem_muts, 2)), | |
171 str(longest_tandem[1]), | |
172 str(tandem_muts))) | |
173 gene = genedic[ID] | |
174 if gene.find("unmatched") == -1: | |
175 tandem_sum_by_class[gene] += num_tandem_muts | |
176 expected_tandem_sum_by_class[gene] += expected_tandem_muts | |
177 | |
178 tandem_sum_by_class["all"] += num_tandem_muts | |
179 expected_tandem_sum_by_class["all"] += expected_tandem_muts | |
180 | |
181 gene = gene[:3] | |
182 if gene in ["IGA", "IGG"]: | |
183 tandem_sum_by_class[gene] += num_tandem_muts | |
184 expected_tandem_sum_by_class[gene] += expected_tandem_muts | |
185 else: | |
186 tandem_sum_by_class["unmatched"] += num_tandem_muts | |
187 expected_tandem_sum_by_class["unmatched"] += expected_tandem_muts | |
188 | |
189 | |
190 for tandem_mut in tandem_muts: | |
191 tandem_frequency[str(tandem_mut[1])] += 1 | |
192 #print "\t".join([ID, str(len(tandem_muts)), str(longest_tandem[1]) , str(tandem_muts)]) | |
193 | |
194 tandem_freq_file = os.path.join(os.path.dirname(outfile), "tandem_frequency.txt") | |
195 with open(tandem_freq_file, 'w') as o: | |
196 for frq in sorted([int(x) for x in tandem_frequency.keys()]): | |
197 o.write("{0}\t{1}\n".format(frq, tandem_frequency[str(frq)])) | |
198 | |
199 tandem_row = [] | |
200 print genes | |
201 print tandem_sum_by_class | |
202 print expected_tandem_sum_by_class | |
203 genes_extra = list(genes) | |
204 genes_extra.append("all") | |
205 for x, y, in zip([tandem_sum_by_class[x] for x in genes_extra], [expected_tandem_sum_by_class[x] for x in genes_extra]): | |
206 if y != 0: | |
207 tandem_row += [x, round(y, 2), round(x / y, 2)] | |
208 else: | |
209 tandem_row += [x, round(y, 2), 0] | |
210 | |
211 """ | |
212 print tandem_row | |
213 tandem_row += tandem_row[-3:] | |
214 print tandem_row | |
215 all_expected_tandem = expected_tandem_sum_by_class["all"] | |
216 all_tandem = tandem_sum_by_class["all"] | |
217 if all_expected_tandem == 0: | |
218 tandem_row[-6:-3] = [all_tandem, round(all_expected_tandem, 2), 0] | |
219 else: | |
220 tandem_row[-6:-3] = [all_tandem, round(all_expected_tandem, 2), round(all_tandem / all_expected_tandem, 2)] | |
221 print tandem_row | |
222 """ | |
223 for i in range(len(genes_extra)): | |
224 gene = genes_extra[i] | |
225 print gene, tandem_row[i*3:i*3+3] | |
226 | |
227 tandem_freq_file = os.path.join(os.path.dirname(outfile), "shm_overview_tandem_row.txt") | |
228 with open(tandem_freq_file, 'w') as o: | |
229 o.write("Tandems/Expected (ratio),{0}\n".format(",".join([str(x) for x in tandem_row]))) | |
230 | |
231 #print mutationList, linecount | |
232 | |
233 AALength = (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 | |
234 if AALength < 60: | |
235 AALength = 64 | |
236 | |
237 AA_mutation = [0] * AALength | |
238 AA_mutation_dic = {"IGA": AA_mutation[:], "IGG": AA_mutation[:], "IGM": AA_mutation[:], "IGE": AA_mutation[:], "unm": AA_mutation[:], "all": AA_mutation[:]} | |
239 AA_mutation_empty = AA_mutation[:] | |
240 | |
241 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt" | |
242 with open(aa_mutations_by_id_file, 'w') as o: | |
243 o.write("ID\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n") | |
244 for ID in mutationListByID.keys(): | |
245 AA_mutation_for_ID = AA_mutation_empty[:] | |
246 for mutation in mutationListByID[ID]: | |
247 if mutation[4]: | |
248 AA_mutation_position = int(mutation[4]) | |
249 AA_mutation[AA_mutation_position] += 1 | |
250 AA_mutation_for_ID[AA_mutation_position] += 1 | |
251 clss = genedic[ID][:3] | |
252 AA_mutation_dic[clss][AA_mutation_position] += 1 | |
253 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n") | |
254 | |
255 | |
256 | |
257 #absent AA stuff | |
258 absentAACDR1Dic = defaultdict(list) | |
259 absentAACDR1Dic[5] = range(29,36) | |
260 absentAACDR1Dic[6] = range(29,35) | |
261 absentAACDR1Dic[7] = range(30,35) | |
262 absentAACDR1Dic[8] = range(30,34) | |
263 absentAACDR1Dic[9] = range(31,34) | |
264 absentAACDR1Dic[10] = range(31,33) | |
265 absentAACDR1Dic[11] = [32] | |
266 | |
267 absentAACDR2Dic = defaultdict(list) | |
268 absentAACDR2Dic[0] = range(55,65) | |
269 absentAACDR2Dic[1] = range(56,65) | |
270 absentAACDR2Dic[2] = range(56,64) | |
271 absentAACDR2Dic[3] = range(57,64) | |
272 absentAACDR2Dic[4] = range(57,63) | |
273 absentAACDR2Dic[5] = range(58,63) | |
274 absentAACDR2Dic[6] = range(58,62) | |
275 absentAACDR2Dic[7] = range(59,62) | |
276 absentAACDR2Dic[8] = range(59,61) | |
277 absentAACDR2Dic[9] = [60] | |
278 | |
279 absentAA = [len(IDlist)] * (AALength-1) | |
280 for k, cdr1Length in cdr1LengthDic.iteritems(): | |
281 for c in absentAACDR1Dic[cdr1Length]: | |
282 absentAA[c] -= 1 | |
283 | |
284 for k, cdr2Length in cdr2LengthDic.iteritems(): | |
285 for c in absentAACDR2Dic[cdr2Length]: | |
286 absentAA[c] -= 1 | |
287 | |
288 | |
289 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt" | |
290 with open(aa_mutations_by_id_file, 'w') as o: | |
291 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n") | |
292 for ID in IDlist: | |
293 absentAAbyID = [1] * (AALength-1) | |
294 cdr1Length = cdr1LengthDic[ID] | |
295 for c in absentAACDR1Dic[cdr1Length]: | |
296 absentAAbyID[c] -= 1 | |
297 | |
298 cdr2Length = cdr2LengthDic[ID] | |
299 for c in absentAACDR2Dic[cdr2Length]: | |
300 absentAAbyID[c] -= 1 | |
301 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n") | |
302 | |
303 if linecount == 0: | |
304 print "No data, exiting" | |
305 with open(outfile, 'w') as o: | |
306 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | |
307 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | |
308 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | |
309 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | |
310 import sys | |
311 | |
312 sys.exit() | |
313 | |
314 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") | |
315 RGYWCount = {} | |
316 WRCYCount = {} | |
317 WACount = {} | |
318 TWCount = {} | |
319 | |
320 #IDIndex = 0 | |
321 ataIndex = 0 | |
322 tatIndex = 0 | |
323 aggctatIndex = 0 | |
324 atagcctIndex = 0 | |
325 first = True | |
326 with open(infile, 'ru') as i: | |
327 for line in i: | |
328 if first: | |
329 linesplt = line.split("\t") | |
330 ataIndex = linesplt.index("X.a.t.a") | |
331 tatIndex = linesplt.index("t.a.t.") | |
332 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | |
333 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | |
334 first = False | |
335 continue | |
336 linesplt = line.split("\t") | |
337 gene = linesplt[best_matchIndex] | |
338 ID = linesplt[IDIndex] | |
339 RGYW = [(int(x), int(y), z) for (x, y, z) in | |
340 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] | |
341 WRCY = [(int(x), int(y), z) for (x, y, z) in | |
342 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] | |
343 WA = [(int(x), int(y), z) for (x, y, z) in | |
344 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] | |
345 TW = [(int(x), int(y), z) for (x, y, z) in | |
346 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] | |
347 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0 | |
348 | |
349 mutationList = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
350 for mutation in mutationList: | |
351 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | |
352 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW]) | |
353 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY]) | |
354 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA]) | |
355 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW]) | |
356 | |
357 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | |
358 | |
359 if in_how_many_motifs > 0: | |
360 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | |
361 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | |
362 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | |
363 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | |
364 | |
365 | |
366 def mean(lst): | |
367 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0 | |
368 | |
369 | |
370 def median(lst): | |
371 lst = sorted(lst) | |
372 l = len(lst) | |
373 if l == 0: | |
374 return 0 | |
375 if l == 1: | |
376 return lst[0] | |
377 | |
378 l = int(l / 2) | |
80 | 379 |
81 IDlist += [ID] | 380 if len(lst) % 2 == 0: |
82 | 381 return float(lst[l] + lst[(l - 1)]) / 2.0 |
83 #print mutationList, linecount | 382 else: |
84 | 383 return lst[l] |
85 AALength = (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 | 384 |
86 if AALength < 60: | 385 funcs = {"mean": mean, "median": median, "sum": sum} |
87 AALength = 64 | 386 |
88 | 387 directory = outfile[:outfile.rfind("/") + 1] |
89 AA_mutation = [0] * AALength | 388 value = 0 |
90 AA_mutation_dic = {"IGA": AA_mutation[:], "IGG": AA_mutation[:], "IGM": AA_mutation[:], "IGE": AA_mutation[:], "unm": AA_mutation[:], "all": AA_mutation[:]} | 389 valuedic = dict() |
91 AA_mutation_empty = AA_mutation[:] | 390 |
92 | 391 for fname in funcs.keys(): |
93 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt" | 392 for gene in genes: |
94 with open(aa_mutations_by_id_file, 'w') as o: | 393 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v: |
95 o.write("ID\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n") | 394 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip()) |
96 for ID in mutationListByID.keys(): | 395 with open(directory + "all_" + fname + "_value.txt", 'r') as v: |
97 AA_mutation_for_ID = AA_mutation_empty[:] | 396 valuedic["total_" + fname] = float(v.readlines()[0].rstrip()) |
98 for mutation in mutationListByID[ID]: | |
99 if mutation[4]: | |
100 AA_mutation_position = int(mutation[4]) | |
101 AA_mutation[AA_mutation_position] += 1 | |
102 AA_mutation_for_ID[AA_mutation_position] += 1 | |
103 clss = genedic[ID][:3] | |
104 AA_mutation_dic[clss][AA_mutation_position] += 1 | |
105 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n") | |
106 | |
107 | |
108 | |
109 #absent AA stuff | |
110 absentAACDR1Dic = defaultdict(list) | |
111 absentAACDR1Dic[5] = range(29,36) | |
112 absentAACDR1Dic[6] = range(29,35) | |
113 absentAACDR1Dic[7] = range(30,35) | |
114 absentAACDR1Dic[8] = range(30,34) | |
115 absentAACDR1Dic[9] = range(31,34) | |
116 absentAACDR1Dic[10] = range(31,33) | |
117 absentAACDR1Dic[11] = [32] | |
118 | |
119 absentAACDR2Dic = defaultdict(list) | |
120 absentAACDR2Dic[0] = range(55,65) | |
121 absentAACDR2Dic[1] = range(56,65) | |
122 absentAACDR2Dic[2] = range(56,64) | |
123 absentAACDR2Dic[3] = range(57,64) | |
124 absentAACDR2Dic[4] = range(57,63) | |
125 absentAACDR2Dic[5] = range(58,63) | |
126 absentAACDR2Dic[6] = range(58,62) | |
127 absentAACDR2Dic[7] = range(59,62) | |
128 absentAACDR2Dic[8] = range(59,61) | |
129 absentAACDR2Dic[9] = [60] | |
130 | |
131 absentAA = [len(IDlist)] * (AALength-1) | |
132 for k, cdr1Length in cdr1LengthDic.iteritems(): | |
133 for c in absentAACDR1Dic[cdr1Length]: | |
134 absentAA[c] -= 1 | |
135 | |
136 for k, cdr2Length in cdr2LengthDic.iteritems(): | |
137 for c in absentAACDR2Dic[cdr2Length]: | |
138 absentAA[c] -= 1 | |
139 | |
140 | |
141 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt" | |
142 with open(aa_mutations_by_id_file, 'w') as o: | |
143 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n") | |
144 for ID in IDlist: | |
145 absentAAbyID = [1] * (AALength-1) | |
146 cdr1Length = cdr1LengthDic[ID] | |
147 for c in absentAACDR1Dic[cdr1Length]: | |
148 absentAAbyID[c] -= 1 | |
149 | |
150 cdr2Length = cdr2LengthDic[ID] | |
151 for c in absentAACDR2Dic[cdr2Length]: | |
152 absentAAbyID[c] -= 1 | |
153 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n") | |
154 | |
155 if linecount == 0: | |
156 print "No data, exiting" | |
157 with open(outfile, 'w') as o: | |
158 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | |
159 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | |
160 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | |
161 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | |
162 import sys | |
163 | |
164 sys.exit() | |
165 | |
166 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") | |
167 RGYWCount = {} | |
168 WRCYCount = {} | |
169 WACount = {} | |
170 TWCount = {} | |
171 | |
172 #IDIndex = 0 | |
173 ataIndex = 0 | |
174 tatIndex = 0 | |
175 aggctatIndex = 0 | |
176 atagcctIndex = 0 | |
177 first = True | |
178 with open(infile, 'ru') as i: | |
179 for line in i: | |
180 if first: | |
181 linesplt = line.split("\t") | |
182 ataIndex = linesplt.index("X.a.t.a") | |
183 tatIndex = linesplt.index("t.a.t.") | |
184 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | |
185 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | |
186 first = False | |
187 continue | |
188 linesplt = line.split("\t") | |
189 gene = linesplt[best_matchIndex] | |
190 ID = linesplt[IDIndex] | |
191 RGYW = [(int(x), int(y), z) for (x, y, z) in | |
192 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] | |
193 WRCY = [(int(x), int(y), z) for (x, y, z) in | |
194 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] | |
195 WA = [(int(x), int(y), z) for (x, y, z) in | |
196 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] | |
197 TW = [(int(x), int(y), z) for (x, y, z) in | |
198 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] | |
199 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0 | |
200 | |
201 mutationList = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
202 for mutation in mutationList: | |
203 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | |
204 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW]) | |
205 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY]) | |
206 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA]) | |
207 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW]) | |
208 | |
209 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | |
210 | |
211 if in_how_many_motifs > 0: | |
212 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | |
213 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | |
214 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | |
215 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | |
216 | |
217 | |
218 def mean(lst): | |
219 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0 | |
220 | |
221 | |
222 def median(lst): | |
223 lst = sorted(lst) | |
224 l = len(lst) | |
225 if l == 0: | |
226 return 0 | |
227 if l == 1: | |
228 return lst[0] | |
229 | 397 |
230 l = int(l / 2) | 398 |
231 | 399 def get_xyz(lst, gene, f, fname): |
232 if len(lst) % 2 == 0: | 400 x = round(round(f(lst), 1)) |
233 return float(lst[l] + lst[(l - 1)]) / 2.0 | 401 y = valuedic[gene + "_" + fname] |
234 else: | 402 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0" |
235 return lst[l] | 403 return (str(x), str(y), z) |
236 | 404 |
237 funcs = {"mean": mean, "median": median, "sum": sum} | 405 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} |
238 | 406 arr = ["RGYW", "WRCY", "WA", "TW"] |
239 directory = outfile[:outfile.rfind("/") + 1] | 407 |
240 value = 0 | 408 for fname in funcs.keys(): |
241 valuedic = dict() | 409 func = funcs[fname] |
242 | 410 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt" |
243 for fname in funcs.keys(): | 411 with open(foutfile, 'w') as o: |
244 for gene in genes: | 412 for typ in arr: |
245 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v: | 413 o.write(typ + " (%)") |
246 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip()) | 414 curr = dic[typ] |
247 with open(directory + "all_" + fname + "_value.txt", 'r') as v: | 415 for gene in genes: |
248 valuedic["total_" + fname] = float(v.readlines()[0].rstrip()) | 416 geneMatcher = geneMatchers[gene] |
249 | 417 if valuedic[gene + "_" + fname] is 0: |
250 | 418 o.write(",0,0,0") |
251 def get_xyz(lst, gene, f, fname): | 419 else: |
252 x = round(round(f(lst), 1)) | 420 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]], gene, func, fname) |
253 y = valuedic[gene + "_" + fname] | 421 o.write("," + x + "," + y + "," + z) |
254 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0" | 422 x, y, z = get_xyz([y for x, y in curr.iteritems() if not genedic[x].startswith("unmatched")], "total", func, fname) |
255 return (str(x), str(y), z) | 423 #x, y, z = get_xyz([y for x, y in curr.iteritems()], "total", func, fname) |
256 | 424 o.write("," + x + "," + y + "," + z + "\n") |
257 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} | 425 |
258 arr = ["RGYW", "WRCY", "WA", "TW"] | 426 |
259 | 427 # for testing |
260 geneMatchers = {gene: re.compile("^" + gene + ".*") for gene in genes} | 428 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" |
261 | 429 with open(seq_motif_file, 'w') as o: |
262 for fname in funcs.keys(): | 430 o.write("ID\tRGYW\tWRCY\tWA\tTW\n") |
263 func = funcs[fname] | 431 for ID in IDlist: |
264 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt" | 432 #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") |
265 with open(foutfile, 'w') as o: | 433 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n") |
266 for typ in arr: | 434 |
267 o.write(typ + " (%)") | 435 if __name__ == "__main__": |
268 curr = dic[typ] | 436 main() |
269 for gene in genes: | |
270 geneMatcher = geneMatchers[gene] | |
271 if valuedic[gene + "_" + fname] is 0: | |
272 o.write(",0,0,0") | |
273 else: | |
274 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]], gene, func, fname) | |
275 o.write("," + x + "," + y + "," + z) | |
276 x, y, z = get_xyz([y for x, y in curr.iteritems() if not genedic[x].startswith("unmatched")], "total", func, fname) | |
277 #x, y, z = get_xyz([y for x, y in curr.iteritems()], "total", func, fname) | |
278 o.write("," + x + "," + y + "," + z + "\n") | |
279 | |
280 | |
281 # for testing | |
282 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" | |
283 with open(seq_motif_file, 'w') as o: | |
284 o.write("ID\tRGYW\tWRCY\tWA\tTW\n") | |
285 for ID in IDlist: | |
286 #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") | |
287 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n") |