Motif.cpp
18.5 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "Motif.h"
#include "Pool.h"
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <regex>
#include <sstream>
#include <thread>
using namespace boost::filesystem;
using namespace std;
struct recursive_directory_range {
typedef recursive_directory_iterator iterator;
recursive_directory_range(path p) : p_(p) {}
iterator begin() { return recursive_directory_iterator(p_); }
iterator end() { return recursive_directory_iterator(); }
path p_;
};
Motif::Motif(void) {}
Motif::Motif(const vector<Component>& v, string PDB) : comp(v), PDBID(PDB)
{
is_model_ = false;
reversed_ = false;
source_ = RNA3DMOTIF;
}
void Motif::build_from_desc(args_of_parallel_func arg_struct)
// void Motif::build_from_desc(path descfile, string rna, vector<Motif>& final_results)
{
path descfile = arg_struct.descfile;
string& rna = arg_struct.rna;
vector<Motif>& final_results = arg_struct.final_results;
mutex& posInsertionSites_access = arg_struct.posInsertionSites_mutex;
std::ifstream motif;
vector<vector<Component>> vresults;
string line;
string seq;
vector<string> component_sequences;
vector<string> bases;
int last;
char c = 'a';
char* prev = &c;
motif = std::ifstream(descfile.string());
getline(motif, line); // ignore "id: number"
getline(motif, line); // Bases: 866_G 867_G 868_G 869_G 870_U 871_A ...
boost::split(bases, line, [prev](char c) {
bool res = (*prev == ' ' or *prev == ':');
*prev = c;
return (c == ' ' and res);
}); // get a vector of 866_G, 867_G, etc...
seq = "";
last = stoi(bases[1].substr(0, bases[1].find('_')));
for (vector<string>::iterator b = bases.begin() + 1; b != bases.end() - 1; b++) {
char nt = b->substr(b->find('_') + 1, 1).back();
int pos = stoi(b->substr(0, b->find('_')));
if (pos - last > 5) { // finish this component and start a new one
component_sequences.push_back(seq);
seq = "";
} else if (pos - last == 2) {
seq += '.';
} else if (pos - last == 3) {
seq += "..";
} else if (pos - last == 4) {
seq += "...";
} else if (pos - last == 5) {
seq += "....";
}
seq += nt;
last = pos;
}
component_sequences.push_back(seq);
// Now component_sequences is a vector of sequences like {AGCGC, CGU..GUUU}
// identify components of length 1 or 2 to extend them to length 3
vector<uint> comp_of_size_1;
vector<uint> comp_of_size_2;
for (uint p = 0; p < component_sequences.size(); ++p) {
if (component_sequences[p].length() == 1) comp_of_size_1.push_back(p);
if (component_sequences[p].length() == 2) comp_of_size_2.push_back(p);
}
if (comp_of_size_1.size() or comp_of_size_2.size()) {
vector<vector<string>> motif_variants; // Will contain several component_sequences vectors according to the size where you extend too short components
component_sequences.clear(); // rebuild from scratch
motif_variants.push_back(component_sequences);
uint actual_comp = 0;
seq = "";
last = stoi(bases[1].substr(0, bases[1].find('_')));
for (vector<string>::iterator b = bases.begin() + 1; b < bases.end() - 1; b++) {
int pos = stoi(b->substr(0, b->find('_')));
char nt = b->substr(b->find('_') + 1, 1).back();
if (comp_of_size_1.size() and actual_comp == comp_of_size_1[0]) // we are on the first component of size 1
{
b--;
nt = b->substr(b->find('_') + 1, 1).back();
string seq1 = "";
seq1 += nt;
seq1 += "..";
string seq2 = ".";
seq2 += nt;
seq2 += ".";
string seq3 = "..";
seq3 += nt;
uint end = motif_variants.size(); // before to add the new ones
for (uint u = 0; u < end; ++u) {
motif_variants.push_back(motif_variants[u]); // copy 1 for seq2
motif_variants.back().push_back(seq2);
motif_variants.push_back(motif_variants[u]); // copy 2 for seq3
motif_variants.back().push_back(seq3);
motif_variants[u].push_back(seq1);
}
seq = "";
actual_comp++;
comp_of_size_1.erase(comp_of_size_1.begin()); // the first element has been processed, remove it
last = pos;
} else if (comp_of_size_2.size() and actual_comp == comp_of_size_2[0]) { // we are on the first component of size 2
b--;
nt = b->substr(b->find('_') + 1, 1).back();
b++; // skip the next nucleotide
char next = b->substr(b->find('_') + 1, 1).back();
last = stoi(b->substr(0, b->find('_')));
string seq1 = "";
seq1 += nt;
seq1 += next;
seq1 += ".";
string seq2 = ".";
seq2 += nt;
seq2 += next;
uint end = motif_variants.size(); // before to add the new one
for (uint u = 0; u < end; ++u) {
motif_variants.push_back(motif_variants[u]); // copy 1 for seq2
motif_variants.back().push_back(seq2);
motif_variants[u].push_back(seq1);
}
seq = "";
actual_comp++;
comp_of_size_2.erase(comp_of_size_2.begin()); // the first element has been processed, remove it
} else { // we are on a longer component
if (pos - last > 5) { // finish this component and start a new one
actual_comp++;
for (vector<string>& c_s : motif_variants) c_s.push_back(seq);
seq = "";
} else if (pos - last == 2) {
seq += '.';
} else if (pos - last == 3) {
seq += "..";
} else if (pos - last == 4) {
seq += "...";
} else if (pos - last == 5) {
seq += "....";
}
seq += nt;
last = pos;
}
}
for (auto c_s : motif_variants)
if (seq.length()) c_s.push_back(seq); // pushing the last one after iterating over the bases
// We need to search for the different positions where to insert the first component
for (auto c_s : motif_variants) {
vector<vector<Component>> new_results = find_next_ones_in(rna, 0, c_s);
vresults.insert(vresults.end(), new_results.begin(), new_results.end());
}
} else {
// No multiple motif variants : we serach in a single vector component_sequences
// We need to search for the different positions where to insert the first component
vresults = find_next_ones_in(rna, 0, component_sequences);
}
// Now create proper motifs with Motif class
for (vector<Component>& v : vresults) {
unique_lock<mutex> lock(posInsertionSites_access);
final_results.push_back(Motif(v, path(descfile).stem().string()));
lock.unlock();
}
}
void Motif::load_from_csv(string csv_line)
{
vector<string> tokens;
split(tokens, csv_line, boost::is_any_of(","));
if (csv_line.find(string("True")) != std::string::npos or csv_line.find(string("False")) != std::string::npos) { // This has been created by jar3d
atlas_id = tokens[0];
score_ = stoi(tokens[2]);
comp.push_back(Component(make_pair<int, int>(stoi(tokens[3]), stoi(tokens[4]))));
if (tokens[5] != "-") comp.push_back(Component(make_pair<int, int>(stoi(tokens[5]), stoi(tokens[6]))));
reversed_ = (tokens[1] == "True");
is_model_ = true;
PDBID = "";
source_ = RNAMOTIFATLAS;
} else { // this has been created by BayesPairing
score_ = stoi(tokens[1]);
// identify source:
if (tokens[0].find(string("rna3dmotif")) == std::string::npos) {
is_model_ = true;
PDBID = "";
source_ = RNAMOTIFATLAS;
atlas_id = tokens[0];
} else {
is_model_ = false;
PDBID = tokens[0];
source_ = RNA3DMOTIF;
atlas_id = "";
}
uint i = 2;
while (i < tokens.size()) {
comp.push_back(Component(make_pair<int, int>(stoi(tokens[i]), stoi(tokens[i + 1]))));
i += 2;
}
}
}
string Motif::pos_string(void) const
{
stringstream s;
s << atlas_id << " ( ";
for (auto c : comp) s << c.pos.first << '-' << c.pos.second << ' ';
s << ')';
return s.str();
}
string Motif::get_identifier(void) const
{
switch (source_) {
case RNAMOTIFATLAS: return atlas_id; break;
default: return PDBID;
}
}
vector<vector<Component>> Motif::find_next_ones_in(string rna, uint offset, vector<string> vc)
{
pair<uint, uint> pos;
vector<vector<Component>> results;
vector<vector<Component>> next_ones;
vector<string> next_seqs;
regex c(vc[0]);
// cout << "\t\t>Searching " << vc[0] << " in " << rna << endl;
if (vc.size() > 1) {
if (regex_search(rna, c)) {
if (vc.size() > 2)
next_seqs = vector<string>(&vc[1], &vc[vc.size() - 1]);
else
next_seqs = vector<string>(1, vc.back());
// Pour chacun des matches
for (sregex_iterator i = sregex_iterator(rna.begin(), rna.end(), c); i != sregex_iterator(); ++i) {
smatch match = *i;
pos.first = match.position() + offset;
pos.second = pos.first + match.length() - 1;
// cout << "\t\t>Inserting " << vc[0] << " in [" << pos.first << ',' << pos.second << "]" << endl;
if (pos.second - offset + 5 >= rna.length()) {
// cout << "\t\t... but we cannot place the next components : Ignored." << endl;
continue;
}
next_ones = find_next_ones_in(rna.substr(pos.second - offset + 5), pos.second + 5, next_seqs);
if (!next_ones.size()) {
// cout << "\t\t... but we cannot place the next components : Ignored." << endl;
continue;
}
// cout << endl;
for (vector<Component> v : next_ones) // Pour chacune des combinaisons suivantes
{
// Combiner le match et la combinaison suivante
vector<Component> r;
r.push_back(Component(pos));
for (Component& c : v) r.push_back(c);
results.push_back(r);
}
}
}
} else {
if (regex_search(rna, c)) {
// Pour chacun des matches
for (sregex_iterator i = sregex_iterator(rna.begin(), rna.end(), c); i != sregex_iterator(); ++i) {
smatch match = *i;
pos.first = match.position() + offset;
pos.second = pos.first + match.length() - 1;
// cout << "\t\t>Inserting " << vc[0] << " in [" << pos.first << ',' << pos.second << "]" << endl;
// Combiner le match et la combinaison suivante
vector<Component> r;
r.push_back(Component(pos));
results.push_back(r);
}
}
}
return results;
}
char Motif::is_valid_DESC(const string& descfile)
{
// /!\ returns 0 iff no errors
std::ifstream motif;
string line;
vector<string> bases;
char c = 'a';
char* prev = &c;
motif = std::ifstream(descfile);
getline(motif, line); // ignore "id: number"
getline(motif, line); // Bases: 866_G 867_G 868_G 869_G 870_U 871_A ...
boost::split(bases, line, [prev](char c) {
bool res = (*prev == ' ' or *prev == ':');
*prev = c;
return (c == ' ' and res);
}); // get a vector of 866_G, 867_G, etc...
for (vector<string>::iterator b = (bases.begin() + 1); b != (bases.end() - 1); b++) {
char nt = b->substr(b->find('_') + 1, 1).back();
int pos = stoi(b->substr(0, b->find('_')));
if (string(1, nt).find_first_not_of("ACGU") != string::npos) return nt;
if (pos <= 0) return '-';
}
while (getline(motif, line)) {
uint slash = line.find('/');
string interaction(line.substr(slash - 1, 3));
string b1 = line.substr(line.find('(') + 1, 8); // The first base (position_nucleotide)
string temp = line.substr(slash + 1);
string b2 = temp.substr(temp.find('(') + 1, 8); // The second base (position_nucleotide)
b1.erase(remove(b1.begin(), b1.end(), ' '), b1.end());
b2.erase(remove(b2.begin(), b2.end(), ' '), b2.end());
int p1 = stoi(b1.substr(0, b1.find('_')));
int p2 = stoi(b2.substr(0, b2.find('_')));
if ((p2 - p1 != 1) and !interaction.compare("C/C")) return 'b';
if ((p2 - p1 < 4) and (!interaction.compare("+/+") or !interaction.compare("-/-"))) return 'l';
}
return 0;
}
vector<Motif> load_desc_folder(const string& path, const string& rna, bool verbose)
{
vector<Motif> posInsertionSites;
mutex posInsertionSites_access;
Pool pool;
int errors = 0;
int accepted = 0;
int inserted = 0;
// int num_threads = thread::hardware_concurrency();
int num_threads = 2;
vector<thread> thread_pool;
if (!exists(path)) {
cerr << "Hmh, i can't find that folder: " << path << endl;
return posInsertionSites;
} else {
if (verbose) cout << "loading DESC motifs from " << path << "..." << endl;
}
for (int i = 0; i < num_threads; i++) thread_pool.push_back(thread(&Pool::infinite_loop_func, &pool));
char error;
for (auto it : recursive_directory_range(path)) { // Add every .desc file to the queue (iff valid)
if ((error = Motif::is_valid_DESC(it.path().string()))) {
if (verbose) {
cerr << "\t>Ignoring motif " << it.path().stem();
switch (error) {
case '-': cerr << ", some nucleotides have a negative number..."; break;
case 'l': cerr << ", hairpin (terminal) loops must be at least of size 3 !"; break;
case 'b': cerr << ", backbone link between non-consecutive residues ?"; break;
default: cerr << ", use of an unknown nucleotide " << error;
}
cerr << endl;
}
errors++;
continue;
}
accepted++;
if (is_desc_insertible(it.path().string(), rna, verbose)) {
args_of_parallel_func args(it.path(), rna, posInsertionSites, posInsertionSites_access);
inserted++;
pool.push(bind(Motif::build_from_desc, args));
// Motif::build_from_desc(it.path(), rna, posInsertionSites);
}
}
pool.done();
for (unsigned int i = 0; i < thread_pool.size(); i++) thread_pool.at(i).join();
if (verbose)
cout << "Inserted " << inserted << " motifs on " << accepted + errors << " (" << errors << " ignored motifs)" << endl;
return posInsertionSites;
}
vector<Motif> load_csv(const string& path)
{
vector<Motif> posInsertionSites;
std::ifstream motifs;
string line;
motifs = std::ifstream(path);
getline(motifs, line); // skip header
while (getline(motifs, line)) {
posInsertionSites.push_back(Motif());
posInsertionSites.back().load_from_csv(line);
}
return posInsertionSites;
}
bool is_desc_insertible(const string& descfile, const string& rna, bool verbose)
{
std::ifstream motif;
string line;
string seq;
vector<string> bases;
int last;
char c = 'a';
char* prev = &c;
motif = std::ifstream(descfile);
getline(motif, line); // ignore "id: number"
getline(motif, line); // Bases: 866_G 867_G 868_G 869_G 870_U 871_A ...
boost::split(bases, line, [prev](char c) {
bool res = (*prev == ' ' or *prev == ':');
*prev = c;
return (c == ' ' and res);
}); // get a vector of 866_G, 867_G, etc...
seq = "";
last = stoi(bases[1].substr(0, bases[1].find('_')));
for (vector<string>::iterator b = (bases.begin() + 1); b != (bases.end() - 1); b++) {
char nt = b->substr(b->find('_') + 1, 1).back();
int pos = stoi(b->substr(0, b->find('_')));
if (pos - last > 5) { // finish this component and start a new one
seq += ".{5,}";
} else if (pos - last == 2) {
seq += ".";
} else if (pos - last == 3) {
seq += "..";
} else if (pos - last == 4) {
seq += "...";
} else if (pos - last == 5) {
seq += "....";
}
seq += nt; // pos - last == 1 in particular
last = pos;
}
smatch m;
regex e(seq);
if (regex_search(rna, m, e)) {
if (verbose)
cout << "\t>Motif " << boost::filesystem::path(descfile).stem() << " \t" << seq << "\tcan be inserted " << endl;
return true;
} else {
// if (verbose) cout << "Ignoring motif " << descfile.substr(0, descfile.find(".desc")) << " \t" << seq << endl;
return false;
}
}
bool operator==(const Component& c1, const Component& c2)
{
if (c1.pos.first != c2.pos.first) return false;
if (c1.pos.second != c2.pos.second) return false;
return true;
}
bool operator!=(const Component& c1, const Component& c2) { return not(c1 == c2); }
bool operator==(const Motif& m1, const Motif& m2)
{
if (m1.get_identifier() != m2.get_identifier()) return false;
if (m1.score_ != m2.score_) return false;
if (m1.reversed_ != m2.reversed_) return false;
for (uint i = 0; i < m1.comp.size(); i++)
if (m1.comp[i] != m2.comp[i]) return false;
return true;
}
bool operator!=(const Motif& m1, const Motif& m2) { return not(m1 == m2); }