-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhandlers.rs
More file actions
70 lines (58 loc) · 1.9 KB
/
handlers.rs
File metadata and controls
70 lines (58 loc) · 1.9 KB
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
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
use anyhow::{Context, Result};
use clap::ArgMatches;
use gtars_core::models::RegionSet;
use gtars_genomicdist::consensus::consensus;
pub fn run_consensus(matches: &ArgMatches) -> Result<()> {
let bed_paths: Vec<&String> = matches
.get_many::<String>("beds")
.expect("--beds is required")
.collect();
let min_count: u32 = matches
.get_one::<String>("min-count")
.unwrap()
.parse()
.context("--min-count must be a positive integer")?;
let output_path = matches.get_one::<String>("output");
// Load all BED files
let sets: Vec<RegionSet> = bed_paths
.iter()
.map(|p| {
RegionSet::try_from(p.as_str())
.map_err(|e| anyhow::anyhow!("Failed to load BED file {}: {}", p, e))
})
.collect::<Result<Vec<_>>>()?;
eprintln!("Computing consensus across {} BED files...", sets.len());
let regions = consensus(&sets);
// Filter by min-count
let filtered: Vec<_> = regions
.iter()
.filter(|r| r.count >= min_count)
.collect();
eprintln!(
"{} consensus regions ({} after --min-count {} filter)",
regions.len(),
filtered.len(),
min_count,
);
match output_path {
Some(p) => {
let mut file = File::create(Path::new(p))
.with_context(|| format!("Failed to create output file: {}", p))?;
for r in &filtered {
writeln!(file, "{}\t{}\t{}\t{}", r.chr, r.start, r.end, r.count)?;
}
eprintln!("Output written to {}", p);
}
None => {
let stdout = io::stdout();
let mut out = stdout.lock();
for r in &filtered {
writeln!(out, "{}\t{}\t{}\t{}", r.chr, r.start, r.end, r.count)?;
}
}
}
Ok(())
}