SeqAn3
translation.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
42 #include <tuple>
43 
50 #include <seqan3/std/ranges>
52 
53 namespace seqan3
54 {
55 
56 // forwards:
57 class dna4;
58 class dna5;
59 class dna15;
60 class rna4;
61 class rna5;
62 class rna15;
63 
82 template <genetic_code gc = genetic_code::CANONICAL, nucleotide_concept nucl_type>
83 constexpr aa27 translate_triplet(nucl_type const & n1, nucl_type const & n2, nucl_type const & n3) noexcept
84 {
86  {
87  // table exists for dna15 and is generated for dna4 and dna5 (compile time ok, because small)
88  return seqan3::detail::translation_table<nucl_type, gc>::VALUE[to_rank(n1)][to_rank(n2)][to_rank(n3)];
89  }
91  {
92  using rna2dna_t = std::conditional_t<std::Same<nucl_type, rna4>, dna4,
93  std::conditional_t<std::Same<nucl_type, rna5>, dna5,
94  std::conditional_t<std::Same<nucl_type, rna15>, dna15, void>>>;
95 
96  // we can use dna's tables, because ranks are identical
97  return seqan3::detail::translation_table<rna2dna_t, gc>::VALUE[to_rank(n1)][to_rank(n2)][to_rank(n3)];
98  }
99  else // composites or user defined nucleotide
100  {
101  // we cast to dna15; slightly slower run-time, but lot's of compile time saved for large alphabets.
102  // (nucleotide types can be converted to dna15 by definition)
103  return seqan3::detail::translation_table<dna15, gc>::VALUE[to_rank(static_cast<dna15>(n1))]
104  [to_rank(static_cast<dna15>(n2))]
105  [to_rank(static_cast<dna15>(n3))];
106  }
107 }
108 
125 template <genetic_code gc = genetic_code::CANONICAL, typename tuple_type>
127  requires std::tuple_size<tuple_type>::value == 3 &&
132 constexpr aa27 translate_triplet(tuple_type const & input_tuple) noexcept
133 {
134  return translate_triplet(std::get<0>(input_tuple), std::get<1>(input_tuple), std::get<2>(input_tuple));
135 }
136 
153 template <genetic_code gc = genetic_code::CANONICAL, std::ranges::InputRange range_type>
157 constexpr aa27 translate_triplet(range_type && input_range)
158 {
159  auto n1 = begin(input_range);
160  auto n2 = ++n1;
161  auto n3 = ++n2;
162 
163  assert(n1 != end(input_range));
164  assert(n2 != end(input_range));
165  assert(n3 != end(input_range));
166 
167  return translate_triplet(*n1, *n2, *n3);
168 }
169 
170 
187 template <genetic_code gc = genetic_code::CANONICAL, std::ranges::RandomAccessRange range_type>
191 constexpr aa27 translate_triplet(range_type && input_range)
192 {
193  assert(input_range.begin() != end(input_range));
194  assert(input_range.begin() + 1 != end(input_range));
195  assert(input_range.begin() + 2 != end(input_range));
196 
197  return translate_triplet(input_range[0], input_range[1], input_range[2]);
198 }
199 
200 } // namespace seqan3
Contains translation details for nucleotide to aminoacid translation.
The four letter DNA alphabet of A,C,G,T.
Definition: dna4.hpp:73
Contains various shortcuts for common std::ranges functions.
Contains seqan3::aa27, container aliases and string literals.
The 15 letter DNA alphabet, containing all IUPAC smybols minus the gap.
Definition: dna15.hpp:73
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
Genetic codes used for translating a triplet of nucleotides into an amino acid.
The twenty-seven letter amino acid alphabet.
Definition: aa27.hpp:67
The five letter DNA alphabet of A,C,G,T and the unknown character N.
Definition: dna5.hpp:73
Provides various metafunctions base templates and shortcuts.
Provides seqan3::nucleotide_concept.
Adaptations of concepts from the Ranges TS.
::ranges::begin begin
Alias for ranges::begin. Returns an iterator to the beginning of a range.
Definition: ranges:185
The concept std::Same<T, U> is satisfied if and only if T and U denote the same type.
Provides various metafunctions used by the range module.
constexpr aa27 translate_triplet(nucl_type const &n1, nucl_type const &n2, nucl_type const &n3) noexcept
Translate one nucleotide triplet into single amino acid (single nucleotide interface).
Definition: translation.hpp:83
::ranges::end end
Alias for ranges::end. Returns an iterator to the end of a range.
Definition: ranges:190
constexpr underlying_rank_t< alphabet_type > to_rank(alphabet_type const alph) requires requires(alphabet_type alph)
Implementation of seqan3::semi_alphabet_concept::to_rank() that delegates to a member function...
Definition: member_exposure.hpp:97
A concept that indicates whether an alphabet represents nucleotides.In addition to the requirements f...