Repository: cogent3/cogent3
Pull request #2377 (2025-06-25 00:46:42)

Fixed last tests failing on new type
## Summary by Sourcery

Refactor data_store source resolution to chain through nested data types and update tests to align with the new API while removing obsolete mask tests

Enhancements:
- Chain source resolution through get_data_source for new_alignment.Alignment, SequenceCollection, TreeNode, and PhyloNode to allow nested source extraction

Tests:
- Remove legacy annotated region mask tests now ported to a new module
- Update generic_result tests to supply source via the new parameter and new_type flag
- Adjust get_data_source tests to pass source separately instead of via info and remove conditional logic on _NEW_TYPE


Patch:
diff --git a/src/cogent3/app/data_store.py b/src/cogent3/app/data_store.py
index f06cc4511..abf3f5581 100644
--- a/src/cogent3/app/data_store.py
+++ b/src/cogent3/app/data_store.py
@@ -765,22 +765,22 @@ def _(data: old_alignment.Alignment) -> str | None:
 
 @get_data_source.register
 def _(data: new_alignment.Alignment) -> str | None:
-    return data.source
+    return get_data_source(data.source)
 
 
 @get_data_source.register
 def _(data: new_alignment.SequenceCollection) -> str | None:
-    return data.source
+    return get_data_source(data.source)
 
 
 @get_data_source.register
 def _(data: c3tree.TreeNode) -> str | None:
-    return data.source
+    return get_data_source(data.source)
 
 
 @get_data_source.register
 def _(data: c3tree.PhyloNode) -> str | None:
-    return data.source
+    return get_data_source(data.source)
 
 
 @get_data_source.register


Test patch:
diff --git a/tests/test_app/test_data_store.py b/tests/test_app/test_data_store.py
index 8cf692abe..71555e6d4 100644
--- a/tests/test_app/test_data_store.py
+++ b/tests/test_app/test_data_store.py
@@ -546,7 +546,9 @@ def test_get_data_source_seqcoll(klass):
     obj = make_unaligned_seqs(
         data={"seq1": "ACGG"},
         moltype="dna",
-        info={"source": value, "random_key": 1234},
+        info={"random_key": 1234},
+        new_type=True,
+        source=value,
     )
     got = get_data_source(obj)
     assert got == "path.txt"
diff --git a/tests/test_app/test_result.py b/tests/test_app/test_result.py
index 7e07bb390..5bc489fe6 100644
--- a/tests/test_app/test_result.py
+++ b/tests/test_app/test_result.py
@@ -75,8 +75,10 @@ def test_infers_source():
     source = pathlib.Path("path/blah.fasta")
     aln = cogent3.make_aligned_seqs(
         {"A": "ACGT"},
-        info={"source": str(source), "random_key": 1234},
+        info={"random_key": 1234},
         moltype="dna",
+        source=str(source),
+        new_type=True,
     )
     gr = generic_result(aln)
     assert gr.source == source.name
@@ -91,10 +93,7 @@ def test_infers_source():
     gr = generic_result(aln)
     assert str(gr.source) == source.name
 
-    if _NEW_TYPE:
-        aln.source = None
-    else:
-        aln.info = {}
+    aln.source = None
     with pytest.raises(ValueError):
         generic_result(aln)
 
diff --git a/tests/test_core/test_features.py b/tests/test_core/test_features.py
index 7527ea291..5c7719a86 100755
--- a/tests/test_core/test_features.py
+++ b/tests/test_core/test_features.py
@@ -315,149 +315,6 @@ def test_terminal_gaps():
     assert aln_exons[0].get_slice().to_dict() == {"x": "AAAAA", "y": "--T--"}
 
 
-def test_annotated_region_masks():  # ported to test_new_aln_annotation.py
-    """masking a sequence with specific features"""
-
-    # Annotated regions can be masked (observed sequence characters
-    # replaced by another), either through the sequence on which they
-    # reside or by projection from the alignment. Note that mask_char must
-    # be a valid character for the sequence MolType. Either the features
-    # (multiple can be named), or their shadow, can be masked.
-
-    # We create an alignment with a sequence that has two different annotation types.
-    orig_data = {"x": "C-CCCAAAAAGGGAA", "y": "-T----TTTTG-GTT"}
-    db = GffAnnotationDb()
-    db.add_feature(seqid="x", biotype="exon", name="norwegian", spans=[(0, 4)])
-    db.add_feature(
-        biotype="repeat",
-        name="blue",
-        spans=[(9, 12)],
-        seqid="x",
-    )
-    db.add_feature(seqid="y", biotype="repeat", name="frog", spans=[(5, 7)])
-    aln = cogent3.make_aligned_seqs(data=orig_data, array_align=False, moltype="dna")
-    aln.annotation_db = db
-
-    assert aln.to_dict() == {"x": "C-CCCAAAAAGGGAA", "y": "-T----TTTTG-GTT"}
-    x = aln.get_seq("x")
-    y = aln.get_seq("y")
-    exon = next(iter(x.get_features(biotype="exon")))
-    assert str(exon.get_slice()) == "CCCC"
-    repeat_x = next(iter(x.get_features(biotype="repeat")))
-    assert str(repeat_x.get_slice()) == "GGG"
-    repeat_y = next(iter(y.get_features(biotype="repeat")))
-    assert str(repeat_y.get_slice()) == "GG"
-
-    # Each sequence should correctly mask either the single feature,
-    # it's shadow, or the multiple features, or shadow.
-
-    assert (
-        str(aln.get_seq("x").with_masked_annotations("exon", mask_char="?"))
-        == "????AAAAAGGGAA"
-    )
-    assert (
-        str(
-            aln.get_seq("x").with_masked_annotations(
-                "exon",
-                mask_char="?",
-                shadow=True,
-            ),
-        )
-        == "CCCC??????????"
-    )
-    assert (
-        str(aln.get_seq("x").with_masked_annotations(["exon", "repeat"], mask_char="?"))
-        == "????AAAAA???AA"
-    )
-    assert (
-        str(
-            aln.get_seq("x").with_masked_annotations(
-                ["exon", "repeat"],
-                mask_char="?",
-                shadow=True,
-            ),
-        )
-        == "CCCC?????GGG??"
-    )
-    assert (
-        str(aln.get_seq("y").with_masked_annotations("exon", mask_char="?"))
-        == "TTTTTGGTT"
-    )
-    assert (
-        str(aln.get_seq("y").with_masked_annotations("repeat", mask_char="?"))
-        == "TTTTT??TT"
-    )
-    assert (
-        str(
-            aln.get_seq("y").with_masked_annotations(
-                "repeat",
-                mask_char="?",
-                shadow=True,
-            ),
-        )
-        == "?????GG??"
-    )
-
-    # The same methods can be applied to annotated Alignment's.
-
-    assert aln.with_masked_annotations("exon", mask_char="?").to_dict() == {
-        "x": "?-???AAAAAGGGAA",
-        "y": "-T----TTTTG-GTT",
-    }
-    assert aln.with_masked_annotations(
-        "exon",
-        mask_char="?",
-        shadow=True,
-    ).to_dict() == {"x": "C-CCC??????????", "y": "-?----?????-???"}
-    assert aln.with_masked_annotations("repeat", mask_char="?").to_dict() == {
-        "x": "C-CCCAAAAA???AA",
-        "y": "-T----TTTT?-?TT",
-    }
-    assert aln.with_masked_annotations(
-        "repeat",
-        mask_char="?",
-        shadow=True,
-    ).to_dict() == {"x": "?-????????GGG??", "y": "-?----????G-G??"}
-    assert aln.with_masked_annotations(["repeat", "exon"], mask_char="?").to_dict() == {
-        "x": "?-???AAAAA???AA",
-        "y": "-T----TTTT?-?TT",
-    }
-    assert aln.with_masked_annotations(["repeat", "exon"], shadow=True).to_dict() == {
-        "x": "C-CCC?????GGG??",
-        "y": "-?----????G-G??",
-    }
-
-
-def test_nested_annotated_region_masks():  # ported to test_new_aln_annotation.py
-    """masking a sequence with specific features when nested annotations"""
-    db = GffAnnotationDb()
-    db.add_feature(seqid="x", biotype="gene", name="norwegian", spans=[(0, 4)])
-    db.add_feature(seqid="x", biotype="repeat", name="blue", spans=[(1, 3)])
-    db.add_feature(seqid="y", biotype="repeat", name="frog", spans=[(1, 4)])
-    aln = cogent3.make_aligned_seqs(
-        data=[["x", "C-GGCAAAAATTTAA"], ["y", "-T----TTTTG-GTT"]],
-        array_align=False,
-        moltype="dna",
-    )
-    aln.annotation_db = db
-    gene = next(iter(aln.get_seq("x").get_features(biotype="gene")))
-    assert str(gene.get_slice()) == "CGGC"
-
-    # evaluate the sequence directly
-    masked = str(aln.get_seq("x").with_masked_annotations("repeat", mask_char="?"))
-    assert masked == "C??CAAAAATTTAA"
-
-    exon = next(iter(aln.get_seq("y").get_features(biotype="repeat", name="frog")))
-    assert str(exon.get_slice()) == "TTT"
-    # evaluate the sequence directly
-    masked = str(aln.get_seq("y").with_masked_annotations("repeat", mask_char="?"))
-    assert masked == "T???TGGTT"
-    masked = aln.with_masked_annotations("gene", mask_char="?")
-    got = masked.to_dict()
-    assert got["x"] == "?-???AAAAATTTAA"
-    assert got["y"] == "-T----TTTTG-GTT"
-
-
 def test_feature_from_alignment():
     """seq features obtained from the alignment"""