├── .DS_Store ├── .Rbuildignore ├── .Rhistory ├── .gitignore ├── DESCRIPTION ├── GBMatlas.Rproj ├── Meta_Data_GBMatlas.txt ├── NAMESPACE ├── R ├── .DS_Store ├── Figure1.R ├── Figure2.R ├── Figure3.R ├── Figure4.R ├── Figure5.R ├── Figure6.R ├── suppfig4a heatmap.R ├── suppfig7d dotplot.R └── suppfigs.R ├── README.md ├── Rcode └── UMAP_Data_GBMatlas.txt /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/parveendabas/GBMatlas/f1d0d59076134680e890476cd4710b005a581a65/.DS_Store -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^GBMatlas\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.Rhistory: -------------------------------------------------------------------------------- 1 | library(ggplot2) 2 | library(gridExtra) 3 | library(grid) 4 | library(gtable) 5 | library(data.table) 6 | library(cowplot) 7 | library(plyr) 8 | library(reshape2) 9 | library(tidyr) 10 | library(pheatmap) 11 | library(RColorBrewer) 12 | library(tidyverse) 13 | library(DESeq2) 14 | library(colorspace) 15 | library(splines) 16 | library(AnnotationDbi) 17 | library(dplyr) 18 | library(gridBase) 19 | library(ggrepel) 20 | library(calibrate) 21 | library(futile.matrix) 22 | library(monocle) 23 | install.packages("futile.matrix") 24 | library(Seurat) 25 | library(Matrix) 26 | library(stringr) 27 | library(useful) 28 | library(ggplot2) 29 | library(gridExtra) 30 | library(grid) 31 | library(gtable) 32 | library(data.table) 33 | library(cowplot) 34 | library(plyr) 35 | library(reshape2) 36 | library(tidyr) 37 | library(pheatmap) 38 | library(RColorBrewer) 39 | library(tidyverse) 40 | library(colorspace) 41 | library(splines) 42 | library(AnnotationDbi) 43 | library(dplyr) 44 | library(gridBase) 45 | library(ggrepel) 46 | library(calibrate) 47 | library(ggpubr) 48 | library(Seurat) 49 | library(Matrix) 50 | library(stringr) 51 | library(useful) 52 | library(ggplot2) 53 | library(gridExtra) 54 | library(grid) 55 | library(gtable) 56 | library(data.table) 57 | library(cowplot) 58 | library(plyr) 59 | library(reshape2) 60 | library(tidyr) 61 | library(pheatmap) 62 | library(RColorBrewer) 63 | library(tidyverse) 64 | library(colorspace) 65 | library(splines) 66 | library(AnnotationDbi) 67 | library(dplyr) 68 | library(gridBase) 69 | library(ggrepel) 70 | library(calibrate) 71 | library(ggpubr) 72 | library(Seurat) 73 | library(Matrix) 74 | library(stringr) 75 | library(useful) 76 | library(ggplot2) 77 | library(gridExtra) 78 | library(grid) 79 | library(gtable) 80 | library(data.table) 81 | library(cowplot) 82 | library(plyr) 83 | library(reshape2) 84 | library(tidyr) 85 | library(pheatmap) 86 | library(RColorBrewer) 87 | library(tidyverse) 88 | library(DESeq2) 89 | library(colorspace) 90 | library(splines) 91 | library(AnnotationDbi) 92 | library(dplyr) 93 | library(gridBase) 94 | library(ggrepel) 95 | library(calibrate) 96 | library(futile.matrix) 97 | ## Oct 15 98 | ##Analysis of residuals 99 | #Permutation 100 | ## two-sample equal-variances t-test: 101 | rm(list=ls()) 102 | set.seed(1) 103 | dat <- mtcars 104 | dat <- dat[dat$cyl %in% c(4, 8), c('mpg', 'cyl')] 105 | (x <- dat$mpg[dat$cyl == 4]) 106 | (y <- dat$mpg[dat$cyl == 8]) 107 | (fit <- t.test(x=x, y=y, var.equal=T)) 108 | fit$statistic 109 | ## but neither dataset is large or normally distributed: 110 | par(mfrow=c(1, 2)) 111 | qqnorm(x, main='x') 112 | qqline(x) 113 | qqnorm(y, main='y') 114 | qqline(y) 115 | ## however homogeneous enough to try permutation: 116 | bartlett.test(mpg ~ cyl, data = dat) 117 | ## Set up permutation 118 | R <- 9999 119 | rslts <- rep(as.numeric(NA), R) 120 | for(i in R){ 121 | dat.i <- dat 122 | dat.i$cyl <- sample(dat.i$cyl, nrow(dat.i), replace = F) 123 | x.i <- dat.i$mpg[dat.i$cyl == 4] 124 | y.i <- dat.i$mpg[dat.i$cyl == 8] 125 | fit.i <- t.test(x=x.i, y=y.i, var.equal = T) 126 | rslts[i] <- fit.i$statistic 127 | } 128 | x ## original values for 1st group 129 | y ## original values for 2d group 130 | x.i ## same length as x, but values from both grps 131 | y.i ## same length as y, but values from both grps 132 | ?sample 133 | dat.i <- dat 134 | dat.i 135 | dat.i$cyl <- sample(dat.i$cyl, nrow(dat.i), replace = F) 136 | dat.i$cyl 137 | install.packages("bbot") 138 | install.packages("boot") 139 | library(boot) 140 | ?GetAssayData 141 | rm(list=ls()) # clear workspace 142 | GroupName <- "Patient" 143 | ClusterGroup <- "PatientALL" 144 | RDSname <- paste0("Seurat_Formatted_Normalized_USE_GBM_",ClusterGroup) 145 | pkWD <- "/Users/kumarpa/Desktop/Work/Jax/Kyuson/Manuscript_Draft" 146 | RDSdir <- paste0("/Users/kumarpa/Desktop/Work/Jax/Kyuson/Bill_Pipeline_Data/Data_Input/",ClusterGroup) 147 | ColToUse <- "Custom.Cluster" 148 | Suffix <- "PatientALL" 149 | ## Change Figure1 as well 150 | NameInPdf.main <- paste0(ColToUse,"_Based_",Suffix) 151 | IdentToSubsetColName="Custom.Cluster" 152 | IdentToSubset="NA" 153 | downsampleHeatmap=25000 154 | FDR=0.1 155 | FoldChangeCutoff=1.4 156 | setwd(pkWD) 157 | plotWD <- paste(getwd(),paste0("For_Github_Main_Figures"),sep="/"); print(plotWD) 158 | dir.create(file.path(getwd(),paste0("For_Github_Main_Figures")), showWarnings = FALSE) 159 | setwd(plotWD) 160 | plotWD1 <- paste(getwd(),paste0("Figure1_",Suffix),sep="/"); print(plotWD1) 161 | dir.create(file.path(getwd(),paste0("Figure1_",Suffix)), showWarnings = FALSE) 162 | #Custom.Cluster = c(`Cluster01` = "#0000ee", `Cluster02` = "#27408B", `Cluster03` = "#56B4E9", `Cluster14` = "#00ffff", `Cluster04` = "#00ff00", `Cluster05` = "#DEB887", `Cluster06` = "#008000", `Cluster07` = "#bf3eff", `Cluster08` = "#ff0000", `Cluster09` = "#8b0000", `Cluster10` = "#CD5C5C", `Cluster11` = "#ff80bf", `Cluster12` = "#F0E442", `Cluster13` = "#ffff00"), 163 | ClusOrder.main <- c("Cluster01", "Cluster02", "Cluster03", "Cluster04", "Cluster05", "Cluster06", "Cluster07", "Cluster08", "Cluster09", "Cluster10", "Cluster11", "Cluster12", "Cluster13", "Cluster14"); ClusOrder.main 164 | ClusOrder <- c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"); ClusOrder 165 | cbPalette.Cluster <- c("darkblue", "#0073ff" ,"#009FFF" ,"#00EEFF", "#FF0073", "#FF00D3" ,"#C900FF" ,"#FF8C00" ,"#FF5900", "#FF2500" ,"darkred" ,"#5FFF00", "#AFFF00" ,"#FFFF00") 166 | #, "#CAFF70", "#b5d4ff", "#000000", "#787878", "#999999", "#D3D3D3" 167 | # Has 20 colors cbPalette.Cluster <- c("#0000ee", "#27408B", "#56B4E9", "#ff0000", "#CD5C5C", "#DEB887", "#008000", "#E69F00", "#00ffff", "#F0E442", "#bf3eff", "#00ff00", "#ff80bf", "#ffff00", "#CAFF70", "#b5d4ff", "#000000", "#787878", "#999999", "#D3D3D3") 168 | #cbPalette.CT <- c("#0000ee", "#ff0000", "#008000", "#E69F00", "#00ffff", "#F0E442", "#bf3eff", "#00ff00", "#ff80bf", "#00EE00", "#FF0000", "#787878", "#999999", "#D3D3D3", "#e066ff", "#b452cd") 169 | #cbPalette.CT <- c("#00008b", "#87ceff", "#009acd", "#8470ff", "#cd5b45", "#ff7f50", "#ff0000", "#8b0000", "#cdad00", "#FFD700", "#eee685", "#FFFF00", "#008b00", "#00ff00", "#6b8e23", "#20B2AA") 170 | CTOrder <- c("Glioma", "Pericyte", "Tcells", "Myeloid", "Oligo", "Mix"); CTOrder 171 | cbPalette.CT <- c("darkblue", "#FF00D3", "#C900FF", "darkred", "#5FFF00", "grey") 172 | GroupOrder.temp = c("CNSTM-068", "CNSTM-070", "CNSTM-081", "CNSTM-096") 173 | GroupPalette.temp <- c("#0039d1","#c20f00","purple","#26cc00") 174 | FragOrder = c("CNSTM-068-A", "CNSTM-068-B", "CNSTM-068-C", "CNSTM-068-D", "CNSTM-070-A", "CNSTM-070-C", "CNSTM-070-D", "CNSTM-070-F", "CNSTM-081-A", "CNSTM-081-B", "CNSTM-081-C", "CNSTM-081-D", "CNSTM-096-1", "CNSTM-096-2", "CNSTM-096-4", "CNSTM-096-5") 175 | FragPalette <- c("#0000FF", "#0045FF", "#008AFF" ,"#00CFFF", "#FF9A00", "#FF6E00", "#FF1400" ,"darkred" ,"#FF0052", "#FF00A6", "#FF00F9" ,"#B000FF","darkgreen","#30FF00","#B9FF00", "#FFFF00") 176 | PredictionOrder <- c("diploid", "aneuploid") 177 | PredictionPalette <- c("#ff7f50", "#009FFF") 178 | setwd(RDSdir) 179 | FullInfo <- read.table(file = paste0("Full_Information_",ClusterGroup,"_Cells.txt"), header = T, sep = "\t"); head(FullInfo); dim(FullInfo) 180 | setwd(RDSdir) 181 | ?GetAssayData 182 | SCdata.main <- readRDS(paste0(RDSname,".rds")) 183 | ?GetAssayData 184 | SCdata.main 185 | head(SCdata.main@meta.data) 186 | dim(GetAssayData(object = SCdata.main, slot = "scale.data")) 187 | head(SCdata.main@meta.data) 188 | dim(GetAssayData(object = SCdata.main, slot = "scale.data")) 189 | library(Seurat) 190 | dim(GetAssayData(object = SCdata.main, slot = "scale.data")) 191 | SCdata.main$Custom.Cluster <- gsub(" ", "",SCdata.main$Custom.Cluster) 192 | SCdata.main$prediction <- gsub("tumor", "aneuploid",SCdata.main$prediction) 193 | SCdata.main$prediction <- gsub("normal", "diploid",SCdata.main$prediction) 194 | SCdata.main$prediction <- factor(SCdata.main$prediction, levels = PredictionOrder) 195 | Idents(SCdata.main) <- ColToUse 196 | DefaultAssay(SCdata.main) <- "RNA" 197 | ### Figure1 Clsuter and heatmaps #"#787878", "#9A6324", "#ee1289", 198 | ToUsePallete <- cbPalette.Cluster 199 | ToUseOrder <- ClusOrder.main 200 | ToUseCol <- ColToUse 201 | MarkerDir <- paste0("/Users/kumarpa/Desktop/Work/Jax/Kyuson/Bill_Pipeline_Data/DGEs/DGEs_",ToUseCol,"_Based_",Suffix) 202 | setwd(MarkerDir) 203 | MarkerGenes <- read.table(file = paste0("DEGs_Heatmap_",ToUseCol,"_Based_",Suffix,".txt"), header = T, sep = "\t"); head(MarkerGenes); dim(MarkerGenes) 204 | markers <- MarkerGenes 205 | markers <- Remove_Genes_Rp_mt_Rna_Human(markers) 206 | #markers <- Remove_Genes_Rp_mt_Rna_Human(markers) 207 | markers <- markers[markers$p_val_adj < FDR,]; dim(markers) 208 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 209 | #library(cowplot) 210 | #library(plyr) 211 | #library(reshape2) 212 | #library(tidyr) 213 | #library(pheatmap) 214 | #library(RColorBrewer) 215 | #library(tidyverse) 216 | #library(colorspace) 217 | #library(splines) 218 | #library(AnnotationDbi) 219 | library(dplyr) 220 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 221 | #library(gridExtra) 222 | #library(grid) 223 | #library(gtable) 224 | #library(data.table) 225 | #library(cowplot) 226 | #library(plyr) 227 | #library(reshape2) 228 | #library(tidyr) 229 | #library(pheatmap) 230 | #library(RColorBrewer) 231 | library(tidyverse) 232 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 233 | #library(Matrix) 234 | #library(stringr) 235 | #library(useful) 236 | library(ggplot2) 237 | #library(gridBase) 238 | #library(ggrepel) 239 | #library(calibrate) 240 | #library(ggplotify) 241 | #library(scales) 242 | library(viridis) 243 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 244 | #library(gridExtra) 245 | #library(grid) 246 | #library(gtable) 247 | #library(data.table) 248 | #library(cowplot) 249 | library(plyr) 250 | #library(colorspace) 251 | #library(splines) 252 | #library(AnnotationDbi) 253 | library(dplyr) 254 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 255 | #library(reshape2) 256 | library(tidyr) 257 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 258 | library(Matrix) 259 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 260 | #library(gridExtra) 261 | #library(grid) 262 | #library(gtable) 263 | library(data.table) 264 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 265 | #markers <- Remove_Genes_Rp_mt_Rna_Human(markers) 266 | markers <- markers[markers$p_val_adj < FDR,]; dim(markers) 267 | markers <- markers[!rownames(markers) %like% "^RP[SL]",]; dim(markers) 268 | markers <- markers[!rownames(markers) %like% "^RP11",]; dim(markers) 269 | markers <- markers[!rownames(markers) %like% "^MT",]; dim(markers) 270 | markers <- markers[!rownames(markers) %like% "^RNA",]; dim(markers) 271 | topnumber=30 272 | top <- markers %>% group_by(cluster) %>% top_n(n = topnumber, wt = avg_logFC); dim(top) 273 | topFDR <- markers %>% dplyr::arrange(p_val_adj, desc(avg_logFC)) %>% dplyr::group_by(cluster) %>% dplyr::slice(1:topnumber); dim(topFDR) 274 | setwd(MarkerDir) 275 | temp <- as.data.frame(topFDR) 276 | temp.unique <- temp[!duplicated(temp$gene),] 277 | write.table(temp, file = paste0("DEGs_Heatmap_",ToUseCol,"_Based_",Suffix,"_Top",topnumber,"_Genes.txt"),quote=F,sep="\t") 278 | write.table(temp.unique, file = paste0("DEGs_Heatmap_",ToUseCol,"_Based_",Suffix,"_Top",topnumber,"_Genes_UNIQUE.txt"),quote=F,sep="\t") 279 | SCdata.main$Temp <- "Temp" 280 | Idents(SCdata.main) <- "Temp" 281 | SCdata.temp.Heatmap <- subset(SCdata.main, downsample=downsampleHeatmap) 282 | #SCdata.temp.Heatmap <- subset(SCdata.main, downsample=10) 283 | Idents(SCdata.main) <- ToUseCol 284 | Idents(SCdata.main) <- factor(Idents(SCdata.main), levels= ToUseOrder) 285 | SCdata.temp.Heatmap <- ScaleData(object = SCdata.temp.Heatmap, verbose = FALSE, features = markers$gene, scale.max = 2) 286 | dtype="scale.data" 287 | nor.exp <- GetAssayData(object = SCdata.temp.Heatmap, slot = dtype); print(dim(nor.exp)) 288 | UseGenes <- intersect(markers$gene, rownames(nor.exp)); length(UseGenes) 289 | nor.exp <- nor.exp[UseGenes,,drop=FALSE]; dim(nor.exp) 290 | table(SCdata.temp.Heatmap@meta.data[,ToUseCol], SCdata.temp.Heatmap@meta.data[,GroupName]) 291 | meta.data.plot <- SCdata.temp.Heatmap@meta.data[,c(GroupName, ColToUse)] 292 | meta.data.plot[,ColToUse] <- factor(meta.data.plot[,ColToUse], levels = ToUseOrder) 293 | meta.data.plot[,GroupName] <- factor(meta.data.plot[,GroupName], levels = GroupOrder.temp) 294 | meta.data.plot <- meta.data.plot[order(factor(meta.data.plot[,ColToUse], levels = ToUseOrder), factor(meta.data.plot[,GroupName], levels = GroupOrder.temp)),]; head(meta.data.plot) 295 | meta.data.plot[,ColToUse] <- as.character(meta.data.plot[,ColToUse]) 296 | nor.exp <- nor.exp[,rownames(meta.data.plot), drop=FALSE] 297 | colnames(meta.data.plot) <- c("Patient", "Cluster") 298 | print(dim(nor.exp)) 299 | Custom.Cluster.FULL = cbPalette.Cluster[1:length(ClusOrder.main)]; names(Custom.Cluster.FULL) <- ClusOrder.main; Custom.Cluster.FULL 300 | Fragment.FULL = FragPalette[1:length(FragPalette)]; names(Fragment.FULL) <- FragPalette; Fragment.FULL 301 | prediction.FULL = PredictionPalette[1:length(PredictionOrder)]; names(prediction.FULL) <- PredictionOrder; prediction.FULL 302 | Patient.FULL = GroupPalette.temp[1:length(GroupOrder.temp)]; names(Patient.FULL) <- GroupOrder.temp; Patient.FULL 303 | ann_colors = list( 304 | Cluster = Custom.Cluster.FULL[names(Custom.Cluster.FULL) %in% as.character(unique(meta.data.plot$Cluster))], 305 | Fragment = Fragment.FULL[names(Fragment.FULL) %in% as.character(unique(meta.data.plot$Fragment))], 306 | prediction = prediction.FULL[names(prediction.FULL) %in% as.character(unique(meta.data.plot$prediction))], 307 | Patient = Patient.FULL[names(Patient.FULL) %in% as.character(unique(meta.data.plot$Patient))] 308 | ) 309 | ### Lighter Shade of blue near Zero 310 | colors <- c(seq(-2,2,by=0.01)) 311 | my_palette <- c(colorRampPalette(colors = c("darkblue", "#a7c5f2", "#e6f0f5", "gray97", "darksalmon", "orangered3", "darkred")) (n = length(colors))) 312 | Temp <- pheatmap(nor.exp[as.character(unique(topFDR$gene)),],annotation_col=meta.data.plot,show_colnames=F,show_rownames=T, color=my_palette, breaks=colors, 313 | annotation_colors = ann_colors, cluster_rows = FALSE, cluster_cols = FALSE, fontsize = 10, fontsize_row=9); dev.off() 314 | library(pheatmap) 315 | Temp <- pheatmap(nor.exp[as.character(unique(topFDR$gene)),],annotation_col=meta.data.plot,show_colnames=F,show_rownames=T, color=my_palette, breaks=colors, 316 | annotation_colors = ann_colors, cluster_rows = FALSE, cluster_cols = FALSE, fontsize = 10, fontsize_row=9); dev.off() 317 | sigGenes = c("PDGFRA", "OLIG1", "GFAP", "SOX2", "ACTA2", "CD3G", "S100A9", "S100A4", "ITGAM", "CD68", "APOE", "HLA-DRA", "P2RY12", "CCL3", "MBP") 318 | p4 <- Gene.Labels.pheatmap(Temp, kept.labels = sigGenes, repel.degree = 0) 319 | source("/Users/kumarpa/Desktop/Work/Jax/Scripts/Scripts_Kyuson/Functions_Kyuson_Project_GitHub.R") 320 | p4 <- Gene.Labels.pheatmap(Temp, kept.labels = sigGenes, repel.degree = 0) 321 | library(gridExtra) 322 | library(grid) 323 | library(gtable) 324 | p4 <- Gene.Labels.pheatmap(Temp, kept.labels = sigGenes, repel.degree = 0) 325 | setwd(plotWD1) 326 | pdf(paste0("Figure1_",ToUseCol,"_",Suffix,"_Panel_b_HEATMAP_UPLOAD.pdf"), height = 8, width = 6) 327 | print(plot_grid(p4)) 328 | dev.off() 329 | library(gridBase) 330 | setwd(plotWD1) 331 | pdf(paste0("Figure1_",ToUseCol,"_",Suffix,"_Panel_b_HEATMAP_UPLOAD.pdf"), height = 8, width = 6) 332 | print(plot_grid(p4)) 333 | dev.off() 334 | library(gridBase) 335 | #library(calibrate) 336 | library(ggplotify) 337 | library(ggrepel) 338 | setwd(plotWD1) 339 | pdf(paste0("Figure1_",ToUseCol,"_",Suffix,"_Panel_b_HEATMAP_UPLOAD.pdf"), height = 8, width = 6) 340 | print(plot_grid(p4)) 341 | dev.off() 342 | library(cowplot) 343 | dev.off() 344 | setwd(plotWD1) 345 | pdf(paste0("Figure1_",ToUseCol,"_",Suffix,"_Panel_b_HEATMAP_UPLOAD.pdf"), height = 8, width = 6) 346 | print(plot_grid(p4)) 347 | dev.off() 348 | RUNFigure1c="YES" 349 | if(RUNFigure1c=="YES"){ 350 | Idents(SCdata.main) <- ToUseCol 351 | Idents(SCdata.main) <- factor(Idents(SCdata.main), levels= ToUseOrder) 352 | r2.1 <- DimPlot(SCdata.main, reduction = "umap", cols = ToUsePallete, label = F, label.size = 1.5, pt.size = 0.01) + 353 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.4,"line"), legend.text=element_text(size=9)) + 354 | guides(color = guide_legend(override.aes = list(size = 1.2))) 355 | setwd(plotWD1) 356 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.1_Cluster_UPLOAD.pdf"), height = 2, width = 3) 357 | print(plot_grid(r2.1)) 358 | dev.off() 359 | #r2.2 <- DimPlot(SCdata.main, reduction = "umap", cols = GroupPalette.temp, label = F, label.size = 4, pt.size = 0.01, group.by = "Patient") + 360 | # theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.5,"line"), legend.text=element_text(size=9)) + 361 | # guides(color = guide_legend(override.aes = list(size = 2))) 362 | CellToPlot <- rownames(SCdata.main@meta.data[SCdata.main@meta.data$prediction %in% c("aneuploid", "diploid"),]) 363 | r2.3 <- DimPlot(SCdata.main, reduction = "umap", cells = CellToPlot, cols = PredictionPalette, label = F, label.size = 4, pt.size = 0.01, group.by = "prediction") + 364 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.5,"line"), legend.text=element_text(size=11)) + 365 | guides(color = guide_legend(override.aes = list(size = 2))) 366 | setwd(plotWD1) 367 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.2_Status_UPLOAD.pdf"), height = 2, width = 3) 368 | print(plot_grid(r2.3)) 369 | dev.off() 370 | SCdata.main@meta.data$CT <- "FILL" 371 | table(SCdata.main@meta.data$CT, SCdata.main@meta.data$Custom.Cluster) 372 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster01", "Cluster02", "Cluster03", "Cluster04"),"CT"] <- "Glioma" 373 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% "Cluster05","CT"] <- "Mix" 374 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% "Cluster06","CT"] <- "Pericyte" 375 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% "Cluster07","CT"] <- "Tcells" 376 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster08", "Cluster09", "Cluster10", "Cluster11"),"CT"] <- "Myeloid" 377 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster12", "Cluster13"),"CT"] <- "Oligo" 378 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster14"),"CT"] <- "Glioma" 379 | table(SCdata.main@meta.data$CT, SCdata.main@meta.data$Custom.Cluster) 380 | Idents(SCdata.main) <- "CT" 381 | Idents(SCdata.main) <- factor(Idents(SCdata.main), levels= CTOrder) 382 | r2.4 <- DimPlot(SCdata.main, reduction = "umap", cols = cbPalette.CT[CTOrder %in% unique((SCdata.main$CT))], label = F, label.size = 6, pt.size = 0.01) + 383 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.5,"line"), legend.text=element_text(size=11)) + 384 | guides(color = guide_legend(override.aes = list(size = 2))) 385 | setwd(plotWD1) 386 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.3_CT_UPLOAD.pdf"), height = 2, width = 3) 387 | print(plot_grid(r2.4)) 388 | dev.off() 389 | ##Code for new dimplot--> from iSEE package 390 | SE <- as.SingleCellExperiment(SCdata.main) 391 | red.dim <- SingleCellExperiment::reducedDim(SE, "UMAP"); 392 | plot.data <- data.frame(X=red.dim[, 1], Y=red.dim[, 2], row.names=colnames(SE)); 393 | plot.data$ColorBy <- colData(SE)[, "Patient"]; 394 | plot.data[["ColorBy"]] <- factor(plot.data[["ColorBy"]]); 395 | # Avoid visual biases from default ordering by shuffling the points 396 | set.seed(60025); 397 | plot.data <- plot.data[sample(nrow(plot.data)),,drop=FALSE]; 398 | r2.2 <- ggplot() + geom_point(aes(x=X, y=Y, color=ColorBy), alpha=1, plot.data, size=0.1) + labs(x=NULL, y=NULL, color="Patient") + scale_color_manual(values=GroupPalette.temp, na.value='grey50', drop=FALSE) + 399 | theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), 400 | axis.title.x=element_blank(), axis.title.y = element_blank(), text = element_text(size=14), legend.key.size = unit(0.7,"line"), legend.text=element_text(size=8), legend.title = element_text(size=10)) + 401 | guides(color = guide_legend(override.aes = list(size = 1.2))) 402 | setwd(plotWD1) 403 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.4_Patient_UPLOAD.pdf"), height = 2, width = 3) 404 | print(plot_grid(r2.2)) 405 | dev.off() 406 | rm(SE); rm(red.dim); rm(plot.data) 407 | } 408 | library(SummarizedExperiment) 409 | dev.off() 410 | RUNFigure1c="YES" 411 | if(RUNFigure1c=="YES"){ 412 | Idents(SCdata.main) <- ToUseCol 413 | Idents(SCdata.main) <- factor(Idents(SCdata.main), levels= ToUseOrder) 414 | r2.1 <- DimPlot(SCdata.main, reduction = "umap", cols = ToUsePallete, label = F, label.size = 1.5, pt.size = 0.01) + 415 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.4,"line"), legend.text=element_text(size=9)) + 416 | guides(color = guide_legend(override.aes = list(size = 1.2))) 417 | setwd(plotWD1) 418 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.1_Cluster_UPLOAD.pdf"), height = 2, width = 3) 419 | print(plot_grid(r2.1)) 420 | dev.off() 421 | #r2.2 <- DimPlot(SCdata.main, reduction = "umap", cols = GroupPalette.temp, label = F, label.size = 4, pt.size = 0.01, group.by = "Patient") + 422 | # theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.5,"line"), legend.text=element_text(size=9)) + 423 | # guides(color = guide_legend(override.aes = list(size = 2))) 424 | CellToPlot <- rownames(SCdata.main@meta.data[SCdata.main@meta.data$prediction %in% c("aneuploid", "diploid"),]) 425 | r2.3 <- DimPlot(SCdata.main, reduction = "umap", cells = CellToPlot, cols = PredictionPalette, label = F, label.size = 4, pt.size = 0.01, group.by = "prediction") + 426 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.5,"line"), legend.text=element_text(size=11)) + 427 | guides(color = guide_legend(override.aes = list(size = 2))) 428 | setwd(plotWD1) 429 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.2_Status_UPLOAD.pdf"), height = 2, width = 3) 430 | print(plot_grid(r2.3)) 431 | dev.off() 432 | SCdata.main@meta.data$CT <- "FILL" 433 | table(SCdata.main@meta.data$CT, SCdata.main@meta.data$Custom.Cluster) 434 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster01", "Cluster02", "Cluster03", "Cluster04"),"CT"] <- "Glioma" 435 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% "Cluster05","CT"] <- "Mix" 436 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% "Cluster06","CT"] <- "Pericyte" 437 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% "Cluster07","CT"] <- "Tcells" 438 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster08", "Cluster09", "Cluster10", "Cluster11"),"CT"] <- "Myeloid" 439 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster12", "Cluster13"),"CT"] <- "Oligo" 440 | SCdata.main@meta.data[SCdata.main@meta.data$Custom.Cluster %in% c("Cluster14"),"CT"] <- "Glioma" 441 | table(SCdata.main@meta.data$CT, SCdata.main@meta.data$Custom.Cluster) 442 | Idents(SCdata.main) <- "CT" 443 | Idents(SCdata.main) <- factor(Idents(SCdata.main), levels= CTOrder) 444 | r2.4 <- DimPlot(SCdata.main, reduction = "umap", cols = cbPalette.CT[CTOrder %in% unique((SCdata.main$CT))], label = F, label.size = 6, pt.size = 0.01) + 445 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), legend.key.size = unit(0.5,"line"), legend.text=element_text(size=11)) + 446 | guides(color = guide_legend(override.aes = list(size = 2))) 447 | setwd(plotWD1) 448 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.3_CT_UPLOAD.pdf"), height = 2, width = 3) 449 | print(plot_grid(r2.4)) 450 | dev.off() 451 | ##Code for new dimplot--> from iSEE package 452 | SE <- as.SingleCellExperiment(SCdata.main) 453 | red.dim <- SingleCellExperiment::reducedDim(SE, "UMAP"); 454 | plot.data <- data.frame(X=red.dim[, 1], Y=red.dim[, 2], row.names=colnames(SE)); 455 | plot.data$ColorBy <- colData(SE)[, "Patient"]; 456 | plot.data[["ColorBy"]] <- factor(plot.data[["ColorBy"]]); 457 | # Avoid visual biases from default ordering by shuffling the points 458 | set.seed(60025); 459 | plot.data <- plot.data[sample(nrow(plot.data)),,drop=FALSE]; 460 | r2.2 <- ggplot() + geom_point(aes(x=X, y=Y, color=ColorBy), alpha=1, plot.data, size=0.1) + labs(x=NULL, y=NULL, color="Patient") + scale_color_manual(values=GroupPalette.temp, na.value='grey50', drop=FALSE) + 461 | theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), 462 | axis.title.x=element_blank(), axis.title.y = element_blank(), text = element_text(size=14), legend.key.size = unit(0.7,"line"), legend.text=element_text(size=8), legend.title = element_text(size=10)) + 463 | guides(color = guide_legend(override.aes = list(size = 1.2))) 464 | setwd(plotWD1) 465 | pdf(paste0("Figure1_",ToUseCol,"_UMAP_",Suffix,"_Panel_c.4_Patient_UPLOAD.pdf"), height = 2, width = 3) 466 | print(plot_grid(r2.2)) 467 | dev.off() 468 | rm(SE); rm(red.dim); rm(plot.data) 469 | } 470 | RUNFigure1d="YES" 471 | if(RUNFigure1d=="YES"){ 472 | Idents(SCdata.main) <- ToUseCol 473 | Idents(SCdata.main) <- factor(Idents(SCdata.main), levels= ToUseOrder) 474 | PlotGenes <- c("OLIG2", "PDGFRA", "SOX2", "GFAP", "PTPRZ1", "NES", "VEGFA", "VIM", "TGFBI", "PDGFRB", "PTPRC", "CD8A", "ITGAM", "CD14", "MSR1", "CD68", "MBP") 475 | setwd(plotWD1) 476 | pdf(file = paste0("Figure1_",ToUseCol,"_",Suffix,"_Panel_d_DOTPLOT_UPLOAD.pdf"), height = 5, width = 9) 477 | print(DotPlot(SCdata.main, features = PlotGenes, cols= c("gray80", "red")) + 478 | theme(axis.title.x=element_blank(), axis.title.y = element_blank(), axis.text=element_text(size=13), legend.text=element_text(size=13), legend.title=element_text(size=15), 479 | legend.key.size = unit(0.4, "cm")) + RotatedAxis() + scale_colour_viridis_c(option = "plasma")) 480 | dev.off() 481 | } 482 | rm(list=ls()) # clear workspace 483 | #install.packages("devtools") 484 | library("devtools") 485 | #devtools::install_github("klutometis/roxygen") 486 | library(roxygen2) 487 | SCApackagedir <- "/Users/kumarpa/Desktop/Work/Jax/GitHub/" 488 | setwd(SCApackagedir) 489 | SCApackagedir <- "/Users/kumarpa/Desktop/Work/Jax/GitHub/" 490 | setwd(SCApackagedir) 491 | create("GBMatlas") 492 | setwd("./GBMatlas") 493 | WhenMakingGBMatlas="YES" 494 | if(WhenMakingGBMatlas=="YES"){ 495 | document() 496 | #rm(list = c("cat_function")) 497 | } 498 | rm(list=ls()) # clear workspace 499 | #install.packages("devtools") 500 | library("devtools") 501 | #devtools::install_github("klutometis/roxygen") 502 | library(roxygen2) 503 | GBMatlaspackagedir <- "/Users/kumarpa/Desktop/Work/Jax/GitHub/" 504 | setwd(GBMatlaspackagedir) 505 | create("GBMatlas") 506 | setwd("./GBMatlas") 507 | WhenMakingGBMatlas="YES" 508 | if(WhenMakingGBMatlas=="YES"){ 509 | document() 510 | #rm(list = c("cat_function")) 511 | } 512 | library(GBMatlas) 513 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: GBMatlas 2 | Title: What the Package Does (One Line, Title Case) 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "First", 6 | family = "Last", 7 | role = c("aut", "cre"), 8 | email = "first.last@example.com", 9 | comment = c(ORCID = "YOUR-ORCID-ID")) 10 | Description: What the package does (one paragraph). 11 | License: `use_mit_license()`, `use_gpl3_license()` or friends to 12 | pick a license 13 | Encoding: UTF-8 14 | LazyData: true 15 | Roxygen: list(markdown = TRUE) 16 | RoxygenNote: 7.1.1 17 | -------------------------------------------------------------------------------- /GBMatlas.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | LineEndingConversion: Posix 18 | 19 | BuildType: Package 20 | PackageUseDevtools: Yes 21 | PackageInstallArgs: --no-multiarch --with-keep.source 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | -------------------------------------------------------------------------------- /R/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/parveendabas/GBMatlas/f1d0d59076134680e890476cd4710b005a581a65/R/.DS_Store -------------------------------------------------------------------------------- /R/Figure1.R: -------------------------------------------------------------------------------- 1 | library(easypackages) 2 | MyPackages<-c("dplyr","ggplot2","ggpubr","gridExtra","viridis","egg", 3 | "grid","lattice","gtools","Biobase","RColorBrewer", 4 | "Seurat","cowplot","patchwork","stringr","ComplexHeatmap","SingleCellExperiment", "ggmin") 5 | libraries(MyPackages) 6 | 7 | #Set working directory 8 | Directory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/" 9 | #dir.create(Directory) 10 | 11 | setwd(Directory) 12 | 13 | 14 | #Change variables 15 | ObjName= "HumanGlioma-AllSamples-" 16 | Subset="All_Clusters" 17 | resolution= 0.1 18 | 19 | RobjDirectory=paste0(Directory,"R_Objects/") 20 | #dir.create(RobjDirectory) 21 | OutDirectory=paste0(Directory,"Output/") 22 | #dir.create(OutDirectory) 23 | OutputDirectory=paste0(OutDirectory,"AllClusters/") 24 | #dir.create(OutputDirectory) 25 | featuremapDirectory=paste0(OutputDirectory,"Featuremaps/") 26 | #dir.create(featuremapDirectory) 27 | 28 | #Load File 29 | SeuratObj=readRDS(paste0(RobjDirectory,"Allhuman-11-3-21 patients renamed.rds")) 30 | 31 | #Extract Meta Data 32 | CellInfo <- SeuratObj@meta.data 33 | 34 | #Assign colors 35 | SampleColors=c(`LGG-01` = "#003F5C", `LGG-03` = "#008796", `LGG-04` = "#2F4B7C", 36 | `ndGBM-01` = "#227ED4", `ndGBM-02` = "#665191", `ndGBM-03` = "#9E71C7", 37 | `ndGBM-04` = "#A05195", `ndGBM-05` = "#E082C1", `ndGBM-06` = "#D45087", 38 | `ndGBM-07` = "#F57689", `ndGBM-08` = "#BD3E3E", `ndGBM-09` = "#F95D6A", 39 | `ndGBM-10` = "#E07B18", `rGBM-01` = "#FF7C43", `rGBM-02` = "#FFA600", 40 | `rGBM-03` = "#F5D256", `rGBM-04` = "#42A665", `rGBM-05` = "#8DB032" 41 | ) 42 | 43 | 44 | ClusterColors=c(C1 = "#003F5C", C2 = "#196687", C3 = "#2879CD", C4 = "#8965B3", 45 | C5 = "#AB599D", C6 = "#D75D96", C7 = "#E56674", C8 = "#EE5761", 46 | C9 = "#EB7B27", C10 = "#FFA206", C11 = "#A3BD5C", C12 = "#8DB032" 47 | ) 48 | 49 | FragmentColors=c(`LGG-01-A` = "#003F5C", `LGG-01-B` = "#005B72", `LGG-01-C` = "#007789", 50 | `LGG-01-D` = "#087B91", `LGG-03` = "#1B6486", `LGG-04-1` = "#2D4C7C", 51 | `LGG-04-2` = "#2A5D9C", `LGG-04-3` = "#2572BF", `ndGBM-01-A` = "#2D76C9", 52 | `ndGBM-01-C` = "#4764AE", `ndGBM-01-D` = "#625394", `ndGBM-01-F` = "#795CA3", 53 | `ndGBM-02-1` = "#8F68B9", `ndGBM-02-2` = "#9E6CC0", `ndGBM-02-4` = "#9F5FAC", 54 | `ndGBM-02-5` = "#9F5398", `ndGBM-03-1` = "#B460A3", `ndGBM-03-2` = "#CE74B4", 55 | `ndGBM-03-3` = "#DE7CBA", `ndGBM-04` = "#D968A3", `ndGBM-05` = "#D5548C", 56 | `ndGBM-06` = "#DD5B87", `ndGBM-07` = "#EB6A88", `ndGBM-08` = "#EF7082", 57 | `ndGBM-09` = "#D95A64", `ndGBM-10` = "#C34446", `rGBM-01-A` = "#CD464A", 58 | `rGBM-01-B` = "#E5525B", `rGBM-01-C` = "#F75F64", `rGBM-01-D` = "#ED6A43", 59 | `rGBM-02-2` = "#E37623", `rGBM-02-3` = "#E77B23", `rGBM-02-4` = "#F47B34", 60 | `rGBM-02-5` = "#FF7D3F", `rGBM-03-1` = "#FF8E25", `rGBM-03-2` = "#FF9F0A", 61 | `rGBM-03-3` = "#FCB013", `rGBM-04-1` = "#F8C136", `rGBM-04-2` = "#F0D056", 62 | `rGBM-04-3` = "#AABF5C", `rGBM-04-4` = "#63AE62", `rGBM-05-1` = "#51A85A", 63 | `rGBM-05-2` = "#6FAC46", `rGBM-05-3` = "#8DB032") 64 | 65 | AssignmentColors=c(TCells = "#8DB032", BCells = "#F9BF31", Other = "#E47B1E", 66 | Myeloid = "#CD4D53", Endo = "#DC73B0", Oligo = "#966CBF", Pericytes = "#2960A1", 67 | Glioma = "#003F5C") 68 | PredictionColors=c(diploid = "#ff7f50", aneuploid = "#009FFF") 69 | 70 | ###Figure1b 71 | 72 | colortxt=c("white", "white","black","black","black","black","black", "black","black","black","black","black") 73 | 74 | U1=DimPlot(SeuratObj, reduction = "umap",group.by = "Cluster" ,cols = ClusterColors,label=T,label.color = colortxt)+ggmin::theme_min()+ 75 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 76 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Clusters")+ 77 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 5)) 78 | 79 | U2=DimPlot(SeuratObj, reduction = "umap",group.by = "Patient",cols = SampleColors )+ggmin::theme_min()+ 80 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 81 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Patients")+ 82 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 4)) 83 | 84 | 85 | colortxt2=c("white", "black","black","black", "black","black","black","black") 86 | 87 | U3=DimPlot(SeuratObj, reduction = "umap",group.by = "Assignment" ,cols = AssignmentColors,label=T,label.color = colortxt2)+ggmin::theme_min()+ 88 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 89 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Assignment")+ 90 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 4)) 91 | 92 | U4=DimPlot(SeuratObj, reduction = "umap",group.by = "CopyKatPrediction" ,cols = PredictionColors,label=T, 93 | cells = row.names(CellInfo%>% filter(CopyKatPrediction 94 | %in% na.omit(SeuratObj$CopyKatPrediction))) )+ggmin::theme_min()+ 95 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 96 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="CopyKat Prediction")+ 97 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 4)) 98 | 99 | plots2=ggarrange(U1,U2,U3,U4,ncol = 2) 100 | 101 | pdf(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"Figure 1b.pdf"),width = 12,height = 10) 102 | print(plots2) 103 | dev.off() 104 | #Supp Table 2 105 | write.csv(as.matrix(table(SeuratObj@meta.data$Cluster,SeuratObj@meta.data$Patient)), 106 | file = paste0(OutputDirectory,ObjName,Subset," Supp Table 2 number of cells per cluster and sample.csv")) 107 | 108 | #Figure 1c 109 | Idents(SeuratObj)=CellInfo$Cluster 110 | markers = FindAllMarkers(SeuratObj,logfc.threshold = 0.5,test.use = "wilcox",only.pos = T ) 111 | write.csv(markers,paste0(OutputDirectory,ObjName,Subset," Supp Table 3 cluster markers ","res",resolution,".csv")) 112 | #markers=read.csv(paste0(OutputDirectory,"HumanGlioma-AllSamples-All_Clusters cluster markers res0.1.csv"),row.names = 1) 113 | #markers$cnum=stringr::str_remove_all(markers$cluster,"C") 114 | #markers$cnum=as.numeric(markers$cnum) 115 | #markers=markers[order(markers$cnum),] 116 | top20.markers <- markers %>% group_by(cluster) %>% top_n(n = 20, wt = avg_log2FC) 117 | #top20.markers=top20.markers[order(top20.markers$cluster),] 118 | set.seed(12) 119 | Idents(SeuratObj)=CellInfo$Sample 120 | #sampled.cells <- sample(x = SeuratObj@active.ident, size = 5000, replace = F) 121 | subset=subset(SeuratObj, downsample=1000) 122 | DimPlot(subset,split.by = "Sample",group.by = "Cluster") 123 | SeuratObj.sce=as.SingleCellExperiment(subset) 124 | plot.data<-as.data.frame(assay(SeuratObj.sce, "logcounts")) 125 | plot.data<-plot.data[top20.markers$gene,] 126 | plot.data <- plot.data - rowMeans(plot.data) 127 | plot.data=na.omit(plot.data) 128 | CellInfoS=subset@meta.data 129 | 130 | column_annot <-CellInfoS[,c("Cluster","Patient"),drop=F] 131 | column_annot$Patient = as.factor(as.character(column_annot$Patient)) 132 | column_annot=with(column_annot, column_annot[order(Patient), , drop=F]) 133 | column_annot=with(column_annot, column_annot[order(Cluster), , drop=F]) 134 | plot.data<-plot.data[,row.names(column_annot)] 135 | 136 | column.colors=list() 137 | column.colors[["Patient"]]<-SampleColors 138 | column.colors[["Cluster"]]<-ClusterColors 139 | Patient=as.matrix(column_annot[,c("Patient"),drop=F]) 140 | Cluster=as.matrix(column_annot[,c("Cluster"),drop=F]) 141 | 142 | colanno <- columnAnnotation(df=column_annot, 143 | show_annotation_name =T,show_legend = F,col=column.colors) 144 | top2 <- markers %>% group_by(cluster) %>% top_n(n = 2, wt = avg_log2FC) 145 | genes= top2$gene 146 | rows=rowAnnotation(sel = anno_mark(at = match(genes,row.names(plot.data)), labels = genes,labels_gp =gpar(col = "black", fontsize = 9))) 147 | 148 | col<- circlize::colorRamp2(breaks = c(-2, 0, 2), colors = c("#007dd1", "white", "#ab3000")) 149 | HM=Heatmap(name="logcounts",as.matrix(plot.data),cluster_rows =F,cluster_columns = F,top_annotation = colanno, right_annotation = rows,row_names_gp = gpar(fontsize=5), 150 | col = col,show_column_names= F,show_row_names=F,border = F,show_heatmap_legend = F,use_raster = T) 151 | lgd=Legend(title = "logcounts", at= c(-2,0, 2),col_fun = col) 152 | lgd1=Legend(labels = levels(as.factor(column_annot$Cluster)),title="Cluster",legend_gp = gpar(fill=ClusterColors,fontsize=5)) 153 | lgd2=Legend(labels = levels(as.factor(column_annot$Patient)),title="Patient",legend_gp = gpar(fill=SampleColors,fontsize=5)) 154 | #lgd3=Legend(labels = levels(as.factor(column_annot$Fragment)),title="Fragment",legend_gp = gpar(fill=column.col3,fontsize=5)) 155 | 156 | pdf(paste0(OutputDirectory,"heatmap",ObjName,Subset,"res",resolution,"top20 genes per cluster Fig1c.pdf"),width=7.5,height=9) 157 | draw(HM,heatmap_legend_list = list( lgd,lgd1, lgd2), heatmap_legend_side = "right") 158 | dev.off() 159 | 160 | #Figure 1d 161 | pdf(paste0(OutputDirectory,"dotplot ",ObjName,Subset, "by Cluster fig 1d.pdf"),width=6,height=3.2) 162 | DotPlot(SeuratObj,group.by = "Cluster",dot.scale = 5 ,features = c("PDGFRA","SOX2","OLIG1","NES","GFAP","S100B","EGFR","MBP","PDGFRB","ACTA2", 163 | "PROM1","PECAM1","TEK", 164 | "P2RY12","PTPRC","ITGAM","ITGAX","S100A8","CD68","MRC1","NKG7","CD3E", 165 | "CD8A","CD4","CD79A","MKI67"),cols = c("blue","red"),scale = T)+scale_colour_viridis_c(option = "plasma")+ 166 | ggmin::theme_min()+ RotatedAxis()+theme(axis.title = element_blank()) 167 | dev.off() 168 | 169 | 170 | #Figure 1e 171 | library(forcats) 172 | P1=CellInfo %>% ggplot(aes(x=Patient, fill=fct_rev(Cluster))) +scale_fill_manual(values = ClusterColors)+ 173 | geom_bar(color="black",position = "fill",width = 0.7) + 174 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), legend.text = element_text(color="black",size=13,face = "bold"),legend.title = element_text(color="black",size=13,face = "bold"), 175 | axis.line = element_line(colour = "black"), axis.text.y = element_text(color="black",size=12),axis.text.x = element_text(color="black",angle = 45,hjust = 1,size=15), 176 | axis.text=element_text(size=15, face="bold"), axis.title=element_text(size=15,face="bold"), 177 | plot.margin = unit(c(0.2, 0.5,0.2, 0.5),"cm"))+labs(y ="Composition(percentage of cells)", x= NULL)+ 178 | scale_y_continuous(expand = c(0,0),limits = c(0,1),breaks = c(0,0.20,0.40,0.60,0.80,1),labels = scales::percent)+ 179 | theme(legend.title = element_blank()) 180 | P1g=ggplotGrob(P1) 181 | P2=ggplot(CellInfo, aes(Cluster , fill=Cluster))+geom_bar(stat="count",colour = "black",width = 0.7)+ scale_fill_manual(values = ClusterColors)+ 182 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), 183 | axis.line = element_line(colour = "black"), axis.text.y = element_blank(),axis.text.x = element_text(color="black",angle = 45,hjust = 1,size=10), 184 | axis.text=element_text(size=6.5, face="bold"), axis.title=element_text(size=15,face="bold"), 185 | plot.margin = unit(c(-0.1, 0.5,2.5, -0.5),"cm"))+labs(y ="number of cells", x= NULL)+ 186 | scale_y_continuous(expand=c(0,0),trans ="log2",limits=c(256,131072),oob = scales::squish)+coord_flip()+ 187 | theme(legend.position = "none") 188 | P2g=ggplotGrob(P2) 189 | 190 | pdf(paste0(OutputDirectory,"barplots ",ObjName,Subset, "fig 1e.pdf"),width=12,height=5.4) 191 | 192 | grid.arrange(grobs=list(P1g,P2g), widths = c(1,0.35),heights=c(0.19,1),layout_matrix = rbind(c(1, NA),c(1,2))) 193 | dev.off() 194 | 195 | ##Figure 1f 196 | SeuratObj@meta.data$Type2=factor(SeuratObj@meta.data$Type, levels=c("Astrocytoma", "Oligodendroglioma", "GBM", "Recurrent GBM")) 197 | 198 | x1=ggplot(SeuratObj@meta.data, aes("", fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 199 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ Type2) + #labs(title=pat2[i])+ 200 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 201 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 202 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 203 | x2=ggplot(SeuratObj@meta.data, aes("", fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 204 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ sex) + #labs(title=pat2[i])+ 205 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 206 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 207 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 208 | x3=ggplot(SeuratObj@meta.data, aes("", fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 209 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ Grade) + #labs(title=pat2[i])+ 210 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 211 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 212 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 213 | 214 | mylegend<-get_legend(x3+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title=Y,title.position = "top",title.theme = element_text(size=10)))) 215 | 216 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts type,sexandgrade and assignment fig1f.pdf")), height = 4, width =6,onefile = T) 217 | ggpubr::ggarrange(x1+NoLegend(),x2+NoLegend(),x3+NoLegend(),ncol =1,nrow = 3, 218 | legend.grob = mylegend,legend = "right") 219 | dev.off() 220 | 221 | 222 | -------------------------------------------------------------------------------- /R/Figure2.R: -------------------------------------------------------------------------------- 1 | library(easypackages) 2 | MyPackages<-c("dplyr","ggplot2","ggpubr","gridExtra","viridis","egg", 3 | "grid","lattice","gtools","Biobase","RColorBrewer","tibble", 4 | "Seurat","cowplot","patchwork","stringr","ComplexHeatmap","SingleCellExperiment", "ggmin") 5 | libraries(MyPackages) 6 | 7 | #Set working directory 8 | Directory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESNovember2021/" 9 | 10 | #dir.create(Directory) 11 | 12 | setwd(Directory) 13 | 14 | #Change variables 15 | 16 | ObjName= "HumanGlioma-AllSamples-" 17 | Subset="Glioma" 18 | resolution= 0.2 19 | dimz=1:30 20 | abb="GC" 21 | 22 | RobjDirectory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/R_Objects/" 23 | #paste0(Directory,"R_Objects/") 24 | 25 | OutputDirectory=paste0(Directory,"Output/",Subset,"/") 26 | #dir.create(OutputDirectory) 27 | featuremapDirectory=paste0(OutputDirectory,"Featuremaps/") 28 | #dir.create(featuremapDirectory) 29 | 30 | #Load File 31 | SeuratObj=readRDS(paste0(RobjDirectory,"GliomaClusters-11-3-21 patients renamed.rds")) 32 | 33 | #Extract Meta Data 34 | CellInfo <- SeuratObj@meta.data 35 | 36 | #Assign colors 37 | SampleColors=c(`LGG-03` = "#003F5C", `LGG-04` = "#008796", `ndGBM-01` = "#2F4B7C", 38 | `ndGBM-02` = "#227ED4", `ndGBM-03` = "#665191", `ndGBM-04` = "#9E71C7", 39 | `ndGBM-05` = "#A05195", `ndGBM-06` = "#E082C1", `ndGBM-07` = "#D45087", 40 | `ndGBM-08` = "#F57689", `ndGBM-09` = "#BD3E3E", `ndGBM-10` = "#F95D6A", 41 | `ndGBM-11` = "#E07B18", `rGBM-01` = "#FF7C43", `rGBM-02` = "#FFA600", 42 | `rGBM-03` = "#F5D256", `rGBM-04` = "#42A665", `rGBM-05` = "#8DB032" 43 | ) 44 | 45 | ClusterColors=c(GC01 = "#4BA75E", GC02 = "#003F5C", GC03 = "#235A82", GC04 = "#877EAE", 46 | GC05 = "#4367B2", GC06 = "#8DB032", GC07 = "#007D8E", GC08 = "#266AB3", 47 | GC09 = "#7B5DA5") 48 | 49 | FragmentColors=c(`LGG-03` = "#003F5C", `LGG-04-1` = "#005B72", `LGG-04-2` = "#007789", 50 | `LGG-04-3` = "#087B91", `ndGBM-01-A` = "#1B6486", `ndGBM-01-C` = "#2D4C7C", 51 | `ndGBM-01-D` = "#2A5D9C", `ndGBM-01-F` = "#2572BF", `ndGBM-02-1` = "#2D76C9", 52 | `ndGBM-02-2` = "#4764AE", `ndGBM-02-4` = "#625394", `ndGBM-02-5` = "#795CA3", 53 | `ndGBM-03-1` = "#8F68B9", `ndGBM-03-2` = "#9E6CC0", `ndGBM-03-3` = "#9F5FAC", 54 | `ndGBM-04` = "#9F5398", `ndGBM-05` = "#B460A3", `ndGBM-06` = "#CE74B4", 55 | `ndGBM-07` = "#DE7CBA", `ndGBM-08` = "#D968A3", `ndGBM-09` = "#D5548C", 56 | `ndGBM-10` = "#DD5B87", `ndGBM-11-A` = "#EB6A88", `ndGBM-11-B` = "#EF7082", 57 | `ndGBM-11-C` = "#D95A64", `ndGBM-11-D` = "#C34446", `rGBM-01-A` = "#CD464A", 58 | `rGBM-01-B` = "#E5525B", `rGBM-01-C` = "#F75F64", `rGBM-01-D` = "#ED6A43", 59 | `rGBM-02-2` = "#E37623", `rGBM-02-3` = "#E77B23", `rGBM-02-4` = "#F47B34", 60 | `rGBM-02-5` = "#FF7D3F", `rGBM-03-1` = "#FF8E25", `rGBM-03-2` = "#FF9F0A", 61 | `rGBM-03-3` = "#FCB013", `rGBM-04-1` = "#F8C136", `rGBM-04-2` = "#F0D056", 62 | `rGBM-04-3` = "#AABF5C", `rGBM-04-4` = "#63AE62", `rGBM-05-1` = "#51A85A", 63 | `rGBM-05-2` = "#6FAC46", `rGBM-05-3` = "#8DB032") 64 | TypeColors=c(Astrocytoma = "#003F5C", GBM = "#795192", Oligodendroglioma = "#EC5873", 65 | `Recurrent GBM` = "#FFA600") 66 | GradeColors=c(II = "#FFA600", IV = "#003F5C") 67 | 68 | 69 | #Make figure 2a 70 | Idents(SeuratObj)=CellInfo$Cluster 71 | markers = FindAllMarkers(SeuratObj,logfc.threshold = 0.5,test.use = "wilcox",only.pos = T ) 72 | write.csv(markers,paste0(OutputDirectory,ObjName,Subset," cluster markers ","res",resolution,".csv")) 73 | #markers=read.csv("~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/Output/Glioma/HumanGlioma-AllSamples-Glioma cluster markers res0.2.csv",row.names = 1) 74 | markers=markers[order(markers$cluster),] 75 | top20.markers <- markers %>% group_by(cluster) %>% top_n(n = 20, wt = avg_log2FC) 76 | top20.markers$cluster=as.character(top20.markers$cluster) 77 | top20.markers=top20.markers[order(top20.markers$cluster),] 78 | set.seed(12) 79 | Idents(SeuratObj)=CellInfo$Sample 80 | subset=subset(SeuratObj,downsample=1000) 81 | DimPlot(subset,split.by = "Sample",group.by = "Cluster") 82 | SeuratObj.sce=as.SingleCellExperiment(subset) 83 | plot.data<-as.data.frame(assay(SeuratObj.sce, "logcounts")) 84 | plot.data<-plot.data[top20.markers$gene,] 85 | plot.data <- plot.data - rowMeans(plot.data) 86 | plot.data=na.omit(plot.data) 87 | CellInfoS=subset@meta.data 88 | column_annot <-CellInfoS[,c("Cluster","Patient","Type"),drop=F] 89 | column_annot$Patient = as.factor(as.character(column_annot$Patient)) 90 | column_annot=with(column_annot, column_annot[order(Patient), , drop=F]) 91 | column_annot=with(column_annot, column_annot[order(Cluster), , drop=F]) 92 | plot.data<-plot.data[,row.names(column_annot)] 93 | 94 | column.colors=list() 95 | column.colors[["Patient"]]<-SampleColors 96 | column.colors[["Cluster"]]<-ClusterColors 97 | column.colors[["Type"]]<-TypeColors 98 | 99 | Patient=as.matrix(column_annot[,c("Patient"),drop=F]) 100 | Cluster=as.matrix(column_annot[,c("Cluster"),drop=F]) 101 | Type=as.matrix(column_annot[,c("Type"),drop=F]) 102 | 103 | colanno <- columnAnnotation(df=column_annot, 104 | show_annotation_name =T,show_legend = F,col=column.colors) 105 | top2 <- markers %>% group_by(cluster) %>% top_n(n = 2, wt = avg_log2FC) 106 | genes= top2$gene 107 | genes=c(genes,"APOD", "PDGFRA", "SPP1", "CHI3L1","CD74", "VEGFA","GFAP", "ATP5E", "FAM64A", "NMU","OLIG2","OLIG1","GFAP", "NES", "PROM1", "VIM", "ERBB2", "TGFBI", "SOX2", "ERBB3", "EGFR", "SOX9", "S100A4", "PTPRZ"," MBP", "B2M", "HLA-DRA", "CCL3") 108 | genes= unique(genes) 109 | rows=rowAnnotation(sel = anno_mark(at = match(genes,row.names(plot.data)), labels = genes,labels_gp =gpar(col = "black", fontsize = 9))) 110 | 111 | col<- circlize::colorRamp2(breaks = c(-2, 0, 2), colors = c("#007dd1", "white", "#ab3000")) 112 | HM=Heatmap(name="logcounts",as.matrix(plot.data),cluster_rows =F,cluster_columns = F,top_annotation = colanno, right_annotation = rows,row_names_gp = gpar(fontsize=5), 113 | col = col,show_column_names= F,show_row_names=F,border = F,show_heatmap_legend = F,use_raster = T) 114 | lgd=Legend(title = "logcounts", at= c(-2,0, 2),col_fun = col) 115 | lgd1=Legend(labels = levels(as.factor(column_annot$Cluster)),title="Cluster",legend_gp = gpar(fill=ClusterColors,fontsize=5)) 116 | lgd2=Legend(labels = levels(as.factor(column_annot$Patient)),title="Patient",legend_gp = gpar(fill=SampleColors,fontsize=5)) 117 | lgd4=Legend(labels = levels(as.factor(column_annot$Type)),title="Type",legend_gp = gpar(fill=TypeColors,fontsize=5)) 118 | 119 | pdf(paste0(OutputDirectory,"heatmap",ObjName,Subset,"res",resolution,"top20 genes per cluster Fig2a.pdf"),width=6,height=5) 120 | draw(HM,heatmap_legend_list = list( lgd,lgd1,lgd4, lgd2), heatmap_legend_side = "right") 121 | dev.off() 122 | 123 | 124 | # Fig2b 125 | U1=DimPlot(SeuratObj, reduction = "umap",group.by = "Cluster" ,cols = ClusterColors,label=T)+ggmin::theme_min()+ 126 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 127 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Clusters")+ 128 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 4)) 129 | 130 | U2=DimPlot(SeuratObj, reduction = "umap",group.by = "Patient",cols = SampleColors,shuffle = T )+ggmin::theme_min()+ 131 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 132 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Patients")+ 133 | guides(colour = guide_legend(override.aes = list(size=4),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=10),ncol = 3)) 134 | 135 | U3=DimPlot(SeuratObj, reduction = "umap",group.by = "Type" ,cols = TypeColors,label=F)+ggmin::theme_min()+ 136 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 137 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Type")+ 138 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=10,face = "bold"),ncol = 2)) 139 | 140 | U4=DimPlot(SeuratObj, reduction = "umap",group.by = "Grade" ,cols = GradeColors,label=T)+ggmin::theme_min()+ 141 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 142 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Grade")+ 143 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=10,face = "bold"),ncol = 3)) 144 | 145 | plots2=ggarrange(U1,U2,U3,U4,ncol = 2) 146 | 147 | pdf(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"all UMAPs Fig2b.pdf"),width = 8,height = 9,family = "ArialMT") 148 | print(plots2) 149 | dev.off() 150 | 151 | #Fig2c 152 | library(msigdbr) 153 | library(scrabble) 154 | 155 | CellInfo=SeuratObj@meta.data 156 | Idents(SeuratObj)=CellInfo$Cluster 157 | subset=subset(SeuratObj, downsample=5000) 158 | 159 | Counts <- as.matrix(GetAssayData(object = subset, assay = "RNA", slot = "counts")) 160 | 161 | #Read the Genelist file #from Neftel et al 162 | Subtype.Markers=read.csv("~/Box/Yun lab manuscripts/GBM Single Cells/Nature Communications revision/RCode/SuvaGBMsubtype genelist.csv") 163 | 164 | #get scores 165 | subtypelist=Subtype.Markers%>% split(x = .$human_gene_symbol, f = .$gs_name) 166 | sc=score(Counts, 167 | groups=subtypelist, 168 | binmat = NULL, 169 | bins = NULL, 170 | controls = NULL, 171 | bin.control = F, 172 | center = T, 173 | nbin = 30, 174 | n = 100, 175 | replace = T) 176 | #write.csv(sc,paste0(OutputDirectory,"HumanGliomacellstates20000cells.csv")) 177 | sc=read.csv(paste0(OutputDirectory,"HumanGliomacellstates20000cells.csv"),row.names = 1) 178 | #get coordinates 179 | h=hierarchy (sc, quadrants = c("AC","OPC","Mesenchymal","Proneural"), log.scale = T) 180 | # make plots 181 | 182 | ##per cluster 183 | CellInfo=SeuratObj@meta.data 184 | for( i in 1: length(names(ClusterColors))){ 185 | name=names(ClusterColors)[i] 186 | color=ClusterColors[i] 187 | CellInfo$Clustercolor [CellInfo$Cluster== name] <- color 188 | } 189 | Clustercolor <- CellInfo$Clustercolor 190 | names(Clustercolor ) <- row.names(CellInfo) 191 | SeuratObj <- AddMetaData(object = SeuratObj,metadata = Clustercolor ,col.name = 'Clustercolor') 192 | 193 | # make plots 194 | xmax=max(h$X)+(max(h$X)*0.2) 195 | xmin=min(h$X)+(min(h$X)*0.2) 196 | ymax=max(h$Y)+(max(h$Y)*0.6) 197 | ymin=min(h$Y)+(min(h$Y)*0.6) 198 | 199 | #Make the big plot--> all Clusters 200 | groups=SeuratObj@meta.data[,c("Clustercolor","Cluster")] 201 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 202 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 203 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 204 | row.names(matrix)=matrix$Row.names 205 | x=matrix$Clustercolor 206 | y=matrix$Cluster 207 | col=x[!duplicated(x)] 208 | names(col)=y[!duplicated(y)] 209 | matrix=matrix[,-1] 210 | title="All Glioma Clusters" 211 | p0 <- ggplot(matrix, aes(x = X, 212 | y =Y,color=factor(Cluster)))+geom_point()+geom_point(data = subset(matrix, Cluster !="Other"))+ 213 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Cluster")+theme(legend.position = "none")+ 214 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 215 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 216 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 217 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 218 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 219 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 220 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 221 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 222 | geom_hline(yintercept=0, color = "black", size=0.5)+ 223 | geom_vline(xintercept=0, color = "black", size=0.5)+ 224 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymax-2, ymax = ymax, fill= "black") + 225 | annotate("text",x = xmin+2.5, y = ymax-1,label = "Mes-Like",color="white",fontface="bold",size=7.5)+ 226 | annotate("rect", xmin = xmax-5, xmax = xmax, ymin = ymax-2, ymax = ymax, fill= "black") + 227 | annotate("text",x = xmax-2.5, y = ymax-1,label = "NPC-Like",color="white",fontface="bold",size=7.5)+ 228 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymin+2, ymax = ymin, fill= "black") + 229 | annotate("text",x = xmin+2.5, y = ymin+1,label = "AC-Like",color="white",fontface="bold",size=7.5)+ 230 | annotate("rect", xmin = xmax-5, xmax =xmax, ymin = ymin+2, ymax = ymin, fill= "black") + 231 | annotate("text",x = xmax-2.5, y = ymin+1,label = "OPC-Like",color="white",fontface="bold",size=7.5) 232 | P0=ggplotGrob(p0) 233 | ##make the small plots--> one per Cluster 234 | Final <- list() 235 | Final[[1]]=ggplotGrob(p0) 236 | for (i in 1:length(names(ClusterColors))) { 237 | ClusterMD=SeuratObj@meta.data[SeuratObj@meta.data$Cluster==paste0(names(ClusterColors)[i]),] 238 | groups=ClusterMD[,c("Clustercolor","Cluster")] 239 | title=paste0(names(ClusterColors)[i]) 240 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 241 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 242 | matrix$Cluster=as.character(matrix$Cluster) 243 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 244 | row.names(matrix)=matrix$Row.names 245 | x=matrix$Clustercolor 246 | y=matrix$Cluster 247 | col=x[!duplicated(x)] 248 | names(col)=y[!duplicated(y)] 249 | matrix=matrix[,-1] 250 | P=ggplot(matrix, aes(x = X, 251 | y =Y,color=factor(Cluster)))+geom_point()+geom_point(data = subset(matrix, Cluster !="Other"))+ 252 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 253 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 254 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 255 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 256 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 257 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 258 | geom_hline(yintercept=0, color = "black", size=0.5)+ 259 | geom_vline(xintercept=0, color = "black", size=0.5)+ 260 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymax-2, ymax = ymax, fill= "black") + 261 | annotate("text",x = xmin+2.5, y = ymax-1,label = "Mes-Like",color="white",fontface="bold",size=3)+ 262 | annotate("rect", xmin = xmax-5, xmax = xmax, ymin = ymax-2, ymax = ymax, fill= "black") + 263 | annotate("text",x = xmax-2.5, y = ymax-1,label = "NPC-Like",color="white",fontface="bold",size=3)+ 264 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymin+2, ymax = ymin, fill= "black") + 265 | annotate("text",x = xmin+2.5, y = ymin+1,label = "AC-Like",color="white",fontface="bold",size=3)+ 266 | annotate("rect", xmin = xmax-5, xmax =xmax, ymin = ymin+2, ymax = ymin, fill= "black") + 267 | annotate("text",x = xmax-2.5, y = ymin+1,label = "OPC-Like",color="white",fontface="bold",size=3) 268 | Final[[i+1]] = ggplotGrob(P) 269 | } 270 | Final[[1]]=ggplotGrob(p0) 271 | numofplots= length(Final) 272 | 273 | 274 | pdf(file =paste0(OutputDirectory,ObjName,Subset," subtype butterfly by Cluster8-6.pdf"), height = 8, width =12,onefile = T) 275 | grid.arrange(grobs=Final, widths = c(2,1,1,1),layout_matrix = rbind(c(1, 2, 3,4),c(1,5,6,7),c(1,8,9,10))) 276 | dev.off() 277 | 278 | 279 | 280 | #Figure 2d 281 | 282 | library(presto) 283 | library(msigdbr) 284 | library(dplyr) 285 | library(fgsea) 286 | library(tibble) 287 | library(pheatmap) 288 | 289 | m_df_H<- msigdbr(species = "Homo sapiens", category = cat) 290 | Annot.pathway2=as.data.frame(levels(as.factor(m_df_H$gs_name))) 291 | names(Annot.pathway2)="pathway" 292 | fgsea_sets<- m_df_H %>% split(x = .$gene_symbol, f = .$gs_name) 293 | Clusters=levels(as.factor(Annot$Cluster)) 294 | #run test 295 | Clusters.genes <- wilcoxauc(SeuratObj , 'Cluster') 296 | dplyr::count(Clusters.genes, group) 297 | objname="Glioma" 298 | name="Hallmark" 299 | 300 | for (i in 1: length(Clusters)){ 301 | X= Clusters[i] 302 | print(X) 303 | Genes<-Clusters.genes %>% 304 | dplyr::filter(group == Clusters[[i]]) %>% 305 | arrange(desc(auc)) %>% 306 | dplyr::select(feature, auc) 307 | ranks<- deframe(Genes) 308 | head(ranks) 309 | 310 | fgseaRes<- fgsea(fgsea_sets, stats = ranks,minSize=10) 311 | fgseaResTidy <- fgseaRes %>% 312 | as_tibble() %>% 313 | arrange(desc(NES)) 314 | fgseaResTidy %>% 315 | dplyr::select(-leadingEdge, -ES, -log2err) %>% 316 | arrange(padj) %>% 317 | head() 318 | fgseaResTidy$Enrichment = ifelse(fgseaResTidy$NES > 0, "Up-regulated", "Down-regulated") 319 | Filtidy<-fgseaResTidy %>% filter(padj < 0.05) 320 | filtRes = rbind(Filtidy %>% filter(Enrichment == "Up-regulated") %>% head(n= 10), 321 | Filtidy %>% filter(Enrichment == "Down-regulated") %>% head(n= 10)) 322 | Y<-fgseaResTidy 323 | hmc=as.data.frame(Y) 324 | hmc=apply(Y,2,as.character) 325 | #write.csv(hmc,paste(name, X,".csv")) 326 | names(Y)[names(Y)=="NES"]=paste(X) 327 | Annot.pathway<-Y[,c("pathway",paste(X))] 328 | Annot.pathway2<-merge(Annot.pathway, Annot.pathway2, by.x="pathway", by.y="pathway") 329 | 330 | 331 | 332 | } 333 | ##make a heatmap 334 | rownames(Annot.pathway2)=Annot.pathway2$pathway 335 | Annot.pathway2=Annot.pathway2[,-1] 336 | positions=Clusters 337 | 338 | Annot.pathway2=Annot.pathway2[,positions] 339 | Annot.pathway2[is.na(Annot.pathway2)]=0 340 | pathnames=c(row.names(Annot.pathway2 %>% top_n(n = 2, wt = eval(parse(text=names(Annot.pathway2)[1])))),row.names(Annot.pathway2 %>% top_n(n = -2, wt = eval(parse(text=names(Annot.pathway2)[1]))))) 341 | for (i in 2: length(names(Annot.pathway2))){ 342 | pathnames1=c(row.names(Annot.pathway2 %>% top_n(n = 2, wt = eval(parse(text=names(Annot.pathway2)[i])))),row.names(Annot.pathway2 %>% top_n(n = -2, wt = eval(parse(text=names(Annot.pathway2)[i]))))) 343 | pathnames=c(pathnames,pathnames1) 344 | } 345 | pathnames =pathnames[!duplicated(pathnames)] 346 | Annot.pathway4=Annot.pathway2[pathnames,] 347 | Annot.pathway4[is.na(Annot.pathway4)]=0 348 | redblackblu=CustomPalette(low = "blue", high = "red", mid = "white", k = 1000) 349 | pheatmap(Annot.pathway4,scale = "none",cluster_rows = T,cluster_cols = F,border_color = "black",treeheight_row = 0,legend_breaks = c(-5,-2,0,2,5), 350 | fontsize =8, cellwidth = 15,cellheight =10,color= plasma(10),angle_col = 90, 351 | main = paste(" ",name,objname," (NES)"),filename = paste("GSEA",name," (NES) top and bottom 2each-Heatmap Fig2c.pdf")) 352 | 353 | 354 | #Fig 2e 355 | ## Figure 2e 356 | 357 | m_df_H<- msigdbr::msigdbr(species = "Homo sapiens", category = "H") 358 | Filtered.genesets=filter(m_df_H,gs_name=="HALLMARK_HYPOXIA"|gs_name=="HALLMARK_INTERFERON_GAMMA_RESPONSE"|gs_name=="HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION"|gs_name=="HALLMARK_MYC_TARGETS_V1") 359 | geneset.all <- Filtered.genesets[Filtered.genesets$gene_symbol %in% rownames(Counts),] 360 | geneset <- geneset.all[!duplicated(geneset.all$gene_symbol),] 361 | genesetlist=geneset.all%>% split(x = .$gene_symbol, f = .$gs_name) 362 | #get score 363 | sc=score(mat=Counts, 364 | groups=genesetlist, 365 | binmat = NULL, 366 | bins = NULL, 367 | controls = NULL, 368 | bin.control = F, 369 | center = T, 370 | nbin = 30, 371 | n = 100, 372 | replace = T) 373 | 374 | #get coordinates 375 | h=hierarchy (sc, quadrants = c("HALLMARK_HYPOXIA","HALLMARK_INTERFERON_GAMMA_RESPONSE","HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION","HALLMARK_MYC_TARGETS_V1"), log.scale =T) 376 | 377 | #add cluster colors to metadata 378 | 379 | CellInfo=SeuratObj@meta.data 380 | for( i in 1: length(names(ClusterColors))){ 381 | name=names(ClusterColors)[i] 382 | color=ClusterColors[i] 383 | CellInfo$Clustercolor [CellInfo$Cluster== name] <- color 384 | } 385 | Clustercolor <- CellInfo$Clustercolor 386 | names(Clustercolor ) <- row.names(CellInfo) 387 | SeuratObj <- AddMetaData(object = SeuratObj,metadata = Clustercolor ,col.name = 'Clustercolor') 388 | 389 | #Make the big plot--> all clusters 390 | 391 | 392 | xmax=max(h$X)+(max(h$X)*0.2) 393 | xmin=min(h$X)+(min(h$X)*0.2) 394 | ymax=max(h$Y)+(max(h$Y)*0.6) 395 | ymin=min(h$Y)+(min(h$Y)*0.6) 396 | 397 | #Make the big plot--> all Clusters 398 | groups=SeuratObj@meta.data[,c("Clustercolor","Cluster")] 399 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 400 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 401 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 402 | row.names(matrix)=matrix$Row.names 403 | x=matrix$Clustercolor 404 | y=matrix$Cluster 405 | col=x[!duplicated(x)] 406 | names(col)=y[!duplicated(y)] 407 | matrix=matrix[,-1] 408 | title="All Glioma Clusters" 409 | p0 <- ggplot(matrix, aes(x = X, 410 | y =Y,color=factor(Cluster)))+geom_point()+geom_point(data = subset(matrix, Cluster !="Other"))+ 411 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Cluster")+theme(legend.position = "none")+ 412 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 413 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 414 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 415 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 416 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 417 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 418 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 419 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 420 | geom_hline(yintercept=0, color = "black", size=0.5)+ 421 | geom_vline(xintercept=0, color = "black", size=0.5)+ 422 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 423 | annotate("text",x = xmin+2, y = ymax-0.5,label = "EMT",color="white",fontface="bold",size=7.5)+ 424 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 425 | annotate("text",x = xmax-2, y = ymax-0.5,label = "MYC-Tar",color="white",fontface="bold",size=7.5)+ 426 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 427 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Hypoxia",color="white",fontface="bold",size=7.5)+ 428 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 429 | annotate("text",x = xmax-2, y = ymin+0.5,label = "IFNg-res",color="white",fontface="bold",size=7.5) 430 | P0=ggplotGrob(p0) 431 | ##make the small plots--> one per Cluster 432 | Final <- list() 433 | Final[[1]]=ggplotGrob(p0) 434 | for (i in 1:length(names(ClusterColors))) { 435 | ClusterMD=SeuratObj@meta.data[SeuratObj@meta.data$Cluster==paste0(names(ClusterColors)[i]),] 436 | groups=ClusterMD[,c("Clustercolor","Cluster")] 437 | title=paste0(names(ClusterColors)[i]) 438 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 439 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 440 | matrix$Cluster=as.character(matrix$Cluster) 441 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 442 | row.names(matrix)=matrix$Row.names 443 | x=matrix$Clustercolor 444 | y=matrix$Cluster 445 | col=x[!duplicated(x)] 446 | names(col)=y[!duplicated(y)] 447 | matrix=matrix[,-1] 448 | P=ggplot(matrix, aes(x = X, 449 | y =Y,color=factor(Cluster)))+geom_point()+geom_point(data = subset(matrix, Cluster !="Other"))+ 450 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 451 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 452 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 453 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 454 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 455 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 456 | geom_hline(yintercept=0, color = "black", size=0.5)+ 457 | geom_vline(xintercept=0, color = "black", size=0.5)+ 458 | 459 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 460 | annotate("text",x = xmin+2, y = ymax-0.5,label = "EMT",color="white",fontface="bold",size=3)+ 461 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 462 | annotate("text",x = xmax-2, y = ymax-0.5,label = "MYC-Tar",color="white",fontface="bold",size=3)+ 463 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 464 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Hypoxia",color="white",fontface="bold",size=3)+ 465 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 466 | annotate("text",x = xmax-2, y = ymin+0.5,label = "IFNg-res",color="white",fontface="bold",size=3) 467 | Final[[i+1]] = ggplotGrob(P) 468 | } 469 | Final[[1]]=ggplotGrob(p0) 470 | length(Final) 471 | pdf(file =paste0(OutputDirectory,ObjName,Subset," pathway butterfly by Cluster8-6.pdf"), height = 8, width =12,onefile = T) 472 | grid.arrange(grobs=Final, widths = c(2,1,1,1),layout_matrix = rbind(c(1, 2, 3,4),c(1,5,6,7),c(1,8,9,10))) 473 | dev.off() 474 | ##Fig2f 475 | Idents(SeuratObj)=CellInfo$Cluster 476 | subset=subset(SeuratObj, downsample=5000) 477 | 478 | Counts <- as.matrix(GetAssayData(object = subset, assay = "RNA", slot = "counts")) 479 | 480 | #get pathway scores 481 | Filtered.genesets=filter(m_df_H,gs_name=="HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION"|gs_name=="HALLMARK_HYPOXIA"|gs_name=="HALLMARK_INTERFERON_GAMMA_RESPONSE"|gs_name=="HALLMARK_MYC_TARGETS_V1"|gs_name=="HALLMARK_OXIDATIVE_PHOSPHORYLATION"|gs_name=="HALLMARK_TNFA_SIGNALING_VIA_NFKB") 482 | geneset.all <- Filtered.genesets[Filtered.genesets$gene_symbol %in% rownames(Counts),] 483 | geneset <- geneset.all[!duplicated(geneset.all$gene_symbol),] 484 | genesetlist=geneset.all%>% split(x = .$gene_symbol, f = .$gs_name) 485 | #get score 486 | PathwayScore=score(mat=Counts, 487 | groups=genesetlist, 488 | binmat = NULL, 489 | bins = NULL, 490 | controls = NULL, 491 | bin.control = F, 492 | center = T, 493 | nbin = 30, 494 | n = 100, 495 | replace = T) 496 | head(PathwayScore) 497 | colnames(PathwayScore)=c("EMT","Hypoxia","IFNg-res","MYC-tar","Ox-Phos","TNFa-sig") 498 | 499 | #get subtype cell scores 500 | Subtype.Markers=read.csv("~/Box/Yun lab manuscripts/GBM Single Cells/Nature Communications revision/RCode/SuvaGBMsubtype genelist.csv") 501 | subtypelist=Subtype.Markers%>% split(x = .$human_gene_symbol, f = .$gs_name) 502 | SubtypeScore=score(Counts, 503 | groups=subtypelist, 504 | binmat = NULL, 505 | bins = NULL, 506 | controls = NULL, 507 | bin.control = F, 508 | center = T, 509 | nbin = 30, 510 | n = 100, 511 | replace = T) 512 | head(SubtypeScore) 513 | colnames(SubtypeScore)=c("AC","Mes","OPC","NPC") 514 | 515 | mergedsc=cbind(PathwayScore,SubtypeScore) 516 | corrsc=cor(mergedsc) 517 | corrsctrunk=corrsc[1:6,7:10] 518 | p.mat <- corrplot::cor.mtest(mergedsc) 519 | p.mattrunk=p.mat$p 520 | p.mattrunk=p.mattrunk[1:6,7:10] 521 | 522 | pdf(file =paste0(OutputDirectory,ObjName,Subset," correlogram fig2e.pdf")) 523 | corrplot::corrplot(corrsctrunk,type = "full",method = "pie",p.mat = p.mattrunk,insig = "label_sig",sig.level = 0.05, 524 | col = colorRampPalette(c("green","black","red"))(5),tl.col = "black", cl.ratio= 0.5,pch.col ="black") 525 | dev.off() 526 | -------------------------------------------------------------------------------- /R/Figure3.R: -------------------------------------------------------------------------------- 1 | library(easypackages) 2 | MyPackages<-c("dplyr","ggplot2","ggpubr","gridExtra","viridis","egg", 3 | "grid","lattice","gtools","Biobase","RColorBrewer","tibble", 4 | "Seurat","cowplot","patchwork","stringr","ComplexHeatmap","SingleCellExperiment", "ggmin") 5 | libraries(MyPackages) 6 | 7 | #Set working directory 8 | Directory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESNovember2021/" 9 | #dir.create(Directory) 10 | 11 | setwd(Directory) 12 | 13 | #Change variables 14 | 15 | ObjName= "HumanGlioma-AllSamples-" 16 | Subset="TCells" 17 | resolution= 0.5 18 | dimz=1:30 19 | abb="TC" 20 | 21 | RobjDirectory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/R_Objects/" 22 | OutputDirectory=paste0(Directory,"Output/",Subset,"/") 23 | #dir.create(OutputDirectory) 24 | featuremapDirectory=paste0(OutputDirectory,"Featuremaps/") 25 | #dir.create(featuremapDirectory) 26 | 27 | #Load File 28 | SeuratObj=readRDS(paste0(RobjDirectory,"TcellClusters-7-22-21-goodwithoutcluster6 new patient names.rds")) 29 | 30 | #Extract Meta Data 31 | CellInfo <- SeuratObj@meta.data 32 | 33 | #Assign colors 34 | SampleColors=c(`LGG-03` = "#003F5C", `LGG-04` = "#008796", `ndGBM-01` = "#2F4B7C", 35 | `ndGBM-02` = "#227ED4", `ndGBM-03` = "#665191", `ndGBM-04` = "#9E71C7", 36 | `ndGBM-05` = "#A05195", `ndGBM-06` = "#E082C1", `ndGBM-07` = "#D45087", 37 | `ndGBM-08` = "#F57689", `ndGBM-09` = "#BD3E3E", `ndGBM-10` = "#F95D6A", 38 | `ndGBM-11` = "#E07B18", `rGBM-01` = "#FF7C43", `rGBM-02` = "#FFA600", 39 | `rGBM-03` = "#F5D256", `rGBM-04` = "#42A665", `rGBM-05` = "#8DB032" 40 | ) 41 | 42 | ClusterColors=c(TC01 = "#8DB032", TC02 = "#42A665", TC03 = "#9E71C7", TC04 = "#665191", 43 | TC05 = "#227ED4", TC06 = "#2F4B7C", TC07 = "#008796", TC08 = "#003F5C" 44 | ) 45 | 46 | FragmentColors=c(`LGG-03` = "#003F5C", `LGG-04-1` = "#005B72", `LGG-04-2` = "#007789", 47 | `LGG-04-3` = "#087B91", `ndGBM-01-A` = "#1B6486", `ndGBM-01-C` = "#2D4C7C", 48 | `ndGBM-01-D` = "#2A5D9C", `ndGBM-01-F` = "#2572BF", `ndGBM-02-1` = "#2D76C9", 49 | `ndGBM-02-2` = "#4764AE", `ndGBM-02-4` = "#625394", `ndGBM-02-5` = "#795CA3", 50 | `ndGBM-03-1` = "#8F68B9", `ndGBM-03-2` = "#9E6CC0", `ndGBM-03-3` = "#9F5FAC", 51 | `ndGBM-04` = "#9F5398", `ndGBM-05` = "#B460A3", `ndGBM-06` = "#CE74B4", 52 | `ndGBM-07` = "#DE7CBA", `ndGBM-08` = "#D968A3", `ndGBM-09` = "#D5548C", 53 | `ndGBM-10` = "#DD5B87", `ndGBM-11-A` = "#EB6A88", `ndGBM-11-B` = "#EF7082", 54 | `ndGBM-11-C` = "#D95A64", `ndGBM-11-D` = "#C34446", `rGBM-01-A` = "#CD464A", 55 | `rGBM-01-B` = "#E5525B", `rGBM-01-C` = "#F75F64", `rGBM-01-D` = "#ED6A43", 56 | `rGBM-02-2` = "#E37623", `rGBM-02-3` = "#E77B23", `rGBM-02-4` = "#F47B34", 57 | `rGBM-02-5` = "#FF7D3F", `rGBM-03-1` = "#FF8E25", `rGBM-03-2` = "#FF9F0A", 58 | `rGBM-03-3` = "#FCB013", `rGBM-04-1` = "#F8C136", `rGBM-04-2` = "#F0D056", 59 | `rGBM-04-3` = "#AABF5C", `rGBM-04-4` = "#63AE62", `rGBM-05-1` = "#51A85A", 60 | `rGBM-05-2` = "#6FAC46", `rGBM-05-3` = "#8DB032") 61 | TypeColors=c(Astrocytoma = "#003F5C", GBM = "#795192", Oligodendroglioma = "#EC5873", 62 | `Recurrent GBM` = "#FFA600") 63 | GradeColors=c(II = "#FFA600", IV = "#003F5C") 64 | AssignmentColors=c(`CD4 TCells` = "#003F5C", `CD8 TCells` = "#74599E", `Naiive TCells` = "#E46388", 65 | NKCells = "#F77B38", Tregs = "#8DB032") 66 | 67 | #Figure3a 68 | Idents(SeuratObj)=CellInfo$Cluster 69 | markers = FindAllMarkers(SeuratObj,logfc.threshold = 0.5,test.use = "wilcox",only.pos = T ) 70 | write.csv(markers,paste0(OutputDirectory,ObjName,Subset," cluster markers ","res",resolution,".csv")) 71 | #markers = read.csv(paste0("~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/Output/",Subset,"/",ObjName,Subset," cluster markers ","res",resolution,".csv"),row.names = 1) 72 | top20.markers <- markers %>% group_by(cluster) %>% top_n(n = 20, wt = avg_log2FC) 73 | top20.markers$cluster=as.character(top20.markers$cluster) 74 | top20.markers=top20.markers[order(top20.markers$cluster),] 75 | SeuratObj.sce=as.SingleCellExperiment(SeuratObj) 76 | plot.data<-as.data.frame(assay(SeuratObj.sce, "logcounts")) 77 | plot.data<-plot.data[top20.markers$gene,] 78 | plot.data <- plot.data - rowMeans(plot.data) 79 | plot.data=na.omit(plot.data) 80 | column_annot <-CellInfo[,c("Cluster","Patient","Type"),drop=F] 81 | column_annot$Patient = as.factor(as.character(column_annot$Patient)) 82 | column_annot=with(column_annot, column_annot[order(Patient), , drop=F]) 83 | column_annot=with(column_annot, column_annot[order(Cluster), , drop=F]) 84 | plot.data<-plot.data[,row.names(column_annot)] 85 | column.col= SampleColors 86 | 87 | 88 | column.colors=list() 89 | column.colors[["Patient"]]<-column.col 90 | column.colors[["Cluster"]]<-ClusterColors 91 | column.colors[["Type"]]<-TypeColors 92 | 93 | Patient=as.matrix(column_annot[,c("Patient"),drop=F]) 94 | Cluster=as.matrix(column_annot[,c("Cluster"),drop=F]) 95 | Type=as.matrix(column_annot[,c("Type"),drop=F]) 96 | 97 | colanno <- columnAnnotation(df=column_annot, 98 | show_annotation_name =T,show_legend = F,col=column.colors) 99 | top2 <- markers %>% group_by(cluster) %>% top_n(n = 2, wt = avg_log2FC) 100 | genes= top2$gene 101 | 102 | rows=rowAnnotation(sel = anno_mark(at = match(genes,row.names(plot.data)), labels = genes,labels_gp =gpar(col = "black", fontsize = 9))) 103 | 104 | col<- circlize::colorRamp2(breaks = c(-2, 0, 2), colors = c("#007dd1", "white", "#ab3000")) 105 | HM=Heatmap(name="logcounts",as.matrix(plot.data),cluster_rows =F,cluster_columns = F,top_annotation = colanno, right_annotation = rows,row_names_gp = gpar(fontsize=5), 106 | col = col,show_column_names= F,show_row_names=F,border = F,show_heatmap_legend = F,use_raster = T) 107 | lgd=Legend(title = "logcounts", at= c(-2,0, 2),col_fun = col) 108 | lgd1=Legend(labels = levels(as.factor(column_annot$Cluster)),title="Cluster",legend_gp = gpar(fill=ClusterColors,fontsize=5)) 109 | lgd2=Legend(labels = levels(as.factor(column_annot$Patient)),title="Patient",legend_gp = gpar(fill=column.col,fontsize=5)) 110 | lgd4=Legend(labels = levels(as.factor(column_annot$Type)),title="Type",legend_gp = gpar(fill=TypeColors,fontsize=5)) 111 | 112 | pdf(paste0(OutputDirectory,"heatmap",ObjName,Subset,"res",resolution,"top20 genes per cluster figure3a.pdf"),width=8,height=4.5) 113 | draw(HM,heatmap_legend_list = list( lgd,lgd1,lgd4, lgd2), heatmap_legend_side = "right") 114 | dev.off() 115 | 116 | ## Figure 3b 117 | U1=DimPlot(SeuratObj, reduction = "umap",group.by = "Cluster" ,cols = ClusterColors,label=T)+ggmin::theme_min()+ 118 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 119 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "right")+labs(title="Clusters")+ 120 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol =1)) 121 | 122 | pdf(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"Clusters UMAP fig 3b.pdf"),width = 7.5,height =5,family = "ArialMT") 123 | U1 124 | dev.off() 125 | 126 | 127 | #Figure 3c 128 | 129 | Genes=c('CD3E','CD4','CD8A','TCF7','CCR7','LAG3','PDCD1','FOXP3','IL2RA','CTLA4','TIGIT','KLRB1','HOPX','S100A4','JUNB','GZMB','IFNG','NKG7','GNLY','MKI67','GZMH','KLRD1') 130 | pdf(paste0(OutputDirectory,"dotplot ",ObjName,Subset, " Fig 3c.pdf"),width=7,height=3.5) 131 | DotPlot(SeuratObj,group.by = "Cluster",dot.scale = 5 ,features = Genes ,scale = T)+scale_colour_viridis_c(option = "plasma")+ 132 | ggmin::theme_min()+ RotatedAxis()+theme(axis.title = element_blank()) 133 | 134 | dev.off() 135 | 136 | #Figure 3d 137 | 138 | SeuratObj@meta.data$Type2=factor(SeuratObj@meta.data$Type, levels=c("Astrocytoma", "Oligodendroglioma", "GBM", "Recurrent GBM")) 139 | 140 | x1=ggplot(SeuratObj@meta.data, aes("", fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 141 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ Type2) + #labs(title=pat2[i])+ 142 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 143 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 144 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 145 | x2=ggplot(SeuratObj@meta.data, aes("", fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 146 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ sex) + #labs(title=pat2[i])+ 147 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 148 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 149 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 150 | x3=ggplot(SeuratObj@meta.data, aes("", fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 151 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ Grade) + #labs(title=pat2[i])+ 152 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 153 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 154 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 155 | 156 | mylegend<-get_legend(x3+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Assignment",title.position = "top",title.theme = element_text(size=10)))) 157 | 158 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"Figure 3d.pdf")), height = 4, width =6,onefile = T) 159 | ggpubr::ggarrange(x1+NoLegend(),x2+NoLegend(),x3+NoLegend(),ncol =1,nrow = 3, 160 | legend.grob = mylegend,legend = "right") 161 | dev.off() 162 | 163 | 164 | 165 | 166 | #Figure 3e 167 | Final=list() 168 | for(i in 1:length(levels(as.factor(SeuratObj@meta.data$Patient)))){ 169 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==levels(as.factor(SeuratObj@meta.data$Patient))[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 170 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + #facet_grid(~ Fragment) + # 171 | labs(title=levels(as.factor(SeuratObj@meta.data$Patient))[i])+ 172 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 173 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 174 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 175 | Final[[i]]=x 176 | } 177 | mylegend<-get_legend(Final[[1]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Assignment",title.position = "top",title.theme = element_text(size=10)))) 178 | 179 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts Patient and Assignment figure 3e.pdf")), height = 6, width =10,onefile = T) 180 | 181 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[2]]+NoLegend(),NA,NA,NA,NA,Final[[3]]+NoLegend(),Final[[4]]+NoLegend(), 182 | Final[[5]]+NoLegend(),Final[[6]]+NoLegend(),Final[[7]]+NoLegend(),Final[[8]]+NoLegend(), 183 | Final[[9]]+NoLegend(),Final[[10]]+NoLegend(),Final[[11]]+NoLegend(),Final[[12]]+NoLegend(),Final[[13]]+NoLegend(),NA, 184 | Final[[14]]+NoLegend(),Final[[15]]+NoLegend(),Final[[16]]+NoLegend(),Final[[17]]+NoLegend(),Final[[18]]+NoLegend(),NA, 185 | ncol = 6,nrow = 4, 186 | legend.grob = mylegend,legend = "right",widths = c(1,1,1,1,1,1)) 187 | dev.off() 188 | 189 | 190 | 191 | 192 | #Figure 3f 193 | pdf(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"violinplot fig 3f.pdf"),width = 6.5,height =4,family = "ArialMT") 194 | VlnPlot(SeuratObj,features="PDCD1", group.by = "Patient", cols = SampleColors,log = T)+theme_min()+RotatedAxis() 195 | dev.off() 196 | 197 | 198 | saveRDS(SeuratObj,file = paste0(RobjDirectory,"TCellClusters-11-3-21 patients renamed.rds")) 199 | -------------------------------------------------------------------------------- /R/Figure4.R: -------------------------------------------------------------------------------- 1 | library(easypackages) 2 | MyPackages<-c("dplyr","ggplot2","ggpubr","gridExtra","viridis","egg", 3 | "grid","lattice","gtools","Biobase","RColorBrewer","tibble", 4 | "Seurat","cowplot","patchwork","stringr","ComplexHeatmap","SingleCellExperiment", "ggmin") 5 | libraries(MyPackages) 6 | 7 | #Set working directory 8 | Directory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESNovember2021/" 9 | #dir.create(Directory) 10 | 11 | setwd(Directory) 12 | 13 | #Change variables 14 | 15 | ObjName= "HumanGlioma-AllSamples-" 16 | Subset="Myeloid-no Tcells or Astrocytes" 17 | resolution= 0.25 18 | dimz=1:30 19 | abb="MC" 20 | 21 | RobjDirectory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/R_Objects/" 22 | 23 | OutputDirectory=paste0(Directory,"Output/",Subset,"/") 24 | #dir.create(OutputDirectory) 25 | featuremapDirectory=paste0(OutputDirectory,"Featuremaps/") 26 | #dir.create(featuremapDirectory) 27 | 28 | #Load File 29 | SeuratObj=readRDS(paste0(RobjDirectory,"MyeloidClusters-8-10-21-patients renamed.rds")) 30 | 31 | #Extract Meta Data 32 | CellInfo <- SeuratObj@meta.data 33 | 34 | #Assign colors 35 | SampleColors=c(`LGG-03` = "#003F5C", `LGG-04` = "#008796", `ndGBM-01` = "#2F4B7C", 36 | `ndGBM-02` = "#227ED4", `ndGBM-03` = "#665191", `ndGBM-04` = "#9E71C7", 37 | `ndGBM-05` = "#A05195", `ndGBM-06` = "#E082C1", `ndGBM-07` = "#D45087", 38 | `ndGBM-08` = "#F57689", `ndGBM-09` = "#BD3E3E", `ndGBM-10` = "#F95D6A", 39 | `ndGBM-11` = "#E07B18", `rGBM-01` = "#FF7C43", `rGBM-02` = "#FFA600", 40 | `rGBM-03` = "#F5D256", `rGBM-04` = "#42A665", `rGBM-05` = "#8DB032" 41 | ) 42 | 43 | ClusterColors=c(MC01 = "#003F5C", MC02 = "#E25159", MC03 = "#73589E", MC04 = "#8DB032", 44 | MC05 = "#F6CC4B", MC06 = "#B863A5", MC07 = "#F77B38", MC08 = "#2D5186", 45 | MC09 = "#E46388") 46 | 47 | AssignmentColors=c(`a-microglia` = "#003F5C", `s-mac 1` = "#2D5187", `AP-microglia` = "#74599E", 48 | DCs = "#B863A5", `h-microglia` = "#E46388", `i-microglia` = "#E25159", 49 | `MDSC` = "#F77B38", Proliferating = "#F6CC4B", `s-mac 2` = "#8DB032" 50 | ) 51 | 52 | 53 | #Figure 4a 54 | U1=DimPlot(SeuratObj, reduction = "umap",group.by = "Cluster" ,cols = ClusterColors,label=T)+ggmin::theme_min()+ 55 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 56 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Clusters")+ 57 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 5)) 58 | 59 | pdf(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"Clusters UMAP Figure 4b.pdf"),width = 5.5,height =6,family = "ArialMT") 60 | U1 61 | dev.off() 62 | 63 | 64 | #Figure 4b 65 | pdf(paste0(OutputDirectory,"dotplot ",ObjName,Subset, "by Cluster figure4c.pdf"),width=10,height=3.5) 66 | 67 | DotPlot(SeuratObj,group.by = "Cluster" ,features = c('ITGAM','ITGAX','CD14','CD33','CD68',"MSR1" 68 | ,'HLA-DRB1',"CD74","B2M",'IFNGR1','CD1C','S100A4','MIF','LYZ', 69 | 'CX3CR1','TMEM119','P2RY12','BHLHE41','SPRY1','CCL4', 70 | 'CCL3','ITGA4','IL1B','IL6','IL12A','IL23A','CXCL9',"NFKBIZ" 71 | ,'NOS2','CD80','CD86','TNF','MRC1','IL1R1', 72 | 'CCL17','TGFB1','IGF1','FN1','CCL1','IL10','TNFSF14', 73 | 'MERTK','CXCL13','CD163','VEGFA',"MKI67"), 74 | scale = T)+RotatedAxis()+RotatedAxis()+scale_colour_viridis_c(option = "plasma") 75 | 76 | dev.off() 77 | #Figure 4c 78 | pdf(paste0(OutputDirectory,ObjName,Subset, "bargraph fig4c.pdf"),width=8.5,height=5) 79 | 80 | CellInfo %>% ggplot(aes(x=Cluster, fill=fct_rev(Patient))) +scale_fill_manual(values = SampleColors)+ 81 | geom_bar(color="black",position = "fill",width = 0.7) + 82 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), legend.text = element_text(color="black",size=13,face = "bold"),legend.title = element_text(color="black",size=13,face = "bold"), 83 | axis.line = element_line(colour = "black"), axis.text.y = element_text(color="black",size=15),axis.text.x = element_text(color="black",angle = 45,hjust = 1,size=19), 84 | axis.text=element_text(size=15, face="bold"), axis.title=element_text(size=19,face="bold"), 85 | plot.margin = unit(c(0.2, 0.5,0.2, 0.5),"cm"))+labs(y ="Composition(percentage of cells)", x= NULL)+ 86 | scale_y_continuous(expand = c(0,0),limits = c(0,1),breaks = c(0,0.20,0.40,0.60,0.80,1),labels = scales::percent)+ 87 | theme(legend.title = element_blank()) 88 | 89 | dev.off() 90 | #Figure 4d-> made in prism 91 | write.csv(as.matrix(table(SeuratObj@meta.data$Assignment,SeuratObj@meta.data$Patient)), 92 | file = paste0(OutputDirectory,ObjName,Subset," number of cells per Assignment and sample to make fig4d.csv")) 93 | #Figure 4e 94 | library(presto) 95 | library(msigdbr) 96 | library(dplyr) 97 | library(fgsea) 98 | library(tibble) 99 | library(pheatmap) 100 | 101 | m_df_H<- msigdbr(species = "Homo sapiens", category = cat) 102 | Annot.pathway2=as.data.frame(levels(as.factor(m_df_H$gs_name))) 103 | names(Annot.pathway2)="pathway" 104 | fgsea_sets<- m_df_H %>% split(x = .$gene_symbol, f = .$gs_name) 105 | Clusters=levels(as.factor(Annot$Cluster)) 106 | #run test 107 | Clusters.genes <- wilcoxauc(SeuratObj , 'Cluster') 108 | dplyr::count(Clusters.genes, group) 109 | objname="Glioma" 110 | name="Hallmark" 111 | 112 | for (i in 1: length(Clusters)){ 113 | X= Clusters[i] 114 | print(X) 115 | Genes<-Clusters.genes %>% 116 | dplyr::filter(group == Clusters[[i]]) %>% 117 | arrange(desc(auc)) %>% 118 | dplyr::select(feature, auc) 119 | ranks<- deframe(Genes) 120 | head(ranks) 121 | 122 | fgseaRes<- fgsea(fgsea_sets, stats = ranks,minSize=10) 123 | fgseaResTidy <- fgseaRes %>% 124 | as_tibble() %>% 125 | arrange(desc(NES)) 126 | fgseaResTidy %>% 127 | dplyr::select(-leadingEdge, -ES, -log2err) %>% 128 | arrange(padj) %>% 129 | head() 130 | fgseaResTidy$Enrichment = ifelse(fgseaResTidy$NES > 0, "Up-regulated", "Down-regulated") 131 | Filtidy<-fgseaResTidy %>% filter(padj < 0.05) 132 | filtRes = rbind(Filtidy %>% filter(Enrichment == "Up-regulated") %>% head(n= 10), 133 | Filtidy %>% filter(Enrichment == "Down-regulated") %>% head(n= 10)) 134 | Y<-fgseaResTidy 135 | hmc=as.data.frame(Y) 136 | hmc=apply(Y,2,as.character) 137 | #write.csv(hmc,paste(name, X,".csv")) 138 | names(Y)[names(Y)=="NES"]=paste(X) 139 | Annot.pathway<-Y[,c("pathway",paste(X))] 140 | Annot.pathway2<-merge(Annot.pathway, Annot.pathway2, by.x="pathway", by.y="pathway") 141 | 142 | 143 | 144 | } 145 | ##make a heatmap 146 | rownames(Annot.pathway2)=Annot.pathway2$pathway 147 | Annot.pathway2=Annot.pathway2[,-1] 148 | positions=Clusters 149 | 150 | Annot.pathway2=Annot.pathway2[,positions] 151 | Annot.pathway2[is.na(Annot.pathway2)]=0 152 | pathnames=c(row.names(Annot.pathway2 %>% top_n(n = 2, wt = eval(parse(text=names(Annot.pathway2)[1])))),row.names(Annot.pathway2 %>% top_n(n = -2, wt = eval(parse(text=names(Annot.pathway2)[1]))))) 153 | for (i in 2: length(names(Annot.pathway2))){ 154 | pathnames1=c(row.names(Annot.pathway2 %>% top_n(n = 2, wt = eval(parse(text=names(Annot.pathway2)[i])))),row.names(Annot.pathway2 %>% top_n(n = -2, wt = eval(parse(text=names(Annot.pathway2)[i]))))) 155 | pathnames=c(pathnames,pathnames1) 156 | } 157 | pathnames =pathnames[!duplicated(pathnames)] 158 | Annot.pathway4=Annot.pathway2[pathnames,] 159 | Annot.pathway4[is.na(Annot.pathway4)]=0 160 | redblackblu=CustomPalette(low = "blue", high = "red", mid = "white", k = 1000) 161 | pheatmap(Annot.pathway4,scale = "none",cluster_rows = T,cluster_cols = F,border_color = "black",treeheight_row = 0,legend_breaks = c(-5,-2,0,2,5), 162 | fontsize =8, cellwidth = 15,cellheight =10,color= plasma(10),angle_col = 90, 163 | main = paste(" ",name,objname," (NES)"),filename = paste("GSEA",name," (NES) top and bottom 2each-Heatmap Fig4e.pdf")) 164 | 165 | #Figure 4f 166 | library(gprofiler2) 167 | library(stringr) 168 | library(dplyr) 169 | library(GOplot) 170 | Myeloid.Markers=Clusters.genes 171 | names(Myeloid.Markers)[1] <- "ID" 172 | names(Myeloid.Markers)[8] <- "adj.P.Val" 173 | names(Myeloid.Markers)[2] <- "cluster" 174 | 175 | Clusters=levels(as.factor(CellInfo$Cluster)) 176 | for (i in 1: length(Clusters)){ 177 | X= Clusters[i] 178 | Genes<-Clusters.genes %>% 179 | dplyr::filter(group == X) %>% 180 | dplyr::filter(padj <= 0.05) %>% 181 | dplyr::filter(logFC <= -0.1|logFC >= 0.1) %>% 182 | arrange(desc(auc)) %>% 183 | dplyr::select(feature) 184 | Y=gost(query = Genes$feature,organism = "hsapiens",ordered_query = T,multi_query = F,evcodes =T, 185 | significant = T,correction_method = "fdr" ) 186 | Z=Y[["result"]] 187 | names(Z)[10] <- "Category" 188 | names(Z)[3] <- "adj_pval" 189 | names(Z)[16] <- "Genes" 190 | names(Z)[9] <- "ID" 191 | names(Z)[11] <- "Term" 192 | 193 | assign(X, Z) 194 | } 195 | Final=list() 196 | for (i in 1:9){ 197 | MC=paste0("MC0",i) 198 | print(MC) 199 | DEgenes=Myeloid.Markers%>%dplyr::filter(cluster==MC) 200 | df=eval(parse(text=MC)) 201 | circ<-circle_dat(df,DEgenes) 202 | process = c("canonical glycolysis","cellular response to hypoxia","Antigen processing-Cross presentation","cellular response to interferon-gamma","cellular response to tumor necrosis factor","NF-kappa B signaling pathway","oxidative phosphorylation","M Phase") 203 | genes=as.data.frame(DEgenes[,c("ID","logFC")]) 204 | chord=chord_dat(circ,process = process,genes = genes) 205 | Chord1=chord[,colSums(chord)>0] 206 | term.col<-c("#73A27E","#73C2B5","#008F91","#194D94","#7D0202","#CC2929","#F47C54","#F9A953") 207 | names(term.col)=process 208 | P1=GOCluster(chord,process = process,lfc.col =c("darkred","white","darkblue"),term.col = term.col,lfc.min = -1,lfc.max = 1)+guides(size=guide_legend("GO Terms",title.position ="top",order = 20,ncol=2,byrow=F,override.aes=list(shape=22,fill=term.col,size = 4,linetype = 0)))+ 209 | theme(legend.position='right',legend.background = element_rect(fill='transparent'),legend.spacing.x = unit(0.1, 'cm'),legend.spacing.y = unit(0.1, 'cm'),legend.title = element_text(size=10,face="bold"), 210 | legend.box='horizontal',legend.box.just = "center",legend.key.height = unit(0.3,'cm'),legend.key.width =unit(0.5,'cm'),legend.direction='horizontal',legend.text = element_text(size=9,face="bold"),plot.margin = unit(c(0,0,0,0),'cm'))+ggtitle(paste0("MC",i))+ 211 | theme(plot.title = element_text(hjust = 0.5,vjust = -5,face = "bold",size = 10),panel.spacing = unit(c(0,0,0,0),'cm'),plot.margin = unit(c(0,-1,-0.5,-1),'cm'),axis.text = element_blank(),axis.ticks.length=unit(0,'cm'))+labs(x=NULL, y=NULL) 212 | Final[[i]]=ggplotGrob(P1) 213 | NAME=paste0("Graphs.MC0",i) 214 | assign(NAME, P1) 215 | print(paste(MC,"finished")) 216 | } 217 | g_legend<-function(a.gplot){ 218 | tmp <- ggplot_gtable(ggplot_build(a.gplot)) 219 | leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 220 | legend <- tmp$grobs[[leg]] 221 | return(legend)} 222 | 223 | mylegend<-g_legend(Graphs.MC01) 224 | lay <- rbind(c(1,2,3), 225 | c(4,5,6), 226 | c(7,8,9), 227 | c(10,10,10)) 228 | mylegend$vp$angle=0 229 | png(paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"Figure4c.png")),width=7,height=6.5,units = "in",res = 1200,type = "quartz",pointsize = 10) 230 | grid.arrange(Graphs.MC01+ theme(legend.position="none"),Graphs.MC02+ theme(legend.position="none"), 231 | Graphs.MC03+ theme(legend.position="none"),Graphs.MC04+ theme(legend.position="none"), 232 | Graphs.MC05+ theme(legend.position="none"),Graphs.MC06+ theme(legend.position="none"), 233 | Graphs.MC07+ theme(legend.position="none"),Graphs.MC08+ theme(legend.position="none"), 234 | Graphs.MC09+ theme(legend.position="none"),mylegend, ncol = 3, nrow = 4,layout_matrix=lay) 235 | dev.off() 236 | Idents(SeuratObj)=SeuratObj$Cluster 237 | Genes=c("ITGAM","ITGAX","TGFBI","CD68","CD163","CD1C","BATF3","P2RY12") 238 | FeaturePlot(SeuratObj,features=Genes,order = T,ncol = 4,label = T,label.size = 2,cols = Nour_cols(c("darkpurple","lightorange"))) 239 | for (i in 1:length(c(Genes))){ 240 | Y=Genes[i] 241 | X=FeaturePlot(SeuratObj,features=Y,order = T,label = T,label.size = 2,cols = Nour_cols(c("darkpurple","lightorange")))+theme_min() 242 | pdf(paste0(featuremapDirectory,Y," featuremap ",ObjName," ",Subset,".pdf"),width = 4.5,height = 4) 243 | print(X) 244 | dev.off() 245 | 246 | } 247 | 248 | 249 | #Figure 4g 250 | 251 | Idents(SeuratObj)=CellInfo$Cluster 252 | sampled.cells <- sample(x = SeuratObj@active.ident, size = 20000, replace = F) 253 | subset=subset(SeuratObj,cells=names(sampled.cells)) 254 | 255 | Counts <- as.matrix(GetAssayData(object = subset, assay = "RNA", slot = "counts")) 256 | 257 | 258 | 259 | m_df_H<- msigdbr(species = "Homo sapiens", category = "H") 260 | Filtered.genesets=filter(m_df_H,gs_name=="HALLMARK_OXIDATIVE_PHOSPHORYLATION"|gs_name=="HALLMARK_TNFA_SIGNALING_VIA_NFKB"|gs_name=="HALLMARK_INTERFERON_GAMMA_RESPONSE"|gs_name=="HALLMARK_HYPOXIA") 261 | geneset.all <- Filtered.genesets[Filtered.genesets$gene_symbol %in% rownames(Counts),] 262 | geneset <- geneset.all[!duplicated(geneset.all$gene_symbol),] 263 | genesetlist=geneset.all%>% split(x = .$gene_symbol, f = .$gs_name) 264 | #get score 265 | sc=score(mat=Counts, 266 | groups=genesetlist, 267 | binmat = NULL, 268 | bins = NULL, 269 | controls = NULL, 270 | bin.control = F, 271 | center = T, 272 | nbin = 30, 273 | n = 100, 274 | replace = T) 275 | #get coordinates 276 | h=hierarchy (sc, quadrants = c("HALLMARK_OXIDATIVE_PHOSPHORYLATION","HALLMARK_TNFA_SIGNALING_VIA_NFKB","HALLMARK_INTERFERON_GAMMA_RESPONSE","HALLMARK_HYPOXIA"), log.scale =T) 277 | group.cols= ClusterColors 278 | CellInfo=SeuratObj@meta.data 279 | for( i in 1: length(names(ClusterColors))){ 280 | name=names(ClusterColors)[i] 281 | color=ClusterColors[i] 282 | CellInfo$Clustercolor [CellInfo$Cluster== name] <- color 283 | } 284 | Clustercolor <- CellInfo$Clustercolor 285 | names(Clustercolor ) <- row.names(CellInfo) 286 | SeuratObj <- AddMetaData(object = SeuratObj,metadata = Clustercolor ,col.name = 'Clustercolor') 287 | #figure 2d 288 | #Make the big plot--> all clusters 289 | groups=SeuratObj@meta.data[,c("Clustercolor","Cluster")] 290 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 291 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 292 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 293 | row.names(matrix)=matrix$Row.names 294 | x=matrix$Clustercolor 295 | y=matrix$Cluster 296 | col=x[!duplicated(x)] 297 | names(col)=y[!duplicated(y)] 298 | matrix=matrix[,-1] 299 | title="All Myeloid Clusters" 300 | 301 | xmax=max(h$X)+(max(h$X)*0.2) 302 | xmin=min(h$X)+(min(h$X)*0.2) 303 | ymax=max(h$Y)+(max(h$Y)*0.6) 304 | ymin=min(h$Y)+(min(h$Y)*0.6) 305 | 306 | #Make the big plot--> all Clusters 307 | groups=SeuratObj@meta.data[,c("Clustercolor","Cluster")] 308 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 309 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 310 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 311 | row.names(matrix)=matrix$Row.names 312 | x=matrix$Clustercolor 313 | y=matrix$Cluster 314 | col=x[!duplicated(x)] 315 | names(col)=y[!duplicated(y)] 316 | matrix=matrix[,-1] 317 | title="All Myeloid Clusters" 318 | p0 <- ggplot(matrix, aes(x = X, 319 | y =Y,color=factor(Cluster)))+geom_point()+geom_point(data = subset(matrix, Cluster !="Other"))+ 320 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Cluster")+theme(legend.position = "none")+ 321 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 322 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 323 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 324 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 325 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 326 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 327 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 328 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 329 | geom_hline(yintercept=0, color = "black", size=0.5)+ 330 | geom_vline(xintercept=0, color = "black", size=0.5)+ 331 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 332 | annotate("text",x = xmin+2, y = ymax-0.5,label = "IFNG-res",color="white",fontface="bold",size=8)+ 333 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 334 | annotate("text",x = xmax-2, y = ymax-0.5,label = "Hypoxia",color="white",fontface="bold",size=8)+ 335 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 336 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Ox-Phos",color="white",fontface="bold",size=8)+ 337 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 338 | annotate("text",x = xmax-2, y = ymin+0.5,label = "TNFA-sig",color="white",fontface="bold",size=8) 339 | P0=ggplotGrob(p0) 340 | ##make the small plots--> one per Cluster 341 | Final <- list() 342 | Final[[1]]=ggplotGrob(p0) 343 | for (i in 1:length(names(ClusterColors))) { 344 | ClusterMD=SeuratObj@meta.data[SeuratObj@meta.data$Cluster==paste0(names(ClusterColors)[i]),] 345 | groups=ClusterMD[,c("Clustercolor","Cluster")] 346 | title=paste0(names(ClusterColors)[i]) 347 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 348 | matrix$Clustercolor[is.na(matrix$Clustercolor)] <- "gray" 349 | matrix$Cluster=as.character(matrix$Cluster) 350 | matrix$Cluster[is.na(matrix$Cluster)] <- "Other" 351 | row.names(matrix)=matrix$Row.names 352 | x=matrix$Clustercolor 353 | y=matrix$Cluster 354 | col=x[!duplicated(x)] 355 | names(col)=y[!duplicated(y)] 356 | matrix=matrix[,-1] 357 | P=ggplot(matrix, aes(x = X, 358 | y =Y,color=factor(Cluster)))+geom_point()+geom_point(data = subset(matrix, Cluster !="Other"))+ 359 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 360 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 361 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 362 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 363 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 364 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 365 | geom_hline(yintercept=0, color = "black", size=0.5)+ 366 | geom_vline(xintercept=0, color = "black", size=0.5)+ 367 | 368 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 369 | annotate("text",x = xmin+2, y = ymax-0.5,label = "IFNG-res",color="white",fontface="bold",size=4)+ 370 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 371 | annotate("text",x = xmax-2, y = ymax-0.5,label = "Hypoxia",color="white",fontface="bold",size=4)+ 372 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 373 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Ox-Phos",color="white",fontface="bold",size=4)+ 374 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 375 | annotate("text",x = xmax-2, y = ymin+0.5,label = "TNFA-sig",color="white",fontface="bold",size=4) 376 | Final[[i+1]] = ggplotGrob(P) 377 | } 378 | Final[[1]]=ggplotGrob(p0) 379 | numofplots= length(Final) 380 | pdf(file =paste0(OutputDirectory,ObjName,Subset," pathway butterfly by Cluster fig4g.pdf"), height = 6, width =17,onefile = T) 381 | grid.arrange(grobs=Final, widths = c(2,1,1,1,1,1),layout_matrix = rbind(c(1, 2, 3,4,5,6),c(1,7,8,9,10,NA))) 382 | dev.off() 383 | 384 | 385 | 386 | 387 | #Figure 4h 388 | 389 | #Load Dataset 390 | CGGA.EXP <- read.table("~/Downloads/CGGA.mRNAseq_325.RSEM-genes.20200506.txt",as.is=T,header = T,row.names=1) 391 | AnnotationCGGA <- read.csv("~/Downloads/CGGA.mRNAseq_All_clinical.csv",as.is=T,header = T,row.names=1) 392 | CGGA.GBM=filter(AnnotationCGGA,Histology=="GBM") 393 | #got markers 394 | AllMyeloid.Markers.surv= read.csv("~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/Output/Myeloid-no Tcells or Astrocytes/HumanGlioma-AllSamples-Myeloid-no Tcells or Astrocytes cluster markers res0.25.csv") 395 | Myeloid.Markers.surv=AllMyeloid.Markers.surv %>% group_by(cluster) %>% top_n(n = 50, wt = avg_log2FC) 396 | #write.csv(Myeloid.Markers.surv,"~/Box/Yun lab manuscripts/GBM Single Cells/Nature Communications revision/RCode/files needed for figure 2e and f/Myeloidmarkers.csv") 397 | #Myeloid.Markers.surv=top20.markers 398 | 399 | #remove genes from signature gene lists that do not exist in expression matrix ##otherwise the code won't work 400 | Counts=as.data.frame(CGGA.EXP) 401 | geneset.all <- Myeloid.Markers.surv[ Myeloid.Markers.surv$gene %in% rownames(Counts),] 402 | geneset <- geneset.all[!duplicated(geneset.all$gene),] 403 | genesetlist=geneset.all%>% split(x =.$gene, f = .$cluster,drop = F) 404 | Scounts=Counts[geneset$gene,] 405 | #get scores 406 | library(scrabble) 407 | 408 | sc=score(mat=Scounts, 409 | groups=genesetlist, 410 | binmat = NULL, 411 | bins = NULL, 412 | controls = NULL, 413 | bin.control = F, 414 | center = T, 415 | nbin = 30, 416 | n = 100, 417 | replace = T) 418 | #write.csv(sc,paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"cluster CGGAscores.csv"))) 419 | #scores will slightly chage everytime you run them because of random sampling of control genes 420 | #sc=read.csv("~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021//Output/Myeloid-no Tcells or Astrocytes/HumanGlioma-AllSamples-Myeloid-no Tcells or Astrocytesres0.25cluster CGGAscores.csv",row.names = 1) 421 | ##Survival Analysis 422 | library(survival) 423 | library(survminer) 424 | CGGA.sc=merge(sc,AnnotationCGGA,by.x=0, by.y=0) 425 | CGGA.sc$OS<-as.numeric(CGGA.sc$survival) 426 | CGGA.sc$status<-as.numeric(CGGA.sc$status) 427 | surv_object <- Surv(time = CGGA.sc$OS, event = CGGA.sc$status) 428 | #for loop to assign "positive" or "negative" to cell scores then draw survival plots 429 | data.subset=colnames(sc) 430 | data.subset=sort(data.subset, decreasing = FALSE) 431 | CGGA.sc2 =CGGA.sc 432 | Final <- list() 433 | rnames=c("ExpLevelPositive", "GenderMale", "HistologyAnaplastic Oligodendrolgioma", 434 | "HistologyAstrocytoma", "HistologyGBM", "HistologyOligodendroglioma", 435 | "RecurrenceRecurrent", "RecurrenceSecondary", 436 | "MGMT_statusun-methylated", "IDH_mutation_statusWildtype") 437 | Summtable=as.data.frame(row.names =rnames,x=rnames ) 438 | for( i in 1: length(data.subset)) 439 | { 440 | YY= data.subset[i] 441 | Expression.Level=paste(YY,"Levels",sep=".") 442 | CGGA.sc2 <- CGGA.sc2%>% mutate(Expression.Level = ifelse(CGGA.sc2[YY]>=0, "Positive", "Negative")) 443 | CGGA.sc2$Expression.Level <-factor(CGGA.sc2$Expression.Level) 444 | fit1 <- survfit(surv_object ~Expression.Level, data = CGGA.sc2) 445 | #next three lines sdded by Joshy 446 | fit1.mv <- coxph(surv_object ~Expression.Level + Gender + Histology + Recurrence + MGMT_status +IDH_mutation_status , data = CGGA.sc2) 447 | mv.res <- summary(fit1.mv)$coefficients 448 | mv.fname <- paste(OutputDirectory,ObjName,Subset,"res",resolution,data.subset[i],"_multivariat_results.csv",sep="") 449 | write.csv(mv.res,mv.fname) 450 | mv.res=as.data.frame(mv.res) 451 | Summtable[i]=mv.res$`Pr(>|z|)` 452 | colnames(Summtable)[i]=YY 453 | XX <- ggsurvplot(fit1, data = CGGA.sc2, pval = TRUE,pval.cExpression.Levelrd = c(50, 1),pval.size=6,legend="top",palette = c("Blue","Red"),risk.table = F,font.main = c(12, "bold"),legend.title = "status", 454 | title=YY, legend.labs = c("Not-Enriched", "Enriched"),font.x = c(14, "bold"),font.y = c(14, "bold"),font.tickslab = c(12, "plain"),xlab="Time (Months)") 455 | Final[[i]] = XX 456 | } 457 | write.csv(Summtable,paste(OutputDirectory,ObjName,Subset,"res",resolution,"summary_multivariat_results supp table 10.csv",sep="")) 458 | 459 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"survival plots CGGA allFigure4E.pdf")), height = 3.5, width =16) 460 | arrange_ggsurvplots(Final,print = TRUE, ncol = 5,nrow = 1,surv.plot.height = NULL,risk.table.height = NULL,ncensor.plot.height = NULL) 461 | dev.off() 462 | ### to get figure 4f- I subset the matrix and only get the scores for GBM patients 463 | x=intersect(row.names(CGGA.GBM),colnames(Counts)) 464 | Scounts=Scounts[,x] 465 | #get scores ###don't forget to load the functions from ##scrabble package into your environment 466 | sc=score(mat=Scounts, 467 | groups=genesetlist, 468 | binmat = NULL, 469 | bins = NULL, 470 | controls = NULL, 471 | bin.control = F, 472 | center = T, 473 | nbin = 30, 474 | n = 100, 475 | replace = T) 476 | #write.csv(sc,paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"cluster GBMonlyCGGAscores.csv"))) 477 | #Since everytime you run the scoring the results are slightly different I provided the scoring file that reproduces the exact figures in the paper 478 | #sc=read.csv(paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"cluster GBMonlyCGGAscores.csv")),row.names = 1) 479 | ##Survival Analysis 480 | library(survival) 481 | library(survminer) 482 | CGGA.sc=merge(sc,AnnotationCGGA,by.x=0, by.y=0) 483 | CGGA.sc$OS<-as.numeric(CGGA.sc$survival) 484 | CGGA.sc$status<-as.numeric(CGGA.sc$status) 485 | surv_object <- Surv(time = CGGA.sc$OS, event = CGGA.sc$status) 486 | #for lExpression.Levelp to assign "positive" or "negative" to cell scores then draw survival plots 487 | data.subset=colnames(sc) 488 | data.subset=sort(data.subset, decreasing = FALSE) 489 | CGGA.sc2 =CGGA.sc 490 | Final <- list() 491 | rnames=c("ExpLevelPositive", "GenderMale", 492 | "RecurrenceRecurrent", "RecurrenceSecondary", 493 | "MGMT_statusun-methylated", "IDH_mutation_statusWildtype") 494 | Summtable=as.data.frame(row.names =rnames,x=rnames ) 495 | 496 | for( i in 1: length(data.subset)) 497 | { 498 | YY= data.subset[i] 499 | Expression.Level=paste(YY,"Levels",sep=".") 500 | CGGA.sc2 <- CGGA.sc2%>% mutate(Expression.Level = ifelse(CGGA.sc2[YY]>=0, "Positive", "Negative")) 501 | CGGA.sc2$Expression.Level <-factor(CGGA.sc2$Expression.Level) 502 | fit1 <- survfit(surv_object ~Expression.Level, data = CGGA.sc2) 503 | #next three lines sdded by Joshy 504 | fit1.mv <- coxph(surv_object ~Expression.Level + Gender + Recurrence + MGMT_status +IDH_mutation_status , data = CGGA.sc2) 505 | mv.res <- summary(fit1.mv)$coefficients 506 | mv.fname <- paste(OutputDirectory,ObjName,Subset,"res",resolution,data.subset[i],"_GBM only multivariat_results.csv",sep="") 507 | write.csv(mv.res,mv.fname) 508 | mv.res=as.data.frame(mv.res) 509 | Summtable[i]=mv.res$`Pr(>|z|)` 510 | colnames(Summtable)[i]=YY 511 | 512 | XX <- ggsurvplot(fit1, data = CGGA.sc2, pval = TRUE,pval.cExpression.Levelrd = c(50, 1),pval.size=6,legend="top",palette = c("Blue","Red"),risk.table = F,font.main = c(12, "bold"),legend.title = "status", 513 | title=YY, legend.labs = c("Not-Enriched", "Enriched"),font.x = c(14, "bold"),font.y = c(14, "bold"),font.tickslab = c(12, "plain"),xlab="Time (Months)") 514 | Final[[i]] = XX 515 | } 516 | write.csv(Summtable,paste(OutputDirectory,ObjName,Subset,"res",resolution,"GBM onlysummary_multivariat_results supp table 10.csv",sep="")) 517 | 518 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"GBM only survivalFigure4F.pdf")), height = 3.5, width =16) 519 | arrange_ggsurvplots(Final,print = TRUE, ncol = 5,nrow = 1,surv.plot.height = NULL,risk.table.height = NULL,ncensor.plot.height = NULL) 520 | dev.off() 521 | 522 | -------------------------------------------------------------------------------- /R/Figure5.R: -------------------------------------------------------------------------------- 1 | library(easypackages) 2 | MyPackages<-c("dplyr","ggplot2","ggpubr","gridExtra","viridis","egg", 3 | "grid","lattice","gtools","Biobase","RColorBrewer","tibble", 4 | "Seurat","cowplot","patchwork","stringr","ComplexHeatmap","SingleCellExperiment", "ggmin") 5 | libraries(MyPackages) 6 | #Figure 6A 7 | Directory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/" 8 | RobjDirectory=paste0(Directory,"R_Objects/") 9 | 10 | #Figure5a 11 | #load all cluster r object 12 | 13 | SeuratObj=readRDS(paste0(RobjDirectory,"Allhuman-11-3-21 patients renamed.rds")) 14 | AssignmentColors=c(TCells = "#8DB032", BCells = "#F9BF31", Other = "#E47B1E", 15 | Myeloid = "#CD4D53", Endo = "#DC73B0", Oligo = "#966CBF", Pericytes = "#2960A1", 16 | Glioma = "#003F5C") 17 | 18 | pat2=c("LGG-04", "ndGBM-01", "ndGBM-02", "ndGBM-03","ndGBM-11", "rGBM-01", "rGBM-02", "rGBM-03", "rGBM-04", "rGBM-05") 19 | 20 | for(i in 1:length(pat2)){ 21 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==pat2[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 22 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ Fragment) + 23 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 24 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 25 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 26 | Final[[i]]=x 27 | } 28 | mylegend<-get_legend(Final[[2]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Assignment",title.position = "top",title.theme = element_text(size=10)))) 29 | 30 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts fragment and assignment figure 5a.pdf")), height = 6, width =10,onefile = T) 31 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[6]]+NoLegend(),Final[[2]]+NoLegend(),Final[[7]]+NoLegend(),Final[[3]]+NoLegend(), 32 | Final[[8]]+NoLegend(),Final[[4]]+NoLegend(),Final[[9]]+NoLegend(),Final[[5]]+NoLegend(),Final[[10]],ncol = 2,nrow = 5, 33 | legend.grob = mylegend,legend = "right",widths = c(1,1)) 34 | dev.off() 35 | 36 | 37 | 38 | 39 | 40 | #Figure5c 41 | FragmentColors=c(`LGG-03` = "#003F5C", `LGG-04-1` = "#005B72", `LGG-04-2` = "#007789", 42 | `LGG-04-3` = "#087B91", `ndGBM-01-A` = "#1B6486", `ndGBM-01-C` = "#2D4C7C", 43 | `ndGBM-01-D` = "#2A5D9C", `ndGBM-01-F` = "#2572BF", `ndGBM-02-1` = "#2D76C9", 44 | `ndGBM-02-2` = "#4764AE", `ndGBM-02-4` = "#625394", `ndGBM-02-5` = "#795CA3", 45 | `ndGBM-03-1` = "#8F68B9", `ndGBM-03-2` = "#9E6CC0", `ndGBM-03-3` = "#9F5FAC", 46 | `ndGBM-04` = "#9F5398", `ndGBM-05` = "#B460A3", `ndGBM-06` = "#CE74B4", 47 | `ndGBM-07` = "#DE7CBA", `ndGBM-08` = "#D968A3", `ndGBM-09` = "#D5548C", 48 | `ndGBM-10` = "#DD5B87", `ndGBM-11-A` = "#EB6A88", `ndGBM-11-B` = "#EF7082", 49 | `ndGBM-11-C` = "#D95A64", `ndGBM-11-D` = "#C34446", `rGBM-01-A` = "#CD464A", 50 | `rGBM-01-B` = "#E5525B", `rGBM-01-C` = "#F75F64", `rGBM-01-D` = "#ED6A43", 51 | `rGBM-02-2` = "#E37623", `rGBM-02-3` = "#E77B23", `rGBM-02-4` = "#F47B34", 52 | `rGBM-02-5` = "#FF7D3F", `rGBM-03-1` = "#FF8E25", `rGBM-03-2` = "#FF9F0A", 53 | `rGBM-03-3` = "#FCB013", `rGBM-04-1` = "#F8C136", `rGBM-04-2` = "#F0D056", 54 | `rGBM-04-3` = "#AABF5C", `rGBM-04-4` = "#63AE62", `rGBM-05-1` = "#51A85A", 55 | `rGBM-05-2` = "#6FAC46", `rGBM-05-3` = "#8DB032") 56 | 57 | #load myeloid clusters R object 58 | SeuratObj=readRDS(paste0(RobjDirectory,"MyeloidClusters-7-20-21-patients renamed.rds")) 59 | set.seed(12) 60 | Idents(SeuratObj)=CellInfo$Cluster 61 | sampled.cells <- sample(x = SeuratObj@active.ident, size = 20000, replace = F) 62 | subset=subset(SeuratObj,cells=names(sampled.cells)) 63 | 64 | Counts <- as.matrix(GetAssayData(object = subset, assay = "RNA", slot = "counts")) 65 | 66 | library(msigdbr) 67 | m_df_H<- msigdbr(species = "Homo sapiens", category = "H") 68 | Filtered.genesets=filter(m_df_H,gs_name=="HALLMARK_OXIDATIVE_PHOSPHORYLATION"|gs_name=="HALLMARK_TNFA_SIGNALING_VIA_NFKB"|gs_name=="HALLMARK_INTERFERON_GAMMA_RESPONSE"|gs_name=="HALLMARK_HYPOXIA") 69 | geneset.all <- Filtered.genesets[Filtered.genesets$gene_symbol %in% rownames(Counts),] 70 | geneset <- geneset.all[!duplicated(geneset.all$gene_symbol),] 71 | genesetlist=geneset.all%>% split(x = .$gene_symbol, f = .$gs_name) 72 | #get score 73 | sc=score(mat=Counts, 74 | groups=genesetlist, 75 | binmat = NULL, 76 | bins = NULL, 77 | controls = NULL, 78 | bin.control = F, 79 | center = T, 80 | nbin = 30, 81 | n = 100, 82 | replace = T) 83 | #get coordinates 84 | h=hierarchy (sc, quadrants = c("HALLMARK_OXIDATIVE_PHOSPHORYLATION","HALLMARK_TNFA_SIGNALING_VIA_NFKB","HALLMARK_INTERFERON_GAMMA_RESPONSE","HALLMARK_HYPOXIA"), log.scale =T) 85 | 86 | CellInfo=SeuratObj@meta.data 87 | for( i in 1: length(names(FragmentColors))){ 88 | name=names(FragmentColors)[i] 89 | color=FragmentColors[i] 90 | CellInfo$Fragmentcolor [CellInfo$Fragment== name] <- color 91 | } 92 | Fragmentcolor <- CellInfo$Fragmentcolor 93 | names(Fragmentcolor ) <- row.names(CellInfo) 94 | SeuratObj <- AddMetaData(object = SeuratObj,metadata = Fragmentcolor ,col.name = 'Fragmentcolor') 95 | 96 | # make plots 97 | xmax=max(h$X)+(max(h$X)*0.2) 98 | xmin=min(h$X)+(min(h$X)*0.2) 99 | ymax=max(h$Y)+(max(h$Y)*0.6) 100 | ymin=min(h$Y)+(min(h$Y)*0.6) 101 | 102 | #Make the big plot--> all Fragments 103 | groups=SeuratObj@meta.data[,c("Fragmentcolor","Fragment")] 104 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 105 | matrix$Fragmentcolor[is.na(matrix$Fragmentcolor)] <- "gray" 106 | matrix$Fragment[is.na(matrix$Fragment)] <- "Other" 107 | row.names(matrix)=matrix$Row.names 108 | x=matrix$Fragmentcolor 109 | y=matrix$Fragment 110 | col=x[!duplicated(x)] 111 | names(col)=y[!duplicated(y)] 112 | matrix=matrix[,-1] 113 | title="All Myeloid Fragments" 114 | p0 <- ggplot(matrix, aes(x = X, 115 | y =Y,color=factor(Fragment)))+geom_point()+geom_point(data = subset(matrix, Fragment !="Other"))+ 116 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Fragment")+theme(legend.position = "none")+ 117 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 118 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 119 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 120 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 121 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 122 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 123 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 124 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 125 | geom_hline(yintercept=0, color = "black", size=0.5)+ 126 | geom_vline(xintercept=0, color = "black", size=0.5)+ 127 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 128 | annotate("text",x = xmin+2, y = ymax-0.5,label = "IFNG-res",color="white",fontface="bold",size=9)+ 129 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 130 | annotate("text",x = xmax-2, y = ymax-0.5,label = "Hypoxia",color="white",fontface="bold",size=9)+ 131 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 132 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Ox-Phos",color="white",fontface="bold",size=9)+ 133 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 134 | annotate("text",x = xmax-2, y = ymin+0.5,label = "TNFA-sig",color="white",fontface="bold",size=9) 135 | P0=ggplotGrob(p0) 136 | ##make the small plots--> one per Fragment 137 | 138 | for (i in 1:length(names(FragmentColors))) { 139 | FragmentMD=SeuratObj@meta.data[SeuratObj@meta.data$Fragment==paste0(names(FragmentColors)[i]),] 140 | groups=FragmentMD[,c("Fragmentcolor","Fragment")] 141 | title=paste0(names(FragmentColors)[i]) 142 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 143 | matrix$Fragmentcolor[is.na(matrix$Fragmentcolor)] <- "gray" 144 | matrix$Fragment=as.character(matrix$Fragment) 145 | matrix$Fragment[is.na(matrix$Fragment)] <- "Other" 146 | row.names(matrix)=matrix$Row.names 147 | x=matrix$Fragmentcolor 148 | y=matrix$Fragment 149 | col=x[!duplicated(x)] 150 | names(col)=y[!duplicated(y)] 151 | matrix=matrix[,-1] 152 | P=ggplot(matrix, aes(x = X, 153 | y =Y,color=factor(Fragment)))+geom_point()+geom_point(data = subset(matrix, Fragment !="Other"))+ 154 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 155 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 156 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 157 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 158 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 159 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 160 | geom_hline(yintercept=0, color = "black", size=0.5)+ 161 | geom_vline(xintercept=0, color = "black", size=0.5)+ 162 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 163 | annotate("text",x = xmin+2, y = ymax-0.5,label = "IFNG-res",color="white",fontface="bold",size=4)+ 164 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 165 | annotate("text",x = xmax-2, y = ymax-0.5,label = "Hypoxia",color="white",fontface="bold",size=4)+ 166 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 167 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Ox-Phos",color="white",fontface="bold",size=4)+ 168 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 169 | annotate("text",x = xmax-2, y = ymin+0.5,label = "TNFA-sig",color="white",fontface="bold",size=4) 170 | Final[[i+1]] = ggplotGrob(P) 171 | } 172 | Final[[1]]=ggplotGrob(p0) 173 | numofplots= length(Final) 174 | pdf(file =paste0(OutputDirectory,ObjName,Subset," pathway butterfly by Fragment figure 5b.pdf"), height = 15, width =35,onefile = T) 175 | grid.arrange(grobs=Final, widths = c(4,1,1,1,1,1,1,1,1,1,1,1),layout_matrix = rbind(c(1, 2, 3,4,5,NA,NA,NA,NA,NA,NA,NA),c(1,6,7,8,9,10, 11, 12,13,14,15,16), 176 | c(1,17,18,19, 20, 21,22,23,24,25,26,27), 177 | c(1, 28,29, 30,31,32,33,34,35,36,37,38), 178 | c(1, 39,40,41,42,43,44,45,NA,NA,NA,NA))) 179 | dev.off() 180 | 181 | 182 | ##please insert code for cellphonedb -------------------------------------------------------------------------------- /R/Figure6.R: -------------------------------------------------------------------------------- 1 | library(easypackages) 2 | MyPackages<-c("dplyr","ggplot2","ggpubr","gridExtra","viridis","egg", 3 | "grid","lattice","gtools","Biobase","RColorBrewer","tibble", 4 | "Seurat","cowplot","patchwork","stringr","ComplexHeatmap","SingleCellExperiment", "ggmin") 5 | libraries(MyPackages) 6 | 7 | #Figure 6A 8 | Directory="~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/" 9 | RobjDirectory=paste0(Directory,"R_Objects/") 10 | #load myeloid clusters R object 11 | SeuratObj=readRDS(paste0(RobjDirectory,"MyeloidClusters-7-20-21-patients renamed.rds")) 12 | VlnPlot(SeuratObj,group.by = "Cluster",features = "S100A4",cols=ClusterColors,pt.size = 0)+theme_min2()+NoLegend()+theme(title = element_blank()) 13 | 14 | 15 | #Figure 6B 16 | #load T cell clusters R object 17 | SeuratObj=readRDS(paste0(RobjDirectory,"TcellClusters-7-22-21-goodwithoutcluster6 new patient names.rds")) 18 | VlnPlot(SeuratObj,group.by = "Cluster",features = "S100A4",cols=ClusterColors,pt.size = 0)+theme_min2()+NoLegend()+theme(title = element_blank()) 19 | 20 | #Figure 6D 21 | 22 | library(survival) 23 | library(survminer) 24 | #load data 25 | CGGA.EXP <- read.table("~/Downloads/CGGA.mRNAseq_325.RSEM-genes.20200506.txt",as.is=T,header = T,row.names=1) 26 | AnnotationCGGA <- read.csv("~/Downloads/CGGA.mRNAseq_All_clinical.csv",as.is=T,header = T,row.names=1) 27 | CGGA.GBM=filter(AnnotationCGGA,Histology=="GBM") 28 | #extract S100A4 expression and merge with annotation 29 | data <- CGGA.EXP["S100A4",] 30 | t.data=t(data) 31 | CGGA.dat=merge(t.data,AnnotationCGGA,by.x=0, by.y=0) 32 | #get KM plots 33 | CGGA.dat$OS<-as.numeric(CGGA.dat$survival) 34 | CGGA.dat$status<-as.numeric(CGGA.dat$status) 35 | surv_object <- Surv(time = CGGA.dat$OS, event = CGGA.dat$status) 36 | hist(CGGA.dat$S100A4) 37 | median(CGGA.dat$S100A4) 38 | CGGA.dat <-CGGA.dat %>% mutate(S100A4.Levels = ifelse(S100A4 >=14.91, "High", "Low")) 39 | table(CGGA.dat$S100A4.Levels) 40 | CGGA.dat$S100A4.Levels <-factor(CGGA.dat$S100A4.Levels) 41 | fit1 <- survfit(surv_object ~ S100A4.Levels, data = CGGA.dat) 42 | fit1.mv <- coxph(surv_object ~S100A4.Levels + Gender + Histology + Recurrence + MGMT_status +IDH_mutation_status , data = CGGA.dat) 43 | mv.res <- summary(fit1.mv)$coefficients 44 | mv.fname <- paste("S100a4 All glioma_multivariat_results.csv",sep="") 45 | write.csv(mv.res,mv.fname) 46 | 47 | summary(fit1) 48 | P1=ggsurvplot(fit1, data = CGGA.dat, pval = TRUE,pval.coord = c(50, 1),pval.size=6,legend="top",palette = c("Red","Blue"),risk.table = F,font.main = c(12, "bold"),legend.title = "Expression", 49 | title="S100A4-AllGliomas-CGGA", legend.labs = c("High", "Low"),font.x = c(14, "bold"),font.y = c(14, "bold"),font.tickslab = c(12, "plain")) 50 | #get GBM patients survival only 51 | Counts=as.data.frame(CGGA.EXP) 52 | data <- Counts["S100A4",] 53 | t.data=t(data) 54 | CGGA.dat=merge(t.data,CGGA.GBM,by.x=0, by.y=0) 55 | CGGA.dat$OS<-as.numeric(CGGA.dat$survival) 56 | CGGA.dat$status<-as.numeric(CGGA.dat$status) 57 | surv_object <- Surv(time = CGGA.dat$OS, event = CGGA.dat$status) 58 | hist(CGGA.dat$S100A4) 59 | median(CGGA.dat$S100A4) 60 | CGGA.dat <-CGGA.dat %>% mutate(S100A4.Levels = ifelse(S100A4 >=38.39, "High", "Low")) 61 | table(CGGA.dat$S100A4.Levels) 62 | CGGA.dat$S100A4.Levels <-factor(CGGA.dat$S100A4.Levels) 63 | fit1 <- survfit(surv_object ~ S100A4.Levels, data = CGGA.dat) 64 | fit1.mv <- coxph(surv_object ~S100A4.Levels + Gender + Recurrence + MGMT_status +IDH_mutation_status , data = CGGA.dat) 65 | mv.res <- summary(fit1.mv)$coefficients 66 | mv.fname <- paste("GBM only_multivariat_results.csv",sep="") 67 | write.csv(mv.res,mv.fname) 68 | summary(fit1) 69 | P2=ggsurvplot(fit1, data = CGGA.dat, pval = TRUE,pval.coord = c(50, 1),pval.size=6,legend="top",palette = c("Red","Blue"),risk.table = F,font.main = c(12, "bold"),legend.title = "Expression", 70 | title="S100A4-GBM only-CGGA", legend.labs = c("High", "Low"),font.x = c(14, "bold"),font.y = c(14, "bold"),font.tickslab = c(12, "plain")) 71 | P=list(P1,P2) 72 | pdf(file ="Figure3e.pdf", height = 3.5, width =14,onefile = T) 73 | arrange_ggsurvplots(P,print = TRUE, ncol = 4,nrow = 1,surv.plot.height = NULL,risk.table.height = NULL,ncensor.plot.height = NULL) 74 | dev.off() 75 | -------------------------------------------------------------------------------- /R/suppfig4a heatmap.R: -------------------------------------------------------------------------------- 1 | #Figure 4a 2 | Idents(SeuratObj)=CellInfo$Assignment2 3 | markers = FindAllMarkers(SeuratObj,logfc.threshold = 0.25,test.use = "wilcox",only.pos = T ) 4 | write.csv(markers,paste0(OutputDirectory,ObjName,Subset," Assignment2 markers ","res",resolution,".csv")) 5 | markers=markers[order(markers$cluster),] 6 | top20.markers <- markers %>% group_by(cluster) %>% top_n(n = 20, wt = avg_log2FC) 7 | top20.markers$cluster=as.character(top20.markers$cluster) 8 | top20.markers=top20.markers[order(top20.markers$cluster),] 9 | set.seed(12) 10 | Idents(SeuratObj)=CellInfo$Sample 11 | subset=subset(SeuratObj, downsample=1000) 12 | 13 | SeuratObj.sce=as.SingleCellExperiment(subset) 14 | plot.data<-as.data.frame(assay(SeuratObj.sce, "logcounts")) 15 | plot.data<-plot.data[top20.markers$gene,] 16 | plot.data <- plot.data - rowMeans(plot.data) 17 | plot.data=na.omit(plot.data) 18 | CellInfoS=subset@meta.data 19 | 20 | column_annot <-CellInfoS[,c("Cluster","Patient","Type"),drop=F] 21 | column_annot$Patient = as.factor(as.character(column_annot$Patient)) 22 | column_annot=with(column_annot, column_annot[order(Patient), , drop=F]) 23 | column_annot=with(column_annot, column_annot[order(Cluster), , drop=F]) 24 | plot.data<-plot.data[,row.names(column_annot)] 25 | 26 | column.colors=list() 27 | column.colors[["Patient"]]<-SampleColors 28 | column.colors[["Cluster"]]<-ClusterColors 29 | column.colors[["Type"]]<-TypeColors 30 | 31 | Patient=as.matrix(column_annot[,c("Patient"),drop=F]) 32 | Cluster=as.matrix(column_annot[,c("Cluster"),drop=F]) 33 | Type=as.matrix(column_annot[,c("Type"),drop=F]) 34 | 35 | colanno <- columnAnnotation(df=column_annot, 36 | show_annotation_name =T,show_legend = F,col=column.colors) 37 | top2 <- markers %>% group_by(cluster) %>% top_n(n = 5, wt = avg_log2FC) 38 | genes= top2$gene 39 | 40 | rows=rowAnnotation(sel = anno_mark(at = match(genes,row.names(plot.data)), labels = genes,labels_gp =gpar(col = "black", fontsize = 9))) 41 | 42 | col<- circlize::colorRamp2(breaks = c(-2, 0, 2), colors = c("#007dd1", "white", "#ab3000")) 43 | HM=Heatmap(name="logcounts",as.matrix(plot.data),cluster_rows =F,cluster_columns = F,top_annotation = colanno, right_annotation = rows,row_names_gp = gpar(fontsize=5), 44 | col = col,show_column_names= F,show_row_names=F,border = F,show_heatmap_legend = F,use_raster = T) 45 | lgd=Legend(title = "logcounts", at= c(-2,0, 2),col_fun = col) 46 | lgd1=Legend(labels = levels(as.factor(column_annot$Cluster)),title="Cluster",legend_gp = gpar(fill=ClusterColors,fontsize=5)) 47 | lgd2=Legend(labels = levels(as.factor(column_annot$Patient)),title="Patient",legend_gp = gpar(fill=SampleColors,fontsize=5)) 48 | lgd4=Legend(labels = levels(as.factor(column_annot$Type)),title="Type",legend_gp = gpar(fill=TypeColors,fontsize=5)) 49 | 50 | pdf(paste0(OutputDirectory,"heatmap",ObjName,Subset,"res",resolution,"top20 genes per cluster Fig4a.pdf"),width=6,height=5) 51 | draw(HM,heatmap_legend_list = list( lgd,lgd1,lgd4, lgd2), heatmap_legend_side = "right") 52 | dev.off() 53 | 54 | Final=list() 55 | 56 | for(i in 1:length(levels(as.factor(SeuratObj@meta.data$Patient)))){ 57 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==levels(as.factor(SeuratObj@meta.data$Patient))[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 58 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + #facet_grid(~ Fragment) + # 59 | labs(title=levels(as.factor(SeuratObj@meta.data$Patient))[i])+ 60 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 61 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 62 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 63 | Final[[i]]=x 64 | } 65 | mylegend<-get_legend(Final[[2]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title=Y,title.position = "top",title.theme = element_text(size=10)))) 66 | 67 | 68 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts patient and Assignment.pdf")), height = 6, width =10,onefile = T) 69 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[2]]+NoLegend(),Final[[3]]+NoLegend(),NA,NA,Final[[4]]+NoLegend(), 70 | Final[[5]]+NoLegend(),Final[[6]]+NoLegend(),Final[[7]]+NoLegend(),Final[[8]]+NoLegend(), 71 | Final[[9]]+NoLegend(),Final[[10]]+NoLegend(),Final[[11]]+NoLegend(),Final[[12]]+NoLegend(),Final[[13]]+NoLegend(), 72 | Final[[14]]+NoLegend(),Final[[15]]+NoLegend(),Final[[16]]+NoLegend(),Final[[17]]+NoLegend(),Final[[18]]+NoLegend(), 73 | ncol = 5,nrow = 4, 74 | legend.grob = mylegend,legend = "right",widths = c(1,1,1,1,1)) 75 | dev.off() 76 | -------------------------------------------------------------------------------- /R/suppfig7d dotplot.R: -------------------------------------------------------------------------------- 1 | rm(list=ls()) # clear workspace 2 | 3 | ### cellphonedb plot dot_plot --rows in/rows.txt --columns in/columns.txt 4 | 5 | library(ggplot2) 6 | #setwd() 7 | 8 | 9 | pkWD <- "/Users/kumarpa/Desktop/Work/Jax/Kyuson/Upload_To_Github" 10 | PatientDir <- paste0("/Users/kumarpa/Desktop/Work/Jax/Kyuson/CellCellComm/cpdb/Output_from_cpdb_ALL_Patients"); PatientDir 11 | 12 | setwd(pkWD) 13 | plotWD.main <- paste(getwd(),paste0("DotPlot_CellphoneDB"),sep="/"); print(plotWD.main) 14 | dir.create(file.path(getwd(),paste0("DotPlot_CellphoneDB")), showWarnings = FALSE) 15 | 16 | 17 | CTlist=c("Glioma|Myeloid", "Glioma|Tcells", "Myeloid|Glioma", "Myeloid|Tcells", "Tcells|Glioma", "Tcells|Myeloid") 18 | Log2MeanCutoff=2 19 | Patient <- c("Output_cpdb_") 20 | CellCount="Analysis_cpdb_ALLcells_per_CT" 21 | pvalcutoff=0.01 22 | 23 | for(CTuse in CTlist){ 24 | #CTuse="Glioma|Myeloid" 25 | 26 | sampleNames=dir(path = PatientDir, pattern = paste0(Patient,".*$")); sampleNames 27 | print(paste0("Processig for pval-cutoff:", pvalcutoff)) 28 | 29 | pval.list=list(); mean.list=list(); mean.list.ORI=list(); pval.list.ORI=list(); 30 | for(sample in sampleNames){ 31 | #sample="Output_cpdb_ndGBM-09" 32 | 33 | print(paste0("Processing sample: ",sample)) 34 | pkWD <- paste0(PatientDir,"/",sample,"/",CellCount); pkWD 35 | 36 | setwd(pkWD) 37 | means_path = './means.txt' 38 | pvalues_path = './pvalues.txt' 39 | significant_means_path='./significant_means.txt' 40 | means_separator = '\t' 41 | pvalues_separator = '\t' 42 | 43 | ## Process the files 44 | all_pval.Full = read.table(pvalues_path, header=T, stringsAsFactors = F, sep=means_separator, comment.char = '', check.names=F); print(dim(all_pval.Full)) 45 | all_pval.Full <- all_pval.Full[!duplicated(all_pval.Full$interacting_pair),] 46 | rownames(all_pval.Full) <- all_pval.Full$interacting_pair; head(all_pval.Full) 47 | all_pval <- all_pval.Full 48 | intr_pairs = all_pval$interacting_pair; length(intr_pairs) 49 | all_pval = all_pval[,-c(1:11)] 50 | selected_columns = colnames(all_pval) 51 | selected_columns = selected_columns[selected_columns %in% paste0(CTuse)]; selected_columns 52 | all_pval <- all_pval[,selected_columns, drop=FALSE]; head(all_pval) 53 | sig_pval_intr_pairs <- rownames(all_pval[rowSums(all_pval<=pvalcutoff)>0,,drop=FALSE]); length(sig_pval_intr_pairs) 54 | 55 | all_means = read.table(means_path, header=T, stringsAsFactors = F, sep=pvalues_separator, comment.char = '', check.names=F); print(dim(all_means)) 56 | all_means <- all_means[!duplicated(all_means$interacting_pair),] 57 | rownames(all_means) <- all_means$interacting_pair; head(all_means) 58 | all_means = all_means[,-c(1:11)] 59 | all_means <- all_means[,selected_columns,drop=FALSE]; head(all_means) 60 | all_means.Full <- all_means 61 | 62 | 63 | if(length(sig_pval_intr_pairs) > 0){ 64 | 65 | sig_means = read.table(means_path, header=T, stringsAsFactors = F, sep=pvalues_separator, comment.char = '', check.names=F); print(dim(sig_means)) 66 | sig_means <- sig_means[!duplicated(sig_means$interacting_pair),] 67 | rownames(sig_means) <- sig_means$interacting_pair; head(sig_means) 68 | sig_means = sig_means[,-c(1:11)]; head(sig_means) 69 | sig_means<- sig_means[,selected_columns,drop=FALSE]; head(sig_means) 70 | sig_means$Log2Mean <- log2(sig_means[,CTuse]); 71 | sig_means <- sig_means[sig_means$Log2Mean > Log2MeanCutoff,]; head(sig_means); dim(sig_means) 72 | sig_means$Log2Mean <- NULL 73 | sig_means$sum <- rowSums(sig_means); head(sig_means) 74 | 75 | discarded_rows <- rownames(sig_means[sig_means$sum<=0,]); head(discarded_rows); length(discarded_rows) 76 | Temp_selected_rows <- rownames(sig_means[sig_means$sum>0,]); head(Temp_selected_rows); length(Temp_selected_rows) 77 | selected_rows_pval <- intersect(Temp_selected_rows, sig_pval_intr_pairs); length(selected_rows_pval) 78 | 79 | colnames(all_pval) <- paste0(colnames(all_pval),"|", gsub("Output_cpdb_","", sample)) 80 | all_pval <- all_pval[selected_rows_pval,,drop=FALSE] 81 | all_pval$LR <- rownames(all_pval) 82 | pval.list[[sample]] <- all_pval 83 | 84 | colnames(all_means) <- paste0(colnames(all_means),"|", gsub("Output_cpdb_","", sample)) 85 | all_means$LR <- rownames(all_means) 86 | mean.list[[sample]] <- all_means 87 | 88 | 89 | 90 | 91 | 92 | } else { 93 | print(paste0("No interactions found in sample: ",sample, " for interaction: ",CTuse)) 94 | print(paste0("*************************")) 95 | Sys.sleep(0.5) 96 | print("Using empty data.frame") 97 | all_pval <- data.frame(A=as.numeric(),B=as.character()); colnames(all_pval) <- c(paste0(CTuse,"|",gsub("Output_cpdb_","", sample)), "LR") 98 | pval.list[[sample]] <- all_pval 99 | all_means <- all_pval 100 | mean.list[[sample]] <- all_means 101 | 102 | 103 | } 104 | 105 | 106 | if(length(selected_columns) > 0){ 107 | print("Filling Full data.frames") 108 | all_pval.Full <- all_pval.Full[,selected_columns,drop=FALSE] 109 | colnames(all_pval.Full) <- paste0(colnames(all_pval.Full),"|", gsub("Output_cpdb_","", sample)) 110 | all_pval.Full$LR <- rownames(all_pval.Full); head(all_pval.Full) 111 | pval.list.ORI[[sample]] <- all_pval.Full 112 | 113 | all_means.Full <- all_means.Full[,selected_columns,drop=FALSE] 114 | colnames(all_means.Full) <- paste0(colnames(all_means.Full),"|", gsub("Output_cpdb_","", sample)) 115 | all_means.Full$LR <- rownames(all_means.Full) 116 | mean.list.ORI[[sample]] <- all_means.Full; head(all_means.Full) 117 | print("Done Filling Full data.frames") 118 | 119 | } else { 120 | 121 | print(paste0("No Columns itself found in sample: ",sample, " for interaction: ",CTuse)) 122 | Sys.sleep(0.3) 123 | print("Using empty data.frame for Full") 124 | all_pval.Full <- data.frame(A=as.numeric(),B=as.character()); colnames(all_pval.Full) <- c(paste0(CTuse,"|",gsub("Output_cpdb_","", sample)), "LR") 125 | pval.list.ORI[[sample]] <- all_pval.Full 126 | all_means.Full <- all_pval.Full 127 | mean.list.ORI[[sample]] <- all_means.Full 128 | } 129 | 130 | } 131 | 132 | ## Merge data frames 133 | print(paste0("Merging ",length(sampleNames), " data frames")) 134 | CombinedLRs <- Reduce(function(...) merge(..., by = "LR", all=TRUE), pval.list) 135 | all_pval_patient <- Reduce(function(...) merge(..., by = "LR", all=TRUE), pval.list.ORI) 136 | rownames(all_pval_patient) <- all_pval_patient$LR 137 | all_pval_patient <- all_pval_patient[CombinedLRs$LR,] 138 | all_mean_patient <- Reduce(function(...) merge(..., by = "LR", all=TRUE), mean.list.ORI) 139 | rownames(all_mean_patient) <- all_mean_patient$LR 140 | all_mean_patient <- all_mean_patient[CombinedLRs$LR,] 141 | 142 | rownames(all_pval_patient) <- all_pval_patient$LR; all_pval_patient$LR <- NULL; head(all_pval_patient,1) 143 | selected_rows_pval <- rownames(all_pval_patient) 144 | head(all_pval_patient,1); dim(all_pval_patient) 145 | 146 | rownames(all_mean_patient) <- all_mean_patient$LR; all_mean_patient$LR <- NULL; head(all_mean_patient,1) 147 | selected_rows_mean <- rownames(all_mean_patient) 148 | head(all_mean_patient,1); dim(all_mean_patient) 149 | 150 | ColsOrder <- sort(colnames(all_pval_patient)) 151 | all_pval_patient <- all_pval_patient[,ColsOrder] 152 | all_mean_patient <- all_mean_patient[,ColsOrder] 153 | 154 | 155 | PlotPval="YES" 156 | if(PlotPval=="YES"){ 157 | 158 | setwd(plotWD.main) 159 | pdf(file=paste0("Plotting_",Patient,"ALL_",CellCount,"_pval_based_",pvalcutoff,"_",CTuse,"_Log2mean_above_",Log2MeanCutoff,"_Total_",length(selected_rows_pval),"_Pairs.pdf"),height = 13,width = 17) 160 | 161 | 162 | set <- split(selected_rows_pval, ceiling(seq_along(selected_rows_pval)/60)); length(set) 163 | selected_columns=colnames(all_pval_patient) 164 | intr_pairs=rownames(all_pval_patient) 165 | 166 | for(i in 1:length(set)){ 167 | #i=1 168 | print(paste0("Set no. ",i," of total ",length(set)," sets")) 169 | 170 | Plot_rows <- c(set[[i]]); head(Plot_rows); length(Plot_rows) 171 | 172 | 173 | all_mean_patient.temp=all_mean_patient[intr_pairs,] 174 | all(rownames(all_pval_patient)==rownames(all_mean_patient.temp)) 175 | 176 | sel_pval = all_pval_patient[match(Plot_rows, intr_pairs), selected_columns] 177 | sel_means = all_mean_patient.temp[match(Plot_rows, intr_pairs), selected_columns] 178 | 179 | df_names = expand.grid(Plot_rows, selected_columns) 180 | pval = unlist(sel_pval) 181 | pval[pval==0] = 0.0009 182 | plot.data = cbind(df_names,pval) 183 | pr = unlist(as.data.frame(sel_means)) 184 | pr[pr==0] = 1 185 | plot.data = cbind(plot.data,log2(pr)) 186 | colnames(plot.data) = c('pair', 'clusters', 'pvalue', 'mean') 187 | print(dim(plot.data)) 188 | head(plot.data) 189 | 190 | my_palette <- colorRampPalette(c("black", "blue", "yellow", "red"), alpha=TRUE)(n=399) 191 | 192 | p1 <- ggplot(plot.data,aes(x=clusters,y=pair)) + 193 | geom_point(aes(size=-log10(pvalue),color=mean)) + 194 | scale_color_gradientn('Log2 mean (Molecule 1, Molecule 2)', colors=my_palette) + 195 | scale_x_discrete(position = "top") + 196 | theme_bw() + 197 | theme(panel.grid.minor = element_blank(), 198 | panel.grid.major = element_blank(), 199 | axis.text=element_text(size=15, colour = "black"), 200 | axis.text.x = element_text(size=15, angle = 90, hjust = 1), 201 | axis.text.y = element_text(size=15, colour = "black"), 202 | axis.title=element_blank(), 203 | panel.border = element_rect(size = 0.7, linetype = "solid", colour = "black")) 204 | 205 | print(p1) 206 | 207 | } 208 | 209 | dev.off() 210 | 211 | selected_rows_pval 212 | head(all_pval_patient) 213 | write.table(all_pval_patient[selected_rows_pval,selected_columns],file=paste0("Details_",Patient,"ALL_",CellCount,"_pval_based_",pvalcutoff,"_",CTuse,"_Log2mean_above_",Log2MeanCutoff,"_Total_",length(selected_rows_pval),"_Pairs.txt"),row.names=T,quote=F,sep="\t") 214 | 215 | 216 | } 217 | 218 | 219 | ## CTlist 220 | } 221 | -------------------------------------------------------------------------------- /R/suppfigs.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | SeuratObj=readRDS(paste0(RobjDirectory,"Allhuman-11-3-21 patients renamed.rds")) 4 | 5 | ClusterColors=c(C1 = "#003F5C", C2 = "#196687", C3 = "#2879CD", C4 = "#8965B3", 6 | C5 = "#AB599D", C6 = "#D75D96", C7 = "#E56674", C8 = "#EE5761", 7 | C9 = "#EB7B27", C10 = "#FFA206", C11 = "#A3BD5C", C12 = "#8DB032" 8 | ) 9 | 10 | pdf(paste0(OutputDirectory,ObjName,Subset,"Cluster UMAP Iteration by sample suppfig2a.pdf"),width = 20,height =10,family = "ArialMT") 11 | DimPlot(SeuratObj, reduction = "umap",group.by = "Cluster",cols =ClusterColors,split.by = "Patient",ncol = 6, pt.size = 0.8)+ggmin::theme_min()+ 12 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("") + ylab("")+labs(title="Clusters per patient")+ 13 | FontSize(x.title = 16, y.title = 16,main = 18)+ theme(legend.position = "right",text =element_text(size=15) )+ 14 | guides(colour = guide_legend(title="Clusters", override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15))) 15 | dev.off() 16 | 17 | 18 | 19 | #suppfig2d 20 | AssignmentColors=c(TCells = "#8DB032", BCells = "#F9BF31", Other = "#E47B1E", 21 | Myeloid = "#CD4D53", Endo = "#DC73B0", Oligo = "#966CBF", Pericytes = "#2960A1", 22 | Glioma = "#003F5C") 23 | Final=list() 24 | for(i in 1:length(levels(as.factor(SeuratObj@meta.data$Patient)))){ 25 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==levels(as.factor(SeuratObj@meta.data$Patient))[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 26 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + #facet_grid(~ Fragment) + # 27 | labs(title=levels(as.factor(SeuratObj@meta.data$Patient))[i])+ 28 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 29 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 30 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 31 | Final[[i]]=x 32 | } 33 | mylegend<-get_legend(Final[[1]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Assignment",title.position = "top",title.theme = element_text(size=10)))) 34 | 35 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts Patient and Assignment suppfigure 2d.pdf")), height = 6, width =10,onefile = T) 36 | 37 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[2]]+NoLegend(),NA,NA,NA,NA,Final[[3]]+NoLegend(),Final[[4]]+NoLegend(), 38 | Final[[5]]+NoLegend(),Final[[6]]+NoLegend(),Final[[7]]+NoLegend(),Final[[8]]+NoLegend(), 39 | Final[[9]]+NoLegend(),Final[[10]]+NoLegend(),Final[[11]]+NoLegend(),Final[[12]]+NoLegend(),Final[[13]]+NoLegend(),NA, 40 | Final[[14]]+NoLegend(),Final[[15]]+NoLegend(),Final[[16]]+NoLegend(),Final[[17]]+NoLegend(),Final[[18]]+NoLegend(),NA, 41 | ncol = 6,nrow = 4, 42 | legend.grob = mylegend,legend = "right",widths = c(1,1,1,1,1,1)) 43 | dev.off() 44 | 45 | ### 46 | SeuratObj=readRDS(paste0(RobjDirectory,"GliomaClusters-11-3-21 patients renamed.rds")) 47 | 48 | SampleColors=c(`LGG-03` = "#003F5C", `LGG-04` = "#008796", `ndGBM-01` = "#2F4B7C", 49 | `ndGBM-02` = "#227ED4", `ndGBM-03` = "#665191", `ndGBM-04` = "#9E71C7", 50 | `ndGBM-05` = "#A05195", `ndGBM-06` = "#E082C1", `ndGBM-07` = "#D45087", 51 | `ndGBM-08` = "#F57689", `ndGBM-09` = "#BD3E3E", `ndGBM-10` = "#F95D6A", 52 | `ndGBM-11` = "#E07B18", `rGBM-01` = "#FF7C43", `rGBM-02` = "#FFA600", 53 | `rGBM-03` = "#F5D256", `rGBM-04` = "#42A665", `rGBM-05` = "#8DB032" 54 | ) 55 | 56 | CellInfo=SeuratObj@meta.data 57 | set.seed(12) 58 | Idents(SeuratObj)=CellInfo$Patient 59 | sampled.cells <- sample(x = SeuratObj@active.ident, size = 10000, replace = F) 60 | subset=subset(SeuratObj,cells=names(sampled.cells)) 61 | 62 | Counts <- as.matrix(GetAssayData(object = subset, assay = "RNA", slot = "counts")) 63 | 64 | #Read the Genelist file #from Neftel et al 65 | Subtype.Markers=read.csv("~/Box/Yun lab manuscripts/GBM Single Cells/Nature Communications revision/RCode/SuvaGBMsubtype genelist.csv") 66 | 67 | #get scores 68 | subtypelist=Subtype.Markers%>% split(x = .$human_gene_symbol, f = .$gs_name) 69 | sc=score(Counts, 70 | groups=subtypelist, 71 | binmat = NULL, 72 | bins = NULL, 73 | controls = NULL, 74 | bin.control = F, 75 | center = T, 76 | nbin = 30, 77 | n = 100, 78 | replace = T) 79 | #write.csv(sc,paste0(OutputDirectory,"HumanGliomacellstates20000cells.csv")) 80 | sc=read.csv("~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/Output/Glioma/HumanGliomacellstates20000cells.csv",row.names = 1) 81 | #get coordinates 82 | h=hierarchy (sc, quadrants = c("AC","OPC","Mesenchymal","Proneural"), log.scale = T) 83 | # make plots 84 | ##Perpatient 85 | CellInfo=SeuratObj@meta.data 86 | for( i in 1: length(names(SampleColors))){ 87 | name=names(SampleColors)[i] 88 | color=SampleColors[i] 89 | CellInfo$Patientcolor [CellInfo$Patient== name] <- color 90 | } 91 | Patientcolor <- CellInfo$Patientcolor 92 | names(Patientcolor ) <- row.names(CellInfo) 93 | SeuratObj <- AddMetaData(object = SeuratObj,metadata = Patientcolor ,col.name = 'Patientcolor') 94 | 95 | # make plots 96 | xmax=max(h$X)+(max(h$X)*0.2) 97 | xmin=min(h$X)+(min(h$X)*0.2) 98 | ymax=max(h$Y)+(max(h$Y)*0.6) 99 | ymin=min(h$Y)+(min(h$Y)*0.6) 100 | 101 | #Make the big plot--> all Patients 102 | groups=SeuratObj@meta.data[,c("Patientcolor","Patient")] 103 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 104 | matrix$Patientcolor[is.na(matrix$Patientcolor)] <- "gray" 105 | matrix$Patient[is.na(matrix$Patient)] <- "Other" 106 | row.names(matrix)=matrix$Row.names 107 | x=matrix$Patientcolor 108 | y=matrix$Patient 109 | col=x[!duplicated(x)] 110 | names(col)=y[!duplicated(y)] 111 | matrix=matrix[,-1] 112 | title="All Patients" 113 | p0 <- ggplot(matrix, aes(x = X, 114 | y =Y,color=factor(Patient)))+geom_point()+geom_point(data = subset(matrix, Patient !="Other"))+ 115 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Patient")+theme(legend.position = "none")+ 116 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 117 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 118 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 119 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 120 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 121 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 122 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 123 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 124 | geom_hline(yintercept=0, color = "black", size=0.5)+ 125 | geom_vline(xintercept=0, color = "black", size=0.5)+ 126 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymax-2, ymax = ymax, fill= "black") + 127 | annotate("text",x = xmin+2.5, y = ymax-1,label = "Mes-Like",color="white",fontface="bold",size=9)+ 128 | annotate("rect", xmin = xmax-5, xmax = xmax, ymin = ymax-2, ymax = ymax, fill= "black") + 129 | annotate("text",x = xmax-2.5, y = ymax-1,label = "NPC-Like",color="white",fontface="bold",size=9)+ 130 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymin+2, ymax = ymin, fill= "black") + 131 | annotate("text",x = xmin+2.5, y = ymin+1,label = "AC-Like",color="white",fontface="bold",size=9)+ 132 | annotate("rect", xmin = xmax-5, xmax =xmax, ymin = ymin+2, ymax = ymin, fill= "black") + 133 | annotate("text",x = xmax-2.5, y = ymin+1,label = "OPC-Like",color="white",fontface="bold",size=9) 134 | P0=ggplotGrob(p0) 135 | ##make the small plots--> one per Patient 136 | Final <- list() 137 | Final[[1]]=ggplotGrob(p0) 138 | for (i in 1:length(names(SampleColors))) { 139 | PatientMD=SeuratObj@meta.data[SeuratObj@meta.data$Patient==paste0(names(SampleColors)[i]),] 140 | groups=PatientMD[,c("Patientcolor","Patient")] 141 | title=paste0(names(SampleColors)[i]) 142 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 143 | matrix$Patientcolor[is.na(matrix$Patientcolor)] <- "gray" 144 | matrix$Patient=as.character(matrix$Patient) 145 | matrix$Patient[is.na(matrix$Patient)] <- "Other" 146 | row.names(matrix)=matrix$Row.names 147 | x=matrix$Patientcolor 148 | y=matrix$Patient 149 | col=x[!duplicated(x)] 150 | names(col)=y[!duplicated(y)] 151 | matrix=matrix[,-1] 152 | P=ggplot(matrix, aes(x = X, 153 | y =Y,color=factor(Patient)))+geom_point()+geom_point(data = subset(matrix, Patient !="Other"))+ 154 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 155 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 156 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 157 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 158 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 159 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 160 | geom_hline(yintercept=0, color = "black", size=0.5)+ 161 | geom_vline(xintercept=0, color = "black", size=0.5)+ 162 | annotate("rect", xmin = xmin, xmax = xmin+2, ymin = ymax-1, ymax = ymax, fill= "black") + 163 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymax-2, ymax = ymax, fill= "black") + 164 | annotate("text",x = xmin+2.5, y = ymax-1,label = "Mes-Like",color="white",fontface="bold",size=4)+ 165 | annotate("rect", xmin = xmax-5, xmax = xmax, ymin = ymax-2, ymax = ymax, fill= "black") + 166 | annotate("text",x = xmax-2.5, y = ymax-1,label = "NPC-Like",color="white",fontface="bold",size=4)+ 167 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymin+2, ymax = ymin, fill= "black") + 168 | annotate("text",x = xmin+2.5, y = ymin+1,label = "AC-Like",color="white",fontface="bold",size=4)+ 169 | annotate("rect", xmin = xmax-5, xmax =xmax, ymin = ymin+2, ymax = ymin, fill= "black") + 170 | annotate("text",x = xmax-2.5, y = ymin+1,label = "OPC-Like",color="white",fontface="bold",size=4) 171 | Final[[i+1]] = ggplotGrob(P) 172 | } 173 | Final[[1]]=ggplotGrob(p0) 174 | numofplots= length(Final) 175 | pdf(file =paste0(OutputDirectory,ObjName,Subset," subtype butterfly by Patient ordered by type3-suppfig5e.pdf"), height = 10, width =22,onefile = T) 176 | grid.arrange(grobs=Final, widths = c(3,1,1,1,1,1,1),layout_matrix = rbind(c(1, 2,NA, NA,NA,NA,3),c(1,4,5,6,7,8,9), 177 | c(1,10,11,12,13,14,NA), 178 | c(1,15,16,17,18,19,NA))) 179 | dev.off() 180 | 181 | #Suppfig4a heatmap 182 | SeuratObj=readRDS(paste0(RobjDirectory,"MyeloidClusters-8-10-21-patients renamed.rds")) 183 | ClusterColors=c(MC01 = "#003F5C", MC02 = "#E25159", MC03 = "#73589E", MC04 = "#8DB032", 184 | MC05 = "#F6CC4B", MC06 = "#B863A5", MC07 = "#F77B38", MC08 = "#2D5186", 185 | MC09 = "#E46388") 186 | AssignmentColors=c(`a-microglia` = "#003F5C", `s-mac 1` = "#2D5187", `AP-microglia` = "#74599E", 187 | DCs = "#B863A5", `h-microglia` = "#E46388", `i-microglia` = "#E25159", 188 | `MDSC` = "#F77B38", Proliferating = "#F6CC4B", `s-mac 2` = "#8DB032" 189 | ) 190 | 191 | #Figure 4a 192 | Idents(SeuratObj)=CellInfo$Cluster 193 | markers = FindAllMarkers(SeuratObj,logfc.threshold = 0.25,test.use = "wilcox",only.pos = T ) 194 | write.csv(markers,paste0(OutputDirectory,ObjName,Subset," cluster markers ","res",resolution,".csv")) 195 | 196 | #markers =read.csv("~/Box/Yun lab projects/scRNAseq Project/Human Glioma/ALLSAMPLESJuly2021/Output/Myeloid-no Tcells or Astrocytes/HumanGlioma-AllSamples-Myeloid-no Tcells or Astrocytes cluster markers res0.25.csv") 197 | 198 | markers=markers[order(markers$cluster),] 199 | top20.markers <- markers %>% group_by(cluster) %>% top_n(n = 20, wt = avg_log2FC) 200 | top20.markers$cluster=as.character(top20.markers$cluster) 201 | top20.markers=top20.markers[order(top20.markers$cluster),] 202 | set.seed(12) 203 | Idents(SeuratObj)=CellInfo$Sample 204 | subset=subset(SeuratObj, downsample=1000) 205 | 206 | SeuratObj.sce=as.SingleCellExperiment(subset) 207 | plot.data<-as.data.frame(assay(SeuratObj.sce, "logcounts")) 208 | plot.data<-plot.data[top20.markers$gene,] 209 | plot.data <- plot.data - rowMeans(plot.data) 210 | plot.data=na.omit(plot.data) 211 | CellInfoS=subset@meta.data 212 | 213 | column_annot <-CellInfoS[,c("Cluster","Patient","Type"),drop=F] 214 | column_annot$Patient = as.factor(as.character(column_annot$Patient)) 215 | column_annot=with(column_annot, column_annot[order(Patient), , drop=F]) 216 | column_annot=with(column_annot, column_annot[order(Cluster), , drop=F]) 217 | plot.data<-plot.data[,row.names(column_annot)] 218 | 219 | column.colors=list() 220 | column.colors[["Patient"]]<-SampleColors 221 | column.colors[["Cluster"]]<-ClusterColors 222 | column.colors[["Type"]]<-TypeColors 223 | 224 | Patient=as.matrix(column_annot[,c("Patient"),drop=F]) 225 | Cluster=as.matrix(column_annot[,c("Cluster"),drop=F]) 226 | Type=as.matrix(column_annot[,c("Type"),drop=F]) 227 | 228 | colanno <- columnAnnotation(df=column_annot, 229 | show_annotation_name =T,show_legend = F,col=column.colors) 230 | top2 <- markers %>% group_by(cluster) %>% top_n(n = 3, wt = avg_log2FC) 231 | genes= top2$gene 232 | 233 | rows=rowAnnotation(sel = anno_mark(at = match(genes,row.names(plot.data)), labels = genes,labels_gp =gpar(col = "black", fontsize = 9))) 234 | 235 | col<- circlize::colorRamp2(breaks = c(-2, 0, 2), colors = c("#007dd1", "white", "#ab3000")) 236 | HM=Heatmap(name="logcounts",as.matrix(plot.data),cluster_rows =F,cluster_columns = F,top_annotation = colanno, right_annotation = rows,row_names_gp = gpar(fontsize=5), 237 | col = col,show_column_names= F,show_row_names=F,border = F,show_heatmap_legend = F,use_raster = T) 238 | lgd=Legend(title = "logcounts", at= c(-2,0, 2),col_fun = col) 239 | lgd1=Legend(labels = levels(as.factor(column_annot$Cluster)),title="Cluster",legend_gp = gpar(fill=ClusterColors,fontsize=5)) 240 | lgd2=Legend(labels = levels(as.factor(column_annot$Patient)),title="Patient",legend_gp = gpar(fill=SampleColors,fontsize=5)) 241 | lgd4=Legend(labels = levels(as.factor(column_annot$Type)),title="Type",legend_gp = gpar(fill=TypeColors,fontsize=5)) 242 | 243 | pdf(paste0(OutputDirectory,"heatmap",ObjName,Subset,"res",resolution,"top20 genes per cluster Fig4a.pdf"),width=8,height=5) 244 | draw(HM,heatmap_legend_list = list( lgd,lgd1,lgd4, lgd2), heatmap_legend_side = "right") 245 | dev.off() 246 | 247 | Final=list() 248 | 249 | for(i in 1:length(levels(as.factor(SeuratObj@meta.data$Patient)))){ 250 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==levels(as.factor(SeuratObj@meta.data$Patient))[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 251 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + #facet_grid(~ Fragment) + # 252 | labs(title=levels(as.factor(SeuratObj@meta.data$Patient))[i])+ 253 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 254 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 255 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 256 | Final[[i]]=x 257 | } 258 | mylegend<-get_legend(Final[[2]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title=Y,title.position = "top",title.theme = element_text(size=10)))) 259 | 260 | 261 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts patient and Assignment.pdf")), height = 6, width =10,onefile = T) 262 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[2]]+NoLegend(),Final[[3]]+NoLegend(),NA,NA,Final[[4]]+NoLegend(), 263 | Final[[5]]+NoLegend(),Final[[6]]+NoLegend(),Final[[7]]+NoLegend(),Final[[8]]+NoLegend(), 264 | Final[[9]]+NoLegend(),Final[[10]]+NoLegend(),Final[[11]]+NoLegend(),Final[[12]]+NoLegend(),Final[[13]]+NoLegend(), 265 | Final[[14]]+NoLegend(),Final[[15]]+NoLegend(),Final[[16]]+NoLegend(),Final[[17]]+NoLegend(),Final[[18]]+NoLegend(), 266 | ncol = 5,nrow = 4, 267 | legend.grob = mylegend,legend = "right",widths = c(1,1,1,1,1)) 268 | dev.off() 269 | ##suppfig 4b 270 | Final=list() 271 | for(i in 1:length(levels(as.factor(SeuratObj@meta.data$Patient)))){ 272 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==levels(as.factor(SeuratObj@meta.data$Patient))[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 273 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + #facet_grid(~ Fragment) + # 274 | labs(title=levels(as.factor(SeuratObj@meta.data$Patient))[i])+ 275 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 276 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 277 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 278 | Final[[i]]=x 279 | } 280 | mylegend<-get_legend(Final[[1]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Assignment",title.position = "top",title.theme = element_text(size=10)))) 281 | 282 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts Patient and Assignment suppfigure 4b.pdf")), height = 6, width =10,onefile = T) 283 | 284 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[2]]+NoLegend(),NA,NA,NA,NA,Final[[3]]+NoLegend(),Final[[4]]+NoLegend(), 285 | Final[[5]]+NoLegend(),Final[[6]]+NoLegend(),Final[[7]]+NoLegend(),Final[[8]]+NoLegend(), 286 | Final[[9]]+NoLegend(),Final[[10]]+NoLegend(),Final[[11]]+NoLegend(),Final[[12]]+NoLegend(),Final[[13]]+NoLegend(),NA, 287 | Final[[14]]+NoLegend(),Final[[15]]+NoLegend(),Final[[16]]+NoLegend(),Final[[17]]+NoLegend(),Final[[18]]+NoLegend(),NA, 288 | ncol = 6,nrow = 4, 289 | legend.grob = mylegend,legend = "right",widths = c(1,1,1,1,1,1)) 290 | dev.off() 291 | 292 | ##suppfig4c 293 | U1=DimPlot(SeuratObj, reduction = "umap",group.by = "Cluster" ,cols = ClusterColors,label=T)+ggmin::theme_min()+ 294 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 295 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Clusters")+ 296 | guides(colour = guide_legend(override.aes = list(size=5),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=15),ncol = 4)) 297 | 298 | U2=DimPlot(SeuratObj, reduction = "umap",group.by = "Patient",cols = SampleColors,shuffle = T )+ggmin::theme_min()+ 299 | scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)+ xlab("UMAP1") + ylab("UMAP2")+ 300 | FontSize(x.title = 20, y.title = 20,main = 20)+ theme(legend.position = "bottom")+labs(title="Patients")+ 301 | guides(colour = guide_legend(override.aes = list(size=4),title.theme = element_text(size=15, face="bold"),title.position = "top",label.theme = element_text(size=10),ncol = 4)) 302 | U1+U2 303 | ##supp fig5a big heatmap 304 | m_df_H<- msigdbr::msigdbr(species = "Homo sapiens", category = "H") 305 | Filtered.genesets=filter(m_df_H,gs_name=="HALLMARK_OXIDATIVE_PHOSPHORYLATION"|gs_name=="HALLMARK_TNFA_SIGNALING_VIA_NFKB"|gs_name=="HALLMARK_INTERFERON_GAMMA_RESPONSE"|gs_name=="HALLMARK_HYPOXIA") 306 | geneset.all <- Filtered.genesets[Filtered.genesets$gene_symbol %in% rownames(Counts),] 307 | genesetlist=geneset.all%>% split(x = .$gene_symbol, f = .$gs_name) 308 | 309 | for(i in 1:length(genesetlist)){ 310 | genes=genesetlist[[i]] 311 | plot.data<-as.data.frame(assay(SeuratObj.sce, "logcounts")) 312 | plot.data<-plot.data[genes,] 313 | plot.data<-filter(plot.data,rowMeans(plot.data)>0.2) 314 | plot.data <- plot.data - rowMeans(plot.data) 315 | plot.data=na.omit(plot.data) 316 | plot.data<-plot.data[order(rowMeans(plot.data),decreasing=T),] 317 | 318 | column_annot <-CellInfoS[,c("Cluster","Patient","Type"),drop=F] 319 | column_annot$Patient = as.factor(as.character(column_annot$Patient)) 320 | column_annot=with(column_annot, column_annot[order(Patient), , drop=F]) 321 | column_annot=with(column_annot, column_annot[order(Cluster), , drop=F]) 322 | plot.data<-plot.data[,row.names(column_annot)] 323 | column.col= SampleColors 324 | 325 | column.col2= ClusterColors 326 | column.col3= FragmentColors 327 | column.col4= TypeColors 328 | 329 | 330 | column.colors=list() 331 | column.colors[["Patient"]]<-column.col 332 | #column.colors[["Fragment"]]<-column.col3 333 | column.colors[["Cluster"]]<-column.col2 334 | column.colors[["Type"]]<-column.col4 335 | 336 | Patient=as.matrix(column_annot[,c("Patient"),drop=F]) 337 | Cluster=as.matrix(column_annot[,c("Cluster"),drop=F]) 338 | #Fragment=as.matrix(column_annot[,c("Fragment"),drop=F]) 339 | Type=as.matrix(column_annot[,c("Type"),drop=F]) 340 | 341 | col<- circlize::colorRamp2(breaks = c(-2, 0, 2), colors = c("#007dd1", "white", "#ab3000")) 342 | XX=names(genesetlist[i]) 343 | YY=str_remove(XX, "HALLMARK_") 344 | HM=Heatmap(name=XX,as.matrix(t(plot.data)),cluster_rows =F,cluster_columns = T,column_title =YY,column_title_gp = gpar(fontsize=18,fontface="bold"),height = 5,width = 7,row_split = Cluster, 345 | col = col,show_column_names=T,show_row_names=F,show_column_dend = F,row_names_side = "left",column_names_gp =gpar(fontsize=4.5) ,border = "black",show_heatmap_legend = F,use_raster = T) 346 | assign(paste0("HM",i),HM) 347 | } 348 | colanno <- rowAnnotation(df=column_annot, 349 | show_annotation_name =F,show_legend = F,col=column.colors) 350 | lgd=Legend(title = "logcounts", at= c(-2,0, 2),col_fun = col) 351 | lgd1=Legend(labels = levels(as.factor(column_annot$Cluster)),title="Cluster",legend_gp = gpar(fill=column.col2,fontsize=5)) 352 | lgd2=Legend(labels = levels(as.factor(column_annot$Patient)),title="Patient",legend_gp = gpar(fill=column.col,fontsize=5)) 353 | #lgd3=Legend(labels = levels(as.factor(column_annot$Fragment)),title="Fragment",legend_gp = gpar(fill=column.col3,fontsize=5)) 354 | lgd4=Legend(labels = levels(as.factor(column_annot$Type)),title="Type",legend_gp = gpar(fill=column.col4,fontsize=5)) 355 | HM=HM1+HM2+HM3+HM4+colanno 356 | pdf(paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"large Pathway combined heatmap fixed.pdf")),width=25,height=10) 357 | draw(HM,heatmap_legend_list = list(lgd,lgd4, lgd2,lgd1), heatmap_legend_side = "right",legend_labels_gp =gpar(col = "black", fontsize = 20,fontface="bold")) 358 | dev.off() 359 | 360 | 361 | #Suppfig7b 362 | pat2=c("LGG-04", "ndGBM-01", "ndGBM-02", "ndGBM-03","ndGBM-11", "rGBM-01", "rGBM-02", "rGBM-03", "rGBM-04", "rGBM-05") 363 | 364 | for(i in 1:length(pat2)){ 365 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==pat2[i]), aes(Patient, fill=Assignment))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 366 | scale_fill_manual(values = AssignmentColors)+coord_polar("y",start=0) + facet_grid(~ Fragment) + 367 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 368 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 369 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 370 | Final[[i]]=x 371 | } 372 | mylegend<-get_legend(Final[[2]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Assignment",title.position = "top",title.theme = element_text(size=10)))) 373 | 374 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts fragment and assignment suppfigure 5b.pdf")), height = 6, width =10,onefile = T) 375 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[6]]+NoLegend(),Final[[2]]+NoLegend(),Final[[7]]+NoLegend(),Final[[3]]+NoLegend(), 376 | Final[[8]]+NoLegend(),Final[[4]]+NoLegend(),Final[[9]]+NoLegend(),Final[[5]]+NoLegend(),Final[[10]],ncol = 2,nrow = 5, 377 | legend.grob = mylegend,legend = "right",widths = c(1,1)) 378 | dev.off() 379 | 380 | 381 | #suppfig5a 382 | SeuratObj=readRDS(paste0(RobjDirectory,"GliomaClusters-11-3-21 patients renamed.rds")) 383 | ClusterColors=c(GC01 = "#4BA75E", GC02 = "#003F5C", GC03 = "#235A82", GC04 = "#877EAE", 384 | GC05 = "#4367B2", GC06 = "#8DB032", GC07 = "#007D8E", GC08 = "#266AB3", 385 | GC09 = "#7B5DA5") 386 | pat2=c("LGG-04", "ndGBM-01", "ndGBM-02", "ndGBM-03","ndGBM-11", "rGBM-01", "rGBM-02", "rGBM-03", "rGBM-04", "rGBM-05") 387 | 388 | for(i in 1:length(pat2)){ 389 | x=ggplot(SeuratObj@meta.data%>%filter(Patient==pat2[i]), aes(Patient, fill=Cluster))+geom_bar(stat="count",position="fill",colour = "white",width =0.7)+ 390 | scale_fill_manual(values = ClusterColors)+coord_polar("y",start=0) + facet_grid(~ Fragment) + 391 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.text=element_blank(),axis.ticks = element_blank(), 392 | panel.background = element_blank(), axis.title = element_blank(),strip.background=element_rect(fill="white"), 393 | strip.text=element_text(face = "bold",color = "black",size=8),plot.margin = unit(c(0,-0.2,-0.2,0),'cm')) 394 | Final[[i]]=x 395 | } 396 | mylegend<-get_legend(Final[[2]]+theme_min()+ NoAxes()+guides(colour = guide_colourbar(title="Cluster",title.position = "top",title.theme = element_text(size=10)))) 397 | 398 | pdf(file =paste(paste0(OutputDirectory,ObjName,Subset,"res",resolution,"piecharts fragment and assignment suppfigure 5a.pdf")), height = 6, width =10,onefile = T) 399 | ggpubr::ggarrange(Final[[1]]+NoLegend(),Final[[6]]+NoLegend(),Final[[2]]+NoLegend(),Final[[7]]+NoLegend(),Final[[3]]+NoLegend(), 400 | Final[[8]]+NoLegend(),Final[[4]]+NoLegend(),Final[[9]]+NoLegend(),Final[[5]]+NoLegend(),Final[[10]],ncol = 2,nrow = 5, 401 | legend.grob = mylegend,legend = "right",widths = c(1,1)) 402 | dev.off() 403 | 404 | 405 | #suppfig7c 406 | CellInfo=SeuratObj@meta.data 407 | set.seed(12) 408 | Idents(SeuratObj)=CellInfo$Patient 409 | sampled.cells <- sample(x = SeuratObj@active.ident, size = 10000, replace = F) 410 | subset=subset(SeuratObj,cells=names(sampled.cells)) 411 | 412 | Counts <- as.matrix(GetAssayData(object = subset, assay = "RNA", slot = "counts")) 413 | 414 | #get scores 415 | m_df_H<- msigdbr::msigdbr(species = "Homo sapiens", category = "H") 416 | Filtered.genesets=filter(m_df_H,gs_name=="HALLMARK_HYPOXIA"|gs_name=="HALLMARK_INTERFERON_GAMMA_RESPONSE"|gs_name=="HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION"|gs_name=="HALLMARK_MYC_TARGETS_V1") 417 | geneset.all <- Filtered.genesets[Filtered.genesets$gene_symbol %in% rownames(Counts),] 418 | geneset <- geneset.all[!duplicated(geneset.all$gene_symbol),] 419 | genesetlist=geneset.all%>% split(x = .$gene_symbol, f = .$gs_name) 420 | #get score 421 | sc=score(mat=Counts, 422 | groups=genesetlist, 423 | binmat = NULL, 424 | bins = NULL, 425 | controls = NULL, 426 | bin.control = F, 427 | center = T, 428 | nbin = 30, 429 | n = 100, 430 | replace = T) 431 | 432 | #get coordinates 433 | h=hierarchy (sc, quadrants = c("HALLMARK_HYPOXIA","HALLMARK_INTERFERON_GAMMA_RESPONSE","HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION","HALLMARK_MYC_TARGETS_V1"), log.scale =T) 434 | 435 | #add cluster colors to metadata 436 | 437 | CellInfo=SeuratObj@meta.data 438 | for( i in 1: length(names(ClusterColors))){ 439 | name=names(ClusterColors)[i] 440 | color=ClusterColors[i] 441 | CellInfo$Clustercolor [CellInfo$Cluster== name] <- color 442 | } 443 | Clustercolor <- CellInfo$Clustercolor 444 | names(Clustercolor ) <- row.names(CellInfo) 445 | SeuratObj <- AddMetaData(object = SeuratObj,metadata = Clustercolor ,col.name = 'Clustercolor') 446 | 447 | #Make the big plot--> all clusters 448 | 449 | 450 | xmax=max(h$X)+(max(h$X)*0.2) 451 | xmin=min(h$X)+(min(h$X)*0.2) 452 | ymax=max(h$Y)+(max(h$Y)*0.6) 453 | ymin=min(h$Y)+(min(h$Y)*0.6) 454 | 455 | #Make the big plot--> all Patients 456 | groups=SeuratObj@meta.data[,c("Patientcolor","Patient")] 457 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 458 | matrix$Patientcolor[is.na(matrix$Patientcolor)] <- "gray" 459 | matrix$Patient[is.na(matrix$Patient)] <- "Other" 460 | row.names(matrix)=matrix$Row.names 461 | x=matrix$Patientcolor 462 | y=matrix$Patient 463 | col=x[!duplicated(x)] 464 | names(col)=y[!duplicated(y)] 465 | matrix=matrix[,-1] 466 | title="All Patients" 467 | p0 <- ggplot(matrix, aes(x = X, 468 | y =Y,color=factor(Patient)))+geom_point()+geom_point(data = subset(matrix, Patient !="Other"))+ 469 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Patient")+theme(legend.position = "none")+ 470 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 471 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 472 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 473 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 474 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 475 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 476 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 477 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 478 | geom_hline(yintercept=0, color = "black", size=0.5)+ 479 | geom_vline(xintercept=0, color = "black", size=0.5)+ 480 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymax-2, ymax = ymax, fill= "black") + 481 | annotate("text",x = xmin+2.5, y = ymax-1,label = "EMT",color="white",fontface="bold",size=9)+ 482 | annotate("rect", xmin = xmax-5, xmax = xmax, ymin = ymax-2, ymax = ymax, fill= "black") + 483 | annotate("text",x = xmax-2.5, y = ymax-1,label = "MYC-Tar",color="white",fontface="bold",size=9)+ 484 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymin+2, ymax = ymin, fill= "black") + 485 | annotate("text",x = xmin+2.5, y = ymin+1,label = "Hypoxia",color="white",fontface="bold",size=9)+ 486 | annotate("rect", xmin = xmax-5, xmax =xmax, ymin = ymin+2, ymax = ymin, fill= "black") + 487 | annotate("text",x = xmax-2.5, y = ymin+1,label = "IFNg-res",color="white",fontface="bold",size=9) 488 | P0=ggplotGrob(p0) 489 | ##make the small plots--> one per Patient 490 | Final <- list() 491 | Final[[1]]=ggplotGrob(p0) 492 | for (i in 1:length(names(SampleColors))) { 493 | PatientMD=SeuratObj@meta.data[SeuratObj@meta.data$Patient==paste0(names(SampleColors)[i]),] 494 | groups=PatientMD[,c("Patientcolor","Patient")] 495 | title=paste0(names(SampleColors)[i]) 496 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 497 | matrix$Patientcolor[is.na(matrix$Patientcolor)] <- "gray" 498 | matrix$Patient=as.character(matrix$Patient) 499 | matrix$Patient[is.na(matrix$Patient)] <- "Other" 500 | row.names(matrix)=matrix$Row.names 501 | x=matrix$Patientcolor 502 | y=matrix$Patient 503 | col=x[!duplicated(x)] 504 | names(col)=y[!duplicated(y)] 505 | matrix=matrix[,-1] 506 | P=ggplot(matrix, aes(x = X, 507 | y =Y,color=factor(Patient)))+geom_point()+geom_point(data = subset(matrix, Patient !="Other"))+ 508 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 509 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 510 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 511 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 512 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 513 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 514 | geom_hline(yintercept=0, color = "black", size=0.5)+ 515 | geom_vline(xintercept=0, color = "black", size=0.5)+ 516 | annotate("rect", xmin = xmin, xmax = xmin+2, ymin = ymax-1, ymax = ymax, fill= "black") + 517 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymax-2, ymax = ymax, fill= "black") + 518 | annotate("text",x = xmin+2.5, y = ymax-1,label = "EMT",color="white",fontface="bold",size=4)+ 519 | annotate("rect", xmin = xmax-5, xmax = xmax, ymin = ymax-2, ymax = ymax, fill= "black") + 520 | annotate("text",x = xmax-2.5, y = ymax-1,label = "MYC-Tar",color="white",fontface="bold",size=4)+ 521 | annotate("rect", xmin = xmin, xmax = xmin+5, ymin = ymin+2, ymax = ymin, fill= "black") + 522 | annotate("text",x = xmin+2.5, y = ymin+1,label = "Hypoxia",color="white",fontface="bold",size=4)+ 523 | annotate("rect", xmin = xmax-5, xmax =xmax, ymin = ymin+2, ymax = ymin, fill= "black") + 524 | annotate("text",x = xmax-2.5, y = ymin+1,label = "IFNg-res",color="white",fontface="bold",size=4) 525 | Final[[i+1]] = ggplotGrob(P) 526 | } 527 | Final[[1]]=ggplotGrob(p0) 528 | numofplots= length(Final) 529 | pdf(file =paste0(OutputDirectory,ObjName,Subset," subtype butterfly by Patient ordered by type3-suppfig7c.pdf"), height = 10, width =22,onefile = T) 530 | grid.arrange(grobs=Final, widths = c(3,1,1,1,1,1,1),layout_matrix = rbind(c(1, 2,NA, NA,NA,NA,3),c(1,4,5,6,7,8,9), 531 | c(1,10,11,12,13,14,NA), 532 | c(1,15,16,17,18,19,NA))) 533 | dev.off() 534 | 535 | groups=SeuratObj@meta.data[,c("Fragmentcolor","Fragment")] 536 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 537 | matrix$Fragmentcolor[is.na(matrix$Fragmentcolor)] <- "gray" 538 | matrix$Fragment[is.na(matrix$Fragment)] <- "Other" 539 | row.names(matrix)=matrix$Row.names 540 | x=matrix$Fragmentcolor 541 | y=matrix$Fragment 542 | col=x[!duplicated(x)] 543 | names(col)=y[!duplicated(y)] 544 | matrix=matrix[,-1] 545 | title="All Fragments" 546 | p0 <- ggplot(matrix, aes(x = X, 547 | y =Y,color=factor(Fragment)))+geom_point()+geom_point(data = subset(matrix, Fragment !="Other"))+ 548 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL,colour="Fragment")+theme(legend.position = "none")+ 549 | theme(legend.text=element_text(size=15),legend.title =element_text(size=15,face="bold") )+ 550 | theme(axis.title.x = element_text(hjust = 0, vjust=-2, colour="black",size=10,face="bold"))+ 551 | theme(axis.title.y = element_text(hjust = 0, vjust=4, colour="black",size=10,face="bold"))+ 552 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 553 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 554 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 555 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 556 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 557 | geom_hline(yintercept=0, color = "black", size=0.5)+ 558 | geom_vline(xintercept=0, color = "black", size=0.5)+ 559 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 560 | annotate("text",x = xmin+2, y = ymax-0.5,label = "EMT",color="white",fontface="bold",size=9)+ 561 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 562 | annotate("text",x = xmax-2, y = ymax-0.5,label = "MYC-Tar",color="white",fontface="bold",size=9)+ 563 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 564 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Hypoxia",color="white",fontface="bold",size=9)+ 565 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 566 | annotate("text",x = xmax-2, y = ymin+0.5,label = "IFNg-res",color="white",fontface="bold",size=9) 567 | P0=ggplotGrob(p0) 568 | ##make the small plots--> one per Fragment 569 | 570 | for (i in 1:length(names(FragmentColors))) { 571 | FragmentMD=SeuratObj@meta.data[SeuratObj@meta.data$Fragment==paste0(names(FragmentColors)[i]),] 572 | groups=FragmentMD[,c("Fragmentcolor","Fragment")] 573 | title=paste0(names(FragmentColors)[i]) 574 | matrix=merge(h,groups,by.x=0,by.y=0,all.x = T) 575 | matrix$Fragmentcolor[is.na(matrix$Fragmentcolor)] <- "gray" 576 | matrix$Fragment=as.character(matrix$Fragment) 577 | matrix$Fragment[is.na(matrix$Fragment)] <- "Other" 578 | row.names(matrix)=matrix$Row.names 579 | x=matrix$Fragmentcolor 580 | y=matrix$Fragment 581 | col=x[!duplicated(x)] 582 | names(col)=y[!duplicated(y)] 583 | matrix=matrix[,-1] 584 | P=ggplot(matrix, aes(x = X, 585 | y =Y,color=factor(Fragment)))+geom_point()+geom_point(data = subset(matrix, Fragment !="Other"))+ 586 | scale_color_manual(values=col,aesthetics = "colour", breaks = waiver()) +labs(x=NULL,y=NULL)+theme(legend.position = "none")+ 587 | scale_x_continuous(expand = c(0, 0), limits = c(xmin,xmax)) + scale_y_continuous(expand = c(0, 0), limits = c(ymin,ymax))+ 588 | theme(panel.background = element_rect(fill = "white",colour = "white"),axis.ticks.x=element_blank(),axis.text.x=element_blank(), 589 | axis.ticks.y=element_blank(), axis.text.y=element_blank())+ 590 | ggtitle(title)+theme(plot.title =element_text(size=25,face="bold") )+ 591 | theme(panel.border = element_rect(colour = "black", fill=NA, size=2))+ 592 | geom_hline(yintercept=0, color = "black", size=0.5)+ 593 | geom_vline(xintercept=0, color = "black", size=0.5)+ 594 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymax-1, ymax = ymax, fill= "black") + 595 | annotate("text",x = xmin+2, y = ymax-0.5,label = "EMT",color="white",fontface="bold",size=4)+ 596 | annotate("rect", xmin = xmax-4, xmax = xmax, ymin = ymax-1, ymax = ymax, fill= "black") + 597 | annotate("text",x = xmax-2, y = ymax-0.5,label = "MYC-Tar",color="white",fontface="bold",size=4)+ 598 | annotate("rect", xmin = xmin, xmax = xmin+4, ymin = ymin+1, ymax = ymin, fill= "black") + 599 | annotate("text",x = xmin+2, y = ymin+0.5,label = "Hypoxia",color="white",fontface="bold",size=4)+ 600 | annotate("rect", xmin = xmax-4, xmax =xmax, ymin = ymin+1, ymax = ymin, fill= "black") + 601 | annotate("text",x = xmax-2, y = ymin+0.5,label = "IFNg-res",color="white",fontface="bold",size=4) 602 | Final[[i+1]] = ggplotGrob(P) 603 | } 604 | Final[[1]]=ggplotGrob(p0) 605 | numofplots= length(Final) 606 | pdf(file =paste0(OutputDirectory,ObjName,Subset," pathway butterfly by Fragment suppfigure 7c.pdf"), height = 15, width =37,onefile = T) 607 | grid.arrange(grobs=Final, widths = c(4,1,1,1,1,1,1,1,1,1,1,1),layout_matrix = rbind(c(1, 2, 3,4,5,NA,NA,NA,NA,NA,NA,NA),c(1,6,7,8,9,10, 11, 12,13,14,15,16), 608 | c(1,17,18,19, 20, 21,22,23,24,25,26,27), 609 | c(1, 28,29, 30,31,32,33,34,35,36,37,38), 610 | c(1, 39,40,41,42,43,44,45,NA,NA,NA,NA))) 611 | dev.off() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A glioma immune cell atlas identifies prognostic macrophage signatures and a novel immunotherapy target, S100A4 2 | 3 | 4 | # What is this? 5 | This repository contains R codes used to perform the analysis in the "A glioma immune cell atlas identifies prognostic macrophage signatures and a novel immunotherapy target, S100A4" paper. The purpose of making the code available is for transparency and data analysis reproducibility. 6 | 7 | # Date Source 8 | Single-cell sequencing data was generated from human GBM patients using 10X platform and sequenced on Illumina. 9 | 10 | # Where can I find the raw data? 11 | Raw sequencing data (FASTQ) and processed 10X format matrix files can be found on GEO at GSE182109 series. 12 | -------------------------------------------------------------------------------- /Rcode: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------