Skip to content

Commit

Permalink
Merge pull request #446 from jamesmartini/develop_regex
Browse files Browse the repository at this point in the history
Develop to_regex method and associated test
  • Loading branch information
GavinHuttley committed Dec 17, 2019
2 parents 0f617ce + c0ec287 commit 69a5e10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cogent3/core/moltype.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,15 @@ def to_json(self):
data = self.to_rich_dict(for_pickle=False)
return json.dumps(data)

def to_regex(self, seq):
"""returns a regex pattern with ambiguities expanded to a character set"""
degen_indices = self.get_degenerate_positions(sequence=seq, include_gap=False)
seq = list(seq) # seq can now be modified
for index in degen_indices:
expanded = self.ambiguities[seq[index]]
seq[index] = f"[{''.join(expanded)}]"
return "".join(seq)

def gettype(self):
"""Returns type, e.g. 'dna', 'rna', 'protein'. Delete?"""
return self.label
Expand Down
5 changes: 5 additions & 0 deletions tests/test_core/test_moltype.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def test_init(self):
class MolTypeTests(TestCase):
"""Tests of the MolType class. Should support same API as old Alphabet."""

def test_to_regex(self):
seq = "ACYGR"
regular_expression = DNA.to_regex(seq=seq)
self.assertEqual(regular_expression, "AC[CT]G[AG]")

def test_pickling(self):
pkl = pickle.dumps(DNA)
got = pickle.loads(pkl)
Expand Down

0 comments on commit 69a5e10

Please sign in to comment.