Thomas 7 сар өмнө
parent
commit
3c1d73bab5
1 өөрчлөгдсөн 25 нэмэгдсэн , 4 устгасан
  1. 25 4
      src/collection/bam.rs

+ 25 - 4
src/collection/bam.rs

@@ -16,6 +16,8 @@ use serde::{Deserialize, Serialize};
 
 use crate::config::Config;
 
+use super::flowcells::FlowCell;
+
 #[derive(Debug, Clone, Deserialize, Serialize)]
 pub struct WGSBam {
     pub id: String,
@@ -247,6 +249,25 @@ pub fn bam_compo(file_path: &str, sample_size: usize) -> anyhow::Result<Vec<(Str
         .collect())
 }
 
+pub fn bam_has_fc(bam: &WGSBam, flow_cell: &FlowCell) -> anyhow::Result<bool> {
+    let mut bam = rust_htslib::bam::Reader::from_path(&bam.path)?;
+
+    let mut rgs: HashMap<String, u64> = HashMap::new();
+    for result in bam.records().filter_map(Result::ok).take(2_000) {
+        if let rust_htslib::bam::record::Aux::String(s) = result.aux(b"fn")? {
+            *rgs.entry(s.to_string()).or_default() += 1;
+        }
+    }
+
+    for k in rgs.keys() {
+        if k.contains(&flow_cell.sample_sheet.flow_cell_id) {
+            return Ok(true);
+        }
+    }
+
+    Ok(false)
+}
+
 pub struct BamReadsSampler {
     pub positions: Vec<(String, u64)>,
     pub reader: rust_htslib::bam::IndexedReader,
@@ -679,9 +700,9 @@ pub enum PileBase {
     G,
     T,
     N,
-    Ins,  // insertion immediately after the queried base
-    Del((Vec<u8>, u32)),  // deletion covering the queried base
-    Skip, // reference skip (N)
+    Ins,                 // insertion immediately after the queried base
+    Del((Vec<u8>, u32)), // deletion covering the queried base
+    Skip,                // reference skip (N)
 }
 
 /// Decode one HTSlib 4bit nucleotide (0=A,1=C,2=G,3=T,4=N, …) to `Base`
@@ -729,7 +750,7 @@ pub fn base_at_new(
         /* Does the queried reference coordinate fall inside this CIGAR op? */
         if ref_pos + consume_ref > at_pos {
             return Ok(Some(match op {
-                Cigar::Del(e) => PileBase::Del((record.qname().to_vec(), *e )),
+                Cigar::Del(e) => PileBase::Del((record.qname().to_vec(), *e)),
                 Cigar::RefSkip(_) => PileBase::Skip,
                 _ => {
                     let offset = (at_pos - ref_pos) as usize;