44 #include <string_view> 47 #include <range/v3/algorithm/copy.hpp> 48 #include <range/v3/utility/iterator.hpp> 49 #include <range/v3/view/chunk.hpp> 50 #include <range/v3/view/drop_while.hpp> 51 #include <range/v3/view/join.hpp> 52 #include <range/v3/view/remove_if.hpp> 53 #include <range/v3/view/take_while.hpp> 136 template <
typename stream_type,
137 typename seq_legal_alph_type,
bool seq_qual_combined,
141 void read(stream_type & stream,
145 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
148 decltype(std::istreambuf_iterator<char>{})>
149 {std::istreambuf_iterator<char>{stream},
150 std::istreambuf_iterator<char>{}};
152 read_id(stream_view, options,
id);
155 read_seq(stream_view, options, sequence);
158 if ((std::istreambuf_iterator<char>{stream} == std::istreambuf_iterator<char>{}) &&
166 template <
typename stream_type,
172 seq_type && sequence,
174 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
180 if constexpr (detail::decays_to_ignore_v<id_type>)
182 throw std::logic_error{
"The ID field may not be set to ignore when writing FASTA files."};
187 throw std::runtime_error{
"The ID field may not be empty when writing FASTA files."};
189 write_id(stream_it, options,
id);
193 if constexpr (detail::decays_to_ignore_v<seq_type>)
195 throw std::logic_error{
"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
200 throw std::runtime_error{
"The SEQ field may not be empty when writing FASTA files."};
202 write_seq(stream_it, options, sequence);
209 template <
typename stream_view_t,
210 typename seq_legal_alph_type,
bool seq_qual_combined,
212 void read_id(stream_view_t & stream_view,
216 auto const is_id = is_char<'>
'> || is_char<';
'>; 218 if (!is_id(*begin(stream_view))) 219 throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg.string() + 220 " evaluated to false on " + detail::make_printable(*begin(stream_view))}; 223 if constexpr (!detail::decays_to_ignore_v<id_type>) 225 if (options.truncate_ids) 227 std::ranges::copy(stream_view | ranges::view::drop_while(is_id || is_blank) // skip leading > 228 | view::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter… 229 | view::char_to<value_type_t<id_type>>, 230 std::back_inserter(id)); // … ^A is old delimiter 232 // consume rest of line 233 detail::consume(stream_view | view::take_line_or_throw); 237 std::ranges::copy(stream_view | view::take_line_or_throw // read line 238 | ranges::view::drop_while(is_id || is_blank) // skip leading > 239 | view::char_to<value_type_t<id_type>>, 240 std::back_inserter(id)); 245 detail::consume(stream_view | view::take_line_or_throw); 250 template <typename stream_view_t, 251 typename seq_legal_alph_type, bool seq_qual_combined, 253 void read_seq(stream_view_t & stream_view, 254 sequence_file_input_options<seq_legal_alph_type, seq_qual_combined> const &, 257 auto constexpr is_id = is_char<'>
'> || is_char<';
'>; 259 if constexpr (!detail::decays_to_ignore_v<seq_type>) 261 auto constexpr is_legal_alph = is_in_alphabet<seq_legal_alph_type>; 262 std::ranges::copy(stream_view | view::take_until(is_id) // until next header (or end) 263 | ranges::view::remove_if(is_space || is_digit)// ignore whitespace and numbers 264 | view::transform([is_legal_alph] (char const c) 266 if (!is_legal_alph(c)) 268 throw parse_error{std::string{"Encountered an unexpected letter: "} + 269 is_legal_alph.msg.string() + 270 " evaluated to false on " + 271 detail::make_printable(c)}; 274 }) // enforce legal alphabet 275 | view::char_to<value_type_t<seq_type>>, // convert to actual target alphabet 276 std::back_inserter(seq)); 280 detail::consume(stream_view | view::take_until(is_id)); 285 template <typename stream_it_t, 287 void write_id(stream_it_t & stream_it, 288 sequence_file_output_options const & options, 291 if (options.fasta_legacy_id_marker) 296 if (options.fasta_blank_before_id) 299 std::ranges::copy(id, stream_it); 301 detail::write_eol(stream_it, options.add_carriage_return); 305 template <typename stream_it_t, 307 void write_seq(stream_it_t & stream_it, 308 sequence_file_output_options const & options, 311 if (options.fasta_letters_per_line > 0) 313 std::ranges::copy(seq | view::to_char 314 | ranges::view::chunk(options.fasta_letters_per_line) 315 | ranges::view::join(options.add_carriage_return 316 ? std::string_view{"\r\n"} 317 : std::string_view{"\n"}), 319 // TODO(h-2): benchmark the above vs: 321 // for (auto seq_it = begin(seq); seq_it != end(seq_it); ++seq_it) 323 // stream_it = to_char(*seq_it); 325 // if (count % fasta_letters_per_line == 0) 327 // detail::write_eol(stream_it, options.add_carriage_return); 333 std::ranges::copy(seq | view::to_char, stream_it); 336 detail::write_eol(stream_it, options.add_carriage_return); 340 } // namespace seqan3 Provides C++20 additions to the <iterator> header.
Contains various shortcuts for common std::ranges functions.
Provides seqan3::detail::ignore_output_iterator for writing to null stream.
Provides seqan3::view::take.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
Contains seqan3::dna5, container aliases and string literals.
::ranges::ostreambuf_iterator ostreambuf_iterator
Alias for ranges::ostreambuf_iterator. Output iterator that writes to std::basic_streambuf.
Definition: ranges:230
Provides seqan3::view::take_line and seqan3::view::take_line_or_throw.
Provides seqan3::view::subrange.
Provides seqan3::sequence_file_output_options.
Provides seqan3::view::take_until and seqan3::view::take_until_or_throw.
Provides various utility functions.
Provides various utility functions.
Provides seqan3::view::char_to.
Adaptations of concepts from the Ranges TS.
std::ranges::iterator_range< it_t, sen_t > subrange
Create a view from a pair of iterator and sentinel.
Definition: subrange.hpp:96
The options type defines various option members that influence the behaviour of all or some formats...
Definition: output_options.hpp:48
::ranges::empty empty
Alias for ranges::empty. Checks whether a range is empty.
Definition: ranges:205
Provides seqan3::view::to_char.
Provides parse conditions for tokenization.
Provides various metafunctions used by the range module.
Contains aliases for qualified.
Provides seqan3::view::take_exactly and seqan3::view::take_exactly_or_throw.