Browse Source

filter cat

Thomas 1 year ago
parent
commit
9e347cad15
2 changed files with 105 additions and 8 deletions
  1. 2 2
      src/lib.rs
  2. 103 6
      src/variants.rs

+ 2 - 2
src/lib.rs

@@ -27,7 +27,7 @@ mod tests {
 
     #[test]
     fn load_from_vcf() -> Result<()> {
-        let name = "ROBIN";
+        let name = "FAVOT";
 
         let logger =
             env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
@@ -42,7 +42,7 @@ mod tests {
 
     #[test]
     fn load_from_db() -> Result<()> {
-        let name = "ROBIN";
+        let name = "FAVOT";
         let logger =
             env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
                 .build();

+ 103 - 6
src/variants.rs

@@ -865,8 +865,8 @@ impl Variants {
             callers_cat,
             n_caller_data as u32,
         ));
-        
-        let res = serde_json::to_string(&results)?;
+
+        // let res = serde_json::to_string(&results)?;
 
         Ok(results)
     }
@@ -911,6 +911,16 @@ impl Variants {
             mp,
         })
     }
+
+    pub fn filter_category(&self, and_categories: &Vec<Category>) -> Vec<&Variant> {
+        self.data.par_iter().flat_map(|v| {
+            if v.is_from_category(and_categories) {
+                vec![v]
+            } else {
+                vec![]
+            }
+        }).collect()
+    }
 }
 
 #[derive(Debug, Clone, Serialize)]
@@ -1000,6 +1010,8 @@ impl CallerData {
             return false;
         }
     }
+
+    
 }
 
 #[derive(Debug, Clone, Serialize, Eq, PartialEq, Deserialize)]
@@ -1097,10 +1109,11 @@ impl Variant {
         }
     }
 
-    pub fn vaf(&mut self) -> f64 {
-        let n_alt = self.get_n_alt() as f64;
-        let depth = self.get_depth() as f64;
-        n_alt / depth
+    pub fn vaf(&mut self) -> f32 {
+        let n_alt = self.get_n_alt() as f32;
+        let depth = self.get_depth() as f32;
+        self.vaf = Some(n_alt / depth);
+        self.vaf.unwrap()
     }
 
     fn is_ins(&self) -> bool {
@@ -1176,6 +1189,77 @@ impl Variant {
     pub fn get_best_vep(&self) -> Result<VEP> {
         get_best_vep(&self.get_veps())
     }
+
+    pub fn is_from_category(&self, and_categories: &Vec<Category>) -> bool {
+        let mut vec_bools = Vec::new();
+        for category in and_categories.iter() {
+            match category {
+                Category::VariantCategory(vc) => {
+                    for annotations in self.annotations.iter() {
+                        match annotations {
+                            AnnotationType::VariantCategory(vvc) => {
+                                if vc == vvc {
+                                    vec_bools.push(true);
+                                    break;
+                                }
+                            }
+                            _ => (),
+                        }
+                    }
+                }
+                Category::PositionRange { contig, from, to } => {
+                    if self.contig == *contig {
+                        match (from, to) {
+                            (None, None) => vec_bools.push(true),
+                            (None, Some(to)) => vec_bools.push(self.position <= *to),
+                            (Some(from), None) => vec_bools.push(self.position >= *from),
+                            (Some(to), Some(from)) => {
+                                vec_bools.push(self.position >= *from && self.position <= *to)
+                            }
+                        }
+                    } else {
+                        vec_bools.push(false);
+                    }
+                }
+                Category::VCFSource(_) => (),
+                Category::NCosmic(n) => {
+                    let mut bools = Vec::new();
+                    for annotations in self.annotations.iter() {
+                        match annotations {
+                            AnnotationType::Cosmic(c) => {
+                                bools.push(c.cosmic_cnt >= *n);
+                                break;
+                            }
+                            _ => (),
+                        }
+                    }
+                    vec_bools.push(bools.iter().any(|&b| b));
+                }
+                Category::NCBIFeature(ncbi_feature) => {
+                    let mut bools = Vec::new();
+                    for annotations in self.annotations.iter() {
+                        match annotations {
+                            AnnotationType::NCBIGFF(v) => {
+                                bools.push(v.feature == *ncbi_feature);
+                            }
+                            _ => (),
+                        }
+                    }
+                    vec_bools.push(bools.iter().any(|&b| b));
+                }
+                Category::VAF{ min, max } => {
+                    let v = if self.vaf.is_none() {
+                        let mut s = self.clone();
+                        s.vaf()
+                    } else {
+                        self.vaf.unwrap()
+                    };
+                    vec_bools.push(v >= *min && v <= *max);
+                },
+            }
+        }
+        vec_bools.iter().all(|&x| x)
+    }
 }
 #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
 enum AlterationCategory {
@@ -1368,6 +1452,19 @@ pub fn sort_variants(d: Vec<Variant>, dict_path: &str) -> Result<Vec<Variant>> {
         .collect())
 }
 
+pub enum Category {
+    VariantCategory(VariantCategory),
+    PositionRange {
+        contig: String,
+        from: Option<u32>,
+        to: Option<u32>,
+    },
+    VCFSource(VCFSource),
+    NCosmic(u64),
+    NCBIFeature(String),
+    VAF{min: f32, max: f32},
+}
+
 pub fn run_pipe(name: &str, multi: &MultiProgress) -> Result<()> {
     let cfg = config::Config::get()?;
     let deepvariant_diag_vcf = format!(