fastreeR: Fast Tree Reconstruction Tools for Genomics (VCF/FASTA to Distance/Tree)
fastreeR is a hybrid toolkit combining a high-performance Java backend (BioInfoJava-Utils, a modular Java library for bioinformatics pipelines) with flexible and user-friendly interfaces across multiple platforms and environments, enabling seamless integration into a variety of genomic workflows.
It enables fast computation of distance matrices and phylogenetic trees from genetic variant data in VCF or genomic sequences in FASTA format.
Integration and Accessibility
fastreeR offers interface, which is accessible in the following ways:
🆕 Java Backend (v2.5.0) !! introduces embedding-based distance calculation for VCF files. Provide pre-computed variant embeddings (from genomic language models like BioFM, DNA-BERT, Nucleotide Transformer, etc.) to weight variant contributions during distance computation.
Java Backend (v2.3.0) supports reading from gzip (for example .gz), bzip2 (for example .bz2) and xz compressed VCF files.
Java Backend (v2.2.0) implements streaming bootstrap; from VCF file get a newick tree with encoded bootstrap support values.
Java Backend (v2.0.0) 100x times FASTreER and only a couple hundred MB RAM needed. Java 11+ suggested.
Bioconda: install with conda install -c bioconda fastreer (recipe)
Docker: available on DockerHub and GHCR for containerized execution
PyPI: install with pip install fastreer (repository)
Python CLI: through a lightweight Python wrapper that calls the Java backend
📁 Input from standard VCF (gz, bzip2, xz compressed or uncompressed) and FASTA files.
🧠 Embedding-based distance calculation using pre-computed variant embeddings from genomic language models.
🥾 Streaming bootstrap support from VCF to NEWICK.
🚀 With a superior multithreaded concurrency model and minimal RAM usage, from GBs down to just MBs!
⚡ Ultra-fast computation of sample-wise cosine distances from large VCF and D2S k-mer based distances from FASTA files.
Generate agglomerative neighbor-joining phylogenetic trees directly from VCF or distance matrices.
Multithreaded execution for speed and scalability.
Cluster distance matrices hierarchically with dynamic tree pruning.
Clean Python CLI for scripting and pipeline integration
Streamlined integration with R via rJava
Available on Galaxy Toolshed
Compatible with standard bioinformatics formats (PHYLIP, Newick)
Requirements
Java 17+ (LTS version with improved concurrency)
Python 3.7+
Maven (if you want to build from the source)
GNU/Linux, Windows or macOS
Memory requirements for VCF input
No more GBs of RAM! Only the distance matrix is kept in memory:
4 bytes x (#samples²) x #threads
Example: 1000 samples with 32 threads → ~128MB RAM
VCF caching is minimal:
Only 2 VCF lines per thread are pre-cached.
In the simple diploid case (e.g., 0/1, 1|0), each genotype requires ~4 characters (8 bytes).
For 1000 samples and 32 threads, this adds up to ~1MB RAM.
JVM will need at least 64-128 MB in order to efficiently run.
Total memory footprint: just a few hundred MB, even for large datasets.
It is not straightforward to define a strict minimum amount of RAM required for a given number of SNPs and samples, as JVM behavior can vary across different systems and configurations.
From our own experiments, a rough estimate for the minimum usable memory is around 10 bytes per variant per sample.
For example, a VCF file with 1 million variants and 1,000 samples would require at least 10 x 10⁶ x 10³ = 10 GB of allocated memory.
However, running with this minimal allocation may result in frequent and prolonged garbage collection events, leading to significantly longer runtimes.
For optimal execution, we recommend allocating 15-20 bytes per variant per sample (i.e., 15-20 GB for the same example), which reduces garbage collection overhead and ensures smoother performance.
In order to allocate RAM, a special parameter needs to be passed while JVM initializes. JVM parameters can be passed by setting java.parameters option.
The -Xmx parameter, followed (without space) by an integer value and a letter, is used to tell JVM what is the maximum amount of heap RAM that it can use.
The letter in the parameter (uppercase or lowercase), indicates RAM units.
For example, parameters -Xmx1024m or -Xmx1024M or -Xmx1g or -Xmx1G, allocate 1 Gigabyte or 1024 Megabytes of maximum RAM for JVM.
In order to allocate 1024MB of RAM for the JVM, through R code, use:
options(java.parameters = "-Xmx1024M")
When using fastreeR as a CLI, then RAM allocation in MB can be achieved with the relevant argument --mem MEM.
Installation and Usage
Via Conda
fastreeR is available on Bioconda. You can install it in a new conda environment like so:
Calculates a cosine type dissimilarity measurement between the n samples of a VCF file.
Biallelic or multiallelic (maximum 7 alternate alleles) SNP and/or INDEL variants are considered, phased or not. Some VCF encoding examples are:
heterozygous variants : 1/0 or 0/1 or 0/2 or 1|0 or 0|1 or 0|2
homozygous to the reference allele variants : 0/0 or 0|0
homozygous to the first alternate allele variants : 1/1 or 1|1
If there are n samples and m variants, an nxn zero-diagonal symmetric distance matrix is calculated.
The calculated cosine type distance (1-cosine_similarity)/2 is in the range [0,1] where value 0 means completely identical samples (cosine is 1), value 0.5 means perpendicular samples (cosine is 0) and value 1 means completely opposite samples (cosine is -1).
The calculation is performed by a Java back-end implementation, that supports multi-core CPU utilization and can be demanding in terms of memory resources.
Output distances is a PHYLIP compatible file will contain n+1 lines.
The first line contains the number n of samples and number m of variants, separated by space.
Each of the subsequent n lines contains n+1 values, separated by space.
The first value of each line is a sample name and the rest n values are the calculated distances of this sample to all the samples.
Example output file of the distances of 3 samples calculated from 1000 variants:
3 1000
Sample1
0.0
0.5
0.2
Sample2
0.5
0.0
0.9
Sample3
0.2
0.9
0.0
Embedding-Based Distance Calculation
Version 2.5.0 of the Java backend introduces support for embedding-based distance calculation in VCF2DIST and VCF2TREE. This feature allows you to incorporate pre-computed variant embeddings (e.g., from genomic language models like BioFM, DNA-BERT, Nucleotide Transformer, or custom embeddings) to compute distances in embedding space rather than genotype space.
How It Works
Instead of computing cosine similarity directly from genotype vectors, the embedding mode:
Projects each sample into embedding space: H_i = Σ_v dosage_i^v × e_v
Computes cosine distance between sample embeddings
This captures functional relationships between variants - samples with alleles at functionally similar positions become more similar in embedding space.
You can also request bootstrap replicates directly from the VCF source. The Java backend will perform streaming bootstrap sampling and encode bootstrap support values at internal nodes of the returned Newick string.
For example:
The VCF2EMB command uses the BioFM-265M genomic language model to generate embeddings for each variant in a VCF file. These embeddings can then be used with VCF2DIST or VCF2TREE for embedding-based distance calculation.
Supports gzipped input files: VCF (.vcf.gz), reference genome (.fa.gz, .fasta.gz, .fna.gz), and annotation (.gff.gz, .gff3.gz) files are automatically decompressed during processing.
fastreeR: Fast Tree Reconstruction Tools for Genomics (VCF/FASTA to Distance/Tree)
fastreeRis a hybrid toolkit combining a high-performance Java backend (BioInfoJava-Utils, a modular Java library for bioinformatics pipelines) with flexible and user-friendly interfaces across multiple platforms and environments, enabling seamless integration into a variety of genomic workflows. It enables fast computation of distance matrices and phylogenetic trees from genetic variant data in VCF or genomic sequences in FASTA format.Integration and Accessibility
fastreeRoffers interface, which is accessible in the following ways:conda install -c bioconda fastreer(recipe)pip install fastreer(repository)rJava(package)Key Features
rJavaRequirements
Memory requirements for VCF input
No more GBs of RAM! Only the distance matrix is kept in memory:
4 bytes x (#samples²) x #threadsVCF caching is minimal: Only 2 VCF lines per thread are pre-cached.
0/1,1|0), each genotype requires ~4 characters (8 bytes).JVM will need at least 64-128 MB in order to efficiently run.
Total memory footprint: just a few hundred MB, even for large datasets.
It is not straightforward to define a strict minimum amount of RAM required for a given number of SNPs and samples, as JVM behavior can vary across different systems and configurations. From our own experiments, a rough estimate for the minimum usable memory is around 10 bytes per variant per sample. For example, a VCF file with 1 million variants and 1,000 samples would require at least 10 x 10⁶ x 10³ = 10 GB of allocated memory. However, running with this minimal allocation may result in frequent and prolonged garbage collection events, leading to significantly longer runtimes. For optimal execution, we recommend allocating 15-20 bytes per variant per sample (i.e., 15-20 GB for the same example), which reduces garbage collection overhead and ensures smoother performance.In order to allocate RAM, a special parameter needs to be passed while JVM initializes. JVM parameters can be passed by setting
java.parametersoption. The-Xmxparameter, followed (without space) by an integer value and a letter, is used to tell JVM what is the maximum amount of heap RAM that it can use. The letter in the parameter (uppercase or lowercase), indicates RAM units. For example, parameters-Xmx1024mor-Xmx1024Mor-Xmx1gor-Xmx1G, allocate 1 Gigabyte or 1024 Megabytes of maximum RAM for JVM.In order to allocate 1024MB of RAM for the JVM, through R code, use:
When using
fastreeRas a CLI, then RAM allocation in MB can be achieved with the relevant argument--mem MEM.Installation and Usage
Via Conda
fastreeRis available on Bioconda. You can install it in a new conda environment like so:Via Docker
fastreeRis available as a lightweight, multithreaded, platform-independent Docker image hosted on both DockerHub and GHCR.From DockerHub:
Or from GitHub Container Registry (GHCR):
To compute a tree directly from a VCF file:
This:
$(pwd)inside the containerinput.vcfand writesoutput.nwkrelative to your hostThe Docker image includes:
.jarlibrariesfastreeR.pyCLI entry pointExample: FASTA to distance
Memory tuning. Use the
--memoption to control how much memory is allocated to the Java backend:As a PyPI Module
You can install the Python CLI directly from PyPI using:
This will install the fastreeR command-line tool (
fastreer) and include the Java backend jars required for running all commands.To check it installed correctly:
Via a Python CLI wrapper
Another easy method for using
fastreeRis by its Python CLI:Note: If you want to use a custom backend location, set the environment variable
FASTREER_JAR_DIR.As an R package
To install
fastreeRas an R package:You can install the development version of
fastreeRR package like so:With Galaxy
Search in Galaxy Tools for
fastreeror ask your Galaxy Admin to install it from toolshed.From java backend source
To build the Java backend from source code:
Then copy the resulting
.jarfile(s) to thefastreeR/inst/java/directory:Finally run the tool from its Python CLI:
Distances from VCF
Calculates a cosine type dissimilarity measurement between the
nsamples of a VCF file.Biallelic or multiallelic (maximum 7 alternate alleles) SNP and/or INDEL variants are considered, phased or not. Some VCF encoding examples are:
1/0or0/1or0/2or1|0or0|1or0|20/0or0|01/1or1|1If there are
nsamples andmvariants, annxnzero-diagonal symmetric distance matrix is calculated. The calculated cosine type distance (1-cosine_similarity)/2 is in the range[0,1]where value0means completely identical samples (cosine is1), value0.5means perpendicular samples (cosine is0) and value 1 means completely opposite samples (cosine is-1).The calculation is performed by a Java back-end implementation, that supports multi-core CPU utilization and can be demanding in terms of memory resources.
Output distances is a PHYLIP compatible file will contain
n+1lines. The first line contains the numbernof samples and numbermof variants, separated by space. Each of the subsequentnlines containsn+1values, separated by space. The first value of each line is a sample name and the restnvalues are the calculated distances of this sample to all the samples. Example output file of the distances of 3 samples calculated from 1000 variants:Embedding-Based Distance Calculation
Version 2.5.0 of the Java backend introduces support for embedding-based distance calculation in
VCF2DISTandVCF2TREE. This feature allows you to incorporate pre-computed variant embeddings (e.g., from genomic language models like BioFM, DNA-BERT, Nucleotide Transformer, or custom embeddings) to compute distances in embedding space rather than genotype space.How It Works
Instead of computing cosine similarity directly from genotype vectors, the embedding mode:
H_i = Σ_v dosage_i^v × e_vThis captures functional relationships between variants - samples with alleles at functionally similar positions become more similar in embedding space.
Embedding File Formats
TSV Format:
HuggingFace JSON Format:
Embedding Command Line Options
-e, --embeddings--embeddings-formatTSVorHUGGINGFACE(auto-detected if not specified)--variant-keyCHROM_POS,CHROM_POS_REF_ALT(default), orVCF_IDEmbedding Examples
Variants without matching embeddings are automatically skipped, and the tool reports how many variants were used vs. skipped.
CLI Interface
The Python CLI (
fastreeR.py) interfaces with the Java backend viasubprocess, providing a unified command-line interface for all supported tools.Commands
General Syntax
VCF2DISTVCF2TREEDIST2TREEFASTA2DISTVCF2EMBExamples
Compute Distance Matrix from VCF
Compute Newick NJ tree directly from a VCF file.
You can also request bootstrap replicates directly from the VCF source. The Java backend will perform streaming bootstrap sampling and encode bootstrap support values at internal nodes of the returned Newick string. For example:
The generated Newick will contain node support values (percentage across replicates) which can be inspected with phylogenetic tools such as
apein R.Compute Tree from Distance Matrix
Input format: tab-separated PHYLIP-compatible matrix.
Compute D2S k-mer distance matrix from a FASTA file.
Generate Variant Embeddings from VCF using BioFM
The
VCF2EMBcommand uses the BioFM-265M genomic language model to generate embeddings for each variant in a VCF file. These embeddings can then be used withVCF2DISTorVCF2TREEfor embedding-based distance calculation.Supports gzipped input files: VCF (
.vcf.gz), reference genome (.fa.gz,.fasta.gz,.fna.gz), and annotation (.gff.gz,.gff3.gz) files are automatically decompressed during processing.Prerequisites:
Python 3.11 environment (required by biofm-eval):
Install PyTorch:
Install biofm-eval from source (not available on PyPI):
Download reference genome (GRCh38): NCBI
Download gene annotations (GENCODE v38): GENCODE
You can set default paths via environment variables:
Pipe input from gzip-compressed file
Output Examples
Options (common to all commands)
-i, --input: Input file (VCF or distance matrix). Use-for stdin.-o, --output: Output file. If omitted, prints to stdout.-t, --threads: Number of threads (default: 1).--mem MEM: Max RAM for JVM in MB (default: 256).--lib LIB: Path to the folder containing backend JAR libraries (default: inst/java)--verbose: Print progress information to stderr.--pipe-stderr: Pipe stderr and forward from Python (default: direct passthrough to terminal).--version: Print version and citation information.Embedding options (VCF2DIST and VCF2TREE only)
-e, --embeddings: Path to variant embeddings file for embedding-based distance calculation.--embeddings-format: Embeddings file format:TSVorHUGGINGFACE(auto-detected if not specified).--variant-key: Variant key format for embedding lookup:CHROM_POS,CHROM_POS_REF_ALT(default), orVCF_ID.VCF2EMB options (embedding generation)
-i, --input: Input VCF file.-o, --output: Output embeddings file (default: stdout).-r, --reference: Path to reference genome FASTA file (or setBIOFM_REFERENCE_GENOMEenv var).-a, --annotation: Path to gene annotation GFF3 file (or setBIOFM_GENE_ANNOTATIONenv var).-m, --model: HuggingFace model name or local path (default:m42-health/BioFM-265M).-f, --format: Output format:TSVorHUGGINGFACE(default:TSV).--variant-key: Variant key format in output:CHROM_POS,CHROM_POS_REF_ALT(default), orVCF_ID.--max-variants: Maximum number of variants to process (default: all).--batch-size: Batch size for embedding extraction (default: 32).--device: Device for model inference:cudaorcpu(default: auto-detect).Integration with Java Backend
The CLI wraps tools from the BioInfoJava-Utils project and dynamically builds the Java classpath from all
.jarfiles located ininst/java/.Integration with R
All core functionality is available via the
fastreeRR package (Bioconductor/devel):See fastreeR R manual and fastreeR R vignette for usage in R.
Sample data
Toy vcf, fasta and distance sample data files are provided in
inst/extdata.samples.vcf.gz
Sample VCF file of 100 individuals and 1000 variants, in Chromosome22, from the 1K Genomes project. Original file available at http://hgdownload.cse.ucsc.edu/gbdb/hg19/1000Genomes/phase3/
samples.vcf.dist.gz
Distances from the previous sample VCF
samples.vcf.istats
Individual statistics from the previous sample VCF
samples.fasta.gz
Sample FASTA file of 48 random bacteria RefSeq from ftp://ftp.ncbi.nlm.nih.gov/genomes/refseq/bacteria/.
samples.fasta.dist.gz
Distances from the previous sample FASTA
Citation
If you use
fastreeRin your research, please cite:Author
Anestis Gkanogiannis
Bioinformatics/ML Scientist
Linkedin: https://www.linkedin.com/in/anestis-gkanogiannis/
Website: https://github.com/gkanogiannis
ORCID: 0000-0002-6441-0688
License
fastreeRis licensed under the GNU General Public License v3.0.See the LICENSE file for details.