|
| 1 | +use std::process; |
| 2 | + |
| 3 | +use clap::{Arg, ArgAction, Command}; |
| 4 | + |
| 5 | +use rustynetics::granges::GRanges; |
| 6 | +use rustynetics::granges_table::OptionPrintStrand; |
| 7 | + |
| 8 | +mod common; |
| 9 | + |
| 10 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 11 | +enum InputFormat { |
| 12 | + Bed3, |
| 13 | + Bed6, |
| 14 | + Bed9, |
| 15 | + Table, |
| 16 | +} |
| 17 | + |
| 18 | +fn read_table_auto(path: Option<&str>) -> Result<GRanges, Box<dyn std::error::Error>> { |
| 19 | + let mut reader = common::open_reader(path)?; |
| 20 | + let mut granges = GRanges::default(); |
| 21 | + granges.read_table_all(&mut reader)?; |
| 22 | + Ok(granges) |
| 23 | +} |
| 24 | + |
| 25 | +fn import_input( |
| 26 | + path: Option<&str>, |
| 27 | + format: InputFormat, |
| 28 | +) -> Result<GRanges, Box<dyn std::error::Error>> { |
| 29 | + match format { |
| 30 | + InputFormat::Bed3 => { |
| 31 | + let mut reader = common::open_reader(path)?; |
| 32 | + let mut granges = GRanges::default(); |
| 33 | + granges.read_bed3(&mut reader)?; |
| 34 | + Ok(granges) |
| 35 | + } |
| 36 | + InputFormat::Bed6 => { |
| 37 | + let mut reader = common::open_reader(path)?; |
| 38 | + let mut granges = GRanges::default(); |
| 39 | + granges.read_bed6(&mut reader)?; |
| 40 | + Ok(granges) |
| 41 | + } |
| 42 | + InputFormat::Bed9 => { |
| 43 | + let mut reader = common::open_reader(path)?; |
| 44 | + let mut granges = GRanges::default(); |
| 45 | + granges.read_bed9(&mut reader)?; |
| 46 | + Ok(granges) |
| 47 | + } |
| 48 | + InputFormat::Table => read_table_auto(path), |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +fn export_output( |
| 53 | + granges: &GRanges, |
| 54 | + path: Option<&str>, |
| 55 | + format: InputFormat, |
| 56 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 57 | + let mut writer = common::open_writer(path)?; |
| 58 | + match format { |
| 59 | + InputFormat::Bed3 => granges.write_bed3(&mut writer)?, |
| 60 | + InputFormat::Bed6 => granges.write_bed6(&mut writer)?, |
| 61 | + InputFormat::Bed9 => granges.write_bed9(&mut writer)?, |
| 62 | + InputFormat::Table => granges.write_table(&mut writer, &[&OptionPrintStrand(true)])?, |
| 63 | + } |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +fn main() { |
| 68 | + let matches = Command::new("bed-remove-overlaps") |
| 69 | + .about("Remove regions that overlap a set of inadmissible intervals") |
| 70 | + .arg( |
| 71 | + Arg::new("input-format") |
| 72 | + .long("input-format") |
| 73 | + .default_value("bed3") |
| 74 | + .value_parser(["bed3", "bed6", "bed9", "table"]), |
| 75 | + ) |
| 76 | + .arg( |
| 77 | + Arg::new("verbose") |
| 78 | + .short('v') |
| 79 | + .long("verbose") |
| 80 | + .action(ArgAction::Count), |
| 81 | + ) |
| 82 | + .arg(Arg::new("inadmissible").required(true).index(1)) |
| 83 | + .arg(Arg::new("input").index(2)) |
| 84 | + .arg(Arg::new("output").index(3)) |
| 85 | + .get_matches(); |
| 86 | + |
| 87 | + let format = match matches.get_one::<String>("input-format").unwrap().as_str() { |
| 88 | + "bed3" => InputFormat::Bed3, |
| 89 | + "bed6" => InputFormat::Bed6, |
| 90 | + "bed9" => InputFormat::Bed9, |
| 91 | + "table" => InputFormat::Table, |
| 92 | + _ => unreachable!(), |
| 93 | + }; |
| 94 | + let inadmissible = matches.get_one::<String>("inadmissible").unwrap(); |
| 95 | + let input = matches.get_one::<String>("input").map(String::as_str); |
| 96 | + let output = matches.get_one::<String>("output").map(String::as_str); |
| 97 | + let verbose = matches.get_count("verbose") > 0; |
| 98 | + |
| 99 | + if verbose { |
| 100 | + eprintln!("Reading inadmissible regions `{inadmissible}`..."); |
| 101 | + } |
| 102 | + let remove = import_input(Some(inadmissible), InputFormat::Bed3).unwrap_or_else(|error| { |
| 103 | + eprintln!("reading inadmissible regions failed: {error}"); |
| 104 | + process::exit(1); |
| 105 | + }); |
| 106 | + if verbose { |
| 107 | + if let Some(path) = input { |
| 108 | + eprintln!("Reading input `{path}`..."); |
| 109 | + } else { |
| 110 | + eprintln!("Reading input from stdin..."); |
| 111 | + } |
| 112 | + } |
| 113 | + let input_ranges = import_input(input, format).unwrap_or_else(|error| { |
| 114 | + eprintln!("reading input failed: {error}"); |
| 115 | + process::exit(1); |
| 116 | + }); |
| 117 | + let before = input_ranges.num_rows(); |
| 118 | + let filtered = input_ranges.remove_overlaps_with(&remove); |
| 119 | + if verbose { |
| 120 | + eprintln!("Removed {} regions", before - filtered.num_rows()); |
| 121 | + } |
| 122 | + if let Err(error) = export_output(&filtered, output, format) { |
| 123 | + eprintln!("writing output failed: {error}"); |
| 124 | + process::exit(1); |
| 125 | + } |
| 126 | +} |
0 commit comments