###
# IQTREE Supplementary Figure
###

# Produced by Sven BUERKI (svenbuerki@boisestate.edu), October 2025

###
# Require package(s)
###
library(ape)
library(phytools)

###~~~
# 1. Load data
###~~~
#IQTREE
IQtree <- ape::read.tree(file = "IQTREE_Turraea/partition.nex.contree")

# Load CSV with tip names
tip.names <- read.csv("translation_tips_Turraea_revMC.csv")
# Produce final names
tip.names$Final.Names <- gsub(" ", "_", paste(tip.names$Species.name, " - ", tip.names$Collectors, sep = ""))

###~~~
#2. Discard bootstrap values < 50%
###~~~
#Find nodes with low statistical supports
IQtree$node.label[which(as.numeric(IQtree$node.label) < 50)]

#Replace/overwrite values by ""
IQtree$node.label[which(as.numeric(IQtree$node.label) < 50)] <- ""

###~~~
#3. Rename tips
###~~~
###
# Rename tips
###
# Create vector with new names
new_tips <- sapply(IQtree$tip.label, function(tip) {
  if (tip %in% tip.names$idSequencing) {
    tip.names$Final.Names[match(tip, tip.names$idSequencing)]
  } else {
    tip  # keep original name
  }
})
# Rename tips in tree 
IQtree$tip.label <- as.vector(new_tips)

###
#4. Prepare tree
###
# Ensure the tree is rooted (it was done in our IQTREE analysis)
IQtree_rooted <- root(IQtree, outgroup = tip.names$Final.Names[which(tip.names$idSequencing == "PAFTOL_06473_L001")], resolve.root = TRUE)

# Ladderize the rooted tree for cleaner layout
IQtree_ladderized <- ladderize(IQtree_rooted, right = F)
# Rm "Root" label
IQtree_ladderized$node.label[which(IQtree_ladderized$node.label == "Root")] <- "100"

###~~~
#5. Define MRCA for clades
###~~~
clade_I <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_pulchella_-_Pelger_730", "Turraea_streyi_-_Strey_6876"))
clade_II <- ape::getMRCA(IQtree_ladderized, tip = c("Nymania_capensis_-_Maurin_1096", "Nymania_capensis_-_Chase_270"))
clade_III <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_africana_-_Grossweiler_4914", "Naregamia_alata_-_Kanodia_89603"))
#clade_IV <- ape::getMRCA(IQtree_ladderized, tip = as.vector(tip.names$Final.Names[which(tip.names$idSequencing %in% c("Turraea_023_L001", "Turraea_015_L001"))]))
clade_IVa <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_sp._nov._1_-_Leroy_16", "Turraea_sp._nov._3_-_Nusbaumer_1283"))
clade_IVb <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_humberti_-_Léandri_2042", "Turraea_silvestris_-_Antilahimena_7148"))
clade_IVc <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_cadetii_-_Friedman_2607", "Turraea_ovata_-_Strasberg_752"))
clade_IVd <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_geayi_-_Philippson_2968", "Turraea_virens_-_Ralimanana_260"))
clade_IVe <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_pervillei_-_Jongkind_3615", "Turraea_fockei_-_Razafitsalama_1239"))
clade_IVf <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_sambavensis_-_Service_Forestier_27118", "Turraea_sericea_-_Nusbaumer_1924"))
clade_IVg <- ape::getMRCA(IQtree_ladderized, tip = c("Humbertioturraea_labatii_-_Labat_2788", "Humbertioturraea_grandidieri_-_Jongkind_et_al._3605"))
clade_IVh <- ape::getMRCA(IQtree_ladderized, tip = c("Turraea_wakefieldii_-_Burrows_8596", "Turraea_pubescens_-_Frodin_843"))

###~~~
#6. Plot tree
###~~~
pdf("Figures/SuppMat_Figure_IQTREE_Turraea.pdf")
#ape::plot.phylo(IQtree_ladderized, cex=.8, show.node.label = TRUE, show.tip.label = T, align.tip.label = T, no.margin = TRUE)

# Plot the tree with custom tip and node label sizes
par(mar = c(4, 4, 2, 2))  # Set reasonable margins to avoid rotation/clipping
plot.phylo(IQtree_ladderized, 
           cex = 0.4,  # Size of tip labels (adjust as needed, e.g., 1.0 for larger, 0.5 for smaller)
           show.node.label = TRUE,  # Temporarily disable to add node labels separately
           show.tip.label = TRUE, 
           align.tip.label = TRUE, 
           use.edge.length = FALSE,
           no.margin = FALSE,  # Use margins to prevent layout issues
           label.offset = 0)  # Space between tips and labels

# Add node labels with custom size
#nodelabels(IQtree_ladderized$node.label, 
#           cex = 0.3,  # Size of node labels (adjust as needed, e.g., 1.0 for larger, 0.4 for smaller)
#           frame = "none",  # No background frame for cleaner look
#           adj = c(0.5, -0.5))  # Adjust position for visibility (tweak if needed)

###
#Clades
###
###
# Clades
###
# Clade I
cladebox(IQtree_ladderized, clade_I, make.transparent("red", 0.4))
cladelabels(tree=IQtree_ladderized, text = "Clade I", node = clade_I, offset=30, wing.length=.1, cex=1,
            orientation="horizontal")
# Clade II
cladebox(IQtree_ladderized, clade_II, make.transparent("blue", 0.4))
cladelabels(tree=IQtree_ladderized, text = "Clade II", node = clade_II, offset=29, wing.length=.1, cex=1,
            orientation="horizontal")

# Clade III
cladebox(IQtree_ladderized, clade_III, make.transparent("orange", 0.4))
cladelabels(tree=IQtree_ladderized, text = "Clade III", node = clade_III, offset=30, wing.length=.1, cex=1,
            orientation="horizontal")

# Clade IVa
cladebox(IQtree_ladderized, clade_IVa, make.transparent("gold", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVa", node = clade_IVa, offset=26, wing.length=.1, cex=1,
            orientation="vertical")

# Clade IVb
cladebox(IQtree_ladderized, clade_IVb, make.transparent("green", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVb", node = clade_IVb, offset=28, wing.length=.1, cex=1,
            orientation="vertical")

# Clade IVc
cladebox(IQtree_ladderized, clade_IVc, make.transparent("violet", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVc", node = clade_IVc, offset=28, wing.length=.1, cex=1,
            orientation="vertical")

# Clade IVd
cladebox(IQtree_ladderized, clade_IVd, make.transparent("brown", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVd", node = clade_IVd, offset=28, wing.length=.1, cex=1,
            orientation="vertical")

# Clade IVe
cladebox(IQtree_ladderized, clade_IVe, make.transparent("plum1", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVe", node = clade_IVe, offset=28, wing.length=.1, cex=1,
            orientation="vertical")

# Clade IVf
cladebox(IQtree_ladderized, clade_IVf, make.transparent("grey", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVf", node = clade_IVf, offset=28, wing.length=.1, cex=1,
            orientation="vertical")
# Clade IVg
cladebox(IQtree_ladderized, clade_IVg, make.transparent("deeppink", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVg", node = clade_IVg, offset=28, wing.length=.1, cex=1,
            orientation="vertical")
# Clade IVh
cladebox(IQtree_ladderized, clade_IVh, make.transparent("cadetblue1", 0.4))
cladelabels(tree=IQtree_ladderized, text = "IVh", node = clade_IVh, offset=28, wing.length=.1, cex=1,
            orientation="vertical")
dev.off()
