Skip to content

Commit

Permalink
Fix bug in get_orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
rrwick committed Jan 11, 2024
1 parent 590d455 commit dfad550
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::misc::{quit_with_error, reverse_complement};
use crate::pileup::Pileup;

use std::collections::HashMap;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::{prelude::*, BufReader};
Expand Down Expand Up @@ -203,6 +204,14 @@ impl Alignment {
}
}

impl fmt::Display for Alignment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let strand = if self.is_on_forward_strand() { "+" } else { "-" };
write!(f, "{}:{}{}:{}-{}",
self.read_name, self.ref_name, strand, self.ref_start, self.get_ref_end())
}
}


pub fn process_sam(filename: &PathBuf, pileups: &mut HashMap<String, Pileup>,
max_errors: u32) -> (usize, usize, usize) {
Expand Down
29 changes: 11 additions & 18 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,24 @@ fn get_insert_size_thresholds(alignments: &HashMap<String, Vec<Alignment>>,
}


fn get_orientation(alignment_1: &Alignment, alignment_2: &Alignment) -> String {
let strand_1 = if alignment_1.is_on_forward_strand() { 'f' } else { 'r' };
let strand_2 = if alignment_2.is_on_forward_strand() { 'f' } else { 'r' };
fn get_orientation(a_1: &Alignment, a_2: &Alignment) -> String {
let strand_1 = if a_1.is_on_forward_strand() { 'f' } else { 'r' };
let strand_2 = if a_2.is_on_forward_strand() { 'f' } else { 'r' };

// Get the read start positions, which is the ref-end position if on the negative strand.
let a_1_pos = if a_1.is_on_forward_strand() { a_1.ref_start } else { a_1.get_ref_end() };
let a_2_pos = if a_2.is_on_forward_strand() { a_2.ref_start } else { a_2.get_ref_end() };

match (strand_1, strand_2) {
('f', 'r') | ('r', 'f') => {
if alignment_1.ref_start < alignment_2.ref_start {
if a_1_pos < a_2_pos {
format!("{}{}", strand_1, strand_2)
} else {
format!("{}{}", strand_2, strand_1)
}
},
('f', 'f') => {
if alignment_1.ref_start < alignment_2.ref_start {
"ff".to_string()
} else {
"rr".to_string()
}
},
('r', 'r') => {
if alignment_2.ref_start < alignment_1.ref_start {
"ff".to_string()
} else {
"rr".to_string()
}
},
('f', 'f') => { if a_1_pos < a_2_pos { "ff".to_string() } else { "rr".to_string() } },
('r', 'r') => { if a_2_pos < a_1_pos { "ff".to_string() } else { "rr".to_string() } },
_ => unreachable!()
}
}
Expand Down

0 comments on commit dfad550

Please sign in to comment.