diff --git a/src/exports/fasta.rs b/src/exports/fasta.rs index 109964a..f6e61a5 100644 --- a/src/exports/fasta.rs +++ b/src/exports/fasta.rs @@ -16,7 +16,7 @@ pub fn export_fasta(conn: &Connection, operation_id: i64, filename: &PathBuf) { let block_group = BlockGroup::get_by_id(conn, path.block_group_id); let definition = fasta::record::Definition::new(block_group.name, None); - let sequence = fasta::record::Sequence::from(Path::sequence(conn, path).into_bytes()); + let sequence = fasta::record::Sequence::from(path.sequence(conn).into_bytes()); let record = fasta::Record::new(definition, sequence); let _ = writer.write_record(&record); diff --git a/src/exports/gfa.rs b/src/exports/gfa.rs index 5e07122..646d89a 100644 --- a/src/exports/gfa.rs +++ b/src/exports/gfa.rs @@ -62,7 +62,7 @@ pub fn export_gfa( let boundary_edges = Edge::boundary_edges_from_sequences(&blocks); edges.extend(boundary_edges.clone()); - let (graph, edges_by_node_pair) = Edge::build_graph(&edges, &blocks); + let (graph, _edges_by_node_pair) = Edge::build_graph(&edges, &blocks); let file = File::create(filename).unwrap(); let mut writer = BufWriter::new(file); diff --git a/src/models/edge.rs b/src/models/edge.rs index 45ad238..5fad442 100644 --- a/src/models/edge.rs +++ b/src/models/edge.rs @@ -537,7 +537,7 @@ mod tests { block_group::{BlockGroup, PathChange}, block_group_edge::BlockGroupEdge, collection::Collection, - path::{Path, PathBlock}, + path::PathBlock, sequence::Sequence, }; use crate::test_helpers::{get_connection, setup_block_group}; diff --git a/src/models/path.rs b/src/models/path.rs index 50c24b3..028e41a 100644 --- a/src/models/path.rs +++ b/src/models/path.rs @@ -543,6 +543,7 @@ impl Path { }) .clone() .collect() + } pub fn new_path_for( &self, @@ -554,7 +555,7 @@ impl Path { ) -> Path { // Creates a new path from the current one by replacing all edges between path_start and // path_end with the input edges that are to and from a new node - let tree = Path::intervaltree_for(conn, self); + let tree = self.intervaltree(conn); let block_with_start = tree.query_point(path_start).next().unwrap().value.clone(); let block_with_end = tree.query_point(path_end).next().unwrap().value.clone(); @@ -2468,4 +2469,119 @@ mod tests { assert_eq!(result_annotation.start, 0); assert_eq!(result_annotation.end, 4); } + + #[test] + fn test_new_path_for() { + let conn = &mut get_connection(None); + Collection::create(conn, "test collection"); + let block_group = BlockGroup::create(conn, "test collection", None, "test block group"); + let sequence1 = Sequence::new() + .sequence_type("DNA") + .sequence("ATCGATCG") + .save(conn); + let node1_id = Node::create(conn, sequence1.hash.as_str(), None); + let edge1 = Edge::create( + conn, + PATH_START_NODE_ID, + -123, + Strand::Forward, + node1_id, + 0, + Strand::Forward, + 0, + 0, + ); + let sequence2 = Sequence::new() + .sequence_type("DNA") + .sequence("AAAAAAAA") + .save(conn); + let node2_id = Node::create(conn, sequence2.hash.as_str(), None); + let edge2 = Edge::create( + conn, + node1_id, + 8, + Strand::Forward, + node2_id, + 0, + Strand::Forward, + 0, + 0, + ); + let edge3 = Edge::create( + conn, + node2_id, + 8, + Strand::Forward, + PATH_END_NODE_ID, + -1, + Strand::Forward, + 0, + 0, + ); + + let path1 = Path::create( + conn, + "chr1", + block_group.id, + &[edge1.id, edge2.id, edge3.id], + ); + assert_eq!(path1.sequence(conn), "ATCGATCGAAAAAAAA"); + + let sequence3 = Sequence::new() + .sequence_type("DNA") + .sequence("CCCCCCCC") + .save(conn); + let node3_id = Node::create(conn, sequence3.hash.as_str(), None); + let edge4 = Edge::create( + conn, + node1_id, + 4, + Strand::Forward, + node3_id, + 0, + Strand::Forward, + 0, + 0, + ); + let edge5 = Edge::create( + conn, + node3_id, + 8, + Strand::Forward, + node2_id, + 3, + Strand::Forward, + 0, + 0, + ); + + let path2 = path1.new_path_for(conn, 4, 11, &edge4, &edge5); + assert_eq!(path2.sequence(conn), "ATCGCCCCCCCCAAAAA"); + + let edge6 = Edge::create( + conn, + node1_id, + 4, + Strand::Forward, + node3_id, + 0, + Strand::Forward, + 0, + 0, + ); + let edge7 = Edge::create( + conn, + node3_id, + 8, + Strand::Forward, + node1_id, + 7, + Strand::Forward, + 0, + 0, + ); + + let path3 = path1.new_path_for(conn, 4, 7, &edge6, &edge7); + assert_eq!(path3.sequence(conn), "ATCGCCCCCCCCGAAAAAAAA"); + } } diff --git a/src/test_helpers.rs b/src/test_helpers.rs index 61d6a22..d198fdf 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -156,14 +156,14 @@ pub fn save_graph(graph: &DiGraphMap, path: &str) { use petgraph::dot::{Config, Dot}; use std::fs::File; let mut file = File::create(path).unwrap(); - file.write_all( + let _ = file.write_all( format!( "{dot:?}", dot = Dot::with_attr_getters( &graph, &[Config::NodeNoLabel, Config::EdgeNoLabel], &|_, (_, _, edge_weight)| format!("label = \"{}\"", edge_weight.chromosome_index), - &|_, (node, weight)| format!( + &|_, (node, _weight)| format!( "label = \"{}[{}-{}]\"", node.node_id, node.sequence_start, node.sequence_end ), diff --git a/src/updates/fasta.rs b/src/updates/fasta.rs index 7015551..f9a7bea 100644 --- a/src/updates/fasta.rs +++ b/src/updates/fasta.rs @@ -108,7 +108,7 @@ pub fn update_with_fasta( phased: 0, }; - let interval_tree = Path::intervaltree_for(conn, &path); + let interval_tree = path.intervaltree(conn); BlockGroup::insert_change(conn, &path_change, &interval_tree); let edge_to_new_node = Edge::query(