def testAssemble(self): # Assemble module hierarchy doc = sbol2.Document() root = sbol2.ModuleDefinition('root') sub = sbol2.ModuleDefinition('sub') doc.addModuleDefinition([root, sub]) root.assemble([sub])
def testApplyCallbackRecursively(self): # Assemble module hierarchy doc = sbol2.Document() root = sbol2.ModuleDefinition('root') sub = sbol2.ModuleDefinition('sub') leaf = sbol2.ModuleDefinition('leaf') doc.addModuleDefinition([root, sub, leaf]) root.assemble([sub]) sub.assemble([leaf]) # Define callback which performs an operation on the given ModuleDefinition def callback(md, params): level = params[0] level += 1 params[0] = level # Apply callback level = 0 params = [level] flattened_module_tree = root.applyToModuleHierarchy(callback, params) level = params[0] flattened_module_tree = [md.identity for md in flattened_module_tree] expected_module_tree = [md.identity for md in [root, sub, leaf]] self.assertSequenceEqual(flattened_module_tree, expected_module_tree) self.assertEqual(level, 3)
def deprecated_test_set_property_value(self): # The bottom line is that SBOLObject.getPropertyValue() always # returns a string. That's what pysbol did, and what sbol2 # should do. my_property = 'http://example.com/myproperty' md = sbol.ModuleDefinition('md1') # value argument must be of type string. If not, expect type error with self.assertRaises(TypeError): md.setPropertyValue(my_property, 2) foo_str = 'foo' bar_str = 'bar' with self.assertRaises(TypeError): md.setPropertyValue(my_property, [foo_str, bar_str]) # Test basic setting empty_str = '' foo_literal = rdflib.Literal(foo_str) md.setPropertyValue(my_property, foo_literal) self.assertEqual(md.getPropertyValue(my_property), foo_str) # Test overwriting a single existing value md.setPropertyValue(my_property, bar_str) self.assertEqual(md.getPropertyValue(my_property), bar_str) # Test setting an existing multi-value property md.properties[rdflib.URIRef(my_property)] = [foo_str, bar_str] self.assertEqual(md.getPropertyValue(my_property), foo_str) self.assertEqual(md.getPropertyValues(my_property), [foo_str, bar_str]) baz_str = 'baz' md.setPropertyValue(my_property, baz_str) self.assertEqual(md.getPropertyValue(my_property), baz_str) self.assertEqual([baz_str, bar_str], md.getPropertyValues(my_property)) # Unset the value md.setPropertyValue(my_property, empty_str) self.assertEqual(md.getPropertyValue(my_property), empty_str) # This may seem odd, but it is the way pySBOL/libSBOL worked, # so we do it for backward compatibility self.assertEqual([empty_str, bar_str], md.getPropertyValues(my_property)) # What about a plain string? Does that get converted to URIRef or Literal? # If a value is present, mimic that value's type # Else make the value a Literal foo_uri = rdflib.URIRef(foo_str) md2 = sbol.ModuleDefinition('md2') # String gets converted to Literal md2.setPropertyValue(my_property, foo_str) self.assertEqual(md2.getPropertyValue(my_property), foo_str) # Ok to overwrite a Literal with a URIRef md2.setPropertyValue(my_property, foo_uri) self.assertEqual(md2.getPropertyValue(my_property), foo_str) # Now setting a str value should convert it to a URIRef # because that's what is already there md2.setPropertyValue(my_property, bar_str) self.assertEqual(md2.getPropertyValue(my_property), bar_str) # Ok to overwrite a URIRef with a Literal md2.setPropertyValue(my_property, foo_literal) self.assertEqual(md2.getPropertyValue(my_property), foo_str) # Unsetting the value md2.setPropertyValue(my_property, '') self.assertEqual(md2.getPropertyValue(my_property), empty_str)
def test_compare_owned_objects(self): # Test that two objects are equal even if owned objects are in # a different order expected1 = [sbol2.Module('m1'), sbol2.Module('m2')] expected2 = [sbol2.Module('m2'), sbol2.Module('m1')] md1 = sbol2.ModuleDefinition('md1') md1.modules = expected1 md2 = sbol2.ModuleDefinition('md1') md2.modules = expected2 self.assertTrue(md1.compare(md2))
def test_compare_properties(self): # Test that two objects are equal even if a property is in a # different order expected1 = [sbol2.SBO_INHIBITION, sbol2.SBO_CONTROL] expected2 = [sbol2.SBO_CONTROL, sbol2.SBO_INHIBITION] md1 = sbol2.ModuleDefinition('md1') md1.roles = expected1 md2 = sbol2.ModuleDefinition('md1') md2.roles = expected2 self.assertTrue(md1.compare(md2))
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_literal_property_properties(self): md = sbol.ModuleDefinition() self.assertNotIn(rdflib.URIRef(sbol.UNDEFINED), md.properties) sbol.property.TextProperty(md, sbol.UNDEFINED, '0', '*', [], 'foo') # Creating the property should also create the entry in the # parent properties dict self.assertIn(rdflib.URIRef(sbol.UNDEFINED), md.properties)
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 testAttachments(self): # All TopLevels can have attachments # TODO: There's a better test here involving a Document attachment = sbol2.Attachment('attachment') md = sbol2.ModuleDefinition('md') md.attachments = attachment.identity self.assertEqual([attachment.identity], md.attachments)
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_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_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_find(self): md = sbol.ModuleDefinition('foo') m1 = md.modules.create('m1') self.assertEqual(m1, md.modules.get(0)) self.assertEqual(m1, md.modules.find(m1.identity)) self.assertEqual(m1, md.modules.find(m1.displayId)) self.assertEqual(m1, md.modules.find(str(m1.displayId))) self.assertEqual(m1, md.modules.find('m1'))
def test_set_no_doc(self): # Add a module when the parent module definition is not in a # Document md = sbol.ModuleDefinition('foo') m1 = sbol.Module('m1') md.modules.set([m1]) self.assertEqual(m1, md.modules.get(0)) self.assertEqual(m1, md.modules.find(m1.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 build_md_tree_3(self, suffix='1'): # Build a md->m->measurement tree for comparison meter_uri = 'http://www.ontology-of-units-of-measure.org/resource/om-2/metre' mdef = sbol2.ModuleDefinition('md' + suffix) module = sbol2.Module('m' + suffix) measurement = sbol2.Measurement('meas' + suffix, 5.0, meter_uri) module.measurements = [measurement] mdef.modules = [module] return mdef
def test_owned_object_remove(self): md = sbol.ModuleDefinition('md') m1 = md.modules.create('m1') m2 = md.modules.create('m2') self.assertEqual(list(md.modules), [m1, m2]) obj = md.modules.remove(m2.identity) self.assertEqual(list(md.modules), [m1]) self.assertEqual(obj.identity, m2.identity) self.assertIsNone(obj.doc) self.assertIsNone(m2.doc)
def test_validation_rules(self): md = sbol2.ModuleDefinition() with self.assertRaises(TypeError): # No validation rules, so `'AND'` is interpreted # as validation rules. tp = sbol2.TextProperty(md, 'http://example.com#logic', '0', '1', 'AND', 'initial_value') # Use an empty list to specify no validation rules tp = sbol2.TextProperty(md, 'http://example.com#logic', '0', '1', [], 'AND')
def test_this(self): # The `this` attribute should return the object itself. This # is for backward compatibility with pySBOL. It is a # deprecated attribute. md = sbol2.ModuleDefinition('md') # Enable all warnings warnings.simplefilter("default") with warnings.catch_warnings(record=True) as warns: self.assertEqual(md.this, md) # Verify that a warning was issued self.assertEqual(len(warns), 1)
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_add_remove_role(self): md = sbol2.ModuleDefinition('c1') self.assertEqual([], md.roles) md.addRole(sbol2.SO_PROMOTER) self.assertEqual([sbol2.SO_PROMOTER], md.roles) md.addRole(sbol2.SO_MISC) self.assertEqual([sbol2.SO_PROMOTER, sbol2.SO_MISC], md.roles) md.addRole(sbol2.SO_CDS) self.assertEqual([sbol2.SO_PROMOTER, sbol2.SO_MISC, sbol2.SO_CDS], md.roles) md.removeRole(1) self.assertEqual([sbol2.SO_PROMOTER, sbol2.SO_CDS], md.roles)
def test2ArgConstructor(self): name = 'foo' i = sbol.Interaction(name, sbol.SBO_INHIBITION) expected_identity = self.make_identity(self.homespace, name, sbol.VERSION_STRING) self.assertEqual(i.identity, expected_identity) self.assertEqual(i.displayId, name) self.assertEqual(i.types, [sbol.SBO_INHIBITION]) self.assertEqual(i.rdf_type, sbol.SBOL_INTERACTION) md = sbol.ModuleDefinition() md.interactions.add(i) self.assertEqual(len(md.interactions), 1)
def test_text_property_constructor(self): # Test None as parent object with self.assertRaises(AttributeError): sbol.TextProperty(None, sbol.SBOL_NAME, '0', '*', [], 'foo') # Test string as parent object with self.assertRaises(AttributeError): sbol.TextProperty('foo', sbol.SBOL_NAME, '0', '*', [], 'foo') # Test with object whose properties attribute is not a dict with self.assertRaises(TypeError): md = sbol.ModuleDefinition() md.properties = [] sbol.TextProperty(md, sbol.SBOL_NAME, '0', '*', [], 'foo')
def test_containers(self): # Note: Alphabetical Order x = sbol.Activity() x = sbol.CombinatorialDerivation() x = sbol.Component() x = sbol.ComponentDefinition() x = sbol.Document() x = sbol.Interaction() x = sbol.Module() x = sbol.ModuleDefinition() x = sbol.Participation() x = sbol.SequenceAnnotation()
def create_synbiohub_entry(self, sbol_type, sbol_type_map, display_id, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag): document = sbol.Document() document.addNamespace('http://sd2e.org#', 'sd2') document.addNamespace('http://purl.org/dc/terms/', 'dcterms') document.addNamespace('http://www.w3.org/ns/prov#', 'prov') document.displayId = 'foo' document.version = '1' if sbol_type == 'component': if item_type == 'CHEBI': item_sbol_type = item_definition_uri else: item_sbol_type = sbol_type_map[item_type] component = sbol.ComponentDefinition(display_id, item_sbol_type) sbol.TextProperty(component, 'http://sd2e.org#stub_object', '0', '1', 'true') self.set_item_properties(component, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.addComponentDefinition(component) elif sbol_type == 'module': module = sbol.ModuleDefinition(display_id) sbol.TextProperty(module, 'http://sd2e.org#stub_object', '0', '1', 'true') module.roles = sbol_type_map[item_type] self.set_item_properties(module, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.addModuleDefinition(module) elif sbol_type == 'external': top_level = sbol.TopLevel('http://sd2e.org/types/#attribute', display_id) self.set_item_properties(top_level, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.add(top_level) elif sbol_type == 'collection': collection = sbol.Collection(display_id) self.set_item_properties(collection, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.addCollection(collection) else: raise IntentParserException( 'Failed to create a SynBioHub entry: %s as a supported sbol type in Intent Parser' % sbol_type) return document
def testEmptyConstructor(self): # This is the default name in the interaction constructor name = 'example' i = sbol.Interaction() expected_identity = self.make_identity(self.homespace, name, sbol.VERSION_STRING) self.assertEqual(i.identity, expected_identity) self.assertEqual(i.displayId, name) self.assertEqual(i.version, sbol.VERSION_STRING) self.assertEqual(i.rdf_type, sbol.SBOL_INTERACTION) md = sbol.ModuleDefinition() md.interactions.add(i) self.assertEqual(len(md.interactions), 1)
def test_compare_recursive(self): # Test that the compare method is doing a recursive compare m1a = sbol2.Module('m1') m1b = sbol2.Module('m1') # m1a and m1b compare True self.assertTrue(m1a.compare(m1b)) m2 = sbol2.Module('m2') # m2 is different from m1a and m1b self.assertFalse(m2.compare(m1a)) self.assertFalse(m2.compare(m1b)) md1a = sbol2.ModuleDefinition('md1') md1b = sbol2.ModuleDefinition('md1') # md1a and md1b compare True self.assertTrue(md1a.compare(md1b)) md1a.modules = [m1a] md1b.modules = [m1b] # md1a and md1b still compare True self.assertTrue(md1a.compare(md1b)) md1a.modules = [m2] # Now md1a and md1b compare False because of recursive # comparison of modules self.assertFalse(md1a.compare(md1b))
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 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)