Coerce a column to a GRanges object from a rectangular object

colToRanges(x, ...)

# S4 method for class 'DataFrame'
colToRanges(x, var, seqinfo = NULL, ...)

# S4 method for class 'GRanges'
colToRanges(x, var, ..., keep_metadata = TRUE)

# S4 method for class 'data.frame'
colToRanges(x, var, seqinfo = NULL, ...)

Arguments

x

A data-frame or GRanges object containing the column to coerce

...

Used to pass arguments to lower-level functions

var

The name of the column to coerce

seqinfo

A seqinfo object to be applied to the new GRanges object. Ignored if the column is already a GRanges object

keep_metadata

logical(1) If the original object is a GRanges object, retain all metadata from the original ranges in the replaced ranges

Value

A GenomicRanges object

Details

Take a data.frame-like object and coerce one column to a GRanges object, setting the remainder as the mcols. A particularly useful application of this is when you have a GRanges object with one mcol being a secondary GRanges object.

Alternatively, if you have a data.frame with GRanges represented as a character column, this provides a simple method of coercion. In this case, no Seqinfo element will be applied to the GRanges element.

Examples

set.seed(73)
x <- GRanges(c("chr1:1-10", "chr1:6-15", "chr1:51-60"))
seqinfo(x) <- Seqinfo("chr1", 60, FALSE, "Example")
df <- data.frame(logFC = rnorm(3), logCPM = rnorm(3,8), p = 10^-rexp(3))
mcols(x) <- df
gr <- mergeByCol(x, col = "logCPM", pval = "p")
colToRanges(gr, "keyval_range")
#> GRanges object with 2 ranges and 7 metadata columns:
#>       seqnames    ranges strand | n_windows      n_up    n_down    logCPM
#>          <Rle> <IRanges>  <Rle> | <integer> <integer> <integer> <numeric>
#>   [1]     chr1      1-10      * |         2         1         1   7.87263
#>   [2]     chr1     51-60      * |         1         1         0   8.14117
#>            logFC         p     p_fdr
#>        <numeric> <numeric> <numeric>
#>   [1] -0.1450469  0.296375  0.440965
#>   [2]  0.0937974  0.440965  0.440965
#>   -------
#>   seqinfo: 1 sequence from Example genome