Metadata-Version: 2.5
Name: patchkori
Version: 0.1.0
Summary: Modular open-source bioinformatics platform for large-scale pyramidal biological imaging
Author: Jubayer
License: MIT
Keywords: bioinformatics,gradio,ome-tiff,pathology,qptiff,whole-slide-imaging,zarr
Requires-Python: >=3.10
Requires-Dist: gradio<6.0.0,>=5.0.0
Requires-Dist: imagecodecs>=2024.6.1
Requires-Dist: numpy>=1.26.0
Requires-Dist: pillow>=10.3.0
Requires-Dist: pydantic>=2.7.0
Requires-Dist: tifffile>=2024.5.22
Requires-Dist: typer>=0.12.0
Requires-Dist: zarr>=2.18.0
Provides-Extra: dev
Requires-Dist: mkdocs-material>=9.5.0; extra == 'dev'
Requires-Dist: pytest>=8.2.0; extra == 'dev'
Requires-Dist: ruff>=0.5.0; extra == 'dev'
Description-Content-Type: text/markdown

<div align="center">
  <img src="assets/logo.svg" alt="PatchKori Logo" width="96" height="96" />
  <h1>PatchKori</h1>
  <p><strong>Bioinformatics Patch Kits Hub for Gigapixel Biological Image Datasets</strong></p>
  <p><em>Out-of-core, memory-safe pyramidal tile extraction & analysis platform (OME-TIFF, QPTIFF, Pyramidal TIFF)</em></p>
</div>

---

## 🔬 What is PatchKori?

Whole-slide biological and multiplexed fluorescence images (**OME-TIFF, QPTIFF, Pyramidal TIFF, SVS, OME-Zarr**) frequently exceed **10 GB to 100+ GB per image**. Standard web tools and image viewers crash trying to load gigapixel planes into memory.

**PatchKori** solves this with a **zero-copy, out-of-core streaming architecture**:
- **Instant Header Inspection (<50ms):** Reads multi-resolution pyramid levels ($L_0 \dots L_N$) and channels without reading image pixels into RAM.
- **Interactive Low-Res Preview Canvas:** Displays an eye-soothing, colorblind-safe thumbnail preview. Draw an ROI box with your cursor or enter exact coordinates.
- **Direct-to-Disk Chunk Slicing:** Uses Zarr-backed `tifffile` tile streaming to extract only the compressed tiles you need directly to your local drive without memory spikes.
- **Extensible Hub Architecture:** Easily drop in future bioinformatics tools (e.g. segmentation, stain normalization) as self-contained plugins.

---

## 🚀 Quickstart: How to Use PatchKori

### 1. Installation

Clone and install PatchKori locally (Python 3.10+):

```bash
git clone git@github.com:jubayer98/PatchKori.git
cd PatchKori
pip install -e ".[dev]"
```

### 2. Launch the Web Interface

Start the local web application with one command:

```bash
patchkori launch
```

Your default browser will open automatically to **`http://127.0.0.1:7860`**.

---

## 🖥️ How to Use the Web Interface (Step-by-Step)

```
┌─────────────────────────────────┬─────────────────────────────────┬─────────────────────────────────┐
│ 1. Configuration & Tiling       │ 2. Interactive Canvas           │ 3. Execution & Gallery          │
│ • Local File Path               │ • [📐 Full Level ROI]           │ • Output Directory              │
│ • Optional markerlist.txt       │ • [✏️ Apply Hand-Drawn Box]     │ • Format (ome.tif / png / zarr) │
│ • [🧪 Load Sample Dataset]      │ • Thumbnail Preview             │ • [✂️ Run Out-of-Core Crop]     │
│ • Crop / Grid Tile Parameters   │ • Pyramid Level Table (L0..LN)  │ • Live Progress Feedback        │
│ • Target Level & Channels       │ • Full Channel Name Badges      │ • Instant Preview Gallery       │
└─────────────────────────────────┴─────────────────────────────────┴─────────────────────────────────┘
```

1. **Load Your Image:**
   - Enter your **Local File Path** (e.g. `/path/to/my_slide.qptiff` or `.ome.tif`).
   - *(Optional)* Provide a **`markerlist.txt`** file (one channel name per line) to automatically name multiplex channels. If omitted, channel names are read from OME-XML or fallback to index numbers `[0] Channel 0`, `[1] Channel 1`...
   - Click **🔍 Inspect Pyramid** (or click **🧪 Load Sample Dataset** to test with the built-in 4-level pyramidal image).
2. **Select Region of Interest (ROI):**
   - **Hand-Drawn Box:** Select the brush on the preview canvas, draw over your desired region, and click **✏️ Apply Hand-Drawn Box**.
   - **Full Slide:** Click **📐 Full Level ROI** to select the entire slide.
   - **Numeric Tuning:** Fine-tune `X`, `Y`, `Width`, and `Height` on the left.
3. **Choose Target Level & Channels:**
   - **Target Level:** Choose from $L_0$ (highest resolution, 1.0×) down to $L_N$ (lowest resolution).
   - **Channels Dropdown:** Pick any subset of markers (e.g. `[0] DAPI`, `[2] PanCK`). Leaving it empty extracts all channels.
4. **Choose Crop Mode:**
   - **Single Bounding-Box ROI:** Extracts a single cropped patch of the chosen region.
   - **Sliding-Window Tile Grid:** Slices the ROI into regular tiles (e.g. $256 \times 256$ or $512 \times 512$ with customizable overlap) for deep learning training datasets.
5. **Run Crop:**
   - Click **✂️ Run Out-of-Core Crop**.
   - Crops are written directly to your chosen local folder (`outputs/crops`) with instant visual previews in the gallery.

---

## ⚡ Headless CLI Usage (For HPC, Clusters & Pipelines)

You can run PatchKori headless without opening the browser:

```bash
# Slicing an ROI directly from the terminal
patchkori crop /data/wsi/sample.qptiff \
    --level 0 \
    --x 1024 --y 2048 --width 512 --height 512 \
    --format ome.tif \
    --output-dir ./results

# Sliding-window tiling for ML datasets
patchkori crop /data/wsi/sample.qptiff \
    --level 1 \
    --x 0 --y 0 --width 4096 --height 4096 \
    --mode tile_grid --tile-size 512 --overlap 64 \
    --format png \
    --output-dir ./training_tiles
```

---

## 🔌 Adding Future Bioinformatics Tools

Adding a 2nd or 3rd bioinformatics tool requires **zero modifications** to existing core code:

1. Add pure algorithmic logic to `src/patchkori/core/<my_tool>/`.
2. Create `src/patchkori/ui/tools/<my_tool>.py`:

```python
import gradio as gr
from patchkori.ui.registry import BaseBioTool, ToolRegistry

@ToolRegistry.register
class MyBioTool(BaseBioTool):
    tool_id = "my-tool"
    title = "My Bio Tool"
    icon = "🔬"
    description = "Self-contained bioinformatics tool plugin."
    order = 20

    def build_ui(self, is_demo_mode: bool = False) -> None:
        gr.Markdown("### Custom Tool Workspace")
```

3. Import it in `src/patchkori/ui/tools/__init__.py`. It automatically appears as a new tab in the Hub!

---

## 🛡️ License

MIT License. Developed for open-source biological imaging and computational pathology research.
