utils.cpp
4.24 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
#include "utils.h"
#include <sys/stat.h>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <numeric>
int find_bp(std::vector < std::pair < unsigned int, unsigned int > > listBp, std::pair< unsigned int, unsigned int > p)
{
std::vector < std::pair< unsigned int, unsigned int > >::iterator it = std::find(listBp.begin(), listBp.end(), p);
int r = -1;
if(it != listBp.end())
r = std::distance(listBp.begin(), it);
return r;
}
std::vector < unsigned int > find_bp_with_i (std::vector < std::pair < unsigned int, unsigned int > > listBp, unsigned int i)
{
std::vector < unsigned int > res = std::vector< unsigned int > ();
for(unsigned int j = 0; j < listBp.size(); j++) {
if(listBp[j].first == i or listBp[j].second == i)
res.push_back(j);
}
return res;
}
std::vector< int > find_bp_with_i(
std::vector < std::pair < std::pair < uint, uint >, std::pair < uint, uint > > > listBp,
std::pair < uint, uint > i) {
std::vector < int > res;
for(unsigned int j = 0; j < listBp.size(); j++) {
if(listBp[j].first == i or listBp[j].second == i)
res.push_back(j);
}
return res;
}
bool find_i_at_first(std::vector < std::pair < unsigned int, unsigned int > > v, unsigned int i )
{
bool res = false;
for(size_t j = 0, size = v.size(); j != size and !res; j++) {
if(v[j].first == i)
res = true;
}
return res;
}
bool find_i_at_second(std::vector < std::pair < unsigned int, unsigned int > > v, unsigned int i )
{
bool res = false;
for(size_t j = 0, size = v.size(); j != size and !res; j++) {
if(v[j].second == i)
res = true;
}
return res;
}
int find_i_at_first_int(std::vector < std::pair < unsigned int, unsigned int > > v, unsigned int i )
{
int res = -1;
for(size_t j = 0, size = v.size(); j != size and res == -1; j++) {
if(v[j].first == i)
res = j;
}
return res;
}
int find_i_at_second_int(std::vector < std::pair < unsigned int, unsigned int > > v, unsigned int i )
{
int res = -1;
for(size_t j = 0, size = v.size(); j != size and res == -1; j++) {
if(v[j].second == i)
res = j;
}
return res;
}
bool app(const char c1, const char c2)
{
std::stringstream ss;
ss << c1 << c2;
std::string bp;
ss >> bp;
if (bp.compare("AU") != 0 and bp.compare("UA") != 0 and
bp.compare("UG") != 0 and bp.compare("GU") != 0 and
bp.compare("GC") != 0 and bp.compare("CG") != 0)
return false;
return true;
}
// Check if a string is an integer >= 0 and <= 100
bool checkIC(std::string ic) {
bool res = true;
int e;
try {
e = std::stoi(ic);
if (e < 0 or e > 100)
res = false;
} catch (std::string e) {
throw (std::string("Invalid confidence index."));
}
return res;
}
/* return 1 if c1 dominates c2,
* return 0 if c1 is not comparable to c2
* return -1 if c1 is dominated by c2 */
int dominate(std::vector < float > c1, std::vector < float > c2) {
/* first objective : max constraint
* second objective : max compatibility
* third objective : min energy
* fourth objective : max probing data score */
std::vector < int > strict = std::vector < int > (c1.size(), 0);
int dominate = 0;
for(size_t i = 0, size = c1.size(); i < size; i++) {
if (i == 2) { // min energy
if (c1[i] < c2[i])
strict[i] = 1;
else if (c1[i] > c2[i])
strict[i] = -1;
} else { // max
if (c1[i] > c2[i])
strict[i] = 1;
else if (c1[i] < c2[i])
strict[i] = -1;
}
}
int sum = std::accumulate(strict.begin(), strict.end(),0);
if (sum == int(c1.size())){
dominate = 1;
} else if (sum == -(int(c1.size()))){
dominate = -1;
}
return dominate;
}
std::string join(const std::vector < std::string >& v, std::string d) {
size_t size;
std::string s = "";
s.clear();
for (size_t i = 0, size = v.size(); i != size; i++) {
s += v[i];
if (i != v.size() - 1){
s += d;
}
}
return s;
}