cluster_colors_AB <- c("#8B63A6","#8B63A6","#d7301f","#d7301f","#0570b0","#0570b0") # purple, red, blue
cluster_colors_glia <- c("#53B848","#53B848","#E8C51E","#E8C51E","#364eA1","#364eA1") # green, yellow, blue
cluster_colors <- c("#0A7B83","#7ABAF2","#FFD265","#595959","#2AA876","#F19C65","#8B63A6","#607D8B","#ff5252","#4C1B1B","#fb9a99")
new.cluster.ids <- c( "L5", "L3-5" ,"WM", "L1-5 Low UMI", "L2-3", "L6", "L1/Astrocytes", "Meninges", "Blood Vessel", "Interneuron", "Mixed glia" )
names(cluster_colors) = new.cluster.ids
Make gene background
niches_db = NICHES::LoadOmniPath("human")
ligand_background = unique(na.omit(unlist(as.list(niches_db$source.subunits))))
receptors_background = unique(na.omit(unlist(as.list(niches_db$target.subunits))))
lr_background = unique(c(ligand_background,receptors_background))
res_df = niches_df_norm$mat_counts %>% rownames_to_column("ligand_receptor")
# Get gene background
lr_sep = "—"
res_df$ligand_receptor = gsub("\"","",res_df$ligand_receptor)
res_df$ligand_receptor = gsub("HLA-","HLA/",res_df$ligand_receptor)
res_df$ligand_receptor = gsub("EXOC3-","EXOC3/",res_df$ligand_receptor)
res_df$ligand_receptor = gsub("PIK3CD-","PIK3CD/",res_df$ligand_receptor)
res_df$ligand_receptor = gsub("IGKV1-","IGKV1/",res_df$ligand_receptor)
res_df$ligand_receptor = gsub("KRTAP4-","KRTAP4/",res_df$ligand_receptor)
fix_gene_names <- function(genelist){
genelist = gsub("HLA/","HLA-",genelist)
genelist = gsub("EXOC3/","EXOC3-",genelist)
genelist = gsub("PIK3CD/","PIK3CD-",genelist)
genelist = gsub("IGKV1/","IGKV1-",genelist)
genelist = gsub("KRTAP4/","KRTAP4-",genelist)
return(genelist)
}
background_genes = unique(unlist(str_split(res_df$ligand_receptor,lr_sep)))
background_genes = unique(unlist(str_split(background_genes,"-")))
background_genes = fix_gene_names(background_genes)
library(HGNChelper)
background_genes = background_genes[which(utf8::utf8_valid(background_genes))]
#background_genes.check = checkGeneSymbols(background_genes)
#background_genes.check[!background_genes.check$Approved,] # 7 invalid
# Get expression matrices (pseudo-bulk)
metadata_pseudo = distinct(niches_df_norm$long[,c("sample_section","Bnum","SpotSet","age","ad_reagan")]) %>%
mutate(rowid = paste0(sample_section,"_",SpotSet)) %>%
column_to_rownames("rowid")
metadata_4deg_RGNpos_RGNneg = metadata_pseudo %>% filter(SpotSet %in% c("RGN_neg","RGN_pos"))
metadata_4deg_RGNpos_RGNneg$SpotSet = factor(metadata_4deg_RGNpos_RGNneg$SpotSet, levels = c("RGN_neg","RGN_pos"))
metadata_4deg_RGNpos_RGNneg_low = metadata_pseudo %>% filter(SpotSet %in% c("low_RGN_neg","low_RGN_pos"))
metadata_4deg_RGNpos_RGNneg_low$SpotSet = factor(metadata_4deg_RGNpos_RGNneg_low$SpotSet, levels = c("low_RGN_neg","low_RGN_pos"))
metadata_4deg_RGNpos_RGNneg_high = metadata_pseudo %>% filter(SpotSet %in% c("high_RGN_neg","high_RGN_pos"))
metadata_4deg_RGNpos_RGNneg_high$SpotSet = factor(metadata_4deg_RGNpos_RGNneg_high$SpotSet, levels = c("high_RGN_neg","high_RGN_pos"))
res_mean_RGNpos_RGNneg = analisys_wrapper(
counts = niches_df_norm$mat_mean[,rownames(metadata_4deg_RGNpos_RGNneg)] ,
metadata_4deg = metadata_4deg_RGNpos_RGNneg, evcodes=T)
res_mean_RGNpos_RGNneg_low = analisys_wrapper(
counts = niches_df_norm$mat_mean[,rownames(metadata_4deg_RGNpos_RGNneg_low)] ,
metadata_4deg = metadata_4deg_RGNpos_RGNneg_low, evcodes=T)
res_mean_RGNpos_RGNneg_high = analisys_wrapper(
counts = niches_df_norm$mat_mean[,rownames(metadata_4deg_RGNpos_RGNneg_high)],
metadata_4deg = metadata_4deg_RGNpos_RGNneg_high, evcodes=T)
save(res_mean_RGNpos_RGNneg, file = paste0(work_dir,"/",run_id,"/res_mean_RGNpos_RGNneg_GM.RData"))
save(res_mean_RGNpos_RGNneg_low, file = paste0(work_dir,"/",run_id,"/res_mean_RGNpos_RGNneg_low_GM.RData"))
save(res_mean_RGNpos_RGNneg_high, file = paste0(work_dir,"/",run_id,"/res_mean_RGNpos_RGNneg_high_GM.RData"))
load(paste0(work_dir,"/",run_id,"/res_mean_RGNpos_RGNneg_GM.RData"))
load(paste0(work_dir,"/",run_id,"/res_mean_RGNpos_RGNneg_low_GM.RData"))
load(paste0(work_dir,"/",run_id,"/res_mean_RGNpos_RGNneg_high_GM.RData"))
Enrichments
enrichments_combined_df = bind_rows(res_mean_RGNpos_RGNneg$enrich_res %>% mutate(contrast = "RGNpos-RNGneg"),
res_mean_RGNpos_RGNneg_low$enrich_res %>% mutate(contrast = "lowRGNpos-lowRNGneg"),
res_mean_RGNpos_RGNneg_high$enrich_res %>% mutate(contrast = "highRGNpos-highRNGneg")) %>%
arrange(-logP) %>%
select(contrast, Direction, source, term_name, p_value, precision, recall, model, intersection) %>% filter(model == "LR")
enrich_selected = read_xlsx(paste0(work_dir,"/../ST_NICHES_RGN_selected.xlsx"))
enrich_selected = enrich_selected$term_name[enrich_selected$highlight=="T"]
createDT(enrichments_combined_df[enrichments_combined_df$p_value<0.05,])
suppressWarnings(ggarrange(res_mean_RGNpos_RGNneg$enrich_res_LR$enrich_p + labs(title = "glia-high vs. glia-low"),
res_mean_RGNpos_RGNneg_low$enrich_res_LR$enrich_p + labs(title = "glia-high vs. glia-low (low Abeta)"),
res_mean_RGNpos_RGNneg_high$enrich_res_LR$enrich_p + labs(title = "glia-high vs. glia-low (high Abeta)"), nrow = 3))
#top_10_UP = enrichments_combined_df[enrichments_combined_df$Direction == "UP",]$term_name[1:5]
#top_10_DOWN = enrichments_combined_df[enrichments_combined_df$Direction == "DOWN",]$term_name[1:5]
#enrich_selected = unique(c(enrich_selected,top_10_UP,top_10_DOWN))
enrichments_combined_df$highlight = enrichments_combined_df$term_name %in% enrich_selected
absmax <- function(x) { x[which.max( abs(x) )]}
absmin <- function(x) { x[which.min( abs(x) )]}
# up_down_mtx = enrichments_combined_df[enrichments_combined_df$highlight,] %>%
# filter(p_value <= 0.05) %>%
# mutate(signed_logP = ifelse(Direction=="DOWN",log10(p_value),-log10(p_value))) %>%
# dplyr::select(term_name,contrast,signed_logP,Direction) %>% distinct() %>%
# pivot_wider(id_cols = term_name, names_from = c(contrast,Direction), values_from = signed_logP) %>%
# column_to_rownames("term_name")
# rg = max(abs(up_down_mtx),na.rm = T)
# pheatmap(up_down_mtx, cluster_rows = F, cluster_cols = F,
# color=colorRampPalette(c("navy", "white", "darkred"))(100),
# breaks = seq(-rg, rg, length.out = 100))
enrich_selected_highlight_pval = enrichments_combined_df[enrichments_combined_df$highlight,] %>%
filter(p_value <= 0.05) %>%
dplyr::select(term_name,contrast,p_value) %>% distinct() %>%
pivot_wider(id_cols = term_name, names_from = contrast, values_from = p_value, values_fn = absmin) %>%
column_to_rownames("term_name")
enrich_selected_highlight_pval[is.na(enrich_selected_highlight_pval)] <- 2
enrich_selected_highlight = enrichments_combined_df[enrichments_combined_df$highlight,] %>%
filter(p_value <= 0.05) %>%
mutate(signed_logP = ifelse(Direction=="DOWN",log10(p_value),-log10(p_value))) %>%
dplyr::select(term_name,contrast,signed_logP) %>% distinct() %>%
pivot_wider(id_cols = term_name, names_from = contrast, values_from = signed_logP, values_fn = absmax) %>%
column_to_rownames("term_name")
enrich_selected_highlight_pval = enrich_selected_highlight_pval[,c("lowRGNpos-lowRNGneg","highRGNpos-highRNGneg","RGNpos-RNGneg")]
enrich_selected_highlight = enrich_selected_highlight[,c("lowRGNpos-lowRNGneg","highRGNpos-highRNGneg","RGNpos-RNGneg")]
colnames(enrich_selected_highlight)[colnames(enrich_selected_highlight)=="RGNpos-RNGneg"] = "glia-high vs. glia-low (All)"
colnames(enrich_selected_highlight)[colnames(enrich_selected_highlight)=="lowRGNpos-lowRNGneg"] = "glia-high vs. glia-low (low AB)"
colnames(enrich_selected_highlight)[colnames(enrich_selected_highlight)=="highRGNpos-highRNGneg"] = "glia-high vs. glia-low (high AB)"
enrich_selected_highlight = as.matrix(enrich_selected_highlight)
(enrich_heatmap = ComplexHeatmap::Heatmap(enrich_selected_highlight,
cell_fun = function(j, i, x, y, width, height, fill) {
if((enrich_selected_highlight_pval[i, j] <= 0.001)) {
grid.text("***", x, y, gp = gpar(fontsize = 8, fontfamily = "sans"))
} else if((enrich_selected_highlight_pval[i, j] <= 0.01)) {
grid.text("**", x, y, gp = gpar(fontsize = 8, fontfamily = "sans"))
} else if((enrich_selected_highlight_pval[i, j] <= 0.05)) {
grid.text("*", x, y, gp = gpar(fontsize = 8, fontfamily = "sans"))
} else if((enrich_selected_highlight_pval[i, j] <= 0.1)) {
grid.text(".", x, y, gp = gpar(fontsize = 8, fontfamily = "sans"))
}
},
na_col = "lightgrey", name = "Signed log10(P)",
col = colorRamp2(c(-max(abs(enrich_selected_highlight),na.rm = T), -2,0,2,
max(abs(enrich_selected_highlight),na.rm = T)),
rev(brewer.pal(n=7, name="RdBu"))[c(1,3,4,5,7)]),
cluster_columns = F, cluster_rows = F, row_names_gp = gpar(fontsize = 9),
column_names_gp = gpar(rotate = 45),
row_names_side = "left", show_row_names = T,
column_names_rot = 45, rect_gp = gpar(col = "white", lwd = 0.1),
row_names_max_width = max_text_width(rownames(enrich_selected_highlight), gp = gpar(fontsize = 9))))

pdf(file = paste0(work_dir,"/Figures/",figDir_id,"_GliaNet_LR_enrichment_heatmap.pdf"), width = 5, height = 7)
enrich_heatmap
dev.off()
## png
## 2
Comparison between contrasts
## Get only LR pairs with expression in example feature plot (B16002_4)
LR_with_expression_in_example = niches_df_norm$long %>% filter(sample_section == "B16002_4") %>%
select(ligand_receptor, estimate_fraction) %>%
distinct() %>% filter(estimate_fraction > 100)
LR_with_expression_in_example = unique(LR_with_expression_in_example$ligand_receptor)
res_combined = res_mean_RGNpos_RGNneg_low$DE_result[,c("ligand_receptor","t","P.adj")] %>%
inner_join(res_mean_RGNpos_RGNneg_high$DE_result[,c("ligand_receptor","t","P.adj")],
suffix = c(".low",".high"),
by = "ligand_receptor")
res_combined = na.omit(res_combined)
res_combined$color = "NS"
res_combined$color[res_combined$P.adj.low<=0.05 & res_combined$P.adj.high<=0.05] = "Both Signf"
res_combined$color[res_combined$P.adj.low<=0.05 & res_combined$P.adj.high>0.05] = "glia-high vs. glia-low (low AB) only"
res_combined$color[res_combined$P.adj.low>0.05 & res_combined$P.adj.high<=0.05] = "glia-high vs. glia-low (high AB) only"
res_combined$color = factor(res_combined$color,
levels = c("Both Signf",
"glia-high vs. glia-low (low AB) only",
"glia-high vs. glia-low (high AB) only",
"NS"))
res_combined = res_combined[order(rowMeans(res_combined[,c("P.adj.high","P.adj.low")])), ]
topList = res_combined[res_combined$color == "Both Signf","ligand_receptor"]
completeList = res_combined[res_combined$color != "NS","ligand_receptor"]
completeList = unique(completeList[completeList %in% LR_with_expression_in_example])
matches <- unique(grep(paste(ADgene_list,collapse="|"), completeList, value=TRUE))
enrichments_combined_df_signif = enrichments_combined_df[enrichments_combined_df$p_value<=0.05,]
genes_l1 = paste(unlist(enrichments_combined_df_signif[grep("autophagy",tolower(enrichments_combined_df_signif$term_name)),]$intersection),collapse = ",")
genes_l1 = unique(str_split(genes_l1,",")[[1]])
print(paste0("autophagy: ",paste0(genes_l1,collapse = ";")))
## [1] "autophagy: UBA52;RPS27A;UBB;UBC;HSPA8;VIM;HSP90AA1;GFAP"
genes_l2 = paste(unlist(enrichments_combined_df_signif[grep("ion binding",tolower(enrichments_combined_df_signif$term_name)),]$intersection),collapse = ",")
genes_l2 = unique(str_split(genes_l2,",")[[1]])
print(paste0("ion binding: ",paste0(genes_l2,collapse = ";")))
## [1] "ion binding: SPARC;SMAP1;LDLR;ACTR2;APOE;ZYX;RPS27A;APP;VLDLR;CALM3;ADCY8;CALM2;ACVR1;NOTCH1;GPC1;COL18A1;GNAS;PRNP;ARF1;NOTCH4;TGFBR1;BMPR1B;PKM;EGFR;SPP1;GNAI2;TYROBP;CALR;ITGA3;HSPA8;TF;ABCA1;S100A1;RYR1;SMAD3;HSP90AA1;GAS6;MERTK;TGFBR2;KCNAB1;ATP1A3;AGRN;EFEMP1;SLIT1;GRM7;RIPK1;TIMP3;CD320;ADAM17;GSTP1;NOTCH2;TIMP2;PSAP;KIT;CACNA1C;RTN4;NELL2;CD4;TRAF2;ADCY9;RTN4R;RYR2;NRXN3"
genes_l3 = paste(unlist(enrichments_combined_df_signif[grep("vesicle",tolower(enrichments_combined_df_signif$term_name)),]$intersection),collapse = ",")
genes_l3 = unique(str_split(genes_l3,",")[[1]])
print(paste0("vesicle: ",paste0(genes_l3,collapse = ";")))
## [1] "vesicle: SPARC;CTTN;LRPAP1;LDLR;ACTR2;APOE;UBA52;ZYX;GFRA1;MIF;CD44;RPS27A;VWF;APP;TSPAN15;UBB;CALM3;ADCY8;CALM2;UBC;AQP1;PDGFB;ARRB2;NOTCH1;GPC1;COL18A1;GNAS;CNTN1;PRNP;ARF1;TGFBR1;ACKR3;PKM;EGFR;SPP1;GNAI2;PLXNA1;TYROBP;CALR;ITGA3;ARPC5;HSPA8;TF;ABCA1;PTPN13;VIM;RYR1;HSP90AA1;GAS6;B2M;SERPINF1;WNT3;ATP1A3;AGRN;CTNNB1;EFEMP1;CD74;RIPK1;CD53;FCGRT;PDGFC;TIMP3;ADCYAP1R1;GSTP1;TIMP2;CLU;GLG1;PSAP;GSTO1;PDGFA;CD63;KIT;RTN4RL1;CD4;TRAF2;OPRM1;CD82;RTN4R;ACKR1;FLRT2;CD81;FN1;FGFRL1;CD9"
genes_l4 = paste(unlist(enrichments_combined_df_signif[grep("cell adhesion",tolower(enrichments_combined_df_signif$term_name)),]$intersection),collapse = ",")
genes_l4 = unique(str_split(genes_l4,",")[[1]])
print(paste0("cell adhesion: ",paste0(genes_l4,collapse = ";")))
## [1] "cell adhesion: PLXNB1;SEMA4D;ITGB1;EPHA7;NOTCH1;JAG2;DSCAM;PTPRT;RET;TNFSF13B;TFRC;LRFN5;LAMA2;ITGAV;ITGB8;FBN1;EFNA5;PTPRM;DCC;PLXNA1;LAMB3;DSCAML1;SHH;EFNB1;TJP1;FLRT3;ITGA6;NRG1;EGFR;CNTN5;COL7A1;TGFB1;ERBB3;CD9;EPHB2;NECTIN3;PVR;PLXNB2;ITGB4;LAMA5;NRXN1;FYN;THY1;LAMB1;CD22;IGSF21;WNT5A;PTPRK;PDGFB;S1PR1;NRP2;NFASC;CD47;SIRPA;TNFRSF14;LRRC4;CDH6;SDC4;CADM4;CADM2;CADM3;LAMA4;LGALS9;FARP2;PLXNA4;CD58;CSF1;CELSR3;CNTN1;NLGN3;SEMA6A;THBS2;RTN4;LRRC4B;ENTPD1;LAMC1;PTPRS;NLGN1;DAG1;PTPRD;NRCAM;NRXN3;EPHB1;EFNB2;EPHB6;CCN3;BCAN;ERBB2;PTPRA;CNTN2;LAMA3;MELTF;NRXN2;CD151;NTN4;DDR1;ADGRL1;TNFSF9;GP1BB;L1CAM;CLDN11;BMP7;NLGN4X;COL18A1;ITGA3;CNTN4;APP;ADORA2A;AZGP1;NECTIN1;UNC5D;EPHA4;TYK2;JAK1;IGSF11;NECTIN2;EPHB3;EFNB3;PRKCZ;PLXNA2;AFDN;NEGR1;ITGB5;ROBO2;NRP1;ADAM10;UNC5C;FLRT2;ICOSLG;NTM;BMP4;NLGN2;NCAM1;NTNG2;STX4;ITGA7;CNTNAP1;VCAN;TNR;SCN1B;LAMB2;GPC4;FGFRL1;ADAM15;F11R;JAM3;CADM1;MAG;SIGLEC8;LSAMP;EFNA1;TNFRSF21;KITLG;SLITRK3;PTPRF;THBS3;CHL1;APLP1;MOG;EPHA3;CD81;ARF6;CNTNAP2;ADAM22;CDH13;NCAM2;ALCAM;OPCML;PAPLN;CD200;ADAM23;PDGFRA;VEGFA;LRRC4C;SEMA5A;PARD3;SPON1;SLC7A1;CD4;FLOT1;AXL;THBS4;OMG;ANXA1;LRRN2;CXCL12;RGMB;JAM2;CD63;PIK3R1;ENG;NTNG1;COL16A1;ANOS1;MDK;ANXA2;ROBO1;MCAM;SLITRK1;COL5A3;NID1;ROBO3;COL6A1;EFEMP2;STX3;LRP6;LRFN4;CCL28;CD83;LAMC3;WNT10B;PLXNC1;NOTCH4;PECAM1;ADAM9;PLXNA3;ATP5F1B;PIK3CA;EPHA8;IL1RAPL1;EPHA2;DLL1;SPON2;ADGRB1;NTN1;SLITRK2;NEO1;MFGE8;PLXND1;PSEN1;SH2B3;VCAM1;EZR;ADAM17;ITGB2;ICAM1;IGF1;THBS1;ITGAM;KDR;FN1;SFRP2;ITGAX;FGF2;HMGB1;CALR;ITGA2B;FAP;PTPRZ1;ITGA1;ITGA8;ITGA5;TJP2;VWF;SPP1;ITGA9;BCAM;ICAM5;CD44;COL6A2;CD93;B2M;CXCR4;SERPINE1;CD74;TNC;BMP2;PRNP;DDR2;SELPLG;BMP6;MDGA1;SIGLEC10;SELL;TGFB2;CLDN5;RELN;CHRD;SDK2;PCDHB7;TEK;BMPR2;SPTBN2;ADAM11;SPTAN1;FGF1;NOTCH3;PTN;GFRA1;SEMA7A"
genes_l5 = paste(unlist(enrichments_combined_df_signif[grep("cell migration",tolower(enrichments_combined_df_signif$term_name)),]$intersection),collapse = ",")
genes_l5 = unique(str_split(genes_l5,",")[[1]])
print(paste0("cell migration: ",paste0(genes_l5,collapse = ";")))
## [1] "cell migration: ADAM17;GNAI2;F2R;ICAM1;IGFBP5;IGF1;THBS1;SEMA6D;KDR;LGALS9;SPARC;ENG;FN1;SFRP2;ITGAX;FLT1;PDGFD;CXCL14;CXCR4;FGF2;HMGB1;CALR;ITGA2B;SERPINE1;ITGAV;DCN;CD74;CD81;TNC;NBL1;BMP2;F2RL1;DDR2;LRP1;JAM3;IGFBP3;LAMA4;MIF;SDC4;DRD2;HGF;ITGA5;IL6R;PDGFB;APP;LAMA2;RTN4;TGFB2;PDGFA;CLDN5;ITGB1;CD47;LAMA5;IFITM1;FGFR1;C5AR1;TGFB1;RELN;CHRD;ITGA6;TLR4;TEK;VCAM1;CD44;ITGB2;VCAN;LAMC1;EDNRA;FAP;F11R;SELPLG;ITGA1;MDGA1;GPC3;SELL;ITGA9;MET;PLXNB1;SEMA4D;FGF7;NOTCH1;PTPRT;RET;TIE1;PTPRM;SEMA3F;PLXNA1;ACVR1B;SHH;TJP1;SEMA4B;EGFR;PTPRG;HBEGF;CD9;EPHB2;SEMA4G;PLXNB2;THY1;LAMB1;WNT5A;PTPRK;S1PR1;NRP2;SEMA4F;TNFRSF14;PLXNA4;CSF1;ERBB4;SEMA6A;CXCL16;ADORA1;FGF17;DAG1;PDGFC;PDGFRB;CCN3;BMPR2;BMPR1A;LAMA3;CD151;BMP7;ITGA3;GRN;UNC5D;EPHA4;SLIT2;PLXNA2;AFDN;NRP1;ADAM10;UNC5C;FLRT2;BMP4;NTNG2;CORT;STX4;TNR;SEMA5B;ADAM15;NRG3;EFNA1;LRIG2;TREM2;IL34;CSF1R;KITLG;NTRK3;ARF6;SEMA6C;CDH13;SEMA3A;SEMA4A;CD200;PDGFRA;VEGFA;SEMA5A;FGF1;THBS4;ANXA1;CXCL12;JAM2;PIK3R1;TGFBR1;NTNG1;IGF1R;MDK;FGF9;ROBO1;MCAM;PTN;SCARB1;ECM1;CCL28;PLXNC1;PECAM1;ADAM9;PLXNA3;GPI;SEMA3B;SORL1;ATP5F1B;FGF18;INSR;EPHA2;WNT5B;SEMA7A;ACVR1C;ADGRB1;NTN1;TBC1D24;MYLK;PLXND1;SEMA6B;EFNB1;DCC;GPC1;FLRT3;ITGB4;FYN;SIRPA;FZD3;NRCAM;EPHB1;EFNB2;CNTN2;NTN4;DDR1;L1CAM;EPHB3;PRKCZ;FGF13;ITGB5;SDC2;ITGA7;GPC4;PTPRF;CHL1;EPHA3;SDC3;AXL;HTR6;CD63;PIK3CA;GFRA1;BAMBI;EPHA8;NEO1;PSEN1;TGFBR3"
genes_l6 = paste(unlist(enrichments_combined_df_signif[grep("myelination",tolower(enrichments_combined_df_signif$term_name)),]$intersection),collapse = ",")
genes_l6 = unique(str_split(genes_l6,",")[[1]])
print(paste0("myelination: ",paste0(genes_l6,collapse = ";")))
## [1] "myelination: GPC1;TGFB1;CD9;ITGB4;NFASC;CNTN1;DAG1;ERBB2;CNTN2;SCN8A;PTPRZ1;CNTNAP1;JAM3;MAG;TNFRSF21;ADAM22;PARD3;LGI4;JAM2;PTN;NCSTN"
genes_fig5 = c("AQP4","SPARC","C3AR1","SYT2","GABRD","PVALB","SCN1B","EEF1A1","RORB","ALDH1A3","TRIM22","SEMA3C","CD68","SERPINE2","ZNF561")
genes_from_pathways = unique(c(genes_l1,genes_l2,genes_l3,genes_l4,genes_l5,genes_l6,genes_fig5))
# top_n = 2
# genes_from_pathways = unique(c(genes_l1[1:top_n],genes_l2[1:top_n],genes_l3[1:top_n],genes_l4[1:top_n],genes_l5[1:top_n],genes_l6[1:top_n],genes_fig5[1:top_n]))
matches_from_pathways <- unique(grep(paste(genes_from_pathways,collapse="|"), completeList, value=TRUE))
genes2show = unique(c(matches_from_pathways))
#gene_list = c("SPARC—ENG","GFAP—APLNR","RPH3A—NRXN1","APP—TSPAN15","SEMA6D—PLXNC1","CLU—TREM2","APOE—LDLR","DSCAM—DCC")
gene_list = c(
"SPARC—ENG", # ;vesicle;cell adhesion;cell migration;Fig5
"GFAP—APLNR", # ;vesicle;cell adhesion;cell migration;AD genes
"RPH3A—NRXN1", # ;cell adhesion
"APP—TSPAN15", # ;vesicle;AD genes
"SEMA6D—PLXNC1", # ;vesicle;cell adhesion;cell migration;AD genes
"CLU—TREM2", # ;ion binding;cell adhesion;cell migration;AD genes
"APOE—LDLR", # ;vesicle;cell adhesion;cell migration;AD genes
"DSCAM—DCC", # ;cell adhesion;cell migration
"PLXNA1—TREM2", # ;vesicle;cell adhesion;cell migration;myelination;AD genes
"TGFB1—ITGAV", # ;cell adhesion;myelination
"TNFRSF14—LRFN5", # ;autophagy;cell adhesion;cell migration
"ADAM15—ITGAV", # ;autophagy;ion binding;cell adhesion;cell migration
"GNB3—GABBR2",
"RIMS1—SLC17A7",
"CXCL16—GRM7",
"APLN—GRM7",
"CCL28—GRM7",
"CORT—GRM7"
)
topList1 = res_combined[res_combined$color == "Both Signf","ligand_receptor"]
topList2 = res_combined[res_combined$color == "glia-high vs. glia-low (low AB) only","ligand_receptor"]
topList3 = res_combined[res_combined$color == "glia-high vs. glia-low (high AB) only","ligand_receptor"]
topList4 = res_combined[res_combined$color != "NS" & res_combined$t.low<0 & res_combined$t.high>0,"ligand_receptor"]
topList5 = res_combined[res_combined$color == "glia-high vs. glia-low (low AB) only" & res_combined$t.low>0,"ligand_receptor"]
genes2show = unique(c(gene_list,topList1[1:2],topList2[1:3],topList3[1:3],topList4[1:6],topList5[1]))
res_combined$gene2label = ""
res_combined$gene2label[res_combined$ligand_receptor %in% genes2show] = res_combined$ligand_receptor[res_combined$ligand_receptor %in% genes2show]
res_combined$gene2label = gsub("—","-",res_combined$gene2label)
cluster_colors_AB <- c("#8B63A6","#8B63A6","#d7301f","#d7301f","#0570b0","#0570b0") # purple, red, blue
cluster_colors_glia <- c("#53B848","#53B848","#E8C51E","#E8C51E","#364eA1","#364eA1") # green, yellow, blue
signif_colors = setNames(rev(c("grey","#364eA1","#E8C51E","#53B848")), levels(res_combined$color))
(scatter_p = ggplot(res_combined, aes(x = t.low, y = t.high)) +
geom_abline(color = "darkgray", linetype = "dashed") +
geom_point(alpha = 0.25, aes(color = color),
show.legend = T) +
geom_text_repel(aes(label = gene2label, color = color),
show.legend = F, segment.alpha = .3,
max.overlaps = Inf, size = 3.5, force = 10,
fontface = "bold.italic") +
scale_color_manual(values = signif_colors) +
stat_cor(method = "pearson", size = 4.5) +
theme_test(base_size = 13) +
guides(color =
guide_legend(label.position = "left",
label.hjust = 1,
override.aes = list(size = 4, alpha = 1, fill = NA))) +
theme(legend.position = c(0.8, 0.12),
legend.title.align = 1,
legend.background=element_rect(fill = alpha("white", 0)),
legend.key.size = unit(0.45, "cm")) +
labs(x = "glia-high vs. glia-low (low AB) (t-statistics)",
y = "glia-high vs. glia-low (high AB) (t-statistics)",
title = "",
color = "Significance"))

save(res_combined, file = paste0(work_dir,"/Figures/",figDir_id,"_GliaNet_LR_combined_results.RData"))
pdf(file = paste0(work_dir,"/Figures/",figDir_id,"_GliaNet_LR_low_high_scatter.pdf"), width = 8, height = 6)
scatter_p
dev.off()
## png
## 2
Examples of LR pairs
res_combined_sel = res_combined %>% filter(gene2label != "")
res_combined_sel$source = ""
res_combined_sel$source[grep(paste(genes_l1,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_l1,collapse="|"),res_combined_sel$gene2label)],";autophagy")
res_combined_sel$source[grep(paste(genes_l2,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_l2,collapse="|"),res_combined_sel$gene2label)],";ion binding")
res_combined_sel$source[grep(paste(genes_l3,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_l3,collapse="|"),res_combined_sel$gene2label)],";vesicle")
res_combined_sel$source[grep(paste(genes_l4,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_l4,collapse="|"),res_combined_sel$gene2label)],";cell adhesion")
res_combined_sel$source[grep(paste(genes_l5,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_l5,collapse="|"),res_combined_sel$gene2label)],";cell migration")
res_combined_sel$source[grep(paste(genes_l6,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_l6,collapse="|"),res_combined_sel$gene2label)],";myelination")
res_combined_sel$source[grep(paste(genes_fig5,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(genes_fig5,collapse="|"),res_combined_sel$gene2label)],";Fig5")
res_combined_sel$source[grep(paste(ADgene_list,collapse="|"),res_combined_sel$gene2label)] = paste0(res_combined_sel$source[grep(paste(ADgene_list,collapse="|"),res_combined_sel$gene2label)],";AD genes")
createDT(res_combined_sel)
gene_list2 = c("GFAP-APLNR",
"SPARC-ENG",
"CLU-TREM2",
"RPH3A-NRXN1",
"APP-TSPAN15",
"PLXNA1-TREM2",
"DSCAM-DCC",
"TGFB1-ITGAV",
"APOE-LDLR",
"GNB3—GABBR2",
"RIMS1—SLC17A7",
"CXCL16—GRM7")
gene_list2 = gsub("-","—",gene_list2)
library(tidyverse)
library(data.table)
library(reshape2)
prep4boxplot = function(counts_voom,contrast = ""){
pseudo_bulk_mtx = counts_voom %>% as.data.frame()
mtx_1_long = setNames(melt(as.matrix(pseudo_bulk_mtx)),c("gene","sample","expression")) %>%
mutate(strat = gsub("(.*?)_(.*?)_(.*)","\\3",sample),
sample_section = gsub("(.*?)_(.*?)_(.*)_(.*)","\\1_\\2",sample))
mtx_1_long$contrast = contrast
return(mtx_1_long)
}
mtx_voom_long = bind_rows(prep4boxplot(res_mean_RGNpos_RGNneg$counts_voom, contrast = "glia-high vs. glia-low (combined)"),
prep4boxplot(res_mean_RGNpos_RGNneg_low$counts_voom, contrast = "glia-high vs. glia-low (low AB)"),
prep4boxplot(res_mean_RGNpos_RGNneg_high$counts_voom, contrast = "glia-high vs. glia-low (high AB)"))
mtx_voom_long2plot_Pvalues = res_df_model %>%
dplyr::filter(ligand_receptor %in% gene_list2) %>%
select(ligand_receptor,P.adj,model) %>%
pivot_wider(names_from = model,values_from = P.adj)
colnames(mtx_voom_long2plot_Pvalues)[colnames(mtx_voom_long2plot_Pvalues) == "glia-high vs. glia-low"] = "combined"
colnames(mtx_voom_long2plot_Pvalues)[colnames(mtx_voom_long2plot_Pvalues) == "glia-high vs. glia-low (high Abeta)"] = "high AB"
colnames(mtx_voom_long2plot_Pvalues)[colnames(mtx_voom_long2plot_Pvalues) == "glia-high vs. glia-low (low Abeta)"] = "low AB"
mtx_voom_long2plot_Pvalues_long = mtx_voom_long2plot_Pvalues %>%
pivot_longer(cols = -ligand_receptor, names_to = "contrast", values_to = "P.adj")
mtx_voom_long2plot = mtx_voom_long %>%
dplyr::filter(gene %in% gene_list2)
mtx_voom_long2plot$strat[mtx_voom_long2plot$strat=="RGN_neg"] <- "glia-low (combined)"
mtx_voom_long2plot$strat[mtx_voom_long2plot$strat=="RGN_pos"] <- "glia-high (combined)"
mtx_voom_long2plot$strat[mtx_voom_long2plot$strat=="low_RGN_neg"] <- "glia-low (low AB)"
mtx_voom_long2plot$strat[mtx_voom_long2plot$strat=="low_RGN_pos"] <- "glia-high (low AB)"
mtx_voom_long2plot$strat[mtx_voom_long2plot$strat=="high_RGN_neg"] <- "glia-low (high AB)"
mtx_voom_long2plot$strat[mtx_voom_long2plot$strat=="high_RGN_pos"] <- "glia-high (high AB)"
mtx_voom_long2plot$contrast = gsub("(.*) \\((.*)\\)","\\2",mtx_voom_long2plot$contrast)
mtx_voom_long2plot$contrast_strat = paste0(mtx_voom_long2plot$contrast,"_",mtx_voom_long2plot$strat)
mtx_voom_long2plot$contrast_strat_labels = gsub("(.*)_(.*) \\((.*)","\\2",mtx_voom_long2plot$contrast_strat)
contrast_strat_labels = mtx_voom_long2plot$contrast_strat_labels
names(contrast_strat_labels) = mtx_voom_long2plot$contrast_strat
cluster_colors_AB <- c("#8B63A6","#8B63A6","#d7301f","#d7301f","#0570b0","#0570b0") # purple, red, blue
cluster_colors_glia <- c("#53B848","#53B848","#E8C51E","#E8C51E","#364eA1","#364eA1") # green, yellow, blue
signif_colors = setNames(rev(c("#364eA1","#E8C51E","#53B848")), unique(mtx_voom_long2plot$contrast))
mtx_voom_long2plot = mtx_voom_long2plot %>%
left_join(mtx_voom_long2plot_Pvalues_long, by = c("contrast","gene"="ligand_receptor"))
# convert pvalues to asterisk code
mtx_voom_long2plot$signif = ifelse(mtx_voom_long2plot$P.adj <= 0.001,"***",
ifelse(mtx_voom_long2plot$P.adj <= 0.01,"**",
ifelse(mtx_voom_long2plot$P.adj <= 0.05,"*","")))
mtx_voom_long2plot_labels = mtx_voom_long2plot %>%
dplyr::group_by(gene,contrast,contrast_strat) %>%
dplyr::summarize(p.adj = min(P.adj)) %>%
mutate(signif = ifelse(p.adj <= 0.001,"***",
ifelse(p.adj <= 0.01,"**",
ifelse(p.adj <= 0.05,"*",""))))
mtx_voom_long2plot$gene = gsub("—","-",mtx_voom_long2plot$gene)
mtx_voom_long2plot_labels$gene = gsub("—","-",mtx_voom_long2plot_labels$gene)
mtx_voom_long2plot_labels$gene = factor(mtx_voom_long2plot_labels$gene,
levels = gsub("—","-",gene_list2))
mtx_voom_long2plot$gene = factor(mtx_voom_long2plot$gene,
levels = gsub("—","-",gene_list2))
# mtx_voom_long2plot_labels$`.y.` = "expression"
# mtx_voom_long2plot_labels$group1 = "glia-high"
# mtx_voom_long2plot_labels$group1 = "glia-low"
# mtx_voom_long2plot_labels$p = mtx_voom_long2plot_labels$p.adj
# mtx_voom_long2plot_labels$`p.signif` = mtx_voom_long2plot_labels$signif
# mtx_voom_long2plot_labels$contrast_strat = gsub("(.*)_(.*) \\((.*)","\\2",mtx_voom_long2plot_labels$contrast_strat)
# mtx_voom_long2plot_labels = unique(mtx_voom_long2plot_labels)
mtx_voom_long2plot$contrast = factor(mtx_voom_long2plot$contrast,
levels = c("combined","low AB","high AB"))
mtx_voom_long2plot$contrast_strat = factor(mtx_voom_long2plot$contrast_strat,
levels = c("combined_glia-high (combined)",
"combined_glia-low (combined)",
"low AB_glia-high (low AB)",
"low AB_glia-low (low AB)",
"high AB_glia-high (high AB)",
"high AB_glia-low (high AB)"))
(p = ggplot(mtx_voom_long2plot,
aes(x = contrast_strat, y = expression, fill = contrast)) +
geom_boxplot(outlier.shape = NA) +
ggbeeswarm::geom_quasirandom(alpha = .2) +
# stat_pvalue_manual(mtx_voom_long2plot_labels, label = "p.signif") +
geom_text(data = mtx_voom_long2plot_labels %>% filter(grepl("glia-high",contrast_strat)),
aes(label = signif,
x = stage(contrast_strat, after_stat = x + 0.45),
y = Inf),
color = "black",
hjust = 0.5, vjust = 1, size = 20/.pt) +
scale_x_discrete(labels = contrast_strat_labels) +
scale_y_continuous(breaks = scales::breaks_extended(3)) +
scale_fill_manual(values = signif_colors) +
facet_wrap(~gene, ncol = 3, scales = "free") +
theme_classic(base_size = 20) +
theme(panel.border = element_blank(),
axis.text.x = element_text(size = 14),
strip.text = element_text(size=20, colour="black",face = "italic"),
strip.background = element_rect(colour="white", fill="white")) +
easy_rotate_x_labels(angle = 20, side = "right") +
labs(y = "Normalized expression", x = "", fill = "") +
easy_move_legend(to = "bottom") +
easy_change_legend(what = "direction", to = "horizontal"))

pdf(file = paste0(work_dir,"/Figures/",figDir_id,"_GliaNet_LR_boxplots.pdf"), width = 14, height = 12)
p
dev.off()
## png
## 2