-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ginkgobioworks/refactor-block-splitting
Refactor some models
- Loading branch information
Showing
5 changed files
with
189 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use rusqlite::Connection; | ||
|
||
#[derive(Debug)] | ||
pub struct Block { | ||
pub id: i32, | ||
pub sequence_hash: String, | ||
pub block_group_id: i32, | ||
pub start: i32, | ||
pub end: i32, | ||
pub strand: String, | ||
} | ||
|
||
impl Block { | ||
pub fn create( | ||
conn: &Connection, | ||
hash: &String, | ||
block_group_id: i32, | ||
start: i32, | ||
end: i32, | ||
strand: &String, | ||
) -> Block { | ||
let mut stmt = conn | ||
.prepare_cached("INSERT INTO block (sequence_hash, block_group_id, start, end, strand) VALUES (?1, ?2, ?3, ?4, ?5) RETURNING *") | ||
.unwrap(); | ||
match stmt.query_row((hash, block_group_id, start, end, strand), |row| { | ||
Ok(Block { | ||
id: row.get(0)?, | ||
sequence_hash: row.get(1)?, | ||
block_group_id: row.get(2)?, | ||
start: row.get(3)?, | ||
end: row.get(4)?, | ||
strand: row.get(5)?, | ||
}) | ||
}) { | ||
Ok(res) => res, | ||
Err(rusqlite::Error::SqliteFailure(err, _details)) => { | ||
if err.code == rusqlite::ErrorCode::ConstraintViolation { | ||
Block { | ||
id: conn | ||
.query_row( | ||
"select id from block where sequence_hash = ?1 AND block_group_id = ?2 AND start = ?3 AND end = ?4 AND strand = ?5;", | ||
(hash, block_group_id, start, end, strand), | ||
|row| row.get(0), | ||
) | ||
.unwrap(), | ||
sequence_hash: hash.clone(), | ||
block_group_id, | ||
start, | ||
end, | ||
strand: strand.clone(), | ||
} | ||
} else { | ||
panic!("something bad happened querying the database") | ||
} | ||
} | ||
Err(_e) => { | ||
panic!("failure in making block {_e}") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use rusqlite::{params_from_iter, Connection}; | ||
|
||
#[derive(Debug)] | ||
pub struct Edge { | ||
pub id: i32, | ||
pub source_id: i32, | ||
pub target_id: Option<i32>, | ||
pub chromosome_index: i32, | ||
pub phased: i32, | ||
} | ||
|
||
impl Edge { | ||
pub fn create( | ||
conn: &Connection, | ||
source_id: i32, | ||
target_id: Option<i32>, | ||
chromosome_index: i32, | ||
phased: i32, | ||
) -> Edge { | ||
let query; | ||
let id_query; | ||
let mut placeholders = vec![]; | ||
if target_id.is_some() { | ||
query = "INSERT INTO edges (source_id, target_id, chromosome_index, phased) VALUES (?1, ?2, ?3, ?4) RETURNING *"; | ||
id_query = "select id from edges where source_id = ?1 and target_id = ?2"; | ||
placeholders.push(source_id); | ||
placeholders.push(target_id.unwrap()); | ||
placeholders.push(chromosome_index); | ||
placeholders.push(phased); | ||
} else { | ||
id_query = "select id from edges where source_id = ?1 and target_id is null and chromosome_index = ?2 and phased = ?3"; | ||
query = "INSERT INTO edges (source_id, chromosome_index, phased) VALUES (?1, ?2, ?3) RETURNING *"; | ||
placeholders.push(source_id); | ||
placeholders.push(chromosome_index); | ||
placeholders.push(phased); | ||
} | ||
let mut stmt = conn.prepare(query).unwrap(); | ||
match stmt.query_row(params_from_iter(&placeholders), |row| { | ||
Ok(Edge { | ||
id: row.get(0)?, | ||
source_id: row.get(1)?, | ||
target_id: row.get(2)?, | ||
chromosome_index: row.get(3)?, | ||
phased: row.get(4)?, | ||
}) | ||
}) { | ||
Ok(edge) => edge, | ||
Err(rusqlite::Error::SqliteFailure(err, details)) => { | ||
if err.code == rusqlite::ErrorCode::ConstraintViolation { | ||
println!("{err:?} {details:?}"); | ||
Edge { | ||
id: conn | ||
.query_row(id_query, params_from_iter(&placeholders), |row| row.get(0)) | ||
.unwrap(), | ||
source_id, | ||
target_id, | ||
chromosome_index, | ||
phased, | ||
} | ||
} else { | ||
panic!("something bad happened querying the database") | ||
} | ||
} | ||
Err(_) => { | ||
panic!("something bad happened querying the database") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.