Thomas 8 месяцев назад
Родитель
Сommit
487af93b06
1 измененных файлов с 30 добавлено и 1 удалено
  1. 30 1
      src/variant/variant.rs

+ 30 - 1
src/variant/variant.rs

@@ -1,7 +1,7 @@
 use crate::{
     annotation::Annotations,
     collection::ShouldRun,
-    helpers::Hash128,
+    helpers::{estimate_shannon_entropy, Hash128},
     positions::{GenomePosition, GetGenomePosition, VcfPosition},
     runners::Run,
     variant::variant_collection::VariantCollection,
@@ -1654,6 +1654,35 @@ pub enum ReferenceAlternative {
     Unstructured(String),
 }
 
+impl ReferenceAlternative {
+    pub fn len(&self) -> usize {
+        match self {
+            ReferenceAlternative::Nucleotide(_) => 1,
+            ReferenceAlternative::Nucleotides(bases) => bases.len(),
+            ReferenceAlternative::Unstructured(_) => 0,
+        }
+    }
+
+    pub fn is_empty(&self) -> bool {
+        self.len() == 0
+    }
+
+    pub fn ent(&self) -> Option<f64> {
+        match self {
+            ReferenceAlternative::Nucleotide(_) => None,
+            ReferenceAlternative::Nucleotides(bases) => {
+                let seq = bases.iter().skip(1).map(|b| b.to_string()).collect::<String>();
+                if seq.len() > 1 {
+                    Some(estimate_shannon_entropy(&seq))
+                } else {
+                    None
+                }
+            },
+            ReferenceAlternative::Unstructured(_) => None,
+        }
+    }
+}
+
 impl FromStr for ReferenceAlternative {
     type Err = anyhow::Error;