microBioRust_seqmetrics/
write_dst_csv.rs

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