[Phylobase-commits] r292 - in pkg: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 21 10:45:51 CET 2008


Author: jombart
Date: 2008-11-21 10:45:50 +0100 (Fri, 21 Nov 2008)
New Revision: 292

Modified:
   pkg/R/treewalk.R
   pkg/man/treewalk.Rd
Log:
Added shortestPath function and its doc.


Modified: pkg/R/treewalk.R
===================================================================
--- pkg/R/treewalk.R	2008-11-20 22:36:38 UTC (rev 291)
+++ pkg/R/treewalk.R	2008-11-21 09:45:50 UTC (rev 292)
@@ -117,3 +117,43 @@
         getnodes(phy, max(Reduce(intersect, ancests)))
     }
 }
+
+
+
+
+
+###############
+# shortestPath
+###############
+shortestPath <- function(phy, node1, node2){
+    if(!require(phylobase)) stop("phylobase package is not installed")
+
+    ## conversion from phylo, phylo4 and phylo4d
+    x <- as(phy, "phylo4")
+
+    ## come 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(NULL)
+
+    ## main computations
+    comAnc <- MRCA(x, t1, t2) # common ancestor
+    desComAnc <- descendants(x, comAnc, which="all")
+    ancT1 <- ancestors(x, t1, which="all")
+    path1 <- intersect(desComAnc, ancT1) # path: common anc -> t1
+
+    ancT2 <- ancestors(x, t2, which="all")
+    path2 <- intersect(desComAnc, ancT2) # path: common anc -> t2
+
+    res <- union(path1, path2) # union of the path
+    ## add the common ancestor if it differs from t1 or t2
+    if(!comAnc %in% c(t1,t2)){
+        res <- c(comAnc,res)
+    }
+
+    res <- getnodes(x, res)
+
+    return(res)
+} # end shortestPath

Modified: pkg/man/treewalk.Rd
===================================================================
--- pkg/man/treewalk.Rd	2008-11-20 22:36:38 UTC (rev 291)
+++ pkg/man/treewalk.Rd	2008-11-21 09:45:50 UTC (rev 292)
@@ -6,10 +6,11 @@
 \alias{siblings}
 \alias{MRCA}
 \alias{getnodes}
+\alias{shortestPath}
 \title{tree traversal and utility functions}
 \description{
   Functions for describing relationships among
-  phylogenetic nodes
+  phylogenetic nodes (i.e. internal nodes or tips).
 }
 \usage{
 getnodes(phy,node)
@@ -19,10 +20,11 @@
 children(phy, node)
 descendants(phy, node, which=c("tips","children","all"))
 MRCA(phy,\dots)
+shortestPath(phy, node1, node2)
 }
 \arguments{
-  \item{phy}{a \code{phylo4} object (or one inheriting from
-    \code{phylo4}, e.g. a \code{phylo4d} object)
+  \item{phy}{a \linkS4class{phylo4} object (or one inheriting from
+    \linkS4class{phylo4}, e.g. a \linkS4class{phylo4d} object)
   }
   \item{node}{a node number (or name)}
   \item{which}{(\code{ancestors}) specify whether to return just direct
@@ -33,6 +35,8 @@
   \item{include.self}{whether to include self in list of siblings}
   \item{\dots}{a list of node numbers or names,
     or a vector of node numbers or names}
+  \item{node1}{a node number (or name)}
+  \item{node2}{a node number (or name)}
 }
 \value{
   \code{getnodes} takes character strings corresponding to tip or
@@ -50,10 +54,12 @@
   of the same parent).
   \code{mrca} returns the most recent common ancestor of two or
   more nodes.
+  \code{shortestPath} returns the nodes of the shortest path from one
+  node to another (excluding \code{node1} and \code{node2}).
 }
 \note{\code{MRCA} is uppercase to avoid conflict with \code{mrca}
   in ape}
-\seealso{\code{mrca}, in the ape package, gives
+\seealso{\code{\link[pkg:ape]{mrca}}, in the ape package, gives
   a list of all subtrees}
 \examples{
   data(geospiza)
@@ -68,5 +74,7 @@
   ancestors(geospiza,"N11")
   MRCA(geospiza,"conirostris","difficilis","fuliginosa")
   MRCA(geospiza,"olivacea","conirostris")
+  shortestPath(geospiza, "fortis","fuliginosa")
+  shortestPath(geospiza, "N02","N05")
 }
 \keyword{misc}



More information about the Phylobase-commits mailing list