use std::io::Write;
use crate::PseudoAln;
type E = Box<dyn std::error::Error>;
pub fn format_fulgor_line<W: Write>(
aln: &PseudoAln,
conn: &mut W,
) -> Result<(), E> {
let separator: char = '\t';
let mut formatted: String = String::new();
if let Some(name) = &aln.query_name {
let q_name = String::from_utf8(name.clone())?;
formatted += &q_name;
formatted += &separator.to_string();
} else {
return Err(Box::new(crate::errors::PseudoAlnQueryNameIsEmpty{}))
};
if let Some(ones) = &aln.ones {
formatted += &ones.len().to_string();
for target_idx in ones {
formatted += &separator.to_string();
formatted += &target_idx.to_string();
}
formatted += "\n";
} else {
return Err(Box::new(crate::errors::PseudoAlnOnesIsEmpty{}))
}
conn.write_all(formatted.as_bytes())?;
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn format_fulgor_line_1st_aligned() {
use crate::PseudoAln;
use super::format_fulgor_line;
let data = PseudoAln{ones_names: None, query_id: Some(1262953), ones: Some(vec![0]), query_name: Some("ERR4035126.1262954".as_bytes().to_vec()) };
let expected: Vec<u8> = b"ERR4035126.1262954\t1\t0\n".to_vec();
let mut got: Vec<u8> = Vec::new();
format_fulgor_line(&data, &mut got).unwrap();
assert_eq!(got, expected);
}
#[test]
fn format_fulgor_line_2nd_aligned() {
use crate::PseudoAln;
use super::format_fulgor_line;
let data = PseudoAln{ones_names: None, query_id: Some(1262953), ones: Some(vec![1]), query_name: Some("ERR4035126.1262954".as_bytes().to_vec()) };
let expected: Vec<u8> = b"ERR4035126.1262954\t1\t1\n".to_vec();
let mut got: Vec<u8> = Vec::new();
format_fulgor_line(&data, &mut got).unwrap();
assert_eq!(got, expected);
}
#[test]
fn format_fulgor_line_two_alignments() {
use crate::PseudoAln;
use super::format_fulgor_line;
let data = PseudoAln{ones_names: None, query_id: Some(1262953), ones: Some(vec![0, 1]), query_name: Some("ERR4035126.1262954".as_bytes().to_vec()) };
let expected: Vec<u8> = b"ERR4035126.1262954\t2\t0\t1\n".to_vec();
let mut got: Vec<u8> = Vec::new();
format_fulgor_line(&data, &mut got).unwrap();
assert_eq!(got, expected);
}
#[test]
fn format_fulgor_line_no_alignments() {
use crate::PseudoAln;
use super::format_fulgor_line;
let data = PseudoAln{ones_names: None, query_id: Some(1262953), ones: Some(vec![]), query_name: Some("ERR4035126.1262954".as_bytes().to_vec()) };
let expected: Vec<u8> = b"ERR4035126.1262954\t0\n".to_vec();
let mut got: Vec<u8> = Vec::new();
format_fulgor_line(&data, &mut got).unwrap();
assert_eq!(got, expected);
}
#[test]
fn error_if_no_query_name() {
use crate::PseudoAln;
use super::format_fulgor_line;
let data = PseudoAln{ones_names: None, query_name: None, ones: Some(vec![0, 3, 7, 11]), query_id: None};
let got = format_fulgor_line(&data, &mut Vec::new());
assert!(!got.is_ok());
}
#[test]
fn error_if_no_ones() {
use crate::PseudoAln;
use super::format_fulgor_line;
let data = PseudoAln{ones_names: None, query_name: Some("ERR4035126.1262954".as_bytes().to_vec()), query_id: Some(128), ones: None};
let got = format_fulgor_line(&data, &mut Vec::new());
assert!(!got.is_ok());
}
}