Skip to content

fixed plot_phyloseq checking for wrong slots ... #1330

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions R/IO-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ import_qiime <- function(otufilename=NULL, mapfilename=NULL,
#' read_tree(system.file("extdata", "GP_tree_rand_short.newick.gz", package="phyloseq"))
read_tree <- function(treefile, errorIfNULL=FALSE, ...){
# "phylo" object provided directly
if( class(treefile)[1] %in% c("phylo") ){
if( is(treefile, "phylo") ){
tree <- treefile
} else {
# file path to tree file provided.
Expand Down Expand Up @@ -637,7 +637,7 @@ import_env_file <- function(envfilename, tree=NULL, sep="\t", ...){
# Convert to otu_table-class table (trivial table)
physeq <- envHash2otu_table(tipSampleTable)
# If tree is provided, combine it with the OTU Table
if( class(tree) == "phylo" ){
if( is(tree, "phylo") ){
# Create phyloseq-class with a tree and OTU Table (will perform any needed pruning)
physeq <- phyloseq(physeq, tree)
}
Expand Down Expand Up @@ -1472,8 +1472,8 @@ import_mothur_dist <- function(mothur_dist_file){
#' myDistObject <- as.dist(ape::cophenetic.phylo(phy_tree(esophagus)))
#' export_mothur_dist(myDistObject)
export_mothur_dist <- function(x, out=NULL, makeTrivialNamesFile=NULL){
if( class(x)== "matrix" ){ x <- as.dist(x) }
if( class(x)!= "dist" ){ stop("x must be a dist object, or symm matrix") }
if( is.matrix(x) ){ x <- as.dist(x) }
if( !is(x, "dist") ){ stop("x must be a dist object, or symm matrix") }

# While x is a dist-object, get the length of unique pairs
# to initialize the dist table.
Expand Down Expand Up @@ -1769,9 +1769,9 @@ import_biom <- function(BIOMfilename,
argumentlist <- list()

# Read the data
if(class(BIOMfilename)=="character"){
if( is.character(BIOMfilename) ){
x = read_biom(biom_file=BIOMfilename)
} else if (class(BIOMfilename)=="biom"){
} else if (is(BIOMfilename, "biom")){
x = BIOMfilename
} else {
stop("import_biom requires a 'character' string to a biom file or a 'biom-class' object")
Expand Down
12 changes: 6 additions & 6 deletions R/merge-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ setMethod("merge_phyloseq_pair", signature("phylo", "phylo"), function(x, y){
#' @aliases merge_phyloseq_pair,XStringSet,XStringSet-method
#' @rdname merge_phyloseq_pair-methods
setMethod("merge_phyloseq_pair", signature("XStringSet", "XStringSet"), function(x, y){
if( class(x) != class(y) ){
if( is(x, class(y)) ){
# if class of x and y don't match, throw warning, try anyway (just in case)
warning("For merging reference sequence objects, x and y should be same type.\n",
"That is, the same subclass of XStringSet. e.g. both DNAStringSet.\n",
Expand Down Expand Up @@ -522,11 +522,11 @@ setMethod("merge_samples", signature("sample_data"), function(x, group, fun=mean
x1 <- data.frame(x)

# Check class of group and modify if "character"
if( class(group)=="character" & length(group)==1 ){
if( is.character(group) & length(group)==1 ){
if( !group %in% colnames(x) ){stop("group not found among sample variable names.")}
group <- x1[, group]
}
if( class(group)!="factor" ){
if( !is.factor(group) ){
# attempt to coerce to factor
group <- factor(group)
}
Expand Down Expand Up @@ -571,20 +571,20 @@ setMethod("merge_samples", signature("phyloseq"), function(x, group, fun=mean){
# Check if phyloseq object has a sample_data
if( !is.null(sample_data(x, FALSE)) ){
# Check class of group and modify if single "character" (column name)
if( class(group)=="character" & length(group)==1 ){
if( is.character(group) & length(group)==1 ){
x1 <- data.frame(sample_data(x))
if( !group %in% colnames(x1) ){stop("group not found among sample variable names.")}
group <- x1[, group]
}
# coerce to factor
if( class(group)!="factor" ){ group <- factor(group) }
if( !is.factor(group) ){ group <- factor(group) }
# Perform merges.
newSM <- merge_samples(sample_data(x), group, fun)
newOT <- merge_samples(otu_table(x), group)
phyloseqList <- list(newOT, newSM)
# Else, the only relevant object to "merge_samples" is the otu_table
} else {
if( class(group)!="factor" ){ group <- factor(group) }
if( !is.factor(group) ){ group <- factor(group) }
phyloseqList <- list( newOT=merge_samples(otu_table(x), group) )
}

Expand Down
8 changes: 4 additions & 4 deletions R/network-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ make_network <- function(physeq, type="samples", distance="jaccard", max.dist =

if( type %in% c("taxa", "species", "OTUs", "otus", "otu")){
# Calculate or asign taxa-wise distance matrix
if( class(distance) == "dist" ){
if( is(distance,"dist") ){
# If distance a distance object, use it rather than re-calculate
obj.dist <- distance
if( attributes(obj.dist)$Size != ntaxa(physeq) ){
Expand All @@ -109,7 +109,7 @@ make_network <- function(physeq, type="samples", distance="jaccard", max.dist =
if( !setequal(attributes(obj.dist)$Labels, taxa_names(physeq)) ){
stop("taxa_names does not exactly match dist-indices")
}
} else if( class(distance) == "character" ){
} else if( is.character(distance) ){
# If character string, pass on to distance(), assume supported
obj.dist <- distance(physeq, method=distance, type=type, ...)
# Else, assume a custom function and attempt to calculate.
Expand All @@ -127,7 +127,7 @@ make_network <- function(physeq, type="samples", distance="jaccard", max.dist =
CoMa <- TaDiMa < max.dist
} else if( type == "samples" ){
# Calculate or asign sample-wise distance matrix
if( class(distance) == "dist" ){ # If argument is already a distance matrix.
if( is(distance, "dist") ){ # If argument is already a distance matrix.
# If distance a distance object, use it rather than re-calculate
obj.dist <- distance
if( attributes(obj.dist)$Size != nsamples(physeq) ){
Expand All @@ -137,7 +137,7 @@ make_network <- function(physeq, type="samples", distance="jaccard", max.dist =
stop("sample_names does not exactly match dist-indices")
}
# If character string, pass on to distance(), assume supported
} else if( class(distance) == "character" ){
} else if( is.character(distance) ){
# Else, assume a custom function and attempt to calculate.
obj.dist <- distance(physeq, method=distance, type=type, ...)
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/ordination-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ ordinate = function(physeq, method="DCA", distance="bray", formula=NULL, ...){
# Define ps.dist. Check the class of distance argument is character or dist
if( inherits(distance, "dist") ){
ps.dist <- distance
} else if( class(distance) == "character" ){
} else if( is.character(distance) ){
# There are some special options for NMDS/metaMDS if distance-method
# is supported by vegdist, so check first. If not, just calculate distance
vegdist_methods <- c("manhattan", "euclidean", "canberra", "bray",
Expand Down Expand Up @@ -323,7 +323,7 @@ ordinate = function(physeq, method="DCA", distance="bray", formula=NULL, ...){
#' plot_ordination(GP, GP.dpcoa, color="SampleType")
DPCoA <- function(physeq, correction=cailliez, scannf=FALSE, ...){
# Check that physeq is a phyloseq-class
if(!class(physeq)=="phyloseq"){stop("physeq must be phyloseq-class")}
if(!is(physeq, "phyloseq")){stop("physeq must be phyloseq-class")}

# Remove any OTUs that are absent from all the samples.
physeq <- prune_taxa((taxa_sums(physeq) > 0), physeq)
Expand Down
8 changes: 4 additions & 4 deletions R/plot-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ setGeneric("plot_phyloseq", function(physeq, ...){ standardGeneric("plot_phylose
#' @aliases plot_phyloseq,phyloseq-method
#' @rdname plot_phyloseq-methods
setMethod("plot_phyloseq", "phyloseq", function(physeq, ...){
if( all(c("otu_table", "sample_data", "phy_tree") %in% getslots.phyloseq(physeq)) ){
if( all(c("otu_table", "sam_data", "phy_tree") %in% getslots.phyloseq(physeq)) ){
plot_tree(esophagus, color="samples")
} else if( all(c("otu_table", "sample_data", "tax_table") %in% getslots.phyloseq(physeq) ) ){
} else if( all(c("otu_table", "sam_data", "tax_table") %in% getslots.phyloseq(physeq) ) ){
plot_bar(physeq, ...)
} else if( all(c("otu_table", "phy_tree") %in% getslots.phyloseq(physeq)) ){
plot_tree(esophagus, color="samples")
Expand Down Expand Up @@ -1168,7 +1168,7 @@ rm.na.phyloseq <- function(DF, key.var){
# DF[!is.na(DF[, key.var]), ]
DF <- subset(DF, !is.na(eval(parse(text=key.var))))
# (2) Remove NA from the factor level, if a factor.
if( class(DF[, key.var]) == "factor" ){
if( is.factor(DF[, key.var]) ){
DF[, key.var] <- factor(as(DF[, key.var], "character"))
}
return(DF)
Expand Down Expand Up @@ -2840,4 +2840,4 @@ plot_clusgap = function(clusgap, title="Gap Statistic results"){
p = p + ggtitle(title)
return(p)
}
################################################################################
################################################################################
2 changes: 1 addition & 1 deletion R/sampleData-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ subset_samples <- function(physeq, ...){
} else {
oldDF <- as(sample_data(physeq), "data.frame")
newDF <- subset(oldDF, ...)
if( class(physeq) == "sample_data" ){
if( is(physeq, "sample_data") ){
return(sample_data(newDF))
} else {
sample_data(physeq) <- sample_data(newDF)
Expand Down