Skip to content

Commit

Permalink
account for -1 shift in bam_to_bed and variable_shift_bam workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldcampbelljr committed Dec 11, 2024
1 parent a6014b3 commit 5cd0d98
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions gtars/src/uniwig/counting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,8 @@ pub fn bam_to_bed_no_counts(

let end_site = unwrapped_coord.alignment_end().unwrap().unwrap().get() as i32;

let shifted_pos = get_shifted_pos(&flags, start_site, end_site);
// we must shift the start position by -1 to convert bam/sam 1 based position to bed 0 based pos
let shifted_pos = get_shifted_pos(&flags, start_site-1, end_site);

// Relevant comment from original bamSitesToWig.py:
// The bed file needs 6 columns (even though some are dummy)
Expand All @@ -1222,6 +1223,8 @@ pub fn bam_to_bed_no_counts(
strand,
);

//eprintln!("here is shifted with smoothing: {} {}", shifted_pos - smoothsize, shifted_pos + smoothsize);

writer.write_all(single_line.as_bytes())?;
writer.flush()?;
}
Expand All @@ -1242,7 +1245,7 @@ pub fn variable_shifted_bam_to_bw( records: &mut Box<Query<noodles::bgzf::reader
let mut write_lock = write_fd.lock().unwrap(); // Acquire lock for writing
let mut writer = BufWriter::new(&mut *write_lock);

let mut coordinate_position = 1;
let mut coordinate_position = 0;

let mut prev_count: i32 = 0;
let mut count: i32 = 0;
Expand Down Expand Up @@ -1288,15 +1291,15 @@ pub fn variable_shifted_bam_to_bw( records: &mut Box<Query<noodles::bgzf::reader

let end_site = first_record.alignment_end().unwrap().unwrap().get() as i32;

let shifted_pos = get_shifted_pos(&flags, start_site, end_site);
let shifted_pos = get_shifted_pos(&flags, start_site - 1, end_site); // we must shift the start position by -1 to convert bam/sam 1 based position to bedgraph 0 based pos

let mut adjusted_start_site = shifted_pos - smoothsize;

//current_end_site = adjusted_start_site;
current_end_site = adjusted_start_site + 1 + smoothsize * 2;

if adjusted_start_site < 1 {
adjusted_start_site = 1;
if adjusted_start_site < 0 {
adjusted_start_site = 0; // must ensure we start at 0 for bedGraph 0 position

Check warning on line 1302 in gtars/src/uniwig/counting.rs

View check run for this annotation

Codecov / codecov/patch

gtars/src/uniwig/counting.rs#L1302

Added line #L1302 was not covered by tests
}

while coordinate_position < adjusted_start_site {
Expand All @@ -1314,15 +1317,15 @@ pub fn variable_shifted_bam_to_bw( records: &mut Box<Query<noodles::bgzf::reader

let end_site = unwrapped_coord.alignment_end().unwrap().unwrap().get() as i32;

let shifted_pos = get_shifted_pos(&flags, start_site, end_site);
let shifted_pos = get_shifted_pos(&flags, start_site - 1, end_site);

adjusted_start_site = shifted_pos - smoothsize;


count += 1;

if adjusted_start_site < 1 {
adjusted_start_site = 1;
if adjusted_start_site < 0 {
adjusted_start_site = 0;

Check warning on line 1328 in gtars/src/uniwig/counting.rs

View check run for this annotation

Codecov / codecov/patch

gtars/src/uniwig/counting.rs#L1328

Added line #L1328 was not covered by tests
}

let new_end_site = adjusted_start_site + 1 + smoothsize * 2;
Expand Down Expand Up @@ -1507,5 +1510,8 @@ pub fn get_shifted_pos(flags: &Flags, start_site:i32, end_site:i32) -> i32 {
}

Check warning on line 1510 in gtars/src/uniwig/counting.rs

View check run for this annotation

Codecov / codecov/patch

gtars/src/uniwig/counting.rs#L1505-L1510

Added lines #L1505 - L1510 were not covered by tests
}

//eprintln!("Here is read.reference_start {} and read.reference_end {}", start_site, end_site);
//eprintln!("here is shifted_pos -> {shifted_pos}");

shifted_pos
}

0 comments on commit 5cd0d98

Please sign in to comment.