-
Notifications
You must be signed in to change notification settings - Fork 1
/
bayesub.cpp
1673 lines (1560 loc) · 44.2 KB
/
bayesub.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
/* bayesub.cpp
Definitions of all mathematical and I/O subroutines.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <math.h>
#include "bayesub.h"
#include "globals.h"
#include "sa.h"
#include "fisher2.h"
// Learn Bayesian network - BBNet.
double bbnet(vector<Constraint>& cons, vector<CPTRow>& cpt, const vector<Case>& genlst)
{
double s = addpres(0, cons, cpt, 1, genlst); // Add first motif into Bayesian network.
#ifdef VERBOSE
cout << "Adding motif " << mscor[0].name << endl;
#endif
for(size_t i = 0; i < mscor.size();)
{
// Delete constraint to improve score.
if(i != 0) // This step is added to possibly delete constraints before new constraints are added.
s = delcons(cons, cpt, s, genlst);
for(set<int>::const_iterator mi = mbnd.begin(); mi != mbnd.end(); mi++) // Try all rules for current motifs in list.
{
Constraint c;
// Test a new functional depth.
if(i != 0)
s = updepth(*mi, cons, cpt, s, genlst);
// Add constraint: distance to TSS.
if(rb[0] == '1' && !chkcons(cons, "tss", *mi, -1))
{
c.desc = "tss";
c.motif0 = *mi;
c.motif1 = -1;
s = addcons(cons, cpt, c, s, genlst, tss_thrds, ntsst);
}
// Add constraint: orientation.
if(rb[1] == '1' && !chkcons(cons, "orien", *mi, -1))
{
c.desc = "orien";
c.motif0 = *mi;
c.motif1 = -1;
int orien[] = {0, 1};
s = addcons(cons, cpt, c, s, genlst, orien, 2);
}
// Add constraint: second copy.
if(rb[2] == '1' && !chkcons(cons, "sec", *mi, -1))
{
c.desc = "sec";
c.motif0 = *mi;
c.motif1 = -1;
int sec[] = {-1};
s = addcons(cons, cpt, c, s, genlst, sec, 1);
}
// Add constraint: distance between any two motifs.
if(rb[3] == '1')
{
for(set<int>::const_iterator mj = mbnd.begin(); mj != mbnd.end(); mj++)
{
if(*mi == *mj)
continue;
if(!chkcons(cons, "dist", *mi, *mj))
{
c.desc = "dist";
c.motif0 = *mi;
c.motif1 = *mj;
s = addcons(cons, cpt, c, s, genlst, dist_thrds, ndistt);
}
}
}
// Add constraint: order relative to TSS between any two motifs.
if(rb[4] == '1')
{
for(set<int>::const_iterator mj = mbnd.begin(); mj != mbnd.end(); mj++)
{
if(*mi == *mj)
continue;
if(!chkcons(cons, "order", *mi, *mj))
{
c.desc = "order";
c.motif0 = *mi;
c.motif1 = *mj;
int order[] = {0, 1};
s = addcons(cons, cpt, c, s, genlst, order, 2);
}
}
}
// Add constraint: looping of any two motifs.
if(rb[5] == '1')
{
for(set<int>::const_iterator mj = mbnd.begin(); mj != mbnd.end(); mj++)
{
if(*mi == *mj)
continue;
if(!chkcons(cons, "loop", *mi, *mj))
{
c.desc = "loop";
c.motif0 = *mi;
c.motif1 = *mj;
s = addcons(cons, cpt, c, s, genlst, loop_thrds, nloopt);
}
}
}
}
// Delete constraint to improve score.
s = delcons(cons, cpt, s, genlst);
// Add one extra updepth step to find correct depth and distance and to improve score.
for(set<int>::const_iterator mi = mbnd.begin(); mi != mbnd.end(); mi++)
s = updepth(*mi, cons, cpt, s, genlst);
// Add constraint: another new motif. If no improvement, break the loop.
double s1 = s;
for(;i < mscor.size()-1 && s1 <= s; i++) // Keep adding motif until improvement achieved OR the last one.
s1 = addpres((int)i+1, cons, cpt, s, genlst);
if(s1 <= s)
break;
else
{
s = s1;
#ifdef VERBOSE
cout << "Adding motif " << mscor[(int)i].name << endl;
#endif
}
}
return s;
}
// Learn Bayesian network - GBNet.
double gbnet(vector<Constraint>& cons, vector<CPTRow>& cpt, const vector<Case>& genlst)
{
double s; // Bayesian score.
int rep, iter; // global iterators for repeat AND iteration.
s = addpres(0, cons, cpt, 1, genlst, true); // Add first motif into Bayesian network.
#ifdef VERBOSE
cout << "Adding motif " << mscor[0].name << endl;
#endif
for(rep = 0; rep < Repeat; rep++) // repeat level.
{
cout << "**** Running Bayesian network at temperature: " << Temp << " ****" << endl;
for(iter = 0; iter < Iteration; iter++) // iteration level.
{
if(!chkcons(cons, "pres", 0))
{
double s1 = addpres(0, cons, cpt, s, genlst, true); // Add first motif into Bayesian network.
if(s1 != s)
{
#ifdef VERBOSE
cout << "Adding motif " << mscor[0].name << endl;
#endif
s = s1;
}
}
for(size_t i = 0; i < mscor.size();)
{
// Delete constraint to improve score before considering new constraints.
// s = delcons(cons, cpt, s, genlst, mbnd);
for(set<int>::const_iterator mi = mbnd.begin(); mi != mbnd.end(); mi++) // Try all different functional depths for current motifs.
{
Constraint c;
// Test a new functional depth.
s = updepth(*mi, cons, cpt, s, genlst, true);
// Add constraint: distance to TSS.
if(rb[0] == '1' && !chkcons(cons, "tss", *mi))
{
c.desc = "tss";
c.motif0 = *mi;
c.motif1 = -1;
s = addcons(cons, cpt, c, s, genlst, tss_thrds, ntsst, true);
}
// Add constraint: orientation.
if(rb[1] == '1' && !chkcons(cons, "orien", *mi))
{
c.desc = "orien";
c.motif0 = *mi;
c.motif1 = -1;
int orien[] = {0, 1};
s = addcons(cons, cpt, c, s, genlst, orien, 2, true);
}
// Add constraint: second copy.
if(rb[2] == '1' && !chkcons(cons, "sec", *mi))
{
c.desc = "sec";
c.motif0 = *mi;
c.motif1 = -1;
int sec[] = {-1};
s = addcons(cons, cpt, c, s, genlst, sec, 1, true);
}
// Add constraint: distance between any two motifs.
if(rb[3] == '1')
{
for(set<int>::const_iterator mj = mbnd.begin(); mj != mbnd.end(); mj++)
{
if(*mj == *mi)
continue;
if(!chkcons(cons, "dist", *mi, *mj))
{
c.desc = "dist";
c.motif0 = *mi;
c.motif1 = *mj;
s = addcons(cons, cpt, c, s, genlst, dist_thrds, ndistt, true);
}
}
}
// Add constraint: order relative to TSS between any two motifs.
if(rb[4] == '1')
{
for(set<int>::const_iterator mj = mbnd.begin(); mj != mbnd.end(); mj++)
{
if(*mj == *mi)
continue;
if(!chkcons(cons, "order", *mi, *mj))
{
c.desc = "order";
c.motif0 = *mi;
c.motif1 = *mj;
int order[] = {0, 1};
s = addcons(cons, cpt, c, s, genlst, order, 2, true);
}
}
}
// Add constraint: looping of any two motifs.
if(rb[5] == '1')
{
for(set<int>::const_iterator mj = mbnd.begin(); mj != mbnd.end(); mj++)
{
if(*mj == *mi)
continue;
if(!chkcons(cons, "loop", *mi, *mj))
{
c.desc = "loop";
c.motif0 = *mi;
c.motif1 = *mj;
s = addcons(cons, cpt, c, s, genlst, loop_thrds, nloopt, true);
}
}
}
}
// Delete constraint to improve score.
s = delcons(cons, cpt, s, genlst);
// Add constraint: another new motif. If no improvement, break the loop.
double s1 = s;
for(;i < mscor.size()-1 && s1 == s; i++)
{
if(!chkcons(cons, "pres", (int)i+1))
s1 = addpres((int)i+1, cons, cpt, s, genlst, true); // "i" refer to passed motif.
}
if(s1 == s)
break;
else
{
s = s1;
#ifdef VERBOSE
cout << "Adding motif " << mscor[(int)i].name << endl; // "i" now refer to the new added motif.
#endif
}
} // Iteration.
if(Restag)
s = restart(s, cons, cpt, iter);
if(chng > Changes || rests > Restarts) // Required changes have been made. Go to next repeat.
break; // OR, restarting reaches maximum number.
} // Repeat.
// Summarize information about this repeat.
cout << chng << " changes have been made at temperature: " << Temp << endl;
cout << "After " << iter << " iterations." << endl;
cout << "And " << rests << " restarts." << endl;
if(chng < Resthrld && iter == Iteration) // Temperature is cool now.
Restag = true; // Set tag to restart SA if bad condition happens.
if(chng == 0 && rests == 0) // No changes(restarts) have been made. Exit.
break;
chng = 0; // Reset counter for changes.
rests = 0; // Reset counter for restartings.
Temp *= Alpha; // Decrease temperature by rate alpha.
} // Whole procedure.
return s;
}
// *************** Main Routines End Here ******************* //
// Take a bootstrap sample for a vector of objects.
vector<Case> bsamp(const vector<Case>& t, const vector<Case>& b)
{
vector<Case> s;
for(size_t i = 0; i < t.size(); i++)
{
int index = (int)floor(((double)rand()-1)/RAND_MAX*t.size());
s.push_back(t[index]);
}
for(size_t i = 0; i < b.size(); i++)
{
int index = (int)floor(((double)rand()-1)/RAND_MAX*b.size());
s.push_back(b[index]);
}
return s;
}
// Calculate number of cases in each category.
int calcnum(const vector<Case>& v, int c)
{
int n = 0;
for(size_t i = 0; i < v.size(); i++)
{
if(v[i].label == c)
n++;
}
return n;
}
// Find the index in depth array according to the depth.
int depidx(double depth)
{
for(int i = 0; i < nfunc; i++)
{
if(func_depths[i] - depth < 1e-8 && func_depths[i] - depth > -1e-8)
return i;
}
return -1;
}
// Add a presence node into Bayesian network.
double addpres(int mi, vector<Constraint>& cons, vector<CPTRow>& cpt, double s, const vector<Case>& genlst, bool jump)
{
Constraint c;
c.desc = "pres";
c.motif0 = mi;
c.motif1 = -1;
c.para = -1;
string motif = mscor[mi].name;
#ifdef VERBOSE
cout << "Considering constraint: pres of " << motif;
#endif
int didx = -1; // Depth index of original binding, if exist.
bool tag = false; // tag to test whether this motif's binding is in stack.
if(mbnd.find(mi) == mbnd.end())
{
tag = true;
mbnd.insert(mi);
}
else
didx = depidx(mscor[mi].depth);
double s0 = 1.0; // Best Bayesian score.
vector<Constraint> cons0; // Best Constraints.
vector<CPTRow> cpt0; // Best CPT.
int didx0; // Best depth index.
for(int i = 0; i < nfunc; i++)
{
vector<Constraint> cons1 = cons;
vector<CPTRow> cpt1 = cpt;
mscor[mi].depth = func_depths[i];
int pres[] = {-1};
double s1 = addcons(cons1, cpt1, c, s0, genlst, pres, 1, false); // Add presence without jumping.
if(s1 > s0 || s0 == 1)
{
s0 = s1;
cons0 = cons1;
cpt0 = cpt1;
didx0 = i;
}
}
#ifdef VERBOSE
cout << " ..." << s0 << "(" << s << ")" << endl;
#endif
if(s == 1 || s0 > s || (1/Temp*(s0-s) > log10((double)rand()/RAND_MAX) && jump)) // Use temperature to control jumping.
{
#ifdef VERBOSE
cout << "Accepting constraint: pres of " << motif << endl;
#endif
chng++; // Increase counter if accept presence.
s = s0;
cons = cons0;
cpt = cpt0;
mscor[mi].depth = func_depths[didx0];
if(tagbests)
bestsolu(s, cons, cpt, mbnd, mscor);
}
else if(tag) // Motif's binding wasn't in stack, delete it.
mbnd.erase(mi);
else // Motif's binding was in stack, recover it.
mscor[mi].depth = func_depths[didx];
return s;
}
// Update functional depth of one motif to improve score.
double updepth(int mi, vector<Constraint>& cons, vector<CPTRow>& cpt, double s, const vector<Case>& genlst, bool jump)
{
// Test whether there is one constraint contain motif "mi".
bool tag = false;
for(size_t i = 0; i < cons.size(); i++)
{
if(mi == cons[i].motif0 || mi == cons[i].motif1)
{
tag = true;
break;
}
}
if(!tag)
return s;
string motif = mscor[mi].name;
#ifdef VERBOSE
cout << "Choosing a new depth for motif " << motif;
#endif
// Backup the original binding index.
int didx = depidx(mscor[mi].depth);
double s0 = 1.0; // Best Bayesian score.
vector<Constraint> cons0 = cons; // Best constraints.
vector<CPTRow> cpt0; // Best CPT.
int didx0 = -1; // Best depth index.
// Try all different functional depths, update CPT and score if necessary.
for(int i = 0; i < nfunc; i++)
{
if(i == didx) // skip original depth.
continue;
mscor[mi].depth = func_depths[i];
for(size_t j = 0; j < cons.size(); j++) // consider all parameters for constraints that contain motif "mi".
{
if(mi != cons[j].motif0 && mi != cons[j].motif1)
continue;
vector<int> paraset; // set up a set of parameters for tuning.
if(cons[j].desc == "pres" || cons[j].desc == "sec")
paraset.push_back(-1);
else if(cons[j].desc == "tss")
{
for(int k = 0; k < ntsst; k++)
paraset.push_back(tss_thrds[k]);
}
else if(cons[j].desc == "orien" || cons[j].desc == "order")
{
for(int k = 0; k < 2; k++)
paraset.push_back(k);
}
else if(cons[j].desc == "dist")
{
for(int k = 0; k < ndistt; k++)
paraset.push_back(dist_thrds[k]);
}
else if(cons[j].desc == "loop")
{
for(int k = 0; k < nloopt; k++)
paraset.push_back(loop_thrds[k]);
}
// Try different parameters.
for(size_t k = 0; k < paraset.size(); k++)
{
vector<Constraint> cons1 = cons0;
cons1[j].para = paraset[k];
vector<CPTRow> cpt1, ppt1;
constrcpt(cpt1, ppt1, genlst, cons1);
double s1;
if(!itag)
s1 = score((int)cons1.size(), cpt1, ppt1);
else
s1 = iscore((int)cons1.size(), cpt1);
if(s1 > s0 || s0 == 1) // Store the best data structures.
{
cons0 = cons1;
cpt0 = cpt1;
s0 = s1;
didx0 = i;
}
}
}
}
#ifdef VERBOSE
cout << " ..." << s0 << "(" << s << ")" << endl;
#endif
if(s0 > s || (1/Temp*(s0-s) > log10((double)rand()/RAND_MAX) && jump)) // Use temperature to control jumping.
{
#ifdef VERBOSE
cout << "Accepting depth change: " << func_depths[didx0] << "(" << func_depths[didx] << ")" << endl;
#endif
s = s0;
cons = cons0;
cpt = cpt0;
mscor[mi].depth = func_depths[didx0];
chng++; // Increase counter if accept depth change.
if(tagbests)
bestsolu(s, cons, cpt, mbnd, mscor);
}
else
mscor[mi].depth = func_depths[didx];
return s;
}
// Add one new constraint into Bayesian network and update everything if necessary.
double addcons(vector<Constraint>& cons, vector<CPTRow>& cpt, Constraint c, double s,
const vector<Case>& genlst, const int paraset[], int npara, bool jump)
{
if(c.desc != "pres")
{
#ifdef VERBOSE
cout << "Considering constraint: " << c.desc << " of " << mscor[c.motif0].name;
if(c.motif1 != -1)
cout << " and " << mscor[c.motif1].name;
#endif
}
if((int)cons.size() >= maxpa)
{
#ifdef VERBOSE
cout << " ...Reach maximum number of parents...Skip!" << endl;
#endif
return s;
}
double s0, s1; // "0" means best; "1" means current.
vector<Constraint> cons0, cons1;
vector<CPTRow> cpt0, cpt1;
s0 = 1.0;
for(int i = 0; i < npara; i++)
{
c.para = paraset[i];
cons1 = cons;
cons1.push_back(c);
vector<CPTRow> ppt1;
constrcpt(cpt1, ppt1, genlst, cons1);
if(!itag)
s1 = score((int)cons1.size(), cpt1, ppt1);
else
s1 = iscore((int)cons1.size(), cpt1);
if(s1 > s0 || s0 == 1)
{
s0 = s1;
cons0 = cons1;
cpt0 = cpt1;
}
}
if(c.desc != "pres")
{
#ifdef VERBOSE
cout << " ..." << s0 << "(" << s << ")" << endl;
#endif
}
if(s0 > s || s == 1 || (1/Temp*(s0-s) > log10((double)rand()/RAND_MAX) && jump)) // Use jumping depends on switch.
{
s = s0;
cons = cons0;
cpt = cpt0;
if(c.desc != "pres")
{
#ifdef VERBOSE
cout << "Accepting constraint: " << c.desc << endl;
#endif
chng++; // Increase counter if accept adding constraint.
if(tagbests)
bestsolu(s, cons, cpt, mbnd, mscor);
}
}
return s;
}
// Delete constraint to improve score.
double delcons(vector<Constraint>& cons, vector<CPTRow>& cpt, double s, const vector<Case>& genlst)
{
bool tag = true;
while(tag)
{
tag = false;
for(size_t i = 0; i < cons.size(); i++)
{
if(cons.size() <= 1) // stop before all constraints are removed.
break;
#ifdef VERBOSE
cout << "Deleting constraint " << cons[i].desc << " of " << mscor[cons[i].motif0].name;
if(cons[i].motif1 != -1)
cout << " and " << mscor[cons[i].motif1].name;
#endif
vector<Constraint> cons1 = cons;
cons1.erase(cons1.begin() + i);
vector<CPTRow> cpt1, ppt;
constrcpt(cpt1, ppt, genlst, cons1);
double s1;
if(!itag)
s1 = score((int)cons1.size(), cpt1, ppt);
else
s1 = iscore((int)cons1.size(), cpt1);
#ifdef VERBOSE
cout << " ..." << s1 << "(" << s << ")" << endl;
#endif
if(s1 > s) // Deletion is greedy.
{
#ifdef VERBOSE
cout << "Accepting deletion" << endl;
#endif
tag = true;
cons = cons1;
cpt = cpt1;
s = s1;
chng++; // Increase counter if accept deletion.
break;
}
}
}
// Check whether there is any hanging binding for motifs that no longer exists in parents.
vector<int> vdel; // array for motif ids to be deleted.
for(set<int>::const_iterator mi = mbnd.begin(); mi != mbnd.end(); mi++)
{
bool tag = false; // Assume no longer exist.
for(size_t j = 0; j < cons.size(); j++)
{
if(*mi == cons[j].motif0 || *mi == cons[j].motif1)
{
tag = true;
break;
}
}
if(!tag) // Delete the motif's binding if it no longer exists in parents.
vdel.push_back(*mi);
}
for(size_t i = 0; i < vdel.size(); i++)
mbnd.erase(vdel[i]);
if(tagbests)
bestsolu(s, cons, cpt, mbnd, mscor);
return s;
}
// Check whether a constraint has already been added.
bool chkcons(const vector<Constraint>& cons, const string& desc, int motif0, int motif1)
{
for(size_t i = 0; i < cons.size(); i++)
{
if(cons[i].desc == desc && (cons[i].motif0 == motif0 && cons[i].motif1 == motif1 ||
cons[i].motif0 == motif1 && cons[i].motif1 == motif0))
return true;
}
return false;
}
// Format and output the results of Bayesian network on a cluster.
int outbayes(ofstream& hOut, double s, const vector<Constraint>& cons, const vector<CPTRow>& cpt, const vector<MotifScore>& vms, size_t node, size_t bkg)
{
// Header information.
hOut << "************ Bayesian network parameters & results ************" << endl << endl;
if(tagbests)
{
hOut << "Number of repeats: " << Repeat << endl;
hOut << "Number of iterations: " << Iteration << endl;
hOut << "Number of required changes: " << Changes << endl;
hOut << "Temperature changing rate Alpha: " << Alpha << endl;
hOut << "Initial temperature: " << Initemp << endl << endl;
}
hOut << "Candidate motifs: " << motifcand << endl;
hOut << "Use prior information for preferred motifs? " << prior << endl;
hOut << "Parameter of prior of network structure: " << logK << endl;
hOut << endl;
if(!primo.empty() && prior == 1)
{
hOut << "Prior counts for preferred motifs: " << pricnt << endl;
hOut << "Preferred motifs: " << endl;
for(set<string>::const_iterator i = primo.begin(); i != primo.end(); i++)
hOut << *i << endl;
}
hOut << endl << "Single node presence score and optimal depth:" << endl;
outscor(hOut, vms);
hOut << endl << "Bayesian score of the network: " << s << endl;
hOut << "Un-penalized Bayesian score: " << s + logK*cons.size() << endl;
hOut << endl << "Constraints: " << endl;
for(size_t i = 0; i < cons.size(); i++)
{
hOut << i + 1 << ". ";
outcons(hOut, cons[i], mscor);
}
hOut << endl << "Number of genes that satisfy each constraint: " << endl;
hOut << "\tIn bkg\tIn node\tP-value" << endl;
vector<CPTRow> nv = ebitcpt(cpt, cons.size());
for(size_t i = 0; i < nv.size(); i++)
hOut << i + 1 << "\t" << nv[i].k0 << "\t" << nv[i].k1 << "\t"
<< fpval(nv[i].k1, node-nv[i].k1, nv[i].k0, bkg-nv[i].k1) << endl;
hOut << endl << "Conditional probability table:" << endl;
hOut << "\tk = 0\tk = 1\tP-value" << endl;
for(size_t i = 0; i < cpt.size(); i++)
{
hOut << fmtbinary((int)i, cons.size()) << "\t" << cpt[i].k0 << "\t" << cpt[i].k1;
if(i != 0)
hOut << "\t" << fpval(cpt[i].k1, node-cpt[i].k1, cpt[i].k0, bkg-cpt[i].k0) << endl;
else
hOut << endl;
}
if(tagbests)
{
hOut << endl << "* " << bsolu.s << endl;
for(size_t i = 0; i < bsolu.cons.size(); i++)
{
hOut << "* " << i + 1 << ". ";
outcons(hOut, bsolu.cons[i], bsolu.mscor);
}
hOut << endl << "* \tIn bkg\tIn node" << endl;
nv = ebitcpt(bsolu.cpt, bsolu.cons.size());
for(size_t i = 0; i < nv.size(); i++)
hOut << "* " << i + 1 << "\t" << nv[i].k0 << "\t" << nv[i].k1 << endl;
hOut << endl << "* \tk = 0\tk = 1" << endl;
for(size_t i = 0; i < bsolu.cpt.size(); i++)
hOut << "* " << fmtbinary((int)i, bsolu.cons.size()) << "\t" << bsolu.cpt[i].k0 << "\t" << bsolu.cpt[i].k1 << endl;
}
return 0;
}
// The number of genes that satisfy each constraint from CPT.
vector<CPTRow> ebitcpt(const vector<CPTRow>& cpt, size_t nc)
{
vector<CPTRow> nv;
for(size_t i = 0; i < nc; i++)
{
int mask = (int)pow((double)2, (int)i);
CPTRow np;
np.k0 = np.k1 = 0;
for(size_t j = 0; j < cpt.size(); j++)
{
if((mask & j) != 0)
{
np.k0 += cpt[j].k0;
np.k1 += cpt[j].k1;
}
}
nv.push_back(np);
}
return nv;
}
// Output one constraint using file handle.
void outcons(ofstream& h, const Constraint& c, const vector<MotifScore>& mscor)
{
if(c.desc == "pres")
h << "Presence of " << mscor[c.motif0].name << ":" << mscor[c.motif0].depth << endl;
else if(c.desc == "tss")
h << "Distance to TSS of " << mscor[c.motif0].name << ":" << c.para << ", " << mscor[c.motif0].depth << endl;
else if(c.desc == "orien")
{
h << "Orientation of " << mscor[c.motif0].name << ":";
if(c.para == 0)
h << "F";
else if(c.para == 1)
h << "R";
h << ", " << mscor[c.motif0].depth << endl;
}
else if(c.desc == "sec")
h << "Second copy of " << mscor[c.motif0].name << ", " << mscor[c.motif0].depth << endl;
else if(c.desc == "dist")
h << "Distance between " << mscor[c.motif0].name << " and " << mscor[c.motif1].name << ":" << c.para
<< ", (" << mscor[c.motif0].depth << "," << mscor[c.motif1].depth << ")" << endl;
else if(c.desc == "order")
{
if(c.para == 0)
h << mscor[c.motif0].name << " is before " << mscor[c.motif1].name << ":" << c.para
<< ", (" << mscor[c.motif0].depth << "," << mscor[c.motif1].depth << ")" << endl;
else if(c.para == 1)
h << mscor[c.motif1].name << " is before " << mscor[c.motif0].name << ":" << c.para
<< ", (" << mscor[c.motif1].depth << "," << mscor[c.motif0].depth << ")" << endl;
}
else if(c.desc == "loop")
h << "Looping of " << mscor[c.motif0].name << " and " << mscor[c.motif1].name << ":" << c.para
<< ", (" << mscor[c.motif0].depth << "," << mscor[c.motif1].depth << ")" << endl;
}
// Output all motif scores and optimal functional depths to file.
void outscor(ofstream& h, const vector<MotifScore>& mscor)
{
for(size_t i = 0; i < mscor.size(); i++)
h << mscor[i].name << "\t" << mscor[i].score << "\t" << mscor[i].depth << endl;
}
// Format a non-negative integer to a string using binary representation.
string fmtbinary(int n, size_t t)
{
string sb; // string of bits.
sb.resize(t);
for(size_t i = 0; i < t; i++)
{
int mask = (int)pow((double)2, (int)i);
if((n & mask) != 0)
sb[t-1-i] = '1';
else
sb[t-1-i] = '0';
}
return sb;
}
// Load motif scores from file and sort them in descending order.
int loadscor(vector<MotifScore>& mscor, const string& s)
{
ifstream hScor(s.data());
if(!hScor)
{
cerr << "Can't open " << s << endl;
return 1;
}
while(hScor.good())
{
string strLn;
getline(hScor, strLn);
if(strLn == "")
continue;
istringstream strmLn(strLn);
MotifScore ascor;
strmLn >> ascor.name >> ascor.score >> ascor.depth;
if(ascor.name[0] == '*')
continue;
mscor.push_back(ascor);
}
hScor.close();
sort(mscor.begin(), mscor.end(), cmp);
if(mscor.end() - mscor.begin() > motifcand) // prevent deletion out of range.
mscor.erase(mscor.begin() + motifcand, mscor.end());
return 0;
}
// Load gene list from file.
int loadgene(vector<Case>& tlst, vector<Case>& blst, const string& n, const string& b)
{
// Genes in the cluster: label = 1.
ifstream hGen(n.data());
if(!hGen)
{
cerr << "Can't open " << n << endl;
return 1;
}
while(hGen.good())
{
string strLn;
getline(hGen, strLn);
if(strLn == "")
continue;
istringstream strmLn(strLn);
string gene;
strmLn >> gene;
str2upper(gene);
Case c;
c.name = gene;
c.label = 1;
tlst.push_back(c);
}
hGen.close();
// Genes in the background: label = 0.
ifstream hBkg(b.data());
if(!hBkg)
{
cerr << "Can't open " << b << endl;
return 1;
}
while(hBkg.good())
{
string strLn;
getline(hBkg, strLn);
if(strLn == "")
continue;
istringstream strmLn(strLn);
string gene;
strmLn >> gene;
str2upper(gene);
Case c;
c.name = gene;
c.label = 0;
blst.push_back(c);
}
hBkg.close();
return 0;
}
// Load motif list from file.
int loadmotif(vector<string>& motiflst, const string& f)
{
ifstream h(f.data());
if(!h)
{
cerr << "Can't open " << f << endl;
return 1;
}
while(h.good())
{
string strLn;
getline(h, strLn);
if(strLn == "")
continue;
istringstream strmLn(strLn);
string motif;
strmLn >> motif;
motiflst.push_back(motif);
}
h.close();
return 0;
}
// Load one motif's binding.
int loadone(GBMap& onebind, const string& motif, const set<string>& genset, const string& folder)
{
string fBind = folder + "/" + motif + ".func";
ifstream hBind(fBind.data());
if(!hBind)
{
cerr << "Can't open " << fBind << endl;
return 1;
}
while(hBind.good())
{
string strLn;
getline(hBind, strLn);
if(strLn == "")
continue;
istringstream gStrm(strLn);
string gene;
int nb;
gStrm >> gene >> nb;
str2upper(gene);
if(genset.find(gene) == genset.end())
continue;
VGB vgb;
for(int j = 0; j < nb; j++)
{
string site;
gStrm >> site;
vgb.e.push_back(extrbnd(site));
}
onebind.e[gene] = vgb;
}
return 0;
}
// Load all motifs' binding.
int loadbind(MotifMap& allbind, const vector<string>& motiflst, const set<string>& genset, const string& folder)
{
for(size_t i = 0; i < motiflst.size(); i++)
{
const string& motif = motiflst[i];
//GBMap onemap; // Gene binding map for one motif only.
//pair<string, GBMap> elem(motif, onemap); // An element for motif map.
//allbind.e.insert(elem);
if(loadone(allbind.e[motif], motif, genset, folder) != 0)
return 1;
}
return 0;
}
// A version for motif score list.
int loadbind(MotifMap& allbind, const vector<MotifScore>& mscor, const set<string>& genset, const string& folder)
{
for(size_t i = 0; i < mscor.size(); i++)
{
const string& motif = mscor[i].name;
//GBMap onemap; // Gene binding map for one motif only.
//pair<string, GBMap> elem(motif, onemap); // An element for motif map.
//allbind.e.insert(elem);
if(loadone(allbind.e[motif], motif, genset, folder) != 0)
return 1;
}
return 0;
}
// Extract binding of a site from a string.
GBinding extrbnd(const string& s)
{
GBinding gb;
string orien = s.substr(0, s.find(','));
gb.orien = orien[0];
size_t semi = s.find(',', 2);
string score = s.substr(2, semi - 2);
gb.score = atof(score.data());
string loc = s.substr(semi + 1);
gb.loc = atoi(loc.data());
return gb;
}
// Comparing routine for sorting motif scores.
bool cmp(MotifScore s0, MotifScore s1)
{
// Arrange the TRANSFAC motifs on top if necessary.
/*if((s0.name[3] != 'X' || !isnum(s0.name[2])) && (s1.name[3] == 'X' && isnum(s1.name[2])))