Working_directory ="T:\\TimelapseData\\201215GA12"
setwd(Working_directory)

data = read.table("allquant_201216.csv",header=TRUE, row.names=NULL, sep=";")

data = data[which(data$TimePoint==1),]

data$SumCh02 = ifelse(data$SumCh02 <=0, 0.00000001, data$SumCh02)
data$SumCh03 = ifelse(data$SumCh03 <=0, 0.00000001, data$SumCh03)

data$SumBaSiCWithRatioflatCh02 = ifelse(data$SumBaSiCWithRatioflatCh02 <=0, 0.00000001, data$SumBaSiCWithRatioflatCh02)
data$SumBaSiCWithRatioflatCh03 = ifelse(data$SumBaSiCWithRatioflatCh03 <=0, 0.00000001, data$SumBaSiCWithRatioflatCh03)

data$MeanCh02 = data$SumCh02/data$Area
data$MeanCh03 = data$SumCh03/data$Area

data$MeanBaSiCWithRatioflatCh02 = data$SumBaSiCWithRatioflatCh02/data$Area
data$MeanBaSiCWithRatioflatCh03= data$SumBaSiCWithRatioflatCh03/data$Area

data$PositionComment = as.character(data$PositionComment)
temp = strsplit(data$PositionComment,split = " ")
data$Comment = sapply(temp,"[[",1)

Mash1IRESDsRed_pos = c("B2","C2","B4","C4","B6","C6",
                       "B10","C10","F2","G2","F6","G6","F10","G10")
                   
pMash1IRESDsRed_pos = c("B3","C3","B5","C5","B7","C7",
                        "B11","C11","F3","G3","F7","G7","F11","G11")

Novirus_pos = c("F4","F5","G4","G5")

data$Virus = NA

data[data$Comment %in% Mash1IRESDsRed_pos,]$Virus = "Mash1IRESDsRed"
data[data$Comment %in% pMash1IRESDsRed_pos,]$Virus = "pMash1IRESDsRed"
data[data$Comment %in% Novirus_pos,]$Virus ="No Virus"

data$StimCond = NA

Teststim_1 = c("B2","B3","C2","C3")
Teststim_2 = c("B6","B7","C6","C7")
No_light = c("B4","B5","C4","C5","F4","F5","G4","G5")
Teststim_3 = c("B10","B11","C10","C11")
Teststim_4 = c("F2","F3","G2","G3")
Teststim_5 = c("F6","G6","F7","G7")
Teststim_6 = c("F10","F11","G10","G11")

##### 6 hr on means "1 min on 15 min off" within that 6 hr duration

data[data$Comment %in% Teststim_1,]$StimCond = "6 hr on"
data[data$Comment %in% Teststim_2,]$StimCond = "12 hr on"
data[data$Comment %in% No_light,]$StimCond = "No light"

data[data$Comment %in% Teststim_3,]$StimCond = "12 hr on 12 hr off"
data[data$Comment %in% Teststim_4,]$StimCond = "6 hr on 6 hr off"
data[data$Comment %in% Teststim_5,]$StimCond = "6 hr on 12 hr off"
data[data$Comment %in% Teststim_6,]$StimCond = "12 hr on 6 hr off"


data$StimCond = factor(data$StimCond)

Rep2_pos = c("B2","B3","B4","B5","B6","B7","B10","B11",
             "F2","F3","F4","F5","F6","F7","F10","F11")

Rep3_pos = c("C2","C3","C4","C5","C6","C7","C10","C11",
             "G2","G3","G4","G5","G6","G7","G10","G11")

data$ExpName = NA

data[data$Comment %in% Rep2_pos,]$ExpName = "Rep2"
data[data$Comment %in% Rep3_pos,]$ExpName = "Rep3"




data$SumCh02_log = log10(data$SumCh02)
data$SumCh03_log = log10(data$SumCh03)

data$MeanCh02_log = log10(data$MeanCh02)
data$MeanCh03_log = log10(data$MeanCh03)

data$SumBaSiCWithRatioflatCh02_log = log10(data$SumBaSiCWithRatioflatCh02)
data$SumBaSiCWithRatioflatCh03_log = log10(data$SumBaSiCWithRatioflatCh03)

data$MeanBaSiCWithRatioflatCh02_log = log10(data$MeanBaSiCWithRatioflatCh02)
data$MeanBaSiCWithRatioflatCh03_log = log10(data$MeanBaSiCWithRatioflatCh03)


data$Mash1 = log10(data$SumCh02)
data$mCherry = log10(data$SumCh03)

data = data[!is.na(data$StimCond),]

# data$M1 = NA
# data[which(data$SumCh02_log<=1.25),]$M1 = 0
# data[which(data$SumCh02_log>1.25),]$M1 = 1
# 
# data$mCh = NA
# data[which(data$SumCh03_log<=1.19),]$mCh = 0
# data[which(data$SumCh03_log>1.19),]$mCh = 1
# 
# data$B3 = NA
# data[which(data$SumCh04_log<=1.63),]$B3 = 0
# data[which(data$SumCh04_log>1.63),]$B3 = 1



library("ggplot2")

Mash1neg_threshold = quantile(c(data[which(data$Comment=="F4"),]$SumBaSiCWithRatioflatCh02_log,
                                data[which(data$Comment=="F5"),]$SumBaSiCWithRatioflatCh02_log),0.99)

cbbPalette<- c("#000000", "#0072B2", "#CC79A7","#F0E442","#009E73", "#E69F00", "#56B4E9", "#D55E00")

cbbPalette_mod <- c("#000000", "#E69F00", "#56B4E9", "#D55E00","#F0E442","#009E73", "#0072B2", "#CC79A7")
#Pos_tobeplotted = Novirus_pos
#Pos_tobeplotted = c("B2","B6","B10","B4","F2","F6")


Rep_tobeplotted = Rep2_pos
Pos_tobeplotted = Novirus_pos
Idx = data$Comment %in% Pos_tobeplotted & data$Comment %in% Rep_tobeplotted
#data$temp = log(data$SumBaSiCWithRatioflatCh04)

ggplot(data[Idx,], aes(x = SumBaSiCWithRatioflatCh02_log)) +
  geom_density(size=1) +
  #scale_x_log10()+
  facet_wrap(~ StimCond+Virus+Comment, scales = "fixed") +
  theme_bw() +
  coord_cartesian(xlim=c(-1,2))+
  geom_vline(xintercept = -0.06, color="#CC79A7",size=1)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        strip.text = element_text(size = 10, face ="bold"), axis.text = element_text(size = 20, face = "bold"),
        axis.title=element_text(size=14,face="bold")) +
  labs(x = "Mash1 (log transformed)")+
  scale_color_manual(values = cbbPalette_mod)


Rep_tobeplotted = Rep2_pos
Pos_tobeplotted = Mash1IRESDsRed_pos
Idx = data$Comment %in% Pos_tobeplotted & data$Comment %in% Rep_tobeplotted
#data$temp = log(data$SumBaSiCWithRatioflatCh04)

ggplot(data[Idx,], aes(x = SumBaSiCWithRatioflatCh02_log )) +
  geom_density(size=1) +
  #scale_x_log10()+
  facet_wrap(~ StimCond+Virus, scales = "fixed") +
  theme_bw() +
  coord_cartesian(xlim=c(-1,2))+
  geom_vline(xintercept = -0.065, color="#CC79A7",size=1)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        strip.text = element_text(size = 10, face ="bold"), axis.text = element_text(size = 20, face = "bold"),
        axis.title=element_text(size=14,face="bold")) +
  labs(x = "Mash1 (log transformed)")+
  scale_color_manual(values = cbbPalette_mod)


Rep_tobeplotted = Rep2_pos
Pos_tobeplotted = pMash1IRESDsRed_pos
Idx = data$Comment %in% Pos_tobeplotted & data$Comment %in% Rep_tobeplotted
#data$temp = log(data$SumBaSiCWithRatioflatCh04)

ggplot(data[Idx,], aes(x = SumBaSiCWithRatioflatCh02_log  )) +
  geom_density(size=1) +
  #scale_x_log10()+
  facet_wrap(~ StimCond+Virus, scales = "fixed") +
  theme_bw() +
  coord_cartesian(xlim=c(-1,2))+
  geom_vline(xintercept = -0.06, color="#CC79A7",size=1)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        strip.text = element_text(size = 10, face ="bold"), axis.text = element_text(size = 20, face = "bold"),
        axis.title=element_text(size=14,face="bold")) +
  labs(x = "pMash1 (log transformed)")+
  scale_color_manual(values = cbbPalette_mod)



Mash1neg_threshold = quantile(c(data[which(data$Comment=="G4"),]$SumBaSiCWithRatioflatCh02_log,
                                data[which(data$Comment=="G5"),]$SumBaSiCWithRatioflatCh02_log),0.99)

Rep_tobeplotted = Rep3_pos
Pos_tobeplotted = Mash1IRESDsRed_pos
Idx = data$Comment %in% Pos_tobeplotted & data$Comment %in% Rep_tobeplotted
#data$temp = log(data$SumBaSiCWithRatioflatCh04)

ggplot(data[Idx,], aes(x = SumBaSiCWithRatioflatCh02_log)) +
  geom_density(size=1) +
  #scale_x_log10()+
  facet_wrap(~ StimCond+Virus, scales = "fixed") +
  theme_bw() +
  coord_cartesian(xlim=c(-1,2))+
  geom_vline(xintercept = -0.2, color="#CC79A7",size=1)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        strip.text = element_text(size = 10, face ="bold"), axis.text = element_text(size = 20, face = "bold"),
        axis.title=element_text(size=14,face="bold")) +
  labs(x = "Mash1 (log transformed)")+
  scale_color_manual(values = cbbPalette_mod)


Rep_tobeplotted = Rep3_pos
Pos_tobeplotted = pMash1IRESDsRed_pos
Idx = data$Comment %in% Pos_tobeplotted & data$Comment %in% Rep_tobeplotted
#data$temp = log(data$SumBaSiCWithRatioflatCh04)

ggplot(data[Idx,], aes(x = SumCh02_log )) +
  geom_density(size=1) +
  #scale_x_log10()+
  facet_wrap(~ StimCond+Virus, scales = "fixed") +
  theme_bw() +
  coord_cartesian(xlim=c(0,2))+
  geom_vline(xintercept = 0.86, color="#CC79A7",size=1)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        strip.text = element_text(size = 10, face ="bold"), axis.text = element_text(size = 20, face = "bold"),
        axis.title=element_text(size=14,face="bold")) +
  labs(x = "pMash1 (log transformed)")+
  scale_color_manual(values = cbbPalette_mod)


Rep_tobeplotted = Rep3_pos
Pos_tobeplotted = Novirus_pos
Idx = data$Comment %in% Pos_tobeplotted & data$Comment %in% Rep_tobeplotted
#data$temp = log(data$SumBaSiCWithRatioflatCh04)

ggplot(data[Idx,], aes(x = SumCh02_log )) +
  geom_density(size=1) +
  #scale_x_log10()+
  facet_wrap(~ StimCond+Virus+Comment, scales = "fixed") +
  theme_bw() +
  coord_cartesian(xlim=c(0,2))+
  geom_vline(xintercept = 0.61, color="#CC79A7",size=1)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        strip.text = element_text(size = 10, face ="bold"), axis.text = element_text(size = 20, face = "bold"),
        axis.title=element_text(size=14,face="bold")) +
  labs(x = "Mash1 (log transformed)")+
  scale_color_manual(values = cbbPalette_mod)




temp = data[which(data$ExpName=="Rep2"),]

temp$M1 = NA
temp[which(temp$SumBaSiCWithRatioflatCh02_log<=-0.06),]$M1 = 0
temp[which(temp$SumBaSiCWithRatioflatCh02_log>-0.06),]$M1 = 1
library(dplyr)
count_data = temp %>%
  group_by(Virus,StimCond,M1) %>%
  summarise (n = n()) %>%
  mutate(freq = n / sum(n))



# count_data_doublepos = data %>%
#   group_by(Cell_Type,Condition) %>%
#   summarise (n = n()) %>%
#   mutate(freq = n / sum(n))
# 
# temp = count_data_doublepos[which(count_data_doublepos$N==0 & count_data_doublepos$S1 ==1 &
#                                     count_data_doublepos$H3b==1),]

write.csv(count_data,"count_data_Mash1posorneg.csv", row.names=FALSE)


data_TP2$M1 = NA
data_TP2[which(data_TP2$SumCh02_log<=1.15),]$M1 = 0
data_TP2[which(data_TP2$SumCh02_log>1.15),]$M1 = 1
library(dplyr)
count_data_TP2 = data_TP2 %>%
  group_by(Virus,StimCond,M1) %>%
  summarise (n = n()) %>%
  mutate(freq = n / sum(n))

write.csv(count_data_TP2,"count_data_TP2_Mash1posorneg.csv", row.names=FALSE)

