combat
combat ¶
combat(
data,
batch="imageid",
layer="raw",
log=True,
replaceOriginal=False,
label="combat",
chunkSize=10000,
maxWorkers=None,
verbose=True,
outputDir=None,
streamData=False,
sdataTable=None,
)
ComBat batch correction across images or any other batch variable.
Wraps pycombat to remove additive and multiplicative batch effects while
preserving biological variation. The typical use is correcting slide-to-slide
staining differences before pooling samples.
By default the corrected matrix is written to layers[label] and the original
is left alone; replaceOriginal=True writes it into .X instead. The matrix is
assembled with Polars, and the optional log1p step runs in parallel chunks.
At least two batches are required
A single-image object raises an Exception — there is nothing to correct
between.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(AnnData | SpatialData | str, required)
|
The cell table. An |
required |
batch
|
str
|
Column in |
'imageid'
|
layer
|
str
|
Which matrix to correct. |
'raw'
|
log
|
bool
|
Apply |
True
|
replaceOriginal
|
bool
|
Write the corrected matrix into |
False
|
label
|
str
|
Layer the corrected matrix is written to when |
'combat'
|
chunkSize
|
int
|
Rows per chunk for the parallel |
10000
|
maxWorkers
|
int
|
Threads for the |
None
|
verbose
|
bool
|
Print progress messages. |
True
|
outputDir
|
str
|
Directory to write the updated object to. With |
None
|
streamData
|
bool
|
Run out of core against an |
False
|
sdataTable
|
str
|
Which |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
adata |
AnnData | SpatialData | None
|
The updated object with the corrected matrix in |
Raises:
| Type | Description |
|---|---|
Exception
|
When |
Example
adata = sp.pp.combat(adata, batch="imageid")
# Compare before and after.
sp.pl.heatmap(adata, groupBy="imageid", layer=None)
sp.pl.heatmap(adata, groupBy="imageid", layer="combat")
# Correct in place, using a slide column rather than the image id.
adata = sp.pp.combat(adata, batch="slide", replaceOriginal=True)