RegionSet & RegionSetList (R)
The gtars R package exposes S4 classes RegionSet and RegionSetList — the R-idiomatic equivalents of Bioconductor's GRanges and GRangesList, backed by a high-performance Rust implementation via externalptr.
Unlike most gtars bindings, the R API is polymorphic on input type: nearly every function that takes a query will accept a RegionSet, a GRanges object, a file path, or a data.frame interchangeably. This lets you mix existing Bioconductor workflows with gtars without converting explicitly.
For the underlying semantics see gtars-core (core types) and gtars-genomicdist (statistics, set algebra).
Loading the package
Section titled “Loading the package”library(gtars)RegionSet
Section titled “RegionSet”An S4 class representing a set of genomic regions. Internally wraps a Rust RegionSet pointer plus an R-side strand vector.
Construction
Section titled “Construction”RegionSet() accepts any of these inputs:
# 1. File path (BED, bed.gz, narrowPeak, etc.)rs <- RegionSet("peaks.bed")
# 2. GRanges object (requires GenomicRanges)library(GenomicRanges)gr <- GRanges(seqnames = c("chr1", "chr1"), ranges = IRanges(start = c(100, 500), end = c(200, 600)), strand = c("+", "-"))rs <- RegionSet(gr) # coords converted from 1-based closed to 0-based half-open
# 3. data.frame with chr/start/end (and optional strand)df <- data.frame(chr = c("chr1", "chr1"), start = c(100, 500), end = c(200, 600), strand = c("+", "-"))rs <- RegionSet(df)
# 4. An existing RegionSet (returned as-is)rs2 <- RegionSet(rs)as_regionset() is an alias for RegionSet().
Basic S4 methods
Section titled “Basic S4 methods”length(rs) # number of regionsshow(rs) # pretty-print summary (first 5 regions)rs # same — calls show()
as.data.frame(rs) # convert to a data.frame with chr/start/end/strandrs[1:10] # subset by index (numeric, logical, or character)rs[as.data.frame(rs)$strand == "+"] # filter by strandConverting to GRanges
Section titled “Converting to GRanges”gr <- as_granges(rs)# Coordinates converted from 0-based half-open to 1-based closed.# Strand information is preserved.Requires the GenomicRanges package to be installed.
Statistics methods
Section titled “Statistics methods”All of these accept RegionSet, GRanges, file path, or data.frame as input:
widths(rs) # numeric vector of region widths (end - start)neighborDistances(rs) # signed gaps between consecutive regions per chromosomenearestNeighbors(rs) # per-region min neighbor distancechromosomeStatistics(rs) # data.frame, one row per chromosome
distribution(rs, nBins = 250) # bin counts across the genomedistribution(rs, nBins = 250, chromSizes = hg38) # with reference chrom sizes
clusterRegions(rs, maxGap = 5000L) # cluster id per regionInterval set algebra
Section titled “Interval set algebra”R gtars overrides the standard Bioconductor generics (union, intersect, setdiff, reduce, promoters, shift, flank, resize, narrow, disjoin, gaps, findOverlaps, countOverlaps) plus adds gtars-specific ones (trim, pintersect, concat, jaccard).
The unary methods (reduce, trim, promoters, shift, flank, resize, narrow, disjoin, gaps) dispatch to gtars for a RegionSet, file path, or data.frame. A GRanges input keeps its own IRanges method and returns a GRanges; wrap it in RegionSet() first to use gtars.
merged <- reduce(rs)trimmed <- trim(rs, chromSizes = hg38)proms <- promoters(rs, upstream = 2000L, downstream = 200L)shifted <- shift(rs, shift = 100L)resized <- resize(rs, width = 500L, fix = "center")disjoint <- disjoin(rs)gapped <- gaps(rs, chrom_sizes = hg38)
# Binary operationsu <- union(rs1, rs2)i <- intersect(rs1, rs2)d <- setdiff(rs1, rs2)pi <- pintersect(rs1, rs2) # pairwise (by index)c <- concat(rs1, rs2)
j <- jaccard(rs1, rs2) # scalar Jaccard similarity
# Overlap querieshits <- findOverlaps(rs1, rs2)counts <- countOverlaps(rs1, rs2)Methods dispatch on ("RegionSet", "RegionSet"), ("RegionSet", "ANY"), and ("ANY", "RegionSet") so you can mix in GRanges, file paths, or data.frames on either side:
# Works — second argument is a file pathj <- jaccard(rs, "other_peaks.bed")
# Works — first argument is a GRangesmerged <- union(gr, rs)Consensus regions
Section titled “Consensus regions”cons <- consensus(list(rep1_rs, rep2_rs, rep3_rs))# Returns a data.frame with chr, start, end, count columns.# 'count' is the number of input sets overlapping each union region.
# Keep regions present in ≥ 2/3 replicatesrobust <- cons[cons$count >= 2, ]RegionSetList
Section titled “RegionSetList”An S4 class for collections of RegionSets — the gtars equivalent of GRangesList. Provides the single most efficient path for operating on many region sets at once, since pointers are passed between R and Rust without copying.
Construction
Section titled “Construction”# Variadic: any mix of RegionSet / file path / GRanges / data.framersl <- RegionSetList( RegionSet("rep1.bed"), RegionSet("rep2.bed"), RegionSet("rep3.bed"),)
# Equivalent: single list argumentrsl <- RegionSetList(list(rs1, rs2, rs3))
# Or directly from file paths — each is auto-wrapped via RegionSet()rsl <- RegionSetList("rep1.bed", "rep2.bed", "rep3.bed")
# Empty listempty <- RegionSetList()S4 methods
Section titled “S4 methods”length(rsl) # number of region setsshow(rsl) # pretty-print with sizesrsl[[1]] # extract a single RegionSet by index (1-based)names(rsl) # character vector of names, or NULLOperations
Section titled “Operations”# Flatten into a single RegionSet (no merge/dedup)flat <- concat(rsl)# Apply reduce() on the result if you want the unionmerged_all <- reduce(concat(rsl))
# Full N x N Jaccard similarity matrixjac <- pairwise_jaccard(rsl)# Returns a symmetric numeric matrix with 1.0 on the diagonal.Coordinate-system notes
Section titled “Coordinate-system notes”| system | representation | gtars handling |
|---|---|---|
| BED / gtars | 0-based half-open | internal — always used by RegionSet |
| GRanges / IRanges | 1-based closed | converted in both directions by RegionSet(gr) / as_granges(rs) |
| data.frame input | 0-based half-open | passed through as-is — make sure your start/end columns are BED-style |
If you're working entirely in GRanges-land, use as_granges() to convert back before handing off to Bioconductor workflows. If you're reading BED files and writing results back to BED, stay in RegionSet to avoid double conversion.
See also
Section titled “See also”- R GenomicDistributions wrappers —
calcWidth,calcGCContent,calcPartitions, etc. — the drop-in API for users migrating from R GenomicDistributions. - R LOLA interface —
loadRegionDB,runLOLA,checkUniverseAppropriateness, etc. - R IGD interface —
igd_create/igd_searchfor low-level IGD access. - gtars-core — the underlying Rust types.
- Core models tour — Python + Rust walkthrough of the same concepts.