seurat
seurat copied to clipboard
Error with vars.to.regress: "contrasts can be applied only to factors with 2 or more levels"
When running:
soup_obj <- SCTransform(soup_obj, vars.to.regress="Sex")
I get the error:
Regressing out Sex
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
But my variable "Sex" is a factor with two levels:
> table(soup_obj$Sex)
Male Female
205006 49637
> levels(soup_obj$Sex)
[1] "Male" "Female"
Seurat_5.0.3
I am facing the exact issue (Seurat 5.0.3)
> ce
An object of class Seurat
13336 features across 50032 samples within 1 assay
Active assay: RNA (13336 features, 0 variable features)
12 layers present: counts.nf1, counts.om1, counts.of2, ...
> ce <- SCTransform(cv, vars.to.regress='Batch', verbose=FALSE)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
> str(ce$Batch)
Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "names")= chr [1:50032] "om1_TACTGCCCAACCACGC-1 ...
> table(ce$Batch)
A B
40787 9245
I am also facing this issue with Seurat 5.1.0
Regressing out Donation_Type, Enrichment
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Calls: SCTransform ... model.matrix -> model.matrix.default -> contrasts<-
I am also facing this issue with Seurat 5.0.2. I split the object before running SCTransform.
` seurat_obj <- SCTransform(seurat_obj, vars.to.regress = c("nCount_RNA","batch"))
Running SCTransform on assay: RNA Running SCTransform on layer: counts.sample1 vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes. Variance stabilizing transformation of count matrix of size 13488 by 1557 Model formula is y ~ log_umi Get Negative Binomial regression parameters per gene Using 2000 genes, 1557 cells Found 7 outliers - those will be ignored in fitting/regularization step
Second step: Get residuals using fitted parameters for 13488 genes
Computing corrected count matrix for 13488 genes
Calculating gene attributes
Wall clock passed: Time difference of 6.538674 secs
Determine variable features
Regressing out 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`