NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
alphabet_inl.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 #include <nvbio/basic/dna.h>
32 
33 namespace nvbio {
34 
38 {
39  return c == 0 ? 'A' :
40  c == 1 ? 'C' :
41  c == 2 ? 'G' :
42  c == 3 ? 'U' :
43  'N';
44 }
45 
49 {
50  return c == 'A' ? 0u :
51  c == 'C' ? 1u :
52  c == 'G' ? 2u :
53  c == 'U' ? 3u :
54  4u;
55 }
56 
60 {
61  return c == 0 ? 'A' :
62  c == 1 ? 'C' :
63  c == 2 ? 'D' :
64  c == 3 ? 'E' :
65  c == 4 ? 'F' :
66  c == 5 ? 'G' :
67  c == 6 ? 'H' :
68  c == 7 ? 'I' :
69  c == 8 ? 'K' :
70  c == 9 ? 'L' :
71  c == 10 ? 'M' :
72  c == 11 ? 'N' :
73  c == 12 ? 'O' :
74  c == 13 ? 'P' :
75  c == 14 ? 'Q' :
76  c == 15 ? 'R' :
77  c == 16 ? 'S' :
78  c == 17 ? 'T' :
79  c == 18 ? 'V' :
80  c == 19 ? 'W' :
81  c == 20 ? 'Y' :
82  c == 21 ? 'B' :
83  c == 22 ? 'Z' :
84  c == 23 ? 'X' :
85  'X';
86 }
87 
91 {
92  return c == 'A' ? 0u :
93  c == 'C' ? 1u :
94  c == 'D' ? 2u :
95  c == 'E' ? 3u :
96  c == 'F' ? 4u :
97  c == 'G' ? 5u :
98  c == 'H' ? 6u :
99  c == 'I' ? 7u :
100  c == 'K' ? 8u :
101  c == 'L' ? 9u :
102  c == 'M' ? 10u :
103  c == 'N' ? 11u :
104  c == 'O' ? 12u :
105  c == 'P' ? 13u :
106  c == 'Q' ? 14u :
107  c == 'R' ? 15u :
108  c == 'S' ? 16u :
109  c == 'T' ? 17u :
110  c == 'V' ? 18u :
111  c == 'W' ? 19u :
112  c == 'Y' ? 20u :
113  c == 'B' ? 21u :
114  c == 'Z' ? 22u :
115  c == 'X' ? 23u :
116  23u;
117 }
118 
119 // convert a given symbol to its ASCII character
120 //
121 template <Alphabet ALPHABET>
123 {
124  if (ALPHABET == DNA)
125  return dna_to_char( c );
126  else if (ALPHABET == DNA_N)
127  return dna_to_char( c );
128  else if (ALPHABET == DNA_IUPAC)
129  return iupac16_to_char( c );
130  else if (ALPHABET == PROTEIN)
131  return protein_to_char( c );
132  else if (ALPHABET == RNA)
133  return rna_to_char( c );
134  else if (ALPHABET == RNA_N)
135  return rna_to_char( c );
136  else if (ALPHABET == ASCII)
137  return char( c );
138 
139  return 0;
140 }
141 
142 // convert a given symbol to its ASCII character
143 //
144 template <Alphabet ALPHABET>
146 {
147  if (ALPHABET == DNA)
148  return char_to_dna( c );
149  else if (ALPHABET == DNA_N)
150  return char_to_dna( c );
151  else if (ALPHABET == DNA_IUPAC)
152  return char_to_iupac16( c );
153  else if (ALPHABET == PROTEIN)
154  return char_to_protein( c );
155  else if (ALPHABET == RNA)
156  return char_to_rna( c );
157  else if (ALPHABET == RNA_N)
158  return char_to_rna( c );
159  else if (ALPHABET == ASCII)
160  return uint8( c );
161 
162  return uint8(0);
163 }
164 
165 // convert from the given alphabet to an ASCII string
166 //
167 template <Alphabet ALPHABET, typename SymbolIterator>
169  const SymbolIterator begin,
170  const uint32 n,
171  char* string)
172 {
173  for (uint32 i = 0; i < n; ++i)
174  string[i] = to_char<ALPHABET>( begin[i] );
175 
176  string[n] = '\0';
177 }
178 
179 // convert from the given alphabet to an ASCII string
180 //
181 template <Alphabet ALPHABET, typename SymbolIterator>
183  const SymbolIterator begin,
184  const SymbolIterator end,
185  char* string)
186 {
187  for (SymbolIterator it = begin; it != end; ++it)
188  string[ (it - begin) % (end - begin) ] = to_char<ALPHABET>( *it );
189 
190  string[ end - begin ] = '\0';
191 }
192 
193 // convert from an ASCII string to the given alphabet
194 //
195 template <Alphabet ALPHABET, typename SymbolIterator>
197  const char* begin,
198  const char* end,
199  SymbolIterator symbols)
200 {
201  for (const char* it = begin; it != end; ++it)
202  symbols[ (it - begin) % (end - begin) ] = from_char<ALPHABET>( *it );
203 }
204 
205 // convert from an ASCII string to the given alphabet
206 //
207 template <Alphabet ALPHABET, typename SymbolIterator>
209  const char* begin,
210  SymbolIterator symbols)
211 {
212  for (const char* it = begin; *it != '\0'; ++it)
213  symbols[ it - begin ] = from_char<ALPHABET>( *it );
214 }
215 
216 } // namespace nvbio
217