Skip to content

Commit

Permalink
Merge pull request #275 from tcmitchell/8-read-write
Browse files Browse the repository at this point in the history
Speed up document loading
  • Loading branch information
tcmitchell authored Jun 4, 2020
2 parents 5350e24 + 5f1a4dc commit 267bc83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
37 changes: 11 additions & 26 deletions sbol2/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,6 @@ def parse_all(self):
# Instantiate all objects with an RDF type
for s, _, o in self.graph.triples((None, rdflib.RDF.type, None)):
self.parse_objects_inner(s, o)
# Find everything in the triple store
all_query = "PREFIX : <http://example.org/ns#> " \
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " \
"PREFIX sbol: <http://sbols.org/v2#> " \
"SELECT ?s ?p ?o " \
"{ ?s ?p ?o }"
all_results = self.graph.query(all_query)
# Find the graph base uri. This is the location of the sbol
# file, and begins with the "file://" scheme. Any URI in the
# file without a scheme will appear relative to this URI, after
Expand All @@ -488,18 +481,16 @@ def parse_all(self):
pos = graphBaseURIStr.rfind('/')
if pos != -1:
pos += 1
rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
for result in all_results:
rdf_type = rdflib.RDF.type
for result_s, result_p, result_o in self.graph:
# Look for properties
if str(result.p) != rdf_type:
obj = result.o
lval = str(obj)
if isinstance(result.o, URIRef) and pos != -1:
if lval[:pos] == graphBaseURIStr:
if result_p != rdf_type:
obj = result_o
if isinstance(result_o, URIRef) and pos != -1:
if obj[:pos] == graphBaseURIStr:
# This was a URI without a scheme. Remove URI base
lval = lval[pos:]
obj = URIRef(lval)
self.parse_properties_inner(result.s, result.p, obj)
obj = URIRef(obj[pos:])
self.parse_properties_inner(result_s, result_p, obj)

# Remove objects from SBOLObjects if they are not TopLevel AND
# they have a parent object.
Expand Down Expand Up @@ -541,8 +532,7 @@ def parse_objects_inner(self, subject, obj):
values.clear()
new_obj.identity = subject
# Update document
identity_uri = rdflib.URIRef(new_obj.identity)
self.SBOLObjects[identity_uri] = new_obj
self.SBOLObjects[new_obj.identity] = new_obj
new_obj.doc = self
# For now, set the parent to the Document.
# This may get overwritten later for child objects.
Expand All @@ -557,8 +547,7 @@ def parse_objects_inner(self, subject, obj):
new_obj = SBOLObject()
new_obj.identity = subject
new_obj.rdf_type = obj
identity_uri = rdflib.URIRef(new_obj.identity)
self.SBOLObjects[identity_uri] = new_obj
self.SBOLObjects[new_obj.identity] = new_obj
new_obj.doc = self

def parse_properties_inner(self, subject, predicate, obj):
Expand All @@ -584,11 +573,7 @@ def parse_properties_inner(self, subject, predicate, obj):
# del self.SBOLObjects[obj]
else:
# Extension data
if predicate not in parent.properties:
parent.properties[predicate] = []
parent.properties[predicate].append(obj)
else:
parent.properties[predicate].append(obj)
parent.properties[predicate] = [obj]
else:
msg = 'Subject {} ({}) not found in my SBOLObjects'
msg = msg.format(subject, type(subject))
Expand Down
9 changes: 3 additions & 6 deletions sbol2/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,11 @@ def __getattribute__(self, name):
# Call the default method
result = object.__getattribute__(self, name)
if isinstance(result, OwnedObject):
sbol_property = object.__getattribute__(self, name)
if sbol_property.upper_bound == 1:
if len(sbol_property):
result = sbol_property[0]
if result.getUpperBound() == '1':
if result:
result = result[0]
else:
result = None
else:
result = sbol_property
elif isinstance(result, Property):
# Else if attribute is any other kind of Property besides
# OwnedObject, convert so the Property attributes are
Expand Down

0 comments on commit 267bc83

Please sign in to comment.