Beispiel #1
0
    def load_ontologies(self):
        """
        Load all required ontologies.
        """
        for ontology in self.ontologies.values():
            rdfgraph = rdflib.Graph()
            input_format = rdflib.util.guess_format(ontology)
            rdfgraph.parse(ontology, format=input_format)
            triples = rdfgraph.triples((None, rdflib.RDFS.subClassOf, None))
            for s, p, o in triples:
                subject_curie = contract(s)
                object_curie = contract(o)
                self.ontology_graph.add_node(subject_curie)
                self.ontology_graph.add_node(object_curie)
                key = generate_edge_key(subject_curie, 'subclass_of',
                                        object_curie)
                self.ontology_graph.add_edge(
                    subject_curie, object_curie, key, **{
                        'edge_label': 'subclass_of',
                        'relation': 'rdfs:subClassOf'
                    })

            triples = rdfgraph.triples((None, rdflib.RDFS.label, None))
            for s, p, o in triples:
                key = contract(s)
                value = o.value
                value = value.replace(' ', '_')
                self.curie_map[key] = value
                self.ontology_graph.add_node(key, name=value)
Beispiel #2
0
def test_contract(query):
    curie = contract(query[1], prefix_maps=None, fallback=True)
    # get the CURIE
    assert curie == query[0]

    # provide custom prefix_maps, with fallback
    curie = contract(query[2],
                     prefix_maps=[{
                         'HGNC': 'https://identifiers.org/hgnc:'
                     }],
                     fallback=True)
    # get the CURIE
    assert curie == query[0]

    # provide custom prefix_maps, but no fallback
    curie = contract(query[2],
                     prefix_maps=[{
                         'HGNC': 'https://identifiers.org/hgnc:'
                     }],
                     fallback=False)
    # get the CURIE
    assert curie == query[0]

    # provide no prefix_maps, and no fallback
    curie = contract(query[2], prefix_maps=None, fallback=False)
    # get back the IRI
    assert curie == query[2]
Beispiel #3
0
def test_contract(query):
    """
    Test contract method for contracting an IRI to a CURIE.
    """
    curie = contract(query[1], prefix_maps=None, fallback=True)
    # get the CURIE
    assert curie == query[0]

    # provide custom prefix_maps, with fallback
    curie = contract(
        query[2], prefix_maps=[{"HGNC": "https://identifiers.org/hgnc:"}], fallback=True
    )
    # get the CURIE
    assert curie == query[0]

    # provide custom prefix_maps, but no fallback
    curie = contract(
        query[2],
        prefix_maps=[{"HGNC": "https://identifiers.org/hgnc:"}],
        fallback=False,
    )
    # get the CURIE
    assert curie == query[0]

    # provide no prefix_maps, and no fallback
    curie = contract(query[2], prefix_maps=None, fallback=False)
    # get back the IRI
    assert curie == query[2]
Beispiel #4
0
    def contract(self, uri: str, fallback: bool = True) -> str:
        """
        Contract a given URI to a CURIE, based on mappings from `prefix_map`.

        Parameters
        ----------
        uri: str
            A URI

        fallback: bool
            Determines whether to fallback to default prefix mappings, as determined
            by `prefixcommons.curie_util`, when URI prefix is not found in `reverse_prefix_map`.

        Returns
        -------
        str
            A CURIE corresponding to the URI

        """
        # always prioritize non-CURIE shortform
        if uri in self.reverse_prefix_map:
            curie = self.reverse_prefix_map[uri]
        else:
            curie = contract(uri, [self.prefix_map], fallback)
        return str(curie)
Beispiel #5
0
def infer_category(iri: URIRef, rdfgraph:rdflib.Graph) -> List[str]:
    """
    Infer category for a given iri by traversing rdfgraph.

    Parameters
    ----------
    iri: rdflib.term.URIRef
        IRI
    rdfgraph: rdflib.Graph
        A graph to traverse

    Returns
    -------
    List[str]
        A list of category corresponding to the given IRI

    """
    category = None
    subj = None
    closure = list(rdfgraph.transitive_objects(iri, URIRef(RDFS.subClassOf)))
    category = [top_level_terms[x] for x in closure if x in top_level_terms.keys()]
    if category:
        logging.debug("Inferred category as {} based on transitive closure over 'subClassOf' relation".format(category))
    else:
        subj = closure[-1]
        if subj == iri:
            return category
        subject_curie = contract(subj)
        if '_' in subject_curie:
            fixed_curie = subject_curie.split(':', 1)[1].split('_', 1)[1]
            logging.warning("Malformed CURIE {} will be fixed to {}".format(subject_curie, fixed_curie))
            subject_curie = fixed_curie
        cls = get_curie_lookup_service()
        category = get_category_via_superclass(cls.ontology_graph, subject_curie)
    return category
Beispiel #6
0
def process_iri(iri:Union[str, URIRef]) -> str:
    """
    Casts iri to a string, and then checks whether it maps to any pre-defined
    values. If so returns that value, otherwise converts that iri to a curie
    and returns.

    Parameters
    ----------
    iri: Union[str, URIRef]
        IRI to process; can be a string or a rdflib.term.URIRef

    Returns
    -------
    str
        A string corresponding to the IRI

    """
    mappings = [
        predicate_mapping,
        category_mapping,
        property_mapping,
    ]

    for mapping in mappings:
        for key, value in mapping.items():
            if iri.lower() == key.lower():
                return value

    return contract(iri)