1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::gfa::gfa::GFAtk;
use crate::load::{load_gfa, load_gfa_stdin};
use crate::utils;
use anyhow::{bail, ensure, Context, Result};
use gfa::gfa::Orientation;
use std::collections::HashMap;
use std::fs;
use std::io::{BufRead, BufReader};
pub enum CLIOpt {
String,
File,
}
pub fn path(matches: &clap::ArgMatches) -> Result<()> {
let gfa_file = matches.value_of("GFA");
let path_cli = matches.value_of("path_cli");
let path_file = matches.value_of("path_file");
if path_cli.is_none() && path_file.is_none() {
bail!(
"Please specify either a path as a positional argument string, or as a file `--path`."
)
}
if path_cli.is_some() && path_file.is_some() {
bail!("Specify either <path>, or `--path`, not both.")
}
let gfa: GFAtk = match gfa_file {
Some(f) => {
if !f.ends_with(".gfa") {
bail!("Input file is not a GFA.")
}
GFAtk(load_gfa(f)?)
}
None => match utils::is_stdin() {
true => GFAtk(load_gfa_stdin(std::io::stdin().lock())?),
false => bail!("No input from STDIN. Run `gfatk path -h` for help."),
},
};
let (path, link_map) = match path_cli {
Some(p) => parse_path(p, CLIOpt::String, &gfa),
None => match path_file {
Some(f) => parse_path(f, CLIOpt::File, &gfa),
None => bail!("Should never reach here."),
},
}?;
gfa.from_path_cli(path, link_map, "path", None)?;
Ok(())
}
pub fn parse_path(
path: &str,
is_cli: CLIOpt,
gfa: &GFAtk,
) -> Result<(GFAPath, HashMap<String, usize>)> {
match is_cli {
CLIOpt::String => parse_path_string(path, gfa),
CLIOpt::File => {
let mut first_line = String::new();
let file = fs::File::open(path)?;
let mut buffer = BufReader::new(file);
buffer.read_line(&mut first_line)?;
parse_path_string(&first_line, gfa)
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct GFAPathElement {
pub segment_id: usize,
pub orientation: Orientation,
pub index: usize,
}
#[derive(Debug, Clone)]
pub struct GFAPath {
pub inner: Vec<GFAPathElement>,
}
impl GFAPath {
fn new() -> Self {
Self { inner: vec![] }
}
fn push(&mut self, other: GFAPathElement) {
self.inner.push(other);
}
pub fn to_fasta_header(&self) -> String {
let mut output = String::new();
for el in &self.inner {
output += &format!("{}{},", el.segment_id, el.orientation);
}
output.pop();
output
}
}
fn parse_path_string(path_string: &str, gfa: &GFAtk) -> Result<(GFAPath, HashMap<String, usize>)> {
let gfa = &gfa.0;
let mut link_map = HashMap::new();
for link in &gfa.links {
let path_pair = format!(
"{}{}|{}{}",
link.from_segment, link.from_orient, link.to_segment, link.to_orient
);
let cigar = utils::parse_cigar(&link.overlap)?;
link_map.insert(path_pair, cigar);
}
let mut split_path: Vec<&str> = path_string.split(',').collect();
for token in split_path.iter_mut() {
*token = token.trim();
}
let mut gfa_path = GFAPath::new();
for (index, token) in split_path.into_iter().enumerate() {
let mut token_string = token.to_owned();
let orientation = token_string
.pop()
.context("Each path element should contain a character.")?;
ensure!(
orientation == '-' || orientation == '+',
"The last char in the split_path token was {}, not \'+\' or \'-\'. Check path is specified correctly.",
orientation
);
let o_enum = match orientation {
'+' => Orientation::Forward,
'-' => Orientation::Backward,
_ => bail!("Orientation can only be the chars \'+\' or \'-\'."),
};
gfa_path.push(GFAPathElement {
segment_id: token_string
.parse::<usize>()
.context("Could not parse the token string as a `usize`.")?,
orientation: o_enum,
index,
});
}
Ok((gfa_path, link_map))
}