From e9483e92c60b016a73c7e54de1edb983ba59f0a7 Mon Sep 17 00:00:00 2001 From: Adam Wright <668955+adamjohnwright@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:44:31 -0400 Subject: [PATCH] leaf_refs.csv: a modifications column (each leaf's modified residues), so a consumer can tell a compartment copy from a modified form (deltasignal specs/107 amendment 1: SMAD2 and p-SMAD2 share a reference entity) Co-Authored-By: Claude Opus 5.5 --- src/logic_network_generator.py | 12 ++++++++---- src/neo4j_connector.py | 15 +++++++++++++++ tests/test_provenance_exports.py | 15 +++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/logic_network_generator.py b/src/logic_network_generator.py index 73c53bc..f6b42e8 100755 --- a/src/logic_network_generator.py +++ b/src/logic_network_generator.py @@ -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}") diff --git a/src/neo4j_connector.py b/src/neo4j_connector.py index d211da5..3fa5edf 100755 --- a/src/neo4j_connector.py +++ b/src/neo4j_connector.py @@ -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). diff --git a/tests/test_provenance_exports.py b/tests/test_provenance_exports.py index 5da2f51..9bda891 100644 --- a/tests/test_provenance_exports.py +++ b/tests/test_provenance_exports.py @@ -520,6 +520,8 @@ 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"}, @@ -527,14 +529,19 @@ def refs(stids): 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) ------------------------------------------