Loading...
Searching...
No Matches
format_fasta.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/adaptation/char.hpp>
#include <seqan3/alphabet/concept.hpp>
#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/alphabet/quality/aliases.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/ignore_output_iterator.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>
namespace seqan3
{
class format_fasta
{
public:
format_fasta() noexcept = default;
format_fasta(format_fasta const &) noexcept = default;
format_fasta & operator=(format_fasta const &) noexcept = default;
format_fasta(format_fasta &&) noexcept = default;
format_fasta & operator=(format_fasta &&) noexcept = default;
~format_fasta() noexcept = default;
static inline std::vector<std::string> file_extensions{
{"fasta"},
{"fa"},
{"fna"},
{"ffn"},
{"faa"},
{"frn"},
{"fas"},
};
protected:
template <typename stream_type, // constraints checked by file
typename 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<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);
// ID
read_id(stream_view, options, id);
// Sequence
read_seq(stream_view, options, sequence);
}
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()};
// 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 FASTA files."};
}
else
{
if (std::ranges::empty(id)) //[[unlikely]]
throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
write_id(stream_it, options, id);
}
// Sequence
if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
{
throw std::logic_error{
"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
}
else
{
if (std::ranges::empty(sequence)) //[[unlikely]]
throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
write_seq(stream_it, options, sequence);
}
}
private:
template <typename stream_view_t, typename seq_legal_alph_type, typename id_type>
void
read_id(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const & options, id_type & id)
{
auto const is_id = is_char<'>'> || is_char<';'>;
if (!is_id(*begin(stream_view)))
throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg
+ " evaluated to false on " + detail::make_printable(*begin(stream_view))};
if constexpr (detail::decays_to_ignore_v<id_type>) // Skip the ID, it is not requested by the user.
{
detail::consume(stream_view | detail::take_line_or_throw);
}
else // read ID
{
if (options.truncate_ids)
{
#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
auto it = stream_view.begin();
auto e = stream_view.end();
++it; // already checked `is_id`
if (options.fasta_ignore_blanks_before_id)
{
for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
{}
}
bool at_delimiter = false;
for (; it != e; ++it)
{
if ((is_cntrl || is_blank)(*it))
{
at_delimiter = true;
break;
}
id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
}
if (!at_delimiter)
throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
{}
#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
if (options.fasta_ignore_blanks_before_id)
{
std::ranges::copy(
stream_view | std::views::drop(1) // skip leading '>' or ';'
| std::views::drop_while(is_blank) // skip leading ' '
| detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id)); // … ^A is old delimiter
}
else
{
std::ranges::copy(
stream_view | std::views::drop(1) // skip leading '>' or ';'
| detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id)); // … ^A is old delimiter
}
// consume rest of line
detail::consume(stream_view | detail::take_line_or_throw);
#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
}
else // options.truncate_ids
{
#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
auto it = stream_view.begin();
auto e = stream_view.end();
++it; // skip leading '>' or ';'
if (options.fasta_ignore_blanks_before_id)
{
for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
{}
}
bool at_delimiter = false;
for (; it != e; ++it)
{
if ((is_char<'\n'>)(*it))
{
at_delimiter = true;
break;
}
id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
}
if (!at_delimiter)
throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
if (options.fasta_ignore_blanks_before_id)
{
std::ranges::copy(stream_view | detail::take_line_or_throw // read line
| std::views::drop(1) // skip leading '>' or ';'
| std::views::drop_while(is_blank) // skip leading ' '
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id));
}
else
{
std::ranges::copy(stream_view | detail::take_line_or_throw // read line
| std::views::drop(1) // skip leading '>' or ';'
| views::char_to<std::ranges::range_value_t<id_type>>,
std::back_inserter(id));
}
#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
}
}
}
template <typename stream_view_t, typename seq_legal_alph_type, typename seq_type>
void read_seq(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const &, seq_type & seq)
{
constexpr auto is_id = is_char<'>'> || is_char<';'>;
if constexpr (!detail::decays_to_ignore_v<seq_type>)
{
constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
auto it = stream_view.begin();
auto e = stream_view.end();
if (it == e)
throw unexpected_end_of_input{"No sequence information given!"};
for (; (it != e) && ((!is_id)(*it)); ++it)
{
if ((is_space || is_digit)(*it))
{
continue;
}
else if (is_legal_alph(*it))
{
seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
}
else
{
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(*it)};
}
}
#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
throw unexpected_end_of_input{"No sequence information given!"};
std::ranges::copy(
stream_view | detail::take_until(is_id) // until next header (or end)
| std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
| std::views::transform(
[is_legal_alph](char const c)
{
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;
}) // enforce legal alphabet
| views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
std::back_inserter(seq));
#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
}
else
{
detail::consume(stream_view | detail::take_until(is_id));
}
}
template <typename stream_it_t, typename id_type>
void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
{
if (options.fasta_legacy_id_marker)
stream_it = ';';
else
stream_it = '>';
if (options.fasta_blank_before_id)
stream_it = ' ';
stream_it.write_range(id);
stream_it.write_end_of_line(options.add_carriage_return);
}
template <typename stream_it_t, typename seq_type>
void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
{
auto char_sequence = seq | views::to_char;
if (options.fasta_letters_per_line > 0)
{
/* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
auto it = std::ranges::begin(char_sequence);
auto end = std::ranges::end(char_sequence);
while (it != end)
{
/* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
auto current_end = it;
size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
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, (options.fasta_letters_per_line - steps)});
stream_it.write_end_of_line(options.add_carriage_return);
}
}
else
{
stream_it.write_range(char_sequence);
stream_it.write_end_of_line(options.add_carriage_return);
}
}
};
} // namespace seqan3
algorithm
aliases.hpp
Provides aliases for qualified.
concept.hpp
Core alphabet concept and free function/type trait wrappers.
std::back_inserter
T back_inserter(T... args)
std::string
char.hpp
Provides alphabet adaptations for standard char types.
char_to.hpp
Provides seqan3::views::char_to.
seqan3::format_fasta::read_sequence_record
void read_sequence_record(stream_type &stream, sequence_file_input_options< 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_fasta.hpp:110
seqan3::format_fasta::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_fasta.hpp:92
seqan3::format_fasta::format_fasta
format_fasta() noexcept=default
Defaulted.
seqan3::format_fasta::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_fasta.hpp:136
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.
std::end
T end(T... args)
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::assign_char_to
constexpr auto assign_char_to
Assign a character to an alphabet object.
Definition alphabet/concept.hpp:517
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::field::seq
@ seq
The "sequence", usually a range of nucleotides or amino acids.
Definition record.hpp:62
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
ignore_output_iterator.hpp
Provides seqan3::detail::ignore_output_iterator for writing to null stream.
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.
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::fasta_ignore_blanks_before_id
bool fasta_ignore_blanks_before_id
Remove spaces after ">" (or ";") before the actual ID.
Definition sequence_file/input_options.hpp:30
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
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