I'm not sure how it scales to a large tree, though! For an example
phylogeny of 1,000 tips and a clade of 116 species it ran in the blink
of an idea, so I suppose it's probably OK.
Obviously, I meant to write "blink of an eye." It's been a long day.
Liam J. Revell
Professor of Biology, University of Massachusetts Boston
Web: http://faculty.umb.edu/liam.revell/
Book: Phylogenetic Comparative Methods in R
<https://press.princeton.edu/books/phylogenetic-comparative-methods-in-r>
(/Princeton University Press/, 2022)
On 1/18/2023 5:19 PM, Liam J. Revell wrote:
CAUTION: EXTERNAL SENDER
Dear Jhonny.
If I understand your problem correctly, this will work in /theory/ --
shown for clade/t5/ through /t9/ in your example.
This works when your tips in a sample form a /clade/ -- but will
otherwise sum the internal edges leading to the internal nodes of all
taxa in a set, while not counting any edge twice. I think that's what
you want.
library(phytools)
library(phangorn)
## set tips
tips<-c("t9","t7","t5")
## get ancestors, leaving out the global root
anc<-setdiff(Reduce(union,
lapply(sapply(tips,function(x,y) which(x==y),
y=samptree$tip.label),FUN=Ancestors,x=samptree)),
Ntip(samptree)+1)
## get sum of edge lengths
summed.edges<-sum(samptree$edge.length[sapply(anc,
function(x,y) which(x==y),
y=samptree$edge[,2])])
summed.edges ## this is your value
I'm not sure how it scales to a large tree, though! For an example
phylogeny of 1,000 tips and a clade of 116 species it ran in the blink
of an idea, so I suppose it's probably OK.
Let me know if it works for you.
All the best, Liam
Liam J. Revell
Professor of Biology, University of Massachusetts Boston
Web: http://faculty.umb.edu/liam.revell/
Book: Phylogenetic Comparative Methods in R
<https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpress.princeton.edu%2Fbooks%2Fphylogenetic-comparative-methods-in-r&data=05%7C01%7Cliam.revell%40umb.edu%7C6e8084093e574f7693cf08daf9a21fbe%7Cb97188711ee94425953c1ace1373eb38%7C0%7C0%7C638096771974920509%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=0Fu38rJ14JGirC0URfQYnpyeJe1M9jpTfkisaMS6ymA%3D&reserved=0>
(/Princeton University Press/, 2022)
On 1/18/2023 4:56 PM, Jhonny Capichoni Massante wrote:
CAUTION: EXTERNAL SENDER
Dear colleagues,
I would like to ask for a little help from you.
I have a phylogenetic tree with nine taxa and three samples (see
figure below). Each sample has three taxa from the phylogeny. I am
interested in extracting the internal branch lengths (corresponding
to the ancestors of all tips) that are present in each sample
(represented by the corresponding tips) and summarising them for each
sample (e.g., summing all internal branch lengths on samples). Here
is a rough reproducible example until getting all branch lengths:
#number of taxa
ntax <- 9
#get a phylogeny with 9 taxa
set.seed(999)
samptree <- rcoal(ntax)
#color fot the tips according to their occurrence on samples
colors <- c("blue", "blue", "blue", "green", "green", "green",
"purple", "purple", "purple")
#plot the tree and branch lengths with rounded values
plot(samptree, label.offset = 0.2, tip.color = colors)
edgelabels(round(samptree$edge.length, 2), cex=1, width = 1, height
= 0.1,
frame = "none", bg = NULL, col = "red", offset = 1.5, adj = c(-0.1,
-0.2))
#matrix with samples based on the phylogeny and three samples as in
the figure
sample <- matrix(data =
c(1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1),
nrow = 3,
ncol = 9,
dimnames = list(c("purple", "green", "blue"),
c("t4", "t3", "t6", "t8", "t2", "t1", "t9", "t7", "t5")),
byrow = TRUE)
#get all branch lengths from the tree
all_branch_lenghts <- round(samptree$edge.length, 2)
From here, I could not separate internal branch lengths to tip
lengths and trace these internal branches on samples based on their
occurrence. Does anyone have an idea to do such a thing?
The real phylogeny has around 6,000 tips and the real number of
samples is around 130. Thank you very much for any help!
Here is the figure that exemplifies what I want to do:
Best regards,
--
*Dr Jhonny Capichoni Massante*
Postdoctoral Research Fellow in Macroecology
Universidade de São Paulo
Instituto de Biociências
Departamento de Ecologia
Laboratório de Ecologia de Florestas Tropicais
Twitter
<https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftwitter.com%2FJCMassante&data=05%7C01%7Cliam.revell%40umb.edu%7C6e8084093e574f7693cf08daf9a21fbe%7Cb97188711ee94425953c1ace1373eb38%7C0%7C0%7C638096771974920509%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=IaHwkwgusy%2FuXZxrEOb63Cy6qG4iJcEWAwXohxp9O58%3D&reserved=0>
| RG
<https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.researchgate.net%2Fprofile%2FJhonny-Massante&data=05%7C01%7Cliam.revell%40umb.edu%7C6e8084093e574f7693cf08daf9a21fbe%7Cb97188711ee94425953c1ace1373eb38%7C0%7C0%7C638096771974920509%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FSw9AoFgN%2B%2FxppzcwdXNXldPqqRiO044k6OTjpvNzNo%3D&reserved=0>
| LinkedIn
<https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.linkedin.com%2Fin%2Fjhonny-capichoni-massante%2F&data=05%7C01%7Cliam.revell%40umb.edu%7C6e8084093e574f7693cf08daf9a21fbe%7Cb97188711ee94425953c1ace1373eb38%7C0%7C0%7C638096771974920509%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=DsZwlfHVFxjX5yNkhEXkySVRmj%2FdCztrjiQCEvvQDns%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 athttp://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 athttp://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/