1 About fastreeR

The goal of fastreeR is to provide functions for calculating distance matrix, building phylogenetic tree or performing hierarchical clustering between samples, directly from a VCF or FASTA file.

2 Installation

To install fastreeR package:

if (!requireNamespace("BiocManager", quietly=TRUE))

3 Preparation

3.1 Allocate RAM and load required libraries

You should allocate minimum 10kb per sample per variant of RAM for the JVM. The more RAM you allocate, the faster the execution will be (less pauses for garbage collection). 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, all allocate 1 Gigabyte or 1024 Megabytes of maximum RAM for JVM.


3.2 Download sample vcf file

We download, in a temporary location, a small vcf file from 1K project, with around 150 samples and 100k variants (SNPs and INDELs). We use BiocFileCache for this retrieval process so that it is not repeated needlessly. If for any reason we cannot download, we use the small sample vcf from fastreeR package.

bfc <- BiocFileCache::BiocFileCache(ask = FALSE)
tempVcfUrl <-
tempVcf <- BiocFileCache::bfcquery(bfc,field = "rname", "tempVcf")$rpath[1]
if( {
    { tempVcf <- BiocFileCache::bfcadd(bfc,"tempVcf",fpath=tempVcfUrl)[[1]]
    error=function(cond) {
        tempVcf <- system.file("extdata", "samples.vcf.gz", package="fastreeR")
    warning=function(cond) {
        tempVcf <- system.file("extdata", "samples.vcf.gz", package="fastreeR")
if(file.size(tempVcf) == 0L) {
    tempVcf <- system.file("extdata", "samples.vcf.gz", package="fastreeR")

3.3 Download sample fasta files

We download, in temporary location, some small bacterial genomes. We use BiocFileCache for this retrieval process so that it is not repeated needlessly. If for any reason we cannot download, we use the small sample fasta from fastreeR package.

tempFastasUrls <- c(
    #Mycobacterium liflandii
    #Pelobacter propionicus
    #Rickettsia prowazekii
    #Salmonella enterica
    #Staphylococcus aureus
tempFastas <- list()
for (i in seq(1,5)) {
    tempFastas[[i]] <- BiocFileCache::bfcquery(bfc,field = "rname", 
    if([[i]])) {
        { tempFastas[[i]] <- 
            BiocFileCache::bfcadd(bfc, paste0("temp_fasta",i), 
        error=function(cond) {
            tempFastas <- system.file("extdata", "samples.fasta.gz", 
        warning=function(cond) {
            tempFastas <- system.file("extdata", "samples.fasta.gz", 
    if(!file.exists(tempFastas[[i]])) {
        tempFastas <- system.file("extdata", "samples.fasta.gz", 
    if(file.size(tempFastas[[i]]) == 0L) {
        tempFastas <- system.file("extdata", "samples.fasta.gz", 

4 Functions on vcf files

4.1 Sample Statistics

myVcfIstats <- fastreeR::vcf2istats(inputFile = tempVcf)
Sample statistics from vcf file

Figure 1: Sample statistics from vcf file

4.2 Calculate distances from vcf

The most time consuming process is calculating distances between samples. Assign more processors in order to speed up this operation.

myVcfDist <- fastreeR::vcf2dist(inputFile = tempVcf, threads = 2)

4.3 Histogram of distances

graphics::hist(myVcfDist, breaks = 100, main=NULL, 
                                xlab = "Distance", xlim = c(0,max(myVcfDist)))
Histogram of distances from vcf file

Figure 2: Histogram of distances from vcf file

We note two distinct groups of distances. One around of distance value 0.05 and the second around distance value 0.065.

4.4 Plot tree from fastreeR::dist2tree

Notice that the generated tree is ultrametric.

myVcfTree <- fastreeR::dist2tree(inputDist = myVcfDist)
plot(ape::read.tree(text = myVcfTree), direction = "down", cex = 0.3)
ape::axisPhylo(side = 2)
Tree from vcf with fastreeR

Figure 3: Tree from vcf with fastreeR

Of course the same can be achieved directly from the vcf file, without calculating distances.

myVcfTree <- fastreeR::vcf2tree(inputFile = tempVcf, threads = 2)
plot(ape::read.tree(text = myVcfTree), direction = "down", cex = 0.3)
ape::axisPhylo(side = 2)
Tree from vcf with fastreeR

Figure 4: Tree from vcf with fastreeR

As expected from the histogram of distances, two groups of samples also emerge in the tree. The two branches, one at height around 0.055 and the second around height 0.065, are clearly visible.

4.5 Plot tree from stats::hclust

For comparison, we generate a tree by using stats package and distances calculated by fastreeR.

myVcfTreeStats <- stats::hclust(myVcfDist)
plot(myVcfTreeStats, ann = FALSE, cex = 0.3)
Tree from vcf with stats::hclust

Figure 5: Tree from vcf with stats::hclust

Although it does not initially look very similar, because it is not ultrametric, it is indeed quite the same tree. We note again the two groups (two branches) of samples and the 4 samples, possibly clones, that they show very close distances between them.

4.6 Hierarchical Clustering

We can identify the two groups of samples, apparent from the hierarchical tree, by using dist2clusters or vcf2clusters or tree2clusters. By playing a little with the cutHeight parameter, we find that a value of cutHeight=0.067 cuts the tree into two branches. The first group contains 106 samples and the second 44.

myVcfClust <- fastreeR::dist2clusters(inputDist = myVcfDist, cutHeight = 0.067)
#>  ..done.
if (length(myVcfClust) > 1) {
    tree <- myVcfClust[[1]]
    clusters <- myVcfClust[[2]]
#> [1] "1 106 HG00124 HG00153 HG00247 HG00418 HG00427 HG00501 HG00512 HG00577 HG00578 HG00635 HG00702 HG00716 HG00733 HG00866 HG00983 HG01195 HG01274 HG01278 HG01322 HG01347 HG01452 HG01453 HG01473 HG01477 HG01480 HG01482 HG01483 HG01590 HG01983 HG01995 HG02024 HG02046 HG02218 HG02288 HG02344 HG02347 HG02363 HG02372 HG02377 HG02381 HG02387 HG02388 HG02524 HG02525 HG02781 HG03487 HG03606 HG03618 HG03621 HG03633 HG03639 HG03650 HG03656 HG03699 HG03700 HG03715 HG03723 HG03761 HG03794 HG03797 HG03799 HG03806 HG03811 HG03842 HG03845 HG03847 HG03901 HG03904 HG03929 HG03948 HG03972 HG03982 HG03988 HG04024 HG04037 HG04050 HG04053 HG04055 HG04058 HG04114 HG04127 HG04128 HG04132 HG04135 HG04147 HG04149 HG04150 HG04174 HG04191 HG04192 NA07346 NA11993 NA12891 NA12892 NA19660 NA19675 NA19685 NA19737 NA19797 NA19798 NA20336 NA20344 NA20526 NA20871 NA20893 NA20898"
#> [2] "2 44 HG02478 HG02762 HG02869 HG02964 HG02965 HG03033 HG03034 HG03076 HG03249 HG03250 HG03306 HG03307 HG03309 HG03312 HG03339 HG03361 HG03373 HG03383 HG03408 HG03454 HG03493 HG03508 HG03566 HG03569 HG03574 HG03582 NA18487 NA19150 NA19240 NA19311 NA19313 NA19373 NA19381 NA19382 NA19396 NA19444 NA19453 NA19469 NA19470 NA19985 NA20313 NA20322 NA20341 NA20361"

5 Functions on fasta files

Similar analysis we can perform when we have samples represented as sequences in a fasta file.

5.1 Calculate distances from fasta

Use of the downloaded sample fasta file :

myFastaDist <- fastreeR::fasta2dist(tempFastas, kmer = 6)

Or use the provided by fastreeR fasta file of 48 bacterial RefSeq :

myFastaDist <- fastreeR::fasta2dist(
    system.file("extdata", "samples.fasta.gz", package="fastreeR"), kmer = 6)

5.2 Histogram of distances

graphics::hist(myFastaDist, breaks = 100, main=NULL, 
                                xlab="Distance", xlim = c(0,max(myFastaDist)))
Histogram of distances from fasta file

Figure 6: Histogram of distances from fasta file

5.3 Plot tree from fastreeR::dist2tree

myFastaTree <- fastreeR::dist2tree(inputDist = myFastaDist)
plot(ape::read.tree(text = myFastaTree), direction = "down", cex = 0.3)
ape::axisPhylo(side = 2)
Tree from fasta with fastreeR

Figure 7: Tree from fasta with fastreeR

5.4 Plot tree from stats::hclust

myFastaTreeStats <- stats::hclust(myFastaDist)
plot(myFastaTreeStats, ann = FALSE, cex = 0.3)
Tree from fasta with stats::hclust

Figure 8: Tree from fasta with stats::hclust

6 Session Info

#> R version 4.4.0 beta (2024-04-15 r86425)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.4 LTS
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.19-bioc/R/lib/ 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
#>  [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
#>  [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8      
#>  [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8          
#>  [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> attached base packages:
#> [1] grid      stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> other attached packages:
#> [1] BiocFileCache_2.12.0 dbplyr_2.5.0         ape_5.8             
#> [4] fastreeR_1.8.0       BiocStyle_2.32.0    
#> loaded via a namespace (and not attached):
#>  [1] sass_0.4.9            utf8_1.2.4            generics_0.1.3       
#>  [4] stringi_1.8.3         RSQLite_2.3.6         lattice_0.22-6       
#>  [7] digest_0.6.35         magrittr_2.0.3        evaluate_0.23        
#> [10] dynamicTreeCut_1.63-1 bookdown_0.39         fastmap_1.1.1        
#> [13] blob_1.2.4            R.oo_1.26.0           jsonlite_1.8.8       
#> [16] R.utils_2.12.3        DBI_1.2.2             tinytex_0.50         
#> [19] BiocManager_1.30.22   httr_1.4.7            purrr_1.0.2          
#> [22] fansi_1.0.6           jquerylib_0.1.4       cli_3.6.2            
#> [25] rlang_1.1.3           R.methodsS3_1.8.2     bit64_4.0.5          
#> [28] withr_3.0.0           cachem_1.0.8          yaml_2.3.8           
#> [31] tools_4.4.0           parallel_4.4.0        memoise_2.0.1        
#> [34] dplyr_1.1.4           filelock_1.0.3        curl_5.2.1           
#> [37] vctrs_0.6.5           R6_2.5.1              magick_2.8.3         
#> [40] lifecycle_1.0.4       stringr_1.5.1         bit_4.0.5            
#> [43] pkgconfig_2.0.3       rJava_1.0-11          pillar_1.9.0         
#> [46] bslib_0.7.0           glue_1.7.0            Rcpp_1.0.12          
#> [49] highr_0.10            xfun_0.43             tibble_3.2.1         
#> [52] tidyselect_1.2.1      knitr_1.46            htmltools_0.5.8.1    
#> [55] nlme_3.1-164          rmarkdown_2.26        compiler_4.4.0