An Overview of Common R Packages for Building and Analyzing Phylogenetic Trees: Introduction to poppr

Key R Packages for Phylogenetic Analysis

  1. geiger:
    • Application: Macroevolutionary analysis including branch-specific models, ancestral state reconstruction, and lineage diversity analysis.
    • Data Input/Output: Primarily accepts tree objects and character data from the ape package.
    • Description: Provides functions for exploring evolutionary patterns and trends across species.
  2. diversitree:
    • Application: Analysis of species diversity and lineage diversification dynamics, including Birth-Death processes and multistate evolution models.
    • Data Input/Output: Accepts tree objects and trait data from the ape package.
    • Description: Offers statistical modeling tools to investigate speciation and extinction patterns.
  3. picante:
    • Application: Calculation of community richness, diversity indices, and correlation analyses.
    • Data Input/Output: Accepts community data matrices and phylogenetic tree objects.
    • Description: A specialized package for ecological and community ecology studies, integrating evolutionary and community structure analysis.
  4. ade4:
    • Application: Multivariate data analysis including principal component analysis, clustering, and ordination methods.
    • Data Input/Output: Accepts various types of tabular and categorical data.
    • Description: A general-purpose package offering numerous functions for exploratory data analysis and statistical modeling.
  5. mvMORPH:
    • Application: Handling and analyzing multivariate morphological data, including principal coordinates analysis and Procrustes analysis.
    • Data Input/Output: Accepts tabular morphological datasets.
    • Description: Specifically designed for morphological research, supporting complex multivariate morphometric analyses.
  6. r8s:
    • Application: Estimating molecular clock parameters and node ages using constrained optimization approaches.
    • Data Input/Output: Accepts sequence alignment files and Newick-formatted tree files.
    • Description: Dedicated to molecular clock analysis and node time estimation.
  7. strap:
    • Application: Joint analysis of phylogenetic and species trees.
    • Data Input/Output: Accepts gene tree and species tree data.
    • Description: Useful for studying gene tree-species tree relationships, especially under incomplete lineage sorting conditions.
  8. TreeSim:
    • Application: Simulating phylogenetic trees and species distribution processes.
    • Data Input/Output: Generates and accepts Newick-formatted tree files.
    • Description: Facilitates simulation of evolutionary and species distribution scenarios to understand evolutionary patterns.
  9. TreeSearch:
    • Application: Searching for optimal phylogenetic trees and performing model selection.
    • Data Input/Output: Accepts and generates Newick-formatted tree files.
    • Description: Provides multiple algorithms for tree search and optimization to identify the best-fitting phylogeny.
  10. evobiR:
    • Application: Phenotypic evolution and adaptive radiation analysis.
    • Data Input/Output: Accepts phenotypic data tables and phylogenetic tree objects.
    • Description: Focused on phenotypic evolution and adaptive radiations, offering statistical methods and visualization tools.
  11. ouch:
    • Application: Analysis of haplotype data.
    • Data Input/Output: Accepts haplotype data tables and phylogenetic tree objects.
    • Description: Designed for haplotype data analysis, including haplotype network construction and associated statistical tests.
  12. pegas:
    • Application: Analysis of population genetic data.
    • Data Input/Output: Accepts genetic marker data tables and population information.
    • Description: Provides functions for population genetics analysis, including gene flow, population structure, and genetic diversity metrics.
  13. adegenet:
    • Application: Analysis of polymorphic data, including principal coordinates analysis and clustering.
    • Data Input/Output: Accepts tabular polymorphism data.
    • Description: Tailored for genetic marker data analysis, including polymorphism detection and population structure assessment.
  14. poppr:
    • Application: Handling and analyzing population genetic data with repeated measurements.
    • Data Input/Output: Accepts genetic marker data tables and population information.
    • Description: Specifically designed for population genetic datasets containing repeated measures, providing related statistical methods and visualization tools.
  15. phrapl:
    • Application: Analysis of region-based phylogenetic data.
    • Data Input/Output: Accepts tabular regional data and phylogenetic tree objects.
    • Description: Mainly used for regional phylogenetic analysis, including regional partitioning and tree construction.
  16. treeio:
    • Application: Reading and writing various formats of phylogenetic tree files.
    • Data Input/Output: Supports formats such as Newick, Nexus, PHYLOXML, and NeXML.
    • Description: A convenient package for handling different phylogenetic tree file formats.
  17. ggtree:
    • Application: Phylogenetic tree visualization based on ggplot2.
    • Data Input/Output: Accepts tree objects from the ape package and outputs SVG or PDF graphics.
    • Description: Offers a ggplot2-based approach for creating highly customizable tree visualizations.

These packages cover a broad spectrum of tasks in phylogenetic tree construction and analysis—from simple tree building to complex macroevolutionary investigations. Choosing the appropriate tool depends on your specific research question and data characteristics. Most packages require installation before use, often accompanied by documentation and example scripts to facilitate quick adoption.

Frequently Used Phylogenetic Tree Formats and Their Generation Software

  1. Newick (.tre, .nw, .nwk):
    • Software: PhyML, RAxML, FastTree, MrBayes
    • Input: Multiple sequence alignment files (e.g., fasta)
    • Output: Text-based description of trees, including branch lengths and node support values
    • Example Command (RAxML): raxmlHPC -s aligned_sequences.fasta -n my_tree -m GTRGAMMA
  2. Nexus (.nex):
    • Software: PAUP*, TNT, MrBayes
    • Input: Multiple sequence alignment files, possibly with metadata
    • Output: Contains systematics analysis information including sequence data, model parameters, and tree structures
    • Example Command (MrBayes): mb ./my mrbayes.nex
  3. NHX (.nhx):
    • Software: ETE Toolkit, PhyloXML
    • Input: Newick formatted phylogenetic trees
    • Output: Extended Newick format with additional metadata and attributes
    • Example: Usually generated via script or tool to convert Newick to NHX
  4. Phylip (.phy):
    • Software: PHYLIP, RAxML, IQ-TREE
    • Input: Multiple sequence alignment files
    • Output: Compact text format, each line representing a species and its corresponding sequence
    • Example Command (RAxML): raxmlHPC-PTHREADS -s aligned_sequences.phy -n my_tree -m GTRGAMMA
  5. Jplace (.jplace):
    • Software: raxml-place, pplacer
    • Input: Typically raw sequence data and a reference tree
    • Output: JSON format file detailing placement information of sequences on the reference tree
    • Example: Generated by placement software
  6. BEAST (.xml):
    • Software: BEAST, BEAUti
    • Input: Multiple sequence alignment files and parameter settings
    • Output: XML configuration file containing all information for Bayesian phylogenetic inference
    • Example: Generated using BEAUti GUI, then run with BEAST
  7. PhyloXML (.phyloxml):
    • Software: PhyloXML Tools, ETE Toolkit
    • Input: Various phylogenetic data formats
    • Output: XML file documenting detailed tree information and metadata
    • Example: Convert Newick to PhyloXML using ETE Toolkit command-line tools
  8. NeXML (.nexml):
    • Software: nexml-python, CDAOtools
    • Input: Various phylogenetic data formats
    • Output: XML file following NeXML standard, including tree and metadata
    • Example: Use nexml-python library to convert Newick to NeXML
  9. NHWT (.tre):
    • Software: Dendroscope
    • Input: Newick formatted phylogenetic trees
    • Output: Binary tree file for fast loading and visualization in Dendroscope
    • Example: Open or save Newick files as NHWT in Dendroscope
  10. PDF / SVG (.pdf, .svg):
    • Software: FigTree, iTOL, EvolView
    • Input: Various phylogenetic tree formats
    • Output: Scalable vector graphic formats for high-quality publication and presentation
    • Example: Open tree file in FigTree and export as PDF or SVG
  11. CSV / TSV (.csv, .tsv):
    • Software: Custom scripts, Python libraries (ete3, biopython)
    • Input: Phylogenetic trees and related data
    • Output: Tabular format suitable for data analysis and processing
    • Example: Extract tree information from Newick and save as CSV using Python scripts
  12. JSON (.json):
    • Software: Custom scripts, JavaScript libraries (phylocanvas)
    • Input: Phylogenetic trees and related data
    • Output: Lightweight data exchange format suitable for web applications
    • Example: Convert Newick to JSON using JavaScript libraries

Example of Phylogenetic Tree Construction Using ape

This section presents a complete example including input data format, processing steps, and corresponding code.

Input Data Format:

Input data is typically in FASTA format. For example:

>Seq1
ATCGATCGATCG
>Seq2
ATCGATCGTGC
>Seq3
ATCGTAGCTAG
...

Each sequence begins with ">" followed by an identifier (e.g., Seq1), followed by the nucleotide or protein sequence.

Processing Steps:

  1. Reading FASTA Data: Use read.fasta() to load FASTA formatted sequence data into a list.
  2. Calculating Distance Matrix: Use dist.dna() or dist.alignment() to compute pairwise distances among sequences.
  3. Building Phylogenetic Tree: Apply nj() function to construct a tree using the Neighbor-Joining algorithm.
  4. Visualizing Tree: Use plot() to draw the tree with optional customization.

Here's a sample R script:

# Load the ape package
library(ape)

# Read FASTA formatted sequence data
seqs <- read.fasta("sequences.fasta")

# Align sequences
alignment <- align.seqs(seqs)

# Compute distance matrix
dist_matrix <- dist.dna(alignment)

# Construct phylogenetic tree using NJ method
tree <- nj(dist_matrix)

# Plot the tree
plot(tree, type="unrooted", cex=0.8, label.offset=0.5)

In this example, we first load the ape package and read a FASTA file named "sequences.fasta". Then, we align the sequences, calculate the DNA distance matrix, build a phylogenetic tree using the NJ method, and finally plot it with customized appearance.

Using Phytools

Phytools is an R package for analyzing phylogenetic trees and evolutionary traits. Below is a basic example using Phytools to build a phylogenetic tree.

Input Data Format:

  • Nexus File: Commonly used to store molecular phylogenetic data including sequences and tree structures.
  • Species Trait Data (Optional): A CSV or other tabular data file containing trait values for each species.

Nexus File Generation:

Nexus files are usually generated by molecular phylogenetics software that infers evolutionary relationships from biological sequence data (DNA, RNA, or proteins). Here’s a typical workflow:

  1. Sequence Collection: Obtain target species' gene or protein sequences from databases or experimental data.
  2. Sequence Preprocessing: Clean and prepare sequences if needed.
  3. Multiple Sequence Alignment: Align sequences using tools like ClustalW, MAFFT, or Muscle.
  4. Model Selection: Choose an appropriate evolutionary model using tools like JModelTest.
  5. Tree Construction: Build trees using maximum likelihood (RAxML), Bayesian inference (MrBayes), or neighbor-joining (PHYLIP).
  6. Run Analysis: Execute the analysis with chosen parameters.
  7. Export Nexus File: Save the resulting tree in Nexus format.

Example command for RAxML:

raxmlHPC -s aligned_sequences.fasta -n my_tree -m GTRGAMMA -p 12345

Species Trait Data Format:

A typical CSV file may look like:

Species,Trait1,Trait2,Trait3
SpeciesA,10.5,Yes,Blue
SpeciesB,8.2,No,Green
SpeciesC,12.1,Yes,Red
SpeciesD,9.8,No,Yellow

Each row represents a species with its respective trait values.

Processing Steps:

  1. Load Required Package: Install and load the phytools package.
  2. Read Tree File: Use read.nexus() to load a Nexus tree file.
  3. (Optional) Read Trait Data: Use read.csv() to load trait data.
  4. Map Traits to Tree: Use trait.data() to associate traits with tree tips.
  5. Visualize Tree and Traits: Use plotTree() or plotSimmap().

Complete R code example:

# Load phytools
library(phytools)

# Read Nexus tree file
tree <- read.nexus("example_tree.nex")

# (Optional) Read trait data
traits <- read.csv("species_traits.csv", header=TRUE)

# Map traits to tree
tree <- trait.data(tree, traits$Trait1, tip.labels=tree$tip.label)

# Plot tree with traits
plotTree(tree, type="fan", show.tip.label=TRUE, cex=0.8, label.offset=0.02)

Constructing Trees with RAxML

RAxML (Randomized Axelerated Maximum Likelihood) is a widely used tool for constructing maximum likelihood (ML) phylogenetic trees. It supports various models and optimization algorithms, making it efficient for large-scale sequence datasets.

Below is a detailed example script for building a maximum likelihood phylogenetic tree using RAxML:

  1. Prepare Input Files: Start with a FASTA file containing aligned sequences, e.g., aligned_sequences.fasta.

For more resources and learning materials, refer to the provided links or documentation.

Tags: R phylogenetics ape phytools RAxML

Posted on Wed, 26 Aug 2026 16:19:45 +0000 by php_b34st