Skip to content

Commit 897902f

Browse files
committed
-Fixes #29
1 parent 0b45718 commit 897902f

File tree

1 file changed

+66
-19
lines changed

1 file changed

+66
-19
lines changed

poplars/hypermut.py

+66-19
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ def __init__(self, seq_name=None, num_muts=None, potential_muts=None, ctrl_muts=
5050
self.mut_sites = mut_sites
5151
self.ctrl_sites = ctrl_sites
5252

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+
5363

5464
def parse_args():
5565
parser = argparse.ArgumentParser(
@@ -142,15 +152,14 @@ def hypermut(infile, cons, skip=None):
142152
fasta = convert_fasta(handle)
143153

144154
if cons:
145-
print("Generating consensus sequence.")
146-
refseq = consensus(fasta)
155+
refseq = get_consensus(fasta)
147156
query_seqs = fasta
148157
else:
149158
refseq = fasta[0][1] # First sequence is the reference sequence
150159
query_seqs = fasta[1:]
151160

152161
if skip:
153-
print("Skipping first {} records".format(skip))
162+
print("Skipping first {} records...\n".format(skip))
154163
fasta = fasta[int(skip):]
155164
query_seqs = fasta
156165

@@ -170,6 +179,12 @@ def hypermut(infile, cons, skip=None):
170179
return results
171180

172181

182+
def get_consensus(fasta):
183+
print("Generating consensus sequence...\n")
184+
refseq = consensus(fasta)
185+
return refseq
186+
187+
173188
def rate_ratio(ctable):
174189
"""
175190
Calculate rate ratio from contingency table
@@ -195,42 +210,74 @@ def pretty_print(results):
195210
Print results
196211
:param results: a list of MutationInfo objects
197212
"""
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
199215
("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)
201218

202219
# Print values of rows under corresponding headings
203220
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)))
208226

209227
# Print summary of hypermutated sequences
210-
print("\n\033[1m\033[4mSummary:\033[0m")
228+
print("\nSummary:")
211229
for result in results:
212230
if result.p_value <= 0.05:
213231
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
214235

215236

216-
def make_data_file(file_name, mutation_info_list):
237+
def make_data_file(file_name, results):
217238
"""
218239
Writes detailed output of hypermut to a text file
219240
:param file_name: name of the output file
220-
:param mutation_info_list: list of MutationInfo objects
241+
:param results: list of MutationInfo objects
221242
"""
222243

223244
with open(file_name, "w+") as output:
224-
for mutation_info in mutation_info_list:
225-
output.write("\nSequence 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("\nSummary:\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("\nLocations of matches:\n")
271+
for result in results:
272+
output.write("\nSequence Name: {}".format(result.seq_name[:8]))
226273
output.write("\nPos\tMut\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]))
229276

230-
output.write("\nSequence Name: {} control".format(mutation_info.seq_name))
277+
output.write("\nSequence Name: {} control".format(result.seq_name))
231278
output.write("\nPos\tMut\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]))
234281

235282

236283
def main():

0 commit comments

Comments
 (0)