Skip to main content

A Python package for batch correction (CALM) and activity scoring (CARZ, TAS) of gene expression data.

Project description

calmcarz

A Python package for batch correction (CALM) and activity scoring (CARZ, TAS) of perturbed expression data

calmcarz is a lightweight and easy-to-use Python package for normalizing high-throughput gene expression data from perturbation screens. It provides a simple, function-based workflow to remove technical batch effects and calculate robust, interpretable scores for transcriptional activity.

The pipeline consists of three main steps:

  • CALM (Control-Anchored Linear Model): Removes batch effects by fitting a linear model on control samples.
  • CARZ (Control-Anchored Robust Z-score): Standardizes gene expression relative to the control distribution using robust statistics.
  • TAS (Transcriptional Activity Score): Summarizes the overall degree of perturbation for each cell based on the number of significantly altered genes.

Installation

You can install calmcarz directly from PyPI using pip:

pip install calmcarz

Ensure you have the necessary dependencies (or it will be installed automatically: numpy, pandas, anndata, statsmodels, scipy, and tqdm)

Usage

The library is designed to work seamlessly with AnnData objects.

import anndata as ad
from calmcarz import calm_correction, calculate_carz, calculate_tas

# 1. Load your data into an AnnData object
# This assumes your data is in a log-transformed layer (e.g., 'log1p_counts')
# and adata.obs contains 'batch' and 'treatment' columns.
adata = ad.read_h5ad("path/to/your/data.h5ad")

# 2. Apply CALM for batch correction
# The corrected data will be stored in a new layer, 'calm_corrected'.
calm_correction(
    adata,
    plate_column='rna_plate',
    treatment_column='cmap_name',
    control_treatment_value='DMSO',
    corrected_layer_name='X_CALM' # the CALM-corrected expression will be store in layer 'X_CALM'
)

# 3. Calculate CARZ scores
# CARZ scores will be stored in a new layer, 'carz_scores'.
calculate_carz(
    adata,
    plate_key="rna_plate",
    layer="X_CALM", # calculate using CALM-corrected expression
    pert_type_key="pert_type",
    control_categories=["ctl_vehicle"], # calculate using plate controls
    key_added="X_CARZ", # the CARZ matrix will be stored in layer 'X_CARZ'
    copy=False,
)

# 4. Calculate Transcriptional Activity Score (TAS)
# The final TAS will be added to adata.obs.
calculate_tas(
    adata,
    layer='X_CARZ',
    tas_obs_key='TAS_score',
    alpha=2.5 # Set the significance threshold for CARZ scores
)

# View the results
print(adata.obs[['cmap_name', 'TAS_score']].head())

API Overview

calm_correction(adata, ...)

Corrects for batch effects using a control-anchored linear model.

  • adata: AnnData object. Assumes expression data in adata.X (cells x genes). adata.obs must contain the plate_column and treatment_column.
  • plate_column: Name of the column in adata.obs that identifies the plate.
  • treatment_column: Name of the column in adata.obs that identifies the treatment.
  • control_treatment_value: Value in treatment_column that identifies control samples.
  • corrected_layer_name: Name of the layer in adata to store the corrected expression data. Original adata.X is not modified.
  • min_control_samples_per_gene_for_fitting: Minimum number of total control samples (across all plates) required for a gene to attempt model fitting.
  • min_control_variance_for_fitting: Minimum variance of expression in control samples for a gene to attempt model fitting.
  • verbose: If True, prints progress information.

calculate_carz(adata, ...)

Calculates robust Z-scores relative to the control distribution.

  • adata: Annotated data matrix (cells x genes).
  • plate_key: Key in adata.obs for plate identifiers.
  • pert_type_key: Key in adata.obs indicating perturbation type.
  • control_categories: List of strings in adata.obs[pert_type_key] that identify control samples.
  • layer: Layer in adata.layers to use as input. If None, adata.X.
  • key_added: Name of the layer to store results. If None, overwrites input.
  • copy: Whether to modify adata inplace (False) or return a copy (True).

calculate_tas(adata, ...)

Calculates transcriptional activity score (TAS).

  • adata: AnnData object with Z-scored expression data in adata.X. adata.X should have cells as rows and genes as columns.
  • layer: Layer in adata.layers to use as input. If None, adata.X.
  • alpha: The threshold for the absolute Z-score.
  • tas_obs_key: The key under which the TAS will be stored in adata.obs. Defaults to "TAS_score".
  • gene_subset: Optional list of gene names (strings). If provided, TAS will be calculated only for these genes. If None, all genes in adata.X will be used. Defaults to None.

Contributing

Contributions are welcome! If you have suggestions for improvements or find a bug, please feel free to open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

calmcarz-0.1.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

calmcarz-0.1.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file calmcarz-0.1.1.tar.gz.

File metadata

  • Download URL: calmcarz-0.1.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for calmcarz-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2370f43bd53a3a52752dae993e338b2142a35ff361e3836ae377ce606aa5e778
MD5 b046da83b38e7c9988f4d0ec9f866dd7
BLAKE2b-256 7b06243af58c3a8378292750675d9826d051799d5c330e479b97b5a2bf12f164

See more details on using hashes here.

File details

Details for the file calmcarz-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: calmcarz-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for calmcarz-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 879561b93f148715b580eaf6973e5a9caa0e3239f8185e5fda0a202064fd722d
MD5 1f700e070fbcec4f25a9673615b6dcbd
BLAKE2b-256 903956cb9cbfe1d5668dcf386b5eecd8f478616e958b717e594abbf78dea38e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page