| 1 | use bgzip::tabix::Tabix; |
| 2 | use clap::Parser; |
| 3 | use std::fs::File; |
| 4 | use std::io::{stdout, Write}; |
| 5 | |
| 6 | #[derive(Debug, Parser)] |
| 7 | struct Args { |
| 8 | #[command()] |
| 9 | file: String, |
| 10 | #[arg(short, long)] |
| 11 | output: Option<String>, |
| 12 | } |
| 13 | |
| 14 | fn main() -> anyhow::Result<()> { |
| 15 | let parser = Args::parse(); |
| 16 | |
| 17 | let file = Tabix::from_reader(File::open(&parser.file)?)?; |
| 18 | let out: Box<dyn Write> = if let Some(out) = parser.output { |
| 19 | Box::new(File::create(out)?) |
| 20 | } else { |
| 21 | Box::new(stdout().lock()) |
| 22 | }; |
| 23 | let mut csv_out = csv::WriterBuilder::new().flexible(true).from_writer(out); |
| 24 | |
| 25 | csv_out.write_record(&[ |
| 26 | "# of sequences", |
| 27 | "format", |
| 28 | "coordinate rule", |
| 29 | "column for the sequence name", |
| 30 | "column for the start of a region", |
| 31 | "column for the end fo a region", |
| 32 | "meta", |
| 33 | "skip", |
| 34 | "Length of concatenated sequence names", |
| 35 | ])?; |
| 36 | |
| 37 | csv_out.write_record(&[ |
| 38 | format!("{}", file.number_of_references), |
| 39 | match file.format & 0xffff { |
| 40 | 0 => "Generic".to_string(), |
| 41 | 1 => "SAM".to_string(), |
| 42 | 2 => "VCF".to_string(), |
| 43 | _ => format!("Unknown: {}", file.format), |
| 44 | }, |
| 45 | match file.format & 0x10000 { |
| 46 | 0 => "GFF Rule".to_string(), |
| 47 | _ => "BED Rule".to_string(), |
| 48 | }, |
| 49 | format!("{}", file.column_for_sequence), |
| 50 | format!("{}", file.column_for_begin), |
| 51 | format!("{}", file.column_for_end), |
| 52 | format!("{}", String::from_utf8_lossy(&file.meta)), |
| 53 | format!("{}", file.skip), |
| 54 | format!("{}", file.length_of_concatenated_sequence_names), |
| 55 | ])?; |
| 56 | |
| 57 | csv_out.write_record(&[""])?; |
| 58 | csv_out.write_record(&[ |
| 59 | "sequence index", |
| 60 | "sequence name", |
| 61 | "bin index", |
| 62 | "bin", |
| 63 | "chunk index", |
| 64 | "begin", |
| 65 | "end", |
| 66 | ])?; |
| 67 | for (i, (ref_name, sequence)) in file.names.iter().zip(file.sequences.iter()).enumerate() { |
| 68 | let mut bins: Vec<_> = sequence.bins.values().collect(); |
| 69 | bins.sort_by_key(|x| x.bin); |
| 70 | for (j, bin) in bins.iter().enumerate() { |
| 71 | for (k, x) in bin.chunks.iter().enumerate() { |
| 72 | csv_out.write_record(&[ |
| 73 | format!("{}", i), |
| 74 | String::from_utf8_lossy(ref_name).to_string(), |
| 75 | format!("{}", j), |
| 76 | format!("0x{:x}", bin.bin), |
| 77 | format!("{}", k), |
| 78 | format!("0x{:x}", x.begin), |
| 79 | format!("0x{:x}", x.end), |
| 80 | ])?; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | Ok(()) |
| 86 | } |