[Phylobase-devl] Propositions: 'sumEdgeLength' and 'ancestors'

François Michonneau francois.michonneau at gmail.com
Thu Nov 20 17:41:16 CET 2008


  I want to propose to add another method to phylobase which returns the
sum of edge lengths given a phylo4 object and a set of nodes (I pasted
the proposed code below).

  Initially I was thinking of using it to compute the branch length from
a tip of the root of the tree, using something like:
  sumEdgeLength(geospiza, ancestors(geospiza, "fuliginosa")

  However, in this example, one branch length is missing (the one
connecting "fuliginosa" to node 24). I was then thinking it could be
useful to add another option to the 'ancestors' function that would also
return the node associated with the tip.

  Let me know if you think it's something that can be useful or if there
is already a better way of doing something like this in 'phylobase'.

    - François

Index: pkg/R/phylo4.R
--- pkg/R/phylo4.R	(revision 286)
+++ pkg/R/phylo4.R	(working copy)
@@ -34,6 +34,10 @@
+setGeneric("sumEdgeLength", function(phy, node) {
+    standardGeneric("sumEdgeLength")
 setGeneric("hasNodeLabels", function(x) {
Index: pkg/R/methods-phylo4.R
--- pkg/R/methods-phylo4.R	(revision 288)
+++ pkg/R/methods-phylo4.R	(working copy)
@@ -68,6 +68,17 @@
     else x at edge.length
+setMethod("sumEdgeLength", "phylo4", function(phy, node) {
+    if(!hasEdgeLength(phy))
+        NULL
+    else {
+        nd <- getnodes(phy, node)
+        iEdges <- which(phy at edge[,2] %in% nd)
+        sumEdges <- sum(phy at edge.length[iEdges])
+        sumEdges
+    }
 setMethod("hasNodeLabels", "phylo4", function(x) {
     length(x at node.label) > 0

More information about the Phylobase-devl mailing list