
install.packages("ape")  # Install ape if not already installed
install.packages("phytools")
library(ape)
library(phytools)

setwd("E:/Palaeontology/Projects/Group_works/group_PAPERS/Cong_Liu/Sunellidae/ASR/MLASR_LC")
# Load tree
tree_QUES <- read.tree("LC_unbi_TREE.nwk")
# Root the tree and Resolve polytomies
tree_QUES_rooted <- root(tree_QUES, outgroup = "Priapulus_caudatus")
tree_QUES_resolved <- multi2di(tree_QUES_rooted)

########################################################################    AT_QUES    ######################################################################
# Load the trait data from the .txt file
trait_data_AT_QUES <- read.table("LC_AT_QUES.txt", header = FALSE, stringsAsFactors = FALSE)

# View the data to ensure it loaded correctly
head(trait_data_AT_QUES)

# Convert to a named vector
trait_vector_AT_QUES <- setNames(trait_data_AT_QUES$V2, trait_data_AT_QUES$V1)

# Check the result
print(trait_vector_AT_QUES)

# Check for mismatched names
setdiff(names(trait_vector_AT_QUES), tree_QUES_resolved$tip.label)

# Subset the trait vector to include only species present in the tree
trait_vector_AT_QUES <- trait_vector_AT_QUES[tree_QUES_resolved$tip.label]

# Perform ASR based on three different models
result_ATL_QUES_ER <- ace(trait_vector_AT_QUES, tree_QUES_resolved, type = "discrete", model = "ER")
result_ATL_QUES_SYM <- ace(trait_vector_AT_QUES, tree_QUES_resolved, type = "discrete", model = "SYM")
result_ATL_QUES_ARD <- ace(trait_vector_AT_QUES, tree_QUES_resolved, type = "discrete", model = "ARD")

# AICc weights to calculate the model-averaged marginal probabilities at the nodes
AIC(result_ATL_QUES_ER)
AIC(result_ATL_QUES_SYM)
AIC(result_ATL_QUES_ARD)

# CHOOSE 'SYM' model due to the lowest AIC value

# Visualize results
# Position labels at max tree height

plot(tree_QUES_resolved, cex = 1)
nodelabels(pie = result_AT_QUES_ARD$lik.anc, piecol = c("orange", "black"), cex = 0.3)

# Examine reconstructed states
result_AT_2HEAD_SYM$lik.anc  # Likelihoods for states at each node
result_AT_2HEAD_SYM$rates    # Transition rates


########################################################################    ATL_QUES    ######################################################################
# Load the trait data from the .txt file
trait_data_ATL_QUES <- read.table("LC_ATL_QUES.txt", header = FALSE, stringsAsFactors = FALSE)

# View the data to ensure it loaded correctly
head(trait_data_ATL_QUES)

# Convert to a named vector
trait_vector_ATL_QUES <- setNames(trait_data_ATL_QUES$V2, trait_data_ATL_QUES$V1)

# Check the result
print(trait_vector_ATL_QUES)

# Check for mismatched names
setdiff(names(trait_vector_ATL_QUES), tree_QUES_resolved$tip.label)

# Subset the trait vector to include only species present in the tree
trait_vector_ATL_QUES <- trait_vector_ATL_QUES[tree_QUES_resolved$tip.label]

# Perform ASR based on three different models
result_ATL_QUES_ER <- ace(trait_vector_ATL_QUES, tree_QUES_resolved, type = "discrete", model = "ER")
result_ATL_QUES_SYM <- ace(trait_vector_ATL_QUES, tree_QUES_resolved, type = "discrete", model = "SYM")
result_ATL_QUES_ARD <- ace(trait_vector_ATL_QUES, tree_QUES_resolved, type = "discrete", model = "ARD")

# AICc weights to calculate the model-averaged marginal probabilities at the nodes
AIC(result_ATL_QUES_ER)
AIC(result_ATL_QUES_SYM)
AIC(result_ATL_QUES_ARD)

# CHOOSE 'ARD' model due to the lowest AIC value

# Visualize results
# Position labels at max tree height

plot(tree_QUES_resolved, cex = 1)
nodelabels(pie = result_ATL_QUES_ARD$lik.anc, piecol = c("orange", "black"), cex = 0.3)

# Examine reconstructed states
result_ATL_2HEAD_SYM$lik.anc  # Likelihoods for states at each node
result_ATL_2HEAD_SYM$rates    # Transition rates


