fastq_to_fasta
A template for creation of SeqAn3 apps, with a FASTQ to FASTA example app.
validators.hpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/raptor/blob/main/LICENSE.md
6 // --------------------------------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <sharg/parser.hpp>
11 
12 #include <seqan3/io/sequence_file/input.hpp>
13 
14 #include <raptor/strong_types.hpp>
15 
16 namespace raptor::detail
17 {
18 
19 static inline std::vector<std::string> sequence_extensions{
20  seqan3::detail::valid_file_extensions<typename seqan3::sequence_file_input<>::valid_formats>()};
21 
22 static inline std::vector<std::string> compression_extensions{[]()
23  {
24  std::vector<std::string> result;
25 #ifdef SEQAN3_HAS_BZIP2
26  result.push_back("bz2");
27 #endif
28 #ifdef SEQAN3_HAS_ZLIB
29  result.push_back("gz");
30  result.push_back("bgzf");
31 #endif
32  return result;
33  }()}; // GCOVR_EXCL_LINE
34 
35 static inline std::vector<std::string> combined_extensions{
36  []()
37  {
38  if (compression_extensions.empty())
39  return sequence_extensions; // GCOVR_EXCL_LINE
40  std::vector<std::string> result;
41  for (auto && sequence_extension : sequence_extensions)
42  {
43  result.push_back(sequence_extension);
44  for (auto && compression_extension : compression_extensions)
45  result.push_back(sequence_extension + std::string{'.'} + compression_extension);
46  }
47  return result;
48  }()};
49 
50 } // namespace raptor::detail
51 
52 namespace raptor
53 {
54 
56 {
57  using option_value_type = size_t;
58 
59  void operator()(option_value_type const & val) const
60  {
61  if (!std::has_single_bit(val))
62  throw sharg::validation_error{"The value must be a power of two."};
63  }
64 
65  static std::string get_help_page_message()
66  {
67  return "Value must be a power of two.";
68  }
69 };
70 
72 {
73 public:
74  using option_value_type = size_t;
75 
82 
83  explicit positive_integer_validator(bool const is_zero_positive_) : is_zero_positive{is_zero_positive_}
84  {}
85 
86  void operator()(window const & val) const
87  {
88  return operator()(val.v);
89  }
90 
91  void operator()(option_value_type const & val) const
92  {
93  if (!is_zero_positive && !val)
94  throw sharg::validation_error{"The value must be a positive integer."};
95  }
96 
97  std::string get_help_page_message() const
98  {
99  if (is_zero_positive)
100  return "Value must be a positive integer or 0.";
101  else
102  return "Value must be a positive integer.";
103  }
104 
105 private:
106  bool is_zero_positive{false};
107 };
108 
110 {
111 public:
112  using option_value_type = std::string;
113 
114  size_validator() = default;
115  size_validator(size_validator const &) = default;
119  ~size_validator() = default;
120 
121  explicit size_validator(std::string const & pattern) : expression{pattern}
122  {}
123 
124  void operator()(option_value_type const & cmp) const
125  {
126  if (!std::regex_match(cmp, expression))
127  throw sharg::validation_error{
128  seqan3::detail::to_string("Value ",
129  cmp,
130  " must be an integer followed by [k,m,g,t] (case insensitive).")};
131  }
132 
133  template <std::ranges::forward_range range_type>
134  requires std::convertible_to<std::ranges::range_value_t<range_type>, option_value_type const &>
135  void operator()(range_type const & v) const
136  {
137  std::for_each(v.begin(),
138  v.end(),
139  [&](auto cmp)
140  {
141  (*this)(cmp);
142  });
143  }
144 
145  std::string get_help_page_message() const
146  {
147  return "Must be an integer followed by [k,m,g,t] (case insensitive).";
148  }
149 
150 private:
151  std::regex expression;
152 };
153 
155 {
156 public:
157  using option_value_type = std::vector<std::vector<std::string>>;
158 
159  bin_validator() = default;
160  bin_validator(bin_validator const &) = default;
161  bin_validator & operator=(bin_validator const &) = default;
164  ~bin_validator() = default;
165 
166  void operator()(option_value_type const & values) const
167  {
168  if (values.empty())
169  throw sharg::validation_error{"The list of input files cannot be empty."};
170 
171  bool const is_minimiser_input = std::filesystem::path{values[0][0]}.extension() == ".minimiser";
172 
173  for (std::vector<std::string> const & vector_of_paths : values)
174  {
175  for (std::string const & value : vector_of_paths)
176  {
177  std::filesystem::path const file_path{value};
178 
179  if (is_minimiser_input && (file_path.extension() != ".minimiser"))
180  throw sharg::validation_error{"You cannot mix sequence and minimiser files as input."};
181  if (std::filesystem::file_size(file_path) == 0u)
182  throw sharg::validation_error{"The file " + value + " is empty."};
183 
184  if (is_minimiser_input)
185  minimiser_file_validator(file_path);
186  else
187  sequence_file_validator(file_path);
188  }
189  }
190  }
191 
192  std::string get_help_page_message() const
193  {
194  // Update
195  return seqan3::detail::to_string("The file must contain at least one file path per line, with multiple paths "
196  "being separated by a whitespace. Each line in the file corresponds to one "
197  "bin. Valid extensions for the paths in the file are [minimiser] when "
198  " preprocessing, and ",
199  raptor::detail::sequence_extensions,
200 #if defined(SEQAN3_HAS_BZIP2) || defined(SEQAN3_HAS_ZLIB)
201  ", possibly followed by ",
202  raptor::detail::compression_extensions,
203 #endif
204  " otherwise. ");
205  }
206 
207 private:
208  sharg::input_file_validator minimiser_file_validator{{"minimiser"}};
209 
210 public:
211  sharg::input_file_validator sequence_file_validator{raptor::detail::combined_extensions};
212 };
213 
214 } // namespace raptor
Definition: validators.hpp:155
sharg::input_file_validator sequence_file_validator
Definition: validators.hpp:211
bin_validator(bin_validator &&)=default
void operator()(option_value_type const &values) const
Definition: validators.hpp:166
std::vector< std::vector< std::string > > option_value_type
Definition: validators.hpp:157
std::string get_help_page_message() const
Definition: validators.hpp:192
bin_validator & operator=(bin_validator &&)=default
bin_validator & operator=(bin_validator const &)=default
bin_validator(bin_validator const &)=default
Definition: validators.hpp:72
std::string get_help_page_message() const
Definition: validators.hpp:97
positive_integer_validator & operator=(positive_integer_validator &&)=default
size_t option_value_type
Definition: validators.hpp:74
void operator()(window const &val) const
Definition: validators.hpp:86
void operator()(option_value_type const &val) const
Definition: validators.hpp:91
positive_integer_validator(positive_integer_validator &&)=default
positive_integer_validator & operator=(positive_integer_validator const &)=default
positive_integer_validator(positive_integer_validator const &)=default
positive_integer_validator(bool const is_zero_positive_)
Definition: validators.hpp:83
Definition: validators.hpp:110
void operator()(option_value_type const &cmp) const
Definition: validators.hpp:124
size_validator & operator=(size_validator const &)=default
size_validator(size_validator const &)=default
std::string get_help_page_message() const
Definition: validators.hpp:145
std::string option_value_type
Definition: validators.hpp:112
size_validator(size_validator &&)=default
size_validator & operator=(size_validator &&)=default
size_validator(std::string const &pattern)
Definition: validators.hpp:121
requires std::convertible_to< std::ranges::range_value_t< range_type >, option_value_type const & > void operator()(range_type const &v) const
Definition: validators.hpp:135
Definition: parse_bin_path.hpp:17
Definition: adjust_seed.hpp:13
Definition: validators.hpp:56
size_t option_value_type
Definition: validators.hpp:57
void operator()(option_value_type const &val) const
Definition: validators.hpp:59
static std::string get_help_page_message()
Definition: validators.hpp:65
Strong type for passing the window size.
Definition: strong_types.hpp:17
uint32_t v
Definition: strong_types.hpp:18