SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
structure_file/input.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 <cassert>
13#include <filesystem>
14#include <fstream>
15#include <limits>
16#include <optional>
17#include <string>
18#include <type_traits>
19#include <utility>
20#include <variant>
21#include <vector>
22
31#include <seqan3/io/detail/record.hpp>
40
41namespace seqan3
42{
43// ----------------------------------------------------------------------------
44// structure_file_input_traits
45// ----------------------------------------------------------------------------
46
138template <typename t>
140 requires (t v) {
141 // TODO(joergi-w) The expensive concept checks are currently omitted. Check again when compiler has improved.
142 // sequence
147
148 // id
151
152 // bpp
153 requires std::is_floating_point_v<typename t::bpp_prob>;
155
156 // requires container // TODO check Associative container Concept when implemented
157 // <typename t::template bpp_queue
158 // <typename t::template bpp_item
159 // <typename t::bpp_prob, typename t::bpp_partner>>>
160 // && requires(typename t::template bpp_queue // TODO maybe implement also a version that allows emplace_back
161 // <typename t::template bpp_item
162 // <typename t::bpp_prob, typename t::bpp_partner>> value) { value.emplace(1.0, 1); };
163 // requires sequence_container
164 // <typename t::template bpp_container
165 // <typename t::template bpp_queue
166 // <typename t::template bpp_item
167 // <typename t::bpp_prob, typename t::bpp_partner>>>>;
168
169 // structure
170 requires std::is_same_v<typename t::structure_alphabet, dssp9> // TODO(joergi-w) add aa_structure_concept
173
174 // structured sequence: tuple composites of seq and structure
175 requires std::is_base_of_v<
176 alphabet_tuple_base<
177 typename t::template structured_seq_alphabet<typename t::seq_alphabet, typename t::structure_alphabet>,
178 typename t::seq_alphabet,
179 typename t::structure_alphabet>,
180 typename t::template structured_seq_alphabet<typename t::seq_alphabet, typename t::structure_alphabet>>;
181 // requires sequence_container
182 // <typename t::template structured_seq_container
183 // <typename t::template structured_seq_alphabet
184 // <typename t::seq_alphabet, typename t::structure_alphabet>>>;
185
186 // energy: std::optional of floating point number
187 requires std::is_floating_point_v<typename t::energy_type::value_type>;
188
189 // reactivity [error]
190 requires std::is_floating_point_v<typename t::react_type>;
192
193 // comment
196
197 // offset
199 };
201
202// ----------------------------------------------------------------------------
203// structure_file_input_default_traits
204// ----------------------------------------------------------------------------
205
220{
226 // sequence
227
230
233
235 template <typename _seq_alphabet>
237
238 // id
239
241 using id_alphabet = char;
242
244 template <typename _id_alphabet>
246
247 // base pair probability structure
248
250 using bpp_prob = double;
251
253 using bpp_partner = size_t;
254
256 template <typename _bpp_prob, typename _bpp_partner>
258
260 template <typename _bpp_item>
262
264 template <typename _bpp_queue>
266
267 // fixed structure
268
270 using structure_alphabet = wuss51;
271
273 template <typename _structure_alphabet>
275
276 // combined sequence and structure
277
279 template <typename _seq_alphabet, typename _structure_alphabet>
281
283 template <typename _structured_seq_alphabet>
285
286 // energy
287
290
291 // reactivity [error]
292
294 using react_type = double;
295
297 template <typename _react_type>
299
300 // comment
301
303 using comment_alphabet = char;
304
306 template <typename _comment_alphabet>
308
309 // offset
310
312 using offset_type = size_t;
314};
315
336
337// ----------------------------------------------------------------------------
338// structure_file_input
339// ----------------------------------------------------------------------------
340
356 detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::structure>,
357 detail::type_list_of_structure_file_input_formats valid_formats_ = type_list<format_vienna>>
359{
360public:
366 using traits_type = traits_type_;
368 using selected_field_ids = selected_field_ids_;
370 using valid_formats = valid_formats_;
372 using stream_char_type = char;
374
379 field::id,
388
389 static_assert(
390 []() constexpr
391 {
392 for (field f : selected_field_ids::as_array)
393 if (!field_ids::contains(f))
394 return false;
395 return true;
396 }(),
397 "You selected a field that is not valid for structure files, please refer to the documentation "
398 "of structure_file_input::field_ids for the accepted values.");
399
400 static_assert(
401 []() constexpr
402 {
403 return !(selected_field_ids::contains(field::structured_seq)
404 && (selected_field_ids::contains(field::seq) || (selected_field_ids::contains(field::structure))));
405 }(),
406 "You may not select field::structured_seq and either of field::seq and field::structure "
407 "at the same time.");
408
415 using seq_type = typename traits_type::template seq_container<typename traits_type::seq_alphabet>;
417 using id_type = typename traits_type::template id_container<typename traits_type::id_alphabet>;
419 using bpp_type = typename traits_type::template bpp_container<typename traits_type::template bpp_queue<
420 typename traits_type::template bpp_item<typename traits_type::bpp_prob, typename traits_type::bpp_partner>>>;
422 using structure_type = typename traits_type::template structure_container<typename traits_type::structure_alphabet>;
424 using structured_seq_type = typename traits_type::template structured_seq_container<
425 typename traits_type::template structured_seq_alphabet<typename traits_type::seq_alphabet,
426 typename traits_type::structure_alphabet>>;
428 using energy_type = typename traits_type::energy_type;
430 using react_type = typename traits_type::template react_container<typename traits_type::react_type>;
432 using comment_type = typename traits_type::template comment_container<typename traits_type::comment_alphabet>;
434 using offset_type = typename traits_type::offset_type;
435
438 id_type,
439 bpp_type,
447
452
462 using const_reference = void;
464 using size_type = size_t;
468 using iterator = detail::in_file_iterator<structure_file_input>;
470 using const_iterator = void;
472 using sentinel = std::default_sentinel_t;
474
490
508 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
509 primary_stream{new std::ifstream{}, stream_deleter_default}
510 {
511 primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
512 static_cast<std::basic_ifstream<char> *>(primary_stream.get())
513 ->open(filename, std::ios_base::in | std::ios::binary);
514
515 if (!primary_stream->good())
516 throw file_open_error{"Could not open file " + filename.string() + " for reading."};
517
518 // possibly add intermediate decompression stream
519 secondary_stream = detail::make_secondary_istream(*primary_stream, filename);
520
521 // initialise format handler
522 detail::set_format(format, filename);
523 }
524
540 template <input_stream stream_t, structure_file_input_format file_format>
541 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, char>
542 structure_file_input(stream_t & stream,
543 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
544 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
545 primary_stream{&stream, stream_deleter_noop},
546 format{detail::structure_file_input_format_exposer<file_format>{}}
547 {
548 static_assert(list_traits::contains<file_format, valid_formats>,
549 "You selected a format that is not in the valid_formats of this file.");
550
551 // possibly add intermediate decompression stream
552 secondary_stream = detail::make_secondary_istream(*primary_stream);
553 }
554
556 template <input_stream stream_t, structure_file_input_format file_format>
557 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, char>
558 structure_file_input(stream_t && stream,
559 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
560 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
561 primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
562 format{detail::structure_file_input_format_exposer<file_format>{}}
563 {
564 static_assert(list_traits::contains<file_format, valid_formats>,
565 "You selected a format that is not in the valid_formats of this file.");
566
567 // possibly add intermediate compression stream
568 secondary_stream = detail::make_secondary_istream(*primary_stream);
569 }
571
591 {
592 // buffer first record
593 if (!first_record_was_read)
594 {
595 read_next_record();
596 first_record_was_read = true;
597 }
598
599 return {*this};
600 }
601
615 sentinel end() noexcept
616 {
617 return {};
618 }
619
643 reference front() noexcept
644 {
645 return *begin();
646 }
648
650 structure_file_input_options<typename traits_type::seq_legal_alphabet,
651 selected_field_ids::contains(field::structured_seq)>
653
654protected:
656
660 record_type record_buffer;
662 std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
664 std::streampos position_buffer{};
666
674 static void stream_deleter_noop(std::basic_istream<stream_char_type> *)
675 {}
677 static void stream_deleter_default(std::basic_istream<stream_char_type> * ptr)
678 {
679 delete ptr;
680 }
681
683 stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
685 stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
686
688 bool first_record_was_read{false};
690 bool at_end{false};
691
693 using format_type =
694 typename detail::variant_from_tags<valid_formats, detail::structure_file_input_format_exposer>::type;
696 format_type format;
698
700 void read_next_record()
701 {
702 // clear the record
703 record_buffer.clear();
704
705 // store the current position in the position buffer
706 position_buffer = secondary_stream->tellg();
707
708 // at end if we could not read further
709 if ((std::istreambuf_iterator<stream_char_type>{*secondary_stream}
711 {
712 at_end = true;
713 return;
714 }
715
716 assert(!format.valueless_by_exception());
718 [&](auto & f)
719 {
720 // read new record
721 if constexpr (selected_field_ids::contains(field::structured_seq))
722 {
723 static_assert(!selected_field_ids::contains(field::structure),
724 "You may not select field::structured_seq and field::structure at the same time.");
725 static_assert(!selected_field_ids::contains(field::seq),
726 "You may not select field::structured_seq and field::seq at the same time.");
727 f.read_structure_record(*secondary_stream,
728 options,
729 detail::get_or_ignore<field::structured_seq>(record_buffer), // seq
730 detail::get_or_ignore<field::id>(record_buffer),
731 detail::get_or_ignore<field::bpp>(record_buffer),
732 detail::get_or_ignore<field::structured_seq>(record_buffer), // structure
733 detail::get_or_ignore<field::energy>(record_buffer),
734 detail::get_or_ignore<field::react>(record_buffer),
735 detail::get_or_ignore<field::react_err>(record_buffer),
736 detail::get_or_ignore<field::comment>(record_buffer),
737 detail::get_or_ignore<field::offset>(record_buffer));
738 }
739 else
740 {
741 f.read_structure_record(*secondary_stream,
742 options,
743 detail::get_or_ignore<field::seq>(record_buffer),
744 detail::get_or_ignore<field::id>(record_buffer),
745 detail::get_or_ignore<field::bpp>(record_buffer),
746 detail::get_or_ignore<field::structure>(record_buffer),
747 detail::get_or_ignore<field::energy>(record_buffer),
748 detail::get_or_ignore<field::react>(record_buffer),
749 detail::get_or_ignore<field::react_err>(record_buffer),
750 detail::get_or_ignore<field::comment>(record_buffer),
751 detail::get_or_ignore<field::offset>(record_buffer));
752 }
753 },
754 format);
755 }
756
758 friend iterator;
759};
760
767template <input_stream stream_type,
768 structure_file_input_format file_format,
769 detail::fields_specialisation selected_field_ids>
770structure_file_input(stream_type && stream, file_format const &, selected_field_ids const &)
774
776template <input_stream stream_type,
777 structure_file_input_format file_format,
778 detail::fields_specialisation selected_field_ids>
779structure_file_input(stream_type & stream, file_format const &, selected_field_ids const &)
784
785} // namespace seqan3
Provides seqan3::aa27, container aliases and string literals.
Provides alphabet adaptations for standard char types.
The twenty-seven letter amino acid alphabet.
Definition aa27.hpp:43
The protein structure alphabet of the characters "HGIEBTSCX".
Definition dssp9.hpp:59
The 15 letter RNA alphabet, containing all IUPAC smybols minus the gap.
Definition rna15.hpp:48
The five letter RNA alphabet of A,C,G,U and the unknown character N.
Definition rna5.hpp:46
The generic concept for structure file in formats.
Definition structure_file/input_format_concept.hpp:139
A class for reading structured sequence files, e.g. Stockholm, Connect, Vienna, ViennaRNA bpp matrix ...
Definition structure_file/input.hpp:359
structure_file_input_options< typename traits_type::seq_legal_alphabet, selected_field_ids::contains(field::structured_seq)> options
The options are public and its members can be set directly.
Definition structure_file/input.hpp:652
reference front() noexcept
Return the record we are currently at in the file.
Definition structure_file/input.hpp:643
structure_file_input(structure_file_input const &)=delete
Copy construction is explicitly deleted, because you cannot have multiple access to the same file.
detail::in_file_iterator< structure_file_input > iterator
The iterator type of this view (an input iterator).
Definition structure_file/input.hpp:468
typename traits_type::template id_container< typename traits_type::id_alphabet > id_type
The type of the ID field (default std::string).
Definition structure_file/input.hpp:417
size_t size_type
An unsigned integer type, usually std::size_t.
Definition structure_file/input.hpp:464
typename traits_type::template seq_container< typename traits_type::seq_alphabet > seq_type
The type of the sequence field (default std::vector of seqan3::rna5).
Definition structure_file/input.hpp:415
typename traits_type::template comment_container< typename traits_type::comment_alphabet > comment_type
The type of the comment field (default double).
Definition structure_file/input.hpp:432
std::default_sentinel_t sentinel
The type returned by end().
Definition structure_file/input.hpp:472
structure_file_input & operator=(structure_file_input const &)=delete
Copy assignment is explicitly deleted, because you cannot have multiple access to the same file.
structure_file_input(stream_t &stream, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from an existing stream and with specified format.
Definition structure_file/input.hpp:542
structure_record< detail::select_types_with_ids_t< field_types, field_ids, selected_field_ids >, selected_field_ids > record_type
The type of the record, a specialisation of seqan3::record; acts as a tuple of the selected field typ...
Definition structure_file/input.hpp:450
structure_file_input(stream_type &&stream, file_format const &, selected_field_ids const &) -> structure_file_input< typename structure_file_input<>::traits_type, selected_field_ids, type_list< file_format > >
Deduction of the selected fields, the file format and the stream type.
structure_file_input(stream_t &&stream, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition structure_file/input.hpp:558
char stream_char_type
Character type of the stream(s).
Definition structure_file/input.hpp:372
void const_reference
The const_reference type is void, because files are not const-iterable.
Definition structure_file/input.hpp:462
structure_file_input()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
traits_type_ traits_type
A traits type that defines aliases and template for storage of the fields.
Definition structure_file/input.hpp:366
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition structure_file/input.hpp:470
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition structure_file/input.hpp:368
structure_file_input(structure_file_input &&)=default
Move construction is defaulted.
structure_file_input(stream_type &stream, file_format const &, selected_field_ids const &) -> structure_file_input< typename structure_file_input<>::traits_type, selected_field_ids, type_list< file_format > >
This is an overloaded member function, provided for convenience. It differs from the above function o...
typename traits_type::template structure_container< typename traits_type::structure_alphabet > structure_type
The type of the structure field (default std::vector of seqan3::wuss51).
Definition structure_file/input.hpp:422
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition structure_file/input.hpp:615
structure_file_input & operator=(structure_file_input &&)=default
Move assignment is defaulted.
iterator begin()
Returns an iterator to current position in the file.
Definition structure_file/input.hpp:590
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition structure_file/input.hpp:370
typename traits_type::energy_type energy_type
The type of the energy field (default double).
Definition structure_file/input.hpp:428
typename traits_type::offset_type offset_type
The type of the offset field (default size_t).
Definition structure_file/input.hpp:434
typename traits_type::template structured_seq_container< typename traits_type::template structured_seq_alphabet< typename traits_type::seq_alphabet, typename traits_type::structure_alphabet > > structured_seq_type
The type of the sequence-structure field (default std::vector of structured_rna<rna5,...
Definition structure_file/input.hpp:426
typename traits_type::template bpp_container< typename traits_type::template bpp_queue< typename traits_type::template bpp_item< typename traits_type::bpp_prob, typename traits_type::bpp_partner > > > bpp_type
The type of the base pair probabilies (default std::vector of std::set<std::pair<double,...
Definition structure_file/input.hpp:420
structure_file_input(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition structure_file/input.hpp:507
~structure_file_input()=default
Destructor is defaulted.
typename traits_type::template react_container< typename traits_type::react_type > react_type
The type of the reactivity and reactivity error fields (default double).
Definition structure_file/input.hpp:430
A seqan3::alphabet_tuple_base that joins an aminoacid alphabet with a protein structure alphabet.
Definition structured_aa.hpp:52
A seqan3::alphabet_tuple_base that joins a nucleotide alphabet with an RNA structure alphabet.
Definition structured_rna.hpp:53
T data(T... args)
Provides the dssp format for protein structure.
Provides the seqan3::format_vienna.
T format(T... args)
T get(T... args)
field
An enumerator for the fields used in file formats.
Definition record.hpp:60
@ energy
Energy of a folded sequence, represented by one float number.
@ comment
Comment field of arbitrary content, usually a string.
@ structure
Fixed interactions, usually a string of structure alphabet characters.
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
@ react
Reactivity values of the sequence characters given in a vector of float numbers.
@ react_err
Reactivity error values given in a vector corresponding to seqan3::field::react.
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
@ structured_seq
Sequence and fixed interactions combined in one range.
@ id
The identifier, usually a string.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
Provides the seqan3::detail::in_file_iterator class template.
Checks whether from can be explicitly converted to to.
A concept that indicates whether an alphabet represents RNA structure.
A more refined container concept than seqan3::container.
The requirements a traits_type for seqan3::structure_file_input must meet.
Refines seqan3::alphabet and adds assignability.
Provides exceptions used in the I/O module.
Stream concepts.
T is_base_of_v
Provides various utility functions required only for input.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
Provides seqan3::rna15, container aliases and string literals.
Provides seqan3::rna5, container aliases and string literals.
T size(T... args)
A class template that holds a choice of seqan3::field.
Definition record.hpp:125
Thrown if there is an unspecified filesystem or stream error while opening, e.g. permission problem.
Definition io/exception.hpp:36
void clear() noexcept(noexcept(std::apply(expander, std::declval< record & >().as_base())))
Clears containers that provide .clear() and (re-)initialises all other elements with = {}.
Definition record.hpp:242
A traits type that specifies input as amino acids.
Definition structure_file/input.hpp:319
The default traits for seqan3::structure_file_input.
Definition structure_file/input.hpp:220
wuss51 structure_alphabet
The alphabet for a structure annotation is seqan3::phred42.
Definition structure_file/input.hpp:270
size_t offset_type
The type of the offset is size_t.
Definition structure_file/input.hpp:312
double bpp_prob
The type for a base pair probability is double.
Definition structure_file/input.hpp:250
char id_alphabet
The alphabet for an identifier string is char.
Definition structure_file/input.hpp:241
char comment_alphabet
The alphabet for a comment string is char.
Definition structure_file/input.hpp:303
double react_type
The type of the reactivity and reactivity error is double.
Definition structure_file/input.hpp:294
size_t bpp_partner
The type for the partner position of a base pair probability is size_t.
Definition structure_file/input.hpp:253
The options type defines various option members that influence the behaviour of all or some formats.
Definition structure_file/input_options.hpp:27
Type that contains multiple types.
Definition type_list.hpp:26
Provides seqan3::structure_file_input_format.
Provides seqan3::structure_file_input_options.
Provides seqan3::structure_record.
Provides the composite of aminoacid with structure alphabets.
Provides traits for seqan3::type_list.
Adaptations of concepts from the standard library.
T visit(T... args)
Hide me