satijalab / sctransform

R package for modeling single cell UMI expression data using regularized negative binomial regression
GNU General Public License v3.0
212 stars 33 forks source link

SCTransform vars.to.regress Contrasts can be applied only to factors with 2 or more levels #193

Open UKECat opened 6 months ago

UKECat commented 6 months ago

Hi there, I encountered an error while attempting to run SCTransform() in the Seurat package. The error message suggests a problem with the second non-regularized linear regression step. The error only occurs, when I add the parameter "batch" to vars.to.regress. Here are the details: Running this code:

merged_seurat_b <- SCTransform(
     merged_seurat,
     vars.to.regress = c("percent.mt", "nFeature_RNA", "nCount_RNA", "batch"), 
     do.scale=FALSE, 
     do.center=TRUE,
     verbose=TRUE)

Produces this error:

Running SCTransform on layer: counts.C1
vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes.
Variance stabilizing transformation of count matrix of size 14474 by 4786
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 2000 genes, 4786 cells
Found 42 outliers - those will be ignored in fitting/regularization step

Second step: Get residuals using fitted parameters for 14474 genes
Computing corrected count matrix for 14474 genes
Calculating gene attributes
Wall clock passed: Time difference of 39.14677 secs
Determine variable features
Regressing out percent.mt, nFeature_RNA, nCount_RNA, batch
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  Contrasts can be applied only to factors with 2 or more levels

Every sample has been assigned to a batch using this code beforehand:

merged_seurat <- RenameIdents(merged_seurat,
                              'C1' = '01',
                              'C2' = '01',
                              'C3' = '01',
                              'C4' = '02',
                              'C5' = '02')
merged_seurat$batch <- Idents(merged_seurat)

I am using the Seurat Package Version 5.0.2

Does anybody have an idea how to solve this? I appreciate any help! Cheers!