Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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