Python pipeline for quantifying mitochondrial distribution in fluorescence microscopy images of micropatterned cells. The pipeline localizes fibronectin micropatterns via template matching, segments nuclei, and computes perinuclear vs. peripheral mitochondrial signal ratios.
Cells are cultured on fibronectin micropatterns that constrain cell shape. This pipeline:
- Localizes the micropattern center in each ND2 image via FFT-based template matching
- Extracts a 1024×1024 px crop centered on the pattern
- Segments the nucleus (405 nm / DAPI channel)
- Computes Euclidean distance transforms from the nuclear boundary and pattern arch
- Subtracts background from the mitochondrial channel (488 nm)
- Quantifies mitochondrial signal in perinuclear (<5 µm from nucleus) and peripheral (<5 µm from pattern arch) zones
- Outputs per-cell metrics to CSV/Excel and diagnostic figures to PDF
An optional second stage aggregates per-cell projections into mean images per experimental condition for visual comparison.
Dependencies are managed with Pixi. With Pixi installed:
pixi installThis installs Python 3.12 and all required packages into an isolated environment on Linux or macOS.
Process all ND2 files under a root directory:
pixi run python template_matching_bulk.py /path/to/patterned_data/plate_nameOptions:
| Flag | Description |
|---|---|
--keep-sums |
Retain raw signal sum columns (default: dropped) |
--include-complex |
Include non-simple peripheral measurements |
--include-acute |
Include acute peripheral zone measurements |
--include-all-percents |
Include all percentage columns (default: _total only) |
Outputs (written relative to the working directory):
projections/{plate}/{well}/*.nc— per-cell cropped z-sum projections (NetCDF4)projections/{plate}/{well}/*_488_bg_subtracted.nc— background-subtracted mitochondrial projectionstemplate_matching/{plate}/{well}/template_matching.csv— per-cell metricstemplate_matching/{plate}/{well}/template_matching.xlsx— same data as Exceltemplate_matching/{plate}/{well}/*.pdf— diagnostic figures per cell
For large datasets, bsub_analysis.sh submits one LSF job per directory using the paths listed in config/20251229_paths_for_analysis.txt:
bash bsub_analysis.shTo target a different set of directories, replace the contents of config/20251229_paths_for_analysis.txt with the desired paths (one per line), or edit bsub_analysis.sh directly. Each job requests 8 cores via bsub -n 8 -P vale.
After running the batch analysis, generate mean projection images per experimental condition:
pixi run python generate_comparison_projections.pyThis reads condition/well assignments from config/Comparisons_table_v3.xlsx and writes mean TIFF images to comparison_projections/.
The Marimo notebooks can be launched for interactive exploration:
pixi run marimo edit micropattern_cell_analysis_viewer.py # browse raw ND2 files
pixi run marimo edit prototype_comparisons.py # explore condition comparisons
pixi run marimo edit micropattern_cell_analysis.py # single-cell analysisOr launch the viewer via the configured Pixi task:
pixi run notebook| Script | Description |
|---|---|
template_matching_bulk.py |
Main pipeline. Batch processes ND2 files: template matching, cropping, nuclear segmentation, background subtraction, zone quantification, output to NetCDF4/CSV/Excel/PDF. |
comparison_loader.py |
Utility module. Provides load_comparisons(), find_well_directory(), list_cells(), load_cell(), and channel aggregation helpers used by other scripts. |
generate_comparison_projections.py |
Aggregates per-cell projections into mean images per experimental condition. Handles standard, denoised, and background-subtracted variants. |
combine_analysis.py |
Merges per-well template_matching.csv files into a single summary Excel workbook. |
plot_comparisons.py |
Plots template matching scores and per-condition metrics from CSV output. |
micropattern_cell_analysis.py |
Marimo notebook for interactive single-cell analysis and visualization. |
micropattern_cell_analysis_batch.py |
Marimo notebook version of the batch pipeline with enhanced visualization. |
micropattern_cell_analysis_viewer.py |
Marimo notebook for browsing ND2 files with channel and z-slice selection. |
micropattern_cell_analysis_pattern_center.py |
Marimo notebook for validating and manually setting pattern center coordinates. |
prototype_comparisons.py |
Marimo notebook for comparing projections across experimental conditions. |
When template matching fails or requires manual correction, add entries to coordinate_overrides.csv (no header):
/path/to/Cell1.nd2,x_pixels,y_pixels
The x and y values specify the top of the pattern (not the center); the pipeline converts these to pattern center coordinates automatically.
For images where the pattern is not near the default image offset, add entries to the offset_overrides dict in template_matching_bulk.py (lines 80–89). The default offset is [128, 128] pixels.
To restrict template matching to a sub-region of an image, add entries to the roi_overrides dict in template_matching_bulk.py (lines 137–139).
Input (ND2 files):
/path/to/patterned_data/
└── {plate_name}/
└── {well_id}_{condition}/
├── Cell1.nd2
├── Cell2.nd2
└── ...
Output:
projections/
└── {plate_name}/{well_id}_{condition}/
├── Cell1.nc # all-channel cropped projection
├── Cell1_488_bg_subtracted.nc # background-subtracted mito channel
└── denoised/ # denoised variants (if acquired)
template_matching/
└── {plate_name}/{well_id}_{condition}/
├── Cell1.pdf # diagnostic figures
├── template_matching.csv
└── template_matching.xlsx
comparison_projections/
└── {condition}/
├── mean_488.tif # mean mitochondrial projection
├── mean_405.tif # mean nuclear projection
└── individual_wells/
└── {plate}_{well}_mean_*.tif
Each row in template_matching.csv corresponds to one cell and includes:
| Column | Description |
|---|---|
path |
Path to the source ND2 file |
template_matching_score |
Fractional overlap of Otsu-thresholded image with the pattern template (quality control) |
cropped_background_threshold |
Background level estimated from image edges |
peripheral_{d}um_simple_percent_total |
Peripheral mitochondrial signal within d µm of arch / total crop signal (%) |
perinuclear_{d}um_percent_total |
Perinuclear mitochondrial signal within d µm of nucleus / total crop signal (%) |
Distances d = 1, 2, 3, 4, 5 µm are computed for each zone.
Detailed methods suitable for a Methods section are provided in:
methods/2026_03_06_methods.md— template matching, segmentation, and quantificationmethods/2026_03_06_methods_comparison_projections.md— comparison projection generationmethods/2026_03_06_references.md— package citations
Key packages (see pixi.toml for full list):
| Package | Purpose |
|---|---|
nd2 |
Reading Nikon ND2 microscopy files |
numpy / scipy |
FFT-based template matching, distance transforms |
scikit-image |
Otsu thresholding, contour finding, region properties |
xarray / netCDF4 |
Labeled image arrays, projection file I/O |
polars |
DataFrame operations and Excel output |
matplotlib |
Diagnostic figure generation |
cairosvg / pymupdf |
Rasterizing the SVG micropattern template |
marimo |
Reactive Python notebooks for interactive exploration |
Mark Kittisopikul — kittisopikulm@janelia.hhmi.org
Copyright © 2025 Howard Hughes Medical Institute
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of HHMI nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
See LICENSE.md for the full license text.