Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query cleanup #87

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/exports/fasta.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use noodles::fasta;
use rusqlite;
use rusqlite::{types::Value as SQLValue, Connection};
use std::fs::File;
use std::path::PathBuf;
Expand All @@ -17,16 +18,16 @@ pub fn export_fasta(
block_groups = BlockGroup::query(
conn,
"select * from block_groups where collection_name = ?1 AND sample_name = ?2;",
vec![
rusqlite::params!(
SQLValue::from(collection_name.to_string()),
SQLValue::from(sample_name.to_string()),
],
),
);
} else {
block_groups = BlockGroup::query(
conn,
"select * from block_groups where collection_name = ?1 AND sample_name IS NULL;",
vec![SQLValue::from(collection_name.to_string())],
rusqlite::params!(SQLValue::from(collection_name.to_string())),
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/exports/gfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub fn export_gfa(
let block_groups = BlockGroup::query(
conn,
"select * from block_groups where collection_name = ?1 AND sample_name = ?2;",
vec![
rusqlite::params!(
SQLValue::from(collection_name.to_string()),
SQLValue::from(sample.clone()),
],
),
);
if block_groups.is_empty() {
panic!(
Expand Down Expand Up @@ -176,7 +176,7 @@ fn write_paths(
collection_name: &str,
blocks: &[GroupBlock],
) {
let paths = Path::get_paths_for_collection(conn, collection_name);
let paths = Path::query_for_collection(conn, collection_name);
let edges_by_path_id =
PathEdge::edges_for_paths(conn, paths.iter().map(|path| path.id).collect());

Expand Down Expand Up @@ -362,7 +362,7 @@ mod tests {

assert_eq!(all_sequences, all_sequences2);

let paths = Path::get_paths_for_collection(&conn, "test collection 2");
let paths = Path::query_for_collection(&conn, "test collection 2");
assert_eq!(paths.len(), 1);
assert_eq!(paths[0].sequence(&conn), "AAAATTTTGGGGCCCC");
}
Expand Down
4 changes: 2 additions & 2 deletions src/gfa_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ impl<'a, T: SampleType, S: Opt, U: Opt> Pansn<'a, T, S, U> {

/// Get all path
#[allow(clippy::type_complexity)]
pub fn get_paths_direct(&self) -> Vec<(String, Vec<&Path<T, S, U>>)> {
pub fn query_direct(&self) -> Vec<(String, Vec<&Path<T, S, U>>)> {
let mut result = Vec::new();
for x in self.genomes.iter() {
for y in x.haplotypes.iter() {
Expand All @@ -720,6 +720,6 @@ impl<'a, T: SampleType, S: Opt, U: Opt> Pansn<'a, T, S, U> {
"Number of individual haplotypes: {}",
self.get_haplo_path().len()
);
println!("Total number of paths: {}", self.get_paths_direct().len());
println!("Total number of paths: {}", self.query_direct().len());
}
}
11 changes: 9 additions & 2 deletions src/imports/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::models::{
};
use crate::{calculate_hash, operation_management};
use noodles::fasta;
use rusqlite;
use rusqlite::{session, Connection};

pub fn import_fasta(
Expand Down Expand Up @@ -205,14 +206,20 @@ mod tests {
conn,
op_conn,
);
assert_eq!(Node::query(conn, "select * from nodes;", vec![]).len(), 3);
assert_eq!(
Node::query(conn, "select * from nodes;", rusqlite::params!()).len(),
3
);
import_fasta(
&fasta_path.to_str().unwrap().to_string(),
&collection,
false,
conn,
op_conn,
);
assert_eq!(Node::query(conn, "select * from nodes;", vec![]).len(), 3);
assert_eq!(
Node::query(conn, "select * from nodes;", rusqlite::params!()).len(),
3
);
}
}
45 changes: 23 additions & 22 deletions src/imports/gfa.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rusqlite;
use rusqlite::Connection;
use std::collections::{HashMap, HashSet};
use std::path::Path as FilePath;
Expand Down Expand Up @@ -234,20 +235,20 @@ mod tests {
import_gfa(&gfa_path, &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
let path = Path::query(
conn,
"select * from paths where block_group_id = ?1 AND name = ?2",
vec![
rusqlite::params!(
SQLValue::from(block_group_id),
SQLValue::from("m123".to_string()),
],
),
)[0]
.clone();

let result = path.sequence(conn);
assert_eq!(result, "ATCGATCGATCGATCGATCGGGAACACACAGAGA");

let node_count = Node::query(conn, "select * from nodes", vec![]).len() as i64;
let node_count = Node::query(conn, "select * from nodes", rusqlite::params!()).len() as i64;
assert_eq!(node_count, 6);
}

Expand All @@ -266,7 +267,7 @@ mod tests {
HashSet::from_iter(vec!["AAAATTTTGGGGCCCC".to_string()])
);

let node_count = Node::query(conn, "select * from nodes", vec![]).len() as i64;
let node_count = Node::query(conn, "select * from nodes", rusqlite::params!()).len() as i64;
assert_eq!(node_count, 6);
}

Expand All @@ -279,20 +280,20 @@ mod tests {
import_gfa(&gfa_path, &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
let path = Path::query(
conn,
"select * from paths where block_group_id = ?1 AND name = ?2",
vec![
rusqlite::params!(
SQLValue::from(block_group_id),
SQLValue::from("291344".to_string()),
],
),
)[0]
.clone();

let result = path.sequence(conn);
assert_eq!(result, "ACCTACAAATTCAAAC");

let node_count = Node::query(conn, "select * from nodes", vec![]).len() as i64;
let node_count = Node::query(conn, "select * from nodes", rusqlite::params!()).len() as i64;
assert_eq!(node_count, 6);
}

Expand All @@ -305,20 +306,20 @@ mod tests {
import_gfa(&gfa_path, &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
let path = Path::query(
conn,
"select * from paths where block_group_id = ?1 AND name = ?2",
vec![
rusqlite::params!(
SQLValue::from(block_group_id),
SQLValue::from("124".to_string()),
],
),
)[0]
.clone();

let result = path.sequence(conn);
assert_eq!(result, "TATGCCAGCTGCGAATA");

let node_count = Node::query(conn, "select * from nodes", vec![]).len() as i64;
let node_count = Node::query(conn, "select * from nodes", rusqlite::params!()).len() as i64;
assert_eq!(node_count, 6);
}

Expand All @@ -330,17 +331,17 @@ mod tests {
let conn = &get_connection(None);
import_gfa(&gfa_path, &collection_name, conn);

let paths = Path::get_paths_for_collection(conn, &collection_name);
let paths = Path::query_for_collection(conn, &collection_name);
assert_eq!(paths.len(), 20);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
let path = Path::query(
conn,
"select * from paths where block_group_id = ?1 AND name = ?2",
vec![
rusqlite::params!(
SQLValue::from(block_group_id),
SQLValue::from("BBa_J23100".to_string()),
],
),
)[0]
.clone();

Expand Down Expand Up @@ -423,7 +424,7 @@ mod tests {
assert_eq!(all_sequences.len(), 1024);
assert_eq!(all_sequences, expected_sequences);

let node_count = Node::query(conn, "select * from nodes", vec![]).len() as i64;
let node_count = Node::query(conn, "select * from nodes", rusqlite::params!()).len() as i64;
assert_eq!(node_count, 28);
}

Expand All @@ -437,13 +438,13 @@ mod tests {
import_gfa(&gfa_path, &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
let path = Path::query(
conn,
"select * from paths where block_group_id = ?1 AND name = ?2",
vec![
rusqlite::params!(
SQLValue::from(block_group_id),
SQLValue::from("124".to_string()),
],
),
)[0]
.clone();

Expand All @@ -453,7 +454,7 @@ mod tests {
let all_sequences = BlockGroup::get_all_sequences(conn, block_group_id, false);
assert_eq!(all_sequences, HashSet::from_iter(vec!["AA".to_string()]));

let node_count = Node::query(conn, "select * from nodes", vec![]).len() as i64;
let node_count = Node::query(conn, "select * from nodes", rusqlite::params!()).len() as i64;
assert_eq!(node_count, 4);
}
}
6 changes: 3 additions & 3 deletions src/models/accession.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::models::edge::EdgeData;
use crate::models::strand::Strand;
use crate::models::traits::Query;
use crate::models::traits::*;
use rusqlite::types::Value;
use rusqlite::{params_from_iter, Connection, Result as SQLResult, Row};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -199,7 +199,7 @@ impl AccessionEdge {
let formatted_edge_rows = edge_rows.join(", ");

let select_statement = format!("SELECT * FROM accession_edges WHERE (source_node_id, source_coordinate, source_strand, target_node_id, target_coordinate, target_strand, chromosome_index) in ({0});", formatted_edge_rows);
let existing_edges = AccessionEdge::query(conn, &select_statement, vec![]);
let existing_edges = AccessionEdge::query(conn, &select_statement, rusqlite::params!());
for edge in existing_edges.iter() {
edge_map.insert(AccessionEdgeData::from(edge), edge.id);
}
Expand Down Expand Up @@ -333,7 +333,7 @@ mod tests {
Accession::query(
conn,
"select * from accessions where name = ?1",
vec![Value::from("test".to_string())]
rusqlite::params!(Value::from("test".to_string())),
),
vec![Accession {
id: accession.id,
Expand Down
Loading