unravel.allen_institute.abca.merfish.merfish_expression_to_nii module#
Use abca_merfish_expression_to_nii or me from UNRAVEL to make 3D .nii.gz
images of ABCA MERFISH expression data.
- Inputs:
MERFISH expression .h5ad (or use -b to find it in the ABCA download cache)
MERFISH cell metadata CSV (or use -b to find it in the ABCA download cache)
Reference .nii.gz for affine/header/shape info (e.g., image_volumes/MERFISH-C57BL6J-638850-CCF/20230630/resampled_annotation.nii.gz)
Optional: -g <gene> … to select specific genes. If omitted, all genes in the selected MERFISH dataset are processed.
Optional: -i path/cell_metadata_filtered.csv to use a pre-filtered cell metadata CSV instead of the default ABCA MERFISH metadata.
- Outputs:
<gene>.nii.gz for each requested gene, with the same affine and header as the reference .nii.gz
Dir: MERFISH[_neuronal|_nonneuronal]_expression_maps/
Dir with -c and -val: MERFISH[_neuronal|_nonneuronal]_expression_maps_<filter_column>-<filter_value>/
Dir with -i: MERFISH[_neuronal|_nonneuronal]_expression_maps_<input_stem>/
Notes:#
-wuses threads, not processes, to avoid duplicating the loaded expression matrix.Each worker still creates one full 3D output image in memory, so keep
-wmodest.
Usage:#
abca_merfish_expression_to_nii -b <abc_download_root> -r <ref_nii> [-g <gene> …] [-i path/cell_metadata_filtered.csv] [–imputed] [–neurons | –nonneurons] [-o <output>] [-w N] [–dense] [–no-csc] [-dt float32|float64] [-f] [-v]
Usage for one gene:#
abca_merfish_expression_to_nii -b <abc_download_root> -g <gene> –no-csc
Usage for selected genes:#
abca_merfish_expression_to_nii -b <abc_download_root> -g <gene> <gene> …
Usage for all genes:#
abca_merfish_expression_to_nii -b <abc_download_root>
- class unravel.allen_institute.abca.merfish.merfish_expression_to_nii.GeneJobResult(gene: 'str', output_path: 'Path', nonzero_cells: 'int', expression_sum: 'float')[source]#
Bases:
object
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.unique_preserve_order(values)[source]#
Return unique strings while preserving first occurrence order.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.get_requested_genes_from_args(args)[source]#
Return requested genes and whether the user omitted -g/–gene.
When
-gis omitted, use the curated ABCA MERFISH gene-list helpers frommerfish.py. These lists let us build output paths and skip existing files before loading the large expression matrix. The final list is still validated againstadata.varafter AnnData is loaded.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.format_gene_preview(genes, max_genes=20)[source]#
Return a compact gene-list preview for console output.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.find_expression_h5ad(download_base, imputed, verbose=False)[source]#
Try to find the ABCA MERFISH expression h5ad without using backed subsetting.
This lets the script bypass
mf.load_expression_data(), which may subset a CSRDataset-backed h5ad before loading it into memory. The selected AnnData is opened backed/read-only, thenadata.Xis loaded fully into memory later.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.default_output_path(gene, args, n_requested_genes)[source]#
Return the default output path for one gene.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.get_var_symbols(adata)[source]#
Return gene symbols aligned to
adata.varcolumns.- Return type:
ndarray
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.get_gene_to_cols(adata, requested_genes)[source]#
Map requested gene symbols to integer column positions in
adata.X.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.load_and_prepare_cell_metadata(download_base, input_path, neurons, nonneurons, filter_column=None, filter_value=None)[source]#
Load and optionally filter MERFISH cell metadata.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.infer_cell_id_column(cell_df, obs_names)[source]#
Infer which cell metadata field matches AnnData obs_names best.
The original ABCA metadata generally uses cell labels as the index, but this keeps the script robust if future exports expose them as a column instead.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.align_cells_to_adata(cell_df, adata)[source]#
Align cell metadata to AnnData row order without subsetting backed AnnData.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.precompute_linear_indices(cell_df, shape, pixel_size_um)[source]#
Convert reconstructed MERFISH coordinates to flattened image indices once.
- This mirrors
mf.points_to_img_sumstyle accumulation, but does it in 3D: img[int(x / pixel_size), int(y / pixel_size), z_index] += expression
- This mirrors
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.load_X_fully_into_memory(adata)[source]#
Load
adata.Xinto memory once, avoiding backed slicing/subsetting.For AnnData CSRDataset/CSCDataset,
to_memory()should return a scipy sparse matrix. Fallbacks cover older anndata/scipy combinations.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.subset_matrix_after_memory_load(X, row_indices, valid_cell_mask, gene_to_cols, genes_to_process, dense, convert_to_csc)[source]#
Subset rows/columns after full in-memory load and return local gene-column mapping.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.get_gene_values(X, cols, dtype)[source]#
Return a dense 1D expression vector for one gene from the working matrix.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.expression_values_to_img(values, linear_idx, shape, dtype)[source]#
Accumulate per-cell expression values into a 3D image.
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.save_nii(img, affine, header, output_path, dtype)[source]#
Save image with reference affine/header and a floating output dtype.
- Parameters:
img (ndarray) –
affine (ndarray) –
output_path (Path) –
dtype (dtype) –
- Return type:
None
- unravel.allen_institute.abca.merfish.merfish_expression_to_nii.process_gene_job(gene, X, gene_to_cols, linear_idx, shape, dtype, affine, header, output_path)[source]#
Build and save one gene image from the shared in-memory expression matrix.