Created
November 12, 2014 16:22
-
-
Save aaronwolen/c5084e5e7b1f16447789 to your computer and use it in GitHub Desktop.
Identify clusters of contiguous factors within a dendrogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Identify clusters of contiguous factors within a dendrogram | |
# Aaron Wolen | |
# | |
# See http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0038422 | |
# for an example situation in which this would be useful | |
# | |
# x: dendrogram | |
# f: a named vector containing the factor that defines the grouping of leaves, | |
# must be named using the same values that define x's leaf labels | |
contigs <- function(x, f) { | |
UseMethod("contigs", x) | |
} | |
contigs.dendrogram <- function(x, f) { | |
if(is.null(names(f))) stop("f must be a named vector.") | |
# obtain ordered leaf labels | |
leaves <- unlist(dendrapply(x, attr, which = "label")) | |
groups <- f[leaves] | |
# identify clusters | |
cluster <- rle(groups) | |
clustered <- cluster$values[cluster$lengths > 1] | |
clustered <- rep(cluster$length, cluster$length) > 1 | |
groups[!clustered] <- NA | |
return(groups) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment