---
title: "Megastigmidae_evoltree_script"
author: "G. Brown"
date: "2024-09-23"
output: html_document
---

### Context: Megastigmid wasps (Bootanomyia sp.; prev. Megastigmus) emerged from oak galls from two different geographic locations in the United States. These wasps are primarily associated with European, Asian, and Middle Eastern galls. Sequencing of COI reveals that there are two species, consistent with previous work on the species composition of Bootanomyia. 

### In this script, I am working with a phylogeny created from COI products of Bootanomyia wasps using iqtree under testmodel, bnni, and alrt 1000. This tree was computed with 5000 ultra-fast bootstraps. Here I will be attempting to partition the tree for simplicity and create a graphic that is suitable to publication.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r Initialize}
# Empties environment to prevent misinterpretation of data
rm(list=ls())

# Loads Library
library(phytools)
library(ape)
library(dplyr)
library(stringr)

# Sets working directory
#setwd("F:/BioDataLab/Galls/projects/Bootanomyia/trees/")

# Path to data
data <- "F:/BioDataLab/Galls/projects/Bootanomyia/data/trees/Bootanomyia_evoltree_iqtree-bnni-5kbs-testmodels_raw_v04.treefile"

# Reading of Data
tree_raw <- read.tree(data)

# Test reading of tree
plot(tree_raw)
```

```{r Rooting}
# The outgroup of this tree is "MF956344.1 Bortesia sp. 1 PJ-2017 voucher PJAN1109 cytochrome oxidase subunit I gene, partial cds; mitochondrial|revcompl"
outgroup <- "MF956344.1"
tree_rooted <- root(tree_raw, outgroup = outgroup)
plot(tree_rooted)
```

```{r Raw Tip, include=FALSE}
# Calling raw tip labels
raw_tips <- tree_rooted$tip.label

# Printing a reformatted version
cat(paste(sprintf('"%s"', raw_tips), collapse = ", "))
```

```{r Dropping Raw Tip}
# Setting tips to be dropped
tree_raw_prune <- c("MT375388.1_Megastigmus_lawsoni_voucher_USC_HE009_COI_cytochrome_c_oxidase_subunit_I__COX1__gene__partial_cds__mitochondrial|revcompl", "MN165919.1_Megastigmus_pretorianensis_isolate_HE070M1145_voucher_HE70_cytochrome_c_oxidase__COI__gene__partial_cds__mitochondrial|revcompl", "MN165923.1_-_Megastigmus_manonae._Eucalyptus_galls._Le_et_al._2020__reversed__A_new_nucleotide_sequence_entered_manually",  "MN165915.1_Megastigmus_zebrinus_isolate_HE049M1145_voucher_HE49_cytochrome_c_oxidase__COI__gene__partial_cds__mitochondrial|revcompl",  "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis__P47F_NW_R6_26_COW_B_1__reversed__2_reads_from_R6_26
_NW_COW_B_P47F_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Andricus_opertus_P47_AO_R5_25_RP_A__reversed__2_reads_from_25_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Andricus_opertus_P47_AO_R5_140__reversed__2_reads_from_R5_140_AO_P47_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R5_69_RP_A__reversed__2_reads_from_R5_69_NW_RP_A_P47F_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_saltatorius_P47_NSA_R5_107_RP_C__reversed__2_reads_from_107_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R5_105_RP_C__reversed__2_reads_from_R5_105_NW_RP_C_P47F_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R6_26_COW_B_2__reversed__2_reads_from_Plate_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R4_132_GHE_B__reversed__2_reads_from_R4_132_NW_GHE_B_P47F_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47M_NW_R4_138_GHE_C__reversed_", "Bootanomyia_dorsalis_from_Neuroterus_saltatorius_P47_NSA_R5_80_RP_B__reversed__2_reads_from_80_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_R1.26.NW.COW.B.P47F", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis__P47F_NW_R6_26_COW_B_1__reversed__2_reads_from_R6_26_NW_COW_B_P47F_assembled_using_Geneious", "Bootanomyia_dorsalis_from_Neuroterus_quercusbataus_2576-410-E16", "JQ417142.1_-_Megastigmus_stigmatizans._Msti411._Stone_et_al.__2012___reversed__A_new_nucleotide_sequence_entered_manually", "AY317240.1_Megastigmus_dorsalis._Fanjing_Mt.__Guizhou__China._Chen_et_al._2004__reversed_"
)

# Drop these tips from the tree
tree_2 <- drop.tip(tree_rooted, tree_raw_prune, trim.internal = T)

# Checking phylogeny
plot(tree_2)

```

```{r Tree 2 Tips}
# Calling first prune tip labels
pruned_tips <- tree_2$tip.label

# Printing a reformatted version
cat(paste(sprintf('"%s"', pruned_tips), collapse = ", "))
```

```{r Renaming Tree 2 Tips}
# New tip labels
tree_3_labels <- c("Bortesia_sp.(MF956344.1)",
"Bootanomyia_dorsalis_sp.1_from_Neuroterus_quercusbatatus_on_Quercus_bicolor_(Brooklyn,NY;2576-410-E16)",
"Bootanomyia_dorsalis_sp.1_from_Andricus_kollari_(Portugal;_JQ417138.1)",
"Bootanomyia_dorsalis_sp.1._from_Adricus_lucidus_(Hungary;_JQ417139.1)",
"Bootanomyia_dorsalis_sp.1_from_Neuroterus_quercusbatatus_on_Quercus_bicolor_(Brooklyn,NY;2576-410-E14)",
"Bootanomyia_dorsalis_sp.1_from_Acraspis_erinacei_on_Quercus_alba_(Westchester_Co.,NY;2835-2-1)",
"Bootanomyia_dorsalis_sp.1_from_Neuroterus_quercusbatatus_on_Quercus_bicolor_(Brooklyn,NY;2576-410-E13)",
"Bootanomyia_dorsalis_sp.1_from_Andricus_grossulariae_(Iran;JQ417140.1)",
"Bootanomyia_dorsalis_sp.2_from_Andricus_grossulariae_(Iran;JQ417137.1)",
"Bootanomyia_dorsalis_sp.2_from_Andricus_foecundatrix_(Hungary;JQ417136.1)",
"Bootanomyia_dorsalis_sp.2_from_Andricus_kollari_(Spain;JQ417135.1)",
"Bootanomyia_dorsalis_sp.2_from_Andricus_opertus_(Rocky_Point;P47-AO-R5-138-RP-A)",
"Bootanomyia_dorsalis_sp.2_from_Andricus_opertus_(Notch_Hill;P47-AO-R6-48-NAN-A)",
"Bootanomyia_dorsalis_sp.2_from_Neuroterus_saltatorius_(Mount_Douglas_Park;P47-NSA-R5-96-LMD-A)",
"Bootanomyia_dorsalis_sp.2_from_Neuroterus_washingtonensis_(Cowichan_Nature_Preserve;P47M-NW-R6-40-COW-B)",
"Bootanomyia_dorsalis_sp.2_from_Neuroterus_washingtonensis_(Notch_Hill;P47M-NW-R6-11-NAN-A)",
"Bootanomyia_dorsalis_sp.2_from_Neuroterus_washingtonensis_(Glacial_Heritage_Preserve;P47M-NW-R4-78-GHE-B)",
"Megastigmus_stigmatizans_(JQ417141.1)")

# Adding tips to the tree       
tree_3 <- tree_2
tree_3$tip.label <- tree_3_labels
plot(tree_3)
```

```{r Rotations, include = FALSE}
# Rotating nodes for aesthetics
tree_rotated1 <- rotate(tree_3, node = 19) #moving outgroup to the bottom
tree_rotated2 <- rotate(tree_rotated1, node = 28) #flipping sp2 clade so that genbank sequences are on top
tree_rotated3 <- rotate(tree_rotated2, node = 23) #flipping sp1 clade so that genbank sequences are grouped and on top
plot(tree_rotated3)
tree_4 <- tree_rotated3 #saving this with a more consistent object label

# Print newly rearranged labels for italics and formating
rotated_tips <- tree_4$tip.label

# Printing a reformatted version
cat(paste(sprintf('"%s"', rotated_tips), collapse = ", "))
```


```{r Isolating labels for specific font plotting}
# Our labels are quite complex. We have strings that need to be italicezed and some that do not. What makes this particuarly challenging is that these strings are not ordered. Meaning, It can change depending on the label and some labels will have italic plain italic plain setups instead of italic plain. There is frequent transition. Here I am attempting to order that mostly programatically but we will need to manually adjust some labels.
split_label <- function(label) {
  parts <- strsplit(label, "\\(")[[1]]
  main_part <- strsplit(parts[1], "_")[[1]]
  
  # Initialize components
  species <- paste(main_part[1:2], collapse = " ")
  intermediate1 <- ""
  host <- ""
  intermediate2 <- ""
  
  if (length(main_part) > 2) {
    if (main_part[3] %in% c("sp.1", "sp.2", "sp.")) {
      # Standard case
      intermediate1 <- paste(main_part[3:4], collapse = " ")
      remaining <- main_part[5:length(main_part)]
    } else {
      # Edge case
      remaining <- main_part[3:length(main_part)]
    }
    
    # Process remaining parts
    on_index <- which(remaining == "on")
    from_index <- which(remaining == "from")
    
    if (length(on_index) > 0) {
      host <- paste(remaining[1:(on_index[1]-1)], collapse = " ")
      intermediate2 <- paste(remaining[on_index[1]:length(remaining)], collapse = " ")
    } else if (length(from_index) > 0) {
      host <- paste(remaining[1:(from_index[1]-1)], collapse = " ")
      intermediate2 <- paste(remaining[from_index[1]:length(remaining)], collapse = " ")
    } else {
      host <- paste(remaining, collapse = " ")
    }
  }
  
  location <- if(length(parts) > 1) paste0("(", gsub("_", " ", parts[2])) else ""
  
  # Clean up components
  clean <- function(x) gsub("^ +| +$", "", gsub(" +", " ", x))
  
  list(species = clean(species),
       intermediate1 = clean(intermediate1),
       host = clean(host),
       intermediate2 = clean(intermediate2),
       location = clean(location))
}

# Apply the function to all labels
split_labels <- lapply(tree_3_labels, split_label)

# Extract components
species <- sapply(split_labels, `[[`, "species")
intermediate1 <- sapply(split_labels, `[[`, "intermediate1")
host <- sapply(split_labels, `[[`, "host")
intermediate2 <- sapply(split_labels, `[[`, "intermediate2")
location <- sapply(split_labels, `[[`, "location")

# Create mixed labels
mixed_labels <- mixedFontLabel(species, intermediate1, host, intermediate2, location,
                               sep = " ",
                               italic = c(1, 3))

# Manual label adjustment
mixed_labels_revised <- expression(
    italic("Bortesia")*" "*plain("sp.")*" "*plain("(MF956344.1)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Neuroterus quercusbatatus")*" "*plain("(Brooklyn, NY; 2576-410-E16)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Andricus kollari")*" "*plain("(Portugal; JQ417138.1)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Adricus lucidus")*" "*plain("(Hungary; JQ417139.1)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Neuroterus quercusbatatus")*" "*plain("(Brooklyn, NY; 2576-410-E14)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Acraspis erinacei")*" "*plain("(Westchester Co, NY; 2835-2-1)"),
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Neuroterus quercusbatatus")*" "*plain("(Brooklyn, NY; 2576-410-E13)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.1 from")*" "*italic("Andricus grossulariae")*" "*plain("(Iran; JQ417140.1)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Andricus grossulariae")*" "*plain("(Iran; JQ417137.1)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Andricus foecundatrix")*" "*plain("(Hungary; JQ417136.1)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Andricus kollari")*" "*plain("(Spain; JQ417135.1)"),
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Andricus opertus")*" "*plain("(Rocky Point; P47-AO-R5-138-RP-A)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Andricus opertus")*" "*plain("(Notch Hill; P47-AO-R6-48-NAN-A)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Neuroterus saltatorius")*" "*plain("(Mount Douglas Park; P47-NSA-R5-96-LMD-A)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Neuroterus washingtonensis")*" "*plain("(Cowichan Nature Preserve; P47M-NW-R6-40-COW-B)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Neuroterus washingtonensis")*" "*plain("(Notch Hill; P47M-NW-R6-11-NAN-A)"), 
    italic("Bootanomyia dorsalis")*" "*plain("sp.2 from")*" "*italic("Neuroterus washingtonensis")*" "*plain("(Glacial Heritage Preserve; P47M-NW-R4-78-GHE-B)"), 
    italic("Megastigmus stigmatizans")*" "*plain("(JQ417141.1)"))

# Assign the mixed labels to the tree
tree_4$tip.label <- mixed_labels_revised
```

```{r Bootstrap Values}
# Extract UFBoot values >90 from node labels
# Node labels are in format "UFBoot/SH-aLRT" (e.g., "0.95/85" or "0.99/100")
bootstrap_labels <- tree_4$node.label
high_support_labels <- c()

for(i in 1:length(bootstrap_labels)) {
  if(!is.na(bootstrap_labels[i]) && bootstrap_labels[i] != "") {
    # Split by "/" to get UFBoot value only
    parts <- strsplit(bootstrap_labels[i], "/")[[1]]
    if(length(parts) >= 1) {
      ufboot <- as.numeric(parts[1])
      
      # Convert UFBoot to percentage if it's in decimal format (0-1)
      if(ufboot <= 1) {
        ufboot <- ufboot * 100
      }
      
      # Show only UFBoot value if >90
      if(ufboot > 90) {
        high_support_labels[i] <- as.character(round(ufboot))
      } else {
        high_support_labels[i] <- ""
      }
    } else {
      high_support_labels[i] <- ""
    }
  } else {
    high_support_labels[i] <- ""
  }
}
```

```{r Final Plotting of the Phylogeny, fig.align='right', fig.height=15, fig.width=40}
# Saving the final figure
png(file ="F:/BioDataLab/Galls/projects/Bootanomyia/data/trees/Bootanomyia_evoltree_iqtree-5kbs-testmodels-bnni-alrt1000_final.png", 
    height = 5800, width = 11800, res = 300)

# Define colors for tip labels
tip_colors <- rep("black", length(tree_4$tip.label))
# Make tip labels 1-4 blue
tip_colors[2] <- "blue"
tip_colors[5:7] <- "blue"
# Make tip labels 7-12 red
tip_colors[12:17] <- "red"

suppressWarnings({
# Plot the tree
plot.phylo(tree_4, 
           cex = 2.66, 
           align.tip.label = F, 
           label.offset = 0.002, 
           use.edge.length = TRUE, 
           edge.width = 4.5,
           no.margin = T,
           tip.color = tip_colors)

# Add node labels (only showing bootstrap >90)
nodelabels(high_support_labels, adj = c(1.15, -0.5), cex = 1.75, frame = "none")

# Get the plot limits
plot_limits <- par("usr")
x_max <- plot_limits[2]
y_max <- plot_limits[4]
y_min <- plot_limits[3]

# For the blue clade
# Estimate y position for the center of the blue clade
blue_y_center <- y_min + (y_max - y_min) * (2.5/19)  # Assuming 19-20 tips total

# For the red clade
# Estimate y position for the center of the red clade
red_y_center <- y_min + (y_max - y_min) * (9.5/19)   # Assuming 19-20 tips total

# Add arrows and text for red clade
arrows(x0 = x_max * 0.14, y0 = red_y_center * 1.57, 
       x1 = x_max * 0.16, y1 = red_y_center * 1.515, 
       length = 0.15, angle = 30, code = 1, lwd = 4, col = "black")
text(x = x_max * 0.09, y = red_y_center * 1.6, 
     labels = "97.3–98.3%", 
     cex = 2, col = "black", font = 2, pos = 4)
text(x = x_max * 0.097, y = red_y_center * 1.56, 
     labels = "Similar", 
     cex = 2, col = "black", font = 2, pos = 4)

# Add arrows and text for blue clade
arrows(x0 = x_max * 0.18, y0 = red_y_center * 0.59, 
       x1 = x_max * 0.196, y1 = red_y_center * 0.645, 
       length = 0.15, angle = 30, code = 1, lwd = 4, col = "black")
text(x = x_max * 0.14, y = red_y_center * 0.555, 
     labels = "98.3-99.3%",
     cex = 2, col = "black", font = 2, pos = 4)
text(x = x_max * 0.147, y = red_y_center * 0.515, 
     labels = "Similar",
     cex = 2, col = "black", font = 2, pos = 4)

# Add a scale bar of 0.5 units in the top right
add.scale.bar(x = x_max * 0.0, y = y_max * 0.8, 
              lwd = 3, cex = 2.5, font = 2, length = 0.01, frame = "none")})

# Turning off png saving
dev.off()
```

```{r Refined Supplemental Raw Tree}
# Some of the tip labels printed incorrectly from the tree calculation. Here I am changing those and adding the bootstrap/alrt values to the raw tree
# Plot raw tree without changes
plot(tree_raw)

# Printing the tip labels
cat(paste(sprintf('"%s"', raw_tips), collapse = ", "))

# Dropping unnecessary outgroups, leaving just the Bortesia sp.
rawremove <- c("MN165915.1_Megastigmus_zebrinus_isolate_HE049M1145_voucher_HE49_cytochrome_c_oxidase__COI__gene__partial_cds__mitochondrial|revcompl", "MN165923.1_-_Megastigmus_manonae._Eucalyptus_galls._Le_et_al._2020__reversed__A_new_nucleotide_sequence_entered_manually", "MT375388.1_Megastigmus_lawsoni_voucher_USC_HE009_COI_cytochrome_c_oxidase_subunit_I__COX1__gene__partial_cds__mitochondrial|revcompl", "MN165919.1_Megastigmus_pretorianensis_isolate_HE070M1145_voucher_HE70_cytochrome_c_oxidase__COI__gene__partial_cds__mitochondrial|revcompl")
tree_raw_supp <- drop.tip(tree_raw, rawremove, trim.internal=T)

# Updating the nomenclature
cat(paste(sprintf('"%s"', tree_raw_supp$tip.label), collapse = ", "))
tree_raw_supp$tip.label <- c("MF956344.1_Bortesia_sp.", "2576-410-E16-Bootanomyia_dorsalis_from_Neuroterus_quercusbatatus_Quercus_bicolor_Brooklyn_NY", "Bootanomyia_dorsalis_from_Neuroterus_quercusbataus_2576-410-E16", "JQ417138.1_-_Bootanomyia_dorsalis_sp.1._Mdor339._from_Andricus_kollari_Portugal._Stone_et_al.__2012", "JQ417139.1_-_Bootanomyia_dorsalis_sp.1._Mdor478._from_Adricus_lucidus_Hungary._Stone_et_al.__2012", "Bootanomyia_dorsalis_from_Neuroterus_quercusbataus_2576-410-E14", "Bootanomyia_dorsalis_from_Acraspsi_erinacei_2835-2-1", "Bootanomyia_dorsalis_from_Neuroterus_quercusbataus_2576-410-E13l", "JQ417140.1_-_Bootanomyia_dorsalis_sp.1._Mdor724._from_Andricus_grossulariae_Iran._Stone_et_al.__2012", "JQ417137.1_-_Bootanomyia_dorsalis_sp.2._Mdor725._from_Andricus_grossulariae_Iran._Stone_et_al.__2012", "JQ417136.1_-_Bootanomyia_dorsalis_sp.2._Mdor450._from_Andricus_foecundatrix_Hungary._Stone_et_al.__2012", "JQ417135.1_-_Bootanomyia_dorsalis_sp.2._Mdor311._from_Andricus_kollari_Spain._Stone_et_al.__2012", "Bootanomyia_dorsalis_from_Andricus_opertus_P47_AO_R5_138_RP_A", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis__P47F_NW_R6_26_COW_B_1", "Bootanomyia_dorsalis_from_Andricus_opertus_P47_AO_R6_48_NAN_A", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47M_NW_R6_40_COW_B", "Bootanomyia_dorsalis_from_Andricus_opertus_P47_AO_R5_25_RP_A", "Bootanomyia_dorsalis_from_Andricus_opertus_P47_AO_R5_140", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R5_69_RP_A", "Bootanomyia_dorsalis_from_Neuroterus_saltatorius_P47_NSA_R5_107_RP_C", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R5_105_RP_C", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R6_26_COW_B_2", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47F_NW_R4_132_GHE_B", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47M_NW_R4_138_GHE_C", "Bootanomyia_dorsalis_from_Neuroterus_saltatorius_P47_NSA_R5_96_LMD_A", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47M_NW_R6_11_NAN_A", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_P47M_NW_R4_78_GHE_B", "Bootanomyia_dorsalis_from_Neuroterus_saltatorius_P47_NSA_R5_80_RP_B", "Bootanomyia_dorsalis_from_Neuroterus_washingtonensis_R1.26.NW.COW.B.P47F", "JQ417142.1_-_Megastigmus_stigmatizans._Msti411._Stone_et_al.__2012", "JQ417141.1_-_Megastigmus_stigmatizans._Msti089._Stone_et_al.__2012",  "AY317240.1_Megastigmus_dorsalis._Fanjing_Mt.__Guizhou__China._Chen_et_al._2004")

# I also want to show the significant bootstrap (>0.95) and alrt (>80) values on the nodes. Here I am separating those from the insignificant values
# Extract significant bootstrap/SH-aLRT values
bootstrap_labels <- tree_raw_supp$node.label
significant_labels <- c()

for(i in 1:length(bootstrap_labels)) {
  if(!is.na(bootstrap_labels[i]) && bootstrap_labels[i] != "") {
    # Split by "/" to get UFBoot and SH-aLRT values
    parts <- strsplit(bootstrap_labels[i], "/")[[1]]
    if(length(parts) >= 2) {
      ufboot <- as.numeric(parts[1])
      shalrt <- as.numeric(parts[2])
      
      # Convert UFBoot to percentage if it's in decimal format (0-1)
      if(ufboot <= 1) {
        ufboot <- ufboot * 100
      }
      
      # Show label if either UFBoot ≥80 OR SH-aLRT ≥80 (significant thresholds)
      if(ufboot >= 80 || shalrt >= 80) {
        significant_labels[i] <- bootstrap_labels[i]
      } else {
        significant_labels[i] <- ""
      }
    } else {
      significant_labels[i] <- ""
    }
  } else {
    significant_labels[i] <- ""
  }
}
```

```{r Plotting of Supp Tree, fig.align='right', fig.height=15, fig.width=40}
# Saving the file
png(file ="F:/BioDataLab/Galls/projects/Bootanomyia/data/trees/Bootanomyia_evoltree_iqtree-5kbs-testmodels-bnni-alrt1000_raw-supp_final.png", height = 5000, width = 10000, res = 300)

suppressWarnings({
# Final supplemental tree plotting
plot.phylo(tree_raw_supp, 
           cex = 1.5, 
           align.tip.label = F, 
           label.offset = 0.002, 
           use.edge.length = TRUE,
           no.margin = T)
nodelabels(significant_labels, frame = "none", cex = 1, adj = c(1.2, 1.2))
})

# Turn off png saving
dev.off()
```

