Skip to contents


Rbiom objects make it easy to access and manipulate your BIOM data, ensuring all the disparate components remain in sync. These objects behave largely like lists, in that you can access and assign to them using the $ operator. The sections below list all the fields which can be read and/or written, and the helper functions for common tasks like rarefying and subsetting. To create an rbiom object, see as_rbiom().

Use $clone() to create a copy of an rbiom object. This is necessary because rbiom objects are passed by reference. The usual <- assignment operator will simply create a second reference to the same object - it will not create a second object. See speed ups for more details.

Readable Fields

Reading from fields will not change the rbiom object.

AccessorContent
$countsAbundance of each OTU in each sample.
$metadataSample mappings to metadata (treatment, patient, etc).
$taxonomyOTU mappings to taxonomic ranks (genus, phylum, etc).
$otus, $n_otusOTU names.
$samples, $n_samplesSample names.
$fields, $n_fieldsMetadata field names.
$ranks, $n_ranksTaxonomic rank names.
$tree, $sequencesPhylogenetic tree / sequences for the OTUs, or NULL.
$id, $commentArbitrary strings for describing the dataset.
$depthRarefaction depth, or NULL if unrarefied.
$dateDate from BIOM file.


Writable Fields

Assigning new values to these components will trigger validation checks and inter-component synchronization.

ComponentWhat can be assigned.
$countsMatrix of abundances; OTUs (rows) by samples (columns).
$metadataData.frame with '.sample' column, or a file name.
$taxonomyData.frame with '.otu' as the first column.
$otusCharacter vector with new names for the OTUs.
$samplesCharacter vector with new names for the samples.
$treePhylo object with the phylogenetic tree for the OTUs.
$sequencesNamed character vector of OTU reference sequences.
$id, $commentString with dataset's title or comment.
$dateDate-like object, or "%Y-%m-%dT%H:%M:%SZ" string.


Transformations

All functions return an rbiom object.

FunctionTransformation
<rbiom>$clone()Safely duplicate an rbiom object.
<rbiom>[Subset to a specific set of sample names.
subset()Subset samples according to metadata properties.
slice()Subset to a specific number of samples.
mutate()Create, modify, and delete metadata fields.
rarefy()Sub-sample OTU counts to an even sampling depth.


Examples

    library(rbiom)
    
    # Duplicate the HMP50 example dataset.
    biom <- hmp50$clone()
    
    
    # Display an overall summary of the rbiom object.
    biom
#> 
#> ══ Human Microbiome Project - 50 Sample Demo ═══════════════
#> 
#> Oral, nasal, vaginal, and fecal samples from a diverse set
#> of healthy volunteers. Source: Human Microbiome Project
#> (<https://hmpdacc.org>).
#> 
#>      50 Samples: HMP01, HMP02, HMP03, ..., and HMP50 
#>     490 OTUs:    Unc01yki, Unc53100, LtbAci52, ... 
#>       7 Ranks:   .otu, Kingdom, Phylum, ..., and Genus 
#>       5 Fields:  .sample, Age, BMI, Body Site, and Sex 
#>         Tree:    <present>
#> 
#> ── 182 - 22k reads/sample ──────────────────── 2023-09-22 ──
#> 
    
    
    # Markdown syntax for comments is recommended.
    biom$comment %>% cli::cli_text()
#> Oral, nasal, vaginal, and fecal samples from a diverse set of healthy
#> volunteers. Source: [Human Microbiome Project](https://hmpdacc.org).
    
    
    # Demonstrate a few accessors.
    biom$n_samples
#> [1] 50
    biom$fields
#> [1] ".sample"   "Age"       "BMI"       "Body Site" "Sex"      
    biom$metadata
#> # A tibble: 50 × 5
#>    .sample   Age   BMI `Body Site`    Sex   
#>  * <chr>   <dbl> <dbl> <fct>          <fct> 
#>  1 HMP01      22    20 Buccal mucosa  Female
#>  2 HMP02      24    23 Buccal mucosa  Male  
#>  3 HMP03      28    26 Saliva         Male  
#>  4 HMP04      25    23 Saliva         Male  
#>  5 HMP05      27    24 Buccal mucosa  Female
#>  6 HMP06      32    25 Saliva         Male  
#>  7 HMP07      26    22 Buccal mucosa  Male  
#>  8 HMP08      27    26 Saliva         Female
#>  9 HMP09      33    32 Saliva         Male  
#> 10 HMP10      22    20 Anterior nares Female
#> # ℹ 40 more rows
    
    
    # Edit the metadata table.
    biom$metadata$rand <- sample(1:50)
    biom %<>% mutate(Obese = BMI >= 30, Sex = NULL)
    biom %<>% rename('Years Old' = "Age")
    biom$metadata
#> # A tibble: 50 × 6
#>    .sample `Years Old`   BMI `Body Site`     rand Obese
#>  * <chr>         <dbl> <dbl> <fct>          <int> <lgl>
#>  1 HMP01            22    20 Buccal mucosa      5 FALSE
#>  2 HMP02            24    23 Buccal mucosa     25 FALSE
#>  3 HMP03            28    26 Saliva            22 FALSE
#>  4 HMP04            25    23 Saliva            49 FALSE
#>  5 HMP05            27    24 Buccal mucosa     32 FALSE
#>  6 HMP06            32    25 Saliva            47 FALSE
#>  7 HMP07            26    22 Buccal mucosa     23 FALSE
#>  8 HMP08            27    26 Saliva            35 FALSE
#>  9 HMP09            33    32 Saliva            40 TRUE 
#> 10 HMP10            22    20 Anterior nares    30 FALSE
#> # ℹ 40 more rows
    
    
    # Subset the rbiom object
    biom %<>% subset(`Body Site` == "Saliva" & !Obese)
    biom$metadata
#> # A tibble: 8 × 6
#>   .sample `Years Old`   BMI `Body Site`  rand Obese
#> * <chr>         <dbl> <dbl> <fct>       <int> <lgl>
#> 1 HMP03            28    26 Saliva         22 FALSE
#> 2 HMP04            25    23 Saliva         49 FALSE
#> 3 HMP06            32    25 Saliva         47 FALSE
#> 4 HMP08            27    26 Saliva         35 FALSE
#> 5 HMP18            28    24 Saliva          7 FALSE
#> 6 HMP28            23    19 Saliva         10 FALSE
#> 7 HMP29            36    25 Saliva         18 FALSE
#> 8 HMP30            24    21 Saliva         39 FALSE
    
    
    # Rarefy to an even sampling depth
    sample_sums(biom)
#> HMP03 HMP04 HMP06 HMP08 HMP18 HMP28 HMP29 HMP30 
#>  1353  1895  4150  1695  2202  1695  2423  3938 
    
    biom %<>% rarefy()
    sample_sums(biom)
#> HMP03 HMP04 HMP06 HMP08 HMP18 HMP28 HMP29 HMP30 
#>  1353  1353  1353  1353  1353  1353  1353  1353