Skip to content

Commit

Permalink
Merge pull request #515 from GavinHuttley/develop
Browse files Browse the repository at this point in the history
improve robustness of ArrayAlignment methods
  • Loading branch information
GavinHuttley committed Jan 31, 2020
2 parents 70ba904 + 81d26ab commit 8e533f1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/cogent3/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3039,7 +3039,9 @@ def counts_per_seq(
motifs = set()
for name in self.names:
if is_array:
seq = self.named_seqs[name]
seq = self.moltype.make_array_seq(
self.array_seqs[self.names.index(name)]
)
else:
seq = self.get_gapped_seq(name)
c = seq.counts(
Expand Down
25 changes: 24 additions & 1 deletion src/cogent3/core/moltype.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def __init__(
self.array_type = None

# set modeling sequence
self.make_array_seq = array_seq_constructor
self._make_array_seq = array_seq_constructor

self._colors = colors or defaultdict(_DefaultValue("black"))

Expand Down Expand Up @@ -762,6 +762,29 @@ def make_seq(self, seq, name=None, **kwargs):
"""Returns sequence of correct type."""
return self._make_seq(seq, name, **kwargs)

def make_array_seq(self, seq, name=None, **kwargs):
"""
creates an array sequence
Parameters
----------
seq
characters or array
name : str
kwargs
keyword arguments for the ArraySequence constructor.
Returns
-------
ArraySequence
"""
alphabet = kwargs.pop("alphabet", None)
if alphabet is None and hasattr(self, "alphabets"):
alphabet = self.alphabets.degen_gapped
elif alphabet is None:
alphabet = self.alphabet
return self._make_array_seq(seq, alphabet=alphabet, name=name, **kwargs)

def verify_sequence(self, seq, gaps_allowed=True, wildcards_allowed=True):
"""Checks whether sequence is valid on the default alphabet.
Expand Down
21 changes: 20 additions & 1 deletion tests/test_core/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
from numpy.testing import assert_allclose

from cogent3.core.annotation import Feature, SimpleVariable, Variable
from cogent3.core.moltype import ASCII, BYTES, DNA, PROTEIN, RNA, AlphabetError
from cogent3.core.moltype import (
ASCII,
BYTES,
DNA,
PROTEIN,
RNA,
AlphabetError,
get_moltype,
)
from cogent3.core.sequence import (
ABSequence,
ArrayCodonSequence,
ArrayDnaCodonSequence,
ArrayDnaSequence,
Expand Down Expand Up @@ -1110,6 +1119,7 @@ class ModelSequenceTests(SequenceTests):
RNA = ArrayRnaSequence
DNA = ArrayDnaSequence
PROT = ArrayProteinSequence
AB = ABSequence

def test_distance_indices(self):
"""ArraySequence distance should work with function of indices"""
Expand Down Expand Up @@ -1163,6 +1173,15 @@ def test_gap_indices(self):
v = r.gap_indices()
self.assertEqual(v, array([0, 1]))

def test_count_ab(self):
"""abseq array seq should count characters"""
AB = get_moltype("ab")
seq = AB.make_array_seq("aaba-", alphabet=AB.alphabet.with_gap_motif())
c = seq.counts()
self.assertEqual(c.to_dict(), {"a": 3, "b": 1})
c = seq.counts(allow_gap=True)
self.assertEqual(c.to_dict(), {"a": 3, "b": 1, "-": 1})


# run if called from command-line
if __name__ == "__main__":
Expand Down

0 comments on commit 8e533f1

Please sign in to comment.