Skip to content

Commit

Permalink
Updates after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhofer committed Oct 30, 2024
1 parent fd7afb4 commit 08b6b33
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/exports/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/exports/gfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/models/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
118 changes: 117 additions & 1 deletion src/models/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ impl Path {
})
.clone()
.collect()
}

pub fn new_path_for(
&self,
Expand All @@ -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();

Expand Down Expand Up @@ -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");
}
}
4 changes: 2 additions & 2 deletions src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ pub fn save_graph(graph: &DiGraphMap<GraphNode, GraphEdge>, 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
),
Expand Down
2 changes: 1 addition & 1 deletion src/updates/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 08b6b33

Please sign in to comment.