# [Phylobase-devl] path between two tips

Thibaut Jombart jombart at biomserv.univ-lyon1.fr
Thu Nov 20 20:54:34 CET 2008

```Peter Cowan wrote:
> There is a function for calculating the MRCA.  Line 101 of the
> treewalk.R file.  Perhaps this is helpful.
>
> Peter
Yes, it is. I used it in the function:

shortestPath <- function(x, node1, node2){
if(!require(phylobase)) stop("phylobase package is not installed")

## conversion from phylo, phylo4 and phylo4d
x <- as(x, "phylo4")

## check phylo4 object
if (is.character(checkval <- check_phylo4(x))) stop(checkval)

## main computations
t1 <- getnodes(x, node1)
t2 <- getnodes(x, node2)

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
res <- c(comAnc,res) # add the common ancestor
res <- getnodes(x, res)

return(res)
} # end shortestPath

Example:
##
tr=as(rtree(20),"phylo4")
plot(tr, show.node=T)
shortestPath(tr,"t1","t2")
##

It works for tips, but some pb occur with internal nodes.
The pb seems to come from MRCA. For instance:
##
data(geospiza)
plot(as(geospiza,"phylo4"), show.node=T)
shortestPath(geospiza, "N02","N05")
MRCA(geospiza, "N02","N05")
##

MRCA returns N01 (the root) instead of N02.

Best,

Thibaut.

--
######################################
Dr Thibaut JOMBART
CNRS UMR 5558 - Laboratoire de Biométrie et Biologie Evolutive
Universite Lyon 1
43 bd du 11 novembre 1918
69622 Villeurbanne Cedex
Tél. : 04.72.43.29.35
Fax : 04.72.43.13.88
jombart at biomserv.univ-lyon1.fr
http://biomserv.univ-lyon1.fr/%7Ejombart/