setwd("/Users/berenice/Dropbox/Manuscripts_and_Publications/2022/2022_Bryan_PIWI_manuscript/CODE/6_Animal_Analysis/")
#set working directory
# setwd("/Users/bryanteefy/Dropbox/PIWI/CODE/6_Animal_Analysis")
setwd("/Users/berenice/Dropbox/Manuscripts_and_Publications/2022/2022_Bryan_PIWI_manuscript/CODE/6_Animal_Analysis/")
#Generate Fecundity and Lifespan Plots
# R version 4.1.2 (2021-11-01)
#load libraries
library(beeswarm)  # beeswarm_0.4.0
library(survival)  # survival_3.3-1
library(ranger)    # ranger_0.14.1
library(ggplot2)   # ggplot2_3.3.6
library(dplyr)     # dplyr_1.0.9
library(ggfortify) # ggfortify_0.4.14
################################
#1. Import required data
#import fecundity table
fec_table <- read.table("./Input/Female_Fecundity_Table.txt", header = T)
#Import Killfish lifespan data
span <-read.table("./Input/Killifish_Lifespans.txt", header = T)
###############################
#1. Plot Female Fecundity
#remove data > than the study timeframe (>16 weeks)
# these were not consistently collected and the animals were "rested" longer between breeding events
fec_table <- fec_table[fec_table$female_age<=16,]
#split into age ranges
fec_table$group <- findInterval(fec_table$female_age, c(0, 8, 10, 12, 14, 16))
#plot and export
my.boxout <- paste0("./Results/", Sys.Date(),"_historical_Female_Fecundity_Boxplot.pdf")
pdf(my.boxout, width = 7, height = 5)
boxplot(fry ~ group,
data     = fec_table,
names    = c("<8", "8-10", "10-12", "12-14", "14-16"),
col      = "#0000ff22",
outline  = F,
xlab     = 'Age (Weeks)',
ylab     = 'Viable Progeny per breeding event',
main     = "Female GRZ Fecundity by Age")
beeswarm(fry~ group, data = fec_table,
add = T,
method = 'swarm',
pch = 16)
dev.off()
#run anova and tukey test
fec_table$group <- as.factor(fec_table$group)
anova <- aov(fry ~ group, data = fec_table)
summary(anova)
#Df Sum Sq Mean Sq F value Pr(>F)
#  group        4    423  105.74    2.92 0.0297 *
#  Residuals   52   1883   36.22
#---
#  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
posthoc_group_diff <- TukeyHSD(anova)
#
#Tukey multiple comparisons of means
#95% family-wise confidence level
#
#Fit: aov(formula = fry ~ group, data = fec_table)
#
#$group
#diff         lwr       upr     p adj
#2-1  3.07142857  -4.6349787 10.777836 0.7919834
#3-1  8.90476190   0.8168910 16.992633 0.0241360
#4-1  3.11688312  -5.1053151 11.339081 0.8203375
#5-1  3.84415584  -4.3780423 12.066354 0.6794600
#3-2  5.83333333  -0.6608636 12.327530 0.0977412
#4-2  0.04545455  -6.6152873  6.706196 1.0000000
#5-2  0.77272727  -5.8880145  7.433469 0.9974247
#4-3 -5.78787879 -12.8865000  1.310742 0.1600337
#5-3 -5.06060606 -12.1592273  2.038015 0.2736168
#5-4  0.72727273  -6.5240245  7.978570 0.9985419
#########################
#2. Plot Lifespan
#fit using the survival package
km_trt_fit <- survfit(Surv(time, status) ~ sex, data=span)
#running lg rank test
surv.test <- survdiff(Surv(time, status) ~ sex, span)
surv.test
#survdiff(formula = Surv(time, status) ~ sex, data = span)
#
#N Observed Expected (O-E)^2/E (O-E)^2/V
#sex=Female 24       24     35.1      3.51      9.61
#sex=Male   37       37     25.9      4.76      9.61
#
#Chisq= 9.6  on 1 degrees of freedom, p= 0.002
#make into a dataframe
modf <- fortify(km_trt_fit)
#change "stata" back to "sex"
colnames(modf)[9] <- "sex"
#remove excess data
modf <- modf[,c(1,5,9)]
#format for plotting
modf[58,] <- c(0, 1, "Female")
modf[59,] <- c(0, 1, "Male")
modf$time <- as.numeric(modf$time)
modf$surv <- as.numeric(modf$surv)
modf$sex  <- as.character(modf$sex)
#plot and export
my.curve.out <- paste0("./Results/", Sys.Date(),"_GRZ_survival_curve.pdf")
pdf(my.curve.out, onefile = F, width = 10)
ggplot(modf, aes(x = time, y = surv, group=sex)) +
geom_point(aes(color=sex)) +
geom_line(aes(color=sex)) +
ggtitle("GRZ Survival Curve") + xlab("Time (Weeks)")+ylab("Fraction Surviving") +
scale_color_manual(values=c("deeppink", "deepskyblue")) +theme_bw() +
xlim(0, 50)+
theme(text = element_text(size=20),
axis.text.x = element_text(angle=0, hjust=0.5),
plot.title = element_text(color="black", size=20, hjust = 0.5))
dev.off()
#######################
sink(file = paste0("./Results/", Sys.Date(),"_session_Info.txt", sep =""))
sessionInfo()
sink()
