Skip to content
Snippets Groups Projects

biom2phyloseq.R

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Cedric Midoux
    Edited
    biom2phyloseq.R 2.13 KiB
    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)
    }
    • Cedric Midoux @cedric.midoux ·

      Usage :

      source('https://forgemia.inra.fr/-/snippets/205/raw/main/biom2phyloseq.R')
      biom2physeq(biomfilename = "proj.biom1", metafilename = "proj_data-table.csv", rename_file = "proj_rename.csv", col_types = "cfD") |>
        saveRDS("proj.RDS")
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment