From f12fd2f04dadbf002198dc7ea79ae6164d85bf28 Mon Sep 17 00:00:00 2001
From: Donald Campbell <125581724+donaldcampbelljr@users.noreply.github.com>
Date: Thu, 12 Dec 2024 13:56:24 -0500
Subject: [PATCH] clamp number of counts based on chromsize for #43

---
 gtars/src/uniwig/mod.rs     | 3 +++
 gtars/src/uniwig/writing.rs | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/gtars/src/uniwig/mod.rs b/gtars/src/uniwig/mod.rs
index 99fb5bf8..3e0ecd07 100644
--- a/gtars/src/uniwig/mod.rs
+++ b/gtars/src/uniwig/mod.rs
@@ -316,6 +316,7 @@ pub fn uniwig_main(
                                                         smoothsize,
                                                     ),
                                                     stepsize,
+                                                    current_chrom_size,
                                                 );
                                             }
                                             "bedGraph" => {
@@ -507,6 +508,7 @@ pub fn uniwig_main(
                                                         smoothsize,
                                                     ),
                                                     stepsize,
+                                                    current_chrom_size,
                                                 );
                                             }
                                             "csv" => {
@@ -658,6 +660,7 @@ pub fn uniwig_main(
                                                         0,
                                                     ),
                                                     stepsize,
+                                                    current_chrom_size,
                                                 );
                                             }
                                             "csv" => {
diff --git a/gtars/src/uniwig/writing.rs b/gtars/src/uniwig/writing.rs
index 446a3738..45a363ba 100644
--- a/gtars/src/uniwig/writing.rs
+++ b/gtars/src/uniwig/writing.rs
@@ -96,6 +96,7 @@ pub fn write_to_wig_file(
     chromname: String,
     start_position: i32,
     stepsize: i32,
+    chrom_size: i32,
 ) {
     let path = std::path::Path::new(&filename).parent().unwrap();
     let _ = create_dir_all(path);
@@ -117,7 +118,7 @@ pub fn write_to_wig_file(
 
     let mut buf = BufWriter::new(file);
 
-    for count in counts.iter() {
+    for count in counts.iter().take(chrom_size as usize) { // must set upper bound for wiggles based on reported chromsize, this is for downstream tool interoperability
         writeln!(&mut buf, "{}", count).unwrap();
     }
     buf.flush().unwrap();