From 2454cf98a1fae9847be1a3658fea94a2d841301f Mon Sep 17 00:00:00 2001 From: Tom Mitchell Date: Mon, 1 Jun 2020 21:25:07 -0400 Subject: [PATCH 1/5] Remove needless SPARQL query --- sbol2/document.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/sbol2/document.py b/sbol2/document.py index f7ce7fe..b47010d 100644 --- a/sbol2/document.py +++ b/sbol2/document.py @@ -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 : " \ - "PREFIX rdf: " \ - "PREFIX sbol: " \ - "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 @@ -489,17 +482,17 @@ def parse_all(self): if pos != -1: pos += 1 rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" - for result in all_results: + for result_s, result_p, result_o in self.graph: # Look for properties - if str(result.p) != rdf_type: - obj = result.o + if str(result_p) != rdf_type: + obj = result_o lval = str(obj) - if isinstance(result.o, URIRef) and pos != -1: + if isinstance(result_o, URIRef) and pos != -1: if lval[: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) + self.parse_properties_inner(result_s, result_p, obj) # Remove objects from SBOLObjects if they are not TopLevel AND # they have a parent object. From 7edabe53ba5397b52d38d63b5e34b8e1b1ba2549 Mon Sep 17 00:00:00 2001 From: Tom Mitchell Date: Tue, 2 Jun 2020 15:09:33 -0400 Subject: [PATCH 2/5] Tighten up property parsing loop --- sbol2/document.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/sbol2/document.py b/sbol2/document.py index b47010d..4335766 100644 --- a/sbol2/document.py +++ b/sbol2/document.py @@ -481,17 +481,15 @@ 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" + rdf_type = rdflib.RDF.type for result_s, result_p, result_o in self.graph: # Look for properties - if str(result_p) != rdf_type: + if result_p != rdf_type: obj = result_o - lval = str(obj) if isinstance(result_o, URIRef) and pos != -1: - if lval[:pos] == graphBaseURIStr: + if obj[:pos] == graphBaseURIStr: # This was a URI without a scheme. Remove URI base - lval = lval[pos:] - obj = URIRef(lval) + obj = URIRef(obj[pos:]) self.parse_properties_inner(result_s, result_p, obj) # Remove objects from SBOLObjects if they are not TopLevel AND From 706507801f00ebc6cb5344c185af5d830bbeef02 Mon Sep 17 00:00:00 2001 From: Tom Mitchell Date: Tue, 2 Jun 2020 17:25:14 -0400 Subject: [PATCH 3/5] Skip needless URIRef construction --- sbol2/document.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sbol2/document.py b/sbol2/document.py index 4335766..76e9823 100644 --- a/sbol2/document.py +++ b/sbol2/document.py @@ -532,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. @@ -548,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): From 505ba7894781a6f71fdde249dd4b4b8341265372 Mon Sep 17 00:00:00 2001 From: Tom Mitchell Date: Tue, 2 Jun 2020 17:25:47 -0400 Subject: [PATCH 4/5] Simplify setting extension data --- sbol2/document.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/sbol2/document.py b/sbol2/document.py index 76e9823..cf0a17b 100644 --- a/sbol2/document.py +++ b/sbol2/document.py @@ -573,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)) From 38bcb8c10207203186fb4a7e1f35cac7ddc66c50 Mon Sep 17 00:00:00 2001 From: Tom Mitchell Date: Tue, 2 Jun 2020 17:26:16 -0400 Subject: [PATCH 5/5] Streamline OwnedObject attribute access --- sbol2/object.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sbol2/object.py b/sbol2/object.py index 85d5167..4343dd2 100644 --- a/sbol2/object.py +++ b/sbol2/object.py @@ -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