Skip to content

Commit

Permalink
Merge pull request #77 from INCATools/additional-sqlite-tests
Browse files Browse the repository at this point in the history
additional sqlite tests
  • Loading branch information
cmungall authored May 17, 2022
2 parents d0fd8f1 + 950c762 commit aed556c
Show file tree
Hide file tree
Showing 9 changed files with 1,298 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/oaklib/implementations/sqldb/sql_implementation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from abc import ABC
from collections import defaultdict
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import List, Any, Iterable, Optional, Type, Dict, Union, Tuple, Iterator

import semsql.builder.builder as semsql_builder
Expand Down Expand Up @@ -70,7 +70,6 @@ class SqlImplementation(RelationGraphInterface, OboGraphInterface, ValidatorInte
_session: Any = None
_connection: Any = None
_ontology_metadata_model: SchemaView = None
autosave : bool = None

def __post_init__(self):
if self.engine is None:
Expand Down Expand Up @@ -169,9 +168,11 @@ def curies_by_subset(self, subset: SUBSET_CURIE) -> Iterable[CURIE]:
yield self._get_subset_curie(row.subject)

def set_label_for_curie(self, curie: CURIE, label: str) -> bool:
stmt = update(Statements).where(Statements.subject == curie and Statements.predicate == LABEL_PREDICATE).values(value=label)
self.session.execute(
update(Statements).where(Statements.subject == curie and Statements.predicate == LABEL_PREDICATE).values(value=label)
stmt
)
self.session.flush()
if self.autosave:
self.save()

Expand Down Expand Up @@ -521,7 +522,8 @@ def gap_fill_relationships(self, seed_curies: List[CURIE], predicates: List[PRED
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def get_information_content(self, curie: CURIE, background: CURIE = None,
predicates: List[PRED_CURIE] = None):
raise NotImplementedError
# TODO: fix
return 1.0

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Implements: PatcherInterface
Expand All @@ -540,5 +542,6 @@ def migrate_curies(self, curie_map: Dict[CURIE, CURIE]) -> None:
self.save()

def save(self):
logging.info(f'Committing and flushing changes')
self.session.commit()
self.session.flush()
5 changes: 4 additions & 1 deletion src/oaklib/interfaces/basic_ontology_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Dict, List, Iterable, Tuple, Optional, Any, Iterator

from oaklib.interfaces.ontology_interface import OntologyInterface
Expand Down Expand Up @@ -66,6 +66,7 @@ class BasicOntologyInterface(OntologyInterface, ABC):
"""
strict: bool = False
autosave: bool = field(default_factory= lambda: True)


def get_prefix_map(self) -> PREFIX_MAP:
Expand Down Expand Up @@ -335,3 +336,5 @@ def create_entity(self, curie: CURIE, label: str = None, relationships: RELATION
"""
raise NotImplementedError

def save(self):
pass
6 changes: 5 additions & 1 deletion src/oaklib/interfaces/semsim_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from oaklib.interfaces.basic_ontology_interface import BasicOntologyInterface
from oaklib.interfaces.obograph_interface import OboGraphInterface
from oaklib.types import CURIE, LABEL, URI, PRED_CURIE
from oaklib.utilities.semsim.similarity_utils import setwise_jaccard_similarity


class SemanticSimilarityInterface(BasicOntologyInterface, ABC):
Expand Down Expand Up @@ -52,6 +53,9 @@ def pairwise_similarity(self, subject: CURIE, object: CURIE = None,
best_mrcas = [a for a in cas if ics[a] == max_ic]
sim = TermPairwiseSimilarity(subject_id=subject, object_id=object, ancestor_id=best_mrcas[0])
sim.ancestor_information_content = max_ic
sim.phenodigm_score = math.sqrt(sim.jaccard_similarity * sim.information_content)
if isinstance(self, OboGraphInterface):
sim.jaccard_similarity = setwise_jaccard_similarity(list(self.ancestors(subject, predicates=predicates)),
list(self.ancestors(object, predicates=predicates)))
#sim.phenodigm_score = math.sqrt(sim.jaccard_similarity * sim.information_content)
return sim

10 changes: 8 additions & 2 deletions src/oaklib/utilities/lexical/lexical_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ def add_labels_from_uris(oi: BasicOntologyInterface):
:return:
"""
logging.info(f'Adding labels from URIs')
for curie in oi.all_entity_curies():
curies = list(oi.all_entity_curies())
for curie in curies:
if not oi.get_label_by_curie(curie):
if '#' in curie:
sep = '#'
elif curie.startswith('http'):
elif curie.startswith('http') or curie.startswith('<http'):
sep = '/'
else:
sep = ':'
label = curie.split(sep)[-1]
if curie.startswith('<http'):
label = label.replace('>', '')
label = " ".join(label.split('_'))
#print(f'{curie} ==> {label} // {type(oi)}')
oi.set_label_for_curie(curie, label)
#print(oi.get_label_by_curie(curie))


def create_lexical_index(oi: BasicOntologyInterface,
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions src/oaklib/utilities/reasoning/relation_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from collections import Iterator
from dataclasses import dataclass
import oaklib.datamodels.obograph as og
from oaklib.utilities.obograph_utils import index_graph_edges_by_subject, index_graph_edges_by_subject_object


@dataclass
class RelationGraphReasoner:
ontology: og.Graph = None

def get_non_redundant_edges(self) -> Iterator[og.Edge]:
ont = self.ontology
six = index_graph_edges_by_subject(ont.edges)
soix = index_graph_edges_by_subject_object(ont.edges)
for e in ont.edges:
yield e
Binary file added tests/input/ssn.db
Binary file not shown.
Loading

0 comments on commit aed556c

Please sign in to comment.