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: 18.66166 14.67557 8.706945  9.952455 7.445008 15.68797 11.42975 7.270189
#> 2: 18.09879 15.05303 8.203533 10.730894 8.616466 15.47895 11.68307 7.390515
#> 3: 19.81200 16.08995 6.965015  9.821704 7.485418 15.68850 11.12787 6.492989
#> 4: 17.66362 15.76011 7.566500 10.220306 8.217676 16.39013 10.11438 5.590943
#> 5: 18.68569 17.44711 8.945474  9.680363 7.805182 15.46082 10.90919 8.146412
#> 6: 19.40749 15.41470 9.194666  9.538715 8.165569 16.19986 10.71044 5.388505
#>    Marker_9 Marker_10 Marker_11 Marker_12 Marker_13 Marker_14 Marker_15
#>       <num>     <num>     <num>     <num>     <num>     <num>     <num>
#> 1: 15.07599  11.91141  9.392598  5.498087  16.40505  14.87021  12.94225
#> 2: 14.65558  10.12491  7.700919  5.254985  17.32689  15.00145  12.11334
#> 3: 15.29217  10.50212  8.401940  8.409872  17.68798  14.90479  13.23396
#> 4: 13.78151  11.46511  9.023574  9.754800  16.07110  14.79188  13.43020
#> 5: 13.82182  11.28610  9.846044  5.968751  16.20242  14.44244  12.77941
#> 6: 13.99472  11.68993  9.633610  6.499100  17.65832  13.72835  12.07258
#>      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:       2.027662       1.797726       1.321618       1.439375       1.188647
#> 2:       1.998110       1.821795       1.270362       1.507161       1.312571
#> 3:       2.085552       1.885216       1.133908       1.427574       1.193144
#> 4:       1.974672       1.865454       1.202118       1.463168       1.271834
#> 5:       2.028905       1.962810       1.345132       1.414675       1.228150
#> 6:       2.065564       1.844353       1.369194       1.401599       1.266404
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:       1.861082       1.564662      1.1689945       1.823241        1.602602
#> 2:       1.848311       1.584781      1.1825553       1.796436        1.454752
#> 3:       1.861115       1.540193      1.0775959       1.836765        1.487665
#> 4:       1.902882       1.453820      0.9625267       1.738429        1.567493
#> 5:       1.847195       1.522119      1.2644016       1.741175        1.553087
#> 6:       1.891719       1.505432      0.9352635       1.752874        1.585321
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.387949       0.9500895        1.903752        1.810206
#> 2:        1.216848       0.9169781        1.956165        1.818538
#> 3:        1.290834       1.2916449        1.975998        1.812408
#> 4:        1.352728       1.4214865        1.884096        1.805201
#> 5:        1.429780       1.0119660        1.891870        1.782581
#> 6:        1.410376       1.0783413        1.974383        1.734797
#>    Marker_15_asinh
#>              <num>
#> 1:        1.679589
#> 2:        1.618121
#> 3:        1.700410
#> 4:        1.714192
#> 5:        1.667787
#> 6:        1.615006

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:       2.030792       1.889194       1.296757       1.478647      0.9552725
#> 2:       2.015755       1.847724       1.300774       1.503042      1.0816487
#> 3:       2.051833       1.872143       1.347190       1.504466      1.1357659
#> 4:       2.053329       1.838608       1.368063       1.483708      1.2444587
#> 5:       2.045220       1.903176       1.285275       1.394959      1.2401866
#> 6:       2.018416       1.865810       1.272394       1.543937      1.0874435
#>    Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#>             <num>          <num>          <num>          <num>           <num>
#> 1:       1.916484       1.560950       1.111449       1.770894        1.467389
#> 2:       1.894508       1.500223       1.199956       1.726182        1.422733
#> 3:       1.940638       1.428977       1.115918       1.776039        1.645551
#> 4:       1.895114       1.510931       1.157724       1.742000        1.522115
#> 5:       1.942225       1.446211       1.032188       1.806583        1.594681
#> 6:       1.902191       1.489521       1.033341       1.751213        1.507967
#>    Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#>              <num>           <num>           <num>           <num>
#> 1:        1.310927        1.070728        1.916141        1.835138
#> 2:        1.180934        1.270048        1.879638        1.849707
#> 3:        1.387912        1.226354        1.905113        1.853484
#> 4:        1.191208        0.877812        1.858954        1.843362
#> 5:        1.236837        1.177886        1.929564        1.859792
#> 6:        1.334257        1.155070        1.895779        1.831291
#>    Marker_15_asinh   Sample                 SuperCellId
#>              <num>   <char>                      <char>
#> 1:        1.574754 Sample_1 SuperCell_1_Sample_Sample_1
#> 2:        1.622231 Sample_1 SuperCell_2_Sample_Sample_1
#> 3:        1.593754 Sample_1 SuperCell_3_Sample_Sample_1
#> 4:        1.660196 Sample_1 SuperCell_4_Sample_Sample_1
#> 5:        1.579082 Sample_1 SuperCell_5_Sample_Sample_1
#> 6:        1.513464 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_257_Sample_Sample_1 Cell_1 Sample_1
#> 2: SuperCell_165_Sample_Sample_1 Cell_2 Sample_1
#> 3:  SuperCell_16_Sample_Sample_1 Cell_3 Sample_1
#> 4:  SuperCell_25_Sample_Sample_1 Cell_4 Sample_1
#> 5:  SuperCell_38_Sample_Sample_1 Cell_5 Sample_1
#> 6: SuperCell_109_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:      2.0465942      1.8810497       1.376774       1.386051
#>    2:      2.0525390      1.8889584       1.251093       1.422461
#>    3:      2.0038867      1.8490726       1.290219       1.425064
#>    4:      2.0190592      1.8439090       1.210658       1.376309
#>    5:      2.0310737      1.8423907       1.361158       1.455008
#>   ---                                                            
#> 2996:      0.8926846      1.2037395       1.998251       1.860713
#> 2997:      1.0533348      0.9439701       2.013559       1.969654
#> 2998:      0.9855625      1.2213906       2.000424       1.890212
#> 2999:      1.2512924      1.3325312       1.954834       1.898184
#> 3000:      1.3179425      1.0431481       2.015063       1.911719
#>       Marker_5_asinh Marker_6_asinh Marker_7_asinh Marker_8_asinh
#>                <num>          <num>          <num>          <num>
#>    1:       1.182781       1.941786      1.6033076      1.0637632
#>    2:       1.170061       1.941542      1.3656241      1.2307151
#>    3:       1.032296       1.897189      1.4538516      0.9350636
#>    4:       1.289330       1.889635      1.4470129      1.1540703
#>    5:       1.193687       1.922298      1.4238595      1.2650588
#>   ---                                                            
#> 2996:       1.327220       1.262128      0.7983871      1.9648453
#> 2997:       1.378195       1.071212      0.8174117      2.0296621
#> 2998:       1.404590       1.290814      0.7688277      2.0168034
#> 2999:       1.351328       1.332034      0.6207944      2.0356430
#> 3000:       1.184735       1.245241      0.8076515      2.0890221
#>       Marker_9_asinh Marker_10_asinh Marker_11_asinh Marker_12_asinh
#>                <num>           <num>           <num>           <num>
#>    1:       1.779458        1.604420        1.208749        1.296052
#>    2:       1.801232        1.464639        1.292836        1.260000
#>    3:       1.768029        1.553006        1.130103        1.145217
#>    4:       1.721072        1.515504        1.249987        1.021760
#>    5:       1.749624        1.529217        1.360838        1.047187
#>   ---                                                               
#> 2996:       1.200105        1.500846        1.547227        1.942979
#> 2997:       1.374284        1.240771        1.473224        1.928638
#> 2998:       1.267158        1.334663        1.593272        1.870292
#> 2999:       1.107236        1.319514        1.583990        1.950013
#> 3000:       1.365948        1.396366        1.641237        1.957735
#>       Marker_13_asinh Marker_14_asinh Marker_15_asinh   Sample
#>                 <num>           <num>           <num>   <char>
#>    1:        1.935919        1.852712        1.661290 Sample_1
#>    2:        1.932959        1.847837        1.616616 Sample_1
#>    3:        1.888603        1.844711        1.646503 Sample_1
#>    4:        1.908710        1.826460        1.592783 Sample_1
#>    5:        1.919423        1.816971        1.690130 Sample_1
#>   ---                                                         
#> 2996:        1.891530        1.807007        1.970442 Sample_3
#> 2997:        1.915195        1.778940        2.047158 Sample_3
#> 2998:        1.832912        1.785736        1.959016 Sample_3
#> 2999:        1.859389        1.667351        2.032216 Sample_3
#> 3000:        1.858762        1.828926        2.013671 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_494_Sample_Sample_1     Cell_1 Sample_1
#>     2: SuperCell_358_Sample_Sample_1     Cell_2 Sample_1
#>     3:  SuperCell_17_Sample_Sample_1     Cell_3 Sample_1
#>     4:  SuperCell_65_Sample_Sample_1     Cell_4 Sample_1
#>     5: SuperCell_884_Sample_Sample_1     Cell_5 Sample_1
#>    ---                                                  
#> 29996: SuperCell_208_Sample_Sample_3 Cell_29996 Sample_3
#> 29997:  SuperCell_34_Sample_Sample_3 Cell_29997 Sample_3
#> 29998: SuperCell_404_Sample_Sample_3 Cell_29998 Sample_3
#> 29999: SuperCell_153_Sample_Sample_3 Cell_29999 Sample_3
#> 30000: SuperCell_406_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: 18.99549 17.03811 12.856692  7.885850 11.339723 14.33410 10.91049 18.37924
#> 2: 18.77783 16.66061 12.853287  8.917754 13.389811 14.59164 11.63613 17.46422
#> 3: 17.37057 17.10508 13.986527  7.025879 12.451159 13.08287 11.91778 20.39509
#> 4: 13.15363 14.51968  8.355371 14.481904  5.836080 11.11945 15.43129 18.04163
#> 5: 10.40715 13.64847  7.195894 11.381287  4.968926 14.03245 14.70342 18.96517
#> 6: 11.71156 13.44127  8.673149 12.759420  7.052345 13.65220 14.86596 16.30150
#>     Marker_9 Marker_10   Sample                   SuperCellId
#>        <num>     <num>   <char>                        <char>
#> 1: 10.209722 13.604336 Sample_1   SuperCell_1_Sample_Sample_1
#> 2:  9.438033 13.629839 Sample_1   SuperCell_2_Sample_Sample_1
#> 3:  9.601730 13.771621 Sample_1   SuperCell_3_Sample_Sample_1
#> 4:  6.144485  8.624689 Sample_2 SuperCell_498_Sample_Sample_2
#> 5:  8.525607  9.785547 Sample_2 SuperCell_499_Sample_Sample_2
#> 6:  7.214033  8.422413 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_42_Sample_Sample_1     Cell_1 Sample_1
#> 2: SuperCell_494_Sample_Sample_1     Cell_2 Sample_1
#> 3: SuperCell_554_Sample_Sample_1     Cell_3 Sample_1
#> 4:  SuperCell_20_Sample_Sample_2 Cell_19998 Sample_2
#> 5:  SuperCell_20_Sample_Sample_2 Cell_19999 Sample_2
#> 6: SuperCell_115_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-09-10 r88807)
#> Platform: x86_64-apple-darwin20
#> Running under: macOS Monterey 12.7.6
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-x86_64/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.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