-
Notifications
You must be signed in to change notification settings - Fork 1
/
#_GseaGo_Main_scRNA_Cachexia.R
255 lines (188 loc) · 9.06 KB
/
#_GseaGo_Main_scRNA_Cachexia.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
##### Presetting ######
rm(list = ls()) # Clean variable
memory.limit(150000)
##### Load Packages #####
# if(!require("tidyverse")) install.packages("tidyverse")
# library(tidyverse)
#### Basic installation ####
## Check whether the installation of those packages is required from basic
Package.set <- c("tidyverse","ggplot2","Seurat","SeuratData","patchwork","plyr","eoffice","DT")
for (i in 1:length(Package.set)) {
if (!requireNamespace(Package.set[i], quietly = TRUE)){
install.packages(Package.set[i])
}
}
## Load Packages
lapply(Package.set, library, character.only = TRUE)
rm(Package.set,i)
#### BiocManager installation ####
## Set the desired organism
# organism = "org.Hs.eg.db" ## c("org.Hs.eg.db","org.Mm.eg.db") ## c("org.Dm.eg.db")
## Check whether the installation of those packages is required from BiocManager
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
Package.set <- c("clusterProfiler","enrichplot","pathview") # c(organism,"fgsea","clusterProfiler","enrichplot","pathview")
for (i in 1:length(Package.set)) {
if (!requireNamespace(Package.set[i], quietly = TRUE)){
BiocManager::install(Package.set[i])
}
}
## Load Packages
lapply(Package.set, library, character.only = TRUE)
rm(Package.set,i)
# options(stringsAsFactors = FALSE)
# Sys.setlocale(category = "LC_ALL", locale = "UTF-8")
##### Load RData* #####
# load("D:/Dropbox/##_GitHub/##_PHH_Lab/PDAC_Cachexia_10X/2022-08-13_PBMC_Main/06_Cell_type_annotation.RData")
load("D:/Dropbox/##_GitHub/##_PHH_Lab/PDAC_Cachexia_10X/2022-09-09_PBMC_Main/06_Cell_type_annotation.RData")
# Clean up Object
scRNA.SeuObj <- PBMC.combined
rm(list=setdiff(ls(), "scRNA.SeuObj"))
## Save Ori
scRNA_Ori.SeuObj <- scRNA.SeuObj
## Clean up data (Delete other type)
scRNA.SeuObj <- scRNA.SeuObj[,!grepl("Other", [email protected][["celltype"]] )]
## Clean up data (Delete other type)
##### Function setting #####
## Call function
source("FUN_Beautify_ggplot.R")
source("FUN_Find_Markers.R")
source("FUN_VolcanoPlot.R")
source("FUN_ggPlot_vline.R")
source("FUN_GSEA_ANAL.R")
source("FUN_DistrPlot.R")
##### Import setting and Import #####
## Import GSEA gene sets
# InputGSEA <- "GSEA_Geneset_Pathway_3Database_WithoutFilter.txt"
# InputGSEA <- "m5_go_bp_v0_3_symbols.gmt" # InputGSEA <- "m2.all.v0.3.symbols.gmt"
InputGSEA <- "m5_go_bp_v0_3_symbols.gmt"
InFOLName_GSEA <- "Input_Genesets"
Pathway.all <- read.delim2(paste0(getwd(),"/",InFOLName_GSEA,"/",InputGSEA),
col.names = 1:max(count.fields(paste0(getwd(),"/",InFOLName_GSEA,"/",InputGSEA))),
header = F,sep = "\t")
##### Conditions setting* #####
Group_Mode <- "GoupByGeneExp" # c("GoupByPheno","GoupByGeneExp")
TarGene_name <- "Chil3"
GeneExpSet.lt <- list(GeneExpMode = "Mean", # c("Mean","Mean1SD","Mean2SD","Mean3SD","Median","Quartile","Customize"))
UpCutoff = 1, LowerCutoff = 1)
if(Group_Mode == "GoupByGeneExp"){
## Group by GeneExp
AnnoSet.lt <- list(GroupType = TarGene_name, GroupCompare = c("High","Low") ) ## DEG by GeneExp group
}else{
## Group by Pheno
AnnoSet.lt <- list(GroupType = "sample_type", GroupCompare = c("Primary Tumor","Recurrent Tumor") )
}
Thr.lt <- list(LogFC = c("logFC",1), pVal = c("PValue",0.05) )
##### Current path and new folder setting* #####
ProjectName = "CC10X"
Sampletype = "PBMC"
ExportAnno2 = "EO_Neu"
if(Group_Mode == "GoupByGeneExp"){
ExportAnno = paste0(TarGene_name,"_",GeneExpSet.lt$GeneExpMode,"_",ExportAnno2)
}else{
ExportAnno = paste0(Group_Mode,"_",ExportAnno2)
}
# ExportAnno = "Chil3Mean_PathM2"
# ExportAnno = "Recur2Prim"
ExportName = paste0(ProjectName,"_",Sampletype,"_",ExportAnno)
Version = paste0(Sys.Date(),"_",ProjectName,"_",Sampletype,"_", ExportAnno)
Save.Path = paste0(getwd(),"/",Version)
## Create new folder
if (!dir.exists(Save.Path)){
dir.create(Save.Path)
}
##### Update the genename ####
# ## Test
# UpdateSymbolList("SEPT1")
# A <- UpdateSymbolList(row.names(GeneExp.df))
# B <- row.names(GeneExp.df)
# # sum(c("a","c")==c("a","b"))
# sum(A==B)
# summary(A==B)
#
## Error: Timeout was reached: [rest.genenames.org] Operation timed out after 10005 milliseconds with 0 bytes received
## Update the genename ##* Take very long time
UpdateGene <- "No" # UpdateGene <- c("Yes","No")
if(UpdateGene == "Yes"){
row.names(GeneExp.df) <- UpdateSymbolList(row.names(GeneExp.df))
}
#************************************************************************************************************************#
##### Data preprocess setting #####
## Extract data from scRNA.SeuObj
## Old version (Without normalizaiton) ## GeneExp.df <- scRNA.SeuObj@assays[["RNA"]]@counts %>% as.data.frame()
GeneExp.df <- GetAssayData(scRNA.SeuObj, assay = "RNA", slot = "data") %>% as.data.frame() # normalized data matrix
Anno.df <- [email protected]
Anno.df <- data.frame(ID=row.names(Anno.df), Anno.df)
row.names(Anno.df) <- Anno.df[,1]
## Select Pheno column
Anno_Ori.df <- Anno.df
colnames(Anno.df)
# PhenoColKeep.set <- c("X_INTEGRATION","X_PATIENT","histological_type","sample_type","gender")
# Anno.df <- Anno.df[,c(PhenoColKeep.set)]
# colnames(Anno.df)
#
# head(Anno.df)
## Select Pheno row
PhenoRowKeep.set <- list(col="Cachexia" ,row=c("EO"))
Anno.df <- Anno.df[Anno.df[,PhenoRowKeep.set[["col"]]] %in% PhenoRowKeep.set[["row"]], ]
GeneExp.df <- GeneExp.df[,colnames(GeneExp.df) %in% Anno.df[,1] ]
## Select Pheno row2
PhenoRowKeep.set <- list(col="celltype" ,row=c("Neu"))
Anno.df <- Anno.df[Anno.df[,PhenoRowKeep.set[["col"]]] %in% PhenoRowKeep.set[["row"]], ]
GeneExp.df <- GeneExp.df[,colnames(GeneExp.df) %in% Anno.df[,1] ]
# ## Delete specific cell type
# ## Clean up data
# Anno.df <- Anno.df[!grepl("Other", Anno.df$celltype),]
# GeneExp.df <- GeneExp.df[,colnames(GeneExp.df) %in% Anno.df$ID]
#************************************************************************************************************************#
##### Visualization #####
source("FUN_DistrPlot.R")
##### Group by gene expression 1: CutOff by total #####
Plot.DistrPlot <- FUN_DistrPlot(GeneExp.df,
TarGeneName = TarGene_name, GroupSet = GeneExpSet.lt,
Save.Path = Save.Path, ExportName = ExportName)
Plot.DistrPlot_SD_Q <- Plot.DistrPlot[["TGeneDen_SD_Q.p"]]
Plot.DistrPlot_SD_Q
#************************************************************************************************************************#
##### Grouping #####
source("FUN_Group_GE.R")
##### Group by gene expression 1: CutOff by total #####
GeneExp_group.set <- FUN_Group_GE(GeneExp.df, Anno.df,
TarGeneName = TarGene_name, GroupSet = GeneExpSet.lt,
Save.Path = Save.Path, ExportName = ExportName)
Anno.df <- GeneExp_group.set[["AnnoNew.df"]]
GeneExp_high.set <- GeneExp_group.set[["GeneExp_high.set"]]
GeneExp_low.set <- GeneExp_group.set[["GeneExp_low.set"]]
##### Group by gene expression 2: CutOff by Comparison #####
## FUN Comparison (Visualization and value)
##### Group by phenotype #####
#************************************************************************************************************************#
##### Run Enrichment analysis in R #####
#### Run DEG ####
source("FUN_DEG_Analysis.R")
DEG_ANAL.lt <- FUN_DEG_Analysis(GeneExp.df, Anno.df,
GroupType = AnnoSet.lt[["GroupType"]], GroupCompare = AnnoSet.lt[["GroupCompare"]],
ThrSet = Thr.lt,
TarGeneName = TarGene_name, GroupMode = GeneExpSet.lt, SampleID = "ID",
Save.Path = Save.Path, ExportName = ExportName, AnnoName = "AvB")
DE_Extract.df <- DEG_ANAL.lt[["DE_Extract.df"]]
#### Run GSEA ####
source("FUN_GSEA_ANAL.R")
GSEA_Result.lt <- FUN_GSEA_ANAL(DE_Extract.df, CMGeneSet = Pathway.all,
NumGenesetsPlt=15,
TarGeneName = TarGene_name,
ThrSet = Thr.lt, Species = "Homo sapiens", # Speices type can check by msigdbr_species()
Save.Path = Save.Path, ExportName = ExportName, AnnoName = "Path")
#### Run ORA ####
## FUN ORA
#************************************************************************************************************************#
##### Build files for GSEA official input #####
source("FUN_GSEA_ForOFFL.R")
FUN_GSEA_ForOFFL(GeneExp.df, Group1 = GeneExp_high.set, Group2 = GeneExp_low.set,
GroupMode = Group_Mode,
TarGeneName = TarGene_name, GeneExpSet = GeneExpSet.lt,
Save.Path = Save.Path, ExportName= ExportName,
AnnoName="Recur2Prim")
##### Build files for Metascape official input #####
#### Save RData ####
save.image(paste0(Save.Path,"/GseaGo_",ExportName,".RData"))