# How to create supercells

## Introduction

This vignette describes the steps to generate supercells for cytometry data using SuperCellCyto R package.

Briefly, supercells are “mini” clusters of cells that are similar in their marker expressions. The motivation behind supercells is that instead of analysing millions of individual cells, you can analyse thousands of supercells, making downstream analysis much faster while maintaining biological interpretability.

See other vignettes for how to:

- Prepare your data for SuperCellCyto
- Use SuperCellCyto for downsampling
- Integrate SuperCellCyto output with SingleCellExperiment objects
- Integrate SuperCellCyto output with Seurat objects

## Installation

You can install stable version of SuperCellCyto from Bioconductor using:

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

For the latest development version, you can install it from GitHub using `pak`:

    if (!requireNamespace("pak", quietly = TRUE))
        install.packages("pak")
    pak::install_github("phipsonlab/SuperCellCyto")

## Preparing your dataset

The function which creates supercells is called `runSuperCellCyto`, and it operates on a `data.table` object, an enhanced version of R native `data.frame`.

In addition to needing the data stored in a `data.table` object it also requires:

1. The markers you will be using to create supercells to have been appropriately transformed, typically using either arcsinh transformation or linear binning (using FlowJo). `runSuperCellCyto` does not perform any data transformation or scaling.
2. The object to have a column denoting the unique ID of each cell. You most likely have to create this column yourself, and it can simply just be a numerical value ranging from 1 to however many cells you have in your data.
3. The object to have a column denoting the biological sample each cell comes from. This column is critical to ensure that cells from different samples will not be mixed in a supercell.

If you are not sure how to import CSV or FCS files into `data.table` object, and/or how to subsequently prepare the object ready for SuperCellCyto, please consult this vignette. In that vignette, we also provide an explanation behind why we need to have the cell ID and sample column.

For this vignette, we will simulate some toy data using the `simCytoData` function. Specifically, we will simulate 15 markers and 3 samples, with each sample containing 10,000 cells. Hence in total, we will have a toy dataset containing 15 markers and 30,000 cells.

    n_markers <- 15
    n_samples <- 3
    dat <- simCytoData(nmarkers = n_markers, ncells = rep(10000, n_samples))
    head(dat)
    #>    Marker_1 Marker_2 Marker_3 Marker_4 Marker_5 Marker_6  Marker_7 Marker_8
    #>       <num>    <num>    <num>    <num>    <num>    <num>     <num>    <num>
    #> 1: 14.02706 15.63391 7.299256 12.56116 11.80067 5.450905  8.840493 15.80892
    #> 2: 13.18763 18.06999 7.268064 12.91395 13.95301 5.036927 10.798073 16.05024
    #> 3: 10.73677 18.71431 7.223482 13.86355 12.85411 4.765124  8.698596 14.08871
    #> 4: 13.06779 16.18576 8.398245 13.82975 12.99954 5.956930  7.557316 14.16298
    #> 5: 13.87461 17.34231 8.175115 14.71247 15.03300 4.689248  9.009362 14.87641
    #> 6: 12.48752 17.12249 6.698252 13.14981 14.02546 4.968059 10.397265 15.79044
    #>     Marker_9 Marker_10 Marker_11 Marker_12 Marker_13 Marker_14 Marker_15
    #>        <num>     <num>     <num>     <num>     <num>     <num>     <num>
    #> 1: 10.427376  6.899546  14.18299  17.78195  18.34222  6.408567  9.715223
    #> 2:  9.778965 10.629022  15.49085  18.63701  18.04916  7.554339 10.452028
    #> 3: 12.779378  7.138252  13.92474  20.15171  19.00676  5.384693  9.951428
    #> 4: 10.565481  7.476897  16.27255  16.80486  19.62767  7.286467 10.820309
    #> 5: 13.236856  8.784311  14.66600  17.77189  17.32046  5.881146 10.089926
    #> 6: 10.898114  7.609250  15.10611  17.37946  17.05887  6.754582  8.994977
    #>      Sample Cell_Id
    #>      <char>  <char>
    #> 1: Sample_1  Cell_1
    #> 2: Sample_1  Cell_2
    #> 3: Sample_1  Cell_3
    #> 4: Sample_1  Cell_4
    #> 5: Sample_1  Cell_5
    #> 6: Sample_1  Cell_6

For our toy dataset, we will transform our data using arcsinh transformation. We will use the base R `asinh` function to do this:

    # Specify which columns are the markers to transform
    marker_cols <- paste0("Marker_", seq_len(n_markers))
    # The co-factor for arc-sinh
    cofactor <- 5

    # Do the transformation
    dat_asinh <- asinh(dat[, marker_cols, with = FALSE] / cofactor)

    # Rename the new columns
    marker_cols_asinh <- paste0(marker_cols, "_asinh")
    names(dat_asinh) <- marker_cols_asinh

    # Add them our previously loaded data
    dat <- cbind(dat, dat_asinh)

    head(dat[, marker_cols_asinh, with = FALSE])
    #>    Marker_1_asinh Marker_2_asinh Marker_3_asinh Marker_4_asinh Marker_5_asinh
    #>             <num>          <num>          <num>          <num>          <num>
    #> 1:       1.755048       1.857794       1.172284       1.651764       1.593995
    #> 2:       1.697130       1.996575       1.168754       1.677547       1.750063
    #> 3:       1.507656       2.030384       1.163690       1.744010       1.673217
    #> 4:       1.688599       1.890887       1.290456       1.741714       1.683710
    #> 5:       1.744761       1.957020       1.267401       1.800103       1.820531
    #> 6:       1.646304       1.944769       1.102397       1.694445       1.754940
    #>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
    #>             <num>          <num>          <num>          <num>           <num>
    #> 1:      0.9437258       1.334842       1.868402       1.481220        1.126249
    #> 2:      0.8865862       1.512821       1.882856       1.423689        1.498522
    #> 3:      0.8477640       1.320786       1.759179       1.667785        1.153949
    #> 4:      1.0104469       1.201105       1.764136       1.493099        1.192197
    #> 5:      0.8367369       1.351350       1.810601       1.700615        1.329297
    #> 6:      0.8768492       1.478614       1.867287       1.521196        1.206823
    #>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
    #>              <num>           <num>           <num>           <num>
    #> 1:        1.765467        1.981098        2.010995       1.0672521
    #> 2:        1.849042        2.026385        1.995464       1.2007765
    #> 3:        1.748154        2.102046        2.045372       0.9347447
    #> 4:        1.895998        1.926808        2.076492       1.1708380
    #> 5:        1.797108        1.980553        1.955808       1.0006660
    #> 6:        1.825136        1.959076        1.941196       1.1091178
    #>    Marker_15_asinh
    #>              <num>
    #> 1:        1.417870
    #> 2:        1.483350
    #> 3:        1.439283
    #> 4:        1.514688
    #> 5:        1.451650
    #> 6:        1.349953

We will also create a column Cell_id_dummy which uniquely identify each cell. It will have values such as `Cell_1, Cell_2,` all the way until `Cell_x` where x is the number of cells in the dataset.

    dat$Cell_id_dummy <- paste0("Cell_", seq_len(nrow(dat)))
    head(dat$Cell_id_dummy, n = 10)
    #>  [1] "Cell_1"  "Cell_2"  "Cell_3"  "Cell_4"  "Cell_5"  "Cell_6"  "Cell_7"
    #>  [8] "Cell_8"  "Cell_9"  "Cell_10"

By default, the `simCytoData` function will generate cells for multiple samples, and that the resulting `data.table` object will already have a column called Sample that denotes the sample the cells come from.

    unique(dat$Sample)
    #> [1] "Sample_1" "Sample_2" "Sample_3"

Let’s take note of the sample and cell id column for later.

    sample_col <- "Sample"
    cell_id_col <- "Cell_id_dummy"

## Creating supercells

Now that we have our data, let’s create some supercells. To do this, we will use `runSuperCellCyto` function and pass the markers, sample and cell ID columns as parameters.

The reason why we need to specify the markers is because the function will create supercells based on only the expression of those markers. We highly recommend creating supercells using all markers in your data, let that be cell type or cell state markers. However, if for any reason you only want to only use a subset of the markers in your data, then make sure you specify them in a vector that you later pass to `runSuperCellCyto` function.

For this tutorial, we will use all the arcsinh transformed markers in the toy data.

    supercells <- runSuperCellCyto(
        dt = dat,
        markers = marker_cols_asinh,
        sample_colname = sample_col,
        cell_id_colname = cell_id_col
    )

Let’s dig deeper into the object it created:

    class(supercells)
    #> [1] "list"

It is a list containing 3 elements:

    names(supercells)
    #> [1] "supercell_expression_matrix" "supercell_cell_map"
    #> [3] "supercell_object"

### Supercell object

The `supercell_object` contains the metadata used to create the supercells. It is a list, and each element contains the metadata used to create the supercells for a sample. This will come in handy if we need to either regenerate the supercells using different gamma values (so we get more or less supercells) or do some debugging later down the line. More on regenerating supercells on Controlling supercells granularity section below.

### Supercell expression matrix

The `supercell_expression_matrix` contains the marker expression of each supercell. These are calculated by taking the average of the marker expression of all the cells contained within a supercell.

    head(supercells$supercell_expression_matrix)
    #>    Marker_1_asinh Marker_2_asinh Marker_3_asinh Marker_4_asinh Marker_5_asinh
    #>             <num>          <num>          <num>          <num>          <num>
    #> 1:       1.695975       1.968647      1.2614149       1.767878       1.700618
    #> 2:       1.707788       1.960498      0.9665544       1.785666       1.702395
    #> 3:       1.686070       1.938568      1.2367424       1.781278       1.744574
    #> 4:       1.679547       1.951769      1.0602099       1.784210       1.646614
    #> 5:       1.727889       1.943670      1.3517581       1.771521       1.736175
    #> 6:       1.624424       1.945974      1.1864452       1.777805       1.748588
    #>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
    #>             <num>          <num>          <num>          <num>           <num>
    #> 1:      0.9563698       1.554815       1.833458       1.481165        1.203895
    #> 2:      0.9796867       1.395891       1.847787       1.598120        1.160687
    #> 3:      1.0153188       1.351294       1.840541       1.491845        1.167477
    #> 4:      0.9330285       1.448671       1.855848       1.555931        1.234564
    #> 5:      0.8932649       1.433724       1.802597       1.504549        1.266174
    #> 6:      0.9789826       1.403885       1.833972       1.719007        1.266114
    #>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
    #>              <num>           <num>           <num>           <num>
    #> 1:        1.801446        2.027888        1.995221       1.1068392
    #> 2:        1.821896        2.013257        1.995230       1.1461121
    #> 3:        1.802573        2.006773        2.005932       1.1421855
    #> 4:        1.813503        2.016490        1.981844       0.8715029
    #> 5:        1.808608        2.033927        1.997574       1.0390746
    #> 6:        1.824237        2.038967        2.013334       0.9838300
    #>    Marker_15_asinh   Sample                 SuperCellId
    #>              <num>   <char>                      <char>
    #> 1:        1.602000 Sample_1 SuperCell_1_Sample_Sample_1
    #> 2:        1.472662 Sample_1 SuperCell_2_Sample_Sample_1
    #> 3:        1.553487 Sample_1 SuperCell_3_Sample_Sample_1
    #> 4:        1.495976 Sample_1 SuperCell_4_Sample_Sample_1
    #> 5:        1.494948 Sample_1 SuperCell_5_Sample_Sample_1
    #> 6:        1.499681 Sample_1 SuperCell_6_Sample_Sample_1

Therein, we will have the following columns:

1. All the markers we previously specified in the `markers_col` variable. In this example, they are the arcsinh transformed markers in our toy data.
2. A column (`Sample` in this case) denoting which sample a supercell belongs to, (note the column name is the same as what is stored in `sample_col` variable).
3. The `SuperCellId` column denoting the unique ID of the supercell.

#### SuperCellId

Let’s have a look at `SuperCellId`:

    head(unique(supercells$supercell_expression_matrix$SuperCellId))
    #> [1] "SuperCell_1_Sample_Sample_1" "SuperCell_2_Sample_Sample_1"
    #> [3] "SuperCell_3_Sample_Sample_1" "SuperCell_4_Sample_Sample_1"
    #> [5] "SuperCell_5_Sample_Sample_1" "SuperCell_6_Sample_Sample_1"

Let’s break down one of them, `SuperCell_1_Sample_Sample_1`. `SuperCell_1` is a numbering (1 to however many supercells there are in a sample) used to uniquely identify each supercell in a sample. Notably, you may encounter this (`SuperCell_1`, `SuperCell_2`) being repeated across different samples, e.g.,

    supercell_ids <- unique(supercells$supercell_expression_matrix$SuperCellId)
    supercell_ids[grep("SuperCell_1_", supercell_ids)]
    #> [1] "SuperCell_1_Sample_Sample_1" "SuperCell_1_Sample_Sample_2"
    #> [3] "SuperCell_1_Sample_Sample_3"

While these 3 supercells’ id are pre-fixed with `SuperCell_1`, it does not make them equal to one another! `SuperCell_1_Sample_Sample_1` will only contain cells from `Sample_1` while `SuperCell_1_Sample_Sample_2` will only contain cells from `Sample_2`.

By now, you may have noticed that we appended the sample name into each supercell id. This aids in differentiating the supercells in different samples.

### Supercell cell map

`supercell_cell_map` maps each cell in our dataset to the supercell it belongs to.

    head(supercells$supercell_cell_map)
    #>                      SuperCellID CellId   Sample
    #>                           <char> <char>   <char>
    #> 1: SuperCell_144_Sample_Sample_1 Cell_1 Sample_1
    #> 2: SuperCell_119_Sample_Sample_1 Cell_2 Sample_1
    #> 3:  SuperCell_33_Sample_Sample_1 Cell_3 Sample_1
    #> 4:   SuperCell_3_Sample_Sample_1 Cell_4 Sample_1
    #> 5:  SuperCell_10_Sample_Sample_1 Cell_5 Sample_1
    #> 6:  SuperCell_61_Sample_Sample_1 Cell_6 Sample_1

This map is very useful if we later need to expand the supercells out. Additionally, this is also the reason why we need to have a column in the dataset which uniquely identify each cell.

## Running `runSuperCellCyto` in parallel

By default, `runSuperCellCyto` will process each sample one after the other. As each sample is processed independent of one another, strictly speaking, we can process all of them in parallel.

To do this, we need to:

1. Create a `BiocParallelParam` object from the BiocParallel package. This object can either be of type `MulticoreParam`or `SnowParam`. We highly recommend consulting their vignette for more information.
2. Set the number of tasks for the `BiocParallelParam` object to the number of samples we have in the dataset.
3. Set the `load_balancing` parameter for `runSuperCellCyto` function to TRUE. This is to ensure even distribution of the supercell creation jobs. As each sample will be processed by a parallel job, we don’t want a job that processs large sample to also be assigned other smaller samples if possible. If you want to know more how this feature works, please refer to our manuscript.

    supercell_par <- runSuperCellCyto(
        dt = dat,
        markers = marker_cols_asinh,
        sample_colname = sample_col,
        cell_id_colname = cell_id_col,
        BPPARAM = MulticoreParam(tasks = n_samples),
        load_balancing = TRUE
    )

## Controlling supercells granularity

This is described in the `runSuperCellCyto` function’s documentation, but let’s briefly go through it here.

The `runSuperCellCyto` function is equipped with various parameters which can be customised to alter the composition of the supercells. The one that is very likely to be used the most is the gamma parameter, denoted as `gam` in the function. By default, the value for `gam` is set to 20, which we found work well for most cases.

The gamma parameter controls how many supercells to generate, and indirectly, how many cells are captured within each supercell. This parameter is resolved into the following formula `gamma=n_cells/n_supercells` where `n_cell` denotes the number of cells and `n_supercells` denotes the number of supercells.

In general, the larger gamma parameter is set to, the less supercells we will get. Say for instance we have 10,000 cells. If gamma is set to 10, we will end up with about 1,000 supercells, whereas if gamma is set to 50, we will end up with about 200 supercells.

You may have noticed, after reading the sections above, `runSuperCellCyto` is ran on each sample independent of each other, and that we can only set 1 value as the gamma parameter. Indeed, for now, the same gamma value will be used across all samples, and that depending on how many cells we have in each sample, we will end up with different number of supercells for each sample. For instance, say we have 10,000 cells for sample 1, and 100,000 cells for sample 2. If gamma is set to 10, for sample 1, we will get 1,000 supercells (10,000/10) while for sample 2, we will get 10,000 supercells (100,000/10).

Do note: whatever gamma value you chose, you should not expect each supercell to contain exactly the same number of cells. This behaviour is intentional to ensure rare cell types are not intermixed with non-rare cell types in a supercell.

### Adjusting gamma value after one run of runSuperCellCyto

If you have run `runSuperCellCyto` once and have not discarded the SuperCell object it generated (no serious, please don’t!), you can use the object to quickly regenerate supercells using different gamma values.

As an example, using the SuperCell object we have generated for our toy dataset, we will regenerate the supercells using gamma of 10 and 50. The function to do this is `recomputeSupercells`. We will store the output in a list, one element per gamma value.

    addt_gamma_vals <- c(10, 50)
    supercells_addt_gamma <- lapply(addt_gamma_vals, function(gam) {
        recomputeSupercells(
            dt = dat,
            sc_objects = supercells$supercell_object,
            markers = marker_cols_asinh,
            sample_colname = sample_col,
            cell_id_colname = cell_id_col,
            gam = gam
        )
    })

We should end up with a list containing 2 elements. The 1st element contains supercells generated using gamma = 10, and the 2nd contains supercells generated using gamma = 50.

    supercells_addt_gamma[[1]]
    #> $supercell_expression_matrix
    #>       Marker_1_asinh Marker_2_asinh Marker_3_asinh Marker_4_asinh
    #>                <num>          <num>          <num>          <num>
    #>    1:       1.707697       1.957919       1.246158       1.808405
    #>    2:       1.745278       1.961237       1.121217       1.775111
    #>    3:       1.663395       1.969190       1.290053       1.787292
    #>    4:       1.662868       1.965070       1.158405       1.785473
    #>    5:       1.763220       1.946304       1.218993       1.782739
    #>   ---
    #> 2996:       1.241481       1.869393       1.913995       1.557217
    #> 2997:       1.101451       1.826971       1.903874       1.388947
    #> 2998:       1.255012       1.882252       1.857123       1.382831
    #> 2999:       1.090200       1.858001       1.821574       1.449144
    #> 3000:       1.020672       1.857079       1.879252       1.426511
    #>       Marker_5_asinh Marker_6_asinh Marker_7_asinh Marker_8_asinh
    #>                <num>          <num>          <num>          <num>
    #>    1:       1.694619      0.9313020       1.444881       1.856305
    #>    2:       1.744289      1.0601882       1.477309       1.836570
    #>    3:       1.754117      0.9990884       1.455166       1.798454
    #>    4:       1.647842      0.9179028       1.458177       1.851681
    #>    5:       1.743366      0.8150327       1.484435       1.829929
    #>   ---
    #> 2996:       1.875707      1.5634495       1.706614       1.191750
    #> 2997:       1.933122      1.4995217       1.751926       1.104210
    #> 2998:       1.919471      1.8123626       1.601594       1.231660
    #> 2999:       1.889054      1.4568335       1.765966       1.328332
    #> 3000:       1.973475      1.6990564       1.852774       1.202663
    #>       Marker_9_asinh Marker_10_asinh Marker_11_asinh Marker_12_asinh
    #>                <num>           <num>           <num>           <num>
    #>    1:       1.591931        1.235461        1.834228       2.0243895
    #>    2:       1.465417        1.378092        1.803463       2.0218208
    #>    3:       1.541620        1.206629        1.804849       2.0223045
    #>    4:       1.571938        1.241638        1.827091       2.0213342
    #>    5:       1.653821        1.272178        1.796987       2.0377078
    #>   ---
    #> 2996:       2.046020        1.552298        1.434901       0.7757942
    #> 2997:       2.100859        1.652811        1.588273       0.7097858
    #> 2998:       2.082562        1.606963        1.623706       0.8465875
    #> 2999:       2.046108        1.592507        1.605812       1.0283409
    #> 3000:       2.105253        1.534081        1.644466       1.0672931
    #>       Marker_13_asinh Marker_14_asinh Marker_15_asinh   Sample
    #>                 <num>           <num>           <num>   <char>
    #>    1:        1.994159       1.1048759        1.482858 Sample_1
    #>    2:        1.976593       1.1113031        1.427221 Sample_1
    #>    3:        1.977521       0.9560697        1.420958 Sample_1
    #>    4:        1.979585       1.1823172        1.411753 Sample_1
    #>    5:        1.972270       0.9863168        1.273552 Sample_1
    #>   ---
    #> 2996:        1.938619       1.5739190        2.051214 Sample_3
    #> 2997:        1.898013       1.3105186        2.065234 Sample_3
    #> 2998:        1.989674       1.5531819        2.078115 Sample_3
    #> 2999:        1.815391       1.4429089        2.033882 Sample_3
    #> 3000:        1.891530       1.3257889        2.033576 Sample_3
    #>                          SuperCellId
    #>                               <char>
    #>    1:    SuperCell_1_Sample_Sample_1
    #>    2:    SuperCell_2_Sample_Sample_1
    #>    3:    SuperCell_3_Sample_Sample_1
    #>    4:    SuperCell_4_Sample_Sample_1
    #>    5:    SuperCell_5_Sample_Sample_1
    #>   ---
    #> 2996:  SuperCell_996_Sample_Sample_3
    #> 2997:  SuperCell_997_Sample_Sample_3
    #> 2998:  SuperCell_998_Sample_Sample_3
    #> 2999:  SuperCell_999_Sample_Sample_3
    #> 3000: SuperCell_1000_Sample_Sample_3
    #>
    #> $supercell_cell_map
    #>                          SuperCellID     CellId   Sample
    #>                               <char>     <char>   <char>
    #>     1:  SuperCell_76_Sample_Sample_1     Cell_1 Sample_1
    #>     2: SuperCell_462_Sample_Sample_1     Cell_2 Sample_1
    #>     3: SuperCell_595_Sample_Sample_1     Cell_3 Sample_1
    #>     4: SuperCell_737_Sample_Sample_1     Cell_4 Sample_1
    #>     5: SuperCell_288_Sample_Sample_1     Cell_5 Sample_1
    #>    ---
    #> 29996: SuperCell_609_Sample_Sample_3 Cell_29996 Sample_3
    #> 29997: SuperCell_355_Sample_Sample_3 Cell_29997 Sample_3
    #> 29998:  SuperCell_12_Sample_Sample_3 Cell_29998 Sample_3
    #> 29999:  SuperCell_59_Sample_Sample_3 Cell_29999 Sample_3
    #> 30000: SuperCell_213_Sample_Sample_3 Cell_30000 Sample_3

The output generated by `recomputeSupercells` is essentially a list:

1. `supercell_expression_matrix`: A data.table object that contains the marker expression for each supercell.
2. `supercell_cell_map`: A data.table that maps each cell to its corresponding supercell.

As mentioned before, gamma dictates the granularity of supercells. Compared to the previous run where gamma was set to 20, we should get more supercells for gamma = 10, and less for gamma = 50. Let’s see if that’s the case.

    n_supercells_gamma20 <- nrow(supercells$supercell_expression_matrix)
    n_supercells_gamma10 <- nrow(
        supercells_addt_gamma[[1]]$supercell_expression_matrix
    )
    n_supercells_gamma50 <- nrow(
        supercells_addt_gamma[[2]]$supercell_expression_matrix
    )

    n_supercells_gamma10 > n_supercells_gamma20
    #> [1] TRUE

    n_supercells_gamma50 < n_supercells_gamma20
    #> [1] TRUE

### Specifying different gamma value for different samples

In the future, we may add the ability to specify different `gam` value for different samples. For now, if we want to do this, we will need to break down our data into multiple `data.table` objects, each containing data from 1 sample, and run `runSuperCellCyto` function on each of them with different `gam` parameter value. Something like the following:

    n_markers <- 10
    dat <- simCytoData(nmarkers = n_markers)
    markers_col <- paste0("Marker_", seq_len(n_markers))
    sample_col <- "Sample"
    cell_id_col <- "Cell_Id"

    samples <- unique(dat[[sample_col]])
    gam_values <- c(10, 20, 10)

    supercells_diff_gam <- lapply(seq_len(length(samples)), function(i) {
        sample <- samples[i]
        gam <- gam_values[i]
        dat_samp <- dat[dat$Sample == sample, ]
        supercell_samp <- runSuperCellCyto(
            dt = dat_samp,
            markers = markers_col,
            sample_colname = sample_col,
            cell_id_colname = cell_id_col,
            gam = gam
        )
        return(supercell_samp)
    })

Subsequently, to extract and combine the `supercell_expression_matrix` and `supercell_cell_map`, we will need to use `rbind`:

    supercell_expression_matrix <- do.call(
        "rbind", lapply(
            supercells_diff_gam, function(x) x[["supercell_expression_matrix"]]
        )
    )

    supercell_cell_map <- do.call(
        "rbind", lapply(
            supercells_diff_gam, function(x) x[["supercell_cell_map"]]
        )
    )

    rbind(
        head(supercell_expression_matrix, n = 3),
        tail(supercell_expression_matrix, n = 3)
    )
    #>     Marker_1 Marker_2 Marker_3 Marker_4  Marker_5  Marker_6  Marker_7  Marker_8
    #>        <num>    <num>    <num>    <num>     <num>     <num>     <num>     <num>
    #> 1: 16.211054 14.91943 10.56451 20.95005 19.015744  8.546717  8.840176  7.309379
    #> 2: 16.036312 14.35038 12.80379 19.29045 17.970337  8.981264  7.978600  6.544881
    #> 3: 14.279986 16.51350 11.98877 19.54727 17.385101  9.719845  8.520499  7.473318
    #> 4: 11.876363 11.00050 11.14677 17.92020  6.464099 10.666095 12.496560 13.386191
    #> 5: 11.285662 10.49172 10.92950 15.85961  6.017023 11.162505 12.101406 15.693726
    #> 6:  9.957905 10.37258 11.93811 16.59626  5.835826 11.735965 10.670657 16.788434
    #>     Marker_9 Marker_10   Sample                   SuperCellId
    #>        <num>     <num>   <char>                        <char>
    #> 1:  5.681944 10.830575 Sample_1   SuperCell_1_Sample_Sample_1
    #> 2:  7.704902 11.949514 Sample_1   SuperCell_2_Sample_Sample_1
    #> 3:  7.913309  9.101584 Sample_1   SuperCell_3_Sample_Sample_1
    #> 4: 16.162279 11.160861 Sample_2 SuperCell_498_Sample_Sample_2
    #> 5: 14.552756 10.450120 Sample_2 SuperCell_499_Sample_Sample_2
    #> 6: 14.262912 12.289016 Sample_2 SuperCell_500_Sample_Sample_2

    rbind(head(supercell_cell_map, n = 3), tail(supercell_cell_map, n = 3))
    #>                      SuperCellID     CellId   Sample
    #>                           <char>     <char>   <char>
    #> 1: SuperCell_705_Sample_Sample_1     Cell_1 Sample_1
    #> 2:  SuperCell_55_Sample_Sample_1     Cell_2 Sample_1
    #> 3: SuperCell_302_Sample_Sample_1     Cell_3 Sample_1
    #> 4: SuperCell_222_Sample_Sample_2 Cell_19998 Sample_2
    #> 5:   SuperCell_7_Sample_Sample_2 Cell_19999 Sample_2
    #> 6:   SuperCell_7_Sample_Sample_2 Cell_20000 Sample_2

## Mixing cells from different samples in a supercell

If for whatever reason you don’t mind (or perhaps more to the point want) each supercell to contain cells from different biological samples, you still need to have the sample column in your `data.table`. However, what you need to do is essentially set the value in the column to exactly one unique value. That way, SuperCellCyto will treat all cells as coming from one sample.

Just note, the parallel processing feature in SuperCellCyto won’t work for this as you will essentially only have 1 sample and nothing for SuperCellCyto to parallelise.

## I have more cells than RAM in my computer

Is your dataset so huge that you are constantly running out of RAM when generating supercells? This thing happens and we have a solution for it.

Since supercells are generated for each sample independent of others you can easily break up the process. For example:

1. Load up a subset of the samples (say 1-10).
2. Generate supercells for those samples.
3. Save the output using the qs package.
4. Extract the `supercell_expression_matrix` and `supercell_cell_map`, and export them out as a csv file using `data.table`’s `fwrite` function.
5. Load another sets of samples (say 11-20), rinse and repeat step 2-4.

Once you have processed all the samples, you can then load all `supercell_expression_matrix` and `supercell_cell_map` csv files and analyse them.

If you want to regenerate the supercells using different gamma values, load the relevant output saved using the qs package and the relevant data (remember to note which output belongs to which sets of samples!), and run `recomputeSupercells` function.

## Session information

    sessionInfo()
    #> R version 4.5.2 (2025-10-31)
    #> 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=C
    #>  [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] parallel  stats     graphics  grDevices utils     datasets  methods
    #> [8] base
    #>
    #> other attached packages:
    #> [1] BiocParallel_1.45.0 SuperCellCyto_1.1.0 BiocStyle_2.39.0
    #>
    #> loaded via a namespace (and not attached):
    #>  [1] cli_3.6.5           knitr_1.51          rlang_1.1.7
    #>  [4] xfun_0.56           otel_0.2.0          data.table_1.18.2.1
    #>  [7] jsonlite_2.0.0      buildtools_1.0.0    plyr_1.8.9
    #> [10] htmltools_0.5.9     maketools_1.3.2     sys_3.4.3
    #> [13] sass_0.4.10         rmarkdown_2.30      grid_4.5.2
    #> [16] evaluate_1.0.5      jquerylib_0.1.4     fastmap_1.2.0
    #> [19] yaml_2.3.12         lifecycle_1.0.5     BiocManager_1.30.27
    #> [22] compiler_4.5.2      igraph_2.2.2        codetools_0.2-20
    #> [25] Rcpp_1.1.1          pkgconfig_2.0.3     lattice_0.22-9
    #> [28] digest_0.6.39       SuperCell_1.1       R6_2.6.1
    #> [31] RANN_2.6.2          magrittr_2.0.4      bslib_0.10.0
    #> [34] Matrix_1.7-4        tools_4.5.2         cachem_1.1.0