ahda 0.1.0

Pseudoalignment compression and conversion between formats.
Documentation
// ahda: Pseudoalignment compression and conversion between formats.
//
// Copyright 2025 Tommi Mäklin [tommi@maklin.fi].
//
// Copyrights in this project are retained by contributors. No copyright assignment
// is required to contribute to this project.
//
// Except as otherwise noted (below and/or in individual files), this
// project is licensed under the Apache License, Version 2.0
// <LICENSE-APACHE> or <http://www.apache.org/licenses/LICENSE-2.0> or
// the MIT license, <LICENSE-MIT> or <http://opensource.org/licenses/MIT>,
// at your option.
//
use std::io::Write;

use crate::PseudoAln;

type E = Box<dyn std::error::Error>;

/// Format a single pseudoalignment in Themisto format
///
/// Writes bytes containing the formatted line containing the contents of
/// `aln` to `conn`.
///
/// Terminates with a [ThemistoPrinterError](crate::errors::ThemistoPrinterError)
/// if the `query_id` field of [PseudoAln] or the `ones` field
/// of [PseudoAln] is None.
///
pub fn format_themisto_line<W: Write>(
    aln: &PseudoAln,
    conn: &mut W,
) -> Result<(), E> {
    let separator: char = ' ';
    let mut formatted: String = String::new();

    if let Some(q_id) = &aln.query_id {
        formatted += &q_id.to_string();
    } else {
        return Err(Box::new(crate::errors::PseudoAlnQueryIdIsEmpty{}))
    };

    if let Some(ones) = &aln.ones {
        ones.iter().for_each(|idx| {
            formatted += &separator.to_string();
            formatted += &idx.to_string();
        });
        formatted += "\n";
    } else {
        return Err(Box::new(crate::errors::PseudoAlnOnesIsEmpty{}))
    }

    conn.write_all(formatted.as_bytes())?;
    Ok(())
}

// Tests
#[cfg(test)]
mod tests {

    #[test]
    fn format_themisto_line_single_alignment() {
        use crate::PseudoAln;
        use super::format_themisto_line;

        let data = PseudoAln{ones_names: None,  query_id: Some(128), ones: Some(vec![0]), ..Default::default()};
        let expected: Vec<u8> = b"128 0\n".to_vec();

        let mut got: Vec<u8> = Vec::new();
        format_themisto_line(&data, &mut got).unwrap();

        assert_eq!(got, expected);
    }

    #[test]
    fn format_themisto_line_many_alignments() {
        use crate::PseudoAln;
        use super::format_themisto_line;

        let data = PseudoAln{ones_names: None,  query_id: Some(128), ones: Some(vec![0, 3, 7, 11]), ..Default::default()};
        let expected: Vec<u8> = b"128 0 3 7 11\n".to_vec();

        let mut got: Vec<u8> = Vec::new();
        format_themisto_line(&data, &mut got).unwrap();

        assert_eq!(got, expected);
    }

    #[test]
    fn format_themisto_line_no_alignments() {
        use crate::PseudoAln;
        use super::format_themisto_line;

        let data = PseudoAln{ones_names: None,  query_id: Some(128), ones: Some(vec![]), ..Default::default()};
        let expected: Vec<u8> = b"128\n".to_vec();

        let mut got: Vec<u8> = Vec::new();
        format_themisto_line(&data, &mut got).unwrap();

        assert_eq!(got, expected);
    }

    #[test]
    fn error_if_no_query_id() {
        use crate::PseudoAln;
        use super::format_themisto_line;

        let data = PseudoAln{ones_names: None,  query_id: None, ones: Some(vec![0, 3, 7, 11]), ..Default::default()};

        let got = format_themisto_line(&data, &mut Vec::new());

        assert!(!got.is_ok());
    }

    #[test]
    fn error_if_no_ones() {
        use crate::PseudoAln;
        use super::format_themisto_line;

        let data = PseudoAln{ones_names: None,  query_id: Some(128), ones: None, ..Default::default()};

        let got = format_themisto_line(&data, &mut Vec::new());

        assert!(!got.is_ok());
    }
}