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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::load::load_gfa;
use crate::utils;
use crate::{gfa::gfa::GFAtk, gfa::graph::segments_subgraph, load::load_gfa_stdin};
use anyhow::{bail, Context, Result};
use petgraph::algo::is_cyclic_directed;
#[derive(PartialEq, Clone, Copy)]
pub enum GenomeType {
Mitochondria,
Chloroplast,
None,
}
#[derive(Clone, Debug)]
pub struct Stat {
pub index: usize,
pub gc: f32,
pub cov: f32,
pub segments: Vec<usize>,
pub total_sequence_length: usize,
pub is_circular: bool,
}
pub struct Stats(pub Vec<Stat>);
impl Stats {
pub fn push(&mut self, stat: Stat) {
let stats = &mut self.0;
stats.push(stat);
}
#[allow(unused_variables)]
pub fn extract_organelle(
&mut self,
size_lower: usize,
mut size_upper: usize,
gc_lower: f32,
gc_upper: f32,
) -> Result<Vec<usize>> {
let seq_len_adj = 20000;
size_upper += seq_len_adj;
let stat_vec = &mut self.0;
let stat_vec_len = stat_vec.len();
if stat_vec_len > 0 {
let stat_vec: Vec<&Stat> = stat_vec
.iter()
.filter(
|Stat {
index,
gc,
cov,
segments,
total_sequence_length,
is_circular,
}| {
(gc > &gc_lower && gc < &gc_upper)
&& (total_sequence_length > &size_lower
&& total_sequence_length < &size_upper)
},
)
.collect();
match stat_vec.len() {
0 => bail!("No subgraphs within the bounds:\nsize_upper: {size_upper}\nsize_lower: {size_lower}\ngc_upper: {gc_upper}\ngc_lower: {gc_lower}\nTry changing limits?"),
1.. => {
let segments = stat_vec.iter().flat_map(|Stat { segments, .. }| segments.clone()).collect();
Ok(segments)
},
_ => unreachable!()
}
} else {
bail!("There were no segments to be extracted. Check input GFA file.");
}
}
}
pub fn stats(
matches: &clap::ArgMatches,
genome_type: GenomeType,
) -> Result<Option<(GFAtk, Vec<usize>)>> {
let gfa_file = matches.value_of("GFA");
let mito_args = if matches!(genome_type, GenomeType::Mitochondria) {
let size_lower: usize = matches.value_of_t("size-lower")?;
let size_upper: usize = matches.value_of_t("size-upper")?;
let gc_lower: f32 = matches.value_of_t("gc-lower")?;
let gc_upper: f32 = matches.value_of_t("gc-upper")?;
Some((size_lower, size_upper, gc_lower, gc_upper))
} else {
None
};
let chloro_args = if matches!(genome_type, GenomeType::Chloroplast) {
let size_lower: usize = matches.value_of_t("size-lower")?;
let size_upper: usize = matches.value_of_t("size-upper")?;
let gc_lower: f32 = matches.value_of_t("gc-lower")?;
let gc_upper: f32 = matches.value_of_t("gc-upper")?;
Some((size_lower, size_upper, gc_lower, gc_upper))
} else {
None
};
let gfa = 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 {} -h` for help.",
match genome_type {
GenomeType::Chloroplast => "extract-chloro",
GenomeType::Mitochondria => "extract-mito",
GenomeType::None => "stats",
}
),
},
};
let (graph_indices, gfa_graph) = gfa.into_digraph()?;
let subgraphs = gfa_graph.weakly_connected_components(graph_indices)?;
let mut no_subgraphs = 0;
let mut store_stats = Stats(Vec::new());
for id_set in &subgraphs {
let subgraph_gfa = GFAtk(segments_subgraph(&gfa.0, id_set.to_vec()));
let (graph_indices_subgraph, subgraph) = subgraph_gfa.into_digraph()?;
let is_circular = is_cyclic_directed(&subgraph.0);
if genome_type == GenomeType::None {
println!("Subgraph {}:", no_subgraphs + 1);
println!("\tNumber of nodes/segments: {}", subgraph.node_count());
println!("\tNumber of edges/links: {}", subgraph.edge_count());
println!("\tCircular: {}", is_circular);
println!("{}", graph_indices_subgraph);
}
let (avg_gc, cov, total_sequence_length) = subgraph_gfa.sequence_stats(genome_type)?;
store_stats.push(Stat {
index: no_subgraphs,
gc: avg_gc,
cov,
segments: id_set.clone(),
total_sequence_length,
is_circular,
});
no_subgraphs += 1;
}
match genome_type {
GenomeType::Mitochondria => {
let mito_args = mito_args.context("There were no `extract-mito` arguments.")?;
return Ok(Some((
gfa,
store_stats.extract_organelle(
mito_args.0,
mito_args.1,
mito_args.2,
mito_args.3,
)?,
)));
}
GenomeType::Chloroplast => {
let chloro_args = chloro_args.context("There were no `extract-chloro` arguments.")?;
return Ok(Some((
gfa,
store_stats.extract_organelle(
chloro_args.0,
chloro_args.1,
chloro_args.2,
chloro_args.3,
)?,
)));
}
GenomeType::None => println!("Total number of subgraphs: {}", no_subgraphs),
}
Ok(None)
}