Nikhil Gopal
Nikhil Gopal

Reputation: 43

How do you split the URI of a SPARQL query result into pieces?

I'm new to semantic web programming and I'm trying to do something with a SPARQL query I'm not entirely sure is possible. However, I thought I'd ask the gurus before trying another approach.

I have a SPARQL query which returns a URI resource as one of the columns. Example below:

http://purl.uniprot.org/uniprot/Q9UKM9

I'd like to take the last portion of that URL (Q9UKM9) and put it into a variable to use in a regex command to find that same protein (identified by that number I'm extracting) in another resource.

I can't just do a straight query like:

?random_resource_one X:propertyOne ?what_I_am_interested_in .
?random_resource_two Y:hasURI ?what_I_am_interested_in . 

because the resource URIs are different:

http://purl.uniprot.org/interpro/IPR000504

vs

http://purl.org/obo/owl/InterPro#InterPro_IPR000504

I'm totally open to ideas! Thanks!

Upvotes: 4

Views: 3636

Answers (2)

Jeen Broekstra
Jeen Broekstra

Reputation: 22052

You can use SPARQL's STRAFTER function for this:

 STRAFTER("http:xx//purl.uniprot.org/uniprot/Q9UKM9", "http:xx//purl.uniprot.org/uniprot/")

will return "Q9UKM9".

As a further tip, you can reuse namespace prefixes as a shorthand. So assuming you have this in your query:

PREFIX uniprot: <http:xx//purl.uniprot.org/uniprot/>

you can do this:

 STRAFTER("http:xx//purl.uniprot.org/uniprot/Q9UKM9", str(uniprot:))

Upvotes: 5

Alfredo Serafini
Alfredo Serafini

Reputation: 194

there is a substr(...) function in sparql 1.1, but it needs an index as a second operator, so i suppose it's not so useful in your case. http://www.w3.org/TR/sparql11-query/#func-substr

I suggest to use instead the replace function http://www.w3.org/TR/sparql11-query/#func-replace

with something like: replace("http://xxpurl.uniprot.org/interpro/IPR000504 ","http://xxpurl.uniprot.org/interpro/", "") in order to obtain only the id, then test it with something more or less like this (just to give you an idea):

SELECT DISTINCT *
WHERE {

    ?uri_1 a ?type_1 .
    ?uri_2 a ?type_2 .

    FILTER (replace(str(?uri_1),"http://xxpurl.uniprot.org/interpro/", "") = replace(str(?uri_2),"http://xxpurl.org/obo/owl/InterPro#InterPro_", ""))

}

Let know if that works ;-)

Upvotes: 1

Related Questions