library(car)
library(lme4)
library(emmeans)
library(ggplot2)


#read the dataset
data <- read.table(file="data.txt", header = TRUE, sep = "\t")
names (data)
str(data)

data$days <- as.factor(data$days)
data$block <- as.factor(data$block)
data$id <- as.factor(data$id)
str(data)

###----------SPERM NUMBER
number<- lmer(sperm_number~days + (1|id) +(1|block), data=data)
Anova(number)
summary(number)
lsmeans(number, pairwise ~ days | days)

#normality of residuals
qqnorm(resid(number))
qqline(resid(number))
hist((resid(number) - mean(resid(number))) / sd(resid(number)), freq = FALSE); curve(dnorm, add = TRUE) #ok

#model homogeneity of variance 
plot(fitted(number), resid(number)) 

#plot - sperm number
plot_a<-ggplot(data, aes(x=days, y=sperm_number)) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() + 
  geom_jitter(width=0.1) + 
  labs(x="Days of isolation", y = "Sperm number") + 
  theme(legend.position="none", axis.title.x=element_blank(), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) + #togliere legenda
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(0, 600000, 100000)) +
  expand_limits(y=c(0,500000))
plot_a


###----------SPERM LONGEVITY
data$sperm_longevity_sec <- as.integer(data$sperm_longevity_sec) #transform data in integer to meet poisson assumption model

long <- glmer(sperm_longevity_sec~days + (1|id) + (1|block), data=data, family= poisson())
Anova(long)
lsmeans(long, pairwise ~ days|days )

#normality of residuals
qqnorm(resid(long))
qqline(resid(long))
hist((resid(long) - mean(resid(long))) / sd(resid(long)), freq = FALSE); curve(dnorm, add = TRUE) 

#variance homogeneity model
plot(fitted(long), resid(long)) 

# plot
plot_b<-ggplot(data, aes(x=days, y=sperm_longevity_sec)) + 
  geom_boxplot() + 
  theme_classic() + 
  geom_jitter(width=0.1) + 
  labs(x="Days of isolation", y = "Sperm longevity (seconds)") + 
  theme(legend.position="none", axis.title.x=element_blank(), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) +
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(50,350, 50))+
  expand_limits(y=c(50,350))
plot_b


###----------SPERM VELOCITY 

##---VCL0
vcl0<- lmer(VCL0~days + (1|id) + (1|block), data=data)
Anova(vcl0)
lsmeans(vcl0, pairwise ~ days| days)

#normality of residuals
qqnorm(resid(vcl0))
qqline(resid(vcl0))
hist((resid(vcl0) - mean(resid(vcl0))) / sd(resid(vcl0)), freq = FALSE); curve(dnorm, add = TRUE)

#variance homogeneity model
plot(fitted(vcl0), resid(vcl0))

# plot
plot_c<-ggplot(data, aes(x=days, y=VCL0)) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() +
  geom_jitter(width=0.1) + 
  labs(x="Days of isolation", y = "Sperm velocity t0 (μm/s)") + #nomi assi
  theme(legend.position="none", axis.title.x=element_blank(), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) + #togliere legenda
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(85,210, 25)) +
  expand_limits(y=c(70,210))
plot_c


##---VCL 30
vcl30 <- lmer(VCL30~days+(1|id) +(1|block) , data=data)
Anova(vcl30) 
lsmeans(vcl30, pairwise ~ days| days) 

#variance homogeneity model
plot(fitted(vcl30), resid(vcl30))

#normality of residuals
qqnorm(resid(vcl30))
qqline(resid(vcl30))
hist((resid(vcl30) - mean(resid(vcl30))) / sd(resid(vcl0)), freq = FALSE); curve(dnorm, add = TRUE)

#plot
plot_d<-ggplot(data, aes(x=days, y=VCL30)) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() + 
  geom_jitter(width=0.1) + 
  labs(x="Days of isolation", y = "Sperm velocity t30 (μm/s)") + 
  theme(legend.position="none", axis.title.x=element_blank(), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) + 
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(85,210, 25)) +
  expand_limits(y=c(70,210))
plot_d


###--------SPERM MOTILITY 

#---motility 0
data$non_motile0 <-as.integer(data$non_motile0)
data$motile0 <-as.integer(data$motile0)

y=cbind(data$motile0,data$non_motile0)
mot0<-glmer(y~ days + (1 | id) + (1 | block) , data=data, family=binomial(logit))
Anova(mot0)
lsmeans(mot0, pairwise~days|days)

#normality of residuals
qqnorm(resid(mot0))
qqline(resid(mot0))
hist((resid(mot0) - mean(resid(mot0))) / sd(resid(mot0)), freq = FALSE); curve(dnorm, add = TRUE)

#variance homogeneity model
plot(fitted(mot0), resid(mot0))

#plot 
plot_e<-ggplot(data, aes(x=days, y=motility_perc0)) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() + #tipo di tema
  geom_jitter(width=0.1) + #poszione dei dots (trovarla in maniera empirica)
  labs(x="Days of isolation", y = "Sperm motility t0 (%)") + #nomi assi
  theme(legend.position="none", axis.title.x=element_blank(), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) +
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(40,100,10)) +
  expand_limits(y=c(40,100))  
plot_e

#---motility 30
data$non_motile30 <-as.integer(data$non_motile30)
data$motile30 <-as.integer(data$motile30)

y=cbind(data$motile30,data$non_motile30)
mot30<-glmer(y~ days + (1 | id) + (1 | block), data=data, family=binomial(logit))
Anova(mot30)
lsmeans(mot30, pairwise~days|days)

#normality of residuals
qqnorm(resid(mot30))
qqline(resid(mot30))
hist((resid(mot30) - mean(resid(mot30))) / sd(resid(mot30)), freq = FALSE); curve(dnorm, add = TRUE)

#variance homogeneity model
plot(fitted(mot30), resid(mot30))

#plot
plot_f <- ggplot(data, aes(x=days, y=motility_perc30)) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() + 
  geom_jitter(width=0.1) + 
  labs(x="Days of isolation", y = "Sperm motility t30 (%)") + 
  theme(legend.position="none", axis.title.x=element_blank(), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) +
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(40,100,10)) +
  expand_limits(y=c(40,100))  
plot_f


###----------SPERM VIABILITY 
y=cbind(data$live_sperm,data$dead_sperm)
viab<-glmer(y~ days + (1 | id) + (1 | block), data=data, family=binomial(logit))
Anova(viab)
lsmeans(viab, pairwise ~ days| days)

#normality of residuals
qqnorm(resid(viab))
qqline(resid(viab))
hist((resid(viab) - mean(resid(viab))) / sd(resid(viab)), freq = FALSE); curve(dnorm, add = TRUE)

#variance homogeneity model
plot(fitted(viab), resid(viab))

#plot
plot_g<-ggplot(data, aes(x=days, y=viability_perc)) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() + #tipo di tema
  geom_jitter(width=0.1) + #poszione dei dots (trovarla in maniera empirica)
  labs(x="Days of isolation", y = "Sperm viability (%)") + #nomi assi
  theme(legend.position="none", axis.title.x=element_text(size=11), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) + #togliere legenda
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(87.5, 100, 2.5)) +
  expand_limits(y=c(87.5, 100))
plot_g

###----------SPERM LINEARITY (supp. material)
#---LIN0 
lin0<- lmer(LIN0~days+(1|id) + (1|block), data=data) 
Anova(lin0) 
lsmeans(lin0, pairwise ~ days| days)

#normality of residuals
qqnorm(resid(lin0))
qqline(resid(lin0))
hist((resid(lin0) - mean(resid(lin0))) / sd(resid(lin0)), freq = FALSE); curve(dnorm, add = TRUE)

#model homogeneity of variance 
plot(fitted(lin0), resid(lin0))


#---LIN30 
lin30<- lmer(LIN30~days +(1|id) + (1|block), data=data) 
Anova(lin30) 
lsmeans(lin30, pairwise ~ days| days)

#normality of residuals
qqnorm(resid(lin30))
qqline(resid(lin30))
hist((resid(lin30) - mean(resid(lin30))) / sd(resid(lin30)), freq = FALSE); curve(dnorm, add = TRUE)

#model homogeneity of variance 
plot(fitted(lin30), resid(lin30))


###---------- SPERM DNA FRAGMENTATION 
#read the dataset
data2 <- read.table(file="data2.txt", header = TRUE, sep = "\t")
str(data2)

data2$days <- as.factor(data2$days)
data2$age <- as.factor(data2$age)
data2$block <- as.factor(data2$block)

y=cbind(data2$fragm_sperm,data2$intact_sperm)
frag <- glmer(y~ days + (1|block),data=data2, family=binomial(logit))
Anova(frag)
lsmeans(frag, pairwise ~ days| days)
 
#normality of residuals
qqnorm(resid(frag))
qqline(resid(frag))
hist((resid(frag) - mean(resid(frag))) / sd(resid(frag)), freq = FALSE); curve(dnorm, add = TRUE)

#variance homogeneity model
plot(fitted(frag), resid(frag))

#plot 
plot_h<-ggplot(data2, aes(x=days, y=(prop_frag*100))) + 
  geom_boxplot(outlier.shape = NA) + 
  theme_classic() + #tipo di tema
  geom_jitter(width=0.1) + #poszione dei dots (trovarla in maniera empirica)
  labs(x="Days of isolation", y = "Sperm DNA fragmentation (%)") + #nomi assi
  theme(legend.position="none", axis.title.x=element_text(size=11), axis.text.x=element_text(size=11), axis.title.y=element_text(size=10), axis.text.y = element_text(size=10)) +
  scale_x_discrete(labels= c("4", "7", "12")) +
  scale_y_continuous(breaks=seq(10,50,10)) +
  expand_limits(y=c(10,50))  
plot_h


