From 29143a499d3066ef5277d5053ca0a8dd9a6aeb16 Mon Sep 17 00:00:00 2001 From: hofer Date: Wed, 4 Sep 2024 12:21:43 -0400 Subject: [PATCH 1/2] Return full Sequence object when saving it --- src/main.rs | 12 ++---- src/models/block_group.rs | 62 ++++++++++++------------------- src/models/edge.rs | 44 +++++++++++----------- src/models/path.rs | 78 +++++++++++++++++++-------------------- src/models/sequence.rs | 47 +++++++++++------------ 5 files changed, 111 insertions(+), 132 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8049594..16721c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,7 @@ fn import_fasta(fasta: &String, name: &str, shallow: bool, conn: &mut Connection .to_string(); let name = String::from_utf8(record.name().to_vec()).unwrap(); let sequence_length = record.sequence().len() as i32; - let seq_hash = if shallow { + let seq = if shallow { Sequence::new() .sequence_type("DNA") .name(&name) @@ -105,7 +105,7 @@ fn import_fasta(fasta: &String, name: &str, shallow: bool, conn: &mut Connection Edge::PATH_START_HASH.to_string(), 0, "+".to_string(), - seq_hash.to_string(), + seq.hash.to_string(), 0, "+".to_string(), 0, @@ -113,7 +113,7 @@ fn import_fasta(fasta: &String, name: &str, shallow: bool, conn: &mut Connection ); let edge_out_of = Edge::create( conn, - seq_hash.to_string(), + seq.hash.to_string(), sequence_length, "+".to_string(), Edge::PATH_END_HASH.to_string(), @@ -212,14 +212,10 @@ impl<'a> SequenceCache<'_> { if let Some(found_sequence) = sequence_lookup { found_sequence.clone() } else { - let new_sequence_hash = Sequence::new() + let new_sequence = Sequence::new() .sequence_type("DNA") .sequence(&sequence) .save(sequence_cache.conn); - let new_sequence = NewSequence::new() - .sequence_type(sequence_type) - .sequence(&sequence) - .build(); sequence_cache .cache diff --git a/src/models/block_group.rs b/src/models/block_group.rs index bbc04d4..710d5ff 100644 --- a/src/models/block_group.rs +++ b/src/models/block_group.rs @@ -547,19 +547,19 @@ mod tests { } fn setup_block_group(conn: &Connection) -> (i32, Path) { - let a_seq_hash = Sequence::new() + let a_seq = Sequence::new() .sequence_type("DNA") .sequence("AAAAAAAAAA") .save(conn); - let t_seq_hash = Sequence::new() + let t_seq = Sequence::new() .sequence_type("DNA") .sequence("TTTTTTTTTT") .save(conn); - let c_seq_hash = Sequence::new() + let c_seq = Sequence::new() .sequence_type("DNA") .sequence("CCCCCCCCCC") .save(conn); - let g_seq_hash = Sequence::new() + let g_seq = Sequence::new() .sequence_type("DNA") .sequence("GGGGGGGGGG") .save(conn); @@ -570,7 +570,7 @@ mod tests { Edge::PATH_START_HASH.to_string(), 0, "+".to_string(), - a_seq_hash.clone(), + a_seq.hash.clone(), 0, "+".to_string(), 0, @@ -578,10 +578,10 @@ mod tests { ); let edge1 = Edge::create( conn, - a_seq_hash, + a_seq.hash, 10, "+".to_string(), - t_seq_hash.clone(), + t_seq.hash.clone(), 0, "+".to_string(), 0, @@ -589,10 +589,10 @@ mod tests { ); let edge2 = Edge::create( conn, - t_seq_hash, + t_seq.hash, 10, "+".to_string(), - c_seq_hash.clone(), + c_seq.hash.clone(), 0, "+".to_string(), 0, @@ -600,10 +600,10 @@ mod tests { ); let edge3 = Edge::create( conn, - c_seq_hash, + c_seq.hash, 10, "+".to_string(), - g_seq_hash.clone(), + g_seq.hash.clone(), 0, "+".to_string(), 0, @@ -611,7 +611,7 @@ mod tests { ); let edge4 = Edge::create( conn, - g_seq_hash, + g_seq.hash, 10, "+".to_string(), Edge::PATH_END_HASH.to_string(), @@ -668,11 +668,10 @@ mod tests { fn insert_and_deletion_get_all() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -704,12 +703,10 @@ mod tests { ]) ); - let deletion_sequence_hash = Sequence::new() + let deletion_sequence = Sequence::new() .sequence_type("DNA") .sequence("") .save(&conn); - let deletion_sequence = - Sequence::sequence_from_hash(&conn, &deletion_sequence_hash).unwrap(); let deletion = NewBlock { id: 0, sequence: deletion_sequence.clone(), @@ -749,11 +746,10 @@ mod tests { fn simple_insert_get_all() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -790,11 +786,10 @@ mod tests { fn insert_on_block_boundary_middle() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -831,11 +826,10 @@ mod tests { fn insert_within_block() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -872,11 +866,10 @@ mod tests { fn insert_on_block_boundary_start() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -913,11 +906,10 @@ mod tests { fn insert_on_block_boundary_end() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -954,11 +946,10 @@ mod tests { fn insert_across_entire_block_boundary() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -995,11 +986,10 @@ mod tests { fn insert_across_two_blocks() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -1036,11 +1026,10 @@ mod tests { fn insert_spanning_blocks() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), @@ -1077,12 +1066,10 @@ mod tests { fn simple_deletion() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let deletion_sequence_hash = Sequence::new() + let deletion_sequence = Sequence::new() .sequence_type("DNA") .sequence("") .save(&conn); - let deletion_sequence = - Sequence::sequence_from_hash(&conn, &deletion_sequence_hash).unwrap(); let deletion = NewBlock { id: 0, sequence: deletion_sequence.clone(), @@ -1121,11 +1108,10 @@ mod tests { fn doesnt_apply_same_insert_twice() { let conn = get_connection(); let (block_group_id, path) = setup_block_group(&conn); - let insert_sequence_hash = Sequence::new() + let insert_sequence = Sequence::new() .sequence_type("DNA") .sequence("NNNN") .save(&conn); - let insert_sequence = Sequence::sequence_from_hash(&conn, &insert_sequence_hash).unwrap(); let insert = NewBlock { id: 0, sequence: insert_sequence.clone(), diff --git a/src/models/edge.rs b/src/models/edge.rs index 33d9be2..27bbe03 100644 --- a/src/models/edge.rs +++ b/src/models/edge.rs @@ -250,7 +250,7 @@ mod tests { fn test_bulk_create() { let conn = &mut get_connection(); Collection::create(conn, "test collection"); - let sequence1_hash = Sequence::new() + let sequence1 = Sequence::new() .sequence_type("DNA") .sequence("ATCGATCG") .save(conn); @@ -258,28 +258,28 @@ mod tests { source_hash: Edge::PATH_START_HASH.to_string(), source_coordinate: -1, source_strand: "+".to_string(), - target_hash: sequence1_hash.clone(), + target_hash: sequence1.hash.clone(), target_coordinate: 1, target_strand: "+".to_string(), chromosome_index: 0, phased: 0, }; - let sequence2_hash = Sequence::new() + let sequence2 = Sequence::new() .sequence_type("DNA") .sequence("AAAAAAAA") .save(conn); let edge2 = EdgeData { - source_hash: sequence1_hash.clone(), + source_hash: sequence1.hash.clone(), source_coordinate: 2, source_strand: "+".to_string(), - target_hash: sequence2_hash.clone(), + target_hash: sequence2.hash.clone(), target_coordinate: 3, target_strand: "+".to_string(), chromosome_index: 0, phased: 0, }; let edge3 = EdgeData { - source_hash: sequence2_hash.clone(), + source_hash: sequence2.hash.clone(), source_coordinate: 4, source_strand: "+".to_string(), target_hash: Edge::PATH_END_HASH.to_string(), @@ -301,13 +301,13 @@ mod tests { let edge_result1 = edges_by_source_hash.get(Edge::PATH_START_HASH).unwrap(); assert_eq!(edge_result1.source_coordinate, -1); - assert_eq!(edge_result1.target_hash, sequence1_hash); + assert_eq!(edge_result1.target_hash, sequence1.hash); assert_eq!(edge_result1.target_coordinate, 1); - let edge_result2 = edges_by_source_hash.get(&sequence1_hash).unwrap(); + let edge_result2 = edges_by_source_hash.get(&sequence1.hash).unwrap(); assert_eq!(edge_result2.source_coordinate, 2); - assert_eq!(edge_result2.target_hash, sequence2_hash); + assert_eq!(edge_result2.target_hash, sequence2.hash); assert_eq!(edge_result2.target_coordinate, 3); - let edge_result3 = edges_by_source_hash.get(&sequence2_hash).unwrap(); + let edge_result3 = edges_by_source_hash.get(&sequence2.hash).unwrap(); assert_eq!(edge_result3.source_coordinate, 4); assert_eq!(edge_result3.target_hash, Edge::PATH_END_HASH); assert_eq!(edge_result3.target_coordinate, -1); @@ -317,7 +317,7 @@ mod tests { fn test_bulk_create_with_existing_edge() { let conn = &mut get_connection(); Collection::create(conn, "test collection"); - let sequence1_hash = Sequence::new() + let sequence1 = Sequence::new() .sequence_type("DNA") .sequence("ATCGATCG") .save(conn); @@ -327,7 +327,7 @@ mod tests { Edge::PATH_START_HASH.to_string(), -1, "+".to_string(), - sequence1_hash.clone(), + sequence1.hash.clone(), 1, "+".to_string(), 0, @@ -335,35 +335,35 @@ mod tests { ); assert_eq!(existing_edge.source_hash, Edge::PATH_START_HASH); assert_eq!(existing_edge.source_coordinate, -1); - assert_eq!(existing_edge.target_hash, sequence1_hash); + assert_eq!(existing_edge.target_hash, sequence1.hash); assert_eq!(existing_edge.target_coordinate, 1); let edge1 = EdgeData { source_hash: Edge::PATH_START_HASH.to_string(), source_coordinate: -1, source_strand: "+".to_string(), - target_hash: sequence1_hash.clone(), + target_hash: sequence1.hash.clone(), target_coordinate: 1, target_strand: "+".to_string(), chromosome_index: 0, phased: 0, }; - let sequence2_hash = Sequence::new() + let sequence2 = Sequence::new() .sequence_type("DNA") .sequence("AAAAAAAA") .save(conn); let edge2 = EdgeData { - source_hash: sequence1_hash.clone(), + source_hash: sequence1.hash.clone(), source_coordinate: 2, source_strand: "+".to_string(), - target_hash: sequence2_hash.clone(), + target_hash: sequence2.hash.clone(), target_coordinate: 3, target_strand: "+".to_string(), chromosome_index: 0, phased: 0, }; let edge3 = EdgeData { - source_hash: sequence2_hash.clone(), + source_hash: sequence2.hash.clone(), source_coordinate: 4, source_strand: "+".to_string(), target_hash: Edge::PATH_END_HASH.to_string(), @@ -388,13 +388,13 @@ mod tests { assert_eq!(edge_result1.id, existing_edge.id); assert_eq!(edge_result1.source_coordinate, -1); - assert_eq!(edge_result1.target_hash, sequence1_hash); + assert_eq!(edge_result1.target_hash, sequence1.hash); assert_eq!(edge_result1.target_coordinate, 1); - let edge_result2 = edges_by_source_hash.get(&sequence1_hash).unwrap(); + let edge_result2 = edges_by_source_hash.get(&sequence1.hash).unwrap(); assert_eq!(edge_result2.source_coordinate, 2); - assert_eq!(edge_result2.target_hash, sequence2_hash); + assert_eq!(edge_result2.target_hash, sequence2.hash); assert_eq!(edge_result2.target_coordinate, 3); - let edge_result3 = edges_by_source_hash.get(&sequence2_hash).unwrap(); + let edge_result3 = edges_by_source_hash.get(&sequence2.hash).unwrap(); assert_eq!(edge_result3.source_coordinate, 4); assert_eq!(edge_result3.target_hash, Edge::PATH_END_HASH); assert_eq!(edge_result3.target_coordinate, -1); diff --git a/src/models/path.rs b/src/models/path.rs index 53c6b5b..933d839 100644 --- a/src/models/path.rs +++ b/src/models/path.rs @@ -240,7 +240,7 @@ mod tests { let conn = &mut get_connection(); Collection::create(conn, "test collection"); let block_group = BlockGroup::create(conn, "test collection", None, "test block group"); - let sequence1_hash = Sequence::new() + let sequence1 = Sequence::new() .sequence_type("DNA") .sequence("ATCGATCG") .save(conn); @@ -249,52 +249,52 @@ mod tests { Edge::PATH_START_HASH.to_string(), -123, "+".to_string(), - sequence1_hash.clone(), + sequence1.hash.clone(), 0, "+".to_string(), 0, 0, ); - let sequence2_hash = Sequence::new() + let sequence2 = Sequence::new() .sequence_type("DNA") .sequence("AAAAAAAA") .save(conn); let edge2 = Edge::create( conn, - sequence1_hash.clone(), + sequence1.hash.clone(), 8, "+".to_string(), - sequence2_hash.clone(), + sequence2.hash.clone(), 1, "+".to_string(), 0, 0, ); - let sequence3_hash = Sequence::new() + let sequence3 = Sequence::new() .sequence_type("DNA") .sequence("CCCCCCCC") .save(conn); let edge3 = Edge::create( conn, - sequence2_hash.clone(), + sequence2.hash.clone(), 8, "+".to_string(), - sequence3_hash.clone(), + sequence3.hash.clone(), 1, "+".to_string(), 0, 0, ); - let sequence4_hash = Sequence::new() + let sequence4 = Sequence::new() .sequence_type("DNA") .sequence("GGGGGGGG") .save(conn); let edge4 = Edge::create( conn, - sequence3_hash.clone(), + sequence3.hash.clone(), 8, "+".to_string(), - sequence4_hash.clone(), + sequence4.hash.clone(), 1, "+".to_string(), 0, @@ -302,7 +302,7 @@ mod tests { ); let edge5 = Edge::create( conn, - sequence4_hash.clone(), + sequence4.hash.clone(), 8, "+".to_string(), Edge::PATH_END_HASH.to_string(), @@ -326,13 +326,13 @@ mod tests { let conn = &mut get_connection(); Collection::create(conn, "test collection"); let block_group = BlockGroup::create(conn, "test collection", None, "test block group"); - let sequence1_hash = Sequence::new() + let sequence1 = Sequence::new() .sequence_type("DNA") .sequence("ATCGATCG") .save(conn); let edge5 = Edge::create( conn, - sequence1_hash.clone(), + sequence1.hash.clone(), 8, "-".to_string(), Edge::PATH_END_HASH.to_string(), @@ -341,46 +341,46 @@ mod tests { 0, 0, ); - let sequence2_hash = Sequence::new() + let sequence2 = Sequence::new() .sequence_type("DNA") .sequence("AAAAAAAA") .save(conn); let edge4 = Edge::create( conn, - sequence2_hash.clone(), + sequence2.hash.clone(), 7, "-".to_string(), - sequence1_hash.clone(), + sequence1.hash.clone(), 0, "-".to_string(), 0, 0, ); - let sequence3_hash = Sequence::new() + let sequence3 = Sequence::new() .sequence_type("DNA") .sequence("CCCCCCCC") .save(conn); let edge3 = Edge::create( conn, - sequence3_hash.clone(), + sequence3.hash.clone(), 7, "-".to_string(), - sequence2_hash.clone(), + sequence2.hash.clone(), 0, "-".to_string(), 0, 0, ); - let sequence4_hash = Sequence::new() + let sequence4 = Sequence::new() .sequence_type("DNA") .sequence("GGGGGGGG") .save(conn); let edge2 = Edge::create( conn, - sequence4_hash.clone(), + sequence4.hash.clone(), 7, "-".to_string(), - sequence3_hash.clone(), + sequence3.hash.clone(), 0, "-".to_string(), 0, @@ -391,7 +391,7 @@ mod tests { Edge::PATH_START_HASH.to_string(), -1, "-".to_string(), - sequence4_hash.clone(), + sequence4.hash.clone(), 0, "-".to_string(), 0, @@ -419,7 +419,7 @@ mod tests { let conn = &mut get_connection(); Collection::create(conn, "test collection"); let block_group = BlockGroup::create(conn, "test collection", None, "test block group"); - let sequence1_hash = Sequence::new() + let sequence1 = Sequence::new() .sequence_type("DNA") .sequence("ATCGATCG") .save(conn); @@ -428,52 +428,52 @@ mod tests { Edge::PATH_START_HASH.to_string(), -1, "+".to_string(), - sequence1_hash.clone(), + sequence1.hash.clone(), 0, "+".to_string(), 0, 0, ); - let sequence2_hash = Sequence::new() + let sequence2 = Sequence::new() .sequence_type("DNA") .sequence("AAAAAAAA") .save(conn); let edge2 = Edge::create( conn, - sequence1_hash.clone(), + sequence1.hash.clone(), 8, "+".to_string(), - sequence2_hash.clone(), + sequence2.hash.clone(), 1, "+".to_string(), 0, 0, ); - let sequence3_hash = Sequence::new() + let sequence3 = Sequence::new() .sequence_type("DNA") .sequence("CCCCCCCC") .save(conn); let edge3 = Edge::create( conn, - sequence2_hash.clone(), + sequence2.hash.clone(), 8, "+".to_string(), - sequence3_hash.clone(), + sequence3.hash.clone(), 1, "+".to_string(), 0, 0, ); - let sequence4_hash = Sequence::new() + let sequence4 = Sequence::new() .sequence_type("DNA") .sequence("GGGGGGGG") .save(conn); let edge4 = Edge::create( conn, - sequence3_hash.clone(), + sequence3.hash.clone(), 8, "+".to_string(), - sequence4_hash.clone(), + sequence4.hash.clone(), 1, "+".to_string(), 0, @@ -481,7 +481,7 @@ mod tests { ); let edge5 = Edge::create( conn, - sequence4_hash.clone(), + sequence4.hash.clone(), 8, "+".to_string(), Edge::PATH_END_HASH.to_string(), @@ -501,7 +501,7 @@ mod tests { let blocks1: Vec<_> = tree.query_point(2).map(|x| x.value.clone()).collect(); assert_eq!(blocks1.len(), 1); let block1 = &blocks1[0]; - assert_eq!(block1.sequence.hash, sequence1_hash); + assert_eq!(block1.sequence.hash, sequence1.hash); assert_eq!(block1.sequence_start, 0); assert_eq!(block1.sequence_end, 8); assert_eq!(block1.path_start, 0); @@ -511,7 +511,7 @@ mod tests { let blocks2: Vec<_> = tree.query_point(12).map(|x| x.value.clone()).collect(); assert_eq!(blocks2.len(), 1); let block2 = &blocks2[0]; - assert_eq!(block2.sequence.hash, sequence2_hash); + assert_eq!(block2.sequence.hash, sequence2.hash); assert_eq!(block2.sequence_start, 1); assert_eq!(block2.sequence_end, 8); assert_eq!(block2.path_start, 8); @@ -521,7 +521,7 @@ mod tests { let blocks4: Vec<_> = tree.query_point(25).map(|x| x.value.clone()).collect(); assert_eq!(blocks4.len(), 1); let block4 = &blocks4[0]; - assert_eq!(block4.sequence.hash, sequence4_hash); + assert_eq!(block4.sequence.hash, sequence4.hash); assert_eq!(block4.sequence_start, 1); assert_eq!(block4.sequence_end, 8); assert_eq!(block4.path_start, 22); diff --git a/src/models/sequence.rs b/src/models/sequence.rs index cad1475..7686a17 100644 --- a/src/models/sequence.rs +++ b/src/models/sequence.rs @@ -103,7 +103,7 @@ impl<'a> NewSequence<'a> { } } - pub fn save(mut self, conn: &Connection) -> String { + pub fn save(mut self, conn: &Connection) -> Sequence { let mut length = 0; if self.sequence.is_none() && self.file_path.is_none() { panic!("Sequence or file_path must be set."); @@ -155,7 +155,15 @@ impl<'a> NewSequence<'a> { .unwrap(); obj_hash = rows.next().unwrap().unwrap(); } - obj_hash + Sequence { + hash: obj_hash, + sequence_type: self.sequence_type.unwrap().to_string(), + sequence: self.sequence.unwrap_or("").to_string(), + name: self.name.unwrap_or("").to_string(), + file_path: self.file_path.unwrap_or("").to_string(), + length: self.length.unwrap_or(length), + external_sequence: !self.file_path.unwrap_or("").is_empty(), + } } } @@ -323,16 +331,10 @@ mod tests { #[test] fn test_create_sequence_in_db() { let conn = &mut get_connection(); - let seq_hash = Sequence::new() + let sequence = Sequence::new() .sequence_type("DNA") .sequence("AACCTT") .save(conn); - let sequences = Sequence::sequences( - conn, - "select * from sequence where hash = ?1", - vec![Value::from(seq_hash)], - ); - let sequence = sequences.first().unwrap(); assert_eq!(&sequence.sequence, "AACCTT"); assert_eq!(sequence.sequence_type, "DNA"); assert!(!sequence.external_sequence); @@ -341,18 +343,12 @@ mod tests { #[test] fn test_create_sequence_on_disk() { let conn = &mut get_connection(); - let seq_hash = Sequence::new() + let sequence = Sequence::new() .sequence_type("DNA") .name("chr1") .file_path("/some/path.fa") .length(10) .save(conn); - let sequences = Sequence::sequences( - conn, - "select * from sequence where hash = ?1", - vec![Value::from(seq_hash)], - ); - let sequence = sequences.first().unwrap(); assert_eq!(sequence.sequence_type, "DNA"); assert_eq!(&sequence.sequence, ""); assert_eq!(sequence.name, "chr1"); @@ -366,19 +362,21 @@ mod tests { let conn = &mut get_connection(); let mut fasta_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); fasta_path.push("fixtures/simple.fa"); - let seq_hash = Sequence::new() + let sequence = Sequence::new() .sequence_type("DNA") .sequence("ATCGATCGATCGATCGATCGGGAACACACAGAGA") .save(conn); - let seq = Sequence::sequence_from_hash(conn, &seq_hash).unwrap(); assert_eq!( - seq.get_sequence(None, None), + sequence.get_sequence(None, None), "ATCGATCGATCGATCGATCGGGAACACACAGAGA" ); - assert_eq!(seq.get_sequence(0, 5), "ATCGA"); - assert_eq!(seq.get_sequence(10, 15), "CGATC"); - assert_eq!(seq.get_sequence(3, None), "GATCGATCGATCGATCGGGAACACACAGAGA"); - assert_eq!(seq.get_sequence(None, 5), "ATCGA"); + assert_eq!(sequence.get_sequence(0, 5), "ATCGA"); + assert_eq!(sequence.get_sequence(10, 15), "CGATC"); + assert_eq!( + sequence.get_sequence(3, None), + "GATCGATCGATCGATCGGGAACACACAGAGA" + ); + assert_eq!(sequence.get_sequence(None, 5), "ATCGA"); } #[test] @@ -386,13 +384,12 @@ mod tests { let conn = &mut get_connection(); let mut fasta_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); fasta_path.push("fixtures/simple.fa"); - let seq_hash = Sequence::new() + let seq = Sequence::new() .sequence_type("DNA") .name("m123") .file_path(fasta_path.to_str().unwrap()) .length(34) .save(conn); - let seq = Sequence::sequence_from_hash(conn, &seq_hash).unwrap(); assert_eq!( seq.get_sequence(None, None), "ATCGATCGATCGATCGATCGGGAACACACAGAGA" From 5930afc92b207db970f275d23d47d30cb96aef96 Mon Sep 17 00:00:00 2001 From: hofer Date: Wed, 4 Sep 2024 15:00:53 -0400 Subject: [PATCH 2/2] Use strict typing --- migrations/01-initial/up.sql | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/migrations/01-initial/up.sql b/migrations/01-initial/up.sql index 9e0682f..395b8fd 100644 --- a/migrations/01-initial/up.sql +++ b/migrations/01-initial/up.sql @@ -1,10 +1,10 @@ CREATE TABLE collection ( name TEXT PRIMARY KEY NOT NULL -); +) STRICT; CREATE TABLE sample ( name TEXT PRIMARY KEY NOT NULL -); +) STRICT; CREATE TABLE sequence ( hash TEXT PRIMARY KEY NOT NULL, @@ -13,7 +13,7 @@ CREATE TABLE sequence ( name TEXT NOT NULL, file_path TEXT NOT NULL, length INTEGER NOT NULL -); +) STRICT; CREATE TABLE block_group ( id INTEGER PRIMARY KEY NOT NULL, @@ -22,7 +22,7 @@ CREATE TABLE block_group ( name TEXT NOT NULL, FOREIGN KEY(collection_name) REFERENCES collection(name), FOREIGN KEY(sample_name) REFERENCES sample(name) -); +) STRICT; CREATE UNIQUE INDEX block_group_uidx ON block_group(collection_name, sample_name, name); CREATE TABLE path ( @@ -30,7 +30,7 @@ CREATE TABLE path ( block_group_id INTEGER NOT NULL, name TEXT NOT NULL, FOREIGN KEY(block_group_id) REFERENCES block_group(id) -); +) STRICT; CREATE UNIQUE INDEX path_uidx ON path(block_group_id, name); CREATE TABLE change_log ( @@ -44,7 +44,7 @@ CREATE TABLE change_log ( sequence_strand TEXT NOT NULL, FOREIGN KEY(path_id) REFERENCES path(id), FOREIGN KEY(sequence_hash) REFERENCES sequence(hash) -); +) STRICT; CREATE UNIQUE INDEX change_log_uidx ON change_log(hash); CREATE TABLE edges ( @@ -60,7 +60,7 @@ CREATE TABLE edges ( FOREIGN KEY(source_hash) REFERENCES sequence(hash), FOREIGN KEY(target_hash) REFERENCES sequence(hash), constraint chk_phased check (phased in (0, 1)) -); +) STRICT; CREATE UNIQUE INDEX edge_uidx ON edges(source_hash, source_coordinate, source_strand, target_hash, target_coordinate, target_strand, chromosome_index, phased); CREATE TABLE path_edges ( @@ -70,7 +70,7 @@ CREATE TABLE path_edges ( edge_id INTEGER NOT NULL, FOREIGN KEY(edge_id) REFERENCES edges(id), FOREIGN KEY(path_id) REFERENCES path(id) -); +) STRICT; CREATE UNIQUE INDEX path_edges_uidx ON path_edges(path_id, edge_id); CREATE TABLE block_group_edges ( @@ -79,7 +79,7 @@ CREATE TABLE block_group_edges ( edge_id INTEGER NOT NULL, FOREIGN KEY(block_group_id) REFERENCES block_group(id), FOREIGN KEY(edge_id) REFERENCES edges(id) -); +) STRICT; CREATE UNIQUE INDEX block_group_edges_uidx ON block_group_edges(block_group_id, edge_id); INSERT INTO sequence (hash, sequence_type, sequence, name, file_path, "length") values ("start-node-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", "OTHER", "start-node-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", "", "", 64), ("end-node-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "OTHER", "end-node-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "", "", 64);