-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsetReadable.R
121 lines (89 loc) · 2.83 KB
/
setReadable.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
##' mapping geneID to gene Symbol
##'
##'
##' @title setReadable
##' @param x enrichResult Object
##' @param OrgDb OrgDb
##' @param keyType keyType of gene
##' @return enrichResult Object
##' @author Yu Guangchuang
##' @export
setReadable <- function(x, OrgDb, keyType="auto") {
OrgDb <- load_OrgDb(OrgDb)
if (!'SYMBOL' %in% columns(OrgDb)) {
warning("Fail to convert input geneID to SYMBOL since no SYMBOL information available in the provided OrgDb...")
}
if (!(is(x, "enrichResult") || is(x, "groupGOResult") || is(x, "gseaResult") || is(x,"compareClusterResult")))
stop("input should be an 'enrichResult' , 'gseaResult' or 'compareClusterResult' object...")
isGSEA <- FALSE
isCompare <- FALSE
if (is(x, 'gseaResult'))
isGSEA <- TRUE
if (is(x, 'compareClusterResult'))
isCompare <- TRUE
if (keyType == "auto") {
keyType <- x@keytype
if (keyType == 'UNKNOWN') {
stop("can't determine keyType automatically; need to set 'keyType' explicitly...")
}
}
if (x@readable)
return(x)
gc <- geneInCategory(x)
if (isGSEA) {
genes <- names(x@geneList)
} else if (isCompare) {
if ("core_enrichment" %in% colnames(as.data.frame(x))) {
geneslist <- x@geneClusters
names(geneslist) <- NULL
genes <- unique(names(unlist(geneslist)))
} else {
genes <- unique(unlist(x@geneClusters))
}
} else {
genes <- x@gene
}
gn <- EXTID2NAME(OrgDb, genes, keyType)
if(isCompare) {
gc2 <- list()
k <- 1
for(i in seq_len(length(gc))) {
for(j in seq_len(length(gc[[i]]))) {
gc2[[k]] <- gc[[i]][[j]]
names(gc2)[k] <- paste(names(gc)[[i]], names(gc[[i]])[j], sep="-")
k <- k + 1
}
}
gc <- gc2
gc <- lapply(gc, function(i) gn[i])
res <- x@compareClusterResult
gc <- gc[paste(res$Cluster, res$ID, sep= "-")]
} else {
gc <- lapply(gc, function(i) gn[i])
res <- x@result
gc <- gc[as.character(res$ID)]
}
## names(gc) should be identical to res$ID
## gc <- gc[as.character(res$ID)]
geneID <- sapply(gc, paste0, collapse="/")
# if (isGSEA) {
if ("core_enrichment" %in% colnames(as.data.frame(x))) {
res$core_enrichment <- unlist(geneID)
} else {
res$geneID <- unlist(geneID)
}
x@gene2Symbol <- gn
x@keytype <- keyType
x@readable <- TRUE
if(isCompare){
x@compareClusterResult <- res
} else {
x@result <- res
}
return(x)
}
# geneInCategory2 <- function(x){
# setNames(strsplit(geneID(x), "/", fixed=TRUE),
# paste(x@compareClusterResult$Cluster,
# x@compareClusterResult$ID, sep= "-"))
# }