/
move_set.cpp
745 lines (628 loc) · 19.9 KB
/
move_set.cpp
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <map>
#include <algorithm>
extern "C" {
#include "pair_mat.h"
#include "fold.h"
}
#include "move_set.h"
#include "RNAlandmap.h"
#include "hash_util.h"
// ############################## DECLARATION #####################################
// private functions & declarations
static int cnt_move = 0;
int count_move() {return cnt_move;}
// array for energy_of_move
short *min_pt;
// compatible base pair?
inline bool compat(char a, char b);
// done with all structures along the way to deepest
int update_deepest(encoded &enc, float &deepest, short *pt, degen °, bool verbose);
// if the base is lone
inline bool lone_base(short *pt, int i);
// try insert base pair (i,j)
inline bool try_insert(short *pt, const char *seq, int i, int j);
// ############################## IMPLEMENTATION #####################################
// reads a line no matter how long
char* my_getline(FILE *fp)
{
char s[512], *line, *cp;
line = NULL;
do {
if(fgets(s, 512, fp) == NULL) break;
cp = strchr(s, '\n');
if(cp != NULL) *cp = '\0';
if(line == NULL) line = (char *) calloc(strlen(s) + 1, sizeof(char));
else line = (char *) realloc(line, strlen(s) + strlen(line) + 1);
strcat (line, s);
} while (cp == NULL);
return (line);
}
// compatible base pair?
inline bool compat(char a, char b) {
if (a=='A' && b=='U') return true;
if (a=='C' && b=='G') return true;
if (a=='G' && b=='U') return true;
if (a=='U' && b=='A') return true;
if (a=='G' && b=='C') return true;
if (a=='U' && b=='G') return true;
// and with T's
if (a=='A' && b=='T') return true;
if (a=='T' && b=='A') return true;
if (a=='G' && b=='T') return true;
if (a=='T' && b=='G') return true;
return false;
}
// create encoded structure
encoded *encode_seq(const char* seq)
{
encoded *res;
res = (encoded*) space(sizeof(encoded));
// inicialize encode_sequence
make_pair_matrix();
res->seq = seq;
res->pt = NULL;
res->s0 = encode_sequence(seq, 0);
res->s1 = encode_sequence(seq, 1);
res->bp_left = 0;
res->bp_right = 0;
res->bp_left2 = 0;
res->bp_right2 = 0;
return res;
}
void encode_str(encoded *enc, const char *str)
{
if (enc->pt) free(enc->pt);
enc->pt = make_pair_table(str);
}
inline void forget(encoded &enc)
{
enc.bp_left=0;
enc.bp_right=0;
enc.bp_left2=0;
enc.bp_right2=0;
}
void free_encode(encoded *enc)
{
if (enc) {
if (enc->s0) free(enc->s0);
if (enc->s1) free(enc->s1);
if (enc->pt) free(enc->pt);
free(enc);
}
}
inline void do_move(short *pt, int bp_left, int bp_right)
{
// delete
if (bp_left<0) {
pt[-bp_left]=0;
pt[-bp_right]=0;
} else { // insert
pt[bp_left]=bp_right;
pt[bp_right]=bp_left;
}
}
inline void do_moves(encoded &enc, bool first = true, bool second = true)
{
if (first && enc.bp_left != 0) {
do_move(enc.pt, enc.bp_left, enc.bp_right);
}
if (second && enc.bp_left2 != 0) {
do_move(enc.pt, enc.bp_left2, enc.bp_right2);
}
}
inline void undo_moves(encoded &enc, bool first = true, bool second = true)
{
if (second && enc.bp_left2 != 0) {
do_move(enc.pt, -enc.bp_left2, -enc.bp_right2);
}
if (first && enc.bp_left != 0) {
do_move(enc.pt, -enc.bp_left, -enc.bp_right);
}
forget(enc);
}
inline void undo_move(short *pt, int bp_left, int bp_right)
{
// do insert = undo delete
do_move(pt, -bp_left, -bp_right);
}
// done with all structures along the way to deepest
int update_deepest(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose)
{
// debug
/*if (deg.opt->f_point) {
fprintf(stderr, "UD: %s %.2f (%d, %d) (%d, %d)\n", pt_to_str(enc.pt).c_str(), deepest/100.0, enc.bp_left, enc.bp_right, enc.bp_left2, enc.bp_right2);
}*/
int tmp_en;
if (deg.opt->EOM) {
tmp_en = deg.current + energy_of_move_pt(enc.pt, enc.s0, enc.s1, enc.bp_left, enc.bp_right);
do_moves(enc, true, false);
if (enc.bp_left2 != 0) {
tmp_en += energy_of_move_pt(enc.pt, enc.s0, enc.s1, enc.bp_left2, enc.bp_right2);
do_moves(enc, false, true);
}
} else {
do_moves(enc);
tmp_en = energy_of_structure_pt(enc.seq, enc.pt, enc.s0, enc.s1, 0);
}
// our function
bool res = 0;
if (deg.opt->f_point) {
res = deg.opt->f_point(enc.pt, tmp_en);
undo_moves(enc);
return res;
}
// default behaviour
if (tmp_en <= deepest) {
deepest = tmp_en;
copy_arr(min_pt, enc.pt);
}
//printf("%s %7.2f\n", pt_to_str(enc.pt).c_str(), tmp_en/100.0);
undo_moves(enc);
return res;
}
// deletions move set
int deletions(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose)
{
int cnt = 0;
short *pt = enc.pt;
int len = pt[0];
for (int i=1; i<=len; i++) {
if (pt[i]>pt[pt[i]]) { // '('
enc.bp_left=-i;
enc.bp_right=-pt[i];
//if nolp enabled, make (maybe) 2nd delete
if (deg.opt->noLP) {
int lone = -1;
if (lone_base(pt, i-1)) lone=i-1;
else if (lone_base(pt, i+1)) lone=i+1;
else if (lone_base(pt, pt[i]-1)) lone=pt[i]-1;
else if (lone_base(pt, pt[i]+1)) lone=pt[i]+1;
// check
if (lone != -1 && (pt[lone]==0 || pt[pt[lone]]==0)) {
fprintf(stderr, "WARNING: pt[%d(or %d)]!=\'.\'", lone, pt[lone]);
}
if (lone != -1) {
enc.bp_left2=-lone-1;
enc.bp_right2=-pt[lone]-1;
}
if (!lone_base(pt, pt[lone]-1) && !lone_base(pt, pt[lone]+1)) {
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
} else { // nolp not enabled
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
}
}
return cnt;
}
// insertions move set
int insertions(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose)
{
int cnt = 0;
short *pt = enc.pt;
int len = pt[0];
for (int i=1; i<=len; i++) {
if (pt[i]==0) {
for (int j=i+1; j<=len; j++) {
// end if found closing bracket
if (pt[j]!=0 && pt[j]<j) break; //')'
if (pt[j]!=0 && pt[j]>j) { //'('
j = pt[j];
continue;
}
// if conditions are met, do insert
if (try_insert(pt, enc.seq, i, j)) {
enc.bp_left=i;
enc.bp_right=j;
if (deg.opt->noLP) {
// if lone bases occur, try inserting one another base
if (lone_base(pt, i) || lone_base(pt, j)) {
// inside
if (try_insert(pt, enc.seq, i+1, j-1)) {
enc.bp_left2=i+1;
enc.bp_right2=j-1;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
} else //outside
if (try_insert(pt, enc.seq, i-1, j+1)) {
enc.bp_left2=i-1;
enc.bp_right2=j+1;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
} else {
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
} else {
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
}
}
}
}
return cnt;
}
//shift move set
int shifts(encoded &enc, int &deepest, short *min_pt, degen °, bool verbose)
{
int cnt = 0;
int brack_num = 0;
short *pt = enc.pt;
int len = pt[0];
for (int i=1; i<=len; i++) {
if (pt[i]!=0 && pt[i]>i) { //'('
int j=pt[i];
// outer switch left
if (verbose) fprintf(stderr, "%2d bracket %2d position, outer switch left\n", brack_num+1, i);
for (int k=i-1; k>0; k--) {
if (pt[k]!=0 && pt[k]>k/*'('*/) break;
if (pt[k]!=0 && pt[k]<k/*')'*/) {
k = pt[k];
continue;
}
// checks
if (pt[k]!=0) {
fprintf(stderr, "WARNING: \'%c\'should be \'.\' at pos %d!\n", pt[k], k);
}
// switch (i,j) to (k,j)
if (j-k>MINGAP && compat(enc.seq[k-1], enc.seq[j-1])) {
enc.bp_left=-i;
enc.bp_right=-j;
enc.bp_left2=k;
enc.bp_right2=j;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
// switch (i,j) to (k,i)
if (i-k>MINGAP && compat(enc.seq[i-1], enc.seq[k-1])) {
enc.bp_left=-i;
enc.bp_right=-j;
enc.bp_left2=k;
enc.bp_right2=i;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
}
// outer switch right
if (verbose) fprintf(stderr, "%2d bracket %2d position, outer switch right\n", brack_num+1, i);
for (int k=j+1; k<=len; k++) {
if (pt[k]!=0 && pt[k]<k/*')'*/) break;
if (pt[k]!=0 && pt[k]>k/*'('*/) {
k = pt[k];
continue;
}
// check
if (pt[k]!=0) {
fprintf(stderr, "WARNING: \'%c\'should be \'.\' at pos %d!\n", pt[k], k);
}
// switch (i,j) to (i,k)
if (k-i>MINGAP && compat(enc.seq[i-1], enc.seq[k-1])) {
enc.bp_left=-i;
enc.bp_right=-j;
enc.bp_left2=i;
enc.bp_right2=k;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
// switch (i,j) to (j,k)
if (k-j>MINGAP && compat(enc.seq[j-1], enc.seq[k-1])) {
enc.bp_left=-i;
enc.bp_right=-j;
enc.bp_left2=j;
enc.bp_right2=k;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
}
if (verbose) fprintf(stderr, "%2d bracket %2d position, inner switch\n", brack_num+1, i);
// inner switch
for (int k=i+1; k<j; k++) {
// jump to end of the sub-bracketing
if (pt[k]!=0 && pt[k]>k/*'('*/) {
k=pt[k];
continue;
}
// left switch (i,j) to (k,j)
if (j-k>MINGAP && compat(enc.seq[k-1], enc.seq[j-1])) {
enc.bp_left=-i;
enc.bp_right=-j;
enc.bp_left2=k;
enc.bp_right2=j;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
// right switch (i,j) to (i,k)
if (k-i>MINGAP && compat(enc.seq[i-1], enc.seq[k-1])) {
enc.bp_left=-i;
enc.bp_right=-j;
enc.bp_left2=i;
enc.bp_right2=k;
cnt += update_deepest(enc, deepest, min_pt, deg, verbose);
// in case useFirst is on and structure is found, end
if (deg.opt->first && cnt > 0) return cnt;
}
} // end inner switch for
brack_num++;
} // end if (pt[i]=='(')
} // end for in switches
return cnt;
}
void copy_arr(short *dest, short *src)
{
if (!src || !dest) {
fprintf(stderr, "Empty pointer in copying\n");
return;
}
memcpy(dest, src, sizeof(short)*(src[0]+1));
}
short *allocopy(short *src)
{
short *res = (short*) space(sizeof(short)*(src[0]+1));
copy_arr(res, src);
return res;
}
void erase_set(set<short*, setcomp> &_set)
{
for (set<short*, setcomp>::iterator it=_set.begin(); it!=_set.end(); it++) {
if (*it) free(*it);
}
_set.clear();
}
// move to deepest (or first) neighbour
int move_set(encoded &enc, int &deepest, degen °)
{
// count how many times called
cnt_move++;
// count better neighbours
int cnt = 0;
// deepest descent
deg.current = deepest;
short *min_pt = allocopy(enc.pt);
if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n start of MS:\n %s %d\n\n", pt_to_str(enc.pt).c_str(), deepest);
// if using first dont do all of them
bool end = false;
// insertions
if (!end) cnt += insertions(enc, deepest, min_pt, deg, deg.opt->verbose_lvl>3);
if (deg.opt->first && cnt>0) end = true;
fprintf(stderr, "\n");
// deletions
if (!end) cnt += deletions(enc, deepest, min_pt, deg, deg.opt->verbose_lvl>3);
if (deg.opt->first && cnt>0) end = true;
fprintf(stderr, "\n");
// shifts (only if enabled + noLP disabled)
if (!end && deg.opt->shift && !deg.opt->noLP) {
cnt += shifts(enc, deepest, min_pt, deg, deg.opt->verbose_lvl>3);
if (deg.opt->first && cnt>0) end = true;
}
if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n %s\n %s\n", enc.seq, pt_to_str(min_pt).c_str());
// if degeneracy occurs, solve it!
if (!end && deg.unprocessed.size()>0) {
deg.processed.insert(allocopy(enc.pt));
// take first
if (enc.pt) free(enc.pt);
enc.pt = (*deg.unprocessed.begin());
deg.unprocessed.erase(deg.unprocessed.begin());
if (deg.opt->minh>0.1001) {
deepest = energy_of_structure_pt(enc.seq, enc.pt, enc.s0, enc.s1, 0);
}
cnt += move_set(enc, deepest, deg);
} else {
copy_arr(enc.pt, min_pt);
}
free(min_pt);
// resolve degeneracy in local minima
if (deg.processed.size()>0) {
deg.processed.insert(enc.pt);
enc.pt = *deg.processed.begin();
deg.processed.erase(deg.processed.begin());
erase_set(deg.processed);
}
return cnt;
}
// browse neighbours without degeneracy (almost the same as move_set) assume deg.first = true
bool browse_neighs(encoded &enc, int &energy, degen °, int &saddle_en)
{
// count how many times called
cnt_move++;
// unused, just for necessity of functions
short *min_pt = allocopy(enc.pt);
// used
deg.current = energy;
// did we just find escape from basin?
bool escape = false;
//if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n browse neighs:\n %s %d\n\n", pt_to_str(enc.pt).c_str(), energy);
escape = insertions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
//if (deg.opt->verbose_lvl>3) fprintf(stderr, "\n");
// deletions
if (!escape) escape = deletions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
// shifts (only if enabled + noLP disabled)
if (deg.opt->shift && !deg.opt->noLP) {
if (!escape) escape = shifts(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
}
if (escape) saddle_en = deg.current;
free(min_pt);
return escape;
}
// stuff for find_equal_energies
int degen_energy;
set<short*, setcomp> degen_done;
set<short*, setcomp> degen_undone;
bool degen_saddle;
bool direct;
// I dont like it at all, but its easy to code
// global things from RNAlandmap.cpp
extern set<set<int> > numbers;
extern set<int> saddles;
extern unordered_map<short*, struct_info, hash_fncts, hash_eq> struct_map;
bool equal_energies(short *str, int energy)
{
if (energy == degen_energy) {
if (degen_done.count(str)==0 && degen_undone.count(str)==0) {
degen_undone.insert(allocopy(str));
//if (DEBUGG) fprintf(stderr, "inserting(undn): %s\n", pt_to_str(str).c_str());
}
}
if (energy < degen_energy) {
degen_saddle = true;
if (!direct) { // collect info about same level structs
unordered_map<short*, struct_info, hash_fncts, hash_eq>::iterator it = struct_map.find(str);
// collect number sets (collect minima information)
if (it!=struct_map.end()) {
if (it->second.LM_nums.size()>0) numbers.insert(it->second.LM_nums);
// collect saddle information
saddles.insert(it->second.saddle_nums.begin(), it->second.saddle_nums.end());
}
}
}
return false;
}
set<short*, setcomp> find_equal_energy(encoded &enc, int energy, degen °, bool direct, bool &lm)
{
bool (*old_funct) (short *, int) = deg.opt->f_point;
deg.opt->f_point = equal_energies;
// count how many times called
cnt_move++;
// unused, just for necessity of functions
short *min_pt = allocopy(enc.pt);
// used
deg.current = energy;
// setup
degen_energy = energy;
degen_saddle = false;
degen_done.clear();
degen_undone.clear();
degen_undone.insert(allocopy(enc.pt));
//fprintf(stderr, " ins: %s\n", pt_to_str(enc.pt).c_str() );
while (degen_undone.size()!=0) {
// take first
short *last = enc.pt;
enc.pt = *degen_undone.begin();
degen_undone.erase(degen_undone.begin());
// move it
insertions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
deletions(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
// shifts (only if enabled + noLP disabled)
if (deg.opt->shift && !deg.opt->noLP) {
shifts(enc, energy, min_pt, deg, deg.opt->verbose_lvl>3);
}
degen_done.insert(enc.pt);
//fprintf(stderr, "inserting(done): %s\n", pt_to_str(enc.pt).c_str());
enc.pt = last;
}
free(min_pt);
deg.opt->f_point = old_funct;
lm = !degen_saddle;
return degen_done;
}
//check if base is lone
bool lone_base(short *pt, int i)
{
if (i<=0 || i>pt[0]) return false;
// is not a base pair
if (pt[i]==0) return false;
// base is lone:
if (i-1>0) {
// is base pair and is the same bracket
if (pt[i-1]!=0 && ((pt[i-1]<pt[pt[i-1]]) == (pt[i]<pt[pt[i]]))) return false;
}
if (i+1<=pt[0]) {
if (pt[i+1]!=0 && ((pt[i-1]<pt[pt[i-1]]) == (pt[i]<pt[pt[i]]))) return false;
}
return true;
}
// find the structure's lone pairs
int find_lone_pair(string &str)
{
for(unsigned int i=0; i<str.length(); i++) {
if (str[i]=='(') {
if (i+1==str.length() || str[i+1]!='(') {
return i;
} else while (i+1!=str.length() && str[i+1]=='(') i++;
}
if (str[i]==')') {
if (i+1==str.length() || str[i+1]!=')') {
return i;
} else while (i+1!=str.length() && str[i+1]==')') i++;
}
}
return -1;
}
// if the structure has lone pairs
int find_lone_pair(short* str)
{
for(int i=1; i<str[0]; i++) {
if (str[i]==0) continue; // '.'
if (str[i]>str[str[i]]) { // '('
if (i+1==str[0] || str[i+1]==0 || str[i+1]<str[str[i+1]]) {
return i;
} else while (i+1!=str[0] && str[i+1]!=0 && str[i+1]>str[str[i+1]]) i++;
}
if (str[i]<str[str[i]]) { // ')'
if (i+1==str[0] || str[i+1]==0 || str[i+1]>str[str[i+1]]) {
return i;
} else while (i+1!=str[0] && str[i+1]!=0 && str[i+1]<str[str[i+1]]) i++;
}
}
return -1;
}
// try insert base pair (i,j)
bool try_insert(short *pt, const char *seq, int i, int j)
{
if (i<=0 || j<=0 || i>pt[0] || j>pt[0]) return false;
return (j-i>MINGAP && pt[j]==0 && pt[i]==0 && compat(seq[i-1], seq[j-1]));
}
// print rates to a file
void print_rates(char *filename, double temp, int num, float *energy_barr, vector<int> &output_en)
{
FILE *rates;
rates = fopen(filename, "w");
if (rates==NULL) {
fprintf(stderr, "ERROR: couldn't open file \"%s\" for rates! (using stderr instead)\n", filename);
rates = stderr;
}
double _kT = 0.00198717*(273.15 + temp);
for (int i=0; i<num; i++) {
for (int j=0; j<num; j++) {
float res = 0.0;
if (i!=j) {
// Arhenius kinetics (as A method in treekin)
res = 1.0*exp(-(energy_barr[i*num+j]-(output_en[i]/100.0))/_kT);
}
fprintf(rates, "%10.4g ", res);
}
fprintf(rates, "\n");
}
fclose(rates);
}
// pt to str
string pt_to_str(const short *pt)
{
string str;
str.resize(pt[0]);
for (int i=1; i<=pt[0]; i++) {
if (pt[i]==0) str[i-1]='.';
else if (pt[i]<i) str[i-1]=')';
else str[i-1]='(';
}
return str;
}