Skip to content
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
12 changes: 8 additions & 4 deletions src/logic_network_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4041,17 +4041,21 @@ def export_leaf_refs(nodes_file: str, output_file: str) -> None:
"""Write leaf_refs.csv (deltasignal specs/107): every leaf named in the
``member_leaves`` of ``nodes_file`` (this bundle's nodes.csv) that has a
reference entity, with that reference entity's stable id, so a consumer can tell
that GRIP2 at the plasma membrane and GRIP2 in the cytosol are one protein.
that GRIP2 at the plasma membrane and GRIP2 in the cytosol are one protein, and
the leaf's modified residues (``modifications``, empty when it has none;
amendment 1), so a consumer can tell a compartment copy from a modified form.
Always written (a header alone when no leaf has a reference entity), so a
missing file means an older bundle, not "no references"."""
from src.neo4j_connector import get_leaf_references
from src.neo4j_connector import get_leaf_references, get_leaf_modifications
leaves: set[str] = set()
nodes = pd.read_csv(nodes_file, dtype=str, keep_default_na=False)
for v in nodes.get("member_leaves", pd.Series(dtype=str)):
leaves.update(x for x in str(v).split("|") if x)
refs = get_leaf_references(leaves)
rows = [{"leaf_stid": leaf, "reference_stid": refs[leaf]} for leaf in sorted(refs)]
pd.DataFrame(rows, columns=["leaf_stid", "reference_stid"]).to_csv(output_file, index=False)
mods = get_leaf_modifications(set(refs)) if refs else {}
rows = [{"leaf_stid": leaf, "reference_stid": refs[leaf], "modifications": mods.get(leaf, "")}
for leaf in sorted(refs)]
pd.DataFrame(rows, columns=["leaf_stid", "reference_stid", "modifications"]).to_csv(output_file, index=False)
logger.info(f"Exported {len(rows)} leaf references to {output_file}")


Expand Down
15 changes: 15 additions & 0 deletions src/neo4j_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,21 @@ def get_leaf_references(stable_ids) -> Dict[str, str]:
return {r["s"]: r["ref"] for r in rows if r["ref"]}


def get_leaf_modifications(stable_ids) -> Dict[str, str]:
"""Each of ``stable_ids`` that carries modified residues, mapped to their display
names, sorted and joined by "; " ("O-phospho-L-serine at 465; O-phospho-L-serine
at 467"): deltasignal specs/107 amendment 1. Two copies of one reference entity
with the same modifications differ only in compartment; SMAD2 and p-SMAD2 do not.
Entities without modified residues are left out."""
ids = sorted({s for s in stable_ids if s})
if not ids:
return {}
rows = get_graph().run(
"UNWIND $ids AS s MATCH (e:PhysicalEntity {stId: s})-[:hasModifiedResidue]->(m) "
"RETURN s, collect(m.displayName) AS mods", ids=ids).data()
return {r["s"]: "; ".join(sorted(x for x in r["mods"] if x)) for r in rows if r["mods"]}


def get_pathogen_entities(stable_ids) -> Dict[str, Dict[str, str]]:
"""The entities among ``stable_ids`` that are PATHOGEN-DERIVED, with their
schema class and display name (deltasignal specs/048).
Expand Down
15 changes: 11 additions & 4 deletions tests/test_provenance_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,21 +520,28 @@ def refs(stids):
seen["ids"] = set(stids)
return {"R-HSA-GM": "uniprot:Q9C0E4", "R-HSA-GC": "uniprot:Q9C0E4", "R-ALL-PE": "chebi:57613"}
monkeypatch.setattr(neo4j_connector, "get_leaf_references", refs)
monkeypatch.setattr(neo4j_connector, "get_leaf_modifications",
lambda stids: {"R-HSA-GC": "O-phospho-L-serine at 465; O-phospho-L-serine at 467"})
nodes = tmp_path / "nodes.csv"
pd.DataFrame([{"uuid": "u1", "node_kind": "simple_entity", "member_leaves": "R-HSA-GM"},
{"uuid": "u2", "node_kind": "simple_complex", "member_leaves": "R-HSA-GC|R-ALL-PE|R-HSA-X"},
{"uuid": "u3", "node_kind": "reaction", "member_leaves": ""}]).to_csv(nodes, index=False)
out = tmp_path / "leaf_refs.csv"
m.export_leaf_refs(str(nodes), str(out))
assert seen["ids"] == {"R-HSA-GM", "R-HSA-GC", "R-ALL-PE", "R-HSA-X"}
df = pd.read_csv(out)
assert list(df.columns) == ["leaf_stid", "reference_stid"]
df = pd.read_csv(out, dtype=str, keep_default_na=False)
assert list(df.columns) == ["leaf_stid", "reference_stid", "modifications"]
# GM and GC share a reference entity; only the modifications tell a compartment
# copy from a modified form (specs/107 amendment 1)
assert list(df.itertuples(index=False, name=None)) == [
("R-ALL-PE", "chebi:57613"), ("R-HSA-GC", "uniprot:Q9C0E4"), ("R-HSA-GM", "uniprot:Q9C0E4")]
("R-ALL-PE", "chebi:57613", ""),
("R-HSA-GC", "uniprot:Q9C0E4", "O-phospho-L-serine at 465; O-phospho-L-serine at 467"),
("R-HSA-GM", "uniprot:Q9C0E4", "")]
# nothing with a reference: the header alone, so the file still exists
monkeypatch.setattr(neo4j_connector, "get_leaf_references", lambda s: {})
m.export_leaf_refs(str(nodes), str(out))
assert list(pd.read_csv(out).columns) == ["leaf_stid", "reference_stid"] and len(pd.read_csv(out)) == 0
assert list(pd.read_csv(out).columns) == ["leaf_stid", "reference_stid", "modifications"]
assert len(pd.read_csv(out)) == 0


# --- rnas.csv (deltasignal specs/100) ------------------------------------------
Expand Down
Loading