@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#> .

ex:003 a sh:SPARQLExecutable,
        sh:SPARQLSelectExecutable ;
    rdfs:comment "Filter the positive ionization mode features of Melochia umbellate annotated as [M+H]+ by SIRIUS to keep the ones for which a feature in negative ionization mode is detected with the same retention time (± 3 seconds) and a mass corresponding to the [M-H]- adduct (± 5 ppm)."@en ;
    sh:prefixes _:sparql_examples_prefixes ;
    sh:select """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX emi: <https://w3id.org/emi#>
PREFIX sosa: <http://www.w3.org/ns/sosa/>

SELECT DISTINCT ?feature ?rt ?pm ?feature_opp ?rt_opp ?pm_opp
WHERE {
    VALUES ?ppm {
        "5"^^xsd:decimal # m/z tolerance
    }
    VALUES ?rt_tol {
        "0.05"^^xsd:decimal # RT tolerance (minute)
    }
    ?sample a emi:ExtractSample ;
        sosa:isSampleOf* ?organe .
    ?organe emi:inTaxon ?taxon .
    ?taxon rdfs:label "melochia umbellata" .
    ?sample sosa:isFeatureOfInterestOf ?lcms .
    ?lcms a emi:LCMSAnalysisPos ;
        sosa:hasResult ?feature_list .
    ?feature_list emi:hasLCMSFeature ?feature .
    ?feature emi:hasParentMass ?pm ;
        emi:hasRetentionTime ?rt ;
        emi:hasAnnotation ?sirius .
	?sirius a emi:StructuralAnnotation ;
        prov:wasGeneratedBy ?activiy .
    ?activiy prov:wasAssociatedWith <https://bio.informatik.uni-jena.de/software/sirius> .
    ?sirius emi:hasAdduct ?adduct .
    FILTER(regex(str(?adduct), "[M+H]+"))
    ?sample sosa:isFeatureOfInterestOf ?lcms_opp .
    ?lcms_opp a emi:LCMSAnalysisNeg ;
        sosa:hasResult ?feature_list_opp .
    ?feature_list_opp emi:hasLCMSFeature ?feature_opp .
	?feature_opp emi:hasParentMass ?pm_opp ;
        emi:hasRetentionTime ?rt_opp .
    FILTER(((?rt - ?rt_tol) < ?rt_opp) && ((?rt + ?rt_tol) > ?rt_opp))
    FILTER((?pm_opp > ((?pm - 2.014) - ((?ppm * 0.000001) * (?pm - 2.014)))) && (?pm_opp < ((?pm - 2.014) + ((?ppm * 0.000001) * (?pm - 2.014)))))
}""" ;
    schema:target <https://kg.earthmetabolome.org/metrin/api/> .

