From f45695235f3d03fb845b60904e13b8f8dc70946e Mon Sep 17 00:00:00 2001 From: chris Mitchell Date: Thu, 29 Aug 2024 11:56:28 -0400 Subject: [PATCH 1/2] Clone edge ids wit block group clone --- src/models/block_group.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/models/block_group.rs b/src/models/block_group.rs index aec6b4e..64c4874 100644 --- a/src/models/block_group.rs +++ b/src/models/block_group.rs @@ -84,6 +84,12 @@ impl BlockGroup { vec![Value::from(source_block_group_id)], ); + let edge_ids = BlockGroupEdge::edges_for_block_group(conn, source_block_group_id) + .iter() + .map(|edge| edge.id) + .collect(); + BlockGroupEdge::bulk_create(conn, target_block_group_id, edge_ids); + for path in existing_paths { let edge_ids = PathEdge::edges_for(conn, path.id) .into_iter() From 4e6fbf439cd483d75a214a696ce601fbe327ad95 Mon Sep 17 00:00:00 2001 From: chris Mitchell Date: Thu, 29 Aug 2024 12:09:51 -0400 Subject: [PATCH 2/2] Clone non-path edge ids with blockgroup cline --- src/main.rs | 2 +- src/models.rs | 10 +++++++--- src/models/block_group.rs | 42 +++++++++++++++++++++++++++++++++------ src/models/edge.rs | 2 +- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index ecdbb0c..54b9c58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,7 +137,7 @@ fn import_fasta(fasta: &String, name: &str, shallow: bool, conn: &mut Connection fn update_with_vcf( vcf_path: &String, - collection_name: &String, + collection_name: &str, fixed_genotype: String, fixed_sample: String, conn: &mut Connection, diff --git a/src/models.rs b/src/models.rs index 3b348c3..e1fc17c 100644 --- a/src/models.rs +++ b/src/models.rs @@ -56,16 +56,20 @@ pub struct Sample { } impl Sample { - pub fn create(conn: &Connection, name: &String) -> Sample { + pub fn create(conn: &Connection, name: &str) -> Sample { let mut stmt = conn .prepare("INSERT INTO sample (name) VALUES (?1)") .unwrap(); match stmt.execute((name,)) { - Ok(_) => Sample { name: name.clone() }, + Ok(_) => Sample { + name: name.to_string(), + }, Err(rusqlite::Error::SqliteFailure(err, details)) => { if err.code == rusqlite::ErrorCode::ConstraintViolation { println!("{err:?} {details:?}"); - Sample { name: name.clone() } + Sample { + name: name.to_string(), + } } else { panic!("something bad happened querying the database") } diff --git a/src/models/block_group.rs b/src/models/block_group.rs index 64c4874..3a6aefb 100644 --- a/src/models/block_group.rs +++ b/src/models/block_group.rs @@ -77,7 +77,7 @@ impl BlockGroup { } } - pub fn clone(conn: &mut Connection, source_block_group_id: i32, target_block_group_id: i32) { + pub fn clone(conn: &Connection, source_block_group_id: i32, target_block_group_id: i32) { let existing_paths = Path::get_paths( conn, "SELECT * from path where block_group_id = ?1", @@ -100,10 +100,10 @@ impl BlockGroup { } pub fn get_or_create_sample_block_group( - conn: &mut Connection, - collection_name: &String, - sample_name: &String, - group_name: &String, + conn: &Connection, + collection_name: &str, + sample_name: &str, + group_name: &str, ) -> i32 { let mut bg_id : i32 = match conn.query_row( "select id from block_group where collection_name = ?1 AND sample_name = ?2 AND name = ?3", @@ -449,7 +449,7 @@ impl BlockGroup { mod tests { use super::*; use crate::migrations::run_migrations; - use crate::models::Collection; + use crate::models::{Collection, Sample}; fn get_connection() -> Connection { let mut conn = Connection::open_in_memory() @@ -546,6 +546,36 @@ mod tests { (block_group.id, path) } + #[test] + fn test_blockgroup_create() { + let conn = &get_connection(); + Collection::create(conn, "test"); + let bg1 = BlockGroup::create(conn, "test", None, "hg19"); + assert_eq!(bg1.collection_name, "test"); + assert_eq!(bg1.name, "hg19"); + Sample::create(conn, "sample"); + let bg2 = BlockGroup::create(conn, "test", Some("sample"), "hg19"); + assert_eq!(bg2.collection_name, "test"); + assert_eq!(bg2.name, "hg19"); + assert_eq!(bg2.sample_name, Some("sample".to_string())); + assert_ne!(bg1.id, bg2.id); + } + + #[test] + fn test_blockgroup_clone() { + let conn = &get_connection(); + Collection::create(conn, "test"); + let bg1 = BlockGroup::create(conn, "test", None, "hg19"); + assert_eq!(bg1.collection_name, "test"); + assert_eq!(bg1.name, "hg19"); + Sample::create(conn, "sample"); + let bg2 = BlockGroup::get_or_create_sample_block_group(conn, "test", "sample", "hg19"); + assert_eq!( + BlockGroupEdge::edges_for_block_group(conn, bg1.id), + BlockGroupEdge::edges_for_block_group(conn, bg2) + ); + } + #[test] fn insert_and_deletion_new_get_all() { let mut conn = get_connection(); diff --git a/src/models/edge.rs b/src/models/edge.rs index 3443dfd..98b8245 100644 --- a/src/models/edge.rs +++ b/src/models/edge.rs @@ -3,7 +3,7 @@ use rusqlite::{params_from_iter, Connection}; use std::collections::HashSet; use std::hash::RandomState; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct Edge { pub id: i32, pub source_hash: String,