forked from SuiYinG2000/Signature-Correlation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneSignature-Pathways_Correlation.Rmd
484 lines (375 loc) · 14.7 KB
/
GeneSignature-Pathways_Correlation.Rmd
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
---
title: "GeneSignature-Pathways_Correlation"
author: "Botao Wang"
date: '2022-09-02'
output: html_document
params:
workdir: "E:\\PostGraduate\\Botao\\Code_Verification\\SignatureGenes_GO-PW_Correlation"
libdir: "E:\\GithubRepository\\Signature-Correlation\\lib"
# resultdir: "\\results\\"
# picdir: "\\pics\\"
expression_file: "\\data\\TCGA_Brain-GBM_AllSample_Gene-Name_Expression_tpm_unstranded_2022-09-27.csv"
# exprType: "TPM"
GOGeneset: "\\data\\goGeneSet_normalized.gmt"
PWGeneset: "\\data\\pathwayGeneSet_normalized.gmt"
# already been Normalized or not
GeneSet_Name_Normalized: TRUE
GeneNameAnno: "\\data\\Homo_sapiens.gene_info.csv"
SignatureGenes_Name: "m6A_Signature_Genes"
SignatureGenes: "METTL3,METTL14,WTAP,RBM15,ZC3H13,KIAA1429,METTL16,FTO,ALKBH5"
corAnalysis_Method: "pearson"
# params for func : get_Geneset_Score
calOrder: "standFirst"
# "calFirst" or "standFirst" or "noStand"
# calFirst means calculate signature mean/median expression first
pickMethod: "median"
log: "log2(n)"
# "log2(n)" or "log2(n+1)"
exp: FALSE
---
# README
```{r setup, include=FALSE}
# rm(list = ls())
# .rs.restartR()
library("knitr")
# library("limma")
# library("ComplexHeatmap")
# library("circlize")
library("log4r")
knitr::opts_chunk$set(echo = TRUE)
#* HTML显示结果时同时显示代码
dir.exists(params$workdir)
knitr::opts_knit$set(root.dir = params$workdir)
#* 设定工作目录
getwd()
source(file.path(params$lib, "GeneSignature-Pathways_Correlation_Function.R"))
# entering Genes for Gene Signature
SignatureGenes <- unlist(strsplit(params$SignatureGenes,split=","))
```
## Project Description
```{r Description}
describe <- paste(unlist(strsplit(params$SignatureGenes_Name, split="_"))[1],
"-GOPW-Correlation", sep="")
expression_file <- unlist(strsplit(params$expression_file, split="\\\\"))[3]
if (startsWith(expression_file, "TCGA")){
describe <- paste(describe, "TCGA", sep="_")
} else if (startsWith(expression_file, "CGGA")){
describe <- paste(describe, "CGGA", sep="_")
}
if (params$GeneSet_Name_Normalized){
describe <- paste(describe, "Symbol", sep="_")
} else {
describe <- paste(describe, "PartSym", sep="_")
}
if (startsWith(expression_file, "TCGA")){
describe <- paste(describe,
unlist(strsplit(params$expression_file, split="_"))[6],
sep="_")
} else if (startsWith(expression_file, "CGGA")){
exprType <- unlist(strsplit(params$expression_file, split="\\."))[3]
exprType <- unlist(strsplit(exprType, split="-"))[1]
describe <- paste(describe,
exprType,
sep="_")
}
describe <- paste(describe,
params$corAnalysis_Method,
params$calOrder,
params$pickMethod,
sep="_")
if (params$log != FALSE){
describe <- paste(describe, params$log, sep="_")
}
if (params$exp){
describe <- paste(describe, "exp", sep="_")
}
```
## Prepare direction
```{r prepare direction}
resultdir <- paste(params$workdir, "\\results\\", sep="")
picdir <- paste(params$workdir, "\\pics\\", sep="")
if (!dir.exists(resultdir)){dir.create(resultdir)}
if (!dir.exists(picdir)){dir.create(picdir)}
```
## Prepare log
```{r prepare log}
logdir <- paste(params$workdir, "\\log\\", sep="")
if (!dir.exists(logdir)){dir.create(logdir)}
logfile <- paste(logdir, "perform.log", sep="")
if (!file.exists(logfile)){file.create(logfile)}
logger <- create.logger(logfile = paste(logdir, "perform.log", sep=""),
level="INFO")
# descript_work <- paste("[ "
# , unlist(strsplit(params$SignatureGenes_Name,split="_"))[1]
# , "_", params$exprType
# , "_Symbol[", params$GeneSet_Name_Normalized, "]"
# , "_", params$corAnalysis_Method
# , "_", params$calOrder
# , "_", params$pickMethod
# , "_Log2[", params$log, "]"
# , "_Exp[", params$exp, "]"
# , " ]"
# , sep="")
descript_work <- paste("[ ", describe, " ]", sep="")
info(logger,
"------------------------------------------",
descript_work,
"------------------------------------------")
info(logger, "START PROJECT:")
info(logger, "Signature Name:\t", params$SignatureGenes_Name)
info(logger, "Signature Genes:\t", params$SignatureGenes)
```
## Prepare data
读入数据,包括:
* 表达谱数据
* GO基因集
* Pathway基因集
* 基因Symbol和同名对应集
```{r Preparation}
# Read in expression data
expr <- read.csv(paste(params$workdir, params$expression_file, sep=""),
row.names=1, check.names=F)
info(logger, "Expression Data:\t", params$expression_file)
info(logger, "Expression Data Type:\t", unlist(strsplit(params$expression_file, split="_"))[6])
mode(expr[1,1]) == "numeric"
# Read in Gene Sets from GO&PW
info(logger, "Gene Sets normalized to Symbol:\t", params$GeneSet_Name_Normalized)
goGeneSet <- readGMT(paste(params$workdir, params$GOGeneset, sep=""),
normalized=params$GeneSet_Name_Normalized)
info(logger, "GO Gene Sets:\t", params$GOGeneset)
info(logger, paste("GO共有", length(goGeneSet), "个基因集"))
pathwayGeneSet <- readGMT(paste(params$workdir, params$PWGeneset, sep=""),
normalized=params$GeneSet_Name_Normalized)
info(logger, "Pathway Gene Sets:\t", params$PWGeneset)
info(logger, paste("Pathway共有", length(pathwayGeneSet), "个基因集"))
# Read in Gene Name Annotation
geneNameAnno <- read.csv(paste(params$workdir, params$GeneNameAnno, sep=""),
check.names = F)
```
## Gene Name Conversion
```{r GeneName Conversion}
geneNameAnno_list <- strsplit(geneNameAnno$Synonyms, split="\\|")
names(geneNameAnno_list) <- geneNameAnno$Symbol
not_Symbol_Num <- 0
not_convert_Num <- 0
convert_num <- 0
not_convert_gene <- c()
# 遍历检查每一个SignatureGene的名称
for (i in 1:length(SignatureGenes)){
# 检查是否为Gene Symbol
if (!(SignatureGenes[i] %in% names(geneNameAnno_list))){
not_Symbol_Num <- not_Symbol_Num + 1
# 检查是否为别称
for (j in 1:length(geneNameAnno_list)){
if (SignatureGenes[i] %in% geneNameAnno_list[[j]]){
# 更正名称为Symbol
SignatureGenes[i] <- names(geneNameAnno_list)[j]
convert_num <- convert_num + 1
} else {
not_convert_Num <- not_convert_Num + 1
not_convert_gene <- c(not_convert_gene, SignatureGenes[i])
}
}
}
}
if (!is.null(not_convert_gene)){
not_convert_gene <- paste(not_convert_gene, collapse=",")
}
if (not_Symbol_Num != convert_num){
info(logger,
paste("Signature Genes共有", not_Symbol_Num, "个基因需要转化为Symbol,其中",
convert_num, "个转换成功,",
not_convert_Num, "个转换失败,为:",
not_convert_gene))
} else {
info(logger,
paste("Signature Genes共有", not_Symbol_Num, "个基因需要转化为Symbol,全部转化成功"))
}
```
## Calculate SignatrueGenes Scores
分别计算SignatureGenes和GO/PATHWAY的Score值
```{r calculate GS Scores}
info(logger, "Calculation Method:\t", params$pickMethod)
info(logger, "Log:\t", params$log)
info(logger, "Exp:\t", params$exp)
info(logger, "Calculation Order:\t", params$calOrder)
# precreate a data.frame for Signature Gene Scores
SG_Scores_DF <- data.frame(names=colnames(expr))
SG_Scores_DF <- data.frame(t(SG_Scores_DF),stringsAsFactors=F)
colnames(SG_Scores_DF) <- SG_Scores_DF[1,]
# Signature Genes Score
####################################################
###############################################################################
# debug #
# #
# length(SignatureGenes) #
# length(rownames(expr[rownames(expr) %in% SignatureGenes,])) #
# setdiff(SignatureGenes, rownames(expr[rownames(expr) %in% SignatureGenes,]))#
# #
#
#
# SignatureGenes_expr <- expr[rownames(expr) %in% SignatureGenes,]
# SignatureGenes_expr <- log2(SignatureGenes_expr+1)
# Score <- apply(SignatureGenes_expr,1,function(tmp){
# scale(tmp)
# })
# Score <- apply(Score, 1, function(tmp){ mean(tmp) })
# Score1 <- Score+1
# Score2 <- log2(Score1)
#
# a <- data.frame(a=c(1,2,3), b=c(3,4,5), c=c(6,7,8))
# score1 <- apply(a, 1, function(tmp){median(tmp)}) #按行操作
# score2 <- apply(a, 2, function(tmp){median(tmp)}) #按列操作
###############################################################################
# Calculate Gene Signature scores
SGscores.raw <- get_GeneSignature_Score(expr, SignatureGenes,
pickMethod=params$pickMethod,
log=params$log,
exp=params$exp,
calOrder=params$calOrder)
# Scaled scores
#=============================================
if (params$calOrder == "calFirst"){
SGscores <- as.numeric(scale(SGscores.raw))
names(SGscores) <- names(SGscores.raw)
names(SGscores) == colnames(expr)
} else if (params$calOrder == "standFirst") {
SGscores <- SGscores.raw
} else if (params$calOrder == "noStand"){
SGscores <- SGscores.raw
}
SG_Scores_DF <- rbind(SG_Scores_DF, SGscores)
SG_Scores_DF <- SG_Scores_DF[-1,]
SG_Scores_DF <- as.data.frame(lapply(SG_Scores_DF, as.numeric))
rownames(SG_Scores_DF)[1] <- params$SignatureGenes_Name
```
## Calculate GO&PW Gene-set Scores
```{r calculate GO&PW-set scores}
###############################################################################
# debug #
# #
###############################################################################
tryCatch({
GO_Scores_DF <- get_Geneset_Score(
goGeneSet,
expr,
pickMethod = params$pickMethod,
log = params$log,
exp = params$exp,
calOrder = params$calOrder
)
info(logger, paste("GO共计算了", length(rownames(GO_Scores_DF)), "个基因集"))
PW_Scores_DF <- get_Geneset_Score(
pathwayGeneSet,
expr,
pickMethod = params$pickMethod,
log = params$log,
exp = params$exp,
calOrder = params$calOrder
)
info(logger, paste("Pathway共计算了", length(rownames(PW_Scores_DF)), "个基因集"))
}, warning=function(w){
log4r::warn(logger, w)
}, error=function(e){
log4r::error(logger, e)
})
# GO_Scores_DF <- get_Geneset_Score(
# goGeneSet,
# expr,
# pickMethod = params$pickMethod,
# log = params$log,
# exp = params$exp,
# calOrder = params$calOrder
# )
# info(logger, paste("GO共计算了", length(rownames(GO_Scores_DF)), "个基因集"))
#
#
# PW_Scores_DF <- get_Geneset_Score(
# pathwayGeneSet,
# expr,
# pickMethod = params$pickMethod,
# log = params$log,
# exp = params$exp,
# calOrder = params$calOrder
# )
# info(logger, paste("Pathway共计算了", length(rownames(PW_Scores_DF)), "个基因集"))
```
## Merge SignatureGenes and GO&KEGG data of Scores
```{r Merge Scores}
# merge SG&GO&PW scores dataframe
scores_DF <- rbind(SG_Scores_DF, GO_Scores_DF, PW_Scores_DF)
scores_DF <- na.omit(scores_DF)
scores_DF1 <- data.frame(t(scores_DF),stringsAsFactors=F)
# 转置之后数据变为矩阵,需要重新变成数据框,stringsAsFactors=F,否则下一步转数值型会出错
scores_DF <- as.data.frame(lapply(scores_DF1, as.numeric))
rownames(scores_DF) <- rownames(scores_DF1)
# scores_DF <- apply(scores_DF,2,as.numeric)
# 检查是否为数值型矩阵
if (class(scores_DF[1,1]) == "numeric"){
info(logger, "Signatures Scores为数值型矩阵")
} else {
error(logger, "错误!Signature Scores不是数值型矩阵!")
}
# class(scores_DF[1,1]) == list("numeric")
```
## Save Intermedian Data
```{r intermedian Data}
allinterdir <- paste(params$workdir, "\\intermedian\\", sep="")
if (!dir.exists(allinterdir)){dir.create(allinterdir)}
# interdir <- paste(unlist(strsplit(params$SignatureGenes_Name,split="_"))[1]
# , "_", params$exprType
# , "_Symbol[", params$GeneSet_Name_Normalized, "]"
# , "_", params$corAnalysis_Method
# , "_", params$calOrder
# , "_", params$pickMethod
# , "_Log2[", params$log, "]"
# , "_Exp[", params$exp, "]"
# , "\\"
# , sep="")
interdir <- paste(describe, "\\", sep="")
interdir <- paste(allinterdir, interdir, sep="")
if (!dir.exists(interdir)){
dir.create(interdir)
} else {
warn(logger, "已存在的中间数据被覆盖!")
}
# write.csv(SGscores, file.path(interdir, "Signature-Genes_Scores.csv"))
write.csv(scores_DF, file.path(interdir, "Signatures_Scores.csv"))
```
## Pearson correlation analysis
```{r pearson correlation}
info(logger, "Correlation method:\t", params$corAnalysis_Method)
# 计算相关性
tryCatch({
corAnalysis <- cor(scores_DF, method=params$corAnalysis_Method)
}, warning = function(w){
log4r::warn(logger, w)
}, error = function(e){
log4r::error(logger, e)
})
corAnalysis <- cor(scores_DF, method=params$corAnalysis_Method)
# corAnalysis <- cor(scores_DF, method=params$corAnalysis_Method)
correlationScores <- data.frame(Term=rownames(corAnalysis), Correlation_Score=corAnalysis[,1])
correlationScores <- na.omit(correlationScores)
# 降序排列
correlationScores <- correlationScores[order(correlationScores$Correlation_Score, decreasing = T),]
# output_fileName <- paste(unlist(strsplit(params$SignatureGenes_Name,split="_"))[1]
# , "-GO&PW"
# , "-Correlation"
# , "_[", params$exprType, "]"
# , "_Symbol[", params$GeneSet_Name_Normalized, "]"
# , "_[", params$corAnalysis_Method, "]"
# , "_calOrder[", params$calOrder, "]"
# , "_ScoreMethod-", params$pickMethod
# , "_Log2[", params$log, "]"
# , "_Exp[", params$exp, "]"
# , "_", Sys.Date()
# , ".csv", sep="")
output_fileName <- paste(describe,"_", Sys.Date(), ".csv", sep="")
# 保存相关系数矩阵
write.csv(correlationScores,
file.path(resultdir, output_fileName),
row.names=F)
info(logger, "Output Dir:\t", resultdir)
info(logger, "Output File:\t", output_fileName, "\n")
```