Skip to content

Commit

Permalink
Clone non-path edge ids with blockgroup cline
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris7 committed Aug 29, 2024
1 parent f456952 commit 4e6fbf4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
42 changes: 36 additions & 6 deletions src/models/block_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/models/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4e6fbf4

Please sign in to comment.