1 ## evolve.tree.R (2005-12-04)
3 ## Character Simulation under a Brownian Model
5 ## Copyright 2005 Julien Dutheil
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 evolve.phylo <- function(phy, value, var) {
11 if (!("phylo" %in% class(phy)))
12 stop("object \"phy\" is not of class \"phylo\"")
13 if (is.null(phy$edge.length))
14 stop("tree \" phy\" must have branch lengths.")
15 nchar <- max(length(value), length(var))
16 value <- rep(value, length=nchar)
17 var <- rep(var, length=nchar)
18 char.names <- names(value);
20 ## added by EP for the new coding of "phylo" (2006-10-04):
21 phy <- new2old.phylo(phy)
24 nodes <- unique(as.vector(edges))
25 n <- length(nodes) # Number of nodes
26 root <- match("-1", nodes)
28 states[["-1"]] <- value
29 for(node in nodes[-root]) {
30 edge.index <- match(node, edges[,2])
31 edge.length <- phy$edge.length[edge.index]
32 ancestor <- edges[edge.index, 1]
33 ancestor.node.index <- match(ancestor, nodes)
34 ancestor.states <- states[[ancestor.node.index]]
35 index <- match(node, nodes)
38 x[i] <- rnorm(1, mean=ancestor.states[i], sd=sqrt(var[i]*edge.length))
42 nodes.states <- as.data.frame(matrix(ncol=nchar, nrow=0))
43 if(!is.null(char.names)) names(nodes.states) <- char.names
45 for(i in unique(edges[,1])) {
46 nodes.states[i,] <- states[[match(i, nodes)]]
50 nl <- length(phy$tip.label) #Number of leaves
51 leaves.states <- as.data.frame(matrix(ncol=nchar, nrow=0))
52 if(!is.null(char.names)) names(leaves.states) <- char.names
55 leaves.states[as.character(count),] <- states[[match(as.character(i), nodes)]]
59 phy[["node.character"]] <- nodes.states;
60 phy[["tip.character"]] <- leaves.states;
61 if(! "ancestral" %in% class(phy)) class(phy) <- c("ancestral", class(phy));
62 ## added by EP for the new coding of "phylo" (2006-10-04):
63 phy <- old2new.phylo(phy)