# BRIDGE SCRIPT: Connects JuliaMSI framework to the CNN Preprocessing pipeline. # This script is designed to run within the Docker container. # Includes Benchmarking and Memory Profiling. using Pkg juliamsi_path = "/opt/JuliaMSI" push!(LOAD_PATH, juliamsi_path) # Load JuliaMSI core files directly as per documentation include(joinpath(juliamsi_path, "src", "julia_mzML_imzML.jl")) include(joinpath(juliamsi_path, "src", "mask.jl")) using Statistics, Images, Interpolations, BenchmarkTools # CONFIGURATION data_path = "/app/data/Leaf.imzML" mask_path = "/app/data/region_of_interest.png" output_folder = "/app/datos_para_ai" img_size = (224, 224) mkpath(output_folder) println("--- JULIA PREPROCESSING BENCHMARK ---") println("Dataset: ", basename(data_path)) if isfile(data_path) println("File Size: ", round(filesize(data_path) / 1024^2, digits=2), " MB") end function run_preprocessing() # 1. LOAD DATA println("Step 1: Ingesting MSI Data...") # OpenMSIData: Core ingestion function from the JuliaMSI framework. # Citation: Sierra-Álvarez et al. (2025). data = OpenMSIData(data_path) # 2. EXTRACT AVERAGE SPECTRUM (Inside Mask/ROI) println("Step 2: Calculating ROI Average Spectrum...") mzs, avg_ints = get_average_spectrum(data, mask_path=mask_path) threshold = maximum(avg_ints) * 0.02 peak_indices = findall(x -> x > threshold, avg_ints) println("Found $(length(peak_indices)) significant ions.") # 3. TRiq NORMALIZATION & RESIZING println("Step 3: Exporting Normalized Ion Maps...") for idx in peak_indices mz_val = mzs[idx] if mz_val < 50.0 || mz_val > 700.0 continue end # Extract slice: Using a ±0.05 Da window. # This tolerance is standard for low-resolution Ion Trap instruments (LCQ Fleet) # to ensure the entire peak centroid and its isotopic distribution are captured. raw_slice = get_mz_slice(data, mz_val, 0.05, mask_path=mask_path) # TrIQ Normalization (Threshold Intensity Quantization) # Optimized for resolving low-abundance features in the presence of hotspots. # Citation: Rosas-Román et al. (2024). vals = filter(x -> x > 0 && isfinite(x), raw_slice) if isempty(vals) continue end q = quantile(vals, 0.98) q = q == 0 ? maximum(vals) : q enhanced = clamp.(raw_slice ./ q, 0.0, 1.0) # Resize for CNN img_resized = imresize(enhanced, img_size) # Save to intermediate folder filename = "ion_$(round(mz_val, digits=2)).png" save(joinpath(output_folder, filename), Gray.(img_resized)) end end # Execution with Benchmarking @time run_preprocessing() println("--- BENCHMARK COMPLETE ---")