sparql-examples

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

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

examples023

rq turtle/ttl

Retrieve metabolites of organisms with data available from 4 traits in leaf economics spectrum.

Use at

PREFIX emi: <https://purl.org/emi#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX sosa: <http://www.w3.org/ns/sosa/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?structure_inchikey ?wd_chem ?source_wdx ?sourceName  WHERE 
	{
		# trait data
		
		# trait/non-trait data for trySpName (scientific name of plant species as listed in trydb)
		?trySpObs sosa:isSampleOf ?trySpName ; 
			rdfs:label ?sourceName ;
			sosa:isFeatureOfInterestOf ?tryObId .
		?tryObId sosa:hasResult ?tryData .
		?trySpName emi:inTaxon ?source_wdx . # wikidata-ids wdx for trySpName
		?tryData rdfs:label ?traitLabel ; 
			rdf:type emi:Trait ; #retrieve data which is labelled as 'Trait' and its values
		
		# retrieve trait labels
		BIND (
        		IF (REGEX(STR(?traitLabel), "SLA", "i"), "Specific leaf area",
    			IF (REGEX(STR(?traitLabel), "leaf dry matter content", "i"), "Leaf dry matter content",
	    		IF (REGEX(STR(?traitLabel), "leaf nitrogen content", "i"), "Leaf nitrogen content",
    			IF (REGEX(STR(?traitLabel), "leaf phosphorus content", "i"), "Leaf phosphorus content",
    			"Other")))) AS ?traitCategory
  		)
  		
		# filter to LES categories only
	  	FILTER (?traitCategory != "Other")
		
		# metabolite data
		{ 
			SELECT ?source_wdx ?structure_inchikey ?wd_chem WHERE {	
				?material sosa:hasSample ?extract ;				
		        		sosa:isSampleOf ?organe .	
				?organe emi:inTaxon ?source_wdx . # filter metabolite data which is found in wikidata-ids wdx 
				?extract sosa:isFeatureOfInterestOf ?lcms .
			    	?lcms sosa:hasResult ?feature_list .
				?feature_list emi:hasLCMSFeature ?feature .
				?feature emi:hasAnnotation ?sirius_annotation .
				?sirius_annotation a emi:StructuralAnnotation ;
					emi:hasChemicalStructure ?ik2d .
				?ik2d emi:hasSMILES ?smiles ;
		    			emi:isInChIKey2DOf ?structure_inchikey .
				?structure_inchikey emi:isInChIKeyOf ?wd_chem .	# wikidata-ids for metabolites
			}
		}
		UNION # union with data from lotus (integrated in wikidata)
		{
			SERVICE <https://qlever.cs.uni-freiburg.de/api/wikidata> {		
				?wd_chem wdt:P235 ?structure_inchikey;				
	   				wdt:P703 ?source_wdx .	
			}
		}
	}

	# group results and retain only the ones from organisms with all four traits available 
	GROUP BY ?source_wdx ?sourceName ?traitCategory ?structure_inchikey ?wd_chem
	HAVING(COUNT(?traitCategory) = 4)

	# order by organism and trait names
	ORDER BY ?source_wdx ?traitCategory
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v20("?_anon_39b94405334d4c98a94d4349b2e252701241")
  v10("?extract")
  v14("?feature")
  v13("?feature_list")
  v16("?ik2d")
  v12("?lcms")
  v9("?material")
  v11("?organe")
  v15("?sirius_annotation")
  v17("?smiles")
  v5("?sourceName"):::projected 
  v1("?source_wdx"):::projected 
  v18("?structure_inchikey"):::projected 
  v9("?traitCategory")
  v8("?traitLabel")
  v7("?tryData")
  v6("?tryObId")
  v4("?trySpName")
  v3("?trySpObs")
  v19("?wd_chem"):::projected 
  a1((" "))
  c9(["emi:Trait"]):::iri 
  c13(["emi:StructuralAnnotation"]):::iri 
  f0[[" = '4^^xsd:integer'"]]
  f0 --> a1
  f1[["?traitCategory != 'Other'"]]
  f1 --> v9
  v3 --"sosa:isSampleOf"-->  v4
  v3 --"rdfs:label"-->  v5
  v3 --"sosa:isFeatureOfInterestOf"-->  v6
  v6 --"sosa:hasResult"-->  v7
  v4 --"emi:inTaxon"-->  v1
  v7 --"rdfs:label"-->  v8
  v7 --"a"-->  c9
  bind2[/"if(regex(str(?traitLabel),'SLA','i'),'Specific leaf area',if(regex(str(?traitLabel),'leaf dry matter content','i'),'Leaf dry matter content',if(regex(str(?traitLabel),'leaf nitrogen content','i'),'Leaf nitrogen content',if(regex(str(?traitLabel),'leaf phosphorus content','i'),'Leaf phosphorus content','Other'))))"/]
  v8 --o bind2
  bind2 --as--o v9
  subgraph union0[" Union "]
  subgraph union0l[" "]
    style union0l fill:#abf,stroke-dasharray: 3 3;
    subgraph s1["https://qlever.cs.uni-freiburg.de/api/wikidata"]
      style s1 stroke-width:4px;
      v19 --"wdt:P235"-->  v18
      v19 --"wdt:P703"-->  v1
    end
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    v9 --"sosa:hasSample"-->  v10
    v9 --"sosa:isSampleOf"-->  v11
    v11 --"emi:inTaxon"-->  v1
    v10 --"sosa:isFeatureOfInterestOf"-->  v12
    v12 --"sosa:hasResult"-->  v13
    v13 --"emi:hasLCMSFeature"-->  v14
    v14 --"emi:hasAnnotation"-->  v15
    v15 --"a"-->  c13
    v15 --"emi:hasChemicalStructure"-->  v16
    v16 --"emi:hasSMILES"-->  v17
    v16 --"emi:isInChIKey2DOf"-->  v18
    v18 --"emi:isInChIKeyOf"-->  v19
  end
  union0r <== or ==> union0l
  end
  bind4[/"count(?traitCategory)"/]
  v9 --o bind4
  bind4 --as--o v20