Files
MSI_Julia_CNN_Release/scripts_julia/graph_cluster_spectra.jl
T

71 lines
2.1 KiB
Julia

using MSI_src
using CSV, DataFrames
using Plots
using Statistics
using Plots.Measures
# 1. Data loading
data_path = "Leaf.imzML"
mask_path = "region_of_interest.png"
clusters_path = "msi_clusters_results.csv"
println("Loading data...")
data = OpenMSIData(data_path)
clusters_df = CSV.read(clusters_path, DataFrame)
# 2. Get Average Spectrum (Single reference)
mzs_avg, ints_avg = MSI_src.get_average_spectrum(data, mask_path=mask_path)
# 3. Configure base plot (Thin, clean gray line)
p = plot(mzs_avg, ints_avg,
linecolor = :darkgray,
linewidth = 0.8,
label = "Average Spectrum (Reference)",
xlabel = "m/z",
ylabel = "Relative Intensity",
title = "Cluster Mapping on Isotopic Pattern",
size = (1200, 600),
legend = :outerright,
grid = false, # Clean article style
framestyle = :box, # Full frame
left_margin = 15mm,
bottom_margin = 15mm
)
# 4. Distinctive color palette
cluster_colors = palette(:tab10)
# 5. Color specific peaks (Sticks)
println("Highlighting chemical clusters...")
for cluster_id in sort(unique(clusters_df.cluster))
sub_df = filter(row -> row.cluster == cluster_id, clusters_df)
label_ready = false
for target_mz in sub_df.mz
# Find the index of the real peak in the average spectrum
idx = argmin(abs.(mzs_avg .- target_mz))
# Tolerance to center the color on the real peak
if abs(mzs_avg[idx] - target_mz) < 0.1
m = mzs_avg[idx]
i = ints_avg[idx]
# Draw the cluster "stick" over the base spectrum
plot!(p, [m, m], [0, i],
linecolor = cluster_colors[cluster_id + 1],
linewidth = 1.2,
alpha = 1.0, # Fully opaque to stand out
label = label_ready ? "" : "Cluster $cluster_id"
)
label_ready = true
end
end
end
# 6. Save with high resolution
savefig("espectro_msi_paper_style.png")
println("Final plot saved as 'espectro_msi_paper_style.png'")
savefig("espectro_msi_clusters_julia.png")
println("Plot saved as 'espectro_msi_clusters_julia.png'")