View source: R/make_genomiccoord.R
| make_genomiccoord | R Documentation |
Tiles one or more chromosomes from a BSgenome object into
overlapping windows and returns a named character vector of coordinate
strings in the format
"chr:start-end:strand:genome_pkg:region_id".
This vector is the primary input for downstream Tm calculation
functions (tm_nn, tm_gc, tm_wallace) that
accept genomic coordinate strings.
make_genomiccoord(
bsgenome,
chromosomes = NULL,
window = 200L,
slide = 50L,
start = NULL,
end = NULL,
strand = "+",
trim_N = c("ends", "filter", "none"),
max_N_frac = 0.1,
N_scan_block = window,
region_prefix = "region",
genome_pkg_name = NULL,
as_vector = TRUE,
verbose = TRUE
)
bsgenome |
A |
chromosomes |
Character vector of chromosome names to tile.
Must be present in |
window |
Integer. Width of each sliding window in base
pairs. Default |
slide |
Integer. Step size between consecutive window
starts in base pairs. |
start |
Integer or |
end |
Integer or |
strand |
Character. Strand label embedded in the
coordinate string. One of |
trim_N |
Character. N-base handling strategy. One of
|
max_N_frac |
Numeric in |
N_scan_block |
Integer. Block size (bp) for the coarse N-end
detection scan used by |
region_prefix |
Character. Prefix for region IDs embedded in
the coordinate string. Default |
genome_pkg_name |
Character or |
as_vector |
Logical. When |
verbose |
Logical. Print per-chromosome progress messages.
Default |
When as_vector = TRUE (default): a named character
vector of coordinate strings, one element per window. Names are
the region IDs (region1, region2, ...).
When as_vector = FALSE: a data.frame with columns
coord, chr, win_start, win_end,
strand, genome, region_id,
chr_start_used, chr_end_used.
Each element of the returned vector follows the pattern:
chr1:10001-10200:+:BSgenome.Hsapiens.UCSC.hg38:region1 chr1:10051-10250:+:BSgenome.Hsapiens.UCSC.hg38:region2 ...
Fields (colon-separated):
chromosome – e.g. chr1
start-end – 1-based, inclusive coordinates
strand – + or -
genome – BSgenome package name (character)
region_id – unique label regionN
Human (and most mammalian) chromosomes begin and end with long
stretches of N bases that represent assembly gaps. Windows
that consist entirely or predominantly of Ns produce
meaningless Tm values. The function offers two trimming strategies
controlled by trim_N:
"none"No trimming. Windows start at start
and end at end (or chromosome length).
"ends"Detect the first and last positions of
non-N bases on each chromosome using
Biostrings::letterFrequency() on a coarse block scan,
then trim start and end to those positions.
Efficient: reads the chromosome sequence only once in blocks.
"filter"Generate windows across the full
start-end range but then remove any window whose
N-base fraction exceeds max_N_frac (default 0.1, i.e.
10%). More granular than "ends" but slower because it
reads each window sequence.
Junhui Li
tm_nn, tm_gc,
tm_wallace, BSgenome,
letterFrequency
## Not run:
library(BSgenome.Hsapiens.UCSC.hg38)
## -- Basic usage: tile chr1 with 200 bp windows, 50 bp slide ----------
coords <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = "chr1",
window = 200L,
slide = 50L
)
length(coords) # number of windows on chr1
head(coords, 3)
# region1 "chr1:10001-10200:+:BSgenome.Hsapiens.UCSC.hg38:region1"
# region2 "chr1:10051-10250:+:BSgenome.Hsapiens.UCSC.hg38:region2"
# region3 "chr1:10101-10300:+:BSgenome.Hsapiens.UCSC.hg38:region3"
## -- Non-overlapping tiling (slide == window) -------------------------
coords_nonoverlap <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = paste0("chr", 1:22),
window = 200L,
slide = 200L # no overlap
)
length(coords_nonoverlap) # ~15 million windows across autosomes
## -- Custom start/end (e.g. a specific sub-region) --------------------
coords_sub <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = "chr1",
window = 200L,
slide = 50L,
start = 1000000L,
end = 2000000L
)
length(coords_sub) # 19,981 windows in 1 Mb region
## -- No N-trimming (use full chromosome length) ------------------------
coords_noN <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = "chr1",
window = 200L,
slide = 50L,
trim_N = "none"
)
## -- Per-window N-filtering (removes windows with >10% N) -------------
coords_filt <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = "chr1",
window = 200L,
slide = 50L,
trim_N = "filter",
max_N_frac = 0.10
)
## -- Get data.frame output for GRanges construction -------------------
df <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = "chr1",
window = 200L,
slide = 50L,
as_vector = FALSE
)
gr <- GenomicRanges::GRanges(
seqnames = df$chr,
ranges = IRanges::IRanges(start = df$win_start, end = df$win_end),
strand = df$strand
)
## -- Pass directly to tm_nn -------------------------------------------
coords <- make_genomiccoord(
bsgenome = BSgenome.Hsapiens.UCSC.hg38,
chromosomes = "chr1",
window = 200L,
slide = 200L,
start = 1000000L,
end = 1010000L
)
tm_results <- tm_nn(coords, Na = 50)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.