This is another small R function to download updated Gene Ontology mappings for a set of Ensembl Gene IDs. I use it to obtain the Gene Ontology categories linked to each gene from the zebrafish Zv10 assembly when performing enrichment of a RNAseq in goseq.

I use this function to update the GO Mappings to Ensembl Gene IDs because he data in the amazing package is not up to date, and the EnsDb for Zebrafish used to update the gene lengths in the previous post does not include GO Data.

# Function downloadGOMapping
# Input: the a list of Ensembl IDs.
# Output: A dataframe with the GO mappings.
downloadGOMapping <- function(vectorOfIDs) {
  # print("Annotating using BioMaRt")
  zfishMart <- useMart("ensembl", dataset="drerio_gene_ensembl")
  goMapping <- getBM(attributes = c('ensembl_gene_id','go_id'), 
                     filters = 'ensembl_gene_id',
                     values = vectorOfIDs, 
                     mart = zfishMart)
  # Clean blank mappings
  goMapping <- goMapping[!goMapping$go_id=="",]

The zfishMart object here is the Ensembl biomart link for Danio Rerio. You can change this object to the species of the Ensembl DB that you are working on.