SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
format_embl.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13#include <iterator>
14#include <ranges>
15#include <string>
16#include <string_view>
17#include <vector>
18
38
39namespace seqan3
40{
72{
73public:
83
85
88 {"embl"},
89 };
90
91protected:
93 template <typename stream_type, // constraints checked by file
94 typename seq_legal_alph_type,
95 typename stream_pos_type,
96 typename seq_type, // other constraints checked inside function
97 typename id_type,
98 typename qual_type>
101 stream_pos_type & position_buffer,
102 seq_type & sequence,
103 id_type & id,
105 {
106 // Store current position in buffer
107 // Must happen before constructing the view.
108 // With libc++, tellg invalidates the I/O buffer.
109 position_buffer = stream.tellg();
110
111 auto stream_view = detail::istreambuf(stream);
113
115 std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank),
117 if (idbuffer != "ID")
118 throw parse_error{"An entry has to start with the code word ID."};
119
120 if constexpr (!detail::decays_to_ignore_v<id_type>)
121 {
122 if (options.embl_genbank_complete_header)
123 {
124 std::ranges::copy(idbuffer | views::char_to<std::ranges::range_value_t<id_type>>,
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>>,
131 id.push_back(*stream_it);
132 ++stream_it;
133 }
134 while (*stream_it != 'Q');
135 id.pop_back(); // remove 'S' from id
136 idbuffer = "SQ";
137 }
138 else
139 {
140 // ID
141 detail::consume(stream_view | detail::take_until(!is_blank));
142
143 // read id
144 if (options.truncate_ids)
145 {
146 std::ranges::copy(stream_view | detail::take_until_or_throw(is_blank || is_char<';'> || is_cntrl)
147 | views::char_to<std::ranges::range_value_t<id_type>>,
149 }
150 else
151 {
152 std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<';'>)
153 | views::char_to<std::ranges::range_value_t<id_type>>,
155 }
156 }
157 }
158
159 // Jump to sequence
160 if (idbuffer != "SQ")
161 {
162 do
163 {
164 detail::consume(stream_view | detail::take_until_or_throw(is_char<'S'>));
165 ++stream_it;
166 }
167 while (*stream_it != 'Q');
168 }
169 detail::consume(stream_view | detail::take_line_or_throw); //Consume line with infos to sequence
170
171 // Sequence
172 constexpr auto is_end = is_char<'/'>;
173 if constexpr (!detail::decays_to_ignore_v<seq_type>)
174 {
175 auto seq_view = stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
176 | detail::take_until_or_throw(is_end); // until //
177
181 | std::views::transform(
182 [is_legal_alph](char const c) // enforce legal alphabet
183 {
184 if (!is_legal_alph(c))
185 {
186 throw parse_error{std::string{"Encountered an unexpected letter: "}
187 + "char_is_valid_for<"
188 + detail::type_name_as_string<seq_legal_alph_type>
189 + "> evaluated to false on " + detail::make_printable(c)};
190 }
191 return c;
192 })
193 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
195 }
196 else
197 {
198 detail::consume(stream_view | detail::take_until_or_throw(is_end)); // consume until "//"
199 }
200
201 detail::consume(stream_view | detail::take_line); // Skip `//` and the line break
202 }
203
205 template <typename stream_type, // constraints checked by file
206 typename seq_type, // other constraints checked inside function
207 typename id_type,
208 typename qual_type>
210 sequence_file_output_options const & options,
211 seq_type && sequence,
212 id_type && id,
214 {
215 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
216
217 [[maybe_unused]] size_t sequence_size = 0;
218
219 if constexpr (!detail::decays_to_ignore_v<seq_type>)
220 sequence_size = std::ranges::distance(sequence);
221
222 // ID
223 if constexpr (detail::decays_to_ignore_v<id_type>)
224 {
225 throw std::logic_error{"The ID field may not be set to ignore when writing embl files."};
226 }
227 else
228 {
229 if (std::ranges::empty(id)) //[[unlikely]]
230 throw std::runtime_error{"The ID field may not be empty when writing embl files."};
231
233 {
234 stream_it.write_range(id);
235 }
236 else
237 {
238 stream_it.write_range(std::string_view{"ID "});
239 stream_it.write_range(id);
240 stream_it.write_range(std::string_view{"; "});
241 stream_it.write_number(sequence_size);
242 stream_it.write_range(std::string_view{" BP."});
243 stream_it.write_end_of_line(options.add_carriage_return);
244 }
245 }
246
247 // Sequence
248 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
249 {
250 throw std::logic_error{"The SEQ field may not be set to ignore when writing embl files."};
251 }
252 else
253 {
254 if (std::ranges::empty(sequence)) //[[unlikely]]
255 throw std::runtime_error{"The SEQ field may not be empty when writing embl files."};
256
257 // write beginning of sequence record
258 stream_it.write_range(std::string_view{"SQ Sequence "});
259 stream_it.write_number(sequence_size);
260 stream_it.write_range(std::string_view{" BP;"});
261 stream_it.write_end_of_line(options.add_carriage_return);
262
263 // write sequence in lines of 60 bp's with a space after 10 bp's
265
266 if constexpr (std::ranges::random_access_range<decltype(char_sequence)>)
267 {
268 size_t written_chars{0};
269
270 for (auto && line : char_sequence | views::chunk(60))
271 {
272 for (auto && block : line | views::chunk(10))
273 {
274 stream_it.write_range(block);
275 stream_it = ' ';
276 }
277
278 // A full line has 6 blocks of 10 bp's, each followed by a space. Pad shorter lines to the same width.
279 size_t const line_size = std::min<size_t>(sequence_size - written_chars, 60u);
280 size_t const number_of_blocks = (line_size + 9u) / 10u;
282 stream_it.write_range(views::repeat_n(' ', 66u - line_size - number_of_blocks));
283 stream_it.write_number(written_chars);
284 stream_it.write_end_of_line(options.add_carriage_return);
285 }
286 }
287 else // views::chunk is slow for ranges that are not random access.
288 {
290 size_t written_chars{0};
291 uint8_t chunk_size{10u};
292
293 while (it != std::ranges::end(char_sequence))
294 {
295 auto current_end = it;
296 size_t steps = std::ranges::advance(current_end, chunk_size, std::ranges::end(char_sequence));
297
298 using subrange_t =
299 std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
301 stream_it = ' ';
303
304 if (written_chars % 60 == 0 && it != std::ranges::end(char_sequence)) // last line is written below
305 {
306 stream_it.write_number(written_chars);
307 stream_it.write_end_of_line(options.add_carriage_return);
308 }
309 }
310
311 // fill last line: a full line has 6 chunks of 10 bp's, each followed by a space
312 auto characters_in_last_line = (sequence_size - 1) % 60 + 1;
316 stream_it.write_number(sequence_size);
317 stream_it.write_end_of_line(options.add_carriage_return);
318 }
319
320 // write end-of-record-symbol
321 stream_it.write_range(std::string_view{"//"});
322 stream_it.write_end_of_line(options.add_carriage_return);
323 }
324 }
325};
326
327} // namespace seqan3
Core alphabet concept and free function/type trait wrappers.
T back_inserter(T... args)
T begin(T... args)
Provides seqan3::views::char_to.
Provides seqan3::views::chunk.
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The EMBL format.
Definition format_embl.hpp:72
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:87
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:209
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:99
format_embl() noexcept=default
Defaulted.
T copy(T... args)
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:60
auto const char_to
A view over an alphabet, given a range of characters.
Definition char_to.hpp:64
constexpr auto is_blank
Checks whether c is a blank character.
Definition predicate.hpp:139
constexpr auto is_digit
Checks whether c is a digital character.
Definition predicate.hpp:259
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition predicate.hpp:60
constexpr auto is_space
Checks whether c is a space character.
Definition predicate.hpp:122
constexpr auto is_cntrl
Checks whether c is a control character.
Definition predicate.hpp:87
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition repeat_n.hpp:88
seqan::stl::views::chunk chunk
A view adaptor that divides a range into chunks. <dl class="no-api">This entity is not part of the Se...
Definition chunk.hpp:23
The generic concept for a (biological) sequence.
Provides various utility functions.
Provides seqan3::detail::istreambuf.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
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 io/exception.hpp:45
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/output_options.hpp:23
bool add_carriage_return
The default plain text line-ending is "\n", but on Windows an additional carriage return is recommend...
Definition sequence_file/output_options.hpp:39
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition sequence_file/output_options.hpp:42
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.
Hide me