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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use anyhow::Result;
use clap::{crate_version, Arg, Command};
use clap::{value_parser, ArgAction};
use std::path::PathBuf;
use std::process;
use mmft::fasta::extract;
use mmft::fasta::filter;
use mmft::fasta::gc;
use mmft::fasta::length;
use mmft::fasta::merge;
use mmft::fasta::min;
use mmft::fasta::n50;
use mmft::fasta::number;
use mmft::fasta::regex;
use mmft::fasta::reverse;
use mmft::fasta::sample;
use mmft::fasta::split;
use mmft::fasta::translate;
fn main() -> Result<()> {
let matches = Command::new("mmft")
.version(crate_version!())
.author("Max Brown <max.carter-brown@aru.ac.uk>")
.about("My Minimal Fasta Toolkit")
.propagate_version(true)
.arg_required_else_help(true)
.subcommand(
Command::new("len")
.about("Calculate lengths of fasta file records.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("extract")
.long("extract")
.short('e')
.value_parser(value_parser!(usize))
.num_args(1)
.required(false)
.help("Fasta records with a length greater than specified are printed."),
)
.arg(Arg::new("less")
.long("less")
.action(ArgAction::SetTrue)
.short('l').help(
"Print records with lengths less than value of extract. Default is greater.",
)),
)
.subcommand(
Command::new("gc")
.about("Calculate GC content of fasta file records.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
),
)
.subcommand(
Command::new("n50")
.about("Calculate n50 of fasta files.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
),
)
.subcommand(
Command::new("regex")
.about("Extract fasta records using regex on headers.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("regex")
.short('r')
.long("regex")
.num_args(1)
.required(true)
.help("Regex to compile."),
)
.arg(
Arg::new("inverse")
.short('i')
.long("inverse")
.action(ArgAction::SetTrue)
.help("Inverse regex match."),
),
)
.subcommand(
Command::new("extract")
.about("Extract (sub)sequence within a fasta file record.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("region")
.short('r')
.long("region")
.num_args(1)
.required(true)
.help("Numeric region to extract."),
),
)
.subcommand(
Command::new("num")
.about("Calculate number and total base count of fasta file records.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
),
)
.subcommand(
Command::new("merge")
.about(
"Merge sequence records within/between fasta files into a single fasta record.",
)
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("header")
.long("header")
.num_args(1)
.help("Name of output fasta header."),
),
)
.subcommand(
Command::new("trans")
.about("Translate a fasta into all six frames.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
),
)
.subcommand(
Command::new("filter")
.about("Filter sequences on a file of ID's")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("file")
.short('f')
.long("file")
.num_args(1)
.required(true)
.help("Name of text file with one ID per line."),
),
)
.subcommand(
Command::new("sample")
.about("Randomly sample records from a fasta file.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("sample-number")
.short('n')
.long("sample-number")
.value_parser(value_parser!(i32))
.num_args(1)
.required_unless_present("sample-size")
.help("Number of records to sample."),
)
.arg(
Arg::new("sample-size")
.short('s')
.long("sample-size")
.value_parser(value_parser!(String))
.num_args(1)
.required_unless_present("sample-number")
.help("Target file size for the sampled sequences (e.g. 10Mb, 3Kb, 5Gb)")
),
)
.subcommand(
Command::new("reverse")
.about("Reverse complement each record in an input fasta")
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
)
.subcommand(
Command::new("min")
.about("Return the lexicographically minimal rotation of fasta file record sequences.")
// output file name
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
),
)
.subcommand(
Command::new("split")
.about("Split a fasta into multiple files based on record count.")
.arg(
Arg::new("fasta")
.value_parser(value_parser!(PathBuf))
.num_args(0..)
.help("Input fasta file path(s)."),
)
.arg(
Arg::new("number")
.short('n')
.long("number")
.value_parser(value_parser!(i32))
.num_args(1)
.required(true)
.help("Number of records per file."),
)
.arg(
Arg::new("dir")
.short('d')
.long("dir")
.value_parser(value_parser!(PathBuf))
.num_args(1)
.default_value(".")
.help(
"Output directory for split files. Default is current working directory.",
),
)
)
.get_matches();
// feed command line options to each main function
match matches.subcommand() {
Some(("len", matches)) => {
length::get_lengths(matches)?;
}
Some(("gc", matches)) => {
gc::get_gc(matches)?;
}
Some(("n50", matches)) => {
n50::get_n50(matches)?;
}
Some(("regex", matches)) => {
regex::regex_sequences(matches)?;
}
Some(("extract", matches)) => {
extract::extract_region(matches)?;
}
Some(("num", matches)) => {
number::get_number_seq_bases(matches)?;
}
Some(("merge", matches)) => {
merge::merge_fastas(matches)?;
}
Some(("filter", matches)) => {
filter::filter_sequences(matches)?;
}
Some(("trans", matches)) => {
translate::six_frame_translate(matches)?;
}
Some(("sample", matches)) => {
sample::sample(matches)?;
}
Some(("reverse", matches)) => reverse::reverse(matches)?,
Some(("min", matches)) => {
min::min(matches)?;
}
Some(("split", matches)) => {
split::split_fasta(matches)?;
}
_ => {
println!("Subcommand invalid, run with '--help' for subcommand options. Exiting.");
process::exit(1);
}
}
Ok(())
}