Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for XStringSet in msaplot #631

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

yasche
Copy link

@yasche yasche commented Sep 13, 2024

Description

Added support for XStringSet objects (i.e., BStringSet, AAStringSet and DNAStringSet) for function msaplot() in response to #626. The problem came up because an old example on R-Bloggers does not work anymore.

In the proposed solution, if the object is of type BStringSet it is first checked, if library Biostrings is installed. If so, a temporary file is created and read via treeio::read.fasta() to circumvent the need to check if it as amino acid or DNA sequence. Otherwise it throws a helpful error message. AAStringSet and DNAStringSet objects are converted using functions from the ape package.

Tests

library(ggtree)
library(Biostrings)
library(phyloseq)
library(ggplot2)
library(scales)


data(GlobalPatterns)
GP <- prune_taxa(taxa_sums(GlobalPatterns) > 0, GlobalPatterns)
GP.chl <- subset_taxa(GP, Phylum=="Chlamydiae")

p <- ggtree(GP.chl, ladderize = FALSE) + geom_text2(aes(subset=!isTip, label=label), hjust=-.2, size=4) +
  geom_tiplab(aes(label=Genus), hjust=-.3) +
  geom_point(aes(x=x+hjust, color=SampleType, shape=Family, size=Abundance),na.rm=TRUE) +
  scale_size_continuous(trans=log_trans(5)) +
  theme(legend.position="right") 

df <- fortify(GP.chl)
barcode <- as.character(df$Barcode_full_length)
names(barcode) <- df$label
barcode <- barcode[!is.na(barcode)]

#With Biostrings installed
##XStringSets
bss_barcode <- Biostrings::BStringSet(barcode)
p1 <- p + ggtitle("reproduce phyloseq by ggtree, BStringSet")
msaplot(p1, bss_barcode, width=.3, offset=.05)
ggsave("plots/bss.svg", height = 9)

aass_barcode <- AAStringSet(bss_barcode)
p2 <- p + ggtitle("reproduce phyloseq by ggtree, AAStringSet")
msaplot(p2, aass_barcode, width=.3, offset=.05)
ggsave("plots/aass.svg", height = 9)

dnass_barcode <- DNAStringSet(bss_barcode)
p3 <- p + ggtitle("reproduce phyloseq by ggtree, DNAStringSet")
msaplot(p3, dnass_barcode, width=.3, offset=.05)
ggsave("plots/dnass.svg", height = 9)


##XXX.bin
aabin_barcode <- ape::as.AAbin(aass_barcode)
p4 <- p + ggtitle("reproduce phyloseq by ggtree, AABin")
msaplot(p4, aabin_barcode, width=.3, offset=.05)
ggsave("plots/aabin.svg", height = 9)

dnabin_barcode <- ape::as.DNAbin(dnass_barcode)
p5 <- p + ggtitle("reproduce phyloseq by ggtree, DNABin")
msaplot(p5, dnabin_barcode, width=.3, offset=.05)
ggsave("plots/dnabin.svg", height = 9)


##file
writeXStringSet(bss_barcode, "fastafile.fasta")
p6 <- p + ggtitle("reproduce phyloseq by ggtree, file")
msaplot(p6, "fastafile.fasta", width=.3, offset=.05)
ggsave("plots/file.svg", height = 9)


#Without Biostrings installed
unloadNamespace("phyloseq")
unloadNamespace("Biostrings")
remove.packages("Biostrings")


##XStringSet
a <- list("ABC")
class(a) <- "BStringSet"
p7 <- p + ggtitle("reproduce phyloseq by ggtree, BStringSet without Biostrings")
msaplot(p7, a, width=.3, offset=.05)

#Error in msaplot(p7, a, width = 0.3, offset = 0.05) : 
#object is of class 'BStringSet' but library 'Biostrings' is not installed...
#-> please install 'Biostrings' from https://bioconductor.org/packages/Biostrings for handling objects of type 'BStringSet'.

The function produces the expected outcome:

bss
aass
dnass
file
aabin
dnabin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant