3

I am in a learning phase of SPARQL. I am working with a Turtle file to extract some information. The condition is: if the exact synonym has a substring 'stroke' or 'Stroke', the query should return all the synonyms and rdfs:label.

I am using below query but getting no output:

prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
prefix obo: <http://purl.obolibrary.org/obo/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
               
Select * where { 
  ?s ?p ?o . 
  rdfs:label <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> "stroke"^^xsd:string
}

Below is the sample Turtle file:

###  https://ontology.aaaa.com/aaaa/meddra_10008196

:meddra_10008196 
  rdf:type owl:Class ;
  <http://www.geneontology.org/formats/oboInOwl#hasDbXref> "DOID:6713" , "EFO:0000712" , "EFO:0003763" , "HE:A10008190" ;
  <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> 
    "(cva) cerebrovascular accident" , 
    "Acute Cerebrovascular Accident" , 
    "Acute Cerebrovascular Accidents" , 
    "Acute Stroke" , 
    "Acute Strokes" ;
  rdfs:label "Cerebrovascular disorder"@en ;
  :hasSocs "Nervous system disorders [meddra:10029205]" , "Vascular disorders [meddra:10047065]" ;
  :uid "6e46da69b727e4e924c31027cdf47b8a" .

I am expecting this output:

(cva) cerebrovascular accident
Acute Cerebrovascular Accident
Acute Cerebrovascular Accidents
Acute Stroke
Acute Strokes
Cerebrovascular disorder
rshar
  • 1,381
  • 10
  • 28

1 Answers1

1

With this triple pattern, you are querying for rdfs:label as subject, not as predicate:

rdfs:label <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> "stroke"^^xsd:string

What you are asking with this is: "Does the resource rdfs:label have the property oboInOwl:hasExactSynonym with the string value 'stroke'?"

But you want to ask this about the class (e.g., :meddra_10008196), not rdfs:label:

?class oboInOwl:hasExactSynonym "stroke" .

Finding matches

As you don’t want to find only exact string matches, you can use CONTAINS:

?class oboInOwl:hasExactSynonym ?matchingSynonym .
FILTER( CONTAINS(?matchingSynonym, "stroke") ) .

As you want to ignore case, you can query lower-cased synonyms with LCASE:

?class oboInOwl:hasExactSynonym ?matchingSynonym .
FILTER( CONTAINS(LCASE(?matchingSynonym), "stroke") ) .

Displaying results

To display the label and all synonyms in the same column, you could use a property path with | (AlternativePath):

?class rdfs:label|oboInOwl:hasExactSynonym ?labelOrSynonym .

Full query

# [prefixes]

SELECT ?class ?labelOrSynonym
WHERE {

  ?class rdfs:label|oboInOwl:hasExactSynonym ?labelOrSynonym .

  FILTER EXISTS {
    ?class oboInOwl:hasExactSynonym ?matchingSynonym .
    FILTER( CONTAINS(LCASE(?matchingSynonym), "stroke") ) .
  }

}
  • Well explained. I wish I could upvote your answer few more times. Thank you so much! Is it possible to extract the terms instead of URI? – rshar Oct 19 '22 at 06:40
  • Does the above query provide only the synonym with 'stroke' or other associated synonyms with stroke as well (with NO Stroke mentioned as shown in the expected output) – rshar Oct 19 '22 at 08:19
  • 1
    @rshar **1)** What do you mean with "terms instead of URI"? The query outputs a URI in the first column (= the URI of the class), and a string in the second column (= a synonym or a label). **2)** The expected output, according to your question, includes *all* synonyms (no matter what they are) and the label, right? So my query does that, too: as soon as a class matches (i.e., it has at least one synonym that contains "stroke"), it displays every synonym of this class + the label. – Stefan - brox IT-Solutions Oct 19 '22 at 08:29