a b/Functions/IDConvert.R
1
#### convert gene ID
2
#author: Zhilin long
3
#time: 2021.1.19
4
5
#'@author Zhilin Long
6
#'@param gene Character vector, ID vector to be converted
7
#'@param fromType Character vector, initial ID type
8
#'@param toType Character vector, ID type to be converted
9
#'
10
#'@return data.frame has removed duplication and NA, and can be used directly
11
12
13
IDConvert <- function(genes, 
14
                       method = c("clusterProfiler", "org.Hs.eg.db"),
15
                       fromType = c("ENTREZID", "SYMBOL", "ENSEMBL"), 
16
                       toType = c("ENTREZID", "SYMBOL", "ENSEMBL")
17
                       ){
18
    require(org.Hs.eg.db)
19
    require(clusterProfiler)
20
21
    method <- match.arg(method)
22
    fromType <- match.arg(fromType)
23
    toType <- match.arg(toType)
24
25
    if(method == "clusterProfiler"){
26
        gene.df <- bitr(genes, fromType = fromType, toType = toType, OrgDb = "org.Hs.eg.db")
27
        #By default, IDs that are not mapped will be filtered, that is, the entries will be reduced
28
    }else{
29
        gene.df <- mapIds(org.Hs.eg.db, 
30
                          keys = genes, 
31
                          column = toType, 
32
                          keytype = fromType,
33
                          multiVals="first")
34
        gene.df <- data.frame(ENSEMBL = names(gene.df), SYMBOL = gene.df)
35
        colnames(gene.df) <- c(fromType, toType)
36
        # If there is no map, it is NA, that is, the entry remains unchanged
37
        gene.df <- na.omit(gene.df) 
38
    }
39
40
    #remove duplicates
41
    gene.df <- gene.df[!duplicated(gene.df[,2]),]
42
    gene.df <- gene.df[!duplicated(gene.df[,1]),]
43
    return(gene.df)
44
}