Skip to main content

libsize

A library, a culture or an ecosystem holds some number of distinct types – genotypes, molecules, species. Sequencing draws a sample, and the sample shows fewer types than exist because the rare ones are missed. libsize estimates U(n), the number of distinct types that would be seen in a draw of n, from a sample much smaller than that.

It was written for DGRec mutagenesis libraries, but nothing in it is specific to them: the same question comes up for error-prone PCR and site-saturation libraries in directed evolution, for guide and variant libraries in massively parallel assays, and for species richness in ecology.

Install

pip install libsize

The core needs only numpy and scipy. The DGRec LSTM adapter additionally needs the dgrec package and TensorFlow:

pip install "libsize[dgrec]"

The development version lives at gitlab.pasteur.fr/synbiolab/libsize:

pip install git+https://gitlab.pasteur.fr/synbiolab/libsize.git

From a sample

Given counts per type – molecules per genotype, reads per variant – fit the rank-abundance tail and extrapolate. G is the number of types the process can reach; it is a hard ceiling, so count the positions that actually vary.

from libsize import estimate

estimate(counts, G=4**14, N=10**8)
# {'library_size': 3114026, 'B': 3, 'alpha': 1.296, ...}

Use method="mle" (the default). method="leastsq" reproduces the fit used for the published DGRec library sizes and is kept for comparison, not for use – it is the outlier against every other method here, by a factor of twenty at a 2e4-fold extrapolation.

From a model of the process

If you can model the process that generates the types, you do not have to assume a shape for the tail at all – the model scores types it has never seen, and U(n) follows by importance sampling. For a DGRec template repeat:

from libsize import DgrecLSTM, estimate_from_model

model = DgrecLSTM("TTGTGGCCTTTATCTTCTACGTAGTGAGGATCTCTCAGCGTATGGTTGTCGCCTGAGCTGTAGTTGCCTT")
estimate_from_model(model, n=93_471_023, n_samples=15_000, seed=0)
# {'library_size': 2130948.0, 'se': 181011.0, 'model': 'dgrec-lstm:57585fba7123', ...}

n counts mutagenized molecules, not cells: the model describes the mutational profile given that conversion happened, not the rate of conversion.

For any other process, implement sample(k) and logprob(types) and estimate_from_model works unchanged – see generative.

The result carries the model’s identity, and that is not decoration. Retraining the DGRec LSTM – a change nobody treated as a change of method – moved U(n) at the full extrapolation by a third. An estimate from a model is not interpretable without naming the model.

Check before you trust

An extrapolated library size is easy to get wrong by orders of magnitude. The bounds module holds cheap checks that rule whole answers out:

from libsize import good_turing_coverage, chao1, predictable_range, class_floor

good_turing_coverage(counts)   # how much mass the sample actually covers
predictable_range(counts)      # past this depth, no sample-based answer is supported
class_floor(mols, sizes)       # a lower bound needing no extrapolation at all

The limit is worth stating plainly: no estimator reading the tail from the sample can see beyond about t ~ log n further samples per sample drawn, and that bound is provably optimal (Orlitsky, Suresh & Wu 2016). smoothed_good_toulmin implements the estimator that attains it; ask it for more and it returns a negative number rather than a wrong one. Past that range, an answer rests on an assumed functional form or on a model of the process – not on the data.

Status

Early. The API may move. The evidence behind the choices here – the comparison against preseq, the limits, the prior art in directed evolution and immune repertoires – lives in the complexity-estimation-kb knowledge base.

Metadata

Release files for libsize 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libsize 0.1.0
File Size Uploaded Provenance Actions
libsize-0.1.0.tar.gz 16.6 kB Without provenance Details for libsize-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for libsize 0.1.0
File Interpreter ABI Platform Actions
libsize-0.1.0-py3-none-any.whl Python 3 none any Details for libsize-0.1.0-py3-none-any.whl

Total release size: 32.9 kB

Release files / libsize-0.1.0.tar.gz

Download URL libsize-0.1.0.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9020f2cba7766d1de7a7a97f1164508eb8df309e1834323f3809fdf07d7ecbae
BLAKE2b-256 checksum
How to use checksums
8853c68bceb9244e15cbcacbe9946b58034ae7542f956d79ce1882bf71018b6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.9

Release files / libsize-0.1.0-py3-none-any.whl

Download URL libsize-0.1.0-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e33a284a0ebcd832500c59324fdbba0368e6fac5a6e6c1d0282fef8fb2ac0f0
BLAKE2b-256 checksum
How to use checksums
638ee4de57b8627781496e6ed7a50c25d250bc5098b0aefcb58d31bc880617a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.9

Security for libsize 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page