3
$\begingroup$

I have a phylogenetic tree with a lot of different populations and I wanted to removes most of them and apply new names to the labels. I wrote this code:

library(ape)
library(phylotools)
tree<- read.tree("Consensus_COMBINED_Cod3genesRatesC_trees.trees")

tree<- drop.tip(tree, tip = c("CeuMist2","CeuMist1","Ceu_PAN_2","Ceu_PAN_1","Ceu_PAN_3","Ceu_PAN_4"))
tree<- drop.tip(tree, tip= c("CcNav1","CcNav2","Cc_Mal_1","Cc_KalR2_2","CcRod1","CcRod2","CcRod3","CcRod4","CcHr1"))
tree<- drop.tip(tree, tip= c("CeST6_4","CeST6_5","CeAA7_1","CeST6_3","CeST6_2"))
tree<- drop.tip(tree, tip= c("Cg_MelR3_1","Cg_MelR6_2","Cg_MelR7_2","Cg_MelR6_1","Cg_MelR7_3","Cg_MelR5_1","Cg_MelR4_2","Cg_MelR1_1","Cg_MelR2_1","Cg_MelR4_1","Cg_MelR7_4","Cg_MelR5_2"))
tree<- drop.tip(tree, tip= c("ChTM5_1","ChTM4_3","ChTM4_1","Ch_MEN_5","Ch_MEN_13","Ch_MEN_8","Ch_LEV_3","Ch_LEV_5","Ch_LEV_4","Ch_MEN_6","Ch_MEN_10","Ch_LEV_2","Ch_MEN_4","Ch_MEN_14","Ch_LEV_1","Ch_MEN_11","Ch_MEN_3","Ch_MEN_9"))
tree<- drop.tip(tree, tip= c("Ci_MSP_2","Ci_MSP_1","Ci_DIA_1","Ci_DIA_2","CiPLAN1","CiPLAN3","CiPLAN2","Ci_MEN_1","Ci_MEN_2","Ci_MEN_12"))
tree<- drop.tip(tree, tip= c("CpGravR3","CpGravR4","CpGio_2","CpGravR5_2","CpGravR2","CpGravR7","CpGio_1","CpGio_3","CpGravR1","CpGravR6_2","CpGravR5_1","CpThe_1","CpSka_1"))
tree<- drop.tip(tree, tip= c("Cn_IG_2","Cn_IG_3","Cn_IG_1","LevantinaFC4779","Assyriella_naegelei"))

newlabels<- c("C. eucineta", "C. codringtonii", "C. elisabethae","C. gittenbergeri", "C. helenae", "C. intusplicata", "C. parnassia")
tree$tip.label<- newlabels

write.nexus(tree, "CodTree.trees")

When I use the write.nexus command I get the following error:

Error in y$tip.label : $ operator is invalid for atomic vectors

How can I circumvent this error and export the tree in nexus format?

I will also provide the tree here: https://drive.google.com/file/d/1lYmzW9htwaclC_yYs3fQom-Z3zbjk8wH/view?usp=drive_link

$\endgroup$
1
  • $\begingroup$ Hi @Nickmofoe I'll look at this in detail, but will not be until the end of the week (really busy week). $\endgroup$
    – M__
    Commented Oct 16, 2023 at 13:52

1 Answer 1

3
$\begingroup$

So after digging I found the solution. What worked for me was adding the file = in the write.nexus command. The final line should be write.nexus(tree, file = "Cod.trees")

$\endgroup$
1
  • $\begingroup$ Ahhh! Right of course! Many thanks for posting back. Could you very kindly mark you post as "accepted". Admittedly, you don't any bonus rep, but it helps site statistics and it is definitely the solution. I completely missed the file =. $\endgroup$
    – M__
    Commented Oct 17, 2023 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.