# Introduction to `metabolomicsWorkbenchR`

# Introduction

Metabolomics Workbench (link) hosts a metabolomics data repository. It contains over 1000 publicly available studies including raw data, processed data and metabolite/compound information.

The repository is searchable using a REST service API. The metabolomicsWorkbenchR package makes the endpoints of this service available in R and provides functionality to search the database and import datasets and metabolite information into commonly used formats such as data frames and SummarizedExperiment objects.

# Installation

To install this package enter:

    if (!requireNamespace("BiocManager", quietly = TRUE))
        install.packages("BiocManager")

    BiocManager::install("metabolomicsWorkbenchR")

For older versions, please refer to the appropriate Bioconductor release.

# Running a query

The Metabolomics Workbench API has a number of endpoints that can be used to query several different databases. Complete details are provided in the API documentation (link).

`metabolomicsWorkbenchR` provides a simple interface to all API endpoints via the `do_query` method. Four inputs are required:

- `context` - The context determines the type of data to be accessed from the Metabolomics Workbench, such as metadata or results related to the submitted studies, data from metabolites, genes/proteins and analytical chemistry databases as well as other services related to mass spectrometry and metabolite identification. Valid contexts are “study”, “compound”, “refmet”, “gene”, “protein”, “moverz” and “exactmass”.
- `input_item` - Input items direct the search towards a specific part of the database. If the database is a table, then `input_item` is a column in that table that will be searched for matching values.
- `input_value` - The value to search for in the named `input_item`.
- `output_item` - The type of output to be returned. Usually some data in the form of a table, but sometimes a file (e.g. png, mol).

By combining different context, input and output items a variety of information can be returned. In this first example, we query the study context for study titles containing the keyword “Diabetes” and request a summary of each matching study.

    # search for all studies with "Diabetes" in the title and return a summary
    df = do_query(
        context = 'study',
        input_item = 'study_title',
        input_value = 'Diabetes',
        output_item = 'summary'
    )
    df[1:3,c(1,4)]

    ##   study_id                       institute
    ## 1 ST000057 University of California, Davis
    ## 2 ST000075 University of California, Davis
    ## 3 ST000091                     Mayo Clinic

The result is a 14x12 data.frame with study titles, authors, descriptions etc.

In the next example we query the compound context for “regno” identifier 11 and request all available information for the matching compound.

    df = do_query(
        context = 'compound',
        input_item = 'regno',
        input_value = '11',
        output_item = 'compound_exact'
    )

    df[,1:3]

    ##   regno    formula  exactmass
    ## 1    11 C47H75N5O8 837.561565

We can also request an image of the molecular structure:

    img = do_query(
            context = 'compound',
            input_item = 'regno',
            input_value = '11',
            output_item = 'png'
          )

    grid.raster(img)

Valid contexts, input items and output items can be listed using the `names` function:

    # valid contexts
    names(context) # context, input_item or output_item

    ## [1] "study"     "compound"  "refmet"    "gene"      "protein"   "moverz"
    ## [7] "exactmass"

Valid inputs and outputs for a context can be displayed by accessing the list of context objects. Valid inputs for a particular output can also be displayed by accessing the list of output item objects. Use of `metabolmicsWorkbenchR` objects is detailed in a later section. In addition, functions `context_inputs`, `context_outputs` and `input_example` are provided for convenience.

    # valid inputs for "study" context
    context_inputs('study')

    ## [1] "study_id"      "study_title"   "institute"     "last_name"
    ## [5] "analysis_id"   "metabolite_id"

More information about the different contexts can be found in the API documentation (link)

# Special cases

`metabolomicsWorkBenchR` includes some output items in addition to those specified by the API documentation. These special cases are described here.

## `input_item` “ignored”

The input item is used with the “study” context and the “untarg_studies” input_item. The API ignores the input_item and the input_value when using this query and returns a list of studies with untargeted data.

    df = do_query(
      context = 'study',
      input_item = 'ignored',
      input_value = 'ignored',
      output_item = 'untarg_studies'
    )

    df[1:3,1:3]

    ##   study_id analysis_id                         analysis_display
    ## 1 ST000009    AN000023 LC/Electro-spray /QTOF positive ion mode
    ## 2 ST000009    AN000024 LC/Electro-spray /QTOF negative ion mode
    ## 3 ST000010    AN000025 LC/Electro-spray /QTOF positive ion mode

## `output_item` “compound_exact”, “protein_exact” and “gene_exact”

These outputs refer to compound, protein and gene context API outputs that can be used with exact matching. This means that only exact matches to the input_value will be returned. For these outputs all available output fields will be returned. These output items are used in place of the ‘all’ item specified in the API documentation.

    df = do_query(
      context = 'compound',
      input_item = 'regno',
      input_value = '11',
      output_item = 'compound_exact'
    )

    df[,1:3]

    ##   regno    formula  exactmass
    ## 1    11 C47H75N5O8 837.561565

## `output_item` “protein_partial” and “gene_partial”

These outputs refer to protein and gene contexts API outputs that can be used with partial matching. This means that all records with a partial match to the input_value will be returned. For these outputs all available output fields will be returned.

    df = do_query(
      context = 'gene',
      input_item = 'gene_name',
      input_value = 'acetyl-CoA',
      output_item = 'gene_partial'
    )

    df[1:3,1:3]

    ##                      gene_name    mgp_id gene_id
    ## 1 acetyl-CoA acyltransferase 1 MGP000015      30
    ## 2 acetyl-CoA carboxylase alpha MGP000016      31
    ## 3  acetyl-CoA carboxylase beta MGP000017      32

## `output_item` “SummarizedExperiment” and “DatasetExperiment”

This output refers to the study context and uses multiple queries to return a SummarizedExperiment or DatasetExperiment object for a study_id or analysis_id.

    SE = do_query(
        context = 'study',
        input_item = 'study_id',
        input_value = 'ST000001',
        output_item = 'SummarizedExperiment' # or 'DatasetExperiment'
    )

    SE

    ## class: SummarizedExperiment
    ## dim: 102 24
    ## metadata(8): data_source study_id ... description subject_type
    ## assays(1): ''
    ## rownames(102): ME000097 ME000096 ... ME000002 ME000001
    ## rowData names(3): metabolite_name metabolite_id refmet_name
    ## colnames(24): LabF_115811 LabF_115816 ... LabF_115924 LabF_115929
    ## colData names(4): local_sample_id study_id Arabidopsis_Genotype
    ##   Plant_Wounding_Treatment

## `output_item` “MultiAssayExperiment”

This output refers to the study context and uses multiple queries to return a MultiAssayExperiment object for a study_id.

    MAE = do_query(
        context = 'study',
        input_item = 'study_id',
        input_value = 'ST000009',
        output_item = 'MultiAssayExperiment'
    )

    MAE

    ## A MatchedAssayExperiment object of 2 listed
    ##  experiments with user-defined names and respective classes.
    ##  Containing an ExperimentList class object of length 2:
    ##  [1] AN000023: SummarizedExperiment with 230 rows and 114 columns
    ##  [2] AN000024: SummarizedExperiment with 172 rows and 114 columns
    ## Functionality:
    ##  experiments() - obtain the ExperimentList instance
    ##  colData() - the primary/phenotype DataFrame
    ##  sampleMap() - the sample coordination DataFrame
    ##  `$`, `[`, `[[` - extract colData columns, subset, or experiment
    ##  *Format() - convert into a long or wide DataFrame
    ##  assays() - convert ExperimentList to a SimpleList of matrices
    ##  exportClass() - save data to flat files

## `output_item` “untarg_SummarizedExperiment” and “untarg_DatasetExperiment”

This output refers to the study context and uses multiple queries to return a SummarizedExperiment or DatasetExperiment object of untargeted data for an analysis_id.

    SE = do_query(
        context = 'study',
        input_item = 'analysis_id',
        input_value = 'AN000025',
        output_item = 'untarg_SummarizedExperiment' # or 'untarg_DatasetExperiment'
    )

    SE

# S4 classes

A number of classes have been defined in this package and for completeness they are described below. They are used to implement access to the API endpoints and it is not expected that they will be used as objects by the user. The `do_query` function uses character strings to access predefined instances of these objects and simplify the query.

## Contexts

Each database is referred to as a ‘context’. These contexts can be searched using input/output pairs to search the database for matches and return the results.

In `metabolmicsWorkbenchR` a predefined list called `context` contains `mw_context` objects. These objects define which inputs and outputs are valid options for a context.

The name of all valid contexts can be displayed:

    # list all context names
    names(metabolomicsWorkbenchR::context)

    ## [1] "study"     "compound"  "refmet"    "gene"      "protein"   "moverz"
    ## [7] "exactmass"

Information about a specific context can be obtained using the show method for a an `mw_context` object:

    # list valid inputs/outputs for the "study" context
    metabolomicsWorkbenchR::context$study

    ## A Metabolomics Workbench "context"
    ##
    ## Name:    "study"
    ##
    ## Valid input_item names:
    ##  "study_id"
    ##  "study_title"
    ##  "institute"
    ##  "last_name"
    ##  "analysis_id"
    ##  "metabolite_id"
    ##
    ## Valid output_item names:
    ##  "summary"
    ##  "factors"
    ##  "analysis"
    ##  "metabolites"
    ##  "mwtab"
    ##  "source"
    ##  "species"
    ##  "disease"
    ##  "number_of_metabolites"
    ##  "data"
    ##  "datatable"
    ##  "untarg_studies"
    ##  "untarg_factors"
    ##  "untarg_data"
    ##  "metabolite_info"
    ##  "SummarizedExperiment"
    ##  "untarg_SummarizedExperiment"
    ##  "DatasetExperiment"
    ##  "untarg_DatasetExperiment"

## Input / Output Items

Once the context of the search has been decided upon valid inputs and outputs can be chosen. All input and output items have been predefined as lists called `input_item` and `output_item`.

The `input_item` list contains `mw_input_item` objects that specify valid pattern matching of the input value using regex.

    # input item "study_id" info
    input_item$study_id

    ## A Metabolomics Workbench "input_item"
    ##
    ## Name:    "study_id"
    ##
    ## Exact pattern matching:
    ##  "^ST[0-9]{6}$"
    ##
    ## Partial pattern matching:
    ##  "^S(T[0-9]{0,6})?$"
    ##
    ## Examples:
    ##  "ST000001"
    ##  "ST"

The `output_item` list contains `mw_output_item` objects that specify valid inputs, the expected return fields (if the return is a data.frame) and the type of input matching that is supported.

    # output item 'summary' info
    output_item$summary

    ## A Metabolomics Workbench "output_item"
    ##
    ## Name:    "summary"
    ##
    ## Returns:
    ##  "study_id"
    ##  "study_title"
    ##  "study_type"
    ##  "institute"
    ##  "department"
    ##  "last_name"
    ##  "first_name"
    ##  "email"
    ##  "phone"
    ##  "submit_date"
    ##  "study_summary"
    ##  "subject_species"
    ##
    ## Allowed input_item names:
    ##  "study_id"
    ##  "study_title"
    ##  "institute"
    ##  "last_name"
    ##
    ## Type of matching supported:  "partial"

# Session Info

    ## R version 4.6.0 (2026-04-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    ## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    ##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    ##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    ## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    ##
    ## time zone: Etc/UTC
    ## tzcode source: system (glibc)
    ##
    ## attached base packages:
    ## [1] grid      stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ## [1] curl_7.1.0                    metabolomicsWorkbenchR_1.22.0
    ## [3] httptest_4.2.3                testthat_3.3.2
    ## [5] structToolbox_1.22.0          struct_1.24.0
    ## [7] BiocStyle_2.40.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] tidyselect_1.2.1            dplyr_1.2.1
    ##  [3] farver_2.1.2                S7_0.2.2
    ##  [5] fastmap_1.2.0               digest_0.6.39
    ##  [7] lifecycle_1.0.5             magrittr_2.0.5
    ##  [9] compiler_4.6.0              rlang_1.2.0
    ## [11] sass_0.4.10                 rngtools_1.5.2
    ## [13] tools_4.6.0                 yaml_2.3.12
    ## [15] data.table_1.18.2.1         knitr_1.51
    ## [17] labeling_0.4.3              S4Arrays_1.12.0
    ## [19] doRNG_1.8.6.3               sp_2.2-1
    ## [21] DelayedArray_0.38.0         plyr_1.8.9
    ## [23] RColorBrewer_1.1-3          abind_1.4-8
    ## [25] withr_3.0.2                 purrr_1.2.2
    ## [27] BiocGenerics_0.58.0         itertools_0.1-3
    ## [29] sys_3.4.3                   stats4_4.6.0
    ## [31] ggplot2_4.0.3               scales_1.4.0
    ## [33] iterators_1.0.14            MultiAssayExperiment_1.38.0
    ## [35] SummarizedExperiment_1.42.0 cli_3.6.6
    ## [37] rmarkdown_2.31              generics_0.1.4
    ## [39] reshape2_1.4.5              httr_1.4.8
    ## [41] BiocBaseUtils_1.14.0        cachem_1.1.0
    ## [43] stringr_1.6.0               ggthemes_5.2.0
    ## [45] parallel_4.6.0              impute_1.86.0
    ## [47] BiocManager_1.30.27         XVector_0.52.0
    ## [49] matrixStats_1.5.0           vctrs_0.7.3
    ## [51] Matrix_1.7-5                jsonlite_2.0.0
    ## [53] IRanges_2.46.0              S4Vectors_0.50.0
    ## [55] maketools_1.3.2             foreach_1.5.2
    ## [57] jquerylib_0.1.4             missForest_1.6.1
    ## [59] glue_1.8.1                  codetools_0.2-20
    ## [61] stringi_1.8.7               gtable_0.3.6
    ## [63] GenomicRanges_1.64.0        tibble_3.3.1
    ## [65] pillar_1.11.1               pcaMethods_2.4.0
    ## [67] rappdirs_0.3.4              htmltools_0.5.9
    ## [69] Seqinfo_1.2.0               brio_1.1.5
    ## [71] pmp_1.22.1                  randomForest_4.7-1.2
    ## [73] R6_2.6.1                    httr2_1.2.2
    ## [75] Rdpack_2.6.6                evaluate_1.0.5
    ## [77] lattice_0.22-9              Biobase_2.72.0
    ## [79] rbibutils_2.4.1             bslib_0.10.0
    ## [81] Rcpp_1.1.1-1.1              gridExtra_2.3
    ## [83] SparseArray_1.12.0          ranger_0.18.0
    ## [85] xfun_0.57                   MatrixGenerics_1.24.0
    ## [87] buildtools_1.0.0            pkgconfig_2.0.3