library(openxlsx)
library(ggplot2)
library(coxme)
library(glmmTMB)
library(multcomp)
library(DHARMa)
library(multcompView)
#library(ggpubr)
library(patchwork)
library(ggbeeswarm)
library(survminer)
#install.packages("viridis")  # Install
library("viridis")           # Load
library(RColorBrewer)
library(dplyr)
setwd("~/Documents/PhD Tesis db/") 
maternal_data <- read.xlsx("Pop_survey.xlsx",sheet = 2)
#Creating the ID for each queen colony


maternal_data$Exp_nest <- paste(maternal_data$Code, maternal_data$W, "_", maternal_data$Replicate, sep = "")

# The 16 maternal queens are
mat16 <- c("A30_10", "A30_17", "A30_2",  "A30_29", "A30_7",  "A30_9",  "C10_1",  "C10_13", "C10_30", "C10_9",  "E20_16", "E20_17", "E20_2",  "E20_27", "F10_10", "F10_6")
maternal_data2 <- maternal_data[maternal_data$Exp_nest %in% mat16, ]

maternal_data3 <- data.frame (Exp_nest = maternal_data2$Exp_nest, W = maternal_data2$W,  Eggs_total = maternal_data2$Eggs, Queen_Ptotal = maternal_data2$Pupae_Q, Worker_Ptotal = maternal_data2$Pupae_W, Males_Ptotal =  maternal_data2$Pupae_M, Treatment = as.numeric(maternal_data2$W))

maternal_summarized <- maternal_data3   %>% 
  group_by(Exp_nest) %>% 
  summarise(across(everything(),~ sum(.x, na.rm = T)))

# Maternal Lifespan ###
data_na <- subset(maternal_data2, Queen_alive==1)# get ride of lines of queens after been sacrified or died
# cols <- c("Code", "Replicate")
# data_na$queen.id <- do.call(paste, c(data_na[cols], sep="_"))
# data_na$queen.id <- as.factor(data_na$queen.id)
# f0_names_115q <- c("A_10", "A_17" ,"A_29", "A_7" , "C_1",  "C_13" ,"A_2" ,
#                    "A_9" , "C_9" , "E_27" ,"F_10" ,"C_30", "E_16", "E_17", "E_2",  "F_6" , "A_11"   ,  "A_14"  ,   "A_15"  ,   "A_16"  ,   "A_18"   ,  "A_19"   ,  "A_2"    ,  "A_21"  ,   "A_22"  ,   "A_23"   ,  "A_24"   ,  "A_25"  , "A_27"   ,  "A_27b" ,   "A_28"    , "A_29"   ,  "A_3"    ,  "A_4" ,     "A_5" ,     "A_6" ,     "A_6b"  ,   "A_7"  ,    "A_9"  ,    "B_1"   ,   "B_10" ,    "B_11"  ,   "B_14"   ,  "B_15"  ,   "B_16"  ,   "B_17"  ,   "B_2"   ,   "B_20"  ,   "B_23"  ,   "B_25"   ,  "B_27"   ,  "B_28"   ,  "B_32b" ,   "B_34"   ,  "B_36"  ,   "B_38"  ,   "B_7" ,     "B_9"  ,    "C_1"    ,  "C_11"  ,   "C_12"  ,   "C_15"  ,   "C_17"    , "C_18"  ,   "C_19"  ,   "C_19b"  ,  "C_2"   ,   "C_20"  ,   "C_21"   ,  "C_22"  ,   "C_25"   ,  "C_26"  ,   "C_28"  ,   "C_3"    ,  "C_4"    ,  "C_5"  ,    "C_6"  , "C_7"    ,  "C_8"   ,   "D_1"   ,   "D_11"   ,  "D_14"   ,  "D_16"   ,  "D_17" ,   "D_18"    , "D_2"  ,    "D_3"  ,    "D_7"   ,   "D_8"   ,   "D_9" ,     "E_1"  , "E_10"  ,   "E_13"  ,   "E_14",     "E_15"  ,   "E_19"  ,   "E_2"   ,   "E_26"   ,  "E_28"   ,  "E_29" ,    "E_3"  ,    "E_30"   ,  "E_4"    ,  "E_6"   ,   "E_8"  , "F_10" ,    "F_14"    , "F_15"   ,  "F_20"   ,  "F_3"  ,    "F_4"   ,   "F_5"  ,    "F_7"   ,   "F_8"  ,    "F_9" )
# 
# colonies <- data_na[(data_na$queen.id %in% f0_names_115q), ]

max_week <- aggregate(data_na$Week, list(data_na$Exp_nest), max) #but inprefer to have not only the week data but the whole row
colnames(max_week) <- c("Exp_nest", "Week")
maternal_summarized <- merge(maternal_summarized, max_week,
      by= "Exp_nest")
#write.xlsx(maternal_summarized, "Selection_lines_records_241011.xlsx")

setwd("~/Documents/PhD_Tesis/04_selected_lines/")


f1_data <- read.xlsx("Selection_lines_records_241011.xlsx",sheet = 1)
f0_data <- read.xlsx("Selection_lines_records_241011.xlsx",sheet = 2)



batch_data <- read.xlsx("Selection_lines_records_241011.xlsx",sheet = 3)

f1_0_data <- merge(f0_data, f1_data,
      by = "Exp_nest")


f1_0_data$casteR <- f1_0_data$QueenP_sel/(f1_0_data$QueenP_sel + f1_0_data$Worker_sel)
f1_0_data$casteRf0 <- f1_0_data$Queen_Ptotal /(f1_0_data$Worker_Ptotal + f1_0_data$Queen_Ptotal)
summary(f1_0_data) 
f1_0_data$Recorded_dead <- as.factor(f1_0_data$Recorded_dead)
f1_0_data$Treatment <- as.factor(f1_0_data$Treatment)
f1_0_data$Productivity <- factor(f1_0_data$Productivity, levels = c("Low", "High"))
#####  Figure 1 #####

setwd("~/Dropbox/2020_Review_Social_aging_Jaimes_etal/Data")

correlation <- read.xlsx("Egg_estimate_productivity.xlsx",sheet = 1)

pvalplot <- ggplot()+
  geom_smooth(data = correlation, aes(x=Week, y=Pearson_r, color =  "#5050F3"), span = 0.8)+
  geom_point(data = correlation, aes(x=Week, y=Pearson_r,  color ="#5050F3"))+
  geom_smooth(data = correlation, aes(x= Week, y = Pvalue,  color= "black"), span = 0.8, se = FALSE)+
  geom_point(data = correlation, aes(x=Week, y=Pvalue),  color = "black")+
  scale_y_continuous(sec.axis = sec_axis(~., name = "P-value"))+
  xlab("Weeks censored")+
  ylab("Pearson correlation")+
  scale_color_manual(values = c("#5050F3", "black"),
                     labels = c( "Correlation","P-value"))+
  theme_minimal()+
  theme(legend.title = element_blank(),
        legend.key = element_rect(colour = "white"),
        legend.position = "bottom",
        legend.text=element_text(size=12),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12))

f1_0_data_mat <- f1_0_data[!duplicated(f1_0_data$Exp_nest),]
colnames(f1_0_data_mat)
mat_eggs_product <- lm(Eggs_total~ Productivity, data=f1_0_data_mat)
summary(mat_eggs_product)
# Call:
#   lm(formula = Eggs_total ~ Productivity, data = f1_0_data_mat)
# 
# Coefficients:
#   (Intercept)  ProductivityHigh  
# 414.9             344.7  
# 
# > mat_eggs_product <- lm(Eggs_total~ Productivity, data=f1_0_data_mat)
# > summary(mat_eggs_product)
# 
# Call:
#   lm(formula = Eggs_total ~ Productivity, data = f1_0_data_mat)
# 
# Residuals:
#   Min      1Q  Median      3Q     Max 
# -212.57  -84.31  -42.73   76.43  295.11 
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)        414.89      54.49   7.613 2.43e-06 ***
#   ProductivityHigh   344.68      82.39   4.184 0.000919 ***
  

egg15_plot <- ggplot(data = f1_0_data_mat, aes(x = Productivity , y = Eggs_15) )+
  geom_violin()+
  geom_boxplot(width=.2)+
  geom_quasirandom(aes(col = Productivity))+
  scale_color_manual(values = c( "#5050F3", "#FF6666"), labels = c("High", "Low" ))+
  #  labs(title = "(a)") +
  theme_classic(base_size = 12)+
  xlab("Productivity in maternal colonies")+
  ylab("Egg production")+
  theme(legend.position = "none",
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))
f0_data <- subset(f1_0_data, Maternal != 1) #16 f0 and 99 col
f0_data$Week <- as.numeric(f0_data$Week)
f0_data$Death <- as.factor(f0_data$Death)

Fig1C <- ggplot(data = batch_data, aes(x = Week , y = Eggs_sel) )+
  geom_point(aes(col = Productivity))+
  scale_color_manual(values =  c("#FF6666", "#5050F3", "black"), labels = c("High", "Low" ))+
  geom_smooth(method = lm, colour = "black")+
  #  labs(title = "(b)") +
  xlab("Lifespan (weeks)")+
  ylab("Total eggs")+
  theme_classic()+
  theme(legend.position = "bottom",
        legend.title = element_text(size = 12),
        legend.text = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        axis.title = element_text(size = 12))
(pvalplot + egg15_plot ) / Fig1C + plot_annotation(tag_levels = 'A')


###### Test Correlation ####
f1_0_numbers <- data.frame (Exp_nest = f1_0_data$Exp_nest, Week = f1_0_data$Week, Eggs_sel = f1_0_data$Eggs_sel,QueenP_sel = f1_0_data$QueenP_sel, Worker_sel= f1_0_data$Worker_sel, casteR =  f1_0_data$casteR, Eggs_total = f1_0_data$Eggs_total, Queen_Ptotal = f1_0_data$Queen_Ptotal, Worker_Ptotal = f1_0_data$Worker_Ptotal, Lifespan_weeks = f1_0_data$Lifespan, Treatment = as.numeric(f1_0_data$Treatment),Mat_castR = f1_0_data$casteRf0)



lapply(f1_0_numbers[,-c(1)], shapiro.test)
# $Worker_Ptotal
# 
# Shapiro-Wilk normality test
# 
# data:  X[[i]]
# W = 0.84405, p-value = 3.904e-06
# $Eggs_total
# 
# Shapiro-Wilk normality test
# 
# data:  X[[i]]
# W = 0.89453, p-value = 0.0001414
# 
# 
# $Queen_Ptotal
# 
# Shapiro-Wilk normality test
# 
# data:  X[[i]]
# W = 0.91535, p-value = 0.0007927



fig2_a <- ggplot(data = f1_0_numbers, aes(x = Eggs_total , y = Eggs_sel) )+
  geom_smooth (method = lm, colour = "black" )+
  geom_point()+
  #  ylim(0,350)+
  #  scale_color_manual(values = c( "orange","darkorchid1"), labels = c("High", "Low" ))+
  xlab("Maternal eggs")+
  ylab("Daughter eggs")+
  theme_classic()+
  theme(legend.position = "bottom",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))

fig2_b <- ggplot(data = f1_0_numbers, aes(x = Worker_Ptotal , y = Worker_sel) )+
  geom_smooth(method = lm, colour = "darkorange" , fill ="tan2")+
  geom_point(color = "darkorange")+
  #  ylim(0,250)+
  #  scale_color_manual(values = c( "orange","darkorchid1"), labels = c("High", "Low" ))+
  xlab("Maternal workers")+
  ylab("Daughter workers")+
  theme_classic()+
  theme(legend.position = "bottom",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))

fig2_c <- ggplot(data = f1_0_numbers, aes(x = Queen_Ptotal , y = QueenP_sel) )+
  geom_smooth(method = lm, colour = "deeppink3" , fill ="deeppink")+
  geom_point( colour = "deeppink3" )+
  #  scale_color_manual(values = c( "orange","darkorchid1"), labels = c("High", "Low" ))+
  xlab("Maternal queen pupae")+
  ylab("Daughter queen pupae")+
  theme_classic()+
  theme(legend.position = "bottom",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))
fig2_d <- ggplot(data = f1_0_numbers, aes(x = Lifespan_weeks , y = Week) )+
  geom_smooth(method = lm, colour = "#4444EC" , fill ="#9595F6")+
  geom_point( colour ="#4444EC" )+
  #  scale_color_manual(values = c( "orange","darkorchid1"), labels = c("High", "Low" ))+
  xlab("Maternal lifespan")+
  ylab("Daughter lifespan")+
  theme_classic()+
  theme(legend.position = "bottom",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))


fig2 <- fig2_a + fig2_b + fig2_c  + fig2_d + plot_annotation(tag_levels = 'A')

#### Eggs ####
model_eggs = glmmTMB(Eggs_sel ~ Eggs_total +  (Treatment) + (1|Exp_nest) ,   zi = ~1, family = nbinom2, data = f1_0_numbers)
res <- simulateResiduals(model_eggs, plot = T)

summary(model_eggs)
#  Family: nbinom2  ( log )
# Formula:          Eggs_sel ~ Eggs_total + (Treatment) + (1 | Exp_nest)
# Zero inflation:            ~1
# Data: f1_0_numbers
# 
# AIC      BIC   logLik deviance df.resid 
# 604.6    616.7   -296.3    592.6       50 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance  Std.Dev.
# Exp_nest (Intercept) 4.122e-09 6.42e-05
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion parameter for nbinom2 family (): 1.48 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept) 4.1759238  0.3565185  11.713   <2e-16 ***
#   Eggs_total  0.0002150  0.0004281   0.502    0.616    
# Treatment   0.0156686  0.1378498   0.114    0.910    
# ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Zero-inflation model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)   -4.179      1.207  -3.462 0.000537 ***
#   ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

#### Workers ### 
model_workers = glmmTMB(Worker_sel ~ Worker_Ptotal +  (Treatment) + (1|Exp_nest)  ,  family = nbinom2, data = f1_0_numbers)
res <- simulateResiduals(model_workers, plot = T)
summary(model_workers)
# Family: nbinom2  ( log )
# Formula:          
#   Worker_sel ~ Worker_Ptotal + (Treatment) + (1 | Exp_nest)
# Data: f1_0_numbers
# 
# AIC      BIC   logLik deviance df.resid 
# 582.3    592.4   -286.2    572.3       51 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance Std.Dev.
# Exp_nest (Intercept) 0.1362   0.369   
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion parameter for nbinom2 family (): 1.32 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)   3.8185615  0.4954620   7.707 1.29e-14 ***
#   Worker_Ptotal 0.0002132  0.0003969   0.537    0.591    
# Treatment     0.0362845  0.1877243   0.193    0.847    


#### Queen pupae ### 
model_qp= glmmTMB(QueenP_sel ~ Queen_Ptotal +  (Treatment) + (1|Exp_nest)  , family = nbinom2, data = f1_0_numbers)

res <- simulateResiduals(model_qp, plot = T)
summary(model_qp)
#  Family: nbinom2  ( log )
# Formula:          QueenP_sel ~ Queen_Ptotal + (Treatment) + (1 | Exp_nest)
# Data: f1_0_numbers
# 
# AIC      BIC   logLik deviance df.resid 
# 550.2    560.4   -270.1    540.2       51 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance Std.Dev.
# Exp_nest (Intercept) 0.3743   0.6118  
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion parameter for nbinom2 family (): 0.708 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)  3.451803   0.645247    5.35 8.82e-08 ***
#   Queen_Ptotal 0.001416   0.002722    0.52    0.603    
# Treatment    0.030184   0.301338    0.10    0.920  

#### Caste Ratio ### 
model_cr= glmmTMB(casteR ~ Mat_castR +  (Treatment) + (1|Exp_nest)  ,  data = f1_0_numbers)

res <- simulateResiduals(model_cr, plot = T)
summary(model_cr)
# Family: gaussian  ( identity )
# Formula:          casteR ~ Mat_castR + (Treatment) + (1 | Exp_nest)
# Data: f1_0_numbers
# 
# AIC      BIC   logLik deviance df.resid 
# -16.2     -6.1     13.1    -26.2       51 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance  Std.Dev. 
# Exp_nest (Intercept) 3.520e-12 1.876e-06
# Residual             3.665e-02 1.915e-01
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion estimate for gaussian family (sigma^2): 0.0367 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)  0.39156    0.07200   5.439 5.37e-08 ***
#   Mat_castR   -0.08829    0.27045  -0.326    0.744    
# Treatment   -0.00722    0.03290  -0.219    0.826 

###### Maternal Lifespan and Lansing effect ####
shapiro.test(f0_data$Week) # does the daugther lifespan exhibit normality?
# 
# Shapiro-Wilk normality test
# 
# data:  f0_data$Week
# W = 0.91894, p-value = 0.001087

# Survival F1
setwd("~/Documents/PhD_Tesis/04_selected_lines/")
f1_0_data <- read.xlsx("Selection_lines_records_complete.xlsx",sheet = 3)
f1_0_data$Week <- as.numeric(f1_0_data$Week)
#The proportion of total reproduction accomplished until the moment of laying the F1 egg was  _> percentage of reprotuction left. Produce a colum with the proportion of cummulative egss produced at each time point
 colonies2 <- data_na %>% 
                               group_by(Exp_nest) %>%
                               mutate(Cum = cumsum(Eggs)/sum(Eggs)) %>%
                               right_join(colonies) %>%
                               select(names(colonies), everything())
                             
 colonies3 <- as.data.frame(cbind(queen.id =as.character(colonies2$Exp_nest), Cum=as.numeric(colonies2$Cum), Week=colonies2$Week))
f1_0_data_eggprop <-merge(f1_0_data, colonies3,
                          by.x=c("Exp_nest", "Age_f0_egg"), by.y=c("queen.id","Week"))
f1_0_data_eggprop$Cum <- as.numeric(f1_0_data_eggprop$Cum)



# residuals only work for coxph that doesnt handle random effects
coxph_model <- coxph(Surv(Week, Death) ~ Lifespan_weeks+ Cum + Productivity,  data = f1_0_data_eggprop)
resid_mart <- residuals(coxph_model, type = "martingale")
plot(resid_mart, ylab = "Martingale Residuals", main = "Martingale Residuals Plot")
abline(h = c(-2, 2), col = "red", lty = 2) # Highlight potential outliers
sort(resid_mart)  # Orders residuals from smallest to largest

outliers <- order(abs(resid_mart), decreasing = TRUE)[1]  # Get indices of extreme values
f1_0_data_eggprop[outliers, ]  # Show the corresponding rows in the dataset


survival_prod <- coxme(Surv(Week, Death) ~   Lifespan_weeks+ Cum + Productivity  + (1| Exp_nest) + (1|Recorded_dead)  , data = f1_0_data_eggprop[-c(18),])

summary(survival_prod)
# Mixed effects coxme model
# Formula: Surv(Week, Death) ~ Lifespan_weeks + Cum + Productivity + (1 |      Exp_nest) + (1 | Recorded_dead) 
# Data: f1_0_data_eggprop[-c(18), ] 
# 
# events, n = 54, 55
# 
# Random effects:
#   group  variable          sd     variance
# 1      Exp_nest Intercept 0.016880455 2.849498e-04
# 2 Recorded_dead Intercept 0.004214828 1.776477e-05
# Chisq   df      p   AIC    BIC
# Integrated loglik  1.42 5.00 0.9224 -8.58 -18.53
# Penalized loglik  1.44 3.01 0.6972 -4.58 -10.57
# 
# Fixed effects:
#                  coef exp(coef)  se(coef)     z     p
# Lifespan_weeks  -0.005748  0.994268  0.023671 -0.24 0.808
# Cum              1.000021  2.718340  1.027988  0.97 0.331
# ProductivityLow  0.087027  1.090926  0.307323  0.28 0.777

# Maternal age at oviposition and daughter lifespan were not correlated 
survival_prod <- coxme(Surv(Week, Death) ~   Lifespan_weeks+ Age_f0_egg + Productivity  + (1| Exp_nest) + (1|Recorded_dead)  , data = f1_0_data_eggprop[-c(18),])
summary(survival_prod)
# Mixed effects coxme model
# Formula: Surv(Week, Death) ~ Lifespan_weeks + Age_f0_egg + Productivity +      (1 | Exp_nest) + (1 | Recorded_dead) 
# Data: f1_0_data_eggprop[-c(18), ] 
# 
# events, n = 54, 55
# 
# Random effects:
#   group  variable          sd     variance
# 1      Exp_nest Intercept 0.011349702 1.288157e-04
# 2 Recorded_dead Intercept 0.003488909 1.217249e-05
# Chisq df      p   AIC    BIC
# Integrated loglik  2.28  5 0.8099 -7.72 -17.67
# Penalized loglik  2.29  3 0.5160 -3.72  -9.70
# 
# Fixed effects:
#   coef exp(coef)  se(coef)     z     p
# Lifespan_weeks  -0.029478  0.970952  0.024269 -1.21 0.225
# Age_f0_egg       0.055958  1.057553  0.040953  1.37 0.172
# ProductivityLow  0.008159  1.008192  0.309060  0.03 0.979


##### Lansing sensu lato #####

mod_lansing2 <- glmmTMB(log(Eggs_sel+1) ~  Cum + (1|Exp_nest), data = f1_0_data_eggprop)
res <- simulateResiduals(mod_lansing2 , plot = T)
summary(mod_lansing2)
#  Family: gaussian  ( identity )
# Formula:          log(Eggs_sel + 1) ~ Cum + (1 | Exp_nest)
# Data: f1_0_data_eggprop
# 
# AIC      BIC   logLik deviance df.resid 
# 173.1    181.2    -82.5    165.1       52 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance Std.Dev.
# Exp_nest (Intercept) 0.121    0.3479  
# Residual             1.013    1.0062  
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion estimate for gaussian family (sigma^2): 1.01 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)   4.0494     0.7676   5.275 1.33e-07 ***
#   Cum          -0.1759     0.9593  -0.183    0.854

mod_lansing3 <- glmmTMB(log(Worker_sel+1) ~  Cum + (1|Exp_nest), data = f1_0_data_eggprop)
res <- simulateResiduals(mod_lansing3 , plot = T)
summary(mod_lansing3)
#   Family: gaussian  ( identity )
# Formula:          log(Worker_sel + 1) ~ Cum + (1 | Exp_nest)
# Data: f1_0_data_eggprop
# 
# AIC      BIC   logLik deviance df.resid 
# 176.3    184.4    -84.2    168.3       52 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance Std.Dev.
# Exp_nest (Intercept) 0.1527   0.3908  
# Residual             1.0558   1.0275  
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion estimate for gaussian family (sigma^2): 1.06 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)   4.1081     0.8139   5.047 4.48e-07 ***
#   Cum          -0.5858     1.0144  -0.578    0.564 

ggplot (data = f1_0_data_eggprop, aes(x=Age_f0, y=Cum))+
  geom_point()

mod_lansing4 <- glmmTMB(log(QueenP_sel+1) ~ Cum + (1|Exp_nest), data = f1_0_data_eggprop)
res <- simulateResiduals(mod_lansing4 , plot = T)
summary(mod_lansing4)
#  Family: gaussian  ( identity )
# Formula:          log(QueenP_sel + 1) ~ Cum + (1 | Exp_nest)
# Data: f1_0_data_eggprop
# 
# AIC      BIC   logLik deviance df.resid 
# 205.4    213.5    -98.7    197.4       52 
# 
# Random effects:
#   
#   Conditional model:
#   Groups   Name        Variance Std.Dev.
# Exp_nest (Intercept) 0.2109   0.4592  
# Residual             1.8077   1.3445  
# Number of obs: 56, groups:  Exp_nest, 16
# 
# Dispersion estimate for gaussian family (sigma^2): 1.81 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)    3.829      1.038   3.688 0.000226 ***
#   Cum           -1.016      1.296  -0.784 0.432820 

#### Visualization Lansing per category - senescent ####

f1_0_data$time_to_die <- f1_0_data$Lifespan - f1_0_data$Age_f0_egg
f1_0_data_nosac <- subset(f1_0_data, Recorded_dead == 1)

ggplot(f1_0_data, aes(x=Age_f0_egg)) + 
  geom_histogram(aes(y = ..density..),color="black", fill="white", bins = 14)+
  geom_density(color = "#000000", fill = "#69b3a2", alpha = 0.6)+
  geom_vline(aes(xintercept=mean(Age_f0_egg)),
             color="black", linetype="dashed", size=1)+
  theme_minimal()+
  xlab("Maternal age")
summary(f1_0_data$Age_f0_egg)


aggregate(f1_0_data_eggprop$Cum, by = list(Age_f0cat = f1_0_data_eggprop$Age_f0cat), fivenum)
#.  Age_f0cat       x.1       x.2       x.3       x.4       x.5
# 1    before 0.4789916 0.5741578 0.6476965 0.7314050 0.8995816
# 2      past 0.5913313 0.8149557 0.8970406 0.9680556 0.9972900



library(survival)
f1_0_data_eggprop$Recorded_dead <- as.numeric(f1_0_data_eggprop$Recorded_dead)
model_reprod_output <- glmmTMB(Cum ~ Age_f0cat + (1|Recorded_dead),data=f1_0_data_eggprop)
res <- simulateResiduals(model_reprod_output, plot =T)
summary(model_reprod_output)
# Family: gaussian  ( identity )
# Formula:          Cum ~ Age_f0cat + (1 | Recorded_dead)
# Data: f1_0_data_eggprop
# 
# AIC      BIC   logLik deviance df.resid 
# -76.8    -68.7     42.4    -84.8       52 
# 
# Random effects:
#   
#   Conditional model:
#   Groups        Name        Variance Std.Dev.
# Recorded_dead (Intercept) 0.000315 0.01775 
# Residual                  0.012631 0.11239 
# Number of obs: 56, groups:  Recorded_dead, 2
# 
# Dispersion estimate for gaussian family (sigma^2): 0.0126 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)    0.65607    0.02615   25.09  < 2e-16 ***
#   Age_f0catpast  0.21391    0.03132    6.83 8.48e-12 ***



fig3_1 <- ggplot(f1_0_data_eggprop, aes(x = Age_f0cat, y= Cum))+
  geom_boxplot()+
  geom_point()+
  theme_minimal()+
  ylab("Proportion of reproductive output")+
  xlab("Maternal age")+
  theme(axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))

survplot_f3 <-  ggsurvplot(
  fit = survfit(Surv(Week, Recorded_dead) ~Age_f0cat , data = f1_0_data_eggprop),
  xlab = "Weeks", 
  ylab = "Overall survival probability",
  palette = c("#5050F3", "#FF6666"),
  conf.int = TRUE,
  legend.title = "Maternal age",
  font.legend = 12,
  font.y =12,
  font.tickslab = 12,
  font.title = 12,
  legend.labs = c("Not senescent", "Senescent"))


fig3c <- ggplot(f1_0_data_eggprop, aes(x = Age_f0cat, y= QueenP_sel, fill =Age_f0cat))+
  geom_boxplot()+
  geom_point()+
  scale_fill_manual(values= c("#9595F6", "#FF9494"))+
  theme_classic()+
  ylab("Daughter queen pupae")+
  xlab("Maternal age")+
  theme(legend.position = "bottom",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))
fig3_1 + survplot_f3$plot +fig3c +plot_annotation(tag_levels = 'A') 


######## Testing maternal lineages ####
# For this any colony with less than 3 replicates is removed
setwd("~/Documents/PhD_Tesis/04_selected_lines/")
f1_0_data <- read.xlsx("Selection_lines_records_complete.xlsx",sheet = 2)
#f1_0_data$sEggs_total <- as.numeric(f1_0_data$sEggs_total)
#f1_0_data$sLifespan_weeks <- as.numeric(f1_0_data$sLifespan_weeks)
f1_0_data$casteR <- f1_0_data$QueenP_sel/(f1_0_data$QueenP_sel + f1_0_data$Worker_sel)
f1_0_data$casteRf0 <- f1_0_data$Worker_Ptotal /(f1_0_data$Worker_Ptotal + f1_0_data$Queen_Ptotal)
f1_0_data$Maternal <- as.factor(f1_0_data$Maternal )
summary(f1_0_data) 
#f1_0_data$Comments_f0 <- as.factor(f1_0_data$Comments_f0 )
f1_0_data$Comments <- as.factor(f1_0_data$Comments)
f1_0_data$Recorded_dead <- as.factor(f1_0_data$Recorded_dead)
f1_0_data <- f1_0_data[f1_0_data$Exp_nest %in% c("C10_30", "A30_7", "A30_10", "A30_29","C10_13", "C10_1", "E20_2", "E20_17", "F10_6","A30_17", "E20_16"),]
f1_0_data$Exp_nest <- as.factor(f1_0_data$Exp_nest)

f1_0_data$Exp_nest <- factor(f1_0_data$Exp_nest, levels = c("C10_30", "A30_7", "A30_10", "A30_29","C10_13", "C10_1", "E20_2", "E20_17", "F10_6","A30_17", "E20_16")) #by qp
f1_0_data$Exp_nest <- factor(f1_0_data$Exp_nest, levels = c("C10_13", "A30_29", "A30_10", "C10_1","E20_17", "A30_17", "C10_30", "A30_7","E20_2", "F10_6", "E20_16")) #by qp?
f1_0_data$Productivity <- factor(f1_0_data$Productivity, levels = c("Low", "High"))

f1_0_data_nomat <- f1_0_data[f1_0_data$Maternal == 0,]

f1_0_data_nomat$Productivity <- factor(f1_0_data_nomat$Productivity, levels = c("Low", "High"))

Lines_eggs_model = glmmTMB(Eggs_sel ~   Exp_nest + (1 | Recorded_dead),  data = f1_0_data_nomat , family = "nbinom2", na.action = "na.fail")
res <- simulateResiduals(Lines_eggs_model , plot = T)
car::Anova(Lines_eggs_model)
# Analysis of Deviance Table (Type II Wald chisquare tests)
# 
# Response: Eggs_sel
# Chisq Df Pr(>Chisq)  
# Exp_nest 22.504 10    0.01273 *
#   ---
  
summary(Lines_eggs_model)
# Family: nbinom2  ( log )
# Formula:          Eggs_sel ~ Exp_nest + (1 | Recorded_dead)
# Data: f1_0_data_nomat
# 
# AIC      BIC   logLik deviance df.resid 
# 523.3    547.6   -248.6    497.3       35 
# 
# Random effects:
#   
#   Conditional model:
#   Groups        Name        Variance  Std.Dev.
# Recorded_dead (Intercept) 9.545e-10 3.09e-05
# Number of obs: 48, groups:  Recorded_dead, 2
# 
# Dispersion parameter for nbinom2 family (): 1.56 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)      3.7917     0.4708   8.054 8.02e-16 ***
#   Exp_nestA30_29   0.1253     0.6223   0.201   0.8405    
# Exp_nestA30_10   0.5416     0.5939   0.912   0.3618    
# Exp_nestC10_1    0.7830     0.5934   1.319   0.1871    
# Exp_nestE20_17   0.5390     0.5939   0.908   0.3641    
# Exp_nestA30_17   0.8293     0.5934   1.398   0.1622    
# Exp_nestC10_30  -1.1891     0.6331  -1.878   0.0603 .  
# Exp_nestA30_7    0.5624     0.5939   0.947   0.3436    
# Exp_nestE20_2    0.3514     0.5756   0.610   0.5416    
# Exp_nestF10_6    0.8397     0.6626   1.267   0.2050    
# Exp_nestE20_16   1.2560     0.6617   1.898   0.0577 .  
summary(glht(Lines_eggs_model,linfct=mcp( Exp_nest = "Tukey")))

# Simultaneous Tests for General Linear Hypotheses
# 
# Multiple Comparisons of Means: Tukey Contrasts
# 
# 
# Fit: glmmTMB(formula = Eggs_sel ~ Exp_nest + (1 | Recorded_dead), 
#              data = f1_0_data_nomat, family = "nbinom2", na.action = "na.fail", 
#              ziformula = ~0, dispformula = ~1)
# 
# Linear Hypotheses:
#                        Estimate Std. Error z value Pr(>|z|)   
# A30_29 - C10_13 == 0  0.125270   0.622266   0.201   1.0000   
# A30_10 - C10_13 == 0  0.541620   0.593924   0.912   0.9980   
# C10_1 - C10_13 == 0   0.782970   0.593450   1.319   0.9650   
# E20_17 - C10_13 == 0  0.538993   0.593930   0.908   0.9981   
# A30_17 - C10_13 == 0  0.829303   0.593371   1.398   0.9485   
# C10_30 - C10_13 == 0 -1.189051   0.633055  -1.878   0.7280   
# A30_7 - C10_13 == 0   0.562401   0.593878   0.947   0.9973   
# E20_2 - C10_13 == 0   0.351394   0.575633   0.610   0.9999   
# F10_6 - C10_13 == 0   0.839747   0.662584   1.267   0.9736   
# E20_16 - C10_13 == 0  1.255976   0.661749   1.898   0.7155   
# A30_10 - A30_29 == 0  0.416350   0.544674   0.764   0.9996   
# C10_1 - A30_29 == 0   0.657700   0.544157   1.209   0.9813   
# E20_17 - A30_29 == 0  0.413722   0.544680   0.760   0.9996   
# A30_17 - A30_29 == 0  0.704032   0.544071   1.294   0.9694   
# C10_30 - A30_29 == 0 -1.314322   0.587096  -2.239   0.4734   
# A30_7 - A30_29 == 0   0.437131   0.544624   0.803   0.9993   
# E20_2 - A30_29 == 0   0.226124   0.524669   0.431   1.0000   
# F10_6 - A30_29 == 0   0.714476   0.618823   1.155   0.9867   
# E20_16 - A30_29 == 0  1.130706   0.617929   1.830   0.7593   
# C10_1 - A30_10 == 0   0.241350   0.511505   0.472   1.0000   
# E20_17 - A30_10 == 0 -0.002628   0.512062  -0.005   1.0000   
# A30_17 - A30_10 == 0  0.287682   0.511414   0.563   1.0000   
# C10_30 - A30_10 == 0 -1.730672   0.556968  -3.107   0.0679 . 
# A30_7 - A30_10 == 0   0.020781   0.512002   0.041   1.0000   
# E20_2 - A30_10 == 0  -0.190226   0.490722  -0.388   1.0000   
# F10_6 - A30_10 == 0   0.298126   0.590316   0.505   1.0000   
# E20_16 - A30_10 == 0  0.714356   0.589379   1.212   0.9809   
# E20_17 - C10_1 == 0  -0.243978   0.511512  -0.477   1.0000   
# A30_17 - C10_1 == 0   0.046332   0.510863   0.091   1.0000   
# C10_30 - C10_1 == 0  -1.972022   0.556462  -3.544   0.0169 * 
#   A30_7 - C10_1 == 0   -0.220569   0.511452  -0.431   1.0000   
# E20_2 - C10_1 == 0   -0.431576   0.490149  -0.881   0.9985   
# F10_6 - C10_1 == 0    0.056776   0.589839   0.096   1.0000   
# E20_16 - C10_1 == 0   0.473006   0.588901   0.803   0.9993   
# A30_17 - E20_17 == 0  0.290310   0.511420   0.568   1.0000   
# C10_30 - E20_17 == 0 -1.728044   0.556974  -3.103   0.0696 . 
# A30_7 - E20_17 == 0   0.023408   0.512009   0.046   1.0000   
# E20_2 - E20_17 == 0  -0.187598   0.490729  -0.382   1.0000   
# F10_6 - E20_17 == 0   0.300754   0.590321   0.509   1.0000   
# E20_16 - E20_17 == 0  0.716984   0.589384   1.216   0.9804   
# C10_30 - A30_17 == 0 -2.018354   0.556378  -3.628   0.0125 * 
#   A30_7 - A30_17 == 0  -0.266902   0.511361  -0.522   1.0000   
# E20_2 - A30_17 == 0  -0.477909   0.490053  -0.975   0.9965   
# F10_6 - A30_17 == 0   0.010444   0.589760   0.018   1.0000   
# E20_16 - A30_17 == 0  0.426674   0.588822   0.725   0.9997   
# A30_7 - C10_30 == 0   1.751452   0.556919   3.145   0.0605 . 
# E20_2 - C10_30 == 0   1.540445   0.537421   2.866   0.1316   
# F10_6 - C10_30 == 0   2.028798   0.629671   3.222   0.0487 * 
#   E20_16 - C10_30 == 0  2.445028   0.628792   3.888    <0.01 **
#   E20_2 - A30_7 == 0   -0.211007   0.490667  -0.430   1.0000   
# F10_6 - A30_7 == 0    0.277346   0.590270   0.470   1.0000   
# E20_16 - A30_7 == 0   0.693575   0.589333   1.177   0.9847   
# F10_6 - E20_2 == 0    0.488352   0.571910   0.854   0.9989   
# E20_16 - E20_2 == 0   0.904582   0.570943   1.584   0.8874   
# E20_16 - F10_6 == 0   0.416230   0.658513   0.632   0.9999 

Lines_worker_model = glmmTMB(Worker_sel ~   Exp_nest + (1 | Recorded_dead),  data = f1_0_data_nomat , family = "nbinom2", na.action = "na.fail")
res <- simulateResiduals(Lines_worker_model , plot = T)
car::Anova(Lines_worker_model)
summary(Lines_worker_model)


Lines_qp_model = glmmTMB(QueenP_sel ~   Exp_nest + (1 | Recorded_dead),  data = f1_0_data_nomat , family = "nbinom2", na.action = "na.fail")
res <- simulateResiduals(Lines_qp_model , plot = T)
f1_0_data_nomat$Exp_nest
car::Anova(Lines_qp_model)
# Analysis of Deviance Table (Type II Wald chisquare tests)
# 
# Response: QueenP_sel
# Chisq Df Pr(>Chisq)    
# Exp_nest 34.732 10  0.0001387 ***
summary(Lines_qp_model)
# Family: nbinom2  ( log )
# Formula:          QueenP_sel ~ Exp_nest + (1 | Recorded_dead)
# Data: f1_0_data_nomat
# 
# AIC      BIC   logLik deviance df.resid 
# 474.6    498.9   -224.3    448.6       35 
# 
# Random effects:
#   
#   Conditional model:
#   Groups        Name        Variance  Std.Dev. 
# Recorded_dead (Intercept) 1.234e-09 3.512e-05
# Number of obs: 48, groups:  Recorded_dead, 2
# 
# Dispersion parameter for nbinom2 family (): 0.867 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)    
# (Intercept)      3.1918     0.6309   5.059 4.21e-07 ***
#   Exp_nestA30_29  -0.8893     0.8433  -1.054   0.2917    
# Exp_nestA30_10  -0.7939     0.8042  -0.987   0.3235    
# Exp_nestC10_1    0.7594     0.7953   0.955   0.3396    
# Exp_nestE20_17   0.9764     0.7948   1.228   0.2193    
# Exp_nestA30_17   1.1149     0.7945   1.403   0.1605    
# Exp_nestC10_30  -1.0816     0.8465  -1.278   0.2013    
# Exp_nestA30_7    1.6894     0.7938   2.128   0.0333 *  
#   Exp_nestE20_2    0.6332     0.7706   0.822   0.4113    
# Exp_nestF10_6    0.8512     0.8878   0.959   0.3377    
# Exp_nestE20_16   1.7402     0.8858   1.964   0.0495 * 

summary(glht(Lines_qp_model,linfct=mcp( Exp_nest = "Tukey")))

# 	 Simultaneous Tests for General Linear Hypotheses

# Multiple Comparisons of Means: Tukey Contrasts
# 
# 
# Fit: glmmTMB(formula = QueenP_sel ~ Exp_nest + (1 | Recorded_dead), 
#              data = f1_0_data_nomat, family = "nbinom2", na.action = "na.fail", 
#              ziformula = ~0, dispformula = ~1)
# 
# Linear Hypotheses:
#                       Estimate Std. Error z value Pr(>|z|)   
# A30_29 - C10_13 == 0 -0.88926    0.84335  -1.054   0.9934   
# A30_10 - C10_13 == 0 -0.79395    0.80422  -0.987   0.9962   
# C10_1 - C10_13 == 0   0.75940    0.79526   0.955   0.9971   
# E20_17 - C10_13 == 0  0.97637    0.79478   1.228   0.9789   
# A30_17 - C10_13 == 0  1.11492    0.79453   1.403   0.9471   
# C10_30 - C10_13 == 0 -1.08163    0.84648  -1.278   0.9721   
# A30_7 - C10_13 == 0   1.68944    0.79379   2.128   0.5528   
# E20_2 - C10_13 == 0   0.63317    0.77058   0.822   0.9992   
# F10_6 - C10_13 == 0   0.85121    0.88778   0.959   0.9970   
# E20_16 - C10_13 == 0  1.74023    0.88584   1.964   0.6703   
# A30_10 - A30_29 == 0  0.09531    0.74966   0.127   1.0000   
# C10_1 - A30_29 == 0   1.64866    0.74004   2.228   0.4817   
# E20_17 - A30_29 == 0  1.86563    0.73953   2.523   0.2867   
# A30_17 - A30_29 == 0  2.00418    0.73926   2.711   0.1924   
# C10_30 - A30_29 == 0 -0.19237    0.79483  -0.242   1.0000   
# A30_7 - A30_29 == 0   2.57870    0.73846   3.492   0.0197 * 
#   E20_2 - A30_29 == 0   1.52243    0.71346   2.134   0.5487   
# F10_6 - A30_29 == 0   1.74047    0.83868   2.075   0.5914   
# E20_16 - A30_29 == 0  2.62949    0.83662   3.143   0.0614 . 
# C10_1 - A30_10 == 0   1.55335    0.69512   2.235   0.4763   
# E20_17 - A30_10 == 0  1.77032    0.69458   2.549   0.2726   
# A30_17 - A30_10 == 0  1.90887    0.69429   2.749   0.1753   
# C10_30 - A30_10 == 0 -0.28768    0.75319  -0.382   1.0000   
# A30_7 - A30_10 == 0   2.48339    0.69345   3.581   0.0147 * 
#   E20_2 - A30_10 == 0   1.42712    0.66675   2.140   0.5443   
# F10_6 - A30_10 == 0   1.64516    0.79932   2.058   0.6038   
# E20_16 - A30_10 == 0  2.53418    0.79717   3.179   0.0560 . 
# E20_17 - C10_1 == 0   0.21697    0.68418   0.317   1.0000   
# A30_17 - C10_1 == 0   0.35552    0.68389   0.520   1.0000   
# C10_30 - C10_1 == 0  -1.84103    0.74361  -2.476   0.3151   
# A30_7 - C10_1 == 0    0.93004    0.68303   1.362   0.9567   
# E20_2 - C10_1 == 0   -0.12623    0.65591  -0.192   1.0000   
# F10_6 - C10_1 == 0    0.09181    0.79031   0.116   1.0000   
# E20_16 - C10_1 == 0   0.98083    0.78812   1.245   0.9769   
# A30_17 - E20_17 == 0  0.13855    0.68334   0.203   1.0000   
# C10_30 - E20_17 == 0 -2.05800    0.74311  -2.769   0.1676   
# A30_7 - E20_17 == 0   0.71307    0.68248   1.045   0.9939   
# E20_2 - E20_17 == 0  -0.34320    0.65534  -0.524   1.0000   
# F10_6 - E20_17 == 0  -0.12516    0.78983  -0.158   1.0000   
# E20_16 - E20_17 == 0  0.76386    0.78765   0.970   0.9967   
# C10_30 - A30_17 == 0 -2.19655    0.74284  -2.957   0.1038   
# A30_7 - A30_17 == 0   0.57452    0.68219   0.842   0.9990   
# E20_2 - A30_17 == 0  -0.48175    0.65503  -0.735   0.9997   
# F10_6 - A30_17 == 0  -0.26371    0.78958  -0.334   1.0000   
# E20_16 - A30_17 == 0  0.62531    0.78739   0.794   0.9994   
# A30_7 - C10_30 == 0   2.77107    0.74205   3.734    <0.01 **
#   E20_2 - C10_30 == 0   1.71480    0.71716   2.391   0.3687   
# F10_6 - C10_30 == 0   1.93284    0.84183   2.296   0.4324   
# E20_16 - C10_30 == 0  2.82186    0.83979   3.360   0.0312 * 
#   E20_2 - A30_7 == 0   -1.05627    0.65413  -1.615   0.8747   
# F10_6 - A30_7 == 0   -0.83823    0.78883  -1.063   0.9930   
# E20_16 - A30_7 == 0   0.05079    0.78664   0.065   1.0000   
# F10_6 - E20_2 == 0    0.21804    0.76547   0.285   1.0000   
# E20_16 - E20_2 == 0   1.10706    0.76322   1.451   0.9342   
# E20_16 - F10_6 == 0   0.88902    0.88140   1.009   0.9954   

mean_C10_30 <- predict(Lines_qp_model, newdata = f1_0_data_nomat[f1_0_data_nomat$Exp_nest == "C10_30", ], type = "response")
mean_E20_16 <- predict(Lines_qp_model, newdata = f1_0_data_nomat[f1_0_data_nomat$Exp_nest == "E20_16", ], type = "response")

f1_0_data_nomat$logQueenP_sel <- log(f1_0_data_nomat$QueenP_sel)
aggregate(f1_0_data_nomat$logQueenP_sel,by = list(f1_0_data_nomat$Exp_nest), median)
# Group.1     x
# 1   C10_13 3.4657359
# 2   A30_29 2.2499048
# 3   A30_10 2.3025851
# 4    C10_1 3.7376696
# 5   E20_17 3.8918203
# 6   A30_17 4.3040651
# 7   C10_30 0.8958797
# 8    A30_7 1.9459101
# 9    E20_2 3.7903499
# 10   F10_6 3.9889840
# 11  E20_16 5.0998664


tukey_qp <- glht(Lines_qp_model,linfct=mcp( Exp_nest = "Tukey"))
cld(tukey_qp)
#  C10_30  A30_7   A30_10 A30_29 C10_13    C10_1  E20_2   E20_17  F10_6 A30_17  E20_16
#   "a"    "c"    "ab"   "ab"    "ac"      "ac"  "ac"        "ac"  "ac"   "ac"   "bc" 
f1_0_data_nomat$qp_letters <- c(rep("ab",5),rep("ac",5), rep("ab", 4), rep("c", 5), rep("ac", 5), rep("ac", 3), rep("a", 4), rep("bc", 3), rep("ac",5), rep("ac", 6), rep("ac",3))
f1_0_data_nomat$Exp_nest <- factor(f1_0_data_nomat$Exp_nest, levels = c("C10_30", "A30_7", "A30_10", "A30_29","C10_13", "C10_1", "E20_2", "E20_17", "F10_6","A30_17", "E20_16")) #by qp



library(viridis)
library(ggpubr)
library(RColorBrewer)
Fig2_Aqp_sel  <- ggplot(data = f1_0_data_nomat, aes(x =Exp_nest , y = log(QueenP_sel), fill = Exp_nest))+
  geom_boxplot(outlier.shape = NA)+ 
  scale_fill_brewer(palette = "PRGn")+
  #scale_fill_viridis(discrete = TRUE, option = "D")+
  geom_quasirandom()+
  # ylim(0,350)+
  xlab("F0 Maternal colony")+
  ylab("F1 log queen pupae")+
  theme_minimal()+
  theme(legend.position = "none",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))+
  geom_text (aes(y = 7,label= qp_letters))
# annotate("text", x=1, y = 250, label = "5")+
# annotate("text", x=2, y = 250, label = "5")+
# annotate("text", x=3, y = 250, label = "3")+
# annotate("text", x=4, y = 250, label = "5")+
# annotate("text", x=5, y = 250, label = "3")+
# annotate("text", x=6, y = 250, label = "5")+
# annotate("text", x=7, y = 250, label = "4")+
# annotate("text", x=8, y = 250, label = "4")+
# annotate("text", x=9, y = 250, label = "3")+
# annotate("text", x=10, y = 250, label = "6")+
# annotate("text", x=11, y = 250, label = "3")

library(emmeans)

cld_eggs = as.data.frame( cld(emmeans(correctedModelGAM, ~ Exp_nest ), Letters = letters ) ) 
f1_0_data <- merge(f1_0_data, cld_eggs, by = "Exp_nest")
FigS2_egg_sel <- ggplot(data = f1_0_data_nomat, aes(x =Exp_nest , y = log(Eggs_sel), fill = Exp_nest))+
  geom_boxplot(outlier.shape = NA)+
  #scale_fill_viridis_d()+
  scale_fill_brewer(palette = "PRGn")+
  geom_quasirandom()+
  # labs(title="(a)")+
  xlab("F0 Maternal colony")+
  ylab("F1 log eggs")+
  # ylim(0,350)+
  geom_text(aes(y = 6.5, label= eggs_letters))+
  theme_minimal()+
  theme(legend.position = "none",
        axis.title = element_text(size=15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))
Fig2_Aqp_sel / FigS2_egg_sel + plot_annotation(tag_levels = 'A')
# annotate("text", x=1, y = 250, label = "5")+
# annotate("text", x=2, y = 250, label = "5")+
# annotate("text", x=3, y = 250, label = "3")+
# annotate("text", x=4, y = 250, label = "5")+
# annotate("text", x=5, y = 250, label = "3")+
# annotate("text", x=6, y = 250, label = "5")+
# annotate("text", x=7, y = 250, label = "4")+
# annotate("text", x=8, y = 250, label = "4")+
# annotate("text", x=9, y = 250, label = "3")+
# annotate("text", x=10, y = 250, label = "6")+
# annotate("text", x=11, y = 250, label = "3")

lines_casteratio_model = glmmTMB(casteR ~  Exp_nest + (1|Recorded_dead) ,  data = f1_0_data )


res <- simulateResiduals(lines_casteratio_model, plot = T)
summary(lines_casteratio_model)
# Family: gaussian  ( identity )
# Formula:          casteR ~ Exp_nest + (1 | Recorded_dead)
# Data: f1_0_data
# 
# AIC      BIC   logLik deviance df.resid 
# 2.1     26.4     12.0    -23.9       35 
# 
# Random effects:
#   
#   Conditional model:
#   Groups        Name        Variance  Std.Dev. 
# Recorded_dead (Intercept) 1.439e-12 0.0000012
# Residual                  3.555e-02 0.1885530
# Number of obs: 48, groups:  Recorded_dead, 2
# 
# Dispersion estimate for gaussian family (sigma^2): 0.0356 
# 
# Conditional model:
#   Estimate Std. Error z value Pr(>|z|)   
# (Intercept)     0.296125   0.108861   2.720  0.00652 **
#   Exp_nestA30_29 -0.057241   0.144010  -0.398  0.69101   
# Exp_nestA30_10  0.004197   0.137700   0.030  0.97568   
# Exp_nestC10_1   0.134218   0.137700   0.975  0.32970   
# Exp_nestE20_17  0.087007   0.137700   0.632  0.52748   
# Exp_nestA30_17  0.065419   0.137700   0.475  0.63473   
# Exp_nestC10_30 -0.034421   0.144010  -0.239  0.81109   
# Exp_nestA30_7   0.142733   0.137700   1.037  0.29995   
# Exp_nestE20_2   0.084306   0.133327   0.632  0.52717   
# Exp_nestF10_6   0.115357   0.153953   0.749  0.45368   
# Exp_nestE20_16  0.161002   0.153953   1.046  0.29566  
car::Anova(lines_casteratio_model)
# Analysis of Deviance Table (Type II Wald chisquare tests)
# 
# Response: casteR
# Chisq Df Pr(>Chisq)
# Exp_nest 6.316 10     0.7881

Fig2B_cr_sel <- ggplot(data = f1_0_data_nomat, aes(x =Exp_nest , y = casteR, fill = Exp_nest))+
  geom_boxplot( outlier.shape = NA, colour = "grey20")+
  scale_fill_viridis( discrete = TRUE, option = "D")+
  geom_quasirandom()+
  labs(title="(b)")+
  xlab("F0 Maternal colony")+
  ylab("F1 caste ratio")+
  ylim(0,1)+
  theme_pubr()+
  theme(legend.position = "none",
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))
Fig2_Aqp_sel / Fig2B_cr_sel


survival_lines <- coxme(Surv(Week, Death) ~  (Exp_nest) + (1|Recorded_dead)  , data = f1_0_data_nomat)
survival_lines2 <- coxme(Surv(Week, Death) ~ (1|Recorded_dead)  , data = f1_0_data_nomat)
anova(survival_lines2, survival_lines)
# Analysis of Deviance Table
# Cox model: response is  Surv(Week, Death)
# Model 1: ~(1 | Recorded_dead)
# Model 2: ~(Exp_nest) + (1 | Recorded_dead)
# loglik  Chisq Df P(>|Chi|)
# 1 -137.73                    
# 2 -133.24 8.9891 10    0.5331

car::Anova(survival_lines)
# Analysis of Deviance Table (Type II tests)
# 
# Response: Surv(Week, Death)
# Df  Chisq Pr(>Chisq)
# Exp_nest 10 9.4903     0.4863
summary(survival_lines)



lif_sel <- ggplot(data = f1_0_data_nomat, aes(x =Exp_nest , y = Week, fill = Exp_nest))+
  geom_boxplot( outlier.shape = NA)+
  scale_fill_viridis_d()+
  geom_quasirandom()+
  # labs(title="(b)")+
  xlab("F0 Maternal colony")+
  ylab("F1 lifespan (weeks)")+
  theme_pubr()+
  #  ylim(13,75)+
  theme(legend.position = "none",
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))

egg_sel / qp_sel/  cr_sel /lif_sel  -> F1_plots
qp_sel/ lif_sel  -> F1_2plot
egg_sel      /  cr_sel   -> supFig    

######## Testing maternal lineages ####
# For this any colony with less than 3 replicates is removed
setwd("~/Documents/PhD_Tesis/04_selected_lines/")
f1_0_data <- read.xlsx("Selection_lines_records_complete.xlsx",sheet = 2)
#f1_0_data$sEggs_total <- as.numeric(f1_0_data$sEggs_total)
#f1_0_data$sLifespan_weeks <- as.numeric(f1_0_data$sLifespan_weeks)
f1_0_data$casteR <- f1_0_data$QueenP_sel/(f1_0_data$QueenP_sel + f1_0_data$Worker_sel)
f1_0_data$casteRf0 <- f1_0_data$Worker_Ptotal /(f1_0_data$Worker_Ptotal + f1_0_data$Queen_Ptotal)
f1_0_data$Maternal <- as.factor(f1_0_data$Maternal )
summary(f1_0_data) 
#f1_0_data$Comments_f0 <- as.factor(f1_0_data$Comments_f0 )
f1_0_data$Comments <- as.factor(f1_0_data$Comments)
f1_0_data$Recorded_dead <- as.factor(f1_0_data$Recorded_dead)
f1_0_data <- f1_0_data[f1_0_data$Exp_nest %in% c("C10_30", "A30_7", "A30_10", "A30_29","C10_13", "C10_1", "E20_2", "E20_17", "F10_6","A30_17", "E20_16"),]
f1_0_data$Exp_nest <- as.factor(f1_0_data$Exp_nest)

f1_0_data$Exp_nest <- factor(f1_0_data$Exp_nest, levels = c("C10_30", "A30_7", "A30_10", "A30_29","C10_13", "C10_1", "E20_2", "E20_17", "F10_6","A30_17", "E20_16")) #by qp
f1_0_data$Exp_nest <- factor(f1_0_data$Exp_nest, levels = c("C10_13", "A30_29", "A30_10", "C10_1","E20_17", "A30_17", "C10_30", "A30_7","E20_2", "F10_6", "E20_16")) #by qp?
f1_0_data$Productivity <- factor(f1_0_data$Productivity, levels = c("Low", "High"))



f1_0_data_nomat$Productivity <- factor(f1_0_data_nomat$Productivity, levels = c("Low", "High"))
correctedModelGAM = glmmTMB(QueenP_sel ~   Exp_nest + (1 | Recorded_dead),  data = f1_0_data_nomat , family = "nbinom2", na.action = "na.fail")

res <- simulateResiduals(correctedModelGAM, plot = T)
summary(correctedModelGAM)

glmmTMB:::Anova.glmmTMB(QueenP_sel ~   Exp_nest + (1 | Recorded_dead),  data = f1_0_data_nomat , family = "nbinom2", na.action = "na.fail")
car::Anova(correctedModelGAM)

summary(glht(correctedModelGAM,linfct=mcp( Exp_nest = "Tukey")))

tukey_qp <- glht(correctedModelGAM,linfct=mcp( Exp_nest = "Tukey"))
# Simultaneous Tests for General Linear Hypotheses
# 
# Multiple Comparisons of Means: Tukey Contrasts
# 
# 
# Fit: glmmTMB(formula = QueenP_sel ~ Exp_nest + (1 | Recorded_dead) + 
#                (1 | Treatment), data = f1_0_data, family = "nbinom2", na.action = "na.fail", 
#              ziformula = ~0, dispformula = ~1)
# 
# Linear Hypotheses:
#   Estimate Std. Error z value Pr(>|z|)   
# A30_17 - A30_10 == 0  1.90887    0.69429   2.749   0.1760
# A30_29 - A30_10 == 0 -0.09531    0.74966  -0.127   1.0000
# A30_7 - A30_10 == 0   2.48339    0.69344   3.581   0.0146 *
#   C10_1 - A30_10 == 0   1.55335    0.69512   2.235   0.4756
# C10_13 - A30_10 == 0  0.79395    0.80422   0.987   0.9961
# C10_30 - A30_10 == 0 -0.28768    0.75319  -0.382   1.0000
# E20_16 - A30_10 == 0  2.53418    0.79717   3.179   0.0555 .
# E20_17 - A30_10 == 0  1.77032    0.69458   2.549   0.2727
# E20_2 - A30_10 == 0   1.42712    0.66675   2.140   0.5443
# F10_6 - A30_10 == 0   1.64516    0.79932   2.058   0.6031
# A30_29 - A30_17 == 0 -2.00418    0.73926  -2.711   0.1912
# A30_7 - A30_17 == 0   0.57452    0.68219   0.842   0.9990
# C10_1 - A30_17 == 0  -0.35552    0.68389  -0.520   1.0000
# C10_13 - A30_17 == 0 -1.11492    0.79453  -1.403   0.9470
# C10_30 - A30_17 == 0 -2.19655    0.74284  -2.957   0.1039
# E20_16 - A30_17 == 0  0.62531    0.78739   0.794   0.9994
# E20_17 - A30_17 == 0 -0.13855    0.68334  -0.203   1.0000
# E20_2 - A30_17 == 0  -0.48175    0.65503  -0.735   0.9997
# F10_6 - A30_17 == 0  -0.26371    0.78958  -0.334   1.0000
# A30_7 - A30_29 == 0   2.57870    0.73846   3.492   0.0202 *
#   C10_1 - A30_29 == 0   1.64866    0.74004   2.228   0.4803
# C10_13 - A30_29 == 0  0.88926    0.84335   1.054   0.9934
# C10_30 - A30_29 == 0 -0.19237    0.79483  -0.242   1.0000
# E20_16 - A30_29 == 0  2.62949    0.83662   3.143   0.0616 .
# E20_17 - A30_29 == 0  1.86563    0.73953   2.523   0.2876
# E20_2 - A30_29 == 0   1.52243    0.71346   2.134   0.5491
# F10_6 - A30_29 == 0   1.74047    0.83868   2.075   0.5909
# C10_1 - A30_7 == 0   -0.93004    0.68303  -1.362   0.9565
# C10_13 - A30_7 == 0  -1.68944    0.79379  -2.128   0.5521
# C10_30 - A30_7 == 0  -2.77107    0.74205  -3.734    <0.01 **
#   E20_16 - A30_7 == 0   0.05079    0.78664   0.065   1.0000
# E20_17 - A30_7 == 0  -0.71307    0.68248  -1.045   0.9939
# E20_2 - A30_7 == 0   -1.05627    0.65413  -1.615   0.8744
# F10_6 - A30_7 == 0   -0.83823    0.78883  -1.063   0.9930
# C10_13 - C10_1 == 0  -0.75940    0.79526  -0.955   0.9971
# C10_30 - C10_1 == 0  -1.84103    0.74361  -2.476   0.3151
# E20_16 - C10_1 == 0   0.98083    0.78812   1.245   0.9769
# E20_17 - C10_1 == 0   0.21697    0.68418   0.317   1.0000
# E20_2 - C10_1 == 0   -0.12623    0.65591  -0.192   1.0000
# F10_6 - C10_1 == 0    0.09181    0.79031   0.116   1.0000
# C10_30 - C10_13 == 0 -1.08163    0.84648  -1.278   0.9721
# E20_16 - C10_13 == 0  1.74022    0.88584   1.964   0.6698
# E20_17 - C10_13 == 0  0.97637    0.79479   1.228   0.9789
# E20_2 - C10_13 == 0   0.63316    0.77058   0.822   0.9992
# F10_6 - C10_13 == 0   0.85120    0.88778   0.959   0.9970
# E20_16 - C10_30 == 0  2.82186    0.83979   3.360   0.0313 *
#   E20_17 - C10_30 == 0  2.05800    0.74311   2.769   0.1675
# E20_2 - C10_30 == 0   1.71480    0.71716   2.391   0.3683
# F10_6 - C10_30 == 0   1.93284    0.84183   2.296   0.4319
# E20_17 - E20_16 == 0 -0.76386    0.78765  -0.970   0.9967
# E20_2 - E20_16 == 0  -1.10706    0.76322  -1.451   0.9342
# F10_6 - E20_16 == 0  -0.88902    0.88140  -1.009   0.9954
# E20_2 - E20_17 == 0  -0.34320    0.65534  -0.524   1.0000
# F10_6 - E20_17 == 0  -0.12516    0.78983  -0.158   1.0000
# F10_6 - E20_2 == 0    0.21804    0.76547   0.285   1.0000

f1_0_data_nomat$logQueenP_sel <- log(f1_0_data_nomat$QueenP_sel)
aggregate(f1_0_data_nomat$logQueenP_sel,by = list(f1_0_data_nomat$Exp_nest), median)
# Group.1     x
# 1   C10_13 3.4657359
# 2   A30_29 2.2499048
# 3   A30_10 2.3025851
# 4    C10_1 3.7376696
# 5   E20_17 3.8918203
# 6   A30_17 4.3040651
# 7   C10_30 0.8958797
# 8    A30_7 1.9459101
# 9    E20_2 3.7903499
# 10   F10_6 3.9889840
# 11  E20_16 5.0998664
cld(tukey_qp)
#  C10_30  A30_7   A30_10 A30_29 C10_13    C10_1  E20_2   E20_17  F10_6 A30_17  E20_16
#   "a"    "c"    "ab"   "ab"    "ac"      "ac"  "ac"        "ac"  "ac"   "ac"   "bc" 
f1_0_data_nomat$qp_letters <- c(rep("ab",5),rep("ac",5), rep("ab", 4), rep("c", 5), rep("ac", 5), rep("ac", 3), rep("a", 4), rep("bc", 3), rep("ac",5), rep("ac", 6), rep("ac",3))
f1_0_data_nomat$Exp_nest <- factor(f1_0_data_nomat$Exp_nest, levels = c("C10_30", "A30_7", "A30_10", "A30_29","C10_13", "C10_1", "E20_2", "E20_17", "F10_6","A30_17", "E20_16")) #by qp



library(viridis)
library(ggpubr)
library(RColorBrewer)
Fig2_Aqp_sel  <- ggplot(data = f1_0_data_nomat, aes(x =Exp_nest , y = log(QueenP_sel), fill = Productivity))+
  geom_boxplot(outlier.shape = NA)+ 
  scale_fill_manual(values = c("#9595F6", "#FF9494"), labels = c("High", "Low" ))+
  #scale_fill_viridis(discrete = TRUE, option = "D")+
  geom_quasirandom()+
  # ylim(0,350)+
  xlab("Maternal colony")+
  ylab("Daughter log(queen pupae)")+
  theme_minimal()+
  theme(legend.position = "none",
        axis.title = element_text(size = 15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))+
  geom_text (aes(y = 7,label= qp_letters))
# annotate("text", x=1, y = 250, label = "5")+
# annotate("text", x=2, y = 250, label = "5")+
# annotate("text", x=3, y = 250, label = "3")+
# annotate("text", x=4, y = 250, label = "5")+
# annotate("text", x=5, y = 250, label = "3")+
# annotate("text", x=6, y = 250, label = "5")+
# annotate("text", x=7, y = 250, label = "4")+
# annotate("text", x=8, y = 250, label = "4")+
# annotate("text", x=9, y = 250, label = "3")+
# annotate("text", x=10, y = 250, label = "6")+
# annotate("text", x=11, y = 250, label = "3")

library(emmeans)

cld_eggs = as.data.frame( cld(emmeans(correctedModelGAM, ~ Exp_nest ), Letters = letters ) ) 
f1_0_data <- merge(f1_0_data, cld_eggs, by = "Exp_nest")
FigS2_egg_sel <- ggplot(data = f1_0_data_nomat, aes(x =Exp_nest , y = log(Eggs_sel), fill = Productivity))+
  geom_boxplot(outlier.shape = NA)+
  #scale_fill_viridis_d()+
  scale_fill_manual(values = c("#9595F6", "#FF9494"), labels = c("High", "Low" ))+
  geom_quasirandom()+
  # labs(title="(a)")+
  xlab("Maternal colony")+
  ylab("Daughter log(eggs)")+
  # ylim(0,350)+
  geom_text(aes(y = 6.5, label= eggs_letters))+
  theme_minimal()+
  theme(legend.position = "none",
        axis.title = element_text(size=15),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12))
Fig2_Aqp_sel / FigS2_egg_sel + plot_annotation(tag_levels = 'A')
