From 5bd44abad1415c5c27dccd07c1901bfb43115749 Mon Sep 17 00:00:00 2001
From: Donald Campbell <125581724+donaldcampbelljr@users.noreply.github.com>
Date: Thu, 12 Dec 2024 13:20:09 -0500
Subject: [PATCH] Attempt fix for #43

---
 gtars/src/uniwig/counting.rs    |  10 +-
 gtars/src/uniwig/mod.rs         | 280 +++++++++++++++++++++++++-------
 gtars/tests/data/out/_core.wig  |   5 +-
 gtars/tests/data/out/_end.wig   |   3 +-
 gtars/tests/data/out/_start.wig |   3 +-
 gtars/tests/test.rs             |   2 +
 6 files changed, 231 insertions(+), 72 deletions(-)

diff --git a/gtars/src/uniwig/counting.rs b/gtars/src/uniwig/counting.rs
index aa08cfa7..e97b74c9 100644
--- a/gtars/src/uniwig/counting.rs
+++ b/gtars/src/uniwig/counting.rs
@@ -34,6 +34,7 @@ pub fn start_end_counts(
     chrom_size: i32,
     smoothsize: i32,
     stepsize: i32,
+    shift: i32,
 ) -> (Vec<u32>, Vec<i32>) {
     //let vin_iter = starts_vector.iter();
 
@@ -54,7 +55,7 @@ pub fn start_end_counts(
 
     adjusted_start_site = starts_vector[0]; // get first coordinate position
 
-    adjusted_start_site.0 = adjusted_start_site.0 - smoothsize;
+    adjusted_start_site.0 = adjusted_start_site.0 - smoothsize + shift;
 
     current_end_site = adjusted_start_site;
     current_end_site.0 = adjusted_start_site.0 + 1 + smoothsize * 2;
@@ -73,7 +74,7 @@ pub fn start_end_counts(
         coordinate_value = *coord;
 
         adjusted_start_site = coordinate_value;
-        adjusted_start_site.0 = coordinate_value.0 - smoothsize;
+        adjusted_start_site.0 = coordinate_value.0 - smoothsize + shift;
 
         let current_score = adjusted_start_site.1;
 
@@ -163,6 +164,7 @@ pub fn core_counts(
     ends_vector: &[(i32, i32)],
     chrom_size: i32,
     stepsize: i32,
+    shift: i32,
 ) -> (Vec<u32>, Vec<i32>) {
     let mut v_coordinate_positions: Vec<i32> = Vec::new(); // these are the final coordinates after any adjustments
     let mut v_coord_counts: Vec<u32> = Vec::new(); // u8 stores 0:255 This may be insufficient. u16 max is 65535
@@ -182,6 +184,8 @@ pub fn core_counts(
     current_start_site = starts_vector[0]; // get first coordinate position
     current_end_site = ends_vector[0];
 
+    current_start_site.0 = current_start_site.0 + shift;
+
     if current_start_site.0 < 1 {
         current_start_site.0 = 1;
     }
@@ -197,6 +201,8 @@ pub fn core_counts(
 
         current_start_site = coordinate_value;
 
+        current_start_site.0 = current_start_site.0 + shift;
+
         let current_score = current_start_site.1;
         count += current_score;
 
diff --git a/gtars/src/uniwig/mod.rs b/gtars/src/uniwig/mod.rs
index 44beac0f..9fd75801 100644
--- a/gtars/src/uniwig/mod.rs
+++ b/gtars/src/uniwig/mod.rs
@@ -272,33 +272,36 @@ pub fn uniwig_main(
                             if smoothsize != 0 {
                                 match j {
                                     0 => {
-                                        let mut count_result = match ft {
-                                            Ok(FileType::BED) => start_end_counts(
-                                                &chromosome.starts,
-                                                current_chrom_size,
-                                                smoothsize,
-                                                stepsize,
-                                            ),
-                                            _ => start_end_counts(
-                                                &chromosome.starts,
-                                                current_chrom_size,
-                                                smoothsize,
-                                                stepsize,
-                                            ),
-                                        };
 
                                         match output_type {
                                             "file" => {
+                                                panic!("Writing to file currently not supported");
                                                 //print!("Writing to CLI");
-                                                let handle = &std::io::stdout();
-                                                let mut buf = BufWriter::new(handle);
-                                                for count in &count_result.0 {
-                                                    writeln!(buf, "{}", count)
-                                                        .expect("failed to write line");
-                                                }
-                                                buf.flush().unwrap();
+                                                // let handle = &std::io::stdout();
+                                                // let mut buf = BufWriter::new(handle);
+                                                // for count in &count_result.0 {
+                                                //     writeln!(buf, "{}", count)
+                                                //         .expect("failed to write line");
+                                                // }
+                                                // buf.flush().unwrap();
                                             }
                                             "wig" => {
+                                                let count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        1,
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        1,
+                                                    ),
+                                                };
                                                 //println!("Writing to wig file!");
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
@@ -316,6 +319,22 @@ pub fn uniwig_main(
                                                 );
                                             }
                                             "bedGraph" => {
+                                                let mut count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0,
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0,
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "start", output_type
@@ -339,6 +358,22 @@ pub fn uniwig_main(
                                                 panic!("Write to CSV. Not Implemented");
                                             }
                                             "npy" => {
+                                                let count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0,
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0,
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "start", output_type
@@ -357,6 +392,22 @@ pub fn uniwig_main(
                                             }
                                             _ => {
                                                 println!("Defaulting to npy file...");
+                                                let count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0,
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.starts,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0,
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "start", output_type
@@ -376,32 +427,36 @@ pub fn uniwig_main(
                                         }
                                     }
                                     1 => {
-                                        let mut count_result = match ft {
-                                            Ok(FileType::BED) => start_end_counts(
-                                                &chromosome.ends,
-                                                current_chrom_size,
-                                                smoothsize,
-                                                stepsize,
-                                            ),
-                                            _ => start_end_counts(
-                                                &chromosome.ends,
-                                                current_chrom_size,
-                                                smoothsize,
-                                                stepsize,
-                                            ),
-                                        };
 
                                         match output_type {
                                             "file" => {
-                                                let handle = &std::io::stdout();
-                                                let mut buf = BufWriter::new(handle);
-                                                for count in &count_result.0 {
-                                                    writeln!(buf, "{}", count)
-                                                        .expect("failed to write line");
-                                                }
-                                                buf.flush().unwrap();
+                                                panic!("Writing to file not currently supported.")
+                                                // let handle = &std::io::stdout();
+                                                // let mut buf = BufWriter::new(handle);
+                                                // for count in &count_result.0 {
+                                                //     writeln!(buf, "{}", count)
+                                                //         .expect("failed to write line");
+                                                // }
+                                                // buf.flush().unwrap();
                                             }
                                             "bedGraph" => {
+                                                let mut count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                };
+
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "end", output_type
@@ -423,6 +478,22 @@ pub fn uniwig_main(
                                                 );
                                             }
                                             "wig" => {
+                                                let count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        1
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        1
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "end", output_type
@@ -442,6 +513,22 @@ pub fn uniwig_main(
                                                 panic!("Write to CSV. Not Implemented");
                                             }
                                             "npy" => {
+                                                let count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "end", output_type
@@ -459,6 +546,22 @@ pub fn uniwig_main(
                                                 );
                                             }
                                             _ => {
+                                                let count_result = match ft {
+                                                    Ok(FileType::BED) => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                    _ => start_end_counts(
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        smoothsize,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                };
                                                 println!("Defaulting to npy file...");
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
@@ -479,32 +582,35 @@ pub fn uniwig_main(
                                         }
                                     }
                                     2 => {
-                                        let mut core_results = match ft {
-                                            Ok(FileType::BED) => core_counts(
-                                                &chromosome.starts,
-                                                &chromosome.ends,
-                                                current_chrom_size,
-                                                stepsize,
-                                            ),
-                                            _ => core_counts(
-                                                &chromosome.starts,
-                                                &chromosome.ends,
-                                                current_chrom_size,
-                                                stepsize,
-                                            ),
-                                        };
 
                                         match output_type {
                                             "file" => {
-                                                let handle = &std::io::stdout();
-                                                let mut buf = BufWriter::new(handle);
-                                                for count in &core_results.0 {
-                                                    writeln!(buf, "{}", count)
-                                                        .expect("failed to write line");
-                                                }
-                                                buf.flush().unwrap();
+                                                panic!("Writing to file not supported.")
+                                                // let handle = &std::io::stdout();
+                                                // let mut buf = BufWriter::new(handle);
+                                                // for count in &core_results.0 {
+                                                //     writeln!(buf, "{}", count)
+                                                //         .expect("failed to write line");
+                                                // }
+                                                // buf.flush().unwrap();
                                             }
                                             "bedGraph" => {
+                                                let mut core_results = match ft {
+                                                    Ok(FileType::BED) => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                    _ => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "core", output_type
@@ -523,6 +629,22 @@ pub fn uniwig_main(
                                                 );
                                             }
                                             "wig" => {
+                                                let core_results = match ft {
+                                                    Ok(FileType::BED) => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        1
+                                                    ),
+                                                    _ => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        1
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "core", output_type
@@ -539,6 +661,22 @@ pub fn uniwig_main(
                                                 panic!("Write to CSV. Not Implemented");
                                             }
                                             "npy" => {
+                                                let core_results = match ft {
+                                                    Ok(FileType::BED) => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                    _ => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                };
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
                                                     bwfileheader, chrom_name, "core", output_type
@@ -553,6 +691,22 @@ pub fn uniwig_main(
                                                 );
                                             }
                                             _ => {
+                                                let core_results = match ft {
+                                                    Ok(FileType::BED) => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                    _ => core_counts(
+                                                        &chromosome.starts,
+                                                        &chromosome.ends,
+                                                        current_chrom_size,
+                                                        stepsize,
+                                                        0
+                                                    ),
+                                                };
                                                 println!("Defaulting to npy file...");
                                                 let file_name = format!(
                                                     "{}{}_{}.{}",
diff --git a/gtars/tests/data/out/_core.wig b/gtars/tests/data/out/_core.wig
index bce79299..81ae5e9f 100644
--- a/gtars/tests/data/out/_core.wig
+++ b/gtars/tests/data/out/_core.wig
@@ -2,9 +2,8 @@ fixedStep chrom=chr1 start=2 step=1
 2
 2
 3
-4
-2
 2
+1
 2
 1
 1
@@ -16,4 +15,4 @@ fixedStep chrom=chr1 start=2 step=1
 0
 0
 0
-0
+0
\ No newline at end of file
diff --git a/gtars/tests/data/out/_end.wig b/gtars/tests/data/out/_end.wig
index e89bdc32..f3119c10 100644
--- a/gtars/tests/data/out/_end.wig
+++ b/gtars/tests/data/out/_end.wig
@@ -12,5 +12,4 @@ fixedStep chrom=chr1 start=5 step=1
 0
 0
 0
-0
-0
+0
\ No newline at end of file
diff --git a/gtars/tests/data/out/_start.wig b/gtars/tests/data/out/_start.wig
index 361beb36..b08c334f 100644
--- a/gtars/tests/data/out/_start.wig
+++ b/gtars/tests/data/out/_start.wig
@@ -16,5 +16,4 @@ fixedStep chrom=chr1 start=1 step=1
 0
 0
 0
-0
-0
+0
\ No newline at end of file
diff --git a/gtars/tests/test.rs b/gtars/tests/test.rs
index 0e538082..b41a9ca0 100644
--- a/gtars/tests/test.rs
+++ b/gtars/tests/test.rs
@@ -315,6 +315,7 @@ mod tests {
                 &chromosome.ends,
                 current_chrom_size,
                 stepsize,
+                0
             );
         }
     }
@@ -335,6 +336,7 @@ mod tests {
                 current_chrom_size,
                 smooth_size,
                 stepsize,
+                0
             );
         }
     }