BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
translate.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
31
32// ============================================================================
33// translate_fn (adaptor definition )
34// ============================================================================
35
36namespace bio::ranges::detail
37{
38
41struct translate_fn
42{
45
47 constexpr auto operator()(alphabet::translation_frames const tf = default_frames) const
48 {
49 return detail::adaptor_from_functor{*this, tf};
50 }
51
57 template <std::ranges::range urng_t>
58 constexpr auto operator()(urng_t && urange, alphabet::translation_frames const tf = default_frames) const
59 {
60 static_assert(std::ranges::viewable_range<urng_t>,
61 "The range parameter to views::translate_single must be viewable.");
62 static_assert(std::ranges::sized_range<urng_t>,
63 "The range parameter to views::translate_single must model std::ranges::sized_range.");
64 static_assert(std::ranges::random_access_range<urng_t>,
65 "The range parameter to views::translate_single must model std::ranges::random_access_range.");
66 static_assert(alphabet::nucleotide<std::ranges::range_reference_t<urng_t>>,
67 "The range parameter to views::translate_single must be over elements of "
68 "bio::alphabet::alphabet::nucleotide.");
69
70 /* frames */
71 small_vector<alphabet::translation_frames, 6> selected_frames{};
73 selected_frames.push_back(alphabet::translation_frames::FWD_FRAME_0);
75 selected_frames.push_back(alphabet::translation_frames::FWD_FRAME_1);
77 selected_frames.push_back(alphabet::translation_frames::FWD_FRAME_2);
79 selected_frames.push_back(alphabet::translation_frames::REV_FRAME_0);
81 selected_frames.push_back(alphabet::translation_frames::REV_FRAME_1);
83 selected_frames.push_back(alphabet::translation_frames::REV_FRAME_2);
84
85 return detail::view_transform_by_pos{std::forward<urng_t>(urange),
86 trans_fn{.selected_frames = selected_frames},
87 selected_frames.size()};
88 }
89
91 template <std::ranges::range urng_t>
92 constexpr friend auto operator|(urng_t && urange, translate_fn const & me)
93 {
94 return me(std::forward<urng_t>(urange));
95 }
96
97private:
99 struct trans_fn
100 {
102 small_vector<alphabet::translation_frames, 6> selected_frames{};
103
105 auto operator()(auto && urange, size_t const n) const
106 {
107 assert(n < selected_frames.size());
108 return urange | views::translate_single(selected_frames[n]);
109 }
110 };
111};
112
113} // namespace bio::ranges::detail
114
115// ============================================================================
116// translate (adaptor object)
117// ============================================================================
118
119namespace bio::ranges::views
120{
121
177inline constexpr auto translate = deep{detail::translate_fn{}};
179
180} // namespace bio::ranges::views
Provides bio::alphabet::aa27, container aliases and string literals.
A wrapper type around an existing view adaptor that enables "deep view" behaviour for that view.
Definition: deep.hpp:104
Provides bio::views::deep.
Provides bio::alphabet::dna5, container aliases and string literals.
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
A view that translates nucleotide into aminoacid alphabet with 1, 2, 3 or 6 frames.
Definition: translate.hpp:177
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.
Adaptations of concepts from the standard library.
Auxiliary header for the views submodule .
A constexpr string implementation to manipulate string literals at compile time.
Provides bio::views::translate and bio::views::translate_single.
Provides functions for translating a triplet of nucleotides into an amino acid.
Provides various transformation traits used by the range module.