[40a513]: / Functions / IDConvert.R

Download this file

44 lines (38 with data), 1.6 kB

 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
#### convert gene ID
#author: Zhilin long
#time: 2021.1.19
#'@author Zhilin Long
#'@param gene Character vector, ID vector to be converted
#'@param fromType Character vector, initial ID type
#'@param toType Character vector, ID type to be converted
#'
#'@return data.frame has removed duplication and NA, and can be used directly
IDConvert <- function(genes,
method = c("clusterProfiler", "org.Hs.eg.db"),
fromType = c("ENTREZID", "SYMBOL", "ENSEMBL"),
toType = c("ENTREZID", "SYMBOL", "ENSEMBL")
){
require(org.Hs.eg.db)
require(clusterProfiler)
method <- match.arg(method)
fromType <- match.arg(fromType)
toType <- match.arg(toType)
if(method == "clusterProfiler"){
gene.df <- bitr(genes, fromType = fromType, toType = toType, OrgDb = "org.Hs.eg.db")
#By default, IDs that are not mapped will be filtered, that is, the entries will be reduced
}else{
gene.df <- mapIds(org.Hs.eg.db,
keys = genes,
column = toType,
keytype = fromType,
multiVals="first")
gene.df <- data.frame(ENSEMBL = names(gene.df), SYMBOL = gene.df)
colnames(gene.df) <- c(fromType, toType)
# If there is no map, it is NA, that is, the entry remains unchanged
gene.df <- na.omit(gene.df)
}
#remove duplicates
gene.df <- gene.df[!duplicated(gene.df[,2]),]
gene.df <- gene.df[!duplicated(gene.df[,1]),]
return(gene.df)
}