<commit_before>"""
Reconsitutes an ontology from SPARQL queries over a remote SPARQL server

 * the first time an ontology is referenced, basic axioms will be fetched via SPARQL
 * these will be cached in `/tmp/.cache/`
 * the second time the same ontology is referenced, the disk cache will be used
 * if an ontology is referenced a second time in the same in-memory session, the disk cache is bypassed and in-memory (lru) cache is used

Note: you should not need to use this directly. An `OntologyFactory` object will automatically use this if a sparql handle is passed.

"""

from SPARQLWrapper import SPARQLWrapper, JSON
from prefixcommons.curie_util import contract_uri, expand_uri
from functools import lru_cache
import networkx
from cachier import cachier
import datetime
import logging

SHELF_LIFE = datetime.timedelta(days=7)

# CACHE STRATEGY:
# by default, the cache is NOT persistent. Only single threaded clients should
# call a cached method with writecache=True.
# Note we are layering the in-memory cache over the persistent cache

cache = lru_cache(maxsize=None)
#cache = cachier(stale_after=SHELF_LIFE)


SUBCLASS_OF = 'subClassOf'
SUBPROPERTY_OF = 'subPropertyOf'

# TODO
# for now we assume ontobee
ontol_sources = {
    'go': "http://rdf.geneontology.org/sparql",
    '': "http://sparql.hegroup.org/sparql"
    }

OIO_SYNS = dict(
    related='oboInOwl:hasRelatedSynonym',
    narrow = 'oboInOwl:hasNarrowSynonym',
    broad = 'oboInOwl:hasBroadSynonym',
    exact = 'oboInOwl:hasExactSynonym')
    

def get_digraph(ont, relations=None, writecache=False):
    """
    Creates a basic graph object corresponding to a remote ontology
    """
    digraph = networkx.MultiDiGraph()
    for (s,p,o) in get_edges(ont):
        if relations is None or p in relations:
            digraph.add_edge(o,s,pred=p)
    for (n,label) in fetchall_labels(ont):
        digraph.add_node(n, attr_dict={'label':label})
    return digraph

def get_xref_graph(ont):
    """
    Creates a basic graph object corresponding to a remote ontology
    """
    g = networkx.MultiGraph()
    for (c,x) in fetchall_xrefs(ont):
        g.add_edge(c,x,source=c)
    return g

<commit_msg>logging for sparql<commit_after>"""
Reconsitutes an ontology from SPARQL queries over a remote SPARQL server

 * the first time an ontology is referenced, basic axioms will be fetched via SPARQL
 * these will be cached in `/tmp/.cache/`
 * the second time the same ontology is referenced, the disk cache will be used
 * if an ontology is referenced a second time in the same in-memory session, the disk cache is bypassed and in-memory (lru) cache is used

Note: you should not need to use this directly. An `OntologyFactory` object will automatically use this if a sparql handle is passed.

"""

from SPARQLWrapper import SPARQLWrapper, JSON
from prefixcommons.curie_util import contract_uri, expand_uri
from functools import lru_cache
import networkx
from cachier import cachier
import datetime
import logging

SHELF_LIFE = datetime.timedelta(days=7)

# CACHE STRATEGY:
# by default, the cache is NOT persistent. Only single threaded clients should
# call a cached method with writecache=True.
# Note we are layering the in-memory cache over the persistent cache

cache = lru_cache(maxsize=None)
#cache = cachier(stale_after=SHELF_LIFE)


SUBCLASS_OF = 'subClassOf'
SUBPROPERTY_OF = 'subPropertyOf'

# TODO
# for now we assume ontobee
ontol_sources = {
    'go': "http://rdf.geneontology.org/sparql",
    '': "http://sparql.hegroup.org/sparql"
    }

OIO_SYNS = dict(
    related='oboInOwl:hasRelatedSynonym',
    narrow = 'oboInOwl:hasNarrowSynonym',
    broad = 'oboInOwl:hasBroadSynonym',
    exact = 'oboInOwl:hasExactSynonym')
    

def get_digraph(ont, relations=None, writecache=False):
    """
    Creates a basic graph object corresponding to a remote ontology
    """
    digraph = networkx.MultiDiGraph()
    logging.info("Getting edges (may be cached)")
    for (s,p,o) in get_edges(ont):
        if relations is None or p in relations:
            digraph.add_edge(o,s,pred=p)
    logging.info("Getting labels (may be cached)")
    for (n,label) in fetchall_labels(ont):
        digraph.add_node(n, attr_dict={'label':label})
    return digraph

def get_xref_graph(ont):
    """
    Creates a basic graph object corresponding to a remote ontology
    """
    g = networkx.MultiGraph()
    for (c,x) in fetchall_xrefs(ont):
        g.add_edge(c,x,source=c)
    return g


{'fraction_non_alphanumeric': 0.08650829224562977, 'fraction_numerical': 0.00044822949350067237, 'mean_word_length': 4.042937853107345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4315495', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1423, 'n_words': 480}