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// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, 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 <algorithm>
16#include <iterator>
17#include <ranges>
18#include <seqan3/std/charconv>
19#include <string>
20#include <string_view>
21#include <vector>
22
41
42namespace seqan3
43{
44
73{
74public:
78 format_genbank() noexcept = default;
79 format_genbank(format_genbank const &) noexcept = default;
80 format_genbank & operator=(format_genbank const &) noexcept = default;
81 format_genbank(format_genbank &&) noexcept = default;
82 format_genbank & operator=(format_genbank &&) noexcept = default;
83 ~format_genbank() noexcept = default;
84
86
88 static inline std::vector<std::string> file_extensions{
89 {"genbank"},
90 {"gb"},
91 {"gbk"},
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 // Store current position in buffer
110 // Must happen before constructing the view.
111 // With libc++, tellg invalidates the I/O buffer.
112 position_buffer = stream.tellg();
113
114 auto stream_view = detail::istreambuf(stream);
115 auto stream_it = std::ranges::begin(stream_view);
116
117 if (!(std::ranges::equal(stream_view | detail::take_until_or_throw(is_cntrl || is_blank),
118 std::string{"LOCUS"})))
119 throw parse_error{"An entry has to start with the code word LOCUS."};
120
121 //ID
122 if constexpr (!detail::decays_to_ignore_v<id_type>)
123 {
125 {
126 std::ranges::copy(std::string_view{"LOCUS"}, std::back_inserter(id));
127
128 while (!is_char<'O'>(*std::ranges::begin(stream_view)))
129 {
130 std::ranges::copy(stream_view | detail::take_line_or_throw
131 | views::char_to<std::ranges::range_value_t<id_type>>,
133 id.push_back('\n');
134 }
135 }
136 else
137 {
138 detail::consume(stream_view | detail::take_until(!is_blank));
139
140 auto read_id_until = [&stream_view, &id](auto predicate)
141 {
142 std::ranges::copy(stream_view | detail::take_until_or_throw(predicate)
143 | views::char_to<std::ranges::range_value_t<id_type>>,
145 };
146
147 if (options.truncate_ids)
148 read_id_until(is_space);
149 else
150 read_id_until(is_cntrl);
151
152 detail::consume(stream_view | detail::take_line_or_throw);
153 }
154 }
155
156 // Jump to sequence
157 while (!(is_char<'O'>(*std::ranges::begin(stream_view)) || options.embl_genbank_complete_header))
158 detail::consume(stream_view | detail::take_line_or_throw);
159
160 // Sequence
161 detail::consume(stream_view | detail::take_line_or_throw); // consume "ORIGIN"
162 constexpr auto is_end = is_char<'/'>;
163 if constexpr (!detail::decays_to_ignore_v<seq_type>)
164 {
165 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
166 std::ranges::copy(
167 stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
168 | detail::take_until_or_throw(is_end) // until //
169 | std::views::transform(
170 [is_legal_alph](char const c) // enforce legal alphabet
171 {
172 if (!is_legal_alph(c))
173 {
174 throw parse_error{std::string{"Encountered an unexpected letter: "}
175 + "char_is_valid_for<"
176 + detail::type_name_as_string<seq_legal_alph_type>
177 + "> evaluated to false on " + detail::make_printable(c)};
178 }
179 return c;
180 })
181 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
183 }
184 else
185 {
186 detail::consume(stream_view | detail::take_until_or_throw(is_end)); // consume until "//"
187 }
188
189 std::ranges::advance(stream_it, 3u, std::ranges::end(stream_view)); // Skip `//` and potentially '\n'
190 }
191
193 template <typename stream_type, // constraints checked by file
194 typename seq_type, // other constraints checked inside function
195 typename id_type,
196 typename qual_type>
197 void write_sequence_record(stream_type & stream,
198 sequence_file_output_options const & options,
199 seq_type && sequence,
200 id_type && id,
201 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
202 {
203 std::ostreambuf_iterator stream_it{stream};
204 size_t sequence_size{0};
205 [[maybe_unused]] char buffer[50];
206 if constexpr (!detail::decays_to_ignore_v<seq_type>)
207 sequence_size = std::ranges::size(sequence);
208
209 // ID
210 if constexpr (detail::decays_to_ignore_v<id_type>)
211 {
212 throw std::logic_error{"The ID field may not be set to ignore when writing genbank files."};
213 }
214 else if (std::ranges::empty(id)) //[[unlikely]]
215 {
216 throw std::runtime_error{"The ID field may not be empty when writing genbank files."};
217 }
218 else if (options.embl_genbank_complete_header)
219 {
220 std::ranges::copy(id, stream_it);
221 }
222 else
223 {
224 std::ranges::copy(std::string_view{"LOCUS "}, stream_it);
225 std::ranges::copy(id, stream_it);
226 std::ranges::copy(std::string_view{" "}, stream_it);
227 auto res = std::to_chars(&buffer[0], &buffer[0] + sizeof(buffer), sequence_size);
228 std::copy(&buffer[0], res.ptr, stream_it);
229 std::ranges::copy(std::string_view{" bp\n"}, stream_it);
230 }
231
232 // Sequence
233 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
234 {
235 throw std::logic_error{"The SEQ field may not be set to ignore when writing genbank files."};
236 }
237 else if (std::ranges::empty(sequence)) //[[unlikely]]
238 {
239 throw std::runtime_error{"The SEQ field may not be empty when writing genbank files."};
240 }
241 else
242 {
243 std::ranges::copy(std::string_view{"ORIGIN\n"}, stream_it);
244 auto seq = sequence | seqan3::views::chunk(60);
245 size_t i = 0;
246 size_t bp = 1;
247
248 while (bp < sequence_size)
249 {
250 // Sequence length with more than 9 digits are not possible in one genbank entry, maximal 350 kb are
251 // allowed. See: https://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html#SequenceLengthA
252 for (size_t j = std::to_string(bp).size(); j < 9; j++)
253 stream_it = ' ';
254 std::ranges::copy(std::to_string(bp), stream_it);
255 stream_it = ' ';
256 std::ranges::copy(seq[i] | views::to_char | views::interleave(10, std::string_view{" "}), stream_it);
257 bp += 60;
258 ++i;
259 detail::write_eol(stream_it, false);
260 }
261 std::ranges::copy(std::string_view{"//"}, stream_it);
262 detail::write_eol(stream_it, false);
263 }
264 }
265};
266
267} // namespace seqan3
Core alphabet concept and free function/type trait wrappers.
T back_inserter(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:73
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:88
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:197
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:102
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.
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
@ 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:142
constexpr auto is_digit
Checks whether c is a digital character.
Definition predicate.hpp:262
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition predicate.hpp:63
constexpr auto is_space
Checks whether c is a space character.
Definition predicate.hpp:125
constexpr auto is_cntrl
Checks whether c is a control character.
Definition predicate.hpp:90
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:26
constexpr auto interleave
A view that interleaves a given range into another range at regular intervals.
Definition interleave.hpp:379
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:29
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:48
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/input_options.hpp:27
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:31
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition sequence_file/input_options.hpp:29
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/output_options.hpp:26
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition sequence_file/output_options.hpp:45
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_string(T... args)
Provides traits to inspect some information of a type, for example its name.