[Ecopd-commits] r44 - in pkg: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Oct 22 01:49:23 CEST 2009
Author: regetz
Date: 2009-10-22 01:49:23 +0200 (Thu, 22 Oct 2009)
New Revision: 44
Modified:
pkg/R/utilities.R
pkg/man/tipLength.Rd
Log:
enhanced tipLength to enable calculation of distance from root to tips
Modified: pkg/R/utilities.R
===================================================================
--- pkg/R/utilities.R 2009-10-21 22:22:30 UTC (rev 43)
+++ pkg/R/utilities.R 2009-10-21 23:49:23 UTC (rev 44)
@@ -55,10 +55,20 @@
}
# tip length extractor
-tipLength <- function(phy) {
- tip.length <- edgeLength(phy, nodeId(phy, type="tip"))
- names(tip.length) <- tipLabels(phy)
- return(tip.length)
+tipLength <- function(phy, from=c("parent", "root")) {
+ from <- match.arg(from)
+ tips <- nodeId(phy, type="tip")
+ if (from=="parent") {
+ len <- edgeLength(phy, tips)
+ } else if (from=="root") {
+ root.node <- rootNode(phy)
+ anc <- ancestors(phy, tips, "ALL")
+ len <- sapply(anc, function(n) {
+ sumEdgeLength(phy, setdiff(n, root.node))
+ })
+ }
+ names(len) <- tipLabels(phy)
+ return(len)
}
# abundance extractor
Modified: pkg/man/tipLength.Rd
===================================================================
--- pkg/man/tipLength.Rd 2009-10-21 22:22:30 UTC (rev 43)
+++ pkg/man/tipLength.Rd 2009-10-21 23:49:23 UTC (rev 44)
@@ -1,25 +1,42 @@
\name{tipLength}
\alias{tipLength}
-\title{Extract terminal branch lengths}
+\title{Calculate distance to tips from ancestors}
\description{
- Returns the lengths of all tip edges of a phylogeny.
+ Returns the edge length between each tip and its parent, or
+ alternatively the total distance to each tip from the root node of the
+ phylogeny.
}
\usage{
- tipLength(phy)
+ tipLength(phy, from=c("parent", "root"))
}
\arguments{
\item{phy}{a \code{phylo4} object (or one that inherits from it)}
+ \item{from}{Should lengths represent distance from parents only (i.e.,
+ simply the lengths of terminal branches), or from the root node?}
}
\value{
Numeric vector of lengths, with the associated tip node labels as
names.
}
+\note{
+ When calculating the distance from root, the length of the root edge
+ itself is \strong{not} included in the returned value. See examples
+ for a simple way to add this additional edge to the result.
+}
\author{Jim Regetz (regetz at nceas.ucsb.edu)}
\seealso{
- This function is a simple wrapper of the
- \code{\link[phylobase:phylo4-accessors]{edgeLength}} phylobase method.
+ Methods \code{\link[phylobase:phylo4-accessors]{edgeLength}} and
+ \code{\link[phylobase:phylo4-accessors]{sumEdgeLength}} in the
+ phylobase package.
}
\examples{
data(weeds)
tipLength(weeds$A)
+
+ # distance from the root node to each tip
+ tipLength(weeds$A, from="root")
+
+ # as above, but include root edge length
+ tipLength(weeds$A, from="root") + edgeLength(weeds$A,
+ rootNode(weeds$A))
}
More information about the Ecopd-commits
mailing list