Skip to content

Instantly share code, notes, and snippets.

@FloWuenne
Last active March 15, 2023 15:45
Show Gist options
  • Save FloWuenne/f8fc922477df04c1642e9d8945c48d47 to your computer and use it in GitHub Desktop.
Save FloWuenne/f8fc922477df04c1642e9d8945c48d47 to your computer and use it in GitHub Desktop.
Translate Human gene symbols into Mouse symbols using bioMart
# Basic function to convert human to mouse gene names
convertHumanGeneList <- function(x){
require("biomaRt")
human = useMart("ensembl", dataset = "hsapiens_gene_ensembl")
mouse = useMart("ensembl", dataset = "mmusculus_gene_ensembl")
genesV2 = getLDS(attributes = c("hgnc_symbol"), filters = "hgnc_symbol", values = x , mart = human, attributesL = c("mgi_symbol"), martL = mouse, uniqueRows=T)
humanx <- unique(genesV2[, 2])
no_mouse_genes <- length(x)
no_human_genes <- length(humanx)
if(no_human_genes != no_mouse_genes){
print("Some genes could not be translated!")
genes_not_trans <- setdiff(x,genesV2$HGNC.symbol)
print("These genes could not be translated:")
print(genes_not_trans)
print(paste("A total number of ",length(genes_not_trans),"genes could not be translated!"),sep=" ")
}else{
print("All genes were translated successfully!")
}
# Print all gene names that could not be translated and the number of genes that were not translated
return(humanx)
}
Copy link

ghost commented May 20, 2020

Indeed, the function must return something, if only a NA. Otherwise, one cannot use it to convert a list of gene symbols and append them to a data-frame or replace the initial list of human genes.

@hyjforesight
Copy link

hyjforesight commented Jan 29, 2022

convert human to mouse gene names (https://gist.github.com/FloWuenne/f8fc922477df04c1642e9d8945c48d47)

humGenes <- c("HMMR", "TLX3", "CPEB4") # prepare a list of human genes needs to be converted

convertHumantoMouse <- function(x){

require("biomaRt")
human = useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl")
mouse = useEnsembl(biomart = "genes", dataset = "mmusculus_gene_ensembl")

genesV2 = getLDS(attributes = c("hgnc_symbol"), filters = "hgnc_symbol", values = x , mart = human, attributesL = c("mgi_symbol"), martL = mouse, uniqueRows=T)

mousex <- unique(genesV2[, 2]) # unique the 2nd column values

human_genes_number <- length(x)
mouse_genes_number <- length(mousex)

if(human_genes_number != mouse_genes_number){
genes_not_trans <- setdiff(x, genesV2$HGNC.symbol)
print("These genes could not be translated:")
print(genes_not_trans)
print(paste("A total number of ",length(genes_not_trans),"genes could not be translated!"),sep=" ")
}else{
print("All genes were translated successfully!")
}

return(mousex)
}

ConvertedGenes <- convertHumantoMouse(humGenes)

convert mouse gene to human gene (https://rjbioinformatics.com/2016/10/14/converting-mouse-to-human-gene-names-with-biomart-package/)

musGenes <- c("Hmmr", "Tlx3", "Cpeb4")

convertMousetohuman <- function(x){

require("biomaRt")
human = useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl")
mouse = useEnsembl(biomart = "genes", dataset = "mmusculus_gene_ensembl")

genesV2 = getLDS(attributes = c("mgi_symbol"), filters = "mgi_symbol", values = x , mart = mouse, attributesL = c("hgnc_symbol"), martL = human, uniqueRows=T)

humanx <- unique(genesV2[, 2])

mouse_genes_number <- length(x)
human_genes_number <- length(humanx)

if(mouse_genes_number != human_genes_number){
genes_not_trans <- setdiff(x, genesV2$MGI.symbol)
print("These genes could not be translated:")
print(genes_not_trans)
print(paste("A total number of ",length(genes_not_trans),"genes could not be translated!"),sep=" ")
}else{
print("All genes were translated successfully!")
}

return(humanx)
}

ConvertedGenes <- convertMousetohuman(musGenes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment