# Machine Learning of Hyperrectangular Gating Strategies for High-Dimensional Cytometry

Given a high-dimensional dataset that typically represents a cytometry dataset, and a subset of the datapoints, this algorithm outputs an hyperrectangle so that datapoints within the hyperrectangle best correspond to the specified subset. In essence, this allows the conversion of clustering algorithms' outputs to gating strategies outputs.

This Vignette will walk you through the usage of the Hypergate R package. Installing dependencies:

Installing the package from github:

This loads 2000 datapoints randomly sampled from the Samusik_01 dataset (available from FlowRepository accession number FR-FCM-ZZPH). This object is a list which includes as elements

1. fs_src a flowSet with 1 flowFrame corresponding to the data subset

2. xp_src a matrix corresponding to the expression of the data subset. Rownames correspond to event numbers in the unsampled dataset. Colnames correspond to protein targets (or other information e.g. events' manually-annotated labels)

3. labels numeric vector encoding manually-annontated labels, with the value -1 for ungated events. The text labels for the gated populations are availble from FlowRepostiry

4. regular_channels A subset of colnames(Samusik_01_subset$xp_src) that corresponds to protein targets 5. tsne A 2D-tSNE ran on the whole dataset and subsampled to 2000 events ## Specifying the cell subset of interest Hypergate requires in particular as its arguments 1. an expression matrix (which we have as Samusik_01_subset$xp_src)

2. a vector specifying which events to attempt to gate on. This section discusses ways to achieve this point

#### Selection from low-dimensional plot

We included in the package a function with a rudimentary (hopefully sufficient) interface that allows for the selection of a cell subset of interest from a 2D biplot by drawing a polygon around it using the mouse. Since this function is interactive we cannot execute it in this Vignette but an example call would be as such (feel free to try it):

For this tutorial we define manually the polygon instead

#### Clustering

Another option to define a cell cluster of interest is to use the output of a clustering algorithm. Popular options for cytometry include FlowSOM (available from Bioconductor) or Phenograph (available from the cytofkit package from Bioconductor). An example call for Rphenograph is below:

In this Vignette we use the simpler kmeans option instead:

In this example we can see that the kmeans cluster 20 corresponds to the population we manually selected from the t-SNE biplot

## Running Hypergate

The function to optimize gating strategies is hypergate. Its main arguments are xp (a numeric matrix encoding expression), gate_vector (a vector with few unique values), level (specificies what value of gate_vector to gate upon, i.e. events satisfying gate_vector==level will be gated in)

## Interpreting and polishing the results

### Gating datapoints

The following function allows to subset an expression matrix given a return from Hypergate. The new matrix needs to have the same column names as the original matrix.

Events of interest Others
Gated-in 116 0
Gated-out 10 1874

Another option, which offers more low-level control, is to examine for each datapoint whether they pass the threshold for each parameter. The function to obtain such a boolean matrix is boolmat. Here our gating strategy specifies SiglecF+cKit-Ly6C-. We would thus obtain a 3-columns x 2000 (the number of events) rows

##     SiglecF_min cKit_max Ly6C_max
## 20        FALSE     TRUE     TRUE
## 28        FALSE     TRUE     TRUE
## 70        FALSE    FALSE     TRUE
## 110        TRUE    FALSE    FALSE
## 120       FALSE     TRUE    FALSE
## 159       FALSE     TRUE    FALSE

Events of interest Others
Gated-out because of SiglecF 9 1829
SiglecF above threshold 117 45

### Examining the output

The following function will plot the output of Hypergate. Arguments are

1. gate an object returned by Hypergate

2. xp an expression matrix whose columns are named similarly as the ones used to create the gate object

3. gate_vector and level to specify which events are "of interest"

4. highlight a color that will be used to highlight the events of interest

Another important point to consider is how the F$\beta$-score increases with each added channel. This gives an idea of how many channels are required to reach a close-to-optimal gating strategy.

This will identify at which steps the parameters were first activated and optimized:

This graph tells us that the biggest increase is by far due to SiglecF+, while the lowest is due to Ly6C-.

### Channels contributions

The previous graph only shows how the F-value evolved during optimization, but what we really want to know is how much each parameter contributes to the final output (sometimes a parameter will have a big impact at the early steps of the optimization but will become relatively unimportant towards the end, if multiple other parameters collectively account for most of its discriminatory power). We use the following function to assess this, which measures how much performances lower when a parameter is ignored. The more the performances lower, the more important the parameter is.

### Reoptimize strategy

Since Ly6C contributes very little, we may want to ignore it to obtain a shorter gating strategy. We could keep the current threshold values for the other parameters, but it is best to re-compute the other thresholds to account for the loss of some parameters. To do that we use the following function:

Finally, we get to plot our final strategy:

Thanks to a nice contribution for SamGG on github, there are three functions that make the outputs more readable:

## [1] "SiglecF+, cKit-, Ly6C-"

## [1] "SiglecF >= 2.21, cKit <= 1.77, Ly6C <= 2.98"

##             channels sign comp threshold
## SiglecF_min  SiglecF    +  >=   2.208221
## cKit_max        cKit    -  <=   1.770901
## Ly6C_max        Ly6C    -  <=   2.983523

##             channels sign comp threshold     deltaF Fscore1D Fscore
## SiglecF_min  SiglecF    +  >=   2.208221 0.76351765   0.8125 0.8125
## cKit_max        cKit    -  <=   1.770901 0.07988981   0.1294 0.9360
## Ly6C_max        Ly6C    -  <=   2.983523 0.02267769   0.1809 0.9587

## [1] "0.8125, 0.936, 0.9587"


## Final notes

### Which channels to use as input?

Anything that would be relevant for a gating strategy should be used as an input. So usually any phenotypic channel would be included. If you know that you would not use certain parameters on subsequent experiments (for instance if the staining is intracellular and you plan to sort a live population and thus cannot permeabilize your cells), you should exclude the corresponding channels. I usually do not use channels that were used in pre-gating steps (e.g. CD45 for immune cells). Finally, if you plan to use flow cytometry and use hypergate on a CyTOF dataset, you probably want to discard the Cell_length channel.

### How big can the input matrix be?

It depends on how much RAM your computer has. If that is an issue I suggest downsampling to (e.g.) 1000 positive cells and a corresponding number of negative cells. The hgate_sample function can help you achieve this:

## Negative pop. Positive pop.
##     0.3204976     0.3205128

##
##             Negative pop. Positive pop.
##   Gated in             37           192
##   Gated out          1110            20


# Reference manual

install.packages("hypergate")

0.8.3 by Etienne Becht, a year ago

Browse source code at https://github.com/cran/hypergate

Authors: Etienne Becht [cre, aut] , Samuel Granjeaud [ctb]

Documentation:   PDF Manual