Skip to content

Instantly share code, notes, and snippets.

@jonesor
Created June 20, 2025 13:24
Show Gist options
  • Save jonesor/cdde22ef183613f529b39bbd69d39b1f to your computer and use it in GitHub Desktop.
Save jonesor/cdde22ef183613f529b39bbd69d39b1f to your computer and use it in GitHub Desktop.
Construct a phylogeny, using V.Phylomaker2, for COMPADRE
# Load necessary packages
library(devtools)
library(V.PhyloMaker2)
library(Rcompadre)
library(dplyr)
library(ape)
# Fetch COMPADRE data
compadre <- cdb_fetch("compadre")
# Extract unique species list (Genus, Species, Family)
speciesList <- cdb_metadata(compadre) %>%
select(Genus, Species, Family) %>%
distinct() %>%
mutate(Species = paste(Genus, Species)) %>%
select(Species, Genus, Family)
# Build phylogeny using V.PhyloMaker2 (scenario 3 only)
# Scenario 3 (S3): maximally resolves the phylogeny by inserting all species as
# polytomies within their genera and attaches genera to the family backbone as
# polytomies if genus-level phylogeny is missing. This produces the most
# fully-resolved tree possible given the data.
result <- phylo.maker(speciesList, scenarios = "S3")
# Extract the tree from scenario 3
tree <- result$scenario.3
# Check if the tree is ultrametric and binary
is.ultrametric(tree)
is.binary(tree)
# If needed, force the tree to be ultrametric
if (!is.ultrametric(tree)) {
tree <- chronos(tree) # Force ultrametric
}
# Ensure the tree is fully dichotomous (binary)
compadreTree <- multi2di(tree)
# Plot the tree in radial format
plot.phylo(compadreTree, type = "fan", cex = 0.3, main = "Phylogeny (Scenario 3)")
# Save result and tree to file
save(result, compadreTree, file = "CompadrePhylogeny.Rdata")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment