Skip to content

Commit

Permalink
Implement more efficient implementations in subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
padix-key committed Sep 8, 2024
1 parent 9c48739 commit a1f8079
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/biotite/sequence/align/kmeralphabet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,23 @@ class KmerAlphabet(Alphabet):
return int(len(self._base_alph) ** self._k)


def __iter__(self):
# Creating all symbols is expensive
# -> Use a generator instead
if isinstance(self._base_alph, LetterAlphabet):
return ("".join(self.decode(code)) for code in range(len(self)))
else:
return (list(self.decode(code)) for code in range(len(self)))


def __contains__(self, symbol):
try:
self.fuse(self._base_alph.encode_multiple(symbol))
return True
except AlphabetError:
return False


def _to_array_form(model_string):
"""
Convert the the common string representation of a *k-mer* spacing
Expand Down
3 changes: 3 additions & 0 deletions src/biotite/sequence/alphabet.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ def decode_multiple(self, code, as_bytes=False):
symbols = symbols.astype("U1")
return symbols

def is_letter_alphabet(self):
return True

def __contains__(self, symbol):
if not isinstance(symbol, (str, bytes)):
return False
Expand Down

0 comments on commit a1f8079

Please sign in to comment.