From 97e4e9b9bfa3f8ded019106ea6ea4fb8c3b1acb0 Mon Sep 17 00:00:00 2001 From: Lee Katz - Aspen Date: Tue, 20 Feb 2024 12:08:09 -0500 Subject: [PATCH] sort by single nts --- src/bin/fasten_sort.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/bin/fasten_sort.rs b/src/bin/fasten_sort.rs index a4e7d4a2..f843546b 100644 --- a/src/bin/fasten_sort.rs +++ b/src/bin/fasten_sort.rs @@ -246,7 +246,7 @@ fn sort_entries (unsorted:Vec, which_field:&str, reverse_sort:bool) -> Vec< a_seq.cmp(&b_seq) }); }, - "GC" => { + "GC" | "CG" => { sorted.sort_by(|a,b| { let a_seq = format!("{}{}", a.seq1, a.seq2); let b_seq = format!("{}{}", b.seq1, b.seq2); @@ -257,6 +257,34 @@ fn sort_entries (unsorted:Vec, which_field:&str, reverse_sort:bool) -> Vec< a_gc.partial_cmp(&b_gc).unwrap() }); }, + "A" | "C" | "G" | "T" | "N" => { + let nt_lowercase = which_field.to_lowercase().chars().next().unwrap_or('\0'); + let nt_uppercase = which_field.to_uppercase().chars().next().unwrap_or('\0'); + let my_nts = vec![nt_uppercase, nt_lowercase]; + + sorted.sort_by(|a,b| { + let a_seq = format!("{}{}", a.seq1, a.seq2); + let b_seq = format!("{}{}", b.seq1, b.seq2); + + let mut a_count: usize = 0; + let mut b_count: usize = 0; + + for nt in &my_nts { + for c in a_seq.chars() { + if nt == &c { + a_count += 1; + } + } + for c in b_seq.chars() { + if nt == &c { + b_count += 1; + } + } + } + + a_count.cmp(&b_count) + }); + }, _ => { panic!("Tried to sort by {} which is not implemented", which_field); }