SPARQL BioPortal

From NCBO Wiki
Jump to: navigation, search

NCBO is releasing a free and open SPARQL endpoint to query ontologies hosted in the BioPortal ontology repository. This SPARQL service, that is in BETA status, is stable for testing by our community of users. If you encounter any errors or unexpected behavior please report it to us

Before using the BioPortal SPARQL service please read our SPARQL Release Notes And Usage Policy

Web Interface and Query Examples

There is a Web interface to test SPARQL queries at

Also, interactive examples can be tested here

Submitting SPARQL queries programmatically

A github project contains examples to query our SPARQL service programmatically:

A tarball with these examples is for download here:

This project contains examples in Java, Python, JavaScript and Perl. Some of the examples use just language built-in capabilities and other need third-party libraries like Jena, Sesame or SPARQLWrapper. The github project and the tarball are self-contained, no need to download and install extra libraries.

To run these examples or any other SPARQL queries programmatically an API key from BioPortal is required. If you do not have a BioPortal account go to [New Account] and create one. Once you have the BioPortal account, login in BioPortal and go to your account details. You should see your API Key as part of your account profile.

Database Named Graph Structure

Each ontology is asserted into a single graph. The graph is named with an acronym based URI. For example, the graph:

contains the Human Phenotype Ontology ontology. And the graph:

contains the SNOMEDCT ontology.

The following query would return all version IDs with the graph IDs where ontologies are located:

PREFIX meta: <> 

SELECT DISTINCT ?version ?graph
    ?version meta:hasDataGraph ?graph

BioPortal Preferred Label

There are problematic cases of label definition. In order to provide a consistent mechanism to query by label across different ontologies we generate labels for the following cases. These label are attached to terms using the predicate (bp:prefLabel)

  • Missing labels: for every owl:Class that is missing a label we generate a label based on the latest fragment of URI.
  • Terms that use rdfs:label as preferred name: BioPortal uses skos:prefLabel and skos:altLabel for preferred names and synonyms respectively. Both skos:prefLabel and skos:altLabel are subproperties of rdfs:label in the SKOS ontology. If someone uses rdfs:label to record preferred names, in the SKOS context, he would be saying that that name can be a preferred name or a synonym. To avoid this confusion we generate bp:prefLabel(s) for every rdfs:label used as preferred name.

Preferred Label, Synonyms and other common predicates

When ontologies are submitted to BioPortal the user can select which predicates that ontology uses for:

  • Preferred Names.
  • Synonyms or alternative names.
  • Author.
  • Description.

The BioPortal SPARQL endpoint supports rdfs:subPropertyOf reasoning to enable cross querying across all these configurable predicates. In the triple store, the following URI:

is used as identifier for the named graph that contains all the sub-property of statements that have been configured by users when uploading their ontologies. The root properties to be used to trigger the reasoning are the following:

  • skos:prefLabel for Preferred name.
  • skos:altLabel for Synonyms or alternative names.
  • dc:author for Author.
  • skos:definition for Description.

When using named graphs if you want to use this reasoning then you should include the globals graph that contains the subproperty statements, i.e:

PREFIX owl:  <>
PREFIX skos: <>
 FROM <>
 FROM <> 
      ?termURI a owl:Class;
      skos:prefLabel ?prefLabel .

Otherwise the subproperty statements that take part in the query processor will not be taken into account.


Notice that as of January 30th (2013) we are hosting the mappings in a separate endpoint. This eases our regular mapping updates. Programmatic queries that target mapping data need to use the following endpoint:

The mapping data in the triple store is stored in the following graphs:

Graph Name Description Loom: lexical mappings. REST: User submitted by users via the REST API. UMLS-CUI: CUI based mappings NOUMLS-CUI: Terms with same CUIs but for ontologies that are not part of UMLS. OBO-XREF: Mappings for terms with same xref attribute. URI-MATCH: Mappings that for terms that in different ontologies are represented by the same URI.

The following query returns the list of the graphs where mappings are located:

GRAPH ?g { 
?s a <> . }}

The following RDF/Turtle sample shows an example of a mapping instance:

@prefix maps: <> .

    maps:has_process_info <.../procinfo/2008-04-23-38138> ;
    maps:comment "Manual mappings between Mouse anatomy and NCIT." ;
    maps:relation skos:closeMatch ;
    maps:target <> ;
    maps:source <> ;
    maps:source_ontology_id <> ;
    maps:target_ontology_id <> ;
    a maps:One_To_One_Mapping .

The predicates we use to represent a mapping record both the target and source terms and target and source ontologies. A SPARQL query to return all the target terms for a giving source term would look like the following:

PREFIX map: <>
  ?s map:source <>;
     map:target ?target .

If one wants to count at all the mappings for two given ontologies then the SPARQL would use the target_ontology and source_ontology predicates:

PREFIX map: <>
SELECT (count(?m) as ?c) WHERE {
  ?m map:source_ontology <> ;
      map:target_ontology  <> .

In this case this query returns a count of 4393 solutions. It is important to notice that for mappings we use version based ontology URIs and not the ones that are based on acronyms.

Federated SPARQL queries

SPARQL Federation can be used with the Jena ARQ library. The Jena ARQ library handles the SERVICE SPARQL construct and directs sets of triple patterns to different endpoint and handles the joins.

BioPortal offers two different SPARQL endpoints one for ontologies (metadata and terms) and a second one for mappings. There are use cases that require queries with joins from both endpoints. This can be achieved by using SPARQL federation and the SERVICE feature (SPARQL 1.1). The following example looks at the CSP term 'Neck' and retrieves the sources for all the mappings for that term (from the mapping endpoint). For each of those sources then gets the parents (from the ontologies endpoint). Notice the change in the SERVICE endpoints from 'mappings' in the first block to 'ontologies' in the second one.

PREFIX map: <>
PREFIX rdfs: <>
PREFIX skos: <>
        ?mapping map:target <> .
        ?mapping map:source ?source .
        ?source rdfs:subClassOf ?mappedParent .

Full example:

Note SPARQL Federation can be used programmatically only. Our web front-end at does not support mediation for the SERVICE clause. Our tests for SPARQL federation use Jena ARQ 2.9.4, this version is able to correctly submit the BioPortal API keys.

Partial or Incomplete Results uses 4store's soft-limit internal mechanism to limit resources for expensive queries. Our setup is configured to bind 8K elements per triple pattern. If you hit these limits a warning message will be appended to the query response. This message says something like: "hit complexity limit 8 times". If you see this warning it means that the results are incomplete, and probably there is a more efficient way to write that query.

Contact our support mail list if you need help to rewrite your query in a more efficient way and avoid incomplete results.