View source: R/integrate_granges.R
| integrate_granges | R Documentation |
Combines the output of tm_calculate (a GRanges object with
Tm and GC columns) with a second GRanges carrying
arbitrary multi-omic metadata (ChIP-seq peaks, ATAC-seq signal, methylation
sites, gene annotations, etc.) using one of four positional strategies:
"overlap"Each tm range is annotated with the aggregated metadata of all feature ranges it directly overlaps.
"nearest"Each tm range is annotated with the metadata of its single closest feature range, plus an added distance column.
"window"Each tm range is expanded symmetrically by
window_size bp and annotated with aggregated metadata from all
features that fall within the expanded window.
"bin"The genomic space covered by the data is tiled into equal-width bins. Each bin is annotated with the mean tm / GC of overlapping tm ranges and the aggregated feature values - suitable for joint heatmaps and genome-wide correlation analyses.
For strategies "overlap" and "window", when a single Tm range
matches multiple features the default behaviour is to summarise:
numeric columns are aggregated via agg_fun (default mean),
and categorical columns are collapsed to a comma-separated string of unique
values.
integrate_granges(
gr_tm,
gr_features,
strategy = c("overlap", "nearest", "window", "bin"),
feature_cols = NULL,
prefix = "",
window_size = 1000L,
bin_size = 1e+06,
agg_fun = mean,
min_overlap = 1L,
ignore_strand = TRUE,
keep_unmatched = TRUE,
distance_col = "distance_to_feature"
)
gr_tm |
A |
gr_features |
A |
strategy |
Character. Integration strategy. One of
|
feature_cols |
Character vector. Names of metadata columns in
|
prefix |
Character. Prefix prepended to transferred column names to
avoid clashes with existing columns in |
window_size |
Integer. Half-width (bp) of the symmetric window added
around each Tm range in |
bin_size |
Integer. Width (bp) of genomic bins in |
agg_fun |
Function. Applied to numeric feature values when multiple
features map to the same Tm range / bin. Must accept a numeric vector and
an |
min_overlap |
Integer. Minimum overlap in base pairs required between
a Tm range and a feature range in |
ignore_strand |
Logical. If |
keep_unmatched |
Logical. In |
distance_col |
Character. Name of the distance column added in
|
"overlap", "nearest", "window": A
GRanges object with the same ranges as gr_tm (minus
unmatched ranges if keep_unmatched = FALSE), with additional
metadata columns from gr_features.
"bin": A new GRanges of genomic bins. Each bin
carries Tm_mean, GC_mean (if available),
n_tm_ranges, n_features, and one aggregated column per
requested feature column.
Junhui Li
## Not run:
library(GenomicRanges)
# -- Sample data ----------------------------------------------------------
set.seed(42)
gr_tm <- GRanges(
seqnames = c(rep("chr1", 60), rep("chr2", 30)),
ranges = IRanges(
start = c(sort(sample(1:249e6, 60)),
sort(sample(1:243e6, 30))),
width = sample(50:200, 90, replace = TRUE)
),
Tm = runif(90, 55, 85),
GC = runif(90, 30, 70)
)
gr_features <- GRanges(
seqnames = c(rep("chr1", 40), rep("chr2", 20)),
ranges = IRanges(
start = c(sort(sample(1:249e6, 40)),
sort(sample(1:243e6, 20))),
width = sample(500:5000, 60, replace = TRUE)
),
score = runif(60, 0, 100),
peak_type = sample(c("narrow", "broad"), 60, replace = TRUE),
signal = rnorm(60, 5, 2)
)
# Strategy 1: overlap - annotate Tm ranges with overlapping peak features
res_overlap <- integrate_granges(gr_tm, gr_features,
strategy = "overlap")
# Strategy 2: nearest - every Tm range gets its closest peak + distance
res_nearest <- integrate_granges(gr_tm, gr_features,
strategy = "nearest")
head(res_nearest$distance_to_feature)
# Strategy 3: window - 5 kb window around each probe
res_window <- integrate_granges(gr_tm, gr_features,
strategy = "window", window_size = 5000)
# Strategy 4: bin - 500 kb genome bins with mean Tm and aggregated signal
res_bin <- integrate_granges(gr_tm, gr_features,
strategy = "bin", bin_size = 5e5)
as.data.frame(res_bin) |> head()
# Use a subset of feature columns and add a prefix
integrate_granges(gr_tm, gr_features,
strategy = "overlap",
feature_cols = c("score", "peak_type"),
prefix = "chip_")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.