Sorry - this is the correct source file. (I just neglected "as.phylo.formula<-" from the start of the source file.)

- Liam

Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: liam.rev...@umb.edu
blog: http://blog.phytools.org

On 8/22/2013 11:38 AM, Liam J. Revell wrote:
Hi Francois.

The reason that this is the case because the algorithm in
as.phylo.formula is to build a Newick string without "singles" (that is,
nodes with only one descendant), and then read it into memory using
read.tree (which can only read Newick trees without singles). In the
attached very slightly modified version, the function does not remove
singles from the Newick string, and then it reads it into memory using
read.newick in the phytools package which can handle singleton nodes
(e.g., http://blog.phytools.org/2013/06/robust-newick-tree-reader.html),
then it assigns equal branch lengths to all edges (one could use a
different formula for branch lengths in theory), and then it collapses
all singles. The effect is that all nodes of order "Family" (for
example) have the same height above the root.

E.g.,

library(phytools)
source("as.phylo.formula.R") # will replace as.phylo.formula
data(carnivora)
tree<-as.phylo.formula(as.phylo(~SuperFamily/Family/Genus/Species,
   data=carnivora)
plotTree(tree)

Let us know if this solves your problem.

All the best, Liam

Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: liam.rev...@umb.edu
blog: http://blog.phytools.org

On 8/22/2013 10:18 AM, Francois KECK wrote:
Hi all,
I 'm trying to build a taxonomic tree from a taxonomic matrix using the
function as.phylo.formula of the ape package. It works well but it
returns a phylo object without branch length. This is problematic in
that it can lead to misleading graphic representation (eg with
plot.phylo). For example a dichotomy between two species belonging to
the same genus may occur at the same y-coordinate than a dichotomy
between two species of different genus.
Is there a way to fix that? One solution might be to set a length to
each edge which reflect the taxonomic distance but I have no idea how to
do that.
Thanks for any help

François

_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at
http://www.mail-archive.com/r-sig-phylo@r-project.org/


_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

## as.phylo.formula from ape written by Julien Dutheil 
(julien.duth...@univ-montp2.fr)
## modified by Liam Revell (liam.rev...@umb.edu)
## depends on ape & phytools

as.phylo.formula<-function (x, data = parent.frame(), ...){
        err <- "Formula must be of the kind \"~A1/A2/.../An\"."
        if (length(x) != 2) 
                stop(err)
        if (x[[1]] != "~") 
                stop(err)
        f <- x[[2]]
        taxo <- list()
        while (length(f) == 3) {
                if (f[[1]] != "/") 
                        stop(err)
                if (!is.factor(data[[deparse(f[[3]])]])) 
                        stop(paste("Variable", deparse(f[[3]]), "must be a 
factor."))
                taxo[[deparse(f[[3]])]] <- data[[deparse(f[[3]])]]
                if (length(f) > 1) 
                        f <- f[[2]]
        }
        if (!is.factor(data[[deparse(f)]])) 
                stop(paste("Variable", deparse(f), "must be a factor."))
        taxo[[deparse(f)]] <- data[[deparse(f)]]
        taxo.data <- as.data.frame(taxo)
        leaves.names <- as.character(taxo.data[, 1])
        taxo.data[, 1] <- 1:nrow(taxo.data)
        f.rec <- function(subtaxo) {
                u <- ncol(subtaxo)
                levels <- unique(subtaxo[, u])
                if (u == 1) {
                        if (length(levels) != nrow(subtaxo)) 
                                warning("Error, leaves names are not unique.")
                        return(as.character(subtaxo[, 1]))
                }
                t <- character(length(levels))
                for (l in 1:length(levels)) {
                        x <- f.rec(subtaxo[subtaxo[, u] == levels[l], ][1:(u - 
1)])
                        t[l] <- paste("(", paste(x, collapse = ","), ")", sep = 
"")
                }
                return(t)
        }
        string <- paste("(", paste(f.rec(taxo.data), collapse = ","),");", sep 
= "")
        phy <- read.newick(text = string) ## so that singles will be read 
without error
        phy$edge.length <- rep(1,nrow(phy$edge))
        phy <- collapse.singles(phy)
        phy$tip.label <- leaves.names[as.numeric(phy$tip.label)]
        return(phy)
}
_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

Reply via email to