EEID_NOVI_Copy_Analysis <- read.csv("EEID_NOVI_Copy_Analysis.csv")

#Loading required packages
library(ggplot2)
library(dplyr)

#Subsetting data into new dataframes broken up by temperature, life-stage and treatment groups
EEID_NOVI_Copy_Analysis_No_Controls<-subset(EEID_NOVI_Copy_Analysis, Treatment!="Control"&Pos=="1")
EEID_NOVI_Copy_Analysis_No_Controls

EEID_NOVI_Eft_Data<-subset(EEID_NOVI_Copy_Analysis_No_Controls,LifeStage=="Eft")
EEID_NOVI_Adult_Data<-subset(EEID_NOVI_Copy_Analysis_No_Controls,LifeStage=="Adult")
EEID_NOVI_Eft_Data$Temp<-factor(EEID_NOVI_Eft_Data$Temp,levels=c("6C","14C","22C"))
EEID_NOVI_Adult_Data$Temp<-factor(EEID_NOVI_Adult_Data$Temp,levels=c("6C","14C","22C"))
EEID_NOVI_Copy_Analysis_No_Controls$Temp<-factor(EEID_NOVI_Copy_Analysis_No_Controls$Temp,levels=c("6C","14C","22C"))
EEID_NOVI_Copy_Analysis_No_Controls$LifeStage<-factor(EEID_NOVI_Copy_Analysis_No_Controls$LifeStage,levels=c("Eft","Adult"))

#New
EEID_NOVI_All_Necropsy6C<-subset(EEID_NOVI_Copy_Analysis_No_Controls,Temp=="6C")
EEID_NOVI_All_Necropsy14C<-subset(EEID_NOVI_Copy_Analysis_No_Controls,Temp=="14C")
EEID_NOVI_All_Necropsy22C<-subset(EEID_NOVI_Copy_Analysis, Temp=="22C")

EEID_NOVI_All_Necropsy103<-subset(EEID_NOVI_Copy_Analysis_No_Controls, Treatment=="5x10^3")
EEID_NOVI_All_Necropsy104<-subset(EEID_NOVI_Copy_Analysis_No_Controls,  Treatment=="5x10^4")
EEID_NOVI_All_Necropsy105<-subset(EEID_NOVI_Copy_Analysis_No_Controls,  Treatment=="5x10^5")
EEID_NOVI_All_Necropsy106<-subset(EEID_NOVI_Copy_Analysis_No_Controls, Treatment=="5x10^6")

EEID_NOVI_Eft_Necropsy103<-subset(EEID_NOVI_Eft_Data, Treatment=="5x10^3")
EEID_NOVI_Eft_Necropsy104<-subset(EEID_NOVI_Eft_Data,  Treatment=="5x10^4")
EEID_NOVI_Eft_Necropsy105<-subset(EEID_NOVI_Eft_Data,  Treatment=="5x10^5")
EEID_NOVI_Eft_Necropsy106<-subset(EEID_NOVI_Eft_Data, Treatment=="5x10^6")

EEID_NOVI_Adult_Necropsy103<-subset(EEID_NOVI_Adult_Data, Treatment=="5x10^3")
EEID_NOVI_Adult_Necropsy104<-subset(EEID_NOVI_Adult_Data,  Treatment=="5x10^4")
EEID_NOVI_Adult_Necropsy105<-subset(EEID_NOVI_Adult_Data,  Treatment=="5x10^5")
EEID_NOVI_Adult_Necropsy106<-subset(EEID_NOVI_Adult_Data, Treatment=="5x10^6")

#Subsetting all temperature data by dose 
EEID_NOVI_All_Necropsy6C10to3<-subset(EEID_NOVI_All_Necropsy6C,Treatment=="5x10^3")
EEID_NOVI_All_Necropsy6C10to4<-subset(EEID_NOVI_All_Necropsy6C,Treatment=="5x10^4")
EEID_NOVI_All_Necropsy6C10to5<-subset(EEID_NOVI_All_Necropsy6C,Treatment=="5x10^5")
EEID_NOVI_All_Necropsy6C10to6<-subset(EEID_NOVI_All_Necropsy6C,Treatment=="5x10^6")

EEID_NOVI_All_Necropsy14C10to3<-subset(EEID_NOVI_All_Necropsy14C,Treatment=="5x10^3")
EEID_NOVI_All_Necropsy14C10to4<-subset(EEID_NOVI_All_Necropsy14C,Treatment=="5x10^4")
EEID_NOVI_All_Necropsy14C10to5<-subset(EEID_NOVI_All_Necropsy14C,Treatment=="5x10^5")
EEID_NOVI_All_Necropsy14C10to6<-subset(EEID_NOVI_All_Necropsy14C,Treatment=="5x10^6")

EEID_NOVI_All_Necropsy22C10to4<-subset(EEID_NOVI_All_Necropsy22C,Treatment=="5x10^4")
EEID_NOVI_All_Necropsy22C10to5<-subset(EEID_NOVI_All_Necropsy22C,Treatment=="5x10^5")
EEID_NOVI_All_Necropsy22C10to6<-subset(EEID_NOVI_All_Necropsy22C,Treatment=="5x10^6")


##Running kurskal wallis tests on eft load data by dose among temperature experiments
kruskal.test(Quantity~Temp,data=EEID_NOVI_Eft_Necropsy103)
kruskal.test(Quantity~Temp,data=EEID_NOVI_Eft_Necropsy104)
kruskal.test(Quantity~Temp,data=EEID_NOVI_Eft_Necropsy105)
kruskal.test(Quantity~Temp,data=EEID_NOVI_Eft_Necropsy106)

##Running wilcox tests on adult load data by dose between temperature experiments
wilcox.test(Quantity~Temp,data=EEID_NOVI_Adult_Necropsy103)
wilcox.test(Quantity~Temp,data=EEID_NOVI_Adult_Necropsy104)
wilcox.test(Quantity~Temp,data=EEID_NOVI_Adult_Necropsy105)
wilcox.test(Quantity~Temp,data=EEID_NOVI_Adult_Necropsy106)

##Running wilcox tests on load data by dose at one temperature between lifestages
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy6C10to3)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy6C10to4)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy6C10to5)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy6C10to6)

##Running wilcox tests on load data by dose at one temperature between lifestages
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy14C10to3)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy14C10to4)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy14C10to5)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy14C10to6)

##Running wilcox tests on load data by dose at one temperature between lifestages
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy22C10to4)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy22C10to5)
wilcox.test(Quantity~LifeStage,data=EEID_NOVI_All_Necropsy22C10to6)



#Boxplots showing Bsal load differences between lifestages by temperature and epxosure dose
EEID_NecropsyplotAllbyLifeStage <- ggplot(EEID_NOVI_Copy_Analysis_No_Controls, aes(x=LifeStage, y=log(Quantity+1), group=LifeStage)) + 
  geom_boxplot(aes(fill=LifeStage))
EEID_NecropsyplotAllbyLifeStage+facet_grid(Temp~Treatment)+theme_bw()+scale_fill_manual(values=c("darkorange","lightgoldenrod4"))+
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.x=element_text(color="black",face="bold"), 
        axis.text.y=element_text(color="black",face="bold"),
        legend.text=element_text(color="black",face="bold"),
        legend.title=element_text(color="black",face="bold"),
        axis.title.y = element_text(color="black", size=14, face="bold"))+
  ylab("Log(Zoospore Copies/uL+1)")+xlab("Life Stage")



#Boxplots showing Bsal load differences between temperatures by lifestage and epxosure dose
EEID_NecropsyplotAllbyTemp<- ggplot(EEID_NOVI_Copy_Analysis_No_Controls, aes(x=Temp, y=log(Quantity+1), group=Temp)) + 
  geom_boxplot(aes(fill=Temp))
EEID_NecropsyplotAllbyTemp + facet_grid(LifeStage~Treatment)+
  theme_bw()+scale_fill_manual(values=c("cornflowerblue", "goldenrod2", "firebrick"))+
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.x=element_text(color="black",face="bold"), 
        axis.text.y=element_text(color="black",face="bold"),
        legend.text=element_text(color="black",face="bold"),
        legend.title=element_text(color="black",face="bold"),
        axis.title.y = element_text(color="black", size=14, face="bold"))+
  ylab("Log(Zoospore Copies/uL+1)")+xlab("Temperature")

#Obtaining summary load data for each exposure dose, treatment, temperature and lifestage
dataSummary<-EEID_NOVI_Copy_Analysis_No_Controls %>%  
  group_by(Temp,Treatment,LifeStage) %>% 
  summarise(N = length(Quantity), 
            Mean = mean(Quantity),
            Median= median(Quantity),
            SD = sd(Quantity),
            SE = SD/sqrt(N)) 
dataSummary
