Loading...
Searching...
No Matches
format_embl.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <algorithm>
#include <iterator>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
#include <seqan3/alphabet/concept.hpp>
#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/alphabet/views/char_to.hpp>
#include <seqan3/alphabet/views/to_char.hpp>
#include <seqan3/core/range/detail/misc.hpp>
#include <seqan3/core/range/type_traits.hpp>
#include <seqan3/io/detail/misc.hpp>
#include <seqan3/io/sequence_file/input_format_concept.hpp>
#include <seqan3/io/sequence_file/input_options.hpp>
#include <seqan3/io/sequence_file/output_format_concept.hpp>
#include <seqan3/io/sequence_file/output_options.hpp>
#include <seqan3/io/stream/detail/fast_ostreambuf_iterator.hpp>
#include <seqan3/io/views/detail/istreambuf_view.hpp>
#include <seqan3/io/views/detail/take_line_view.hpp>
#include <seqan3/io/views/detail/take_until_view.hpp>
#include <seqan3/utility/char_operations/predicate.hpp>
#include <seqan3/utility/detail/type_name_as_string.hpp>
#include <seqan3/utility/views/repeat_n.hpp>
namespace seqan3
{
class format_embl
{
public:
format_embl() noexcept = default;
format_embl(format_embl const &) noexcept = default;
format_embl & operator=(format_embl const &) noexcept = default;
format_embl(format_embl &&) noexcept = default;
format_embl & operator=(format_embl &&) noexcept = default;
~format_embl() noexcept = default;
static inline std::vector<std::string> file_extensions{
{"embl"},
};
protected:
template <typename stream_type, // constraints checked by file
typename seq_legal_alph_type,
typename stream_pos_type,
typename seq_type, // other constraints checked inside function
typename id_type,
typename qual_type>
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 & SEQAN3_DOXYGEN_ONLY(qualities))
{
// Store current position in buffer
// Must happen before constructing the view.
// With libc++, tellg invalidates the I/O buffer.
position_buffer = stream.tellg();
auto stream_view = detail::istreambuf(stream);
auto stream_it = std::ranges::begin(stream_view);
std::string idbuffer;
std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank),
std::back_inserter(idbuffer));
if (idbuffer != "ID")
throw parse_error{"An entry has to start with the code word ID."};
if constexpr (!detail::decays_to_ignore_v<id_type>)
{
if (options.embl_genbank_complete_header)
{
std::ranges::copy(idbuffer | views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id));
do
{
std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<'S'>)
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id));
id.push_back(*stream_it);
++stream_it;
}
while (*stream_it != 'Q');
id.pop_back(); // remove 'S' from id
idbuffer = "SQ";
}
else
{
// ID
detail::consume(stream_view | detail::take_until(!is_blank));
// read id
if (options.truncate_ids)
{
std::ranges::copy(stream_view | detail::take_until_or_throw(is_blank || is_char<';'> || is_cntrl)
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id));
}
else
{
std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<';'>)
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id));
}
}
}
// Jump to sequence
if (idbuffer != "SQ")
{
do
{
detail::consume(stream_view | detail::take_until_or_throw(is_char<'S'>));
++stream_it;
}
while (*stream_it != 'Q');
}
detail::consume(stream_view | detail::take_line_or_throw); //Consume line with infos to sequence
// Sequence
constexpr auto is_end = is_char<'/'>;
if constexpr (!detail::decays_to_ignore_v<seq_type>)
{
auto seq_view = stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
| detail::take_until_or_throw(is_end); // until //
constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
std::ranges::copy(
seq_view
| std::views::transform(
[is_legal_alph](char const c) // enforce legal alphabet
{
if (!is_legal_alph(c))
{
throw parse_error{std::string{"Encountered an unexpected letter: "}
+ "char_is_valid_for<"
+ detail::type_name_as_string<seq_legal_alph_type>
+ "> evaluated to false on " + detail::make_printable(c)};
}
return c;
})
| views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
std::back_inserter(sequence));
}
else
{
detail::consume(stream_view | detail::take_until_or_throw(is_end)); // consume until "//"
}
std::ranges::advance(stream_it, 3u, std::ranges::end(stream_view)); // Skip `//` and potentially '\n'
}
template <typename stream_type, // constraints checked by file
typename seq_type, // other constraints checked inside function
typename id_type,
typename qual_type>
void write_sequence_record(stream_type & stream,
sequence_file_output_options const & options,
seq_type && sequence,
id_type && id,
qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
{
seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
[[maybe_unused]] size_t sequence_size = 0;
if constexpr (!detail::decays_to_ignore_v<seq_type>)
sequence_size = std::ranges::distance(sequence);
// ID
if constexpr (detail::decays_to_ignore_v<id_type>)
{
throw std::logic_error{"The ID field may not be set to ignore when writing embl files."};
}
else
{
if (std::ranges::empty(id)) //[[unlikely]]
throw std::runtime_error{"The ID field may not be empty when writing embl files."};
if (options.embl_genbank_complete_header)
{
stream_it.write_range(id);
}
else
{
stream_it.write_range(std::string_view{"ID "});
stream_it.write_range(id);
stream_it.write_range(std::string_view{"; "});
stream_it.write_number(sequence_size);
stream_it.write_range(std::string_view{" BP.\n"});
}
}
// Sequence
if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
{
throw std::logic_error{"The SEQ field may not be set to ignore when writing embl files."};
}
else
{
if (std::ranges::empty(sequence)) //[[unlikely]]
throw std::runtime_error{"The SEQ field may not be empty when writing embl files."};
// write beginning of sequence record
stream_it.write_range(std::string_view{"SQ Sequence "});
stream_it.write_number(sequence_size);
stream_it.write_range(std::string_view{" BP;\n"});
// write sequence in chunks of 60 bp's with a space after 10 bp's
auto char_sequence = sequence | views::to_char;
auto it = std::ranges::begin(char_sequence);
size_t written_chars{0};
uint8_t chunk_size{10u};
while (it != std::ranges::end(char_sequence))
{
auto current_end = it;
size_t steps = std::ranges::advance(current_end, chunk_size, std::ranges::end(char_sequence));
using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
it = stream_it.write_range(subrange_t{it, current_end, chunk_size - steps});
stream_it = ' ';
written_chars += chunk_size;
if (written_chars % 60 == 0)
{
stream_it.write_number(written_chars);
stream_it.write_end_of_line(options.add_carriage_return);
}
}
// fill last line
auto characters_in_last_line = sequence_size % 60;
auto number_of_padding_needed = 65 - characters_in_last_line - characters_in_last_line / chunk_size;
stream_it.write_range(views::repeat_n(' ', number_of_padding_needed));
stream_it.write_number(sequence_size);
stream_it.write_end_of_line(options.add_carriage_return);
// write end-of-record-symbol
stream_it.write_range(std::string_view{"//"});
stream_it.write_end_of_line(options.add_carriage_return);
}
}
};
} // namespace seqan3
algorithm
concept.hpp
Core alphabet concept and free function/type trait wrappers.
std::back_inserter
T back_inserter(T... args)
std::string
std::string_view
char_to.hpp
Provides seqan3::views::char_to.
seqan3::format_embl::file_extensions
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
seqan3::format_embl::write_sequence_record
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
seqan3::format_embl::read_sequence_record
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
seqan3::format_embl::format_embl
format_embl() noexcept=default
Defaulted.
misc.hpp
Provides various utility functions.
type_traits.hpp
Provides various transformation traits used by the range module.
dna5.hpp
Provides seqan3::dna5, container aliases and string literals.
fast_ostreambuf_iterator.hpp
Provides seqan3::detail::fast_ostreambuf_iterator.
seqan3::views::to_char
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition to_char.hpp:60
seqan3::views::char_to
auto const char_to
A view over an alphabet, given a range of characters.
Definition char_to.hpp:64
seqan3::char_is_valid_for
constexpr auto char_is_valid_for
Returns whether a character is in the valid set of a seqan3::alphabet (usually implies a bijective ma...
Definition alphabet/concept.hpp:661
seqan3::is_blank
constexpr auto is_blank
Checks whether c is a blank character.
Definition predicate.hpp:139
seqan3::is_digit
constexpr auto is_digit
Checks whether c is a digital character.
Definition predicate.hpp:259
seqan3::is_char
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition predicate.hpp:60
seqan3::is_space
constexpr auto is_space
Checks whether c is a space character.
Definition predicate.hpp:122
seqan3::is_cntrl
constexpr auto is_cntrl
Checks whether c is a control character.
Definition predicate.hpp:87
seqan3::views::repeat_n
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition repeat_n.hpp:88
sequence
The generic concept for a (biological) sequence.
misc.hpp
Provides various utility functions.
istreambuf_view.hpp
Provides seqan3::detail::istreambuf.
iterator
std::logic_error
seqan3
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
std
SeqAn specific customisations in the standard namespace.
predicate.hpp
Provides character predicates for tokenisation.
repeat_n.hpp
Provides seqan3::views::repeat_n.
std::runtime_error
input_format_concept.hpp
Provides seqan3::sequence_file_input_format and auxiliary classes.
input_options.hpp
Provides seqan3::sequence_file_input_options.
output_format_concept.hpp
Provides seqan3::sequence_file_output_format and auxiliary classes.
output_options.hpp
Provides seqan3::sequence_file_output_options.
string
string_view
seqan3::parse_error
Thrown if there is a parse error, such as reading an unexpected character from an input stream.
Definition io/exception.hpp:45
seqan3::sequence_file_input_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/input_options.hpp:24
seqan3::sequence_file_input_options::embl_genbank_complete_header
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
seqan3::sequence_file_input_options::truncate_ids
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition sequence_file/input_options.hpp:26
seqan3::sequence_file_output_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/output_options.hpp:23
seqan3::sequence_file_output_options::add_carriage_return
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
seqan3::sequence_file_output_options::embl_genbank_complete_header
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition sequence_file/output_options.hpp:42
take_line_view.hpp
Provides seqan3::detail::take_line and seqan3::detail::take_line_or_throw.
take_until_view.hpp
Provides seqan3::views::take_until and seqan3::views::take_until_or_throw.
to_char.hpp
Provides seqan3::views::to_char.
type_name_as_string.hpp
Provides traits to inspect some information of a type, for example its name.
vector
Hide me