back_to_sequences/cli.rs
1//! Define Command Line Interface
2
3/* std use */
4
5/* crates use */
6use clap::Parser;
7
8/* project use */
9
10/// Extract sequences that contain some kmers
11#[derive(Parser, Debug)]
12#[command(author, version, about, long_about = None)]
13pub struct Args {
14 /// Input fasta file containing the original kmers
15 /// Note: back_to_sequences considers the content as a set of kmers
16 /// This means that a kmer is considered only once,
17 /// even if it occurs multiple times in the file.
18 /// If the stranded option is not used (default), a kmer
19 /// and its reverse complement are considered as the same kmer.
20 #[arg(long, verbatim_doc_comment)]
21 pub in_kmers: String,
22
23 /// Input fasta or fastq [.gz|zst] file containing the original sequences (eg. reads).
24 /// The stdin is used if not provided
25 /// (and if `--in_filelist` is not provided neither)
26 #[arg(long, default_value_t = String::from(""), verbatim_doc_comment)]
27 pub in_sequences: String,
28
29 /// Input txt file containing in each line a path to a fasta or fastq [.gz|zst] file
30 /// containing the original sequences (eg. reads).
31 /// Note1: if this option is used, the `--out_filelist` option must be used.
32 /// The number of lines in out_filelist must be the same as in_filelist
33 /// Note2: Incompatible with `--in_sequences`
34 #[arg(long, default_value_t = String::from(""), verbatim_doc_comment)]
35 pub in_filelist: String,
36
37 /// Output file containing the filtered original sequences (eg. reads).
38 /// It will be automatically in fasta or fastq format depending on the input file.
39 /// If not provided, only the in_kmers with their count is output
40 #[arg(long, default_value_t = String::from(""), verbatim_doc_comment)]
41 pub out_sequences: String,
42
43 /// Output txt file containing in each line a path to a fasta or fastq [.gz] file
44 /// that will contain the related output file from the input files list
45 #[arg(long, default_value_t = String::from(""), verbatim_doc_comment)]
46 pub out_filelist: String,
47
48 /// If provided, output a text file containing the kmers that occur in the reads
49 /// with their
50 /// * number of occurrences
51 /// or
52 /// * their occurrence positions if the --output_kmer_positions option is used
53 /// Note: if `--in_filelist` is used the output counted kmers are
54 /// those occurring the last input file of that list
55 #[arg(long, default_value_t = String::from(""), verbatim_doc_comment)]
56 pub out_kmers: String,
57
58 /// If out_kmers is provided, output only reference kmers whose number of occurrences
59 /// is at least equal to this value.
60 /// If out_kmers is not provided, this option is ignored
61 #[arg(long, default_value_t = 0, verbatim_doc_comment)]
62 pub counted_kmer_threshold: usize,
63
64 /// If out_kmers is provided, either only count their number of occurrences (default)
65 /// or output their occurrence positions (read_id, position, strand) if this option is used
66 #[arg(long, default_value_t = false, verbatim_doc_comment)]
67 pub output_kmer_positions: bool,
68
69 /// If provided, output matching positions on sequences in the
70 /// out_sequence file(s)
71 /// Also outputs the number and ratio of shared kmers
72 /// Also outputs the number of positions covered by shared kmers
73 /// (last value in parathesis)
74 #[arg(long, default_value_t = false, verbatim_doc_comment)]
75 pub output_mapping_positions: bool,
76
77 /// Size of the kmers to index and search
78 #[arg(short, long, default_value_t = 31)]
79 pub kmer_size: usize,
80
81 /// Output sequences are those whose ratio of indexed kmers is in ]min_threshold; max_threshold]
82 /// Minimal threshold of the ratio (%) of kmers that must be found in a sequence to keep it (default 0%).
83 /// Thus by default, if no kmer is found in a sequence, it is not output.
84 #[arg(short, long, default_value_t = 0.0, verbatim_doc_comment)]
85 pub min_threshold: f32,
86
87 /// Output sequences are those whose ratio of indexed kmers is in ]min_threshold; max_threshold]
88 /// Maximal threshold of the ratio (%) of kmers that must be found in a sequence to keep it (default 100%).
89 /// Thus by default, there is no limitation on the maximal number of kmers found in a sequence.
90 #[arg(long, default_value_t = 100.0, verbatim_doc_comment)]
91 pub max_threshold: f32,
92
93 /// Used original kmer strand (else canonical kmers are considered)
94 #[arg(long, default_value_t = false)]
95 pub stranded: bool,
96
97 /// Query the reverse complement of reads. Useless without the --stranded option
98 #[arg(long, default_value_t = false)]
99 pub query_reverse: bool,
100
101 /// Do not index low complexity kmers (ie. with a Shannon entropy < 1.0)
102 #[arg(long, default_value_t = false)]
103 pub no_low_complexity: bool,
104
105 /// Number of threads
106 /// Note: if not provided, the number of threads is set to the number of logical cores
107 #[arg(short, long, default_value_t = 0, verbatim_doc_comment)]
108 pub threads: usize,
109}
110
111/// check that a file name corresponds to a non empty file:
112pub fn validate_non_empty_file(in_file: String) -> anyhow::Result<()> {
113 if let Ok(metadata) = std::fs::metadata(in_file.clone()) {
114 // Check if the file exists
115 if !metadata.is_file() {
116 anyhow::bail!("{:#} exists, but it's not a file.", in_file)
117 }
118 } else {
119 anyhow::bail!("{:#} is not a file.", in_file)
120 }
121
122 Ok(())
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn non_empty_file_test() -> anyhow::Result<()> {
131 let temp_dir = tempfile::tempdir()?;
132 let directory = temp_dir.path().to_path_buf();
133 let file = directory.join("empty.fasta");
134
135 // test directory
136 assert!(
137 validate_non_empty_file(directory.clone().into_os_string().into_string().unwrap())
138 .is_err()
139 );
140
141 // test not exist
142 assert!(
143 validate_non_empty_file(file.clone().into_os_string().into_string().unwrap()).is_err()
144 );
145
146 // test work
147 std::fs::File::create(&file)?;
148 assert!(
149 validate_non_empty_file(file.clone().into_os_string().into_string().unwrap()).is_ok()
150 );
151
152 Ok(())
153 }
154}