satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.24k stars 902 forks source link

Error with vars.to.regress: "contrasts can be applied only to factors with 2 or more levels" #8780

Open yesitsjess opened 5 months ago

yesitsjess commented 5 months ago

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

EDIT: I also get this when I try to use many levels:

Regressing out batch
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels
> levels(soup_obj$batch)
 [1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16"
ashipde commented 4 months ago

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 
jvelghe commented 1 month ago

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<-
chriss1313 commented 1 month ago

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`