2342 |
17 Oct 10 |
peter |
// $Id$ |
2342 |
17 Oct 10 |
peter |
2 |
|
2342 |
17 Oct 10 |
peter |
3 |
/* |
4089 |
07 Sep 21 |
peter |
Copyright (C) 2010, 2011, 2012, 2016, 2021 Peter Johansson |
2342 |
17 Oct 10 |
peter |
5 |
|
2342 |
17 Oct 10 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
2342 |
17 Oct 10 |
peter |
7 |
|
2342 |
17 Oct 10 |
peter |
The yat library is free software; you can redistribute it and/or |
2342 |
17 Oct 10 |
peter |
modify it under the terms of the GNU General Public License as |
2342 |
17 Oct 10 |
peter |
published by the Free Software Foundation; either version 3 of the |
2342 |
17 Oct 10 |
peter |
License, or (at your option) any later version. |
2342 |
17 Oct 10 |
peter |
12 |
|
2342 |
17 Oct 10 |
peter |
The yat library is distributed in the hope that it will be useful, |
2342 |
17 Oct 10 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2342 |
17 Oct 10 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2342 |
17 Oct 10 |
peter |
General Public License for more details. |
2342 |
17 Oct 10 |
peter |
17 |
|
2342 |
17 Oct 10 |
peter |
You should have received a copy of the GNU General Public License |
2342 |
17 Oct 10 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
2342 |
17 Oct 10 |
peter |
20 |
*/ |
2342 |
17 Oct 10 |
peter |
21 |
|
2881 |
18 Nov 12 |
peter |
22 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
23 |
|
2342 |
17 Oct 10 |
peter |
24 |
#include "DNA.h" |
2342 |
17 Oct 10 |
peter |
25 |
|
2342 |
17 Oct 10 |
peter |
26 |
#include <cassert> |
2342 |
17 Oct 10 |
peter |
27 |
#include <ostream> |
3502 |
07 Jun 16 |
peter |
28 |
#include <sstream> |
2342 |
17 Oct 10 |
peter |
29 |
#include <stdexcept> |
2342 |
17 Oct 10 |
peter |
30 |
|
2342 |
17 Oct 10 |
peter |
31 |
namespace theplu { |
2342 |
17 Oct 10 |
peter |
32 |
namespace yat { |
2342 |
17 Oct 10 |
peter |
33 |
namespace omic { |
2342 |
17 Oct 10 |
peter |
34 |
|
2342 |
17 Oct 10 |
peter |
35 |
DNA::DNA(void) |
2741 |
07 Jun 12 |
peter |
36 |
: code_(0) |
2342 |
17 Oct 10 |
peter |
37 |
{ |
2342 |
17 Oct 10 |
peter |
38 |
assert(code_<16); |
2342 |
17 Oct 10 |
peter |
39 |
} |
2342 |
17 Oct 10 |
peter |
40 |
|
2342 |
17 Oct 10 |
peter |
41 |
|
2342 |
17 Oct 10 |
peter |
42 |
DNA::DNA(char c) |
2342 |
17 Oct 10 |
peter |
43 |
{ |
2741 |
07 Jun 12 |
peter |
44 |
try { |
3502 |
07 Jun 16 |
peter |
45 |
code_ = char2code(static_cast<size_t>(c)); |
2741 |
07 Jun 12 |
peter |
46 |
} |
2741 |
07 Jun 12 |
peter |
47 |
catch (std::invalid_argument& e) { |
2342 |
17 Oct 10 |
peter |
48 |
std::string msg("DNA('"); |
2342 |
17 Oct 10 |
peter |
49 |
msg += c; |
2342 |
17 Oct 10 |
peter |
50 |
msg += "')"; |
4060 |
10 May 21 |
peter |
51 |
std::throw_with_nested(std::invalid_argument(msg)); |
2342 |
17 Oct 10 |
peter |
52 |
} |
2342 |
17 Oct 10 |
peter |
53 |
assert(code_<16); |
2342 |
17 Oct 10 |
peter |
54 |
} |
2342 |
17 Oct 10 |
peter |
55 |
|
2342 |
17 Oct 10 |
peter |
56 |
|
3502 |
07 Jun 16 |
peter |
57 |
unsigned short DNA::char2code(char x) const |
2342 |
17 Oct 10 |
peter |
58 |
{ |
3502 |
07 Jun 16 |
peter |
59 |
switch (x) { |
3502 |
07 Jun 16 |
peter |
60 |
case(' '): return 0; |
3502 |
07 Jun 16 |
peter |
61 |
case('A'): return 1; |
3502 |
07 Jun 16 |
peter |
62 |
case('C'): return 2; |
3502 |
07 Jun 16 |
peter |
63 |
case('M'): return 3; |
3502 |
07 Jun 16 |
peter |
64 |
case('G'): return 4; |
3502 |
07 Jun 16 |
peter |
65 |
case('R'): return 5; |
3502 |
07 Jun 16 |
peter |
66 |
case('S'): return 6; |
3502 |
07 Jun 16 |
peter |
67 |
case('V'): return 7; |
3502 |
07 Jun 16 |
peter |
68 |
case('T'): return 8; |
3502 |
07 Jun 16 |
peter |
69 |
case('W'): return 9; |
3502 |
07 Jun 16 |
peter |
70 |
case('Y'): return 10; |
3502 |
07 Jun 16 |
peter |
71 |
case('H'): return 11; |
3502 |
07 Jun 16 |
peter |
72 |
case('K'): return 12; |
3502 |
07 Jun 16 |
peter |
73 |
case('D'): return 13; |
3502 |
07 Jun 16 |
peter |
74 |
case('B'): return 14; |
3502 |
07 Jun 16 |
peter |
75 |
case('N'): return 15; |
3502 |
07 Jun 16 |
peter |
76 |
default: |
3502 |
07 Jun 16 |
peter |
77 |
std::stringstream ss; |
3502 |
07 Jun 16 |
peter |
78 |
ss << "DNA::char2code('" << x << "')"; |
3502 |
07 Jun 16 |
peter |
79 |
throw std::invalid_argument(ss.str()); |
3502 |
07 Jun 16 |
peter |
80 |
} |
3502 |
07 Jun 16 |
peter |
81 |
return 0; |
3502 |
07 Jun 16 |
peter |
82 |
} |
2342 |
17 Oct 10 |
peter |
83 |
|
3502 |
07 Jun 16 |
peter |
84 |
|
3502 |
07 Jun 16 |
peter |
85 |
char DNA::code2char(unsigned short i) const |
3502 |
07 Jun 16 |
peter |
86 |
{ |
3502 |
07 Jun 16 |
peter |
87 |
assert(i<16); |
3502 |
07 Jun 16 |
peter |
88 |
char tmp[17] = " ACMGRSVTWYHKDBN"; |
3502 |
07 Jun 16 |
peter |
89 |
return tmp[i]; |
2342 |
17 Oct 10 |
peter |
90 |
} |
2342 |
17 Oct 10 |
peter |
91 |
|
2342 |
17 Oct 10 |
peter |
92 |
|
2342 |
17 Oct 10 |
peter |
93 |
DNA DNA::complement(void) const |
2342 |
17 Oct 10 |
peter |
94 |
{ |
3502 |
07 Jun 16 |
peter |
// code2char = " ACMGRSVTWYHKDBN"; |
2742 |
08 Jun 12 |
peter |
// complment to string char2code_ |
3502 |
07 Jun 16 |
peter |
97 |
char complement[17] = " TGKCYSBAWRDMHVN"; |
3502 |
07 Jun 16 |
peter |
98 |
assert(code_<16); |
2742 |
08 Jun 12 |
peter |
99 |
return DNA(complement[code_]); |
2342 |
17 Oct 10 |
peter |
100 |
} |
2342 |
17 Oct 10 |
peter |
101 |
|
2342 |
17 Oct 10 |
peter |
102 |
|
2342 |
17 Oct 10 |
peter |
103 |
std::string expand(const DNA& dna) |
2342 |
17 Oct 10 |
peter |
104 |
{ |
2342 |
17 Oct 10 |
peter |
105 |
std::string result; |
2342 |
17 Oct 10 |
peter |
106 |
std::string str="ACGT"; |
2342 |
17 Oct 10 |
peter |
107 |
for (size_t i=0; i<str.size(); ++i) |
2342 |
17 Oct 10 |
peter |
108 |
if ((dna & DNA(str[i])) != DNA(' ')) |
2342 |
17 Oct 10 |
peter |
109 |
result.push_back(str[i]); |
2342 |
17 Oct 10 |
peter |
110 |
return result; |
2342 |
17 Oct 10 |
peter |
111 |
} |
2342 |
17 Oct 10 |
peter |
112 |
|
2342 |
17 Oct 10 |
peter |
113 |
|
2342 |
17 Oct 10 |
peter |
114 |
char DNA::get(void) const |
2342 |
17 Oct 10 |
peter |
115 |
{ |
3502 |
07 Jun 16 |
peter |
116 |
return code2char(code_); |
2342 |
17 Oct 10 |
peter |
117 |
} |
2342 |
17 Oct 10 |
peter |
118 |
|
2741 |
07 Jun 12 |
peter |
119 |
|
2342 |
17 Oct 10 |
peter |
120 |
bool operator==(const DNA& lhs, const DNA& rhs) |
2342 |
17 Oct 10 |
peter |
121 |
{ |
2342 |
17 Oct 10 |
peter |
122 |
return lhs.code_ == rhs.code_; |
2342 |
17 Oct 10 |
peter |
123 |
} |
2342 |
17 Oct 10 |
peter |
124 |
|
2342 |
17 Oct 10 |
peter |
125 |
|
2342 |
17 Oct 10 |
peter |
126 |
std::ostream& operator<<(std::ostream& os, const DNA& dna) |
2342 |
17 Oct 10 |
peter |
127 |
{ |
2342 |
17 Oct 10 |
peter |
128 |
os.put(dna.get()); |
2342 |
17 Oct 10 |
peter |
129 |
return os; |
2342 |
17 Oct 10 |
peter |
130 |
} |
2342 |
17 Oct 10 |
peter |
131 |
|
2342 |
17 Oct 10 |
peter |
132 |
|
2342 |
17 Oct 10 |
peter |
133 |
DNA& DNA::operator&=(const DNA& other) |
2342 |
17 Oct 10 |
peter |
134 |
{ |
2342 |
17 Oct 10 |
peter |
135 |
code_ &= other.code_; |
2342 |
17 Oct 10 |
peter |
136 |
assert(code_<16); |
2342 |
17 Oct 10 |
peter |
137 |
return *this; |
2342 |
17 Oct 10 |
peter |
138 |
} |
2342 |
17 Oct 10 |
peter |
139 |
|
2342 |
17 Oct 10 |
peter |
140 |
|
2342 |
17 Oct 10 |
peter |
141 |
DNA& DNA::operator|=(const DNA& other) |
2342 |
17 Oct 10 |
peter |
142 |
{ |
2342 |
17 Oct 10 |
peter |
143 |
code_ |= other.code_; |
2342 |
17 Oct 10 |
peter |
144 |
assert(code_<16); |
2342 |
17 Oct 10 |
peter |
145 |
return *this; |
2342 |
17 Oct 10 |
peter |
146 |
} |
2342 |
17 Oct 10 |
peter |
147 |
|
2342 |
17 Oct 10 |
peter |
148 |
|
2342 |
17 Oct 10 |
peter |
149 |
DNA& DNA::operator^=(const DNA& other) |
2342 |
17 Oct 10 |
peter |
150 |
{ |
2342 |
17 Oct 10 |
peter |
151 |
code_ ^= other.code_; |
2342 |
17 Oct 10 |
peter |
152 |
assert(code_<16); |
2342 |
17 Oct 10 |
peter |
153 |
return *this; |
2342 |
17 Oct 10 |
peter |
154 |
} |
2577 |
03 Oct 11 |
peter |
155 |
|
2577 |
03 Oct 11 |
peter |
156 |
|
2577 |
03 Oct 11 |
peter |
157 |
DNA& DNA::operator=(char c) |
2577 |
03 Oct 11 |
peter |
158 |
{ |
3502 |
07 Jun 16 |
peter |
159 |
*this = DNA(c); |
2577 |
03 Oct 11 |
peter |
160 |
return *this; |
2577 |
03 Oct 11 |
peter |
161 |
} |
2577 |
03 Oct 11 |
peter |
162 |
|
2342 |
17 Oct 10 |
peter |
163 |
}}} |