Skip to content

Commit

Permalink
attempt at better modularity of parsing, w/r/t paths
Browse files Browse the repository at this point in the history
  • Loading branch information
susan-garry committed Oct 2, 2024
1 parent 7e9f620 commit 8d1ede5
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions flatgfa/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ impl<'a, P: flatgfa::StoreFamily<'a>> Parser<'a, P> {
self.add_link(link);
}
for line in deferred_paths {
self.add_path(&line);
if let gfaline::Line::Path(path) = gfaline::parse_line(&line).unwrap() {
self.add_path(path);
} else {
unreachable!("unexpected deferred line")
}
}

self.flat
Expand Down Expand Up @@ -103,13 +107,15 @@ impl<'a, P: flatgfa::StoreFamily<'a>> Parser<'a, P> {

// "Unwind" the deferred lines.
for line in deferred_lines {
if line[0] == b'P' {
self.add_path(line);
} else {
let gfa_line = gfaline::parse_line(line).unwrap();
if let gfaline::Line::Link(link) = gfa_line {
let gfa_line = gfaline::parse_line(line).unwrap();
match gfa_line {
gfaline::Line::Link(link) => {
self.add_link(link);
} else {
}
gfaline::Line::Path(path) => {
self.add_path(path);
}
gfaline::Line::Header(_) | gfaline::Line::Segment(_) => {
unreachable!("unexpected deferred line")
}
}
Expand Down Expand Up @@ -139,16 +145,10 @@ impl<'a, P: flatgfa::StoreFamily<'a>> Parser<'a, P> {
self.flat.add_link(from, to, link.overlap);
}

fn add_path(&mut self, line: &[u8]) {
// This must be a path line.
assert_eq!(&line[..2], b"P\t");
let line = &line[2..];

// Parse the name.
let (name, rest) = gfaline::parse_field(line).unwrap();

fn add_path(&mut self, path: gfaline::Path) {

// Parse the steps.
let mut step_parser = gfaline::StepsParser::new(rest);
let mut step_parser = gfaline::StepsParser::new(&path.steps);
let steps = self.flat.add_steps((&mut step_parser).map(|(name, dir)| {
Handle::new(
self.seg_ids.get(name).into(),
Expand All @@ -159,13 +159,9 @@ impl<'a, P: flatgfa::StoreFamily<'a>> Parser<'a, P> {
},
)
}));
let rest = step_parser.rest();

// Parse the overlaps.
let (overlaps, rest) = gfaline::parse_maybe_overlap_list(rest).unwrap();
assert!(step_parser.rest().is_empty());

assert!(rest.is_empty());
self.flat.add_path(name, steps, overlaps.into_iter());
self.flat.add_path(path.name, steps, path.overlaps.into_iter());
}
}

Expand Down

0 comments on commit 8d1ede5

Please sign in to comment.