###### Thally FRRF data - fitted with gamms. just for visualitsaionrm(list=ls())

library(data.table)
library(plyr)
library(tools)
library(zoo)
library(lattice)
library(ggplot2)
require(nlme)
require(minpack.lm)
library(reshape2)
#install.packages("devtools")
library(devtools)
#install_github('padpadpadpad/TeamPhytoplankton')
#install_github('padpadpadpad/nlsTools')
library(TeamPhytoplankton)
library(nlsLoop)
library(cowplot)
library(xlsx)
require(reshape2)
#require(gdata)
setwd("/Users/elisaschaum/Dropbox/Thally long fluc paper/Thally actual selection experiment/fastact by time and temp/t300 full curves")

#listed <- list.files()
#listednames<-listed[grep(".xlsx",listed)] 
#testdata1 <- read.xlsx("20161016_FL_32_b1.xlsx", 1,startRow = 22, header=TRUE)
#datasetfromx<-data.frame()
### usually files are returned as an xls file, so we have to read them in and then convert them...
#for (i in 1:length(listednames))      
#{(data<-read.xlsx(listednames[i],1, startRow = 22, header=TRUE)) # 
#colnames(data)<-c(
#   "PAR","Fo" ,  "Fm",  "Fv",  "FvFm",  "p",  "RSigma",  "Sigma",  "Fa",  "Tau",  "NPQ",  "C",  "X.RCII",  "JPII",  "JVPII",  "aLHII","NA.",  "E",  "rP") 		
# det<-gsub(".xlsx","",listednames[i])  #get details for temps and bioreps or ponds depending on experiment	
# data<-cbind(det,data)
#  datasetfromx<-rbind(datasetfromx, data) 
#}


#datasetfromx$selT<-as.character(substr(datasetfromx$det,10,11))
#datasetfromx$assayT<- as.numeric(substr(datasetfromx$det,13,14)) 
#datasetfromx$biorep<-as.character(substr(datasetfromx$det,16,17))
#write.csv(datasetfromx, 'allfvfmdata_thally_t300.csv')


#setwd("/Users/elisaschaum/Dropbox/post doc esi/Thally actual selection experiment/fastact by time and temp/t300 full curves")


f8<-read.csv('allfvfmdata_thally_t300_t000.csv')
colnames(f8)
str(f8)
pfcmelt<-melt(f8, id.vars=c("biorep","PAR","assayT","growthT","generation"), measure.vars=c( "Fo.F.." ,"Fm...", "Fv...","Fv.Fm.Fq..Fm..", "p" , "RSigma", "Sigma","Fa", "Tau","NPQ","NSV" , "C" ,"X.RCII.", "JPII", "JVPII","aLHII","X0","E","rP"), variable.name='variable')
pfcmelt[is.na(pfcmelt)] <- 0

pfcmeltnew <- subset(pfcmelt,value!=0)
str(pfcmeltnew)
summary(pfcmeltnew)

pfc2<-ddply(pfcmeltnew, .(PAR,biorep,variable,assayT,growthT,generation), function(x) x[nrow(x), ])
pfc2$PAR<-as.numeric(pfc2$PAR)
pfc2$variable<-as.character(pfc2$variable)
pfc2<-subset(pfc2,PAR<1600) #two measurements go up to 1900- made all the same length for now


### Exponetial function
exp_dec <- function(a, b, light) {
  y <- a * exp(light * b)
  return(y)
}

Fv.FM<- subset(pfc2,value>0&variable=="Fv.Fm.Fq..Fm.."& value!="NA" &value!="")

Fv.FM$growthT<-as.character(Fv.FM$growthT)
Fv.FM$generation<-as.character(Fv.FM$generation)
Fv.FM$growthT[Fv.FM$generation == "t000"] <- "Ancestor"

Fv.FM <- within(Fv.FM, id.light <- id.light <- as.character(factor(biorep):factor(growthT):factor(PAR):factor(assayT)))
qplot(PAR,as.numeric(value),data=subset(Fv.FM, PAR>0), colour=growthT, facets=growthT~assayT,shape=biorep)

Fv.FM$value<-as.numeric(Fv.FM$value)
Fv.FM<- within(Fv.FM, id.biorep  <- as.character(factor(biorep):factor(growthT)))

Fv.FM1<-subset(Fv.FM, growthT!="26"&growthT!="Ancestor"&PAR>0&assayT!=26)
#bring them in correct order
Fv.FM1$growthT <- factor(Fv.FM1$growthT, levels = c('Ancestor', '22', '26', '32', 'FS','FL'))

Fvfmplot01<-qplot(PAR,as.numeric(value),data=subset(Fv.FM1, PAR>0), colour=growthT, ylab="psiPSII",facets=growthT~assayT,shape=biorep)+
  geom_smooth(aes(group=growthT),method='lm', formula=y~poly(x,2),se=F, size=0.351) + 
  theme(legend.position='none') +
  scale_colour_manual(values=c("darkgreen","red","purple","black"))
Fvfmplot01

#now we fit the lineages per biorep-cluster
Fv.FM1$which<-ifelse(Fv.FM1$id.biorep=="b1:FL"|Fv.FM1$id.biorep=="b2:FL"|Fv.FM1$id.biorep=="b5:FL","pref 22","pref32")

Fvfmplot02<-qplot(PAR,as.numeric(value),data=subset(Fv.FM1, PAR>0), colour=growthT, ylab=expression(psi~PSII),facets=growthT~assayT,shape=biorep)+
  theme_classic()+
  geom_smooth(aes(group=which),method='lm', formula=y~poly(x,2),se=F, size=0.351) + 
  theme(legend.position='none') +
  scale_colour_manual(values=c("darkgreen","red","purple","black"))
Fvfmplot02

#or to check if we get two sep pops anyways

Fv.FM1$which2<-ifelse(Fv.FM1$biorep=="b1"|Fv.FM1$biorep=="b2"|Fv.FM1$biorep=="b5","pref 22","pref32")
Fvfmplot03<-qplot(PAR,as.numeric(value),data=subset(Fv.FM1, PAR>0), colour=growthT, ylab=expression(psi~PSII),facets=growthT~assayT,shape=biorep)+
  theme_classic()+
  geom_smooth(aes(group=which2),method='lm', formula=y~poly(x,2),se=T, size=0.351) + 
  theme(legend.position='none') +
  scale_colour_manual(values=c("darkgreen","red","purple","black"))
Fvfmplot03

###rP

rP <- subset(pfc2, variable == 'rP' &value!="NA" &value!="")
rP$growthT<-as.character(rP$growthT)
rP$generation<-as.character(rP$generation)
rP$growthT[rP$generation == "t000"] <- "Ancestor"

rP <- within(rP, id.light <- id.light <- as.character(factor(biorep):factor(growthT):factor(PAR):factor(assayT)))
qplot(PAR,as.numeric(value),data=subset(rP, PAR>0), colour=growthT, facets=growthT~assayT,shape=biorep)

rP$value<-as.numeric(rP$value)
rP <- within(rP, id.biorep  <- as.character(factor(biorep):factor(growthT):factor(assayT)))
rP$growthT<-as.factor(rP$growthT)


#also this one per replicate 
rP2<-subset(rP, assayT!=26)
rP2<-subset(rP2, PAR>0&growthT=="FS"|growthT=="FL"|growthT=="32"|growthT=="22")

rP2$which2<-ifelse(rP2$biorep=="b1"|rP2$biorep=="b2"|rP2$biorep=="b5","pref 22","pref32")

rP2$growthT <- factor(rP2$growthT, levels = c('Ancestor', '22', '32', 'FS','FL'))

rPplot2<-qplot(PAR,as.numeric(value),data=subset(rP2, PAR>0), ylab="rP",facets=growthT~assayT,shape=biorep, colour=growthT)+scale_colour_manual(values=c('darkgreen','darkred','purple','black'))+
  geom_smooth(aes(group=which2),method='lm', formula=y~poly(x,2),se=T, size=0.351) +
  theme(legend.position='top') +
  theme_classic()
rPplot2

## closed state plot 

C<- subset(pfc2,value>0&variable=="C"& value!="NA" &value!="")

C$growthT<-as.character(C$growthT)
C$generation<-as.character(C$generation)
C$growthT[C$generation == "t000"] <- "Ancestor"

C <- within(C, id.light <- id.light <- as.character(factor(biorep):factor(growthT):factor(PAR):factor(assayT)))

C$value<-as.numeric(C$value)
C<- within(C, id.biorep  <- as.character(factor(biorep):factor(growthT)))

C<-subset(C, growthT=="FS" |growthT=="FL"|growthT=="32"|growthT=="22")
C<-subset(C, assayT!=26)

C$growthT <- factor(C$growthT, levels = c( '22', '32', 'FS','FL'))

C$which2<-ifelse(C$biorep=="b1"|C$biorep=="b2"|C$biorep=="b5","pref 22","pref32")


Cplot<-qplot(PAR,as.numeric(value),data=subset(C, PAR>0&value<=.98), colour=growthT, ylab="C",facets=growthT~assayT,shape=biorep)+
  scale_colour_manual(values=c('darkgreen','darkred','purple','black'))+
  geom_smooth(aes(group=which2),method='lm', formula=y~poly(x,2),se=T, size=0.351) +
  theme(legend.position='top') +
  theme_classic()
Cplot

