Hello Everyone,
Just to stack onto Liam’s “fun with apply family of functions”, I often use lapply, which works well by having a function that only applies to the objects in a list, but often — and for difficult to decipher reasons — breaks down if the function is not predefined, is complex, has a few arguments to vary, or might require several steps. As a way to deal with this kind of issue, I often define a new list for lapply and avoid using the list I want to work as the target. Here is an attempt to show how that can be done (but I think Liam’s suggestion is actually better) n <- length(tree) newTree <- lapply(1:n, function(j){ tree.j <- tree[[j]] bind.tip(tree.j, bind.tip, tip.label="Cercopithecus_albogularis", position=0.59, edge.length = 0.59, where=mrca(tree.j)["Cercopithecus_mitis","Cercopithecus_mitis"]) }) Another way this could be done is to make multiple lists and use the Map function, which is basically a modification of the mapply example Liam used. (This example also uses Liam’s suggestion to use fastMRCA.) tip.label <- lapply(1:n, function(.) "Cercopithecus_albogularis") MRCA <- lapply(1:n, function(j) fastMRCA(tree[[j]], "Cercopithecus_albogularis", "Cercopithecus_albogularis")) newTree <- Map(function(tr, tl, m) bind.tip(tr, tl, position=0.59, edge.length = 0.59, where = m), tree, tip.label, MRCA ) Finally, the do.call function can be helpful when repeating a function that has only one or few arguments among several changing over a list. Building on previous set up, one could do this bind.args <- list(tree = tree[[1]], tip.label = “Cercopithecus_albogularis”, position=0.59, edge.length = 0.59, where = MRCA[[1]], interactive = FALSE) newTree <- lapply(1:n, function(j){ bind.args$tree <- tree[[j]] bind.args$where <- MRCA[[j]] do.call(bind.tip, bind.args) }) I also suggest these without verification with real data. Cheers! Mike > On Jun 2, 2021, at 9:11 PM, Liam J. Revell <liam.rev...@umb.edu> wrote: > > Dear Russell et al. > > Using a for loop is a great idea! Highly underrated in R, IMO. ;) > > However, for future reference, the reason that your code didn't work with > lapply is because the list you're 'applying' over (tree) also appears among > the arguments! > > If you want to use apply-family functions instead of a for loop (just, say, > for fun) then you have two basic options: you can write a custom function; or > you can use mapply. > > Here's some (untested) code to do it. > > ## first, using a custom function & lapply: > foo<-function(tree) bind.tip(tree, > tip.label="Cercopithecus_albogularis", > position=0.59,edge.length=0.59, > where=getMRCA(tree,tip=c("Cercopithecus_mitis", > "Cercopithecus_mitis"))) > newtree<-lapply(tree,foo) > class(newtree)<-"multiPhylo" > > ## now, using mapply: > newtree<-mapply(bind.tip,tree=tree,where=lapply(tree,getMRCA, > tip=c("Cercopithecus_mitis","Cercopithecus_mitis")), > MoreArgs=list(tip.label="Cercopithecus_albogularis", > position=0.59,edge.length=0.59),SIMPLIFY=FALSE) > class(newtree)<-"multiPhylo" > > (Code is not guaranteed! I don't have the data file, so I didn't actually > test it -- but something like this ought to work.) > > Regardless, I recommend using ape::getMRCA (or phytools::fastMRCA) because > otherwise you're computing an N x N matrix in each iteration of your function > call just to get one node index. > > Good luck! All the best, Liam > > Liam J. Revell > University of Massachusetts Boston [Assoc. Prof.] > Universidad Católica de la Ssma Concepción [Adj. Res.] > > Web & phytools: > http://faculty.umb.edu/liam.revell/, http://www.phytools.org, > http://blog.phytools.org > > Academic Director UMass Boston Chile Abroad: > https://www.umb.edu/academics/caps/international/biology_chile > > U.S. COVID-19 explorer web application: > https://covid19-explorer.org/ > > On 6/2/2021 8:18 PM, Nathan Upham wrote: >> EXTERNAL SENDER >> Hi Russell: >> Glad to hear you’re using the VertLife mammal trees — they are built on a >> taxonomy of 5,911 species of which only 4,098 are sampled for DNA, so there >> is already a ~30% chunk that is placed using taxonomic constraints and >> birth-death branch lengths as sampled during the estimation of 28 Bayesian >> patch clades. >> Adding additional species described since the 2015 cutoff of that VertLife >> taxonomy makes sense (e.g., up to ~6,500 species on mammaldiversity.org). >> However, keep in mind that they will not have birth-death estimated branch >> lengths, but rather more likely be added as a polygamy to given clade and >> then randomly resolved. >> Given the sample code you provided, the key thing you’ll want to do is run a >> *loop* rather than using lapply, so that you can specify a given tree each >> time, e.g.: >> newtrees<-vector(“list”,length(trees)) >> for(j in 1:length(trees)){ >> newtrees[[j]] <- bind.tip(tree=trees[[j]], >> tip.label="Cercopithecus_albogularis”, position=0.59,edge.length = 0.59, >> where=mrca(tree[[j]])["Cercopithecus_mitis","Cercopithecus_mitis"]) >> } >> I also wrote some code to prune mammal trees and add extinct Caribbean >> species, which uses a similar approach of making polytomies and randomly >> resolving them — here is the repo: >> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fn8upham%2FCaribbeanExtinctions-WTWTW%2Ftree%2Fmaster%2FmamPhy_pruningCode&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674838188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=KeZWNETOkPBekL3j5AIr2hygW49PbdSKImMV39QTXtE%3D&reserved=0 >> And here is the code file: >> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fn8upham%2FCaribbeanExtinctions-WTWTW%2Fblob%2Fmaster%2FmamPhy_pruningCode%2FpruningCode_MamPhy-to-CaribbeanTaxa.R&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674838188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=10u8o7U8dqC1pyK6GteNycoSACTOXAO2VaHNJmTeddk%3D&reserved=0 >> Hope that helps, >> —nate >> ======================================================================== >> Nathan S. Upham, Ph.D. (he/him) >> Assistant Research Professor & Associate Curator of Mammals >> Arizona State University, School of Life Sciences >> ~> Check out the new Mammal Tree of Life >> <https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fvertlife.org%2Fdata%2Fmammals%2F&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674838188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=DbfdSFyjoCtDqUWuVoRs%2BC8Toe3v2BuFHgIpBTim9M0%3D&reserved=0> >> and the Mammal Diversity Database >> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmammaldiversity.org%2F&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674838188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=sYqcCTEhWozvCAIC1vdxbBf5qeJq%2FBsV54zDRuOKABE%3D&reserved=0> >> Research Associate, Yale University (Ecology and Evolutionary Biology) >> Research Associate, Field Museum of Natural History (Negaunee Integrative >> Research Center) >> Chair, Biodiversity Committee, American Society of Mammalogists >> Taxonomy Advisor, IUCN/SSC Small Mammal Specialist Group >> personal web: n8u.org | Google Scholar >> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fscholar.google.com%2Fcitations%3Fhl%3Den%26user%3DzIn4NoUAAAAJ%26view_op%3Dlist_works%26gmla%3DAJsN-F6ybkfthmTdjTpow6sgMhWKn1EKcfNtmIF_wzZcev7yeHuEu5_aolFS85rWiVRHpiQgbwg43i6eS6kArrabLdFL4bntzUSRmlRP2CW4lbZqeEcColw&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674838188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=sa9VBUNzHzzPUecVRmknFoa36gEQA7hySS7uFuxXh9o%3D&reserved=0> >> | ASU profile >> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fisearch.asu.edu%2Fprofile%2F3682356&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674838188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=D9p3TXN7AdorpSqBqnydAvKNoPXOGZ0wR7ESw8bB4H0%3D&reserved=0> >> e: nathan.up...@asu.edu | Skype: nate_upham | Twitter: @n8_upham >> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftwitter.com%2Fn8_upham&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=OM3Q8KIzJ%2BpAvHy6Z5B74%2FD%2Bsy7LkDGVCSAotBNwjnU%3D&reserved=0> >> ======================================================================== >>> On Jun 2, 2021, at 4:19 PM, Eliot Miller <eliot.is...@gmail.com> wrote: >>> >>> Hi Russell, >>> >>> A package I wrote a while back should be able to do that fairly easily. >>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__https%3A%2F%2Fgithub.com%2Feliotmiller%2FaddTaxa__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQrzxl2aIw%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=kgDYJXKFXRFIM8mv1x8fViY6QDiEjTQ8qP5mQ0SUq3k%3D&reserved=0 >>> The only paper it's described in >>> remains >>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__https%3A%2F%2Fbsapubs.onlinelibrary.wiley.com%2Fdoi%2Ffull%2F10.3732%2Fajb.1500195__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQp7PnRRHg%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=DmHEuwIvxP0uiRhdBPZ%2BuUWUoVpIQ5PW8Xfrjyv9Nyg%3D&reserved=0 >>> It's a wrapper for bind.tip, with some additional stuff. You basically >>> would give it a taxonomic file where you identify the clades you're >>> interested in (e.g. both of those Cercopithecus species could be named some >>> unique clade name and off you go, it'd add the missing one to the other), >>> then lapply that whole addTaxa command over the list of trees in >>> multiPhylo. At some point I made laser a dependency, and it's possible I >>> left it in that state. If that's the case, you can still get laser from old >>> CRAN mirrors I believe. Let me know if you want more help. >>> >>> Best, >>> Eliot >>> >>> On Wed, Jun 2, 2021 at 7:05 PM Russell Engelman <neovenatori...@gmail.com> >>> wrote: >>> >>>> Dear R-sig-phylo, >>>> >>>> I have been working with a mammalian phylogeny I recently downloaded from >>>> VertLife >>>> (https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__http%3A%2F%2Fvertlife.org%2Fphylosubsets%2F__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQoEdEnMAg%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=j6H0DVJ0hHT3CgQa6N3ySbpkOj%2FmSifnMnSuZlFOEEE%3D&reserved=0 >>>> ). Unfortunately, the phylogeny >>>> is missing a large number of species, so I am trying to manually add these >>>> taxa to the phylogeny. I have a series of 100 trees that I am using to do >>>> things such as test for phylogenetic signal. I know how to use bind.tip to >>>> add new taxa to a single tree, but I am having more trouble with a >>>> multiPhylo object. I am primarily adding these taxa by placing them as >>>> sister to their nearest included relative (since most of them are elevated >>>> former subspecies), but the issue here is that in the 100 trees in the >>>> multiPhylo object the node representing the taxon to bind these taxa to is >>>> not the same across all trees due to shifting topologies. >>>> >>>> This is an example of the code I have been using, in which "tree" is the >>>> tree object. This works for a single 'phylo' tree but not 'multiphylo'. >>>> >>>> ``` >>>> newtree<-lapply(tree,bind.tip,tip.label="Cercopithecus_albogularis", >>>> position=0.59,edge.length = 0.59, >>>> >>>> where=mrca(tree)["Cercopithecus_mitis","Cercopithecus_mitis"]) >>>> ``` >>>> >>>> Now, this code will not work, but I know exactly why: 'tree' is a >>>> multiPhylo object and so the 'where' argument cannot find the node for the >>>> terminal taxon. However, the issue is how can I tell R to repeat this >>>> 'where' argument for each of the 100 trees, since the node in question is >>>> not identical across these trees? Is there an easier way to do this than >>>> using the 'mrca' call for each terminal taxon? I've noticed adding a 'mrca' >>>> argument also increases computation time and if I am reinventing the wheel >>>> it would be nice to know if I am overthinking things. >>>> >>>> Sincerely, >>>> Russell >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> _______________________________________________ >>>> R-sig-phylo mailing list - R-sig-phylo@r-project.org >>>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylo__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQqcmU5jCQ%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=9qGpfdSpUHFbwBSiKL9t962V%2FRbNXrD1wl0QtZRoMao%3D&reserved=0 >>>> Searchable archive at >>>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2F__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQq_N5pSwg%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=mRFMrjsvvxLFqUgUpVEog3jySUxoY2uY%2FhzRUEMJZd0%3D&reserved=0 >>>> >>> >>> [[alternative HTML version deleted]] >>> >>> _______________________________________________ >>> R-sig-phylo mailing list - R-sig-phylo@r-project.org >>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylo__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQqcmU5jCQ%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=9qGpfdSpUHFbwBSiKL9t962V%2FRbNXrD1wl0QtZRoMao%3D&reserved=0 >>> Searchable archive at >>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2F__%3B!!IKRxdwAv5BmarQ!OZj7-dFRbxvUothKjSj6hr9B0eXscAO6LVWi1-a0w3J_PxlDqvsFDNb0lQq_N5pSwg%24&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=mRFMrjsvvxLFqUgUpVEog3jySUxoY2uY%2FhzRUEMJZd0%3D&reserved=0 >> [[alternative HTML version deleted]] >> _______________________________________________ >> R-sig-phylo mailing list - R-sig-phylo@r-project.org >> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylo&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=luOOuULSOWEOrL45dkzafC5IfT8lNOuKlKikvIvO5gg%3D&reserved=0 >> Searchable archive at >> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2F&data=04%7C01%7Cliam.revell%40umb.edu%7C1174c283b881427e2df508d926253e2a%7Cb97188711ee94425953c1ace1373eb38%7C0%7C1%7C637582763674848184%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=K4GsTiMEghaUDcciDNJ0NMb2HYCUmtVxQoVpP%2FmFI7c%3D&reserved=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/