Skip to main content

microBioRust_seqmetrics/
write_dst_csv.rs

1use anyhow::Result;
2use tokio::fs::File;
3use tokio::io::{AsyncWriteExt, BufWriter};
4
5pub async fn write_distances_csv(
6    ids: Vec<String>,
7    distances: Vec<Vec<usize>>,
8    filename: &str,
9) -> Result<(), anyhow::Error> {
10    let file = File::create(filename).await?;
11    let mut writer = BufWriter::new(file);
12    let mut header = String::from("id");
13    for id in &ids {
14        header.push(',');
15        header.push_str(id);
16    }
17    header.push('\n');
18    writer.write_all(header.as_bytes()).await?;
19    // Write each row
20    for (i, row) in distances.iter().enumerate() {
21        let mut line = format!("{}", ids[i]);
22        for val in row {
23            line.push_str(&format!(",{:.3}", val));
24        }
25        line.push('\n');
26        writer.write_all(line.as_bytes()).await?;
27    }
28    writer.flush().await?;
29    Ok(())
30}