SecondaryStructure.cpp
9.81 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "SecondaryStructure.h"
#include "MOIP.h"
#include <algorithm>
#include <boost/format.hpp>
using std::abs;
using std::cout;
using std::endl;
static const double PRECISION(0.0001);
SecondaryStructure::SecondaryStructure() {}
SecondaryStructure::SecondaryStructure(const RNA& rna)
: objective_scores_(vector<double>(2)), n_(rna.get_RNA_length()), nBP_(0), rna_(rna)
{
is_empty_structure = false;
}
SecondaryStructure::SecondaryStructure(bool empty) : rna_(RNA()) { is_empty_structure = empty; }
string SecondaryStructure::to_DBN(void) const
{
string res = string(n_, '.');
int pklevel = 0;
bool crosses;
char start, end;
vector<vector<pair<uint, uint>>> noncrossingSG; // Non crossing sets of edges, also called pseudoknot "levels"
vector<uint> possible_sets;
// get the non crossing subsets
for (size_t i = 0; i < nBP_; i++) {
possible_sets.clear();
for (size_t j = 0; j < noncrossingSG.size(); j++) {
// check if basepairs_[i] crosses the subset noncrossingSG[j]
crosses = false;
for (size_t k = 0; k < noncrossingSG[j].size(); k++) {
if (
((basepairs_[i].first < noncrossingSG[j][k].first) and (basepairs_[i].second < noncrossingSG[j][k].second) and (basepairs_[i].second > noncrossingSG[j][k].first)) or
((basepairs_[i].first > noncrossingSG[j][k].first) and (basepairs_[i].second > noncrossingSG[j][k].second) and (noncrossingSG[j][k].second > basepairs_[i].first))) {
crosses = true;
break;
}
}
// if not, consider possible to add basepairs_[i] to noncrossingSG[j]
if (!crosses) possible_sets.push_back(j);
}
if (possible_sets.size()) {
// add it to the largest possible subset (this is a heuristic, the real problem is a k-coloration NP hard problem)
uint max = 0;
uint pos = 0;
for (size_t k = 0; k < possible_sets.size(); k++)
if (max < noncrossingSG[possible_sets[k]].size()) {
max = noncrossingSG[possible_sets[k]].size();
pos = possible_sets[k];
}
noncrossingSG[pos].push_back(basepairs_[i]);
} else {
// If basepairs_[i] has not been inserted in any subset, create a new one
noncrossingSG.push_back(vector<pair<uint, uint>>(1, basepairs_[i]));
}
}
// get the sizes of the non crossing subsets
vector<uint> SGsizes(noncrossingSG.size(), 0);
for (size_t j = 0; j < noncrossingSG.size(); j++) SGsizes[j] = noncrossingSG[j].size();
// Process the subsets from largest to thinest
while (noncrossingSG.size()) {
// Find the largest non-crossing subset
uint j = std::distance(SGsizes.begin(), std::max_element(SGsizes.begin(), SGsizes.end()));
// Apply basepairs in the output string
for (uint i = 0; i < noncrossingSG[j].size(); i++) {
switch (pklevel) {
case 0: start = '(', end = ')'; break;
case 1: start = '[', end = ']'; break;
case 2: start = '{', end = '}'; break;
case 3: start = '<', end = '>'; break;
case 4: start = 'A', end = 'a'; break;
case 5: start = 'B', end = 'b'; break;
case 6: start = 'C', end = 'c'; break;
case 7: start = 'D', end = 'd'; break;
case 8: start = 'E', end = 'e'; break;
case 9: start = 'F', end = 'f'; break;
default: start = '|', end = '|';
}
res[noncrossingSG[j][i].first] = start;
res[noncrossingSG[j][i].second] = end;
}
pklevel++;
// Remove the processed subset from the vector
noncrossingSG.erase(std::remove(noncrossingSG.begin(), noncrossingSG.end(), noncrossingSG[j]), noncrossingSG.end());
SGsizes.erase(SGsizes.begin() + j, SGsizes.begin() + j + 1);
}
return res;
}
string SecondaryStructure::to_string(void) const
{
string s;
s += to_DBN();
for (const Motif& m : motif_info_) s += " + " + m.get_identifier();
s += "\t" + boost::str(boost::format("%.7f") % objective_scores_[0]) + "\t" +
boost::str(boost::format("%.7f") % objective_scores_[1]);
return s;
}
void SecondaryStructure::set_basepair(uint i, uint j)
{
nBP_++;
pair<uint, uint> bp;
bp.first = i;
bp.second = j;
basepairs_.push_back(bp);
}
void SecondaryStructure::insert_motif(const Motif& m) { motif_info_.push_back(m); }
void SecondaryStructure::print(void) const
{
cout << endl;
cout << '\t' << rna_.get_seq() << endl;
cout << '\t' << to_string() << endl;
for (const Motif& m : motif_info_) {
uint i = 0;
cout << '\t';
for (auto c : m.comp) {
while (i != c.pos.second + 1) {
i++;
if (i < c.pos.first + 1)
cout << " ";
else
cout << '-';
}
}
while (i < n_) {
cout << " ";
i++;
}
cout << "\t" << m.pos_string() << endl;
}
cout << endl;
}
void SecondaryStructure::sort(void)
{
std::sort(basepairs_.begin(), basepairs_.end(), basepair_sorter);
std::sort(motif_info_.begin(), motif_info_.end(), motif_sorter);
}
bool basepair_sorter(pair<uint, uint>& i, pair<uint, uint>& j)
{
if (i.first < j.first) return true;
if (i.first == j.first and i.second < j.second) return true;
return false;
}
bool motif_sorter(Motif& m1, Motif& m2)
{
if (m1.get_identifier().compare(m2.get_identifier()) < 0) return true;
return false;
}
bool operator>(const SecondaryStructure& s1, const SecondaryStructure& s2)
{
double s11 = s1.get_objective_score(1);
double s12 = s1.get_objective_score(2);
double s21 = s2.get_objective_score(1);
double s22 = s2.get_objective_score(2);
bool obj1 = false, obj2 = false, strict1 = false, strict2 = false;
if (s11 - s21 > MOIP::precision_) {
strict1 = true;
obj1 = true;
} else if (abs(s11 - s21) < MOIP::precision_) {
obj1 = true;
}
if (s12 - s22 > MOIP::precision_) {
strict2 = true;
obj2 = true;
} else if (abs(s12 - s22) < MOIP::precision_) {
obj2 = true;
}
if (obj1 && obj2 && (strict1 || strict2)) {
return true;
}
return false;
}
bool operator<(const SecondaryStructure& s1, const SecondaryStructure& s2)
{
double s11 = s1.get_objective_score(1);
double s12 = s1.get_objective_score(2);
double s21 = s2.get_objective_score(1);
double s22 = s2.get_objective_score(2);
bool obj1 = false, obj2 = false, strict1 = false, strict2 = false;
if (MOIP::precision_ < s21 - s11) {
strict1 = true;
obj1 = true;
} else if (abs(s11 - s21) < MOIP::precision_) {
obj1 = true;
}
if (MOIP::precision_ < s22 - s12) {
strict2 = true;
obj2 = true;
} else if (abs(s12 - s22) < MOIP::precision_) {
obj2 = true;
}
if (obj1 && obj2 && (strict1 || strict2)) {
return true;
}
return false;
}
bool operator>=(const SecondaryStructure& s1, const SecondaryStructure& s2)
{
double s11 = s1.get_objective_score(1);
double s12 = s1.get_objective_score(2);
double s21 = s2.get_objective_score(1);
double s22 = s2.get_objective_score(2);
bool obj1 = false, obj2 = false, strict1 = false, strict2 = false;
if (s11 - s21 > MOIP::precision_) {
strict1 = true;
obj1 = true;
} else if (abs(s11 - s21) < MOIP::precision_) {
obj1 = true;
}
if (s12 - s22 > MOIP::precision_) {
strict2 = true;
obj2 = true;
} else if (abs(s12 - s22) < MOIP::precision_) {
obj2 = true;
}
if ((obj1 && obj2 && (strict1 || strict2)) || ((abs(s11 - s21) < MOIP::precision_ && abs(s12 - s22) < MOIP::precision_))) {
return true;
}
return false;
}
bool operator<=(const SecondaryStructure& s1, const SecondaryStructure& s2)
{
double s11 = s1.get_objective_score(1);
double s12 = s1.get_objective_score(2);
double s21 = s2.get_objective_score(1);
double s22 = s2.get_objective_score(2);
bool obj1 = false, obj2 = false, strict1 = false, strict2 = false;
if (MOIP::precision_ < s21 - s11) {
strict1 = true;
obj1 = true;
} else if (abs(s11 - s21) < MOIP::precision_) {
obj1 = true;
}
if (MOIP::precision_ < s22 - s12) {
strict2 = true;
obj2 = true;
} else if (abs(s12 - s22) < MOIP::precision_) {
obj2 = true;
}
if ((obj1 && obj2 && (strict1 || strict2)) || ((abs(s11 - s21) < MOIP::precision_ && abs(s12 - s22) < MOIP::precision_))) {
return true;
}
return false;
}
bool operator==(const SecondaryStructure& s1, const SecondaryStructure& s2)
{
// Checks wether the secondary structures are exactly the same, including the inserted motifs.
// fast checks to refute the equality
if (s1.get_objective_score(1) != s2.get_objective_score(1)) return false;
if (s1.get_objective_score(2) != s2.get_objective_score(2)) return false;
if (s1.get_n_motifs() != s2.get_n_motifs()) return false;
if (s1.get_n_bp() != s2.get_n_bp()) return false;
// Deep checking
for (uint i = 0; i < s1.get_n_bp(); i++)
if (s1.basepairs_[i] != s2.basepairs_[i]) return false;
for (uint i = 0; i < s1.get_n_motifs(); i++)
if (s1.motif_info_[i] != s2.motif_info_[i]) return false;
return true;
}
bool operator!=(const SecondaryStructure& s1, const SecondaryStructure& s2)
{
// Checks wether the secondary structures are different, including the inserted motifs.
return not(s1 == s2);
}