Thanks @rodd. This was a simple answer to my immediate question and it was very helpful! I wasn't very familiar with biomaRt but I definitely should be.
Dear all,
I would like to have help with getting just protein_coding genes from gene expression file using biomart. What I have is a file of all genes expression for mouse (mm10) with ensemble gene_names, and I need to get ride from other non-coding and pseudogene.
2 answers
To obtain all protein coding genes from chr 1-22, in an object called genes in R, using biomaRt, for example:
library(biomaRt)
mart <- useMart(biomart = "ENSEMBL_MART_ENSEMBL", dataset = "hsapiens_gene_ensembl", host = 'www.ensembl.org')
genes <- biomaRt::getBM(attributes = c("external_gene_name", "chromosome_name","transcript_biotype"), filters = c("transcript_biotype","chromosome_name"),values = list("protein_coding",c(1:22)), mart = mart)
You can just remove the chromosome_name filter and the 1:22 vector.
You can go to Ensembl Biomart, and select the following attributes in the Gene section: Gene type, Transcript type. "protein-coding" is the one you want. Just do something like grep "protein_coding" biomartResults.txt and you should be set.
I already have my file with all genes on it, and I want to use R to get the protein_coding genes only from my file.
You can really use anything for that. If you want to do it in R:
library(biomaRt)
setwd("~/Downloads/")
shouldImport=TRUE
saveFile="proteinCodingMouseGenes.Rda"
if (!shouldImport || !file.exists(saveFile)){
print("Querying Biomart for protein coding genes")
ensembl=useMart("ensembl")
ensemblMouse = useDataset("mmusculus_gene_ensembl",mart=ensembl)
mouseProteinCodingGenes = getBM(attributes=c("ensembl_gene_id","external_gene_name","description"), filters='biotype', values=c('protein_coding'), mart=ensemblMouse)
save(mouseProteinCodingGenes,file=saveFile)
} else {
print("Loading genes from savefile")
load(saveFile)
}
The only useful part is the one about ensembl, the rest just saves the result of your Biomart query to a file so it can be loaded again (querying Biomart takes a bit of time). biomaRt is the R library you want, you specify what mart you are using, request with getBM a list of the attributes of all the entries whose attributes in filters match the terms in values. listAttributes() does what it is called.
The rest is just dataframe manipulation.
I run this your R code and it worked, but we have to change ensembl=useMart("ensembl") with
myMart <- useMart("ENSEMBL_MART_ENSEMBL",dataset="mmusculus_gene_ensembl", host="www.ensembl.org")
because there are some changes in Ensembl proxy. the issue that I have that I couldn't read .Rda file. Is there any way to save this file as text, because what I need to do is using merge function in R to merge this file with my file to get only protein_coding genes in mine.
Thanks. Worked perfectly!
Log in to answer this question.