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