NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dna.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <nvbio/basic/types.h>
31 
32 namespace nvbio {
33 
37 {
38  return c == 0 ? 'A' :
39  c == 1 ? 'C' :
40  c == 2 ? 'G' :
41  c == 3 ? 'T' :
42  'N';
43 }
44 
48 {
49  // DNA: A, C, G, T -> { 0, 1, 2, 3 }
50  // IUPAC16: A, C, G, T -> { 1, 2, 4, 8 }
51  return 1 << c;
52 }
53 
57 {
58  return c == 0 ? '=' :
59  c == 1 ? 'A' :
60  c == 2 ? 'C' :
61  c == 3 ? 'M' :
62  c == 4 ? 'G' :
63  c == 5 ? 'R' :
64  c == 6 ? 'S' :
65  c == 7 ? 'V' :
66  c == 8 ? 'T' :
67  c == 9 ? 'W' :
68  c == 10 ? 'Y' :
69  c == 11 ? 'H' :
70  c == 12 ? 'K' :
71  c == 13 ? 'D' :
72  c == 14 ? 'B' :
73  'N';
74 }
75 
79 {
80  return c == 'A' ? 0u :
81  c == 'C' ? 1u :
82  c == 'G' ? 2u :
83  c == 'T' ? 3u :
84  4u;
85 }
86 
90 {
91  return c == '=' ? 0u :
92  c == 'A' ? 1u :
93  c == 'C' ? 2u :
94  c == 'M' ? 3u :
95  c == 'G' ? 4u :
96  c == 'R' ? 5u :
97  c == 'S' ? 6u :
98  c == 'V' ? 7u :
99  c == 'T' ? 8u :
100  c == 'W' ? 9u :
101  c == 'Y' ? 10u :
102  c == 'H' ? 11u :
103  c == 'K' ? 12u :
104  c == 'D' ? 13u :
105  c == 'B' ? 14u :
106  15u;
107 }
108 
111 template <typename SymbolIterator>
113  const SymbolIterator begin,
114  const uint32 n,
115  char* string)
116 {
117  for (uint32 i = 0; i < n; ++i)
118  string[i] = dna_to_char( begin[i] );
119 
120  string[n] = '\0';
121 }
122 
125 template <typename SymbolIterator>
127  const SymbolIterator begin,
128  const SymbolIterator end,
129  char* string)
130 {
131  for (SymbolIterator it = begin; it != end; ++it)
132  string[ (it - begin) % (end - begin) ] = dna_to_char( *it );
133 
134  string[ end - begin ] = '\0';
135 }
136 
139 template <typename SymbolIterator>
141  const SymbolIterator begin,
142  const uint32 n,
143  char* string)
144 {
145  for (uint32 i = 0; i < n; ++i)
146  string[i] = iupac16_to_char( begin[i] );
147 
148  string[n] = '\0';
149 }
150 
153 template <typename SymbolIterator>
155  const SymbolIterator begin,
156  const SymbolIterator end,
157  char* string)
158 {
159  for (SymbolIterator it = begin; it != end; ++it)
160  string[ (it - begin) % (end - begin) ] = iupac16_to_char( *it );
161 
162  string[ end - begin ] = '\0';
163 }
164 
167 template <typename SymbolIterator>
169  const char* begin,
170  const char* end,
171  SymbolIterator symbols)
172 {
173  for (const char* it = begin; it != end; ++it)
174  symbols[ (it - begin) % (end - begin) ] = char_to_dna( *it );
175 }
176 
179 template <typename SymbolIterator>
181  const char* begin,
182  SymbolIterator symbols)
183 {
184  for (const char* it = begin; *it != '\0'; ++it)
185  symbols[ it - begin ] = char_to_dna( *it );
186 }
187 
190 template <typename SymbolIterator>
192  const char* begin,
193  const char* end,
194  SymbolIterator symbols)
195 {
196  for (const char* it = begin; it != end; ++it)
197  symbols[ (it - begin) % (end - begin) ] = char_to_iupac16( *it );
198 }
199 
202 template <typename SymbolIterator>
204  const char* begin,
205  SymbolIterator symbols)
206 {
207  for (const char* it = begin; *it != '\0'; ++it)
208  symbols[ it - begin ] = char_to_iupac16( *it );
209 }
210 
211 } // namespace nvbio
212