|
| 1 | +use crate::models::assay::Assay; |
| 2 | +use crate::models::region::{Region, RegionCoordinate}; |
| 3 | +use crate::utils; |
| 4 | +use clap::Args; |
| 5 | +use std::fs; |
| 6 | +use std::io::Write; |
| 7 | +use std::path::PathBuf; |
| 8 | + |
| 9 | +#[derive(Debug, Args)] |
| 10 | +pub struct PrintArgs { |
| 11 | + #[clap(help = "Sequencing specification yaml file", required = true)] |
| 12 | + yaml: PathBuf, |
| 13 | + |
| 14 | + #[clap(short, long, help = "Path to output file", value_name = "OUT")] |
| 15 | + output: Option<PathBuf>, |
| 16 | + |
| 17 | + #[clap( |
| 18 | + short, |
| 19 | + long, |
| 20 | + help = "Format", |
| 21 | + value_name = "FORMAT", |
| 22 | + default_value = "library-ascii", |
| 23 | + value_parser = ["library-ascii", "seqspec-ascii", "seqspec-html", "seqspec-png"], |
| 24 | + )] |
| 25 | + format: String, |
| 26 | +} |
| 27 | + |
| 28 | +pub fn run_print(args: &PrintArgs) { |
| 29 | + validate_print_args(args); |
| 30 | + |
| 31 | + let spec = utils::load_spec(&args.yaml); |
| 32 | + let result = seqspec_print(&spec, &args.format).unwrap_or_else(|err| { |
| 33 | + eprintln!("{}", err); |
| 34 | + std::process::exit(1); |
| 35 | + }); |
| 36 | + |
| 37 | + if let Some(out) = &args.output { |
| 38 | + let mut fh = fs::File::create(out).unwrap(); |
| 39 | + writeln!(fh, "{result}").unwrap(); |
| 40 | + } else { |
| 41 | + println!("{result}"); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +fn validate_print_args(args: &PrintArgs) { |
| 46 | + if !args.yaml.exists() { |
| 47 | + eprintln!("Input file does not exist: {}", args.yaml.display()); |
| 48 | + std::process::exit(1); |
| 49 | + } |
| 50 | + if let Some(out) = &args.output { |
| 51 | + if out.exists() && !out.is_file() { |
| 52 | + eprintln!("Output path exists but is not a file: {}", out.display()); |
| 53 | + std::process::exit(1); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +pub fn seqspec_print(spec: &Assay, fmt: &str) -> Result<String, String> { |
| 59 | + match fmt { |
| 60 | + "library-ascii" => Ok(print_library_ascii(spec)), |
| 61 | + "seqspec-ascii" => print_seqspec_ascii(spec), |
| 62 | + "seqspec-html" => Err("seqspec-html is not implemented in the Rust CLI yet".to_string()), |
| 63 | + "seqspec-png" => Err("seqspec-png is not implemented in the Rust CLI yet".to_string()), |
| 64 | + _ => Err(format!("Unsupported format: {}", fmt)), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn print_seqspec_ascii(spec: &Assay) -> Result<String, String> { |
| 69 | + let mut parts = Vec::new(); |
| 70 | + for modality in &spec.modalities { |
| 71 | + let (p, n) = libseq(spec, modality)?; |
| 72 | + parts.push(format_libseq(spec, modality, p, n)?); |
| 73 | + } |
| 74 | + Ok(parts.join("\n")) |
| 75 | +} |
| 76 | + |
| 77 | +fn format_libseq( |
| 78 | + spec: &Assay, |
| 79 | + modality: &str, |
| 80 | + positive: Vec<String>, |
| 81 | + negative: Vec<String>, |
| 82 | +) -> Result<String, String> { |
| 83 | + let libspec = spec |
| 84 | + .get_libspec(modality) |
| 85 | + .ok_or_else(|| format!("modality '{}' not found", modality))?; |
| 86 | + |
| 87 | + Ok( |
| 88 | + [ |
| 89 | + modality.to_string(), |
| 90 | + "---".to_string(), |
| 91 | + positive.join("\n"), |
| 92 | + libspec.sequence.clone(), |
| 93 | + utils::complement_seq(&libspec.sequence), |
| 94 | + negative.join("\n"), |
| 95 | + ] |
| 96 | + .join("\n"), |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +fn libseq(spec: &Assay, modality: &str) -> Result<(Vec<String>, Vec<String>), String> { |
| 101 | + let libspec = spec |
| 102 | + .get_libspec(modality) |
| 103 | + .ok_or_else(|| format!("modality '{}' not found", modality))?; |
| 104 | + let seqspec = spec.get_seqspec(modality); |
| 105 | + |
| 106 | + let mut positive = Vec::new(); |
| 107 | + let mut negative = Vec::new(); |
| 108 | + |
| 109 | + for (idx, read) in seqspec.iter().enumerate() { |
| 110 | + let leaves = libspec.get_leaves_with_region_id(&read.primer_id); |
| 111 | + let primer_idx = leaves |
| 112 | + .iter() |
| 113 | + .position(|leaf| leaf.region_id == read.primer_id) |
| 114 | + .ok_or_else(|| { |
| 115 | + format!( |
| 116 | + "primer_id '{}' not found in modality '{}'", |
| 117 | + read.primer_id, modality |
| 118 | + ) |
| 119 | + })?; |
| 120 | + |
| 121 | + let cuts: Vec<RegionCoordinate> = utils::project_regions_to_coordinates(leaves); |
| 122 | + let primer_pos = &cuts[primer_idx]; |
| 123 | + let arrow_len = read.max_len.saturating_sub(1) as usize; |
| 124 | + let arrow = "-".repeat(arrow_len); |
| 125 | + |
| 126 | + if read.strand == "pos" { |
| 127 | + let space_len = primer_pos.stop.saturating_sub(1) as usize; |
| 128 | + let ws = " ".repeat(space_len); |
| 129 | + positive.push(format!("{}|{}>({}) {}", ws, arrow, idx + 1, read.read_id)); |
| 130 | + } else { |
| 131 | + let space_len = primer_pos.start.saturating_sub(read.max_len) as usize; |
| 132 | + let ws = " ".repeat(space_len); |
| 133 | + negative.push(format!("{}<{}|({}) {}", ws, arrow, idx + 1, read.read_id)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + Ok((positive, negative)) |
| 138 | +} |
| 139 | + |
| 140 | +fn print_library_ascii(spec: &Assay) -> String { |
| 141 | + spec.library_spec |
| 142 | + .iter() |
| 143 | + .map(|region| render_region_tree(region, "", true)) |
| 144 | + .collect::<Vec<_>>() |
| 145 | + .join("\n") |
| 146 | +} |
| 147 | + |
| 148 | +fn render_region_tree(region: &Region, prefix: &str, is_last: bool) -> String { |
| 149 | + let branch = if prefix.is_empty() { |
| 150 | + "" |
| 151 | + } else if is_last { |
| 152 | + "└── " |
| 153 | + } else { |
| 154 | + "├── " |
| 155 | + }; |
| 156 | + let mut lines = vec![format!( |
| 157 | + "{}{}{}({},{})", |
| 158 | + prefix, branch, region.region_id, region.min_len, region.max_len |
| 159 | + )]; |
| 160 | + |
| 161 | + let child_prefix = if prefix.is_empty() { |
| 162 | + String::new() |
| 163 | + } else if is_last { |
| 164 | + format!("{} ", prefix) |
| 165 | + } else { |
| 166 | + format!("{}│ ", prefix) |
| 167 | + }; |
| 168 | + |
| 169 | + for (idx, child) in region.regions.iter().enumerate() { |
| 170 | + lines.push(render_region_tree( |
| 171 | + child, |
| 172 | + &child_prefix, |
| 173 | + idx + 1 == region.regions.len(), |
| 174 | + )); |
| 175 | + } |
| 176 | + |
| 177 | + lines.join("\n") |
| 178 | +} |
| 179 | + |
| 180 | +#[cfg(test)] |
| 181 | +mod tests { |
| 182 | + use super::*; |
| 183 | + use crate::utils::load_spec; |
| 184 | + |
| 185 | + fn dogma_spec() -> Assay { |
| 186 | + load_spec(&PathBuf::from("tests/fixtures/spec.yaml")) |
| 187 | + } |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn test_print_library_ascii_contains_modalities_and_regions() { |
| 191 | + let rendered = print_library_ascii(&dogma_spec()); |
| 192 | + assert!(rendered.contains("rna(")); |
| 193 | + assert!(rendered.contains("rna_cell_bc")); |
| 194 | + assert!(rendered.contains("atac")); |
| 195 | + } |
| 196 | + |
| 197 | + #[test] |
| 198 | + fn test_print_seqspec_ascii_contains_reads_and_sequence() { |
| 199 | + let rendered = print_seqspec_ascii(&dogma_spec()).unwrap(); |
| 200 | + assert!(rendered.contains("rna")); |
| 201 | + assert!(rendered.contains("rna_R1")); |
| 202 | + assert!(rendered.contains("rna_R2")); |
| 203 | + assert!(rendered.contains("---")); |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn test_print_reports_unimplemented_html() { |
| 208 | + let err = seqspec_print(&dogma_spec(), "seqspec-html").unwrap_err(); |
| 209 | + assert!(err.contains("not implemented")); |
| 210 | + } |
| 211 | +} |
0 commit comments