Repository: cogent3/cogent3
Pull request #1580 (2023-09-20 08:47:12)

Fixed bug #955


Patch:
diff --git a/changelog.d/20230920_184448_Gavin.Huttley.md b/changelog.d/20230920_184448_Gavin.Huttley.md
new file mode 100644
index 000000000..88cc51412
--- /dev/null
+++ b/changelog.d/20230920_184448_Gavin.Huttley.md
@@ -0,0 +1,40 @@
+<!--
+A new scriv changelog fragment.
+
+Uncomment the section that is right (remove the HTML comment wrapper).
+-->
+
+<!--
+### Contributors
+
+- A bullet item for the Contributors category.
+
+-->
+<!--
+### ENH
+
+- A bullet item for the ENH category.
+
+-->
+### BUG
+
+- Degapped sequence and collections now retain features
+
+<!--
+### DOC
+
+- A bullet item for the DOC category.
+
+-->
+<!--
+### Deprecations
+
+- A bullet item for the Deprecations category.
+
+-->
+<!--
+### Discontinued
+
+- A bullet item for the Discontinued category.
+
+-->
diff --git a/src/cogent3/core/sequence.py b/src/cogent3/core/sequence.py
index 65d5807f3..797d411dd 100644
--- a/src/cogent3/core/sequence.py
+++ b/src/cogent3/core/sequence.py
@@ -328,7 +328,11 @@ class SequenceI(object):
 
     def degap(self):
         """Deletes all gap characters from sequence."""
-        return self.__class__(self.moltype.degap(self), name=self.name, info=self.info)
+        result = self.__class__(
+            self.moltype.degap(self), name=self.name, info=self.info
+        )
+        result.annotation_db = self.annotation_db
+        return result
 
     def gap_indices(self):
         """Returns list of indices of all gaps in the sequence, or []."""


Test patch:
diff --git a/tests/test_core/test_features.py b/tests/test_core/test_features.py
index 20ae08f6c..5330b2746 100755
--- a/tests/test_core/test_features.py
+++ b/tests/test_core/test_features.py
@@ -3,6 +3,7 @@ from unittest import TestCase
 import pytest
 
 from cogent3 import ASCII, DNA, get_moltype, make_aligned_seqs
+from cogent3.core.alignment import Alignment, SequenceCollection
 from cogent3.core.annotation import Feature
 from cogent3.core.annotation_db import BasicAnnotationDb, GffAnnotationDb
 # Complete version of manipulating sequence annotations
@@ -705,3 +706,22 @@ def test_hash_feature(ann_seq):
     (f2,) = list(ann_seq.get_features(biotype="gene"))
     got = {f1, f2}
     assert len(got) == 1
+
+
+def test_seq_degap_preserves_annotations():
+    s1 = DNA.make_seq("AAGAAGAAGACCCCCAAAAAAAAAATTTTTTTTTTAAAAAGGGAACCCT", name="seq1")
+    s1.add_feature(biotype="exon", name="A", spans=[(10, 15)])
+    dg = s1.degap()
+    assert len(s1.annotation_db) == len(dg.annotation_db)
+
+
+@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
+def test_align_degap_preserves_annotations(cls):
+    """get translation works on incomplete codons"""
+    coll = cls(data={"seq1": "GATN--", "seq2": "?GATCT"}, moltype=DNA)
+    db = BasicAnnotationDb()
+    db.add_feature(biotype="exon", name="exon1", spans=[(1, 2)], seqid="seq1")
+    coll.annotation_db = db
+    got = coll.degap()
+    assert got.annotation_db is coll.annotation_db
+    assert len(got.annotation_db) == 1
diff --git a/tests/test_core/test_sequence.py b/tests/test_core/test_sequence.py
index 33f622053..f49a68e7f 100644
--- a/tests/test_core/test_sequence.py
+++ b/tests/test_core/test_sequence.py
@@ -2,7 +2,6 @@
 """
 import json
 import os
-import pathlib
 import re
 
 from pickle import dumps