SeqAn3 3.2.0-rc.1
The Modern C++ library for sequence analysis.
format_embl.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2021, 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/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <seqan3/std/algorithm>
16#include <iterator>
17#include <seqan3/std/ranges>
18#include <string>
19#include <string_view>
20#include <vector>
21
40
41namespace seqan3
42{
74{
75public:
79 format_embl() noexcept = default;
80 format_embl(format_embl const &) noexcept = default;
81 format_embl & operator=(format_embl const &) noexcept = default;
82 format_embl(format_embl &&) noexcept = default;
83 format_embl & operator=(format_embl &&) noexcept = default;
84 ~format_embl() noexcept = default;
85
87
89 static inline std::vector<std::string> file_extensions
90 {
91 { "embl" },
92 };
93
94protected:
96 template <typename stream_type, // constraints checked by file
97 typename seq_legal_alph_type,
98 typename stream_pos_type,
99 typename seq_type, // other constraints checked inside function
100 typename id_type,
101 typename qual_type>
102 void read_sequence_record(stream_type & stream,
104 stream_pos_type & position_buffer,
105 seq_type & sequence,
106 id_type & id,
107 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
108 {
109 auto stream_view = detail::istreambuf(stream);
110 auto stream_it = std::ranges::begin(stream_view);
111
112 // Store current position in buffer.
113 position_buffer = stream.tellg();
114
115 std::string idbuffer;
116 std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank),
117 std::cpp20::back_inserter(idbuffer));
118 if (idbuffer != "ID")
119 throw parse_error{"An entry has to start with the code word ID."};
120
121 if constexpr (!detail::decays_to_ignore_v<id_type>)
122 {
124 {
125 std::ranges::copy(idbuffer | views::char_to<std::ranges::range_value_t<id_type>>, std::cpp20::back_inserter(id));
126 do
127 {
128 std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<'S'>)
129 | views::char_to<std::ranges::range_value_t<id_type>>,
130 std::cpp20::back_inserter(id));
131 id.push_back(*stream_it);
132 ++stream_it;
133 } while (*stream_it != 'Q');
134 id.pop_back(); // remove 'S' from id
135 idbuffer = "SQ";
136 }
137 else
138 {
139 // ID
141
142 // read id
143 if (options.truncate_ids)
144 {
145 std::ranges::copy(stream_view | detail::take_until_or_throw(is_blank || is_char<';'> || is_cntrl)
146 | views::char_to<std::ranges::range_value_t<id_type>>,
147 std::cpp20::back_inserter(id));
148 }
149 else
150 {
151 std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<';'>)
152 | views::char_to<std::ranges::range_value_t<id_type>>,
153 std::cpp20::back_inserter(id));
154 }
155 }
156 }
157
158 // Jump to sequence
159 if (idbuffer !="SQ")
160 {
161 do
162 {
163 detail::consume(stream_view | detail::take_until_or_throw(is_char<'S'>));
164 ++stream_it;
165 } while (*stream_it != 'Q');
166 }
167 detail::consume(stream_view | detail::take_line_or_throw); //Consume line with infos to sequence
168
169 // Sequence
170 auto constexpr is_end = is_char<'/'> ;
171 if constexpr (!detail::decays_to_ignore_v<seq_type>)
172 {
173 auto seq_view = stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
174 | detail::take_until_or_throw(is_end); // until //
175
176 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
177 std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
178 {
179 if (!is_legal_alph(c))
180 {
181 throw parse_error{std::string{"Encountered an unexpected letter: "} +
182 "char_is_valid_for<" +
183 detail::type_name_as_string<seq_legal_alph_type> +
184 "> evaluated to false on " +
186 }
187 return c;
188 })
189 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
190 std::cpp20::back_inserter(sequence));
191 }
192 else
193 {
194 detail::consume(stream_view | detail::take_until(is_end));
195 }
196 //Jump over // and cntrl
197 ++stream_it;
198 ++stream_it;
199 ++stream_it;
200 }
201
203 template <typename stream_type, // constraints checked by file
204 typename seq_type, // other constraints checked inside function
205 typename id_type,
206 typename qual_type>
207 void write_sequence_record(stream_type & stream,
208 sequence_file_output_options const & options,
209 seq_type && sequence,
210 id_type && id,
211 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
212 {
213 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
214
215 [[maybe_unused]] size_t sequence_size = 0;
216
217 if constexpr (!detail::decays_to_ignore_v<seq_type>)
218 sequence_size = std::ranges::distance(sequence);
219
220 // ID
221 if constexpr (detail::decays_to_ignore_v<id_type>)
222 {
223 throw std::logic_error{"The ID field may not be set to ignore when writing embl files."};
224 }
225 else
226 {
227 if (ranges::empty(id)) //[[unlikely]]
228 throw std::runtime_error{"The ID field may not be empty when writing embl files."};
229
231 {
232 stream_it.write_range(id);
233 }
234 else
235 {
236 stream_it.write_range(std::string_view{"ID "});
237 stream_it.write_range(id);
238 stream_it.write_range(std::string_view{"; "});
239 stream_it.write_number(sequence_size);
240 stream_it.write_range(std::string_view{" BP.\n"});
241 }
242 }
243
244 // Sequence
245 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
246 {
247 throw std::logic_error{"The SEQ field may not be set to ignore when writing embl files."};
248 }
249 else
250 {
251 if (std::ranges::empty(sequence)) //[[unlikely]]
252 throw std::runtime_error{"The SEQ field may not be empty when writing embl files."};
253
254 // write beginning of sequence record
255 stream_it.write_range(std::string_view{"SQ Sequence "});
256 stream_it.write_number(sequence_size);
257 stream_it.write_range(std::string_view{" BP;\n"});
258
259 // write sequence in chunks of 60 bp's with a space after 10 bp's
260 auto char_sequence = sequence | views::to_char;
261 auto it = std::ranges::begin(char_sequence);
262 size_t written_chars{0};
263 uint8_t chunk_size{10u};
264
265 while (it != std::ranges::end(char_sequence))
266 {
267 auto current_end = it;
268 size_t steps = std::ranges::advance(current_end, chunk_size, std::ranges::end(char_sequence));
269
270 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
271 it = stream_it.write_range(subrange_t{it, current_end, chunk_size - steps});
272 stream_it = ' ';
273 written_chars += chunk_size;
274
275 if (written_chars % 60 == 0)
276 {
277 stream_it.write_number(written_chars);
278 stream_it.write_end_of_line(options.add_carriage_return);
279 }
280 }
281
282 // fill last line
283 auto characters_in_last_line = sequence_size % 60;
284 auto number_of_padding_needed = 65 - characters_in_last_line - characters_in_last_line / chunk_size;
285 stream_it.write_range(views::repeat_n(' ', number_of_padding_needed));
286 stream_it.write_number(sequence_size);
287 stream_it.write_end_of_line(options.add_carriage_return);
288
289 // write end-of-record-symbol
290 stream_it.write_range(std::string_view{"//"});
291 stream_it.write_end_of_line(options.add_carriage_return);
292 }
293 }
294};
295
296} // namespace seqan
The <algorithm> header from C++20's standard library.
Core alphabet concept and free function/type trait wrappers.
Provides seqan3::views::char_to.
Functionally the same as std::ostreambuf_iterator, but offers writing a range more efficiently.
Definition: fast_ostreambuf_iterator.hpp:39
The EMBL format.
Definition: format_embl.hpp:74
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition: format_embl.hpp:90
void write_sequence_record(stream_type &stream, sequence_file_output_options const &options, seq_type &&sequence, id_type &&id, qual_type &&qualities)
Write the given fields to the specified stream.
Definition: format_embl.hpp:207
void read_sequence_record(stream_type &stream, sequence_file_input_options< seq_legal_alph_type > const &options, stream_pos_type &position_buffer, seq_type &sequence, id_type &id, qual_type &qualities)
Read from the specified stream and back-insert into the given field buffers.
Definition: format_embl.hpp:102
format_embl() noexcept=default
Defaulted.
Provides various utility functions.
Provides various transformation traits used by the range module.
Provides seqan3::dna5, container aliases and string literals.
Provides seqan3::detail::fast_ostreambuf_iterator.
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:63
auto const char_to
A view over an alphabet, given a range of characters.
Definition: char_to.hpp:67
constexpr void consume(rng_t &&rng)
Iterate over a range (consumes single-pass input ranges).
Definition: misc.hpp:28
auto constexpr take_line_or_throw
A view adaptor that returns a single line from the underlying range (throws if there is no end-of-lin...
Definition: take_line_view.hpp:85
auto constexpr take_until_or_throw
A view adaptor that returns elements from the underlying range until the functor evaluates to true (t...
Definition: take_until_view.hpp:592
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition: istreambuf_view.hpp:110
auto constexpr take_until
A view adaptor that returns elements from the underlying range until the functor evaluates to true (o...
Definition: take_until_view.hpp:578
auto constexpr is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:145
std::string make_printable(char const c)
Returns a printable value for the given character c.
Definition: pretty_print.hpp:48
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition: predicate.hpp:65
auto constexpr is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:92
auto constexpr is_space
Checks whether c is a space character.
Definition: predicate.hpp:128
auto constexpr is_digit
Checks whether c is a digital character.
Definition: predicate.hpp:269
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: traits.hpp:495
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:91
The generic concept for a (biological) sequence.
Provides various utility functions.
Provides seqan3::detail::istreambuf.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
The <ranges> header from C++20's standard library.
Provides seqan3::views::repeat_n.
Provides seqan3::sequence_file_input_format and auxiliary classes.
Provides seqan3::sequence_file_input_options.
Provides seqan3::sequence_file_output_format and auxiliary classes.
Provides seqan3::sequence_file_output_options.
Thrown if there is a parse error, such as reading an unexpected character from an input stream.
Definition: exception.hpp:48
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:27
bool embl_genbank_complete_header
Read the complete_header into the seqan3::field::id for embl or genbank format.
Definition: input_options.hpp:31
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition: input_options.hpp:29
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:26
bool add_carriage_return
The default plain text line-ending is "\n", but on Windows an additional carriage return is recommend...
Definition: output_options.hpp:43
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition: output_options.hpp:46
Provides seqan3::detail::take_line and seqan3::detail::take_line_or_throw.
Provides seqan3::views::take_until and seqan3::views::take_until_or_throw.
Provides seqan3::views::to_char.
Provides traits to inspect some information of a type, for example its name.