knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>"
)

options(tibble.print_min = 4L, tibble.print_max = 4L)
library(tidytof)
library(dplyr)

This vignette teaches you how to read CyTOF data into an R session from two common file formats in which CyTOF data is typically stored: Flow Cytometry Standard (FCS) and Comma-Separated Value (CSV) files.

Accessing the data for this vignette

{tidytof} comes bundled with several example mass cytometry datasets. To access the raw FCS and CSV files containing these data, use the tidytof_example_data function. When called with no arguments, tidytof_example_data will return a character vector naming the datasets contained in {tidytof}:

tidytof_example_data()

The details of the datasets contained in each of these directories isn't particularly important, but some basic information is as follows:

To obtain the file path for the directory containing each dataset, call tidytof_example_data with one of these dataset names as its argument. For example, to obtain the directory for the phenograph data, we would use the following command:

tidytof_example_data("phenograph")

Reading Data with tof_read_data

Using one of these directories (or any other directory containing CyTOF data on your local machine), we can use tof_read_data to read CyTOF data from raw files. Acceptable formats include FCS files and CSV files. Importantly, tof_read_data is smart enough to read single FCS/CSV files or multiple FCS/CSV files depending on whether its first argument (path) leads to a single file or to a directory of files.

Here, we can use tof_read_data to read in all of the FCS files in the "phenograph" example dataset bundled into {tidytof} and store it in the phenograph variable.

phenograph <-
    tidytof_example_data("phenograph") %>%
    tof_read_data()

phenograph %>%
    class()

Regardless of the input data file type, {tidytof} reads data into an extended tibble class called a tof_tbl (pronounced "tof tibble").

tof tibbles are an S3 class identical to tbl_df, but with one additional attribute ("panel"). {tidytof} stores this additional attribute in tof_tbls because, in addition to analyzing CyTOF data from individual experiments, CyTOF users often want to compare panels between experiments to find common markers or to compare which metals are associated with particular markers across panels. To retrieve this panel information from a tof_tbl, use tof_get_panel:

phenograph %>%
    tof_get_panel()

A few additional notes about tof_tbls:

Using tibble methods with {tidytof} tibbles

As an extension of the tbl_df class, tof_tbls get access to all {dplyr} and {tidyr} for free. These can be useful for performing a variety of common operations.

For example, the phenograph object above has two columns - PhenoGraph and Condition - that encode categorical variables as numeric codes. We might be interested in converting the types of these columns into strings to make sure that we don't accidentally perform any quantitative operations on them later. Thus, {dplyr}'s useful mutate method can be applied to phenograph to convert those two columns into character vectors.

phenograph <-
    phenograph %>%
    # mutate the input tof_tbl
    mutate(
        PhenoGraph = as.character(PhenoGraph),
        Condition = as.character(Condition)
    )

phenograph %>%
    # use dplyr's select method to show
    # that the columns have been changed
    select(where(is.character))

And note that the tof_tbl class is preserved even after these transformations.

phenograph %>%
    class()

Importantly, tof_read_data uses an opinionated heuristic to mine different keyword slots of input FCS file(s) and guess which metals and antigens were used during data acquisition. Thus, when CSV files are read using tof_read_data, it is recommended to use the panel_info argument to provide the panel manually (as CSV files, unlike FCS files, do not provide built-in metadata about the columns they contain).

# when csv files are read, the tof_tibble's "panel"
# attribute will be empty by default
tidytof_example_data("phenograph_csv") %>%
    tof_read_data() %>%
    tof_get_panel()

# to add a panel manually, provide it as a tibble
# to tof_read_data
phenograph_panel <-
    phenograph %>%
    tof_get_panel()

tidytof_example_data("phenograph_csv") %>%
    tof_read_data(panel_info = phenograph_panel) %>%
    tof_get_panel()

Writing data from a tof_tbl to disk

Users may wish to store CyTOF data as FCS or CSV files after transformation, concatenation, filtering, or other data processing. To write single-cell data from a tof_tbl into FCS or CSV files, use tof_write_data. To illustrate how to use this verb, we use the {tidytof}'s built-in phenograph_data dataset.

data(phenograph_data)

print(phenograph_data)
# when copying and pasting this code, feel free to change this path
# to wherever you'd like to save your output files
my_path <- file.path("~", "Desktop", "tidytof_vignette_files")

phenograph_data %>%
    tof_write_data(
        group_cols = phenograph_cluster,
        out_path = my_path,
        format = "fcs"
    )

tof_write_data's trickiest argument is group_cols, the argument used to specify which columns in tof_tibble should be used to group cells (the rows of tof_tibble) into separate FCS or CSV files. Simply put, this argument allows tof_write_data to create a single FCS or CSV file for each unique combination of values in the group_cols columns specified by the user. In the example above, cells are grouped into 3 output FCS files - one for each of the 3 clusters encoded by the phenograph_cluster column in phenograph_data. These files should have the following names (derived from the values in the phenograph_cluster column):

Note that these file names match the distinct values in our group_cols column (phenograph_cluster):

phenograph_data %>%
    distinct(phenograph_cluster)

However, suppose we wanted to write multiple files for each cluster by breaking cells into two groups: those that express high levels of pstat5 and those that express low levels of pstat5. We can use dplyr::mutate to create a new column in phenograph_data that breaks cells into high- and low-pstat5 expression groups, then add this column to our group_cols specification:

phenograph_data %>%
    # create a variable representing if a cell is above or below
    # the median expression level of pstat5
    mutate(
        expression_group = if_else(pstat5 > median(pstat5), "high", "low")
    ) %>%
    tof_write_data(
        group_cols = c(phenograph_cluster, expression_group),
        out_path = my_path,
        format = "fcs"
    )

This will write 6 files with the following names (derived from the values in phenograph_cluster and expression_group).

As above, note that these file names match the distinct values in our group_cols columns (phenograph_cluster and expression_group):

phenograph_data %>%
    mutate(
        expression_group = if_else(pstat5 > median(pstat5), "high", "low")
    ) %>%
    distinct(phenograph_cluster, expression_group)

A useful feature of tof_write_data is that it will automatically concatenate cells into single FCS or CSV files based on the specified group_cols regardless of how many unique files those cells came from. This allows for easy concatenation of FCS or CSV files containing data from a single sample acquired over multiple CyTOF runs, for example.

Session info

sessionInfo()


keyes-timothy/tidytof documentation built on May 7, 2024, 12:33 p.m.