SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
debug_stream_alignment.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, 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 <iomanip>
16 #include <tuple>
17 
24 
25 namespace seqan3::detail
26 {
27 
35 template <typename char_t, tuple_like alignment_t, size_t... idx>
36 void stream_alignment(debug_stream_type<char_t> & stream,
37  alignment_t const & align,
39 {
40  using std::get;
41  size_t const alignment_size = get<0>(align).size();
42 
43  // split alignment into blocks of length 50 and loop over parts
44  for (size_t begin_pos = 0; begin_pos < alignment_size; begin_pos += 50)
45  {
46  size_t const end_pos = std::min(begin_pos + 50, alignment_size);
47 
48  // write header line
49  if (begin_pos != 0)
50  stream << '\n';
51 
52  stream << std::setw(7) << begin_pos << ' ';
53  for (size_t pos = begin_pos + 1; pos <= end_pos; ++pos)
54  {
55  if (pos % 10 == 0)
56  stream << ':';
57  else if (pos % 5 == 0)
58  stream << '.';
59  else
60  stream << ' ';
61  }
62 
63  // write first sequence
64  stream << '\n' << std::setw(8) << "";
65  std::ranges::for_each(get<0>(align) | views::slice(begin_pos, end_pos) | views::to_char,
66  [&stream](char ch)
67  {
68  stream << ch;
69  });
70 
71  auto stream_f = [&](auto const & previous_seq, auto const & aligned_seq)
72  {
73  // write alignment bars
74  stream << '\n' << std::setw(8) << "";
75  std::ranges::for_each(views::zip(previous_seq, aligned_seq) | views::slice(begin_pos, end_pos),
76  [&stream](auto && ch)
77  {
78  stream << (get<0>(ch) == get<1>(ch) ? '|' : ' ');
79  });
80 
81  // write next sequence
82  stream << '\n' << std::setw(8) << "";
83  std::ranges::for_each(aligned_seq | views::slice(begin_pos, end_pos) | views::to_char,
84  [&stream](char ch)
85  {
86  stream << ch;
87  });
88  };
89  (stream_f(get<idx>(align), get<idx + 1>(align)), ...);
90  stream << '\n';
91  }
92 }
93 } // namespace seqan3::detail
94 
95 namespace seqan3
96 {
107 template <typename char_t, typename alignment_t>
108  requires (detail::debug_streamable_tuple<alignment_t>
109  && detail::all_model_aligned_seq<detail::tuple_type_list_t<std::remove_cvref_t<alignment_t>>>)
110 inline debug_stream_type<char_t> & operator<<(debug_stream_type<char_t> & stream, alignment_t && alignment)
111 {
112  constexpr size_t sequence_count = std::tuple_size_v<std::remove_cvref_t<alignment_t>>;
113 
114  static_assert(sequence_count >= 2, "An alignment requires at least two sequences.");
115 
116  detail::stream_alignment(stream, alignment, std::make_index_sequence<sequence_count - 1>{});
117  return stream;
118 }
119 
120 } // namespace seqan3
Includes the aligned_sequence and the related insert_gap and erase_gap functions to enable stl contai...
A "pretty printer" for most SeqAn data structures and related types.
Definition: debug_stream_type.hpp:78
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:63
requires requires
The rank_type of the semi-alphabet; defined as the return type of seqan3::to_rank....
Definition: alphabet/concept.hpp:164
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:178
constexpr auto zip
A view adaptor that takes several views and returns tuple-like values from every i-th element of each...
Definition: zip.hpp:573
Whether a type behaves like a tuple.
T min(T... args)
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: configuration.hpp:415
T setw(T... args)
Provides seqan3::views::slice.
Provides seqan3::views::to_char.
Provides seqan3::debug_stream and related types.
Provides seqan3::tuple_like.
Provides seqan3::views::zip.