Repository: cogent3/cogent3
Pull request #1883 (2024-06-03 21:35:59)

API: MonomerAlphabetABC.get_kmer_alphabet instead of get_word_alphabet


Patch:
diff --git a/src/cogent3/core/new_alphabet.py b/src/cogent3/core/new_alphabet.py
index 041a15846..839ad6ea0 100644
--- a/src/cogent3/core/new_alphabet.py
+++ b/src/cogent3/core/new_alphabet.py
@@ -105,7 +105,7 @@ class AlphabetABC(ABC):
 
 class MonomerAlphabetABC(ABC):
     @abstractmethod
-    def get_word_alphabet(self, size: int): ...
+    def get_kmer_alphabet(self, size: int): ...
 
     @abstractmethod
     def to_indices(self, seq: str) -> numpy.ndarray: ...
@@ -244,7 +244,7 @@ class CharAlphabet(tuple, AlphabetABC, MonomerAlphabetABC):
 
     def with_gap_motif(self): ...
 
-    def get_word_alphabet(self, k: int, include_gap: bool = True) -> "KmerAlphabet":
+    def get_kmer_alphabet(self, k: int, include_gap: bool = True) -> "KmerAlphabet":
         """returns kmer alphabet with words of size k
 
         Parameters


Test patch:
diff --git a/tests/test_core/test_new_alphabet.py b/tests/test_core/test_new_alphabet.py
index c39165306..5d8c5071e 100644
--- a/tests/test_core/test_new_alphabet.py
+++ b/tests/test_core/test_new_alphabet.py
@@ -156,7 +156,7 @@ def test_coord2index_fail():
 def test_kmer_alphabet_construction(k):
     dna = new_moltype.get_moltype("dna")
     monomers = dna.alphabet
-    kmers = monomers.get_word_alphabet(k)
+    kmers = monomers.get_kmer_alphabet(k)
     assert len(kmers) == 4**k
 
 
@@ -164,7 +164,7 @@ def test_kmer_alphabet_construction(k):
 def test_kmer_gapped_alphabet_construction(k):
     dna = new_moltype.get_moltype("dna")
     monomers = dna.gapped_alphabet
-    kmers = monomers.get_word_alphabet(k, include_gap=True)
+    kmers = monomers.get_kmer_alphabet(k, include_gap=True)
     assert len(kmers) == 1 + 4**k
 
 
@@ -186,7 +186,7 @@ def test_kmer_alphabet_pack(k, cast):
     # should match numpy ravel
     dna = new_moltype.get_moltype("dna")
     monomers = dna.gapped_alphabet
-    kmers = monomers.get_word_alphabet(k, include_gap=True)
+    kmers = monomers.get_kmer_alphabet(k, include_gap=True)
     coord, seq = get_rand_coord_seq(k, cast)
     expect = numpy.ravel_multi_index(coord, dims=(4,) * k)
     assert kmers.pack(seq) == expect
@@ -195,7 +195,7 @@ def test_kmer_alphabet_pack(k, cast):
 def test_kmer_alphabet_pack_invalid():
     # should match numpy ravel
     dna = new_moltype.get_moltype("dna")
-    kmers = dna.gapped_alphabet.get_word_alphabet(3, include_gap=True)
+    kmers = dna.gapped_alphabet.get_kmer_alphabet(3, include_gap=True)
     with pytest.raises(TypeError):
         kmers.pack([0, 1, 2])
 
@@ -205,7 +205,7 @@ def test_kmer_alphabet_unpack(k):
     # should match numpy ravel
     dna = new_moltype.get_moltype("dna")
     monomers = dna.gapped_alphabet
-    kmers = monomers.get_word_alphabet(k, include_gap=True)
+    kmers = monomers.get_kmer_alphabet(k, include_gap=True)
     coord = numpy.random.randint(0, 4, size=k, dtype=numpy.uint8)
     index = numpy.ravel_multi_index(coord, dims=(4,) * k)
     assert (kmers.unpack(index) == coord).all()
@@ -233,7 +233,7 @@ def test_kmer_alphabet_to_indices_independent():
     dna = new_moltype.get_moltype("dna")
     seq = "ATGGGCAGA"
     arr = dna.alphabet.to_indices(seq)
-    trinuc_alpha = dna.alphabet.get_word_alphabet(k=3, include_gap=False)
+    trinuc_alpha = dna.alphabet.get_kmer_alphabet(k=3, include_gap=False)
     got = trinuc_alpha.to_indices(arr, independent_kmer=True)
     expect = numpy.array(
         [numpy_func(arr[i : i + 3], dims=dims) for i in range(0, 9, 3)]
@@ -247,7 +247,7 @@ def test_kmer_alphabet_to_indices_non_independent():
     dna = new_moltype.get_moltype("dna")
     seq = "ATGGGCAGA"
     arr = dna.alphabet.to_indices(seq)
-    trinuc_alpha = dna.alphabet.get_word_alphabet(k=3, include_gap=False)
+    trinuc_alpha = dna.alphabet.get_kmer_alphabet(k=3, include_gap=False)
     got = trinuc_alpha.to_indices(arr, independent_kmer=False)
     assert len(got) == len(seq) - 3 + 1
     expect = numpy.array(
@@ -258,7 +258,7 @@ def test_kmer_alphabet_to_indices_non_independent():
 
 def test_kmer_alphabet_to_indices_invalid():
     dna = new_moltype.get_moltype("dna")
-    trinuc_alpha = dna.alphabet.get_word_alphabet(k=3, include_gap=False)
+    trinuc_alpha = dna.alphabet.get_kmer_alphabet(k=3, include_gap=False)
     with pytest.raises(TypeError):
         trinuc_alpha.to_indices(["ACG", "TGG"])
 
@@ -267,7 +267,7 @@ def test_kmer_alphabet_from_indices_independent():
     dna = new_moltype.get_moltype("dna")
     seq = "ATGGGCAGA"
     arr = dna.alphabet.to_indices(seq)
-    trinuc_alpha = dna.alphabet.get_word_alphabet(k=3, include_gap=False)
+    trinuc_alpha = dna.alphabet.get_kmer_alphabet(k=3, include_gap=False)
     kmer_seq = trinuc_alpha.to_indices(arr, independent_kmer=True)
     got = trinuc_alpha.from_indices(kmer_seq)
     assert_allclose(got, arr)
@@ -277,7 +277,7 @@ def test_kmer_alphabet_from_indices_not_independent():
     dna = new_moltype.get_moltype("dna")
     seq = "ATGGGCAGA"
     arr = dna.alphabet.to_indices(seq)
-    trinuc_alpha = dna.alphabet.get_word_alphabet(k=3, include_gap=False)
+    trinuc_alpha = dna.alphabet.get_kmer_alphabet(k=3, include_gap=False)
     kmer_seq = trinuc_alpha.to_indices(arr, independent_kmer=False)
     got = trinuc_alpha.from_indices(kmer_seq, independent_kmer=False)
     assert_allclose(got, arr)