def setUp(self): # All tests in this file use the default homespace sbol.setHomespace('http://examples.org') # All tests in this file use SBOL compliant URIs sbol.Config.setOption(sbol.ConfigOptions.SBOL_COMPLIANT_URIS.value, True) sbol.Config.setOption(sbol.ConfigOptions.SBOL_TYPED_URIS.value, True)
def test_setVersion_hasHomespace(self): sbol.setHomespace('http://sbols.org/CRISPR_Example') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, False) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol.ModuleDefinition('CRISPR_Template') crispr_template.version = '2' self.assertEqual(crispr_template.version, '2')
def test_getitem(self): sbol.setHomespace('http://sbols.org/CRISPR_Example/') d = sbol.Document() d.read(TEST_LOCATION) s1 = d.sequences['CRa_U6_seq'] expected = 'http://sbols.org/CRISPR_Example/CRa_U6_seq/1.0.0' self.assertEqual(expected, str(s1))
def test_eq(self): sbol.setHomespace('http://example.org/Unit_Test') doc = sbol.Document() md1 = doc.moduleDefinitions.create('Foo1') self.assertEqual(len(doc.moduleDefinitions), 1) md2 = sbol.ModuleDefinition(uri='Foo2') self.assertNotEqual(md1, md2)
def test_import_object_into_new_namespace(self): # When copying an object into a new namespace, confirm that it's URI is copied # into the new namespace. Also confirm that any ReferencedObject attributes # whose values point to an object in the old namespace are also copied into the # new namespace sbol.setHomespace('http://examples.org') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) doc = sbol.Document() comp = sbol.ComponentDefinition('cd') seq = sbol.Sequence('seq') doc.addComponentDefinition(comp) doc.addSequence(seq) comp.sequences = seq.identity # Import from old homespace into new homespace old_homespace = sbol.getHomespace() sbol.setHomespace('http://acme.com') comp_copy = comp.copy(None, old_homespace) # Verify new namespace was correctly substituted self.assertEqual(comp_copy.identity, 'http://acme.com/cd/1') self.assertEqual(comp_copy.persistentIdentity, 'http://acme.com/cd') self.assertEqual(comp_copy.sequences[0], 'http://acme.com/seq/1') # Verify wasDerivedFrom relationship self.assertEqual(comp_copy.wasDerivedFrom[0], comp.identity) # Ensure these are equal under the covers self.assertEqual(type(comp.properties[sbol.SBOL_SEQUENCE_PROPERTY][0]), rdflib.URIRef) self.assertEqual(type(comp.properties[sbol.SBOL_SEQUENCE_PROPERTY][0]), type(comp_copy.properties[sbol.SBOL_SEQUENCE_PROPERTY][0]))
def test_getDisplayId_SBOLCompliant(self): sbol.setHomespace('http://sbols.org/CRISPR_Example') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) expected = 'CRISPR_Template' crispr_template = sbol.ModuleDefinition(expected) self.assertEqual(crispr_template.displayId, expected)
def test_import_into_typed_namespace_from_nontyped_namespace(self): # Copy an sbol-typed URI to a non-typed, sbol-compliant URI sbol.setHomespace('http://examples.org') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) doc = sbol.Document() comp = sbol.ComponentDefinition('cd') seq = sbol.Sequence('seq') comp.sequences = seq.identity doc.addComponentDefinition(comp) doc.addSequence(seq) # Import the object into the new namespace sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, True) old_homespace = sbol.getHomespace() sbol.setHomespace('http://acme.com') comp_copy = comp.copy(None, old_homespace) # Verify new namespace was correctly substituted and type token was successfully # added self.assertEqual(comp_copy.identity, 'http://acme.com/ComponentDefinition/cd/1') self.assertEqual(comp_copy.persistentIdentity, 'http://acme.com/ComponentDefinition/cd') self.assertEqual(comp_copy.sequences[0], 'http://acme.com/Sequence/seq/1') # Verify wasDerivedFrom relationship self.assertEqual(comp_copy.wasDerivedFrom[0], comp.identity)
def test_getPersistentIdentity_hasHomespace(self): sbol.setHomespace('http://sbols.org/CRISPR_Example') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, False) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol.ModuleDefinition('CRISPR_Template') expected = 'http://sbols.org/CRISPR_Example/CRISPR_Template' self.assertEqual(crispr_template.persistentIdentity, expected)
def test_replace_namespace(self): sbol.setHomespace('http://wallacecorporation.com') old_namespace = 'http://tyrellcorporation.com' old_uri = rdflib.URIRef(old_namespace + '/foo') new_uri = sbol.identified.replace_namespace(old_uri, old_namespace, sbol.SBOL_COMPONENT_DEFINITION) self.assertEqual(type(new_uri), rdflib.URIRef) self.assertEqual(new_uri, rdflib.URIRef(sbol.getHomespace() + '/foo'))
def test_SBOLCompliant(self): """See: https://pysbol2.readthedocs.io/en/latest/getting_started.html""" sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol2.ModuleDefinition('CRISPR_Template') self.assertEqual(CRISPR_URI_1, str(crispr_template)) self.assertEqual(CRISPR_URI_1, crispr_template.identity)
def test_openworld_noHomespace(self): """See: https://pysbol2.readthedocs.io/en/latest/getting_started.html""" sbol2.setHomespace('') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, False) sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol2.ModuleDefinition(CRISPR_URI) self.assertEqual(CRISPR_URI, str(crispr_template)) self.assertEqual(CRISPR_URI, crispr_template.identity)
def test_compare(self): # This didn't work because the SBOLObject.__eq__ method was # broken. See issue #62. sbol.setHomespace('http://example.org/Unit_Test') doc = sbol.Document() md1 = doc.moduleDefinitions.create('Foo1') self.assertEqual(len(doc.moduleDefinitions), 1) md2 = doc.moduleDefinitions.create('Foo2') self.assertEqual(len(doc.moduleDefinitions), 2)
def testUpdateSequenceElement(self): sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) doc = sbol2.Document() doc.read(CRISPR_EXAMPLE) # Sequence to test against seq = 'AAAAA' doc.sequences.get('CRP_b_seq').elements = seq seq_read = doc.sequences.get('CRP_b_seq').elements self.assertEqual(seq_read, seq)
def test_submit_to_collection(self): sbh = SynBioHub(SD2Constants.SD2_STAGING_SERVER, self.user, self.password, SD2Constants.SD2_STAGING_SERVER + '/sparql', SD2Constants.SD2_SERVER) sbol.Config.setOption('sbol_typed_uris', False) sbol.Config.setOption('validate', False) sbol.setHomespace('http://dummy.org') member_ID1 = 'foo' doc1 = sbol.Document() doc1.componentDefinitions.create(member_ID1) collection_ID = 'adapter_test_3' collection_name = 'Adapter Test Collection 3' collection_description = 'This is a test of the upload_sbol module of synbiohub_adapter.' collection_version = '1' sbh.submit_collection(doc1, collection_ID, collection_version, collection_name, collection_description, overwrite=True) member_identity1 = '/'.join([ SD2Constants.SD2_SERVER, 'user', self.user, collection_ID, member_ID1, '1' ]) collection_identity = '/'.join([ SD2Constants.SD2_SERVER, 'user', self.user, collection_ID, collection_ID + '_collection', '1' ]) member_ID2 = 'bar' doc2 = sbol.Document() doc2.componentDefinitions.create(member_ID2) sbh.submit_to_collection([doc2], collection_identity) member_identity2 = '/'.join([ SD2Constants.SD2_SERVER, 'user', self.user, collection_ID, member_ID2, '1' ]) query_result = sbh.query_collection_members( collection_uris=[collection_identity]) self.assertIn(collection_identity, query_result) self.assertEqual(len(query_result[collection_identity]), 2) self.assertIn(member_identity1, query_result[collection_identity]) self.assertIn(member_identity2, query_result[collection_identity])
def testAddComponentDefinition(self): sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) expected = 'BB0001' test_CD = sbol2.ComponentDefinition(expected) doc = sbol2.Document() doc.addComponentDefinition(test_CD) self.assertIsNotNone(doc.componentDefinitions.get(expected)) displayId = doc.componentDefinitions.get(expected).displayId self.assertEqual(displayId, expected)
def test_openworld_useHomespace(self): """See: https://pysbol2.readthedocs.io/en/latest/getting_started.html""" # Note: In tutorial CRISPR_Example is followed by a trailing slash. # This results in two slashes instead of one in both the original pySBOL # and the new code. sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, False) sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol2.ModuleDefinition('CRISPR_Template') self.assertEqual(CRISPR_URI, str(crispr_template)) self.assertEqual(CRISPR_URI, crispr_template.identity)
def test_copy_issue_397(self): sbol2.Config.setOption('sbol_typed_uris', False) sbol2.Config.setOption('validate', False) sbol2.setHomespace('http://synbict.org') test_doc = sbol2.Document() parent_comp = test_doc.componentDefinitions.create('parent') child_sub_comp = parent_comp.components.create('child') child_sub_comp.definition = 'http://synbict.org/child/1' sbol2.setHomespace('http://sd2e.org') parent_copy = parent_comp.copy(test_doc, 'http://synbict.org', '1') child_copy = parent_copy.components.get('http://sd2e.org/parent/child/1') self.assertIsNotNone(child_copy.definition) self.assertEqual('http://synbict.org/child/1', child_copy.definition)
def testSequenceElement(self): sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) doc = sbol2.Document() doc.read(CRISPR_EXAMPLE) # Sequence to test against seq = ('GCTCCGAATTTCTCGACAGATCTCATGTGATTACGCCAAGCTACGGGCGGAGTACTGTCCTC' 'CGAGCGGAGTACTGTCCTCCGAGCGGAGTACTGTCCTCCGAGCGGAGTACTGTCCTCCGAGC' 'GGAGTTCTGTCCTCCGAGCGGAGACTCTAGATACCTCATCAGGAACATGTTGGAATTCTAGG' 'CGTGTACGGTGGGAGGCCTATATAAGCAGAGCTCGTTTAGTGAACCGTCAGATCGCCTCGAG' 'TACCTCATCAGGAACATGTTGGATCCAATTCGACC') seq_read = doc.sequences.get('CRP_b_seq').elements self.assertEqual(seq_read, seq)
def test_submit_sub_collection(self): sbh = SynBioHub(SD2Constants.SD2_STAGING_SERVER, self.user, self.password, SD2Constants.SD2_STAGING_SERVER + '/sparql', SD2Constants.SD2_SERVER) sbol.Config.setOption('sbol_typed_uris', False) sbol.Config.setOption('validate', False) sbol.setHomespace('http://dummy.org') doc = sbol.Document() collection_ID = 'adapter_test_2' collection_name = 'Adapter Test Collection 2' collection_description = 'This is a test of the upload_sbol module of synbiohub_adapter.' collection_version = '1' sub_collection_ID = 'sub_adapter_test' sub_collection_name = 'Sub Adapter Test Collection' sub_collection_description = 'This is a test of the upload_sbol module of synbiohub_adapter.' sub_collection_version = '1' sbh.submit_collection(doc, collection_ID, collection_version, collection_name, collection_description, 0, sub_collection_ID, sub_collection_version, sub_collection_name, sub_collection_description, overwrite=True) collection_identity = '/'.join([ SD2Constants.SD2_SERVER, 'user', self.user, collection_ID, collection_ID + '_collection', '1' ]) sub_collection_identity = '/'.join([ SD2Constants.SD2_SERVER, 'user', self.user, collection_ID, sub_collection_ID, '1' ]) query_result = sbh.query_collection_members( collection_uris=[collection_identity]) self.assertIn(collection_identity, query_result) self.assertEqual(len(query_result[collection_identity]), 1) self.assertIn(sub_collection_identity, query_result[collection_identity])
def test_addGetTopLevel_displayId(self): doc = sbol.Document() sbol.setHomespace('http://sbols.org/CRISPR_Example') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol.ModuleDefinition('CRISPR_Template') cas9 = sbol.ComponentDefinition('Cas9', sbol.BIOPAX_PROTEIN) doc.addModuleDefinition(crispr_template) doc.addComponentDefinition(cas9) crispr_template_2 = doc.moduleDefinitions['CRISPR_Template'] cas9_2 = doc.componentDefinitions['Cas9'] self.assertEqual(crispr_template, crispr_template_2) self.assertEqual(cas9, cas9_2)
def test_SBOLCompliant_typed(self): """See: https://pysbol2.readthedocs.io/en/latest/getting_started.html""" sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, True) crispr_template_md = sbol2.ModuleDefinition('CRISPR_Template') expected_crispr_template_md = CRISPR_MD_URI_1 self.assertEqual(expected_crispr_template_md, str(crispr_template_md)) self.assertEqual(expected_crispr_template_md, crispr_template_md.identity) crispr_template_cd = sbol2.ComponentDefinition('CRISPR_Template') expected_crispr_template_cd = CRISPR_CD_URI_1 self.assertEqual(expected_crispr_template_cd, str(crispr_template_cd)) self.assertEqual(expected_crispr_template_cd, crispr_template_cd.identity)
def execute(self, params: dict): """Initialize the SBOL Document.""" setHomespace('http://sbols.org/SuperGSL_Example/') Config.setOption('sbol_compliant_uris', True) Config.setOption('sbol_typed_uris', True) self.sbol_doc = Document() self.assembly_count = 0 assembly_list: AssemblyResultSet = params[0] for assembly in assembly_list: self.handle_assembly(assembly) self.sbol_doc.write('output_sbol.xml')
def test_addGetTopLevel_indexing(self): doc = sbol.Document() # Tutorial doesn't drop final forward slash, but this isn't right. sbol.setHomespace('http://sbols.org/CRISPR_Example') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol.ModuleDefinition('CRISPR_Template') cas9 = sbol.ComponentDefinition('Cas9', sbol.BIOPAX_PROTEIN) doc.addModuleDefinition(crispr_template) doc.addComponentDefinition(cas9) crispr_template_2 = doc.moduleDefinitions[0] cas9_2 = doc.componentDefinitions[0] self.assertEqual(crispr_template, crispr_template_2) self.assertEqual(cas9, cas9_2)
def test_pruning_overlapping(self): __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) HOMESPACE = 'http://synbict.org' MIN_TARGET_LENGTH = 800 sbol2.setHomespace(HOMESPACE) sbol2.Config.setOption('validate', False) sbol2.Config.setOption('sbol_typed_uris', False) cello_doc = load_sbol(os.path.join(__location__, 'cello_library.xml')) feature_library = FeatureLibrary([cello_doc]) target_doc = load_sbol(os.path.join(__location__, 'simple_device.xml')) target_construct_library = FeatureLibrary([target_doc], True) dummy_canonical_library = FeatureLibrary([], False) feature_annotater = FeatureAnnotater(feature_library, 40) annotated_identities = feature_annotater.annotate(target_construct_library, MIN_TARGET_LENGTH) added_features = target_construct_library.update() feature_pruner = FeaturePruner(feature_library) feature_pruner.prune(target_construct_library, 14, MIN_TARGET_LENGTH, False, dummy_canonical_library, keep_flat=False) annotated_features = [] annotating_features = [] for added_feature in added_features: if added_feature.identity in annotated_identities: annotated_features.append(added_feature) else: annotating_features.append(added_feature) feature_pruner.clean(target_construct_library, annotated_features, annotating_features) pruned_definition = target_doc.getComponentDefinition('/'.join([HOMESPACE, 'UnnamedPart', '1'])) self.assertEqual(len(target_doc.componentDefinitions), 2, "Cleaned document does not contain exactly two ComponentDefinitions. Should contain two for UnnamedPart and none for pBAD or L3S3P11.") self.assertEqual(len(target_doc.sequences), 1, "Cleaned document does not contain exactly one Sequence. Should contain one for UnnamedPart and none for pBAD or L3S3P11.") self.assertEqual(len(pruned_definition.sequenceAnnotations), 1, "Pruned ComponentDefinition does not contain exactly one SequenceAnnotation. Should contain one for CDS and none for pBAD, L3S3P11, promoter, or terminator.") self.assertEqual(len(pruned_definition.components), 0, "Pruned ComponentDefinition does not contain exactly zero Components. Should contain none for pBAD or L3S3P11.")
def test_find_property_value(self): # find_property_value wasn't comparing against the passed # `value` parameter, so it was returning all identities in the # document. sbol.setHomespace('http://examples.org') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, True) doc = sbol.Document() cd = doc.moduleDefinitions.create('foo') cd.roles = sbol.SO_PROMOTER test_uri = 'http://examples.org/does/not/exist/1' matches = doc.find_property_value(sbol.SBOL_IDENTITY, test_uri) self.assertEqual(len(matches), 0) matches = doc.find_property_value(sbol.SBOL_ROLES, sbol.SO_PROMOTER) self.assertEqual(len(matches), 1) self.assertEqual(matches[0], cd)
def testUpdateWrite(self): sbol2.setHomespace('http://sbols.org/CRISPR_Example') sbol2.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) doc = sbol2.Document() doc.read(CRISPR_EXAMPLE) # Sequence to test against seq = 'AAAAA' doc.sequences.get('CRP_b_seq').elements = seq doc2 = sbol2.Document() # Document to compare for equality with tempfile.TemporaryDirectory() as tmpdirname: test_file = os.path.join(tmpdirname, 'test.xml') # Write to disk doc.write(test_file) # Compare doc2.read(test_file) seq_read = doc2.sequences.get('CRP_b_seq').elements self.assertEqual(seq_read, seq)
def test_addGetTopLevel_uri(self): doc = sbol.Document() # Tutorial doesn't drop final forward slash, but this isn't right. sbol.setHomespace('http://sbols.org/CRISPR_Example') sbol.Config.setOption(sbol2.ConfigOptions.SBOL_COMPLIANT_URIS, True) sbol.Config.setOption(sbol2.ConfigOptions.SBOL_TYPED_URIS, False) crispr_template = sbol.ModuleDefinition('CRISPR_Template') cas9 = sbol.ComponentDefinition('Cas9', sbol.BIOPAX_PROTEIN) doc.addModuleDefinition(crispr_template) doc.addComponentDefinition(cas9) # Note: tutorial has 1.0.0 instead of 1 but this doesn't work crispr_template_uri = 'http://sbols.org/CRISPR_Example/CRISPR_Template/1' crispr_template_2 = doc.getModuleDefinition(crispr_template_uri) cas9_2 = doc.getComponentDefinition('http://sbols.org/CRISPR_Example/Cas9/1') self.assertEqual(crispr_template, crispr_template_2) self.assertEqual(cas9, cas9_2)
def sbh_login(SBH_USER, SBH_PASSWORD, spoof_bool, parts_doc): # submit to staging server if spoof_bool: sbh_server = sbol.PartShop('https://hub-staging.sd2e.org') sbh_server.spoof("https://hub.sd2e.org") sbol.setHomespace( 'https://hub-staging.sd2e.org/user/sd2e/{collection}'.format( collection=parts_doc.displayId)) # submit to production server else: sbh_server = sbol.PartShop('https://hub.sd2e.org') sbol.setHomespace('https://hub.sd2e.org/user/sd2e/{collection}'.format( collection=parts_doc.displayId)) sbh_server.login(SBH_USER, SBH_PASSWORD) return sbh_server
def test_addImplementationGenerator(self): # We should be able to add implementations from a generator. # We should not be restricted to adding only a list. Any # Iterable should be fine. def impl_gen(count): for i in range(count): uri = 'impl{}'.format(i) yield sbol.Implementation(uri=uri) homespace = 'http://sbols.org/sbol_test' sbol.setHomespace(homespace) doc = sbol.Document() doc.addImplementation(impl_gen(3)) uri_template = '{}/impl{}/1' self.assertEqual(doc.implementations[0].identity, uri_template.format(homespace, '0')) self.assertEqual(doc.implementations[1].identity, uri_template.format(homespace, '1')) self.assertEqual(doc.implementations[2].identity, uri_template.format(homespace, '2'))
def test_getImplementation(self): homespace = 'http://sbols.org/sbol_test' sbol.setHomespace(homespace) doc = sbol.Document() impl = sbol.Implementation(uri='foo') doc.addImplementation(impl) impl2 = sbol.Implementation(uri='bar') doc.addImplementation(impl2) # Get by index self.assertEqual(doc.getImplementation(0), impl) self.assertEqual(doc.getImplementation(1), impl2) # Get by short URI self.assertEqual(doc.getImplementation('foo'), impl) self.assertEqual(doc.getImplementation('bar'), impl2) # Get by full URI uri_template = '{}/Implementation/{}/1' uri = rdflib.URIRef(uri_template.format(homespace, 'foo')) self.assertEqual(doc.getImplementation(uri), impl) uri = rdflib.URIRef(uri_template.format(homespace, 'bar')) self.assertEqual(doc.getImplementation(uri), impl2)