Browse Source

variant.n_cosmic()

Thomas 9 months ago
parent
commit
57321e6099
2 changed files with 36 additions and 15 deletions
  1. 2 0
      src/pipes/somatic.rs
  2. 34 15
      src/variant/variant_collection.rs

+ 2 - 0
src/pipes/somatic.rs

@@ -239,6 +239,7 @@ impl Run for Somatic {
         let id = self.id.clone();
 
         let result_json = format!("{}/somatic_variants.json.gz", config.tumoral_dir(&id));
+        let result_bit = format!("{}/somatic_variants.bit", config.tumoral_dir(&id));
 
         if Path::new(&result_json).exists() && !config.somatic_pipe_force {
             return Err(anyhow::anyhow!("already exists"));
@@ -512,6 +513,7 @@ impl Run for Somatic {
 
         info!("Final unique variants: {}", variants.data.len());
         variants.save_to_json(&result_json)?;
+        variants.save_to_file(&result_bit)?;
 
         Ok(())
     }

+ 34 - 15
src/variant/variant_collection.rs

@@ -426,7 +426,13 @@ impl PartialEq for Variant {
 
 impl Variant {
     pub fn id(&self) -> String {
-        format!("{}:{}_{}_{}", self.position.contig(), self.position.position + 1, self.reference, self.alternative)
+        format!(
+            "{}:{}_{}_{}",
+            self.position.contig(),
+            self.position.position + 1,
+            self.reference,
+            self.alternative
+        )
     }
 
     pub fn vep(&self) -> Vec<VEP> {
@@ -464,6 +470,19 @@ impl Variant {
 
         (mean(&n_alts), mean(&depths))
     }
+
+    pub fn n_cosmic(&self) -> u64 {
+        self.annotations
+            .iter()
+            .find_map(|a| {
+                if let Annotation::Cosmic(c) = a {
+                    Some(c.cosmic_cnt)
+                } else {
+                    None
+                }
+            })
+            .unwrap_or_default()
+    }
 }
 
 /// A collection of genomic variants.
@@ -744,7 +763,7 @@ impl Variants {
         Ok(variants)
     }
 
-    pub fn save_to_file(&self, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
+    pub fn save_to_file(&self, filename: &str) -> anyhow::Result<()> {
         info!("Saving file: {filename}");
         let encoded = bitcode::encode(self);
         let mut file = File::create(filename)?;
@@ -752,7 +771,7 @@ impl Variants {
         Ok(())
     }
 
-    pub fn load_from_file(filename: &str) -> Result<Self, Box<dyn std::error::Error>> {
+    pub fn load_from_file(filename: &str) -> anyhow::Result<Self> {
         info!("Load from file: {filename}");
         let mut file = File::open(filename)?;
         let mut buffer = Vec::new();
@@ -760,8 +779,6 @@ impl Variants {
         let decoded: Self = bitcode::decode(&buffer)?;
         Ok(decoded)
     }
-
-
 }
 
 #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -838,7 +855,6 @@ impl VariantsStats {
             *n_alts.entry(n_alt as u32).or_default() += 1;
             *depths.entry(depth as u32).or_default() += 1;
 
-            
             let vaf = OrderedFloat::from(((n_alt * 1_000.0 / depth).round() / 10.0) as f32);
             if !vaf.is_nan() {
                 *vafs.entry(vaf).or_default() += 1;
@@ -870,13 +886,16 @@ impl VariantsStats {
                 .entry(alteration_category_str.join(", "))
                 .or_default() += 1;
         });
-        
+
         let mut n_gnomad = 0;
-        let gnomad = gnomads.iter().map(|e| {
-            let data = e.value().to_vec();
-            n_gnomad = data.len();
-            (e.key().to_string(), bin_data(data, 0.0001))
-        }).collect();
+        let gnomad = gnomads
+            .iter()
+            .map(|e| {
+                let data = e.value().to_vec();
+                n_gnomad = data.len();
+                (e.key().to_string(), bin_data(data, 0.0001))
+            })
+            .collect();
 
         Self {
             n,
@@ -901,9 +920,9 @@ impl VariantsStats {
         serde_json::to_writer(&mut writer, self)
             .map_err(|e| anyhow::anyhow!("Failed to serialize JSON to file: {}\n{e}", filename))?;
 
-        writer
-            .close()
-            .map_err(|e| anyhow::anyhow!("Failed to close BGZF writer for file: {}\n{e}", filename))?;
+        writer.close().map_err(|e| {
+            anyhow::anyhow!("Failed to close BGZF writer for file: {}\n{e}", filename)
+        })?;
 
         debug!("Successfully saved variants to {}", filename);
         Ok(())