Skip to main content

FoldMatch

Version 0.14.0

Overview

FoldMatch is a Python toolkit to encode macromolecular 3D structures into fixed-length vector embeddings for efficient large-scale structure similarity search and clustering.

Reference: Multi-scale structural similarity embedding search across entire proteomes.

A web-based implementation using this tool for structure similarity search is available at RCSB.org under 3D Similarity.

If you are interested in training a new model with a new structure dataset, visit the rcsb-embedding-search repository, which provides scripts and documentation for training.

Features

  • Residue-level embeddings computed using the ESM3 protein language model
  • Sequence-based embeddings from FASTA files without requiring 3D structures
  • Structure-level embeddings aggregated via a transformer-based aggregator network
  • Fast and efficient FAISS-based similarity search
  • Two-stage sequence search — an embedding prefilter followed by exact pairwise Smith-Waterman alignment on FoldMatch's native SIMD kernel, reporting sequence identity, coverage, and calibrated E-values over selectable output columns
  • Structural clustering: Leiden on the similarity graph (python-igraph, or NetworKit's multithreaded ParallelLeiden as an optional backend), greedy representatives in one streamed pass, or greedy representatives then Leiden on the representatives' graph (--method greedy-leiden), resumable from a work folder, for collections whose direct graph does not fit in memory; FAISS search on the CPU or on every visible GPU
  • Command-line interface implemented with Typer for high-throughput inference workflows
  • Python API for interactive embedding computation and integration into analysis pipelines
  • High-performance inference leveraging PyTorch Lightning, with multi-node and multi-GPU support

Installation

From PyPI

pip install foldmatch

From Source (Development)

git clone https://github.com/rcsb/foldmatch.git
cd foldmatch
pip install -e .

Requirements:

  • Python ≥ 3.12
  • ESM 3.2.3
  • Lightning 2.6.1
  • Typer 0.24.1
  • Biotite 1.7.1
  • FAISS 1.15.1 (the faiss-cpu wheel)
  • igraph 1.0.0
  • PyTorch with CUDA support (recommended for GPU acceleration)
  • A C compiler (GCC 12 or later, or Clang 14 or later) when FoldMatch is built from source (an install from the sdist, pip install -e .): it compiles the native Smith-Waterman kernel of the sequence search's Stage 2. The platform wheels (Linux x86_64 and aarch64, macOS arm64 and x86_64) include it. Without a compiler the install still succeeds, with a warning, as a pure-Python package in which Stage 2 refuses to run and prints the reinstall command; everything else works.

Optional Dependencies:

  • faiss-gpu for GPU-accelerated similarity search (instead of faiss-cpu)
  • pip install 'foldmatch[networkit]' (NetworKit 11.3) for fm-search cluster --leiden-backend networkit: wheels for Linux x86_64, macOS 26 arm64 and Windows, none for Linux aarch64 or macOS before 26
  • a FAISS built on MKL, for faster ivf_pq builds of large databases on Linux x86_64 with Python 3.12 (see FAISS threads below)

FAISS threads

FoldMatch installs the faiss-cpu 1.15.1 wheel. On Linux the wheel bundles an OpenMP build of OpenBLAS (faiss_cpu.libs/libopenblaso-*.so) and its own libgomp. FAISS's BLAS work therefore runs on FAISS's OpenMP threads: the k-means and OPQ steps of an ivf_pq training, the IVF assignment of its adds, and batched flat searches. Earlier FoldMatch versions pinned faiss-cpu 1.13.2, whose OpenBLAS is single-threaded. With it, a 30-million-entry ivf_pq index took about 7 hours on any node.

  • Choosing the threads. Export OMP_NUM_THREADS to set FAISS's threads. It also sets the threads of FAISS's OpenBLAS. OPENBLAS_NUM_THREADS has no effect on FAISS's OpenBLAS; it changes only the OpenBLAS copies of numpy and scipy. When OMP_NUM_THREADS is unset, fm-search build sequences-index gives FAISS the physical cores it may use and logs the count.
  • Build times. On NERSC Perlmutter, fm-search build sequences-index --residue-codes over 30.4 million sequences built the ivf_pq index in 1 h 45 min on a GPU node (one 64-core AMD EPYC 7763, 64 threads; 2 h 47 min for the whole command) and in 2 h 37 min on a CPU node (two 64-core EPYC 7763, 128 threads; 3 h 33 min). Most of the difference is the training, 40 min against 1 h 16 min: the OPQ step of the wheel's training does not gain from more threads. In a benchmark on the CPU node, two OPQ iterations took 66 to 70 s at 4 to 16 threads and 173 s at 128, while 10 iterations of the IVF k-means gained up to about 32 threads.
  • Recommended on large nodes. With the wheel, run build sequences-index with OMP_NUM_THREADS=64 on a node with more than 64 physical cores, such as a 128-core CPU node. The setting applies to the whole command. This advice rests on the 64-thread GPU-node build above and on the OPQ benchmark (121 s at 64 threads against 173 s at 128); a full build at 64 threads on a 128-core node was not timed. With it, build sequences-index warns that FAISS has fewer threads than physical cores; with the wheel that warning is expected. With the MKL build below, leave OMP_NUM_THREADS unset: it built a 30-million-entry index in 43 minutes at 128 threads.
  • 128 BLAS threads at most. The x86_64 wheel's OpenBLAS is built with MAX_THREADS=128, so FAISS's BLAS work runs on at most 128 threads. That OpenBLAS also lowers a higher OpenMP thread count to 128 when FAISS calls it. On a node with more than 128 physical cores, FAISS therefore runs 128 threads, and build sequences-index warns that FAISS has fewer threads than physical cores.
  • Reproducibility. Two hnsw or ivf_pq builds are byte-identical only at OMP_NUM_THREADS=1. The wheel's k-means results also change with the thread count. OPENBLAS_NUM_THREADS=1 alone does not make FAISS single-threaded. With the wheel, search results can change with FAISS's thread count too, in the last digits of the scores and so in the order of tied hits: between 32 and 256 threads, about a quarter of 1,000 batched flat searches and 3 to 5 ivf_pq searches were reordered. For bit-identical reruns, set OMP_NUM_THREADS to the same value (unset, FAISS gets each node's core count).
  • Existing databases. FAISS 1.13.2 and 1.15.1 read each other's index files. Searching one database with either version gives the same hits up to float rounding (flat and ivf_pq scores within about 3e-7), which can reorder tied hits and swap hits tied at the cut-off; with hnsw, 5 to 7% of 1,000 searches changed at least one hit. A rebuilt ivf_pq index is a different, equally valid index: at 30 million entries, rebuilds shared 57 to 59% of their top-100 hits with each other (1.13.2 against the MKL build below, the wheel against the MKL build, and two wheel builds at 64 and 128 threads), with about the same self-hit rates. Searches whose results you compare should all use the same database.
  • Start-up log. On Linux x86_64, import faiss logs at INFO Could not load library with AVX2 support due to: ModuleNotFoundError("No module named 'faiss.swigfaiss_avx2'"), then Successfully loaded faiss.. This is expected: the 1.15.1 wheel has one library, which picks the CPU's instructions at run time (its compile options include DD).
  • macOS. The macOS wheel uses Apple Accelerate for BLAS and bundles its own libomp, so FoldMatch keeps OMP_NUM_THREADS=1 there by default (see macOS notes).

Optional: FAISS with MKL. For Linux x86_64 and Python 3.12 only, the official FAISS conda channel, pytorch, publishes a faiss-cpu 1.15.1 build on MKL. It builds large ivf_pq indexes faster than the wheel, mostly because its OPQ training is faster. On one NERSC Perlmutter GPU node (64 cores), build sequences-index --residue-codes over 30.4 million sequences took 2 h 02 min with it, of which 59 min was the index (the wheel: 2 h 47 min and 1 h 45 min). In a FAISS-only script on one CPU node (2× AMD EPYC 7763), with 128 threads bound to cores (OMP_PROC_BIND=spread OMP_PLACES=cores; FoldMatch does not bind threads), it built a 30-million-entry ivf_pq index in 43 minutes. The commands below replace the wheel with the conda build. They were tested on NERSC, in a conda env (Python 3.12) in which FoldMatch and its dependencies had been installed with pip:

python -m pip uninstall -y faiss-cpu
conda install -y --no-deps --override-channels -c pytorch -c conda-forge \
  "pytorch::faiss-cpu=1.15.1=py3.12_hf65b397_0_cpu" "pytorch::libfaiss=1.15.1=py3.12_hf65b397_0_cpu" \
  "conda-forge::mkl=2025.3.1=h0e700b2_12" "conda-forge::onemkl-license=2025.3.1=hf2ce2f3_12" \
  "conda-forge::tbb=2023.1.0=haa8c3ee_2" "conda-forge::libhwloc=2.15.0=default_hd7c4d0c_1001" \
  "conda-forge::libxml2=2.15.4=h7df9aa5_1" "conda-forge::libxml2-16=2.15.4=hf3af7cc_1" \
  "conda-forge::libiconv=1.18=h0cb94f2_3" "conda-forge::llvm-openmp=23.1.4=h7148c6a_0" \
  "conda-forge::_openmp_mutex=4.5=8_kmp_llvm" "conda-forge::libsvs-runtime=0.4.0=g7f7c639_0_0" \
  "conda-forge::python_abi=3.12=9_cp312" "conda-forge::libgcc=16.2.0=ha9f2e26_7" \
  "conda-forge::libgcc-ng=16.2.0=h69a702a_7" "conda-forge::libgomp=16.2.0=he0feb66_7" \
  "conda-forge::libstdcxx=16.2.0=h934c35e_7"
python -c "import faiss; print(faiss.__version__, faiss.get_compile_options())"   # 1.15.1 OPTIMIZE AVX2 ...
  • Why --no-deps. It keeps conda from replacing pip's numpy and its BLAS, but conda then checks nothing. The listed packages are what libfaiss and MKL needed in that env, which had the rest; libfaiss needs libgcc 16 or later. In another env, a library still missing makes import faiss fail with an ImportError that names it.
  • Reinstalling FoldMatch. The conda package registers as the distribution faiss, not faiss-cpu, so pip check reports foldmatch ... requires faiss-cpu, which is not installed. That is expected. Reinstall or upgrade FoldMatch in this env only with pip install --no-deps .... Without --no-deps, pip installs the faiss-cpu wheel over the conda FAISS.
  • Updating the env. Never run conda update, or a conda install without --no-deps, in this env: conda's view of it is inconsistent on purpose. Repeat the --no-deps form instead.
  • OpenMP runtime. It depends on the import order. FoldMatch's commands load torch first, so torch's libgomp serves FAISS and MKL. A script that imports faiss first gets LLVM's libomp. Both work.
  • Thread logs. threadpoolctl 3.6.0 does not list this MKL, so the index phase's Thread pool: lines do not show FAISS's BLAS.

Usage

The package provides two main interfaces:

  1. Command-line Interface (CLI) for batch processing and high-throughput workflows
  2. Python API for interactive use and integration into custom pipelines

Command-Line Interface (CLI)

The toolkit ships two CLIs, fm-embedding and fm-search. Each is invoked with --help for full option documentation; the canonical examples below are enough to get started.

fm-embedding — compute embeddings

Two subcommand groups reflect input modality:

# Output folders must exist; give each kind of embedding its own (residue and chain files share names)
mkdir -p out/residue out/chain out/assembly out/seq-residue out/seq-chain

# Residue / chain / assembly embeddings from a folder of 3D structures
fm-embedding from-structures residue  --src-folder data/pdb --output-folder out/residue  --structure-format mmcif
fm-embedding from-structures chain    --src-folder data/pdb --output-folder out/chain    --structure-format mmcif
fm-embedding from-structures assembly --src-folder data/pdb --output-folder out/assembly --structure-format mmcif

# Residue / chain embeddings from protein sequences in a FASTA file (no 3D required)
fm-embedding from-sequences  residue  --fasta-file seqs.fasta --output-folder out/seq-residue
fm-embedding from-sequences  chain    --fasta-file seqs.fasta --output-folder out/seq-chain

# One-shot model download
fm-embedding download-models

Assembly-level embeddings are only available under from-structures — there is no assembly concept for a bare sequence.

Each FASTA record's embedding is stored under its id, the first word of its header, so from-sequences refuses a repeated id before ESM3 is loaded. With --output-format csv or pt (one file per record or chain), an id is a file name: one that holds / or cannot name a file, ids that the output folder takes for one file (abc and ABC on macOS's default file system), and a --fasta-file named like an output are refused too, and a run stops at a second write of one file (chains A and a of one structure on macOS). Parquet and JSON keep any id. A rerun into the same folder replaces its own files, as before.

from-sequences residue and from-sequences chain take --output-codes-folder FOLDER: the same ESM3 pass also computes each sequence's residue codes, the 8-byte per-residue ESM3 codes that fm-search build sequences --residue-codes stores (esm-codes-v1 target codes: 8 integers, 0-255, per residue), and writes them to that folder. ESM3 runs once; the embedding files are byte for byte those of a run without the option, and the output folder holds them only, so fm-search build embeddings and query embedding read it as before (for chain embeddings). The codes folder must exist and must not be --output-folder (compared as folders on disk: a symbolic link, ., or another case of the name on macOS are the same folder; a usage error, exit status 2); a subfolder of it, or a folder that holds it, will do (fm-search reads the files of a folder, not its subfolders), unless it is named as a file of the other folder's output. The codes follow --output-format, in the layout of the embeddings, one [L, 8] table per sequence of L residues, one row per residue: 0-based row i of the codes is character i of the normalised sequence (residue i + 1, counting from 1) and 0-based row i + 1 of the residue embeddings ([L + 2, 1536], ESM3's BOS and EOS rows included; BOS and EOS are never rows of the codes):

Format Residue-code files in the codes folder Layout
csv <id>.csv, as the embedding is <output-folder>/<id>.csv L lines of 8 integers, no header or index
pt <id>.pt a CPU uint8 tensor [L, 8]
parquet <output-codes-name>-<rank>.parquet columns id, residue_codes (list<fixed_size_list<uint8, 8>>), seq_sha1; the schema metadata names the scoring system, its digests and the pass (--min-res-n, device, ranks, versions)
json <output-codes-name>-<rank>.json.gz records {"id", "residue_codes"}, a list of 8 integers per residue

--output-codes-name NAME names the parquet and json files (default <output-name>-codes; the output name itself is allowed, as the folders differ); like --output-name, it names no csv or pt file. It is refused without --output-codes-folder. Before ESM3 is loaded, a run with --output-codes-folder refuses a codes name that cannot name those files, ids (csv, pt) that the codes folder takes for one file, a --fasta-file named like a code file, one folder inside the other under the name of a file that the other one gets (out/<id>.csv as the codes folder of a csv run, say), and FASTA records whose sequence holds <, > or non-ASCII characters (as the build does). Nor does any fm-embedding run replace a file of the other kind: before ESM3 (exit status 1), it refuses to replace an embedding file in its codes folder (an earlier run's output folder given as the codes folder, say) or a residue-code file in its output folder, with or without --output-codes-folder; files of other names are left alone, and a rerun into the same folders replaces its own files as before. The esm-codes bundle (~26 MB) is downloaded once, as for builds; on several nodes, or ranks started by srun or torchrun, run fm-embedding download-models first. A sequence whose codes cannot be computed (ESM3's rows do not match its length) is logged as an ERROR and gets an empty table (an empty file in csv); the run still writes every file, then exits with status 1, naming how many sequences got no codes, the first ids and where their empty tables are. With several devices or nodes, each rank that ends its pass waits for the others, and rank 0 then fails for the whole run (the other ranks log their count), so that no rank fails while another one still writes: the exit status is 1 under Lightning's own launcher, srun or torchrun alike. The code files are neither embeddings nor embedding shards: fm-search build embeddings, query embedding and build residue-codes --from-shards refuse them if they are moved among embedding files.

mkdir -p out/seq-chain out/seq-codes
fm-embedding from-sequences chain --fasta-file seqs.fasta --output-folder out/seq-chain \
    --output-format parquet --output-codes-folder out/seq-codes
# out/seq-chain/inference-0.parquet (chain embeddings), out/seq-codes/inference-codes-0.parquet (residue codes)
fm-search build embeddings --embedding-folder out/seq-chain --output-db db/chains

Run fm-embedding [from-structures|from-sequences] [command] --help for full options (batch size, accelerator, devices, output format, distributed settings, etc.).

fm-search — build and query FAISS databases

# Build a similarity-search database from structures, FASTA, or pre-computed embeddings
fm-search build structures  --structure-folder data/pdb --output-db dbs/my_db --tmp-embedding-folder tmp
fm-search build sequences   --fasta-file seqs.fasta     --output-db dbs/my_db --tmp-embedding-folder tmp
fm-search build embeddings  --embedding-folder out/chain --output-db dbs/my_db

# Query the database (--output-file is required)
fm-search query structure   --db-path dbs/my_db --query-structure q.cif --output-file hits.tsv
fm-search query sequences   --db-path dbs/my_db --fasta-file q.fasta --tmp-embedding-folder tmp --output-file hits.tsv
fm-search query embedding   --db-path dbs/my_db --embedding-folder out/chain --output-file hits.tsv
fm-search query db          --query-db-path dbs/queries --subject-db-path dbs/my_db --output-file hits.tsv

# Inspect, cluster, export
fm-search stats             --db-path dbs/my_db
fm-search cluster           --db-path dbs/my_db --output-file clusters.tsv
fm-search cluster           --db-path dbs/my_db --output-file clusters.tsv --method greedy
fm-search cluster           --db-path dbs/my_db --output-file clusters.tsv --method greedy-leiden --work-folder work
fm-search similarity-graph  --db-path dbs/my_db --output-file graph.graphml

All build commands accept --index-type [auto|flat|hnsw|ivf_pq] and IVF-PQ tuning flags (--ivf-nlist, --ivf-nprobe). See fm-search <subcommand> --help for the full surface.

cluster and similarity-graph work on every database type (--method greedy-leiden and --use-gpu-index on flat and ivf_pq only). On ivf_pq a score is estimated from the PQ codes (the chain's decoded embedding, normalised, against each stored code, as in query db), so two identical chains score close to, but not exactly, 1. With --method leiden (the default) the k-NN search takes time in proportion to the number of chains and --max-neighbors, whatever --threshold; --method greedy searches only chains no representative has taken, so its time also depends on --threshold and on database order. The graph's memory grows with the number of edges, which --threshold and --max-neighbors bound (at most --max-neighbors edges from each chain's search, plus the rescued edges below), and the Leiden step's memory also grows with the number of chains. The Leiden step runs a fixed number of iterations of Leiden (modularity at --resolution), 10 unless --leiden-iterations says otherwise; --seed makes it repeatable. With the default --leiden-backend igraph (python-igraph) it runs on one CPU thread. --leiden-backend networkit runs NetworKit's ParallelLeiden on --leiden-threads threads (default: every OpenMP thread; 1 on macOS, where more are refused), 5-25x faster on large graphs at 64 threads on Linux (126 s against 2,111 s on a 10M-chain graph); there --leiden-iterations is a maximum, and the same --seed gives the same clusters only with --leiden-threads 1 (on more threads --seed logs a WARNING; repeats at 64 threads agreed at AMI 0.984-0.996 on CATH and Swiss-Prot graphs, 0.795-0.805 on decoy-heavy 10M- and 30.4M-chain graphs). It needs pip install 'foldmatch[networkit]'; without it the run is refused before the database is loaded.

Edges. --edge-rule (leiden and greedy-leiden, and similarity-graph) says which k-NN hits become edges. lower keeps an edge (i, j), i < j, only from the search of the lower chain i, the graph of FoldMatch up to 0.12.0. A chain whose own hits at or above --threshold are all lower chains, and that no lower chain's search keeps, gets no edge that way; such chains sit mostly at the end of the database, and their number grows with the database (11,503 chains, 0.115%, at 10M TrEMBL chains and --max-neighbors 100, 6.8x the count at 1M). rescue, the default, adds for each such chain an edge to each of its own hits at or above --threshold, weighted by its own score, in the same pass. On Swiss-Prot (573,010 chains, --threshold 0.8) it removed 4,616 / 1,892 / 551 / 207 singletons at --max-neighbors 30 / 50 / 100 / 300 for +1.59% / +0.62% / +0.14% / +0.012% edges, and the partitions agreed with lower's at AMI 0.987 / 0.995 / 0.994 (30 / 50 / 100).

Defaults by method. --max-neighbors is 1000 under leiden and greedy, 100 under greedy-leiden (the k of its representatives' graph). --resolution is 1.0 under leiden and 20 under greedy-leiden. Options a method does not take are refused in one message before the database is loaded, naming each one and the methods that take it (--resolution, --leiden-iterations, --seed, --edge-rule, --leiden-backend and --leiden-threads under greedy; --greedy-threshold and --greedy-max-neighbors outside greedy-leiden; --work-folder under leiden).

Search depth. --nprobe (cluster under every method, and similarity-graph) sets the IVF cells probed per query of an ivf_pq database; the default is the value stored at build (--ivf-nprobe, or --ivf-nlist // 64). Higher finds more neighbours and is slower. On flat and hnsw databases it is refused.

GPU. --use-gpu-index (needs FAISS with CUDA GPU support, faiss-gpu) puts one replica of the index on every visible GPU (each search call's queries are split between them), with the PQ lookup tables of an ivf_pq index in float16 (float32 tables of an ivf_pq index of 64 sub-quantizers of 8 bits are refused on an A100). The stored vectors are read from the CPU index, which stays in memory, and greedy-leiden copies its representatives' database from it before moving that database to the GPUs too. On 4 A100s FAISS searched 28,188-32,607 queries a second at nlist 16,384, nprobe 256, k = 1000 on a 30.4M-chain database, against 1,011-1,752 on the node's 64 CPU cores. The float16 tables move scores slightly: GPU graphs of a 10M- and a 30.4M-chain database had 0.02% fewer and 2.07% more edges than the CPU graphs (edge Jaccard 0.96 and 0.91), and on a 10M-chain TrEMBL sample --method greedy-leiden on the GPUs kept 26 more representatives than on the CPU (1,886,496 against 1,886,470), and its label AMIs (Pfam clan and family, Gene3D, SUPERFAMILY, UniRef50) were within 0.0007 of the CPU's at --resolution 10 and 20, with greedy 7.5x and the graph 9.9x faster than on the same node's CPU. --max-neighbors, --greedy-max-neighbors and --nprobe are refused above 2048 (FAISS's GPU limit), and without GPU support the run is refused (exit status 1) before the database is loaded; macOS has none.

cluster --method greedy clusters collections whose similarity graph does not fit in memory. When most chains have --max-neighbors neighbours above --threshold, the Leiden route keeps about n x --max-neighbors / 2 edges for n chains: 447,547,205 edges for 1,000,000 chains in an ivf_pq database (--max-neighbors 1000, --threshold 0.8), about 15.1e9 for 30.4M chains (ivf_pq); with fewer neighbours above the threshold, fewer (219,292,519 for the same 1,000,000 chains on hnsw, where few chains reach --max-neighbors). Leiden's memory grows with the edges (on that 1M-chain ivf_pq database, cluster peaked at 28.16 GiB, about 66 bytes an edge above the memory before the load, on Linux x86_64 at 128 threads), greedy's with the chains: it picks representatives in one streamed pass over the database, dropping each block's search results once used. A cluster is a representative and the still-unclustered chains among its --max-neighbors nearest that score at least --threshold (MMseqs2/CD-HIT style): chains are taken in database order, and a chain in no cluster yet becomes a representative. When most representatives have --max-neighbors neighbours above the threshold (the statistics report the share; 98.96% of rows in the first 10 blocks of a 30.4M-chain ivf_pq database), clusters are bounded by --max-neighbors, and a representative or singleton can score above the threshold with an earlier representative. Greedy clusters are smaller and tighter than Leiden's few large communities at 0.8: higher family purity, lower completeness and family ARI (measured at 100k and 1M chains). Results depend on database order (the order chains were added), as CD-HIT's depend on length order. --max-neighbors caps cluster size at --max-neighbors + 1 chains (on hnsw, FAISS's efSearch of 16 returns fewer: 481.7 of 1000 on average on a 100k-chain database, so a larger value adds few). Greedy needs no graph and computes no modularity; it has no resolution or iteration count and is deterministic, so --resolution, --leiden-iterations, --seed and the other Leiden options are refused with it. --work-folder keeps greedy's state for a resume (below).

cluster --method greedy-leiden combines the two for collections whose direct graph does not fit in memory: greedy representatives at --greedy-threshold (default 0.9) with --greedy-max-neighbors (default 1000, also the greedy cluster-size cap), then Leiden on the representatives' similarity graph at --threshold (default 0.8), --max-neighbors (default 100), --edge-rule and --resolution (default 20); every chain takes its representative's cluster. The representatives' database is copied from the parent, with nothing trained or re-encoded: on ivf_pq their PQ codes in the parent's lists, under its trained OPQ, coarse quantizer and PQ, so their search is the parent's search restricted to them, at the same --nprobe; on flat their stored vectors (held in memory, so for small sets). An hnsw database (an auto one that is hnsw included) is refused: use --method leiden there. The resolution default 20 was chosen on TrEMBL samples of 1M and 10M chains (greedy 0.9, --max-neighbors 100): it left no cluster above 1.745% of the chains at 10M (at most one at 1M, against 4-7 at resolution 1) and agreed with Pfam families better than 10 (+0.008 to +0.014 AMI), at a cost of up to 0.007 on Gene3D and SUPERFAMILY; a lower value such as 10 keeps more superfamilies whole. With --greedy-threshold above every score every chain is its own representative and the result is --method leiden's at the same settings.

--work-folder is required by greedy-leiden (optional with greedy, refused with leiden). It holds run.json (the parent database's fingerprint, the effective nprobe, that is the cells probed, and the settings of each stored step, as resolved, and each run's steps with FAISS's thread count and GPUs), greedy's state (written every 15 minutes of greedy and at its end), the representatives' database and the representatives' graph's edges, each step with a done marker. Running the same command again resumes greedy from its last state and reuses every finished step; Leiden, the propagation to the chains and the export always run, so a rerun at another --resolution, --leiden-iterations, --seed, --leiden-backend or --leiden-threads searches nothing. A run with another recorded setting or another parent database is refused, naming each difference; another FAISS thread count, a change between CPU and GPU search, or another FoldMatch or FAISS version is a WARNING (the graph can differ by a few edges).

The layout confound. The share of representatives depends on the IVF layout as well as on --greedy-threshold: the same 1M TrEMBL chains gave 265,039 representatives at nlist 16,384 and 359,379 at nlist 4,096. Runs on databases of different --ivf-nlist are not comparable, and a resolution tuned at one layout may not transfer to another (the resolution default above was measured at nlist 16,384).

Giants. The statistics log gives, under every method, the largest cluster's size and share of the chains, the number of giant clusters (more than 1.745% of the chains) and the 10 largest cluster sizes. Under greedy-leiden, with 1,000,000 chains or more, giants are also a WARNING that a higher --resolution splits large clusters. Under leiden there is no such warning: on direct graphs every measured run at the default resolution had giants, and a higher resolution there cost domain labels (at 10M TrEMBL chains, resolution 10 lowered Gene3D agreement by 0.026 and SUPERFAMILY by 0.032 against resolution 1), so a giant there is not by itself a reason to raise it. On small or redundant sets a 1.745% cluster is ordinary.

Clustering very large collections

A recipe for one to a few hundred million chains, with greedy-leiden on one GPU node (64 CPU cores, 256 GB, 4 GPUs). Its figures for 200M chains are projections from measurements at up to 10M (TrEMBL) and 30.4M chains, not measurements.

  1. Put the FASTA in a random order (for example sorted by a salted hash of the id) before the build: an ivf_pq build trains on the first 1,000,000 vectors of its input.
  2. Build with --index-type ivf_pq --ivf-nlist 16384, the layout the greedy-leiden defaults were measured on.
  3. In each job, set the FAISS threads with OMP_NUM_THREADS (for example 64; the thread count changes a graph by a few edges, so use the same one for reruns that must match), copy the database's .index, .invlists and .metadata files to node-local storage, and put --work-folder on scratch (tens of GiB at 200M chains). --use-gpu-index below searches on the node's GPUs (see GPU above for how its results compared with the CPU's at 10M chains); leave it out to search on the CPU:
    export OMP_NUM_THREADS=64
    fm-search cluster --db-path /local/db/my_db --method greedy-leiden --work-folder $SCRATCH/clusters-work \
        --leiden-backend networkit --leiden-threads 64 --use-gpu-index --output-file clusters.tsv
    
  4. After greedy the log gives the number of representatives R, the bound of --max-neighbors x R edges of their graph and Leiden's memory at that bound for each backend. Expect up to about 0.47 x k x R edges (0.46 x k a chain on a direct 1M-chain TrEMBL graph at k = 100; 39.3 edges a representative on a 10M-chain cascade), so plan Leiden's node from the bound and that ratio.
  5. A job that stops (wall-time limit, failure) is resumed by the same command; chain the jobs with sbatch --dependency=afterany:<job id>.
  6. Other resolutions: the same command with another --resolution and the same --work-folder; only Leiden, the propagation and the export run.

Projections for 200M chains (unmeasured, from runs at 10M and 30.4M chains): 24M-38M representatives; 1.0-1.6e9 edges in their graph at k = 100; 12-19 GB of edge files and 15-23 GB in the work folder in all; NetworKit's Leiden 84-117 GiB, 5-8 minutes a run at 64 threads (igraph 51-81 minutes); the two searches about 65-200 hours on the CPU at nprobe 256 (12-34 hours at nprobe 64), about 4-7 hours on the node's 4 GPUs at nprobe 256.

Output format

Every command writes tab-separated rows to --output-file — one record per line. query results are ordered best-first per query, so rank is simply the row order.

--output-file cannot be one of the command's inputs: a file of a database it reads (<db path>.index, .metadata, .invlists, .sequences or .rescodes.*), the query FASTA or structure file, or a query embedding file; such a run is refused before any work. A file written by an earlier command is replaced. query embedding refuses query embeddings that share an id (results are kept by query id).

--format-mode selects the file layout:

Mode Layout
headless_tsv (default) Tab-separated, no header row
tsv Tab-separated, with a header row naming each column
# Add a header row naming the columns
fm-search query sequences --db-path dbs/my_db --fasta-file q.fasta \
    --tmp-embedding-folder tmp --output-file hits.tsv --format-mode tsv

The header always names the columns actually emitted, in the order emitted — for a sequence-identity search that is your --format-output selection. --format-mode controls only the file's shape; --format-output controls which columns it holds.

An embedding-only search — query structure, query embedding, or any query whose database has no sequence store — emits three columns:

query   target   embscore

A sequence-identity search (Stage 2, below) emits the columns chosen with --format-output, which default to those same three followed by the alignment statistics.

cluster uses the same convention with its own columns:

chain_id   cluster_id   cluster_size

and, with --method greedy and --method greedy-leiden, a fourth column, the chain's greedy representative's chain id (equal to chain_id on the representative's own row):

chain_id   cluster_id   cluster_size   representative

Under greedy the cluster is the representative's greedy cluster. Under greedy-leiden cluster_id and cluster_size are the final cluster (the Leiden cluster of the chain's representative, counted in chains), and representative is still the greedy representative, so the chains of one greedy cluster share a cluster_id. cluster_id 0 is the largest cluster, under every method (ties under greedy in representative order, under leiden and greedy-leiden by the smallest chain index). Rows are in database order; --min-cluster-size drops the rows of smaller clusters (under greedy-leiden, by the final size).

(similarity-graph is the one exception: GraphML is a graph format, not tabular.)

Two-stage sequence search (exact identity)

build sequences also writes a sidecar {db}.sequences store next to the FAISS index. This lets sequence-built databases report exact sequence identity, not just embedding similarity: when you run query sequences (or query db) against such a database, a second stage pairwise-aligns each embedding hit (local Smith-Waterman, BLOSUM62) and re-ranks the surviving hits by bitscore.

Stage 2 aligns on FoldMatch's native SIMD Smith-Waterman kernel, a compiled library (see Requirements). It reproduces biotite 1.7.1's align_optimal (local, affine gaps), ties included, which Stage 2 used before: the same scores, coordinates and alignments, so the same output. Without the kernel, query sequences and query db refuse to run Stage 2 before the queries are embedded or the database is searched, and print the command that rebuilds it; --no-seq-identity still runs an embedding-only search.

Stage-2 hits use the same tab-separated, no-header format as every other query output. Pick the columns with --format-output:

# Stage 2 turns on automatically when the database has a sequence store
fm-search query sequences --db-path dbs/my_db --fasta-file q.fasta \
    --tmp-embedding-folder tmp --output-file hits.tsv

# Choose the output columns
fm-search query sequences --db-path dbs/my_db --fasta-file q.fasta \
    --tmp-embedding-folder tmp --output-file hits.tsv \
    --format-output "query,target,evalue,qaln,taln"

Default columns: query,target,embscore,fident,alnlen,mismatch,gapopen,qstart,qend,tstart,tend,evalue,bits.

Supported --format-output fields:

Field Meaning Field Meaning
query Query sequence identifier tlen Target sequence length
target Target sequence identifier alnlen Alignment length (aligned columns)
embscore Stage-1 embedding similarity score raw Raw alignment score
evalue E-value bits Bit score (integer)
gapopen Number of gap open events (not gap chars) cigar CIGAR string (M match, D gap in query, I gap in target)
pident Percentage of identical matches qseq / tseq Query / target sequence
fident Fraction of identical matches qaln / taln Aligned query / target sequence with gaps
nident Number of identical matches mismatch Number of mismatches
qstart / qend 1-indexed alignment bounds in query qcov / tcov Fraction of query / target covered
qlen Query sequence length
tstart / tend 1-indexed alignment bounds in target
  • Auto by default: Stage 2 runs when the database(s) carry a sequence store and falls back to embedding-only otherwise. Force it with --seq-identity (errors if no store is present) or disable with --no-seq-identity. query db requires both databases to have sequence stores.
  • Hits below --min-seq-identity (default 0.3) or --min-coverage (default 0.8) are dropped, as are hits above --max-evalue (default 1e-3; pass inf to keep every hit). --max-evalue turns the significance pass on even when neither evalue nor bits is in --format-output.
  • Tuning: --gap-open (default 11) and --gap-extend (default 1), where a gap of length L costs gap-open + (L − 1) × gap-extend, and --align-workers (defaults to all CPUs on the node). --gap-open must be at least 1 and --gap-extend at least 0, both at most 1,048,576; the sign is ignored. With --significance-mode default, a run that needs significance (an evalue or bits column, or a finite --max-evalue, which the default 1e-3 is) needs the gap costs 11/1: for other gap costs use --significance-mode sampled, or --max-evalue inf with neither column in --format-output. Both rules are checked before the queries are embedded (query sequences) or the database is searched (query db).
  • Compositional bias correction: off by default, so alignments are scored with plain BLOSUM62. --comp-bias-corr (scaled by --comp-bias-corr-scale, default 1) applies mmseqs's correction for locally biased amino-acid composition, which mmseqs enables by default; use it when comparing with mmseqs search. With the same setting on both tools, raw scores, bit scores and E-values match mmseqs search (18-8cc5c) on the same pairs, except for alignments containing a non-standard residue (X, B, Z, J, U, O, *), which the two tools score differently. The correction can shorten alignments over biased regions, so coordinates, identity and coverage change too.
  • evalue/bits use calibrated Karlin–Altschul statistics by default (--significance-mode default). --significance-mode sampled estimates λ/K by sampling for other gap penalties, but its absolute magnitudes are relative-only. The significance pass is skipped automatically when neither evalue nor bits is in --format-output.

Python API

The FoldMatch class provides methods for computing embeddings programmatically.

Basic Usage

from foldmatch import FoldMatch

# Initialize model
model = FoldMatch(min_res=10, max_res=5000)

# Load models (optional - loads automatically on first use)
model.load_models()  # Auto-detects CUDA
# or specify device:
# import torch
# model.load_models(device=torch.device("cuda:0"))

A protein chain is embedded only if it has at least min_res residues (default 10), counted as polymer amino-acid residues: free amino acids are not counted. With max_res (default None, no limit), a structure method raises ValueError, before any ESM3 pass, when the protein chains it is about to embed have more than max_res residues together, counted the same way: the chains with chain_id (in an assembly, one per copy), else every protein chain of the structure or assembly. sequence_embedding raises ValueError when the sequence is shorter than min_res or longer than max_res.

Methods

load_models(device=None)

Load both residue and aggregator models.

import torch
model.load_models(device=torch.device("cuda"))

load_residue_embedding(device=None)

Load only the ESM3 residue embedding model.

model.load_residue_embedding()

load_aggregator_embedding(device=None)

Load only the aggregator model.

model.load_aggregator_embedding()

residue_embedding(src_structure, structure_format='mmcif', chain_id=None, assembly_id=None)

Compute per-residue embeddings for a structure.

Parameters:

  • src_structure: File path, URL, or file-like object
  • structure_format: 'mmcif', 'binarycif', or 'pdb'
  • chain_id: Chain ID (optional): the protein chains with this ID (in an assembly, one per copy); every protein chain if None
  • assembly_id: Assembly ID for biological assembly (optional)

Returns: torch.Tensor of shape [L + 2, 1536] for one chain of L residues: one row per residue, between ESM3's BOS and EOS rows. Several chains give their tensors one after another, each with its BOS and EOS rows. Raises ValueError when no protein chain qualifies, or when max_res is exceeded.

# Single chain
residue_emb = model.residue_embedding(
    src_structure="1abc.cif",
    structure_format="mmcif",
    chain_id="A"
)

# All chains concatenated
all_residues = model.residue_embedding(
    src_structure="1abc.cif",
    structure_format="mmcif"
)

# Biological assembly
assembly_residues = model.residue_embedding(
    src_structure="1abc.cif",
    structure_format="mmcif",
    assembly_id="1"
)

residue_embedding_by_chain(src_structure, structure_format='mmcif', chain_id=None)

Compute per-residue embeddings separately for each chain.

Returns: dict[str, torch.Tensor] mapping each chain ID to its embeddings, of shape [L + 2, 1536]; {} when no chain qualifies

chain_embeddings = model.residue_embedding_by_chain(
    src_structure="1abc.cif",
    structure_format="mmcif"
)
# Returns: {'A': tensor(...), 'B': tensor(...), ...}

# Get specific chain
chain_a = model.residue_embedding_by_chain(
    src_structure="1abc.cif",
    chain_id="A"
)

residue_embedding_by_assembly(src_structure, structure_format='mmcif', assembly_id=None)

Compute residue embeddings for an assembly.

Returns: dict[str, torch.Tensor] mapping the assembly ID ('0' for the asymmetric unit when assembly_id is None) to the tensors of its protein chains (one per copy) one after another, each with its BOS and EOS rows; {} when no chain qualifies

assembly_emb = model.residue_embedding_by_assembly(
    src_structure="1abc.cif",
    structure_format="mmcif",
    assembly_id="1"
)
# Returns: {'1': tensor(...)}

sequence_embedding(sequence)

Compute residue embeddings from amino acid sequence (no structural information).

Parameters:

  • sequence: Amino acid sequence string (plain or FASTA format)

Returns: torch.Tensor of shape [L + 2, 1536] for a sequence of L residues (ESM3's BOS and EOS rows included)

# Plain sequence
seq_emb = model.sequence_embedding("ACDEFGHIKLMNPQRSTVWY")

# FASTA format
fasta = """>Protein1
ACDEFGHIKLMNPQRSTVWY
ACDEFGHIKLMNPQRSTVWY"""
seq_emb = model.sequence_embedding(fasta)

aggregator_embedding(residue_embedding)

Aggregate residue embeddings into a single structure-level vector.

Parameters:

  • residue_embedding: torch.Tensor from any of the residue embedding methods (for residue_embedding_by_chain and residue_embedding_by_assembly, a value of their dict)

Returns: torch.Tensor of shape [1536]

residue_emb = model.residue_embedding("1abc.cif", chain_id="A")
structure_emb = model.aggregator_embedding(residue_emb)

structure_embedding(src_structure, structure_format='mmcif', chain_id=None, assembly_id=None)

End-to-end: compute residue embeddings and aggregate in one call.

# Complete structure embedding
structure_emb = model.structure_embedding(
    src_structure="1abc.cif",
    structure_format="mmcif",
    chain_id="A"
)
# Returns: tensor of shape [1536]

Complete Example

from foldmatch import FoldMatch
import torch

# Initialize
model = FoldMatch(min_res=10, max_res=5000)

# Option 1: Full structure embedding (one-shot)
embedding = model.structure_embedding(
    src_structure="1abc.cif",
    structure_format="mmcif",
    chain_id="A"
)

# Option 2: Step-by-step with residue embeddings
residue_emb = model.residue_embedding(
    src_structure="1abc.cif",
    structure_format="mmcif",
    chain_id="A"
)
structure_emb = model.aggregator_embedding(residue_emb)

# Option 3: Process multiple chains
chain_embeddings = model.residue_embedding_by_chain(
    src_structure="1abc.cif"
)
for chain_id, res_emb in chain_embeddings.items():
    chain_emb = model.aggregator_embedding(res_emb)
    print(f"Chain {chain_id}: {chain_emb.shape}")

# Sequence-based embedding
seq_emb = model.sequence_embedding("ACDEFGHIKLMNPQRSTVWY")
structure_from_seq = model.aggregator_embedding(seq_emb)

See the examples/ and tests/ directories for more use cases.


Residue-code alignment (esm-codes Stage 2)

foldmatch.search.code_alignment runs the esm-codes Stage 2 of fm-search query sequences --scoring esm-codes on arrays in memory: one query against any number of targets, in this process, with no database, query-encoding file or residue-code store. The hits are fm-search's, field for field and bit for bit (see Reproducibility), and each hit's alignment comes back as explicit pairs of rows. The API is provisional in 0.14: a structure-conditioned scoring system may change the records and the type of scoring_system; the indexing convention and the bit-identity guarantee below are the stable part. Import by module path (nothing is added to foldmatch.__all__); code_alignment.__all__ lists the public names. The module needs numpy only (no torch, no biotite), except load_code_targets, which runs fm-search's database checks.

Inputs: CodeQuery and CodeTarget

CodeQuery(sequence, encoding, *, seq_sha1=None, residue_keys=None)
CodeTarget(sequence, codes, *, seq_sha1=None, residue_keys=None)
  • CodeQuery.encoding: the query's S5 query encoding, a native float16 array [n, 64], n >= 1, finite. It is never cast: float32 or byte-swapped '>f2' is a TypeError.
  • CodeTarget.codes: the target's S4 residue codes, uint8 [m, 8] (64 bits per residue, packbits, most significant bit first), m >= 1. The [0, 8] table predict returns for a sequence whose codes failed is refused.
  • sequence: normalised on construction (ASCII whitespace removed, ASCII letters upper-cased, non-ASCII refused) and it must have exactly one character per row. seq_sha1 (optional, as predict returns it) must be the SHA-1 of the normalised sequence; after construction the field holds that SHA-1 in any case.
  • residue_keys (optional): one opaque, hashable, unique key per row (not None, which marks a gap; a str is refused, since it would be one key per character), for example a mmCIF (label_asym_id, label_seq_id), a model file's (chain, resSeq, iCode) or a UniProt position.
  • Each record keeps a read-only, C-contiguous copy of its array. A CUDA/MPS tensor or one that requires grad is a TypeError ("call .detach().cpu()"); a CPU tensor without grad is accepted. Wrong dtypes or shapes are a TypeError; a row count other than the normalised length, an empty array, a seq_sha1 mismatch, a non-finite encoding and bad keys are a RefusalError.

Where the arrays come from. inference.chain_complete_inference.predict makes both in memory, in the same ESM3 pass as the chain embedding, from FASTA input only (esm-codes-v1 encodings and codes are defined for sequence-only ESM3; the producer refuses structure input). With out_folder=None nothing is written. Each prediction batch is (chain_embeddings, ids, payloads, seq_sha1s), each payload the raw bytes of one sequence: '<f2' [L, 64] for residue_output="query_encoding", uint8 [L, 8] for residue_output="residue_codes" (an empty payload: that row failed). The score is directional (the query side is an encoding, the target side codes), so align(A, B) is not align(B, A), and each side needs its own pass. The 0.12.0 route sequence_inference.predict(out_codes_folder=...) also returns target codes, as CPU tensors (CodeTarget(seq, codes[i], seq_sha1=sha1s[i]), the sequence read from the FASTA), but it writes code files too; when some rows fail it raises MissingResidueCodesError at the end, and except MissingResidueCodesError as e: preds = e.predictions keeps the others. Structure-conditioned ESM3 rows (FoldMatch.residue_embedding, residue_embedding_by_chain) run through the encoders by hand are not valid input, and nothing can detect them; a multi-chain residue_embedding() table is not one table either (it concatenates per-chain [L_c + 2] tables).

align_query_codes(query, targets, *, scoring_system, db_residues, emb_scores=None, q90=None, min_seq_identity=0.0, min_coverage=0.0, max_evalue=1e-3, output_needs=None)

Parameters:

  • query: a CodeQuery.
  • targets: Mapping[str, CodeTarget], target id to record; the ids are the hits' subject_id.
  • scoring_system (required): the system that made the encodings and codes, as a name ("esm-codes-v1"), an EsmCodesSystem (exactly the registry entry) or a PreparedSystem. A name or a system is prepared on every call (the ~26 MB bundle is hashed, and downloaded if it is not cached); prepare it once per process with scoring_system.prepare(scoring_system.get(name)), with local_files_only=True to never download.
  • db_residues (required, no default): N, the search space of the E-values in residues (see Choosing N). None: no E-values (evalue and log_evalue None, rendered empty), but Q90, the adjusted score and the bit score are still computed when needed; a finite max_evalue with None is a ValueError. An int > 0 (numpy integers too); 0, negative numbers and bool are refused.
  • emb_scores: target id to Stage-1 embedding score (Hit.emb_score and the tie-break of the order), exactly the targets ids; None gives every target 0.0, so ties then go by id.
  • q90: a PanelQ90 from panel_q90() or an earlier result's .q90, to skip the panel pass. Refused when the call needs no panel (ValueError), and when it is another query's or another setup's (RefusalError).
  • min_seq_identity, min_coverage, max_evalue: fm-search's filters and defaults (fident >= min_seq_identity, min(qcov, tcov) >= min_coverage, E <= max_evalue; max_evalue=None or inf keeps every target with a non-empty alignment).
  • output_needs: the --format-output field names you will render, or an OutputNeeds; None is fm-search's default columns (output.DEFAULT_OUTPUT_FIELDS). A name --format-output does not know is a ValueError. The cigar, qaln, taln, qseq and tseq strings are built only when named, and render empty otherwise. The panel pass (Q90, about n x 355,626 similarity cells) runs when a finite max_evalue or one of evalue, bits, q90, rawadj asks for it.

Returns: a QueryCodeAlignment:

  • hits: fm-search's Hit objects for this query, same fields, bits and order (S descending, then emb_score descending, then id); output.format_row("Q", hit, fields) renders fm-search's columns and output.write_aligned_results({"Q": res.hits}, fields, path) its file;
  • paths: target id to AlignedResidues, one per hit, in hit order;
  • q90: the query's PanelQ90 when the panel was computed or reused; None when the call needs no panel, for an empty targets (even with q90= given) and for a degenerate query;
  • degenerate: True when the query gets no hits by rule (one row, or every row bitwise equal), not an error;
  • skipped: the targets whose pair exceeds 2^28 similarity cells, in targets order (no score, no hit);
  • db_residues, and provenance: what the bits depend on besides the inputs (FoldMatch version, system name, digest and calibration, kernel ISA, DP id, source and compiler, numpy and its BLAS, and blas_limited, whether numpy's own BLAS, the one the scoring's matrix product runs on, ran at one thread; another library's BLAS does not count).

A target in neither hits nor skipped was filtered by the E-value, an empty alignment, or the identity / coverage filters (pass max_evalue=None to keep them).

Every refusal of the arguments (TypeError, ValueError, or RefusalError for a Q90 of another query or setup) comes before the system is prepared; a query too long for the panel pass (over 268,973 residues for esm-codes-v1) is a RefusalError before any alignment. RefusalError is a ValueError.

panel_q90(query, *, scoring_system)

The panel pass alone: the query's PanelQ90(value, encoding_sha256, provenance), to pass as q90= to many calls with the same query (it depends on the query and the system only, not on the targets or N). None for a degenerate query.

load_code_targets(db_path, ids)

Reads the CodeTarget of each id from a database built with fm-search build sequences --residue-codes, through fm-search's own database checks, and returns CodeDatabaseTargets(targets, total_residues, prepared): the targets (no residue keys: the stores keep none), N, and the prepared system the database names. ids is required and may not repeat an id. It imports torch (through those checks).

Residue indexing

  1. Rows. 0-based row i of CodeQuery.encoding or of CodeTarget.codes is character i of the normalised sequence (residue i + 1, counting from 1) and 0-based row i + 1 of ESM3's [L + 2, 1536] table. ESM3's rows 0 (BOS) and L + 1 (EOS) are never rows here. Paths are 0-based rows; qstart, qend, tstart and tend are 1-based.
  2. Input mode. esm-codes-v1 encodings and codes are defined only for sequence-only ESM3 (FASTA input).
  3. BOS and EOS. The producer drops them in one place (it refuses a table without L + 2 rows, then keeps [1:-1]); an array whose rows differ from the normalised length is refused.
  4. Paths. AlignedResidues.rows is the kernel's path itself, int32 [P, 2], read-only: column c is (query row, target row), 0-based indices into the arrays given, -1 for a gap, in forward order. Rows are indices into the arrays, not residue numbers: never join them to atom lists by position.
  5. Bounds. qstart = rows[rows[:, 0] >= 0, 0].min() + 1, qend = ... .max() + 1, and the same for the target with column 1.
  6. Keys. With residue_keys, the key of path column c is residue_keys[rows[c, side]], None at a gap: query_keys(), target_keys(), and matched_keys() for the aligned pairs (matched() gives their rows). Keys are opaque; you join them to atoms or features. Without keys a row's identity is (seq_sha1 of the normalised sequence, row), which AlignedResidues carries as query_seq_sha1 and target_seq_sha1.
  7. Sequence use. The sequence enters only identity, the strings, the calibrated-domain checks of non-standard residues and the length / SHA-1 checks. The scoring never reads letters.

Worked example: query input "mkT AYw" normalises to "MKTAYW" (n = 6): ESM3 rows 1-6 are encoding rows 0-5, residues 1-6, with the keys (A,1) ... (A,6) of a model file. Target "KTYW" (m = 4) has codes rows 0-3 and the keys P12345:101 ... P12345:104 (UniProt positions: a key is not a position in the array). An (illustrative) path [[1,0], [2,1], [3,-1], [4,2], [5,3]] is the CIGAR 2M1I2M, qstart 2, qend 6, tstart 1, tend 4, alnlen 5, nident 4, gapopen 1; matched() is [[1,0], [2,1], [4,2], [5,3]] and matched_keys() is [((A,2), P12345:101), ((A,3), P12345:102), ((A,5), P12345:103), ((A,6), P12345:104)]. ESM3's own row numbers (2, 3, 5, 6 for the query) appear nowhere.

Choosing N, and what an E-value means

  • E is the expected number of chance hits with an adjusted score at least this large when one query of length n searches N residues of unrelated targets. The bit score does not depend on N: E = n x N x 2^-bits.
  • To reproduce fm-search on database D: N is the database's total_residues (load_code_targets(D, ids).total_residues, or ResidueCodeStore.from_db_path(D).manifest()["total_residues"]), the residues of the first kept record of each id: two ids with one sequence count twice.
  • For a collection of your own: the summed lengths of the distinct ids you would search.
  • One pair alone: the bit score is the pairwise statistic; call with db_residues=None and max_evalue=None, and compute E for the N you choose. N = m, that pair only, is far below the calibration's search space, so such an E-value is an extrapolation.
  • Rescaling ln E to another N afterwards agrees with a call at that N within rounding, not bit for bit, and a finite max_evalue filters at the N of the call.
  • Outside the calibrated domain (queries of 21-966 residues, targets up to 3,000, at most 1% non-standard residues, targets with at least 2 distinct codes) hits are reported with ecal=0, not refused.

Reproducibility

For the same encoding and code arrays, sequences, embedding scores, N, thresholds, output fields and system, align_query_codes(...).hits equals fm-search query sequences --scoring esm-codes's hits for that query bit for bit: every metrics field, emb_score, subject_id, the order and the rendered row. This holds for the same FoldMatch install (kernel library, numpy, BLAS), the same FOLDMATCH_SW_ISA and BLAS at one thread in both, which result.provenance.blas_limited reports for the API, and for candidate lists without repeated ids (a targets mapping cannot repeat one; fm-search's builds refuse repeated ids). It holds by construction: the API runs fm-search's own kernel calls, statistics and hit assembly. Arrays recomputed on another device, machine or BLAS build agree within the method's tolerances only.

  • threadpoolctl cannot limit Apple Accelerate (numpy's BLAS on macOS): there the API uses Accelerate's own thread count and blas_limited is False. For the bits of fm-search's worker pool set VECLIB_MAXIMUM_THREADS=1 before Python starts.
  • Threads. For the length of a call BLAS and OpenMP run at one thread process-wide, so torch or FAISS running in other threads do too; calls are serialised by a lock. A call overlapping an in-process fm-search Stage 2 (align_candidates_esm_codes) in another thread shares that one-thread setting with it, and the last of them to finish restores the thread counts.
  • Parallel use. The API has no worker pool: one call uses one core. For throughput, run calls in a spawn concurrent.futures.ProcessPoolExecutor whose initializer sets OMP_NUM_THREADS, OPENBLAS_NUM_THREADS, MKL_NUM_THREADS, VECLIB_MAXIMUM_THREADS and BLIS_NUM_THREADS to 1 before importing foldmatch, with the worker function in an importable module and the script guarded by if __name__ == "__main__":; prepare one PreparedSystem per worker, and compute panel_q90 once per query in the same environment (a PanelQ90 is accepted only where its provenance matches).

Example

import numpy as np
from foldmatch.inference.chain_complete_inference import predict
from foldmatch.types.api_types import SrcEsmFrom
from foldmatch.search import scoring_system as ss, output
from foldmatch.search.code_alignment import CodeQuery, CodeTarget, align_query_codes, panel_q90

prepared = ss.prepare(ss.get("esm-codes-v1"))           # once per process

def payloads(fasta, kind):                               # kind: "query_encoding" or "residue_codes"
    out = {}
    for _emb, ids, blobs, sha1s in predict(fasta, src_from=SrcEsmFrom.fasta, residue_output=kind,
                                           scoring_system=prepared, out_folder=None, return_predictions=True,
                                           accelerator="cpu", devices=1):   # one process (see below)
        out.update(zip(ids, zip(blobs, sha1s)))          # b"" = that row failed
    return out

seqs = {...}                                             # id -> sequence, from the same FASTA files
qb, qsha = payloads("queries.fasta", "query_encoding")["Q"]
q = CodeQuery(seqs["Q"], np.frombuffer(qb, "<f2").reshape(-1, 64), seq_sha1=qsha,
              residue_keys=[("A", i) for i in range(1, len(seqs["Q"]) + 1)])
targets = {tid: CodeTarget(seqs[tid], np.frombuffer(b, np.uint8).reshape(-1, 8), seq_sha1=s,
                           residue_keys=[(tid, i) for i in range(1, len(seqs[tid]) + 1)])
           for tid, (b, s) in payloads("targets.fasta", "residue_codes").items()
           if b}                                         # skip the rows that failed

fields = output.parse_format_output("query,target,evalue,bits,cigar")
res = align_query_codes(q, targets, scoring_system=prepared, db_residues=N, output_needs=fields)
for h in res.hits:
    print(output.format_row("Q", h, fields))             # fm-search's columns
    rows = res.paths[h.subject_id].rows                  # (query row, target row), -1 = gap
    pairs = res.paths[h.subject_id].matched_keys()       # [(("A", i), (tid, j)), ...]: keys on both sides
    # without keys on a side, .matched() gives the aligned (query row, target row) pairs

# many targets over several calls: the panel pass once
pq = panel_q90(q, scoring_system=prepared)
for chunk in chunks:                                     # dicts of id -> CodeTarget
    res = align_query_codes(q, chunk, scoring_system=prepared, db_residues=N, q90=pq, output_needs=fields)

predict's defaults (accelerator="auto", devices="auto") start a DDP strategy, which Lightning refuses on Apple MPS; devices=1 keeps the pass in this process (with accelerator="cpu", or the device of your choice). N is as in Choosing N; chunks are dicts of target id to CodeTarget.


Model Architecture

The embedding model is trained to predict structural similarity by approximating TM-scores using cosine distances between embeddings. It consists of two main components:

  • Protein Language Model (PLM): Computes residue-level embeddings from a given 3D structure.
  • Residue Embedding Aggregator: A transformer-based neural network that aggregates these residue-level embeddings into a single vector.

Embedding model architecture

Protein Language Model (PLM)

Residue-wise embeddings of protein structures are computed using the ESM3 generative protein language model.

Residue Embedding Aggregator

The aggregation component consists of six transformer encoder layers, each with a 3,072-neuron feedforward layer and ReLU activations. After processing through these layers, a summation pooling operation is applied, followed by 12 fully connected residual layers that refine the embeddings into a single 1,536-dimensional vector.


Testing

After installation, run the test suite:

pytest

macOS notes

The problem. PyPI wheels for faiss-cpu and torch (pulled in via lightning) each bundle their own copy of libomp.dylib. On macOS, both copies get loaded into the same Python process. Whenever FAISS enters an OpenMP-parallel section (batched search with more than one query vector, IndexHNSWFlat graph construction, IVF-PQ training) the second OpenMP runtime fails to pthread_mutex_init and the call deadlocks — the CLI appears to hang indefinitely — or the process crashes (a segmentation fault at OMP_NUM_THREADS=4 on an M1 Max, with torch 2.11 and faiss-cpu 1.13.2 or 1.15.1). Linux installs are unaffected: there the copies are GNU libgomp builds, which can be loaded together, and FoldMatch's commands, which load torch first, run FAISS on torch's copy.

Affected commands on macOS without mitigation:

  • fm-search build with --index-type hnsw or auto past ~10k vectors, and any --index-type ivf_pq.
  • fm-search query embedding with a multi-row .parquet file.
  • fm-search query sequences with more than one input sequence.
  • fm-search query db (database-to-database).
  • fm-search cluster and fm-search similarity-graph, which search many chains per FAISS call (safe at the default OMP_NUM_THREADS=1 pin).

fm-search cluster --leiden-backend networkit works on macOS 26 arm64 (NetworKit 11.3 ships its own libomp.dylib there): with torch and FAISS loaded and the default pin, a FAISS search, ParallelLeiden and the same search again gave the same results in one process. It runs on 1 thread there, and --leiden-threads above 1 is refused before the database is loaded: with torch and FAISS loaded, NetworKit's copy of libomp.dylib (a third one) failed its first parallel region on 2 or 3 threads (OMP Error #179) and crashed the process with a segmentation fault, after the searches. --use-gpu-index is refused on macOS: the faiss-cpu wheel's Metal backend is not FAISS's CUDA GPU layer, and the commands that fall back to the CPU without a GPU do so there.

Single-query paths (fm-search query structure, small --index-type flat builds) are unaffected.

Possible fixes.

  1. Fix the install environment — install both libraries against a unified OpenMP runtime. On conda-forge:

    conda install -c conda-forge faiss-cpu pytorch llvm-openmp
    

    Once a single libomp is loaded, FAISS's parallel paths just work and you keep the full multi-threaded performance.

  2. Force single-threaded FAISS via environment variable — set OMP_NUM_THREADS=1 before invoking Python:

    export OMP_NUM_THREADS=1
    fm-search query db ...
    

    Sidesteps the parallel section entirely. Toolkit works, but FAISS runs single-threaded so large builds and queries are slower.

What this package does by default. To prevent macOS users from hitting a silent hang out of the box, foldmatch/__init__.py calls os.environ.setdefault("OMP_NUM_THREADS", "1") on darwin only — before any torch or faiss import. This is option 2 above, applied automatically. Linux installs are not touched (the branch is skipped). A user on macOS who has fixed their environment per option 1 can opt back into parallelism by exporting OMP_NUM_THREADS=N before launching Python — setdefault respects an existing value.


Citation

Segura, J., et al. (2026). Multi-scale structural similarity embedding search across entire proteomes. (https://doi.org/10.1093/bioinformatics/btag058)


License

This project uses the EvolutionaryScale ESM-3 model and is distributed under the Cambrian Non-Commercial License Agreement.

Metadata

Release files for foldmatch 0.14.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 foldmatch 0.14.0
File Size Uploaded Provenance Actions
foldmatch-0.14.0.tar.gz 9.1 MB With provenance Details for foldmatch-0.14.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for foldmatch 0.14.0
File Actions
foldmatch-0.14.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details for foldmatch-0.14.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
foldmatch-0.14.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl Python 3 none Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details for foldmatch-0.14.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
foldmatch-0.14.0-py3-none-macosx_11_0_x86_64.whl Python 3 none macOS 11.0+ x86-64 Details for foldmatch-0.14.0-py3-none-macosx_11_0_x86_64.whl
foldmatch-0.14.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for foldmatch-0.14.0-py3-none-macosx_11_0_arm64.whl

Total release size: 11.1 MB

Release files / foldmatch-0.14.0.tar.gz

Download URL foldmatch-0.14.0.tar.gz
Size 9.1 MB
Tags Source
SHA-256 checksum
How to use checksums
1b1e14025b57be3222f2522458a2a261feb86472d4d7091ce96a5c1f36658725
BLAKE2b-256 checksum
How to use checksums
f14e96e29685bf7d8448b4d7392b3b26aa1d576cd3ad01e6c71fbdccdae3ce52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 23, 2026.

Transparency log

Release files / foldmatch-0.14.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL foldmatch-0.14.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 487.3 kB
Tags Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
af3bd2e81edfaca6c88ae9285fefe3d06576b0b3ec74de4f15a083f323a0efdc
BLAKE2b-256 checksum
How to use checksums
c58d2912161b5d6f00b9f14fd292a46025edfe4522b7e77cf8064210f45f5321
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 23, 2026.

Transparency log

Release files / foldmatch-0.14.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL foldmatch-0.14.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 512.0 kB
Tags Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
68b1e9d2409f7e048df435ef04d299fcd759de8acffc0a118521cb08fbd52ff7
BLAKE2b-256 checksum
How to use checksums
0d3a2f71d04f44c9cd2c2dd2072e8660d10d2493ed3c8695eaf41fcef5200f15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 23, 2026.

Transparency log

Release files / foldmatch-0.14.0-py3-none-macosx_11_0_x86_64.whl

Download URL foldmatch-0.14.0-py3-none-macosx_11_0_x86_64.whl
Size 499.5 kB
Tags Python 3 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
9303c9473b4bb302d3d0988988d8f1687437642acc2a32eb63e6f5f32ed9b1e9
BLAKE2b-256 checksum
How to use checksums
9919cfc81af81c3957378aff93084f889c663782f7fa9cd4371dc33a1555e2b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 23, 2026.

Transparency log

Release files / foldmatch-0.14.0-py3-none-macosx_11_0_arm64.whl

Download URL foldmatch-0.14.0-py3-none-macosx_11_0_arm64.whl
Size 483.4 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
52467e45320adfbe1dca3105eb4c6cb9071613a902e6eea03f7cbd42c04c65ac
BLAKE2b-256 checksum
How to use checksums
6bcd8c0c24257a298b478de77b3ee858704e33152bd28a47134d867140e27fa5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 23, 2026.

Transparency log

Security for foldmatch 0.14.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.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for foldmatch-0.14.0.tar.gz

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 Nov 23, 2026.

Transparency log

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.

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