Skip to Content
NotebooksFrom a graph to a shortlist

From a graph to a shortlist

Identifier spaces, a collision, and why a ranking is not a set. This page is rendered from a real run on every docs build — every value below was computed by the engine, not typed out. The notebook itself is ordinary Python you can download and run.

marimo edit examples/notebooks/06_from_a_graph_to_a_shortlist.py # as a notebook python examples/notebooks/06_from_a_graph_to_a_shortlist.py # or as a plain script

Read the source on GitHub 


import atexit import json import pathlib import random import shutil import tempfile import marimo as mo import ligase as lg work = pathlib.Path(tempfile.mkdtemp(prefix='ligase-shortlist-')) atexit.register(shutil.rmtree, work, ignore_errors=True) cache = work / 'cache' def shown(text): """Diagnostics name the files they read; the page must not name ours.""" for form in {str(work), str(work.resolve())}: text = text.replace(form, 'work') return text

6 — From a graph to a shortlist

A target-discovery analysis ends in a short list of genes, and gets there by crossing between naming schemes. A knowledge graph names its genes by Ensembl ID. A differential-expression table names them by HGNC symbol. Cutting a shortlist means joining the two, and a join on names from different schemes matches nothing and says nothing.

This notebook runs that whole path: score a graph, map its ranking from Ensembl IDs to symbols, and intersect it with a DEG ranking. Then it shows what the engine refuses, what it reports about the mapping, and why a ranking is not a set.

_rng = random.Random(6) ensembl = [f'ENSG{10000 + 137 * i:011d}' for i in range(30)] symbols = [f'GENE{i:02d}' for i in range(30)] edge_rows = [] for _i in range(90): _from = _i % 30 _a, _b = (ensembl[_from], ensembl[(_from + 1 + _i * 7 % 29) % 30]) edge_rows.append((_a, _b, round(_rng.uniform(0.1, 0.99), 2))) _totals = dict.fromkeys(ensembl, 0.0) for _a, _b, _s in edge_rows: _totals[_a] += _s _totals[_b] += _s _best = sorted(ensembl, key=lambda e: (-_totals[e], e)) _first, _second = (ensembl.index(_best[0]), ensembl.index(_best[1])) symbols[_second] = symbols[_first] def graph_json(sources): nodes = [{'id': e, 'type': 'gene', 'sources': ['open-targets']} for e in ensembl] edges = [{'source': a, 'target': b, 'type': 'interacts_with', 'sources': sources, 'score': s} for a, b, s in edge_rows] return json.dumps({'nodes': nodes, 'edges': edges}) graph_path = work / 'targets.json' graph_path.write_text(graph_json(['open-targets', 'disgenet']), encoding='utf-8') graph_narrow_path = work / 'targets_one_source.json' graph_narrow_path.write_text(graph_json(['open-targets']), encoding='utf-8') graph_reordered_path = work / 'targets_reordered.json' graph_reordered_path.write_text(graph_json(['disgenet', 'open-targets']), encoding='utf-8') map_path = work / 'ensembl_to_hgnc.tsv' map_path.write_text('id\tsymbol\n' + ''.join((f'{e}\t{s}\n' for e, s in zip(ensembl, symbols, strict=True))), encoding='utf-8') _degs = [f'GENE{7 * i % 30:02d}' for i in range(20)] degs_path = work / 'degs.tsv' degs_path.write_text('id\tscore\n' + ''.join((f'{s}\t{20 - i}\n' for i, s in enumerate(_degs))), encoding='utf-8') collided = (ensembl[_first], ensembl[_second], symbols[_first])
graph = lg.knowledge_graph(str(graph_path), organism='Homo sapiens', identifier_namespace='Ensembl gene', subject='target discovery', release='demo 1') graph_reordered = lg.knowledge_graph(str(graph_reordered_path), organism='Homo sapiens', identifier_namespace='Ensembl gene', subject='target discovery', release='demo 1') graph_narrow = lg.knowledge_graph(str(graph_narrow_path), organism='Homo sapiens', identifier_namespace='Ensembl gene', subject='target discovery', release='demo 1') degs = lg.ranked_list(str(degs_path), organism='Homo sapiens', identifier_namespace='HGNC symbol') mapping = lg.entity_set(str(map_path), organism='Homo sapiens', identifier_namespace='Ensembl gene') sources = graph.dimensions['source_set'].value sources_reordered = graph_reordered.dimensions['source_set'].value

A graph is defined by what went into it

Nobody told the loader which evidence sources built this graph. It read them off the edges: open-targets and disgenet. Add a source and edges appear, neighbourhoods widen and every propagated score moves, so two graphs assembled from different sources are not comparable, and nothing in the file says which is which.

The same edges with their sources listed the other way round, disgenet before open-targets, give open-targets and disgenet, the identical value. The order was never information: the source set is a set, so the graphs are the same graph. A real difference in sources is a different graph, and the next cell hands the engine one.

@lg.step def overlap(a: lg.KnowledgeGraph, b: lg.KnowledgeGraph) -> lg.EntitySet: """The nodes two graphs share.""" import json def produce(out: pathlib.Path) -> None: def nodes(g): doc = json.loads(pathlib.Path(g.file).read_text()) return {node['id'] for node in doc['nodes']} both = sorted(nodes(a) & nodes(b)) out.write_text('id\n' + ''.join((f'{node}\n' for node in both))) return lg.write(produce) different = lg.Pipeline(overlap(graph, graph_narrow)).errors() print('refusal, source sets:') print(shown(str(different[0])))

The second graph has the same genes and the same edges, built from open-targets alone. Comparing it with the first is refused before anything runs, for the reason the loader read the sources at all: the difference between two such graphs is a difference in evidence, and a comparison that reported it would present it as biology.

@lg.step def score(graph: lg.KnowledgeGraph) -> lg.RankedList: """Rank nodes by the summed score of the edges they sit on.""" import json def produce(out: pathlib.Path) -> None: totals = {} for edge in json.loads(pathlib.Path(graph.file).read_text())['edges']: for end in (edge['source'], edge['target']): totals[end] = totals.get(end, 0.0) + edge['score'] ranked = sorted(totals.items(), key=lambda item: (-item[1], item[0])) rows = [f'{node}\t{total:.2f}' for node, total in ranked] out.write_text('id\tscore\n' + '\n'.join(rows) + '\n') return lg.write(produce) @lg.step def shortlist(ranking: lg.RankedList, degs: lg.RankedList) -> lg.EntitySet: """The genes both rankings name, as a set.""" def produce(out: pathlib.Path) -> None: def ids(ranked): lines = pathlib.Path(ranked.file).read_text().splitlines()[1:] return [line.split('\t')[0] for line in lines] both = set(ids(ranking)) & set(ids(degs)) out.write_text('id\n' + ''.join((f'{gene}\n' for gene in sorted(both)))) return lg.write(produce) ranking = score(graph) symbol_ranking = lg.map_ranked_identifiers(ranking, mapping, column='symbol', to='HGNC symbol', id_column='id', table_id_column='id') pipeline = lg.Pipeline(shortlist(symbol_ranking, degs)) problems = pipeline.errors()

Three steps, checked before any ran

score turns the graph into a ranking, in Ensembl IDs. map_ranked_identifiers is the conversion for a ranking, and it takes the mapping table as a dataset, so the table’s identifier space is checked like any other input. shortlist intersects the result with the DEG ranking, which is in symbols.

The graph has 3 steps and errors() found 0 problems. The conversion is what makes that true: without it, shortlist would be handed Ensembl IDs and symbols.

first = pipeline.run(cache=cache) print('run 1, a cold cache:', first.summary) print([(s.step, s.outcome) for s in first.steps])

Two genes, one name

ENSG00000010685 and ENSG00000010137 are different genes, and the mapping table gives both the symbol GENE05. That is a collision: two rows cannot share a symbol, and the engine will not guess which gene the analysis meant. It leaves both out and says so. This is what the mapping step recorded in the run report:

  • the identifier ENSG00000010685 maps to a target another identifier also maps to, so it is not in the output
  • the identifier ENSG00000010137 maps to a target another identifier also maps to, so it is not in the output
  • 2 identifiers did not cross into the target space, so the output is 2 entities smaller than the input
  • the ranks were renumbered by the mapping: rank 1 of this ranking was rank 3 of the list that was mapped, and every rank below a dropped row has moved up by as many rows as were dropped above it

These two were the best-connected genes in the graph, so the loss is at the head of the list: the last note says every rank below them moved up. A shortlist that silently lost its two top candidates would look the same as one that never had them.

assert first.steps _lines = degs_path.read_text().splitlines() degs_path.write_text('\n'.join([_lines[0], *_lines[1:][::-1]]) + '\n') second = pipeline.run(cache=cache) print("run 2, the DEG ranking's rows reversed:", second.summary) print([(s.step, s.outcome) for s in second.steps])

Reorder a ranking and it is a different ranking

Run 2 said 3 steps: 1 ran, 2 from cache. The DEG file has the same twenty genes and the same scores, in the opposite order, and shortlist ran again.

That is deliberate. In a ranking the row order is the result: a table sorted best-first and the same table sorted worst-first answer different questions, so Ligase hashes the order in. Only the one step that reads the ranking reran. score and the mapping neither read the DEG file, so they were reused.

_out = next((s for s in first.steps if s.step == 'shortlist')).output shortlist_path = work / 'shortlist.tsv' shortlist_path.write_text(pathlib.Path(_out).read_text()) shortlist_set = lg.entity_set(str(shortlist_path), organism='Homo sapiens', identifier_namespace='HGNC symbol') @lg.step def review(candidates: lg.EntitySet) -> lg.EntitySet: """Hand the shortlist on to the next stage, one symbol per line.""" def produce(out: pathlib.Path) -> None: rows = pathlib.Path(candidates.file).read_text().splitlines()[1:] out.write_text('id\n' + ''.join((f'{row}\n' for row in rows))) return lg.write(produce) review_pipeline = lg.Pipeline(review(shortlist_set)) third = review_pipeline.run(cache=cache) print('run 3, the shortlist set:', third.summary) _lines = shortlist_path.read_text().splitlines() shortlist_path.write_text('\n'.join([_lines[0], *_lines[1:][::-1]]) + '\n') fourth = review_pipeline.run(cache=cache) print("run 4, the shortlist set's rows reversed:", fourth.summary)

Reorder a set and nothing happens

The shortlist is a set: which genes, with no order. Handed to a further step and run cold, it said 1 step: 1 ran. Then the file’s rows were reversed, which makes it a different file, and the rerun said 1 step: 1 from cache.

Ligase canonicalises a set before hashing it, so the reversed file is the same set and there is nothing to recompute. The pair of runs is the difference between the two kinds: a ranking whose order moved was recomputed, a set whose order moved was not. Getting that wrong in either direction is a silent failure, because a ranking treated as a set loses its order, and a set treated as a ranking reruns on every harmless re-export.

wrong = lg.Pipeline(shortlist(ranking, degs)).errors() print('refusal, identifier namespaces:') print(shown(str(wrong[0])))

The join that matches nothing

The pipeline above is the one that ran, with the conversion step cut out: the Ensembl ranking goes straight into shortlist beside a ranking in symbols. Nothing ran.

Without this check the step would run, intersect two sets of strings that share none, and return an empty shortlist, which reads as “no overlap” rather than “wrong names”. The identifier namespace is a dimension like any other here, and map_ranked_identifiers is how you cross it on purpose: it is a typed conversion, declared in the graph, with its losses on the record, not a rename that happens somewhere in a script.

What it printed

refusal, source sets: DimensionError in step `overlap`: a.source_set = open-targets and disgenet (from the sources named on its nodes and edges: work/targets.json) b.source_set = open-targets (from the sources named on its nodes and edges: work/targets_one_source.json) a also lists disgenet. These must match. Was a source added or removed upstream? run 1, a cold cache: 3 steps: 3 ran. [('score', 'ran'), ('map_ranked_identifiers', 'ran'), ('shortlist', 'ran')] run 2, the DEG ranking's rows reversed: 3 steps: 1 ran, 2 from cache. [('score', 'cached'), ('map_ranked_identifiers', 'cached'), ('shortlist', 'ran')] run 3, the shortlist set: 1 step: 1 ran. run 4, the shortlist set's rows reversed: 1 step: 1 from cache. refusal, identifier namespaces: DimensionError in step `shortlist`: ranking.identifier_namespace = Ensembl gene (from step `score`) degs.identifier_namespace = HGNC symbol (declared) These must match. Add a `map_identifiers` step; identifiers are never renamed implicitly.
Last updated on