def test_copy_stability(self): # Test the stability of naming of objects across copies. # See https://github.com/SynBioDex/pySBOL3/issues/231 # # Strategy: create an object with 10+ children of the same # type. Add to a document and serialize the document. Load the # serialized document. Copy the object to a new document. # Serialize the new document. Compare the serializations. If we # use sorted ntriples, the serializations should be the same. # This will demonstrate that we maintain names properly despite # the inherently unordered nature of SBOL. sbol3.set_namespace('https://github.com/synbiodex/pysbol3') c1 = sbol3.Component('c1', types=[sbol3.SBO_DNA]) # Create a double-digit number of children to test sort of 10, 11, 1, etc. for i in range(12): instance_of_uri = f'https://example.com/instance/i{i}' c1.features.append(sbol3.SubComponent(instance_of=instance_of_uri)) doc1 = sbol3.Document() doc1.add(c1) # Serialize to string doc1_string = doc1.write_string(sbol3.SORTED_NTRIPLES) self.assertIsNotNone(doc1_string) # Load the serialized document into a new document tmp_doc = sbol3.Document() tmp_doc.read_string(doc1_string, sbol3.SORTED_NTRIPLES) # Locate the top level to copy tmp_c1 = tmp_doc.find('c1') self.assertIsNotNone(tmp_c1) self.assertIsInstance(tmp_c1, sbol3.TopLevel) # Copy the top level into a new document doc2 = sbol3.Document() sbol3.copy([tmp_c1], into_document=doc2) doc2_string = doc2.write_string(sbol3.SORTED_NTRIPLES) # Verify that the serializations are identical self.assertEqual(doc1_string, doc2_string)
def test_copy(self): namespace = 'https://github.com/synbiodex/pysbol3' sbol3.set_namespace(namespace) test_path = os.path.join(SBOL3_LOCATION, 'multicellular', 'multicellular.ttl') doc = sbol3.Document() doc.read(test_path) copies1 = sbol3.copy(doc) self.assertEqual(len(doc), len(copies1)) document_checker = self.make_document_checker(None) for obj in copies1: obj.traverse(document_checker) # Verify that the copies get the new namespace copies2 = sbol3.copy(doc, into_namespace=namespace) document_checker = self.make_document_checker(None) namespace_checker = self.make_namespace_checker(namespace) for obj in copies2: obj.traverse(document_checker) obj.traverse(namespace_checker) # Verify new namespace AND new document namespace3 = 'https://github.com/synbiodex/pysbol3/copytest' doc3 = sbol3.Document() copies3 = sbol3.copy(doc, into_namespace=namespace3, into_document=doc3) document_checker = self.make_document_checker(doc3) namespace_checker = self.make_namespace_checker(namespace3) for obj in copies3: obj.traverse(document_checker) obj.traverse(namespace_checker)
def test_shacl_closure_with_toplevels(self): # SBOL closure semantics should allow properties to reference # a TopLevel object not contained in the Document # This is the test case in https://github.com/SynBioDex/pySBOL3/issues/348 doc = sbol3.Document() doc.read(os.path.join(TEST_DIR, 'resources', 'package.nt')) self.assertEqual(len(doc.validate()), 0) minidoc = sbol3.Document() c = doc.find('https://synbiohub.org/public/igem/BBa_I20270') self.assertIsInstance(c, sbol3.TopLevel) sbol3.copy([c], into_document=minidoc) # this assertion failed before the fix to the shacl rules self.assertEqual(len(minidoc.validate()), 0)
def test_copy_properties(self): sbol3.set_namespace('https://github.com/synbiodex/pysbol3') root = sbol3.Component('root', sbol3.SBO_DNA) root.name = 'foo' objects = sbol3.copy([root]) root_copy = objects[0] self.assertEqual(root_copy.name, 'foo')
def test_copy(self): # See https://github.com/SynBioDex/pySBOL3/issues/176 reopened # Copying a tree of objects to a new document left the document # pointer of the child objects unset. This caused "lookup" to # fail. dest_doc = sbol3.Document() def check_document(i: sbol3.Identified): # Verify that the object has a document, and that it is the # expected document. self.assertIsNotNone(i.document) self.assertEqual(dest_doc, i.document) test_path = os.path.join(SBOL3_LOCATION, 'multicellular', 'multicellular.nt') doc = sbol3.Document() doc.read(test_path) sbol3.copy(doc, into_document=dest_doc) self.assertEqual(len(doc), len(dest_doc)) for obj in dest_doc.objects: obj.traverse(check_document)
def test_copy_child_objects(self): sbol3.set_namespace('https://github.com/synbiodex/pysbol3') doc = sbol3.Document() root = sbol3.Component('root', sbol3.SBO_DNA) sub1 = sbol3.Component('sub1', sbol3.SBO_DNA) sub2 = sbol3.Component('sub2', sbol3.SBO_DNA) sc1 = sbol3.SubComponent(sub1) sc2 = sbol3.SubComponent(sub2) root.features.append(sc1) root.features.append(sc2) doc.add(root) doc.add(sub1) doc.add(sub2) doc2 = sbol3.Document() objects = sbol3.copy([root], into_document=doc2) root_copy = objects[0] self.assertIn(root_copy, doc2.objects) self.assertEqual([sc.identity for sc in root.features], [sc.identity for sc in root_copy.features])