Learn R Programming

pegas (version 1.3)

sw: Sliding Windows

Description

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

Usage

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

Value

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

Arguments

x

a vector or a matrix.

width

an integer giving the window width.

step

an integer giving the step separating successive windows.

POS

a numeric vector giving the positions of the sites.

FUN

the function to be applied to the windows.

rowAverage

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.

out.of.pos

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

na.rm

option passed to FUN.

L

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.

quiet

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

type

the type of plotting (see plot.default).

xlab

the label under the x-axis.

x.scaling

the scaling of the x-axis.

show.ranges

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.

Author

Emmanuel Paradis

Details

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.

Examples

Run this code
data(woodmouse)
sw(woodmouse)
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)")

Run the code above in your browser using DataLab