@@ -50,6 +50,16 @@ def __init__(self, seq_name=None, num_muts=None, potential_muts=None, ctrl_muts=
50
50
self .mut_sites = mut_sites
51
51
self .ctrl_sites = ctrl_sites
52
52
53
+ def is_hypermutated (self ):
54
+ """
55
+ Returns if a sequence is hypermutated
56
+ :return: True if the sequence is hypermutated, False otherwise
57
+ """
58
+ self .hypermutated = False
59
+ if self .p_value is not None and self .p_value <= 0.05 :
60
+ self .hypermutated = True
61
+ return self .hypermutated
62
+
53
63
54
64
def parse_args ():
55
65
parser = argparse .ArgumentParser (
@@ -142,15 +152,14 @@ def hypermut(infile, cons, skip=None):
142
152
fasta = convert_fasta (handle )
143
153
144
154
if cons :
145
- print ("Generating consensus sequence." )
146
- refseq = consensus (fasta )
155
+ refseq = get_consensus (fasta )
147
156
query_seqs = fasta
148
157
else :
149
158
refseq = fasta [0 ][1 ] # First sequence is the reference sequence
150
159
query_seqs = fasta [1 :]
151
160
152
161
if skip :
153
- print ("Skipping first {} records" .format (skip ))
162
+ print ("Skipping first {} records... \n " .format (skip ))
154
163
fasta = fasta [int (skip ):]
155
164
query_seqs = fasta
156
165
@@ -170,6 +179,12 @@ def hypermut(infile, cons, skip=None):
170
179
return results
171
180
172
181
182
+ def get_consensus (fasta ):
183
+ print ("Generating consensus sequence...\n " )
184
+ refseq = consensus (fasta )
185
+ return refseq
186
+
187
+
173
188
def rate_ratio (ctable ):
174
189
"""
175
190
Calculate rate ratio from contingency table
@@ -195,42 +210,74 @@ def pretty_print(results):
195
210
Print results
196
211
:param results: a list of MutationInfo objects
197
212
"""
198
- print ("\033 [1m{0:<11} {1:<7} {2:<22} {3:<15} {4:<21} {5:<22} {6:<25} {7:<20}" .format
213
+
214
+ print ("{0}\t \t {1}\t \t {2}\t \t {3}\t \t {4}\t \t {5}\t \t {6}\t \t {7}\t \t {8}" .format
199
215
("Sequence" , "Muts" , "Potential Mut Sites" , "Control Muts" , "Potential Controls" ,
200
- "Rate Ratio" , "Fisher's Exact P-value" , "Odds Ratio\033 [0m" ))
216
+ "Rate Ratio" , "Fisher's Exact P-value" , "Odds Ratio" , "Hypermutated" ))
217
+ print ('-' * 169 )
201
218
202
219
# Print values of rows under corresponding headings
203
220
for result in results :
204
- print ("{0:<11} {1:<7} {2:<22} {3:<15} {4:<21} {5:<22} {6:<25} {7:<20}"
205
- .format (result .seq_name , result .num_muts , result .pot_muts ,
206
- result .ctrl_muts , result .potential_ctrls ,
207
- result .rate_ratio , result .p_value , result .odds_ratio ))
221
+ is_hypermutated = result .is_hypermutated ()
222
+ print ("{0}\t \t {1}\t \t \t {2}\t \t \t \t \t {3}\t \t \t \t \t {4}\t \t \t \t \t {5}\t \t \t {6}\t \t \t \t \t {7}\t \t {8}"
223
+ .format (result .seq_name [:8 ], result .num_muts , result .pot_muts , result .ctrl_muts ,
224
+ result .potential_ctrls , round (result .rate_ratio , 2 ), round (result .p_value , 6 ),
225
+ round (result .odds_ratio , 6 ), str (is_hypermutated )))
208
226
209
227
# Print summary of hypermutated sequences
210
- print ("\n \033 [1m \033 [4mSummary: \033 [0m " )
228
+ print ("\n Summary: " )
211
229
for result in results :
212
230
if result .p_value <= 0.05 :
213
231
print ("{} appears to be hypermutated (OR={})" .format (result .seq_name , result .odds_ratio ))
232
+ else :
233
+ print ("No sequences appear to be hypermutated." )
234
+ break
214
235
215
236
216
- def make_data_file (file_name , mutation_info_list ):
237
+ def make_data_file (file_name , results ):
217
238
"""
218
239
Writes detailed output of hypermut to a text file
219
240
:param file_name: name of the output file
220
- :param mutation_info_list : list of MutationInfo objects
241
+ :param results : list of MutationInfo objects
221
242
"""
222
243
223
244
with open (file_name , "w+" ) as output :
224
- for mutation_info in mutation_info_list :
225
- output .write ("\n Sequence Name: {}" .format (mutation_info .seq_name ))
245
+
246
+ output .write ("Results:\n " )
247
+ output .write ("{0},{1},{2},{3},{4},{5},{6},{7},{8}\n " .format
248
+ ("Sequence" , "Muts" , "Potential Mut Sites" , "Control Muts" , "Potential Controls" ,
249
+ "Rate Ratio" , "Fisher's Exact P-value" , "Odds Ratio" , "Hypermutated" ))
250
+
251
+ # Print values of rows under corresponding headings
252
+ for result in results :
253
+ is_hypermutated = result .is_hypermutated ()
254
+ output .write ("{0},{1},{2},{3},{4},{5},{6},{7},{8}\n "
255
+ .format (result .seq_name [:8 ], result .num_muts , result .pot_muts , result .ctrl_muts ,
256
+ result .potential_ctrls , round (result .rate_ratio , 2 ),
257
+ round (result .p_value , 6 ), round (result .odds_ratio , 6 ),
258
+ str (is_hypermutated )))
259
+
260
+ # Print summary of hypermutated sequences
261
+ output .write ("\n Summary:\n " )
262
+ for result in results :
263
+ if result .p_value <= 0.05 :
264
+ output .write ("{} appears to be hypermutated (OR={})\n " .format (result .seq_name , result .odds_ratio ))
265
+ else :
266
+ output .write ("No sequences appear to be hypermutated.\n " )
267
+ break
268
+
269
+ # Print detailed output
270
+ output .write ("\n Locations of matches:\n " )
271
+ for result in results :
272
+ output .write ("\n Sequence Name: {}" .format (result .seq_name [:8 ]))
226
273
output .write ("\n Pos\t Mut\n " )
227
- for key in mutation_info .mut_sites :
228
- output .write ("{}\t {}\n " .format (key , mutation_info .mut_sites [key ]))
274
+ for key in result .mut_sites :
275
+ output .write ("{}\t {}\n " .format (key , result .mut_sites [key ]))
229
276
230
- output .write ("\n Sequence Name: {} control" .format (mutation_info .seq_name ))
277
+ output .write ("\n Sequence Name: {} control" .format (result .seq_name ))
231
278
output .write ("\n Pos\t Mut\n " )
232
- for key in mutation_info .ctrl_sites :
233
- output .write ("{}\t {}\n " .format (key , mutation_info .ctrl_sites [key ]))
279
+ for key in result .ctrl_sites :
280
+ output .write ("{}\t {}\n " .format (key , result .ctrl_sites [key ]))
234
281
235
282
236
283
def main ():
0 commit comments