use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;
#[derive(Clone, Copy, Debug)]
pub enum ClstrUnit {
Nt,
Aa,
None,
}
impl ClstrUnit {
fn suffix(self) -> Option<&'static str> {
match self {
ClstrUnit::Nt => Some("nt"),
ClstrUnit::Aa => Some("aa"),
ClstrUnit::None => None,
}
}
}
pub struct ClstrWriter {
out: BufWriter<File>,
}
impl ClstrWriter {
pub fn create<P: AsRef<Path>>(path: P) -> io::Result<Self> {
let f = File::create(path)?;
Ok(Self {
out: BufWriter::new(f),
})
}
pub fn write_cluster(
&mut self,
cluster_id: usize,
members: &[usize],
headers: &[String],
lengths: Option<&[u32]>,
unit: ClstrUnit,
) -> io::Result<()> {
writeln!(self.out, ">Cluster {cluster_id}")?;
for (pos, &idx) in members.iter().enumerate() {
let rep_mark = if pos == 0 { " *" } else { "" };
if let Some(len) = lengths.and_then(|ls| ls.get(idx)).copied() {
if let Some(suf) = unit.suffix() {
writeln!(
self.out,
"{pos}\t{len}{suf}, >{}...{rep_mark}",
headers[idx]
)?;
continue;
}
}
writeln!(self.out, "{pos}\t>{}...{rep_mark}", headers[idx])?;
}
Ok(())
}
pub fn finish(mut self) -> io::Result<()> {
self.out.flush()
}
}