This project implements a pipeline for analyzing multi-round fluorescence imaging data using YOLO and SAM (Segment Anything Model) for cell detection and segmentation.
Note: This README.md file was generated by an AI assistant based on the provided Python scripts and ImageJ macro, revised by Long.
Note Nov 2024: Updated analysis methods. Added function of the quantification of more morphological features, see examples in the test notebook.
The 4I Pipeline is designed to process multi-round fluorescence imaging data, detect cells, and analyze their intensities across different imaging rounds. It uses a combination of YOLO (You Only Look Once) for initial cell detection and SAM (Segment Anything Model) for precise cell segmentation. Here is the schematic of the workflow and a sample result of the detection.
Preprocessing workflow can be found in repository Preprocessing, the following instruction is a replicate of the readme.md file of that repository.
This workflow consists of four main steps for processing and organizing microscopy images. Below is a detailed explanation of each step and the resulting file structure changes.
The input of the preprocessing workflow comes directly from microscopy exports, and the resulting file arrangement can be used for downstream analysis of Cell Detection.
Note for the naming of each microscopy image, the name should contain
-
The treatment (WT/EZH2...)
-
The number of single cell clones
-
The number of frame for each clones
Please adjust the preprocessing pipeline according to your experiment setup.
- Move files containing certain strings (0_Move_files.py)
- Organize and rename files (1_Name_process_compressed_simplify.py)
- Merge and color channels (2_Merge_Batch.ijm)
- Organize merged images for analysis (3_Move_Formal.py)
This script moves files containing a specific string in their name to a new folder.
Input:
- Source folder containing .tif files
- String to search for in file names (Typically the treatment, in the case of example data, it should be "WT"/"EZH2"/DNMT")
Output:
- New subfolder named after the search string, containing matching .tif filess
File Structure Change:
src_folder/
├── file1.tif
├── file2.tif
├── ...
└── string/
├── file_with_string.tif
└── ...
This script organizes files into subfolders based on cell types and channels, then renames them.
Input:
- User-selected directory containing image files
- Number of fluorescent channels
- Number of cell types
Output:
- Organized folder structure with renamed files
- In this case, -1-1 means the 1st frame of 1st clone, -10-2 means the 2nd frame of 10th clone, etc. The clone and frame information should be noted when doing the microscopy.
File Structure Change:
selected_directory/
├── -1-1/
│ ├── Channel0/
│ │ ├── 0ch0.png
│ │ └── ...
│ ├── Channel1/
│ │ ├── 0ch1.png
│ │ └── ...
│ └── Merge/
├── -1-2/
│ └── ...
├── -2-1/
│ └── ...
└── ...
This ImageJ macro colors individual channel images and merges them.
This macro can only run in ImageJ, please put this file under ~/Fiji.app/macros/toolsets/ (the path you store your ImageJ program).
Input:
- Directory with subfolders containing channel images (output from Step 2)
Output:
- Colored individual channel images
- Merged images for each frame
- Optional: Z-projected stack of merged images
File Structure Change:
input_directory/
├── -1-1/
│ ├── Channel0/
│ ├── Channel1/
│ └── Merge/
│ ├── Blue0ch0.png
│ ├── Red0ch1.png
│ ├── 0.png (merged)
│ └── ...
├── -1-2/
│ └── ...
└── ...
Stacked.png (optional, in parent directory)
This script organizes the merged images into a structure suitable for analysis.
Input:
- Source directory (output from Step 3)s
- Destination directory for analysis
- Round number
Output:
- Organized folder structure for analysis
File Structure Change:
destination_directory/
├── frame_0/
│ └── R1/
│ ├── 0.png
│ ├── xxxch0.png
│ └── ...
├── frame_1/
│ └── R1/
│ └── ...
└── ...
- Run
0_Move_files.pyto initially organize your .tif files. - Execute
1_Name_process_compressed_simplify.pyto further organize and rename your files. - Use ImageJ to run the
2_Merge_Batch.ijmmacro for coloring and merging channels. - Finally, run
3_Move_Formal.pyto organize the merged images for analysis.
Make sure to follow the prompts in each script to provide the necessary input, such as directory paths, number of channels, and cell types.
- Python 3.x
- ImageJ with the ability to run macros
- Required Python libraries:
os,shutil,tkinter
- Ensure that your initial file naming convention is consistent for the scripts to work correctly.
- The ImageJ macro allows for customization of channel colors and merge options.
- Always verify the output at each step before proceeding to the next one.
The initial file structure, before running the image alignment script, should be as follows (please read the instruction for preprocessing):
Origin_path/
├── frame_1/
│ ├── R1/
│ │ ├── 1.png (channel-merged image)
│ │ ├── xxxch0.png (single channel image)
│ │ └── ...
│ ├── R2/
│ ├── R3/
│ └── ...
├── frame_2/
├── frame_3/
└── ...
# ImageJ alignment file for each frame
Origin_path/
├── frame_1/
│ └── TileConfiguration.registered.txt
├── frame_2/
│ └── TileConfiguration.registered.txt
└── ...
After running the image alignment script, the file structure will be:
Group_path/
├── frame_1/
│ ├── R1/
│ │ ├── 1.png
│ │ ├── 2.png
│ │ └── ...
│ ├── R2/
│ ├── R3/
│ ├── ...
│ └── channels/
│ ├── R1ch0.png
│ ├── R1ch1.png
│ └── ...
├── frame_2/
├── frame_3/
└── ...
- Clone this repository
- Install the required dependencies:
pip install ultralytics roboflow opencv-python numpy torch segment-anything tqdm - Download the SAM checkpoint file and place it in the
weightsfolder:wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth -P weights/
- Ensure your data is organized according to the initial file structure mentioned above (see preprocessing workflow).
- Run the image alignment script (
1_Image_alignment.py) to prepare the data for the pipeline. - Update the
Group_pathandResult_pathvariables inmain.pyto point to your aligned input and output directories. - Run the main script:
python main.py
0_Yolo_Traning.py: Script for training the YOLO model on cell detection.1_Image_alignment.py: Aligns images from different rounds to ensure proper overlap.2_Yolo_Prediction.py: Uses the trained YOLO model to predict bounding boxes for cells.3_SAM_Prediction.py: Applies the SAM model for precise cell segmentation and intensity measurement.main.py: The main script that orchestrates the entire pipeline.
The 4I Pipeline consists of several interconnected scripts, each performing a specific function in the data processing and analysis workflow. Here's a detailed explanation of each script and how they work together:
-
YOLO Training (
0_Yolo_Traning.py)- Function: Trains the YOLO model for cell detection.
- Input: Annotated cell images from the Roboflow dataset.
- Output: Trained YOLO model weights.
- Connection: The trained model is used by
2_Yolo_Prediction.pyfor cell detection.
-
Image Alignment (
1_Image_alignment.py)- Function: Aligns and stitches images from different imaging rounds.
- Input: Raw images from each round and ImageJ alignment files.
- Output: Aligned and stitched images in the
Group_pathdirectory. - Connection: Prepares the data for processing by subsequent scripts.
-
YOLO Prediction (
2_Yolo_Prediction.py)- Function: Detects cells and generates bounding boxes.
- Input: Aligned images from
Group_path. - Output: Bounding box coordinates for detected cells.
- Connection: Provides input for the SAM model in
3_SAM_Prediction.py.
-
SAM Prediction (
3_SAM_Prediction.py)- Function: Performs precise cell segmentation and intensity measurement.
- Input: Aligned images and YOLO-generated bounding boxes.
- Output: Cell intensity measurements and contours.
- Connection: Generates the final output data for analysis.
-
Main Script (
main.py)- Function: Orchestrates the entire pipeline.
- Input: Aligned images in
Group_path. - Output: Calls functions from other scripts to process all frames and generate final results.
- Connection: Ties all the scripts together and manages the overall workflow.
- Raw images → Image Alignment → Aligned images
- Aligned images → YOLO Prediction → Bounding boxes
- Aligned images + Bounding boxes → SAM Prediction → Intensity measurements and contours
The main script (main.py) coordinates this flow, ensuring that each step is executed in the correct order and with the proper inputs.
The pipeline generates the following outputs in the specified Result_path:
- YOLO prediction images:
f{frame}R{round}_yolo.png - Intensity measurements:
f{frame}_intensity.csv - Cell contours (if enabled):
f{frame}_countour.csv
The intensity CSV files contain the following information for each cell:
- Cell center coordinates (x, y)
- Intensity measurements for each channel and round
This pipeline requires that images are pre-aligned using ImageJ. The 1_Image_alignment.py script uses the ImageJ alignment results to prepare the data for the subsequent steps in the pipeline. Ensure that your input images are properly aligned before running the main pipeline.