A screen, and its seed
What a name in a molecule table means, and what a seed is worth. 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/07_a_screen_and_its_seed.py # as a notebook
python examples/notebooks/07_a_screen_and_its_seed.py # or as a plain scriptimport atexit
import pathlib
import shutil
import tempfile
import marimo as mo
import ligase as lg
work = pathlib.Path(tempfile.mkdtemp(prefix='ligase-screen-'))
atexit.register(shutil.rmtree, work, ignore_errors=True)
cache = work / 'cache'
_fixtures = pathlib.Path(__file__).resolve().parents[2].joinpath('crates', 'ligase-core', 'tests', 'fixtures')
for _name in ('library', 'hits_salts', 'no_representation'):
shutil.copy(_fixtures / f'molecules_{_name}.tsv', work / f'{_name}.tsv')
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 text7 — A screen, and its seed
A virtual screen scores a library of compounds, ranks them, and buys the top of the list. Two things in that path fail without a sound. The first is what a compound’s name means: the same substance has a different InChIKey depending on what was done to it before the key was computed. The second is the dice: scoring tools draw on a random seed, and a result nobody can regenerate is not quite a result.
This notebook reads the first off the files, refuses a set that disagrees about it, and then runs a seeded step: cached, rerun when the seed moves, and audited for what a seed is worth.
There is no docking engine in the docs build, and no RDKit, so the scoring step here is not docking. It perturbs each compound’s measured activity with a draw from the seed. The subject of this page is the seed, not the scoring function, and a step that did less than it claimed would be a lie on a page about not lying.
library = lg.molecule_set(str(work / 'library.tsv'), organism='Homo sapiens')
hits = lg.molecule_set(str(work / 'hits_salts.tsv'), organism='Homo sapiens')
library_representation = library.dimensions['representation']
hits_representation = hits.dimensions['representation']A name is only as good as its preparation
The first line of the library file is not data:
#ligase representation: inchikey; salt-stripped, charge-neutralised
That is where the representation dimension is read from. Ligase
reports it as InChIKey, salt-stripped and charge-neutralised. The table of hits
declares itself differently: InChIKey, as given. Both
are columns of twenty-seven-character keys, and nothing in the
columns says which was prepared how. The line above the header is the
only place that is written down.
@lg.step
def enrichment(library: lg.MoleculeSet, hits: lg.MoleculeSet) -> lg.MoleculeSet:
"""The compounds both tables name."""
import pathlib
def produce(out: pathlib.Path) -> None:
def keys(table):
rows = pathlib.Path(table.file).read_text().splitlines()
rows = [row for row in rows if not row.startswith('#')][1:]
return {row.split('\t')[0] for row in rows}
both = sorted(keys(library) & keys(hits))
out.write_text('inchikey\n' + ''.join((f'{key}\n' for key in both)))
return lg.write(produce)
refused = lg.Pipeline(enrichment(library, hits)).errors()
print('refusal, representations:')
print(shown(str(refused[0])))
def _rows(path):
rows = path.read_text().splitlines()
return [row.split('\t') for row in rows if not row.startswith('#')][1:]
_library_rows = _rows(work / 'library.tsv')
_hit_rows = _rows(work / 'hits_salts.tsv')
_library_keys = {row[0] for row in _library_rows}
found = sum((row[0] in _library_keys for row in _hit_rows))
hit_total = len(_hit_rows)
hit_names = [row[2] for row in _hit_rows]
library_names = [row[2] for row in _library_rows]
by_name = sum((any((hit.startswith(name) for name in library_names)) for hit in hit_names))Two sets prepared differently are not joined
The library is salt-stripped and charge-neutralised. The hits are as they came. The engine found 1 problem and ran nothing.
The library names aspirin, ibuprofen, warfarin, metformin. The hits are aspirin sodium, metformin hydrochloride. By name, 2 of the 2 hits are compounds the library holds. By InChIKey, 0 of the 2 are found in the library. The names agree and the keys do not, because a sodium salt and a hydrochloride have keys of their own. A join on the keys would have reported 0, and a low hit rate reads as poor enrichment when it is a measurement of the standardiser.
The engine does not fix this. The fix is
lg.chem.standardize, which writes the state it arrived at onto its
own output, so that the next step is checked against something true
rather than a claim nobody verified. It needs RDKit, which is the
chem extra, and this page does not call it: loading, checking and
hashing molecule tables all work on a default install, and producing
a standardised set does not.
no_declaration = lg.molecule_set(str(work / 'no_representation.tsv'), organism='Homo sapiens')
print('dimensions of a table with no declaration:', sorted(no_declaration.dimensions))
print('its representation:', no_declaration.dimensions.get('representation'))
unknown = lg.Pipeline(enrichment(library, no_declaration)).errors()
print('refusal, an undeclared representation:')
print(shown(str(unknown[0])))No declaration is not “as given”
A table of InChIKeys with no #ligase representation: line loads, and
carries no representation at all: the dimension is unknown. It is
not filled in with the weakest state, and the engine found
1 problem when that table met a declared one. It
names the undeclared dimension and asks for it to be declared where
the data is loaded, or for the table to be standardised.
The refusal is the point. A ChEMBL download is standardised and a vendor list is not, and the keys cannot say which this is.
def draw(table, out, seed):
"""Score every compound: its measured activity, plus a draw from the seed."""
import math
import random
rng = random.Random(seed)
rows = pathlib.Path(table.file).read_text().splitlines()
declaration = rows[0]
header, *body = rows[1:]
columns = header.split('\t')
key, name, activity = (columns.index(c) for c in ('inchikey', 'name', 'activity_nm'))
scored = []
for row in body:
cells = row.split('\t')
score = -(10 - math.log10(float(cells[activity]))) + rng.uniform(-1, 1)
scored.append((round(score, 2), cells[key], cells[name]))
scored.sort(key=lambda item: (item[0], item[1]))
lines = [declaration, 'inchikey\tname\tscore']
lines += [f'{k}\t{n}\t{s}' for s, k, n in scored]
out.write_text('\n'.join(lines) + '\n')
@lg.step(stochastic=True, seed='seed')
def rescore(library: lg.MoleculeSet, *, seed: int) -> lg.MoleculeRanking:
"""Perturb each compound's measured activity with a seeded draw."""
return lg.write(lambda out: draw(library, out, seed))
@lg.step
def top(ranking: lg.MoleculeRanking, *, k: int) -> lg.MoleculeSet:
"""The k best-ranked compounds, as a set."""
def produce(out: pathlib.Path) -> None:
rows = pathlib.Path(ranking.file).read_text().splitlines()
declaration, _header, *body = rows
chosen = sorted((row.split('\t')[0] for row in body[:k]))
out.write_text(declaration + '\ninchikey\n' + ''.join((f'{key}\n' for key in chosen)))
return lg.write(produce)
def screen(seed):
return lg.Pipeline(top(rescore(library, seed=seed), k=2))pipeline = screen(20260101)
problems = pipeline.errors()
first = pipeline.run(cache=cache)
print('run 1, a cold cache:', first.summary)
again = pipeline.run(cache=cache)
print('run 2, the same seed:', again.summary)assert first.steps
assert again.steps
moved = screen(20260102).run(cache=cache)
print('run 3, the seed changed:', moved.summary)
print([(s.step, s.outcome) for s in moved.steps])The same declared seed is the same run
The screen has 2 steps, rescore and top, and
errors() found 0 problems. top accepts what
rescore wrote because rescore writes the representation line
itself, carried over from the library it was given.
Run 1 said 2 steps: 2 ran. Run 2 repeated it and said 2 steps: 2 from cache. The seed is a parameter like any other: it is in the step’s cache key, so the same seed finds the same entry.
Run 3 changed the seed and said 2 steps: 2 ran. A different seed
is a different run, so rescore ran again, and top, which
reads the ranking it wrote, ran with it. Where an unchanged output
stops the steps below it from rerunning is shown with a real report in
a cohort, and what changed.
audit = pipeline.audit('rescore', cache=cache)
print('audit, a declared seed:')
print(audit.text)A seeded step is reproducible, not deterministic
ligase audit runs a step twice and compares. The verdict for rescore
is reproducible.
It is not deterministic, though the two runs wrote the same bytes,
and that is on purpose. A step that declares itself stochastic
promises that its seed is what controls its randomness, and nothing
the engine can observe proves it: a tool with a seed flag it ignores
would write identical bytes on a lucky pair of runs too. So the best
a declared step can earn is reproducible, which says with this
seed, you get this result. It cannot earn the stronger word.
def undeclared_screen():
@lg.step
def rescore(library: lg.MoleculeSet, *, seed: int) -> lg.MoleculeRanking:
"""The same work, with nothing said about where its randomness is."""
return lg.write(lambda out: draw(library, out, seed))
return lg.Pipeline(rescore(library, seed=20260101))
undeclared = undeclared_screen().audit('rescore', cache=cache / 'undeclared')
print('audit, the same body with nothing declared:')
print(shown(undeclared.text))The same body, the same seed, the same library, with the declaration removed, audits as deterministic.
Nothing about the computation changed. What changed is what the author said. Undeclared, the engine takes the two identical runs at face value. Declared, it knows the step was written to be random, so it reports the weaker and more honest thing: not that nothing varies, but that the seed pins it. The pair is the whole of the difference between a claim and a guess, and what a methods section can then say about the seed, and have the engine bind, is the subject of what a paper needs.
What it printed
refusal, representations:
DimensionError in step `enrichment`:
library.representation = InChIKey, salt-stripped and charge-neutralised (from the representation line the standardiser wrote: work/library.tsv)
hits.representation = InChIKey, as given (from the representation line the standardiser wrote: work/hits_salts.tsv)
library also lists salt-stripped and charge-neutralised.
These must match. Were the two sets standardised differently? Add a `standardize` step; molecules are never re-keyed implicitly.
dimensions of a table with no declaration: ['organism']
its representation: None
refusal, an undeclared representation:
UndeclaredDimensionError in step `enrichment`:
representation is unknown in work/no_representation.tsv (this table names its molecules by InChIKey but does not say what standardisation produced them, and the keys cannot say -- declare representation, or standardise the table)
`enrichment` needs representation to agree. Declare it where you load the data.
run 1, a cold cache: 2 steps: 2 ran.
run 2, the same seed: 2 steps: 2 from cache.
run 3, the seed changed: 2 steps: 2 ran.
[('rescore', 'ran'), ('top', 'ran')]
audit, a declared seed:
reproducible `rescore`
the same bytes both times b25ed14d814f
canonical form 90dcc910048b
because `seed` was held at 20260101, not because the step cannot wobble
1 step: 1 reproducible.
audit, the same body with nothing declared:
deterministic `rescore`
the same bytes both times b25ed14d814f
canonical form 90dcc910048b
1 step: 1 deterministic.