Skip to content

Commit

Permalink
potential fix for #64
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldcampbelljr committed Jan 9, 2025
1 parent 47b6316 commit 5c66be9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
11 changes: 4 additions & 7 deletions gtars/src/uniwig/counting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +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();

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
Expand All @@ -55,7 +53,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 + shift;
adjusted_start_site.0 = adjusted_start_site.0 - smoothsize;

current_end_site = adjusted_start_site;
current_end_site.0 = adjusted_start_site.0 + 1 + smoothsize * 2;
Expand All @@ -74,7 +72,7 @@ pub fn start_end_counts(
coordinate_value = *coord;

adjusted_start_site = coordinate_value;
adjusted_start_site.0 = coordinate_value.0 - smoothsize + shift;
adjusted_start_site.0 = coordinate_value.0 - smoothsize;

let current_score = adjusted_start_site.1;

Expand Down Expand Up @@ -164,7 +162,6 @@ 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
Expand All @@ -184,7 +181,7 @@ 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;
current_start_site.0 = current_start_site.0;

if current_start_site.0 < 1 {
current_start_site.0 = 1;
Expand All @@ -201,7 +198,7 @@ pub fn core_counts(

current_start_site = coordinate_value;

current_start_site.0 = current_start_site.0 + shift;
current_start_site.0 = current_start_site.0;

let current_score = current_start_site.1;
count += current_score;
Expand Down
33 changes: 20 additions & 13 deletions gtars/src/uniwig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ pub fn run_uniwig(matches: &ArgMatches) {
}

/// Ensures that the start position is at a minimum equal to `1`
fn clamped_start_position(start: i32, smoothsize: i32) -> i32 {
std::cmp::max(1, start - smoothsize)
fn clamped_start_position(start: i32, smoothsize: i32, wig_shift:i32) -> i32 {
std::cmp::max(1, start - smoothsize + wig_shift)
}
/// Ensure that the start position is at a minimum equal to `0`
fn clamped_start_position_zero_pos(start: i32, smoothsize: i32) -> i32 {
Expand Down Expand Up @@ -222,7 +222,6 @@ pub fn uniwig_main(
.build()
.unwrap();

let mut wig_shift: i32 = 0; // This will be set to 1 when writing to wiggle files, else always set to 0

// Determine Input File Type
let input_filetype = FileType::from_str(filetype.to_lowercase().as_str());
Expand Down Expand Up @@ -258,9 +257,6 @@ pub fn uniwig_main(
if output_type == "bedgraph" || output_type == "bw" || output_type == "bigwig" {
output_type = "bedGraph" // we must create bedgraphs first before creating bigwig files
}
if output_type == "wig" {
wig_shift = 1;
}

// Pare down chromosomes if necessary
let mut final_chromosomes =
Expand Down Expand Up @@ -299,7 +295,6 @@ pub fn uniwig_main(
current_chrom_size,
smoothsize,
stepsize,
wig_shift,
);

match output_type {
Expand All @@ -322,6 +317,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_start.0,
smoothsize,
1 //must shift wiggle starts and core by 1 since it is 1 based
),
stepsize,
current_chrom_size,
Expand Down Expand Up @@ -390,7 +386,6 @@ pub fn uniwig_main(
current_chrom_size,
smoothsize,
stepsize,
wig_shift,
);
match output_type {
"file" => {
Expand All @@ -411,6 +406,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_end.0,
smoothsize,
0,
),
);
write_to_bed_graph_file(
Expand All @@ -432,6 +428,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_end.0,
smoothsize,
0, // ends already 1 based, do not shift further
),
stepsize,
current_chrom_size,
Expand All @@ -450,6 +447,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_end.0,
smoothsize,
0
),
stepsize,
meta_data_file_names[1].clone(),
Expand All @@ -468,6 +466,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_end.0,
smoothsize,
0
),
stepsize,
meta_data_file_names[1].clone(),
Expand All @@ -481,7 +480,6 @@ pub fn uniwig_main(
&chromosome.ends,
current_chrom_size,
stepsize,
wig_shift,
);
match output_type {
"file" => {
Expand All @@ -499,7 +497,10 @@ pub fn uniwig_main(
let count_info: (Vec<u32>, Vec<u32>, Vec<u32>) =
compress_counts(
&mut core_results,
primary_start.0,
clamped_start_position_zero_pos(
primary_start.0,
0,
),
);
write_to_bed_graph_file(
&count_info,
Expand All @@ -517,7 +518,7 @@ pub fn uniwig_main(
&core_results.0,
file_name.clone(),
chrom_name.clone(),
clamped_start_position(primary_start.0, 0),
clamped_start_position(primary_start.0, 0,1), //starts are 1 based must be shifted by 1
stepsize,
current_chrom_size,
);
Expand All @@ -531,7 +532,10 @@ pub fn uniwig_main(
&core_results.0,
file_name.clone(),
chrom_name.clone(),
primary_start.0,
clamped_start_position_zero_pos(
primary_start.0,
0,
),
stepsize,
meta_data_file_names[2].clone(),
);
Expand All @@ -546,7 +550,10 @@ pub fn uniwig_main(
&core_results.0,
file_name.clone(),
chrom_name.clone(),
primary_start.0,
clamped_start_position_zero_pos(
primary_start.0,
0,
),
stepsize,
meta_data_file_names[2].clone(),
);
Expand Down
5 changes: 3 additions & 2 deletions gtars/tests/data/out/_core.wig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
fixedStep chrom=chr1 start=2 step=1
fixedStep chrom=chr1 start=3 step=1
2
2
3
4
2
2
1
2
1
1
Expand Down
1 change: 1 addition & 0 deletions gtars/tests/data/out/_end.wig
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fixedStep chrom=chr1 start=5 step=1
0
0
0
0
0
3 changes: 2 additions & 1 deletion gtars/tests/data/out/_start.wig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fixedStep chrom=chr1 start=1 step=1
fixedStep chrom=chr1 start=2 step=1
2
2
3
Expand All @@ -16,4 +16,5 @@ fixedStep chrom=chr1 start=1 step=1
0
0
0
0
0
10 changes: 7 additions & 3 deletions gtars/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ mod tests {
&chromosome.ends,
current_chrom_size,
stepsize,
0,
);
}
}
Expand All @@ -507,7 +506,6 @@ mod tests {
current_chrom_size,
smooth_size,
stepsize,
0,
);
}
}
Expand Down Expand Up @@ -675,8 +673,10 @@ mod tests {

let tempbedpath = format!("{}{}", path_to_crate, "/tests/data/test5.bed");
let combinedbedpath = tempbedpath.as_str();
//let combinedbedpath = "/home/drc/Downloads/unwig_testing_19dec2024/input/dummy4.bed";

let chromsizerefpath = combinedbedpath;
//let chromsizerefpath = "/home/drc/Downloads/unwig_testing_19dec2024/input/dummy.chrom.sizes";

let tempdir = tempfile::tempdir().unwrap();
let path = PathBuf::from(&tempdir.path());
Expand All @@ -685,8 +685,12 @@ mod tests {
let bwfileheader_path = path.into_os_string().into_string().unwrap();
let bwfileheader = bwfileheader_path.as_str();

let smoothsize: i32 = 5;
//let bwfileheader = "/home/drc/Downloads/unwig_testing_19dec2024/output/npy_output/";
//let bwfileheader = "/home/drc/Downloads/unwig_testing_19dec2024/output/wig_output/";

let smoothsize: i32 = 2;
let output_type = "npy";
//let output_type = "wig";
let filetype = "bed";
let num_threads = 6;
let score = false;
Expand Down

0 comments on commit 5c66be9

Please sign in to comment.