I'm writing some python code to dynamically build some SPARQL query of arbitrary length.
I have a subquery that looks like this:
?topObj a up:Topological_Domain_Annotation;
rdfs:comment '{}';
up:range ?range.
?proteins a up:Protein .
?proteins up:annotation ?otherTop .
?otherTop a up:Topological_Domain_Annotation;
rdfs:comment '{}';
up:range ?otherRange.""",
Where I go in and .format() the {} depending on user specified input. I want a way to stack up multiple of these subqueries that could potentially match to other objects, ie. the rdfs:comment '{}';
line. But of course the variable names that I use to traverse to my desired object are bound in the first subquery.
Would the best solution be:
EDIT:
Here's an example query generated from the above template concatenated together using the random variable approach.
SELECT DISTINCT ?proteins
WHERE {
<http://purl.uniprot.org/uniprot/P04439> up:annotation ?1WGQM.
?1WGQM a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?VLIT1 .
<http://purl.uniprot.org/uniprot/P01911> up:annotation ?FIICT.
?FIICT a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?W89B2 .
<http://purl.uniprot.org/uniprot/P10321> up:annotation ?WU6G3.
?WU6G3 a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?ZSIQ3 .
?proteins a up:Protein .
?proteins up:annotation ?otherTop .
?otherTop a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?OTHERRANGE .
}
LIMIT 10
Just use BNodes instead of generating unique names for these variables, e.g. something along these lines:
SELECT DISTINCT ?proteins
WHERE {
<http://purl.uniprot.org/uniprot/P04439> up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
].
<http://purl.uniprot.org/uniprot/P01911> up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
] .
<http://purl.uniprot.org/uniprot/P10321> up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
].
?proteins a up:Protein .
?proteins up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
].
}
LIMIT 10
HTH