无法保存plotBS()输出的树数据——基于phyDat序列构建进化树的问题
plotBS() in phangorn Got it, I've run into this exact issue before with phangorn's plotBS()! The key thing most people miss is that plotBS() actually returns a modified phylogenetic tree object (with bootstrap values attached as node labels) — you just need to capture that return value instead of only running the function to plot. Here's how to do it step-by-step with your data:
Step 1: Complete Tree Construction & Bootstrap Analysis (if you haven't already)
First, let's tie together your existing sequence data with tree building and bootstrap testing (adjust the tree-building method if you used something like maximum likelihood instead of NJ):
library(seqinr) library(phangorn) library(ape) # Needed for tree-saving functions # Your original sequence setup seqs <- DNAMultipleAlignment( c(a1 = "------ATGTTCATTAACCGCTGACTATTCTCAACCA", a2 = "------ATGTTCACCGACCGCTGACTATTCTCTACAA", a3 = "---GTGACCTTCATCAACCGATGATTATTCTCAA---", a4 = "---TCAGTCGTCACCAGGCGTTG-CAGGACCCGAC--", a5 = "ATGGGGGTCTTCCTCA-TCGCCGTCGCCGCGT-----")) phyDat_seqs <- as.phyDat(seqs) # Build initial tree (example: Neighbor-Joining) dm <- dist.ml(phyDat_seqs) base_tree <- NJ(dm) # Run bootstrap replicates (adjust `bs` to your desired number of replicates) bs_replicates <- bootstrap.phyDat(phyDat_seqs, FUN = function(x) NJ(dist.ml(x)), bs = 100)
Step 2: Capture the Bootstrap Tree from plotBS()
Instead of just running plotBS() to visualize, assign its output to a variable — this variable will hold your tree with bootstrap values embedded:
# Plot the bootstrap tree AND capture the tree object bootstrap_tree <- plotBS(base_tree, bs_replicates, type = "phylogram", # Adjust plot type as needed bs.col = "blue", # Customize bootstrap text color bs.adj = 0.5) # Adjust bootstrap text position
Step 3: Save the Bootstrap Tree to File
Now use ape package functions to write the tree to a standard phylogenetic file format (Newick or Nexus):
# Save as Newick format (most common for tree viewers) write.tree(bootstrap_tree, file = "bootstrap_tree.nwk") # OR save as Nexus format write.nexus(bootstrap_tree, file = "bootstrap_tree.nex")
Key Notes:
- The
bootstrap_treeobject is a standardphyloclass object, so any tree-manipulation or saving function that works withphyloobjects will work here. - Bootstrap values are stored in the
node.labelslot of thebootstrap_treeobject — you can verify this withbootstrap_tree$node.label.
内容的提问来源于stack exchange,提问作者Mel

