Just a quick warning: An alias can map to multiple Entrez Gene IDs.
So, your current solution assumes the first listed ID is correct (which may not be true).
# e.g. The alias "A1B" is assumed to map to "1" and not "6641"
mget("A1B", org.Hs.egALIAS2EG)
# $A1B
# [1] "1" "6641"
If you check out the help for ?org.Hs.egALIAS2EG
, you will see that it is never recommended to use Aliases or Symbols as primary gene identifiers.
## From the 'Details' section of the help:
# Since gene symbols are sometimes redundantly assigned in the literature,
# users are cautioned that this map may produce multiple matching results
# for a single gene symbol. Users should map back from the entrez gene IDs
# produced to determine which result is the one they want when this happens.
# Because of this problem with redundant assigment of gene symbols,
# is it never advisable to use gene symbols as primary identifiers.
Without manual curation, it is impossible to know which ID is "correct". Thus, your safest bet is to get all of the possible IDs and symbols for each alias in your table, while maintaining information about which ones were receptors and which were ligands:
# your example subset with "A1B" and "trash" added for complexity
myTable <- data.frame(
ReceptorGene = c("A1B", "ACVR2B", "ACVR2B", "ACVR2B", "ACVR2B", "AMHR2", "BLR1", "BMPR1A", "BMPR1A", "BMPR1A", "BMPR1A", "BMPR1A"),
LigandGene = c("trash", "INHA", "INHBA", "INHBB", "INHBC", "AMH", "SCYB13", "BMP10", "BMP15", "BMP2", "BMP3", "BMP4"),
stringsAsFactors = FALSE
)
# unlist and rename
my.aliases <- unlist(myTable)
names(my.aliases) <- paste(names(my.aliases), my.aliases, sep = ".")
# determine which aliases have a corresponding Entrez Gene ID
has.key <- my.aliases %in% keys(org.Hs.egALIAS2EG)
# replace Aliases with character vectors of all possible entrez gene IDs
my.aliases[has.key] <- sapply(my.aliases[has.key], function(x) {
eg.ids <- unlist(mget(x, org.Hs.egALIAS2EG))
symbols <- unlist(mget(eg.ids, org.Hs.egSYMBOL))
})
# my.aliases retains all pertinent information regarding the original alias
my.aliases[1:3]
# $ReceptorGene1.A1B
# 1 6641
# "A1BG" "SNTB1"
#
# $ReceptorGene2.ACVR2B
# 93
# "ACVR2B"
#
# $ReceptorGene3.ACVR2B
# 93
# "ACVR2B"
Once you know which Entrez Gene IDs are appropriate, you can store them as additional columns on your table.
myTable$receptor.id <- c("1", "93", "93", "93", "93", "269", "643", "657", "657", "657", "657", "657")
myTable$ligand.id <- c(NA, "3623", "3624", "3625", "3626", "268", "10563", "27302", "9210", "650", "651", "652")
Then, when you need to update to the latest symbol, you can just use the Entrez Gene IDs (which should never need to be updated).
has.key <- myTable$receptor.id %in% keys(org.Hs.egSYMBOL)
myTable$ReceptorGene[has.key] <- unlist(mget(myTable$receptor.id[has.key], org.Hs.egSYMBOL))
has.key <- myTable$ligand.id %in% keys(org.Hs.egSYMBOL)
myTable$LigandGene[has.key] <- unlist(mget(myTable$ligand.id[has.key], org.Hs.egSYMBOL))
head(myTable)
# ReceptorGene LigandGene receptor.id ligand.id
# 1 A1BG trash 1 <NA>
# 2 ACVR2B INHA 93 3623
# 3 ACVR2B INHBA 93 3624
# 4 ACVR2B INHBB 93 3625
# 5 ACVR2B INHBC 93 3626
# 6 AMHR2 AMH 269 268