R Under development (unstable) (2024-01-28 r85838 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(phylobase) > library(ape) Attaching package: 'ape' The following object is masked from 'package:phylobase': edges > > set.seed(1) > > data(geospiza) > > ## make sure geospiza is properly formatted > if(is.character(checkval <- checkPhylo4(geospiza))) + stop(checkval) > > > geospiza0 <- + list(geospiza.tree=as(geospiza,"phylo"),geospiza.data=tipData(geospiza)) Warning messages: 1: In asMethod(object) : losing data while coercing phylo4d to phylo 2: In asMethod(object) : trees with unknown order may be unsafe in ape > ## push data back into list form as in geiger > > t1 <- try(p1 <- phylo4d(geospiza0$geospiza.tree,geospiza0$geospiza.data)) > ## Error in checkData(res, ...) : > ## Tip data names are a subset of tree tip labels. > > p2 <- as(geospiza0$geospiza.tree,"phylo4") > plot(p2) > > lab1 <- tipLabels(p2) > lab2 <- rownames(geospiza0$geospiza.data) > > lab1[!lab1 %in% lab2] ## missing data named character(0) > lab2[!lab2 %in% lab1] ## extra data (none) character(0) > p1 <- phylo4d(p2,geospiza0$geospiza.data, missing.data="warn") > p1 <- phylo4d(p2,geospiza0$geospiza.data, missing.data="OK") > > plot(p1) > plot(p1,show.node.label=TRUE) > ## one way to deal with it: > > p1B <- prune(p1,tip="olivacea") > > ## or ... > p1C <- stats::na.omit(p1) > > labels(p1C, "all") <- tolower(labels(p1C, "all")) > > ## trace("prune",browser,signature="phylo4d") > r1 <- read.tree(text="((t4:0.3210275554,(t2:0.2724586465,t3:0.2724586465):0.0485689089):0.1397952619,(t5:0.07551818331,t1:0.07551818331):0.385304634);") > > ## trace("phylo4d", browser, signature = "phylo") > ## untrace("phylo4d", signature = "phylo") > tipdat <- data.frame(a=1:5, row.names=r1$tip.label) > q1 <- phylo4d(r1,tip.data=tipdat, node.data=data.frame(a=6:9), match.data=FALSE) > q2 <- prune(q1,1) > summary(q2) Phylogenetic tree : as(x, "phylo4") Number of tips : 4 Number of nodes : 3 Branch lengths: mean : 0.2116037 variance : 0.01503145 distribution : Min. 1st Qu. Median Mean 3rd Qu. Max. 0.07552 0.10373 0.23041 0.21160 0.27246 0.38530 Comparative data: Tips: data.frame with 4 taxa and 1 variable(s) a Min. :2.00 1st Qu.:2.75 Median :3.50 Mean :3.50 3rd Qu.:4.25 Max. :5.00 Nodes: data.frame with 3 internal nodes and 1 variables a Min. :6.000 1st Qu.:7.000 Median :8.000 Mean :7.667 3rd Qu.:8.500 Max. :9.000 > > tipdat2 <- tipdat > row.names(tipdat2)[1] <- "s1" > t1 <- try(q1 <- phylo4d(r1,tip.data=tipdat2)) Error in formatData(phy = x, dt = tip.data, type = "tip", ...) : The following nodes are not found in the dataset: t4 > > plot(q2) > plot(q2,type="cladogram") > ## plot(p2,type="dotchart",labels.nodes=nodeLabels(p2)) > ## trace("plot", browser, signature = c("phylo4d","missing")) > tipLabels(q1) <- paste("q",1:5,sep="") > nodeLabels(q1) <- paste("n",1:4,sep="") > p3 <- phylo4d(r1,tip.data=tipdat,node.data=data.frame(b=6:9), match.data=FALSE) > summary(p3) Phylogenetic tree : as(x, "phylo4") Number of tips : 5 Number of nodes : 4 Branch lengths: mean : 0.1988313 variance : 0.0167175 distribution : Min. 1st Qu. Median Mean 3rd Qu. Max. 0.04857 0.07552 0.20613 0.19883 0.28460 0.38530 Comparative data: Tips: data.frame with 5 taxa and 2 variable(s) a b Min. :1 Min. : NA 1st Qu.:2 1st Qu.: NA Median :3 Median : NA Mean :3 Mean :NaN 3rd Qu.:4 3rd Qu.: NA Max. :5 Max. : NA NA's :5 Nodes: data.frame with 4 internal nodes and 2 variables a b Min. : NA Min. :6.00 1st Qu.: NA 1st Qu.:6.75 Median : NA Median :7.50 Mean :NaN Mean :7.50 3rd Qu.: NA 3rd Qu.:8.25 Max. : NA Max. :9.00 NA's :4 > > plot(p1) > > plot(subset(p1,tips.include=c("fuliginosa","fortis","magnirostris", + "conirostris","scandens"))) > ## better error? > ## Error in phy$edge[, 2] : incorrect number of dimensions > > if(dev.cur() == 1) get(getOption("device"))() > plot(subset(p2,tips.include=c("fuliginosa","fortis","magnirostris", + "conirostris","scandens"))) > > plot(p2,show.node.label=TRUE) > > tree.owls <- read.tree(text="(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:13.5);") > > z <- as(tree.owls,"phylo4") > > example("phylo4d") phyl4d> treeOwls <- "((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3);" phyl4d> tree.owls.bis <- ape::read.tree(text=treeOwls) phyl4d> try(phylo4d(as(tree.owls.bis,"phylo4"),data.frame(wing=1:3)), silent=TRUE) label node ancestor edge.length node.type wing 1 Strix_aluco 1 5 4.2 tip 1 2 Asio_otus 2 5 4.2 tip 2 3 Athene_noctua 3 4 7.3 tip 3 4 4 0 NA root NA 5 5 4 3.1 internal NA phyl4d> obj <- phylo4d(as(tree.owls.bis,"phylo4"),data.frame(wing=1:3), match.data=FALSE) phyl4d> obj label node ancestor edge.length node.type wing 1 Strix_aluco 1 5 4.2 tip 1 2 Asio_otus 2 5 4.2 tip 2 3 Athene_noctua 3 4 7.3 tip 3 4 4 0 NA root NA 5 5 4 3.1 internal NA phyl4d> print(obj) label node ancestor edge.length node.type wing 1 Strix_aluco 1 5 4.2 tip 1 2 Asio_otus 2 5 4.2 tip 2 3 Athene_noctua 3 4 7.3 tip 3 4 4 0 NA root NA 5 5 4 3.1 internal NA phyl4d> #### phyl4d> phyl4d> data(geospiza_raw) phyl4d> geoTree <- geospiza_raw$tree phyl4d> geoData <- geospiza_raw$data phyl4d> ## fix differences in tip names between the tree and the data phyl4d> geoData <- rbind(geoData, array(, dim = c(1,ncol(geoData)), phyl4d+ dimnames = list("olivacea", colnames(geoData)))) phyl4d> ### Example using a tree of class 'phylo' phyl4d> exGeo1 <- phylo4d(geoTree, tip.data = geoData) phyl4d> ### Example using a tree of class 'phylo4' phyl4d> geoTree <- as(geoTree, "phylo4") phyl4d> ## some random node data phyl4d> rNodeData <- data.frame(randomTrait = rnorm(nNodes(geoTree)), phyl4d+ row.names = nodeId(geoTree, "internal")) phyl4d> exGeo2 <- phylo4d(geoTree, tip.data = geoData, node.data = rNodeData) phyl4d> ### Example using 'merge.data' phyl4d> data(geospiza) phyl4d> trGeo <- extractTree(geospiza) phyl4d> tDt <- data.frame(a=rnorm(nTips(trGeo)), row.names=nodeId(trGeo, "tip")) phyl4d> nDt <- data.frame(a=rnorm(nNodes(trGeo)), row.names=nodeId(trGeo, "internal")) phyl4d> (matchData1 <- phylo4d(trGeo, tip.data=tDt, node.data=nDt, merge.data=FALSE)) label node ancestor edge.length node.type a.tip a.node 1 fuliginosa 1 24 0.05500 tip -2.21469989 NA 2 fortis 2 24 0.05500 tip 1.12493092 NA 3 magnirostris 3 23 0.11000 tip -0.04493361 NA 4 conirostris 4 22 0.18333 tip -0.01619026 NA 5 scandens 5 21 0.19250 tip 0.94383621 NA 6 difficilis 6 20 0.22800 tip 0.82122120 NA 7 pallida 7 25 0.08667 tip 0.59390132 NA 8 parvulus 8 27 0.02000 tip 0.91897737 NA 9 psittacula 9 27 0.02000 tip 0.78213630 NA 10 pauper 10 26 0.03500 tip 0.07456498 NA 11 Platyspiza 11 18 0.46550 tip -1.98935170 NA 12 fusca 12 17 0.53409 tip 0.61982575 NA 13 Pinaroloxias 13 16 0.58333 tip -0.05612874 NA 14 olivacea 14 15 0.88077 tip -0.15579551 NA 15 N15 15 0 NA root NA -1.47075238 16 N16 16 15 0.29744 internal NA -0.47815006 17 N17 17 16 0.04924 internal NA 0.41794156 18 N18 18 17 0.06859 internal NA 1.35867955 19 N19 19 18 0.13404 internal NA -0.10278773 20 N20 20 19 0.10346 internal NA 0.38767161 21 N21 21 20 0.03550 internal NA -0.05380504 22 N22 22 21 0.00917 internal NA -1.37705956 23 N23 23 22 0.07333 internal NA -0.41499456 24 N24 24 23 0.05500 internal NA -0.39428995 25 N25 25 19 0.24479 internal NA -0.05931340 26 N26 26 25 0.05167 internal NA 1.10002537 27 N27 27 26 0.01500 internal NA 0.76317575 phyl4d> (matchData2 <- phylo4d(trGeo, tip.data=tDt, node.data=nDt, merge.data=TRUE)) label node ancestor edge.length node.type a 1 fuliginosa 1 24 0.05500 tip -2.21469989 2 fortis 2 24 0.05500 tip 1.12493092 3 magnirostris 3 23 0.11000 tip -0.04493361 4 conirostris 4 22 0.18333 tip -0.01619026 5 scandens 5 21 0.19250 tip 0.94383621 6 difficilis 6 20 0.22800 tip 0.82122120 7 pallida 7 25 0.08667 tip 0.59390132 8 parvulus 8 27 0.02000 tip 0.91897737 9 psittacula 9 27 0.02000 tip 0.78213630 10 pauper 10 26 0.03500 tip 0.07456498 11 Platyspiza 11 18 0.46550 tip -1.98935170 12 fusca 12 17 0.53409 tip 0.61982575 13 Pinaroloxias 13 16 0.58333 tip -0.05612874 14 olivacea 14 15 0.88077 tip -0.15579551 15 N15 15 0 NA root -1.47075238 16 N16 16 15 0.29744 internal -0.47815006 17 N17 17 16 0.04924 internal 0.41794156 18 N18 18 17 0.06859 internal 1.35867955 19 N19 19 18 0.13404 internal -0.10278773 20 N20 20 19 0.10346 internal 0.38767161 21 N21 21 20 0.03550 internal -0.05380504 22 N22 22 21 0.00917 internal -1.37705956 23 N23 23 22 0.07333 internal -0.41499456 24 N24 24 23 0.05500 internal -0.39428995 25 N25 25 19 0.24479 internal -0.05931340 26 N26 26 25 0.05167 internal 1.10002537 27 N27 27 26 0.01500 internal 0.76317575 phyl4d> ## Example with 'all.data' phyl4d> nodeLabels(geoTree) <- as.character(nodeId(geoTree, "internal")) phyl4d> rAllData <- data.frame(randomTrait = rnorm(nTips(geoTree) + nNodes(geoTree)), phyl4d+ row.names = labels(geoTree, 'all')) phyl4d> exGeo5 <- phylo4d(geoTree, all.data = rAllData) phyl4d> ## Examples using 'rownamesAsLabels' and comparing with match.data=FALSE phyl4d> tDt <- data.frame(x=letters[1:nTips(trGeo)], phyl4d+ row.names=sample(nodeId(trGeo, "tip"))) phyl4d> tipLabels(trGeo) <- as.character(sample(1:nTips(trGeo))) phyl4d> (exGeo6 <- phylo4d(trGeo, tip.data=tDt, rownamesAsLabels=TRUE)) label node ancestor edge.length node.type x 1 1 1 24 0.05500 tip j 2 13 2 24 0.05500 tip b 3 6 3 23 0.11000 tip n 4 12 4 22 0.18333 tip g 5 4 5 21 0.19250 tip i 6 9 6 20 0.22800 tip m 7 5 7 25 0.08667 tip e 8 8 8 27 0.02000 tip c 9 11 9 27 0.02000 tip h 10 14 10 26 0.03500 tip d 11 3 11 18 0.46550 tip k 12 2 12 17 0.53409 tip l 13 7 13 16 0.58333 tip f 14 10 14 15 0.88077 tip a 15 N15 15 0 NA root 16 N16 16 15 0.29744 internal 17 N17 17 16 0.04924 internal 18 N18 18 17 0.06859 internal 19 N19 19 18 0.13404 internal 20 N20 20 19 0.10346 internal 21 N21 21 20 0.03550 internal 22 N22 22 21 0.00917 internal 23 N23 23 22 0.07333 internal 24 N24 24 23 0.05500 internal 25 N25 25 19 0.24479 internal 26 N26 26 25 0.05167 internal 27 N27 27 26 0.01500 internal phyl4d> (exGeo7 <- phylo4d(trGeo, tip.data=tDt, rownamesAsLabels=FALSE)) label node ancestor edge.length node.type x 1 1 1 24 0.05500 tip j 2 13 2 24 0.05500 tip l 3 6 3 23 0.11000 tip k 4 12 4 22 0.18333 tip i 5 4 5 21 0.19250 tip e 6 9 6 20 0.22800 tip n 7 5 7 25 0.08667 tip f 8 8 8 27 0.02000 tip c 9 11 9 27 0.02000 tip m 10 14 10 26 0.03500 tip a 11 3 11 18 0.46550 tip h 12 2 12 17 0.53409 tip g 13 7 13 16 0.58333 tip b 14 10 14 15 0.88077 tip d 15 N15 15 0 NA root 16 N16 16 15 0.29744 internal 17 N17 17 16 0.04924 internal 18 N18 18 17 0.06859 internal 19 N19 19 18 0.13404 internal 20 N20 20 19 0.10346 internal 21 N21 21 20 0.03550 internal 22 N22 22 21 0.00917 internal 23 N23 23 22 0.07333 internal 24 N24 24 23 0.05500 internal 25 N25 25 19 0.24479 internal 26 N26 26 25 0.05167 internal 27 N27 27 26 0.01500 internal phyl4d> (exGeo8 <- phylo4d(trGeo, tip.data=tDt, match.data=FALSE)) label node ancestor edge.length node.type x 1 1 1 24 0.05500 tip a 2 13 2 24 0.05500 tip b 3 6 3 23 0.11000 tip c 4 12 4 22 0.18333 tip d 5 4 5 21 0.19250 tip e 6 9 6 20 0.22800 tip f 7 5 7 25 0.08667 tip g 8 8 8 27 0.02000 tip h 9 11 9 27 0.02000 tip i 10 14 10 26 0.03500 tip j 11 3 11 18 0.46550 tip k 12 2 12 17 0.53409 tip l 13 7 13 16 0.58333 tip m 14 10 14 15 0.88077 tip n 15 N15 15 0 NA root 16 N16 16 15 0.29744 internal 17 N17 17 16 0.04924 internal 18 N18 18 17 0.06859 internal 19 N19 19 18 0.13404 internal 20 N20 20 19 0.10346 internal 21 N21 21 20 0.03550 internal 22 N22 22 21 0.00917 internal 23 N23 23 22 0.07333 internal 24 N24 24 23 0.05500 internal 25 N25 25 19 0.24479 internal 26 N26 26 25 0.05167 internal 27 N27 27 26 0.01500 internal phyl4d> ## generate a tree and some data phyl4d> set.seed(1) phyl4d> p3 <- ape::rcoal(5) phyl4d> dat <- data.frame(a = rnorm(5), b = rnorm(5), row.names = p3$tip.label) phyl4d> dat.defaultnames <- dat phyl4d> row.names(dat.defaultnames) <- NULL phyl4d> dat.superset <- rbind(dat, rnorm(2)) phyl4d> dat.subset <- dat[-1, ] phyl4d> ## create a phylo4 object from a phylo object phyl4d> p4 <- as(p3, "phylo4") phyl4d> ## create phylo4d objects with tip data phyl4d> p4d <- phylo4d(p4, dat) phyl4d> ###checkData(p4d) phyl4d> p4d.sorted <- phylo4d(p4, dat[5:1, ]) phyl4d> try(p4d.nonames <- phylo4d(p4, dat.defaultnames)) phyl4d> p4d.nonames <- phylo4d(p4, dat.defaultnames, match.data=FALSE) phyl4d> ## Not run: phyl4d> ##D p4d.subset <- phylo4d(p4, dat.subset) phyl4d> ##D p4d.subset <- phylo4d(p4, dat.subset) phyl4d> ##D try(p4d.superset <- phylo4d(p4, dat.superset)) phyl4d> ##D p4d.superset <- phylo4d(p4, dat.superset) phyl4d> ## End(Not run) phyl4d> phyl4d> ## create phylo4d objects with node data phyl4d> nod.dat <- data.frame(a = rnorm(4), b = rnorm(4)) phyl4d> p4d.nod <- phylo4d(p4, node.data = nod.dat, match.data=FALSE) phyl4d> ## create phylo4 objects with node and tip data phyl4d> p4d.all1 <- phylo4d(p4, node.data = nod.dat, tip.data = dat, match.data=FALSE) phyl4d> nodeLabels(p4) <- as.character(nodeId(p4, "internal")) phyl4d> p4d.all2 <- phylo4d(p4, all.data = rbind(dat, nod.dat), match.data=FALSE) > obj1 <- obj2 <- obj3 <- phylo4d(z, data.frame(wing=1:4,color=factor(c("b","w","b","b")), tail=runif(4)*10), match.data=FALSE) > > obj2@data <- as.data.frame(obj2@data[,1]) > obj3@data <- cbind(obj1@data,obj2@data) > obj4 <- obj1 > obj4@data[2,3] <- NA > obj4@data[1,1] <- NA > > nodeLabels(obj4) <- character(0) > > obj5 <- obj1 > tipData(obj4) <- subset(tipData(obj4),select=sapply(tipData(obj4),class)=="numeric") > > treePlot(obj4) > > E <- matrix(c( + 8, 9, + 9, 10, + 10, 1, + 10, 2, + 9, 3, + 9, 4, + 8, 11, + 11, 5, + 11, 6, + 11, 7, + 0, 8), ncol=2,byrow=TRUE) > > P2 <- phylo4(E) > > proc.time() user system elapsed 2.92 0.46 3.39