Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 solid diamonds #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion sbol_factory/sbol_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ class SBOLFactory():

# Prefixes are used to automatically generate module names
namespace_to_prefix = {}
for ns, prefix in graph.namespaces():
namespace_to_prefix[str(ns)] = prefix

def __new__(cls, module_name, ontology_path, ontology_namespace, verbose=False):
if verbose is False:
logging.disable('INFO')
SBOLFactory.graph.parse(ontology_path, format=rdflib.util.guess_format(ontology_path))
for prefix, ns in SBOLFactory.graph.namespaces():
SBOLFactory.namespace_to_prefix[str(ns)] = prefix
# Skip default prefixes
if ns in SBOLFactory.namespace_to_prefix:
continue
# TODO: handle namespace with conflicting prefixes
SBOLFactory.namespace_to_prefix[str(ns)] = prefix

# Use ontology prefix as module name
ontology_namespace = ontology_namespace
Expand Down
3 changes: 3 additions & 0 deletions sbol_factory/uml_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def generate(self, output_path):
class_name = sbol.utils.parse_class_name(class_uri)
dot = graphviz.Digraph(class_name)
# dot.graph_attr['splines'] = 'ortho'
dot.graph_attr['dpi'] = '300'

superclass_uri = self.query.query_superclass(class_uri)
create_inheritance(dot, superclass_uri, class_uri)
Expand All @@ -54,6 +55,8 @@ def generate(self, output_path):
source = graphviz.Source(dot_source_sanitized)
outfile = f'{class_name}_abstraction_hierarchy'
source.render(os.path.join(output_path, outfile))
source = graphviz.Source(dot_source_sanitized, format='png')
source.render(os.path.join(output_path, outfile))
outfile += '.pdf'
width = 470 # default \textwidth of LaTeX document
with open(os.path.join(output_path, outfile), 'rb') as pdf:
Expand Down
8 changes: 8 additions & 0 deletions test/test_files/Activity_abstraction_hierarchy.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
digraph Activity {
graph [dpi=300]
prov_Activity -> uml_Activity [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
prov_Activity [label="{prov:Activity|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
uml_Activity [label="{uml:Activity|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
uml_Activity -> paml_BehaviorExecution [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
paml_BehaviorExecution [label="{paml:BehaviorExecution|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
}
16 changes: 16 additions & 0 deletions test/test_files/Base_abstraction_hierarchy.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
digraph Base {
graph [dpi=300]
sbol_TopLevel -> test_Base [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
sbol_TopLevel [label="{sbol:TopLevel|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
test_Base [label="{test:Base|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
test_Base -> test_Derived1 [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
test_Derived1 [label="{test:Derived1|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
test_Base -> test_Derived2 [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
test_Derived2 [label="{test:Derived2|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
test_Derived2 -> test_Child [arrowhead=vee arrowtail=diamond dir=both fontname="Bitstream Vera Sans" fontsize=8 xlabel="test:hasChild [0..*]"]
test_Child [label="{test:Child|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
test_Child -> test_ChildDerived1 [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
test_ChildDerived1 [label="{test:ChildDerived1|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
test_Child -> test_ChildDerived2 [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
test_ChildDerived2 [label="{test:ChildDerived2|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
}
32 changes: 32 additions & 0 deletions test/test_files/test-minimal-ontology.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@prefix om: <http://www.ontology-of-units-of-measure.org/resource/om-2/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sbol: <http://sbols.org/v3#> .
@prefix test: <http://bioprotocols.org/test#> .
@base <http://bioprotocols.org/test#> .

test:Base rdf:type owl:Class ;
rdfs:subClassOf sbol:TopLevel .

test:Derived1 rdf:type owl:Class ;
rdfs:subClassOf test:Base .

test:Derived2 rdf:type owl:Class ;
rdfs:subClassOf test:Base .

test:Child rdf:type owl:Class ;
rdfs:subClassOf sbol:Identified .

test:ChildDerived1 rdf:type owl:Class ;
rdfs:subClassOf test:Child .

test:ChildDerived2 rdf:type owl:Class ;
rdfs:subClassOf test:Child .

test:hasChild rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf sbol:directlyComprises ;
rdfs:label "has_child" ;
rdfs:domain test:Derived2 ;
rdfs:range test:Child .
25 changes: 25 additions & 0 deletions test/test_files/test-uml.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@prefix paml: <http://bioprotocols.org/paml#> .
@prefix opil: <http://bioprotocols.org/opil/v1#> .
@prefix uml: <http://bioprotocols.org/uml#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sbol: <http://sbols.org/v3#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@base <http://bioprotocols.org/paml#> .

paml:SubClass rdf:type owl:Class ;
rdfs:subClassOf paml:SuperClass .

paml:SuperClass rdf:type owl:Class ;
rdfs:subClassOf sbol:TopLevel .

paml:Child rdf:type owl:Class ;
rdfs:subClassOf sbol:Identified .

paml:child rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf opil:compositionalProperty ;
rdfs:domain paml:SubClass ;
rdfs:range paml:Child ;
rdfs:label "child" .
58 changes: 39 additions & 19 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,59 @@ def tearDown(self):
SBOLFactory.clear()

def test_ontology_to_module(self):
self.assertTrue(check_namespaces())
SBOLFactory('uml',
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_files/test-modules.ttl'),
'http://bioprotocols.org/uml#')
self.assertTrue('uml' in sys.modules)
self.assertTrue(check_namespaces())
SBOLFactory('paml',
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_files/test-modules.ttl'),
'http://bioprotocols.org/paml#')
self.assertTrue('paml' in sys.modules)
self.assertTrue(check_namespaces())
import uml
import paml
uml.Activity('http://test.org/umlact')
paml.BehaviorExecution('http://test.org/BX')

# def test_figure_generation(self):
# path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_files/test-modules.ttl')
# SBOLFactory('uml', path,'http://bioprotocols.org/uml#')
# figure_maker = UMLFactory(path,'http://bioprotocols.org/uml#')
# # TODO: check whether generated figure is correct
# tmp = tempfile.mkdtemp()
# print(f'Exporting test figures into {tmp}')
# figure_maker.generate(tmp)
# dot_source_actual = ''
# with open(os.path.join(tmp, 'Activity_abstraction_hierarchy')) as dot_file:
# dot_source_actual = dot_file.read()
# dot_source_expected = '''digraph Activity {
# prov_Activity -> uml_Activity [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
# prov_Activity [label="{prov:Activity|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
# uml_Activity [label="{uml:Activity|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
# uml_Activity -> paml_BehaviorExecution [arrowtail=empty dir=back fontname="Bitstream Vera Sans" fontsize=8]
# paml_BehaviorExecution [label="{paml:BehaviorExecution|}" fontname="Bitstream Vera Sans" fontsize=8 shape=record]
#}\n'''
# self.assertEqual(dot_source_actual, dot_source_expected)
def test_figure_generation(self):
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_files')
SBOLFactory('uml', os.path.join(path, 'test-modules.ttl'), 'http://bioprotocols.org/uml#')
figure_maker = UMLFactory(os.path.join(path, 'test-modules.ttl'), 'http://bioprotocols.org/uml#')
# TODO: check whether generated figure is correct
tmp = tempfile.mkdtemp()
print(f'Exporting test figures into {tmp}')
figure_maker.generate(tmp)
dot_source_actual = ''
with open(os.path.join(tmp, 'Activity_abstraction_hierarchy')) as dot_file:
dot_source_actual = dot_file.read().replace(' ', '')
with open(os.path.join(path, 'Activity_abstraction_hierarchy.dot')) as dot_file:
dot_source_expected = dot_file.read().replace(' ', '')
self.assertEqual(dot_source_actual, dot_source_expected)

def test_figure_generation2(self):
# This figure includes compositional properties
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_files')
SBOLFactory('minimal', os.path.join(path, 'test-minimal-ontology.ttl'), 'http://bioprotocols.org/test#')
figure_maker = UMLFactory(os.path.join(path, 'test-minimal-ontology.ttl'), 'http://bioprotocols.org/test#')
# TODO: check whether generated figure is correct
tmp = tempfile.mkdtemp()
print(f'Exporting test figures into {tmp}')
figure_maker.generate(tmp)
dot_source_actual = ''
with open(os.path.join(tmp, 'Base_abstraction_hierarchy')) as dot_file:
dot_source_actual = dot_file.read().replace(' ', '')
with open(os.path.join(path, 'Base_abstraction_hierarchy.dot')) as dot_file:
dot_source_expected = dot_file.read().replace(' ', '')
self.assertEqual(dot_source_actual, dot_source_expected)

def check_namespaces():
prefixes = [p for p, ns in SBOLFactory.graph.namespaces()]
if 'default1' in prefixes:
print(prefixes)
return False
return True

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