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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Feb 29 14:48:51 CET 2008


Author: jombart
Date: 2008-02-29 14:48:51 +0100 (Fri, 29 Feb 2008)
New Revision: 131

Modified:
   pkg/R/phylo4.R
   pkg/man/printphylo4.Rd
Log:
Several fixes to the printing of phylo4/phylo4d objects.
Labels and nodes are now consistent.
Works for rooted / unrooted trees.
Commented the old version of print phylo4d, which was still used.
Several adds to the documentation.
Check ok.


Modified: pkg/R/phylo4.R
===================================================================
--- pkg/R/phylo4.R	2008-02-28 17:41:41 UTC (rev 130)
+++ pkg/R/phylo4.R	2008-02-29 13:48:51 UTC (rev 131)
@@ -277,31 +277,43 @@
 ##  Not sure if it works for unrooted trees
 setAs(from='phylo4',to='data.frame',
       def = function(from) {
+        if (is.character(checkval <- check_phylo4(from))) stop(checkval) # check the phylo4
       	x <- from
-	ancestor <- x at edge[,1]
-	node <- x at edge[,2]
+        E <- edges(x) # E: matrix of edges
+	ancestor <- E[,1]
+	node <- E[,2]
 	root <- unique(ancestor[!ancestor %in% node])
-	int.node <- c(root, unique(ancestor[ancestor %in% node]))
+	int.node <- c(root, unique(ancestor[ancestor %in% node])) # internal nodes (root first)
         tip <- node[!(node %in% ancestor)]
 	n.tip <- length(tip)
         n.int <- length(int.node)
-        node <- c(root, node)
-        if (length(ancestor)>0) ancestor <- c(NA, ancestor)
-        branch.length <- c(x at root.edge, x at edge.length)
-        if (length(branch.length) == 1) branch.length <- rep("", n.tip+n.int)
-        if (print.species <- !(is.null(x at node.label) & is.null(x at tip.label)))
-          { 
-              nl <- x at node.label       
-              if (is.null(nl)) nl <-  rep("", n.int)   # phylo4 has a node.label for the root?
-              tl <- x at tip.label
-              if (is.null(tl)) tl <-  rep("", n.tip)
-              species.name <- c(nl, tl)
-          } else species.name <- NULL
-        if (length(root)==0) {
+        # node <- c(root, node) # doesn't fit the ordering: root, other internal nodes, tips
+        node <- c(int.node,tip)
+        ## retrieve the ancestor of each node
+        idx <- match(node,E[,2]) # new ordering of the descendents/edges
+        # if (length(ancestor)>0) ancestor <- c(NA, ancestor)
+        ancestor <- E[idx,1]
+        # branch.length <- c(x at root.edge, x at edge.length) # root.edge is not an edge length
+        branch.length <- EdgeLength(x)[idx]
+        # if (length(branch.length) == 1) branch.length <- rep("", n.tip+n.int)
+        if(is.null(EdgeLength(x))) branch.length <- rep(NA, length(node))
+        ## node and tip labels ##
+        ## beware: they cannot be NULL
+        ## there are always tip labels (or check_phylo4 complains)
+        ## there may not be node labels (character(0))
+        if(hasNodeLabels(x)) {
+            nl <- x at node.label
+        } else {
+            nl <- rep(NA,nNodes(x))
+        }
+        
+        tl <- labels(x)
+        taxon.name <- c(nl, tl)
+        if (!isRooted(x)) {
             node.type <- c(rep("internal", n.int), rep("tip", n.tip))
         }  else node.type <- c("root", rep("internal", n.int-1), rep("tip", n.tip))
         
-        return(data.frame(species.name, node, ancestor, branch.length, node.type))
+        return(data.frame(taxon.name, node, ancestor, branch.length, node.type))
     })
 
 printphylo4 <- function(x, printall = TRUE){
@@ -318,7 +330,7 @@
     tdata(from, "allnode") -> dat               # get data
     t_df$order <- rownames(t_df)     # save roworder of tree
 
-    merge(t_df, dat, by.x="species.name", by.y="row.names", all.x=TRUE, sort=FALSE) -> tdat
+    merge(t_df, dat, by.x="taxon.name", by.y="row.names", all.x=TRUE, sort=FALSE) -> tdat
                                      # merged tree, data, but mixed up
 
     order(tdat$order) -> o           # ordering to get back original roworder                              
@@ -521,24 +533,24 @@
          contains="multiPhylo4")
 
 ################
-## show phylo4d
+## show phylo4d    ### no longer used  
 ################
 ##
-setMethod("show", "phylo4d", function(object){
-    x <- object
+## setMethod("show", "phylo4d", function(object){
+##     x <- object
 
-    cat("\n##Comparative data##\n")
-    ##  print tree
-    cat("\n#Tree#\n")
-    printphylo(x)
+##     cat("\n##Comparative data##\n")
+##     ##  print tree
+##     cat("\n#Tree#\n")
+##     printphylo(x)
 
-    ## print traits
-    cat("\n#Traits#\n")
-    cat("\ntip.data: data.frame containing", ncol(tdata(x,"tip")), "traits for", nrow(tdata(x,"tip")),"tips" )
-    cat("\nnode.data: data.frame containing", ncol(tdata(x,"node")), "traits for", nrow(tdata(x,"node")),"nodes" )
+##     ## print traits
+##     cat("\n#Traits#\n")
+##     cat("\ntip.data: data.frame containing", ncol(tdata(x,"tip")), "traits for", nrow(tdata(x,"tip")),"tips" )
+##     cat("\nnode.data: data.frame containing", ncol(tdata(x,"node")), "traits for", nrow(tdata(x,"node")),"nodes" )
 
-    cat("\n")
-}) # end summary phylo4d
+##     cat("\n")
+## })
 
 ## ?? setMethod("print", "phylo4", o)
 

Modified: pkg/man/printphylo4.Rd
===================================================================
--- pkg/man/printphylo4.Rd	2008-02-28 17:41:41 UTC (rev 130)
+++ pkg/man/printphylo4.Rd	2008-02-29 13:48:51 UTC (rev 131)
@@ -13,28 +13,34 @@
 }
 \details{
 This is a user-friendly version of the tree representation, useful for checking that objects were read in completely and translated correctly. The phylogenetic tree is represented as a list of numbered nodes, linked in a particular way through time (or rates of evolutionary change). 
-The topology is given by the pattern of links from each node to its ancestor. Also given are the species names, node type (root/internal/tip) and phenotypic data (if any) associated with the node, and the branch length from the node to its ancestor. A list of nodes (descendants) and ancestors is minimally required for a phylo4 object.
+The topology is given by the pattern of links from each node to its ancestor. Also given are the taxon names, node type (root/internal/tip) and phenotypic data (if any) associated with the node, and the branch length from the node to its ancestor. A list of nodes (descendants) and ancestors is minimally required for a phylo4 object.
 }
 \value{
-  A data.frame with a row for each node (descendant).
-  \item{species.name}{User-supplied label for the taxon at the node (usually species name). (optional)}
-  \item{node}{Node number.}
+  A data.frame with a row for each node (descendant), sorted as
+  follows: root first, then other internal nodes, and finally tips.\cr
+  The returned data.frame has the following columns:\cr
+  \item{taxon.name}{Label for the taxon at the node (usually species name).}
+  \item{node}{Node number, i.e. the number identifying the node in \code{x at edge}.}
   \item{ancestor}{Node number of the node's ancestor.}
-  \item{branch.length}{The branch length connecting the node to its ancestor. (optional)}
+  \item{branch.length}{The branch length connecting the node to its
+    ancestor (NAs if missing).}
   \item{node.type}{"root", "internal", or "tip". (internally generated)}
   \item{data}{phenotypic data associated with the nodes, with separate columns for each variable.}
 }
-\author{Marguerite Butler}
+\author{
+  Marguerite Butler
+  Thibaut Jombart \email{jombart at biomserv.univ-lyon1.fr}
+}
 \note{This is the default show() method for phylo4, phylo4d. It prints the user-supplied information for building a phylo4 object. For a full description of the phylo4 S4 object and slots, see \code{\link{phylo4}}. }
 \seealso{See Also as \code{\link{printphylo}} for an \code{ape}-like version of \code{print()}. }
 \examples{
 
 tree.phylo <- read.tree(text="((a,b),c);")
 tree <- as(tree.phylo, "phylo4")
-plot(tree)
+plot(tree,show.node=TRUE)
 tip.data <- data.frame(size=c(1,2,3), row.names=c("a", "b", "c"))
 treedata <- phylo4d(tree, tip.data)
-plot(treedata)
+plot(treedata,ratio=.7,cent=FALSE,scale=FALSE)
 print(treedata)
 
 }



More information about the Phylobase-commits mailing list