PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX sio: <http://semanticscience.org/resource/>
PREFIX vocab: <http://rdf.ncbi.nlm.nih.gov/pubchem/vocabulary#>
PREFIX sachem: <http://bioinfo.uochb.cas.cz/rdf/v1.0/sachem#>
PREFIX endpoint: <https://idsm.elixir-czech.cz/sparql/endpoint/>
PREFIX ex: <http://example.org/>

CONSTRUCT
{
    ?compound ex:hasCASnumber        ?cas_number             ;
              ex:hasSimilarCompound  ?similar_compound_chebi ;
              ex:hasCASnumberIRI     ?cas_number_iri         .
}
WHERE
{
    {
        SELECT
            ?cas_number
            ?compound
            ?similar_compound_chebi
            ?score
        WHERE
        {
            {
                # Pollutant CAS numbers retrieved from wikidata (step 1 of use-case).
                VALUES ?cas_number { 
                    '17804-35-2' '17804-35-2' '309-00-2' '86-88-4'
                    '1912-24-9'  '7773-06-0' '61-82-5'
                }

                # sio:SIO_000011     -> 'is attribute of'
                # sio:SIO_000300     -> 'has value'
                # sio:CHEMINF_000446 -> 'CAS registry number'
                ?synonym  sio:SIO_000300 ?cas_number        ;
                        a              sio:CHEMINF_000446 ;
                        sio:SIO_000011 ?compound          .
                ?compound a              vocab:Compound     .

                # SIO_011120  ->  'molecular structure file'
                ?attribute  a               sio:SIO_011120 ;  
                            sio:SIO_000011  ?compound      ;
                            sio:SIO_000300  ?molfile       .

                SERVICE endpoint:chebi {
                    [
                        sachem:compound ?similar_compound_chebi ;
                        sachem:score    ?score
                    ] sachem:similaritySearch [
                        sachem:query             ?molfile                          ;
                        sachem:cutoff            '0.7'^^xsd:double                 ;
                        sachem:similarityRadius  '3'^^xsd:integer                  ;
                        sachem:aromaticityMode   sachem:aromaticityDetectIfMissing ;
                        sachem:tautomerMode      sachem:inchiTautomers
                    ] .
                }
            }

            UNION
            {
                SERVICE endpoint:chebi {
                ?similar_compound_chebi sachem:substructureSearch [ sachem:query '[As]' ] .
                }
            }
        }
    }
    BIND (
      IRI (CONCAT("https://identifiers.org/cas:", ?cas_number)) AS ?cas_number_iri
    )
}

