-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiupac.cpp
More file actions
202 lines (173 loc) · 4.46 KB
/
Copy pathiupac.cpp
File metadata and controls
202 lines (173 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
MODER is a program to learn DNA binding motifs from SELEX datasets.
Copyright (C) 2016, 2017 Jarkko Toivonen,
Department of Computer Science, University of Helsinki
MODER is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
MODER is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "iupac.hpp"
#include "data.hpp"
#include <cstring>
#include <algorithm>
std::string iupac_chars = "ACGTWSMKRYBDHVN";
bool
is_iupac_string(const std::string& str)
{
for (int i=0; i < str.length(); ++i) {
if (not iupac_class.is_iupac_code(str[i]))
return false;
}
return true;
}
// c is an iupac code
// char_class is iupac code
// is c \subset char_class
bool
iupac_match(char c, char char_class)
{
//std::string nucs = "ACGT";
//assert(std::count(iupac_chars.begin(), iupac_chars.end(), c)); // Make sure that c is a nucleotide: A, C, G, or T
assert(iupac_class.is_iupac_code(c));
assert(iupac_class.is_iupac_code(char_class));
int cbits = iupac_class.char_to_bits(c);
if (cbits == (cbits & iupac_class.char_to_bits(char_class)))
return true;
else
return false;
/*
const char* p = iupac_class(char_class);
for (; *p != '\0'; ++p)
if (*p == c) // Is c contained in the char_class
return true;
return false;
*/
}
// str is nucleotide sequence
// pattern is iupac sequence
bool
iupac_string_match(const std::string& str, const std::string& pattern)
{
assert(str.length() == pattern.length());
for (int i=0; i < str.length(); ++i)
if (not iupac_match(str[i], pattern[i]))
return false;
return true;
}
std::string
complement_set(char char_class)
{
std::string result;
std::string nucs("ACGT");
for (int i=0; i < 4; ++i) {
if (not iupac_match(nucs[i], char_class))
result.push_back(nucs[i]);
}
return result;
}
/*
bool
iupac_string_match(const char* str, const char* pattern)
{
return iupac_string_match(std::string(str), std::string(pattern));
}
*/
iupac_class_type::char_class_t iupac_class_type::char_classes[16] =
{{'A', "A"},
{'C', "C"},
{'G', "G"},
{'T', "T"},
{'U', "U"},
{'W', "AT"},
{'S', "CG"},
{'M', "AC"},
{'K', "GT"},
{'R', "AG"},
{'Y', "CT"},
{'B', "CGT"},
{'D', "AGT"},
{'H', "ACT"},
{'V', "ACG"},
{'N', "ACGT"}};
iupac_class_type::char_bits_t iupac_class_type::char_bits[16] =
{{'A', 8 },
{'C', 4 },
{'G', 2 },
{'T', 1},
{'U', 1},
{'W', 8 +1},
{'S', 4+2 },
{'M', 8+4 },
{'K', 2+1},
{'R', 8 +2 },
{'Y', 4 +1},
{'B', 4+2+1},
{'D', 8 +2+1},
{'H', 8+4 +1},
{'V', 8+4+2 },
{'N', 8+4+2+1}};
char complement(char c)
{
switch(c) {
case 'A': return 'T';
case 'C': return 'G';
case 'G': return 'C';
case 'T': return 'A';
case 'W': return 'W';
case 'S': return 'S';
case 'M': return 'K';
case 'K': return 'M';
case 'R': return 'Y';
case 'Y': return 'R';
case 'B': return 'V';
case 'D': return 'H';
case 'H': return 'D';
case 'V': return 'B';
case 'N': return 'N';
default: return c;
}
}
char complement_rna(char c)
{
switch(c) {
case 'A': return 'U';
case 'C': return 'G';
case 'G': return 'C';
case 'U': return 'A';
case 'W': return 'W';
case 'S': return 'S';
case 'M': return 'K';
case 'K': return 'M';
case 'R': return 'Y';
case 'Y': return 'R';
case 'B': return 'V';
case 'D': return 'H';
case 'H': return 'D';
case 'V': return 'B';
case 'N': return 'N';
default: return c;
}
}
// Return a probability distribution.
// Probability is distributed evenly between nucleotides in the iupac class 'c'
dvector
iupac_probability(char c)
{
dvector result(4, 0.0);
const char* str = iupac_class(c);
assert(str != 0);
int size = strlen(str);
assert(0 < size and size <= 4);
double a = 1.0 / size;
for (int i=0; i < size; ++i)
result[to_int(str[i])] = a;
return result;
}