### A RangesMapping encodes a mapping of a set of ranges to some other
## Conceptually, a RangesMapping is a matching of each query range to
## one or more elements in a subject. The geometry of the query range
## is then transformed according to that matching. Thus, this data
## class combines a Hits object with a set of transformed ranges.
## In IRanges, we do not have any tabular structure that links a space
## with an interval, except for RangedData. We could use a RangedData
## in place of this class, but that might be too low-level. Instead,
## we have an accessor for each, and support a coercion.
### The 'map' generic, methods of which produce instances of this class

representation(hits = "Hits", space = "Rle",
RangedData(ranges(from), space = space(from), as(hits(from), "DataFrame"))
setGeneric("map", function(from, to, ...) standardGeneric("map"))
setGeneric("pmap", function(from, to, ...) standardGeneric("pmap"))

## setMethod("excludePattern", "RDApplyParams", function(x) x@excludePattern)
##            function(x, ..., value) standardGeneric("excludePattern<-"))
## setReplaceMethod("excludePattern", "RDApplyParams", function(x, value) {
## Simple convenience constructor around RDApplyParams's initializer
## get the defaults from the class prototype
"apply parameters have duplicated names"
"'applyFun' does not take enough parameters"
"mismatch between names of 'applyParams' and formals of 'applyFun'"
## .valid.RDApplyParams.excludePattern <- function(x) {
##     "'excludePattern' must be a single, non-missing string"
"'simplify' must be FALSE for there to be a 'reducerFun'"
"there must be a 'reducerFun' for there to be 'reducerParams'"
"reducer parameters have duplicated names"
"'reducerFun' does not take enough parameters"
"mismatch b/w 'reducerParams' names and 'reducerFun' formals"
"'iteratorFun' must take at least two parameters"
"'iteratorFun' must take at least three parameters if one is 'simplify'"

representation(rangedData = "RangedData", applyFun = "function",
applyParams = "list", ##excludePattern = "character",
filterRules = "FilterRules", simplify = "logical",
reducerFun = "functionORNULL", reducerParams = "list",
prototype(applyFun = function(rd) NULL, simplify = FALSE,
setReplaceMethod("rangedData", "RDApplyParams", function(x, value) {
setGeneric("applyFun<-", function(x, ..., value) standardGeneric("applyFun<-"))
setReplaceMethod("applyFun", "RDApplyParams", function(x, value) {
setReplaceMethod("applyParams", "RDApplyParams", function(x, value) {
setReplaceMethod("filterRules", "RDApplyParams", function(x, value) {
setGeneric("simplify<-", function(x, ..., value) standardGeneric("simplify<-"))
setReplaceMethod("simplify", "RDApplyParams", function(x, value) {
setReplaceMethod("reducerFun", "RDApplyParams", function(x, value) {
setReplaceMethod("reducerParams", "RDApplyParams", function(x, value) {
setReplaceMethod("iteratorFun", "RDApplyParams", function(x, value) {
RDApplyParams <- function(rangedData, applyFun, applyParams, #excludePattern,
params <- new("RDApplyParams", applyFun = applyFun,
applyParams = applyParams, filterRules = filterRules,
simplify = simplify, reducerFun = reducerFun,
reducerParams = reducerParams, iteratorFun = iteratorFun)
params@rangedData <- rangedData ## set rangedData last for efficiency
else if (!("..." %in% names(formals))) {
if (length(formals) < (length(applyParams(x)) + 1))
if (!is.null(nms) && !all(nms[nchar(nms) > 0] %in% names(formals)))
else if (!is.null(reducerFun(x)) && simplify(x))
return("there must be a 'reducerFun' for there to be 'reducerParams'")
return("reducer parameters have duplicated names")
if (length(formals) < (length(reducerParams(x)) + 1))
return("'reducerFun' does not take enough parameters")
if (!is.null(nms) && !all(nms[nchar(nms) > 0] %in% names(formals)) &&
return("mismatch b/w 'reducerParams' names and 'reducerFun' formals")
else if ("simplify" %in% names(formals) && length(formals) < 3)