I am pulling this from Theresa’s anxontrapR_pipeline.Rmd, primarily
because it looks similar to the other documents, but was modified more
recently. I will change it slightly, primarily because I grabbed a new
mmusculus assembly and therefore I will pull the mmusculus annotations
from a specific biomart (@smedleyBioMartBiologicalQueries2009) archive
that should match it.
A note from the future: multiple ensembl archive servers have been
taken offline since last I ran this. Let us see if Feb. 2023 still
works.
An important
note!
In the recent past, ensembl queries have become inconsistent, failing
much more often than ever in the past. I do not think this is the fault
of ensembl; but I think I need a fallback mechanism for collecting
annotation information.
In the case of ensembl, it should be trivial (but less fun) to use a
combination of the locally installed orgdb and txdb databases.
This does open a risk that the set of genes with annotations will be
different depending on when the container is run due to differences
between the orgdb/txdb instance and the Feb 2023 biomart. I am not sure
there is much I can do about that except to bundle the set of
annotations I downloaded in the container – since
load_biomart_annotations() does save a rda copy of its download.
ok, I did both. If you, dear reader, wish to download your own
annotations, and ensembl is having troubles, the following should work
without a problem; in addition the rda annotations are in /data of the
container and should get loaded.
tx_gene_map <- data.frame()
mm_annot <- try(load_biomart_annotations(species = "mmusculus", host = "useast.ensembl.org",
archive = FALSE, overwrite = FALSE,
gene_requests = annotation_columns))
## The biomart annotations file already exists, loading from it.
fields <- c("ACCNUM", "ENSEMBL", "ENSEMBLTRANS", "ENTEZID", "GENENAME", "SYMBOL")
orgdb_annot <- load_orgdb_annotations("org.Mm.eg.db", fields = fields)
## Loading required package: AnnotationDbi
## Loading required package: stats4
## Loading required package: BiocGenerics
## Loading required package: generics
##
## Attaching package: 'generics'
## The following object is masked from 'package:dplyr':
##
## explain
## The following objects are masked from 'package:base':
##
## as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
## setequal, union
##
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:hpgltools':
##
## IQR, conditions, conditions<-, mad, sd, var
## The following object is masked from 'package:dplyr':
##
## combine
## The following objects are masked from 'package:stats':
##
## IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
##
## Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
## as.data.frame, basename, cbind, colnames, dirname, do.call,
## duplicated, eval, evalq, get, grep, grepl, is.unsorted, lapply,
## mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
## rank, rbind, rownames, sapply, saveRDS, table, tapply, unique,
## unsplit, which.max, which.min
## Loading required package: Biobase
## Welcome to Bioconductor
##
## Vignettes contain introductory material; view with
## 'browseVignettes()'. To cite Bioconductor, see
## 'citation("Biobase")', and for packages 'citation("pkgname")'.
##
## Attaching package: 'Biobase'
## The following objects are masked from 'package:hpgltools':
##
## exprs<-, notes, pData<-, sampleNames<-
## Loading required package: IRanges
## Loading required package: S4Vectors
##
## Attaching package: 'S4Vectors'
## The following objects are masked from 'package:dplyr':
##
## first, rename
## The following object is masked from 'package:utils':
##
## findMatches
## The following objects are masked from 'package:base':
##
## I, expand.grid, unname
##
## Attaching package: 'IRanges'
## The following objects are masked from 'package:dplyr':
##
## collapse, desc, slice
##
## Attaching package: 'AnnotationDbi'
## The following object is masked from 'package:dplyr':
##
## select
##
## Unable to find CDSNAME, setting it to GENENAME.
## Unable to find GENE_TYPE, setting it to GENETYPE.
## Unable to find CDSCHROM in the db, removing it.
## Unable to find CDSSTRAND in the db, removing it.
## Unable to find CDSSTART in the db, removing it.
## Unable to find CDSEND in the db, removing it.
## Some requested columns are not available: ENTEZID.
## The following are available: ACCNUM, ALIAS, ENSEMBL, ENSEMBLPROT, ENSEMBLTRANS, ENTREZID, ENZYME, EVIDENCE, EVIDENCEALL, GENENAME, GENETYPE, GO, GOALL, IPI, MGI, ONTOLOGY, ONTOLOGYALL, PATH, PFAM, PMID, PROSITE, REFSEQ, SYMBOL, UNIPROT
## Extracted all gene ids.
## Attempting to select: GENENAME, GENETYPE, ACCNUM, ENSEMBL, ENSEMBLTRANS, GENENAME, SYMBOL
## 'select()' returned 1:many mapping between keys and columns
gene_info <- orgdb_annot[["genes"]]
## Note, there are a bunch of variants of the txdb package one might use.
## I do not think it matters a lot for our purposes, but I suspect that if we used
## a mismatched BSgenome and tried to pull CDS sequences, that might end badly.
pkg <- "TxDb.Mmusculus.UCSC.mm10.knownGene"
tx_annot <- load_txdb_annotations(pkg)
## Error in `library()`:
## ! there is no package called 'TxDb.Mmusculus.UCSC.mm10.knownGene'
transcripts <- tx_annot[["TX"]]
## Error:
## ! object 'tx_annot' not found
transcripts[["tx"]] <- gsub(x = transcripts[["TXNAME"]],
pattern = "\\.\\d+$", replacement = "")
## Error in `h()`:
## ! error in evaluating the argument 'x' in selecting a method for function 'gsub': object 'transcripts' not found
mm_annot_orgdb <- merge(gene_info, transcripts, by.x = "ensembltrans", by.y = "tx")
## Error in `h()`:
## ! error in evaluating the argument 'y' in selecting a method for function 'merge': object 'transcripts' not found
rownames(mm_annot_orgdb) <- make.names(mm_annot_orgdb[["ensembl"]], unique = TRUE)
## Error:
## ! object 'mm_annot_orgdb' not found
mm_annot_combined <- merge(mm_annot[["annotation"]], mm_annot_orgdb, by.x = "ensembl_transcript_id", by.y = "ensembltrans", all.x = TRUE)
## Error in `h()`:
## ! error in evaluating the argument 'y' in selecting a method for function 'merge': object 'mm_annot_orgdb' not found
mm_annot_genes <- mm_annot_combined
## Error:
## ! object 'mm_annot_combined' not found
rownames(mm_annot_genes) <- make.names(mm_annot_genes[["ensembl_gene_id"]], unique = TRUE)
## Error:
## ! object 'mm_annot_genes' not found
mm_annot_tx <- mm_annot_combined
## Error:
## ! object 'mm_annot_combined' not found
rownames(mm_annot_tx) <- make.names(mm_annot_tx[["ensembl_transcript_id"]], unique = TRUE)
## Error:
## ! object 'mm_annot_tx' not found
tx_gene_map <- mm_annot_genes[, c("TXID", "ensembl_gene_id")]
## Error:
## ! object 'mm_annot_genes' not found