Эх сурвалжийг харах

working likely phases mono and biall

Thomas 1 жил өмнө
parent
commit
14cc867a05
1 өөрчлөгдсөн 47 нэмэгдсэн , 9 устгасан
  1. 47 9
      src/lib.rs

+ 47 - 9
src/lib.rs

@@ -4,7 +4,7 @@ use log::info;
 use minimap2::{Aligner, Mapping};
 use noodles_fasta as fasta;
 use num_format::{CustomFormat, Grouping, Locale, ToFormattedString, WriteFormatted};
-use petgraph::{algo, prelude::*, Graph};
+use petgraph::{algo, dot::Dot, prelude::*, Graph};
 use rust_htslib::bam::{self, Record};
 use std::{
     collections::{HashMap, VecDeque},
@@ -1031,8 +1031,7 @@ pub fn rename_edges(
     graph: &mut StableGraph<String, String>,
     way: &Vec<NodeIndex>,
     value: &str,
-) -> StableGraph<String, String> {
-    let mut graph = graph.clone();
+) -> &StableGraph<String, String> {
     graph.edge_weights_mut().for_each(|e| *e = "".to_string());
     for window in way.windows(2) {
         let edge_id = graph.find_edge(window[0], window[1]).unwrap();
@@ -1046,6 +1045,47 @@ pub fn erase_edges_labels_inplace(graph: &mut StableGraph<String, String>) {
     graph.edge_weights_mut().for_each(|e| *e = "".to_string());
 }
 
+pub fn dot_graph(
+    graph: &StableGraph<String, String>,
+    way: &Vec<NodeIndex>,
+    value: &str,
+    color: &str,
+    erase: bool,
+) -> String {
+    let mut g = graph.clone();
+
+    // erase labels
+    if erase {
+        g.edge_weights_mut().for_each(|e| *e = "".to_string());
+    }
+
+    let mut labels = Vec::new();
+    for [a, b] in way.windows(2) {
+        let edge_id = g.find_edge(*a, *b).unwrap();
+        let edge = g.edge_weight_mut(edge_id).unwrap();
+        let v = if edge != "" {
+            let mut v: Vec<&str> = edge.split(",").filter(|e| !e.is_empty()).collect();
+            v.push(value);
+            v.join(", ")
+        } else {
+            value.to_string()
+        };
+        labels.push(v.clone());
+        *edge = v;
+    }
+
+    let dot = Dot::new(&g)
+        .to_string()
+        .replace("\\\"", "");
+
+    labels.sort_by(|a, b| b.len().cmp(&a.len()));
+    for label in labels {
+        let label_str = format!("label = \"{label}\"");
+        dot.replace(&label_str, &format!("{label_str}, color = \"{color}\""));
+    }
+    dot
+}
+
 #[cfg(test)]
 mod tests {
     use std::ops::Deref;
@@ -1254,17 +1294,15 @@ mod tests {
                         .join(";")
                 );
                 let new_g = rename_edges(&mut empty_edges, b, "A");
-                let dot = Dot::new(&new_g);
-                let u = dot.to_string().replace("\\\"", "");
-                let u = u.replace("label = \"A\"", "label = \"A\", color = \"red\"");
+                // let dot = Dot::new(&new_g);
+                // let u = dot.to_string().replace("\\\"", "");
+                // let u = u.replace("label = \"A\"", "label = \"A\", color = \"red\"");
 
-                println!("{}", u);
+                println!("{}", dot_graph(&g, b, "A", "red", true));
                 monoallelic_graphs.push(new_g);
             }
         }
 
-
-
         // Try to find two ways containig all nodes
         let mut bial_ways = Vec::new();
         for a_way in ways.iter() {