SeqAn3 3.4.0-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-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 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
37
38namespace seqan3
39{
71{
72public:
76 format_embl() noexcept = default;
77 format_embl(format_embl const &) noexcept = default;
78 format_embl & operator=(format_embl const &) noexcept = default;
79 format_embl(format_embl &&) noexcept = default;
80 format_embl & operator=(format_embl &&) noexcept = default;
81 ~format_embl() noexcept = default;
82
84
86 static inline std::vector<std::string> file_extensions{
87 {"embl"},
88 };
89
90protected:
92 template <typename stream_type, // constraints checked by file
93 typename seq_legal_alph_type,
94 typename stream_pos_type,
95 typename seq_type, // other constraints checked inside function
96 typename id_type,
97 typename qual_type>
98 void read_sequence_record(stream_type & stream,
100 stream_pos_type & position_buffer,
101 seq_type & sequence,
102 id_type & id,
103 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
104 {
105 // Store current position in buffer
106 // Must happen before constructing the view.
107 // With libc++, tellg invalidates the I/O buffer.
108 position_buffer = stream.tellg();
109
110 auto stream_view = detail::istreambuf(stream);
111 auto stream_it = std::ranges::begin(stream_view);
112
113 std::string idbuffer;
114 std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank),
115 std::back_inserter(idbuffer));
116 if (idbuffer != "ID")
117 throw parse_error{"An entry has to start with the code word ID."};
118
119 if constexpr (!detail::decays_to_ignore_v<id_type>)
120 {
122 {
123 std::ranges::copy(idbuffer | views::char_to<std::ranges::range_value_t<id_type>>,
125 do
126 {
127 std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<'S'>)
128 | views::char_to<std::ranges::range_value_t<id_type>>,
130 id.push_back(*stream_it);
131 ++stream_it;
132 }
133 while (*stream_it != 'Q');
134 id.pop_back(); // remove 'S' from id
135 idbuffer = "SQ";
136 }
137 else
138 {
139 // ID
140 detail::consume(stream_view | detail::take_until(!is_blank));
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>>,
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>>,
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 }
166 while (*stream_it != 'Q');
167 }
168 detail::consume(stream_view | detail::take_line_or_throw); //Consume line with infos to sequence
169
170 // Sequence
171 constexpr auto is_end = is_char<'/'>;
172 if constexpr (!detail::decays_to_ignore_v<seq_type>)
173 {
174 auto seq_view = stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
175 | detail::take_until_or_throw(is_end); // until //
176
177 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
179 seq_view
180 | std::views::transform(
181 [is_legal_alph](char const c) // enforce legal alphabet
182 {
183 if (!is_legal_alph(c))
184 {
185 throw parse_error{std::string{"Encountered an unexpected letter: "}
186 + "char_is_valid_for<"
187 + detail::type_name_as_string<seq_legal_alph_type>
188 + "> evaluated to false on " + detail::make_printable(c)};
189 }
190 return c;
191 })
192 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
194 }
195 else
196 {
197 detail::consume(stream_view | detail::take_until_or_throw(is_end)); // consume until "//"
198 }
199
200 std::ranges::advance(stream_it, 3u, std::ranges::end(stream_view)); // Skip `//` and potentially '\n'
201 }
202
204 template <typename stream_type, // constraints checked by file
205 typename seq_type, // other constraints checked inside function
206 typename id_type,
207 typename qual_type>
208 void write_sequence_record(stream_type & stream,
209 sequence_file_output_options const & options,
210 seq_type && sequence,
211 id_type && id,
212 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
213 {
214 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
215
216 [[maybe_unused]] size_t sequence_size = 0;
217
218 if constexpr (!detail::decays_to_ignore_v<seq_type>)
219 sequence_size = std::ranges::distance(sequence);
220
221 // ID
222 if constexpr (detail::decays_to_ignore_v<id_type>)
223 {
224 throw std::logic_error{"The ID field may not be set to ignore when writing embl files."};
225 }
226 else
227 {
228 if (std::ranges::empty(id)) //[[unlikely]]
229 throw std::runtime_error{"The ID field may not be empty when writing embl files."};
230
232 {
233 stream_it.write_range(id);
234 }
235 else
236 {
237 stream_it.write_range(std::string_view{"ID "});
238 stream_it.write_range(id);
239 stream_it.write_range(std::string_view{"; "});
240 stream_it.write_number(sequence_size);
241 stream_it.write_range(std::string_view{" BP.\n"});
242 }
243 }
244
245 // Sequence
246 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
247 {
248 throw std::logic_error{"The SEQ field may not be set to ignore when writing embl files."};
249 }
250 else
251 {
252 if (std::ranges::empty(sequence)) //[[unlikely]]
253 throw std::runtime_error{"The SEQ field may not be empty when writing embl files."};
254
255 // write beginning of sequence record
256 stream_it.write_range(std::string_view{"SQ Sequence "});
257 stream_it.write_number(sequence_size);
258 stream_it.write_range(std::string_view{" BP;\n"});
259
260 // write sequence in chunks of 60 bp's with a space after 10 bp's
261 auto char_sequence = sequence | views::to_char;
262 auto it = std::ranges::begin(char_sequence);
263 size_t written_chars{0};
264 uint8_t chunk_size{10u};
265
266 while (it != std::ranges::end(char_sequence))
267 {
268 auto current_end = it;
269 size_t steps = std::ranges::advance(current_end, chunk_size, std::ranges::end(char_sequence));
270
271 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
272 it = stream_it.write_range(subrange_t{it, current_end, chunk_size - steps});
273 stream_it = ' ';
274 written_chars += chunk_size;
275
276 if (written_chars % 60 == 0)
277 {
278 stream_it.write_number(written_chars);
279 stream_it.write_end_of_line(options.add_carriage_return);
280 }
281 }
282
283 // fill last line
284 auto characters_in_last_line = sequence_size % 60;
285 auto number_of_padding_needed = 65 - characters_in_last_line - characters_in_last_line / chunk_size;
286 stream_it.write_range(views::repeat_n(' ', number_of_padding_needed));
287 stream_it.write_number(sequence_size);
288 stream_it.write_end_of_line(options.add_carriage_return);
289
290 // write end-of-record-symbol
291 stream_it.write_range(std::string_view{"//"});
292 stream_it.write_end_of_line(options.add_carriage_return);
293 }
294 }
295};
296
297} // 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.
The EMBL format.
Definition format_embl.hpp:71
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:86
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:208
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:98
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
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/input_options.hpp:24
bool embl_genbank_complete_header
Read the complete_header into the seqan3::field::id for embl or genbank format.
Definition sequence_file/input_options.hpp:28
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition sequence_file/input_options.hpp:26
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