[40a513]: / ATAC / Functions / FindRegion.R

Download this file

30 lines (30 with data), 870 Bytes

 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
# convert region argument to genomic coordinates
# region can be a string, name of a gene, or GRanges object
FindRegion <- function(
object,
region,
sep = c("-", "-"),
assay = NULL,
extend.upstream = 0,
extend.downstream = 0
) {
if (!is(object = region, class2 = "GRanges")) {
# if separators are present in the string and we can convert the
# start to a number, assume we're using genomic coordinates
if (all(sapply(X = sep, FUN = grepl, x = region))) {
region <- StringToGRanges(regions = region, sep = sep)
} else {
region <- LookupGeneCoords(object = object, assay = assay, gene = region)
if (is.null(x = region)) {
stop("Gene not found")
}
}
}
region <- suppressWarnings(expr = Extend(
x = region,
upstream = extend.upstream,
downstream = extend.downstream
)
)
return(region)
}