SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
format_sam.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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 
14 #pragma once
15 
16 #include <iterator>
17 #include <string>
18 #include <string_view>
19 #include <vector>
20 
26 
27 namespace seqan3::detail
28 {
29 
47 template <>
48 class sequence_file_input_format<format_sam>
49 {
50 public:
52  using format_tag = format_sam;
53 
57  sequence_file_input_format() = default;
58  sequence_file_input_format(sequence_file_input_format const &) = delete;
61  sequence_file_input_format & operator=(sequence_file_input_format const &) = delete;
62  sequence_file_input_format(sequence_file_input_format &&) = default;
63  sequence_file_input_format & operator=(sequence_file_input_format &&) = default;
64  ~sequence_file_input_format() = default;
65 
68  template <typename stream_type, // constraints checked by file
69  typename seq_legal_alph_type, bool seq_qual_combined,
70  typename seq_type, // other constraints checked inside function
71  typename id_type,
72  typename qual_type>
73  void read(stream_type & stream,
74  sequence_file_input_options<seq_legal_alph_type, seq_qual_combined> const & SEQAN3_DOXYGEN_ONLY(options),
75  seq_type & sequence,
76  id_type & id,
77  qual_type & qualities)
78  {
79  alignment_file_input_options<seq_legal_alph_type> align_options;
80 
81  if constexpr (seq_qual_combined)
82  {
83  tmp_qual.clear();
84  align_format.read(stream, align_options, std::ignore, default_header, sequence, tmp_qual, id,
85  std::ignore, std::ignore, std::ignore, std::ignore, std::ignore,
86  std::ignore, std::ignore, std::ignore, std::ignore, std::ignore, std::ignore);
87 
88  for (auto sit = tmp_qual.begin(), dit = std::ranges::begin(sequence); sit != tmp_qual.end(); ++sit, ++dit)
89  get<1>(*dit).assign_char(*sit);
90  }
91  else
92  {
93  align_format.read(stream, align_options, std::ignore, default_header, sequence, qualities, id,
94  std::ignore, std::ignore, std::ignore, std::ignore, std::ignore,
95  std::ignore, std::ignore, std::ignore, std::ignore, std::ignore, std::ignore);
96  }
97 
98  if constexpr (!detail::decays_to_ignore_v<seq_type>)
99  if (std::distance(std::ranges::begin(sequence), std::ranges::end(sequence)) == 0)
100  throw format_error{"The sequence information must not be empty."};
101  if constexpr (!detail::decays_to_ignore_v<id_type>)
103  throw format_error{"The sequence information must not be empty."};
104  }
105 
106 private:
108  alignment_file_input_format<format_sam> align_format{};
109 
111  alignment_file_header<> default_header{};
112 
114  std::string tmp_qual{};
115 };
116 
119 template <>
120 class sequence_file_output_format<format_sam>
121 {
122 public:
124  using format_tag = format_sam;
125 
129  sequence_file_output_format() noexcept = default;
130  sequence_file_output_format(sequence_file_output_format const &) = delete;
133  sequence_file_output_format & operator=(sequence_file_output_format const &) = delete;
134  sequence_file_output_format(sequence_file_output_format &&) noexcept = default;
135  sequence_file_output_format & operator=(sequence_file_output_format &&) noexcept = default;
136  ~sequence_file_output_format() noexcept = default;
137 
140  template <typename stream_type, // constraints checked by file
141  typename seq_type, // other constraints checked inside function
142  typename id_type,
143  typename qual_type>
144  void write(stream_type & stream,
145  sequence_file_output_options const & SEQAN3_DOXYGEN_ONLY(options),
146  seq_type && sequence,
147  id_type && id,
148  qual_type && qualities)
149  {
150  using default_align_t = std::pair<std::span<gapped<char>>, std::span<gapped<char>>>;
151  using default_mate_t = std::tuple<std::string_view, std::optional<int32_t>, int32_t>;
152 
153  alignment_file_output_options output_options;
154 
155  align_format.write(stream, output_options, std::ignore,
156  default_or(sequence), default_or(qualities), default_or(id),
157  0, std::string_view{}, std::string_view{}, -1, default_align_t{}, 0, 0,
158  default_mate_t{}, sam_tag_dictionary{}, 0, 0);
159  }
160 
161 private:
163  alignment_file_output_format<format_sam> align_format{};
164 
166  static constexpr std::string_view dummy{};
167 
169  std::string_view const & default_or(detail::ignore_t) const noexcept
170  {
171  return dummy;
172  }
173 
175  template <typename t>
176  decltype(auto) default_or(t && v) const noexcept
177  {
178  return std::forward<t>(v);
179  }
180 };
181 
182 } // namespace seqan3::detail
Provides seqan3::SequenceFileInputFormat and auxiliary classes.
T distance(T... args)
Provides seqan3::sequence_file_output_options.
::ranges::begin begin
Alias for ranges::begin. Returns an iterator to the beginning of a range.
Definition: ranges:174
Provides the seqan3::format_sam tag and the seqan3::alignment_file_input_format and seqan3::alignment...
Definition: aligned_sequence_concept.hpp:35
Provides seqan3::SequenceFileFormatOut and auxiliary classes.
Provides seqan3::sequence_file_input_options.
::ranges::end end
Alias for ranges::end. Returns an iterator to the end of a range.
Definition: ranges:179