How to create supercells
Givanna Putri
Source:vignettes/how_to_create_supercells.Rmd
how_to_create_supercells.Rmd
Introduction
This vignette describes the steps to generate supercells for cytometry data using SuperCellCyto R package.
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:
- 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. - 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.
- 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: 5.354062 18.00190 15.34377 7.850049 5.051249 12.76034 13.45909 9.120952
#> 2: 4.686508 17.25606 13.81100 7.734731 4.959933 12.52533 13.06860 10.242089
#> 3: 8.180677 17.65222 12.35144 8.879722 5.397469 10.84124 10.66265 11.661453
#> 4: 6.674427 17.65331 13.36466 7.837501 6.737175 13.58634 11.60650 8.275668
#> 5: 5.355100 17.10721 13.62514 8.168731 4.915897 11.64593 12.26424 9.469937
#> 6: 6.859295 17.74649 13.33065 7.507828 5.841775 11.42130 12.11751 9.552281
#> Marker_9 Marker_10 Marker_11 Marker_12 Marker_13 Marker_14 Marker_15
#> <num> <num> <num> <num> <num> <num> <num>
#> 1: 15.28438 17.76261 18.55511 6.792268 4.139855 9.517666 11.550815
#> 2: 16.19940 14.33739 17.25021 5.879038 5.566083 10.853051 9.799421
#> 3: 14.97142 16.23926 18.64175 7.594556 6.941313 11.113545 11.836116
#> 4: 13.83856 14.75398 18.88236 6.135554 6.368055 9.458724 12.558570
#> 5: 15.99549 15.71650 19.60589 6.845101 6.096939 9.749784 9.679683
#> 6: 15.44915 16.85604 18.80598 8.261104 5.114830 9.633085 10.437587
#> 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: 0.9305698 1.992938 1.839967 1.232981 0.8886028
#> 2: 0.8363371 1.952231 1.740438 1.220525 0.8756959
#> 3: 1.2679809 1.974051 1.636139 1.338698 0.9364823
#> 4: 1.0995432 1.974110 1.709609 1.231632 1.1070448
#> 5: 0.9307114 1.943912 1.727709 1.266734 0.8694294
#> 6: 1.1215159 1.979176 1.707223 1.195631 0.9955558
#> Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#> <num> <num> <num> <num> <num>
#> 1: 1.666397 1.716206 1.362129 1.836280 1.980050
#> 2: 1.649111 1.688657 1.465081 1.891692 1.775685
#> 3: 1.516443 1.501381 1.583078 1.816637 1.894041
#> 4: 1.725033 1.578739 1.277847 1.742313 1.802771
#> 5: 1.581854 1.629576 1.395195 1.879595 1.862814
#> 6: 1.563985 1.618440 1.402858 1.846477 1.929723
#> Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#> <num> <num> <num> <num>
#> 1: 2.022133 1.113594 0.7543612 1.399643
#> 2: 1.951905 1.000393 0.9592082 1.517431
#> 3: 2.026631 1.205208 1.1311408 1.539018
#> 4: 2.039023 1.033214 1.0622582 1.394147
#> 5: 2.075416 1.119843 1.0283259 1.421029
#> 6: 2.035105 1.276340 0.8975202 1.410328
#> Marker_15_asinh
#> <num>
#> 1: 1.574323
#> 2: 1.425550
#> 3: 1.596758
#> 4: 1.651573
#> 5: 1.414613
#> 6: 1.482103
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.0980359 1.976635 1.744046 1.284006 0.8406754
#> 2: 1.2708808 1.959270 1.768194 1.213510 0.8435833
#> 3: 0.9358373 1.967619 1.753471 1.061320 0.9084702
#> 4: 0.9918583 1.970370 1.759316 1.039592 0.9232826
#> 5: 1.0859961 1.959836 1.772245 1.180816 0.8296192
#> 6: 0.9839823 1.987265 1.776855 1.142725 0.9624379
#> Marker_6_asinh Marker_7_asinh Marker_8_asinh Marker_9_asinh Marker_10_asinh
#> <num> <num> <num> <num> <num>
#> 1: 1.592370 1.633241 1.465175 1.895975 1.913915
#> 2: 1.661065 1.656824 1.310378 1.880409 1.944117
#> 3: 1.669144 1.578482 1.503876 1.887718 1.937788
#> 4: 1.610901 1.649418 1.356484 1.863929 1.918834
#> 5: 1.656676 1.664531 1.367829 1.868905 1.916273
#> 6: 1.551666 1.745967 1.412334 1.879636 1.940357
#> Marker_11_asinh Marker_12_asinh Marker_13_asinh Marker_14_asinh
#> <num> <num> <num> <num>
#> 1: 1.994611 1.202137 0.7467371 1.355013
#> 2: 2.022654 1.213900 0.8402130 1.379115
#> 3: 2.010279 1.276539 0.8606365 1.423122
#> 4: 2.008280 1.023822 0.9759029 1.365365
#> 5: 1.993215 1.193693 0.6546532 1.412407
#> 6: 2.012757 1.160411 0.7766171 1.430625
#> Marker_15_asinh Sample SuperCellId
#> <num> <char> <char>
#> 1: 1.622478 Sample_1 SuperCell_1_Sample_Sample_1
#> 2: 1.510947 Sample_1 SuperCell_2_Sample_Sample_1
#> 3: 1.571943 Sample_1 SuperCell_3_Sample_Sample_1
#> 4: 1.547063 Sample_1 SuperCell_4_Sample_Sample_1
#> 5: 1.578565 Sample_1 SuperCell_5_Sample_Sample_1
#> 6: 1.460961 Sample_1 SuperCell_6_Sample_Sample_1
Therein, we will have the following columns:
- All the markers we previously specified in the
markers_col
variable. In this example, they are the arcsinh transformed markers in our toy data. - A column (
Sample
in this case) denoting which sample a supercell belongs to, (note the column name is the same as what is stored insample_col
variable). - 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_13_Sample_Sample_1 Cell_1 Sample_1
#> 2: SuperCell_45_Sample_Sample_1 Cell_2 Sample_1
#> 3: SuperCell_32_Sample_Sample_1 Cell_3 Sample_1
#> 4: SuperCell_66_Sample_Sample_1 Cell_4 Sample_1
#> 5: SuperCell_64_Sample_Sample_1 Cell_5 Sample_1
#> 6: SuperCell_54_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:
- Create a
BiocParallelParam
object from the BiocParallel package. This object can either be of typeMulticoreParam
orSnowParam
. We highly recommend consulting their vignette for more information. - Set the number of tasks for the
BiocParallelParam
object to the number of samples we have in the dataset. - Set the
load_balancing
parameter forrunSuperCellCyto
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.9696783 1.972155 1.721013 1.276304
#> 2: 1.0124308 1.987264 1.778276 1.221415
#> 3: 1.1291710 1.992491 1.766599 1.217734
#> 4: 0.9216853 1.982625 1.739405 1.282888
#> 5: 0.9276912 1.977609 1.777766 1.342842
#> ---
#> 2996: 1.1350469 1.869372 0.814109 1.957647
#> 2997: 1.2317083 1.822277 1.109015 1.847673
#> 2998: 1.2935757 1.846235 1.195668 1.897921
#> 2999: 1.2896211 1.863785 1.019648 1.892519
#> 3000: 1.1427232 1.895304 1.016605 1.962564
#> Marker_5_asinh Marker_6_asinh Marker_7_asinh Marker_8_asinh
#> <num> <num> <num> <num>
#> 1: 0.9654589 1.541381 1.618511 1.219360
#> 2: 0.8991693 1.637705 1.684431 1.339687
#> 3: 1.0711284 1.728192 1.590427 1.370154
#> 4: 1.0809710 1.622236 1.600255 1.398368
#> 5: 0.6954213 1.594101 1.664940 1.411748
#> ---
#> 2996: 1.9499659 1.344071 2.044141 1.939838
#> 2997: 1.9554664 1.619313 1.984121 1.954528
#> 2998: 2.0205453 1.568201 2.059242 2.041662
#> 2999: 1.9874414 1.553717 2.027931 1.900983
#> 3000: 2.0199546 1.401617 1.999470 1.970249
#> Marker_9_asinh Marker_10_asinh Marker_11_asinh Marker_12_asinh
#> <num> <num> <num> <num>
#> 1: 1.848784 1.892726 1.9783000 1.3134378
#> 2: 1.880648 1.926755 2.0112972 1.1213923
#> 3: 1.874392 1.925758 2.0316442 1.1066837
#> 4: 1.900757 1.906717 2.0092665 1.2250326
#> 5: 1.895166 1.920280 2.0176838 1.2407724
#> ---
#> 2996: 1.620515 1.492665 1.1477082 0.9446480
#> 2997: 1.664717 1.532521 1.2167830 0.9298399
#> 2998: 1.720427 1.391957 1.0542911 0.8148562
#> 2999: 1.718376 1.322157 0.7917405 0.8976586
#> 3000: 1.556032 1.621228 1.1417019 1.0045321
#> Marker_13_asinh Marker_14_asinh Marker_15_asinh Sample
#> <num> <num> <num> <char>
#> 1: 0.9497961 1.426577 1.6016374 Sample_1
#> 2: 1.0677681 1.530339 1.4807448 Sample_1
#> 3: 0.8760023 1.455008 1.5400428 Sample_1
#> 4: 0.8745441 1.365429 1.5336967 Sample_1
#> 5: 1.0326050 1.463607 1.5553632 Sample_1
#> ---
#> 2996: 2.0707835 1.533181 1.0094283 Sample_3
#> 2997: 2.0329529 1.419476 0.6864491 Sample_3
#> 2998: 2.0709370 1.381706 1.0013768 Sample_3
#> 2999: 2.0692234 1.286251 1.0528215 Sample_3
#> 3000: 2.1206257 1.342568 1.1691764 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_39_Sample_Sample_1 Cell_1 Sample_1
#> 2: SuperCell_696_Sample_Sample_1 Cell_2 Sample_1
#> 3: SuperCell_46_Sample_Sample_1 Cell_3 Sample_1
#> 4: SuperCell_951_Sample_Sample_1 Cell_4 Sample_1
#> 5: SuperCell_103_Sample_Sample_1 Cell_5 Sample_1
#> ---
#> 29996: SuperCell_483_Sample_Sample_3 Cell_29996 Sample_3
#> 29997: SuperCell_24_Sample_Sample_3 Cell_29997 Sample_3
#> 29998: SuperCell_661_Sample_Sample_3 Cell_29998 Sample_3
#> 29999: SuperCell_41_Sample_Sample_3 Cell_29999 Sample_3
#> 30000: SuperCell_789_Sample_Sample_3 Cell_30000 Sample_3
The output generated by recomputeSupercells
is
essentially a list:
-
supercell_expression_matrix
: A data.table object that contains the marker expression for each supercell. -
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: 6.511185 8.683492 6.773297 12.854907 10.49031 15.711709 7.177395 17.55178
#> 2: 7.135506 9.407189 7.076024 12.788735 12.37267 14.117383 7.072052 17.42942
#> 3: 6.323042 7.843839 6.425065 13.886867 10.88569 15.240742 6.568253 17.73703
#> 4: 18.799466 16.238249 6.407039 11.033295 15.60010 4.155191 16.978654 14.63903
#> 5: 16.284303 14.418969 7.385808 9.703229 12.78168 3.250727 16.656518 13.68402
#> 6: 16.969357 13.645203 7.800918 11.033554 11.44928 3.254663 17.845143 15.15761
#> Marker_9 Marker_10 Sample SuperCellId
#> <num> <num> <char> <char>
#> 1: 17.122190 12.693334 Sample_1 SuperCell_1_Sample_Sample_1
#> 2: 16.670828 11.585651 Sample_1 SuperCell_2_Sample_Sample_1
#> 3: 17.822578 14.319694 Sample_1 SuperCell_3_Sample_Sample_1
#> 4: 7.558093 6.610632 Sample_2 SuperCell_498_Sample_Sample_2
#> 5: 7.616852 7.626072 Sample_2 SuperCell_499_Sample_Sample_2
#> 6: 7.770940 6.301986 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_139_Sample_Sample_1 Cell_1 Sample_1
#> 2: SuperCell_299_Sample_Sample_1 Cell_2 Sample_1
#> 3: SuperCell_462_Sample_Sample_1 Cell_3 Sample_1
#> 4: SuperCell_58_Sample_Sample_2 Cell_19998 Sample_2
#> 5: SuperCell_287_Sample_Sample_2 Cell_19999 Sample_2
#> 6: SuperCell_230_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:
- Load up a subset of the samples (say 1-10).
- Generate supercells for those samples.
- Save the output using the qs package.
- Extract the
supercell_expression_matrix
andsupercell_cell_map
, and export them out as a csv file usingdata.table
’sfwrite
function. - 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.1 (2025-06-13)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.2 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=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] parallel stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] BiocParallel_1.42.1 SuperCellCyto_0.99.0 BiocStyle_2.36.0
#>
#> loaded via a namespace (and not attached):
#> [1] cli_3.6.5 knitr_1.50 rlang_1.1.6
#> [4] xfun_0.52 textshaping_1.0.1 data.table_1.17.8
#> [7] jsonlite_2.0.0 plyr_1.8.9 htmltools_0.5.8.1
#> [10] ragg_1.4.0 sass_0.4.10 rmarkdown_2.29
#> [13] grid_4.5.1 evaluate_1.0.4 jquerylib_0.1.4
#> [16] fastmap_1.2.0 yaml_2.3.10 lifecycle_1.0.4
#> [19] bookdown_0.43 BiocManager_1.30.26 compiler_4.5.1
#> [22] igraph_2.1.4 codetools_0.2-20 fs_1.6.6
#> [25] Rcpp_1.1.0 pkgconfig_2.0.3 htmlwidgets_1.6.4
#> [28] lattice_0.22-7 systemfonts_1.2.3 digest_0.6.37
#> [31] SuperCell_1.0.1 R6_2.6.1 RANN_2.6.2
#> [34] magrittr_2.0.3 Matrix_1.7-3 bslib_0.9.0
#> [37] tools_4.5.1 pkgdown_2.1.3 cachem_1.1.0
#> [40] desc_1.4.3