nj.rs
Fast neighbor-joining trees from multiple sequence alignments, as a Rust crate and CLI, a Python package and an npm package (WASM).
- Fast. SIMD kernels on stable Rust, bit-packed DNA pair counting and a cache-friendly NJ loop: a tree of 1000 DNA sequences of 1000 sites takes about 40 ms on one core.
- Many models. Classic closed-form distances, maximum-likelihood distances under GTR, HKY85, F84 and the empirical protein matrices (LG, WAG, JTT, Dayhoff), or your own rate matrix. Distances match PAML
basemland PHYLIPprotdist. - Rate variation. Gamma-distributed rates and invariant sites (
+I+Γ) for every model. - Bootstrap support, computed in parallel.
- Robust input handling. DNA or protein is detected automatically, gaps and
N/Xare skipped pair by pair, and saturated pairs are capped with a warning instead of failing.
Models
| Model | Alphabet | Parameters |
|---|---|---|
PDiff |
DNA, protein | |
JukesCantor, Kimura2P, TajimaNei, Tamura |
DNA | |
Gtr |
DNA | gtr_rates (required), base_frequencies |
Hky, F84 |
DNA | kappa (required), base_frequencies |
EmpiricalGtr |
DNA | |
Poisson, KimuraProtein |
protein | |
Lg, Wag, Jtt, JttDcmut, Dayhoff, DayhoffDcmut |
protein | |
Custom |
protein | matrix (required) |
All models except PDiff accept gamma_shape and p_invar. See docs/models.md for what each model estimates, its parameters and citations.
CLI
cargo install nj --features cli
nj alignment.fasta > tree.nwk
nj -m kimura2-p -b 1000 -g 0.5 alignment.fasta > tree.nwk
nj -m hky --kappa 4 alignment.fasta
cat proteins.fasta | nj -m lg -t 8
Run nj --help for all options. Pass --distance-matrix or --average-distance to get JSON output that includes them.
Rust
[dependencies]
nj = "0.1"
use nj::{NJConfig, NJEvent, SubstitutionModel, parse_fasta};
fn main() -> Result<(), nj::NJError> {
let msa = parse_fasta(">A\nACGTACGT\n>B\nACCTACGT\n>C\nTCGTACGT\n")?;
let config = NJConfig {
msa,
substitution_model: SubstitutionModel::Kimura2P,
n_bootstrap_samples: 100,
gamma_shape: Some(0.5),
..Default::default()
};
let on_event = Box::new(|event| {
if let NJEvent::BootstrapProgress { completed, total } = event {
eprintln!("{completed}/{total}");
}
});
let result = nj::nj(config, Some(on_event))?;
println!("{}", result.newick);
Ok(())
}
Distances only:
use nj::{DistConfig, SubstitutionModel, parse_fasta};
fn main() -> Result<(), nj::NJError> {
let msa = parse_fasta(">A\nACGTACGT\n>B\nACCTACGT\n>C\nTCGTACGT\n")?;
let config = DistConfig {
msa,
substitution_model: SubstitutionModel::JukesCantor,
..Default::default()
};
let dm = nj::distance_matrix(config.clone())?; // dm.names, dm.matrix (n×n)
let mean = nj::average_distance(config)?;
println!("{:?} {mean}", dm.names);
Ok(())
}
Cargo features: parallel (Rayon) and cli (the binary, implies parallel).
Python
pip install nj_py
from nj_py import nj, distance_matrix
msa = [
{"identifier": "A", "sequence": "ACGTACGT"},
{"identifier": "B", "sequence": "ACCTACGT"},
{"identifier": "C", "sequence": "TCGTACGT"},
]
result = nj(msa, substitution_model="Kimura2P", n_bootstrap_samples=100, gamma_shape=0.5)
print(result["newick"])
dm = distance_matrix(msa, substitution_model="JukesCantor") # {"names": [...], "matrix": [[...]]}
Pass on_event=callback to receive progress events, e.g. {"type": "BootstrapProgress", "completed": 5, "total": 100}.
JavaScript
npm install @holmrenser/nj
import { nj, distance_matrix } from '@holmrenser/nj';
const msa = [
{ identifier: 'A', sequence: 'ACGTACGT' },
{ identifier: 'B', sequence: 'ACCTACGT' },
{ identifier: 'C', sequence: 'TCGTACGT' },
];
const { newick } = nj(
{ msa, n_bootstrap_samples: 100, substitution_model: 'Kimura2P' },
(event) => {
if (event.type === 'BootstrapProgress') console.log(event.completed, event.total);
},
);
const { names, matrix } = distance_matrix({ msa, substitution_model: 'JukesCantor' });
The options are the same in every binding; TypeScript types are included.
License
MIT
Metadata
Release files for nj-py 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nj_py-0.1.0.tar.gz | 147.2 kB | With provenance | Details for nj_py-0.1.0.tar.gz |
Built distributions (wheels)
| File | Actions Reset | |||
|---|---|---|---|---|
| nj_py-0.1.0-cp310-abi3-win_amd64.whl | CPython 3.10 | abi3 | Windows x86-64 | Details for nj_py-0.1.0-cp310-abi3-win_amd64.whl |
| nj_py-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ x86-64 | Details for nj_py-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
| nj_py-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ ARM64 | Details for nj_py-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
| nj_py-0.1.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 10.12+ x86-64, macOS 10.12+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64 | Details for nj_py-0.1.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl |
Total release size: 2.8 MB
Release files / nj_py-0.1.0.tar.gz
| Download URL | nj_py-0.1.0.tar.gz |
|---|---|
| Size | 147.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3d11b651567be6b840636aa84105d706dce89bcc49d714d3b77605dfdbfe549
|
|
BLAKE2b-256 checksum How to use checksums |
6308f2852e436daf026d294a80e1c05e67a0dec75e0fd055e9c51760e0707820
|
| 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 Dec 1, 2026.
Transparency logRelease files / nj_py-0.1.0-cp310-abi3-win_amd64.whl
| Download URL | nj_py-0.1.0-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 501.9 kB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
9f2a642b99144df625fd946b2e93ddfa6f193321f049b1c78641d5a684182832
|
|
BLAKE2b-256 checksum How to use checksums |
f7a94ab561d283891bf8454a3f50fac22600450f00605be1d40b7550a485817c
|
| 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 Dec 1, 2026.
Transparency logRelease files / nj_py-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | nj_py-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 575.6 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
33c86c3e16731c4ac87cc4c98e63452a3e872cc447bec07b4e7864b0bbba08f9
|
|
BLAKE2b-256 checksum How to use checksums |
8bc0eeb4ce963b5889bb9a96c7d14b2bef76aa50b2d7cd3a82caee50583bbaaf
|
| 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 Dec 1, 2026.
Transparency logRelease files / nj_py-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | nj_py-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 548.5 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
53f85f315d65b51bd54de06f89293bbd8b88549b2c05617e4ea7b6a24d3b2018
|
|
BLAKE2b-256 checksum How to use checksums |
18e346b19062295b2b5c557f255b95a964f548a042ef8bf5d307132bb00c6dd8
|
| 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 Dec 1, 2026.
Transparency logRelease files / nj_py-0.1.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
| Download URL | nj_py-0.1.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
94b6fab011c7c0538a3e9ff6d330993415f6320202b9b7576d4f4374628c2c5a
|
|
BLAKE2b-256 checksum How to use checksums |
8d0368b9d12a3073d222b1c12427a6a157913e983d9530b7b76e4453ca453a5c
|
| 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 Dec 1, 2026.
Transparency logSecurity for nj-py 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.
Full provenance
All 5 files in this release have been uploaded with provenance metadata.
Attestation for nj_py-0.1.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 Dec 1, 2026.
Transparency logA 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.