Re: [R-sig-phylo] A test for valid phylo object (ape package) for package developers
Hi, To answer Elizabeth's question: there is no function that does what you asked for in your first message. Liam's solution is certainly the best solution for the moment. I'll have a look at it later. The code checkValidPhylo() needs to be dusted off a bit: the diagnostic " MODERATE: some nodes are of degree 1 or less" is no more useful since nodes of degree 1 are now supported in ape. Best, Emmanuel Tue, 5 Nov 2019 14:45:58 -0800 Elizabeth Purdom : Thank you very much, I’ll try that! On Nov 5, 2019, at 2:20 PM, Liam Revell wrote: Hi Elizabeth. This does not do exactly what you want, but it is possible to use capture.output() to grab the printout of checkValidPhylo() and then grep() to see if it contains instances of "MODERATE" or "FATAL" errors. The following simple function does that. It returns 0 if neither MODERATE nor FATAL errors are detected in the "phylo" object, 1 if at least one MODERATE error (but no FATAL errors) is detected, and 2 if any FATAL errors are detected: chk.phylo<-function(x){ object<-capture.output(checkValidPhylo(x)) if(length(grep("FATAL",object))>0) return(2) else if(length(grep("MODERATE",object))>0) return(1) else return(0) } E.g.: library(ape) t1<-rtree(n=10) chk.phylo(t1) ## should return 0 t2<-t1 t2$Nnode<-9.5 chk.phylo(t2) ## should return 1 t3<-t1 t3$edge<-t3$edge[-4,] chk.phylo(t3) ## should return 2 All the best, Liam Liam J. Revell Associate Professor, University of Massachusetts Boston Profesor Asistente, Universidad Católica de la Ssma Concepción web: http://faculty.umb.edu/liam.revell/, http://www.phytools.org Academic Director UMass Boston Chile Abroad (starting 2019): https://www.umb.edu/academics/caps/international/biology_chile On 11/5/2019 5:54 PM, Elizabeth Purdom wrote: [EXTERNAL SENDER] Hello, I am working with the phylo object from the ape package in my own package in which I am manipulating the trees. I would like to check that I have successfully created a valid ape object, but the `checkValidPhylo` function appears to be solely interactive — it prints out a display, and always returns NULL. Is there a function in the ape package (or can there be?) that would do the checks, but return the results in a way that I can then process in my function? (e.g. return a vector of each of the checks as TRUE/FALSE) And I don’t want anything printed out, since I don’t want that output to be printed for users of my function). I see the package `paleotree` has ported some of those checks into a test, so I’m guessing such a function doesn’t exist in ape — and I don’t really want a dependency on another package just for these checks. Thanks, Elizabeth Purdom ___ R-sig-phylo mailing list - R-sig-phylo@r-project.org https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylodata=02%7C01%7Cliam.revell%40umb.edu%7C6584f4bb63dd48f9205008d762326164%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637085840853226654sdata=48LVGO%2FHHvX0rpMy6FaVgIhoeV6UO8Ouc%2B6FUfJcFCM%3Dreserved=0 Searchable archive at https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2Fdata=02%7C01%7Cliam.revell%40umb.edu%7C6584f4bb63dd48f9205008d762326164%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637085840853226654sdata=qgug0JIrKlC9fpr8lqrMuBku2XpHV3hfbzPF0m8ByuI%3Dreserved=0 ___ 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/
Re: [R-sig-phylo] A test for valid phylo object (ape package) for package developers
Thank you very much, I’ll try that! > On Nov 5, 2019, at 2:20 PM, Liam Revell wrote: > > Hi Elizabeth. > > This does not do exactly what you want, but it is possible to use > capture.output() to grab the printout of checkValidPhylo() and then > grep() to see if it contains instances of "MODERATE" or "FATAL" errors. > > The following simple function does that. It returns 0 if neither > MODERATE nor FATAL errors are detected in the "phylo" object, 1 if at > least one MODERATE error (but no FATAL errors) is detected, and 2 if any > FATAL errors are detected: > > chk.phylo<-function(x){ > object<-capture.output(checkValidPhylo(x)) > if(length(grep("FATAL",object))>0) return(2) > else if(length(grep("MODERATE",object))>0) return(1) > else return(0) > } > > E.g.: > > library(ape) > > t1<-rtree(n=10) > > chk.phylo(t1) ## should return 0 > > t2<-t1 > t2$Nnode<-9.5 > > chk.phylo(t2) ## should return 1 > > t3<-t1 > t3$edge<-t3$edge[-4,] > > chk.phylo(t3) ## should return 2 > > All the best, Liam > > Liam J. Revell > Associate Professor, University of Massachusetts Boston > Profesor Asistente, Universidad Católica de la Ssma Concepción > web: http://faculty.umb.edu/liam.revell/, http://www.phytools.org > > Academic Director UMass Boston Chile Abroad (starting 2019): > https://www.umb.edu/academics/caps/international/biology_chile > > On 11/5/2019 5:54 PM, Elizabeth Purdom wrote: >> [EXTERNAL SENDER] >> >> Hello, >> >> I am working with the phylo object from the ape package in my own package in >> which I am manipulating the trees. I would like to check that I have >> successfully created a valid ape object, but the `checkValidPhylo` function >> appears to be solely interactive — it prints out a display, and always >> returns NULL. >> >> Is there a function in the ape package (or can there be?) that would do the >> checks, but return the results in a way that I can then process in my >> function? (e.g. return a vector of each of the checks as TRUE/FALSE) And I >> don’t want anything printed out, since I don’t want that output to be >> printed for users of my function). >> >> I see the package `paleotree` has ported some of those checks into a test, >> so I’m guessing such a function doesn’t exist in ape — and I don’t really >> want a dependency on another package just for these checks. >> >> Thanks, >> Elizabeth Purdom >> ___ >> R-sig-phylo mailing list - R-sig-phylo@r-project.org >> https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylodata=02%7C01%7Cliam.revell%40umb.edu%7C6584f4bb63dd48f9205008d762326164%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637085840853226654sdata=48LVGO%2FHHvX0rpMy6FaVgIhoeV6UO8Ouc%2B6FUfJcFCM%3Dreserved=0 >> Searchable archive at >> https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2Fdata=02%7C01%7Cliam.revell%40umb.edu%7C6584f4bb63dd48f9205008d762326164%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637085840853226654sdata=qgug0JIrKlC9fpr8lqrMuBku2XpHV3hfbzPF0m8ByuI%3Dreserved=0 >> ___ 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/
Re: [R-sig-phylo] A test for valid phylo object (ape package) for package developers
Hi Elizabeth, There's a lot of ways for trees to be wrong, and surely no tree-checking or tree-fixing function can handle all possibilities. Dating trees and simulating trees can be a messy business, so I used to often hard-crash R with C++ errors all the time until I wrote functions to try to avoid this. (At one point, now many years ago, and probably not true anymore, but if you had a tree with nodes ordered in a way opposite of what ape expected, you could cause your operating system to blue-screen if you tried to plot the tree...). `paleotree::testEdgeMat` tests the edge matrix, which is often the source of problems, for inconsistencies. There is also paleotree function `cleanNewPhylo` will try to clean a tree of any of the stuff or misordered nodes that can cause a hard crash, particularly by reading it out as a text string and reading it back in with write.tree and read.tree. I hope this helps, -Dave On Tue, Nov 5, 2019 at 2:54 PM Elizabeth Purdom wrote: > > Hello, > > I am working with the phylo object from the ape package in my own package in > which I am manipulating the trees. I would like to check that I have > successfully created a valid ape object, but the `checkValidPhylo` function > appears to be solely interactive — it prints out a display, and always > returns NULL. > > Is there a function in the ape package (or can there be?) that would do the > checks, but return the results in a way that I can then process in my > function? (e.g. return a vector of each of the checks as TRUE/FALSE) And I > don’t want anything printed out, since I don’t want that output to be printed > for users of my function). > > I see the package `paleotree` has ported some of those checks into a test, so > I’m guessing such a function doesn’t exist in ape — and I don’t really want a > dependency on another package just for these checks. > > Thanks, > Elizabeth Purdom > ___ > 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/ -- David W. Bapst, PhD Assistant Instructional Professor Geology & Geophysics Texas A & M University https://github.com/dwbapst/paleotree ___ 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/
Re: [R-sig-phylo] A test for valid phylo object (ape package) for package developers
Hi Elizabeth. This does not do exactly what you want, but it is possible to use capture.output() to grab the printout of checkValidPhylo() and then grep() to see if it contains instances of "MODERATE" or "FATAL" errors. The following simple function does that. It returns 0 if neither MODERATE nor FATAL errors are detected in the "phylo" object, 1 if at least one MODERATE error (but no FATAL errors) is detected, and 2 if any FATAL errors are detected: chk.phylo<-function(x){ object<-capture.output(checkValidPhylo(x)) if(length(grep("FATAL",object))>0) return(2) else if(length(grep("MODERATE",object))>0) return(1) else return(0) } E.g.: library(ape) t1<-rtree(n=10) chk.phylo(t1) ## should return 0 t2<-t1 t2$Nnode<-9.5 chk.phylo(t2) ## should return 1 t3<-t1 t3$edge<-t3$edge[-4,] chk.phylo(t3) ## should return 2 All the best, Liam Liam J. Revell Associate Professor, University of Massachusetts Boston Profesor Asistente, Universidad Católica de la Ssma Concepción web: http://faculty.umb.edu/liam.revell/, http://www.phytools.org Academic Director UMass Boston Chile Abroad (starting 2019): https://www.umb.edu/academics/caps/international/biology_chile On 11/5/2019 5:54 PM, Elizabeth Purdom wrote: > [EXTERNAL SENDER] > > Hello, > > I am working with the phylo object from the ape package in my own package in > which I am manipulating the trees. I would like to check that I have > successfully created a valid ape object, but the `checkValidPhylo` function > appears to be solely interactive — it prints out a display, and always > returns NULL. > > Is there a function in the ape package (or can there be?) that would do the > checks, but return the results in a way that I can then process in my > function? (e.g. return a vector of each of the checks as TRUE/FALSE) And I > don’t want anything printed out, since I don’t want that output to be printed > for users of my function). > > I see the package `paleotree` has ported some of those checks into a test, so > I’m guessing such a function doesn’t exist in ape — and I don’t really want a > dependency on another package just for these checks. > > Thanks, > Elizabeth Purdom > ___ > R-sig-phylo mailing list - R-sig-phylo@r-project.org > https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylodata=02%7C01%7Cliam.revell%40umb.edu%7C6584f4bb63dd48f9205008d762326164%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637085840853226654sdata=48LVGO%2FHHvX0rpMy6FaVgIhoeV6UO8Ouc%2B6FUfJcFCM%3Dreserved=0 > Searchable archive at > https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2Fdata=02%7C01%7Cliam.revell%40umb.edu%7C6584f4bb63dd48f9205008d762326164%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637085840853226654sdata=qgug0JIrKlC9fpr8lqrMuBku2XpHV3hfbzPF0m8ByuI%3Dreserved=0 > ___ 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/