klassify 0.1.6

Classify chimeric reads based on unique kmer contents
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Orchestrate the end-to-end breakpoint→crossover pipeline.
//!
//! Purpose: Drive all stages from raw reads and parental references to a final,
//! high-confidence set of crossover pairs, wiring together internal modules and
//! external tools with sensible defaults, parallelism, and resumability.
//!
//! Typical inputs:
//! - F1 reads (FA/FASTQ)
//! - Parental read sets and/or combined parental reference (FASTA)
//! - Parameters: k, window size, clustering radius, distance limits, min supports,
//!   thread count, temp/output directories, and flags like --resume/--keep-temp.
//!
//! Main stages (enabled by flags and auto-skipped if outputs exist):
//! 1) Preflight: validate inputs, check external tools in PATH, create work dirs.
//! 2) Index: build or load parent-specific unique k-mer index.
//! 3) Label: annotate F1 reads with parental origin per k-mer (streaming).
//! 4) Breakpoint calling: invoke `breakpoint` module to emit per-read calls/TSV.
//! 5) (Optional) Split: cut reads at calls to segments for mapping/debugging.
//! 6) Mapping: align segments or full reads to parental refs (e.g. minimap2),
//!    produce PAF/BAM for coordinate normalization and QC.
//! 7) Site clustering: group breakpoint calls into left/right sites with support.
//! 8) Pairing: form candidate left↔right pairs and select non-overlapping pairs
//!    with a greedy chooser; write final crossover table.
//! 9) Reports: dump TSV/BED, basic metrics, and logs for each stage.
//!
//! Outputs:
//! - Breakpoint TSV per read, site BED/TSV, paired-region (crossover) table,
//!   optional segment FASTA/FASTQ and alignment files for inspection.
//!
//! Execution model:
//! - Parallel where safe (I/O bounded stages are batched; CPU heavy stages use
//!   `--jobs`). Each stage declares its products and can be resumed via stamps.
//! - Errors fail fast with context; partial results are left in temp for audit.
//!
//! Notes:
//! - Coordinates can be normalized to a chosen reference if alignments are present;
//!   otherwise the pipeline operates in read space.
//! - Parameters balance recall vs precision; presets are provided for long reads.
//!
//! Complexity: dominated by mapping and per-read scanning; other stages are near-linear
//! in the number of breakpoints and candidate pairs.

use anyhow::{anyhow, bail, Context, Result};
use clap::{Parser, ValueEnum};
use log::{debug, info};
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::SystemTime;
use which::which;

/// KLASSIFY pipeline (Rust wrapper) — ported from pipeline.py
#[derive(Parser, Debug)]
pub struct PipelineArgs {
    /// F1 reads FASTA
    f1_reads: PathBuf,
    /// Parental reads FASTA
    parent_reads: PathBuf,
    /// Combined parental reference FASTA
    parents_ref: PathBuf,

    /// Working directory (default: .)
    #[arg(long, default_value = ".")]
    workdir: PathBuf,

    /// Threads for minimap2 (-t)
    #[arg(long, default_value_t = 32)]
    threads_minimap2: usize,

    /// Threads for samtools sort (-@)
    #[arg(long, default_value_t = 8)]
    threads_sort: usize,

    /// Minimap2 preset
    #[arg(long, value_enum, default_value_t = Preset::MapHifi)]
    preset: Preset,

    /// Overwrite/redo steps even if outputs exist
    #[arg(long)]
    force: bool,
}

#[derive(Copy, Clone, Debug, ValueEnum)]
enum Preset {
    #[value(name = "map-hifi")]
    MapHifi,
    #[value(name = "map-ont")]
    MapOnt,
}

impl Preset {
    fn as_str(&self) -> &'static str {
        match self {
            Preset::MapHifi => "map-hifi",
            Preset::MapOnt => "map-ont",
        }
    }
}

/// Run the KLASSIFY pipeline. This is useful for small runs. For larger datasets, break the steps
/// into separate commands and use faSplit to split the reads into smaller chunks to increase level
/// of parallelism.
pub fn pipeline(args: PipelineArgs) -> Result<()> {
    // sanity checks
    for p in [&args.f1_reads, &args.parent_reads, &args.parents_ref] {
        if !p.exists() {
            bail!("Input not found: {}", p.display());
        }
    }

    for tool in ["klassify", "minimap2", "samtools"] {
        check_tool(tool)?;
    }

    let work = fs::canonicalize(&args.workdir).unwrap_or_else(|_| args.workdir.clone());
    mkdir(&work)?;

    // paths
    let f1_out_dir = work.join("f1_classify");
    let parent_out_dir = work.join("parent_classify");
    mkdir(&f1_out_dir)?;
    mkdir(&parent_out_dir)?;

    let kmers_bc = work.join("kmers.bc");

    let f1_filtered_tsv = work.join("f1_classify.filtered.tsv");
    let parent_filtered_tsv = work.join("parent_classify.filtered.tsv");

    let f1_extracted_fa = work.join("f1_classify.fa");
    let parent_extracted_fa = work.join("parent_classify.fa");

    let f1_bam = work.join("f1_classify.bam");
    let parent_bam = work.join("parent_classify.bam");

    let regions_tsv = work.join("f1_classify.regions.tsv");
    let regions_fa = work.join("f1_classify.regions.fasta");
    let roi_bam = work.join("f1_classify.roi.bam");
    let roi_tsv = work.join("f1_classify.roi.tsv");
    let paired_regions = work.join("f1_classify.roi.paired.regions"); // kept to mirror Python

    // 1) Build unique k-mers from parental genomes
    if up_to_date(&[&args.parents_ref], &[&kmers_bc]) && !args.force {
        info!("Found kmers DB, skipping build: {}", kmers_bc.display());
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("build"),
                args.parents_ref.as_os_str(),
                OsStr::new("-o"),
                kmers_bc.as_os_str(),
            ],
        )?;
    }

    // 2) Classify F1, extract chimeric reads, map to parents reference
    if up_to_date(&[&kmers_bc, &args.f1_reads], &[&f1_filtered_tsv]) && !args.force {
        info!("F1 classify already done: {}", f1_filtered_tsv.display());
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("classify"),
                kmers_bc.as_os_str(),
                args.f1_reads.as_os_str(),
                OsStr::new("-o"),
                OsStr::new("f1_classify"),
            ],
        )?;
    }

    if up_to_date(&[&f1_filtered_tsv, &args.f1_reads], &[&f1_extracted_fa]) && !args.force {
        info!("F1 extract already done: {}", f1_extracted_fa.display());
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("extract"),
                f1_filtered_tsv.as_os_str(),
                args.f1_reads.as_os_str(),
                OsStr::new("-o"),
                f1_extracted_fa.as_os_str(),
            ],
        )?;
    }

    if up_to_date(&[&f1_extracted_fa, &args.parents_ref], &[&f1_bam]) && !args.force {
        info!("F1 alignment already exists: {}", f1_bam.display());
    } else {
        run_pipe(
            &work,
            (
                "minimap2",
                vec![
                    OsStr::new("-t"),
                    OsStr::new(&args.threads_minimap2.to_string()),
                    OsStr::new("-ax"),
                    OsStr::new(args.preset.as_str()),
                    OsStr::new("--eqx"),
                    OsStr::new("--secondary=no"),
                    args.parents_ref.as_os_str(),
                    f1_extracted_fa.as_os_str(),
                ],
            ),
            (
                "samtools",
                vec![
                    OsStr::new("sort"),
                    OsStr::new("-@"),
                    OsStr::new(&args.threads_sort.to_string()),
                    OsStr::new("-o"),
                    f1_bam.as_os_str(),
                ],
            ),
        )?;
    }

    // 3) Classify parent reads (control), extract, map
    if up_to_date(&[&kmers_bc, &args.parent_reads], &[&parent_filtered_tsv]) && !args.force {
        info!(
            "Parent classify already done: {}",
            parent_filtered_tsv.display()
        );
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("classify"),
                kmers_bc.as_os_str(),
                args.parent_reads.as_os_str(),
                OsStr::new("-o"),
                OsStr::new("parent_classify"),
            ],
        )?;
    }

    if up_to_date(
        &[&parent_filtered_tsv, &args.parent_reads],
        &[&parent_extracted_fa],
    ) && !args.force
    {
        info!(
            "Parent extract already done: {}",
            parent_extracted_fa.display()
        );
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("extract"),
                parent_filtered_tsv.as_os_str(),
                args.parent_reads.as_os_str(),
                OsStr::new("-o"),
                parent_extracted_fa.as_os_str(),
            ],
        )?;
    }

    if up_to_date(&[&parent_extracted_fa, &args.parents_ref], &[&parent_bam]) && !args.force {
        info!("Parent alignment already exists: {}", parent_bam.display());
    } else {
        run_pipe(
            &work,
            (
                "minimap2",
                vec![
                    OsStr::new("-t"),
                    OsStr::new(&args.threads_minimap2.to_string()),
                    OsStr::new("-ax"),
                    OsStr::new(args.preset.as_str()),
                    OsStr::new("--eqx"),
                    OsStr::new("--secondary=no"),
                    args.parents_ref.as_os_str(),
                    parent_extracted_fa.as_os_str(),
                ],
            ),
            (
                "samtools",
                vec![
                    OsStr::new("sort"),
                    OsStr::new("-@"),
                    OsStr::new(&args.threads_sort.to_string()),
                    OsStr::new("-o"),
                    parent_bam.as_os_str(),
                ],
            ),
        )?;
    }

    // 4) Regions present in F1 but NOT parent (control)
    if up_to_date(&[&f1_bam, &parent_bam], &[&regions_tsv]) && !args.force {
        info!("Regions TSV already exists: {}", regions_tsv.display());
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("regions"),
                f1_bam.as_os_str(),
                parent_bam.as_os_str(),
            ],
        )?;
    }

    // 5) Refine the breakpoints (extract BAM segments overlapping regions, call breakpoints)
    let split_output = work.join("f1_classify.regions.split.fasta");
    if up_to_date(&[&regions_tsv, &f1_bam], &[&regions_fa, &split_output]) && !args.force {
        info!("Regions FASTA already exists: {}", regions_fa.display());
    } else {
        run(
            &work,
            "klassify",
            &[
                OsStr::new("extract-bam"),
                regions_tsv.as_os_str(),
                f1_bam.as_os_str(),
            ],
        )?;
        // klassify decides names internally; next call uses regions_fa
        run(
            &work,
            "klassify",
            &[
                OsStr::new("breakpoint"),
                kmers_bc.as_os_str(),
                regions_fa.as_os_str(),
            ],
        )?;
    }

    // Remap the split reads to parents to get a crisp ROI BAM
    if up_to_date(&[&split_output], &[&roi_bam]) && !args.force {
        info!("ROI BAM already exists: {}", roi_bam.display());
    } else {
        run_pipe(
            &work,
            (
                "minimap2",
                vec![
                    OsStr::new("-t"),
                    OsStr::new(&args.threads_minimap2.to_string()),
                    OsStr::new("-ax"),
                    OsStr::new(args.preset.as_str()),
                    OsStr::new("--eqx"),
                    OsStr::new("--secondary=no"),
                    args.parents_ref.as_os_str(),
                    split_output.as_os_str(),
                ],
            ),
            (
                "samtools",
                vec![
                    OsStr::new("sort"),
                    OsStr::new("-@"),
                    OsStr::new(&args.threads_sort.to_string()),
                    OsStr::new("-o"),
                    roi_bam.as_os_str(),
                ],
            ),
        )?;
    }

    // Cluster paired regions (capture stdout into ROI TSV)
    if up_to_date(&[&roi_bam], &[&roi_tsv]) && !args.force {
        info!("Paired regions TSV already exists: {}", roi_tsv.display());
    } else {
        run_to_file(
            &work,
            "klassify",
            &[OsStr::new("cluster-pairs"), roi_bam.as_os_str()],
            &roi_tsv,
        )?;
    }

    info!("Done.");
    info!(
        "Key outputs:\n  k-mers:          {}\n  F1 BAM:          {}\n  Parent BAM:      {}\n  Regions TSV:     {}\n  Regions FASTA:   {}\n  ROI BAM:         {}\n  ROI TSV:         {}\n  Paired Regions:  {}",
        kmers_bc.display(),
        f1_bam.display(),
        parent_bam.display(),
        regions_tsv.display(),
        regions_fa.display(),
        roi_bam.display(),
        roi_tsv.display(),
        paired_regions.display()
    );

    Ok(())
}

// ---------- helpers ----------

fn mkdir(p: &Path) -> Result<()> {
    if !p.exists() {
        fs::create_dir_all(p).with_context(|| format!("Failed to create {}", p.display()))?;
    }
    Ok(())
}

fn check_tool(tool: &str) -> Result<()> {
    match which(tool) {
        Ok(path) => {
            debug!("Found tool {} at {}", tool, path.display());
            Ok(())
        }
        Err(_) => bail!("Required tool not found in PATH: {}", tool),
    }
}

/// Return true if *all outputs* exist and are newer or equal to *all inputs*.
fn up_to_date(inputs: &[&Path], outputs: &[&Path]) -> bool {
    if outputs.is_empty() {
        return false;
    }
    let mut in_times = Vec::new();
    for i in inputs {
        match fs::metadata(i).and_then(|m| m.modified()) {
            Ok(t) => in_times.push(t),
            Err(_) => return false, // missing input? treat as not up-to-date to be safe
        }
    }
    let mut out_times = Vec::new();
    for o in outputs {
        match fs::metadata(o).and_then(|m| m.modified()) {
            Ok(t) => out_times.push(t),
            Err(_) => return false, // missing output -> not up to date
        }
    }
    if in_times.is_empty() || out_times.is_empty() {
        return false;
    }
    let max_in = in_times.into_iter().max().unwrap_or(SystemTime::UNIX_EPOCH);
    let min_out = out_times
        .into_iter()
        .min()
        .unwrap_or(SystemTime::UNIX_EPOCH);
    min_out >= max_in
}

fn run(cwd: &Path, program: &str, args: &[&OsStr]) -> Result<()> {
    info!("$ {} {}", program, join_args(args));
    let status = Command::new(program)
        .args(args)
        .current_dir(cwd)
        .status()
        .with_context(|| format!("Failed to spawn {}", program))?;
    if !status.success() {
        bail!("Command failed (exit {}): {}", status, program);
    }
    Ok(())
}

fn run_pipe(cwd: &Path, cmd1: (&str, Vec<&OsStr>), cmd2: (&str, Vec<&OsStr>)) -> Result<()> {
    info!(
        "$ {} {} | {} {}",
        cmd1.0,
        join_args(&cmd1.1),
        cmd2.0,
        join_args(&cmd2.1)
    );
    // first: spawn cmd1 with stdout piped
    let mut p1 = Command::new(cmd1.0)
        .args(&cmd1.1)
        .current_dir(cwd)
        .stdout(Stdio::piped())
        .spawn()
        .with_context(|| format!("Failed to spawn {}", cmd1.0))?;

    // second: spawn cmd2 with stdin from p1.stdout
    let stdout1 = p1
        .stdout
        .take()
        .ok_or_else(|| anyhow!("Failed to take stdout from {}", cmd1.0))?;
    let p2 = Command::new(cmd2.0)
        .args(&cmd2.1)
        .current_dir(cwd)
        .stdin(stdout1)
        .status()
        .with_context(|| format!("Failed to run {}", cmd2.0))?;

    // wait for p1 to complete
    let status1 = p1
        .wait()
        .with_context(|| format!("Failed to wait for {}", cmd1.0))?;

    if !status1.success() {
        bail!("Upstream command failed (exit {}): {}", status1, cmd1.0);
    }
    if !p2.success() {
        bail!("Downstream command failed (exit {}): {}", p2, cmd2.0);
    }
    Ok(())
}

fn run_to_file(cwd: &Path, program: &str, args: &[&OsStr], out_path: &Path) -> Result<()> {
    info!("$ {} {} > {}", program, join_args(args), out_path.display());
    let mut child = Command::new(program)
        .args(args)
        .current_dir(cwd)
        .stdout(Stdio::piped())
        .spawn()
        .with_context(|| format!("Failed to spawn {}", program))?;

    let mut reader = child
        .stdout
        .take()
        .ok_or_else(|| anyhow!("Failed to capture stdout from {}", program))?;

    let mut writer = BufWriter::new(
        File::create(out_path).with_context(|| format!("Cannot create {}", out_path.display()))?,
    );

    std::io::copy(&mut reader, &mut writer)
        .with_context(|| format!("Failed to write {}", out_path.display()))?;
    writer.flush().ok();

    let status = child
        .wait()
        .with_context(|| format!("Failed to wait for {}", program))?;
    if !status.success() {
        bail!("Command failed (exit {}): {}", status, program);
    }
    Ok(())
}

fn join_args(args: &[&OsStr]) -> String {
    args.iter()
        .map(|a| a.to_string_lossy().into_owned())
        .collect::<Vec<_>>()
        .join(" ")
}