Contents

1 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:

2 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")

3 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: 10.985951 18.92504 15.24139 16.46809  9.892782 7.052671 6.009254  8.294975
#> 2: 10.854232 20.70230 15.62215 17.35025 10.699370 5.637152 7.155407  9.936446
#> 3:  9.575721 19.83976 17.42497 17.66569  9.999425 5.185331 5.625805 12.338316
#> 4: 11.461418 18.68447 17.70840 17.21790  9.647867 6.056827 6.125524 12.388790
#> 5:  9.840082 20.11952 15.50651 18.02336  8.222368 7.263901 6.178444 13.524903
#> 6: 10.651782 19.87201 17.18498 19.26682  9.694247 5.721024 6.289349 11.243914
#>    Marker_9 Marker_10 Marker_11 Marker_12 Marker_13 Marker_14 Marker_15
#>       <num>     <num>     <num>     <num>     <num>     <num>     <num>
#> 1: 14.79969  20.84234  13.94891  16.18453  18.02470  17.63522  15.47656
#> 2: 14.10967  19.14952  15.95355  15.10837  18.70499  18.18957  17.12559
#> 3: 14.05524  21.41114  15.13933  16.26581  19.67174  16.24460  17.69191
#> 4: 13.10017  20.60612  13.59304  15.06647  21.95759  17.71545  16.89051
#> 5: 14.28064  19.27985  13.92026  16.23957  20.62494  18.55540  15.60154
#> 6: 13.72961  18.67405  13.83047  16.70181  21.39723  18.45065  18.37643
#>      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.528497       2.041206       1.833604       1.907422       1.434004
#> 2:       1.517530       2.128228       1.857077       1.957460       1.504495
#> 3:       1.405030       2.086911       1.961590       1.974784       1.443584
#> 4:       1.567197       2.028842       1.977108       1.950104       1.411689
#> 5:       1.429240       2.100494       1.850003       1.994085       1.272321
#> 6:       1.500458       2.088486       1.948266       2.058520       1.415949
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:      1.1440897      1.0171574       1.279842       1.805701        2.134782
#> 2:      0.9686733      1.1559160       1.437937       1.760581        2.052610
#> 3:      0.9073420      0.9671667       1.635154       1.756938        2.160983
#> 4:      1.0232287      1.0319462       1.638939       1.690911        2.123702
#> 5:      1.1682817      1.0386216       1.720780       1.771941        2.059175
#> 6:      0.9797580      1.0524998       1.549663       1.734883        2.028303
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.749787        1.890815        1.994157        1.973124
#> 2:        1.877089        1.825278        2.029902        2.002934
#> 3:        1.827222        1.895601        2.078665        1.894354
#> 4:        1.725495        1.822642        2.185540        1.977491
#> 5:        1.747851        1.894059        2.124589        2.022147
#> 6:        1.741763        1.920914        2.160350        2.016683
#>    Marker_15_asinh
#>              <num>
#> 1:        1.848164
#> 2:        1.944942
#> 3:        1.976212
#> 4:        1.931682
#> 5:        1.855820
#> 6:        2.012792

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"

4 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"

4.1 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.

4.2 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.412935       2.048201       1.901283       1.926233       1.463444
#> 2:       1.428893       2.014394       1.912835       1.913136       1.583205
#> 3:       1.424869       2.048025       1.906901       1.930478       1.430680
#> 4:       1.331828       2.060115       1.866658       1.935375       1.431797
#> 5:       1.484109       2.034663       1.917219       1.924852       1.408893
#> 6:       1.502977       2.047282       1.934003       1.957347       1.332065
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:      1.1114821      1.0785099       1.522875       1.823572        2.082820
#> 2:      1.0864375      1.0531769       1.432117       1.730216        2.070771
#> 3:      0.9789532      1.1289993       1.538851       1.717079        2.086520
#> 4:      1.1873268      0.7610904       1.508708       1.699345        2.074075
#> 5:      1.0510285      1.1941709       1.534359       1.766927        2.072177
#> 6:      0.9918874      0.8957402       1.503569       1.783012        2.110842
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.788818        1.827023        2.087567        1.924981
#> 2:        1.787367        1.870268        2.076605        1.912200
#> 3:        1.762907        1.836119        2.084404        1.931198
#> 4:        1.789969        1.858803        2.072401        1.927824
#> 5:        1.745689        1.857483        2.070711        1.930845
#> 6:        1.735639        1.777169        2.092868        1.944899
#>    Marker_15_asinh   Sample                 SuperCellId
#>              <num>   <char>                      <char>
#> 1:        1.908639 Sample_1 SuperCell_1_Sample_Sample_1
#> 2:        1.827192 Sample_1 SuperCell_2_Sample_Sample_1
#> 3:        1.888304 Sample_1 SuperCell_3_Sample_Sample_1
#> 4:        1.924940 Sample_1 SuperCell_4_Sample_Sample_1
#> 5:        1.833499 Sample_1 SuperCell_5_Sample_Sample_1
#> 6:        1.872055 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.

4.2.1 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.

4.3 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_33_Sample_Sample_1 Cell_1 Sample_1
#> 2: SuperCell_181_Sample_Sample_1 Cell_2 Sample_1
#> 3: SuperCell_144_Sample_Sample_1 Cell_3 Sample_1
#> 4: SuperCell_355_Sample_Sample_1 Cell_4 Sample_1
#> 5: SuperCell_271_Sample_Sample_1 Cell_5 Sample_1
#> 6:  SuperCell_27_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.

5 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 MulticoreParamor 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
)

6 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.

6.1 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.298032      2.0423980      1.8393311       1.894298
#>    2:       1.403487      2.0427359      1.9280171       1.941889
#>    3:       1.571703      2.0011871      1.8871179       1.919377
#>    4:       1.523479      2.0358288      1.8793203       1.904579
#>    5:       1.599126      2.0411400      1.9341608       1.960238
#>   ---                                                            
#> 2996:       1.862170      0.9729094      0.9716588       1.067262
#> 2997:       1.882120      1.0701083      1.0441174       1.040361
#> 2998:       1.866087      1.3212286      1.0065838       1.334461
#> 2999:       1.852806      1.1759656      1.0542429       1.275500
#> 3000:       1.828689      1.2100589      0.9385149       1.174988
#>       Marker_5_asinh Marker_6_asinh Marker_7_asinh Marker_8_asinh
#>                <num>          <num>          <num>          <num>
#>    1:       1.420051       1.144670      1.0328708       1.539273
#>    2:       1.332958       1.089010      0.9194777       1.574707
#>    3:       1.526369       1.163525      0.9120273       1.557306
#>    4:       1.487685       1.074061      0.9192981       1.557385
#>    5:       1.449764       1.136674      0.9505148       1.607799
#>   ---                                                            
#> 2996:       1.878565       1.965739      2.0189411       1.560505
#> 2997:       1.702568       1.970868      2.0326167       1.567569
#> 2998:       1.682192       1.963131      1.9613896       1.750530
#> 2999:       1.895739       1.946141      1.9952235       1.655330
#> 3000:       1.755615       1.957450      1.9969360       1.618182
#>       Marker_9_asinh Marker_10_asinh Marker_11_asinh Marker_12_asinh
#>                <num>           <num>           <num>           <num>
#>    1:       1.733348        2.054012        1.757714        1.830054
#>    2:       1.711952        2.093765        1.681353        1.842326
#>    3:       1.683408        2.054827        1.760872        1.791236
#>    4:       1.738055        2.066526        1.822754        1.769459
#>    5:       1.756893        2.092553        1.810382        1.859215
#>   ---                                                               
#> 2996:       1.548024        2.087263        1.981992        1.310919
#> 2997:       1.645901        2.050793        2.033340        1.395154
#> 2998:       1.633809        2.030802        1.999874        1.273692
#> 2999:       1.453180        2.051019        1.932535        1.544152
#> 3000:       1.684353        2.097208        1.973135        1.358073
#>       Marker_13_asinh Marker_14_asinh Marker_15_asinh   Sample
#>                 <num>           <num>           <num>   <char>
#>    1:        2.073563        1.901118       1.9029961 Sample_1
#>    2:        2.095129        1.932445       1.8620012 Sample_1
#>    3:        2.079724        1.934534       1.8213384 Sample_1
#>    4:        2.063694        1.895983       1.8367392 Sample_1
#>    5:        2.090592        1.965321       1.8387907 Sample_1
#>   ---                                                         
#> 2996:        2.112741        1.747819       0.6238122 Sample_3
#> 2997:        2.038723        1.779414       0.8870070 Sample_3
#> 2998:        2.123540        1.719946       0.9429534 Sample_3
#> 2999:        2.059879        1.693569       0.8802068 Sample_3
#> 3000:        2.088202        1.788657       0.9839559 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_240_Sample_Sample_1     Cell_1 Sample_1
#>     2: SuperCell_207_Sample_Sample_1     Cell_2 Sample_1
#>     3: SuperCell_318_Sample_Sample_1     Cell_3 Sample_1
#>     4: SuperCell_210_Sample_Sample_1     Cell_4 Sample_1
#>     5: SuperCell_185_Sample_Sample_1     Cell_5 Sample_1
#>    ---                                                  
#> 29996: SuperCell_697_Sample_Sample_3 Cell_29996 Sample_3
#> 29997: SuperCell_430_Sample_Sample_3 Cell_29997 Sample_3
#> 29998: SuperCell_848_Sample_Sample_3 Cell_29998 Sample_3
#> 29999: SuperCell_483_Sample_Sample_3 Cell_29999 Sample_3
#> 30000: SuperCell_407_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

6.2 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: 11.15117  5.489100 14.07799 14.042325  7.368316 5.678291 10.410925 14.14179
#> 2: 10.58016  6.900411 14.67900 14.559571  7.384919 7.873829  9.768894 17.17860
#> 3: 11.33809  5.251414 15.35944 15.862501  7.640695 7.642583  9.942032 17.04151
#> 4: 19.20795  7.309590 10.42486  7.929300 17.252944 8.279641 12.923321 15.29973
#> 5: 18.88995  8.617942 10.08036  6.275188 18.209921 8.722158 11.997128 15.05997
#> 6: 17.77008 11.809979 12.22792  8.461766 16.262462 9.177376 12.386524 13.57999
#>    Marker_9 Marker_10   Sample                   SuperCellId
#>       <num>     <num>   <char>                        <char>
#> 1: 14.72846 17.542332 Sample_1   SuperCell_1_Sample_Sample_1
#> 2: 15.59383 17.591050 Sample_1   SuperCell_2_Sample_Sample_1
#> 3: 16.09497 17.444965 Sample_1   SuperCell_3_Sample_Sample_1
#> 4: 17.03363  8.977076 Sample_2 SuperCell_498_Sample_Sample_2
#> 5: 16.80659 10.845891 Sample_2 SuperCell_499_Sample_Sample_2
#> 6: 17.19567  7.515481 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_75_Sample_Sample_1     Cell_1 Sample_1
#> 2:  SuperCell_69_Sample_Sample_1     Cell_2 Sample_1
#> 3: SuperCell_356_Sample_Sample_1     Cell_3 Sample_1
#> 4: SuperCell_180_Sample_Sample_2 Cell_19998 Sample_2
#> 5: SuperCell_257_Sample_Sample_2 Cell_19999 Sample_2
#> 6: SuperCell_137_Sample_Sample_2 Cell_20000 Sample_2

7 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.

8 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.

9 Session information

sessionInfo()
#> R version 4.5.1 Patched (2025-09-10 r88807)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Ventura 13.7.7
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
#> 
#> locale:
#> [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] parallel  stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#> [1] BiocParallel_1.43.4  SuperCellCyto_0.99.2 BiocStyle_2.37.1    
#> 
#> loaded via a namespace (and not attached):
#>  [1] cli_3.6.5           knitr_1.50          rlang_1.1.6        
#>  [4] xfun_0.53           jsonlite_2.0.0      data.table_1.17.8  
#>  [7] plyr_1.8.9          htmltools_0.5.8.1   sass_0.4.10        
#> [10] rmarkdown_2.30      grid_4.5.1          evaluate_1.0.5     
#> [13] jquerylib_0.1.4     fastmap_1.2.0       yaml_2.3.10        
#> [16] lifecycle_1.0.4     bookdown_0.45       BiocManager_1.30.26
#> [19] compiler_4.5.1      igraph_2.1.4        codetools_0.2-20   
#> [22] Rcpp_1.1.0          pkgconfig_2.0.3     lattice_0.22-7     
#> [25] digest_0.6.37       SuperCell_1.0.1     R6_2.6.1           
#> [28] RANN_2.6.2          magrittr_2.0.4      bslib_0.9.0        
#> [31] Matrix_1.7-4        tools_4.5.1         cachem_1.1.0