SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
dna4.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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 <vector>
16 
19 
20 // ------------------------------------------------------------------
21 // dna4
22 // ------------------------------------------------------------------
23 
24 namespace seqan3
25 {
26 
27 class rna4;
28 
48 class dna4 : public nucleotide_base<dna4, 4>
49 {
50 private:
53 
55  friend base_t;
57  friend base_t::base_t;
60  friend rna4;
61 
62 public:
66  constexpr dna4() noexcept = default;
67  constexpr dna4(dna4 const &) noexcept = default;
68  constexpr dna4(dna4 &&) noexcept = default;
69  constexpr dna4 & operator=(dna4 const &) noexcept = default;
70  constexpr dna4 & operator=(dna4 &&) noexcept = default;
71  ~dna4() noexcept = default;
72 
73  using base_t::base_t;
74 
76  template <std::Same<rna4> t> // Accept incomplete type
77  constexpr dna4(t const & r) noexcept
78  {
79  assign_rank(r.to_rank());
80  }
82 
83 protected:
85 
87  static constexpr char_type rank_to_char[alphabet_size]
88  {
89  'A',
90  'C',
91  'G',
92  'T'
93  };
94 
96  static constexpr std::array<rank_type, 256> char_to_rank
97  {
98  [] () constexpr
99  {
101 
102  // reverse mapping for characters and their lowercase
103  for (size_t rnk = 0u; rnk < alphabet_size; ++rnk)
104  {
105  ret[ rank_to_char[rnk] ] = rnk;
106  ret[to_lower(rank_to_char[rnk])] = rnk;
107  }
108 
109  // set U equal to T
110  ret['U'] = ret['T']; ret['u'] = ret['t'];
111 
112  // iupac characters get special treatment, because there is no N
113  ret['R'] = ret['A']; ret['r'] = ret['A']; // or G
114  ret['Y'] = ret['C']; ret['y'] = ret['C']; // or T
115  ret['S'] = ret['C']; ret['s'] = ret['C']; // or G
116  ret['W'] = ret['A']; ret['w'] = ret['A']; // or T
117  ret['K'] = ret['G']; ret['k'] = ret['G']; // or T
118  ret['M'] = ret['A']; ret['m'] = ret['A']; // or T
119  ret['B'] = ret['C']; ret['b'] = ret['C']; // or G or T
120  ret['D'] = ret['A']; ret['d'] = ret['A']; // or G or T
121  ret['H'] = ret['A']; ret['h'] = ret['A']; // or C or T
122  ret['V'] = ret['A']; ret['v'] = ret['A']; // or C or G
123 
124  return ret;
125  }()
126  };
127 
129  static const std::array<dna4, alphabet_size> complement_table;
130 };
131 
132 // ------------------------------------------------------------------
133 // containers
134 // ------------------------------------------------------------------
135 
139 
140 // ------------------------------------------------------------------
141 // literals
142 // ------------------------------------------------------------------
143 
152 constexpr dna4 operator""_dna4(char const c) noexcept
153 {
154  return dna4{}.assign_char(c);
155 }
156 
166 inline dna4_vector operator""_dna4(char const * s, std::size_t n)
167 {
168  dna4_vector r;
169  r.resize(n);
170 
171  for (size_t i = 0; i < n; ++i)
172  r[i].assign_char(s[i]);
173 
174  return r;
175 }
177 
178 // ------------------------------------------------------------------
179 // dna4 (deferred definition)
180 // ------------------------------------------------------------------
181 
182 constexpr std::array<dna4, dna4::alphabet_size> dna4::complement_table
183 {
184  'T'_dna4, // complement of 'A'_dna4
185  'G'_dna4, // complement of 'C'_dna4
186  'C'_dna4, // complement of 'G'_dna4
187  'A'_dna4 // complement of 'T'_dna4
188 };
189 
190 } // namespace seqan3
The four letter DNA alphabet of A,C,G,T.
Definition: dna4.hpp:48
constexpr dna4(t const &r) noexcept
Allow implicit construction from dna/rna of the same size.
Definition: dna4.hpp:77
constexpr dna4() noexcept=default
Defaulted.
The main SeqAn3 namespace.
T resize(T... args)
constexpr dna4 & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:166
Provides seqan3::nucleotide_base.
static detail::min_viable_uint_t< size > constexpr alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:175
Provides utilities for modifying characters.
The four letter RNA alphabet of A,C,G,U.
Definition: rna4.hpp:46
~dna4() noexcept=default
Defaulted.
A CRTP-base that refines seqan3::alphabet_base and is used by the nucleotides.
Definition: nucleotide_base.hpp:40
constexpr derived_type & assign_char(char_type const c) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:141
constexpr dna4 & operator=(dna4 const &) noexcept=default
Defaulted.
std::conditional_t< std::Same< char, void >, char, char > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:61
constexpr char_type to_lower(char_type const c) noexcept
Converts &#39;A&#39;-&#39;Z&#39; to &#39;a&#39;-&#39;z&#39; respectively; other characters are returned as is.
Definition: transform.hpp:82