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

FlatGFA: Add an odgi position -v equivalent #166

Merged
merged 4 commits into from
Apr 7, 2024
Merged
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
62 changes: 62 additions & 0 deletions polbin/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,68 @@ pub fn stats(gfa: &flatgfa::FlatGFA, args: Stats) {
}
}

/// find a nucleotide position within a path
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "position")]
pub struct Position {
/// path_name,offset,orientation
#[argh(option, short = 'p')]
path_pos: String,
}

pub fn position(gfa: &flatgfa::FlatGFA, args: Position) -> Result<(), &'static str> {
// Parse the position triple, which looks like `path,42,+`.
let (path_name, offset, orientation) = {
let parts: Vec<_> = args.path_pos.split(",").collect();
if parts.len() != 3 {
return Err("position must be path_name,offset,orientation");
}
let off: usize = parts[1].parse().or(Err("offset must be a number"))?;
let ori: flatgfa::Orientation = parts[2].parse().or(Err("orientation must be + or -"))?;
(parts[0], off, ori)
};

let path_id = gfa.find_path(path_name.into()).ok_or("path not found")?;
let path = &gfa.paths[path_id as usize];
assert_eq!(
orientation,
flatgfa::Orientation::Forward,
"only + is implemented so far"
);

// Traverse the path until we reach the position.
let mut cur_pos = 0;
let mut found = None;
for step in gfa.get_steps(path) {
let seg = gfa.get_handle_seg(*step);
let end_pos = cur_pos + seg.len();
if offset < end_pos {
// Found it!
found = Some((*step, offset - cur_pos));
break;
}
cur_pos = end_pos;
}

// Print the match.
if let Some((handle, seg_off)) = found {
let seg = gfa.get_handle_seg(handle);
let seg_name = seg.name;
println!("#source.path.pos\ttarget.graph.pos");
println!(
"{},{},{}\t{},{},{}",
path_name,
offset,
orientation,
seg_name,
seg_off,
handle.orient()
);
}

Ok(())
}

/// create a subset graph
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "extract")]
Expand Down
24 changes: 24 additions & 0 deletions polbin/src/flatgfa.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use crate::pool::{Index, Pool, Span};
use bstr::BStr;
use num_enum::{IntoPrimitive, TryFromPrimitive};
Expand Down Expand Up @@ -133,6 +135,20 @@ pub enum Orientation {
Backward, // -
}

impl FromStr for Orientation {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "+" {
Ok(Orientation::Forward)
} else if s == "-" {
Ok(Orientation::Backward)
} else {
Err(())
}
}
}

/// An oriented reference to a segment.
///
/// A Handle refers to the forward (+) or backward (-) orientation for a given segment.
Expand Down Expand Up @@ -234,6 +250,14 @@ impl<'a> FlatGFA<'a> {
.map(|i| i as Index)
}

/// Look up a path by its name.
pub fn find_path(&self, name: &BStr) -> Option<Index> {
self.paths
.iter()
.position(|path| self.get_path_name(path) == name)
.map(|i| i as Index)
}

/// Get all the steps for a path.
pub fn get_steps(&self, path: &Path) -> &[Handle] {
&self.steps[path.steps.range()]
Expand Down
4 changes: 4 additions & 0 deletions polbin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum Command {
Toc(cmds::Toc),
Paths(cmds::Paths),
Stats(cmds::Stats),
Position(cmds::Position),
Extract(cmds::Extract),
}

Expand Down Expand Up @@ -124,6 +125,9 @@ fn main() -> Result<(), &'static str> {
Some(Command::Stats(sub_args)) => {
cmds::stats(&gfa, sub_args);
}
Some(Command::Position(sub_args)) => {
cmds::position(&gfa, sub_args)?;
}
Some(Command::Extract(sub_args)) => {
let store = cmds::extract(&gfa, sub_args)?;
dump(&store.view(), &args.output);
Expand Down
Loading