Skip to content

Instantly share code, notes, and snippets.

@jnpaulson
Last active February 22, 2018 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jnpaulson/324ac1fa3eab1bc7f845 to your computer and use it in GitHub Desktop.
Save jnpaulson/324ac1fa3eab1bc7f845 to your computer and use it in GitHub Desktop.
This will convert a biom class object into a phyloseq object.
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
Copy link

arcys commented Apr 29, 2015

Hi,
I've tried to source this function and import the biome file to phyloseq but it show the following error message:

import_biom2("HMPv35_100nt.biom")
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 .class1(object) : could not find function "biom_data"

So where can I get the function biom_data? Thx

@CarlyMuletzWolz
Copy link

I loaded the library(biom) where the biom_data function is, but I get this error:

biom <- import_biom2("otu_table.biom")

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"’

@jnpaulson
Copy link
Author

@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.

@jnpaulson
Copy link
Author

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!

@CarlyMuletzWolz
Copy link

CarlyMuletzWolz commented Jan 26, 2018

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