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:

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: 6.690293 16.35598 11.538150 8.519740 15.82040 19.49128 11.325890 12.14075
#> 2: 6.362607 17.47847  9.518611 7.691089 15.98748 17.42539 11.079359 13.11950
#> 3: 5.738978 17.59086  9.424304 8.063388 15.94240 18.63041 12.095094 14.39915
#> 4: 7.042030 17.55969 12.125463 7.145059 16.25228 17.01380  9.453220 15.68691
#> 5: 4.594882 17.22542 12.365464 8.792469 16.83030 17.97322 11.823139 17.82280
#> 6: 6.872091 18.28554 12.329209 7.477981 16.58388 18.82708  9.408109 13.72172
#>    Marker_9 Marker_10 Marker_11 Marker_12 Marker_13 Marker_14 Marker_15
#>       <num>     <num>     <num>     <num>     <num>     <num>     <num>
#> 1: 18.55045  11.74391 10.887987  12.54395  16.34810  17.26963  7.197891
#> 2: 19.05781  13.16573  9.596183  14.30942  15.97168  16.43376  5.073170
#> 3: 20.45153  11.13983 12.165618  12.14166  16.37601  18.35742  4.227951
#> 4: 20.01305  11.77154 11.848125  12.50445  15.73711  18.49262  4.834720
#> 5: 18.50030   9.07145 12.577627  11.69245  15.80508  19.24256  4.347960
#> 6: 20.14014  11.49629 10.288674  11.03713  18.62591  16.31259  5.027185
#>      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.1014442       1.900887       1.573317       1.302820       1.869095
#> 2:      1.0615851       1.964537       1.399731       1.215777       1.879116
#> 3:      0.9821189       1.970701       1.390925       1.255683       1.876422
#> 4:      1.1428582       1.968995       1.619046       1.154730       1.894806
#> 5:      0.8229057       1.950523       1.637191       1.330104       1.928258
#> 6:      1.1230224       2.008009       1.634470       1.192318       1.914128
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:       2.069736       1.556305       1.620211       2.021890        1.589558
#> 2:       1.961613       1.536209       1.692289       2.047967        1.695576
#> 3:       2.026043       1.616728       1.779745       2.116385        1.541172
#> 4:       1.938658       1.393632       1.861018       2.095345        1.591720
#> 5:       1.991401       1.595747       1.983307       2.019276        1.357360
#> 6:       2.036189       1.389406       1.734343       2.101488        1.569983
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.520351        1.650491        1.900426        1.952985
#> 2:        1.406922        1.773841        1.878173        1.905425
#> 3:        1.622103        1.620280        1.902058        2.011794
#> 4:        1.597692        1.647561        1.864062        2.018876
#> 5:        1.652982        1.585518        1.868171        2.057301
#> 6:        1.469161        1.532730        2.025810        1.898347
#>    Marker_15_asinh
#>              <num>
#> 1:       1.1607731
#> 2:       0.8916836
#> 3:       0.7678737
#> 4:       0.8578052
#> 5:       0.7860934
#> 6:       0.8852130

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.0984354       1.975371       1.533194       1.399537       1.902018
#> 2:      0.9832703       1.954772       1.599186       1.308966       1.908396
#> 3:      0.9254923       1.983580       1.599452       1.259969       1.902714
#> 4:      1.1589192       1.957294       1.505509       1.204579       1.905435
#> 5:      1.1495620       1.974594       1.509134       1.284347       1.910461
#> 6:      1.0441855       1.958832       1.469640       1.328660       1.917942
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:       2.014745       1.349120       1.877103       2.055161        1.486599
#> 2:       1.997207       1.465276       1.855278       2.042491        1.569791
#> 3:       2.024378       1.503544       1.886475       2.052368        1.736972
#> 4:       2.003124       1.457510       1.839821       2.045881        1.601212
#> 5:       2.018994       1.544091       1.826047       2.036060        1.537187
#> 6:       2.011414       1.388906       1.861328       2.052635        1.636342
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.552390        1.605388        1.873046        1.987688
#> 2:        1.509828        1.684767        1.884852        1.965686
#> 3:        1.574358        1.708684        1.887575        1.988004
#> 4:        1.569710        1.671606        1.896149        1.984240
#> 5:        1.654373        1.649227        1.876425        1.971131
#> 6:        1.534312        1.700082        1.888643        1.978575
#>    Marker_15_asinh   Sample                 SuperCellId
#>              <num>   <char>                      <char>
#> 1:       0.8177384 Sample_1 SuperCell_1_Sample_Sample_1
#> 2:       1.1118809 Sample_1 SuperCell_2_Sample_Sample_1
#> 3:       0.9657170 Sample_1 SuperCell_3_Sample_Sample_1
#> 4:       0.8766668 Sample_1 SuperCell_4_Sample_Sample_1
#> 5:       1.1205160 Sample_1 SuperCell_5_Sample_Sample_1
#> 6:       1.0964279 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_106_Sample_Sample_1 Cell_1 Sample_1
#> 2:  SuperCell_38_Sample_Sample_1 Cell_2 Sample_1
#> 3: SuperCell_186_Sample_Sample_1 Cell_3 Sample_1
#> 4:  SuperCell_49_Sample_Sample_1 Cell_4 Sample_1
#> 5: SuperCell_140_Sample_Sample_1 Cell_5 Sample_1
#> 6: SuperCell_208_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 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
)

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:      0.8403779      1.9800033       1.589779      1.3723062
#>    2:      1.0939673      2.0041286       1.604611      1.1346497
#>    3:      0.8162539      1.9646675       1.580480      1.1680536
#>    4:      0.9365651      1.9717562       1.513621      1.2531718
#>    5:      1.0107698      1.9388276       1.538572      0.9832169
#>   ---                                                            
#> 2996:      1.7225959      0.7223131       1.567073      0.9660857
#> 2997:      1.6502191      0.7623724       1.398465      0.9580294
#> 2998:      1.6775520      1.0798178       1.442021      1.1533282
#> 2999:      1.7372931      0.8628429       1.525781      1.2384986
#> 3000:      1.8090466      0.8524256       1.616617      0.9487394
#>       Marker_5_asinh Marker_6_asinh Marker_7_asinh Marker_8_asinh
#>                <num>          <num>          <num>          <num>
#>    1:      1.9276982       2.009563       1.439912       1.873085
#>    2:      2.0055522       2.036801       1.567558       1.860328
#>    3:      1.9395046       2.022597       1.448242       1.816095
#>    4:      1.9361143       2.043498       1.449453       1.815111
#>    5:      1.9054682       2.008267       1.413753       1.841277
#>   ---                                                            
#> 2996:      1.0856870       1.411232       1.938764       1.330669
#> 2997:      1.2208556       1.443173       1.935883       1.266963
#> 2998:      0.9226135       1.456691       1.910857       1.499850
#> 2999:      1.1290734       1.569584       1.924680       1.267083
#> 3000:      0.9586672       1.508178       1.937747       1.094951
#>       Marker_9_asinh Marker_10_asinh Marker_11_asinh Marker_12_asinh
#>                <num>           <num>           <num>           <num>
#>    1:       2.053800        1.440588        1.606849        1.640653
#>    2:       2.102660        1.655425        1.597475        1.710211
#>    3:       2.025988        1.488078        1.533194        1.661098
#>    4:       2.065783        1.586494        1.643799        1.698179
#>    5:       2.011224        1.551651        1.582777        1.622978
#>   ---                                                               
#> 2996:       1.848986        1.805045        1.986529        1.365181
#> 2997:       1.864314        1.868195        1.973207        1.285372
#> 2998:       1.850673        1.795296        1.920047        1.273880
#> 2999:       1.926953        1.880948        2.043967        1.294178
#> 3000:       1.918639        1.822565        1.996419        1.484516
#>       Marker_13_asinh Marker_14_asinh Marker_15_asinh   Sample
#>                 <num>           <num>           <num>   <char>
#>    1:        1.912314        1.971101       1.1203158 Sample_1
#>    2:        1.936112        2.005102       0.9199828 Sample_1
#>    3:        1.880447        1.999329       1.0592584 Sample_1
#>    4:        1.912689        1.983903       1.1305550 Sample_1
#>    5:        1.884017        1.959900       1.0266989 Sample_1
#>   ---                                                         
#> 2996:        1.102543        2.039751       1.0531125 Sample_3
#> 2997:        1.344772        2.027711       1.1939371 Sample_3
#> 2998:        1.248622        2.074858       1.0283643 Sample_3
#> 2999:        1.403156        2.061042       1.3464771 Sample_3
#> 3000:        1.373011        2.076907       1.2055238 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_223_Sample_Sample_1     Cell_1 Sample_1
#>     2: SuperCell_496_Sample_Sample_1     Cell_2 Sample_1
#>     3: SuperCell_170_Sample_Sample_1     Cell_3 Sample_1
#>     4: SuperCell_324_Sample_Sample_1     Cell_4 Sample_1
#>     5: SuperCell_476_Sample_Sample_1     Cell_5 Sample_1
#>    ---                                                  
#> 29996: SuperCell_159_Sample_Sample_3 Cell_29996 Sample_3
#> 29997:  SuperCell_10_Sample_Sample_3 Cell_29997 Sample_3
#> 29998:  SuperCell_90_Sample_Sample_3 Cell_29998 Sample_3
#> 29999: SuperCell_875_Sample_Sample_3 Cell_29999 Sample_3
#> 30000:  SuperCell_33_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:  9.889391 11.49314 12.906708 4.423114  6.754555 10.350730 18.42971  9.373620
#> 2:  8.842909 10.40142 13.476180 4.076115  7.349778  8.546513 17.93795  9.115597
#> 3: 11.232482 10.56402 14.056184 6.280163  7.718581  9.122282 20.82806  9.171753
#> 4: 11.383727 10.30323  7.510343 7.917540 14.233076 10.770021 19.09221 15.883561
#> 5: 11.168507 12.33879  6.162058 9.053740 16.557420  9.256232 18.54515 16.925443
#> 6: 10.162491 14.88312 10.328629 9.271263 14.749572 10.103672 19.48714 15.764786
#>     Marker_9 Marker_10   Sample                   SuperCellId
#>        <num>     <num>   <char>                        <char>
#> 1: 18.027649  8.219212 Sample_1   SuperCell_1_Sample_Sample_1
#> 2: 18.445920  8.191902 Sample_1   SuperCell_2_Sample_Sample_1
#> 3: 17.885160  8.537640 Sample_1   SuperCell_3_Sample_Sample_1
#> 4: 10.698972 21.363416 Sample_2 SuperCell_498_Sample_Sample_2
#> 5:  9.416567 18.261409 Sample_2 SuperCell_499_Sample_Sample_2
#> 6: 11.314505 18.896204 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_242_Sample_Sample_1     Cell_1 Sample_1
#> 2: SuperCell_128_Sample_Sample_1     Cell_2 Sample_1
#> 3: SuperCell_197_Sample_Sample_1     Cell_3 Sample_1
#> 4: SuperCell_226_Sample_Sample_2 Cell_19998 Sample_2
#> 5:  SuperCell_43_Sample_Sample_2 Cell_19999 Sample_2
#> 6: SuperCell_248_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.3 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.1        codetools_0.2-20   
#> [25] Rcpp_1.1.1          pkgconfig_2.0.3     lattice_0.22-7     
#> [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