Skip to content

Commit

Permalink
bed fix off-by-one since noodles uses 1-based
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Dec 20, 2023
1 parent fcd03ff commit b69ac3a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
18 changes: 17 additions & 1 deletion examples/intersect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn main() -> io::Result<()> {
&args.a_requirements,
&args.b_requirements,
);
eprintln!("{:?} {:?}", report.len(), report);
//eprintln!("{:?} {:?}", report.len(), report);
//eprintln!("a reqs: {:?}", args.a_requirements);

if args.count {
Expand All @@ -106,6 +106,22 @@ fn main() -> io::Result<()> {
)?;
continue;
}
if args.count_base {
writeln!(
&mut stdout,
"{}\t{}\t{}\t{}",
intersection.base_interval.chrom(),
intersection.base_interval.start(),
intersection.base_interval.stop(),
report
.count_bases_by_id()
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",")
)?;
continue;
}

writeln!(
&mut stdout,
Expand Down
6 changes: 3 additions & 3 deletions src/bedder_bed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl crate::position::Positioned for bed::record::Record<3> {

fn set_start(&mut self, start: u64) {
// must build a new record to set start.
let pstart = core::Position::try_from(start as usize).expect("invalid start");
let pstart = core::Position::try_from(start as usize + 1).expect("invalid start");
let record = bed::Record::<3>::builder()
.set_reference_sequence_name(self.reference_sequence_name())
.set_start_position(pstart)
Expand All @@ -33,9 +33,9 @@ impl crate::position::Positioned for bed::record::Record<3> {
*self = record;
}

fn set_stop(&mut self, start: u64) {
fn set_stop(&mut self, stop: u64) {
// must build a new record to set start.
let pstop = core::Position::try_from(start as usize + 1).expect("invalid start");
let pstop = core::Position::try_from(stop as usize).expect("invalid stop");
let record = bed::Record::<3>::builder()
.set_reference_sequence_name(self.reference_sequence_name())
.set_start_position(self.start_position())
Expand Down
21 changes: 21 additions & 0 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,25 @@ impl Report {
});
result
}

/// The number of b-bases in each fragment from each source(id)
/// This is determined by the overlap requirements, modes, and parts.
pub fn count_bases_by_id(&self) -> Vec<u64> {
let mut result = Vec::new();
self.0.iter().for_each(|frag| {
eprintln!("frag: {:?}", frag);
if frag.id >= result.len() {
result.resize(frag.id + 1, 0);
}
result[frag.id] += frag
.b
.iter()
.map(|pos| {
eprintln!("start: {:?} stop: {:?}", pos.start(), pos.stop());
pos.stop() - pos.start()
})
.sum::<u64>();
});
result
}
}

0 comments on commit b69ac3a

Please sign in to comment.