From 62b3bb9a4d1cea1d5c5409c547852486e3b4cf6c Mon Sep 17 00:00:00 2001 From: SeppeDeWinter Date: Fri, 19 Jan 2024 15:07:47 +0100 Subject: [PATCH] Implement Display instead of to_string for Fragment --- rust/src/aggregate_fragments.rs | 39 +++++++++++++++------------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/rust/src/aggregate_fragments.rs b/rust/src/aggregate_fragments.rs index 45d83d3..4379a95 100644 --- a/rust/src/aggregate_fragments.rs +++ b/rust/src/aggregate_fragments.rs @@ -1,6 +1,7 @@ use bgzip::BGZFReader; use rust_htslib::bgzf::Writer; use rust_htslib::tpool::ThreadPool; +use core::fmt; use std::fs::File; /// Aggregates multiple fragment files into a single file /// This code is just a fancy implementation of the unix command `cat | sort -k1,1 -k2,2n -k3,3n | bgzip` @@ -79,27 +80,6 @@ impl Fragment { _ => panic!("Invalid number of fields in fragment file!"), } } - - /// Convert a Fragment to a string. - /// - /// # Example - /// - /// ```rust - /// let fragment = Fragment::new_from_string("chr1\t100\t200\tAACATCGATGGATG-1\t10"); - /// assert_eq!(fragment.to_string(), "chr1\t100\t200\tAACATCGATGGATG-1\t10"); - /// ``` - fn to_string(&self) -> String { - match self.score { - Some(score) => format!( - "{}\t{}\t{}\t{}\t{}", - self.chrom, self.start, self.end, self.cell_barcode, score - ), - None => format!( - "{}\t{}\t{}\t{}", - self.chrom, self.start, self.end, self.cell_barcode - ), - } - } } impl Ord for Fragment { @@ -134,6 +114,23 @@ impl PartialOrd for Fragment { } } +impl fmt::Display for Fragment { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.score { + Some(score) => write!( + f, + "{}\t{}\t{}\t{}\t{}", + self.chrom, self.start, self.end, self.cell_barcode, score + ), + None => write!( + f, + "{}\t{}\t{}\t{}", + self.chrom, self.start, self.end, self.cell_barcode + ), + } + } +} + /// Aggregates multiple fragment files into a single file. /// /// # Arguments