sparql-examples

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

View the Project on GitHub BiGCAT-UM/sparql-examples

049

rq turtle/ttl

Chemical aspect: related chemical structures

Use at

PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX mwapi: <>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX target: <http://www.wikidata.org/entity/Q2270>

# title: related chemical structures
SELECT ?mol ?molLabel ?InChIKey ?CAS ?ChemSpider ?PubChem_CID WITH {
  SELECT ?queryKey ?srsearch ?filter WHERE {
    target: wdt:P235 ?queryKey .
    BIND (CONCAT(substr($queryKey,1,14), " haswbstatement:P235") AS ?srsearch)
    BIND (CONCAT("^", substr($queryKey,1,14)) AS ?filter)
  }
} AS %MOLS WITH {
  SELECT ?mol ?InChIKey WHERE {
    INCLUDE %MOLS
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:endpoint "www.wikidata.org";
        wikibase:api "Search";
        mwapi:srsearch ?srsearch;
        mwapi:srlimit "max".
        ?mol wikibase:apiOutputItem mwapi:title.
      }
    ?mol wdt:P235 ?InChIKey .
    FILTER (regex(str(?InChIKey), ?filter))
    FILTER (?InChIKey != ?queryKey)
  }
} AS %MOLS2 {
  INCLUDE %MOLS2
  OPTIONAL { ?mol wdt:P231 ?CAS }
  OPTIONAL { ?mol wdt:P661 ?ChemSpider }
  OPTIONAL { ?mol wdt:P662 ?PubChem_CID }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en" . }
}

graph TD