uno-ms 0.1.0

MS1-only peptide and protein identification from LC-MS features, with target-decoy FDR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Public CNN workflow. Only training launches optional Python; inspection and
//! independent evaluation use exactly the Rust predictor used during search.
use anyhow::{bail, Context, Result};
use clap::{Args, Subcommand, ValueEnum};
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, HashSet};
use std::path::{Path, PathBuf};
use uno_core::RtModel;

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Train, inspect, predict, or evaluate a charge-aware IM sequence CNN.
    Im {
        #[command(subcommand)]
        command: crate::im_cli::ImCommand,
    },
    /// Train, inspect, or evaluate a retention-time CNN.
    Rt {
        #[command(subcommand)]
        command: RtCommand,
    },
    /// Two-condition DirectMS1Quant-style contrast (experimental) on native
    /// ms1searchpy tables or on uno per-run evidence.
    Quant(Box<crate::quant_cli::QuantArgs>),
}

#[derive(Debug, Subcommand)]
pub enum RtCommand {
    /// Adapt the bundled CNN, or explicitly train from scratch (requires uno-rt Python package).
    Train(TrainArgs),
    /// Inspect a CNN's architecture, normalization, and training provenance (no Python).
    Inspect { model: PathBuf },
    /// Evaluate independent, headered sequence/rt data using Rust inference (no Python).
    Evaluate(EvaluateArgs),
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum Units {
    Minutes,
    Seconds,
    Irt,
    Hi,
}
impl Units {
    fn name(self) -> &'static str {
        match self {
            Self::Minutes => "minutes",
            Self::Seconds => "seconds",
            Self::Irt => "irt",
            Self::Hi => "hi",
        }
    }
}

#[derive(Debug, Args)]
pub struct TrainArgs {
    /// Headered TSV: sequence, rt, optional run_id. One separation method per model.
    #[arg(long)]
    data: PathBuf,
    /// New model file (existing files are never overwritten).
    #[arg(long)]
    out: PathBuf,
    /// Explicit label units; multiple raw-time runs must first be normalized.
    #[arg(long, value_enum)]
    units: Units,
    /// Descriptive column/separation name, recorded with the model.
    #[arg(long)]
    method: String,
    /// Starting CNN; architecture and normalization are read from the file.
    #[arg(long, conflicts_with = "from_scratch")]
    init: Option<PathBuf>,
    /// Initialize a fresh CNN instead of adapting the bundled model.
    #[arg(long)]
    from_scratch: bool,
    /// Separate validation TSV; peptides must not overlap training or parent lineage.
    #[arg(long, conflicts_with = "holdout_run")]
    validation: Option<PathBuf>,
    /// Hold out this run_id; its peptides are also removed from other training runs.
    #[arg(long)]
    holdout_run: Option<String>,
    #[arg(long, default_value_t = 0)]
    seed: u32,
    #[arg(long, default_value_t = 40)]
    epochs: u32,
    /// Advanced JSON: lr, batch_size, patience, val_fraction, device, threads, architecture.
    #[arg(long)]
    config: Option<PathBuf>,
    /// Python interpreter with the optional uno-rt package installed.
    #[arg(long, default_value = "python3")]
    python: PathBuf,
}

#[derive(Debug, Args)]
pub struct EvaluateArgs {
    #[arg(long)]
    model: PathBuf,
    /// Independent TSV with sequence/rt headers, unused in training or model selection.
    #[arg(long)]
    data: PathBuf,
    #[arg(long, value_enum)]
    units: Units,
    /// Separate observations for affine calibration; cannot overlap evaluation peptides.
    #[arg(long)]
    calibration_data: Option<PathBuf>,
    /// Optional starting CNN to compare; requires separate calibration observations.
    #[arg(long, requires = "calibration_data")]
    baseline: Option<PathBuf>,
    /// Save sequence/observed_rt/predicted_rt for inspection or parity checks.
    #[arg(long)]
    out_predictions: Option<PathBuf>,
}

pub fn run(command: Command) -> Result<()> {
    let command = match command {
        Command::Rt { command } => command,
        Command::Im { command } => return crate::im_cli::run(command),
        Command::Quant(args) => return crate::quant_cli::run(*args),
    };
    match command {
        RtCommand::Train(args) => train(args),
        RtCommand::Inspect { model } => {
            let model = cnn(&model)?;
            let mut info = model.cnn_info().unwrap().clone();
            if let Some(metadata) = info["metadata"].as_object_mut() {
                if let Some(hashes) = metadata.remove("seen_peptide_hashes") {
                    metadata.insert(
                        "seen_peptide_count".into(),
                        json!(hashes.as_array().map_or(0, Vec::len)),
                    );
                }
            }
            println!("{}", serde_json::to_string_pretty(&info)?);
            Ok(())
        }
        RtCommand::Evaluate(args) => evaluate(args),
    }
}

fn train(args: TrainArgs) -> Result<()> {
    // The installed Python package may be from a different release. Always
    // adapt this binary's actual default when no starting model was selected.
    let bundled = if args.init.is_none() && !args.from_scratch {
        use std::io::Write;
        let mut file =
            tempfile::NamedTempFile::new().context("preparing bundled CNN for training")?;
        file.write_all(uno_core::rt::DEFAULT_RT_CNN)?;
        Some(file)
    } else {
        None
    };
    // Argument arrays avoid shell interpolation, including for paths/method names.
    let mut command = std::process::Command::new(&args.python);
    command
        .args(["-m", "uno_rt", "train", "--data"])
        .arg(&args.data)
        .arg("--out")
        .arg(&args.out)
        .arg("--units")
        .arg(args.units.name())
        .arg("--method")
        .arg(&args.method)
        .arg("--seed")
        .arg(args.seed.to_string())
        .arg("--epochs")
        .arg(args.epochs.to_string());
    if let Some(p) = args
        .init
        .as_deref()
        .or_else(|| bundled.as_ref().map(|f| f.path()))
    {
        command.arg("--init").arg(p);
    }
    if args.from_scratch {
        command.arg("--from-scratch");
    }
    if let Some(p) = args.validation {
        command.arg("--validation").arg(p);
    }
    if let Some(run) = args.holdout_run {
        command.arg("--holdout-run").arg(run);
    }
    if let Some(p) = args.config {
        command.arg("--config").arg(p);
    }
    let status = command.status().with_context(|| format!(
        "starting {}; install the optional trainer with `python -m pip install ./crates/uno-rt-train/python` and select that interpreter with --python",
        args.python.display()))?;
    if !status.success() {
        bail!("RT training failed ({status}); see the Python error above. The optional trainer is installed with `python -m pip install ./crates/uno-rt-train/python`; --python selects its interpreter");
    }
    // Verify the exported artifact with the production Rust loader as well.
    cnn(&args.out).context("training output failed Rust compatibility validation")?;
    Ok(())
}

fn cnn(path: &Path) -> Result<RtModel> {
    let model = RtModel::load(path)?;
    if model.cnn_info().is_none() {
        bail!("uno rt supports URTC CNNs only; legacy loaders remain available for search");
    }
    Ok(model)
}

#[derive(Debug)]
struct Observations {
    sequences: Vec<String>,
    rts: Vec<f64>,
}

fn read_data(path: &Path, units: Units) -> Result<Observations> {
    let mut reader = csv::ReaderBuilder::new().delimiter(b'\t').from_path(path)?;
    let headers = reader.headers()?.clone();
    if headers.iter().collect::<HashSet<_>>().len() != headers.len() {
        bail!("duplicate TSV headers");
    }
    let seq_col = headers
        .iter()
        .position(|h| h == "sequence")
        .context("TSV requires sequence header")?;
    let rt_col = headers
        .iter()
        .position(|h| h == "rt")
        .context("TSV requires rt header")?;
    let run_col = headers.iter().position(|h| h == "run_id");
    let mut groups = BTreeMap::<(String, String), Vec<f64>>::new();
    let mut runs = HashSet::new();
    for (i, record) in reader.records().enumerate() {
        let record = record?;
        let sequence = record[seq_col].trim();
        if sequence.is_empty()
            || sequence.len() > 50
            || !sequence
                .bytes()
                .all(|c| b"ACDEFGHIKLMNPQRSTVWY".contains(&c))
        {
            bail!(
                "{}:{}: require 1–50 canonical amino acids; variable modifications are unsupported",
                path.display(),
                i + 2
            );
        }
        let rt: f64 = record[rt_col]
            .trim()
            .parse()
            .with_context(|| format!("invalid RT at row {}", i + 2))?;
        if !rt.is_finite() || (matches!(units, Units::Minutes | Units::Seconds) && rt < 0.) {
            bail!("invalid RT at row {}", i + 2);
        }
        let run = run_col.map_or("", |c| record[c].trim());
        if run_col.is_some() && run.is_empty() {
            bail!("run_id must be populated at row {}", i + 2);
        }
        runs.insert(run.to_owned());
        groups
            .entry((sequence.to_owned(), run.to_owned()))
            .or_default()
            .push(rt);
    }
    if matches!(units, Units::Minutes | Units::Seconds) && runs.len() > 1 {
        bail!("normalize multiple runs to shared irt/hi units before pooling");
    }
    if groups.len() < 2 {
        bail!("need at least two observations");
    }
    let mut sequences = Vec::new();
    let mut rts = Vec::new();
    for ((sequence, _), mut values) in groups {
        values.sort_by(f64::total_cmp);
        let n = values.len();
        let rt = if n % 2 == 0 {
            values[n / 2 - 1] / 2. + values[n / 2] / 2.
        } else {
            values[n / 2]
        };
        sequences.push(sequence);
        rts.push(rt);
    }
    Ok(Observations { sequences, rts })
}

fn check_unseen(model: &RtModel, data: &Observations) -> Result<()> {
    let meta = &model.cnn_info().unwrap()["metadata"];
    if let Some(seen) = meta["seen_peptide_hashes"].as_array() {
        let hashes: HashSet<&str> = seen.iter().filter_map(Value::as_str).collect();
        if data
            .sequences
            .iter()
            .any(|s| hashes.contains(format!("{:x}", Sha256::digest(s.as_bytes())).as_str()))
        {
            bail!("evaluation peptides overlap this model's recorded training/validation lineage; provide independent data");
        }
    } else {
        log::warn!(
            "legacy model has no peptide provenance; evaluation independence cannot be checked"
        );
    }
    Ok(())
}

fn affine(predictions: &[f64], y: &[f64]) -> Result<(f64, f64)> {
    let n = y.len() as f64;
    let px = predictions.iter().sum::<f64>() / n;
    let py = y.iter().sum::<f64>() / n;
    let variance = predictions.iter().map(|p| (p - px).powi(2)).sum::<f64>();
    if variance <= 1e-12 {
        bail!("calibration predictions have no variance; add representative peptides");
    }
    let slope = predictions
        .iter()
        .zip(y)
        .map(|(p, y)| (p - px) * (y - py))
        .sum::<f64>()
        / variance;
    Ok((slope, py - slope * px))
}

fn predictions(
    model: &RtModel,
    data: &Observations,
    units: Units,
    calibration: Option<&Observations>,
) -> Result<Vec<f64>> {
    let (slope, intercept) = if let Some(calibration) = calibration {
        affine(
            &model.predict_many(&calibration.sequences),
            &calibration.rts,
        )?
    } else {
        let model_units = model.cnn_info().unwrap()["metadata"]["rt_units"]
            .as_str()
            .unwrap_or("unknown");
        match (model_units, units.name()) {
            (a, b) if a == b => (1., 0.),
            ("seconds", "minutes") => (1. / 60., 0.),
            ("minutes", "seconds") => (60., 0.),
            _ => bail!("model units are {model_units}, data units are {}; provide separate --calibration-data to compare these scales", units.name()),
        }
    };
    let result: Vec<f64> = model
        .predict_many(&data.sequences)
        .into_iter()
        .map(|p| slope * p + intercept)
        .collect();
    if result.iter().any(|p| !p.is_finite()) {
        bail!("nonfinite model predictions");
    }
    Ok(result)
}

pub(crate) fn metrics(y: &[f64], predictions: &[f64]) -> Value {
    let n = y.len() as f64;
    let mean = y.iter().sum::<f64>() / n;
    let mut errors: Vec<f64> = y
        .iter()
        .zip(predictions)
        .map(|(a, b)| (a - b).abs())
        .collect();
    let mse = errors.iter().map(|e| e * e).sum::<f64>() / n;
    let variance = y.iter().map(|y| (y - mean).powi(2)).sum::<f64>() / n;
    errors.sort_by(f64::total_cmp);
    let index = 0.95 * (errors.len() - 1) as f64;
    let low = index.floor() as usize;
    let high = index.ceil() as usize;
    json!({"observations": y.len(), "mae": errors.iter().sum::<f64>() / n, "rmse": mse.sqrt(),
        "r2": if variance > 0. { Some(1. - mse / variance) } else { None },
        "p95_absolute_error": errors[low] + (errors[high] - errors[low]) * index.fract()})
}

fn evaluate(args: EvaluateArgs) -> Result<()> {
    let data = read_data(&args.data, args.units)?;
    let calibration = args
        .calibration_data
        .as_ref()
        .map(|p| read_data(p, args.units))
        .transpose()?;
    if let Some(calibration) = &calibration {
        let held: HashSet<_> = data.sequences.iter().collect();
        if calibration.sequences.iter().any(|s| held.contains(s)) {
            bail!("calibration and evaluation peptides overlap");
        }
    }
    let model = cnn(&args.model)?;
    check_unseen(&model, &data)?;
    let predicted = predictions(&model, &data, args.units, calibration.as_ref())?;
    let mut report = json!({"units": args.units.name(), "calibrated": calibration.is_some(),
        "model": metrics(&data.rts, &predicted),
        "independence": "Checked against recorded lineage only; legacy/pretrained corpora may be unknown.",
        "interpretation": "RT error alone does not establish search quality; validate identifications and FDR before adoption."});
    if let Some(path) = &args.baseline {
        let baseline = cnn(path)?;
        check_unseen(&baseline, &data)?;
        report["baseline"] = metrics(
            &data.rts,
            &predictions(&baseline, &data, args.units, calibration.as_ref())?,
        );
    }
    if let Some(path) = args.out_predictions {
        let file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(path)?;
        let mut writer = csv::WriterBuilder::new().delimiter(b'\t').from_writer(file);
        writer.write_record(["sequence", "observed_rt", "predicted_rt"])?;
        for ((s, y), p) in data.sequences.iter().zip(&data.rts).zip(&predicted) {
            writer.write_record([s.as_str(), &y.to_string(), &p.to_string()])?;
        }
        writer.flush()?;
    }
    println!("{}", serde_json::to_string_pretty(&report)?);
    Ok(())
}