sparql-examples

A set of SPARQL examples that are used in different SIB resources

View the Project on GitHub sib-swiss/sparql-examples

006

Search spectrally related features with and within the VGF151_E05 sample of a datura stramonium through counts of common peaks and loss. Return their species, family and genus names

Use at

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX emi: <https://purl.org/emi#>
PREFIX sosa: <http://www.w3.org/ns/sosa/>

SELECT ?family_name ?genus_name ?species_name ?wd_sp ?feature ?sub_rt ?sub_parent_mass ?count
WHERE {
    # Subquery to get the top N hits with their Wikidata IDs
    {
        SELECT ?wd_sp ?feature (SAMPLE(?rt) AS ?sub_rt) (SAMPLE(?parent_mass) AS ?sub_parent_mass) (COUNT(?peakloss) AS ?count)
        WHERE {
            ?material sosa:hasSample ?extract ;
                sosa:isSampleOf* ?organism.
            ?organism emi:inTaxon ?wd_sp .
            ?extract a emi:ExtractSample ;
                sosa:isFeatureOfInterestOf ?lcms .
            ?lcms a emi:LCMSAnalysisPos ;
                sosa:hasResult ?feature_list .
            ?feature_list emi:hasLCMSFeature ?feature .
            ?feature a emi:LCMSFeature ;
                emi:hasSpec2VecDoc ?doc ;
                emi:hasParentMass ?parent_mass ;
                emi:hasRetentionTime ?rt .
            ?doc emi:hasSpec2VecLoss|emi:hasSpec2VecPeak ?peakloss .

            {
                SELECT ?peakloss WHERE {
                    ?lcms a emi:LCMSAnalysisPos ;
                        sosa:hasResult ?feature_list ;
                        sosa:hasFeatureOfInterest ?sample .
                    ?sample dcterms:identifier "VGF151_E05" ;
                        sosa:isSampleOf* ?organism .
                    ?organism emi:inTaxon/rdfs:label "datura stramonium".
                    ?feature_list emi:hasLCMSFeature ?feature .
                    ?feature a emi:LCMSFeature ;
                        emi:hasSpec2VecDoc ?doc .
                    ?doc emi:hasSpec2VecLoss|emi:hasSpec2VecPeak ?peakloss .
                }
            }
        }
        GROUP BY ?wd_sp ?feature
        ORDER BY DESC(?count)
        LIMIT 5
    }

    # Outer query to fetch species names from Wikidata
    # Fetching taxonomic information from Wikidata
    SERVICE <https://query.wikidata.org/sparql> {
        ?wd_sp wdt:P225 ?species_name .
        OPTIONAL {
            ?family wdt:P31 wd:Q16521 ;
                wdt:P105 wd:Q35409 ;
                wdt:P225 ?family_name ;
                ^wdt:P171* ?wd_sp .
        }
        OPTIONAL {
            ?genus wdt:P31 wd:Q16521 ;
                wdt:P105 wd:Q34740 ;
                wdt:P225 ?genus_name ;
                ^wdt:P171* ?wd_sp .
        }
    }
} GROUP BY ?family_name ?genus_name ?species_name ?wd_sp ?feature ?sub_rt ?sub_parent_mass ?count
ORDER BY DESC(?count)
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v16("?count"):::projected 
  v9("?doc")
  v3("?extract")
  v17("?family")
  v18("?family_name"):::projected 
  v8("?feature"):::projected 
  v7("?feature_list")
  v19("?genus")
  v20("?genus_name"):::projected 
  v6("?lcms")
  v2("?material")
  v4("?organism")
  v10("?parent_mass")
  v12("?peakloss")
  v11("?rt")
  v13("?sample")
  v16("?species_name"):::projected 
  v15("?sub_parent_mass"):::projected 
  v14("?sub_rt"):::projected 
  v5("?wd_sp"):::projected 
  a1((" "))
  c26(["wd:Q35409"]):::iri 
  c10(["emi:LCMSFeature"]):::iri 
  c28(["wd:Q34740"]):::iri 
  c18(["VGF151_E05"]):::literal 
  c7(["emi:LCMSAnalysisPos"]):::iri 
  c20(["datura stramonium"]):::literal 
  c5(["emi:ExtractSample"]):::iri 
  c24(["wd:Q16521"]):::iri 
  v2 --"sosa:hasSample"-->  v3
  v2 --"sosa:isSampleOf"-->  v4
  v4 --"emi:inTaxon"-->  v5
  v3 --"a"-->  c5
  v3 --"sosa:isFeatureOfInterestOf"-->  v6
  v6 --"a"-->  c7
  v6 --"sosa:hasResult"-->  v7
  v7 --"emi:hasLCMSFeature"-->  v8
  v8 --"a"-->  c10
  v8 --"emi:hasSpec2VecDoc"-->  v9
  v8 --"emi:hasParentMass"-->  v10
  v8 --"emi:hasRetentionTime"-->  v11
  subgraph union0[" Union "]
  subgraph union0l[" "]
    style union0l fill:#abf,stroke-dasharray: 3 3;
    v9 --"emi:hasSpec2VecPeak"-->  v12
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    v9 --"emi:hasSpec2VecLoss"-->  v12
  end
  union0r <== or ==> union0l
  end
  v6 --"a"-->  c7
  v6 --"sosa:hasResult"-->  v7
  v6 --"sosa:hasFeatureOfInterest"-->  v13
  v13 --"dcterms:identifier"-->  c18
  v13 --"sosa:isSampleOf"-->  v4
  v4 --"emi:inTaxon"-->  a1
  a1 --"rdfs:label"-->  c20
  v7 --"emi:hasLCMSFeature"-->  v8
  v8 --"a"-->  c10
  v8 --"emi:hasSpec2VecDoc"-->  v9
  subgraph union1[" Union "]
  subgraph union1l[" "]
    style union1l fill:#abf,stroke-dasharray: 3 3;
    v9 --"emi:hasSpec2VecPeak"-->  v12
  end
  subgraph union1r[" "]
    style union1r fill:#abf,stroke-dasharray: 3 3;
    v9 --"emi:hasSpec2VecLoss"-->  v12
  end
  union1r <== or ==> union1l
  end
  bind3[/"sample(?rt)"/]
  v11 --o bind3
  bind3 --as--o v14
  bind4[/"sample(?parent_mass)"/]
  v10 --o bind4
  bind4 --as--o v15
  bind5[/"count(?peakloss)"/]
  v12 --o bind5
  bind5 --as--o v16
  subgraph s1["https://query.wikidata.org/sparql"]
    style s1 stroke-width:4px;
    v5 --"wdt:P225"-->  v16
    subgraph optional0["(optional)"]
    style optional0 fill:#bbf,stroke-dasharray: 5 5;
      v17 -."wdt:P31".->  c24
      v17 --"wdt:P105"-->  c26
      v17 --"wdt:P225"-->  v18
      v5 --"wdt:P171"-->  v17
    end
    subgraph optional1["(optional)"]
    style optional1 fill:#bbf,stroke-dasharray: 5 5;
      v19 -."wdt:P31".->  c24
      v19 --"wdt:P105"-->  c28
      v19 --"wdt:P225"-->  v20
      v5 --"wdt:P171"-->  v19
    end
  end