# Data visualization from a QFeatures object

Laurent Gatto

#### 26 April 2023

#### Abstract

This vignette describes how to visualize quantitative mass spectrometry data contained in a QFeatures object. This vignette is distributed under a CC BY-SA license.

#### Package

QFeatures 1.10.0

# 1 Preparing the data

To demonstrate the data visualization of `QFeatures`, we first perform a quick processing of the `hlpsms` example data. We load the data and read it as a `QFeautres` object. See the processing vignette for more details about data processing with `QFeatures`.

    library("QFeatures")
    data(hlpsms)
    hl <- readQFeatures(hlpsms, ecol = 1:10, name = "psms")

We then aggregate the psms to peptides, and the peptodes to proteins.

    hl <- aggregateFeatures(hl, "psms", "Sequence", name = "peptides", fun = colMeans)

    ## Your row data contain missing values. Please read the relevant
    ## section(s) in the aggregateFeatures manual page regarding the effects
    ## of missing values on data aggregation.

    hl <- aggregateFeatures(hl, "peptides", "ProteinGroupAccessions", name = "proteins", fun = colMeans)

We also add the TMT tags that were used to multiplex the samples. The data is added to the `colData` of the `QFeatures` object and will allow us to demonstrate how to plot data from the `colData`.

    hl$tag <- c("126", "127N", "127C", "128N", "128C", "129N", "129C",
                "130N", "130C", "131")

The dataset is now ready for data exploration.

# 2 Exploring the `QFeatures` hierarchy

`QFeatures` objects can contain several assays as the data goes through the processing workflow. The `plot` function provides an overview of all the assays present in the dataset, showing also the hierarchical relationships between the assays as determined by the `AssayLinks`.

    plot(hl)

This plot is rather simple with only three assays, but some processing workflows may involve more steps. The `feat3` example data illustrates the different possible relationships: one parent to one child, multiple parents to one child and one parent to multiple children.

    data("feat3")
    plot(feat3)

Note that some datasets may contain many assays, for instance because the MS experiment consists of hundreds of batches. This can lead to an overcrowded plot. Therefore, you can also explore this hierarchy of assays through an interactive plot, supported by the `plotly` package (Sievert (2020)). You can use the viewer panel to zoom in and out and navigate across the tree(s).

    plot(hl, interactive = TRUE)

# 3 Basic data exploration

The quantitative data is retrieved using `assay()`, the feature metadata is retrieved using `rowData()` on the assay of interest, and the sample metadata is retrieved using `colData()`. Once retrieved, the data can be supplied to the base R data exploration tools. Here are some examples:

- Plot the intensities for the first protein. These data are available from the `proteins` assay.

    plot(assay(hl, "proteins")[1, ])

- Get the distribution of the number of peptides that were aggregated per protein. These data are available in the column `.n` from the protein `rowData`.

    hist(rowData(hl)[["proteins"]]$.n)

- Get the count table of the different tags used for labeling the samples. These data are available in the column `tag` from the `colData`.

    table(hl$tag)

    ##
    ##  126 127C 127N 128C 128N 129C 129N 130C 130N  131
    ##    1    1    1    1    1    1    1    1    1    1

# 4 Using `ggplot2`

`ggplot2` is a powerful tool for data visualization in `R` and is part of the `tidyverse` package ecosystem (Wickham et al. (2019)). It produces elegant and publication-ready plots in a few lines of code. `ggplot2` can be used to explore `QFeatures` object, similarly to the base functions shown above. Note that `ggplot2` expects `data.frame` or `tibble` objects whereas the quantitative data in `QFeatures` are encoded as `matrix` (or matrix-like objects, see `?SummarizedExperiment`) and the `rowData` and `colData` are encoded as `DataFrame`. This is easily circumvented by converting those objects to `data.frame`s or `tibble`s. See here how we reproduce the plot above using `ggplot2`.

    library("ggplot2")
    df <- data.frame(rowData(hl)[["proteins"]])
    ggplot(df) +
        aes(x = .n) +
        geom_histogram()

We refer the reader to the `ggplot2` package website for more information about the wide variety of functions that the package offers and for tutorials and cheatsheets.

Another useful package for quantitative data exploration is `ComplexHeatmap` (Gu, Eils, and Schlesner (2016)). It is part of the Bioconductor project (Gentleman et al. (2004)) and facilitates visualization of matrix objects as heatmap. See here an example where we plot the protein data.

    library(ComplexHeatmap)
    Heatmap(matrix = assay(hl, "proteins"),
            show_row_names = FALSE)

`ComplexHeatmap` also allows to add row and/or column annotations. Let’s add the predicted protein location as row annotation.

    ha <- rowAnnotation(markers = rowData(hl)[["proteins"]]$markers)
    Heatmap(matrix = assay(hl, "proteins"),
            show_row_names = FALSE,
            left_annotation = ha)

More advanced usage of `ComplexHeatmap` is described in the package reference book.

# 5 Advanced data exploration

In this section, we show how to combine in a single table different pieces of information available in a `QFeatures` object, that are quantitation data, feature metadata and sample metadata. The `QFeatures` package provides the `longFormat` function that converts a `QFeatures` object into a long table. Long tables are very useful when using `ggplot2` for data visualization. For instance, suppose we want to visualize the distribution of protein quantitation (present in the `proteins` assay) with respect to the different acquisition tags (present in the `colData`) for each predicted cell location separately (present in the `rowData` of the assays). Furthermore, we link the quantitation values coming from the same protein using lines. This can all be plotted at once in a few lines of code.

    lf <- longFormat(hl[, , "proteins"],
                     rowvars = "markers",
                     colvars = "tag")

    ## Warning: 'experiments' dropped; see 'metadata'

    ## harmonizing input:
    ##   removing 20 sampleMap rows not in names(experiments)

    ggplot(data.frame(lf)) +
        aes(x = tag,
            y = value,
            group = rowname) +
        geom_line() +
        facet_wrap(~ markers, scales = "free_y", ncol = 3)

`longFormat` allows to retrieve and combine all available data from a `Qfeatures` object. We here demonstrate the ease to combine different pieces that could highlight sample specific and/or feature specific effects on data quantitation.

# 6 Interactive data exploration

Finally, a simply `shiny` app allows to explore and visualise the respective assays of a `QFeatures` object.

    display(hl)

Figure 1: `QFeatures` interactive interface: heatmap of the peptide assay data

Figure 2: `QFeatures` interactive interface: quantitative peptide assay data

Figure 3: `QFeatures` interactive interface: peptide assay row data

A dropdown menu in the side bar allows the user to select an assay of interest, which can then be visualised as a heatmap (figure 1), as a quantitative table (figure 2) or a row data table (figure 3).

# Session information

    ## R version 4.3.0 RC (2023-04-13 r84269 ucrt)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2022 x64 (build 20348)
    ##
    ## Matrix products: default
    ##
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] grid      stats4    stats     graphics  grDevices utils     datasets
    ## [8] methods   base
    ##
    ## other attached packages:
    ##  [1] ComplexHeatmap_2.16.0       gplots_3.1.3
    ##  [3] dplyr_1.1.2                 ggplot2_3.4.2
    ##  [5] QFeatures_1.10.0            MultiAssayExperiment_1.26.0
    ##  [7] SummarizedExperiment_1.30.0 Biobase_2.60.0
    ##  [9] GenomicRanges_1.52.0        GenomeInfoDb_1.36.0
    ## [11] IRanges_2.34.0              S4Vectors_0.38.0
    ## [13] BiocGenerics_0.46.0         MatrixGenerics_1.12.0
    ## [15] matrixStats_0.63.0          BiocStyle_2.28.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] tidyselect_1.2.0        farver_2.1.1            bitops_1.0-7
    ##  [4] fastmap_1.1.1           lazyeval_0.2.2          RCurl_1.98-1.12
    ##  [7] digest_0.6.31           lifecycle_1.0.3         cluster_2.1.4
    ## [10] Cairo_1.6-0             ProtGenerics_1.32.0     magrittr_2.0.3
    ## [13] compiler_4.3.0          rlang_1.1.0             sass_0.4.5
    ## [16] tools_4.3.0             igraph_1.4.2            utf8_1.2.3
    ## [19] yaml_2.3.7              knitr_1.42              labeling_0.4.2
    ## [22] DelayedArray_0.26.0     RColorBrewer_1.1-3      KernSmooth_2.23-20
    ## [25] withr_2.5.0             fansi_1.0.4             caTools_1.18.2
    ## [28] colorspace_2.1-0        scales_1.2.1            gtools_3.9.4
    ## [31] iterators_1.0.14        MASS_7.3-59             cli_3.6.1
    ## [34] crayon_1.5.2            rmarkdown_2.21          generics_0.1.3
    ## [37] rjson_0.2.21            BiocBaseUtils_1.2.0     cachem_1.0.7
    ## [40] zlibbioc_1.46.0         parallel_4.3.0          AnnotationFilter_1.24.0
    ## [43] BiocManager_1.30.20     XVector_0.40.0          vctrs_0.6.2
    ## [46] Matrix_1.5-4            jsonlite_1.8.4          bookdown_0.33
    ## [49] GetoptLong_1.0.5        clue_0.3-64             magick_2.7.4
    ## [52] foreach_1.5.2           limma_3.56.0            jquerylib_0.1.4
    ## [55] glue_1.6.2              codetools_0.2-19        shape_1.4.6
    ## [58] gtable_0.3.3            munsell_0.5.0           tibble_3.2.1
    ## [61] pillar_1.9.0            htmltools_0.5.5         circlize_0.4.15
    ## [64] GenomeInfoDbData_1.2.10 R6_2.5.1                doParallel_1.0.17
    ## [67] evaluate_0.20           lattice_0.21-8          highr_0.10
    ## [70] png_0.1-8               msdata_0.39.0           bslib_0.4.2
    ## [73] Rcpp_1.0.10             xfun_0.39               GlobalOptions_0.1.2
    ## [76] MsCoreUtils_1.12.0      pkgconfig_2.0.3

# References

Gentleman, Robert C., Vincent J. Carey, Douglas M. Bates, Ben Bolstad, Marcel Dettling, Sandrine Dudoit, Byron Ellis, et al. 2004. “Bioconductor: Open Software Development for Computational Biology and Bioinformatics.” Genome Biol 5 (10): –80. https://doi.org/10.1186/gb-2004-5-10-r80.

Gu, Zuguang, Roland Eils, and Matthias Schlesner. 2016. “Complex Heatmaps Reveal Patterns and Correlations in Multidimensional Genomic Data.” Bioinformatics 32 (18): 2847–9.

Sievert, Carson. 2020. Interactive Web-Based Data Visualization with R, Plotly, and Shiny. Chapman; Hall/CRC. https://plotly-r.com.

Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the Tidyverse.” J. Open Source Softw. 4 (43): 1686.