problem using Uniprot SPARQL endpoint and CONSTRUCT - sparql

From SPARQLwrapper I can successfully query and return results using SPARQL select. When I try to use the CONSTRUCT example from the website I get and error "ExpatError: no element found: line 1, column 0"
I've tested my code with a dbpedia example
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX sdo: <https://schema.org/>
CONSTRUCT {
?lang a sdo:Language ;
sdo:alternateName ?iso6391Code .
}
WHERE {
?lang a dbo:Language ;
dbo:iso6391Code ?iso6391Code .
FILTER (STRLEN(?iso6391Code)=2) # to filter out non-valid values
}
LIMIT 3
""")
results = sparql.queryAndConvert()
print("results")
print(results.serialize())
print("- - - - - - - - -")
the DBpedia example works fine.But, when trying the same on Uniprot like this ...
sparql = SPARQLWrapper("http://sparql.uniprot.org/sparql")
sparql.setQuery("""
PREFIX taxon: <http://purl.uniprot.org/taxonomy/>
PREFIX up: <http://purl.uniprot.org/core/>
CONSTRUCT
{
?protein a up:HumanProtein .
}
WHERE
{
?protein a up:Protein .
?protein up:organism taxon:9606 .
}
""")
results = sparql.queryAndConvert()
I get this error
"ExpatError: no element found: line 1, column 0"

Could you try going directly to https://sparql.uniprot.org as this might be the issue.

Related

Access subclassOf/subclassOf level using ForwardChainingRDFSInferencer with SPARQL

I am running some SPARQL queries using the class ForwardChainingRDFSInferencer, which basic constructs an inferencer. For my examples I use the schema.org ontology.
My code looks like the following example
MemoryStore store = new MemoryStore();
ForwardChainingRDFSInferencer inferencer = new ForwardChainingRDFSInferencer(store); //the inference class
Repository repo = new SailRepository(inferencer);
repo.initialize();
URL data = new URL("file:/home/user/Documents/schemaorg-current-https.nt");
RDFFormat fileRDFFormat = RDFFormat.NTRIPLES;
RepositoryConnection con = repo.getConnection();
con.add(data, null, fileRDFFormat);
System.out.println("Repository loaded");
String queryString =
" PREFIX schema: <https://schema.org/>" +
" SELECT DISTINCT ?subclassName_one" +
" WHERE { " +
" ?type rdf:type rdfs:Class ." +
" ?type rdfs:subClassOf/rdfs:subClassOf? schema:Thing ." +
" ?type rdfs:label ?subclassName_one ." +
" }";
TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = tupleQuery.evaluate();
while (result.hasNext()) {
BindingSet bindingSet = result.next();
System.out.println(bindingSet.toString());
stdout.println(bindingSet.toString());
}
repo.close()
What I want is to print two subclass levels down the class Thing. So if for example we have,
Thing > Action (sub-class 1) > ConsumeAction (sub-class 2) > DrinkAction
What I want is to return the class CosumeAction which is two levels (subclasses) down from class Thing, while using the inference java class.
The current query, as given in the code sample above, returns all the classes and subclasses from every class of the schema.org ontology. Thus, there is something that I do wrong while using the inference class.
You could remove the classes you are not interested in with FILTER NOT EXISTS:
FILTER NOT EXISTS {
?type rdfs:subClassOf+/rdfs:subClassOf/rdfs:subClassOf schema:Thing .
}
Examples
For DrinkAction (level 3):
FILTER NOT EXISTS {
?type
rdfs:subClassOf+ / # schema:ConsumeAction
rdfs:subClassOf / # schema:Action
rdfs:subClassOf schema:Thing .
}
For WearAction (level 4):
FILTER NOT EXISTS {
?type
rdfs:subClassOf+ / # schema:UseAction / schema:ConsumeAction
rdfs:subClassOf / # schema:Action
rdfs:subClassOf schema:Thing .
}

SPARQL FILTER() using Jena not working properly

i'm running Ontology project on Netbeans using Jena for SPARQL (version 2.6.2 ) Queries, and now i'm trying to FILTERS for Temperature , this code doesn't work as expected and return no result
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
SELECT ?StarName ?Temperature
WHERE {
?star a ns:Star ;
ns:possessesSpectralType ?SpectralType ;
ns:possessesStarName ?StarName ;
ns:possessesTemperature ?Temperature .
FILTER (?Temperature > 10 ).
}
on the other hand i've tried this code and it works but only with Equal operator
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
SELECT ?star
WHERE {
?star a ns:Star ;
ns:possessesSpectralType ?SpectralType ;
ns:possessesStarName ?StarName ;
ns:possessesTemperature ?Temperature .
FILTER (?Temperature = ns:168 )
}

I want to add data to rdf in Sparql

I'm trying to update some data using SPARQL. Here's the what the original data looks like:
<http://dbpedia.org/resource/Switzerland#1 >
   <http://www.w3.org/2000/01/rdf-schema#label > "1"#ja ;
   <http://linkdata.org/property/rdf1s2307i#num > "1"#ja ;
   <http://learningsparql.com/ns/addressbook#code > "1234"#ja .
Here's what I want the data to look like after the update:
<http://dbpedia.org/resource/Switzerland#1 >
   <http://www.w3.org/2000/01/rdf-schema#label > "1"#ja ;
   <http://linkdata.org/property/rdf1s2307i#num > "1"#ja ;
   <http://learningsparql.com/ns/addressbook#code > "1234"#ja ;
   <http://learningsparql.com/ns/addressbook#name > "taro"#ja .
I've got a query that I'm trying to use, but I end up getting an error back. Here's the query and the error that I get:
PREFIX schema: <http://www.w3.org/2000/01/rdf-schema# >
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos# >
PREFIX ab: <http://learningsparql.com/ns/addressbook# >
PREFIX res: <http://www.w3.org/2000/01/rdf-schema# >
INSERT DATA{
GRAPH <http://ddd.test/addressbook> { ab:name "taro" . }
}
SQLState: 22023 Message: SR007: Function exec_metadata needs a string
or NULL as argument 1, not an arg of type ARRAY_OF_POINTER (193)
Couple of things:
Don't leave any spaces at the end of your Prefix URIs.
When using INSERT DATA make sure your statements are in triples. You were using only a predicate and an object.
The above may have been picked up if your had been passing your query through a SPARQL validator before executing.
Try this:
PREFIX schema: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX ab: <http://learningsparql.com/ns/addressbook#>
PREFIX res: <http://www.w3.org/2000/01/rdf-schema#>
INSERT DATA{
GRAPH <http://ddd.test/addressbook> {
<http://dbpedia.org/resource/Switzerland#1> ab:name "taro" .
}
}

Searching semantically tagged documents in MarkLogic

Can any one please point me to some simple examples of semantic tagging and querying semantically tagged documents in MarkLogic?
I am fairly new in this area,so some beginner level examples will do.
When you say "semantically tagged" do you mean regular XML documents that happen to have some triples in them? The discussion and examples at http://docs.marklogic.com/guide/semantics/embedded are pretty good for that.
Start by enabling the triple index in your database. Then insert a test doc. This is just XML, but the sem:triple element represents a semantic fact.
xdmp:document-insert(
'test.xml',
<test>
<source>AP Newswire</source>
<sem:triple date="1972-02-21" confidence="100">
<sem:subject>http://example.org/news/Nixon</sem:subject>
<sem:predicate>http://example.org/wentTo</sem:predicate>
<sem:object>China</sem:object>
</sem:triple>
</test>)
Then query it. The example query is pretty complicated. To understand what's going on I'd insert variations on that sample document, using different URIs instead of just test.xml, and see how the various query terms match up. Try using just the SPARQL component, without the extra cts query. Try cts:search with no SPARQL, just the cts:query.
xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
sem:sparql('
SELECT ?country
WHERE {
<http://example.org/news/Nixon> <http://example.org/wentTo> ?country
}
',
(),
(),
cts:and-query((
cts:path-range-query( "//sem:triple/#confidence", ">", 80) ,
cts:path-range-query( "//sem:triple/#date", "<", xs:date("1974-01-01")),
cts:or-query((
cts:element-value-query( xs:QName("source"), "AP Newswire"),
cts:element-value-query( xs:QName("source"), "BBC"))))))
In case you are talking about enriching your content using semantic technology, that is not directly provided by MarkLogic.
You can enrich your content externally, for instance by calling a public service like the one provided by OpenCalais, and then insert the enrichments to the content before insert.
You can also build lists of lookup values, and then using cts:highlight to mark such terms within your content. That could be as simple as:
let $labels := ("MarkLogic", "StackOverflow")
return
cts:highlight($doc, cts:word-query($labels), <b>{$cts:text}</b>)
Or with a more dynamic replacement using spraql:
let $labels := map:new()
let $_ :=
for $result in sem:sparql('
PREFIX demo: <http://www.marklogic.com/ontologies/demo#>
SELECT DISTINCT ?label
WHERE {
?s a demo:person.
{
?s demo:fullName ?label
} UNION {
?s demo:initialsName ?label
} UNION {
?s demo:email ?label
}
}
')
return
map:put($labels, map:get($result, 'label'), 'person')
return
cts:highlight($doc, cts:word-query(map:keys($labels)),
let $result := sem:sparql(concat('
PREFIX demo: <http://www.marklogic.com/ontologies/demo#>
SELECT DISTINCT ?s ?p
{
?s a demo:', map:get($labels, $cts:text), ' .
?s ?p "', $cts:text, '" .
}
'))
return
if (map:contains($labels, $cts:text))
then
element { xs:QName(fn:concat("demo:", map:get($labels, $cts:text))) } {
attribute subject { map:get($result, 's') },
attribute predicate { map:get($result, 'p') },
$cts:text
}
else ()
)
HTH!

How to solve SPARQL warning?

I am trying to retrieve the name of inkers of Comic books. I am trying to build an ontology. Inkers has dbpprop and I have imported rdlib and sparqlWrapper whilst I am having following error. Is there any one who understand this problem?
Abcde-MacBook-Pro:example Abcde$ python basicTest.py
WARNING:rdflib.term: does not look like a valid URI, trying to serialize this will break.
Abcde-MacBook-Pro:example Abcde$ python basicTest.py
Traceback (most recent call last):
File "basicTest.py", line 78, in <module>
g = sparql.query().convert()
File "build/bdist.macosx-10.10-intel/egg/SPARQLWrapper/Wrapper.py", line 535, in query
File "build/bdist.macosx-10.10-intel/egg/SPARQLWrapper/Wrapper.py", line 513, in _query
SPARQLWrapper.SPARQLExceptions.EndPointInternalError: EndPointInternalError: endpoint returned code 500 and response.
Response:
Virtuoso RDF01 Error Bad variable value in CONSTRUCT: "Malcolm Jones III" (tag 246 box flags 0) is not a valid subject, only object of a triple can be a literal
SPARQL query:
define sql:big-data-const 0
#output-format:application/rdf+xml
My code looks like
CONSTRUCT {
?comics ma:inked_by ?inker .
?inker rdf:type ma:Inker .
}
WHERE{
?comics rdf:type dbpedia-owl:Comics .
?comics foaf:name ?name .
OPTIONAL {?comics dbpprop:inkers ?inker}
FILTER regex(str(?name), "Batman")
}"""
I think the problem arises when you get the ?inker out. Sometime it is a URI and sometime it is a string. For example, the following are the top two outputs:
"Malcolm Jones III"
http://dbpedia.org/resource/Vince_Colletta
I think you need to change your code in a way that your inker is either a URI or a string. The following will save the URI in your ontology, if it exists. If you need a string use the ?inkername instead.
CONSTRUCT {
?comics ma:inked_by ?inker.
?inker a ma:Inker.
}
where {
?comics a dbpedia-owl:Comics.
?comics foaf:name ?name .
optional{
?comics dbpprop:inkers ?inker.
?inker foaf:name ?inkername.
}
FILTER regex(str(?name), "Batman")
}