Python client¶
The malva_client package is the
recommended way to work with Malva from Python. It wraps the Malva
API and returns pandas-friendly objects, so you can go from a search to a
dataframe in a few lines.
The package uses the same server and search backend as the web interface. It uses the same authentication and counts against your daily search quota.
Install¶
Requires Python 3.9 or later.
Configure¶
Create an API token on your profile page at
https://malva.mdc-berlin.de/profile, then store it with the command-line
tool:
Alternatively, set the token in your environment:
Connect¶
Run a search¶
Gene search by symbol:
The default result is aggregated by sample and cell type. Common columns are
sample_id, cell_type, gene_sequence, rel, exp, pct, raw_kmers
and cell_count. The rel, exp, pct and raw_kmers columns match the
display modes of the Expression Explorer.
Sequence search:
sequence = "ATCGATCGATCGATCGATCGATCG"
sequence_result = client.search_sequences(sequence)
print(sequence_result.df.head())
What else the client does¶
The client exposes a search job workflow, coverage searches, sample and study
browsing, downloads, and population queries. Methods include
submit_search, get_job_status, wait_for_job, get_coverage,
get_sequence_coverage, get_studies, get_samples, download_sample and
get_cells_by_metadata.
Populations, metacells and differential expression¶
The Populations workspace and the Python client share the same coexpression service. You can reproduce an analysis from cell IDs without downloading sample metadata:
import os
from malva_client import MalvaClient
client = MalvaClient(api_token=os.environ["MALVA_API_TOKEN"])
# 1. Pick cells by metadata, or use cell IDs you already have.
b_cells = client.get_cells_by_metadata(
cell_types=["B cell", "plasma cell"],
include_sample_metadata=False,
)
t_cells = client.get_cells_by_metadata(
cell_types=["T cell", "CD8-positive, alpha-beta T cell"],
include_sample_metadata=False,
)
# 2. Project cells to metacells on the atlas.
b_meta = client.project_cells_to_metacells(
b_cells.cell_ids,
b_cells.sample_ids,
dataset_id="Human_Cell_Atlas",
)
t_meta = client.project_cells_to_metacells(
t_cells.cell_ids,
t_cells.sample_ids,
dataset_id="Human_Cell_Atlas",
)
# 3. Differential expression between the projected populations.
de = client.differential_expression(
t_meta.metacell_ids,
b_meta.metacell_ids,
dataset_id="Human_Cell_Atlas",
top_n_genes=0, # all tested genes
min_log2fc=0.0,
)
print(de.to_dataframe().head())
print(de.significant(alpha=0.05).head())
differential_expression_from_cells combines the projection and the test in a
single call when you do not need the metacell IDs:
de = client.differential_expression_from_cells(
t_cells.cell_ids,
t_cells.sample_ids,
b_cells.cell_ids,
b_cells.sample_ids,
dataset_id="Human_Cell_Atlas",
)
The result is a DifferentialExpressionResult with to_dataframe() and
significant(alpha=0.05). Positive log2_fold_change means higher in the
first population (group A).
Cell Explorer cohorts¶
Cohorts are Zarr-backed objects managed by /api/cell-explorer/cohorts; they
are not the same records as groups saved from the Populations page. Use the
cohort methods when you want a persistent, downloadable metacell store:
cohort = client.create_cell_explorer_cohort(
"Activated T cells",
cell_types=["T cell"],
organs=["lung"],
dataset_id="Human_Cell_Atlas",
)
client.preview_cell_explorer_cohort(cohort["id"])
client.download_cell_explorer_cohort(cohort["id"], output_path="cohort.zarr")
Other cohort methods: list_cell_explorer_cohorts,
get_cell_explorer_cohort, delete_cell_explorer_cohort and
load_cell_explorer_cohort (requires the malva-client[zarr] extra).
Further reading¶
The complete reference, tutorials and the query parameter guide live in the dedicated documentation:
For direct HTTP access, without the Python package, see the other pages in this API reference.