Triples

Similarly to how the curies package enables the semantic representation of references (i.e., as CURIEs) with the curies.Reference class, it enables the representation of semantic triples (i.e., as subject-predicate-object triples of CURIEs) with the curies.Triple class.

Constructing Triples

Triples can be constructed either from strings representing CURIEs or pre-parsed Reference objects representing CURIEs.

from curies import Triple, Reference

# construction with string representations of CURIEs
triple = Triple(
    subject="mesh:C000089",
    predicate="skos:exactMatch",
    object="CHEBI:28646",
)

# construction with object representations of CURIEs
triple = Triple(
    subject=Reference(prefix="mesh", identifier="C000089"),
    predicate=Reference(prefix="skos", identifier="exactMatch"),
    object=Reference(prefix="CHEBI", identifier="28646"),
)

Any reference objects can be used, including ones with names:

from curies import NamableReference

triple = Triple(
    subject=NamedReference(prefix="mesh", identifier="C000089", name="ammeline"),
    predicate=NamableReference(prefix="skos", identifier="exactMatch"),
    object=NamedReference(prefix="CHEBI", identifier="28646", name="ammeline"),
)

The Triple interface does not enforce any CURIE validation. The Triple.from_uris() constructor implicitly performs validation against a converter while parsing.

from curies import Triple, Reference, Converter

converter = curies.load_prefix_map(
    {
        "mesh": "http://id.nlm.nih.gov/mesh/",
        "skos": "http://www.w3.org/2004/02/skos/core#",
        "CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
    }
)

triple = Triple.from_uris(
    subject="http://id.nlm.nih.gov/mesh/C000089",
    predicate="http://www.w3.org/2004/02/skos/core#exactMatch",
    object="http://purl.obolibrary.org/obo/CHEBI_28646",
    converter=converter,
)

Identification of Triples

The rdf namespace supports the explicit reification of triples. This means that an explicit identifier (typically, a blank node) can be used to refer to a triple itself, and the rdf:subject, rdf:predicate and rdf:object predicates can be used to connect the identifier representing the triple to its respective subject, predicate, and object components.

RDF enables explicit reification of triples with the following:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX mesh: <http://id.nlm.nih.gov/mesh/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX CHEBI: <http://purl.obolibrary.org/obo/CHEBI_>

mesh:C000089 skos:exactMatch CHEBI:28646 .

[] rdf:type rdf:Statement ;
    rdf:subject mesh:C000089 ;
    rdf:predicate skos:exactMatch ;
    rdf:object CHEBI:28646 .

It would be nice to have an implementation-agnostic way of assigning an identifier to the triple. This example imagines a namespace with the prefix triple that can host identifiers for triples:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX mesh: <http://id.nlm.nih.gov/mesh/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX CHEBI: <http://purl.obolibrary.org/obo/CHEBI_>
PREFIX triple: <https://w3id.org/triple/>

mesh:C000089 skos:exactMatch CHEBI:28646 .

triple:36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a rdf:type rdf:Statement ;
    rdf:subject mesh:C000089 ;
    rdf:predicate skos:exactMatch ;
    rdf:object CHEBI:28646 .

curies.Converter.hash_triple() implements a deterministic, one-way hash of a triple based on the algorithm in https://ts4nfdi.github.io/mapping-sameness-identifier:

import curies
from curies import Triple, Converter

converter = curies.load_prefix_map(
    {
        "mesh": "http://id.nlm.nih.gov/mesh/",
        "skos": "http://www.w3.org/2004/02/skos/core#",
        "CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
    }
)
triple = Triple(
    subject="mesh:C000089",
    predicate="skos:exactMatch",
    object="CHEBI:28646",
)
triple_id = converter.hash_triple(triple)
assert triple_id == "36a1f9244ea7641a90987c82f33c25c0c13712ee8f48207b2a0825f8a4e4e26a"

Functions

encode_curie_triple(curie_triple, converter, *)

Encode a subject-predicate-object CURIE triple.

encode_uri_triple(uri_triple, *[, negate])

Encode a subject-predicate-object URI triple.

exclude_object_prefixes(triples, prefixes, *)

Exclude triples whose objects' prefixes are in the given prefixes.

exclude_prefix_stratified_many_to_many(...)

Exclude prefix pair-stratified many-to-many relationships.

exclude_prefixes_both(triples, prefixes, *)

Exclude triples whose subjects' and objects' prefixes are in the given prefixes.

exclude_references_both(triples, references, *)

Exclude triples whose subject and object appear in the given references.

exclude_same_prefixes(triples, *[, progress])

Exclude triples whose subject and object prefixes are the same.

exclude_subject_prefixes(triples, prefixes, *)

Exclude triples whose subjects' prefixes are in the given prefixes.

exclude_triples(triples, exclusion, *[, ...])

Exclude triples in the given set.

hash_triple(converter, triple, *[, negate])

Encode a triple with URL-safe base64 encoding.

keep_object_prefixes(triples, prefixes, *[, ...])

Keep triples whose objects' prefixes are in the given prefixes.

keep_predicates(triples, predicates, *[, ...])

Keep triples whose predicate appear in the given references.

keep_prefixes_both(triples, prefixes, *[, ...])

Keep triples whose subjects' and objects' prefixes are in the given prefixes.

keep_prefixes_either(triples, prefixes, *[, ...])

Keep triples whose subjects' and objects' prefixes are in the given prefixes.

keep_references_both(triples, references, *)

Keep triples whose subject and object appear in the given references.

keep_references_either(triples, references, *)

Keep triples whose subject and object appear in the given references.

keep_subject_prefixes(triples, prefixes, *)

Keep triples whose subjects' prefixes are in the given prefixes.

keep_triples_by_hash(triples, converter, ...)

Keep triples whose triple hash (under the given converter) is in the given collection.

read_triples(path, *[, reference_cls])

Read triples from a three-column TSV file.

write_triples(triples, path, *[, header])

Write triples as a three-column TSV file.

Classes

StrTriple(subject, predicate, object)

A triple of curies.

Variables

TriplePredicate

TripleType

Type variable.