Skip to content

Commit

Permalink
Merge pull request #222 from Yehuda-Binik/implementation3to2
Browse files Browse the repository at this point in the history
Implementation for SBOL2<-->SBOL3 converter
  • Loading branch information
jakebeal authored Oct 31, 2023
2 parents 3af9492 + 7750c08 commit 3ef7723
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ __pycache__/

# test secrets
test_secret*

.idea/
*.egg-info/
17 changes: 13 additions & 4 deletions sbol_utilities/sbol3_sbol2_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,14 @@ def visit_externally_defined(self, a: sbol3.ExternallyDefined):
# Priority: 3
raise NotImplementedError('Conversion of ExternallyDefined from SBOL3 to SBOL2 not yet implemented')

def visit_implementation(self, a: sbol3.Implementation):
def visit_implementation(self, imp3: sbol3.Implementation):
# Priority: 1
raise NotImplementedError('Conversion of Implementation from SBOL3 to SBOL2 not yet implemented')
# Make the Implement object and add it to the document
imp2 = sbol2.Implementation(imp3.identity, version=self._sbol2_version(imp3))
imp2.built = imp3.built
self.doc2.addImplementation(imp2)
# Map over all other TopLevel properties and extensions not covered by the constructor
self._convert_toplevel(imp3, imp2)

def visit_interaction(self, a: sbol3.Interaction):
# Priority: 2
Expand Down Expand Up @@ -462,9 +467,13 @@ def visit_generic_location(self, a: sbol2.GenericLocation):
# Priority: 3
raise NotImplementedError('Conversion of GenericLocation from SBOL2 to SBOL3 not yet implemented')

def visit_implementation(self, a: sbol2.Implementation):
def visit_implementation(self, imp2: sbol2.Implementation):
# Priority: 1
raise NotImplementedError('Conversion of Implementation from SBOL2 to SBOL3 not yet implemented')
# Make the Implementation object and add it to the document
imp3 = sbol3.Implementation(imp2.identity, namespace=self._sbol3_namespace(imp2), built=imp2.built)
self.doc3.add(imp3)
# Map over all other TopLevel properties and extensions not covered by the constructor
self._convert_toplevel(imp2, imp3)

def visit_interaction(self, a: sbol2.Interaction):
# Priority: 2
Expand Down
13 changes: 13 additions & 0 deletions test/test_files/sbol3_implementation.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<https://sbolstandard.org/examples/impl1> <http://sbols.org/v3#built> <https://sbolstandard.org/examples/TetR_protein> .
<https://sbolstandard.org/examples/impl1> <http://sbols.org/v3#hasNamespace> <https://sbolstandard.org/examples> .
<https://sbolstandard.org/examples/impl1> <http://sbols.org/v3#displayId> "impl1" .
<https://sbolstandard.org/examples/impl1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://sbols.org/v3#Implementation> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#role> <https://identifiers.org/GO:0003700> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#description> "TetR protein" .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#name> "TetR" .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#hasNamespace> <https://sbolstandard.org/examples> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#type> <https://identifiers.org/SBO:0000252> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#displayId> "TetR_protein" .
<https://sbolstandard.org/examples/TetR_protein> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://sbols.org/v3#Component> .
<https://sbolstandard.org/examples/TetR_protein> <http://sboltools.org/backport#sbol2version> "1" .
<https://sbolstandard.org/examples/impl1> <http://sboltools.org/backport#sbol2version> "1" .
19 changes: 19 additions & 0 deletions test/test_files/sbol_3to2_implementation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<rdf:RDF xmlns:brick="https://brickschema.org/schema/Brick#" xmlns:csvw="http://www.w3.org/ns/csvw#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcat="http://www.w3.org/ns/dcat#" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcam="http://purl.org/dc/dcam/" xmlns:doap="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:geo="http://www.opengis.net/ont/geosparql#" xmlns:odrl="http://www.w3.org/ns/odrl/2/" xmlns:org="http://www.w3.org/ns/org#" xmlns:prof="http://www.w3.org/ns/dx/prof/" xmlns:prov="http://www.w3.org/ns/prov#" xmlns:qb="http://purl.org/linked-data/cube#" xmlns:schema="https://schema.org/" xmlns:sh="http://www.w3.org/ns/shacl#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:sosa="http://www.w3.org/ns/sosa/" xmlns:ssn="http://www.w3.org/ns/ssn/" xmlns:time="http://www.w3.org/2006/time#" xmlns:vann="http://purl.org/vocab/vann/" xmlns:void="http://rdfs.org/ns/void#" xmlns:wgs="https://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:backport="http://sboltools.org/backport#" xmlns:om="http://www.ontology-of-units-of-measure.org/resource/om-2/" xmlns:sbol="http://sbols.org/v2#">
<sbol:ComponentDefinition rdf:about="https://sbolstandard.org/examples/TetR_protein">
<sbol:type rdf:resource="http://www.biopax.org/release/biopax-level3.owl#Protein"/>
<sbol:version>1</sbol:version>
<sbol:persistentIdentity rdf:resource="https://sbolstandard.org/examples/TetR_protein"/>
<dcterms:title>TetR</dcterms:title>
<sbol:displayId>TetR_protein</sbol:displayId>
<backport:sbol3namespace rdf:resource="https://sbolstandard.org/examples"/>
<sbol:role rdf:resource="https://identifiers.org/GO:0003700"/>
<dcterms:description>TetR protein</dcterms:description>
</sbol:ComponentDefinition>
<sbol:Implementation rdf:about="https://sbolstandard.org/examples/impl1">
<sbol:built rdf:resource="https://sbolstandard.org/examples/TetR_protein"/>
<backport:sbol3namespace rdf:resource="https://sbolstandard.org/examples"/>
<sbol:persistentIdentity rdf:resource="https://sbolstandard.org/examples/impl1"/>
<sbol:version>1</sbol:version>
<sbol:displayId>impl1</sbol:displayId>
</sbol:Implementation>
</rdf:RDF>
13 changes: 13 additions & 0 deletions test/test_files/sbol_3to2to3_implementation.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<https://sbolstandard.org/examples/TetR_protein> <http://sboltools.org/backport#sbol2version> "1" .
<https://sbolstandard.org/examples/impl1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://sbols.org/v3#Implementation> .
<https://sbolstandard.org/examples/TetR_protein> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://sbols.org/v3#Component> .
<https://sbolstandard.org/examples/impl1> <http://sbols.org/v3#hasNamespace> <https://sbolstandard.org/examples> .
<https://sbolstandard.org/examples/impl1> <http://sbols.org/v3#built> <https://sbolstandard.org/examples/TetR_protein> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#type> <https://identifiers.org/SBO:0000252> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#role> <https://identifiers.org/GO:0003700> .
<https://sbolstandard.org/examples/impl1> <http://sbols.org/v3#displayId> "impl1" .
<https://sbolstandard.org/examples/impl1> <http://sboltools.org/backport#sbol2version> "1" .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#displayId> "TetR_protein" .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#description> "TetR protein" .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#hasNamespace> <https://sbolstandard.org/examples> .
<https://sbolstandard.org/examples/TetR_protein> <http://sbols.org/v3#name> "TetR" .
42 changes: 40 additions & 2 deletions test/test_sbol2_sbol3_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_3to2_conversion(self):
self.assertFalse(file_diff(str(tmp3), str(TEST_FILES / 'BBa_J23101_patched.nt')))

def test_2to3_conversion(self):
"""Test ability to convert a simple part from SBOL3 to SBOL2"""
# Load an SBOL3 document and check its contents
"""Test ability to convert a simple part from SBOL2 to SBOL3"""
# Load an SBOL2 document and check its contents
doc2 = sbol2.Document()
doc2.read(TEST_FILES / 'BBa_J23101.xml')
# Convert to SBOL3 and check contents
Expand All @@ -53,6 +53,44 @@ def test_2to3_conversion(self):
doc2_loop.write(tmp2)
self.assertFalse(file_diff(str(tmp2), str(TEST_FILES / 'BBa_J23101.xml')))

def test_3to2_implementation_conversion(self):
"""Test ability to convert an implementation from SBOL3 to SBOL2"""
# Load an SBOL3 document and check its contents
doc3 = sbol3.Document()
doc3.read(TEST_FILES / 'sbol3_implementation.nt')
# Convert to SBOL2 and check contents
doc2 = convert3to2(doc3, True)
#report = doc2.validate()
#self.assertEqual(len(report), 0, f'Validation failed: {report}')
with tempfile.TemporaryDirectory() as tmpdir:
tmp2 = Path(tmpdir) / 'doc2.xml'
doc2.write(tmp2)
self.assertFalse(file_diff(str(tmp2), str(TEST_FILES / 'sbol_3to2_implementation.xml')))
doc3_loop = convert2to3(doc2, use_native_converter=True)
self.assertEqual(len(doc3_loop.validate()), 0)
tmp3 = Path(tmpdir) / 'doc3_loop.nt'
doc3_loop.write(tmp3)
self.assertFalse(file_diff(str(tmp3), str(TEST_FILES / 'sbol3_implementation.nt')))

def test_2to3_implementation_conversion(self):
"""Test ability to convert an implementation from SBOL2 to SBOL3"""
# Load an SBOL2 document and check its contents
doc2 = sbol2.Document()
doc2.read(TEST_FILES / 'sbol_3to2_implementation.xml')
# Convert to SBOL3 and check contents
doc3 = convert2to3(doc2, use_native_converter=True)
self.assertEqual(len(doc3.validate()), 0)
with tempfile.TemporaryDirectory() as tmpdir:
tmp3 = Path(tmpdir) / 'doc3.nt'
doc3.write(tmp3)
self.assertFalse(file_diff(str(tmp3), str(TEST_FILES / 'sbol3_implementation.nt')))
doc2_loop = convert3to2(doc3, True)
# report = doc2.validate()
# self.assertEqual(len(report), 0, f'Validation failed: {report}')
tmp2 = Path(tmpdir) / 'doc2_loop.xml'
doc2_loop.write(tmp2)
self.assertFalse(file_diff(str(tmp2), str(TEST_FILES / 'sbol_3to2_implementation.xml')))


if __name__ == '__main__':
unittest.main()

0 comments on commit 3ef7723

Please sign in to comment.