SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
format_genbank.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <seqan3/std/charconv>
17 #include <iterator>
18 #include <seqan3/std/ranges>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 #include <range/v3/view/chunk.hpp>
24 
43 
44 namespace seqan3
45 {
46 
74 {
75 public:
79  format_genbank() noexcept = default;
80  format_genbank(format_genbank const &) noexcept = default;
81  format_genbank & operator=(format_genbank const &) noexcept = default;
82  format_genbank(format_genbank &&) noexcept = default;
83  format_genbank & operator=(format_genbank &&) noexcept = default;
84  ~format_genbank() noexcept = default;
85 
87 
89  static inline std::vector<std::string> file_extensions
90  {
91  { "genbank" },
92  { "gb" },
93  { "gbk" },
94  };
95 
96 protected:
98  template <typename stream_type, // constraints checked by file
99  typename seq_legal_alph_type, bool seq_qual_combined,
100  typename seq_type, // other constraints checked inside function
101  typename id_type,
102  typename qual_type>
103  void read_sequence_record(stream_type & stream,
105  seq_type & sequence,
106  id_type & id,
107  qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
108  {
109  auto stream_view = views::istreambuf(stream);
110  auto stream_it = std::ranges::begin(stream_view);
111 
112  if (!(std::ranges::equal(stream_view | views::take_until_or_throw(is_cntrl || is_blank), std::string{"LOCUS"})))
113  throw parse_error{"An entry has to start with the code word LOCUS."};
114 
115  //ID
116  if constexpr (!detail::decays_to_ignore_v<id_type>)
117  {
118  if (options.embl_genbank_complete_header)
119  {
120  std::ranges::copy(std::string_view{"LOCUS"}, std::cpp20::back_inserter(id));
121 
122  while (!is_char<'O'>(*std::ranges::begin(stream_view)))
123  {
124  std::ranges::copy(stream_view | views::take_line_or_throw
125  | views::char_to<std::ranges::range_value_t<id_type>>,
126  std::cpp20::back_inserter(id));
127  id.push_back('\n');
128  }
129  }
130  else
131  {
132  detail::consume(stream_view | views::take_until(!is_blank));
133 
134  auto read_id_until = [&stream_view, &id] (auto predicate)
135  {
136  std::ranges::copy(stream_view | views::take_until_or_throw(predicate)
137  | views::char_to<std::ranges::range_value_t<id_type>>,
138  std::cpp20::back_inserter(id));
139  };
140 
141  if (options.truncate_ids)
142  read_id_until(is_space);
143  else
144  read_id_until(is_cntrl);
145 
147  }
148  }
149 
150  // Jump to sequence
151  while (!(is_char<'O'>(*std::ranges::begin(stream_view)) || options.embl_genbank_complete_header))
153 
154  // Sequence
155  detail::consume(stream_view | views::take_line_or_throw); // consume "ORIGIN"
156  auto constexpr is_end = is_char<'/'> ;
157  if constexpr (!detail::decays_to_ignore_v<seq_type>)
158  {
159  auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
160  std::ranges::copy(stream_view | std::views::filter(!(is_space || is_digit))
161  | views::take_until_or_throw_and_consume(is_end) // consume "//"
162  | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
163  {
164  if (!is_legal_alph(c))
165  {
166  throw parse_error{std::string{"Encountered an unexpected letter: "} +
167  "char_is_valid_for<" +
168  detail::type_name_as_string<seq_legal_alph_type> +
169  "> evaluated to false on " +
171  }
172  return c;
173  })
174  | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
175  std::cpp20::back_inserter(sequence));
176  }
177  else
178  {
179  detail::consume(stream_view | views::take_until_or_throw_and_consume(is_end)); // consume until "//"
180  ++stream_it; // consume "/n"
181  }
182  }
183 
185  template <typename stream_type, // constraints checked by file
186  typename seq_type, // other constraints checked inside function
187  typename id_type,
188  typename qual_type>
189  void write_sequence_record(stream_type & stream,
190  sequence_file_output_options const & options,
191  seq_type && sequence,
192  id_type && id,
193  qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
194  {
195  std::cpp20::ostreambuf_iterator stream_it{stream};
196  size_t sequence_size{0};
197  [[maybe_unused]] char buffer[50];
198  if constexpr (!detail::decays_to_ignore_v<seq_type>)
199  sequence_size = ranges::size(sequence);
200 
201  // ID
202  if constexpr (detail::decays_to_ignore_v<id_type>)
203  {
204  throw std::logic_error{"The ID field may not be set to ignore when writing genbank files."};
205  }
206  else if (ranges::empty(id)) //[[unlikely]]
207  {
208  throw std::runtime_error{"The ID field may not be empty when writing genbank files."};
209  }
210  else if (options.embl_genbank_complete_header)
211  {
212  std::ranges::copy(id, stream_it);
213  }
214  else
215  {
216  std::ranges::copy(std::string_view{"LOCUS "}, stream_it);
217  std::ranges::copy(id, stream_it);
218  std::ranges::copy(std::string_view{" "}, stream_it);
219  auto res = std::to_chars(&buffer[0], &buffer[0] + sizeof(buffer), sequence_size);
220  std::copy(&buffer[0], res.ptr, stream_it);
221  std::ranges::copy(std::string_view{" bp\n"}, stream_it);
222  }
223 
224  // Sequence
225  if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
226  {
227  throw std::logic_error{"The SEQ field may not be set to ignore when writing genbank files."};
228  }
229  else if (std::ranges::empty(sequence)) //[[unlikely]]
230  {
231  throw std::runtime_error{"The SEQ field may not be empty when writing genbank files."};
232  }
233  else
234  {
235  std::ranges::copy(std::string_view{"ORIGIN\n"}, stream_it);
236  auto seq = sequence | ranges::views::chunk(60);
237  size_t i = 0;
238  size_t bp = 1;
239 
240  while (bp < sequence_size)
241  {
242  // Sequence length with more than 9 digits are not possible in one genbank entry, maximal 350 kb are
243  // allowed. See: https://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html#SequenceLengthA
244  for (size_t j = std::to_string(bp).size(); j < 9; j++)
245  stream_it = ' ';
246  std::ranges::copy(std::to_string(bp), stream_it);
247  stream_it = ' ';
248  std::ranges::copy(seq[i] | views::to_char
249  | views::interleave(10, std::string_view{" "}), stream_it);
250  bp += 60;
251  ++i;
252  detail::write_eol(stream_it,false);
253  }
254  std::ranges::copy(std::string_view{"//"}, stream_it);
255  detail::write_eol(stream_it,false);
256  }
257  }
258 };
259 
260 } // namespace seqan
Adaptations of algorithms from the Ranges TS.
Core alphabet concept and free function/type trait wrappers.
Provides seqan3::views::char_to.
The GenBank format.
Definition: format_genbank.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_genbank.hpp:90
void read_sequence_record(stream_type &stream, sequence_file_input_options< seq_legal_alph_type, seq_qual_combined > const &options, 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:103
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:189
T copy(T... args)
Provides various transformation traits used by the range module.
Provides seqan3::dna5, container aliases and string literals.
constexpr auto is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:161
constexpr auto is_digit
Checks whether c is a digital character.
Definition: predicate.hpp:285
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:81
constexpr auto is_space
Checks whether c is a space character.
Definition: predicate.hpp:144
constexpr auto is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:108
constexpr void write_eol(it_t &it, bool const add_cr)
Write "\n" or "\r\n" to the stream iterator, depending on arguments.
Definition: misc.hpp:48
@ id
The identifier, usually a string.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
constexpr void consume(rng_t &&rng)
Iterate over a range (consumes single-pass input ranges).
Definition: misc.hpp:28
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:434
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:150
constexpr auto chunk
A chunk view.
Definition: chunk.hpp:29
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:65
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.hpp:614
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition: istreambuf.hpp:114
constexpr auto take_until
A view adaptor that returns elements from the underlying range until the functor evaluates to true (o...
Definition: take_until.hpp:600
auto const char_to
A view over an alphabet, given a range of characters.
Definition: char_to.hpp:70
constexpr auto take_until_or_throw_and_consume
A view adaptor that returns elements from the underlying range until the functor evaluates to true (t...
Definition: take_until.hpp:642
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.hpp:90
constexpr auto interleave
A view that interleaves a given range into another range at regular intervals.
Definition: interleave.hpp:384
The generic concept for a (biological) sequence.
Provides seqan3::views::interleave.
Provides various utility functions.
Provides seqan3::views::istreambuf.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides various utility functions.
Adaptations of concepts from the Ranges TS.
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.
Provides std::from_chars and std::to_chars if not defined in the stdlib <charconv> header.
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:30
bool embl_genbank_complete_header
Read the complete_header into the seqan3::field::id for embl or genbank format.
Definition: input_options.hpp:34
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition: input_options.hpp:32
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:22
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition: output_options.hpp:42
Provides seqan3::views::take.
Provides seqan3::views::take_line and seqan3::views::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.
Provides character predicates for tokenisation.