Browse Source

Label Trait and impl

Thomas 8 months ago
parent
commit
3700d19af7

+ 8 - 3
src/callers/clairs.rs

@@ -7,7 +7,7 @@ use crate::{
     io::vcf::read_vcf,
     runners::{run_wait, DockerRun, Run},
     variant::{
-        variant::{RunnerVariants, Variants},
+        variant::{Label, Variants},
         variant_collection::VariantCollection,
     },
 };
@@ -51,6 +51,7 @@ impl Initialize for ClairS {
     /// Otherwise, the output directory is cleared for a fresh run.
     fn initialize(id: &str, config: Config) -> anyhow::Result<Self> {
         let id = id.to_string();
+
         info!("Initialize ClairS for {id}.");
         let log_dir = format!("{}/{}/log/clairs", config.result_dir, &id);
 
@@ -294,5 +295,9 @@ impl ClairS {
     }
 }
 
-/// Marker trait implementation to signal ClairS supports variant export.
-impl RunnerVariants for ClairS {}
+impl Label for ClairS {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}
+

+ 6 - 5
src/callers/deep_somatic.rs

@@ -13,7 +13,7 @@ use crate::{
     io::vcf::read_vcf,
     runners::{run_wait, DockerRun, Run},
     variant::{
-        variant::{RunnerVariants, Variants},
+        variant::{Label, Variants},
         variant_collection::VariantCollection,
     },
 };
@@ -201,7 +201,8 @@ impl Variants for DeepSomatic {
     }
 }
 
-// impl  for DeepSomatic {}
-
-/// Marker trait implementation to signal DeepSomatic supports variant export.
-impl RunnerVariants for DeepSomatic {}
+impl Label for DeepSomatic {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}

+ 7 - 3
src/callers/deep_variant.rs

@@ -12,7 +12,7 @@ use crate::{
     io::vcf::read_vcf,
     runners::{run_wait, DockerRun, Run},
     variant::{
-        variant::{RunnerVariants, Variants},
+        variant::{Label, Variants},
         variant_collection::VariantCollection,
     },
 };
@@ -240,5 +240,9 @@ impl Variants for DeepVariant {
     }
 }
 
-/// Marker trait implementation to signal DeepVariant supports variant export.
-impl RunnerVariants for DeepVariant {}
+impl Label for DeepVariant {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}
+

+ 11 - 4
src/callers/nanomonsv.rs

@@ -17,7 +17,7 @@ use crate::{
     io::vcf::read_vcf,
     runners::{run_wait, CommandRun, Run, RunReport},
     variant::{
-        variant::{RunnerVariants, Variants},
+        variant::{Label, Variants},
         variant_collection::VariantCollection,
     },
 };
@@ -197,8 +197,11 @@ impl Variants for NanomonSV {
     }
 }
 
-/// Marker implementation to integrate `NanomonSV` into the unified runner interface.
-impl RunnerVariants for NanomonSV {}
+impl Label for NanomonSV {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}
 
 /// SOLO
 #[derive(Debug)]
@@ -308,7 +311,11 @@ impl CallerCat for NanomonSVSolo {
     }
 }
 
-impl RunnerVariants for NanomonSVSolo {}
+impl Label for NanomonSVSolo {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}
 
 impl Variants for NanomonSVSolo {
     fn variants(&self, annotations: &Annotations) -> anyhow::Result<VariantCollection> {

+ 6 - 2
src/callers/savana.rs

@@ -11,7 +11,7 @@ use crate::{
     positions::{num_to_contig, GenomeRange},
     runners::{run_wait, CommandRun, Run},
     variant::{
-        variant::{RunnerVariants, Variants},
+        variant::{Label, Variants},
         variant_collection::VariantCollection,
     },
 };
@@ -265,7 +265,11 @@ impl Variants for Savana {
     }
 }
 
-impl RunnerVariants for Savana {}
+impl Label for Savana {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}
 
 pub struct SavanaCNRow {
     pub range: GenomeRange,

+ 6 - 2
src/callers/severus.rs

@@ -10,7 +10,7 @@ use crate::{
     io::vcf::read_vcf,
     runners::{run_wait, CommandRun, Run},
     variant::{
-        variant::{RunnerVariants, Variants},
+        variant::{Label, Variants},
         variant_collection::VariantCollection,
     },
 };
@@ -240,7 +240,11 @@ impl Variants for Severus {
     }
 }
 
-impl RunnerVariants for Severus {}
+impl Label for Severus {
+    fn label(&self) -> String {
+        self.caller_cat().to_string()
+    }
+}
 
 /// ========================================================================
 

+ 10 - 2
src/scan/scan.rs

@@ -15,6 +15,7 @@ use crate::io::writers::get_gz_writer;
 use crate::math::filter_outliers_modified_z_score_with_indices;
 
 use crate::runners::Run;
+use crate::variant::variant::Label;
 use crate::{config::Config, io::dict::read_dict, scan::bin::Bin};
 
 /// Represents a count of reads in a genomic bin, including various metrics and outlier information.
@@ -291,7 +292,7 @@ pub fn par_whole_scan(id: &str, time_point: &str, config: &Config) -> anyhow::Re
             .into_par_iter()
             .map_init(
                 || IndexedReader::from_path(bam_path).unwrap(), // per-thread reader
-                |mut bam_reader, i| {
+                |bam_reader, i| {
                     // .flat_map(|i| {
                     // Calculate chunk start position
                     let chunk_start = i * chunk_n_bin * bin_size;
@@ -319,7 +320,7 @@ pub fn par_whole_scan(id: &str, time_point: &str, config: &Config) -> anyhow::Re
                             let bin_length = std::cmp::min(bin_size, chunk_length - j * bin_size);
                             // debug!("chunk start:{chunk_start}, length: {chunk_length}, n_bins: {n_bins_in_chunk}, first bin start: {bin_start} bin length: {bin_length}");
                             match Bin::new(
-                                &mut bam_reader,
+                                bam_reader,
                                 &contig,
                                 bin_start,
                                 bin_length,
@@ -654,3 +655,10 @@ impl Run for SomaticScan {
         par_whole_scan(&self.id, &self.config.tumoral_name, &self.config)
     }
 }
+
+impl Label for SomaticScan {
+    fn label(&self) -> String {
+        "Somatic Scan".to_string()
+    }
+}
+

+ 36 - 27
src/variant/variant.rs

@@ -1,6 +1,6 @@
 use crate::{
     annotation::Annotations,
-    collection::{Initialize, ShouldRun},
+    collection::ShouldRun,
     helpers::Hash128,
     positions::{GenomePosition, GetGenomePosition, VcfPosition},
     runners::Run,
@@ -8,7 +8,7 @@ use crate::{
 };
 use anyhow::{anyhow, Context};
 use bitcode::{Decode, Encode};
-use log::{info, warn};
+use log::{error, info};
 use rayon::prelude::*;
 use serde::{Deserialize, Serialize};
 use std::{cmp::Ordering, collections::HashSet, fmt, hash::Hash, str::FromStr};
@@ -1042,6 +1042,10 @@ pub trait VariantId {
     fn variant_id(&self) -> String;
 }
 
+pub trait Label {
+    fn label(&self) -> String;
+}
+
 // pub trait AsAny {
 //     fn as_any(&self) -> &dyn std::any::Any;
 // }
@@ -1060,10 +1064,10 @@ pub trait VariantId {
 /// - Is thread-safe (`Send + Sync`) to be boxed and dispatched concurrently
 ///
 /// Components implementing this trait can be boxed as `ShouldRunBox`.
-pub trait ShouldRunTrait: ShouldRun + Run + Send + Sync {}
+pub trait ShouldRunTrait: ShouldRun + Run + Send + Sync + Label {}
 
 /// Blanket implementation for all compatible types.
-impl<T> ShouldRunTrait for T where T: ShouldRun + Run + Send + Sync {}
+impl<T> ShouldRunTrait for T where T: ShouldRun + Run + Send + Sync + Label {}
 
 /// A boxed trait object to hold any runner implementing `ShouldRunTrait`.
 pub type ShouldRunBox = Box<dyn ShouldRunTrait>;
@@ -1209,7 +1213,7 @@ pub fn run_if_required(iterable: &mut [ShouldRunBox]) -> anyhow::Result<()> {
         if e.should_run() {
             e.run()
         } else {
-            // info!("Skipping runner: {}", std::any::type_name::<_>()); // or add name field
+            info!("Skipping running: {}", e.label());
 
             Ok(())
         }
@@ -1220,7 +1224,10 @@ pub fn run_if_required(iterable: &mut [ShouldRunBox]) -> anyhow::Result<()> {
 /// conditional re-running, and variant extraction (VCF + annotations).
 ///
 /// Used to enable polymorphic handling of both solo and somatic callers in the pipeline.
-pub trait RunnerVariants: Run + Variants + Send + Sync {}
+pub trait RunnerVariants: Variants + Send + Sync + Label {}
+
+/// Blanket implementation for all compatible types.
+impl<T> RunnerVariants for T where T:  Variants + Send + Sync + Label {}
 
 pub type CallerBox = Box<dyn RunnerVariants + Send + Sync>;
 
@@ -1345,34 +1352,36 @@ pub fn load_variants(
     iterable
         .par_iter()
         .map(|runner| {
-            let r = runner.variants(annotations);
-            if let Err(ref e) = r {
-                warn!("{e}");
+            let result = runner.variants(annotations);
+            if let Err(ref e) = result {
+                error!("Failed to load variants from: {}\n{e}", runner.label());
+            } else {
+                info!("Variants from {} loaded.", runner.label());
             };
-            r
+            result
         })
         .filter(|r| r.is_ok())
         .collect::<anyhow::Result<Vec<_>>>()
         .map_err(|e| anyhow::anyhow!("Failed to load variants.\n{e}"))
 }
 
-pub fn par_load_variants(
-    iterable: &mut [Box<dyn Variants + Send + Sync>],
-    annotations: &Annotations,
-) -> anyhow::Result<Vec<VariantCollection>> {
-    iterable
-        .par_iter()
-        .map(|runner| {
-            let r = runner.variants(annotations);
-            if let Err(ref e) = r {
-                warn!("{e}");
-            };
-            r
-        })
-        .filter(|r| r.is_ok())
-        .collect::<anyhow::Result<Vec<_>>>()
-        .map_err(|e| anyhow::anyhow!("Failed to load variants.\n{e}"))
-}
+// pub fn par_load_variants(
+//     iterable: &mut [Box<dyn Variants + Send + Sync>],
+//     annotations: &Annotations,
+// ) -> anyhow::Result<Vec<VariantCollection>> {
+//     iterable
+//         .par_iter()
+//         .map(|runner| {
+//             let r = runner.variants(annotations);
+//             if let Err(ref e) = r {
+//                 warn!("{e}");
+//             };
+//             r
+//         })
+//         .filter(|r| r.is_ok())
+//         .collect::<anyhow::Result<Vec<_>>>()
+//         .map_err(|e| anyhow::anyhow!("Failed to load variants.\n{e}"))
+// }
 
 pub fn parallel_intersection<T: Hash + Eq + Clone + Send + Sync>(
     vec1: &[T],