TEST 4: Histology and Imaging Analysis
=======================================

> **Prompt library:** This is the canonical prompt for [Clinical Workflow Prompt 4](../../../../prompts/clinical-workflow.md#prompt-4-histopathology-and-immunofluorescence). See that page for clinical interpretation context and expected output summary.

Patient ID: PAT001-OVC-2025

⚠️ IMPORTANT: This test uses MICROSCOPY IMAGES (TIFF files).
- H&E images use BRIGHTFIELD microscopy (chromogenic stains)
- IF/MxIF images use FLUORESCENCE microscopy (fluorescent antibodies)

**SERVER WORKFLOW:**
- **H&E analysis:** Use mcp-openimagedata ONLY (visual morphology assessment)
- **IF/MxIF analysis:** Use mcp-openimagedata (loading/compositing) + mcp-deepcell (segmentation)
- **DeepCell:** Used for MxIF cell segmentation using DeepCell-TF library (fluorescence images only)

## Imaging Analysis

For patient PAT001-OVC-2025, analyze histology and immunofluorescence images:

### Data Files Location:
Files are in GCS at: `gs://sample-inputs-patientone/patient-data/PAT001-OVC-2025/imaging/`
(Also accessible as relative path: `patient-data/PAT001-OVC-2025/imaging/`)

**Test Files (4 files used in this test):**

| File | Image Type | Microscopy | Format | Server(s) to Use | Purpose |
|------|-----------|------------|--------|------------------|---------|
| **PAT001_tumor_HE_20x.tiff** | H&E | Brightfield | RGB TIFF | **openimagedata ONLY** | Morphology assessment, necrosis ID, cellularity |
| **PAT001_tumor_IF_CD8.tiff** | IF (single) | Fluorescence | Grayscale TIFF | **openimagedata + deepcell** | CD8+ T cell segmentation & quantification |
| **PAT001_tumor_IF_KI67.tiff** | IF (single) | Fluorescence | Grayscale TIFF | **openimagedata + deepcell** | Ki67+ proliferation segmentation & index |
| **PAT001_tumor_multiplex_IF_TP53_KI67_DAPI.tiff** | MxIF (3-channel) | Fluorescence | RGB TIFF (3 channels) | **openimagedata + deepcell** | Multi-marker phenotyping (TP53/Ki67 co-expression) |

**Additional Available Files (not used in this test):**
- PAT001_tumor_IF_CD3.tiff (fluorescence - CD3 T cells)
- PAT001_tumor_IF_DAPI.tiff (fluorescence - nuclear stain)
- PAT001_tumor_IF_PanCK.tiff (fluorescence - epithelial marker)

### Pre-Flight Validation:

Before starting analysis, verify image quality:

1. **File existence check:**
   - Confirm all 4 test TIFF files are accessible
   - Check file sizes are reasonable:
     * H&E: ~800 KB (RGB)
     * IF single-channel: ~268 KB each (grayscale)
     * MxIF multiplex: ~63 KB (3-channel RGB)

2. **Image format validation:**
   - H&E image: Should be **RGB TIFF** (brightfield microscopy with chromogenic stains)
   - IF images: Should be **grayscale TIFF** (single fluorescence channel)
   - Multiplex IF: Should be **RGB TIFF** with 3 channels (TP53/KI67/DAPI fluorescence)

3. **Image dimensions:**
   - All test images: 512 × 512 pixels
   - Verify all images from same tissue section have consistent dimensions
   - Note: Production images typically 1000-2000 pixels at 20× magnification

4. **Imaging modality verification:**
   - H&E: Confirm it's **chromogenic staining** (RGB TIFF, NOT fluorescence)
   - IF images: Confirm **fluorescence microscopy** format (grayscale TIFF for single-channel, RGB TIFF for multiplex)

**If validation fails:** Report specific issues (missing files, incorrect format, dimension mismatches) but continue with best-effort analysis using available images.

### Analysis Steps:

1. **H&E Histology Analysis**
   **Server:** mcp-openimagedata ONLY (no deepcell - visual morphology assessment)
   **File:** PAT001_tumor_HE_20x.tiff

   - This is a **BRIGHTFIELD** image with chromogenic stains (NOT fluorescence)
   - Hematoxylin stains nuclei blue, Eosin stains cytoplasm pink
   - Describe tissue architecture (glandular patterns, cell density)
   - Are there necrotic regions visible (pale/acellular areas)? (Expect: Yes)
   - Estimate tumor cellularity percentage using visual assessment (Expect: ~70-80%)
   - Overall tissue morphology consistent with HGSOC?

2. **Immunofluorescence - CD8 T Cell Infiltration**
   **Servers:** mcp-openimagedata (load image) → mcp-deepcell (segment & quantify)
   **File:** PAT001_tumor_IF_CD8.tiff

   - This is a **FLUORESCENCE** image (grayscale TIFF, NOT H&E)
   - Use openimagedata to load the IF image
   - Use deepcell to segment nuclei and identify CD8+ cells
   - Quantify CD8+ T cells (cells/mm²)
   - Spatial distribution: Periphery vs infiltrating?
   - Expected: LOW infiltration (~5-15 cells/mm²), primarily at periphery (immune exclusion)

3. **Immunofluorescence - Ki67 Proliferation**
   **Servers:** mcp-openimagedata (load image) → mcp-deepcell (segment & quantify)
   **File:** PAT001_tumor_IF_KI67.tiff

   - This is a **FLUORESCENCE** image (grayscale TIFF, NOT H&E)
   - Use openimagedata to load the IF image
   - Use deepcell to segment cells and identify Ki67+ nuclei
   - Calculate Ki67 proliferation index (% positive cells)
   - Is proliferation uniform or heterogeneous across tissue?
   - Expected: HIGH proliferation (~45-55%), heterogeneous

4. **Multiplex Immunofluorescence (MxIF)**
   **Servers:** mcp-openimagedata (load & composite) → mcp-deepcell (segment & phenotype)
   **File:** PAT001_tumor_multiplex_IF_TP53_KI67_DAPI.tiff

   - This is a **FLUORESCENCE** multiplex image with 3 channels (RGB TIFF):
     * Channel 1 (Red): TP53 (mutant p53 accumulation)
     * Channel 2 (Green): KI67 (proliferation marker)
     * Channel 3 (Blue): DAPI (nuclear stain)
   - Use openimagedata to load and optionally composite the channels
   - Use deepcell to segment cells based on DAPI channel
   - Quantify marker expression per cell:
     * TP53+ cells (% of total)
     * KI67+ cells (% of total)
     * TP53+/KI67+ double-positive cells (% of total)
   - Are TP53-mutant cells also highly proliferative?

5. **Generate visualizations** (CRITICAL for validation):

   **Use mcp-openimagedata and mcp-deepcell visualization tools:**

   **For CD8 IF image:**
   - **Segmentation overlay** (use tool: `generate_segmentation_overlay` from mcp-deepcell):
     * Original IF image with cell boundaries outlined
     * CD8+ cells marked in one color (e.g., green)
     * CD8- cells marked in different color (e.g., red)
     * Expected: Few green cells, mostly at periphery

     ```
     Tool: generate_segmentation_overlay (mcp-deepcell)
     Inputs:
     - image_path: PAT001_tumor_IF_CD8.tiff
     - marker_name: "CD8"
     - overlay_color: "green"
     ```

   - **Phenotype visualization** (use tool: `generate_phenotype_visualization` from mcp-cell-classify):
     * Heatmap showing CD8+ cell density across tissue
     * Hotspots indicate immune infiltration
     * Expected: Peripheral clustering, central exclusion

     ```
     Tool: generate_phenotype_visualization (mcp-cell-classify)
     Inputs:
     - segmentation_results: (output from segment_cells)
     - phenotype_name: "CD8+"
     - visualization_type: "heatmap"
     ```

   **For Ki67 IF image:**
   - **Segmentation overlay** (use tool: `generate_segmentation_overlay` from mcp-deepcell):
     * Original IF image with nuclear segmentation boundaries
     * Ki67+ nuclei marked (e.g., yellow)
     * Ki67- nuclei marked (e.g., blue)
     * Expected: ~50% yellow nuclei (high proliferation)

     ```
     Tool: generate_segmentation_overlay (mcp-deepcell)
     Inputs:
     - image_path: PAT001_tumor_IF_KI67.tiff
     - marker_name: "Ki67"
     - overlay_color: "yellow"
     ```

   - **Phenotype visualization** (use tool: `generate_phenotype_visualization` from mcp-cell-classify):
     * Spatial map of Ki67+ cell density
     * Expected: Heterogeneous distribution, higher in some regions

     ```
     Tool: generate_phenotype_visualization (mcp-cell-classify)
     Inputs:
     - segmentation_results: (output from segment_cells)
     - phenotype_name: "Ki67+"
     - visualization_type: "heatmap"
     ```

   **For Multiplex IF image:**
   - **Channel composite** (use tool: `generate_multiplex_composite` from mcp-openimagedata):
     * RGB overlay showing all 3 channels
     * Red (TP53) + Green (KI67) + Blue (DAPI)
     * Yellow cells = TP53+/KI67+ double-positive
     * Expected: Many yellow cells (TP53-mutant cells proliferating)

     ```
     Tool: generate_multiplex_composite (mcp-openimagedata)
     Inputs:
     - channel_paths: [TP53.tiff, KI67.tiff, DAPI.tiff]
     - channel_names: ["TP53", "KI67", "DAPI"]
     - channel_colors: ["red", "green", "blue"]
     - normalize: true
     ```

   - **Cell phenotype segmentation** (use tool: `generate_phenotype_visualization` from mcp-cell-classify):
     * Cells colored by phenotype
     * TP53+/KI67+ = yellow
     * TP53+/KI67- = red
     * TP53-/KI67+ = green
     * TP53-/KI67- = gray
     * Expected: Dominant yellow population (~40-50%)

     ```
     Tool: generate_phenotype_visualization (mcp-cell-classify)
     Inputs:
     - segmentation_results: (output from classify_cell_states)
     - phenotype_categories: ["TP53+/KI67+", "TP53+/KI67-", "TP53-/KI67+", "TP53-/KI67-"]
     - visualization_type: "phenotype_map"
     ```

   **For H&E image:**
   - **Annotated morphology** (use tool: `generate_he_annotation` from mcp-openimagedata):
     * H&E image with regions highlighted
     * Necrotic areas outlined in red dashed boxes
     * High cellularity regions outlined in green solid boxes
     * Expected: ~15-20% necrotic area, ~70-80% high cellularity

     ```
     Tool: generate_he_annotation (mcp-openimagedata)
     Inputs:
     - he_image_path: PAT001_tumor_HE_20x.tiff
     - necrotic_regions: [{"x": 500, "y": 300, "width": 200, "height": 150}, ...]
     - high_cellularity_regions: [{"x": 200, "y": 200, "width": 250, "height": 200}, ...]
     - necrotic_color: "red"
     - cellularity_color: "green"
     ```

   **Note:** If servers cannot generate image outputs, provide detailed text descriptions including:
   - Cell counts and percentages
   - Spatial distribution patterns (peripheral vs central, uniform vs clustered)
   - Phenotype breakdowns
   - Quantitative metrics for validation

## Expected Results:

**H&E Morphology:**
- Cellularity: 70-80% tumor cells
- Architecture: High-grade serous features (solid and glandular patterns)
- Necrosis: Present in ~15-20% of tissue
- Nuclear atypia: Marked (high-grade)

**CD8 T Cell Infiltration:**
- Density: LOW (~5-15 cells/mm²)
- Distribution: Predominantly at tumor periphery (stromal interface)
- Infiltration: MINIMAL intratumoral CD8+ cells
- Interpretation: **Immune exclusion phenotype**

**Ki67 Proliferation Index:**
- Ki67 index: HIGH (~45-55% positive cells)
- Distribution: Heterogeneous (higher in tumor_proliferative regions)
- Pattern: Consistent with aggressive HGSOC

**Multiplex IF Cell Segmentation:**
- Total cells segmented: ~500-800 cells
- TP53+ cells: ~65-75% (mutant p53 accumulation)
- KI67+ cells: ~45-55% (high proliferation)
- TP53+/KI67+ double-positive: ~40-50%
- Interpretation: TP53-mutant cells are highly proliferative

## Output Format:

Please provide:

1. **H&E Summary:**
   - Estimated cellularity: XX%
   - Necrotic regions: Present/Absent (XX% of tissue)
   - Tissue architecture description
   - Consistent with HGSOC diagnosis? (Yes/No)
   - **Visualization:** Annotated morphology image (or text description of necrotic/cellular regions)

2. **CD8 T Cell Analysis:**
   - CD8+ cell density: XX cells/mm²
   - Spatial pattern: Periphery vs Infiltrating
   - Immune phenotype: Hot/Warm/Cold?
   - **Visualizations:**
     * Segmentation overlay (CD8+ vs CD8- cells marked)
     * Spatial distribution heatmap (or text description of clustering pattern)

3. **Ki67 Proliferation:**
   - Ki67 index: XX% positive
   - Distribution pattern: Uniform vs Heterogeneous
   - Proliferation level: High/Medium/Low
   - **Visualizations:**
     * Nuclear segmentation overlay (Ki67+ vs Ki67- nuclei marked)
     * Proliferation heatmap (or text description of spatial variation)

4. **Multiplex IF Results:**
   - Total cells segmented: XXX
   - TP53+: XX%
   - KI67+: XX%
   - TP53+/KI67+: XX%
   - Correlation: Are TP53+ cells proliferating? (Yes/No)
   - **Visualizations:**
     * RGB channel composite (TP53/KI67/DAPI overlay)
     * Cell phenotype segmentation (colored by marker expression)
     * Scatter plot (TP53 vs KI67 intensity, or table of quadrant counts)

5. **Overall Imaging Interpretation:**
   - Is this a highly proliferative tumor? (Yes)
   - Is there immune exclusion? (Yes)
   - Does imaging support molecular findings? (Yes)
   - Implications for immunotherapy? (Limited efficacy expected)

**For all visualizations:** If image outputs are not available, provide detailed text descriptions including:
- Quantitative metrics (cell counts, percentages, densities)
- Spatial patterns (peripheral vs central, uniform vs heterogeneous, clustered vs diffuse)
- Color coding schemes used
- Key observations that would be visible in the image

## Validation Checkpoints:

✅ Loaded: 4 imaging files (H&E, CD8, Ki67, multiplex IF)
✅ H&E: High cellularity (~70-80%), necrosis present
✅ CD8: Low infiltration (~5-15 cells/mm²), immune exclusion
✅ Ki67: High proliferation index (~45-55%)
✅ Multiplex: TP53+ cells are highly proliferative (KI67+)
✅ Overall: Aggressive, immune-excluded tumor phenotype
