@prefix ex: <https://kg.earthmetabolome.org/metrin/api/.well-known/examples/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <https://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix spex:<https://purl.expasy.org/sparql-examples/ontology#> .

ex:014 a sh:SPARQLExecutable,
        sh:SPARQLSelectExecutable ;
    rdfs:comment "List traits (and their values) of plants producing Diterpenoids"@en ;
    sh:prefixes _:sparql_examples_prefixes ;
    sh:select """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 emi: <https://w3id.org/emi#>
PREFIX sosa: <http://www.w3.org/ns/sosa/>
PREFIX qudt: <https://qudt.org/schema/qudt/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source_wdx ?sourceName ?tryDataLab ?tryDataVal ?unit ?unitComment WHERE {
	# metabolite data
	{
		SELECT DISTINCT ?source_wdx 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 ?ik ;
		                emi:hasClass ?npcClass .
			?npcClass skos:broader ?npcSuperClass .
		        ?ik emi:isInChIKeyOf ?wd_chem .	# wikidata-ids for metabolites
			FILTER (REGEX(STR(?npcSuperClass), 'DITERPENOIDS'))
		}
	} UNION	#union with data from lotus (integrated in wikidata)
	{
		SERVICE <https://qlever.dev/api/wikidata> {
			?wd_chem wdt:P235 ?structure_inchikey ;
				((wdt:P31|wdt:P279)/(wdt:P279*)) wd:Q47006367 ;	# check if the class/superclass of the chemical is Diterpenoids
   				wdt:P703 ?source_wdx .
		}
	}
	# trait data
	?trySpName emi:inTaxon ?wdx .
	?trySpObs sosa:isSampleOf ?trySpName ; # trait/non-trait data for trySpName (scientific name of plant species as listed in trydb)
		rdfs:label ?sourceName ;
		sosa:isFeatureOfInterestOf ?tryObId .
	?trySpName emi:inTaxon ?source_wdx . # wikidata-ids wdx for trySpName matching the one from metabolite queries
	?tryObId sosa:hasResult ?tryData .
	?tryData rdfs:label ?tryDataLab ;
		rdf:type emi:Trait ; # retrieve data which is labelled as 'Trait' and its values
		rdf:value ?tryDataVal ;
		qudt:hasUnit ?unit ;
		rdfs:comment ?unitComment . # original units for Trait data as listed in trydb
}""" ;
    schema:target <https://kg.earthmetabolome.org/metrin/api/> ;
    spex:federatesWith <https://qlever.dev/api/wikidata> .
