Browse Source

augm +/- all positions fetch

Thomas 1 year ago
parent
commit
ca76fd0e93
2 changed files with 9 additions and 9 deletions
  1. 8 8
      src/bam.rs
  2. 1 1
      src/lib.rs

+ 8 - 8
src/bam.rs

@@ -1,7 +1,5 @@
 use log::warn;
-use rust_htslib::bam::{
-    ext::BamRecordExtensions, record::Aux, IndexedReader, Read, Record,
-};
+use rust_htslib::bam::{ext::BamRecordExtensions, record::Aux, IndexedReader, Read, Record};
 use std::collections::HashMap;
 
 pub fn primary_record(bam: &mut IndexedReader, record: Record) -> Record {
@@ -126,10 +124,10 @@ pub fn get_all_positions(
     bam_path: &str,
     // bam: &mut IndexedReader,
 ) -> anyhow::Result<Vec<(String, u32, u32)>> {
-    let mut bam = rust_htslib::bam::IndexedReader::from_path(bam_path).unwrap();
-    let header = bam.header().to_owned();
     let mut positions = Vec::new();
     let qname = record.qname();
+    let mut bam = rust_htslib::bam::IndexedReader::from_path(bam_path).unwrap();
+    let header = bam.header().to_owned();
     let contig = String::from_utf8(header.tid2name(record.tid().try_into()?).to_vec())?;
     positions.push((
         contig,
@@ -150,7 +148,7 @@ pub fn get_all_positions(
             // let cigar_str = *parts.get(3).unwrap();
             // let end = calculate_end_position(start, cigar_str).unwrap();
 
-            bam.fetch((chr, start, start + 1))?;
+            bam.fetch((chr, start - 2, start + 2))?;
             let mut founded = false;
             for record in bam.records().flatten() {
                 if qname == record.qname() {
@@ -163,8 +161,10 @@ pub fn get_all_positions(
             }
         }
     }
-    positions.sort_by(|a, b| a.1.cmp(&b.1));
-    positions.sort_by(|a, b| a.0.cmp(&b.0));
+    positions.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(&b.1)));
+
+    // positions.sort_by(|a, b| a.1.cmp(&b.1));
+    // positions.sort_by(|a, b| a.0.cmp(&b.0));
     positions.dedup();
     Ok(positions)
 }

+ 1 - 1
src/lib.rs

@@ -370,7 +370,7 @@ mod tests {
     #[test]
     fn whole() {
         init();
-        let id = "LEVASSEUR";
+        let id = "ROBIN";
         let bam_path = format!("/data/longreads_basic_pipe/{id}/diag/{id}_diag_hs1.bam");
         let out_dir = format!("/data/longreads_basic_pipe/{id}/diag/scan");
         par_whole_scan("/data/ref/hs1/chm13v2.0.dict", &bam_path, &out_dir).unwrap();