Skip to content

Commit

Permalink
Merge pull request #178 from gyorilab/relations_name
Browse files Browse the repository at this point in the history
Add optional source/target name for Relation
  • Loading branch information
bgyori authored Jul 20, 2024
2 parents e864377 + 6500fe1 commit 37095b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/indra_cogex/client/neo4j_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,11 @@ def neo4j_to_relations(neo4j_path: neo4j.graph.Path) -> List[Relation]:
rel_type = neo4j_relation.type
props = dict(neo4j_relation)
source_ns, source_id = process_identifier(neo4j_relation.start_node["id"])
source_name = neo4j_relation.start_node.get("name")
target_ns, target_id = process_identifier(neo4j_relation.end_node["id"])
rel = Relation(source_ns, source_id, target_ns, target_id, rel_type, props)
target_name = neo4j_relation.end_node.get("name")
rel = Relation(source_ns, source_id, target_ns, target_id, rel_type, props,
source_name=source_name, target_name=target_name)
relations.append(rel)
return relations

Expand Down
10 changes: 10 additions & 0 deletions src/indra_cogex/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ def __init__(
target_id: str,
rel_type: str,
data: Optional[Mapping[str, Any]] = None,
source_name: Optional[str] = None,
target_name: Optional[str] = None,
):
"""Initialize the relation.
Expand All @@ -181,13 +183,19 @@ def __init__(
The type of relation.
data :
An optional data dictionary associated with the relation.
source_name :
An optional name for the source node.
target_name :
An optional name for the target node.
"""
self.source_ns = source_ns
self.source_id = source_id
self.target_ns = target_ns
self.target_id = target_id
self.rel_type = rel_type
self.data = data if data else {}
self.source_name = source_name
self.target_name = target_name

def to_json(self) -> RelJson:
"""Serialize the relation to JSON format.
Expand All @@ -204,6 +212,8 @@ def to_json(self) -> RelJson:
"target_id": self.target_id,
"rel_type": self.rel_type,
"data": self.data,
"source_name": self.source_name,
"target_name": self.target_name,
}

def __str__(self): # noqa:D105
Expand Down
13 changes: 13 additions & 0 deletions tests/test_neo4j_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,16 @@ def test_process_identifier():
assert process_identifier("hgnc:6871") == ("HGNC", "6871")
assert process_identifier("chebi:1234") == ("CHEBI", "CHEBI:1234")
assert process_identifier("uploc:SL-0086") == ("UPLOC", "SL-0086")


@pytest.mark.nonpublic
def test_get_source_relations():
nc = _get_client()
relations = nc.get_source_relations(
target=("HGNC", "9875"),
relation="indra_rel",
source_type='BioEntity',
target_type='BioEntity',
)

assert relations[0].target_name == "RASGRF1"

0 comments on commit 37095b5

Please sign in to comment.