Skip to content

Commit

Permalink
Enable updates to blockgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris7 committed Oct 29, 2024
1 parent 002a3a5 commit 6248a66
Show file tree
Hide file tree
Showing 8 changed files with 729 additions and 32 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde_json = "1.0.128"
sha2 = "0.10.8"
tempfile = "3.12.0"
tempdir = "0.3.7"
interavl = "0.2.0"

[dev-dependencies]
cargo-llvm-cov = "0.6.13"
26 changes: 14 additions & 12 deletions src/exports/gfa.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use itertools::Itertools;
use petgraph::prelude::DiGraphMap;
use rusqlite::{types::Value as SQLValue, Connection};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;

use crate::graph::{GraphEdge, GraphNode};
use crate::models::{
block_group::BlockGroup,
block_group_edge::BlockGroupEdge,
Expand All @@ -16,6 +9,13 @@ use crate::models::{
path_edge::PathEdge,
strand::Strand,
};
use itertools::Itertools;
use petgraph::prelude::DiGraphMap;
use rusqlite::{types::Value as SQLValue, Connection};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;

pub fn export_gfa(
conn: &Connection,
Expand Down Expand Up @@ -104,12 +104,14 @@ fn segment_line(sequence: &str, node_id: i64, sequence_start: i64) -> String {

fn write_links(
writer: &mut BufWriter<File>,
graph: &DiGraphMap<i64, ()>,
graph: &DiGraphMap<GraphNode, GraphEdge>,
edges_by_node_pair: &HashMap<(i64, i64), Edge>,
node_sequence_starts_by_end_coordinate: HashMap<(i64, i64), i64>,
) {
for (source, target, ()) in graph.all_edges() {
let edge = edges_by_node_pair.get(&(source, target)).unwrap();
for (source, target, edge_weight) in graph.all_edges() {
let edge = edges_by_node_pair
.get(&(source.node_id, target.node_id))
.unwrap();
if Node::is_terminal(edge.source_node_id) || Node::is_terminal(edge.target_node_id) {
continue;
}
Expand All @@ -135,7 +137,7 @@ fn write_links(
.unwrap_or_else(|_| {
panic!(
"Error writing link from segment {} to {} to GFA stream",
source, target,
source.node_id, target.node_id,
)
});
}
Expand Down
63 changes: 63 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ where
})
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct GraphNode {
pub block_id: i64,
pub node_id: i64,
pub sequence_start: i64,
pub sequence_end: i64,
}

impl GraphNode {
pub fn length(&self) -> i64 {
self.sequence_end - self.sequence_start
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct GraphEdge {
pub edge_id: i64,
pub chromosome_index: i64,
pub phased: i64,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -161,4 +182,46 @@ mod tests {
HashSet::from_iter(expected_paths)
);
}

#[test]
fn test_ultra_bubble_path() {
// This graph looks like this:
// 8
// / \
// 6 -> 7
// / \
// 1 -> 2 -> 3 -> 4 -> 5
//
// We ensure that we capture all 3 paths from 1 -> 5
let mut graph: DiGraphMap<i64, ()> = DiGraphMap::new();
graph.add_node(1);
graph.add_node(2);
graph.add_node(3);
graph.add_node(4);
graph.add_node(5);
graph.add_node(6);
graph.add_node(7);
graph.add_node(8);
graph.add_node(9);

graph.add_edge(1, 2, ());
graph.add_edge(2, 3, ());
graph.add_edge(3, 4, ());
graph.add_edge(4, 5, ());
graph.add_edge(2, 6, ());
graph.add_edge(6, 7, ());
graph.add_edge(7, 4, ());
graph.add_edge(6, 8, ());
graph.add_edge(8, 7, ());

let paths = all_simple_paths(&graph, 1, 5).collect::<Vec<Vec<i64>>>();
assert_eq!(
HashSet::<Vec<i64>>::from_iter::<Vec<Vec<i64>>>(paths),
HashSet::from_iter(vec![
vec![1, 2, 3, 4, 5],
vec![1, 2, 6, 7, 4, 5],
vec![1, 2, 6, 8, 7, 4, 5]
])
);
}
}
Loading

0 comments on commit 6248a66

Please sign in to comment.