I realize this might be niche–and the current stuff out there that is tree specific don’t apply in my case.
Trying to annotate the branches of a phylogenetic tree. So these are amino acid sequences. I want the branches to have what are the specific amino acid changes relative to the first sequence in the alignment (also imported). When I check the annotations, the positions are correct, the specific mutations (non-synonymous) not so much. But it is between my “step 7” and “step 8” that things go south. Here is the current script with the specific error when run:
# Load necessary packages
library(ape)
library(seqinr)
library(ggtree)
library(tidytree)
library(dplyr)
# Step 1: Read and parse the existing Newick tree
tree <- read.tree("test.newick")
# Debug: Print the tree to check structure
print(tree)
# Step 2: Read the amino acid sequences
sequences <- read.fasta(file = "test.fasta", as.string = TRUE)
seq_names <- names(sequences)
# Debug: Print sequences to check
print(seq_names)
# Step 3: Identify non-synonymous changes relative to the first sequence
ref_sequence <- sequences[[1]]
ref_aa <- strsplit(ref_sequence, "")[[1]]
non_syn_changes <- lapply(sequences[-1], function(seq) {
compare_aa <- strsplit(seq, "")[[1]]
changes <- which(ref_aa != compare_aa)
changes
})
# Debug: Print non-synonymous changes
print(non_syn_changes)
# Step 4: Create a data frame for branch annotations
annotations <- data.frame(
node = (Ntip(tree) + 1):(Ntip(tree) + Nnode(tree)),
label = "",
stringsAsFactors = FALSE
)
# Step 5: Populate annotations with amino acid changes
for (i in seq_along(non_syn_changes)) {
changes <- non_syn_changes[[i]]
if (length(changes) > 0) {
annotations$label[i] <- paste("Amino acid changes:", paste(changes, collapse = ","))
}
}
# Debug: Print annotations to check
print(annotations)
# Step 6: Convert tree to ggtree object
ggt <- ggtree(tree)
# Debug: Print ggtree object
print(ggt)
# Step 7: Add annotation of amino acid changes
annotations <- filter(annotations, label != "") # Filter out empty labels
# Ensure correct node mapping for annotations
ggt <- ggt %<+% annotations + geom_text(aes(label = label), hjust = -0.3)
# Debug: Print ggtree object after annotation
print(ggt)
# Step 8: Save the plot as PDF
ggsave("annotated_phylogenetic_tree.pdf", ggt, width = 10, height = 8)
And this is the error I keep getting:
Error in `auto_copy()`:
! `x` and `y` must share the same src.
ℹ `x` is a <tbl_tree/tbl_tree/tbl_df/tbl/data.frame> object.
ℹ `y` is an integer vector.
ℹ Set `copy = TRUE` if `y` can be copied to the same source as `x` (may be slow).
Run `rlang::last_trace()` to see where the error occurred.
I have tried a number of various workarounds and even adapting pre-existing tools. But a tree should be considered a “graph”. I am trying to get Figure 5 in terms of the tree from (https://besjournals.onlinelibrary.wiley.com/doi/full/10.1111/2041-210x.12628)
Adam Capoferri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.