Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Commit

Permalink
Add support for equivalence relationships
Browse files Browse the repository at this point in the history
Closes #16
  • Loading branch information
cthoyt committed Jan 31, 2019
1 parent 3b11edd commit 5ae1679
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
32 changes: 16 additions & 16 deletions src/biokeen/convert/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,32 @@ def convert(cls, u: BaseEntity, v: BaseEntity, key: str, edge_data: Dict) -> Tup
)


class SimpleTypedPredicate(Converter):
class SimplePredicate(Converter):
"""Converts BEL statements based on a given relation."""

relation = ...

@classmethod
def predicate(cls, u, v, key, edge_data) -> bool:
"""Test a BEL edge has a given relation."""
return edge_data[RELATION] == cls.relation


class SimpleTypedPredicate(SimplePredicate):
"""Finds BEL statements like ``A(X) B C(Y)`` where relation B and types A and C are defined in the class."""

subject_type = ...
relation = ...
object_type = ...

@classmethod
def predicate(cls, u: BaseEntity, v: BaseEntity, key: str, edge_data: Dict):
def predicate(cls, u: BaseEntity, v: BaseEntity, key: str, edge_data: Dict) -> bool:
"""Test a BEL edge."""
return (
return super().predicate(u, v, key, edge_data) and (
isinstance(u, cls.subject_type) and
edge_data[RELATION] == cls.relation and
isinstance(v, cls.object_type)
)


class _ConvertOnRelation(Converter):
relation = ...

@classmethod
def predicate(cls, u, v, key, edge_data):
"""Test a BEL edge has a given relation."""
return edge_data[RELATION] == cls.relation


class _PartOfConverter(SimpleTypedPredicate, TypedConverter):
relation = PART_OF
target_relation = 'partOf'
Expand Down Expand Up @@ -129,14 +129,14 @@ class TranscriptionConverter(TypedConverter):
'''


class IsAConverter(_ConvertOnRelation):
class IsAConverter(SimplePredicate, SimpleConverter):
"""Converts BEL statements like ``X isA Y``."""

relation = IS_A
target_relation = 'isA'


class EquivalenceConverter(_ConvertOnRelation):
class EquivalenceConverter(SimplePredicate, SimpleConverter):
"""Converts BEL statements like ``X eq Y``."""

relation = EQUIVALENT_TO
Expand Down
28 changes: 17 additions & 11 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
import unittest
from typing import Tuple, Type

from biokeen.convert import get_triple
from biokeen.convert.converters import (
AssociationConverter, Converter, CorrelationConverter, DecreasesAmountConverter, DrugIndicationConverter,
DrugSideEffectConverter, IncreasesAmountConverter, MiRNADecreasesExpressionConverter,
NamedComplexHasComponentConverter, PartOfBiologicalProcess, PartOfNamedComplexConverter, RegulatesActivityConverter,
RegulatesAmountConverter,
)
from pybel import BELGraph
from pybel.constants import (
ASSOCIATION, DECREASES, HAS_COMPONENT, INCREASES, NEGATIVE_CORRELATION, OBJECT, PART_OF, POSITIVE_CORRELATION,
REGULATES, RELATION,
ASSOCIATION, DECREASES, EQUIVALENT_TO, HAS_COMPONENT, INCREASES, IS_A, NEGATIVE_CORRELATION, OBJECT, PART_OF,
POSITIVE_CORRELATION, REGULATES, RELATION,
)
from pybel.dsl import (
Abundance, BaseEntity, BiologicalProcess, MicroRna, NamedComplexAbundance, Pathology, Protein,
Expand All @@ -24,6 +17,14 @@
from pybel.testing.utils import n
from pybel.typing import EdgeData

from biokeen.convert import get_triple
from biokeen.convert.converters import (
AssociationConverter, Converter, CorrelationConverter, DecreasesAmountConverter, DrugIndicationConverter,
DrugSideEffectConverter, EquivalenceConverter, IncreasesAmountConverter, IsAConverter,
MiRNADecreasesExpressionConverter, NamedComplexHasComponentConverter, PartOfBiologicalProcess,
PartOfNamedComplexConverter, RegulatesActivityConverter, RegulatesAmountConverter,
)


def _rel(x):
return {RELATION: x}
Expand All @@ -39,6 +40,7 @@ def _assoc(y):

a1 = Abundance('CHEBI', '1')
p1 = Protein('HGNC', '1')
pf1 = Protein('INTERPRO', '1')
d1 = Pathology('MESH', '1')
b1 = BiologicalProcess('GO', '1')
b2 = BiologicalProcess('GO', '2')
Expand All @@ -54,6 +56,7 @@ def _assoc(y):
(AssociationConverter, r1, r2, _rel(ASSOCIATION), ('HGNC:1', 'association', 'HGNC:2')),
(AssociationConverter, r1, r2, _assoc('similarity'), ('HGNC:1', 'similarity', 'HGNC:2')),
(CorrelationConverter, r1, r2, _rel(POSITIVE_CORRELATION), ('HGNC:1', 'positiveCorrelation', 'HGNC:2')),
(IsAConverter, p1, pf1, _rel(IS_A), ('HGNC:1', 'isA', 'INTERPRO:1')),
# Found in ADEPTUS
(CorrelationConverter, d1, r1, _rel(POSITIVE_CORRELATION), ('MESH:1', 'positiveCorrelation', 'HGNC:1')),
(CorrelationConverter, d1, r1, _rel(NEGATIVE_CORRELATION), ('MESH:1', 'negativeCorrelation', 'HGNC:1')),
Expand All @@ -67,9 +70,12 @@ def _assoc(y):
# Found in miRTarBase
(MiRNADecreasesExpressionConverter, m1, r1, _rel(DECREASES), ('MIRBASE:1', 'repressesExpressionOf', 'HGNC:1')),
# Found in DrugBank
(RegulatesActivityConverter, a1, p1, _rela(REGULATES), ('CHEBI:1', 'activityDirectlyRegulatesActivityOf', 'HGNC:1'))
(RegulatesActivityConverter, a1, p1, _rela(REGULATES), ('CHEBI:1', 'activityDirectlyRegulatesActivityOf',
'HGNC:1')),
# Found in ComPath
(EquivalenceConverter, b1, b2, _rel(EQUIVALENT_TO), ('GO:1', 'equivalentTo', 'GO:2')),
(PartOfBiologicalProcess, b1, b2, _rel(PART_OF), ('GO:1', 'partOf', 'GO:2')),
# Found in HSDN
#
]

converters_false_list = [
Expand Down

0 comments on commit 5ae1679

Please sign in to comment.