BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
translate_join.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <concepts>
17#include <ranges>
18#include <stdexcept>
19#include <vector>
20
25
26// ============================================================================
27// translate_fn (adaptor definition )
28// ============================================================================
29
30namespace bio::ranges::detail
31{
32
35struct translate_join_fn
36{
39
41 constexpr auto operator()(alphabet::translation_frames const tf = default_frames) const
42 {
43 return detail::adaptor_from_functor{*this, tf};
44 }
45
51 template <std::ranges::range urng_t>
52 constexpr auto operator()(urng_t && urange, alphabet::translation_frames const tf = default_frames) const
53 {
54 static_assert(range_dimension_v<urng_t> == 2,
55 "This adaptor only handles range-of-range (two dimensions) as input.");
56 static_assert(std::ranges::viewable_range<urng_t>,
57 "The range parameter to views::translate_join must be viewable.");
58 static_assert(std::ranges::viewable_range<std::ranges::range_reference_t<urng_t>>,
59 "The inner range of the range parameter to views::translate_join must be viewable.");
60 static_assert(std::ranges::sized_range<urng_t>,
61 "The range parameter to views::translate_join must model std::ranges::sized_range.");
62 static_assert(std::ranges::sized_range<std::ranges::range_reference_t<urng_t>>,
63 "The inner range of the range parameter to views::translate_join must model "
64 "std::ranges::sized_range.");
65 static_assert(std::ranges::random_access_range<urng_t>,
66 "The range parameter to views::translate_join must model std::ranges::random_access_range.");
67 static_assert(std::ranges::random_access_range<std::ranges::range_reference_t<urng_t>>,
68 "The inner range of the range parameter to views::translate_join must model "
69 "std::ranges::random_access_range.");
70 static_assert(alphabet::nucleotide<std::ranges::range_reference_t<std::ranges::range_reference_t<urng_t>>>,
71 "The range parameter to views::translate_join must be over a range over elements of "
72 "bio::alphabet::nucleotide.");
73
74 /* frames */
75 small_vector<alphabet::translation_frames, 6> selected_frames{};
77 selected_frames.push_back(alphabet::translation_frames::FWD_FRAME_0);
79 selected_frames.push_back(alphabet::translation_frames::FWD_FRAME_1);
81 selected_frames.push_back(alphabet::translation_frames::FWD_FRAME_2);
83 selected_frames.push_back(alphabet::translation_frames::REV_FRAME_0);
85 selected_frames.push_back(alphabet::translation_frames::REV_FRAME_1);
87 selected_frames.push_back(alphabet::translation_frames::REV_FRAME_2);
88
89 size_t size = std::ranges::size(urange) * selected_frames.size();
90 return detail::view_transform_by_pos{
91 std::forward<urng_t>(urange),
92 trans_fn{.size = size, .selected_frames = selected_frames},
93 size
94 };
95 }
96
98 template <std::ranges::range urng_t>
99 constexpr friend auto operator|(urng_t && urange, translate_join_fn const & me)
100 {
101 return me(std::forward<urng_t>(urange));
102 }
103
104private:
106 struct trans_fn
107 {
109 size_t size{};
111 small_vector<alphabet::translation_frames, 6> selected_frames{};
112
114 auto operator()(auto && urange, size_t const n) const
115 {
116 assert(n < size);
117 size_t index_frame = n % selected_frames.size();
118 size_t index_urange = (n - index_frame) / selected_frames.size();
119 return urange[index_urange] | views::translate_single(selected_frames[index_frame]);
120 }
121 };
122};
123
124} // namespace bio::ranges::detail
125
126// ============================================================================
127// translate (adaptor object)
128// ============================================================================
129
130namespace bio::ranges::views
131{
132
199inline constexpr auto translate_join = detail::translate_join_fn{};
201
202} // namespace bio::ranges::views
constexpr auto size
A type trait that holds the size of a (semi-)alphabet.
Definition: concept.hpp:517
translation_frames
Specialisation values for single and multiple translation frames.
Definition: translation.hpp:85
@ REV_FRAME_0
The first reverse frame starting at position 0.
@ REV_FRAME_1
The second reverse frame starting at position 1.
@ FWD_FRAME_2
The third forward frame starting at position 2.
@ FWD_FRAME_1
The second forward frame starting at position 1.
@ REV_FRAME_2
The third reverse frame starting at position 2.
@ FWD_FRAME_0
The first forward frame starting at position 0.
constexpr auto translate_single
A view that translates nucleotide into aminoacid alphabet for one of the six frames.
Definition: translate_single.hpp:225
constexpr auto translate_join
A view that translates nucleotide into aminoacid alphabet with 1, 2, 3 or 6 frames....
Definition: translate_join.hpp:199
constexpr translation_frames operator|(translation_frames lhs, translation_frames rhs) noexcept
Binary operators for bio::alphabet::translation_frames.
Definition: translation.hpp:111
The BioC++ namespace for views.
Provides the bio::ranges::detail::random_access_iterator class.
A constexpr string implementation to manipulate string literals at compile time.
Provides bio::views::translate and bio::views::translate_single.
Provides various transformation traits used by the range module.