From 0f4fcdb23fec8d0786fd8dff1a13f805ef7277c2 Mon Sep 17 00:00:00 2001 From: Qiang Wang Date: Thu, 19 Sep 2024 08:08:42 +0800 Subject: [PATCH] Update io.rs --- src/libs/io.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/libs/io.rs b/src/libs/io.rs index 905d0ff..803fe44 100644 --- a/src/libs/io.rs +++ b/src/libs/io.rs @@ -1,5 +1,6 @@ use phylotree::tree::Tree; use std::io::Read; +use std::{fmt, io, str}; pub fn read_newick(infile: &str) -> Tree { let mut reader = intspan::reader(infile); @@ -25,3 +26,71 @@ pub fn read_newick(infile: &str) -> Tree { tree } + +#[derive(Default, Clone)] +pub struct AsmEntry { + name: String, + vector: Vec, +} + +impl AsmEntry { + // Immutable accessors + pub fn name(&self) -> &String { + &self.name + } + pub fn vector(&self) -> &Vec { + &self.vector + } + + pub fn new() -> Self { + Self { + name: String::new(), + vector: vec![], + } + } + + /// Constructed from range and seq + /// + /// ``` + /// # use nwr::AsmEntry; + /// let name = "Es_coli_005008_GCF_013426115_1".to_string(); + /// let vector : Vec = vec![1,5,2,7,6,6]; + /// let entry = AsmEntry::from(&name, &vector); + /// # assert_eq!(*entry.name(), "Es_coli_005008_GCF_013426115_1"); + /// # assert_eq!(*entry.vector().get(1).unwrap(), 5i32); + /// ``` + pub fn from(name: &String, vector: &[i32]) -> Self { + Self { + name: name.clone(), + vector: Vec::from(vector), + } + } +} + +impl fmt::Display for AsmEntry { + /// To string + /// + /// ``` + /// # use nwr::AsmEntry; + /// let name = "Es_coli_005008_GCF_013426115_1".to_string(); + /// let vector : Vec = vec![1,5,2,7,6,6]; + /// let entry = AsmEntry::from(&name, &vector); + /// assert_eq!(entry.to_string(), "Es_coli_005008_GCF_013426115_1\t1,5,2,7,6,6\n"); + /// ``` + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{}\t{}\n", + self.name(), + self.vector.iter().map(|e| e.to_string()).collect::>().join(","), + )?; + Ok(()) + } +} + +pub fn parse_asm_line(line: &str) -> AsmEntry { + +} + + +// https://www.maartengrootendorst.com/blog/distances/