Skip to content

Commit

Permalink
Merge pull request #50 from ginkgobioworks/changeset-v2
Browse files Browse the repository at this point in the history
Custom changeset management
  • Loading branch information
Chris7 authored Sep 24, 2024
2 parents 8002f92 + d38f217 commit 88020cd
Show file tree
Hide file tree
Showing 13 changed files with 928 additions and 174 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ noodles = { version = "0.78.0", features = ["core", "vcf", "fasta", "async"] }
petgraph = "0.6.5"
chrono = "0.4.38"
tempdir = "0.3.7"
fallible-streaming-iterator = "0.1.9"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
5 changes: 3 additions & 2 deletions src/imports/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn import_fasta(
println!("Created it");
let mut output = Vec::new();
session.changeset_strm(&mut output).unwrap();
operation_management::write_changeset(&operation, &output);
operation_management::write_changeset(conn, &operation, &output);
} else {
println!("Collection {:1} already exists", name);
}
Expand All @@ -102,6 +102,7 @@ pub fn import_fasta(
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use crate::models::metadata;
use crate::models::operations::setup_db;
use crate::test_helpers::{get_connection, get_operation_connection, setup_gen_dir};
use std::collections::HashSet;
Expand All @@ -113,8 +114,8 @@ mod tests {
let mut fasta_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fasta_path.push("fixtures/simple.fa");
let conn = get_connection(None);
let op_conn = &get_operation_connection(None);
let db_uuid = metadata::get_db_uuid(&conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);

import_fasta(
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn parse_genotype(gt: &str) -> Vec<Option<Genotype>> {
true => Phasing::Unphased,
false => Phasing::Phased,
};
for entry in gt.split_inclusive(|c| c == '|' || c == '/') {
for entry in gt.split_inclusive(['|', '/']) {
let allele;
let mut phasing = Phasing::Unphased;
if entry.ends_with(['/', '|']) {
Expand Down
118 changes: 22 additions & 96 deletions src/models/block_group.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::{HashMap, HashSet};

use intervaltree::IntervalTree;
use petgraph::Direction;
use rusqlite::{params_from_iter, types::Value as SQLValue, Connection};
use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};

use crate::graph::all_simple_paths;
use crate::models::block_group_edge::BlockGroupEdge;
Expand All @@ -11,7 +13,7 @@ use crate::models::path_edge::PathEdge;
use crate::models::sequence::Sequence;
use crate::models::strand::Strand;

#[derive(Debug)]
#[derive(Debug, Deserialize, Serialize)]
pub struct BlockGroup {
pub id: i32,
pub collection_name: String,
Expand Down Expand Up @@ -104,17 +106,29 @@ impl BlockGroup {
Err(rusqlite::Error::SqliteFailure(err, details)) => {
if err.code == rusqlite::ErrorCode::ConstraintViolation {
println!("{err:?} {details:?}");
BlockGroup {
id: conn
let bg_id = match sample_name {
Some(v) => {conn
.query_row(
"select id from block_group where collection_name = ?1 and sample_name is null and name = ?2",
"select id from block_group where collection_name = ?1 and sample_name = ?2 and name = ?3",
(collection_name, v, name),
|row| row.get(0),
)
.unwrap()}
None => {
conn
.query_row(
"select id from block_group where collection_name = ?1 and sample_name is null and name = ?3",
(collection_name, name),
|row| row.get(0),
)
.unwrap(),
.unwrap()
}
};
BlockGroup {
id: bg_id,
collection_name: collection_name.to_string(),
sample_name: sample_name.map(|s| s.to_string()),
name: name.to_string()
name: name.to_string(),
}
} else {
panic!("something bad happened querying the database")
Expand Down Expand Up @@ -416,95 +430,7 @@ impl BlockGroup {
mod tests {
use super::*;
use crate::models::{collection::Collection, sample::Sample};
use crate::test_helpers::get_connection;

fn setup_block_group(conn: &Connection) -> (i32, Path) {
let a_seq = Sequence::new()
.sequence_type("DNA")
.sequence("AAAAAAAAAA")
.save(conn);
let t_seq = Sequence::new()
.sequence_type("DNA")
.sequence("TTTTTTTTTT")
.save(conn);
let c_seq = Sequence::new()
.sequence_type("DNA")
.sequence("CCCCCCCCCC")
.save(conn);
let g_seq = Sequence::new()
.sequence_type("DNA")
.sequence("GGGGGGGGGG")
.save(conn);
let _collection = Collection::create(conn, "test");
let block_group = BlockGroup::create(conn, "test", None, "hg19");
let edge0 = Edge::create(
conn,
Sequence::PATH_START_HASH.to_string(),
0,
Strand::Forward,
a_seq.hash.clone(),
0,
Strand::Forward,
0,
0,
);
let edge1 = Edge::create(
conn,
a_seq.hash,
10,
Strand::Forward,
t_seq.hash.clone(),
0,
Strand::Forward,
0,
0,
);
let edge2 = Edge::create(
conn,
t_seq.hash,
10,
Strand::Forward,
c_seq.hash.clone(),
0,
Strand::Forward,
0,
0,
);
let edge3 = Edge::create(
conn,
c_seq.hash,
10,
Strand::Forward,
g_seq.hash.clone(),
0,
Strand::Forward,
0,
0,
);
let edge4 = Edge::create(
conn,
g_seq.hash,
10,
Strand::Forward,
Sequence::PATH_END_HASH.to_string(),
0,
Strand::Forward,
0,
0,
);
BlockGroupEdge::bulk_create(
conn,
block_group.id,
&[edge0.id, edge1.id, edge2.id, edge3.id, edge4.id],
);
let path = Path::create(
conn,
"chr1",
block_group.id,
&[edge0.id, edge1.id, edge2.id, edge3.id, edge4.id],
);
(block_group.id, path)
}
use crate::test_helpers::{get_connection, setup_block_group};

#[test]
fn test_blockgroup_create() {
Expand Down
Loading

0 comments on commit 88020cd

Please sign in to comment.