Skip to main content

Context-Free Grammars and String Assembly Index

PyPI Python CI DOI

assemblycfg places bounds on the assembly index of directed strings and molecules. Upper bounds come from the RePair smallest-grammar algorithm, which quickly finds a short assembly pathway but does not guarantee the shortest. Lower bounds come from LZ factorisation and vector addition chains.

Installation

assemblycfg supports Python 3.12 and later. Install the package from PyPI; its runtime dependencies are installed automatically:

python -m pip install assemblycfg

Plotting is optional. To run the visual examples, install the plot extra:

python -m pip install "assemblycfg[plot]"

Upper bounds

Strings

The central function, repair_with_pathways, returns an upper bound on the assembly index, the virtual objects used along the path, and a NetworkX directed graph representing that path:

import assemblycfg as cfg

length, virtual_objects, path = cfg.repair_with_pathways("abracadabra")
print(f'a("abracadabra") <= {length}')
print(f"Virtual objects used: {virtual_objects}")

Inputs may be a lowercase ASCII string or a list of such strings for a joint assembly path.

With the optional plotting dependency installed, the path can be visualized as follows:

import matplotlib.pyplot as plt
import networkx as nx

nx.draw(path, with_labels=True, font_weight="bold", pos=nx.spring_layout(path))
plt.show()

These graphs can become unwieldy. The AssemblyTheoryTools package provides more sophisticated pathway plotting functions.

Molecules

calculate_assembly_path_graph_repair runs RePair on the molecular graph itself, adapted from the GraphRePair-inspired bound in parallelassemblycpp. It repeatedly joins the most common pair of incident fragments, counting the most occurrences that share no bond, so branched and cyclic motifs can be reused as well as chains:

import assemblycfg as cfg

smiles = "C[C@H](CCCC(C)C)[C@H]1CC[C@@H]2[C@@]1(CC[C@H]3[C@H]2CC=C4[C@@]3(CC[C@@H](C4)O)C)C"
molgraph = cfg.smi_to_nx(smiles)
length, virtual_objects, path = cfg.calculate_assembly_path_graph_repair(molgraph)
print(f"a(Cholesterol) <= {length}")

The virtual objects are NetworkX graphs of the molecular fragments. Ties are broken in atom order, so the default is deterministic; iterations adds passes over random bond orders and keeps the shortest pathway. graph_repair returns the full construction certificate. More complete programs are available in the examples directory.

Lower bounds

LZ factorisation

lz_lower_bound places a valid lower bound on the assembly index of a single directed string:

import assemblycfg as cfg

print(cfg.lz_lower_bound("abracadabra"))  # 7

Read left to right, every assembly pathway builds the string by appending one character or a substring that already occurs earlier in it. The fewest such steps, found by dynamic programming over prefixes, bounds the assembly index from below.

Vector addition chains

vac_lower_bound places a valid lower bound on the assembly index of a string, a molecule, or a list of them (bounding their joint assembly index):

import assemblycfg as cfg

print(cfg.vac_lower_bound("abracadabra"))          # 7
print(cfg.vac_lower_bound(cfg.smi_to_nx("CCO")))   # 6

Counting the copies of each basic unit (characters, or bonds by element pair and bond order) maps an object to a vector, and joining two objects adds their vectors. Every assembly pathway is therefore a vector addition chain of the same length, so the shortest chain reaching the object's vector bounds its assembly index from below.

Chains are solved by vac, which is installed on first use with cargo (install Rust from rustup.rs) into ~/.cache/assemblycfg/vac. Set VAC_PATH to use your own build, or VAC_REF to install a specific branch, tag or commit. Without vac, the function warns and returns the closed-form bounds it would start from.

Package layout

Module Contents
string_repair RePair upper bounds on string assembly index
molecule_repair Graph RePair upper bounds on molecular assembly index
lz LZ factorisation lower bound for strings
vac Vector addition chain lower bounds for strings and molecules
molecules Conversion between SMILES, Molfiles, RDKit and NetworkX

Every public function is also available from the top-level assemblycfg namespace.

Development

Development dependencies use the standardized dependency-groups table. With a recent version of pip:

python -m pip install --upgrade pip
python -m pip install --group dev -e .
python -m pytest
python -m build
python scripts/check_dist.py

Maintainers should follow the version and Trusted Publishing checklist in RELEASING.md.

Citation

If you use this package, cite the archived software release at doi:10.5281/zenodo.20562899. Complete software citation metadata is provided in CITATION.cff.

Metadata

Release files for assemblycfg 2.0.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 assemblycfg 2.0.0
File Size Uploaded
assemblycfg-2.0.0.tar.gz 117.2 kB Details

Built distribution (wheel)

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

Total release size: 180.1 kB

Release files / assemblycfg-2.0.0.tar.gz

Download URL assemblycfg-2.0.0.tar.gz
Size 117.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d14de48577c06abaa6f47cc4bf016d2f19eb8a26bf5bb5165fc57a95c0689d9e
BLAKE2b-256 checksum
How to use checksums
3782c321799c34305e4c6956f88180c9d8d69c15bf792ae0c4bc22935faff33b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

Release files / assemblycfg-2.0.0-py3-none-any.whl

Download URL assemblycfg-2.0.0-py3-none-any.whl
Size 62.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1653ee53d3e7a355ddd903b19e8eb34b2d0f4b3d13befe36b3f93adfe563466
BLAKE2b-256 checksum
How to use checksums
074a7fde283fcc6c5fa9264adaa38268925d00134459df39f0ba0094f38d04c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.2.5

2 release files

1.2.3

2 release files

1.2.2

2 release files

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