[Adephylo-commits] r19 - in pkg: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 21 16:43:42 CET 2008


Author: jombart
Date: 2008-11-21 16:43:42 +0100 (Fri, 21 Nov 2008)
New Revision: 19

Added:
   pkg/man/distNodes.Rd
   pkg/man/distRoot.Rd
Modified:
   pkg/R/distances.R
Log:
Created a function distNodes that work pretty well. But still too slow to get tips/tips distances (other procedures will have to be more factorized).


Modified: pkg/R/distances.R
===================================================================
--- pkg/R/distances.R	2008-11-21 14:53:51 UTC (rev 18)
+++ pkg/R/distances.R	2008-11-21 15:43:42 UTC (rev 19)
@@ -1,7 +1,9 @@
 ############
 # distNodes
 ############
-distNodes <- function(x, node1, node2, method=c("brlength","nAncestors","Abouheif")){
+distNodes <- function(x, node1, node2,
+                      method=c("brlength","nNodes","Abouheif","sumDD")){
+
     if(!require(phylobase)) stop("phylobase package is not installed")
 
     ## conversion from phylo, phylo4 and phylo4d
@@ -10,10 +12,10 @@
 
     ## some checks
     if (is.character(checkval <- check_phylo4(x))) stop(checkval)
-    t1 <- getnodes(x, node1)
-    t2 <- getnodes(x, node2)
-    if(any(is.na(c(t1,t2)))) stop("wrong node specified")
-    if(t1==t2) return(0)
+    node1 <- getnodes(x, node1)
+    node2 <- getnodes(x, node2)
+    if(any(is.na(c(node1,node2)))) stop("wrong node specified")
+    if(node1==node2) return(0)
 
     ## get the path between node1 and node2
     path <- shortestPath(x, node1, node2)
@@ -22,32 +24,41 @@
     if(method=="brlength"){
         if(!hasEdgeLength(x)) stop("x does not have branch length")
         path <- c(node1, node2, path)
+        path <- path[path != MRCA(x, node1, node2)]
         edge.idx <- getedges(x, path)
-        res <- sum(edgeLength(x)[edge.idx])
+        res <- sum(edgeLength(x)[edge.idx], na.rm=TRUE)
         return(res)
     } # end brlength
 
-    if(method=="nAncestors"){
+    if(method=="nNodes"){
         res <- length(path)
         return(res)
-    } # end nAncestors
+    } # end nNodes
 
     if(method=="Abouheif"){
-
+        E <- x at edge
+        temp <- table(E[,1])[as.character(path)] # number of dd per node
+        res <- prod(temp)
+        return(res)
     } # end Abouheif
 
+    if(method=="sumDD"){
+        E <- x at edge
+        temp <- table(E[,1])[as.character(path)] # number of dd per node
+        res <- sum(temp)
+        return(res)
+ } # end Abouheif
 
-
-    return(res)
 } # end distNodes
 
 
 
 
+
 ###########
 # distRoot
 ###########
-distRoot <- function(x, method=c("brlength","nAncestors","Abouheif")){
+distRoot <- function(x, method=c("brlength","nNodes","Abouheif")){
     if(!require(phylobase)) stop("phylobase package is not installed")
 
     ## conversion from phylo, phylo4 and phylo4d

Added: pkg/man/distNodes.Rd
===================================================================
--- pkg/man/distNodes.Rd	                        (rev 0)
+++ pkg/man/distNodes.Rd	2008-11-21 15:43:42 UTC (rev 19)
@@ -0,0 +1,63 @@
+\name{distNodes}
+\alias{distNodes}
+\title{Compute some phylogenetic distance between two nodes}
+\description{
+  The function \code{distNodes} computes a given distance between two
+  nodes of a phylogeny. This distance corresponds to the shortest path
+  between the two nodes. Several distances can be used, defaulting to the
+  sum of branch lengths (see argument \code{method}).
+}
+\usage{
+distNodes(x, node1, node2, method=c("brlength","nNodes","Abouheif","sumDD"))
+}
+\arguments{
+  \item{x}{a tree of  class \code{\link[pkg:ape]{phylo}},
+    \linkS4class{phylo4} or \linkS4class{phylo4d}.}
+  \item{method}{a character string (full or abbreviated without
+    ambiguity) specifying the method used to compute distances ;
+    possible values are:\cr
+    - \code{brlength}: branch length \cr
+    - \code{nNodes}: number of nodes on the path between the nodes \cr
+    - \code{Abouheif}: Abouheif's distance (see details) \cr
+    - \code{sumDD}: sum of direct descendants of all nodes on the path
+    (see details) \cr
+   }
+}
+\value{
+  A numeric vector containing one distance value for each tip.
+}
+\details{
+  \code{Abouheif} distance refers to the phylogenetic distance
+  underlying the test of Abouheif (see references). Let P be the set of
+  all the nodes in the path going from \code{node1} to \code{node2}. Let
+  DDP be the number of direct descendants from each node in P. Then, the
+  so-called 'Abouheif' distance is the product of all terms in DDP.\cr
+
+   \code{sumDD} refers to a phylogenetic distance quite similar to that
+  of Abouheif. We consider the same sets P and DDP. But instead of
+  computing the product of all terms in DDP, this distance computes the
+  sum of all terms in DDP.
+}
+\author{ Thibaut Jombart \email{jombart at biomserv.univ-lyon1.fr} }
+\seealso{\code{\link{distTips}} which computes several phylogenetic
+  distances between tips.
+}
+\references{
+  Pavoine, S.; Ollier, S.; Pontier, D. & Chessel, D. (2008) Testing for
+  phylogenetic signal in life history variable: Abouheif's test
+  revisited. \emph{Theoretical Population Biology}: \bold{73}, 79-91.
+}
+\examples{
+if(require(ape) & require(phylobase)){
+## make a tree
+x <- as(rtree(10),"phylo4")
+plot(x, show.node=TRUE)
+axisPhylo()
+## compute different distances
+distNodes(x, "t1", "t2")
+distNodes(x, "t1", "t2", "nNodes")
+distNodes(x, "t1", "t2", "Abouheif")
+distNodes(x, "t1", "t2", "sumDD")
+}
+}
+\keyword{manip}

Added: pkg/man/distRoot.Rd
===================================================================
--- pkg/man/distRoot.Rd	                        (rev 0)
+++ pkg/man/distRoot.Rd	2008-11-21 15:43:42 UTC (rev 19)
@@ -0,0 +1,41 @@
+\name{distRoot}
+\alias{distRoot}
+\title{Compute the distance of tips to the root}
+\description{
+  The function \code{distRoot} computes the distance of each tip to the
+  root. Several distances can be used, defaulting to the sum of branch lengths.
+}
+\usage{
+distRoot(x, method=c("brlength","nAncestors","Abouheif"))
+}
+\arguments{
+  \item{x}{a tree of  class \code{\link[pkg:ape]{phylo}},
+    \linkS4class{phylo4} or \linkS4class{phylo4d}.}
+  \item{method}{a character string (full or abbreviated without
+    ambiguity) specifying the method used to compute distances ;
+    possible values are:\cr
+    - \code{brlength}: branch length \cr
+    - \code{nAncestors}: number of Ancestors \cr
+    - \code{Abouheif}: Abouheif's distance \cr
+  }
+}
+\value{
+  A numeric vector containing one distance value for each tip.
+}
+\author{ Thibaut Jombart \email{jombart at biomserv.univ-lyon1.fr} }
+\seealso{\code{\link{distPhylo4}} which computes several phylogenetic
+  distances between tips.
+}
+\examples{
+if(require(ape) & require(phylobase)){
+## make a tree
+x <- as(rtree(10),"phylo4")
+D <- distRoot(x)
+D
+
+## plot these distances along with the tree
+temp <- phylo4d(x, D)
+s.phylo4d(temp, cent=FALSE, scale=FALSE)
+}
+}
+\keyword{manip}



More information about the Adephylo-commits mailing list