### NanoString Analysis ### # Note: before you start, replace the /.../ in file names with your working directory (i.e. the folder where you placed the data from additional file 6) # Outline ----------------------------------------------------------------- ## Set up ## # Download and load packages # Import data: NI_comparison NI_genes COMPH_comparison COMPH_raw COMPH_genes # Create vector objects for colors, factors, filters ## Create tables ## # table_a1 (long): gene, time point, FC, FDR (from PV), de_pattern, dip/peak, mi_pattern # table_a2: count, time point, up/down ## Sample Similarity -- diagnostic ## # PCA # Dendrogram ## Figure 1 ## # Bar graph: number of DEGs -- up and down regulated # Venn diagrams -- p and down regulated # Volcano plots -- FDR vs. FC per time point -- PBS group # Table for pattern of most involvement: derive from differential expressions + peaks/dips # Bar graph: count per pattern # Volcano plot: most involved days 7 & 28 ## Figure 2 ## # Bar graph: median fold change for up and down regulated genes per pathway # Bar graph: DEG count color coded (stacked) by pattern of differential expression # Bar graph: DEG count color coded (stacked) by pattern of most involvement # Volcano plots: Complement genes per time point -- PBS groups ## Figure 3 ## # Dot plot: number of reads for complement panel -- organized by complement pathway and class # Dot plot: FC -- color only statistically significant ## Figure 4 ## # Line graph: number of DEGs with CR2-Crry # Bar graph: Effect on single time points # Volcano plots # Table: effect on multiple time points # Bar graph: count # Set up --------------------------------------------------- # Load libraries (need to install first using install.packages) library(tidyverse) library(readxl) library(writexl) library(gtools) library(ggrepel) library(VennDiagram) library(grDevices) # for pdf files # Import data: comparisons <- read.csv(file = "/.../NI_comparisons.csv") str(comparisons) # check structure genes <- read_excel(path = "/.../NI_genes.xlsx") genes <- genes[, -c(2, 27)] # import without "cell type" and "human gene" str(genes) comparisons_comp <- read.csv(file = "/.../COMPH_comparisons.csv") old_mice_comp <- read.csv(file = "/.../COMPH_comparisons2.csv") # lacks some phagocytic genes pulled from NI panel comparisons_comp <- comparisons_comp %>% left_join(., old_mice_comp) str(comparisons) raw_comp <- read.csv(file = "/.../COMPH_raw.csv") str(raw) genes_comp <- read_excel(path = "/.../COMPH_genes.xlsx") genes_comp$complement_pathway <- factor(genes_comp$complement_pathway, levels = comp_pathways) genes_comp$complement_class <- factor(genes_comp$complement_class, levels = comp_class) # Create vector objects for selection and filtering natural_vector <- c("naive", "d3pbs", "d7pbs", "d28pbs") natural_comparisons_naive <- c("naiveTOd3pbs", "naiveTOd7pbs", "naiveTOd28pbs") # pbs vs naive comparisons_vector <- c("d3pbsTOd3stx", "d7pbsTOd7stx", "d28pbsTOd28stx", "d28pbsTOd28ctx") # pbs vs treated comparisons_vector2 <- c("naiveTOd3stx", "naiveTOd7stx", "naiveTOd28ctx") # treated vs naive vector_X <- c("d3pbsTOd7pbs", "d7pbsTOd28pbs", "d3pbsTOd28pbs", "naiveTOd3pbs", "naiveTOd7pbs", "naiveTOd28pbs") d3_volp_vec <- c("naiveTOd3pbs", "naiveTOd3stx", "d3pbsTOd3stx") d7_volp_vec <- c("naiveTOd7pbs", "naiveTOd7stx", "d7pbsTOd7stx") d28c_volp_vec <- c("naiveTOd28pbs", "naiveTOd28ctx", "d28pbsTOd28ctx") # Factoring day_factors <- c("Days 3 & 7", "Days 7 & 28","Days 3 & 28", "Day 3", "Day 7", "Day 28", "Always", "Noisy Pattern", "None") day_factors2 <- c("Day 3", "Days 3 & 7", "Day 7", "Days 7 & 28", "Day 28", "Days 3 & 28", "No Pattern", "Noisy Pattern", "None") sig_factors <- c("Always", "Days 3 & 7", "Days 7 & 28","Days 3 & 28", "Day 3", "Day 7", "Day 28", "None") tx_effects <- c("Lost Significance","Became Significant: Up", "Became Significant: Down", "Increased Significantly", "Decreased Significantly", "Significant: No Changes", "Not Significant: No Changes") microglia_vector <- c("whole_3dpi", "whole_7dpi", "whole_28dpi", "microglia_2dpi", "microglia_14dpi", "microglia_60dpi") comp_pathways <- c("All", "CP", "LP", "AP", "CP & LP", "TP", "C3 receptor", "C1q receptor", "Anaphylatoxins", "None") comp_class <- c("Pattern Recognition Molecule", "Enzyme-Activator", "Enzymatic Target", "Receptor", "Regulatory", "None") comp_class_counts <- c("Pattern Recognition Molecule (9)", "Enzyme-Activator (8)", "Enzymatic Target (8)", "Receptor (17)", "Regulatory (17)", "Not Complement (52)") fact_comparisons_all <- c("naiveTOd3pbs", "naiveTOd7pbs" , "naiveTOd28pbs", "d3pbsTOd7pbs", "d7pbsTOd28pbs") labs_comparisons_all <- c("Naïve vs Day 3", "Naïve vs Day 7", "Naïve vs Day 28", "Day 3 vs Day 7", "Day 7 vs Day 28") fact_comparisons_pbs <- c("naiveTOd3pbs", "naiveTOd7pbs" , "naiveTOd28pbs") fact_comp_path <- c("All", "CP", "LP", "AP", "CP & LP", "TP", "C3 receptor", "C1q receptor", "Anaphylatoxins", "None") # Color vectors day_colors <- c("#F8766D", "#00BFC4", "#C77CFF") # use scales::show_col(day_colors) to see the colors new_colors <- c("#FDB200", "#007832", "#79277A", "#D33C20", "#FF846D", "#00CCCF", "#BE97CA", "gray") my_palette <- c("#4e79a7", "#59a14f", "#9c755f", "#f28e2b", "#edc948","#b07aa1", "#e15759", "#bab0ac", "#76b7b2", "#ff9da7") scales::show_col(new_colors) # Create tables ------------------------------------------------------- # T: main_table/_tx ----------------------------------------------------------- ### Compute FDR and FDR_relaxed for comparison data (main_table) comparisons_FC <- comparisons %>% dplyr::select(-starts_with("p")) str(comparisons_FC) head(comparisons_FC[,1:5]) comparisons_PV <- comparisons %>% dplyr::select( contains("gene"), starts_with("p")) str(comparisons_PV) head(comparisons_PV[,1:5]) rm(comparisons_FDR) comparisons_FDR <- dplyr::select(comparisons_PV, gene) for (i in 2:length(comparisons_PV)){ table <- comparisons_PV %>% dplyr::select(gene, i) table <- table %>% mutate(column = p.adjust(.[,2], method = "BH")) %>% dplyr::select(gene, column) colnames(table)[2] <- str_c("FDR_", names(comparisons_FC[i])) comparisons_FDR <- left_join(x = comparisons_FDR, y = table) } length(comparisons_FDR) str(comparisons_FDR) # Compute FDR_relaxed for positive p values only comparisons_FDR_relaxed <- dplyr::select(comparisons_PV, gene) for (i in 2:length(comparisons_PV)){ table <- comparisons_PV %>% dplyr::select(gene, i) table <- table %>% filter(.[2] < 0.05) %>% mutate(column = p.adjust(.[,2], method = "BH")) %>% dplyr::select(gene, column) colnames(table)[2] <- str_c("FDR_relaxed_", names(comparisons_FC[i])) comparisons_FDR_relaxed <- left_join(x = comparisons_FDR_relaxed, y = table) comparisons_FDR_relaxed[ , i] <- if_else(is.na(comparisons_FDR_relaxed[,i]), as.numeric(1), comparisons_FDR_relaxed[,i]) } ### Turn tables to long format FC_long <- comparisons_FC %>% pivot_longer(cols = contains("TO"), values_to = "FC", names_to = c("Comparison")) PV_long <- comparisons_PV %>% pivot_longer(cols = contains("TO"), values_to = "PV", names_to = c("Comparison"), names_pattern = "p_(.*)") FDR_long <- comparisons_FDR %>% pivot_longer(cols = contains("TO"), values_to = "FDR", names_to = c("Comparison"), names_pattern = "FDR_(.*)") FDR_relaxed_long <- comparisons_FDR_relaxed %>% pivot_longer(cols = contains("TO"), values_to = "FDR_relaxed", names_to = c("Comparison"), names_pattern = "FDR_relaxed_(.*)") ### Join tables into main_table main_table <- left_join(FC_long, PV_long) %>% left_join(., FDR_long) %>% left_join(., FDR_relaxed_long) main_table$log2_FC <- gtools::foldchange2logratio(main_table$FC, base = 2) str(main_table) ### Count number of significant changes in pbs vs naive (figure 1) main_table_natural <- main_table %>% filter(Comparison %in% natural_comparisons_naive) main_table_natural$Comparison <- factor(main_table_natural$Comparison, levels = natural_comparisons_naive) main_table_natural <- main_table_natural %>% mutate(changes = case_when( FDR < 0.05 & FC > +1 ~ "Significantly Increased", FDR < 0.05 & FC < -1 ~ "Significantly Decreased", TRUE ~ "Not Changed" )) main_table_natural$changes <- factor(main_table_natural$changes, levels = c("Significantly Increased", "Significantly Decreased", "Not Changed")) str(main_table_natural) ### CR2-Crry vs PBS table (figure 4) main_table_tx <- main_table %>% filter(Comparison %in% comparisons_vector) # TX VS PBS main_table_tx$Comparison <- factor(main_table_tx$Comparison, levels = comparisons_vector) ### CR2-Crry & PBX vs Naive main_table_2 <- main_table %>% filter(Comparison %in% c(natural_comparisons_naive, comparisons_vector2)) # TX VS NAIVE main_table_2$Comparison <- factor(main_table_2$Comparison, levels = c(natural_comparisons_naive, comparisons_vector2)) main_table_2 <- main_table_2 %>% mutate(Direction = case_when(FDR < 0.05 & FC > 1 ~ "Up", FDR < 0.05 & FC < -1 ~ "Down", TRUE ~ "Not Significant")) main_table_2 <- main_table_2 %>% mutate(Day = case_when(str_detect(Comparison, "d3") ~ "3", str_detect(Comparison, "d7") ~ "7", str_detect(Comparison, "d28") ~ "28")) main_table_2 <- main_table_2 %>% mutate(Group = case_when(str_detect(Comparison, "pbs") ~ "PBS", str_detect(Comparison, "tx") ~ "CR2-Crry")) # T: patterns_table ---------------------------------------------------------- str(main_table) patterns_table <- main_table %>% pivot_wider(names_from = Comparison, values_from = c(3:7)) patterns_table <- patterns_table %>% dplyr::select(-contains("PV")) str(patterns_table) patterns_table <- patterns_table %>% mutate(sig_sign = case_when( FDR_naiveTOd3pbs < 0.05 & FDR_naiveTOd7pbs < 0.05 & FDR_naiveTOd28pbs < 0.05 ~ "+++", FDR_naiveTOd3pbs < 0.05 & FDR_naiveTOd7pbs < 0.05 & FDR_naiveTOd28pbs >= 0.05 ~ "++-", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd7pbs < 0.05 & FDR_naiveTOd28pbs < 0.05 ~ "-++", FDR_naiveTOd3pbs < 0.05 & FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd28pbs < 0.05 ~ "+-+", FDR_naiveTOd3pbs < 0.05 & FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd28pbs >= 0.05 ~ "+--", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd7pbs < 0.05 & FDR_naiveTOd28pbs >= 0.05 ~ "-+-", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd28pbs < 0.05 ~ "--+", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd28pbs >= 0.05 ~ "---", TRUE ~ "Other")) patterns_table <- patterns_table %>% mutate(FC_sign = case_when( FC_naiveTOd3pbs >= 1 & FC_naiveTOd7pbs >= 1 & FC_naiveTOd28pbs >= 1 ~ "+++", FC_naiveTOd3pbs >= 1 & FC_naiveTOd7pbs >= 1 & FC_naiveTOd28pbs <= -1 ~ "++-", FC_naiveTOd3pbs <= -1 & FC_naiveTOd7pbs >= 1 & FC_naiveTOd28pbs >= 1 ~ "-++", FC_naiveTOd3pbs >= 1 & FC_naiveTOd7pbs <= -1 & FC_naiveTOd28pbs >= 1 ~ "+-+", FC_naiveTOd3pbs >= 1 & FC_naiveTOd7pbs <= -1 & FC_naiveTOd28pbs <= -1 ~ "+--", FC_naiveTOd3pbs <= -1 & FC_naiveTOd7pbs >= 1 & FC_naiveTOd28pbs <= -1 ~ "-+-", FC_naiveTOd3pbs <= -1 & FC_naiveTOd7pbs <= -1 & FC_naiveTOd28pbs >= 1 ~ "--+", FC_naiveTOd3pbs <= -1 & FC_naiveTOd7pbs <= -1 & FC_naiveTOd28pbs <= -1 ~ "---", TRUE ~ "Other")) table(patterns_table$sig_sign, patterns_table$FC_sign) patterns_table <- patterns_table %>% mutate(sig_pattern = case_when( sig_sign == "+++" ~ "Always", sig_sign == "++-" ~ "Days 3 & 7", sig_sign == "-++" ~ "Days 7 & 28", sig_sign == "+-+" ~ "Days 3 & 28", sig_sign == "+--" ~ "Day 3", sig_sign == "-+-" ~ "Day 7", sig_sign == "--+" ~ "Day 28", sig_sign == "---" ~ "None" )) patterns_table <- patterns_table %>% mutate(Up_Down = case_when( sig_sign == "+--" & FC_naiveTOd3pbs >= 1 ~ "Up", sig_sign == "-+-" & FC_naiveTOd7pbs >= 1 ~ "Up", sig_sign == "--+" & FC_naiveTOd28pbs >= 1 ~ "Up", sig_sign == "+--" & FC_naiveTOd3pbs <= -1 ~ "Down", sig_sign == "-+-" & FC_naiveTOd7pbs <= -1 ~ "Down", sig_sign == "--+" & FC_naiveTOd28pbs <= -1 ~ "Down", TRUE ~ "None" )) patterns_table <- patterns_table %>% mutate(Peaks = case_when( sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1.5 & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 3", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1.5 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 7", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1.5 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1.5 ~ "Day 28", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1.5 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1.5 ~ "Days 3 & 7", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1.5 & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1.5 ~ "Days 7 & 28", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1.5 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1.5 ~ "Days 3 & 28", sig_sign == "+++" & FC_sign == "+++" ~ "Peakless", sig_sign == "++-" & FC_sign %in% c("+++", "++-") & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1.5 ~ "Day 3", sig_sign == "++-" & FC_sign %in% c("+++", "++-") & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1.5 ~ "Day 7", sig_sign == "++-" & FC_sign %in% c("+++", "++-") ~ "Peakless", sig_sign == "-++" & FC_sign %in% c("+++", "-++") & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 7", sig_sign == "-++" & FC_sign %in% c("+++", "-++") & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1.5 ~ "Day 28", sig_sign == "-++" & FC_sign %in% c("+++", "-++") ~ "Peakless", sig_sign == "+-+" & FC_sign %in% c("+++", "+-+") & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 3", sig_sign == "+-+" & FC_sign %in% c("+++", "+-+") & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1.5 ~ "Day 28", sig_sign == "+-+" & FC_sign %in% c("+++", "+-+") ~ "Peakless", TRUE ~ "None" )) patterns_table <- patterns_table %>% mutate(Dips = case_when( sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1.5 & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 3", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1.5 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 7", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1.5 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1.5 ~ "Day 28", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1.5 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1.5 ~ "Days 3 & 7", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1.5 & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1.5 ~ "Days 7 & 28", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1.5 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1.5 ~ "Days 3 & 28", sig_sign == "+++" & FC_sign == "---" ~ "Dipless", sig_sign == "++-" & FC_sign %in% c("---", "--+") & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1.5 ~ "Day 3", sig_sign == "++-" & FC_sign %in% c("---", "--+") & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1.5 ~ "Day 7", sig_sign == "++-" & FC_sign %in% c("---", "--+") ~ "Dipless", sig_sign == "-++" & FC_sign %in% c("---", "+--") & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 7", sig_sign == "-++" & FC_sign %in% c("---", "+--") & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1.5 ~ "Day 28", sig_sign == "-++" & FC_sign %in% c("---", "+--") ~ "Dipless", sig_sign == "+-+" & FC_sign %in% c("---", "-+-") & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1.5 ~ "Day 3", sig_sign == "+-+" & FC_sign %in% c("---", "-+-") & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1.5 ~ "Day 28", sig_sign == "+-+" & FC_sign %in% c("---", "-+-") ~ "Dipless", TRUE ~ "None" )) patterns_table <- patterns_table %>% mutate(Sig_Peaks = case_when( sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1 & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 3", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d7pbsTOd28pbs < 0.05 ~ "Day 7", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 28", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Days 3 & 7", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1 & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Days 7 & 28", sig_sign == "+++" & FC_sign == "+++" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d7pbsTOd28pbs < 0.05 ~ "Days 3 & 28", sig_sign == "+++" & FC_sign == "+++" ~ "Peakless", sig_sign == "++-" & FC_sign %in% c("+++", "++-") & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 ~ "Day 3", sig_sign == "++-" & FC_sign %in% c("+++", "++-") & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 ~ "Day 7", sig_sign == "++-" & FC_sign %in% c("+++", "++-") ~ "Peakless", sig_sign == "-++" & FC_sign %in% c("+++", "-++") & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 ~ "Day 7", sig_sign == "-++" & FC_sign %in% c("+++", "-++") & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 ~ "Day 28", sig_sign == "-++" & FC_sign %in% c("+++", "-++") ~ "Peakless", sig_sign == "+-+" & FC_sign %in% c("+++", "+-+") & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 3", sig_sign == "+-+" & FC_sign %in% c("+++", "+-+") & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 28", sig_sign == "+-+" & FC_sign %in% c("+++", "+-+") ~ "Peakless", TRUE ~ "None" )) patterns_table <- patterns_table %>% mutate(Sig_Dips = case_when( sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1 & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 3", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d7pbsTOd28pbs < 0.05 ~ "Day 7", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 28", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1 & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Days 3 & 7", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1 & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d3pbsTOd28pbs < 0.05 ~ "Days 7 & 28", sig_sign == "+++" & FC_sign == "---" & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1 & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 & FDR_d7pbsTOd28pbs < 0.05 ~ "Days 3 & 28", sig_sign == "+++" & FC_sign == "---" ~ "Dipless", sig_sign == "++-" & FC_sign %in% c("---", "--+") & FC_naiveTOd3pbs/FC_naiveTOd7pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 ~ "Day 3", sig_sign == "++-" & FC_sign %in% c("---", "--+") & FC_naiveTOd7pbs/FC_naiveTOd3pbs >= 1 & FDR_d3pbsTOd7pbs < 0.05 ~ "Day 7", sig_sign == "++-" & FC_sign %in% c("---", "--+") ~ "Dipless", sig_sign == "-++" & FC_sign %in% c("---", "+--") & FC_naiveTOd7pbs/FC_naiveTOd28pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 ~ "Day 7", sig_sign == "-++" & FC_sign %in% c("---", "+--") & FC_naiveTOd28pbs/FC_naiveTOd7pbs >= 1 & FDR_d7pbsTOd28pbs < 0.05 ~ "Day 28", sig_sign == "-++" & FC_sign %in% c("---", "+--") ~ "Dipless", sig_sign == "+-+" & FC_sign %in% c("---", "-+-") & FC_naiveTOd3pbs/FC_naiveTOd28pbs >= 1 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 3", sig_sign == "+-+" & FC_sign %in% c("---", "-+-") & FC_naiveTOd28pbs/FC_naiveTOd3pbs >= 1 & FDR_d3pbsTOd28pbs < 0.05 ~ "Day 28", sig_sign == "+-+" & FC_sign %in% c("---", "-+-") ~ "Dipless", TRUE ~ "None" )) patterns_table <- patterns_table %>% mutate(most_involved = case_when( Peaks == "Day 3" | Dips == "Day 3" ~ "Day 3", Peaks == "Day 7" | Dips == "Day 7" ~ "Day 7", Peaks == "Day 28" | Dips == "Day 28" ~ "Day 28", Peaks == "Days 3 & 7" | Dips == "Days 3 & 7" ~ "Days 3 & 7", Peaks == "Days 7 & 28" | Dips == "Days 7 & 28" ~ "Days 7 & 28", Peaks == "Days 3 & 28" | Dips == "Days 3 & 28" ~ "Days 3 & 28", sig_sign == "+--" ~ "Day 3", sig_sign == "-+-" ~ "Day 7", sig_sign == "--+" ~ "Day 28", sig_sign == "+-+" & FC_sign %in% c("++-", "+--", "--+", "-++") ~ "Noisy Pattern", # two genes that are sign +-+ left out of patterns; should I include with the one below? sig_sign == "+-+" ~ "Days 3 & 28", Peaks == "Peakless" | Dips == "Dipless" ~ "No Pattern", TRUE ~ "None" )) patterns_table <- patterns_table %>% mutate(Sig_most_involved = case_when( Sig_Peaks == "Day 3" | Sig_Dips == "Day 3" ~ "Day 3", Sig_Peaks == "Day 7" | Sig_Dips == "Day 7" ~ "Day 7", Sig_Peaks == "Day 28" | Sig_Dips == "Day 28" ~ "Day 28", Sig_Peaks == "Days 3 & 7" | Sig_Dips == "Days 3 & 7" ~ "Days 3 & 7", Sig_Peaks == "Days 7 & 28" | Sig_Dips == "Days 7 & 28" ~ "Days 7 & 28", Sig_Peaks == "Days 3 & 28" | Sig_Dips == "Days 3 & 28" ~ "Days 3 & 28", sig_sign == "+--" ~ "Day 3", sig_sign == "-+-" ~ "Day 7", sig_sign == "--+" ~ "Day 28", sig_sign == "+-+" & FC_sign %in% c("++-", "+--", "--+", "-++") ~ "Noisy Pattern", # two genes that are sign +-+ left out of patterns; should I include with the one below? sig_sign == "+-+" ~ "Days 3 & 28", Sig_Peaks == "Peakless" | Sig_Dips == "Dipless" ~ as.character(sig_pattern), TRUE ~ "None" )) patterns_table <- patterns_table %>% #PorD means peaks or dips; useful to table against significance pattern to explain most involvement mutate(PorD = case_when( Peaks == "Day 3" | Dips == "Day 3" ~ "Day 3", Peaks == "Day 7" | Dips == "Day 7" ~ "Day 7", Peaks == "Day 28" | Dips == "Day 28" ~ "Day 28", Peaks == "Days 3 & 7" | Dips == "Days 3 & 7" ~ "Days 3 & 7", Peaks == "Days 7 & 28" | Dips == "Days 7 & 28" ~ "Days 7 & 28", Peaks == "Days 3 & 28" | Dips == "Days 3 & 28" ~ "Days 3 & 28", most_involved == "Noisy Pattern" ~ "Noisy Pattern", sig_sign != "---" ~ "No Pattern", TRUE ~ "None" )) patterns_table <- patterns_table %>% #PorD means Sig_Peaks or Sig_Dips; useful to table against significance pattern to explain most involvement mutate(Sig_PorD = case_when( Sig_Peaks == "Day 3" | Sig_Dips == "Day 3" ~ "Day 3", Sig_Peaks == "Day 7" | Sig_Dips == "Day 7" ~ "Day 7", Sig_Peaks == "Day 28" | Sig_Dips == "Day 28" ~ "Day 28", Sig_Peaks == "Days 3 & 7" | Sig_Dips == "Days 3 & 7" ~ "Days 3 & 7", Sig_Peaks == "Days 7 & 28" | Sig_Dips == "Days 7 & 28" ~ "Days 7 & 28", Sig_Peaks == "Days 3 & 28" | Sig_Dips == "Days 3 & 28" ~ "Days 3 & 28", Sig_most_involved == "Noisy Pattern" ~ "Noisy Pattern", sig_sign != "---" ~ "No Pattern", TRUE ~ "None" )) # Factoring unique(patterns_table$Sig_most_involved) patterns_table$Sig_most_involved <- factor(patterns_table$Sig_most_involved, levels = day_factors) patterns_table$Sig_PorD <- factor(patterns_table$Sig_PorD, levels = day_factors2) patterns_table$sig_pattern <- factor(patterns_table$sig_pattern, levels = sig_factors) # Explore genes explore_genes <- patterns_table %>% filter(Sig_PorD == "Days 3 & 7") %>% .$gene patterns_table[,-(2:77)] %>% filter(gene %in% explore_genes) patterns_table %>% filter(gene %in% explore_genes) %>% dplyr::select(gene, str_c("FC_",vector_X)) main_table %>% filter(gene %in% explore_genes & Comparison %in% vector_X) #%>% View() main_table %>% filter(gene == "C3" & Comparison %in% vector_X) table(patterns_table$sig_pattern, patterns_table$Sig_PorD) mi_table <- table(patterns_table$sig_pattern, patterns_table$Sig_PorD) # show this table in figure and color based on most_involved # Create patterns table with only the patterns patterns_table2 <- patterns_table[,-(2:77)] patterns_table2 patterns_table2 %>% arrange(gene) %>% write_csv(., "/.../effects.csv") # T: patterns_CR2Crry ----------------------------------------------------- patterns_table <- patterns_table %>% mutate(status_tx_d3 = case_when( FDR_naiveTOd3pbs < 0.05 & FDR_naiveTOd3stx >= 0.05 ~ "Lost Significance", FDR_naiveTOd3pbs < 0.05 & FDR_relaxed_d3pbsTOd3stx < 0.05 & FC_d3pbsTOd3stx > 1 ~ "Increased Significantly", FDR_naiveTOd3pbs < 0.05 & FDR_relaxed_d3pbsTOd3stx < 0.05 & FC_d3pbsTOd3stx < -1 ~ "Decreased Significantly", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd3stx < 0.05 & FC_naiveTOd3stx > 1 ~ "Became Significant: Up", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd3stx < 0.05 & FC_naiveTOd3stx < -1 ~ "Became Significant: Down", FDR_naiveTOd3pbs >= 0.05 & FDR_naiveTOd3stx >= 0.05 ~ "Not Significant: No Changes", TRUE ~ "Significant: No Changes")) table(patterns_table$status_tx_d3) patterns_table <- patterns_table %>% mutate(status_tx_d7 = case_when( FDR_naiveTOd7pbs < 0.05 & FDR_naiveTOd7stx >= 0.05 ~ "Lost Significance", FDR_naiveTOd7pbs < 0.05 & FDR_relaxed_d7pbsTOd7stx < 0.05 & FC_d7pbsTOd7stx > 1 ~ "Increased Significantly", FDR_naiveTOd7pbs < 0.05 & FDR_relaxed_d7pbsTOd7stx < 0.05 & FC_d7pbsTOd7stx < -1 ~ "Decreased Significantly", FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd7stx < 0.05 & FC_naiveTOd7stx > 1 ~ "Became Significant: Up", FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd7stx < 0.05 & FC_naiveTOd7stx < -1 ~ "Became Significant: Down", FDR_naiveTOd7pbs >= 0.05 & FDR_naiveTOd7stx >= 0.05 ~ "Not Significant: No Changes", TRUE ~ "Significant: No Changes")) table(patterns_table$status_tx_d7) patterns_table <- patterns_table %>% mutate(status_tx_d28 = case_when( FDR_naiveTOd28pbs < 0.05 & FDR_naiveTOd28ctx >= 0.05 ~ "Lost Significance", FDR_naiveTOd28pbs < 0.05 & FDR_relaxed_d28pbsTOd28ctx < 0.05 & FC_d28pbsTOd28ctx > 1 ~ "Increased Significantly", FDR_naiveTOd28pbs < 0.05 & FDR_relaxed_d28pbsTOd28ctx < 0.05 & FC_d28pbsTOd28ctx < -1 ~ "Decreased Significantly", FDR_naiveTOd28pbs >= 0.05 & FDR_naiveTOd28ctx < 0.05 & FC_naiveTOd28ctx > 1 ~ "Became Significant: Up", FDR_naiveTOd28pbs >= 0.05 & FDR_naiveTOd28ctx < 0.05 & FC_naiveTOd28ctx < -1 ~ "Became Significant: Down", FDR_naiveTOd28pbs >= 0.05 & FDR_naiveTOd28ctx >= 0.05 ~ "Not Significant: No Changes", TRUE ~ "Significant: No Changes")) table(patterns_table$status_tx_d28) patterns_table$status_tx_d3 <- factor(patterns_table$status_tx_d3, levels = tx_effects) patterns_table$status_tx_d7 <- factor(patterns_table$status_tx_d7, levels = tx_effects) patterns_table$status_tx_d28 <- factor(patterns_table$status_tx_d28, levels = tx_effects) patterns_table_tx <- patterns_table %>% dplyr::select(gene, FC_sign, sig_sign, contains("status")) patterns_table_tx %>% filter(status_tx_d3 == "Significant: No Changes" & status_tx_d7 == "Significant: No Changes" & status_tx_d28 == "Significant: No Changes" & FC_sign == "+++") #%>% write_xlsx(., "/.../resistent_genes.xlsx") # T: Complement Panel ----------------------------------------------------- # Plot # of reads of genes from highest to lowest and color by complement class raw_comp_long <- raw_comp %>% pivot_longer(cols = contains("_"), values_to = "Reads", names_to = c("Group", "Replicate"), names_pattern = "(.*)_(\\d)" ) raw_comp_long$Group <- factor(raw_comp_long$Group, levels = unique(raw_comp_long$Group)) raw_comp_long$log10reads <- log(raw_comp_long$Reads, base = 10) raw_comp_long$log2reads<- log(raw_comp_long$Reads, base = 2) str(raw_comp_long) (arranged <- raw_comp_long %>% filter(Group == "naive") %>% arrange(Reads)) arranged <- unique(arranged$gene) # turn to vector head(arranged) raw_comp_long <- raw_comp_long %>% left_join(., genes_comp, by = c("gene" = "gene_name")) raw_comp_long$gene <- factor(raw_comp_long$gene, levels = arranged) raw_comp_long$complement_class <- factor(raw_comp_long$complement_class, levels = comp_class, labels = c(comp_class[1:5], "Not Complement")) str(raw_comp_long) # Plot FC vs genes comparisons_comp_FC <- comparisons_comp %>% dplyr::select(-starts_with("p")) str(comparisons_comp_FC) head(comparisons_comp_FC[,1:5]) comparisons_comp_PV <- comparisons_comp %>% dplyr::select( contains("gene"), starts_with("p")) str(comparisons_comp_PV) head(comparisons_comp_PV[,1:5]) rm(comparisons_comp_FDR) comparisons_comp_FDR <- dplyr::select(comparisons_comp_PV, gene) for (i in 2:length(comparisons_comp_PV)){ table <- comparisons_comp_PV %>% dplyr::select(gene, i) table <- table %>% filter(.[2] < 0.05) %>% mutate(column = p.adjust(.[,2], method = "BH")) %>% dplyr::select(gene, column) colnames(table)[2] <- str_c("FDR_", names(comparisons_comp_FC[i])) comparisons_comp_FDR <- left_join(x = comparisons_comp_FDR, y = table) comparisons_comp_FDR[ , i] <- if_else(is.na(comparisons_comp_FDR[,i]), as.numeric(1), comparisons_comp_FDR[,i]) } str(comparisons_comp_FDR) FC_comp_long <- comparisons_comp_FC %>% pivot_longer(cols = contains("TO"), values_to = "FC", names_to = c("Comparison")) PV_comp_long <- comparisons_comp_PV %>% pivot_longer(cols = contains("TO"), values_to = "PV", names_to = c("Comparison"), names_pattern = "p_(.*)") FDR_comp_long <- comparisons_comp_FDR %>% pivot_longer(cols = contains("TO"), values_to = "FDR", names_to = c("Comparison"), names_pattern = "FDR_(.*)") main_table_comp <- left_join(FC_comp_long, PV_comp_long) %>% left_join(., FDR_comp_long) main_table_comp$log2_FC <- foldchange2logratio(main_table_comp$FC, base = 2) main_table_comp$Comparison2 <- if_else(main_table_comp$FDR >= 0.05, "Not Signficant", as.character(main_table_comp$Comparison)) unique(main_table_comp$Comparison) main_table_natural_comp <- main_table_comp %>% filter(Comparison %in% c(natural_comparisons_naive, "naivey1TOy1pbs", "naivey2TOy2pbs")) main_table_natural_comp <- main_table_natural_comp %>% left_join(., genes_comp, by = c("gene" = "gene_name")) %>% filter(complement_class != "None") (sequence <- genes_comp %>% filter(complement_class != "None") %>% arrange(complement_pathway, complement_class, gene_name) %>% .$gene_name) main_table_natural_comp$gene <- factor(main_table_natural_comp$gene, levels = sequence) main_table_natural_comp$Comparison <- factor(main_table_natural_comp$Comparison, levels = c(natural_comparisons_naive, "naivey1TOy1pbs", "naivey2TOy2pbs")) main_table_natural_comp$Comparison2 <- factor(main_table_natural_comp$Comparison2, levels = c(natural_comparisons_naive, "naivey1TOy1pbs", "naivey2TOy2pbs", "Not Signficant")) main_table_natural_comp main_table_natural_comp %>% dplyr::select(gene, Comparison, FC, FDR) %>% pivot_wider(., id_cols = "gene", names_from = "Comparison", values_from = c("FC", "FDR")) %>% dplyr::select(1, 2, 7, 3, 8, 4, 9, 5, 10, 6, 11) #%>% write_xlsx(., "/.../table2.xlsx") # Aging and complement system aging_comp <- main_table_comp %>% filter(!str_detect(Comparison, "pbs") & FDR < 0.05 & str_detect(gene, "C1|C4|Ca|CD|Cf|Clu|Lair")) %>% pivot_wider(names_from = Comparison, values_from = c(FC, FDR)) %>% dplyr::select(gene, w12_to_y1 = FC_naivew12TOnaivey1, y1_to_y2 = FC_naivey1TOnaivey2) %>% pivot_longer(cols = c(w12_to_y1, y1_to_y2), names_to = "Group", values_to = "FC") %>% filter(!is.na(FC)) %>% complete(data = ., expand(., gene, Group)) aging_comp$gene <- factor(aging_comp$gene, levels = sequence) main_table_comp %>% filter(gene == "C3") aging_comp %>% ggplot(., aes(x = gene, y = Group)) + geom_tile(fill = "gray90", color = "white") + geom_tile(data = filter(aging_comp, !is.na(FC)), aes(fill = FC), color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", midpoint = 0) + theme( panel.background = element_blank(), panel.border = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", hjust = 1, size = 4), axis.text.y = element_text(size = 5), legend.position = "none" ) + coord_flip() ### T: pathways_table ---------------- pathways_table <- left_join(main_table, genes, by = c("gene" = "Gene")) pathways_table <- pathways_table %>% pivot_longer(cols = c(8:length(pathways_table)), values_to = "Pathways_value", names_to = c("Pathways")) %>% filter(Pathways_value == "+") %>% dplyr::select(-contains("value")) pathways_table_natural <- pathways_table %>% filter (Comparison %in% natural_comparisons_naive) pathways_table_natural$Comparison <- factor(pathways_table_natural$Comparison, levels = natural_comparisons_naive, labels = c("Day 3", "Day 7", "Day 28")) pathways_table_natural <- pathways_table_natural %>% left_join(., patterns_table2) # Sample Similarity ------------------------------------------------------- # 1.A. Plotting PCA library(FactoMineR) library(factoextra) library(paran) library(pheatmap) head(raw[, 1:5]) str(raw) traw <- as.data.frame(t(raw[, -1])) colnames(traw) <- raw[, 1] str(traw) traw[1:10, 1:5] # Computing PCA with {FactoMineR} raw_pca <- FactoMineR::PCA(raw[,-1], axes = c(1, 2)) raw_pca$eig # how much variance PCs explain raw_pca$var$cos2 # quality of representation of variables by a dimension raw_pca$var$contrib # contribution of variables to PCs dimdesc(raw_pca) # correlation of variables (samples to a PC) traw_pca <- FactoMineR::PCA(traw) traw_pca$eig traw_pca$var$cos2 traw_pca$var$contrib dimdesc(traw_pca) head(dimdesc(traw_pca)$Dim.1$quanti, n = 32) # Visualizing PCA stuff with {factoextra} fviz_pca_var(traw_pca, col.var = "contrib", gradient.cols = c("#bb2e00", "#002bbb"), repel = FALSE) fviz_pca_var(traw_pca, select.var = list(contrib = 10), # can select top ___ contributors col.var = "contrib", gradient.cols = c("#bb2e00", "#002bbb"), repel = TRUE) fviz_contrib(traw_pca, choice = "var", axes = 1, top = 10) fviz_cos2(traw_pca, choice = "ind", axes = 1, top = 10) fviz_pca_ind(traw_pca, repel = TRUE) fviz_pca_ind(traw_pca, col.ind="cos2", gradient.cols = c("#bb2e00", "#002bbb"), repel = TRUE) fviz_pca_biplot(traw_pca, col.ind = "red", col.var = "cos2") (samples <- colnames(raw[,-1])) (group1 <- as.character(unique(raw_long$Group))) (group <- rep(group1, each = 3)) samples_groups <- data.frame(samples, group) samples_groups$group <- factor(samples_groups$group, levels = group1) str(samples_groups) ### fviz_pca_ind(traw_pca, repel = TRUE, col.ind = samples_groups$group, axes = c(1, 4)) + ggtitle(label = "Principal Component Analysis") + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30, margin = margin(t = 20, b = 20)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), axis.text.x = element_text(face = "bold", color = "black", size = 10), axis.text.y = element_text(face = "bold", color = "black", size = 10)) # Choosing number of PCs x <- paran(x = traw_pca$eig, iterations = 5000, graph = TRUE, legend = FALSE) # Parallel test to chooce PCs fviz_screeplot(traw_pca) # Elbow/Scree test to choose PCs # 1.B. Plotting heatmap of sample correlation and sample_genes samples_correlation <- as.data.frame(raw[, -1]) # remove gene names samples_correlation <- t(samples_correlation) # transpose table samples_correlation <- scale(samples_correlation, center = FALSE, scale = TRUE) # Should I center? samples_correlation <- as.data.frame(t(samples_correlation)) # transpose table again str(samples_correlation) (samples_correlation2 <- cor(samples_correlation, method = "spearman", use = "all.obs")) ### pheatmap(samples_correlation2, cluster_cols = FALSE, cluster_rows = FALSE, main = "Samples Correlation Matrix", fontsize = 12) # scaled data ### pheatmap(raw[,-1], scale = "row", cluster_cols = T, show_rownames = F, cellwidth = 30, cellheight = 0.8, treeheight_col = 50, treeheight_row = 100) # Figure 1 ---------------------------------------------------------------- ### Draw bar graph DEG_counts_pbs <- main_table_natural %>% group_by(Comparison, changes) %>% count() DEG_bar <- DEG_counts_pbs %>% filter(changes != "Not Changed") %>% ggplot(.) + geom_bar(aes(x = Comparison, y = n, alpha = changes, fill = Comparison), position = "dodge", stat = "identity") + geom_text(aes(x = Comparison, y = n, label = n), size = 2, position = position_dodge2(width = 0.92), vjust = -0.4) + ggtitle("Number of Differentially Expressed Genes") + ylab("Number of Genes") + theme_bw() + theme(plot.title = element_blank(), # plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 10, b = 10)), axis.title.x = element_blank(), #axis.title.y = element_text(face = "bold", size = 20, margin = margin(r = 10, l = 5)), axis.title.y = element_blank(), axis.text.y = element_text(face = "bold", color = "black", size = 5, margin = margin(r = 5, l = 5)), axis.text.x = element_text(face = "bold", color = "black", size = 6, margin = margin(t = 10, b = 5)), # legend.position = "top", legend.position = "none", legend.text = element_text(size = 12), legend.title = element_text(size = 15, face = "bold", hjust = 0.5), legend.background = element_rect(fill="gray90", size = 0.5, linetype="solid", colour ="black"), legend.key.height = unit(0.8, "lines")) + guides(#fill = guide_legend(override.aes = list(size=3), title = "Legend", nrow = 1, order = 1), alpha = guide_legend(title = "Legend")) + scale_x_discrete(labels = c("Day 3", "Day 7", "Day 28")) + scale_y_continuous(expand = c(0,0), limits = c(0, 350)) + scale_fill_manual(values = c("#F8766D", "#00BFC4", "#C77CFF"), labels = c("Day 3", "Day 7", "Day 28"), guide = "none") + scale_alpha_manual(values = c(1, 0.5), labels = c("Upregulated", "Downregulated")) # pdf(file="/.../DEGs_bar.pdf", width = 2.36, height = 1.96) # grid.draw(DEG_bar) # dev.off() ### Draw Venn Diagram of common up and down changes UPday3 <- main_table_natural %>% filter(changes == "Significantly Increased") %>% filter(Comparison == "naiveTOd3pbs") %>% .$gene UPday7 <- main_table_natural %>% filter(changes == "Significantly Increased") %>% filter(Comparison == "naiveTOd7pbs") %>% .$gene UPday28 <- main_table_natural %>% filter(changes == "Significantly Increased") %>% filter(Comparison == "naiveTOd28pbs") %>% .$gene DOWNday3 <- main_table_natural %>% filter(changes == "Significantly Decreased") %>% filter(Comparison == "naiveTOd3pbs") %>% .$gene DOWNday7 <- main_table_natural %>% filter(changes == "Significantly Decreased") %>% filter(Comparison == "naiveTOd7pbs") %>% .$gene DOWNday28 <- main_table_natural %>% filter(changes == "Significantly Decreased") %>% filter(Comparison == "naiveTOd28pbs") %>% .$gene venn1 <- venn.diagram( x = list(Day_3 = UPday3, Day_7 = UPday7, Day_28 = UPday28), filename = NULL, main = "Upregulated", main.cex = 2, fill = day_colors, col = day_colors, lwd = c(2, 2, 2), lty = "blank", cex = 1.2, euler.d = F, scaled = F, category.names = c("Day 3" , "Day 7" , "Day 28"), cat.dist = c(0.045, 0.05, 0.035), cat.cex = 1.5, margin = 0.03) # pdf(file="/.../VennUp.pdf") # grid.draw(venn11) # dev.off() venn2 <- venn.diagram( x = list(Day_3 = DOWNday3, Day_7 = DOWNday7, Day_28 = DOWNday28), filename = NULL, main = "Downregulated", main.cex = 2, fill = day_colors, col = day_colors, lwd = c(2, 2, 2), lty = "blank", cex = 1.2, euler.d = F, scaled = F, category.names = c("Day 3" , "Day 7" , "Day 28"), cat.dist = c(0.045, 0.05, 0.035), cat.cex = 1.5, margin = 0.03) # pdf(file="/.../VennDown.pdf") # grid.draw(venn2) # dev.off() ### Draw volcano plot for day 3 vp3_colored <- ggplot(patterns_table, aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs))) + geom_point(aes(color = sig_pattern), size = 0.3) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + ggtitle(label = "Day 3") + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_color_manual(values = new_colors) + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 8, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="gray90", size = 0.2, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Legend", override.aes = list(size=3), nrow = 1)) + geom_text_repel(data = dplyr::filter(patterns_table, ((FC_naiveTOd3pbs > 6 & FDR_naiveTOd3pbs < 0.05))), aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = sig_pattern, label = gene), nudge_x = 4, nudge_y = 0.5, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 1, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table, (FC_naiveTOd3pbs < -2)), aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = sig_pattern, label = gene), nudge_x = -1, nudge_y = 0.1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 1, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) # pdf(file="/.../vp3_recolored.pdf", width = 2.36, height = 2.36) # grid.draw(vp3_colored) # dev.off() ### Draw volcano plot for day 7 vp7_colored <- ggplot(patterns_table, aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs))) + geom_point(aes(color = sig_pattern), size = 0.3) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + #scale_color_manual(values = c("Gray50", "black")) + #scale_shape_manual(values = c(16, 4, 17, 6, 17, 6)) + ggtitle(label = "Day 7") + scale_color_manual(values = new_colors) + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 8, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="gray90", size = 0.2, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Legend", override.aes = list(size=3), nrow = 1)) + geom_text_repel(data = dplyr::filter(patterns_table, ((FC_naiveTOd7pbs > 8 & FDR_naiveTOd7pbs < 0.05))), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = sig_pattern, label = gene), nudge_x = 2.5, nudge_y = -0.1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 5, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table, (FC_naiveTOd7pbs < -2)), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = sig_pattern, label = gene), nudge_x = -1, nudge_y = 0.1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 1, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) # pdf(file="/.../vp7_recolored.pdf", width = 2.36, height = 2.36) # grid.draw(vp7_colored) # dev.off() ### Draw volcano plot for day 28 vp28_colored <- ggplot(patterns_table, aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs))) + geom_point(aes(color = sig_pattern), size = 0.3) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + #scale_color_manual(values = c("Gray50", "black")) + #scale_shape_manual(values = c(16, 4, 17, 6, 17, 6)) + ggtitle(label = "Day 28") + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_color_manual(values = new_colors) + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 8, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="gray90", size = 0.2, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Legend", override.aes = list(size=3), nrow = 1)) + geom_text_repel(data = dplyr::filter(patterns_table, ((FC_naiveTOd28pbs > 4 & FDR_naiveTOd28pbs < 0.05))), aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = sig_pattern, label = gene), nudge_x = 3.5, nudge_y = -0.1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 1, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table, (FC_naiveTOd28pbs < -2)), aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = sig_pattern, label = gene), nudge_x = -1, nudge_y = 0.1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 1, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) # pdf(file="/.../vp28_recolored.pdf", width = 2.36, height = 2.36) # grid.draw(vp28_colored) # dev.off() ### Draw volcano plot for most changed on days 3 & 7 mi_d37 <- ggplot(patterns_table, aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs))) + geom_point(color = "gray50", size = 0.3) + geom_point(data = filter(patterns_table, Sig_PorD == "Days 3 & 7"), color = "forestgreen", size = 0.3) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + #scale_color_manual(values = c("Gray50", "black")) + #scale_shape_manual(values = c(16, 4, 17, 6, 17, 6)) + ggtitle(label = "Day 7") + scale_color_manual(values = new_colors) + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 8, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="gray90", size = 0.2, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Legend", override.aes = list(size=3), nrow = 1)) + geom_text_repel(data = dplyr::filter(patterns_table, Sig_PorD == "Days 3 & 7" & FC_naiveTOd7pbs > 1), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), label = gene), color = "forestgreen", nudge_x = 1.5, nudge_y = -0.1, size = 1.1, max.overlaps = Inf, box.padding = 0.5, point.padding = 0.1, force = 5, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) pdf(file="/.../mi_d37.pdf", width = 2.36, height = 2.36) grid.draw(mi_d37) dev.off() # Figure 2 -------------------------------------------------------- fig2_table <- pathways_table_natural %>% dplyr::select(Pathways, gene, Comparison, FC, FDR = FDR, Significance = sig_pattern) %>% mutate(Direction = case_when(FC <= -1 ~ "Down", FC >= +1 ~ "Up")) fig2_table2 <- fig2_table %>% filter(FDR < 0.05) %>% group_by(Pathways, Comparison, Direction) %>% summarize(medianFC = median(FC), meanFC = mean(FC), count_all = n()) %>% mutate(logmedianFC = foldchange2logratio(medianFC)) positions <- fig2_table %>% group_by(Pathways, Comparison) %>% summarize(count = n()) %>% ungroup() %>% mutate(position = row_number()) %>% dplyr::select(1, 2, 4) fig2_table2 <- left_join(fig2_table2, positions) %>% arrange(Pathways, Comparison, Direction) fig2_table2 <- fig2_table2 %>% mutate(count_all2 = case_when( medianFC < 0 ~ -count_all, TRUE ~ count_all)) fig2_table2$Comparison <- factor(fig2_table2$Comparison, levels = c("Day 28", "Day 7", "Day 3")) # Plot counts color coded by day ggplot(data = fig2_table2) + geom_bar(aes(x = reorder(Pathways, desc(Pathways)), y = count_all2, fill = Comparison), stat = "identity", position = "dodge") + geom_hline(yintercept = 0, linetype = "solid", color = "black") + coord_flip() + theme_bw() + ggtitle(label = "") + ylab ("Fold Change") + xlab("") + #scale_y_continuous(breaks = seq(-1, 3, 1), labels = c("-2", "±1", "2", "4", "8"), limits = c(-1.7, 3), expand = c(0,0)) + scale_fill_manual(values = c("#C77CFF", "#00BFC4", "#F8766D")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 8, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 8, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5.5), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold"), legend.position = "none", strip.text.x = element_text(size = 7, color = "Black", face = "bold"), strip.background = element_rect(colour = "black", fill = "gray90")) # Prepare table for significance patterns # Calculate median, mean, and count per significance pattern fig2_table3 <- fig2_table %>% filter(FDR < 0.05) %>% group_by(Pathways, Comparison, Direction, Significance) %>% summarize(medianFC = median(FC), meanFC = mean(FC), count_sig = n()) %>% mutate(logmedianFC = foldchange2logratio(medianFC)) # Transform median into median*percentage of a pattern for stacking in bar graph fig2_table4 <- left_join(fig2_table2[,c(1:4, 6:7)], fig2_table3[,c(1:4, 7)]) %>% dplyr::select(1, 2, 3, 7, 4, 6, 5, 8) %>% mutate(percent_sig = (count_sig/count_all)*100) %>% mutate(perXlogmedian = percent_sig*logmedianFC*0.01) fig2_table4 <- left_join(fig2_table4, positions) %>% mutate(count_sig2 = case_when( medianFC < 0 ~ -count_sig, TRUE ~ count_sig)) # Prepare table for most involvement patterns # Calculate median, mean, and count per most_involved pattern fig2_table3.2 <- fig2_table %>% filter(FDR < 0.05) %>% group_by(Pathways, Comparison, Direction, most_involved) %>% summarize(medianFC = median(FC), meanFC = mean(FC), count_sig = n()) %>% mutate(logmedianFC = foldchange2logratio(medianFC)) # Transform median into median*percentage of a pattern for stacking in bar graph fig2_table4.2 <- left_join(fig2_table2[,c(1:4, 6:7)], fig2_table3.2[,c(1:4, 7)]) %>% dplyr::select(1, 2, 3, 7, 4, 6, 5, 8) %>% mutate(percent_sig = (count_sig/count_all)*100) %>% mutate(perXlogmedian = percent_sig*logmedianFC*0.01) fig2_table4.2 <- left_join(fig2_table4.2, positions) %>% mutate(count_sig2 = case_when( medianFC < 0 ~ -count_sig, TRUE ~ count_sig)) # Plot medians color coded by timepoint fig2_medianFC <- ggplot(data = fig2_table2) + # Goes in the figure (median FC) geom_vline(xintercept = seq( -71, -2, by = 3), color = "gray95", linetype = "solid", size = 8.2) + geom_bar(aes(x = -position, y = logmedianFC, fill = Comparison), stat = "identity", width = 0.7) + geom_hline(yintercept = 0, linetype = "solid", color = "black", size = 0.5) + coord_flip() + theme_classic() + ggtitle(label = "") + ylab ("Fold Change") + xlab("") + scale_y_continuous(breaks = seq(-1, 3, 1), labels = c("-2", "±1", "2", "4", "8"), limits = c(-1.7, 3.2), expand = c(0,0)) + scale_x_continuous(expand = c(0,0)) + scale_fill_manual(values = c("#C77CFF", "#00BFC4", "#F8766D")) + theme(plot.title = element_blank(), axis.title = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.position = "none") # pdf(file="/.../fig2_medianFC.pdf", width = 1.34, height = 6.62) # grid::grid.draw(fig2_medianFC) # dev.off() # Plot counts color coded by significance pattern fig2_sig <- ggplot(data = fig2_table4) + # goes in the figure (count per significance pattern) geom_vline(xintercept = seq( -71, -2, by = 3), color = "gray95", linetype = "solid", size = 8.2) + #geom_vline(xintercept = seq( -69.5, -3.5, by = 3), color = "gray90", linetype = "solid", size = 2) + geom_bar(aes(x = -position, y = count_sig2, fill = Significance), stat = "identity", position = "stack", width = 0.7) + geom_hline(yintercept = 0, linetype = "solid", color = "black", size = 0.5) + coord_flip() + theme_classic() + ggtitle(label = "") + ylab ("Fold Change") + xlab("") + scale_y_continuous(breaks = seq(-40, 120, 40), labels = c("40", "0", "40", "80", "120"), limits = c(-50, 130), expand = c(0,0)) + scale_x_continuous(expand = c(0,0)) + scale_fill_manual(values = new_colors) + scale_color_manual(values = c("#F8766D", "#00BFC4", "#C77CFF")) + theme(plot.title = element_blank(), axis.title = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.position = "none") # pdf(file="/.../fig2_sig.pdf", width = 1.34, height = 6.62) # grid::grid.draw(fig2_sig) # dev.off() # Figure 3 ---------------------------------------------------------------- # Plot # of reads of genes from highest to lowerst and color by complement class raw_comp_long %>% filter(Group == "naive" & complement_class!= "Not Complement") %>% ggplot(.) + geom_point(aes(x = reorder(gene, desc(gene)), y =log10reads, color = complement_class), alpha = 1, size = 3) + ggtitle(label = "Range of Complement Gene Expression in Naïve Brains") + xlab("Genes (Total = 59)") + ylab ("# of Reads (log10)") + scale_y_continuous(breaks = seq(0, 4, 1), labels = c("0", "10", "100", "1000", "10000")) + theme_bw() + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", color = "black"), legend.text = element_text(size = 10), legend.title = element_text(size = 12, face = "bold"), legend.position = "top", legend.background = element_rect(fill="gray90", size = 0.5, linetype="solid", colour ="black") ) + guides(color = guide_legend(override.aes = list(size=3), nrow = 1, title = "Complement Protein Type")) + scale_color_manual(values = c(my_palette[1:6], "gray")) # Plot # of reads vs genes arranged by path, class and gene name (sequence <- genes_comp %>% filter(complement_class != "None") %>% arrange(complement_pathway, complement_class, gene_name) %>% .$gene_name) raw_comp_long$gene <- factor(raw_comp_long$gene, levels = sequence) raw_comp_long$complement_class <- factor(raw_comp_long$complement_class,levels = comp_class) CS_reads <- raw_comp_long %>% filter(Group == "naive" & !is.na(gene)) %>% ggplot(.) + geom_point(aes(x = gene, y = log10reads, shape = complement_class), size = 1, color = "black", fill = "black", alpha = 0.7) + geom_hline(yintercept = 0, linetype = "dashed") + ggtitle(label = "Gene Expression in TBI Brains (Compared to Naïve)") + theme_bw() + xlab("Genes (Total = 111)") + ylab ("Count") + labs(color = "Group") + scale_y_continuous(breaks = seq(1, 4, 1), labels = c("10", "100", "1000", "10000")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 0, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 5, b = 5)), axis.title.y = element_text(face = "bold", size = 8, margin = margin(r = 0, l = 0)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", color = "black", size = 4), legend.text = element_text(size = 6), legend.title = element_text(size = 5, face = "bold"), legend.position = "none", legend.background = element_rect(fill="white", size = 0.5, linetype="solid", colour ="black") ) + guides(color = guide_legend(override.aes = list(size=3), nrow = 1)) + scale_shape_manual(values = c(21:25)) pdf(file="/.../CS_reads.pdf", width = 7.2, height = 2) grid.draw(CS_reads) dev.off() # Plot FC vs genes for 3 timepoints CS_FC <- ggplot(main_table_natural_comp) + geom_point(aes(x = gene, y = log2_FC, color = Comparison2, fill = Comparison2, shape = complement_class), size = 1) + geom_hline(yintercept = 0, linetype = "dashed") + ggtitle(label = "Gene Expression in TBI Brains (Compared to Naïve)") + theme_bw() + xlab("Genes (Total = 111)") + ylab ("Fold Change (vs Naïve)") + labs(color = "Group") + scale_y_continuous(breaks = seq(-2, 7, 1), labels = c("-4", "-2", "±1", "2", "4", "8", "16", " 32", " 64", " 128"), limits = c(-1, 7)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 0, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 5, b = 5)), axis.title.y = element_text(face = "bold", size = 8, margin = margin(r = 0, l = 0)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", color = "black", size = 4), legend.text = element_text(size = 6), legend.title = element_text(size = 5, face = "bold"), legend.position = "none", legend.background = element_rect(fill="white", size = 0.5, linetype="solid", colour ="black") ) + guides(color = guide_legend(override.aes = list(size=3), nrow = 1)) + scale_color_manual(values = c("#F8766D", "#00BFC4", "#C77CFF", "gray"), labels = c("Day 3", "Day 7", "Day 28", "Not Significant")) + scale_fill_manual(values = c("#F8766D", "#00BFC4", "#C77CFF", "gray"), labels = c("Day 3", "Day 7", "Day 28", "Not Significant")) + scale_shape_manual(values = c(21:25)) + scale_alpha_manual(values = c(1, 1, 1, 0.7), guide = "none") # pdf(file="/.../CS_FC.pdf", width = 7.2, height = 2) # grid.draw(CS_FC) # dev.off() # Plot FC vs genes for 5 timepoints CS_FC <- ggplot(main_table_natural_comp) + geom_point(aes(x = gene, y = log2_FC, color = Comparison2, fill = Comparison2, shape = complement_class), size = 1) + geom_hline(yintercept = 0, linetype = "dashed") + ggtitle(label = "Gene Expression in TBI Brains (Compared to Naïve)") + theme_bw() + xlab("Genes (Total = 111)") + ylab ("Fold Change (vs Naïve)") + labs(color = "Group") + scale_y_continuous(breaks = seq(-2, 7, 1), labels = c("-4", "-2", "±1", "2", "4", "8", "16", " 32", " 64", " 128"), limits = c(-1, 7)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 0, b = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 8, margin = margin(t = 5, b = 5)), axis.title.y = element_text(face = "bold", size = 8, margin = margin(r = 0, l = 0)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", color = "black", size = 4), legend.text = element_text(size = 6), legend.title = element_text(size = 5, face = "bold"), legend.position = "none", legend.background = element_rect(fill="white", size = 0.5, linetype="solid", colour ="black") ) + guides(color = guide_legend(override.aes = list(size=3), nrow = 1)) + scale_color_manual(values = c("#F8766D", "#00BFC4", "#C77CFF", "forestgreen", "blue","gray"), labels = c("Day 3", "Day 7", "Day 28","1 year", "2 years", "Not Significant")) + scale_fill_manual(values = c("#F8766D", "#00BFC4", "#C77CFF", "forestgreen", "blue", "gray"), labels = c("Day 3", "Day 7", "Day 28", "1 year", "2 years", "Not Significant")) + scale_shape_manual(values = c(21:25)) + scale_alpha_manual(values = c(1, 1, 1, 0.7), guide = "none") pdf(file="/.../CS_FC.pdf", width = 7.2, height = 2) grid.draw(CS_FC) dev.off() # Figure 4 ---------------------------------------------------------------- ### Bar graph -- DEG count with treatment count_tx <- main_table_2 %>% group_by(Group, Day, Direction) %>% count() %>% dplyr::select(Count = n, everything()) %>% filter(Direction != "Not Significant") count_tx$Day <- factor(count_tx$Day, levels = c("3", "7", "28")) barplot_tx <- ggplot(count_tx) + geom_line(aes(x = as.numeric(Day), y = Count, linetype = Direction, color = Group), size = 0.2) + geom_point(aes(x = as.numeric(Day), y = Count, color = Group), size = 0.3) + geom_text(aes(x = as.numeric(Day), y = Count, color = Group, label = Count), vjust = -0.5, hjust = -0.5, size = 1.3) + #geom_text(data = filter(count_tx, Count ==29), aes(x = as.numeric(Day), y = Count, color = Group, label = Count), vjust = 0.7, hjust = -0.5, size = 1.3) + ggtitle(label = "Number of Differentially Expressed Genes ± CR2-Crry") + xlab("Day") + ylab ("Count") + labs(color = "Group") + theme_classic() + #scale_y_continuous(breaks = seq(0, 400, 100), labels = seq(0, 400, 100), limits = c(0, 400)) + scale_x_continuous(breaks = seq(1,3,1), labels = c("3", "7", "28"), limits = c(1, 3)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5, b = 5)), axis.title.x = element_text(face = "bold", size = 6, margin = margin(t = 5, b = 5)), axis.title.y = element_text(face = "bold", size = 6, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 5), legend.position = "top", legend.title = element_text(size = 6, face = "bold"), panel.grid.minor = element_blank(), #legend.position = c(0.064, 0.82) ) + guides(color = guide_legend(override.aes = list(size=3))) + scale_color_discrete(#values = c("#F8766D", "#00BFC4", "#C77CFF"), labels = c("PBS", "STX", "CTX")) + scale_linetype_manual(values = c("solid", "dashed")) # pdf(file="/.../barplot_tx.pdf", width = 3.5, height = 3.2) # grid.draw(barplot_tx) # dev.off() ### Draw volcano plot for CR2-Crry effects on day 3 vp3_tx <- ggplot(patterns_table) + geom_point(aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = status_tx_d3, shape = status_tx_d3), size = 0.3, stroke = 0.1) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + scale_color_manual(values = c("Red", "forestgreen", "forestgreen", "Purple", "Purple", "Gray50", "Gray50")) + scale_shape_manual(values = c(4, 17, 6, 17, 6, 16, 16)) + ggtitle(label = "Day 3 + CR2-Crry") + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="white", size = 0.2, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Legend", override.aes = list(size=3), nrow = 1)) + geom_text_repel(data = dplyr::filter(patterns_table, (abs(FC_naiveTOd3pbs) > 1.5 & status_tx_d3 %in% "Lost Significance")), aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = status_tx_d3, label = gene), nudge_x = -3, nudge_y = 2.5, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table,(abs(FC_naiveTOd3stx) > 1.5 & status_tx_d3 == "Became Significant: Up")), aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = status_tx_d3, label = gene), nudge_x = 2, nudge_y = -1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table,(abs(FC_naiveTOd3stx) > 1.5 & status_tx_d3 == "Became Significant: Down")), aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = status_tx_d3, label = gene), nudge_x = -2, nudge_y = -1, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table, (abs(FC_naiveTOd3pbs) > 4 & status_tx_d3 %in% c("Increased Significantly", "Decreased Significantly"))), aes(x = log2_FC_naiveTOd3pbs, y = -log10(FDR_naiveTOd3pbs), color = status_tx_d3, label = gene), nudge_x = 0.2, nudge_y = 0.2, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) # pdf(file="/.../vp3_tx.pdf", width = 2.36, height = 2.36) # grid.draw(vp3_tx) # dev.off() ### Draw volcano plot for CR2-Crry effects on day 7 vp7_tx <- ggplot(patterns_table) + geom_point(aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = status_tx_d7, shape = status_tx_d7), size = 0.3, stroke = 0.1) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + scale_color_manual(values = c("Red", "forestgreen", "forestgreen", "Purple", "Purple", "Gray50", "Gray50")) + scale_shape_manual(values = c(4, 17, 6, 17, 6, 16, 16)) + ggtitle(label = "Day 7 ± single CR2-Crry") + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 5)), axis.title.x = element_blank(), #element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="white", size = 0.5, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Effect of CR2-Crry"), shape = guide_legend(title = "Effect of CR2-Crry")) + geom_text_repel(data = dplyr::filter(patterns_table, (abs(FC_naiveTOd7pbs) > 1.5 & status_tx_d7 %in% "Lost Significance")), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = status_tx_d7, label = gene), nudge_x = -2.5, nudge_y = 0.8, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table,(abs(FC_naiveTOd7stx) > 1.5 & status_tx_d7 == "Became Significant: Up")), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = status_tx_d7, label = gene), nudge_x = 1, nudge_y = -0.8, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table,(abs(FC_naiveTOd7stx) > 1.5 & status_tx_d7 == "Became Significant: Down")), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = status_tx_d7, label = gene), nudge_x = -1, nudge_y = -0.5, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table, (abs(FC_naiveTOd7pbs) > 4 & status_tx_d7 %in% c("Increased Significantly", "Decreased Significantly"))), aes(x = log2_FC_naiveTOd7pbs, y = -log10(FDR_naiveTOd7pbs), color = status_tx_d7, label = gene), nudge_x = 3, nudge_y = 0, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) # pdf(file="/.../vp7_tx.pdf", width = 2.36, height = 2.36) # grid.draw(vp7_tx) # dev.off() ### Draw volcano plot for CR2-Crry effects on day 28 vp28_tx <- ggplot(patterns_table) + geom_point(aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = status_tx_d28, shape = status_tx_d28), size = 0.3, stroke = 0.1) + geom_hline(yintercept = -log(0.05, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + geom_hline(yintercept = -log(0.01, 10), linetype = "solid", color = "black", size = 0.5, alpha = 0.2) + scale_color_manual(values = c("Red", "forestgreen", "forestgreen", "Purple", "Purple", "Gray50", "Gray50")) + scale_shape_manual(values = c(4, 17, 6, 17, 6, 16, 16)) + ggtitle(label = "Day 28 ± continuous CR2-Crry") + xlab("Fold Change") + ylab ("FDR") + theme_classic() + scale_x_continuous(breaks = seq(-3, 5, 1), labels = c("-8", "-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-3, 6.2)) + scale_y_continuous(breaks = c(-log10(0.05),- log10(0.01)), labels = c("0.05", "0.01")) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9), axis.title.x = element_blank(), #element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5), axis.text.x= element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 8), legend.title = element_text(size = 10, face = "bold", hjust = 0.5), legend.position = "none", legend.background = element_rect(fill="white", size = 0.5, linetype="solid", colour ="black")) + guides(color = guide_legend(title = "Effect of CR2-Crry"), shape = guide_legend(title = "Effect of CR2-Crry")) + geom_text_repel(data = dplyr::filter(patterns_table, (abs(FC_naiveTOd28pbs) > 1.5 & status_tx_d28 %in% "Lost Significance")), aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = status_tx_d28, label = gene), nudge_x = 1.5, nudge_y = 0.7, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table,(abs(FC_naiveTOd28ctx) > 1.5 & status_tx_d28 == "Became Significant: Up")), aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = status_tx_d28, label = gene), nudge_x = 1, nudge_y = -0.7, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table,(abs(FC_naiveTOd28ctx) > 1.5 & status_tx_d28 == "Became Significant: Down")), aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = status_tx_d28, label = gene), nudge_x = -1, nudge_y = -0.5, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) + geom_text_repel(data = dplyr::filter(patterns_table, (abs(FC_naiveTOd28pbs) > 4 & status_tx_d28 %in% c("Increased Significantly", "Decreased Significantly"))), aes(x = log2_FC_naiveTOd28pbs, y = -log10(FDR_naiveTOd28pbs), color = status_tx_d28, label = gene), nudge_x = 0, nudge_y = 0.2, size = 1.1, box.padding = 0.5, point.padding = 0.1, force = 0.2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000) # pdf(file="/.../vp28_tx.pdf", width = 2.36, height = 2.36) # grid.draw(vp28_tx) # dev.off() # Figure 5 ---------------------------------------------------------------- CR2_effect <- patterns_table %>% dplyr::select(gene, contains("status")) %>% pivot_longer(cols = contains("status"), names_to = "Comparison", values_to = "Status") CR2_effect <- CR2_effect %>% filter(Comparison != "day_28s") %>% mutate(Comparison = case_when( Comparison == "status_tx_d3" ~ "Day 3", Comparison == "status_tx_d7" ~ "Day 7", Comparison == "status_tx_d28" ~ "Day 28" )) %>% dplyr::select(gene, Comparison, Status) CR2_FC <- main_table %>% filter(Comparison %in% comparisons_vector2 & FDR < 0.05) %>% # gets rid of "Lost Significance" dplyr::select(gene, Comparison, FC) %>% mutate(Comparison = case_when( Comparison == "naiveTOd3stx" ~ "Day 3", Comparison == "naiveTOd7stx" ~ "Day 7", Comparison == "naiveTOd28ctx" ~ "Day 28" )) fig5_table <- left_join(CR2_FC, CR2_effect) %>% left_join(., genes, by = c("gene" = "Gene")) fig5_table <- fig5_table %>% pivot_longer(cols = c(5:length(fig5_table)), values_to = "Pathways_value",names_to = c("Pathways")) %>% filter(Pathways_value == "+") %>% dplyr::select(-Pathways_value) fig5_table <- fig5_table %>% mutate(Direction = case_when( FC <= -1 ~ "Down", FC >= +1 ~ "Up")) fig5_table <- fig5_table %>% left_join(., positions) fig5_table$Comparison <- factor(fig5_table$Comparison, levels = c("Day 28", "Day 7", "Day 3")) fig5_table2 <- fig5_table %>% group_by(Pathways, Comparison, Direction) %>% summarize(medianFC_tx = median(FC)) %>% mutate(logmedianFC_tx = foldchange2logratio(medianFC_tx)) %>% left_join(., positions) %>% left_join(., fig2_table2[, c(1:4,7)]) fig5_table3 <- fig5_table %>% group_by(Pathways, Comparison, Direction, Status) %>% summarize(count = n()) %>% left_join(., positions) %>% mutate(count = case_when( Direction == "Down" ~ -count, TRUE ~ count )) fig5_table3 <- fig5_table3 %>% left_join(., fig2_table2[,c(1:3, 9)]) distinct(fig2_table2[,c(1:3, 9)]) fig5_table fig5_table2 # for plotting FC with treatment per path fig5_table3 # for plotting number of genes and Tx effects per path # plot FCs fig5_medianFC_tx <- ggplot(data = fig5_table2) + # Goes in the figure (median FC) geom_vline(xintercept = seq( -71, -2, by = 3), color = "gray95", linetype = "solid", size = 8.2) + geom_bar(aes(x = -position, y = logmedianFC), fill = "white", stat = "identity", width = 0.7) + geom_bar(aes(x = -position, y = logmedianFC_tx, fill = Comparison), stat = "identity", width = 0.7) + geom_bar(aes(x = -position, y = logmedianFC), color = "black", fill = "transparent", stat = "identity", width = 0.7) + #geom_point(aes(x = -position, y = logmedianFC, fill = Comparison), shape = 21, color = "gray30") + geom_hline(yintercept = 0, linetype = "solid", color = "black", size = 0.5) + coord_flip() + theme_classic() + ggtitle(label = "") + ylab ("Fold Change") + xlab("") + scale_y_continuous(breaks = seq(-1, 3, 1), labels = c("-2", "±1", "2", "4", "8"), limits = c(-1.7, 3.2), expand = c(0,0)) + scale_x_continuous(expand = c(0,0)) + scale_fill_manual(values = c("#C77CFF", "#00BFC4", "#F8766D")) + theme(plot.title = element_blank(), axis.title = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.position = "none") # pdf(file="/.../fig5_medianFC_tx.pdf", width = 1.34, height = 6.62) # grid::grid.draw(fig5_medianFC_tx) # dev.off() # plot numbers fig5_count <- ggplot(data = fig5_table3) + # goes in the figure (count per most_involved pattern) geom_vline(xintercept = seq( -71, -2, by = 3), color = "gray95", linetype = "solid", size = 8.2) + geom_bar(aes(x = -position, y = count_all2), stat = "identity", position = "identity", width = 0.7, fill = "white") + geom_bar(aes(x = -position, y = count, fill = Status), stat = "identity", position = "stack", width = 0.7) + geom_bar(aes(x = -position, y = count_all2), stat = "identity", position = "identity", width = 0.7, color = "black", fill = "transparent") + #geom_point(aes(x = -position, y = count_all2), fill = "black", stat = "identity", position = "identity", shape = "\U007C", size = 1.25, hjust = 0.5) + geom_hline(yintercept = 0, linetype = "solid", color = "black", size = 0.5) + coord_flip() + theme_classic() + ggtitle(label = "") + ylab ("Fold Change") + xlab("") + scale_y_continuous(breaks = seq(-40, 120, 40), labels = c("40", "0", "40", "80", "120"), limits = c(-50, 130), expand = c(0,0)) + scale_x_continuous(expand = c(0,0)) + scale_fill_manual(values = c("forestgreen", "forestgreen", "deeppink3", "Purple", "Gray")) + theme(plot.title = element_blank(), axis.title = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.position = "none") # pdf(file="/.../fig5_count.pdf", width = 1.34, height = 6.62) # grid::grid.draw(fig5_count) # dev.off() # Table of effects of CR2-Crry per gene table_CR2 <- CR2_effect %>% pivot_wider(names_from = "Comparison", values_from = "Status") %>% arrange(gene) write_csv(table_CR2, "/.../effects.csv") # Figure X1: Compare with microglia dataset ------------------------------------------ whole_microglia <- patterns_table %>% dplyr::select(gene, contains("naive") & contains("pbs") & !contains("relaxed" )) %>% .[, -c(2:4)] microglia_genes <- read_csv("/.../Datasets/2019_Izzy_CCI_2d2w2m/gene_list.csv") microglia_genes <- microglia_genes["gene"] %>% .$gene whole_microglia <- whole_microglia %>% filter(gene %in% microglia_genes) microglia_d2pi <- read_xlsx("/.../Datasets/2019_Izzy_CCI_2d2w2m/2dpi_vs_sham.xlsx") %>% dplyr::select(gene, log2_FC_d2pi = log2FC, FDR_d2pi = qvalue) microglia_d14pi <- read_xlsx("/.../Datasets/2019_Izzy_CCI_2d2w2m/14dpi_vs_sham.xlsx") %>% dplyr::select(gene, log2_FC_d14pi = log2FC, FDR_d14pi = qvalue) microglia_d60pi <- read_xlsx("/.../Datasets/2019_Izzy_CCI_2d2w2m/60dpi_vs_sham.xlsx") %>% dplyr::select(gene, log2_FC_d60pi = log2FC, FDR_d60pi = qvalue) whole_microglia <- whole_microglia %>% left_join(., microglia_d2pi) %>% left_join(., microglia_d14pi) %>% left_join(., microglia_d60pi) whole_microglia_FDR <- whole_microglia %>% dplyr::select(gene, contains("FDR")) %>% pivot_longer(cols = c(2:7), values_to = "FDR", names_to = "Group", names_pattern = "FDR_(.*)") whole_microglia_log2FC <- whole_microglia %>% dplyr::select(gene, contains("FC")) %>% pivot_longer(cols = c(2:7), values_to = "log2FC", names_to = "Group", names_pattern = "log2_FC_(.*)") whole_vs_microglia <- left_join(whole_microglia_FDR, whole_microglia_log2FC) whole_vs_microglia$FDR[which(is.na(whole_vs_microglia$FDR))] <- 1 # To select with >= 0.05 whole_vs_microglia$log2FC[which(is.na(whole_vs_microglia$log2FC))] <- 1 # Doesn't matter, will be gray whole_vs_microglia <- whole_vs_microglia %>% mutate(Group = case_when( Group == "naiveTOd3pbs" ~ "whole_3dpi", Group == "naiveTOd7pbs" ~ "whole_7dpi", Group == "naiveTOd28pbs" ~ "whole_28dpi", Group == "d2pi" ~ "microglia_2dpi", Group == "d14pi" ~ "microglia_14dpi", Group == "d60pi" ~ "microglia_60dpi", )) keep <- whole_vs_microglia %>% group_by(gene) %>% summarise(keep = any(FDR < 0.05)) %>% filter(keep) %>% .$gene whole_vs_microglia <- whole_vs_microglia %>% filter(gene %in% keep) whole_vs_microglia$Group <- factor(whole_vs_microglia$Group, levels = microglia_vector) View(whole_microglia) whole_micro_graph <- whole_vs_microglia %>% ggplot(., aes(x = gene, y = Group)) + #geom_hline(yintercept = 3.5) + geom_tile(fill = "gray90", color = "white") + geom_tile(data = filter(whole_vs_microglia, FDR < 0.05), #& str_detect(Group, "micro")), aes(fill = log2FC), color = "white") + geom_tile(data = filter(whole_vs_microglia, FDR < 0.05 & log2FC > 5), fill = "#5ab4ac", color = "white") + geom_tile(data = filter(whole_vs_microglia, FDR < 0.05 & log2FC < -5), fill = "#d8b365", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-5, 5), midpoint = 0, breaks = c(-5, -2, 0, 2, 5), labels = c("-32", "-4", "±1", "4", "32")) + theme( panel.background = element_blank(), panel.border = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", hjust = 1, size = 4), axis.text.y = element_text(size = 5), legend.position = "right" ) pdf(file="/.../whole_micro_graph.pdf", width = 7.2, height = 2.5) grid.draw(whole_micro_graph) dev.off() # Figure X2: Compare sensome data ------------------------------------------ microglia_d2pi RNAseq <- read_xls("/.../Datasets/2016_Zhong_CCI_1d/1dpi.xls") sens_genes <- read_xlsx("/.../Datasets/2016_Zhong_CCI_1d/sensome_genes.xlsx") %>% .$gene RNAseq <- RNAseq %>% filter(gene %in% sens_genes) RNAseq2 <- data.frame(gene = RNAseq$gene, p_value = NA) for (i in 1: nrow(RNAseq)){ x <- c(RNAseq[[i, 2]], RNAseq[[i, 3]], RNAseq[[i, 4]]) y <- c(RNAseq[[i, 5]], RNAseq[[i, 6]], RNAseq[[i, 7]]) RNAseq2$p_value[i] <- t.test(x=x, y=y)$p.value RNAseq2$FC[i] <- foldchange(mean(y), mean(x)) } RNAseq2$FDR_d1pi <- p.adjust(RNAseq2$p_value, method = "fdr") RNAseq2$log2_FC_d1pi <- foldchange2logratio(RNAseq2$FC) RNAseq2 <- RNAseq2 %>% dplyr::select(gene,log2_FC_d1pi, FDR_d1pi) (RNAseq_vs_micro <- RNAseq2 %>% left_join(., microglia_d2pi)) RNAseq_vs_micro_FDR <- RNAseq_vs_micro %>% dplyr::select(gene, contains("FDR")) %>% pivot_longer(cols = c(2:3), values_to = "FDR", names_to = "Group", names_pattern = "FDR_(.*)") RNAseq_vs_micro_log2FC <- RNAseq_vs_micro %>% dplyr::select(gene, contains("FC")) %>% pivot_longer(cols = c(2:3), values_to = "log2FC", names_to = "Group", names_pattern = "log2_FC_(.*)") RNAseq_vs_micro <- left_join(RNAseq_vs_micro_log2FC, RNAseq_vs_micro_FDR) RNAseq_vs_micro$log2FC[c(which(is.na(RNAseq_vs_micro$log2FC)))] <- 0 # To select with >= 0.05 RNAseq_vs_micro$FDR[which(is.na(RNAseq_vs_micro$FDR))] <- 1 RNAseq_vs_micro RNAseq_vs_micro_graph <- RNAseq_vs_micro %>% ggplot(., aes(x = gene, y = Group)) + #geom_hline(yintercept = 3.5) + geom_tile(fill = "gray90", color = "white") + geom_tile(data = filter(RNAseq_vs_micro, FDR < 0.05), #& str_detect(Group, "micro")), aes(fill = log2FC), color = "white") + geom_tile(data = filter(RNAseq_vs_micro, FDR < 0.05 & log2FC > 5), fill = "#5ab4ac", color = "white") + geom_tile(data = filter(RNAseq_vs_micro, FDR < 0.05 & log2FC < -5), fill = "#d8b365", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-5, 5), midpoint = 0) + theme( panel.background = element_blank(), panel.border = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", hjust = 1, size = 4), axis.text.y = element_text(size = 5), legend.position = "none" ) pdf(file="/.../RNAseq_vs_micro_graph.pdf", width = 7.2, height = 1) grid.draw(RNAseq_vs_micro_graph) dev.off() # Figure X3: Complement RNAseq vs NanoString --------------------------------------------------- length(sequence) RNAseq <- read_xls("/.../Datasets/2016_Zhong_CCI_1d/2016_Zhong_CCI_1d.xls") # modified names to match complement panel RNAseq <- RNAseq %>% filter(gene %in% sequence) setdiff(sequence, RNAseq$gene) RNAseq2 <- data.frame(gene = RNAseq$gene, p_value = NA) for (i in 1: nrow(RNAseq)){ x <- c(RNAseq[[i, 2]], RNAseq[[i, 3]], RNAseq[[i, 4]]) y <- c(RNAseq[[i, 5]], RNAseq[[i, 6]], RNAseq[[i, 7]]) RNAseq2$p_value[i] <- t.test(x=x, y=y)$p.value RNAseq2$FC[i] <- foldchange(mean(y), mean(x)) } RNAseq2$FDR_d1pi <- p.adjust(RNAseq2$p_value, method = "fdr") RNAseq2$log2_FC_d1pi <- foldchange2logratio(RNAseq2$FC) RNAseq2 <- RNAseq2 %>% dplyr::select(gene, log2FC = log2_FC_d1pi, FDR = FDR_d1pi) %>% mutate(Group = "naiveTOd1pi") RNAseq2 <- RNAseq2 %>% filter(is.finite(log2FC)) nanostring_comp <- main_table_natural_comp %>% dplyr::select(gene, log2FC = log2_FC, FDR, Group = Comparison) %>% filter(gene %in% RNAseq2$gene) compd1pi_vs_d3728 <- rbind(RNAseq2, nanostring_comp) compd1pi_vs_d3728 <- compd1pi_vs_d3728 %>% mutate(Group = case_when( Group == "naiveTOd3pbs" ~ "3 dpi", Group == "naiveTOd7pbs" ~ "7 dpi", Group == "naiveTOd28pbs" ~ "28 dpi", Group == "naiveTOd1pi" ~ "1 dpi", )) compd1pi_vs_d3728$Group <- factor(compd1pi_vs_d3728$Group, levels = c("1 dpi", "3 dpi", "7 dpi", "28 dpi")) compd1pi_vs_d3728_graph <- compd1pi_vs_d3728 %>% ggplot(., aes(x = gene, y = Group)) + #geom_hline(yintercept = 3.5) + geom_tile(fill = "gray90", color = "white") + geom_tile(data = filter(compd1pi_vs_d3728, FDR < 0.05), #& str_detect(Group, "micro")), aes(fill = log2FC), color = "white") + geom_tile(data = filter(compd1pi_vs_d3728, FDR < 0.05 & log2FC > 5), fill = "#5ab4ac", color = "white") + geom_tile(data = filter(compd1pi_vs_d3728, FDR < 0.05 & log2FC < -5), fill = "#d8b365", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-5, 5), midpoint = 0) + theme( panel.background = element_blank(), panel.border = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, face = "bold", hjust = 1, size = 4), axis.text.y = element_text(size = 5), legend.position = "none" ) pdf(file="/.../compd1pi_vs_d3728_graph.pdf", width = 7.2, height = 2) grid.draw(compd1pi_vs_d3728_graph) dev.off() # Data Exploration -------------------------------------------------------- keep_decreased <- function(x){ filter(x, str_detect(status_tx_d3, "Decreased S|Lost") | str_detect(status_tx_d7, "Decreased Sig|Lost") | str_detect(status_tx_d28, "Decreased Sig|Lost")) } # Astroglial markers based on Liddelow et al. disease_associated_microglia <- "Clec7a|Tyrobp|Trem2|Axl|Apoe" pan_astrocytes <- "Lcn2|Steap4|S1pr3|Timp1|Hspb|Cxcl10|Cd44|Osmr|Cp|Serpina3|Aspg|Vim|Gfap" a1_astrocytes <- "H2-T23|Serping1|Fbln5|Ugt1a|H2-D1|Ggta1|Ligp1|Gbp2|Psmb8|Srgn|Amigo2|C3" a2_astrocytes <- "Cd109|Tgm1|Ptx3|S100a10|Ptgs2|Emp1|Slc10a6|Tm4sf1|Cd14" patterns_table %>% dplyr::select(gene, contains("sign"), contains("status")) %>% filter(str_detect(gene, pan_astrocytes) & sig_sign != "---")# %>% keep_decreased(.) # Immune cell receptors and phagocytic markers phagocytic_markers <- "Axl|Mertk|Msr1|Trem2|Tyrobp|Cd74|Mfge8|Cd68|Lamp1|Lamp2|Itgam" # 6 out of 9 immune_cells <- "Cd8a|Cd8b1|Cd3e|Cd3d|Ly6|Fcer1g|Siglecf|Itgax|Gz|P2y" patterns_table %>% dplyr::select(gene, contains("sign"), contains("status")) %>% filter(str_detect(gene, phagocytic_markers) & sig_sign != "---") %>% keep_decreased(.) patterns_table %>% dplyr::select(gene, contains("sign"), contains("status")) %>% filter(str_detect(gene, immune_cells)) %>% keep_decreased(.) # Cytokines and cytokine receptors cytokines_receptors <- "Cx|Ccl|Il|Tnf|Tgf|Csf" patterns_table %>% dplyr::select(gene, contains("sign"), contains("status")) %>% filter(str_detect(gene, cytokines_receptors) & sig_sign != "---") %>% keep_decreased(.) # Genes that peak acutely patterns_table2 %>% filter(., Sig_most_involved == "Days 3 & 7") %>% write_csv(., "/.../Days3&7.csv") # Z. Heatmaps -------------------------------------------------- ### Top 60 genes by FC (10 per timepoint and condition) str(main_table) v01 <- main_table %>% filter(Comparison == "naiveTOd3pbs" & FDR < 0.05) %>% arrange(desc(FC)) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Top 10 d3 v02 <- main_table %>% filter(Comparison == "naiveTOd3pbs" & FDR < 0.05) %>% arrange((FC)) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Bottom 10 d3 v03 <- main_table %>% filter(Comparison == "naiveTOd7pbs" & FDR < 0.05) %>% arrange(desc(FC)) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Top 10 d7 v04 <- main_table %>% filter(Comparison == "naiveTOd7pbs" & FDR < 0.05) %>% arrange((FC)) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Bottom 10 d7 v05 <- main_table %>% filter(Comparison == "naiveTOd28pbs" & FDR < 0.05) %>% arrange(desc(FC)) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Top 10 d28 v06 <- main_table %>% filter(Comparison == "naiveTOd28pbs" & FDR < 0.05) %>% arrange((FC)) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Bottom 10 d28 top60FC <- (c(v01, v02, v03, v04, v05, v06)) top60FC temp_hm <- main_table_natural %>% filter(gene %in% v06) temp_hm$gene <- factor(temp_hm$gene, levels = v06) hm_pbs <- ggplot(temp_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(temp_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../botFC10d28.pdf", width = 1.11, height = 1.37) grid.draw(hm_pbs) dev.off() ### Top 60 genes by Q value (10 per timepoint and condition) str(main_table) w01 <- main_table %>% filter(Comparison == "naiveTOd3pbs" & FC > 1) %>% arrange(FDR) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Top 10 d3 w02 <- main_table %>% filter(Comparison == "naiveTOd3pbs" & FC < 1) %>% arrange(FDR) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Bottom 10 d3 w03 <- main_table %>% filter(Comparison == "naiveTOd7pbs" & FC > 1) %>% arrange(FDR) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Top 10 d7 w04 <- main_table %>% filter(Comparison == "naiveTOd7pbs" & FC < 1) %>% arrange(FDR) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Bottom 10 d7 w05 <- main_table %>% filter(Comparison == "naiveTOd28pbs" & FC > 1) %>% arrange(FDR) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Top 10 d28 w06 <- main_table %>% filter(Comparison == "naiveTOd28pbs" & FC < 1) %>% arrange(FDR) %>% filter(row_number() <= 10) %>% .$gene %>% as.character() # Bottom 10 d28 top60QV <- (c(w01, w02, w03, w04, w05, w06)) top60QV temp_hm <- main_table_natural %>% filter(gene %in% w06) temp_hm$gene <- factor(temp_hm$gene, levels = w06) hm_pbs <- ggplot(temp_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(temp_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../bot10QVd28.pdf", width = 1.11, height = 1.37) grid.draw(hm_pbs) dev.off() # Extract legend hm_legend <- ggplot(temp_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0, breaks = c(-2, 0, 2, 4, 6), labels = c("-4", "±1", "4", "16", "64")) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.text = element_text(size = 6), legend.title = element_text(size = 7, face = "bold"), legend.position = "right", legend.background = element_rect(fill="white", size = 1, linetype="solid", colour ="black"), ) pdf(file="/.../legend.pdf", width = 1.11, height = 1.37) grid.draw(hm_legend) dev.off() # Cytokines Heatmap cytokines <- c("Ccl2","Ccl3","Ccl4","Ccl5","Ccl7","Csf1","Cx3cl1","Cxcl9","Cxcl10","Il1a","Il1b","Il3","Tgfb1","TnfTnfsf4","Tnfsf8","Xcl1") # 16 cytokinesR <- c("Ccr2", "Ccr5", "Csf1r", "Csf2rb", "Csf3r", "Cx3cr1", "Ifnar1", "Ifnar2", "Il10rb", "Il15ra", "Il1r1", "Il1r2", "Il1rap", "Il1rn", "Il21r", "Il2rg", "Il3ra", # 31 "Il6ra", "Tgfbr1", "Tnfrsf10b", "Tnfrsf11b", "Tnfrsf12a", "Tnfrsf13c", "Tnfrsf17", "Tnfrsf1a", "Tnfrsf1b", "Tnfrsf25", "Tnfrsf4", "Tnfsf10", "Tnfsf12", "Tnfsf13b") cytokine_hm <- main_table_natural %>% filter(gene %in% cytokines) cytokine_hm$gene <- factor(cytokine_hm$gene, levels = cytokines) cytokineR_hm <- main_table_natural %>% filter(gene %in% cytokinesR) cytokineR_hm$gene <- factor(cytokineR_hm$gene, levels = cytokinesR) cytokines_graph <- ggplot(cytokine_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(cytokine_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) cytokinesR_graph <- ggplot(cytokineR_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(cytokineR_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) ggplot(cytokine_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(cytokine_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#EF8A62", high = "#F7F7F7", mid = "#67A9CF", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) brewer.pal(n=3, name="RdBu") pdf(file="/.../cytokine_hm.pdf", width = 1.6, height = 1.37) grid.draw(cytokines_graph) dev.off() pdf(file="/.../cytokineR_hm.pdf", width = 3.1, height = 1.37) grid.draw(cytokinesR_graph) dev.off() # Neurons and Neurotransmitters heatmap neurons <- c("Arc", "Bdnf", "Calr", "Cntnap2", "Dlg1", "Dlg4", "Dlx1", "Dlx2", "Egr1", "Fos", "Gja1", "Gjb1", "Gria1", "Gria2", "Gria4", "Grin2a", "Grin2b", "Grm2", "Grm3", # 44 "Islr2", "Kcnd1", "Kcnj10", "Kcnk13", "Mapt", "Nefl", "Nlgn1", "Nlgn2", "Nptx1", "Nrgn", "Plekhb1", "Rbfox3", "Reln", "Shank3", "Slc17a6", "Slc17a7", "Slc1a3", "Slc6a1", "Syn2", "Syp", "Tbr1", "Tgfa", "Tubb3", "Tubb4a", "Vamp7") neurons_hm <- main_table_natural %>% filter(gene %in% neurons) neurons_hm$gene <- factor(neurons_hm$gene, levels = neurons) neurons_graph <- ggplot(neurons_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(neurons_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../neurons_hm.pdf", width = 7, height = 1.37) grid.draw(neurons_graph) dev.off() # Complement genes heatmap complement_genes <- c("C1qa","C1qb","C1qc","C3","C3ar1","C4a","C5ar1","C6","Itgam","Itgax","Serping1") complement_hm <- main_table_natural %>% filter(gene %in% complement_genes) complement_hm$gene <- factor(complement_hm$gene, levels = complement_genes) complement_graph <- ggplot(complement_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(complement_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../complement_hm.pdf", width = 7, height = 1.37) grid.draw(complement_graph) dev.off() # Heatmap for epigenetic regulation epigenetic_genes <- c("Ago4","Arid1a","Ash2l","Bmi1","Brca1","Brd2","Brd3","Brd4","Crebbp","Dicer1","Dnmt1","Dnmt3a","Dnmt3b","Dot1l","Eed","Ehmt2","Eif1","Ep300","Ezh1","Ezh2","H2afx", "Hat1","Hdac1","Hdac2","Hdac4","Hdac6","Hira","Jarid2","Kat2a","Kat2b","Kdm1a","Kdm1b","Kdm2a","Kdm2b","Kdm3a","Kdm3b","Kdm4a","Kdm4b","Kdm4c","Kdm4d","Kdm5a","Kdm5b", "Kdm5c","Kdm5d","Kdm6a","Kmt2a","Kmt2c","Mbd2","Mbd3","Ncor1","Ncor2","Padi2","Sall1","Setd1a","Setd1b","Setd2","Setd7","Setdb1","Sin3a","Sirt1","Smarca4","Smarca5", "Smarcd1","Suv39h1","Suv39h2","Suz12","Tarbp2","Tet1","Uty","Wdr5") epigenetic_hm <- main_table_natural %>% filter(gene %in% epigenetic_genes) epigenetic_hm$gene <- factor(epigenetic_hm$gene, levels = epigenetic_genes) epigenetic_graph <- ggplot(epigenetic_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(epigenetic_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../epigenetic_hm.pdf", width = 7, height = 1.37) grid.draw(epigenetic_graph) dev.off() # Growth factors heatmap growth_genes <- c("Egfr","Fgf13","Flt1","Grn","Igf1","Igf1r","Igf2r","Ngf","Ngfr","Tgfa","Tgfb1","Tgfbr1","Vegfa") growth_hm <- main_table_natural %>% filter(gene %in% growth_genes) growth_hm$gene <- factor(growth_hm$gene, levels = growth_genes) growth_graph <- ggplot(growth_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(growth_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../growth_hm.pdf", width = 7, height = 1.37) grid.draw(growth_graph) dev.off() # Phagocytosis heatmap phag_genes <- c("Axl","Mertk","Clec7a","Msr1","Trem2","Tyrobp","Cd74","Mfge8","Cd68","Lamp1","Lamp2") phag_hm <- main_table_natural %>% filter(gene %in% phag_genes) phag_hm$gene <- factor(phag_hm$gene, levels = phag_genes) phag_graph <- ggplot(phag_hm) + geom_tile(aes(x = gene, y = Comparison, fill = log2_FC), color = "white") + geom_tile(data = filter(phag_hm, FDR >= 0.05), aes(x = gene, y = Comparison), fill = "gray", color = "white") + scale_fill_gradient2(low = "#d8b365", high = "#5ab4ac", limits = c(-3, 7), midpoint = 0) + theme_minimal() + theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(face = "bold", color = "black", size = 5, angle = 90), axis.text.y = element_blank(), legend.position = "none" ) pdf(file="/.../phag_hm.pdf", width = 2, height = 1.37) grid.draw(phag_graph) dev.off() patterns_table %>% filter(most_involved == "None" & sig_pattern != "None") %>% dplyr::select(gene, FC_naiveTOd3pbs, FC_naiveTOd7pbs, FC_naiveTOd28pbs) # two genes that are sign +-+ left out of patterns # I fixed this so there should be none anymore str(patterns_table) patterns_table$sig_pattern <- factor(patterns_table$sig_pattern, levels = c("Always", "Days 3 & 7", "Days 7 & 28","Days 3 & 28", "Day 3", "Day 7", "Day 28", "None")) patterns_table$sig_times_1 <- factor(patterns_table$sig_times_1, levels = c("Always", "Twice", "Once", "None")) str(patterns_table) ggplot(data = patterns_table) + geom_bar(aes(x = sig_pattern, fill = sig_times_1)) + ggtitle(label = "Temporal Patterns: Signficance Over Time") + xlab("Signficance Pattern") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 300, 25), labels = seq(0, 300, 25), limits = c(0, 300), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold") #legend.position = "none", #strip.text.x = element_text(size = 12, color = "Black", face = "bold"), #strip.background = element_rect(colour = "black", fill = "gray90") ) + scale_fill_brewer(palette = "Set2") + guides(fill = guide_legend(title = "Signficance")) ggplot(data = patterns_table) + geom_bar(aes(x = sig_times_1, fill = sig_times_1)) + ggtitle(label = "Temporal Patterns: Signficance Over Time") + xlab("Signficance Pattern") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 300, 25), labels = seq(0, 300, 25), limits = c(0, 300), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold") #legend.position = "none", #strip.text.x = element_text(size = 12, color = "Black", face = "bold"), #strip.background = element_rect(colour = "black", fill = "gray90") ) + scale_fill_brewer(palette = "Set2") + guides(fill = guide_legend(title = "Significance")) ggplot(data = filter(patterns_table, sig_pattern %in% c("Day 3", "Day 7", "Day 28"))) + geom_bar(aes(x = sig_pattern, fill = Up_Down)) + ggtitle(label = "Signficance = Once") + xlab("Signficance Pattern") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 300, 25), labels = seq(0, 300, 25), limits = c(0, 200), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold") ) + scale_fill_brewer(palette = "Set2") + guides(fill = guide_legend(title = "Direction")) rlang::last_error() temp7 <- patterns_table %>% filter(sig_pattern == "Always") %>% dplyr::select(gene, Peaks, Dips, FC_sign) # GO BACK UP TO TEMP3 temp3 <- temp3 %>% pivot_longer(cols = c(2:3), names_to = "Pattern", values_to = "Days") temp3$Days <- factor(temp3$Days, levels = c("Day 3", "Day 7", "Day 28", "Days 3 & 7", "Days 7 & 28", "Peakless", "Dipless", "None")) ggplot(data = filter(temp3, Days != "None")) + geom_bar(aes(x = Days, fill = Pattern)) + ggtitle(label = "Significance = Always") + xlab("Peaks or Dips At") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 100, 10), labels = seq(0, 100, 10), limits = c(0, 100), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold") #legend.position = "none", #strip.text.x = element_text(size = 12, color = "Black", face = "bold"), #strip.background = element_rect(colour = "black", fill = "gray90") ) + guides(fill = guide_legend(title = "Direction")) temp4 <- patterns_table %>% filter(sig_times_1 == "Twice") %>% dplyr::select(gene, Peaks, Dips, FC_sign) temp4 <- temp4 %>% pivot_longer(cols = c(2:3), names_to = "Pattern", values_to = "Days") temp4$Days <- factor(temp4$Days, levels = c("Day 3", "Day 7", "Day 28", "Days 3 & 7", "Days 7 & 28", "Peakless", "Dipless", "None")) ggplot(data = filter(temp4, Days != "None")) + geom_bar(aes(x = Days, fill = Pattern)) + ggtitle(label = "Signficance = Two Timepoints") + xlab("Peaks or Dips At") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 300, 25), labels = seq(0, 300, 25), limits = c(0, 200), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold") #legend.position = "none", #strip.text.x = element_text(size = 12, color = "Black", face = "bold"), #strip.background = element_rect(colour = "black", fill = "gray90") ) + scale_fill_brewer(palette = "Set2") + guides(fill = guide_legend(title = "Direction")) table(patterns_table$sig_sign, patterns_table$FC_sign) patterns_table[,c(1,90:length(patterns_table))] gene_patterns2 <- patterns_table[,c(1,90:length(patterns_table))] ### Plot the patterns # Raw numbers (ignore, not clear at all) gene_pattern_plot2 <- left_join(raw_long, gene_patterns2) gene_pattern_plot2$Group <- as.numeric(gene_pattern_plot2$Group) str(gene_pattern_plot2) plot <- gene_pattern_plot2 %>% group_by(gene, sig_pattern, Group) %>% summarise(average = mean(Reads)) %>% left_join(x = . , y = gene_pattern_plot2) plot %>% #filter(s == "Not Significant") %>% ggplot(data = ., aes(x = Group, y = log2(average), group = gene, color = FC_sign)) + geom_line(alpha = 0.8) + theme_classic() + facet_grid(FC_sign ~ sig_pattern) # FC gene_pattern_plot2 <- left_join(main_table_natural, gene_patterns2) gene_pattern_plot2$Comparison <- as.numeric(gene_pattern_plot2$Comparison) str(gene_pattern_plot2) gene_pattern_plot2 %>% filter(sig_pattern %in% c("Always", "Days 3 & 7", "Days 7 & 28", "Days 3 & 28"), Peaks != "None") %>% ggplot(data = ., aes(x = Comparison, y = log2_FC, group = gene, color = Peaks)) + geom_line(alpha = 0.8) + geom_point() + theme_classic() + facet_grid(Peaks ~ sig_pattern) + geom_hline(yintercept = 0) gene_pattern_plot2 %>% filter(sig_pattern %in% c("Always", "Days 3 & 7", "Days 7 & 28", "Days 3 & 28"), Dips != "None") %>% ggplot(data = ., aes(x = Comparison, y = log2_FC, group = gene, color = Dips)) + geom_line(alpha = 0.8) + geom_point() + theme_classic() + facet_grid(Dips ~ sig_pattern) + geom_hline(yintercept = 0) temp5 <- gene_pattern_plot2 %>% dplyr::select(gene, Comparison, FC, log2_FC, FC_sign, sig_times_1, sig_pattern, Up_Down, Peaks, Dips) temp5 <- temp5 %>% pivot_longer(cols = c(Peaks, Dips), names_to = "Pattern", values_to = "Days") temp5$Days <- factor(temp5$Days, levels = c("Day 3", "Day 7", "Day 28", "Days 3 & 7", "Days 7 & 28", "Peakless", "Dipless", "None")) str(temp5) temp5$Days2 <- case_when(temp5$Days %in% c("Peakless", "Dipless") ~ "No Pattern", TRUE ~ as.character(temp5$Days)) temp5$Days2 <- factor(temp5$Days2, levels = c("Day 3", "Day 7", "Day 28", "Days 3 & 7", "Days 7 & 28", "No Pattern", "None")) unique(temp5$sig_pattern) unique(temp5$Pattern) unique(temp5$Days) temp5$Days3 <- case_when(temp5$sig_pattern %in% c("Day 3", "Day 7", "Day 28") ~ "No Pattern", TRUE ~ as.character(temp5$Days2)) temp5$Days3 <- factor(temp5$Days3, levels = c("Day 3", "Day 7", "Day 28", "Days 3 & 7", "Days 7 & 28", "No Pattern", "None")) temp5$Pattern2 <- case_when(temp5$sig_pattern %in% c("Day 3", "Day 7", "Day 28") ~ temp5$Up_Down, temp5$sig_pattern %in% c("Always", "Days 3 & 7", "Days 7 & 28", "Days 3 & 28") & temp5$Days3 == "No Pattern" & temp5$Pattern == "Peaks" ~ "Up", temp5$sig_pattern %in% c("Always", "Days 3 & 7", "Days 7 & 28", "Days 3 & 28") & temp5$Days3 == "No Pattern" & temp5$Pattern == "Dips" ~ "Down", TRUE ~ temp5$Pattern) table(temp5$Pattern2, temp5$Days3) temp5$Pattern2 <- factor(temp5$Pattern2, levels = c("Peaks", "Dips", "Up", "Down")) table(temp5$sig_pattern,temp5$Pattern2) #temp6 <- temp5 %>% filter(sig_pattern %in% c("Always", "Days 3 & 7", "Days 7 & 28", "Days 3 & 28"), Days2 != "None") temp6 <- temp5 %>% filter(Days3 != "None") ggplot(data = temp6, aes(x = Comparison, y = log2_FC, group = gene, color = Pattern)) + facet_grid(Days2 ~ sig_pattern) + geom_line( data = temp6, aes(x = Comparison, y = log2_FC, group = gene, color = Pattern)) + geom_point( data = filter(temp6, !(sig_pattern == "Days 3 & 7" & Comparison == 3) | (sig_pattern == "Days 3 & 28" & Comparison == 2) | (sig_pattern == "Days 7 & 28" & Comparison == 1)), aes(x = Comparison, y = log2_FC, group = gene, color = Pattern)) + geom_point( data = filter(temp6, (sig_pattern == "Days 3 & 7" & Comparison == 3) | (sig_pattern == "Days 3 & 28" & Comparison == 2) | (sig_pattern == "Days 7 & 28" & Comparison == 1)), color = "black") + geom_hline(yintercept = 0, alpha = 0.5, linetype = "dashed") + theme_bw() + scale_x_continuous(breaks = c(1, 2, 3), labels = c("3", "7", "28")) + scale_y_continuous(breaks = c(-2, 0, 2, 4, 6), labels = c("-4", "±1", "4", "16", "64"), limits = c(-3, 6.2)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold")) + xlab("Day post TBI") + ylab("Fold Change") + ggtitle("Peaks & Dips in Genes Differentially Expressed at 2+ Timepoints") temp7 <- temp5 %>% filter(Up_Down != "None") ggplot(data = temp7, aes(x = Comparison, y = log2_FC, group = gene, color = Up_Down)) + facet_grid(. ~ sig_pattern) + geom_line(aes(x = Comparison, y = log2_FC, group = gene, color = Up_Down)) + geom_point(aes(x = Comparison, y = log2_FC, group = gene, color = Up_Down)) + geom_point( data = filter(temp7, !((sig_pattern == "Day 28" & Comparison == 3) | (sig_pattern == "Day 3" & Comparison == 1) | (sig_pattern == "Day 7" & Comparison == 2))), aes(x = Comparison, y = log2_FC, group = gene), color = "black") + geom_hline(yintercept = 0, alpha = 0.5, linetype = "dashed") + theme_bw() + scale_x_continuous(breaks = c(1, 2, 3), labels = c("3", "7", "28")) + scale_y_continuous(breaks = c(-2, 0, 2, 4, 6), labels = c("-4", "±1", "4", "16", "64"), limits = c(-3, 6.2)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold")) + xlab("Day post TBI") + ylab("Fold Change") + ggtitle("Genes Differentially Expression at a Single Timepoint") str(temp6) ### PDF: 7" x 7.2" pattern_map <- ggplot(data = temp6, aes(x = Comparison, y = log2_FC, group = gene, color = Pattern2)) + facet_grid(Days3 ~ sig_pattern) + geom_line( data = temp6, aes(x = Comparison, y = log2_FC, group = gene, color = Pattern2), size =0.2) + geom_point( data = filter(temp6, !((sig_pattern == "Days 3 & 7" & Comparison == 3) | (sig_pattern == "Days 3 & 28" & Comparison == 2) | (sig_pattern == "Days 7 & 28" & Comparison == 1) | (sig_pattern == "Day 3" & Comparison %in% c(2, 3)) | (sig_pattern == "Day 7" & Comparison %in% c(1, 3)) | (sig_pattern == "Day 28" & Comparison %in% c(1, 2)))), aes(x = Comparison, y = log2_FC, group = gene, color = Pattern2), size = 0.3) + geom_point( data = filter(temp6, (sig_pattern == "Days 3 & 7" & Comparison == 3) | (sig_pattern == "Days 3 & 28" & Comparison == 2) | (sig_pattern == "Days 7 & 28" & Comparison == 1) | (sig_pattern == "Day 3" & Comparison %in% c(2, 3)) | (sig_pattern == "Day 7" & Comparison %in% c(1, 3)) | (sig_pattern == "Day 28" & Comparison %in% c(1, 2)) ), color = "gray", size = 0.3) + geom_hline(yintercept = 0, alpha = 0.5, linetype = "dashed") + theme_bw() + scale_x_continuous(breaks = c(1, 2, 3), labels = c("3", "7", "28")) + scale_y_continuous(breaks = c(-2, 0, 2, 4, 6), labels = c("-4", "±1", "4", "16", "64"), limits = c(-3, 6.2)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 7, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 7, margin = margin(r = 10, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 6), axis.text.x = element_text(face = "bold", color = "black", size = 6), legend.text = element_text(size = 6), legend.position = "none", legend.title = element_text(size = 7, face = "bold"), strip.text = element_text(size = 6)) + xlab("Day post TBI") + ylab("Fold Change") + ggtitle("Temporal Patterns of Differentially Expressed Genes after TBI") pdf(file="/.../pattern_map.pdf", width = 7.2, height = 6) grid.draw(pattern_map) dev.off() # Count patterns temp6 %>% dplyr::select(gene, sig_pattern, Days3, Pattern2) %>% distinct(.) %>% group_by(sig_pattern, Days3, Pattern2) %>% count() # Z. Correlations ------------------------------------------------------------ raw_natural <- raw %>% dplyr::select(gene, contains("naive"), contains("pbs")) genes_correlation_natural <- as.data.frame(t(raw_natural[,-1])) colnames(genes_correlation_natural) <- genes_correlation_names genes_correlation_natural <- scale(genes_correlation_natural, center = TRUE, scale = TRUE) str(genes_correlation_natural) (genes_correlation_natural2 <- cor(genes_correlation_natural, method = "spearman", use = "all.obs")) genes_correlation_natural2[col(genes_correlation_natural2) == row(genes_correlation_natural2) | upper.tri(genes_correlation_natural2)] <- NA genes_correlation_natural2 <- as.data.frame(genes_correlation_natural2) genes_correlation_natural2$gene1 <- genes_correlation_names genes_correlation_natural2 <- genes_correlation_natural2 %>% dplyr::select(gene1, everything()) ncol(genes_correlation_natural2) genes_correlation_natural_long <- genes_correlation_natural2 %>% pivot_longer( cols = 2:ncol(genes_correlation_natural2), names_to = "gene2", values_to = "cor_value" ) genes_correlation_natural_long <- genes_correlation_natural_long %>% filter(!is.na(cor_value)) %>% arrange(desc(cor_value)) genes_correlation_natural_long$couple_number <- 1:nrow(genes_correlation_natural_long) genes_correlation_natural_long$group <- "PBS" nrow(genes_correlation_natural_long) str(genes_correlation_natural_long) # Correlations in stx raw_stx <- raw %>% dplyr::select(gene, contains("naive"), contains("stx")) genes_correlation_stx <- as.data.frame(t(raw_stx[,-1])) colnames(genes_correlation_stx) <- genes_correlation_names genes_correlation_stx <- scale(genes_correlation_stx, center = TRUE, scale = TRUE) str(genes_correlation_stx) (genes_correlation_stx2 <- cor(genes_correlation_stx, method = "spearman", use = "all.obs")) genes_correlation_stx2[col(genes_correlation_stx2) == row(genes_correlation_stx2) | upper.tri(genes_correlation_stx2)] <- NA genes_correlation_stx2 <- as.data.frame(genes_correlation_stx2) genes_correlation_stx2$gene1 <- genes_correlation_names genes_correlation_stx2 <- genes_correlation_stx2 %>% dplyr::select(gene1, everything()) ncol(genes_correlation_stx2) genes_correlation_stx_long <- genes_correlation_stx2 %>% pivot_longer( cols = 2:ncol(genes_correlation_stx2), names_to = "gene2", values_to = "cor_value" ) genes_correlation_stx_long <- genes_correlation_stx_long %>% filter(!is.na(cor_value)) %>% arrange(desc(cor_value)) genes_correlation_stx_long$couple_number <- 1:nrow(genes_correlation_stx_long) genes_correlation_stx_long$group <- "STX" nrow(genes_correlation_stx_long) str(genes_correlation_stx_long) # Correlations in ctx raw_ctx <- raw %>% dplyr::select(gene, contains("naive"), contains("d3stx"), contains("d7stx"), contains("ctx")) genes_correlation_ctx <- as.data.frame(t(raw_ctx[,-1])) colnames(genes_correlation_ctx) <- genes_correlation_names genes_correlation_ctx <- scale(genes_correlation_ctx, center = TRUE, scale = TRUE) str(genes_correlation_ctx) (genes_correlation_ctx2 <- cor(genes_correlation_ctx, method = "spearman", use = "all.obs")) genes_correlation_ctx2[col(genes_correlation_ctx2) == row(genes_correlation_ctx2) | upper.tri(genes_correlation_ctx2)] <- NA genes_correlation_ctx2 <- as.data.frame(genes_correlation_ctx2) genes_correlation_ctx2$gene1 <- genes_correlation_names genes_correlation_ctx2 <- genes_correlation_ctx2 %>% dplyr::select(gene1, everything()) ncol(genes_correlation_ctx2) genes_correlation_ctx_long <- genes_correlation_ctx2 %>% pivot_longer( cols = 2:ncol(genes_correlation_ctx2), names_to = "gene2", values_to = "cor_value" ) genes_correlation_ctx_long <- genes_correlation_ctx_long %>% filter(!is.na(cor_value)) %>% arrange(desc(cor_value)) genes_correlation_ctx_long$couple_number <- 1:nrow(genes_correlation_ctx_long) genes_correlation_ctx_long$group <- "CTX" nrow(genes_correlation_ctx_long) # PLot correlations cor_table <- rbind(genes_correlation_natural_long, genes_correlation_stx_long, genes_correlation_ctx_long) cor_table$group <- factor(cor_table$group, levels = c("PBS", "STX", "CTX")) ggplot(data = cor_table, mapping = aes(x = couple_number, y = cor_value, color = group)) + geom_line(size = 1) + geom_hline(yintercept = 0, linetype = "dashed") + theme_bw() + scale_y_continuous(expand = c(0,0)) + scale_x_continuous(expand = c(0,0)) + ggtitle(label = "Gene Correlations Weaken with Treatment") + xlab("Gene:Gene Couplets") + ylab ("Correlation Value") + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x = element_text(face = "bold", color = "black", size = 10)) + guides(color = guide_legend(title = "Treatment Group")) + scale_color_manual(values = c("#F8766D", "#00BFC4", "#C77CFF")) scales::show_col(c("#F8766D", "#00BFC4", "#C77CFF")) # Z. Other plots ------------------------------------------------------------- ### Plot number of DEGs per Pathways in PBS DEG_path_pbs <- pathways_table %>% filter(Comparison %in% natural_comparisons_naive) %>% filter(FDR < 0.05) %>% group_by(Comparison, Pathways) %>% count() DEG_path_pbs$Comparison <- factor(DEG_path_pbs$Comparison, levels = natural_comparisons_naive) ggplot(DEG_path_pbs, aes(x = reorder(Pathways, desc(n)), y = n, fill = Comparison)) + geom_bar(stat = "identity", position = "dodge") + geom_text(aes(label = n), position = position_dodge(width = 0.92), vjust = -0.5, size = 2) + scale_fill_manual(values = c("#F8766D", "#00BFC4", "#C77CFF"), labels = c("Day 3", "Day 7", "Day 28")) + ggtitle(label = "DEG Pathways (PBS)") + xlab("Pathways") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 140, by = 20), labels = seq(0, 140, by = 20), limits = c(0, 140), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 10, b = 10)), axis.title.x = element_blank(), #element_text(face = "bold", size = 6, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 8, margin = margin(r = 0, l = 2)), #axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x= element_blank(), legend.text = element_text(size = 7), legend.title = element_text(size = 8, face = "bold", hjust = 0.5), legend.position = c(0.7, 0.08), legend.background = element_rect(fill="gray90", size = 0.5, linetype="solid", colour ="black"), panel.grid.major.x = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank(), strip.text.x = element_text(size = 5, color = "Black", face = "bold")) + guides(fill = guide_legend(title = "Group")) + facet_wrap(~ Pathways, scales = "free_x") ### Plot number of DEGs per Pathways in CR2-Crry DEG_path_tx <- pathways_table %>% filter(Comparison %in% comparisons_vector2) %>% filter(FDR < 0.05) %>% group_by(Comparison, Pathways) %>% count() DEG_path_tx$Comparison <- factor(DEG_path_tx$Comparison, levels = comparisons_vector2) ggplot(DEG_path_tx, aes(x = reorder(Pathways, desc(n)), y = n, fill = Comparison)) + geom_bar(stat = "identity", position = "dodge") + geom_text(aes(label = n), position = position_dodge(width = 0.92), vjust = -0.5, size = 2.5) + scale_fill_manual(values = c("#F8766D", "#00BFC4", "#C77CFF", "#6c00bd"), labels = c("D3 stx", "D7 stx", "D28 stx", "D28 ctx")) + ggtitle(label = "DEG Pathways (CR2-Crry)") + xlab("Pathways") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 140, by = 20), labels = seq(0, 140, by = 20), limits = c(0, 140), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 25, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 15, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 15, margin = margin(r = 5, l = 5)), #axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x= element_blank(), legend.text = element_text(size = 10), legend.title = element_text(size = 12, face = "bold", hjust = 0.5), legend.position = c(0.7, 0.08), legend.background = element_rect(fill="gray90", size = 0.5, linetype="solid", colour ="black"), panel.grid.major.x = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank()) + guides(fill = guide_legend(title = "Group")) + facet_wrap(~ Pathways, scales = "free_x") ### Plot number of DEGs per Pathways in PBS and CR2-Crry DEG_path_pbs_tx <- rbind(DEG_path_pbs, DEG_path_tx) DEG_path_pbs_tx <- DEG_path_pbs_tx %>% mutate(Group = case_when( str_detect(Comparison, "pbs") ~ "PBS", str_detect(Comparison, "stx") ~ "STX", str_detect(Comparison, "ctx") ~ "CTX")) %>% mutate(Day = case_when( str_detect(Comparison, "d3") ~ 3, str_detect(Comparison, "d7") ~ 7, str_detect(Comparison, "d28") ~ 28 )) DEG_path_pbs_tx$Group <- factor(DEG_path_pbs_tx$Group, levels = c("PBS", "STX", "CTX")) path_tx_supp <- ggplot(filter(DEG_path_pbs_tx, !(Group == "STX" & Day == "28")), aes(x = as.factor(Day), y = n, fill = Group)) + geom_bar(stat = "identity", position = "dodge") + geom_text(aes(label = n), position = position_dodge(width = 0.92), vjust = -0.5, size = 2) + scale_fill_manual(values = c("#F8766D", "#00BFC4", "#00BFC4"), labels = c("PBS", "CR2-Crry", "CR2-Crry")) + ggtitle(label = "Effect of CR2-Crry on Differential Expression by Pathway") + xlab("Day") + ylab ("# of Genes") + theme_bw() + scale_y_continuous(breaks = seq(0, 140, by = 20), labels = seq(0, 140, by = 20), limits = c(0, 140), expand = c(0,0)) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 10, b = 10)), axis.title.x = element_blank(), #element_text(face = "bold", size = 6, margin = margin(t = 10, b = 5)), axis.title.y = element_text(face = "bold", size = 8, margin = margin(r = 0, l = 2)), #axis.text.y = element_text(face = "bold", color = "black", size = 10), axis.text.x= element_blank(), legend.text = element_text(size = 7), legend.title = element_text(size = 8, face = "bold", hjust = 0.5), legend.position = c(0.7, 0.08), legend.background = element_rect(fill="gray90", size = 0.5, linetype="solid", colour ="black"), panel.grid.major.x = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank(), strip.text.x = element_text(size = 5, color = "Black", face = "bold")) + guides(fill = guide_legend(title = "Legend")) + facet_wrap(~ Pathways, scales = "free_x") # pdf(file="/.../path_tx_supp.pdf", width = 7.2, height = 7) # grid.draw(path_tx_supp) # dev.off() ### Plot Pathways vs Timepoints with Examples of Genes nb1 <- 1 nb2 <- 1 nb3 <- 2 temp1 %>% filter(rank(-FC) <= 20) rankd3 <- temp1 %>% dplyr::select(gene, FDR, Comparison, Pathways, FC) %>% filter(Comparison == "Day 3" & FDR < 0.05) %>% group_by(Pathways) %>% mutate(rank = rank(FDR), rank2 = rank(FC), rank3 = rank(-FC)) %>% ungroup() %>% filter(rank <= nb1 | rank2 <= nb2 | rank3 <= nb3) rankd7 <- temp1 %>% dplyr::select(gene, FDR, Comparison, Pathways, FC) %>% filter(Comparison == "Day 7" & FDR < 0.05) %>% group_by(Pathways) %>% mutate(rank = rank(FDR), rank2 = rank(FC), rank3 = rank(-FC)) %>% ungroup() %>% filter(rank <= nb1 | rank2 <=nb2 | rank3 <= nb3) rankd28 <- temp1 %>% dplyr::select(gene, FDR, Comparison, Pathways, FC) %>% filter(Comparison == "Day 28" & FDR < 0.05) %>% group_by(Pathways) %>% mutate(rank = rank(FDR), rank2 = rank(FC), rank3 = rank(-FC)) %>% ungroup() %>% filter(rank <= nb1 | rank2 <=nb2 | rank3 <= nb3) medians <- temp1 %>% dplyr::select(gene, FDR, Comparison, Pathways, FC) %>% filter(FDR < 0.05) %>% group_by(Pathways, Comparison) %>% summarize(median = median(FC)) %>% mutate(medianlog = foldchange2logratio(median)) path_fc <- ggplot(data = temp1) + geom_hline(yintercept = 0, linetype = "dashed") + facet_grid(~ Comparison) + geom_point(aes(x = reorder(Pathways, desc(Pathways)), y = log2_FC, color = sig_pattern), size = 0.3) + geom_point(data = medians, aes(x = reorder(Pathways, desc(Pathways)), y = medianlog), color = "black", size = 1, shape = 8) + coord_flip() + theme_bw() + ggtitle(label = "Fold Change of DEGs by Pathway") + xlab("Pathways (23)") + ylab ("Fold Change") + scale_y_continuous(breaks = seq(-2, 5, 1), labels = c("-4", "-2", "±1", "2", "4", "8", "16", "32"), limits = c(-2.9, 6.2), expand = c(0,0)) + scale_color_manual(values = new_colors) + theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9, margin = margin(t = 10, b = 10)), axis.title.x = element_text(face = "bold", size = 8, margin = margin(t = 10, b = 5)), axis.title.y = element_blank(), #element_text(face = "bold", size = 8, margin = margin(r = 5, l = 5)), axis.text.y = element_text(face = "bold", color = "black", size = 5.5), axis.text.x = element_text(face = "bold", color = "black", size = 5), legend.text = element_text(size = 10), legend.title = element_text(size = 10, face = "bold"), legend.position = "none", strip.text.x = element_text(size = 7, color = "Black", face = "bold"), strip.background = element_rect(colour = "black", fill = "gray90")) + geom_text_repel(data = filter(temp_ranked, gene %in% rankd3$gene & Comparison == "Day 3" & FC > 1), aes(x = Pathways, y = log2_FC, label = gene, color = sig_pattern), nudge_x = 0.1, nudge_y = 0, size = 1.5, box.padding = 0.5, point.padding = 0.1, force = 2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000, min.segment.length = 0) + geom_text_repel(data = filter(temp_ranked, gene %in% rankd7$gene & Comparison == "Day 7" & FC > 1), aes(x = Pathways, y = log2_FC, label = gene, color = sig_pattern), nudge_x = 0.1, nudge_y = 0, size = 1.5, box.padding = 0.5, point.padding = 0.1, force = 2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000, min.segment.length = 0) + geom_text_repel(data = filter(temp_ranked, gene %in% rankd28$gene & Comparison == "Day 28" & FC > 1), aes(x = Pathways, y = log2_FC, label = gene, color = sig_pattern), nudge_x = 0.1, nudge_y = 0, size = 1.5, box.padding = 0.5, point.padding = 0.1, force = 2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000, min.segment.length = 0) + geom_text_repel(data = filter(temp_ranked, gene %in% rankd3$gene & Comparison == "Day 3" & FC < 1), aes(x = Pathways, y = log2_FC, label = gene, color = sig_pattern), nudge_x = 0.1, nudge_y = 0, size = 1.5, box.padding = 0.5, point.padding = 0.1, force = 2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000, min.segment.length = 0) + geom_text_repel(data = filter(temp_ranked, gene %in% rankd7$gene & Comparison == "Day 7" & FC < 1), aes(x = Pathways, y = log2_FC, label = gene, color = sig_pattern), nudge_x = 0.1, nudge_y = 0, size = 1.5, box.padding = 0.5, point.padding = 0.1, force = 2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000, min.segment.length = 0) + geom_text_repel(data = filter(temp_ranked, gene %in% rankd28$gene & Comparison == "Day 28" & FC < 1), aes(x = Pathways, y = log2_FC, label = gene, color = sig_pattern), nudge_x = 0.1, nudge_y = 0, size = 1.5, box.padding = 0.5, point.padding = 0.1, force = 2, segment.size = 0.1, segment.alpha = 0.6, show.legend = FALSE, seed = 123, max.iter = 1000, min.segment.length = 0) # pdf(file="/.../path_fc2.pdf", width = 7.2, height = 7) # grid.draw(path_fc) # dev.off()