-
Notifications
You must be signed in to change notification settings - Fork 0
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
GFA export #42
Merged
Merged
GFA export #42
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cd9d0c
GFA export
dkhofer a810734
Remove todo
dkhofer ba79c9d
Refactor some code, get first test passing
dkhofer adf281f
More cleanup
dkhofer 9be2f4e
Ignore .gen dir
dkhofer 88d58be
add newline
dkhofer f251386
Update after rebase, and use PathBuf for more things
dkhofer 2e0bb27
Simplify path usage
dkhofer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ fixtures/*.ipynb | |
|
||
# Mac droppings | ||
.DS_Store | ||
|
||
# Gen tracking dir | ||
.gen/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
S 0 GGGG * | ||
S 1 AAAA * | ||
S 2 TTTT * | ||
S 3 CCCC * | ||
L 1 + 2 + * | ||
L 2 + 0 + * | ||
L 0 + 3 + * |
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 @@ | ||
pub mod gfa; |
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,212 @@ | ||
use itertools::Itertools; | ||
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}, | ||
path::Path, | ||
sequence::Sequence, | ||
strand::Strand, | ||
}; | ||
|
||
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(); | ||
for block_group in block_groups { | ||
let block_group_edges = BlockGroupEdge::edges_for_block_group(conn, block_group.id); | ||
edge_set.extend(block_group_edges.into_iter()); | ||
} | ||
|
||
let mut edges = edge_set.into_iter().collect(); | ||
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 mut file = File::create(filename).unwrap(); | ||
let mut writer = BufWriter::new(file); | ||
|
||
let mut terminal_block_ids = HashSet::new(); | ||
for block in &blocks { | ||
if block.sequence_hash == Sequence::PATH_START_HASH | ||
|| block.sequence_hash == Sequence::PATH_END_HASH | ||
{ | ||
terminal_block_ids.insert(block.id); | ||
continue; | ||
} | ||
writer | ||
.write_all(&segment_line(&block.sequence, block.id as usize).into_bytes()) | ||
.unwrap_or_else(|_| { | ||
panic!( | ||
"Error writing segment with sequence {} to GFA stream", | ||
block.sequence, | ||
) | ||
}); | ||
} | ||
|
||
let blocks_by_id = blocks | ||
.clone() | ||
.into_iter() | ||
.map(|block| (block.id, block)) | ||
.collect::<HashMap<i32, GroupBlock>>(); | ||
|
||
for (source, target, ()) in graph.all_edges() { | ||
if terminal_block_ids.contains(&source) || terminal_block_ids.contains(&target) { | ||
continue; | ||
} | ||
let edge = edges_by_node_pair.get(&(source, target)).unwrap(); | ||
writer | ||
.write_all( | ||
&link_line(source, edge.source_strand, target, edge.target_strand).into_bytes(), | ||
) | ||
.unwrap_or_else(|_| { | ||
panic!( | ||
"Error writing link from segment {} to {} to GFA stream", | ||
source, target, | ||
) | ||
}); | ||
} | ||
} | ||
|
||
fn segment_line(sequence: &str, index: usize) -> String { | ||
format!("S\t{}\t{}\t{}\n", index, sequence, "*") | ||
} | ||
|
||
fn link_line( | ||
source_index: i32, | ||
source_strand: Strand, | ||
target_index: i32, | ||
target_strand: Strand, | ||
) -> String { | ||
format!( | ||
"L\t{}\t{}\t{}\t{}\t*\n", | ||
source_index, source_strand, target_index, target_strand | ||
) | ||
} | ||
|
||
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::test_helpers::get_connection; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_simple_export() { | ||
// Sets up a basic graph and then exports it to a GFA file | ||
let conn = get_connection(None); | ||
|
||
let collection_name = "test collection"; | ||
let collection = Collection::create(&conn, collection_name); | ||
let block_group = BlockGroup::create(&conn, collection_name, None, "test block group"); | ||
let sequence1 = Sequence::new() | ||
.sequence_type("DNA") | ||
.sequence("AAAA") | ||
.save(&conn); | ||
let sequence2 = Sequence::new() | ||
.sequence_type("DNA") | ||
.sequence("TTTT") | ||
.save(&conn); | ||
let sequence3 = Sequence::new() | ||
.sequence_type("DNA") | ||
.sequence("GGGG") | ||
.save(&conn); | ||
let sequence4 = Sequence::new() | ||
.sequence_type("DNA") | ||
.sequence("CCCC") | ||
.save(&conn); | ||
|
||
let edge1 = Edge::create( | ||
&conn, | ||
Sequence::PATH_START_HASH.to_string(), | ||
0, | ||
Strand::Forward, | ||
sequence1.hash.clone(), | ||
0, | ||
Strand::Forward, | ||
0, | ||
0, | ||
); | ||
let edge2 = Edge::create( | ||
&conn, | ||
sequence1.hash, | ||
4, | ||
Strand::Forward, | ||
sequence2.hash.clone(), | ||
0, | ||
Strand::Forward, | ||
0, | ||
0, | ||
); | ||
let edge3 = Edge::create( | ||
&conn, | ||
sequence2.hash, | ||
4, | ||
Strand::Forward, | ||
sequence3.hash.clone(), | ||
0, | ||
Strand::Forward, | ||
0, | ||
0, | ||
); | ||
let edge4 = Edge::create( | ||
&conn, | ||
sequence3.hash, | ||
4, | ||
Strand::Forward, | ||
sequence4.hash.clone(), | ||
0, | ||
Strand::Forward, | ||
0, | ||
0, | ||
); | ||
let edge5 = Edge::create( | ||
&conn, | ||
sequence4.hash, | ||
4, | ||
Strand::Forward, | ||
Sequence::PATH_END_HASH.to_string(), | ||
0, | ||
Strand::Forward, | ||
0, | ||
0, | ||
); | ||
|
||
BlockGroupEdge::bulk_create( | ||
&conn, | ||
block_group.id, | ||
&[edge1.id, edge2.id, edge3.id, edge4.id, edge5.id], | ||
); | ||
let all_sequences = BlockGroup::get_all_sequences(&conn, block_group.id); | ||
|
||
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 | ||
import_gfa(&gfa_path, "test collection 2", &conn); | ||
|
||
let block_group2 = Collection::get_block_groups(&conn, "test collection 2") | ||
.pop() | ||
.unwrap(); | ||
let all_sequences2 = BlockGroup::get_all_sequences(&conn, block_group2.id); | ||
|
||
assert_eq!(all_sequences, all_sequences2); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a fyi, you can use pathbuf and avoid the string conversions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, that does simplify things a bit, done here and with gfa imports