| 1 |
\name{clusters} |
| 2 |
|
| 3 |
\alias{clusters} |
| 4 |
\alias{clusters,numeric-method} |
| 5 |
\alias{clusters,Intervals_virtual-method} |
| 6 |
|
| 7 |
\title{Identify clusters in a collection of positions or intervals} |
| 8 |
|
| 9 |
\description{ |
| 10 |
This function uses tools in the \pkg{intervals} package to quickly |
| 11 |
identify clusters -- contiguous collections of positions or intervals |
| 12 |
which are separated by no more than a given distance from their |
| 13 |
neighbors to either side. |
| 14 |
} |
| 15 |
|
| 16 |
\usage{ |
| 17 |
\S4method{clusters}{numeric}(x, w, which = FALSE, check_valid = TRUE) |
| 18 |
|
| 19 |
\S4method{clusters}{Intervals_virtual}(x, w, which = FALSE, check_valid = TRUE) |
| 20 |
} |
| 21 |
|
| 22 |
\arguments{ |
| 23 |
|
| 24 |
\item{x}{An appropriate object.} |
| 25 |
|
| 26 |
\item{w}{ |
| 27 |
Maximum permitted distance between a cluster member and its |
| 28 |
neighbors to either side. |
| 29 |
} |
| 30 |
|
| 31 |
\item{which}{ |
| 32 |
Should indices into the \code{x} object be returned instead of |
| 33 |
actual subsets? |
| 34 |
} |
| 35 |
|
| 36 |
\item{check_valid}{ |
| 37 |
Should \code{\link{validObject}} be called before passing to |
| 38 |
compiled code? Also see \code{\link{interval_overlap}} and |
| 39 |
\code{\link{reduce}}. |
| 40 |
} |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
\details{ |
| 45 |
A cluster is defined to be a maximal collection, with at least two |
| 46 |
members, of components of \code{x} which are separated by no more than |
| 47 |
\code{w}. Note that when \code{x} represents intervals, an interval |
| 48 |
must actually \emph{contain a point} at distance \code{w} or less from |
| 49 |
a neighboring interval to be assigned to the same cluster. If the ends |
| 50 |
of both intervals in question are open and exactly at distance |
| 51 |
\code{w}, they will not be deemed to be cluster co-members. See the |
| 52 |
example below. |
| 53 |
} |
| 54 |
|
| 55 |
\note{ |
| 56 |
Implementation is by a call to \code{\link{reduce}} followed by a call |
| 57 |
to \code{\link{interval_overlap}}. The \code{clusters} methods are |
| 58 |
included to illustrate the utility of the core functions in the |
| 59 |
\pkg{intervals} package, although they are also useful in their own |
| 60 |
right. |
| 61 |
} |
| 62 |
|
| 63 |
\value{ |
| 64 |
A list whose components are the clusters. Each component is thus a |
| 65 |
subset of \code{x}, or, if \code{which == TRUE}, a vector of |
| 66 |
indices into the \code{x} object. (The indices correspond to row |
| 67 |
numbers when \code{x} is of class \code{"Intervals_virtual"}.) |
| 68 |
} |
| 69 |
|
| 70 |
\examples{ |
| 71 |
# Numeric method |
| 72 |
w <- 20 |
| 73 |
x <- sample( 1000, 100 ) |
| 74 |
c1 <- clusters( x, w ) |
| 75 |
|
| 76 |
# Check results |
| 77 |
sapply( c1, function( x ) all( diff(x) <= w ) ) |
| 78 |
d1 <- diff( sort(x) ) |
| 79 |
all.equal( |
| 80 |
as.numeric( d1[ d1 <= w ] ), |
| 81 |
unlist( sapply( c1, diff ) ) |
| 82 |
) |
| 83 |
|
| 84 |
# Intervals method, starting with a reduced object so we know that all |
| 85 |
# intervals are disjoint and sorted. |
| 86 |
B <- 100 |
| 87 |
left <- runif( B, 0, 1e4 ) |
| 88 |
right <- left + rexp( B, rate = 1/10 ) |
| 89 |
y <- reduce( Intervals( cbind( left, right ) ) ) |
| 90 |
|
| 91 |
gaps <- function(x) x[-1,1] - x[-nrow(x),2] |
| 92 |
hist( gaps(y), breaks = 30 ) |
| 93 |
|
| 94 |
w <- 200 |
| 95 |
c2 <- clusters( y, w ) |
| 96 |
head( c2 ) |
| 97 |
sapply( c2, function(x) all( gaps(x) <= w ) ) |
| 98 |
|
| 99 |
# Clusters and open end points. See "Details". |
| 100 |
z <- Intervals( |
| 101 |
matrix( 1:4, 2, 2, byrow = TRUE ), |
| 102 |
closed = c( TRUE, FALSE ) |
| 103 |
) |
| 104 |
z |
| 105 |
clusters( z, 1 ) |
| 106 |
closed(z)[1] <- FALSE |
| 107 |
z |
| 108 |
clusters( z, 1 ) |
| 109 |
} |