Skip to content

Commit

Permalink
Implement Display instead of to_string for Fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
SeppeDeWinter committed Jan 19, 2024
1 parent 58771f9 commit 62b3bb9
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions rust/src/aggregate_fragments.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 62b3bb9

Please sign in to comment.