rm(list = ls())
###############################input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\data1\\"
files_list <- dir(dir_path, pattern = "*.",full.names = T, recursive = TRUE)
#files_list
#################################################list_1
list_1 <- read.csv(grep("compounds_list.csv",files_list,value = T),header = T,stringsAsFactors = F)
#head(list_1)
list_1 <- list_1[list_1$AC50..uM < 2 & list_1$Efficacy < -50, ]
#dim(list_1) #[1] 12  6
data_1 <- read.delim(grep("concentration_data$",files_list,value = T),header = T,stringsAsFactors = F)
#dim(data_1) [1] 381  74
data_1 <- data_1[data_1$sample_id %in% list_1$Sample.ID, ]
#length(unique(data_1$sample_id)) #12
#dim(data_1) #[1] 36 74
#################################################### 
data_2 <- data_1[,colSums(is.na(data_1)) < nrow(data_1)]
#dim(data_2) #[1] 36 45
#unique(data_2$sample_data_type)
#colnames(data_2)
data_3 <- data_2[,c(4,5,24:ncol(data_2))]
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## Warning: 程辑包'tidyr'是用R版本4.1.3 来建造的
## Warning: 程辑包'readr'是用R版本4.1.3 来建造的
## Warning: 程辑包'dplyr'是用R版本4.1.3 来建造的
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
list_2 <- seq(0,10,1)
data_list <- list()
for (i in 1:length(list_2)) {
  #i = 1
  data_4 <- data_3[,c(1,2,grep(paste0("[[:alpha:]]",list_2[i],"$"),colnames(data_3), fixed = F))]
  #print(colnames(data_4))
  #head(data_4)
  colnames(data_4) <- c("sample_data_type", "sample_id","conc","resp")
  data_list[[i]] <- data_4
}
data_all <- do.call("rbind", data_list)
#dim(data_all) #[1] 396   4
#head(data_all)
data_all <- na.omit(data_all) 
#dim(data_all) #[1] 396   4
#view(data_all)
#unique(data_all$sample_data_type)
#i = 1
#head(data_all)
####################################################
library(drc) 
## 载入需要的程辑包:MASS
## Warning: 程辑包'MASS'是用R版本4.1.3 来建造的
## 
## 载入程辑包:'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 'drc' has been loaded.
## Please cite R and 'drc' if used for a publication,
## for references type 'citation()' and 'citation('drc')'.
## 
## 载入程辑包:'drc'
## The following objects are masked from 'package:stats':
## 
##     gaussian, getInitial
library(ggplot2)
###################################################plot
#head(data_all)
#View(data_uni_1)
data_uni <- data_all
#dim(data_uni) #[1] 396   4
data_uni_1 <- aggregate(data_uni[4],list(sample_data_type = data_uni$sample_data_type,
                                         sample_id = data_uni$sample_id,
                                         conc = data_uni$conc),FUN = mean)
#dim(data_uni_1) #[1] 30  4
data_uni_1$sd <- aggregate(data_uni[4],list(sample_data_type = data_uni$sample_data_type,
                                            sample_id = data_uni$sample_id,
                                            conc = data_uni$conc),FUN = sd)$resp
#data_uni_1$sd[is.na(data_uni_1$sd)] <- 0
#####################plot
#head(data_uni_1)
#max(data_uni_1$conc)
data_uni_1$conc <- log10(data_uni_1$conc*1e-6)
#print(min(data_uni_1$resp))
#print(max(data_uni_1$resp))
#View(data_uni_1)
####################################
#dim(data_uni_1) #[1] 132   5
#length(unique(data_uni_1$sample_id)) #12
#dim(list_1)
#head(list_1)
list_1 <- list_1[order(list_1$AC50..uM ),]
data_plot_1 <- data_uni_1[data_uni_1$sample_id %in% list_1$Sample.ID[1:3], ]
data_plot_2 <- data_uni_1[data_uni_1$sample_id %in% list_1$Sample.ID[4:6], ]
data_plot_3 <- data_uni_1[data_uni_1$sample_id %in% list_1$Sample.ID[7:9], ]
data_plot_4 <- data_uni_1[data_uni_1$sample_id %in% list_1$Sample.ID[10:12], ]
#length(unique(data_plot_1$sample_id))
#head(data_plot_1)
#unique(data_plot_4$sample_id)
data_plot_1$sample_id <- gsub("-.*", "", data_plot_1$sample_id)
data_plot_2$sample_id <- gsub("-.*", "", data_plot_2$sample_id)
data_plot_3$sample_id <- gsub("-.*", "", data_plot_3$sample_id)
data_plot_4$sample_id <- gsub("-.*", "", data_plot_4$sample_id)
data_plot_1$label <- 1
data_plot_2$label <- 2
data_plot_3$label <- 3
data_plot_4$label <- 4
data_plot <- rbind(data_plot_1, data_plot_2, data_plot_3, data_plot_4)
data_plot$sample_id <- factor(data_plot$sample_id)
levels(data_plot$sample_id) <- LETTERS[1:length(unique(data_plot$sample_id))]
####################
#data_plot_4 <- data_plot_4[data_plot_4$sample_id == "NCGC00600785", ]
#p1 <- 
#data_uni_1$sample_id <- gsub("-.*", "", data_uni_1$sample_id)
library('ggsci')
#########p1
data_plot_1$sample_id <- factor(data_plot_1$sample_id)
levels(data_plot_1$sample_id) <- LETTERS[1:length(unique(data_plot_1$sample_id))]
p1 <- ggplot(data = data_plot_1,
       aes(x = conc, y = resp, group = sample_id)) + 
  geom_point(size= 2, aes(shape= sample_id, color = sample_id, 
                         fill = sample_id),
             position = "identity") + 
  geom_errorbar(aes(ymin=resp-sd, ymax=resp+sd, color = sample_id), width=.05,position = "identity") +
  geom_smooth(method = drm,aes(color =  sample_id),size = 0.5,
              method.args = list(fct = L.4()), se = F) +
  labs(title= "", x = "Log ([compound], M)",  y = "Activity (%)") +
  #scale_shape_manual(values=c(22,21)) +
  #scale_fill_manual(values=c("darkred", "gray60")) +
  #scale_color_manual(values=c("darkred", "gray60")) +
  theme(legend.position =  c(0,0.37),
        panel.background = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        panel.grid = element_blank(),
        legend.title = element_blank(),
        legend.justification = c(0, 1),
        legend.key = element_rect(colour = NA, fill = NA),
        legend.text = element_text(size= 10, color = "black",family = "sans"),
        legend.background = element_blank(),
        axis.text.x   = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        axis.text.y   = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5),
        axis.title  = element_text(size=12, color = "black",family = "sans"),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  scale_y_continuous(limits = c(min(data_uni_1$resp)-data_uni_1$sd[which.min(data_uni_1$resp)], 
                                max(data_uni_1$resp)+data_uni_1$sd[7]),
                     breaks = seq(round(min(data_uni_1$resp), digits = -1),max(data_uni_1$resp),20)) +
  scale_color_aaas()
p1
## `geom_smooth()` using formula 'y ~ x'

################p2
data_plot_2$sample_id <- factor(data_plot_2$sample_id)
levels(data_plot_2$sample_id) <- LETTERS[1:length(unique(data_plot_2$sample_id))]
p2 <- ggplot(data = data_plot_2,
             aes(x = conc, y = resp, group = sample_id)) + 
  geom_point(size= 2, aes(shape= sample_id, color = sample_id, 
                          fill = sample_id),
             position = "identity") + 
  geom_errorbar(aes(ymin=resp-sd, ymax=resp+sd, color = sample_id), width=.05,position = "identity") +
  geom_smooth(method = drm,aes(color =  sample_id),size = 0.5,
              method.args = list(fct = L.4()), se = F) +
  labs(title= "", x = "Log ([compound], M)",  y = "Activity (%)") +
  #scale_shape_manual(values=c(22,21)) +
  #scale_fill_manual(values=c("darkred", "gray60")) +
  #scale_color_manual(values=c("darkred", "gray60")) +
  theme(legend.position =  c(0,0.37),
        panel.background = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        panel.grid = element_blank(),
        legend.title = element_blank(),
        legend.justification = c(0, 1),
        legend.key = element_rect(colour = NA, fill = NA),
        legend.text = element_text(size= 10, color = "black",family = "sans"),
        legend.background = element_blank(),
        axis.text.x   = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        axis.text.y   = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5),
        axis.title  = element_text(size=12, color = "black",family = "sans"),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  scale_y_continuous(limits = c(min(data_uni_1$resp)-data_uni_1$sd[which.min(data_uni_1$resp)], 
                                max(data_uni_1$resp)+data_uni_1$sd[7]),
                     breaks = seq(round(min(data_uni_1$resp), digits = -1),max(data_uni_1$resp),20)) +
  scale_color_aaas()
p2
## `geom_smooth()` using formula 'y ~ x'

#########p3
#View(data_plot_3)
data_plot_3$sample_id <- factor(data_plot_3$sample_id)
levels(data_plot_3$sample_id) <- LETTERS[1:length(unique(data_plot_3$sample_id))]
p3 <- ggplot(data = data_plot_3,
             aes(x = conc, y = resp, group = sample_id)) + 
  geom_point(size= 2, aes(shape= sample_id, color = sample_id, 
                          fill = sample_id),
             position = "identity") + 
  geom_errorbar(aes(ymin=resp-sd, ymax=resp+sd, color = sample_id), width=.05,position = "identity") +
  geom_smooth(method = drm,aes(color =  sample_id),size = 0.5,
              method.args = list(fct = L.4()), se = F) +
  labs(title= "", x = "Log ([compound], M)",  y = "Activity (%)") +
  #scale_shape_manual(values=c(22,21)) +
  #scale_fill_manual(values=c("darkred", "gray60")) +
  #scale_color_manual(values=c("darkred", "gray60")) +
  theme(legend.position =  c(0,0.37),
        panel.background = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        panel.grid = element_blank(),
        legend.title = element_blank(),
        legend.justification = c(0, 1),
        legend.key = element_rect(colour = NA, fill = NA),
        legend.text = element_text(size= 10, color = "black",family = "sans"),
        legend.background = element_blank(),
        axis.text.x   = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        axis.text.y   = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5),
        axis.title  = element_text(size=12, color = "black",family = "sans"),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  scale_y_continuous(limits = c(min(data_uni_1$resp)-data_uni_1$sd[which.min(data_uni_1$resp)], 
                                max(data_uni_1$resp)+data_uni_1$sd[7]+10),
                     breaks = seq(round(min(data_uni_1$resp), digits = -1),max(data_uni_1$resp),20)) +
  scale_color_aaas()
p3
## `geom_smooth()` using formula 'y ~ x'

#########p4
data_plot_4$sample_id <- factor(data_plot_4$sample_id)
levels(data_plot_4$sample_id) <- LETTERS[1:length(unique(data_plot_4$sample_id))]
head(data_plot_4)
##    sample_data_type sample_id      conc       resp       sd label
## 1          activity         A -9.011762 -5.0060342 3.581235     4
## 4          activity         B -9.011762  0.9726948 9.747066     4
## 7          activity         C -9.011762 18.6829439 3.768872     4
## 13         activity         A -8.534641 -8.9805302 9.220001     4
## 16         activity         B -8.534641 -1.6017151 0.855886     4
## 19         activity         C -8.534641  9.8372298 2.054370     4
p4 <- ggplot(data = data_plot_4,
             aes(x = conc, y = resp, group = sample_id)) + 
  geom_point(size= 2, aes(shape= sample_id, color = sample_id, 
                          fill = sample_id),
             position = "identity") + 
  geom_errorbar(aes(ymin=resp-sd, ymax=resp+sd, color = sample_id), width=.05,position = "identity") +
  geom_smooth(method = drm,aes(color =  sample_id),size = 0.5,
              method.args = list(fct = L.4()), se = F) +
  labs(title= "", x = "Log ([compound], M)",  y = "Activity (%)") +
  #scale_shape_manual(values=c(22,21)) +
  #scale_fill_manual(values=c("darkred", "gray60")) +
  #scale_color_manual(values=c("darkred", "gray60")) +
  theme(legend.position = c(0,0.37),
        panel.background = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        panel.grid = element_blank(),
        legend.title = element_blank(),
        legend.justification = c(0, 1),
        legend.key = element_rect(colour = NA, fill = NA),
        legend.text = element_text(size= 10, color = "black",family = "sans"),
        legend.background = element_blank(),
        axis.text.x   = element_text(size= 12, color = "black",family = "sans",hjust = 0.5),
        axis.text.y   = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5),
        axis.title  = element_text(size=12, color = "black",family = "sans"),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  scale_y_continuous(limits = c(min(data_uni_1$resp)-data_uni_1$sd[which.min(data_uni_1$resp)], 
                                max(data_uni_1$resp)+data_uni_1$sd[7]),
                     breaks = seq(round(min(data_uni_1$resp), digits = -1),max(data_uni_1$resp),20)) +
  scale_color_aaas()

p4
## `geom_smooth()` using formula 'y ~ x'

#####################################################
library(cowplot)
plot_grid(
  p1, p2, p3, p4,ncol = 2, labels = NULL)
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'

#?plot_grid


#output pic
ggsave(filename = paste0(Sys.Date(),"-","d12.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 17.5, height = 16.5, units = "cm",dpi = 300, 
       limitsize = TRUE, compression = "lzw")