Skip to content

Commit

Permalink
Merge pull request #51 from ginkgobioworks/september-2024-cleanup
Browse files Browse the repository at this point in the history
Warning cleanup, fix or ignore failing tests
  • Loading branch information
dkhofer authored Sep 23, 2024
2 parents 4da2bb6 + 10dc4ce commit 8002f92
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 127 deletions.
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::migrations::run_operation_migrations;
use crate::models::operations::Operation;
use rusqlite::Connection;
use std::io::{IsTerminal, Read, Write};
use std::string::ToString;
use std::sync::RwLock;
use std::{
Expand Down Expand Up @@ -31,7 +30,7 @@ fn ensure_dir(path: &PathBuf) {

pub fn get_or_create_gen_dir() -> PathBuf {
let start_dir = BASE_DIR.with(|v| v.read().unwrap().clone());
let mut cur_dir = start_dir.as_path();
let cur_dir = start_dir.as_path();
let gen_path = cur_dir.join(".gen");
ensure_dir(&gen_path);
gen_path
Expand Down
28 changes: 5 additions & 23 deletions src/exports/gfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ use itertools::Itertools;
use petgraph::prelude::DiGraphMap;
use rusqlite::Connection;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;

use crate::models::{
self,
block_group::BlockGroup,
block_group_edge::BlockGroupEdge,
collection::Collection,
edge::{Edge, GroupBlock},
Expand All @@ -32,7 +29,7 @@ pub fn export_gfa(conn: &Connection, collection_name: &str, filename: &PathBuf)
let (blocks, boundary_edges) = Edge::blocks_from_edges(conn, &edges);
edges.extend(boundary_edges.clone());

let (graph, edges_by_node_pair) = Edge::build_graph(conn, &edges, &blocks);
let (graph, edges_by_node_pair) = Edge::build_graph(&edges, &blocks);

let file = File::create(filename).unwrap();
let mut writer = BufWriter::new(file);
Expand All @@ -54,13 +51,7 @@ pub fn export_gfa(conn: &Connection, collection_name: &str, filename: &PathBuf)
edges_by_node_pair.clone(),
terminal_block_ids,
);
write_paths(
&mut writer,
conn,
collection_name,
edges_by_node_pair,
&blocks,
);
write_paths(&mut writer, conn, collection_name, &blocks);
}

fn write_segments(
Expand Down Expand Up @@ -137,7 +128,7 @@ fn nodes_for_edges(
blocks_by_hash_and_start: &HashMap<(&str, i32), GroupBlock>,
blocks_by_hash_and_end: &HashMap<(&str, i32), GroupBlock>,
) -> Vec<i32> {
let current_block = blocks_by_hash_and_start
let mut current_block = blocks_by_hash_and_start
.get(&(edge1.target_hash.as_str(), edge1.target_coordinate))
.unwrap();
let end_block = blocks_by_hash_and_end
Expand All @@ -147,7 +138,7 @@ fn nodes_for_edges(
#[allow(clippy::while_immutable_condition)]
while current_block.id != end_block.id {
node_ids.push(current_block.id);
let current_block = blocks_by_hash_and_start
current_block = blocks_by_hash_and_start
.get(&(current_block.sequence_hash.as_str(), current_block.end))
.unwrap();
}
Expand All @@ -160,16 +151,11 @@ fn write_paths(
writer: &mut BufWriter<File>,
conn: &Connection,
collection_name: &str,
edges_by_node_pair: HashMap<(i32, i32), Edge>,
blocks: &[GroupBlock],
) {
let paths = Path::get_paths_for_collection(conn, collection_name);
let edges_by_path_id =
PathEdge::edges_for_paths(conn, paths.iter().map(|path| path.id).collect());
let node_pairs_by_edge_id = edges_by_node_pair
.iter()
.map(|(node_pair, edge)| (edge.id, *node_pair))
.collect::<HashMap<i32, (i32, i32)>>();

let blocks_by_hash_and_start = blocks
.iter()
Expand Down Expand Up @@ -214,15 +200,11 @@ fn path_line(path_name: &str, node_ids: &[i32], node_strands: &[Strand]) -> Stri
}

mod tests {
use rusqlite::Connection;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

use crate::imports::gfa::import_gfa;
use crate::models::{
block_group::BlockGroup, block_group_edge::BlockGroupEdge, collection::Collection,
edge::Edge, sequence::Sequence,
};
use crate::models::{block_group::BlockGroup, collection::Collection};
use crate::test_helpers::{get_connection, setup_gen_dir};
use tempfile::tempdir;

Expand Down
9 changes: 6 additions & 3 deletions src/imports/fasta.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::str;

use crate::models::file_types::FileTypes;
use crate::models::operations::{FileAddition, Operation, OperationSummary};
use crate::models::{
self, block_group::BlockGroup, block_group_edge::BlockGroupEdge, collection::Collection,
edge::Edge, metadata, path::Path, sequence::Sequence, strand::Strand,
block_group::BlockGroup, block_group_edge::BlockGroupEdge, collection::Collection, edge::Edge,
metadata, path::Path, sequence::Sequence, strand::Strand,
};
use crate::operation_management;
use noodles::fasta;
Expand Down Expand Up @@ -103,8 +102,10 @@ pub fn import_fasta(
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use crate::models::operations::setup_db;
use crate::test_helpers::{get_connection, get_operation_connection, setup_gen_dir};
use std::collections::HashSet;
use std::path::PathBuf;

#[test]
fn test_add_fasta() {
Expand All @@ -113,6 +114,8 @@ mod tests {
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);
setup_db(op_conn, &db_uuid);

import_fasta(
&fasta_path.to_str().unwrap().to_string(),
Expand Down
5 changes: 1 addition & 4 deletions src/imports/gfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use gfa_reader::Gfa;
use rusqlite::Connection;
use std::collections::{HashMap, HashSet};
use std::path::Path as FilePath;
use std::path::PathBuf;

use crate::models::{
self,
block_group::BlockGroup,
block_group_edge::BlockGroupEdge,
collection::Collection,
Expand Down Expand Up @@ -213,8 +211,7 @@ fn edge_data_from_fields(
mod tests {
use super::*;
use crate::test_helpers::{get_connection, setup_gen_dir};
use rusqlite::{types::Value as SQLValue, Connection};
use std::fs;
use rusqlite::types::Value as SQLValue;
use std::path::PathBuf;

#[test]
Expand Down
52 changes: 1 addition & 51 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use rusqlite::types::Value;
use rusqlite::{params_from_iter, Connection};
use std::fmt::*;

pub mod block_group;
pub mod block_group_edge;
pub mod collection;
Expand All @@ -11,52 +7,6 @@ pub mod metadata;
pub mod operations;
pub mod path;
pub mod path_edge;
pub mod sample;
pub mod sequence;
pub mod strand;

use crate::models;

#[derive(Debug)]
pub struct Sample {
pub name: String,
}

impl 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.to_string(),
},
Err(rusqlite::Error::SqliteFailure(err, details)) => {
if err.code == rusqlite::ErrorCode::ConstraintViolation {
println!("{err:?} {details:?}");
Sample {
name: name.to_string(),
}
} else {
panic!("something bad happened querying the database")
}
}
Err(_) => {
panic!("something bad happened querying the database")
}
}
}

pub fn query(conn: &Connection, query: &str, placeholders: Vec<Value>) -> Vec<Sample> {
let mut stmt = conn.prepare(query).unwrap();
let rows = stmt
.query_map(params_from_iter(placeholders), |row| {
Ok(Sample { name: row.get(0)? })
})
.unwrap();
let mut objs = vec![];
for row in rows {
objs.push(row.unwrap());
}
objs
}
}
9 changes: 4 additions & 5 deletions src/models/block_group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use intervaltree::IntervalTree;
use petgraph::graphmap::DiGraphMap;
use petgraph::Direction;
use rusqlite::{params_from_iter, types::Value as SQLValue, Connection};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -218,13 +217,13 @@ impl BlockGroup {
let result = if sample_name.is_some() {
conn.query_row(
"select id from block_group where collection_name = ?1 AND sample_name = ?2 AND name = ?3",
(collection_name, sample_name, group_name.clone()),
(collection_name, sample_name, group_name),
|row| row.get(0),
)
} else {
conn.query_row(
"select id from block_group where collection_name = ?1 AND sample_name IS NULL AND name = ?2",
(collection_name, group_name.clone()),
(collection_name, group_name),
|row| row.get(0),
)
};
Expand All @@ -242,7 +241,7 @@ impl BlockGroup {
let mut edges = BlockGroupEdge::edges_for_block_group(conn, block_group_id);
let (blocks, boundary_edges) = Edge::blocks_from_edges(conn, &edges);
edges.extend(boundary_edges.clone());
let (graph, _) = Edge::build_graph(conn, &edges, &blocks);
let (graph, _) = Edge::build_graph(&edges, &blocks);

let mut start_nodes = vec![];
let mut end_nodes = vec![];
Expand Down Expand Up @@ -416,7 +415,7 @@ impl BlockGroup {
#[cfg(test)]
mod tests {
use super::*;
use crate::models::{collection::Collection, Sample};
use crate::models::{collection::Collection, sample::Sample};
use crate::test_helpers::get_connection;

fn setup_block_group(conn: &Connection) -> (i32, Path) {
Expand Down
1 change: 0 additions & 1 deletion src/models/block_group_edge.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::models::edge::Edge;
use crate::models::operations::Operation;
use rusqlite::types::Value;
use rusqlite::{params_from_iter, Connection};

Expand Down
1 change: 0 additions & 1 deletion src/models/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rusqlite::types::Value;
use rusqlite::{params_from_iter, Connection};

use crate::models::block_group::BlockGroup;
use crate::models::path::Path;

#[derive(Clone, Debug)]
pub struct Collection {
Expand Down
6 changes: 1 addition & 5 deletions src/models/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ impl Edge {
}

pub fn build_graph(
conn: &Connection,
edges: &Vec<Edge>,
blocks: &Vec<GroupBlock>,
) -> (DiGraphMap<i32, ()>, HashMap<(i32, i32), Edge>) {
Expand Down Expand Up @@ -457,12 +456,9 @@ impl Edge {
}

mod tests {
use rusqlite::Connection;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use std::collections::HashMap;

use crate::models::{collection::Collection, sequence::Sequence};
use crate::models::collection::Collection;
use crate::test_helpers::get_connection;

#[test]
Expand Down
1 change: 0 additions & 1 deletion src/models/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn get_db_uuid(conn: &Connection) -> String {

#[cfg(test)]
mod tests {
use rusqlite::Connection;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

Expand Down
8 changes: 3 additions & 5 deletions src/models/operations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::graph::all_simple_paths;
use crate::models::file_types::FileTypes;
use crate::models::operations;
use itertools::Itertools;
use petgraph::graphmap::{DiGraphMap, UnGraphMap};
use petgraph::visit::DfsPostOrder;
use petgraph::Direction;
Expand Down Expand Up @@ -98,23 +96,23 @@ impl Operation {
for node in directed_graph.nodes() {
undirected_graph.add_node(node);
}
for (source, target, weight) in directed_graph.all_edges() {
for (source, target, _weight) in directed_graph.all_edges() {
undirected_graph.add_edge(source, target, ());
}
let mut patch_path: Vec<(i32, Direction, i32)> = vec![];
for path in all_simple_paths(&undirected_graph, source_id, target_id) {
let mut last_node = 0;
for node in path {
if node != source_id {
for (edge_src, edge_target, edge_weight) in
for (_edge_src, edge_target, _edge_weight) in
directed_graph.edges_directed(last_node, Direction::Outgoing)
{
if edge_target == node {
patch_path.push((last_node, Direction::Outgoing, node));
break;
}
}
for (edge_src, edge_target, edge_weight) in
for (edge_src, _edge_target, _edge_weight) in
directed_graph.edges_directed(last_node, Direction::Incoming)
{
if edge_src == node {
Expand Down
3 changes: 1 addition & 2 deletions src/models/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,10 @@ impl Path {
}

mod tests {
use rusqlite::Connection;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

use crate::models::{block_group::BlockGroup, collection::Collection, sequence::NewSequence};
use crate::models::{block_group::BlockGroup, collection::Collection};
use crate::test_helpers::get_connection;

#[test]
Expand Down
Loading

0 comments on commit 8002f92

Please sign in to comment.