Skip to content

Commit

Permalink
polbin: Finish loading binary files (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsyo authored Mar 11, 2024
2 parents 41b9522 + 896b7ed commit 5b14d15
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
1 change: 1 addition & 0 deletions polbin/Cargo.lock

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

1 change: 1 addition & 0 deletions polbin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"
[dependencies]
bstr = "1.9.1"
gfa = "0.10.1"
memmap = "0.7.0"
num_enum = "0.7.2"
zerocopy = { version = "0.7.32", features = ["derive"] }
47 changes: 29 additions & 18 deletions polbin/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::flatgfa;
use bstr::BStr;
use zerocopy::{FromBytes, FromZeroes};

const MAGIC_NUMBER: usize = 0x1337_4915;
Expand All @@ -14,36 +13,48 @@ struct TOC {
steps_count: usize,
seq_data_len: usize,
overlaps_count: usize,
alignment_len: usize,
alignment_count: usize,
name_data_len: usize,
optional_data_len: usize,
line_order_len: usize,
}

pub fn load(data: &[u8]) -> flatgfa::FlatGFA {
/// Get the first `len` bytes in a byte slice, and return the rest of the slice.
fn get_prefix(data: &[u8], len: usize) -> (&[u8], &[u8]) {
assert!(data.len() >= len);
(&data[0..len], &data[len..])
}

pub fn view(data: &[u8]) -> flatgfa::FlatGFA {
// Table of contents.
let toc = TOC::ref_from_prefix(data).unwrap();
let rest = &data[std::mem::size_of::<TOC>()..];
assert_eq!(toc.magic, MAGIC_NUMBER);

// Header (version).
let header = BStr::new(&rest[0..toc.header_len]);
let rest = &rest[toc.header_len..];

// Segments.
// Get slices for each chunk.
let (header, rest) = get_prefix(rest, toc.header_len);
let (segs, rest) = flatgfa::Segment::slice_from_prefix(rest, toc.segs_count).unwrap();
let (paths, rest) = flatgfa::Path::slice_from_prefix(rest, toc.paths_count).unwrap();
let (links, rest) = flatgfa::Link::slice_from_prefix(rest, toc.links_count).unwrap();
let (steps, rest) = flatgfa::Handle::slice_from_prefix(rest, toc.steps_count).unwrap();
let (seq_data, rest) = get_prefix(rest, toc.seq_data_len);
let (overlaps, rest) = flatgfa::Span::slice_from_prefix(rest, toc.overlaps_count).unwrap();
let (alignment, rest) = flatgfa::AlignOp::slice_from_prefix(rest, toc.alignment_count).unwrap();
let (name_data, rest) = get_prefix(rest, toc.name_data_len);
let (optional_data, rest) = get_prefix(rest, toc.optional_data_len);
let (line_order, _) = get_prefix(rest, toc.line_order_len);

flatgfa::FlatGFA {
header,
header: header.into(),
segs,
paths: todo!(),
links: todo!(),
steps: todo!(),
seq_data: todo!(),
overlaps: todo!(),
alignment: todo!(),
name_data: todo!(),
optional_data: todo!(),
line_order: todo!(),
paths,
links,
steps,
seq_data,
overlaps,
alignment,
name_data: name_data.into(),
optional_data: optional_data.into(),
line_order,
}
}
21 changes: 17 additions & 4 deletions polbin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ mod file;
mod flatgfa;
mod parse;
mod print;
use memmap::Mmap;

fn map_file(name: &str) -> Mmap {
let file = std::fs::File::open(name).unwrap();
unsafe { Mmap::map(&file) }.unwrap()
}

fn main() {
let stdin = std::io::stdin();
let store = parse::Parser::parse(stdin.lock());
let gfa = store.view();
print::print(&gfa);
// Read either GFA text from stdin or a binary file from the first argument.
if let Some(name) = std::env::args().nth(1) {
let mmap = map_file(&name);
let gfa = file::view(&mmap);
print::print(&gfa);
} else {
let stdin = std::io::stdin();
let store = parse::Parser::parse(stdin.lock());
let gfa = store.view();
print::print(&gfa);
}
}

0 comments on commit 5b14d15

Please sign in to comment.