SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
format_genbank.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 <seqan3/std/charconv>
16#include <string>
17#include <string_view>
18#include <vector>
19
38
39namespace seqan3
40{
41
70{
71public:
75 format_genbank() noexcept = default;
76 format_genbank(format_genbank const &) noexcept = default;
77 format_genbank & operator=(format_genbank const &) noexcept = default;
78 format_genbank(format_genbank &&) noexcept = default;
79 format_genbank & operator=(format_genbank &&) noexcept = default;
80 ~format_genbank() noexcept = default;
81
83
85 static inline std::vector<std::string> file_extensions{
86 {"genbank"},
87 {"gb"},
88 {"gbk"},
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>
99 void read_sequence_record(stream_type & stream,
101 stream_pos_type & position_buffer,
102 seq_type & sequence,
103 id_type & id,
104 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
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);
112 auto stream_it = std::ranges::begin(stream_view);
113
115 std::string{"LOCUS"})))
116 throw parse_error{"An entry has to start with the code word LOCUS."};
117
118 //ID
119 if constexpr (!detail::decays_to_ignore_v<id_type>)
120 {
122 {
124
125 while (!is_char<'O'>(*std::ranges::begin(stream_view)))
126 {
128 | views::char_to<std::ranges::range_value_t<id_type>>,
130 id.push_back('\n');
131 }
132 }
133 else
134 {
136
137 auto read_id_until = [&stream_view, &id](auto predicate)
138 {
139 std::ranges::copy(stream_view | detail::take_until_or_throw(predicate)
140 | views::char_to<std::ranges::range_value_t<id_type>>,
142 };
143
144 if (options.truncate_ids)
145 read_id_until(is_space);
146 else
147 read_id_until(is_cntrl);
148
150 }
151 }
152
153 // Jump to sequence
154 while (!(is_char<'O'>(*std::ranges::begin(stream_view)) || options.embl_genbank_complete_header))
156
157 // Sequence
158 detail::consume(stream_view | detail::take_line_or_throw); // consume "ORIGIN"
159 constexpr auto is_end = is_char<'/'>;
160 if constexpr (!detail::decays_to_ignore_v<seq_type>)
161 {
162 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
164 stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
165 | detail::take_until_or_throw(is_end) // until //
166 | std::views::transform(
167 [is_legal_alph](char const c) // enforce legal alphabet
168 {
169 if (!is_legal_alph(c))
170 {
171 throw parse_error{std::string{"Encountered an unexpected letter: "}
172 + "char_is_valid_for<"
173 + detail::type_name_as_string<seq_legal_alph_type>
174 + "> evaluated to false on " + detail::make_printable(c)};
175 }
176 return c;
177 })
178 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
180 }
181 else
182 {
183 detail::consume(stream_view | detail::take_until_or_throw(is_end)); // consume until "//"
184 }
185
186 std::ranges::advance(stream_it, 3u, std::ranges::end(stream_view)); // Skip `//` and potentially '\n'
187 }
188
190 template <typename stream_type, // constraints checked by file
191 typename seq_type, // other constraints checked inside function
192 typename id_type,
193 typename qual_type>
194 void write_sequence_record(stream_type & stream,
195 sequence_file_output_options const & options,
196 seq_type && sequence,
197 id_type && id,
198 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
199 {
200 std::ostreambuf_iterator stream_it{stream};
201 size_t sequence_size{0};
202 [[maybe_unused]] char buffer[50];
203 if constexpr (!detail::decays_to_ignore_v<seq_type>)
204 sequence_size = std::ranges::size(sequence);
205
206 // ID
207 if constexpr (detail::decays_to_ignore_v<id_type>)
208 {
209 throw std::logic_error{"The ID field may not be set to ignore when writing genbank files."};
210 }
211 else if (std::ranges::empty(id)) //[[unlikely]]
212 {
213 throw std::runtime_error{"The ID field may not be empty when writing genbank files."};
214 }
215 else if (options.embl_genbank_complete_header)
216 {
217 std::ranges::copy(id, stream_it);
218 }
219 else
220 {
221 std::ranges::copy(std::string_view{"LOCUS "}, stream_it);
222 std::ranges::copy(id, stream_it);
223 std::ranges::copy(std::string_view{" "}, stream_it);
224 auto res = std::to_chars(&buffer[0], &buffer[0] + sizeof(buffer), sequence_size);
225 std::copy(&buffer[0], res.ptr, stream_it);
226 std::ranges::copy(std::string_view{" bp\n"}, stream_it);
227 }
228
229 // Sequence
230 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
231 {
232 throw std::logic_error{"The SEQ field may not be set to ignore when writing genbank files."};
233 }
234 else if (std::ranges::empty(sequence)) //[[unlikely]]
235 {
236 throw std::runtime_error{"The SEQ field may not be empty when writing genbank files."};
237 }
238 else
239 {
240 std::ranges::copy(std::string_view{"ORIGIN\n"}, stream_it);
241 auto seq = sequence | seqan3::views::chunk(60);
242 size_t i = 0;
243 size_t bp = 1;
244
245 while (bp < sequence_size)
246 {
247 // Sequence length with more than 9 digits are not possible in one genbank entry, maximal 350 kb are
248 // allowed. See: https://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html#SequenceLengthA
249 for (size_t j = std::to_string(bp).size(); j < 9; j++)
250 stream_it = ' ';
251 std::ranges::copy(std::to_string(bp), stream_it);
252 stream_it = ' ';
254 bp += 60;
255 ++i;
256 detail::write_eol(stream_it, false);
257 }
258 std::ranges::copy(std::string_view{"//"}, stream_it);
259 detail::write_eol(stream_it, false);
260 }
261 }
262};
263
264} // 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 <charconv> header from C++17's standard library.
Provides seqan3::views::chunk.
The GenBank format.
Definition format_genbank.hpp:70
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition format_genbank.hpp:85
format_genbank() noexcept=default
Defaulted.
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_genbank.hpp:194
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_genbank.hpp:99
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.
T equal(T... args)
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 void consume(rng_t &&rng)
Iterate over a range.
Definition core/range/detail/misc.hpp:28
constexpr auto take_until
A view adaptor that returns elements from the underlying range until the functor evaluates to true (o...
Definition take_until_view.hpp:557
constexpr auto 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:571
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition istreambuf_view.hpp:104
constexpr auto 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:82
constexpr void write_eol(it_t &it, bool const add_cr)
Write "\n" or "\r\n" to the stream iterator, depending on arguments.
Definition io/detail/misc.hpp:46
@ id
The identifier, usually a string.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
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
std::string make_printable(char const c)
Returns a printable value for the given character c.
Definition pretty_print.hpp:45
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
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
constexpr auto interleave
A view that interleaves a given range into another range at regular intervals.
Definition interleave.hpp:376
The generic concept for a (biological) sequence.
Provides seqan3::views::interleave.
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::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 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.
T to_chars(T... args)
T to_string(T... args)
Provides traits to inspect some information of a type, for example its name.
Hide me