This document describes a Nextflow pipeline for registration, segmentation, and quantitative analysis of multi‑channel time‑lapse microscopy data (RFP, GFP, DIC) on a SLURM cluster with conda and optional Ilastik and YOLO‑based protist analysis.
The pipeline processes raw ND2/TIFF images into a structured project directory and performs:
- Grouping of raw images per experiment/chip/position.
- Conversion to OME‑TIFF stacks ((T, Z, C, Y, X)) with optional CLAHE and compression.
- Rigid registration (per chip) based on a reference position, and application to all channels.
- Per‑channel max‑projection and GFP spot detection using a normalized Difference‑of‑Gaussians (DoG) workflow.
- RFP segmentation via Ilastik, measurement of RFP objects, and merging of GFP/RFP measurements.
- DIC protist segmentation using YOLO, best z‑plane selection via tracking and focus metrics, and clustering/UMAP visualization.
By default, results are written to project/ inside the repository, with one subfolder per major step:
01_image_grouping/– text files listing raw images grouped by experiment/chip/position.02_process_groups/– processed OME‑TIFF stacks ((T, Z, C, Y, X)).03_registration_mats/–.npytransformation matrices per chip for registration.04_registered/– registered OME‑TIFF stacks, including_zstack_registered.ome.tifused downstream.05_max_projections/– per‑channel max projections_channelX_maxproj.ome.tif.06_gfp_segmentation/– GFP masks and per‑timepoint measurements/summary CSVs.07_rfp_segmentation/– Ilastik segmentation maps for RFP.08_rfp_measurements/– RFP object measurement CSVs.09_combined_results/– merged GFP/RFP measurement tablescombined_*.csv.10_DIC_segmentation/– YOLO predictions (predictions_raw/), cropped protist images (objects/), and per‑group metadata CSVs.11_DIC_plane_selection/– per‑stack best‑focus CSVs and visualizations + mergedbest_focus_objects.csv.12_DIC_clustering/– clustering results, CSVs, and visualizations (e.g. UMAP plots, HTML).
In addition, standard Nextflow reports are written to:
project/timeline.htmlproject/report.htmlproject/trace.txt
- Nextflow (>= 22.04.0).
- SLURM cluster environment, with queues analogous to
htc-el8(CPU) andgpu-el8(GPU) and suitable GPU options (e.g.--gpus 1 -C gpu=A100). - Conda with at least two environments:
image_analysis– for registration, projections, GFP segmentation, RFP measurement, plane selection, clustering.yolo– for YOLO‑based protist segmentation and some grouping/processing steps.
- Ilastik available as a module (e.g.
module load ilastik) if RFP segmentation is used.
The Python scripts under bin/ require typical scientific Python stacks. Depending on your environment, install:
- Core:
numpy,pandas,tifffile,tqdm,scikit-image,scipy,Pillow,opencv-python. - GFP segmentation (
segment_gfp_timeseries.py):numpy,pandas,tifffile,tqdm,Pillow,scikit-image,scipy. - Protist segmentation (
Protists_segmentation.py):numpy,opencv-python,pandas,ultralytics,scikit-image,tifffile,tqdm.
- Raw ND2/TIFF data in a single input folder specified via
--raw_dir. - An Ilastik project
.ilpif RFP segmentation is desired (--ilastik_project). - A trained YOLO
.ptmodel for protist segmentation (--yolo_model_path).
-
Obtain the pipeline
Place
main.nf,nextflow.configand thebin/scripts in a project folder on the cluster (e.g. clone the repository or copy the files). -
Set up conda environments
Create two environments (adjust names/paths as you wish, but keep them in sync with
nextflow.config):# Example: image_analysis env mamba create -n image_analysis python=3.10 \ numpy pandas tifffile tqdm scikit-image scipy pillow opencv # Example: yolo env mamba create -n yolo python=3.10 \ numpy pandas tifffile tqdm opencv ultralytics
-
Adjust paths in
nextflow.config- Set
workDirandprocess.scratchto your cluster’s scratch filesystem. - Update
conda = '/home/USER/.conda/envs/...'entries to match your environment locations. - Adapt
queue,clusterOptionsand any GPU options to your SLURM configuration.
- Set
-
Ensure
bin/is referenced correctlyparams.scripts_dirdefaults to"${baseDir}/bin", so the Python scripts must live inbin/relative tomain.nf.
Minimal full‑pipeline example:
nextflow run main.nf \
--raw_dir /path/to/raw_nd2_or_tiffs \
--project_dir /path/to/output_project \
--ilastik_project /path/to/segmentation.ilp \
--yolo_model_path /path/to/yolo_model.ptKey points:
--raw_diris required and must point to the folder containing the raw ND2/TIFF files.--project_dirdefines where the01_…–12_…subfolders and Nextflow reports will be created.--ilastik_projectenables the RFP segmentation branch; if omitted, RFP segmentation/measurement is skipped with a warning.--yolo_model_pathenables the protist DIC branch; if omitted, DIC segmentation, plane selection and clustering are skipped.
To print a concise built‑in help message:
nextflow run main.nf --helpThe Nextflow workflow block connects processes into two main branches: GFP/RFP and DIC protists, sharing the registration and max‑projection steps.
-
Input validation and logging
- Fails fast if
--raw_diris not provided. - Logs all key parameters (project directory, Ilastik project path, register channels, GFP threshold, YOLO model, device and clustering settings) at startup.
- Fails fast if
-
01 – GROUP_IMAGES
- Runs
group_raw_images.pyon--raw_dir. - Produces:
*_files.txt– lists of raw files per logical group.all_groups.txt– overview of all groups.
- Runs
-
02 – PROCESS_GROUPS
- For each group list, runs
process_groups_multipage.py. - Outputs OME‑TIFF stacks to
02_process_groups/with shape roughly ((T, Z, C, Y, X)). - Controlled by:
params.multichannel_channelsparams.multichannel_claheparams.save_raw_with_claheparams.proc_compression
- For each group list, runs
-
03 – CALC_REGISTRATION
- Filters
_zstack_processed.ome.tiffiles to only those withpos006and runscalculate_registration_stackreg.py. - Writes chip‑specific transformation matrices as
.npyinto03_registration_mats/.
- Filters
-
04 – APPLY_REGISTRATION
- Applies the chip‑specific transformations to all z‑stack processed files using
apply_registration_multicore.py. - If a chip has no transformation matrix (no suitable
pos006), logs a warning and copies the input to a_registeredfile as a degenerate fallback. - Outputs
_registered.ome.tiffiles in04_registered/.
- Applies the chip‑specific transformations to all z‑stack processed files using
-
DIC branch (protists)
- Filters registered stacks to those ending
_zstack_registered.ome.tif. - 10 – PROTISTS_SEGMENT_DIC
- Runs
Protists_segmentation.pyusing YOLO on the DIC channel (channel index 2). - Produces:
10_DIC_segmentation/predictions_raw/<group>/t###_z##.jsonwith bounding boxes, classes, confidences and polygons.10_DIC_segmentation/objects/<group>/t###_z##/*with per‑object crops and masks.10_DIC_segmentation/metadata/<group>_objects.csvwith geometric and intensity features.
- Runs
- 11 – PROTISTS_PLANE_SELECTION_STACK
- For each registered stack, uses predictions + registered stacks to link detections across z and select best‑focus planes.
- Outputs per‑stack
<group>_best_focus_objects.csv, object crops and optional visualizations under11_DIC_plane_selection/metadataandvisualizations.
- 11b – PROTISTS_PLANE_SELECTION_MERGE
- Concatenates all per‑stack best‑focus CSVs into
best_focus_objects.csv.
- Concatenates all per‑stack best‑focus CSVs into
- 12 – PROTISTS_CLUSTER_DIC
- Runs
protist_clustering.pywith the mergedbest_focus_objects.csv, extracting features, applying PCA, KMeans clustering and generating UMAP/visualization outputs in12_DIC_clustering/.
- Runs
- Filters registered stacks to those ending
-
GFP/RFP branch
- 05 – CALC_MAX_PROJECTIONS
- Runs
calculate_max_projections.pyto produce_channelX_maxproj.ome.tiffor each registered stack in05_max_projections/.
- Runs
- 06 – SEGMENT_GFP
- Uses
segment_gfp_timeseries.pyon_channel0_maxprojtime series. - Workflow:
- Normalizes each frame to ([0, 1]).
- Applies Gaussian smoothing or DoG background subtraction.
- Runs DoG blob detection with configurable
min_sigma,max_sigma,sigma_ratio,threshold,overlap. - Filters small objects by
min_object_size. - Saves label masks per timepoint and measurement/summary CSVs in
06_gfp_segmentation/.
- Uses
- 07 – SEGMENT_RFP_ILASTIK
- For
_channel1_maxprojimages, runssegment_rfp_ilastik.pywith the given Ilastik project and export source. - Generates
_segmentation.tifffiles in07_rfp_segmentation/.
- For
- 08 – MEASURE_RFP_OBJECTS
- Pairs each segmentation TIFF with the corresponding
_maxproj.ome.tifintensity image. - Invokes
measure_rfp_objects.pyto compute shape and intensity features per RFP object in08_rfp_measurements/.
- Pairs each segmentation TIFF with the corresponding
- 09 – COMBINE_MEASUREMENTS
- Collects GFP CSVs and RFP CSVs, moves them into subfolders and runs
combine_measurements.py. - Outputs
combined_*.csvinto09_combined_results/.
- Collects GFP CSVs and RFP CSVs, moves them into subfolders and runs
- 05 – CALC_MAX_PROJECTIONS
The main configuration file sets global defaults and per‑process overrides.
workDir– global Nextflow work directory on scratch.conda.enabled = true– enables automatic activation of conda envs defined per process.- Global
processdefaults:executor = 'slurm'queue = 'htc-el8'errorStrategy = 'finish'maxRetries = 0time = '1h'cpus = 1memory = '4.GB'scratch = '/scratch/.../nextflow/'
GROUP_IMAGESconda = '/path/to/envs/yolo',cpus = 2,memory = '2.GB',time = '10m'.
PROCESS_GROUPS- Similar env, but
memory = '16.GB',time = '20m',maxForks = 50.
- Similar env, but
CALC_REGISTRATION,APPLY_REGISTRATION,CALC_MAX_PROJECTIONS,SEGMENT_GFP- Use
image_analysisenv with 1–2 CPUs, 8–16 GB RAM, and times from 30 min to a few hours.
- Use
SEGMENT_RFP_ILASTIKmodule = 'ilastik',conda = null,cpus = 8,memory = '32.GB',time = '4h',maxForks = 16.
MEASURE_RFP_OBJECTS,COMBINE_MEASUREMENTS- 4 CPUs, 8–16 GB RAM, modest runtimes.
PROTISTS_SEGMENT_DICconda = '/path/to/envs/yolo',cpus = 4,memory = '16.GB',time = '30m',queue = 'gpu-el8',clusterOptions = '--gpus 1 -C gpu=A100',maxForks = 4.
PROTISTS_PLANE_SELECTION_STACKcpus = 4,memory = '8.GB',time = '2h',maxForks = 50for high parallelism across positions.
PROTISTS_CLUSTER_DICcpus = 8,memory = '32.GB',time = '2h',maxForks = 1.
timeline,report,traceblocks enable Nextflow HTML and text reports under${params.project_dir}.manifestincludes:name = 'RFP/GFP/DIC Image Processing Pipeline @ EMBL'author = 'Jonas Wagner'- Human‑readable description, version, and
nextflowVersion = '>=22.04.0'.
All parameters below can be overridden on the command line as --param value.
--raw_dir(required): path to raw ND2/TIFF files; controls whetherGROUP_IMAGESruns.--project_dir(default"${baseDir}/project"): output root for all step folders and reports.--scripts_dir(default"${baseDir}/bin"): folder containing the Python scripts.
--grouping_pattern(default"*.nd2"): pattern to match raw files.--multichannel_channels(default"1 2 3"): channels to extract into the multichannel stack.--multichannel_clahe(default"3"): channels to which CLAHE is applied.--save_raw_with_clahe(default"false"): whether to keep both raw and CLAHE versions.--proc_compression(default"none"): TIFF compression (none | deflate | lzw).
--registration_channel(default1): 0‑indexed channel used for registration (e.g. channel 2).--registration_reference(default"first"):"first"or"previous"frame as reference.--register_channels(default"0 1 2"): channels to which registration is applied.--apply_compression(default"none"): compression for registered OME‑TIFF output.
--gfp_preprocess_method(default"dog_background"):"gaussian"or"dog_background"preprocessing.--gfp_sigma_smooth(default1.0): Gaussian sigma when usinggaussian.--gfp_min_sigma,--gfp_max_sigma(default1.0,2.0): DoG blob scale range.--gfp_sigma_ratio(default1.2): spacing of scales in DoG.--gfp_threshold(default0.02): detection threshold on normalized ([0, 1]) image; lower for more sensitive detection.--gfp_overlap(default0.6): maximum allowed overlap between blobs.--gfp_min_object_size(default4): minimum area (pixels) of kept objects.
--ilastik_project(defaultnull): path to Ilastik.ilpfile; required for RFP segmentation.--ilastik_export_source(default"Simple Segmentation Stage 2"): Ilastik export source name.--ilastik_max_forks(default8): maximum parallel Ilastik jobs.
--yolo_model_path(default set in code, but should be overridden): YOLO.ptmodel for DIC protist segmentation.--protist_device(default'0'): GPU index or'cpu'.--protist_conf(default0.2): YOLO confidence threshold.--protist_iou(default0.1): YOLO IoU threshold for NMS.--protist_imgsz(default1024): input size for YOLO.
Best‑plane selection parameters:
--protist_iou_threshold(default0.2): bbox IoU threshold for linking across z.--protist_centroid_dist(default15): maximum centroid distance (pixels) for linking.--protist_max_z_gap(default10): maximum z‑gap allowed in tracks.
Feature‑extraction flags:
--protist_use_glcm(default"true"): toggle GLCM features.--protist_use_lbp(default"true"): toggle LBP features.--protist_use_hog(default"true"): toggle HOG features.
Clustering/embedding:
--protist_n_clusters(default3): KMeans cluster count.--protist_pca_components(default5): PCA dimension.--protist_umap_neighbors(default15): neighbors for UMAP.--protist_max_hover_images(default20000): cap on number of images for interactive plots.
- Adjust
maxForksper process (e.g.PROCESS_GROUPS,SEGMENT_RFP_ILASTIK,MEASURE_RFP_OBJECTS,PROTISTS_SEGMENT_DIC,PROTISTS_PLANE_SELECTION_STACK) to match your cluster’s concurrency limits and memory. - Increase
timeandmemoryinnextflow.configfor large datasets or if jobs hit resource limits. - On GPU nodes, adapt
queueandclusterOptionsto your scheduler (e.g. different GPU type, partition name, or number of GPUs). - The
trace.txt,timeline.htmlandreport.htmloutputs are useful for profiling and further tuning.
The current workflow always executes both branches when their prerequisites are present:
- To skip RFP: omit
--ilastik_project. The pipeline logs a warning and skips Ilastik + RFP measurement + combination. - To skip protists: omit
--yolo_model_path. DIC segmentation, plane selection and clustering are skipped.
If you frequently need partial workflows (e.g. protists‑only), you can adapt workflow {} and when: clauses to guard certain branches behind additional flags.
This pipeline provides an end‑to‑end solution for RFP/GFP/DIC time‑lapse microscopy, including registration, spot detection, RFP quantification and GPU‑accelerated protist segmentation, best‑focus selection and clustering. With appropriate conda environments and SLURM configuration, it can be used as a general‑purpose image‑analysis backbone that you can further specialize by tweaking parameters in main.nf and nextflow.config.
