Skip to content

dropFeatures

dropFeatures

dropFeatures(
    data,
    dropMarkers=None,
    dropCells=None,
    dropMetaColumns=None,
    dropGroups=None,
    groupsColumn=None,
    subsetRaw=True,
    verbose=True,
    outputDir=None,
    sdataTable=None,
)

Drop markers, cells, metadata columns, or phenotype groups.

Everything you pass is dropped in one pass, in this order: groups, then cells, then markers, then obs columns. Dropping markers also prunes .raw when subsetRaw=True, which keeps .X and .raw.X describing the same variables — forget that and later calls with layer='raw' will disagree with .X.

SpatialData tables need their annotation columns

For SpatialData input, keep the table's region_key and instance_key obs columns. Dropping them makes the table fail TableModel validation on write-back.

Parameters:

Name Type Description Default
data (AnnData | SpatialData | str, required)

The cell table. An AnnData, a path to an .h5ad file, a SpatialData object, or a path to a .zarr SpatialData store.

required
dropMarkers str | list

Markers to remove from var.

None
dropCells str | list

Cell names (obs_names) to remove.

None
dropMetaColumns str | list

Columns to remove from obs. Columns that are not present are ignored.

None
dropGroups str | list

Values of groupsColumn whose cells should be removed — e.g. dropping every 'Unknown' cell.

None
groupsColumn str

Column in obs that dropGroups refers to. Required when dropGroups is given.

None
subsetRaw bool

Apply dropMarkers to .raw as well as to .X.

True
verbose bool

Print the resulting shape.

True
outputDir str

Directory to write the updated object to. With None the object is returned instead.

None
sdataTable str

Which SpatialData table to work on.

None

Returns:

Name Type Description
adata AnnData | SpatialData | None

The reduced object. None when outputDir is given.

Raises:

Type Description
ValueError

When dropGroups is given without groupsColumn.

Example
# Drop a bad channel and every unphenotyped cell.
adata = sp.pp.dropFeatures(
    adata,
    dropMarkers=["DNA_6"],
    dropGroups=["Unknown"],
    groupsColumn="phenotype",
)

# Tidy up working columns before saving.
adata = sp.pp.dropFeatures(adata, dropMetaColumns=["kmeans", "leiden"])