Skip to content

Commit

Permalink
Fixes a bug when revcomp was leaving ambiguous bases unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Tovchigrechko committed Mar 9, 2018
1 parent 892f4b9 commit 3b082ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pyfastaq/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ class Fasta:
'''Class to store and manipulate FASTA sequences. They have two things: a name and a sequence'''
# this defines the line length when printing sequences
line_length = 60

# cached translation object for reverse-complementation (with ambiguous codes)
_revcomp_trans = str.maketrans("ATCGatcgMRWSYKVHDBNmrwsykvhdbn",
"TAGCtagcKYWSRMBDHVNkywsrmbdhvn")
def _get_id_from_header_line(self, line):
if line.startswith('>'):
return line.rstrip()[1:]
Expand Down Expand Up @@ -214,7 +216,7 @@ def strip_illumina_suffix(self):

def revcomp(self):
'''Reverse complements the sequence'''
self.seq = self.seq.translate(str.maketrans("ATCGatcg", "TAGCtagc"))[::-1]
self.seq = self.seq.translate(self._revcomp_trans)[::-1]

def is_all_Ns(self, start=0, end=None):
'''Returns true if the sequence is all Ns (upper or lower case)'''
Expand Down
6 changes: 6 additions & 0 deletions pyfastaq/tests/sequences_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ def test_revcomp(self):
fa.revcomp()
self.assertEqual(fa, sequences.Fasta('ID', 'nacgtNACGT'))

def test_revcomp_ambig(self):
'''revcomp() should correctly reverse complement a sequence with ambiguous bases'''
fa = sequences.Fasta('ID', 'ACTGMRWSYKVHDBNmrwsykvhdbn')
fa.revcomp()
self.assertEqual(fa, sequences.Fasta('ID', 'nvhdbmrswykNVHDBMRSWYKCAGT'))

def test_gaps(self):
'''gaps() should find the gaps in a sequence correctly'''
test_seqs = [sequences.Fasta('ID', 'ACGT'),
Expand Down

0 comments on commit 3b082ca

Please sign in to comment.