Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PMID-based querying #146

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/indra_cogex/client/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"get_evidences_for_stmt_hash",
"get_evidences_for_stmt_hashes",
"get_stmts_for_paper",
"get_stmts_for_pubmeds",
"get_stmts_for_pmids",
"get_stmts_for_mesh",
"get_stmts_meta_for_stmt_hashes",
"get_stmts_for_stmt_hashes",
Expand Down Expand Up @@ -951,20 +951,20 @@ def get_stmts_for_paper(
RETURN e.stmt_hash, e.evidence
"""
result = client.query_tx(hash_query, parameter=parameter)
return _run(client=client, result=result, **kwargs)
return _stmts_from_results(client=client, result=result, **kwargs)


@autoclient()
def get_stmts_for_pubmeds(
pubmeds: List[Union[str, int]], *, client: Neo4jClient, **kwargs
def get_stmts_for_pmids(
pmids: List[Union[str, int]], *, client: Neo4jClient, **kwargs
) -> List[Statement]:
"""Return the statements with evidence from the given PubMed ID.
"""Return the statements with evidence from the given PubMed IDs.

Parameters
----------
client :
The Neo4j client.
pubmeds :
pmids :
The PMIDs to query

Returns
Expand All @@ -976,22 +976,22 @@ def get_stmts_for_pubmeds(
-------
.. code-block::

from indra_cogex.client.queries import get_stmts_for_pubmeds
from indra_cogex.client.queries import get_stmts_for_pmids

pubmeds = [20861832, 19503834]
stmts = get_stmts_for_pubmeds(pubmeds)
pmids = [20861832, 19503834]
stmts = get_stmts_for_pmids(pmids)
"""
pubmeds = sorted(f"pubmed:{pubmed}" for pubmed in pubmeds)
pmids = sorted(f"pubmed:{pmid}" for pmid in pmids)
hash_query = f"""\
MATCH (e:Evidence)-[:has_citation]->(p:Publication)
WHERE p.id IN {repr(pubmeds)}
WHERE p.id IN {repr(pmids)}
RETURN e.stmt_hash, e.evidence
"""
result = client.query_tx(hash_query)
return _run(client=client, result=result, **kwargs)
return _stmts_from_results(client=client, result=result, **kwargs)


def _run(client, result, **kwargs) -> List[Statement]:
def _stmts_from_results(client, result, **kwargs) -> List[Statement]:
evidence_map = _get_ev_dict_from_hash_ev_query(result, remove_medscan=True)
stmt_hashes = set(evidence_map.keys())
return get_stmts_for_stmt_hashes(
Expand Down Expand Up @@ -1119,7 +1119,7 @@ def get_stmts_for_stmt_hashes(
"""
logger.info(f"Getting statements for {len(stmt_hashes)} hashes")
rels = client.query_relations(stmts_query, **query_params)
stmts = indra_stmts_from_relations(rels)
stmts = indra_stmts_from_relations(rels, deduplicate=True)

if evidence_limit == 1:
rv = stmts
Expand Down
15 changes: 14 additions & 1 deletion src/indra_cogex/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ def load_statement_json(json_str: str, attempt: int = 1, max_attempts: int = 5)
)


def indra_stmts_from_relations(rels: Iterable[Relation]) -> List[Statement]:
def indra_stmts_from_relations(rels: Iterable[Relation],
deduplicate: bool = True) -> List[Statement]:
"""Convert a list of relations to INDRA Statements.

Any relations that aren't representing an INDRA Statement are skipped.
Expand All @@ -460,6 +461,11 @@ def indra_stmts_from_relations(rels: Iterable[Relation]) -> List[Statement]:
----------
rels :
A list of Relations.
deduplicate :
If True, only unique statements are returned. In some cases
e.g., for Complexes, there are multiple relations for one statement
and this option can be used to return only one of these redundant
statements. Default: True

Returns
-------
Expand All @@ -468,4 +474,11 @@ def indra_stmts_from_relations(rels: Iterable[Relation]) -> List[Statement]:
"""
stmts_json = [load_statement_json(rel.data["stmt_json"]) for rel in rels]
stmts = stmts_from_json(stmts_json)
# Beliefs are not set correctly in the JSON so we fix them here
beliefs = [rel.data["belief"] for rel in rels]
for stmt, belief in zip(stmts, beliefs):
stmt.belief = belief
if deduplicate:
# We do it this way to not change the order of the statements
stmts = list({stmt.get_hash(): stmt for stmt in stmts}.values())
return stmts
6 changes: 3 additions & 3 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,11 @@ def test_get_stmts_for_pmid():


@pytest.mark.nonpublic
def test_get_stmts_for_pmid():
def test_get_stmts_for_pmids():
# Two queries: first evidences, then the statements
client = _get_client()
pubmeds = ["14898026"]
stmts = get_stmts_for_pubmeds(pubmeds, client=client)
pmids = ["14898026"]
stmts = get_stmts_for_pmids(pmids, client=client)
assert stmts
assert isinstance(stmts[0], Statement)

Expand Down
Loading