-
Notifications
You must be signed in to change notification settings - Fork 0
/
setReadable_GENENAME.R
96 lines (81 loc) · 2.54 KB
/
setReadable_GENENAME.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
## This is a quickfix for S. cerevisiae functional analysis
##' 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 (!'GENENAME' %in% columns(OrgDb)) {
warning("Fail to convert input geneID to GENENAME since no GENENAME 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) {
genes <- unique(unlist(x@geneClusters))
} else {
genes <- x@gene
}
#gn <- EXTID2NAME(OrgDb, genes, keyType)
pre_gn <- select(org.Sc.sgd.db, genes, c("GENENAME"), keyType)
gn <- pre_gn$GENENAME
names(gn) <- pre_gn$ENTREZID
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) {
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)
}