### The Views virtual class is a general container for storing a set of views
### on an arbitrary Vector object, called the "subject".
"ranges setter for Views objects not ready yet"
### Methods derived from the IRanges interface.
### TODO: - add a 'check.limits' arg (default to TRUE) for raising an error if
###         some views are "out of limits".
"'end' and 'width' must be NULLs when 'start' is a Ranges object"
## Keep the names that are already in 'ranges' unless the 'names' arg
### The user-friendly "Views" constructor.
### TODO: Same as for the newViews() function above.
### Returns a single view covering the entire sequence.
### Unfortunately, even if we've already defined the IRanges->NormalIRanges
### "coerce" method to override the silly implicit one, we still need to
### define the <class>->NormalIRanges ones for every <class> that contains
### IRanges. Otherwise, again, 'as(x, "NormalIRanges")' would call another
### silly implicit method when 'x' is a <class> instance.
"all Views objects to combine must have the same subject"
###         some views are "out of limits"

setGeneric("ranges<-", function(x, ..., value) standardGeneric("ranges<-"))
stop("ranges setter for Views objects not ready yet")
setMethod("start", "Views", function(x, ...) start(ranges(x)))
setMethod("end", "Views", function(x, ...) end(ranges(x)))
x@ranges <- `start<-`(ranges(x), check=check, value)
x@ranges <- `end<-`(ranges(x), check=check, value)
x@ranges <- `width<-`(ranges(x), check=check, value)
if (length(width(x)) != 0L && min(width(x)) <= 0L)
newViews <- function(subject, start=NULL, end=NULL, width=NULL, names=NULL,
stop("'end' and 'width' must be NULLs when 'start' is a Ranges object")
ranges <- IRanges(start=start, end=end, width=width, names=names)
Class <- paste(class(subject), "Views", sep="")
setMethod("as.matrix", "Views", function(x, rev = FALSE, max.width = NA) {
x_ranges <- restrict(ranges(x), start = 1L)
ord <- mseq(ifelse(rev, end(part), start(part)),
v_fill <- rep.int(NA, max.width * length(x))
i <- as.integer(IRanges(start(part), width = width(x_ranges)))
matrix(v_fill, ncol = max.width, byrow = TRUE,
if (start < 1L || end > length(subject(x)))
stop("all Views objects to combine must have the same subject")
x@ranges <- do.call(c, lapply(args, ranges))
x@ranges <- restrict(ranges(x), start=1L, end=length(subject(x)),
trim(narrow(x, start=start, end=end, width=width, use.names=use.names))
successiveViews <- function(subject, width, gapwidth=0, from=1)
ranges <- successiveIRanges(width, gapwidth=gapwidth, from=from)
function(X, FUN, ..., simplify = TRUE) standardGeneric("viewApply")
ans <- newList("SimpleList", ans, metadata = metadata(X),
function(x, na.rm = FALSE) standardGeneric("viewMins"))
function(x, na.rm = FALSE) standardGeneric("viewMaxs"))
function(x, na.rm = FALSE) standardGeneric("viewSums"))
function(x, na.rm = FALSE) standardGeneric("viewMeans"))
function(x, na.rm = FALSE) standardGeneric("viewWhichMins"))
function(x, na.rm = FALSE) standardGeneric("viewWhichMaxs"))
function(x, na.rm = FALSE) standardGeneric("viewRangeMaxs"))
function(x, na.rm = FALSE) standardGeneric("viewRangeMins"))
setMethod("Summary", "Views", function(x, ..., na.rm = FALSE) {
viewSummaryFunMap <- list(min = viewMins, max = viewMaxs, sum = viewSums)
stop("Passing multiple arguments to '", .Generic, "' is not supported.")