Files
MSI_Julia_CNN_Release/run_full_pipeline.sh
T

114 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# =============================================================================
# run_full_pipeline.sh -- seven-stage MSI pipeline (one or many datasets)
#
# bash run_full_pipeline.sh # every dataset under ai_data/
# bash run_full_pipeline.sh Datura_leaf # a single dataset
# MSI_K=5 bash run_full_pipeline.sh Datura_leaf
#
# Each dataset lives in ai_data/<name>/ and must contain exactly one
# .imzML file and its sibling .ibd binary (optional ROI mask .png).
#
# Stages: 1 Julia preprocessing -> 2 SimCLR -> 3 K sweep -> 4 t-SNE + K-Means
# -> 5 quantitative validation -> 6 per-cluster galleries
# -> 7 mean / per-cluster mass spectra.
# =============================================================================
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DATA_ROOT="${MSI_DATA_ROOT:-$ROOT/ai_data}"
ENV_DIR="$ROOT/environment"
# Native runs: prefer a repo-local venv/ if one exists (ignored inside Docker).
if [ -x "$ROOT/venv/bin/python" ]; then
export PATH="$ROOT/venv/bin:$PATH"
fi
if [ ! -d "$DATA_ROOT" ]; then
echo "ERROR: dataset root '$DATA_ROOT' not found." >&2
echo " Create it and add ai_data/<name>/<file>.imzML (+ .ibd)." >&2
exit 1
fi
# Which datasets to run (explicit args, else every folder holding an .imzML)
if [ "$#" -gt 0 ]; then
DATASETS=("$@")
else
DATASETS=()
while IFS= read -r name; do
[ -n "$name" ] && DATASETS+=("$name")
done < <(for d in "$DATA_ROOT"/*/; do
[ -d "$d" ] || continue
# Case-insensitive match (.imzML / .imzml / .IMZML); `find -iname` avoids the
# glob-pair pitfall where a non-matching pattern makes `ls` return non-zero.
if [ -n "$(find "$d" -maxdepth 1 -type f -iname '*.imzml' -print -quit 2>/dev/null)" ]; then
basename "$d"
fi
done | sort)
fi
if [ "${#DATASETS[@]}" -eq 0 ]; then
echo "ERROR: no datasets with an .imzML found under '$DATA_ROOT'." >&2
echo " Each dataset must be in its own subfolder:" >&2
echo " $DATA_ROOT/<name>/<file>.imzML (+ <file>.ibd)" >&2
if [ -n "$(find "$DATA_ROOT" -maxdepth 1 -type f -iname '*.imzml' -print -quit 2>/dev/null)" ]; then
echo " Note: .imzML file(s) were found directly in '$DATA_ROOT' (loose files are ignored)." >&2
echo " Move each into its own subfolder, e.g.:" >&2
echo " mkdir -p $DATA_ROOT/<name> && mv $DATA_ROOT/*.imzML $DATA_ROOT/*.ibd $DATA_ROOT/<name>/" >&2
fi
exit 1
fi
echo "Data root: $DATA_ROOT"
echo "Datasets : ${DATASETS[*]}"
for ds in "${DATASETS[@]}"; do
ds_dir="$DATA_ROOT/$ds"
if [ ! -d "$ds_dir" ]; then
echo "ERROR: '$ds_dir' not found." >&2
exit 1
fi
echo ""
echo "########## $ds ##########"
echo "--- Stage 1/7: Julia preprocessing ---"
julia --threads auto --project="$ENV_DIR" \
"$ROOT/scripts_julia/julia_msi_bridge.jl" "$ds_dir" "$ds"
echo "--- Stage 2/7: SimCLR training ---"
python3 "$ROOT/scripts_python/CNN_proof_1.py" "$ds"
echo "--- Stage 3/7: clustering evaluation (K sweep 2..20) ---"
python3 "$ROOT/scripts_python/evaluate_clustering.py" "$ds"
# K for stages 4/5: explicit MSI_K wins, else the Silhouette optimum from Stage 3.
K="${MSI_K:-}"
if [ -z "$K" ]; then
kfile="$ROOT/outputs/$ds/best_k_results.csv"
K="$(awk -F, '$1=="best_k_silhouette"{print $2}' "$kfile" 2>/dev/null | tr -d '[:space:]')"
fi
if [ -n "$K" ]; then
echo "--- Stage 4/7: t-SNE + K-Means (K=$K) ---"
python3 "$ROOT/scripts_python/graph_tsne_msi.py" "$ds" "$K"
echo "--- Stage 5/7: quantitative validation (K=$K) ---"
python3 "$ROOT/scripts_python/quantitative_validation.py" "$ds" "$K"
echo "--- Stage 6/7: per-cluster galleries ---"
python3 "$ROOT/scripts_python/visualize_clusters.py" "$ds"
else
echo "WARNING: could not determine best K for '$ds'; skipping stages 4-6." >&2
fi
echo "--- Stage 7/7: mass spectra ---"
julia --project="$ENV_DIR" \
"$ROOT/scripts_julia/plot_mass_spectra.jl" "$ds" "$ds_dir"
echo "########## $ds done ##########"
done
echo ""
echo "All done. Outputs are under $ROOT/outputs/"