/
BHGbuilder.cpp
1861 lines (1585 loc) · 77.9 KB
/
BHGbuilder.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern "C" {
#include "utils.h"
#include "fold_vars.h"
#include "pair_mat.h"
#include "fold.h"
//#include "findpath.h"
}
#include "move_set_pk.h"
#include "findpath_pk.h"
#include "BHGbuilder.h"
#include "hash_util.h"
#include <algorithm>
using namespace std;
DSU::DSU(FILE *input, bool noLP, bool shifts, bool pknots, int time_max, int max_lm, bool just_read, bool debug) {
// NULL::
seq = NULL;
s0 = NULL;
s1 = NULL;
gl_maxen = INT_MIN;
generated = false;
// pseudoknots?
this->pknots = pknots;
// time:
if (time_max) {
time = clock();
}
stop_after = time_max;
if (!input) return ;
// read seq
char *line;
int num = 0;
line = my_getline(input);
char *seq2 = strtok(line, " \t");
if (!isSeq(seq2)) {
free(line);
line = my_getline(input);
seq2 = strtok(line, " \t");
if (!isSeq(seq2)) {
free(line);
return ;
}
}
seq = (char*) malloc((strlen(seq2)+1)*sizeof(char));
strcpy(seq, seq2);
free(line);
//init
make_pair_matrix();
s0 = encode_sequence(seq, 0);
s1 = encode_sequence(seq, 1);
// if we read .dsu file
if (just_read) {
//read structs
line = my_getline(input);
bool saddle_reading = false;
while (line) {
bool empty_line = false;
short *tmp = NULL;
float energy_tmp;
int type;
char *p;
for (int i=0; i<4; i++) {
p = strtok(i==0?line:NULL, " \t");
switch (i) {
case 1:
if (!p) {empty_line = true; break;}
if (isStruct(p)) {
if (tmp) free(tmp); // only one struct per line!
tmp = pknots?make_pair_table_PK(p):make_pair_table(p);
break;
}
case 2:
sscanf(p, "%f", &energy_tmp);
break;
case 3:
for (int i=0; i<4; i++) {
if (!saddle_reading) {
if (strcmp(p, LMtype_string[i])==0) {
type = i;
break;
}
} else {
if (strcmp(p, SDtype_string[i])==0) {
type = i;
break;
}
}
}
break;
}
if (empty_line) break;
}
if (empty_line) {
saddle_reading = true;
free(line);
line = my_getline(input);
edgesV_l.resize(LM.size());
continue;
}
set<int> LM_cs;
set<int> saddle_cs;
int number;
if (saddle_reading) {
while ((p=strtok(NULL, " \t"))) {
if (p[strlen(p)-1]=='S') {
sscanf(p, "%dS", &number);
saddle_cs.insert(number-1);
} else {
sscanf(p, "%d", &number);
LM_cs.insert(number-1);
}
}
}
if (saddle_reading && LM_cs.size()<2) {
fprintf(stderr, "File reading error -- too few LM connected by saddle %d\n", (int)saddles.size()+1);
exit(EXIT_FAILURE);
}
vector<int> LM_c(LM_cs.begin(), LM_cs.end());
vector<int> saddle_c(saddle_cs.begin(), saddle_cs.end());
// add info:
if (tmp) {
if (!saddle_reading) {
RNAlocmin struc;
struc.structure = tmp;
struc.str_ch = pt_to_chars_pk(struc.structure);
struc.energy = en_fltoi(energy_tmp);
struc.type = (LMtype)type;
// insert new LM
vertex_l[struc] = LM.size();
LM.push_back(struc);
gl_maxen = max(gl_maxen, struc.energy);
} else {
int wrong = -1;
for (int a=0; a<(int)LM_c.size(); a++) {
if ((int)LM.size()<=LM_c[a]) {
wrong = LM_c[a];
break;
}
}
if (wrong != -1) {
fprintf(stderr, "WARNING: saddle end not found: %d on line %s\n", wrong, line);
free(tmp);
free(line);
line = my_getline(input);
continue;
}
RNAsaddle struc(LM_c[0], LM_c[1], (SDtype)type);
struc.structure = tmp;
struc.str_ch = pt_to_chars_pk(struc.structure);
struc.energy = en_fltoi(energy_tmp);
// insert new sadle
saddles.push_back(struc);
// edge l-l
for (int i=0; i<(int)LM_c.size(); i++) {
for (int j=i+1; j<(int)LM_c.size(); j++) {
edgeLL e(LM_c[i], LM_c[j], struc.energy, saddles.size()-1);
edges_l.insert(e);
edgesV_l[LM_c[i]].insert(e);
edgesV_l[LM_c[j]].insert(e);
}
}
// edge l-s
for (int i=0; i<(int)LM_c.size(); i++) {
edgeLS e2(LM_c[i], saddles.size()-1);
edges_ls.insert(e2);
}
// edges s-s
for (int i=0; i<(int)saddle_c.size(); i++) {
edgeSS e(saddle_c[i], saddles.size()-1);
edges_s.insert(e);
}
}
} else {
free(tmp);
}
free(line);
line = my_getline(input);
num++;
}
} else {
//read structs
line = my_getline(input);
while (line) {
short *tmp = NULL;
float energy_tmp;
bool has_energy = false;
//if (debug) fprintf(stderr, "%s\n", line);
char *p;
p = strtok(line, " \t");
while (p !=NULL && (!has_energy || !tmp)) {
// is struct?
if (isStruct(p)) {
if (tmp) free(tmp); // only one struct per line!
tmp = pknots?make_pair_table_PK(p):make_pair_table(p);
//if (debug) fprintf(stderr, "%s\n", pt_to_str_pk(tmp).c_str());
has_energy = false;
} else {
// is energy?
if (sscanf(p, "%f", &energy_tmp)==1) {
has_energy = true;
}
}
p = strtok(NULL, " \t");
}
// add info:
if (tmp) {
// check if its true local minima
int en;
if (pknots) {
Structure str(seq, tmp, s0, s1);
en = move_gradient_pk(seq, &str, s0, s1, shifts, 0);
copy_arr(tmp, str.str);
} else {
en = move_gradient(seq, tmp, s0, s1, 0, shifts, noLP);
}
// if we don't have it yet, add it:
if (FindNum(en, tmp)==-1) {
RNAlocmin struc;
struc.structure = tmp;
struc.str_ch = pt_to_chars_pk(struc.structure);
struc.energy = en;
//int en = (has_energy?en_fltoi(energy_tmp):energy_of_structure_pt(seq, tmp, s0, s1, 0));
// insert new LM
vertex_l[struc] = LM.size();
LM.push_back(struc);
gl_maxen = max(gl_maxen, en);
if (max_lm>0 && max_lm==(int)LM.size()) {
free(line);
break;
}
} else {
free(tmp);
}
} else {
// line without struct???
fprintf(stderr, "WARNING: line %d without struct: %s\n", num, line);
}
free(line);
line = my_getline(input);
num++;
}
}
if (debug) PrintLM(stderr, false);
// sort them!
SortFix();
number_lm = (int)LM.size();
//printf("------------------------------------------------------------%d\n", number_lm);
}
DSU::~DSU() {
for (unsigned int i=0; i<LM.size(); i++) {
if (LM[i].structure) free(LM[i].structure);
if (LM[i].str_ch) free(LM[i].str_ch);
}
if (seq) free(seq);
if (s0) free(s0);
if (s1) free(s1);
LM.clear();
if (pknots) freeP();
for (unsigned int i=0; i<saddles.size(); i++) {
if (saddles[i].structure) free(saddles[i].structure);
if (saddles[i].str_ch) free(saddles[i].str_ch);
}
}
bool DSU::InsertUB(RNAsaddle saddle, bool debug)
{
set<RNAsaddle, RNAsaddle_comp>::iterator it = UBlist.find(saddle);
if (it==UBlist.end()) {
if (debug) fprintf(stderr, "UBins: (%3d, %3d) insert saddle %6.2f %s\n", saddle.lm1, saddle.lm2, saddle.energy/100.0, pt_to_str(saddle.structure).c_str());
UBlist.insert(saddle);
return true;
//fprintf(stderr, "cannot find (UB ): (%3d, %3d)\n", num1, num2);
} else {
// update it
if (saddle.energy < it->energy) {
if (debug) fprintf(stderr, "UBupd: (%3d, %3d) from %6.2f to %6.2f %s\n", saddle.lm1, saddle.lm2, it->energy/100.0, saddle.energy/100.0, pt_to_str(it->structure).c_str());
if (it->structure) free(it->structure);
UBlist.erase(it);
UBlist.insert(saddle);
return true;
}
}
free(saddle.structure);
return false;
}
int DSU::LinkCPLM(Opt opt, bool debug)
{
//edgesV_ls.resize(LM.size());
edgesV_l.resize(LM.size());
fprintf(stderr, "Computing lm-* edges.\n");
int trueds = 0;
// create lm-saddle and lm-lm edges
for (unsigned int i=0; i<saddles.size(); i++) {
RNAsaddle stru = saddles[i];
// flood them up!
if (opt.flood_num>0) {
int res = FloodUp(LM[stru.lm1], LM[stru.lm2], stru, opt, debug);
if (res == 1 || res == 2) { // we have found better saddle (1) or reached threshold (2) so better saddle is not possible
stru.type = LDIRECT; // so our saddle is for sure lowest direct saddle
trueds ++;
}
}
// update vertex/edge sets
/*map<RNAstruc, int>::iterator it;
int saddle_num;
if ((it = vertex_s.find(stru)) != vertex_s.end()) {
saddle_num = it->second;
stru.freeMEM();
// also add missing edges:
set<int> miss;
for (set<edgeLM>::iterator it2 = edges_ls.begin(); it2!=edges_ls.end(); it2++) {
if (it2->j == saddle_num) {
miss.insert(it2->i);
}
}
miss.insert(pq.i);
miss.insert(pq.j);
// edges ll
for (set<int>::iterator it2 = miss.begin(); it2!=miss.end(); it2++) {
set<int>::iterator it3 = it2;
for (it3++; it3!=miss.end(); it3++) {
edgeLM e(*it2, *it3, true, true);
e.AddSaddle(stru.energy, saddle_num);
edges_l.insert(e);
edgesV_l[*it2].insert(e);
edgesV_l[*it3].insert(e);
}
}
//fprintf(stderr, "saddle_num = %d\n", saddle_num);
} else {*/
// update vertex
//vertex_s.insert(make_pair(stru, saddle_num));
saddles[i] = stru;
// edges ll
edgeLL e(stru.lm1, stru.lm2, stru.energy, i);
edges_l.insert(e);
edgesV_l[stru.lm1].insert(e);
edgesV_l[stru.lm2].insert(e);
// edges ls
edges_ls.insert(edgeLS(stru.lm1, i));
edges_ls.insert(edgeLS(stru.lm2, i));
}
fprintf(stderr, "Recomputed %d(%d) edges (%d are true direct saddles)\n", (int)edges_l.size(), (int)saddles.size(), trueds);
return 0;
}
int DSU::LinkCPsaddle(Opt opt, bool debug) { // construct vertex and edge set from saddles (saddle to saddle)
fprintf(stderr, "Computing saddle-saddle edges.\n");
set<std::pair<int, int> > saddle_pairs;
int last_lm = -1;
vector<int> tmp;
for (set<edgeLS>::iterator it = edges_ls.begin(); it!=edges_ls.end(); it++) {
if (it->i == last_lm) {
// add every one into saddle_pairs.
for (unsigned int i=0; i<tmp.size(); i++) {
saddle_pairs.insert(make_pair(min(tmp[i], it->j), max(tmp[i], it->j)));
}
} else {
// clear
tmp.clear();
}
// add next one
tmp.push_back(it->j);
last_lm = it->i;
}
// process this list
for (set<std::pair<int, int> >::iterator it=saddle_pairs.begin(); it!=saddle_pairs.end(); it++) {
if (debug) {
fprintf(stderr, "saddles to check: %4d %4d", it->first, it->second);
}
RNAsaddle &first = ((saddles[it->first].energy <= saddles[it->second].energy) ? saddles[it->first] : saddles[it->second]);
RNAsaddle &second = ((saddles[it->first].energy > saddles[it->second].energy) ? saddles[it->first] : saddles[it->second]);
// include them
if (FloodSaddle(first, second, opt, debug)) {
edgeSS e(it->first, it->second);
edges_s.insert(e);
}
}
fprintf(stderr, "Recomputed %d saddle-saddle edges (%d computations done)\n", (int)edges_s.size(), (int)saddle_pairs.size());
return 0;
}
void DSU::PrintDot(char *filename, bool dot_prog, bool print, char *file_print, bool visual, bool print_energies)
{
// landmap not supported yet
// start find_union stuff
UF_set connected;
int color = 180;
//open file
FILE *dot;
dot = fopen(filename, "w");
if (dot) {
fprintf(dot, "Graph G {\n\tnode [width=0.1, height=0.1, shape=circle];\n");
//nodes LM:
for (unsigned int i=0; i<LM.size(); i++) {
char energy[20] = "";
if (print_energies) sprintf(energy, "\\n%.2f", LM[i].energy/100.0);
switch (LM[i].type) {
case NORMAL:
case NORM_CF: fprintf(dot, "\"%d\" [label=\"%d%s\"]\n", i+1, i+1, energy); break;
case EE_DSU: fprintf(dot, "\"%d\" [label=\"%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(0, 0, 255), rgb(0, 0, 255)); break;
case EE_COMP: fprintf(dot, "\"%d\" [label=\"%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(255, 0, 0), rgb(255, 0, 0)); break;
}
}
fprintf(dot, "\n");
// visualisation option (not finished -- currently it prints out only without saddles)
if (visual) {
connected.enlarge_parent(LM.size());
set<edgeLL, edgeLL_compen> tmp;
tmp.insert(edges_l.begin(), edges_l.end());
for (set<edgeLL>::iterator it=tmp.begin(); it!=tmp.end(); it++) {
if (!connected.joint(it->i, it->j)) {
fprintf(dot, "\"%d\" -- \"%d\" [label=\"%.2f\"]\n", (it->i)+1, (it->j)+1, it->en/100.0);
connected.union_set(it->i, it->j);
}
}
fprintf(dot, "\n");
} else {
//nodes saddle:
for (unsigned int i=0; i<saddles.size(); i++) {
char energy[20] = "";
if (print_energies) sprintf(energy, "\\n%.2f", saddles[i].energy/100.0);
switch (saddles[i].type) {
/*case DIRECT: fprintf(dot, "\"S%d\" [label=\"S%d (%d %d)\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, saddles[i].lm1+1, saddles[i].lm2+1, rgb(color, color, color), rgb(color, color, color)); break;
case LDIRECT: fprintf(dot, "\"S%d\" [label=\"S%d (%d %d)\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, saddles[i].lm1+1, saddles[i].lm2+1, rgb(color+30, color+30, color+30), rgb(color+30, color+30, color+30)); break;
case NOT_SURE: fprintf(dot, "\"S%d\" [label=\"S%d (%d %d)\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, saddles[i].lm1+1, saddles[i].lm2+1, rgb(color-30, color-30, color-30), rgb(color-30, color-30, color-30)); break;
case COMP: fprintf(dot, "\"S%d\" [label=\"S%d (%d %d)\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, saddles[i].lm1+1, saddles[i].lm2+1, rgb(255, color, color), rgb(255, color, color)); break;*/
case DIRECT: fprintf(dot, "\"S%d\" [label=\"S%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(color, color, color), rgb(color, color, color)); break;
case LDIRECT: fprintf(dot, "\"S%d\" [label=\"S%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(color+30, color+30, color+30), rgb(color+30, color+30, color+30)); break;
case NOT_SURE: fprintf(dot, "\"S%d\" [label=\"S%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(color-30, color-30, color-30), rgb(color-30, color-30, color-30)); break;
case COMP: fprintf(dot, "\"S%d\" [label=\"S%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(255, color, color), rgb(255, color, color)); break;
case REDUCED: fprintf(dot, "\"S%d\" [label=\"S%d%s\", color=\"%s\", fontcolor=\"%s\"]\n", i+1, i+1, energy, rgb(color, 255, color), rgb(color, 255, color)); break;
}
}
fprintf(dot, "\n");
// edges l-l
for (set<edgeLL>::iterator it=edges_l.begin(); it!=edges_l.end(); it++) {
bool component = (saddles[it->saddle].type == COMP);
fprintf(dot, "\"%d\" -- \"%d\" [label=\"%.2f\", color=\"%s\", fontcolor=\"%s\"]\n", (it->i)+1, (it->j)+1, it->en/100.0, (component?rgb(255, 0, 0):rgb(0, 0, 0)), (component?rgb(255, 0, 0):rgb(0, 0, 0)));
}
fprintf(dot, "\n");
// edges l-s
for (set<edgeLS>::iterator it=edges_ls.begin(); it!=edges_ls.end(); it++) {
bool component = (saddles[it->j].type == COMP);
fprintf(dot, "\"%d\" -- \"S%d\" [color=\"%s\", fontcolor=\"%s\"]\n", (it->i)+1, (it->j)+1, (component?rgb(255, color, color):rgb(color, color, color)), (component?rgb(255, color, color):rgb(color, color, color)));
}
fprintf(dot, "\n");
// edges s-s
for (set<edgeSS>::iterator it=edges_s.begin(); it!=edges_s.end(); it++) {
fprintf(dot, "\"S%d\" -- \"S%d\" [color=\"%s\", fontcolor=\"%s\"]\n",(it->i)+1, (it->j)+1, rgb(color, color, color),rgb(color, color, color));
}
}
fprintf(dot, "\n}\n");
}
fclose(dot);
// start neato/dot:
if (dot && print && file_print) {
char syst[200];
sprintf(syst, "%s -Tps < %s > %s", (dot_prog ? "dot" : "neato"), filename, file_print);
system(syst);
//printf("%s returned %d\n", syst, res);
}
}
static int EN_BARRIERS = true;
struct pq_path {
int lm;
int dist;
int en_barr;
int value() {
return (EN_BARRIERS ? en_barr : dist);
}
bool operator<(const pq_path &second) const {
if (EN_BARRIERS) {
if (en_barr != second.en_barr) {
return en_barr<second.en_barr;
}
}
if (dist == second.dist) {
return lm<second.lm;
}
return dist<second.dist;
}
pq_path(int lm, int dist, int en_barr) {
this->lm = lm;
this->dist = dist;
this->en_barr = en_barr;
}
};
void DSU::VisPath(int src, int dest, bool en_barriers, int max_length, bool dot_prog, bool debug)
{
EN_BARRIERS = en_barriers;
char filename[50];
char file_print[50];
sprintf(filename, "path%d_%d.dot", src+1, dest+1);
sprintf(file_print, "path%d_%d.eps", src+1, dest+1);
float color = 0.5;
//open file
FILE *dot;
dot = fopen(filename, "w");
if (dot) {
fprintf(dot, "Graph G {\n\tnode [width=0.1, height=0.1, shape=circle];\n");
// actual nodes:
// forward pass:
vector<int> LM_tmp(LM.size(), INT_MAX);
priority_queue <pq_path> pq_tmp;
pq_path to_insert(src, 0, LM[src].energy);
LM_tmp[src] = to_insert.value();
pq_tmp.push(to_insert);
bool found_dst = false;
int maximum = INT_MAX;
while (!pq_tmp.empty()) {
pq_path point = pq_tmp.top();
pq_tmp.pop();
if (point.lm == dest) {
maximum = min(point.value(), maximum);
found_dst = true;
if (!EN_BARRIERS) {
break;
}
}
if (LM_tmp[point.lm] < point.value()) continue; // we have found better and this is obsolete
for (set<edgeLL>::iterator it=edgesV_l[point.lm].begin(); it!=edgesV_l[point.lm].end(); it++) {
int en_barr = it->en;
int goesTo = it->goesTo(point.lm);
if (found_dst && (maximum < en_barr)) continue; // we dont want higher energy (really dirty programming :/)
// insert new elements into pq (if they are better)
pq_path to_insert(goesTo, point.dist+1, max(point.en_barr, en_barr));
if (LM_tmp[goesTo] <= to_insert.value()) continue;
pq_tmp.push(to_insert);
LM_tmp[goesTo] = to_insert.value();
}
}
// output
set<int> LM_out;
LM_out.insert(dest);
LM_out.insert(src);
set<edgeLL> edge_out;
// backward pass -- find all paths with same dist/same en_barrier
if (EN_BARRIERS) {
// collect all paths (NP-complete)
vector<SimplePath> paths = ConstructAllPaths(src, dest, max_length, LM_tmp[dest]);
int en_barr;
if (paths.size()>0) {
en_barr = paths[0].max_energy;
for (unsigned int i=0; i<paths.size(); i++) {
// stopping condition:
if (paths[i].max_energy != en_barr) break;
// print them?
if (debug) paths[i].Print(true);
// output them
for (unsigned int j=0; j<paths[i].points.size(); j++) {
LM_out.insert(paths[i].points[j]);
if (j>0) {
edgeLL e(paths[i].points[j-1], paths[i].points[j], paths[i].energies[j-1], -1);
edge_out.insert(e);
}
}
}
}
} else {
// find the shortest path
int max = LM_tmp[dest];
if (max == INT_MAX) {
fprintf(stderr, "WARNING: Cannot reach %d from %d!\n", src+1, dest+1);
} else {
queue<pq_path> que;
que.push(pq_path(dest, max, INT_MAX));
while (!que.empty()) {
pq_path point = que.front();
que.pop();
for (set<edgeLL>::iterator it=edgesV_l[point.lm].begin(); it!=edgesV_l[point.lm].end(); it++) {
int goesTo = it->goesTo(point.lm);
// if this point is on shortest path:
if (LM_tmp[goesTo] == point.dist-1) {
LM_out.insert(goesTo);
edge_out.insert(*it);
if (point.dist>1) que.push(pq_path(goesTo, point.dist-1, INT_MAX));
} // else nothing
}
}
}
}
// nodes
for (set<int>::iterator it=LM_out.begin(); it!=LM_out.end(); it++) {
if ((*it) == dest || (*it) == src) {
fprintf(dot, "\"%d\" [label=\"%d\"]\n", (*it)+1, (*it)+1);
} else fprintf(dot, "\"%d\" [label=\"%d\", color=\"0.0 0.0 %.1f\", fontcolor=\"0.0 0.0 %.1f\"]\n", (*it)+1, (*it)+1, color, color);
}
fprintf(dot, "\n");
// edges:
for (set<edgeLL>::iterator it=edge_out.begin(); it!=edge_out.end(); it++) {
fprintf(dot, "\"%d\" -- \"%d\" [label=\"%.2f\", color=\"0.0 0.0 %.1f\", fontcolor=\"0.0 0.0 %.1f\"]\n", (it->i)+1, (it->j)+1, it->en/100.0, color, color);
}
fprintf(dot, "}\n");
}
fclose(dot);
// start neato/dot:
char syst[200];
sprintf(syst, "%s -Tps < %s > %s", (dot_prog ? "dot" : "neato"), filename, file_print);
system(syst);
//printf("%s returned %d", syst, res);
}
void DSU::ReadFilter(char *filter_file)
{
// firest read the filter file
FILE *filter = fopen(filter_file, "r");
if (filter) {
mapping.clear();
char *line = my_getline(filter);
while (line) {
//fprintf(stderr, "working: %s\n",line);
short *tmp = NULL;
int num;
float energy_tmp;
char *p;
for (int i=0; i<3; i++) {
p = strtok(i==0?line:NULL, " \t");
if (!p) break;
switch (i) {
case 0:
sscanf(p, "%d", &num);
break;
case 1:
if (isStruct(p)) {
if (tmp) free(tmp); // only one struct per line!
tmp = pknots?make_pair_table_PK(p):make_pair_table(p);
break;
}
case 2:
sscanf(p, "%f", &energy_tmp);
break;
}
}
// write down the data to mapping
if (tmp) {
RNAstruc tmpstruc;
tmpstruc.structure = tmp;
tmpstruc.energy = en_fltoi(energy_tmp);
if (vertex_l.count(tmpstruc)==0) {
fprintf(stderr, "WARNING: structure %5d ""%s"" %6.2f not found in DSU\n", num, pt_to_str_pk(tmp).c_str(), tmpstruc.energy/100.0);
} else {
int num_real = vertex_l[tmpstruc];
mapping.push_back(num_real);
}
}
if (tmp) free(tmp); tmp = NULL;
free(line);
line = my_getline(filter);
}
fclose(filter);
mapping_rev.clear();
mapping_rev.resize(LM.size(), -1);
for (int i=0; i<(int)mapping.size(); i++) {
mapping_rev[mapping[i]] = i;
}
} else {
fprintf(stderr, "WARNING: cannot open filter file ""%s""!\n", filter_file);
}
}
void DSU::RemoveLM(int many_remove, char* filter)
{
//make a queue of removal LMs
vector<std::pair<int, int> > transitions;
// read the filter file
if (filter && mapping.empty()) ReadFilter(filter);
// construct queue
for (int i=0; i<(int)edgesV_l.size(); i++) {
// check if not in filter:
if (mapping_rev[i] == -1) transitions.push_back(make_pair(edgesV_l[i].size(), i));
}
sort(transitions.begin(), transitions.end());
// now we can destroy individual minima:
for (int i=0; i<many_remove; i++) {
int to_remove = transitions[i].second;
// remove transitions:
for (auto j=edgesV_l[i].begin(); j!=edgesV_l[i].end(); j++) {
auto k = j;
k++;
for (; k!=edgesV_l[i].end(); k++) {
// do only saddle reusing
int sad1 = j->saddle;
int sad2 = k->saddle;
// pick the higher saddle
int sad;
if (j->en > k->en) sad = sad1;
else sad = sad2;
int ii = j->i == to_remove?j->j:j->i;
int jj = k->i == to_remove?k->j:k->i;
// now check if the saddle already exists:
edgeLL e(ii,jj,max(j->en, k->en), sad);
auto edg = edges_l.find(e);
if (edg != edges_l.end()) {
}
}
}
}
//reset mapping:
mapping.clear();
mapping_rev.clear();
}
void DSU::PrintMatrix(char *filename, bool full, char* filter_file, char type)
{
// read_filter!
if (filter_file && mapping.empty()) ReadFilter(filter_file);
// if not filtered and not full, do only NORMAL ones.
if (!full && mapping.size() == 0) {
mapping_rev.resize(LM.size(), 0);
fprintf(stderr, "Mapping: \n");
for (int i=0; i<(int)LM.size(); i++) {
if (LM[i].type==NORMAL) {
mapping.push_back(i);
mapping_rev[i] = mapping.size()-1;
fprintf(stderr, "%4d[%4d] ", i+1, (int)mapping.size());
}
}
//fprintf(stderr, "(%d mapped to %d) \n", (int)LM.size(), (int)mapping.size());
}
fprintf(stderr, "Generating the matrix %c\n", type);
int size = (full?LM.size():mapping.size());
FILE *energies;
energies = fopen(filename, "w");
if (energies) {
// create matrix
if (type != 'D' && !generated) {
matrix.clear();
for (int i=0; i<size; i++) {
int to_search = full?i:mapping[i];
matrix.push_back(HeightSearch(to_search, edgesV_l));
fprintf(stderr, "done %d/%d\r", i, size);
// discard those we dont want
if (!full) {
for (int j=0; j<(int)mapping.size(); j++) {
matrix[i][j] = matrix[i][mapping[j]];
}
}
/*if ((int)matrix[i].size()!=size) {
matrix[i].resize(size);
}*/
}
// resolve i==i
/*for (unsigned int i=0; i<matrix.size(); i++) {
matrix[i][i] = make_pair(LM[i].energy, 0);
}*/
generated = true;
}
// print
//fprintf(stderr, "Printing the matrix type %c\n", type);
for (unsigned int i=0; i<matrix.size(); i++) {
fprintf(energies, "%6d %s ", mapping[i]+1, LM[mapping[i]].str_ch);
for (int j=0; j<size; j++) {
switch (type) {
case 'E': fprintf(energies, "%6.2f ", matrix[i][j].height/100.0); break;
case 'D': fprintf(energies, "%5d ", HammingDist(LM[i].structure, LM[j].structure)); break;
case 'G': fprintf(energies, "%5d ", matrix[i][j].gdist); break; /// shouldn't it be the minimum??? then the get pah is not always the same as the number reported here
case 'B': fprintf(energies, "%5d ", matrix[i][j].bdist); break;
case 'P': fprintf(energies, "%s ", matrix[i][j].ptype.Print()); break;
}
}
fprintf(energies, "\n");
}
}
fclose(energies);
}
void DSU::PrintRates(char *filename, bool full, double temp, char mode)
{
int size = (full?LM.size():number_lm);
FILE *rates;
rates = fopen(filename, "w");
if (rates) {
mode_rates mod;
switch (mode) {
case 'V': mod = VERTEX_CONTR; break;
case 'E': mod = EDGE_CONTR_MIN; break;
case 'M': mod = EDGE_CONTR_MAX; break;
case 'F': mod = NO_CONTR; break;
case 'S': mod = VERTEX_CONTR_SUM; break;
default: mod = VERTEX_CONTR;
}
Graph graph(edges_l, LM, mod);
fprintf(stderr, "graph created...%d LM\n", (int)LM.size());
if (mod!=NO_CONTR) {
for(int i=LM.size()-1; i>=size; i--) {
/*char filename[20];
char filename_eps[20];
sprintf(filename, "smth%d.dot", i);
sprintf(filename_eps, "smth%d.eps", i);
graph.PrintDot(filename, true, true, filename_eps);*/
// call appropriate contraction
if (mod == VERTEX_CONTR || mod == VERTEX_CONTR_SUM) {
graph.RemoveLastPoint();
} else if (mod == EDGE_CONTR_MAX || mod == EDGE_CONTR_MIN) {
while (!graph.RemoveLowestEdge()) ;
}
if (i%100 ==0) fprintf(stderr, "removed point %d\n", i);
}
char filename[20];
char filename_eps[20];
sprintf(filename, "reduced%c.dot", mode);
sprintf(filename_eps, "reduced%c.eps", mode);
graph.PrintDot(filename, true, true, filename_eps);
}
graph.PrintRates(rates, temp);
}
fclose(rates);
}
vector<SimplePath> DSU::ConstructAllPaths(int source, int dest, int max_length, int threshold)
{
vector<SimplePath> paths;
SimplePath path;
path.AddLast(source, INT_MIN);
// construct all path recursively
ConstructPath(paths, path, dest, max_length-1, threshold);
// score them
for (unsigned int i=0; i<paths.size(); i++) {
paths[i].Score();
}
// sort
sort(paths.begin(), paths.end());
return paths;
}
void DSU::ConstructPath(vector<SimplePath> &paths, SimplePath &path, int dest, int max_length, int threshold)
{
int num = path.points[path.points.size()-1];
if (num == dest) {
paths.push_back(path);
//if (paths.size()%100==1) printf("found paths: %d\n", (int)paths.size());
return ;
}
if (max_length == 0) return;
// all edges
for (set<edgeLL>::iterator it=edgesV_l[num].begin(); it!=edgesV_l[num].end(); it++) {
int goesTo = it->goesTo(num);
if (!path.ContainsNode(goesTo) && it->en <= threshold) {
path.AddLast(goesTo, it->en);
ConstructPath(paths, path, dest, max_length-1, threshold);
path.RemoveLast();
}
}
}
void DSU::FillComps()
{
fprintf(stderr, "Filling components.");
comps.clear();
LM_to_comp.clear();
saddle_to_comp.clear();
vector<int> LM_tmp(LM.size(), -1);
vector<int> sadd_tmp(saddles.size(), -1);
// find components:
for (unsigned int i=0; i<LM.size(); i++) {
if (LM_tmp[i]==-1) {
Component cmp;
Color(i, comps.size(), cmp, LM_tmp, sadd_tmp);