This is a generalization of GenomicRanges::binnedAverage to allow the use of any applicable function instead of simply the mean. The interface is very similar, with two differences:

  1. Added argument fun, for the function to use.

  2. Dropped argument varname, since this function outputs the metadata as a vector, rather than the complete GRanges object with the metadata column added and named as indicated by varname.

binned_function(bins, numvar, fun, ...)

Arguments

bins

GRanges object representing the genomic bins. Typically obtained by calling tileGenome with cut.last.tile.in.chrom=TRUE. No default.

numvar

Named RleList object representing a numerical variable defined along the genome covered by bins (which is the genome described by seqinfo(bins)). No default.

fun

R function to apply to the numerical variable in each bin. No default.

...

optional arguments to fun.

Examples

# NOT RUN {
# Generate 100-bp genome tiles for a given GRanges object
bins <- GenomicRanges::tileGenome(GenomeInfoDb::seqlengths(gr),
                                  tilewidth=100, cut.last.tile.in.chrom=TRUE)

# Get signal as "RleList"; stored in the "score" metadata column
score <- GenomicRanges::coverage(gr, weight="score")

# Now use the function to get the maximum score in each tile
gr$max_per_bin <- binned_function(bins=bins, numvar=score, fun=max)

# You can pass arguments to fun too
gr$max_per_bin <- binned_function(bins=bins, numvar=score,
                                  fun=max, na.rm=TRUE)
# }