Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update external references in change_object_namespace #413

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion sbol3/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,9 @@ def migrate(self, top_levels: Iterable[TopLevel]) -> Any:

@staticmethod
def change_object_namespace(top_levels: Iterable[TopLevel],
new_namespace: str) -> Any:
new_namespace: str,
update_references: Iterable[TopLevel] = None
) -> Any:
"""Change the namespace of all TopLevel objects in `top_levels` to
new_namespace, regardless of the previous value, while
maintaining referential integrity among all the top level
Expand All @@ -734,6 +736,8 @@ def change_object_namespace(top_levels: Iterable[TopLevel],

:param top_levels: objects to change
:param new_namespace: new namespace for objects
:param update_references: objects that should have their references
updated without changing their namespace
:return: Nothing
"""
# Validate the objects and build a map of old name to new name
Expand All @@ -754,6 +758,12 @@ def change_object_namespace(top_levels: Iterable[TopLevel],
top_level.namespace = new_namespace
top_level.set_identity(new_identity)
top_level.update_all_dependents(identity_map)
# Now update any TopLevels in the update_references group. These are
# objects that may have references to the objects that had their
# namespace changed.
if update_references is not None:
for top_level in update_references:
top_level.update_all_dependents(identity_map)
return None

def clone(self) -> List[TopLevel]:
Expand Down
16 changes: 16 additions & 0 deletions test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,22 @@ def test_change_object_namespace_errors(self):
with self.assertRaises(ValueError):
doc.change_object_namespace([i1], new_namespace)

def test_change_object_namespace_with_references(self):
"""Test Document.change_object_namespace with outside references.

See https://github.com/SynBioDex/pySBOL3/issues/412
"""
doc = sbol3.Document()
comp = sbol3.Component('http://sbolstandard.org/testfiles/hello',
sbol3.SBO_DNA)
seq = sbol3.Sequence('http://sbolstandard.org/testfiles/hello_sequence',
elements='ATGC')
doc.add(comp)
doc.add(seq)
comp.sequences = [seq]
doc.change_object_namespace([seq], 'http://parts.igem.org', doc)
self.assertEqual(seq.identity, comp.sequences[0])

def test_clone(self):
namespace = 'https://github.com/synbiodex/pysbol3'
sbol3.set_namespace(namespace)
Expand Down