Contents

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

0.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")

0.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: 11.95062 8.435675 12.12331 12.87478 17.11644  9.242091 4.252338 17.38621
#> 2: 13.67572 6.403048 11.00160 12.43230 17.78372 10.737092 6.608521 17.49549
#> 3: 12.91400 5.597631 10.72676 13.00119 16.80518 10.892443 7.060820 15.79940
#> 4: 13.51546 5.725295 11.55805 13.48030 18.03698 10.936770 5.222383 15.65938
#> 5: 11.92339 5.564151 10.49814 13.22913 18.50773 11.696724 4.990729 17.37764
#> 6: 15.44470 8.106141 13.57077 13.12140 17.90543 11.286487 7.652315 14.77833
#>    Marker_9 Marker_10 Marker_11 Marker_12 Marker_13 Marker_14 Marker_15
#>       <num>     <num>     <num>     <num>     <num>     <num>     <num>
#> 1: 15.40489  9.838332  11.50721  18.23125  6.872547  18.00419  16.67432
#> 2: 14.39686 11.207974  13.39647  17.91081  5.944566  15.07993  17.86603
#> 3: 12.68947  9.716699  12.51227  16.23243  4.675999  18.20679  17.64851
#> 4: 11.94714 10.232539  13.87130  17.69450  7.147230  15.72930  18.63180
#> 5: 15.17089 10.881204  12.56310  17.75726  7.677535  17.47714  17.44191
#> 6: 13.02728  9.325278  12.58588  16.07268  8.921207  17.55593  18.79793
#>      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.605633      1.2942792       1.618882       1.674715       1.944430
#> 2:       1.731189      1.0665728       1.529793       1.642191       1.981194
#> 3:       1.677551      0.9634181       1.506811       1.683829       1.926826
#> 4:       1.720125      0.9803200       1.574898       1.717683       1.994813
#> 5:       1.603529      0.9589499       1.487323       1.700069       2.019664
#> 6:       1.846203      1.2601809       1.723956       1.692424       1.987761
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:       1.373716      0.7715937       1.959450       1.843748        1.429081
#> 2:       1.507684      1.0916153       1.965473       1.779594        1.546739
#> 3:       1.520723      1.1450320       1.867828       1.661214        1.418005
#> 4:       1.524415      0.9124762       1.859344       1.605364        1.464243
#> 5:       1.585854      0.8800619       1.958976       1.829199        1.519785
#> 6:       1.553118      1.2115431       1.804333       1.685700        1.381605
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.570853        2.005141       1.1230761        1.993060
#> 2:        1.711836        1.988051       1.0088562        1.823490
#> 3:        1.648142        1.893638       0.8348028        2.003846
#> 4:        1.744536        1.976353       1.1549788        1.863589
#> 5:        1.651908        1.979760       1.2142989        1.964464
#> 6:        1.653591        1.884191       1.3427620        1.968789
#>    Marker_15_asinh
#>              <num>
#> 1:        1.919336
#> 2:        1.985640
#> 3:        1.973849
#> 4:        2.026115
#> 5:        1.962524
#> 6:        2.034692

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"

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

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

0.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.665955       1.065388       1.685252       1.707283       1.995129
#> 2:       1.772048       1.113388       1.694953       1.715126       2.006789
#> 3:       1.737853       1.250256       1.565679       1.712457       1.979766
#> 4:       1.711704       1.117843       1.634908       1.685022       1.984612
#> 5:       1.691325       1.150517       1.592246       1.693890       1.978676
#> 6:       1.694639       1.267181       1.704771       1.797614       1.996498
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:       1.464966      0.9917763       1.907836       1.792918        1.501953
#> 2:       1.543158      1.1239568       1.917212       1.755198        1.593464
#> 3:       1.601971      0.8680859       1.897587       1.740295        1.581205
#> 4:       1.467794      0.8290940       1.903642       1.746858        1.510302
#> 5:       1.522968      0.8509464       1.894454       1.763536        1.606939
#> 6:       1.446323      0.9934029       1.885816       1.689953        1.524022
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.662301        1.983063       1.1564772        1.960724
#> 2:        1.666631        2.005908       1.1044303        1.944040
#> 3:        1.650040        1.983767       0.8642728        1.951696
#> 4:        1.648245        1.975788       1.1538318        1.953958
#> 5:        1.582963        1.973611       0.8960988        1.940615
#> 6:        1.696823        1.984449       0.9482990        1.929487
#>    Marker_15_asinh   Sample                 SuperCellId
#>              <num>   <char>                      <char>
#> 1:        2.020458 Sample_1 SuperCell_1_Sample_Sample_1
#> 2:        2.023472 Sample_1 SuperCell_2_Sample_Sample_1
#> 3:        2.001630 Sample_1 SuperCell_3_Sample_Sample_1
#> 4:        1.995150 Sample_1 SuperCell_4_Sample_Sample_1
#> 5:        1.989186 Sample_1 SuperCell_5_Sample_Sample_1
#> 6:        1.999426 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.

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

0.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_197_Sample_Sample_1 Cell_1 Sample_1
#> 2:  SuperCell_39_Sample_Sample_1 Cell_2 Sample_1
#> 3: SuperCell_200_Sample_Sample_1 Cell_3 Sample_1
#> 4: SuperCell_127_Sample_Sample_1 Cell_4 Sample_1
#> 5:  SuperCell_11_Sample_Sample_1 Cell_5 Sample_1
#> 6: SuperCell_315_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.

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

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

0.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.561998      1.0953313       1.547804       1.680873
#>    2:       1.710943      1.0678230       1.553871       1.637216
#>    3:       1.724353      1.0556197       1.674375       1.732224
#>    4:       1.720239      1.2801047       1.608758       1.625081
#>    5:       1.698301      1.3508374       1.587092       1.637674
#>   ---                                                            
#> 2996:       1.230298      0.8868147       2.016302       1.418323
#> 2997:       1.184015      1.0805878       2.066806       1.516708
#> 2998:       1.376181      1.2911570       2.013734       1.260505
#> 2999:       1.078902      1.0532022       2.046470       1.169057
#> 3000:       1.320632      0.8614809       2.028356       1.429722
#>       Marker_5_asinh Marker_6_asinh Marker_7_asinh Marker_8_asinh
#>                <num>          <num>          <num>          <num>
#>    1:       1.991138      1.4757717      1.0475832       1.887350
#>    2:       1.962307      1.3595602      0.9708524       1.897077
#>    3:       1.966588      1.4141743      1.0296026       1.888595
#>    4:       1.963315      1.3449590      0.8878490       1.879178
#>    5:       1.963324      1.5543235      0.7854983       1.881250
#>   ---                                                            
#> 2996:       1.841821      0.9330151      0.9656198       1.772328
#> 2997:       1.763729      0.9433282      0.8875151       1.774874
#> 2998:       1.779123      1.0178984      1.1688460       1.814000
#> 2999:       1.840179      1.1090355      1.1055849       1.789374
#> 3000:       1.842192      0.9865207      1.0713674       1.754332
#>       Marker_9_asinh Marker_10_asinh Marker_11_asinh Marker_12_asinh
#>                <num>           <num>           <num>           <num>
#>    1:       1.812341        1.512211       1.6692572        1.949692
#>    2:       1.729211        1.591383       1.6805557        1.937417
#>    3:       1.719623        1.547353       1.5941712        1.960677
#>    4:       1.812646        1.612233       1.5258587        1.981863
#>    5:       1.726053        1.452337       1.6856367        1.957581
#>   ---                                                               
#> 2996:       1.511737        1.824290       0.8813605        1.980513
#> 2997:       1.551244        1.662260       1.2325311        1.976929
#> 2998:       1.594729        1.643664       1.0275736        1.878247
#> 2999:       1.587138        1.802714       1.0046861        2.000651
#> 3000:       1.633334        1.653986       0.8742725        1.927700
#>       Marker_13_asinh Marker_14_asinh Marker_15_asinh   Sample
#>                 <num>           <num>           <num>   <char>
#>    1:       1.1555047        1.951233        1.993595 Sample_1
#>    2:       1.1980303        1.934569        1.987008 Sample_1
#>    3:       0.9603163        1.948845        1.980978 Sample_1
#>    4:       1.0390952        1.930894        1.955025 Sample_1
#>    5:       1.0257334        1.942347        1.988418 Sample_1
#>   ---                                                         
#> 2996:       0.8486283        1.461676        1.810676 Sample_3
#> 2997:       0.6938264        1.512002        1.738329 Sample_3
#> 2998:       0.8830210        1.354056        1.703949 Sample_3
#> 2999:       0.7561270        1.471111        1.709338 Sample_3
#> 3000:       0.8686330        1.388235        1.646737 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_114_Sample_Sample_1     Cell_1 Sample_1
#>     2: SuperCell_187_Sample_Sample_1     Cell_2 Sample_1
#>     3: SuperCell_424_Sample_Sample_1     Cell_3 Sample_1
#>     4: SuperCell_384_Sample_Sample_1     Cell_4 Sample_1
#>     5: SuperCell_638_Sample_Sample_1     Cell_5 Sample_1
#>    ---                                                  
#> 29996: SuperCell_163_Sample_Sample_3 Cell_29996 Sample_3
#> 29997: SuperCell_523_Sample_Sample_3 Cell_29997 Sample_3
#> 29998:  SuperCell_49_Sample_Sample_3 Cell_29998 Sample_3
#> 29999: SuperCell_290_Sample_Sample_3 Cell_29999 Sample_3
#> 30000: SuperCell_226_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

0.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: 16.68977  8.051145 15.365458 10.35878 19.109056 10.753879 10.97049  9.281116
#> 2: 17.69096  9.042282 17.399450 11.20612 18.944000  9.771598 10.45230 10.030333
#> 3: 17.99401 10.020072 16.980768 10.43398 17.764606 10.205347 10.49590  9.438433
#> 4: 15.75206 14.003658  8.134868 11.08480  5.568987  4.786242 16.81919  9.814332
#> 5: 13.70460 16.389803 10.331378 11.02189  6.979339  6.458847 18.42397 12.395933
#> 6: 15.03424 13.354194 11.820032  8.99161  7.317129  6.654031 18.15605 13.047260
#>     Marker_9 Marker_10   Sample                   SuperCellId
#>        <num>     <num>   <char>                        <char>
#> 1: 10.170819  14.17322 Sample_1   SuperCell_1_Sample_Sample_1
#> 2: 12.558570  12.91667 Sample_1   SuperCell_2_Sample_Sample_1
#> 3: 11.574356  12.25823 Sample_1   SuperCell_3_Sample_Sample_1
#> 4:  4.430050  15.67544 Sample_2 SuperCell_498_Sample_Sample_2
#> 5:  6.740685  15.01101 Sample_2 SuperCell_499_Sample_Sample_2
#> 6:  5.373481  15.00494 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_609_Sample_Sample_1     Cell_1 Sample_1
#> 2: SuperCell_933_Sample_Sample_1     Cell_2 Sample_1
#> 3: SuperCell_165_Sample_Sample_1     Cell_3 Sample_1
#> 4: SuperCell_304_Sample_Sample_2 Cell_19998 Sample_2
#> 5:  SuperCell_12_Sample_Sample_2 Cell_19999 Sample_2
#> 6:  SuperCell_90_Sample_Sample_2 Cell_20000 Sample_2

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

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

0.9 Session information

sessionInfo()
#> R version 4.5.1 Patched (2025-08-23 r88802)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.22-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              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: America/New_York
#> tzcode source: system (glibc)
#> 
#> 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.44       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