biom2phyloseq.R
The snippet can be accessed without any authentication.
Authored by
Cedric Midoux
Edited
getMeta <- function(file, delim = NULL, col_types = NULL, original_sample_name = TRUE) {
readr::read_delim(file = file, delim = delim, col_types = col_types, show_col_types = is.null(col_types)) |>
dplyr::rename(Sample = 1) |>
(\(df) if (original_sample_name) dplyr::mutate(df, original_sample_name = Sample) else df)() |>
tibble::column_to_rownames("Sample")
}
getBiom <- function(biomfilename, biomformat = c("FROGS", "stdBIOM"), treefilename = NULL, refseqfilename = NULL, refseqFunction = Biostrings::readDNAStringSet, refseqArgs = NULL) {
biomformat <- match.arg(biomformat)
switch(biomformat,
"FROGS" = phyloseq.extended::import_frogs(biom = biomfilename, treefilename = treefilename, refseqfilename = refseqfilename, refseqFunction = refseqFunction, refseqArgs = refseqArgs),
"stdBIOM" = phyloseq::import_biom(BIOMfilename = biomfilename, treefilename = treefilename, refseqfilename = refseqfilename, refseqFunction = refseqFunction, refseqArgs = refseqArgs)
)
}
rename_sample <- function(physeq, rename_file) {
rename_df <- readr::read_csv(rename_file, col_types = readr::cols()) |> tibble::deframe()
phyloseq::sample_names(physeq) <- phyloseq::sample_names(physeq) |>
purrr::map_chr(~ rename_df[.x] %||% .x)
physeq
}
biom2physeq <- function(
biomfilename, biomformat = c("FROGS", "stdBIOM"),
metafilename = NULL, delim = NULL, col_types = NULL, original_sample_name = TRUE,
treefilename = NULL, refseqfilename = NULL, refseqFunction = Biostrings::readDNAStringSet, refseqArgs = NULL, rename_file = NULL) {
physeq <- getBiom(biomfilename = biomfilename, biomformat = biomformat, treefilename = treefilename, refseqfilename = refseqfilename, refseqFunction = refseqFunction, refseqArgs = refseqArgs)
if (!is.null(metafilename)) {
phyloseq::sample_data(physeq) <- phyloseq::sample_data(getMeta(file = metafilename, delim = delim, col_types = col_types, original_sample_name = original_sample_name))
}
if (!is.null(rename_file)) {
physeq <- rename_sample(physeq, rename_file)
}
return(physeq)
}