sw: Sliding Windows

View source: R/sw.R

swR Documentation

Sliding Windows


Applies a function over a matrix or a vector using sliding windows. sw is a generic function with a method for "DNAbin" matrices.


sw(x, width, step, ...)
## Default S3 method:
sw(x, width = 100, step = 50, POS = NULL,
   FUN = mean, out.of.pos = NA_real_, na.rm = TRUE, L = NULL, ...)
## S3 method for class 'DNAbin'
sw(x, width = 100, step = 50, FUN = GC.content,
   rowAverage = FALSE, quiet = TRUE, ...)
## S3 method for class 'sw'
plot(x, type = "l", xlab = "Position", x.scaling = 1,
     show.ranges = FALSE, col.ranges = "blue",
     lty.ranges = 1, lwd.ranges = 1, ...)



a vector or a matrix.


an integer giving the window width.


an integer giving the step separating successive windows.


a numeric vector giving the positions of the sites.


the function to be applied to the windows.


a logical value: if TRUE, FUN is applied over all rows of x; if FALSE (the default) FUN is applied to each row of x.


the values used for the sites which are not in POS.


option passed to FUN.


the length of the chromosome (or sequence). If not given, this is largest value in POS or the length of x if POS is not given.


a logical value: if FALSE, the progress of the calculations is printed.


the type of plotting (see plot.default).


the label under the x-axis.


the scaling of the x-axis.


a logical value specifying whether to show the ranges of the windows with horizontal segments (ignored with a warning if x is a matrix).

col.ranges, lty.ranges, lwd.ranges

arguments to modify the appearance of the above segments (see segments).


further arguments passed to and from methods.


FUN should return a single value.

x should be a matrix for the "DNAbin" method, or a vector for the default one.

For the default method, the vector x is expanded into a vector of length L (see above on how this value is found) and the positions which are not in POS are filled with the value given in out.of.pos. The resulting vector is then analysed with the function FUN which must have an option na.rm. If the function you want to use does not have this option, you can use something like FUN = function(x, na.rm = TRUE) foo(x[!is.na(x)]), replacing ‘foo’ by the name of your function. You may also include more control on the handling of missing data.


a matrix or a vector (if rowAverage = TRUE).


Emmanuel Paradis


sw(woodmouse, 200, 200)
sw(woodmouse, 200, 200, rowAverage = TRUE)

## to get the proportions of G:
foo <- function(x) base.freq(x)["g"]
sw(woodmouse, 200, 200, FUN = foo, rowAverage = TRUE)

## a simulated example with the default method:
x <- runif(100)
pos <- sort(sample(1e6, 100))
resx <- sw(x, w = 2e4, s = 5e3, POS = pos, L = 1e6)
plot(resx, show.ranges = TRUE, x.scaling = 1e6, xlab = "Position (Mb)")

pegas documentation built on March 7, 2023, 7:21 p.m.