Skip to content
Open
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
32 changes: 26 additions & 6 deletions sbol_utilities/sbol3_sbol2_conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sbol2.model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import should not be needed, given the from sbol2 line below.
In fact, we should probably remove that one too since it makes the references in the code more ambiguous.

import sbol3
import sbol2
from sbol2 import mapsto, model, sequenceconstraint
Expand Down Expand Up @@ -235,9 +236,16 @@ def visit_measure(self, a: sbol3.Measure):
# Priority: 3
raise NotImplementedError('Conversion of Measure from SBOL3 to SBOL2 not yet implemented')

def visit_model(self, a: sbol3.Model):
# Priority: 3
raise NotImplementedError('Conversion of Model from SBOL3 to SBOL2 not yet implemented')
def visit_model(self, model3: sbol3.Model):
# Priority: 3
model2 = sbol2.model.Model(uri=model3.identity, source=model3.source,
language=model3.language, framework=model3.framework)
self.doc2.add(model2)
# Map over all other TopLevel properties and extensions not covered by the constructor
self._convert_toplevel(model3, model2)
# seq2 = sbol2.Sequence(seq3.identity, seq3.elements,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this commented out code.

# encoding=encoding2,
# version=self._sbol2_version(seq3))

def visit_participation(self, a: sbol3.Participation):
# Priority: 2
Expand Down Expand Up @@ -266,7 +274,9 @@ def visit_sequence(self, seq3: sbol3.Sequence):
sbol3.SMILES_ENCODING: sbol2.SBOL_ENCODING_SMILES}
encoding2 = encoding_map.get(seq3.encoding, seq3.encoding)
# Make the Sequence object and add it to the document
seq2 = sbol2.Sequence(seq3.identity, seq3.elements, encoding=encoding2, version=self._sbol2_version(seq3))
seq2 = sbol2.Sequence(seq3.identity, seq3.elements,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the line-wrapping standard on the project. Per the setup.py file at the top, this project uses max-line-length = 119.
There should not be any change to this line of code, since it's just a line-wrapping change.

encoding=encoding2,
version=self._sbol2_version(seq3))
self.doc2.addSequence(seq2)
# Map over all other TopLevel properties and extensions not covered by the constructor
self._convert_toplevel(seq3, seq2)
Expand Down Expand Up @@ -546,9 +556,19 @@ def visit_measure(self, a: sbol2.measurement.Measurement):
# Priority: 3
raise NotImplementedError('Conversion of Measure from SBOL2 to SBOL3 not yet implemented')

def visit_model(self, a: sbol2.model.Model):
def visit_model(self, model2: sbol2.model.Model):
# Priority: 3
raise NotImplementedError('Conversion of Model from SBOL2 to SBOL3 not yet implemented')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace violates python conventions.

Suggested change

model3 = sbol3.Model(model2.identity, source=model2.source, language=model2.language,
framework=model2.framework)
self.doc3.add(model3)

# Map over all other TopLevel properties and extensions not covered by the constructor
self._convert_toplevel(model2, model3)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much whitespace here; please use pycodestyle to follow standard python conventions..





def visit_module(self, a: sbol2.Module):
# Priority: 3
Expand Down
11 changes: 11 additions & 0 deletions test/test_files/example_model_sbol2.xml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file isn't currently used. Please either incorporate into a test or delete.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sbol="http://sbols.org/v2#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:prov="http://www.w3.org/ns/prov#" xmlns:om="http://www.ontology-of-units-of-measure.org/resource/om-2/">
<sbol:Model rdf:about="http://www.sbolstandard.org/examples/pIKE_Toggle_1">
<sbol:persistentIdentity rdf:resource="http://www.sbolstandard.org/examples/pIKE_Toggle_1"/>
<sbol:displayId>pIKE_Toggle_1</sbol:displayId>
<dcterms:title>pIKE_Toggle_1 toggle switch</dcterms:title>
<sbol:source rdf:resource="http://virtualparts.org/part/pIKE_Toggle_1"/>
<sbol:language rdf:resource="http://identifiers.org/edam/format_2585"/>
<sbol:framework rdf:resource="http://identifiers.org/biomodels.sbo/SBO:0000062"/>
</sbol:Model>
</rdf:RDF>
12 changes: 12 additions & 0 deletions test/test_files/example_model_sbol2_tmp.xml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file isn't currently used. Please either incorporate into a test or delete.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<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#" xmlns:ns0="http://www.sbolstandard.org/examples/">
<ns0:pIKE_Toggle_1 rdf:about="example">
<sbol:language rdf:resource="http://identifiers.org/edam/format_2585"/>
<dcterms:title>pIKE_Toggle_1 toggle switch</dcterms:title>
<sbol:source rdf:resource="http://virtualparts.org/part/pIKE_Toggle_1"/>
<backport:sbol3namespace rdf:resource="http://www.sbolstandard.org/examples"/>
<sbol:displayId>pIKE_Toggle_1</sbol:displayId>
<sbol:persistentIdentity rdf:resource="example"/>
<sbol:version>1</sbol:version>
<sbol:framework rdf:resource="http://identifiers.org/biomodels.sbo/SBO:0000062"/>
</ns0:pIKE_Toggle_1>
</rdf:RDF>
12 changes: 12 additions & 0 deletions test/test_files/example_model_sbol2_tmp_2.xml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file isn't currently used. Please either incorporate into a test or delete.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<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:Model rdf:about="http://www.sbolstandard.org/examples/pIKE_Toggle_1">
<sbol:displayId>pIKE_Toggle_1</sbol:displayId>
<sbol:version>1</sbol:version>
<sbol:language rdf:resource="http://identifiers.org/edam/format_2585"/>
<sbol:persistentIdentity rdf:resource="http://www.sbolstandard.org/examples/pIKE_Toggle_1"/>
<backport:sbol3namespace rdf:resource="http://www.sbolstandard.org/examples"/>
<sbol:source rdf:resource="http://virtualparts.org/part/pIKE_Toggle_1"/>
<dcterms:title>pIKE_Toggle_1 toggle switch</dcterms:title>
<sbol:framework rdf:resource="http://identifiers.org/biomodels.sbo/SBO:0000062"/>
</sbol:Model>
</rdf:RDF>
10 changes: 10 additions & 0 deletions test/test_files/example_model_sbol3.ttl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file isn't currently used. Please either incorporate into a test or delete.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@prefix sbol: <http://sbols.org/v3#> .
@prefix ex: <http://example.org/> .
@prefix edam: <http://identifiers.org/edam/> .
@prefix sbo: <http://identifiers.org/biomodels.sbo/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:mymodel a sbol:Model ;
sbol:source <https://models.example.org/my_model.sbml> ;
sbol:language edam:format_2585 ;
sbol:framework sbo:SBO:0000064 .
10 changes: 10 additions & 0 deletions test/test_files/generated_model_sbol3.ttl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to sorted N-triples format, in order to allow direct file-to-file comparison around the loop.
Sorted N-triples guarantees consistent serialization, but TTL does not.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@prefix sbol: <http://sbols.org/v3#> .

<http://www.sbolstandard.org/examples/pIKE_Toggle_1> a sbol:Model ;
sbol:displayId "pIKE_Toggle_1" ;
sbol:framework <http://identifiers.org/biomodels.sbo/SBO:0000062> ;
sbol:hasNamespace <http://www.sbolstandard.org/examples> ;
sbol:language <http://identifiers.org/edam/format_2585> ;
sbol:name "pIKE_Toggle_1 toggle switch" ;
sbol:source <http://virtualparts.org/part/pIKE_Toggle_1> .

45 changes: 33 additions & 12 deletions test/test_sbol2_sbol3_direct.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from sbol2 import Config, ConfigOptions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sbol2 is already imported below; please use references as sbol2.Config, etc., rather than importing.
Also, import is out of order.


import tempfile
from pathlib import Path


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Whitespace goes against python style.

import unittest

import sbol2
Expand All @@ -17,7 +20,7 @@ class TestDirectSBOL2SBOL3Conversion(unittest.TestCase):
# TODO: turn on validation

# J23101.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
'''

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add extra whitespace against python style; use pycodestyle to check your code.

def test_3to2_conversion(self):
"""Test ability to convert a simple part from SBOL3 to SBOL2"""
# Load an SBOL3 document and check its contents
Expand All @@ -36,10 +39,10 @@ def test_3to2_conversion(self):
tmp3 = Path(tmpdir) / 'doc3_loop.nt'
doc3_loop.write(tmp3)
self.assertFalse(file_diff(str(tmp3), str(TEST_FILES / 'BBa_J23101_patched.nt')))
'''


# J23101.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
'''

def test_2to3_conversion(self):
"""Test ability to convert a simple part from SBOL2 to SBOL3"""
# Load an SBOL2 document and check its contents
Expand All @@ -58,10 +61,10 @@ def test_2to3_conversion(self):
tmp2 = Path(tmpdir) / 'doc2_loop.xml'
doc2_loop.write(tmp2)
self.assertFalse(file_diff(str(tmp2), str(TEST_FILES / 'BBa_J23101.xml')))
'''


# sbol_3to2_implementation.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
'''

def test_3to2_implementation_conversion(self):
"""Test ability to convert an implementation from SBOL3 to SBOL2"""
# Load an SBOL3 document and check its contents
Expand All @@ -80,10 +83,10 @@ def test_3to2_implementation_conversion(self):
tmp3 = Path(tmpdir) / 'doc3_loop.nt'
doc3_loop.write(tmp3)
self.assertFalse(file_diff(str(tmp3), str(TEST_FILES / 'sbol3_implementation.nt')))
'''


# sbol_3to2_implementation.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
'''

def test_2to3_implementation_conversion(self):
"""Test ability to convert an implementation from SBOL2 to SBOL3"""
# Load an SBOL2 document and check its contents
Expand All @@ -102,10 +105,10 @@ def test_2to3_implementation_conversion(self):
tmp2 = Path(tmpdir) / 'doc2_loop.xml'
doc2_loop.write(tmp2)
self.assertFalse(file_diff(str(tmp2), str(TEST_FILES / 'sbol_3to2_implementation.xml')))
'''


# sbol_3to2_collection.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
'''

# sbol_3to2_collection.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
def test_3to2_collection_conversion(self):
"""Test ability to convert a collection from SBOL3 to SBOL2"""
Expand All @@ -125,10 +128,10 @@ def test_3to2_collection_conversion(self):
tmp3 = Path(tmpdir) / 'doc3_loop.nt'
doc3_loop.write(tmp3)
self.assertFalse(file_diff(str(tmp3), str(TEST_FILES / 'sbol3_collection.nt')))
'''


# sbol_3to2_collection.xml is not SBOL compliant. Leaving conversions involving it for after the compliant converter is done
'''

def test_2to3_collection_conversion(self):
"""Test ability to convert a collection from SBOL2 to SBOL3"""
# Load an SBOL2 document and check its contents
Expand All @@ -147,7 +150,25 @@ def test_2to3_collection_conversion(self):
tmp2 = Path(tmpdir) / 'doc2_loop.xml'
doc2_loop.write(tmp2)
self.assertFalse(file_diff(str(tmp2), str(TEST_FILES / 'sbol_3to2_collection.xml')))
'''

def test_2to3_and_3to2_model_conversion(self):
"""Test ability to convert a model from SBOL2 to SBOL3"""
# Load an SBOL2 document and check its contents
Config.setOption(ConfigOptions.SBOL_COMPLIANT_URIS, False)
Config.setOption(ConfigOptions.SBOL_TYPED_URIS, False)
doc2 = sbol2.Document()
doc2.read(TEST_FILES / 'example_model_sbol2.xml')
model2 = doc2.models[0]
doc3 = convert2to3(doc2, use_native_converter=True)
self.assertEqual(len(doc3.validate()), 0)
doc3.write(TEST_FILES / 'generated_model_sbol3.ttl',file_format='turtle')
doc2_loop = convert3to2(doc3, True)
model2_loop = doc2_loop.models[0]
self.assertEqual(model2.language, model2_loop.language)
self.assertEqual(model2.framework, model2_loop.framework)
self.assertEqual(model2.source, model2_loop.source)
self.assertEqual(model2.name, model2_loop.name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comparisons are necessary, but not sufficient.
Please also validate:

self.assertFalse(file_diff(str(Path(tmpdir) / 'doc2_loop.xml'), str(TEST_FILES / 'example_model_sbol2.xml')))



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