/
makeGRangesFromEnsembl.R
206 lines (200 loc) · 6.21 KB
/
makeGRangesFromEnsembl.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#' Make genomic ranges (`GRanges`) from Ensembl
#'
#' Quickly obtain gene and transcript annotations from
#' [Ensembl](https://www.ensembl.org/) using
#' [AnnotationHub](https://bioconductor.org/packages/AnnotationHub/) and
#' [ensembldb](https://bioconductor.org/packages/ensembldb/).
#'
#' Simply specify the desired organism, using the full Latin name. For example,
#' we can obtain human annotations with `Homo sapiens`. Optionally, specific
#' Ensembl genome builds (e.g. `GRCh38`) and release versions (e.g. `87`) are
#' supported.
#'
#' Under the hood, this function fetches annotations from AnnotationHub using
#' the ensembldb package. AnnotationHub supports versioned Ensembl releases,
#' back to version 87.
#'
#' Genome build: use `"GRCh38"` instead of `"hg38"` for the genome build, since
#' we're querying Ensembl and not UCSC.
#'
#' @section Broad class definitions:
#'
#' For gene and transcript annotations, a `broadClass` column is added, which
#' generalizes the gene types into a smaller number of semantically-meaningful
#' groups:
#'
#' - `coding`.
#' - `noncoding`.
#' - `pseudo`.
#' - `small`.
#' - `decaying`.
#' - `ig` (immunoglobulin).
#' - `tcr` (T cell receptor).
#' - `other`.
#'
#' @section GRCh37 (hg19) legacy annotations:
#'
#' [makeGRangesFromEnsembl()] supports the legacy *Homo sapiens* GRCh37 (release
#' 75) build by internally querying the [EnsDb.Hsapiens.v75][] package.
#' Alternatively, the corresponding GTF/GFF file can be loaded directly from
#' GENCODE or Ensembl.
#'
#' [EnsDb.Hsapiens.v75]: https://bioconductor.org/packages/EnsDb.Hsapiens.v75/
#'
#' @name makeGRangesFromEnsembl
#' @note Updated 2023-12-05.
#'
#' @inheritParams AcidRoxygen::params
#' @inheritParams params
#'
#' @return `EnsemblGenes` or `EnsemblTranscripts`.
#'
#' @seealso
#' - [AnnotationHub](https://bioconductor.org/packages/AnnotationHub/).
#' - [ensembldb](https://bioconductor.org/packages/ensembldb/).
#' - `ensembldb::ensDbFromGff()`, `ensembldb::ensDbFromGtf()`.
#'
#' @examples
#' ## Get annotations from Ensembl via AnnotationHub query.
#' genes <- makeGRangesFromEnsembl(
#' organism = "Homo sapiens",
#' level = "genes"
#' )
#' summary(genes)
#' transcripts <- makeGRangesFromEnsembl(
#' organism = "Homo sapiens",
#' level = "transcripts"
#' )
#' summary(transcripts)
#'
#' ## Get annotations from specific EnsDb object or package.
#' ## > if (goalie::isInstalled("EnsDb.Hsapiens.v75")) {
#' ## > genes <- makeGRangesFromEnsDb(
#' ## > object = "EnsDb.Hsapiens.v75",
#' ## > level = "genes"
#' ## > )
#' ## > summary(genes)
#' ## > }
NULL
#' @describeIn makeGRangesFromEnsembl Obtain annotations from Ensembl by
#' querying AnnotationHub.
#' @export
makeGRangesFromEnsembl <-
function(organism,
level = c("genes", "transcripts"),
genomeBuild = NULL,
release = NULL,
ignoreVersion = FALSE,
extraMcols = TRUE) {
assert(
isFlag(ignoreVersion),
isFlag(extraMcols)
)
level <- match.arg(level)
alert(sprintf("Making {.cls %s} from Ensembl.", "GRanges"))
edb <- .getEnsDb(
organism = organism,
genomeBuild = genomeBuild,
release = release
)
gr <- makeGRangesFromEnsDb(
object = edb,
level = level,
ignoreVersion = ignoreVersion,
extraMcols = extraMcols
)
metadata(gr)[["call"]] <- tryCatch(
expr = standardizeCall(),
error = function(e) {
NULL
}
)
gr
}
#' @describeIn makeGRangesFromEnsembl Use a specific `EnsDb` object as the
#' annotation source. Alternatively, can pass in an EnsDb package name as
#' a `character(1)`.
#' @export
#'
#' @param object `EnsDb` or `character(1)`.
#' `EnsDb` object or name of specific annotation package containing a
#' versioned EnsDb object (e.g. "EnsDb.Hsapiens.v75").
makeGRangesFromEnsDb <-
function(object,
level = c("genes", "transcripts"),
ignoreVersion = FALSE,
extraMcols = TRUE) {
assert(
requireNamespaces("ensembldb"),
isFlag(ignoreVersion),
isFlag(extraMcols)
)
level <- match.arg(level)
alert(sprintf(
"Making {.cls %s} from {.cls %s}.",
"GRanges", "EnsDb"
))
if (isString(object)) {
package <- object
assert(requireNamespaces(package))
object <- get(
x = package,
envir = asNamespace(package),
inherits = FALSE
)
}
assert(is(object, "EnsDb"))
args <- list(
"x" = object,
"order.type" = "asc",
"return.type" = "GRanges"
)
quietly({
geneCols <- ensembldb::listColumns(object, "gene")
})
geneCols <- sort(unique(c(geneCols, "entrezid")))
quietly({
txCols <- ensembldb::listColumns(object, "tx")
})
txCols <- sort(unique(c(txCols, geneCols)))
switch(
EXPR = level,
"genes" = {
fun <- ensembldb::genes
args <- append(
x = args,
values = list(
"columns" = geneCols,
"order.by" = "gene_id"
)
)
},
"transcripts" = {
fun <- ensembldb::transcripts
args <- append(
x = args,
values = list(
"columns" = txCols,
"order.by" = "tx_id"
)
)
}
)
quietly({
gr <- do.call(what = fun, args = args)
})
assert(is(gr, "GRanges"))
metadata(gr) <- .getEnsDbMetadata(object = object, level = level)
gr <- .makeGRanges(
object = gr,
ignoreVersion = ignoreVersion,
extraMcols = extraMcols
)
metadata(gr)[["call"]] <- tryCatch(
expr = standardizeCall(),
error = function(e) {
NULL
}
)
gr
}