I want to create some species phylogenetic trees. I've reached the point where I get a tree, but it has multiple tips per species. I want only one tip per species but I don't want to randomly prune the tips. So I decided to collapse the nodes that include records of a single species. I've managed to do that using ggtree.
The problem is that I need to do some analysis using the collapsed tree and I can't manage to save the figure of the collapsed tree as a tree object, although I tried to to that using the "as.phylo()", "as.treedata()" and "write.jtree()" commands form the treeio package.
Following is the code I've used to collapse the nodes and the code I used to save the collapsed tree as a new tree.
library(tidyverse)
library(ggtree)
library(treeio)
tree<-
structure(list(edge = structure(c(20L, 20L, 21L, 22L, 23L, 23L,
22L, 24L, 25L, 25L, 24L, 26L, 27L, 28L, 28L, 29L, 29L, 30L, 30L,
27L, 31L, 31L, 32L, 33L, 34L, 34L, 33L, 32L, 35L, 35L, 26L, 21L,
36L, 36L, 20L, 1L, 21L, 22L, 23L, 2L, 3L, 24L, 25L, 4L, 5L, 26L,
27L, 28L, 6L, 29L, 7L, 30L, 8L, 9L, 31L, 10L, 32L, 33L, 34L,
11L, 12L, 13L, 35L, 14L, 15L, 16L, 36L, 17L, 18L, 19L), .Dim = c(35L,
2L)), edge.length = c(0.0069197, 0.0986751, 0.092181, 0.0641865,
0.0113126, 6.7051e-07, 0.0455608, 0.029679, 0.00373064, 6.7051e-07,
0.0177002, 0.0211424, 0.0145342, 0.113045, 0.00749355, 0.0792466,
0.0275828, 0.0501727, 0.0325458, 0.0349394, 0.128613, 0.0991442,
0.0618112, 0.00563612, 6.7051e-07, 6.7051e-07, 0.016074, 0.0297801,
0.00355328, 0.0192589, 0.496174, 0.0368994, 6.7051e-07, 0.0187024,
6.7051e-07), Nnode = 17L, node.label = c("", "99", "99", "97",
"85", "88", "62", "76", "58", "42", "89", "84", "99", "97", "69",
"83", "89"), tip.label = c("Centrodontus_atlas_paucivenosus_AY498481.1",
"Micrutalis_calva_AY593641.1", "Micrutalis_calva_AY498506.1",
"Enchenopa_binotata_AY593689.1", "Enchenopa_binotata_AY498493.1",
"Alchisme_grossa_AY593721.1", "Aconophora_compressa_AY593651.1",
"Campylenchia_latipes_AY593684.1", "Kronides_incumbens_AY593679.1",
"Stalotypa_fairmairii_AY593705.1", "Umbonia_spinosa_AY593718.1",
"Umbonia_reducta_AY593717.1", "Umbonia_ataliba_AY593716.1", "Umbonia_crassicornis_AY593715.1",
"Umbonia_crassicornis_AY593714.1", "Aetalion_reticulatum_AY593611.1",
"Microcentrus_caryae_AY593625.1", "Microcentrus_caryae_AY498507.1",
"Centrodontus_atlas_AY593630.1")), class = "phylo", order = "cladewise")
t=ggtree(tree) #collapse the nodes cp <- collapse(t, node=35) cp <- collapse(cp, node=25) cp <- collapse(cp, node=23) cp <- collapse(cp, node=36)
#create a picture of the tree cp + geom_tiplab(align=TRUE, linesize=1)+ xlim(0,1.3)+ #the next line adds bootstap values geom_text2(aes(subset=!isTip&node!=35&node!=25&node!=23&node!=36,label=label), hjust=-.2,size=2.5)+ geom_cladelabel(node=c(1,19),label = "same species?", color="red", offset=1.2)+ #the next lines indicate the collapsed clades geom_point2(aes(subset=(node == 35)), size=4, shape=21, fill="red")+ geom_point2(aes(subset=(node == 25)), size=4, shape=21, fill="red")+ geom_point2(aes(subset=(node == 23)), size=4, shape=21, fill="red")+ geom_point2(aes(subset=(node == 36)), size=4, shape=21, fill="red")+ # the next line adds the label of the collapsed node geom_cladelabel(node=35, "Umbonia_crassicornis",align = T,color = "red")+ geom_cladelabel(node=25, "Enchenopa_binotata",align = T,color = "red")+ geom_cladelabel(node=23, "Micrutalis_calva",align = T,color = "red")+ geom_cladelabel(node=36, "Microcentrus_caryae",align = T,color = "red")+ #add title ggtitle("wingless")+ theme(plot.title = element_text(hjust = 0.5,size=20))
The above code creates an image showing the original tree with some collapsed nodes. My goal is to have a tree file that when used in a command like plot() it will give the same tree with the collapsed nodes. I tried to do that with the following code but it didn't work.
collapsed_tree<- cp + geom_tiplab(align=TRUE, linesize=1)+ xlim(0,1.3)+ #the next line adds bootstap values geom_text2(aes(subset=!isTip&node!=35&node!=25&node!=23&node!=36,label=label), hjust=-.2,size=2.5)+ geom_cladelabel(node=c(1,19),label = "same species?", color="red", offset=1.2)+ #the next lines indicate the collapsed clades geom_point2(aes(subset=(node == 35)), size=4, shape=21, fill="red")+ geom_point2(aes(subset=(node == 25)), size=4, shape=21, fill="red")+ geom_point2(aes(subset=(node == 23)), size=4, shape=21, fill="red")+ geom_point2(aes(subset=(node == 36)), size=4, shape=21, fill="red")+ # the next line adds the label of the collapsed node geom_cladelabel(node=35, "Umbonia_crassicornis",align = T,color = "red")+ geom_cladelabel(node=25, "Enchenopa_binotata",align = T,color = "red")+ geom_cladelabel(node=23, "Micrutalis_calva",align = T,color = "red")+ geom_cladelabel(node=36, "Microcentrus_caryae",align = T,color = "red")+ #add title ggtitle("wingless")+ theme(plot.title = element_text(hjust = 0.5,size=20)) collapsed_tree<-as.phylo(collapsed_tree) #also tried as.treedata() write.jtree(collapsed_tree,"collapsed_tree.nw")
The above commands give a tree that is pretty much the same as the original.
A second, minor issue I have is that I can't put line to connect the collapsed tips of the tree with the new, aligned labels, so they look the same way as the not collapsed tips, where the aligned labels are automatically connected with the tips with dotted lines.
Any help will be greatly appreciated!
duplicated, see <https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/UeLEdMYndrA/hZghmSkUCwAJ>.