annotate shm_csr.py @ 63:8728284105ee draft

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