Skip to contents

Calculates dynamic features for lineages in a single-cell RNA-seq dataset.

Usage

RunDynamicFeatures(
  srt,
  lineages,
  features = NULL,
  suffix = lineages,
  n_candidates = 1000,
  minfreq = 5,
  family = NULL,
  layer = "counts",
  assay = NULL,
  libsize = NULL,
  BPPARAM = BiocParallel::bpparam(),
  seed = 11
)

Arguments

srt

A Seurat object.

lineages

A character vector specifying the lineage names for which dynamic features should be calculated.

features

A character vector specifying the features (genes or metadata variables) for which dynamic features should be calculated. If NULL, n_candidates must be provided.

suffix

A character vector specifying the suffix to append to the output layer names for each lineage. Defaults to the lineage names.

n_candidates

An integer specifying the number of candidate features to select when features is NULL. Defaults to 1000.

minfreq

An integer specifying the minimum frequency threshold for candidate features. Features with a frequency less than minfreq will be excluded. Defaults to 5.

family

A character or character vector specifying the family of distributions to use for the generalized additive models (GAMs). If family is set to NULL, the appropriate family will be automatically determined based on the data. If length(family) is 1, the same family will be used for all features. Otherwise, family must have the same length as features.

layer

A character vector specifying the layer in the Seurat object to use. Default is "counts".

assay

A character vector specifying the assay in the Seurat object to use. Default is NULL.

libsize

A numeric or numeric vector specifying the library size correction factors for each cell. If NULL, the library size correction factors will be calculated based on the expression matrix. If length(libsize) is 1, the same value will be used for all cells. Otherwise, libsize must have the same length as the number of cells in srt. Defaults to NULL.

BPPARAM

A BiocParallelParam object specifying the parallelization parameters for computing dynamic features. Defaults to BiocParallel::bpparam.

seed

An integer specifying the seed for random number generation. Defaults to 11.

Value

Returns the modified Seurat object with the calculated dynamic features stored in the tools slot.

Examples

data(pancreas_sub)
pancreas_sub <- RunSlingshot(
  pancreas_sub,
  group.by = "SubCellType",
  reduction = "UMAP"
)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_path()`).
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_path()`).
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_path()`).
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_path()`).

pancreas_sub <- RunDynamicFeatures(
  pancreas_sub,
  lineages = c("Lineage1", "Lineage2"),
  n_candidates = 200
)
#>  [2025-07-26 07:45:57] Start RunDynamicFeatures
#>  [2025-07-26 07:45:57] Workers: 2
#> Finding variable features for layer counts
#> Finding variable features for layer counts
#>  [2025-07-26 07:46:00] Number of candidate features(union): 230
#>  [2025-07-26 07:46:00] Calculate dynamic features for Lineage1...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%
#> 
#>  [2025-07-26 07:46:08] Calculate dynamic features for Lineage2...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%
#> 
#>  [2025-07-26 07:46:15] RunDynamicFeatures done
#>  [2025-07-26 07:46:15] Elapsed time:17.35 secs
names(
  pancreas_sub@tools$DynamicFeatures_Lineage1
)
#> [1] "DynamicFeatures" "raw_matrix"      "fitted_matrix"   "upr_matrix"     
#> [5] "lwr_matrix"      "libsize"         "lineages"        "family"         
head(
  pancreas_sub@tools$DynamicFeatures_Lineage1$DynamicFeatures
)
#>      features exp_ncells      r.sq  dev.expl peaktime valleytime pvalue padjust
#> Gcg       Gcg        201 0.6270855 0.7898872 26.64238  21.148093      0       0
#> Ghrl     Ghrl        160 0.2217888 0.5741775 24.53913   9.769869      0       0
#> Iapp     Iapp        289 0.2846216 0.7703114 26.64238   2.734889      0       0
#> Pyy       Pyy        451 0.4213178 0.7273376 25.21058  13.336162      0       0
#> Rbp4     Rbp4        443 0.5682852 0.7780600 25.10067  13.875598      0       0
#> Chgb     Chgb        302 0.4448449 0.7292177 20.73024   3.768112      0       0
ht <- DynamicHeatmap(
  srt = pancreas_sub,
  lineages = c("Lineage1", "Lineage2"),
  cell_annotation = "SubCellType",
  n_split = 6,
  reverse_ht = "Lineage1"
)
#>  [2025-07-26 07:46:15] 173 features from Lineage1,Lineage2 passed the threshold (exp_ncells>20 & r.sq>0.2 & dev.expl>0.2 & padjust<0.05): 
#>  [2025-07-26 07:46:15] Gcg,Ghrl,Iapp,Pyy,Rbp4,Chgb,Lrpprc,Slc38a5,Cdc20,Chga...
#> 'magick' package is suggested to install to give better rasterization.
#> 
#> Set `ht_opt$message = FALSE` to turn off this message.
#>  [2025-07-26 07:46:16] 
#>  [2025-07-26 07:46:16] The size of the heatmap is fixed because certain elements are not scalable.
#>  [2025-07-26 07:46:16] The width and height of the heatmap are determined by the size of the current viewport.
#>  [2025-07-26 07:46:16] If you want to have more control over the size, you can manually set the parameters 'width' and 'height'.

ht$plot


DynamicPlot(
  srt = pancreas_sub,
  lineages = c("Lineage1", "Lineage2"),
  features = c("Arxes1", "Ncoa2"),
  group.by = "SubCellType",
  compare_lineages = TRUE,
  compare_features = FALSE
)
#>  [2025-07-26 07:46:20] Start RunDynamicFeatures
#>  [2025-07-26 07:46:20] Workers: 2
#>  [2025-07-26 07:46:21] Number of candidate features(union): 2
#>  [2025-07-26 07:46:22] Calculate dynamic features for Lineage1...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%
#> 
#>  [2025-07-26 07:46:23] RunDynamicFeatures done
#>  [2025-07-26 07:46:23] Elapsed time:3.09 secs
#>  [2025-07-26 07:46:24] Start RunDynamicFeatures
#>  [2025-07-26 07:46:24] Workers: 2
#>  [2025-07-26 07:46:25] Number of candidate features(union): 2
#>  [2025-07-26 07:46:25] Calculate dynamic features for Lineage2...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%
#> 
#>  [2025-07-26 07:46:27] RunDynamicFeatures done
#>  [2025-07-26 07:46:27] Elapsed time:3.24 secs
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.