SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
minimiser_hash.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 
19 
20 namespace seqan3
21 {
24 struct seed : seqan3::detail::strong_type<uint64_t, seed>
25 {
26  using seqan3::detail::strong_type<uint64_t, seed>::strong_type;
27 };
28 
31 struct window_size : seqan3::detail::strong_type<uint32_t, window_size>
32 {
33  using seqan3::detail::strong_type<uint32_t, window_size>::strong_type;
34 };
35 } // namespace seqan3
36 
37 namespace seqan3::detail
38 {
41 struct minimiser_hash_fn
42 {
49  constexpr auto operator()(shape const & shape, window_size const window_size) const
50  {
51  return seqan3::detail::adaptor_from_functor{*this, shape, window_size};
52  }
53 
61  constexpr auto operator()(shape const & shape, window_size const window_size, seed const seed) const
62  {
63  return seqan3::detail::adaptor_from_functor{*this, shape, window_size, seed};
64  }
65 
75  template <std::ranges::range urng_t>
76  constexpr auto operator()(urng_t && urange,
77  shape const & shape,
78  window_size const window_size,
79  seed const seed = seqan3::seed{0x8F3F73B5CF1C9ADE}) const
80  {
81  static_assert(std::ranges::viewable_range<urng_t>,
82  "The range parameter to views::minimiser_hash cannot be a temporary of a non-view range.");
83  static_assert(std::ranges::forward_range<urng_t>,
84  "The range parameter to views::minimiser_hash must model std::ranges::forward_range.");
85  static_assert(semialphabet<std::ranges::range_reference_t<urng_t>>,
86  "The range parameter to views::minimiser_hash must be over elements of seqan3::semialphabet.");
87 
88  if (shape.size() > window_size.get())
89  throw std::invalid_argument{"The size of the shape cannot be greater than the window size."};
90 
91  auto forward_strand = std::forward<urng_t>(urange) | seqan3::views::kmer_hash(shape)
93  [seed](uint64_t i)
94  {
95  return i ^ seed.get();
96  });
97 
98  auto reverse_strand = std::forward<urng_t>(urange) | seqan3::views::complement | std::views::reverse
101  [seed](uint64_t i)
102  {
103  return i ^ seed.get();
104  })
105  | std::views::reverse;
106 
107  return seqan3::detail::minimiser_view(forward_strand, reverse_strand, window_size.get() - shape.size() + 1);
108  }
109 };
110 
111 } // namespace seqan3::detail
112 
113 namespace seqan3::views
114 {
115 
193 inline constexpr auto minimiser_hash = detail::minimiser_hash_fn{};
194 
196 
197 } // namespace seqan3::views
Provides seqan3::views::complement.
constexpr auto minimiser_hash
Computes minimisers for a range with a given shape, window size and seed.
Definition: minimiser_hash.hpp:193
auto const complement
A view that converts a range of nucleotides to their complement.
Definition: complement.hpp:67
constexpr auto kmer_hash
Computes hash values for each position of a range via a given shape.
Definition: kmer_hash.hpp:750
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: type_list/traits.hpp:469
The basis for seqan3::alphabet, but requires only rank interface (not char).
Provides seqan3::views::kmer_hash.
Provides seqan3::views::minimiser.
The SeqAn namespace for views.
Definition: char_strictly_to.hpp:22
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
Provides basic data structure for strong types.
strong_type for seed.
Definition: minimiser_hash.hpp:25
strong_type for the window_size.
Definition: minimiser_hash.hpp:32