Skip to content

Commit

Permalink
Update after rebase, and use PathBuf for more things
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhofer committed Sep 17, 2024
1 parent 88d58be commit f251386
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
13 changes: 5 additions & 8 deletions src/exports/gfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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,
Expand All @@ -16,7 +17,7 @@ use crate::models::{
strand::Strand,
};

pub fn export_gfa(conn: &Connection, collection_name: &str, filename: &str) {
pub fn export_gfa(conn: &Connection, collection_name: &str, filename: &PathBuf) {
let block_groups = Collection::get_block_groups(conn, collection_name);

let mut edge_set = HashSet::new();
Expand Down Expand Up @@ -193,13 +194,9 @@ mod tests {
);
let all_sequences = BlockGroup::get_all_sequences(&conn, block_group.id);

let tmp_dir = tempdir().expect("Couldn't get handle to temp directory");
let gfa_path = tmp_dir
.path()
.join("intermediate.gfa")
.into_os_string()
.into_string()
.unwrap();
let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
let mut gfa_path = PathBuf::from(temp_dir.path());
gfa_path.push("intermediate.gfa");

export_gfa(&conn, collection_name, &gfa_path);
// NOTE: Not directly checking file contents because segments are written in random order
Expand Down
4 changes: 2 additions & 2 deletions src/imports/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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,
self, 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
14 changes: 8 additions & 6 deletions src/imports/gfa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,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,
Expand All @@ -21,10 +23,10 @@ fn bool_to_strand(direction: bool) -> Strand {
}
}

pub fn import_gfa(gfa_path: &str, collection_name: &str, conn: &Connection) {
pub fn import_gfa(gfa_path: &FilePath, collection_name: &str, conn: &Connection) {
Collection::create(conn, collection_name);
let block_group = BlockGroup::create(conn, collection_name, None, "");
let gfa: Gfa<u64, (), ()> = Gfa::parse_gfa_file(gfa_path);
let gfa: Gfa<u64, (), ()> = Gfa::parse_gfa_file(gfa_path.to_str().unwrap());
let mut sequences_by_segment_id: HashMap<u64, Sequence> = HashMap::new();

for segment in &gfa.segments {
Expand Down Expand Up @@ -222,7 +224,7 @@ mod tests {
gfa_path.push("fixtures/simple.gfa");
let collection_name = "test".to_string();
let conn = &get_connection(None);
import_gfa(gfa_path.to_str().unwrap(), &collection_name, conn);
import_gfa(gfa_path.as_path(), &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
Expand All @@ -245,7 +247,7 @@ mod tests {
gfa_path.push("fixtures/no_path.gfa");
let collection_name = "no path".to_string();
let conn = &get_connection(None);
import_gfa(gfa_path.to_str().unwrap(), &collection_name, conn);
import_gfa(gfa_path.as_path(), &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let all_sequences = BlockGroup::get_all_sequences(conn, block_group_id);
Expand All @@ -261,7 +263,7 @@ mod tests {
gfa_path.push("fixtures/walk.gfa");
let collection_name = "walk".to_string();
let conn = &mut get_connection(None);
import_gfa(gfa_path.to_str().unwrap(), &collection_name, conn);
import_gfa(&gfa_path, &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
Expand All @@ -284,7 +286,7 @@ mod tests {
gfa_path.push("fixtures/reverse_strand.gfa");
let collection_name = "test".to_string();
let conn = &get_connection(None);
import_gfa(gfa_path.to_str().unwrap(), &collection_name, conn);
import_gfa(&gfa_path, &collection_name, conn);

let block_group_id = BlockGroup::get_id(conn, &collection_name, None, "");
let path = Path::get_paths(
Expand Down
13 changes: 5 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use gen::operation_management;
use gen::updates::vcf::update_with_vcf;
use rusqlite::types::Value;
use std::fmt::Debug;
use std::path::PathBuf;
use std::str;

#[derive(Parser)]
Expand Down Expand Up @@ -74,9 +75,6 @@ enum Commands {
/// The name of the collection to export
#[arg(short, long)]
name: String,
/// The path to the database being exported from
#[arg(short, long)]
db: String,
/// The name of the GFA file to export to
#[arg(short, long)]
gfa: String,
Expand Down Expand Up @@ -115,7 +113,7 @@ fn main() {
&operation_conn,
);
} else if gfa.is_some() {
import_gfa(&gfa.clone().unwrap(), name, &conn);
import_gfa(&PathBuf::from(gfa.clone().unwrap()), name, &conn);
} else {
panic!(
"ERROR: Import command attempted but no recognized file format was specified"
Expand Down Expand Up @@ -175,11 +173,10 @@ fn main() {
}
Some(Commands::Checkout { id }) => {
operation_management::move_to(&conn, &Operation::get_by_id(&operation_conn, *id));
}
Some(Commands::Export { name, db, gfa }) => {
let mut conn = get_connection(db);
}
Some(Commands::Export { name, gfa }) => {
conn.execute("BEGIN TRANSACTION", []).unwrap();
export_gfa(&conn, name, gfa);
export_gfa(&conn, name, &PathBuf::from(gfa));
conn.execute("END TRANSACTION", []).unwrap();
}
None => {}
Expand Down

0 comments on commit f251386

Please sign in to comment.