Skip to content

Commit

Permalink
WrongLetterError
Browse files Browse the repository at this point in the history
Raise if sequence and alphabet letters do not match
  • Loading branch information
veghp committed Oct 22, 2020
1 parent 0031b49 commit a003439
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions polymera/polymera.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import math


class WrongLetterError(ValueError):
pass


class Polymer:
"""Class for representing a sequence with its alphabet.
Expand All @@ -15,6 +19,13 @@ class Polymer:
"""

def __init__(self, sequence, alphabet):
letters_only = set(sequence.to_string()) - set(sequence.separators.values())
wrong_letters = letters_only - alphabet.letters
if wrong_letters != set():
raise WrongLetterError(
"These letters are not in the alphabet: %s" % wrong_letters
)

self.sequence = sequence
self.alphabet = alphabet

Expand Down
5 changes: 5 additions & 0 deletions tests/test_polymera.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def test_alphabet():


def test_polymer():
sequence = polymera.Sequence()
sequence.add_sequence_from_string("ATGCX")
with pytest.raises(ValueError):
polymera.Polymer(sequence, alphabet=polymera.bio.DNAAlphabet)

sequence = polymera.Sequence()
sequence.add_sequence_from_string("ATGAA,ATGCC|TATATTAGAAAAAA")
sequence.add_sequence_from_string("ATGAA,ATGCC")
Expand Down

0 comments on commit a003439

Please sign in to comment.