1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| get_qPCR <- function(dataset=dat, ref_gene="Gadph", control_group="2d_Control", grp=c("2d_100uM")){ if(!any(is.element(colnames(dataset), c("Sample_Name", "Target_Name", "CT")))){ stop("Check the sheet's colnames") } sampleid <- c("Sample_Name", "Target_Name", "CT") dat <- dataset %>% select(sampleid) dat_ref_gene <- dat %>% filter(Target_Name == ref_gene) ref_gene_mean <- dat_ref_gene %>% group_by(Sample_Name) %>% dplyr::summarise(CT_ref_mean = mean(CT)) dat_gene <- dat %>% filter(Target_Name != ref_gene) dat_gene_merge <- dat_gene %>% inner_join(ref_gene_mean, by = "Sample_Name") dat_gene_merge$CT_delta <- with(dat_gene_merge, CT - CT_ref_mean) dat_control <- dat_gene_merge %>% filter(Sample_Name == control_group) %>% group_by(Sample_Name, Target_Name) %>% dplyr::summarise(Delta_CT_control_mean=mean(CT_delta)) %>% dplyr::rename(Sample_Name_control=Sample_Name) dat_treat <- dat_gene_merge %>% filter(Sample_Name != control_group) %>% dplyr::rename(Sample_Name_treat=Sample_Name) dat_double_delta <- inner_join(dat_treat, dat_control, by = "Target_Name") dat_double_delta$CT_delta_delta <- with(dat_double_delta, CT_delta - Delta_CT_control_mean) dat_double_delta$qPCR <- 2^-(dat_double_delta$CT_delta_delta) dat_plot <- dat_double_delta %>% dplyr::rename(Sample_Name=Sample_Name_treat) %>% dplyr::select(Sample_Name, Target_Name, qPCR) dat_plot_bar <- Rmisc::summarySE(dat_plot, measurevar = "qPCR", groupvars = c("Sample_Name", "Target_Name")) %>% mutate(Sample_Name=factor(Sample_Name, levels = grp)) pl <- ggplot(dat_plot_bar, aes(x=Sample_Name, weight=qPCR))+ geom_hline(yintercept = seq(0, round(max(dat_plot_bar$qPCR), 1), 0.2), color = "gray")+ geom_bar(color = "black", width = .4, position = "dodge")+ geom_errorbar(aes(ymin = qPCR, ymax = qPCR + se), width = 0.25, size = 0.5, position = position_dodge(0.7))+ labs(x="", y=expression(paste(log[2], " fold change in expression")))+ scale_y_continuous(breaks = seq(0, round(max(dat_plot_bar$qPCR), 1), 0.2), expand = c(0, 0), limits = c(0, round(max(dat_plot_bar$qPCR), 1)+round(max(dat_plot_bar$sd), 1)))+ facet_wrap(. ~ Target_Name, scales = "free")+ theme_bw()+ theme(axis.title = element_text(face = "bold", color = "black", size = 14), axis.text = element_text(color = "black", size = 10), axis.text.x = element_text(angle = 60, hjust = 1, face = "bold"), text = element_text(size = 10, color = "black", family="serif"), panel.grid = element_blank(), legend.position = "right", legend.key.height = unit(0.6, "cm"), legend.text = element_text(face = "bold", color = "black", size = 10), strip.text = element_text(face = "bold", size = 14)) res <- list(dat=dat_double_delta, plot=pl) return(res) }
dat <- read.xlsx("qPCR.xlsx", sheetName = "6d")
qPCR_res <- get_qPCR(dataset=dat, ref_gene="Gapdh", control_group="6d_control", grp=c("6d_100", "6d_300", "6d_1000"))
DT::datatable(qPCR_res$dat)
qPCR_res$plot
|