Last active
February 22, 2018 01:47
-
-
Save jnpaulson/324ac1fa3eab1bc7f845 to your computer and use it in GitHub Desktop.
This will convert a biom class object into a phyloseq object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import_biom2 <- function(x, | |
treefilename=NULL, refseqfilename=NULL, refseqFunction=readDNAStringSet, refseqArgs=NULL, | |
parseFunction=parse_taxonomy_default, parallel=FALSE, version=1.0, ...){ | |
# initialize the argument-list for phyloseq. Start empty. | |
argumentlist <- list() | |
x = biom(x) | |
b_data = biom_data(x) | |
b_data_mat = as(b_data, "matrix") | |
otutab = otu_table(b_data_mat, taxa_are_rows=TRUE) | |
argumentlist <- c(argumentlist, list(otutab)) | |
######################################## | |
# Taxonomy Table | |
######################################## | |
# Need to check if taxonomy information is empty (minimal BIOM file) | |
if( all( sapply(sapply(x$rows, function(i){i$metadata}), is.null) ) ){ | |
taxtab <- NULL | |
} else { | |
# parse once each character vector, save as a list | |
taxlist = lapply(x$rows, function(i){ | |
parseFunction(i$metadata$taxonomy) | |
}) | |
names(taxlist) = sapply(x$rows, function(i){i$id}) | |
taxtab = build_tax_table(taxlist) | |
} | |
argumentlist <- c(argumentlist, list(taxtab)) | |
######################################## | |
# Sample Data ("columns" in QIIME/BIOM) | |
######################################## | |
# If there is no metadata (all NULL), then set sam_data <- NULL | |
if( is.null(sample_metadata(x)) ){ | |
samdata <- NULL | |
} else { | |
samdata = sample_data(sample_metadata(x)) | |
} | |
argumentlist <- c(argumentlist, list(samdata)) | |
######################################## | |
# Tree data | |
######################################## | |
if( !is.null(treefilename) ){ | |
if( inherits(treefilename, "phylo") ){ | |
# If argument is already a tree, don't read, just assign. | |
tree = treefilename | |
} else { | |
# NULL is silently returned if tree is not read properly. | |
tree <- read_tree(treefilename, ...) | |
} | |
# Add to argument list or warn | |
if( is.null(tree) ){ | |
warning("treefilename failed import. It not included.") | |
} else { | |
argumentlist <- c(argumentlist, list(tree) ) | |
} | |
} | |
######################################## | |
# Reference Sequence data | |
######################################## | |
if( !is.null(refseqfilename) ){ | |
if( inherits(refseqfilename, "XStringSet") ){ | |
# If argument is already a XStringSet, don't read, just assign. | |
refseq = refseqfilename | |
} else { | |
# call refseqFunction and read refseqfilename, either with or without additional args | |
if( !is.null(refseqArgs) ){ | |
refseq = do.call("refseqFunction", c(list(refseqfilename), refseqArgs)) | |
} else { | |
refseq = refseqFunction(refseqfilename) | |
} | |
} | |
argumentlist <- c(argumentlist, list(refseq) ) | |
} | |
######################################## | |
# Put together into a phyloseq object | |
######################################## | |
return( do.call("phyloseq", argumentlist) ) | |
} |
@arcys Hey! biom_data comes from the biom
package - http://cran.r-project.org/web/packages/biom/index.html and definitely needs to be installed.
Hey @carlyrae!
Sorry for the delay. I've modified the import_biom2 above and this should be working now, let me know if it doesn't work for you!
Ok, I solved my own issue. But putting comments here in case someone does this too!
Reproducible result:
data("GlobalPatterns")
MGS <- phyloseq_to_metagenomeSeq(GlobalPatterns)
MGS
p <- cumNormStatFast(MGS)
MGS <- cumNorm(MGS, p =p)
b <- MRexperiment2biom(MGS, norm = T)
write_biom(b, biom_file = "test.biom")
biom1 <- import_biom2("test.biom")
You have to read in the biom file first for import biom to work, needs an object not a file
biom2 <-read_biom("test.biom")
biom3 <- import_biom2(biom2)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I loaded the library(biom) where the biom_data function is, but I get this error:
Error in otu_table(as(biom_data(x), "matrix"), taxa_are_rows = TRUE) :
error in evaluating the argument 'object' in selecting a method for function 'otu_table': Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘biom_data’ for signature ‘"character", "missing", "missing"’