Skip to content

Commit

Permalink
Merge pull request #372 from ChristopherBradley/367
Browse files Browse the repository at this point in the history
to_rich_dict works on annotated annotations
  • Loading branch information
GavinHuttley committed Nov 12, 2019
2 parents 578c619 + 1b60ac2 commit 6712007
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/cogent3/core/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ def to_rich_dict(self):
data = dict(annotation_construction=data)
data["type"] = get_object_provenance(self)
data["version"] = __version__

try:
annotations = [a.to_rich_dict() for a in self.annotations]
if annotations:
data["annotations"] = annotations
except AttributeError:
pass

return data

def to_json(self):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_core/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def setUp(self):
)
self.exon1 = self.s.add_annotation(Feature, "exon", "fred", [(10, 15)])
self.exon2 = self.s.add_annotation(Feature, "exon", "trev", [(30, 40)])
self.nested_feature = self.exon1.add_feature("repeat", "bob", [(2, 5)])

def test_exon_extraction(self):
"""exon feature used to slice or directly access sequence"""
Expand Down Expand Up @@ -626,8 +627,14 @@ def test_roundtrip_variable(self):

def test_nested_get_slice(self):
"""check the get_slice method works on nested annotations"""
nested_feature = self.exon1.add_feature("repeat", "C", [(2, 5)])
self.assertEqual(nested_feature.get_slice(), "CCC")
self.assertEqual(self.nested_feature.get_slice(), "CCC")

def test_nested_to_rich_dict(self):
"""check the to_rich_dict method works with nested annotations"""
self.assertEqual(
self.exon1.to_rich_dict()["annotations"][0],
self.nested_feature.to_rich_dict(),
)


if __name__ == "__main__":
Expand Down

0 comments on commit 6712007

Please sign in to comment.