File: DInDel.cpp

package info (click to toggle)
dindel 1.01%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 940 kB
  • ctags: 1,130
  • sloc: cpp: 15,773; makefile: 40
file content (4309 lines) | stat: -rw-r--r-- 140,889 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
/*    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <set>
#include <algorithm>
#include <boost/program_options.hpp>
#include <boost/math/special_functions/digamma.hpp>
#include <seqan/align.h>
#include <seqan/graph_align.h>
#include <boost/foreach.hpp>
#include "bam.h"
#include "DInDel.hpp"
#include "Haplotype.hpp"
#include "HaplotypeDistribution.hpp"
#include "ObservationModelFB.hpp"
#include "Utils.hpp"
#include "faidx.h"
#include "GetCandidates.hpp"
#include "ObservationModelSeqAn.hpp"
#include "VariantFile.hpp"
#include "Faster.hpp"
#include <ext/hash_map>
#include <exception>

const int USECALLWINDOW=0;
//using namespace seqan;
using namespace seqan;
namespace po = boost::program_options;

using namespace std;
//using namespace fasta;


DetInDel::DetInDel(const string & bfName, const Parameters & _params, int multipleFiles) : params(_params)

{
	fai=NULL;
	if (params.alignAgainstReference) {
		fai = fai_load(params.refFileName.c_str());
		if (!fai) {
			cerr << "Cannot open reference sequence file." << endl;
			params.alignAgainstReference=false;
			exit(1);
		}
	}

	if (multipleFiles==0) {
		myBams.push_back(new MyBam(bfName));
	} else {

		ifstream file(bfName.c_str());
		if (!file.is_open()) {
			   cout << "Cannot open file with BAM files:  " << bfName << endl;
			   throw string("File open error.");
		}
		while (!file.eof()) {
			string line;
			getline(file, line);
			if (!line.empty()) {
				istringstream is(line);
				string fname;
				is >> fname;
				if (!fname.empty()) {
					cout << "Reading BAM file " << fname << endl;
					myBams.push_back(new MyBam(fname));
					myBamsFileNames.push_back(fname);
				}
			}
		}
		file.close();
	}
}

DetInDel::~DetInDel()
{
	if (params.alignAgainstReference && fai) {
		fai_destroy(fai);
	}
	for (size_t b=0;b<myBams.size();b++) delete myBams[b];
}

void DetInDel::analyzeDifference(const pair<Haplotype, Haplotype> & hp1, const vector<Read> & reads,  uint32_t leftPos, uint32_t rightPos)
{
	cout << "Inference results" << endl;

	if (params.analyzeLowFreqDiffThreshold<-100.0) {
		size_t offset=50;
		cout << "Haplotype pair: " << endl;
		cout << hp1.first << endl << hp1.second << endl;


		cout << "h.1 alignment: " << endl;
		cout << string(offset,' ') << hp1.first.seq << endl;
		for (size_t r=0;r<reads.size();r++) {
			ObservationModelFBMax om(hp1.first, reads[r], leftPos,params.obsParams);
			MLAlignment ml=om.calcLikelihood();

			double lm=ml.ll;
			ObservationModelFBMax op(hp1.second, reads[r], leftPos, params.obsParams);
			MLAlignment ml2=op.calcLikelihood();
			double lp=ml2.ll;

			if (lm>=lp) om.printAlignment(offset);
		}

		cout << "h.2 alignment: " << endl;
		cout << string(offset,' ') << hp1.second.seq << endl;
		for (size_t r=0;r<reads.size();r++) {
			ObservationModelFBMax om(hp1.first, reads[r], leftPos,params.obsParams);
			MLAlignment ml=om.calcLikelihood();

			double lm=ml.ll;
			ObservationModelFBMax op(hp1.second, reads[r], leftPos, params.obsParams);
			MLAlignment ml2=op.calcLikelihood();
			double lp=ml2.ll;

			if (lp>=lm) op.printAlignment(offset);


		}



	} else {

		double ll=0.0,l1=0.0, l2=0.0;
		vector<size_t > show;
		for (size_t r=0;r<reads.size();r++) {
			ObservationModelFBMax om(hp1.first, reads[r], leftPos,params.obsParams);
			MLAlignment ml=om.calcLikelihood();

			double lm=ml.ll;
			ObservationModelFBMax op(hp1.second, reads[r], leftPos, params.obsParams);
			MLAlignment ml2=op.calcLikelihood();
			double lp=ml2.ll;
			double dll=log(exp(lp)+exp(lm))+log(.5);
			l1+=(addLogs(lm,lm)+log(.5));
			l2+=(addLogs(lp,lp)+log(.5));
			ll+=dll;

			if (lp-lm>params.analyzeLowFreqDiffThreshold) {
				show.push_back(r);
			}
	//		cout << "read[" << r <<"]: 1-mq: " << 1.0-reads[r].mapQual << " first hap lik: " << lm << " second hap lik: " << lp << " combined: " << dll << " lp+lm: " << ll << " lm+lm: " << l1 << " lp+lp: " << l2 << endl;

		}

		size_t offset=50;
		if (show.size()) {
			cout << "Haplotype pair: " << endl;
			cout << hp1.first << endl << hp1.second << endl;


			cout << "h.1 alignment: " << endl;
			cout << string(offset,' ') << hp1.first.seq << endl;

			for (size_t i=0;i<show.size();i++) {
				Read rr=reads[show[i]];
				ObservationModelFBMax om2(hp1.first, rr, leftPos, params.obsParams);
				om2.calcLikelihood();

		//		om2.computeMarginals();
				om2.printAlignment(offset);
			}

			cout << endl << endl;
			cout << "h.2 alignment: " << endl;
			cout << string(offset,' ') << hp1.second.seq << endl;

			for (size_t i=0;i<show.size();i++) {
				Read rr=reads[show[i]];
				ObservationModelFBMax om2(hp1.second, rr, leftPos, params.obsParams);
				om2.calcLikelihood();
		//		om2.computeMarginals();
				om2.printAlignment(offset);
			}
		}
		else { cout << "No differences in log-likelihoods over threshold." << endl; };
	}

}

void DetInDel::showAlignments(const pair<Haplotype, Haplotype> & hp1, const vector<Read> & reads,  uint32_t leftPos, uint32_t rightPos)
{
	cout << "Inference results" << endl;

	double ll=0.0;
	int offset=50;
	vector <double> lf(reads.size(),0.0), ls(reads.size(),0);
	cout << "h.1 alignment: " << endl;
	cout << string(offset,' ') << hp1.first.seq << endl;
	for (size_t r=0;r<reads.size();r++) {
		ObservationModelFBMax om(hp1.first, reads[r], leftPos,params.obsParams);
		double lm=om.getLogLikelihood();
		lf[r]=lm;
		if (lm<params.analyzeLowFreqDiffThreshold) {
			om.printAlignment(offset);
		}
	}

	cout << "h.2 alignment: " << endl;
	cout << string(offset,' ') << hp1.second.seq << endl;
	for (size_t r=0;r<reads.size();r++) {
		ObservationModelFBMax om(hp1.second, reads[r], leftPos,params.obsParams);
		double lm=om.getLogLikelihood();
		ls[r]=lm;
		if (lm<params.analyzeLowFreqDiffThreshold) {
			om.printAlignment(offset);
		}
		ll+=addLogs(lf[r],ls[r])+log(.5);
	}
	cout << "Total loglikelihood: " << ll << endl;


}

void DetInDel::showAlignmentsPerHaplotype(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks, uint32_t candPos, uint32_t leftPos)
{
	cout << "ALIGNMENTS" << endl;

	vector<std::set<size_t> > maxHap(haps.size());
	for (size_t r=0;r<reads.size();r++) {
		size_t idx=0;
		double ml=-HUGE_VAL;
		for (size_t h=0;h<haps.size();h++) {
			if (liks[h][r].ll>ml) {
				ml=liks[h][r].ll;
				idx=h;
			}
		}
		maxHap[idx].insert(r);
	}

	int offset=50;
	for (size_t h=0;h<haps.size();h++) {
		cout << "*******************************************" << endl;
		cout << endl << "HAPLOTYPE " << h << endl << endl;
		cout << string(offset,' ') << haps[h].seq << endl;
		BOOST_FOREACH(size_t r, maxHap[h]) {
			ObservationModelFBMax om(haps[h], reads[r], leftPos,params.obsParams);
			om.calcLikelihood();
			om.printAlignment(offset);
		}
	}


}




string DetInDel::getRefSeq(uint32_t lpos, uint32_t rpos)
{
	if (!fai) throw string("FAI error.");

	char *str;
	char *ref;

	str = (char*)calloc(strlen(params.tid.c_str()) + 30, 1);
	sprintf(str, "%s:%d-%d", params.tid.c_str(), lpos, rpos);
	int len;
	ref = fai_fetch(fai, str, &len);
	if (len==0) throw string("faidx error: len==0");
	free(str);
	string res(ref);
	free(ref);

	transform(res.begin(), res.end(), res.begin(), ::toupper);
	return res;
}


double DetInDel::getMaxHap(Haplotype & h1, Haplotype &h2, HapPairLik & hpl, const vector<Haplotype> & haps, vector<HapPairLik> & likPairs)
{

	size_t idx=0, midx;
	double maxll=-HUGE_VAL;
	for (idx=0;idx<likPairs.size();idx++) {
			double ll=likPairs[idx].ll;
			if (ll>maxll) {
				maxll=ll;
				midx=idx;
			}
	}
	h1=haps[likPairs[midx].h1];
	h2=haps[likPairs[midx].h2];

	/*
	cout << "getMaxHap: " << midx <<  " h1: " << likPairs[midx].h1 << " h2: " << likPairs[midx].h2 << endl;
	cout << "indelcoverage h1: ";
	for (map<int, VariantCoverage>::const_iterator it=likPairs[midx].hapIndelCoverage1.begin();it!=likPairs[midx].hapIndelCoverage1.end();it++) {
		cout << "[" << it->second.nf << "," << it->second.nr << "]";
	}
	cout << endl;
	cout << "indelcoverage h2: ";
	for (map<int, VariantCoverage>::const_iterator it=likPairs[midx].hapIndelCoverage2.begin();it!=likPairs[midx].hapIndelCoverage2.end();it++) {
		cout << "[" << it->second.nf << "," << it->second.nr << "]";
	}
	cout << endl;
	*/

	hpl=likPairs[midx];
	return maxll;
}

void DetInDel::outputMaxHap(ostream *output, const string & prefix, const vector<Haplotype> & haps, vector<HapPairLik> & likPairs)
{

	Haplotype h1, h2;
	HapPairLik hpl;
	getMaxHap(h1,h2, hpl, haps, likPairs);
	*output << prefix << " " << hpl.ll << " " << hpl.numFirst << " " << hpl.numSecond << " " << hpl.numIndFirst << " " << hpl.numIndSecond << " " << hpl.numOffBoth << " " << h1.seq << " " << h2.seq << " ";
	for (map<int, AlignedVariant>::const_iterator it=h1.indels.begin();it!=h1.indels.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
	*output << "!";
	for (map<int, AlignedVariant>::const_iterator it=h2.indels.begin();it!=h2.indels.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
	*output << "!";
	for (map<int, AlignedVariant>::const_iterator it=h1.snps.begin();it!=h1.snps.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
	*output << "!";
	for (map<int, AlignedVariant>::const_iterator it=h2.snps.begin();it!=h2.snps.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
	*output << endl;


}

void DetInDel::outputTopHaps(ostream *output, const string & prefix, const vector<Haplotype> & haps, vector<HapPairLik> & likPairs, int n)
{
	// output n most likely haplotype pairs

	for (int ns=0;ns<n && ns<int(likPairs.size());ns++) {
		const Haplotype & h1 = haps[likPairs[ns].h1];
		const Haplotype & h2 = haps[likPairs[ns].h2];
		const HapPairLik & hpl = likPairs[ns];
		*output << prefix << " " << ns+1 << " " << hpl.ll << " " << hpl.numFirst << " " << hpl.numSecond << " " << hpl.numIndFirst << " " << hpl.numIndSecond << " " << hpl.numOffBoth << " " << h1.seq << " " << h2.seq << " ";
		for (map<int, AlignedVariant>::const_iterator it=h1.indels.begin();it!=h1.indels.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
		*output << "!";
		for (map<int, AlignedVariant>::const_iterator it=h2.indels.begin();it!=h2.indels.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
		*output << "!";
		for (map<int, AlignedVariant>::const_iterator it=h1.snps.begin();it!=h1.snps.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
		*output << "!";
		for (map<int, AlignedVariant>::const_iterator it=h2.snps.begin();it!=h2.snps.end();it++) if (it->second.getString()!="*REF") *output << "[" << it->first << "," << it->second.getStartRead() << "," << it->second.getString() << "]";
		*output << endl;
	}

}

void DetInDel::outputHapsAndFreqs(ostream *output, const string & prefix, const vector<Haplotype> & haps, const vector<double> & freqs, uint32_t leftPos)
{
	// output n most likely haplotype pairs

	for (size_t h=0;h<haps.size();h++) {
		const Haplotype & h1 = haps[h];
		*output << prefix << " " << h+1 << " " << freqs[h] << " ";
		for (map<int, AlignedVariant>::const_iterator it=h1.indels.begin();it!=h1.indels.end();it++) if (it->second.getString()!="*REF") *output << leftPos+it->first << "," << it->second.getString() << "|";
		//*output << "!";
		//for (map<int, AlignedVariant>::const_iterator it=h1.snps.begin();it!=h1.snps.end();it++) if (it->second.getString()!="*REF") *output << leftPos+it->first << "," << it->second.getString() << "|";
		*output << endl;
	}

}



void DetInDel::empiricalDistributionMethod(int index, const vector<Read> & reads, uint32_t pos, uint32_t leftPos, uint32_t rightPos, const AlignedCandidates & candidateVariants,  OutputData & oData, OutputData & glfData)
{
	vector<Haplotype> haps;

	vector<vector<MLAlignment> > liks;
	vector<HapPairLik> likPairs;



	// get the haplotypes
	// changes leftPos and rightPos to haplotype blocks in HDIterator

	// NOTE leftPos will be the left position of the reference sequence each haplotype will be aligned to
	bool skip=getHaplotypes(haps, reads, pos, leftPos, rightPos, candidateVariants);

	if (int(reads.size()*haps.size())>params.maxHapReadProd) {
		stringstream os;
		os << "skipped_numhap_times_numread>" << params.maxHapReadProd;
		throw os.str();
	}

	int refSeqPos=leftPos;

	if (skip) {
		cerr << "tid: " << params.tid << " pos:  " << pos << " SKIPPING!" << endl;
	}
	else {

		if (!params.quiet) cout << "[empiricalDistributionMethod] Number of haplotypes: " << haps.size() << endl;
		// compute likelihood of every read given every haplotype

		if (params.estimateHapFreqs) {
			vector<double> hapFreqs;
			map <int, vector<tuple<AlignedVariant, double, double> > > posteriors;
			vector<HapEstResult> her;

			OutputData::Line prefilledLine(oData);
			prefilledLine.set("index", index);
			prefilledLine.set("tid", params.tid);
			prefilledLine.set("center_position",pos);
			prefilledLine.set("num_reads", reads.size());
			prefilledLine.set("msg","ok");
			// string rseq = getRefSeq(leftPos+1, rightPos+1);
			prefilledLine.set("lpos",leftPos);
			prefilledLine.set("rpos",rightPos);
			// prefilledLine.set("refseq", rseq);


			vector<int> onHap(reads.size(),1); // which reads were mapped inside the haplotype window given an artificially high mapping quality

			if (params.slower) {
				computeLikelihoods(haps, reads, liks, leftPos, rightPos, onHap);
			} else {
				computeLikelihoodsFaster(haps, reads, liks, leftPos, rightPos, onHap);
				// int nrOffAll=0; // number of reads mapped outside all haplotypes
				// for (size_t x=0;x<onHap.size();x++) if (!onHap[x]) nrOffAll++;
			}


			int numReadOffAllHaps=0;
			int numHQReads=0;
			for (size_t r=0;r<reads.size();r++) if (reads[r].mapQual > (1.0-1e-6) ) {
				numHQReads++;
				int offall=1;
				for (size_t h=0;h<haps.size();h++) if (!liks[h][r].offHap) offall=0;
				if (offall) numReadOffAllHaps++;
			}
			prefilledLine.set("num_off_hap", numReadOffAllHaps);
 			prefilledLine.set("num_hqreads", numHQReads);


			//estimateHaplotypeFrequenciesPosterior(haps, reads,liks, hapFreqs, posteriors, pos, leftPos, glfOutput);
			/*
			estimateHaplotypeFrequenciesBayesEM(haps, reads,liks, hapFreqs, her, pos, leftPos, glfOutput, "all");
			BOOST_FOREACH(HapEstResult hr, her) {
				//cout << "EMA " << params.tid << " "  << pos << " " << leftPos+hr.pos << " " <<reads.size() << " " << hr.av.getString() << " " << hr.prob << " " << hr.freq << " " << hr.freq*double(reads.size()) << " " << hr.nrf << " " << hr.nrr << endl;
				OutputData::Line EMALine = prefilledLine;
				EMALine.set("analysis_type", "EMA");
				EMALine.set("realigned_position", leftPos+hr.pos);
				EMALine.set("first_called_all", hr.av.getString());
				EMALine.set("post_prob_variant", hr.prob);
				EMALine.set("est_freq", hr.freq);
				EMALine.set("first_var_cover_forward", hr.nrf);
				EMALine.set("first_var_cover_reverse", hr.nrr);
				oData.output(EMALine);
			}
			*/

			hapFreqs.clear();
			her.clear();
			estimateHaplotypeFrequenciesBayesEM(haps, reads,liks, hapFreqs, her, pos, leftPos, rightPos, glfData, index, candidateVariants, params.bayesType);

			for (size_t x=0;x<her.size();x++) {
				HapEstResult & hr = her[x];
				//cout << "EMSV " << params.tid << " "  << pos << " " << leftPos+hr.pos << " " <<reads.size() << " " << hr.av.getString() << " " << hr.prob << " " << hr.freq << " " << hr.freq*double(reads.size()) << " " << hr.nrf << " " << hr.nrr << endl;

				int var_in_window=0;
				const AlignedVariant & avar = hr.av;
				const AlignedVariant *av = candidateVariants.findVariant(hr.pos+leftPos, avar.getType(), avar.getString());
				if (av!=NULL) {
					var_in_window=1;
				}

				OutputData::Line EMSVLine = prefilledLine;
				EMSVLine.set("analysis_type", params.bayesType);
				EMSVLine.set("realigned_position", leftPos+hr.pos);
				EMSVLine.set("first_called_all", hr.av.getString());
				EMSVLine.set("post_prob_variant", hr.prob);
				EMSVLine.set("est_freq", hr.freq);
				EMSVLine.set("first_var_cover_forward", hr.nrf);
				EMSVLine.set("first_var_cover_reverse", hr.nrr);
				EMSVLine.set("was_candidate_in_window",var_in_window);
			//	oData.output(EMSVLine);
			}




			if (params.outputRealignedBAM && params.slower) {
				stringstream os;
				os << index << "_" << params.tid << "_" << leftPos+params.minReadOverlap << "_" << rightPos-params.minReadOverlap  << ".bam";

				vector < CIGAR > cigars(reads.size());
				for (size_t r=0;r<reads.size();r++) {
					if (onHap[r]) {
						double llmax=-HUGE_VAL;
						int hidx=0;
						for (size_t h=0;h<haps.size();h++) if (liks[h][r].ll>llmax) {
							llmax=liks[h][r].ll;
							hidx=h;
						}
						cigars[r]=getCIGAR(haps[hidx], reads[r], liks[hidx][r], refSeqPos);
					}
				}
				int leftOk = leftPos + params.minReadOverlap;
				int rightOk = rightPos - params.minReadOverlap;
	
				string newBAMFileName=params.fileName;
				newBAMFileName.append(".ra.").append(os.str());
				writeRealignedBAMFile(newBAMFileName, cigars, reads, onHap, myBams[0]->bh);

				if (params.processRealignedBAM != "no")  {
					stringstream cmd;
					cmd << params.processRealignedBAM << " " << newBAMFileName << " " << params.fileName << "_realigned" << " " << params.tid << " " << leftOk << " " << rightOk;
					cout << "Executing: " << cmd.str() << endl;
					system(cmd.str().c_str());
				}


				/*
				newBAMFileName=params.fileName;
				newBAMFileName.append(".ua.").append(os.str());
				writeUnalignedBAMFile(newBAMFileName, reads, onHap, myBams[0].bh);
				*/
			}

		}
		if (params.showHapAlignments) {
			showAlignmentsPerHaplotype(haps, reads, liks, pos, leftPos);
		}

		if (params.doDiploid) {
		//	cout << "A" << endl;
			vector<double> hapFreqs;
			map <int, vector<tuple<AlignedVariant, double, double> > > posteriors;
			vector<HapEstResult> her;

			vector<int> onHap(reads.size(),1); // which reads were mapped inside the haplotype window given an artificially high mapping quality

			if (params.slower) {
				computeLikelihoods(haps, reads, liks, leftPos, rightPos, onHap);
			} else {
				computeLikelihoodsFaster(haps, reads, liks, leftPos, rightPos, onHap);
			}

			diploidGLF(haps, reads, liks, hapFreqs, her, pos, leftPos, rightPos, glfData, index,  candidateVariants,"dip");


			//statisticsHaplotypePair(haps,reads,liks, hpl, prefilledLine);

			/*
			string prefix("FMAP");


			callIndel(haps, reads, liks, likPairs, pos, leftPos, rightPos, prefix, prefilledLine, oData);
			callSNP(haps, reads, liks, likPairs, pos, leftPos, rightPos, prefix, prefilledLine, oData);
			*/
			/*
			if (!(nci==0 && ncs==0 && params.printCallsOnly)) {
				stringstream os; os << " " << params.tid << " " << pos << " " << leftPos; prefix.append(os.str());
				outputTopHaps(&output, prefix, haps, likPairs, params.numOutputTopHap);
			}
			*/

			/*
			if (params.analyzeLowFreq) {
				pair<Haplotype, Haplotype> oh;
				oh.first=h2;
				oh.second=h1;
				analyzeDifference(oh, reads, leftPos, rightPos);

				oh.first=h1;
				oh.second=h2;
				analyzeDifference(oh, reads, leftPos, rightPos);
				//oh.first.printHaps();
				//oh.second.printHaps();
			}
			*/

			if (params.outputRealignedBAM && params.slower) {
				// computes pair likelihoods using priors
				computePairLikelihoods(haps, reads, liks, likPairs, true,candidateVariants, leftPos);
				Haplotype h1, h2; HapPairLik hpl;
				getMaxHap(h1, h2, hpl, haps, likPairs);

				vector < CIGAR > cigars(reads.size());
				for (size_t r=0;r<reads.size();r++) {
						int hmax = hpl.h1;
						if (fabs(liks[hpl.h1][r].ll-liks[hpl.h2][r].ll)<1e-8) {
							if (haps[hpl.h1].countIndels()<haps[hpl.h2].countIndels()) hmax = hpl.h1; else hmax = hpl.h2;
						} else {
							if (liks[hpl.h1][r].ll>liks[hpl.h2][r].ll) {
								hmax = hpl.h1;
							} else {
								hmax = hpl.h2;
							}
						}
						const Haplotype & hx = haps[hmax];
						const Read & rd = reads[r];
						const MLAlignment & ml = liks[hmax][r];
						cigars[r]=getCIGAR(haps[hmax], reads[r], liks[hmax][r], refSeqPos);
				}

				stringstream os;
				int leftOk = leftPos + params.minReadOverlap;
				int rightOk = rightPos - params.minReadOverlap;
				os << index << "_" << params.tid << "_" << leftPos+params.minReadOverlap << "_" << rightPos-params.minReadOverlap  << ".bam";
				string newBAMFileName=params.fileName;
				newBAMFileName.append(".ra.").append(os.str());
				writeRealignedBAMFile(newBAMFileName, cigars, reads, onHap, myBams[0]->bh);
				/*
				newBAMFileName=params.fileName;
				newBAMFileName.append(".ua.").append(os.str());
				writeUnalignedBAMFile(newBAMFileName, reads, onHap, myBams[0]->bh);
				*/
				if (params.processRealignedBAM != "no")  {
					stringstream cmd;
					cmd << params.processRealignedBAM << " " << newBAMFileName << " " << params.fileName << "_realigned" << " " << params.tid << " " << leftOk << " " << rightOk;
					cout << "Executing: " << cmd.str() << endl;
					system(cmd.str().c_str());
				}


			}


		}
//		glf.writeToFile(string(""), output);
	}

}



void DetInDel::writeUnalignedBAMFile(const string & fileName, const vector<Read> & reads, const vector<int> & onHap, const bam_header_t *bh=NULL)
{

	if (onHap.size()!=reads.size()) return;
	bool hasUnaligned=false;
	for (size_t x=0;x<onHap.size();x++) if (!onHap[x]) {
		hasUnaligned=true;
		break;
	}
	if (!hasUnaligned) return;

	bamFile bf = bam_open(fileName.c_str(),"wb");
	if (bf==NULL) throw string("Cannot open bamfile ").append(fileName).append(" for writing!");

	if (bh!=NULL) {
		bam_header_write(bf, bh);
	}

	for (size_t r=0;r<reads.size();r++) if (!onHap[r]) {
		bam1_t *b=reads[r].bam;
		if (bam_write1(bf,b)<=0) throw string("Error writing to unalignable read to bamfile.");
	}

	bam_close(bf);
}

void DetInDel::writeRealignedBAMFile(const string & fileName, const vector<CIGAR> & cigars, const vector<Read> & reads, const vector<int> & onHap, const bam_header_t *bh=NULL)
{
		if (cigars.size()!=reads.size()) throw string("Problem with the cigars.");

		bamFile bf = bam_open(fileName.c_str(),"wb");
		if (bf==NULL) throw string("Cannot open bamfile ").append(fileName).append(" for writing!");

		if (bh!=NULL) {
				bam_header_write(bf, bh);
		}

		for (size_t r=0;r<reads.size();r++) {

				bam1_t *b=reads[r].bam;

				if (onHap[r]) {
						bam1_t *nb=bam_init1();

						uint32_t old_ncig=b->core.n_cigar;
						uint32_t new_ncig=cigars[r].size();
						int old_data_len=b->data_len;
						int new_data_len=old_data_len - old_ncig*4 + new_ncig*4;

						*nb=*b;
						nb->data = (uint8_t*)calloc(new_data_len, 1);
						nb->data_len=new_data_len;
						nb->m_data=nb->data_len;
						// copy cigar
						for (uint32_t n=0;n<new_ncig;n++) {
								bam1_cigar(nb)[n]=(  (((uint32_t) cigars[r][n].second) << BAM_CIGAR_SHIFT) | ( ( (uint32_t) cigars[r][n].first ) )  );
						}
						nb->core.n_cigar=(unsigned int) new_ncig;

						for (size_t n=0;n<b->core.l_qname;n++) {
								nb->data[n]=b->data[n];
						}

						int y=b->core.l_qname+4*new_ncig;
						for (int n=b->core.l_qname+4*old_ncig;n<b->data_len;n++,y++) {
								nb->data[y]=b->data[n];
						}

						// update position of read

						nb->core.pos=cigars[r].refPos;
						// update insert size if mapped
						nb->core.isize=cigars[r].refPos-nb->core.mpos;
						if (bam_write1(bf,nb)<=0) throw string("Error writing alignment to realigned BAM file.");
						bam_destroy1(nb);
				} else {
						if (bam_write1(bf,b)<=0) throw string("Error writing alignment to realigned BAM file.");
				}
		}

		bam_close(bf);
}


DetInDel::CIGAR DetInDel::getCIGAR(const Haplotype & hap, const Read & read, const MLAlignment & ml, int refSeqStart)
{
	if (hap.ml.hpos.size()!=hap.size()) throw string("Haplotype has not been aligned!");
	if (ml.hpos.size()!=read.size()) throw string("Read is not properly aligned!");
	const MLAlignment & hml=hap.ml; // alignment of haplotype to reference

	//string qname = bam1_qname(read.getBam());
	const int debug = 0;
	/*
	if (qname == "IL8_4337:8:102:11530:1494") {
		cout << "YES" << endl;
		cout << qname << endl;
		debug = 1;
	}
	*/

	vector<int> npos(read.size()); // npos records position of read base on the reference sequence
	for (int b=0;b<int(read.size());b++) {
		if (ml.hpos[b]>=0) npos[b]=hml.hpos[ml.hpos[b]]; else npos[b]=ml.hpos[b];
	}

	if (debug) {
		for (size_t h=0;h<npos.size();h++) {
			cout << "[" << h << "," << npos[h] << "]";
		}
		cout << endl;
		cout << endl;

		for (size_t h=0;h<ml.hpos.size();h++) {
			cout << "[" << h << "," << ml.hpos[h] << "]";
		}
		cout << endl;
		cout << endl;
		for (size_t h=0;h<hml.hpos.size();h++) {
				cout << "[" << h << "," << hml.hpos[h] << "]";
			}
			cout << endl;
	}
	CIGAR cig;

	int b=0, prevponr=0; // position on reference previous base aligned on the reference (ie no deletion/insertion/LO/RO)

	// determine last base in read aligned to the haplotype
	b=read.size()-1;
	while (npos[b]<0) b--;
	int lastbonh=b;

	if (lastbonh<0) {
		// clip the whole read, read is de facto off haplotype
		cig.push_back(CIGAR::CIGOp(BAM_CSOFT_CLIP, read.size()));
		return cig;
	}


	if (debug) {
		cout << "lastbonh: " << lastbonh << endl;
	}
	// find first base in read that is aligned to haplotype and to reference
	// all sequence before that is considered 'soft clipped', ie not aligned. This may include sequence that matches perfectly to the reference
	b=0;
	while (npos[b]<0) b++;
 	if (b>0) cig.push_back(CIGAR::CIGOp(BAM_CSOFT_CLIP, b));
 	prevponr=npos[b];
 	cig.refPos=refSeqStart+prevponr;

 	int curr_cop=BAM_CMATCH;
 	int len_curr_cop=1;



 	while (b<lastbonh) {

 		int chp=npos[b]; // position on reference of current base in read
 		int nhp=npos[b+1];

 		if (nhp==MLAlignment::INS) {
 			if (chp==MLAlignment::INS) {
 				// stay on inserted sequence
 				if (curr_cop!=BAM_CINS) throw string("Error(1)!");
 				len_curr_cop++;
 			} else {
 				if (chp>=0) {
 					// going from on reference to insertions
 					if (curr_cop!=BAM_CMATCH) throw string("Error(2)!");
 					// write CIGAR
					cig.push_back(CIGAR::CIGOp(BAM_CMATCH,len_curr_cop));

					// update current CIGAR operation
					len_curr_cop=1;
					curr_cop=BAM_CINS;

					prevponr=chp;
 				} else throw string("How is this possible? (1)");
 			}

 		} else if (chp>=0 && nhp>=0 && nhp-chp==1) {
 	 	  // MATCH to MATCH
 			if (curr_cop!=BAM_CMATCH) {
 				cout << "b: " << b << " chp: " << chp << " nhp: " << nhp << endl;
 				throw string("Error(3)!");
 			}
 			len_curr_cop++;
 			prevponr=nhp;
 		} else if (chp>=0 && nhp>=0 && nhp-chp>1) {
 			// deletion
 			if (curr_cop!=BAM_CMATCH) throw string("Error(4)!");
 			// write CIGAR
 			cig.push_back(CIGAR::CIGOp(BAM_CMATCH,len_curr_cop));

 			// write deletion CIGAR
 			cig.push_back(CIGAR::CIGOp(BAM_CDEL,nhp-chp-1));

 			curr_cop=BAM_CMATCH;
 			len_curr_cop=1;

 			prevponr=nhp;
 		} else if (chp==MLAlignment::INS && nhp-prevponr==1) {
 			// from inserted bases to bases matched to the reference
 			cig.push_back(CIGAR::CIGOp(BAM_CINS,len_curr_cop));

 			//
 			curr_cop=BAM_CMATCH;
 			len_curr_cop=1;

 			prevponr=nhp;
 		} else if (chp==MLAlignment::INS && nhp-prevponr>1) {
 			// next base is again on reference but more than 1 reference base from the last read base aligned to the haplotype
 			cig.push_back(CIGAR::CIGOp(BAM_CINS,len_curr_cop));
 			cig.push_back(CIGAR::CIGOp(BAM_CDEL,nhp-prevponr-1));

 			curr_cop=BAM_CMATCH;
 			len_curr_cop=1;

 			prevponr=nhp;
 		}
 		b++;
 	}

 	// write last cigar
 	cig.push_back(CIGAR::CIGOp(curr_cop,len_curr_cop));

 	// write soft_clip at the end
 	if (read.size()-1 - lastbonh>0) {
 		cig.push_back(CIGAR::CIGOp(BAM_CSOFT_CLIP,read.size()-1 - lastbonh));
 	}

 	/*
 	cout << "cig: ";
 	BOOST_FOREACH(CIGAR::CIGOp cop, cig) {
 		cout << "(" << cop.first << "," << cop.second << ")" ;
 	}
 	cout << endl;
	*/
 	return cig;
}


void DetInDel::getReads(uint32_t leftPos, uint32_t rightPos, vector<Read> & reads, uint32_t & oldLeftPos, uint32_t & oldRightFetchReadPos, vector<Read *> & readBuffer, bool reset)
{
	// filter using map quality
	class SortFunc {
	public:
		static bool sortFunc(const Read & r1, const Read & r2)
		{
			// sort in decreasing order
			if (r1.mapQual>r2.mapQual) return true; else return false;
		}
	};

	if (leftPos<oldLeftPos) {
		cerr << "Windows are not sorted!" << endl;
		exit(3);
	}

	reads.clear();

	if (int(rightPos-leftPos)<3*params.minReadOverlap) throw string("Choose a larger width or a smaller minReadOverlap.");


	int maxDev = int ( libraries.getMaxInsertSize());

	//maxDev = 100;
	//cerr << "CHANGE THIS CHANGE THIS" << endl;

	string_hash< list<int> > mapped_name_to_idx, unmapped_name_to_idx; // query name to read idx
	string_hash< list<int> >::const_iterator hash_it;

	int numUnknownLib = 0;
	string_hash <int> unknownLib;
	const int LEFTPAD = 200;
	// note the idea is to get only consider reads starting at position leftMostReadPos (and not ones merely overlapping)
	// LEFTPAD should take care of overlap effects (note that leftMostReadPos is already generous, based on library insert size)


	uint32_t rightFetchReadPos = rightPos+maxDev;
	uint32_t rightMostReadPos = rightPos+maxDev;

	uint32_t leftFetchReadPos = leftPos-maxDev-LEFTPAD;
	uint32_t leftMostReadPos = leftPos-maxDev-LEFTPAD; // left most position of reads we want to seriously consider

	// reset indicates whether we want to remake the readBuffer

	vector<Read*> newReadBuffer;
	bool leftOverlapsPrevious = false;
	if (reset) {
		for (size_t r=0;r<readBuffer.size();r++) {
			if (readBuffer[r]!=NULL) delete readBuffer[r];
		}
		readBuffer.clear();
		oldRightFetchReadPos = rightFetchReadPos;
	} else {
		// clear reads that do not overlap with new window [leftMostReadPos, rightMostReadPos]

		for (size_t r=0;r<readBuffer.size();r++) {
			uint32_t rend = readBuffer[r]->getEndPos();
			uint32_t rbeg = readBuffer[r]->getBam()->core.pos;
			if (rbeg<leftMostReadPos) {
				delete readBuffer[r];
				readBuffer[r]=NULL;
			} else {
				newReadBuffer.push_back(readBuffer[r]);
			}
		}

		// note that it is required that the new leftPos of the window >= the old leftPos
		// therefore if leftFetchReadPos<=oldRightFetchReadPos the new w
		if (leftMostReadPos<oldRightFetchReadPos) {
			leftFetchReadPos = oldRightFetchReadPos;
			leftOverlapsPrevious = true;
		}
	}




	// cout << "leftFetchReadPos: " << leftFetchReadPos << " rightFetchReadPos: " << rightFetchReadPos << " oldRightFetchReadPos: " << oldRightFetchReadPos << endl;
	// cout << "leftMostReadPos: " << leftMostReadPos << " rightMostReadPos: " << rightMostReadPos << " leftOverlapsPrevious: " << int(leftOverlapsPrevious) << endl;
	// store updated readBuffer
	readBuffer.swap(newReadBuffer);

//	cout << "leftPos : " << leftPos << " rightPos: " << rightPos << " maxDev: " << maxDev << endl;


	// first clean readbuffer


	int numReads = readBuffer.size();

	vector<Read> newReads;
	if (leftFetchReadPos<=rightFetchReadPos) {
		cout << "Fetching reads...." << endl;
		for (size_t b=0;b<myBams.size();b++) {
			//bam_fetch(myBams[b].bf, myBams[b].idx, myBams[b].getTID(params.tid), leftPos+params.minReadOverlap, rightPos-params.minReadOverlap, &reads, &Read::fetchFuncVector);
			Read::FetchReadData data(&newReads, int(b), &(this->libraries), &myBams, numReads, params.maxReads*100);
			bam_fetch(myBams[b]->bf, myBams[b]->idx, myBams[b]->getTID(params.tid), leftFetchReadPos , rightFetchReadPos,&data , &Read::fetchFuncVectorPooled);
			numUnknownLib += data.numUnknownLib;
			numReads = data.numReads;
		}
		oldRightFetchReadPos = rightFetchReadPos;
	}

	// add new reads to readBuffer

	for (size_t r=0;r<newReads.size();r++) {
		if (newReads[r].getBam()->core.pos>=leftFetchReadPos) {
			// only store reads that do not overlap with the boundary;
			// reads overlapping with boundary will have been picked up before.
			readBuffer.push_back(new Read(newReads[r]));
		}
	}

	if (0) {
		// check with regular fetch
		vector<Read> tmpReads;
		for (size_t b=0;b<myBams.size();b++) {
			//bam_fetch(myBams[b].bf, myBams[b].idx, myBams[b].getTID(params.tid), leftPos+params.minReadOverlap, rightPos-params.minReadOverlap, &reads, &Read::fetchFuncVector);
			Read::FetchReadData data(&tmpReads, int(b), &(this->libraries), &myBams, numReads, params.maxReads*100);
			bam_fetch(myBams[b]->bf, myBams[b]->idx, myBams[b]->getTID(params.tid), leftMostReadPos , rightMostReadPos,&data , &Read::fetchFuncVectorPooled);
		}
		for (size_t r=0;r<tmpReads.size();r++) {
			if (tmpReads[r].getBam()->core.pos>=leftMostReadPos) {
				string qname = string(bam1_qname(tmpReads[r].getBam()));
				cout << "glp: "  << leftPos << " qname: " << qname << " pos: " << tmpReads[r].pos << " end: " << tmpReads[r].getEndPos() << endl;
			}
		}
	}



	// check readbuffer for duplicates (debugging)
	if (1) {
		string_hash <int> qnameCount;
		for (size_t r=0;r<readBuffer.size();r++) {
			string qname = string(bam1_qname(readBuffer[r]->getBam()));
			//cout << "lp: "  << leftPos << " qname: " << qname << " pos: " << readBuffer[r]->pos << " end: " << readBuffer[r]->getEndPos() << endl;
			string_hash<int>::iterator it = qnameCount.find(qname);
			if (it == qnameCount.end()) {
				qnameCount[qname]=1;
			} else {
				qnameCount[qname]++;
				if (qnameCount[qname]>2) {
					cerr << "Duplicate reads: readbuffer problem!" << endl;
					throw string("duplicate reads!");
				}
			}
		}
	}



	newReads.clear();

	size_t oldNumReads=readBuffer.size();


	// copy readBuffer to reads

	for (size_t r=0;r<readBuffer.size();r++) {
		reads.push_back(Read(*readBuffer[r]));
	}


	// get query names

	vector<int> unmapped;
	for (size_t r=0; r<reads.size();r++) {
		if (reads[r].isUnmapped())  {
			unmapped.push_back(r);
			unmapped_name_to_idx[ string(bam1_qname(reads[r].getBam())) ].push_back(r);
		//	 cout << " __reads[" << r  << "]: " << bam1_qname(reads[r].getBam()) << " UNMAPPED" << endl;
		} else {
			mapped_name_to_idx[ string(bam1_qname(reads[r].getBam())) ].push_back(r);
		//	cout << " __reads[" << r  << "]: " << bam1_qname(reads[r].getBam()) << " pos: " << reads[r].pos << " " << reads[r].getBam()->core.pos << " mpos: " << reads[r].getBam()->core.mpos << " mu: " << reads[r].mateIsUnmapped() << endl;
		}
	}


	// filter reads based on haplotype window, the minimum read overlap for mapped reads, minimum mapping quality and maximum read length

	/*
	cout << "name_to_idx.size: " << mapped_name_to_idx.size() << endl;
	for (hash_it = mapped_name_to_idx.begin();hash_it!=mapped_name_to_idx.end();hash_it++) {
		cout << "hit: " << hash_it->first;
		BOOST_FOREACH(int x, hash_it->second) {
			cout << " " << x;
		}
		cout << endl;
	}
	*/
	int numTIDmismatch = 0, numOrphan =0, numOrphanUnmapped = 0, numInRegion = 0;

	// reads are filtered by setting mapping quality to -1
	vector<Read> filteredReads;
	double minMapQual = params.mapQualThreshold;
	if (minMapQual<0.0) minMapQual=0.0;
	for (int r=0;r<int(reads.size());r++) {
		//cout << "***" << endl;
		//cout << "reads.mapQual " << reads[r].mapQual <<  " bq: " << reads[r].getBam()->core.qual << endl;
		bool filter = false;
		int tf = 0;
		if (reads[r].size()>params.maxReadLength) filter=true;

		if (reads[r].getEndPos()<leftMostReadPos || reads[r].pos>rightMostReadPos) filter=true;

		if (!reads[r].isUnmapped()) {
	//		cout << "mapped" << endl;
			if (reads[r].pos+int(reads[r].size())<int(leftPos)+params.minReadOverlap || reads[r].pos>int(rightPos)-params.minReadOverlap) {
		//		cout << " { " << reads[r].pos+reads[r].size() << " " << leftPos+params.minReadOverlap << " " << reads[r].pos << " " << rightPos-params.minReadOverlap << " } " << endl;
				filter=true;
				tf = 1;
			} else if (reads[r].mateIsUnmapped() == false ){
				if (reads[r].getBam()->core.mtid != reads[r].getBam()->core.tid) {
					// filter = true;
					// cout << "TIDERR: reads[" << r << "]: " << bam1_qname(reads[r].getBam()) << " matePos: " << reads[r].matePos << " mateLen: " << reads[r].mateLen << endl;
					numTIDmismatch++;
				} else {
					// find mate of mapped read
					// filter if we cannot find it (mapped to another chromosome, those are a bit suspicious)

					// lookup mapped read
					hash_it = mapped_name_to_idx.find(string(bam1_qname(reads[r].getBam())));
					if (hash_it == mapped_name_to_idx.end()) { numOrphan++; filter=true; } else {
						if (hash_it->second.size()>2) cerr << "HUH? DUPLICATE READ LABELS???" << endl;
						if (reads[r].mateIsUnmapped() == false) {
							filter = true;
						}

						BOOST_FOREACH(int idx, hash_it->second) {
							if (idx != r) {
								reads[r].mateLen = reads[idx].size();
								reads[r].matePos = reads[idx].pos;
								filter = false;
								if (reads[r].matePos != reads[r].getBAMMatePos()) {
									cerr << "matepos inconsistency!" << endl;
									cerr << reads[r].matePos << " " << reads[r].getBAMMatePos() << endl;
									exit(1);
								}
							}
						}

						if (filter == true) {
							numOrphan++;
							tf = 2;
						}
						//cout << "mapped read: " << r << " " << qname << " pos: " << reads[r].pos << " " << reads[r].getBam()->core.mtid << " " <<  reads[r].getBam()->core.mpos << " mateunmap: " << reads[r].mateIsUnmapped() << endl;
					}
				}
			} else if (reads[r].mateIsUnmapped() == true) {
				reads[r].matePos=reads[r].pos;
				hash_it = unmapped_name_to_idx.find(string(bam1_qname(reads[r].getBam())));
				if (hash_it == unmapped_name_to_idx.end()) { filter=true; } else {
					filter = true;
					if (hash_it->second.size()>2) cerr << "HUH? DUPLICATE READ LABELS???" << endl;
					BOOST_FOREACH(int idx, hash_it->second) {
						if (idx != r) {
							reads[r].mateLen = reads[idx].size();
							filter = false;
						}
					}

				}
				if (filter==true) {
					numOrphan++;
					tf = 3;
				}
			}
			if (filter == false) numInRegion++;

		} else {
	//		cout << " unmapped" << endl;
			// read is unmapped
			if (params.mapUnmappedReads) {
	//			cout << " unmapped " << qname << " ; ";

				// lookup mapped read
				hash_it = mapped_name_to_idx.find(string(bam1_qname(reads[r].getBam())));
				int idx;
				if (hash_it == mapped_name_to_idx.end()) { numOrphanUnmapped++; filter=true; tf = 4;} else {
					if (hash_it->second.size()!=1) {
						cerr << "UNMAPPED READ HAS MORE THAN ONE MATE!" << endl;
						exit(1);
					}
					idx = *(hash_it->second.begin());
					//cout << " FOUND " << idx << endl ;
					// check if mate will overlap with haplotype
					uint32_t range_l, range_r; // range of mate

					int maxInsert = (int) reads[idx].getLibrary().getMaxInsertSize();
					int minInsert = 0;
					uint32_t rpos = reads[idx].pos;

	//				cout << "idx: " << idx << " unmapped: " << reads[idx].isUnmapped() << " rpos: " << rpos << " isreverse: " << reads[idx].isReverse() << endl;
					if (reads[idx].isReverse()) {
						range_l = rpos-maxInsert;
						range_r = rpos-minInsert;
					} else {
						range_l = rpos+minInsert;
						range_r = rpos+maxInsert;
					}

	//				cout << "insert: " << insert << " std: " << std << " range_l : " << range_l << " range_r: " << range_r << " leftPos: " << leftPos << "  rightPos: " << rightPos << endl;

					if (range_r>leftPos && range_l<rightPos) {
						numInRegion++;
						filter=false;
						reads[r].mapQual = reads[idx].mapQual;
						reads[r].matePos = reads[idx].pos;
						reads[r].mateLen = reads[idx].size();
						if (reads[r].isReverse() == reads[idx].isReverse()) {
							reads[r].reverse();
							reads[r].complement();
						}
					} else {
						filter=true;
						tf = 5;
					}
				}
			} else {
				filter = true;
			}

		}
		if (filter == true) reads[r].mapQual = -1.0;

//		cout << "reads[" << r << "]: " << bam1_qname(reads[r].getBam()) << " matePos: " << reads[r].matePos << " mateLen: " << reads[r].mateLen << " Filter: " << tf << " filter: " << filter <<  " mq: " << reads[r].mapQual << endl;
	}


	int nUnmapped = 0;
	int nMateposError = 0;
	sort(reads.begin(), reads.end(), SortFunc::sortFunc);
	size_t max; for (max=0;max<params.maxReads && max<reads.size();max++) if (!(reads[max].mapQual<minMapQual)) {
		if (reads[max].matePos==-1 && reads[max].isPaired() && !reads[max].mateIsUnmapped() ) {
			nMateposError++;
			reads[max].matePos = reads[max].pos;
		};
		filteredReads.push_back(Read(reads[max]));
		if (reads[max].isUnmapped()) nUnmapped++;
	} else break;


	filteredReads.swap(reads);
	filteredReads.clear();
	//Read::filterReads(reads, params.maxReads, params.mapQualThreshold, params.maxReadLength, params.minReadOverlap, leftPos, rightPos);

	if (params.filterReadAux!="") {
		if (params.filterReadAux.size()>1) {
			size_t before=reads.size();
			int exclude=1;
			if (params.filterReadAux[0]=='+') exclude=0;
			string match=params.filterReadAux.substr(1,params.filterReadAux.size());
			Read::filterReads(reads, exclude, match);
			size_t after=reads.size();
			if (!params.quiet) cout << "filterAux: " << before-after << " reads were filtered based on match string " << params.filterReadAux << endl;
		}
	}

	if (!params.quiet) cout << "Number of reads: " << reads.size() << " out of " << oldNumReads << " # unmapped reads: " << nUnmapped << " numReadsUnknownLib: " << numUnknownLib << " numChrMismatch: " << numTIDmismatch << " numMappedWithoutMate: " << numOrphan << " numUnmappedWithoutMate: " << numOrphanUnmapped << endl;
	if (nMateposError) {
		cerr << "The mate position of " << nMateposError << " reads was recorded as -1 in the BAM file" << endl;
	}

	if (params.showReads) {
		for (size_t r=0;r<reads.size();r++) {
			cout << "read[" << r << "]: " << reads[r] << endl;
		}
	}

	if (reads.size()<2) {
		throw string("too_few_reads");
	} else if (reads.size()>=params.maxReads) {
		throw string("above_read_count_threshold");
	}

}


void DetInDel::detectIndels(const string & variantsFileName)
{

	ofstream output;
	ofstream glfOutput;

	string callsFile=params.fileName; callsFile.append(".calls.txt");
	string glfFile=params.fileName; glfFile.append(".glf.txt");

	OutputData oData=params.makeOutputData(output);

	/*
	output.open(callsFile.c_str());
	if (!output.is_open()) {
		throw(string("Cannot open file ").append(callsFile).append(" for writing."));
	}

	oData.outputLine(oData.headerString());
	*/


	glfOutput.open(glfFile.c_str());
	if (!glfOutput.is_open()) {
		throw(string("Cannot open file ").append(glfFile).append(" for writing."));
	}

	OutputData glfData=params.makeGLFOutputData(glfOutput);
	glfData.outputLine(glfData.headerString());

	VariantFile vf(variantsFileName);

	int index=0;
	//for (map<uint32_t,InDel>::const_iterator it=indels.begin();it!=indels.end();it++, cnt++) {

	vector<Read *> readBuffer;
	uint32_t oldLeftPos=0, oldRightFetchReadPos=0;


	string oldTid("-1");

	// NOTE ReadBuffer should be reset on first usage or on chromosome change!
	bool resetReadBuffer = true;



	while (!vf.eof()) {
		AlignedCandidates candidateVariants;
		candidateVariants=vf.getLineVector(params.varFileIsOneBased);
		if (candidateVariants.variants.size()==0) continue;

		vector<Read> reads;

		uint32_t pos, leftPos, rightPos;
		// get lowest and highest position

		//leftPos=(candidateVariants.leftPos>int(params.width))?(candidateVariants.leftPos-params.width):0;
		//rightPos=(candidateVariants.rightPos+params.width-1);
		leftPos = candidateVariants.leftPos;
		rightPos = candidateVariants.rightPos;


		pos = candidateVariants.centerPos;
		params.tid=candidateVariants.tid;

		if (params.tid!=oldTid) {
			// reinit
			resetReadBuffer = true;
			oldTid = params.tid;
			oldLeftPos = 0;
		}

		if (leftPos < oldLeftPos) {
			cerr << "leftPos: " << leftPos << " oldLeftPos: " << oldLeftPos << endl;
			cerr << "Candidate variant files must be sorted on left position of window!" << endl;
			exit(1);
		}


// TODO either add tid to AlignedVariant or infer it from the vector of aligned variants
// change alige
		index++;
		bool skipped = false;

		if (!params.quiet) cout << "****" << endl << " tid: " << params.tid << " pos: " << pos << " leftPos: " << leftPos << " " << " rightPos: " << rightPos << endl;

		string message="ok";
		/*
		if (!(indels[pos].count[0]>=params.minCount || indels[pos].count[1]>=params.minCount)) {
			message="below_indel_count_threshold";
			skipped=true;
			goto _end;
		}
		*/



		try {
			getReads(leftPos, rightPos, reads, oldLeftPos, oldRightFetchReadPos, readBuffer, resetReadBuffer);


			if (params.inferenceMethod=="empirical") empiricalDistributionMethod(index, reads, pos, leftPos, rightPos,  candidateVariants, oData, glfData);
			else throw string("Unknown inference method");

		}
		catch (string s) {
			for (size_t x=0;x<s.size();x++) if (s[x]==' ') s[x]='_';
			message=string("error_").append(s);
			skipped=true;
			goto _end;
		}
		catch (std::bad_alloc) {
			message = string("error_bad_alloc");
			skipped = true;
			goto _end;
		}
		catch (std::exception& e) {
			message = string("error_exception_").append(e.what());
			skipped = true;
			goto _end;
		}


		_end:

		if (skipped) {
			cerr << "skipped " << params.tid << " " << pos << " reason: " << message << endl;
			//OutputData::Line line(oData);
			//line.set("msg", message);
			//line.set("index", index);
			//oData.output(line);

			OutputData::Line gline(glfData);
			gline.set("msg", message);
			gline.set("index", index);
			gline.set("tid", params.tid);
			gline.set("lpos", leftPos);
			gline.set("rpos", rightPos);
			glfData.output(gline);

			// reset read buffer: all reads will be fetched again
			resetReadBuffer = true;
		} else {
			resetReadBuffer = false;
		}

		oldLeftPos = leftPos;
	}

	//output.close();
	glfOutput.close();

	// clean up read buffer
	for (size_t r=0;r<readBuffer.size();r++) {
		if (readBuffer[r]!=NULL) delete readBuffer[r];
	}


}




bool DetInDel::alignHaplotypes(vector<Haplotype> & haps,  uint32_t pos, uint32_t & leftPos, uint32_t & rightPos, map<int, std::set<AlignedVariant> > & variants)
{
	uint32_t start=leftPos;
	uint32_t end=rightPos+1;

	variants.clear();

	int print=0;

	seqan::Score<int> score(-1, -460, -100,-960);

	Read rh1;
	rh1.pos=0;
	rh1.posStat.first=0;
	rh1.mapQual=1.0-1e-32;
	ObservationModelParameters alignParams("probabilistic");
	alignParams.pError=0.0001;
	alignParams.pMut=0.01;
	alignParams.maxLengthDel=50;
	alignParams.forceReadOnHaplotype=true;
	alignParams.bMid=0;
	//alignParams.maxLengthIndel=12;
	//alignParams.numIndels=2;
	//alignParams.indelDist="uniform";

	vector<Haplotype> tmp_haps;
	for (size_t h=0;h<haps.size();h++) {
		rh1.seq.seq=haps[h].seq;
		rh1.setAllQual(1.0-1e-16);

		Haplotype hRef;
		uint32_t start=leftPos;
		uint32_t end=rightPos;

		string refSeq=getRefSeq(start+1, end+1);




		hRef.append(refSeq);
		/*
		char lc = (haps[h].seq[haps[h].seq.size()-1]);
		char lcl;
		if (lc == 'T') lcl = 'A'; else if (lc == 'A') lcl = 'T'; else if (lc=='G') lcl = 'C'; else if (lc=='C') lcl = 'G';

		hRef.seq+= lcl;
		*/
		/*
		ObservationModelFBMax om(hRef, rh1, 0, alignParams);
		*/
		ObservationModelSeqAn om(hRef, rh1, 0, alignParams, score);
		haps[h].indels.clear();
		haps[h].snps.clear();
		//om.reportVariants(haps[h].indels, haps[h].snps, haps[h].align);
		//om.calcLikelihood();
		om.align();
		const MLAlignment & ml=om.getMLAlignment();
		haps[h].indels=ml.indels;
		haps[h].snps=ml.snps;
		haps[h].align=ml.align;
		haps[h].ml=ml;
		bool hasStartEndIndel = false;
		if (ml.hpos[0] == MLAlignment::LO) hasStartEndIndel = true;
		int hs = ml.hpos.size()-1;
		if (hs>0 && ml.hpos[hs] == MLAlignment::RO) hasStartEndIndel = true;
		//if (params.showCandHap) {
	//			cout << "hap " << h << endl;om.printAlignment(20);
	//			cout << string(20,' ') << haps[h].align << endl;
	//	}

		for (map<int, AlignedVariant>::const_iterator it=haps[h].indels.begin(); it!=haps[h].indels.end();it++) variants[it->first].insert(it->second);
		for (map<int, AlignedVariant>::const_iterator it=haps[h].snps.begin(); it!=haps[h].snps.end();it++) variants[it->first].insert(it->second);
		if (!hasStartEndIndel) {
			tmp_haps.push_back(haps[h]);
		}

	}

	haps.swap(tmp_haps);

	// add REF allele as variant to each haplotype in order to compute coverage statistics
	for (map<int, std::set<AlignedVariant> >::const_iterator it=variants.begin();it!=variants.end();it++) {
		for (size_t h=0;h<haps.size();h++) haps[h].addRefVariant(it->first);
	}

	if (!params.quiet) {
		for (map<int, std::set<AlignedVariant> >::const_iterator it=variants.begin();it!=variants.end();it++) {
			cout << "aligned_var@pos " << pos << " " << leftPos+it->first;
			BOOST_FOREACH(AlignedVariant av, it->second) {
				cout << " " << av.getString();
			}
			cout << endl;
		}
	}


	return true;
}

bool DetInDel::getHaplotypes(vector<Haplotype> &haps, const vector<Read> & reads,uint32_t pos, uint32_t & leftPos, uint32_t & rightPos, const AlignedCandidates & candidateVariants)
{


	uint32_t rs=(int(leftPos)>params.minReadOverlap)?(leftPos-params.minReadOverlap):0;
	uint32_t re=rightPos+params.minReadOverlap;
	string refSeq=getRefSeq(rs+1, re+1);

	HaplotypeDistribution hd(pos, refSeq, rs);

	// infer empirical haplotype distribution from WS alignment
	for (size_t r=0;r<reads.size();r++) {
		hd.insertRead(reads[r].getBam());
	}
	hd.setFrequencies();

	haps.clear();
	vector<Variant> indelVariants;

	/*
	if (!(candidateVariants.size()>0 && params.checkAllCIGARs==0)) {
		indelVariants=hd.getIndelVariantsAtMidPos();
	}


	// add any prespecified variants
	//indelVariants.insert(indelVariants.begin(), variants.begin(), variants.end());
	*/
	if (!params.quiet) {
		cout << "candidate_var@pos: " << pos ;
		BOOST_FOREACH(AlignedVariant v, candidateVariants.variants) {
			cout << " " << v.getStartHap() << "," << v.getString();
		}
		cout << endl;
	}


	// get haplotypes from empirical distribution

	try {
		HDIterator2 hdi(hd, params.maxHap, pos, leftPos, rightPos, params.noIndelWindow);

		double logNumHaps=hdi.getLogNumHaps();
		if (logNumHaps>seqan::log(params.skipMaxHap)) {
			cerr << "tid: " << params.tid << " pos: " << pos << " too many haplotypes [>exp(" << logNumHaps << ")]" << endl;
			return true;
		}

		//hdi.generateHapsWithIndels(haps, indels);
		vector<Haplotype> tmp_haps;
		hdi.generateHapsWithAlignedVariants(haps, candidateVariants, 0, params.changeINStoN);


	

		if (haps.size()>params.skipMaxHap || haps.size()*reads.size()>params.maxHapReadProd) {
			cerr << "tid: " << params.tid << " pos: " << pos << " too many haplotypes [>" << haps.size() << "]" << " numreads: " << reads.size() << endl;
			return true;
		}

		if (params.showHapDist) {
			cout << endl << "Empirical distribution: " << endl;
			cout << hdi << endl;
		}

		leftPos=hdi.start();
		rightPos=hdi.end();


		

		map<int, std::set<AlignedVariant> > variants;
		alignHaplotypes(haps,pos, leftPos, rightPos, variants);

		// remove duplicate reference-haplotypes of different length
		bool foundRef = false;
		for (size_t th=0;th<haps.size();th++) {
			const Haplotype & hap=haps[th];
			int num_indels =  hap.countIndels();
			int num_snps = hap.countSNPs();
			if (num_indels == 0 && num_snps == 0) {
				

				if (!foundRef) {
					tmp_haps.push_back(Haplotype(haps[th]));
					foundRef = true;
				}
			} else {
				tmp_haps.push_back(Haplotype(haps[th]));
			}
		}
		/*
		if (params.showCandHap) {
			for (size_t i=0;i<haps.size();i++) {
				cout << "PRE FILTER hdi[" << i << "]:" << haps[i] << endl;
			}
		}
		*/

		typedef map<int, AlignedVariant>::const_iterator It;
		haps.swap(tmp_haps);

		int nh=0;
		if (params.showCandHap) {
			for (size_t i=0;i<haps.size();i++) {
				cout << "POSTFILTER hdi[" << nh++ << "]:" << haps[i] << endl;
			}
		}
	}
	catch (string s) {
		if (s=="Blocks are not consecutive.") {
			cerr << "tid: " << params.tid <<  "pos: " << pos << s << endl;
			//return true;
			throw string("hapblock");
		} else {
			throw string(s);
		}
	}
	return false;
}




/*
// HaplotypeDistribution method
void DetInDel::computeLikelihoods(const vector<Haplotype> &haps, const vector<Read> & reads, vector<vector<MLAlignment> > & liks, uint32_t leftPos, uint32_t rightPos, vector<int> & onHap)
{
//	cout << "Computing likelihoods for all reads and haplotypes.\n";
	map<size_t, vector<size_t> > hapSizeToIdx;
	onHap = vector<int>(reads.size(),0); // records whether a read was aligned onto at least one haplotype

	typedef map<size_t, vector<size_t> >::const_iterator hapsCIt;

	for (size_t x=0;x<haps.size();x++) {
		hapSizeToIdx[haps[x].size()].push_back(x);
	}

	liks=vector<vector<MLAlignment> >(haps.size(),vector<MLAlignment>(reads.size()));

	for (hapsCIt it=hapSizeToIdx.begin();it!=hapSizeToIdx.end();it++) {
		const vector<size_t> hapIdx=it->second;
		// setup observation models for all the reads
		vector<ObservationModelFBMaxErr> oms; oms.reserve(reads.size());
		for (size_t r=0;r<reads.size();r++) {
			const Haplotype & hap=haps[hapIdx[0]];
			oms.push_back(ObservationModelFBMaxErr(hap, reads[r], leftPos, params.obsParams));
		}

		for (size_t h=0;h<hapIdx.size();h++) {
			size_t hidx=hapIdx[h];
			const Haplotype & hap=haps[hidx];

//			cout << "Haplotype[" << hidx << "]: " << endl;


			for (size_t r=0;r<reads.size();r++) {
				oms[r].changeHaplotype(hap);
				liks[hidx][r]=oms[r].calcLikelihood();
				if (!liks[hidx][r].offHapHMQ) onHap[r]=1;
//				cout << "[" << r << ": " << liks[hidx][r].ll << "] ";
				if (liks[hidx][r].ll>0.1) {
				  ObservationModelFBMaxErr om(hap, reads[r], leftPos, params.obsParams);
				 liks[hidx][r]=om.calcLikelihood();
				 cout << string(25,' ') << hap.seq << endl;
				  om.printAlignment(25);
				  cout << "h: " << h << " r: " << r << endl;
				  cout << bam1_qname(reads[r].getBam()) << endl;
				  cerr << "Likelihood>0" << endl;
				  exit(1);
				}
				if (isnan(liks[hidx][r]) || isinf(liks[hidx][r])) {
					cout << "NAN/Inf error" << endl;
					throw string("Nan detected");
				}
			}
	//		cout << endl;
		}
	}
}
*/
void DetInDel::computeLikelihoods(const vector<Haplotype> &haps, const vector<Read> & reads, vector<vector<MLAlignment> > & liks, uint32_t leftPos, uint32_t rightPos, vector<int> & onHap)
{
//	cout << "Computing likelihoods for all reads and haplotypes.\n";
	onHap = vector<int>(reads.size(),0); // records whether a read was aligned onto at least one haplotype

	typedef map<size_t, vector<size_t> >::const_iterator hapsCIt;

	liks=vector<vector<MLAlignment> >(haps.size(),vector<MLAlignment>(reads.size()));
	for (size_t hidx=0;hidx<haps.size();hidx++) {
		const Haplotype & hap=haps[hidx];
		for (size_t r=0;r<reads.size();r++) {
			ObservationModelFBMaxErr oms(hap, reads[r], leftPos, params.obsParams);
			liks[hidx][r]=oms.calcLikelihood();
			if (!liks[hidx][r].offHapHMQ) onHap[r]=1;
//				cout << "[" << r << ": " << liks[hidx][r].ll << "] ";
			if (liks[hidx][r].ll>0.1) {
				ObservationModelFBMaxErr om(hap, reads[r], leftPos, params.obsParams);
				liks[hidx][r]=om.calcLikelihood();
				cout << string(25,' ') << hap.seq << endl;
				om.printAlignment(25);
				cout << "hidx: " << hidx << " r: " << r << endl;
				cout << bam1_qname(reads[r].getBam()) << endl;
				cerr << "Likelihood>0" << endl;
				exit(1);
			}
			if (isnan(liks[hidx][r]) || isinf(liks[hidx][r])) {
				cout << "NAN/Inf error" << endl;
				throw string("Nan detected");
			}
		}
//		cout << endl;
	}
}


void DetInDel::computeHapPosition(const Haplotype & hap, const Read & read, vector<int> & alPos, int leftPos)
{
	// get position on haplotype of read alignment to reference from the aligned position of first and last base in the read

	const bam1_t *b=read.getBam();
	const bam1_core_t *c=&b->core;
	uint32_t* cigar=bam1_cigar(b);
	int k, end, start;
	end = c->pos;

	int offs=0, l=0, lend; // offset due to SOFT_SKIP at beginning
							// lend is base for which end is computed (there might be a SOFT_CLIP at the end of the read)

	bool al=false;
	for (k = 0; k < (int) c->n_cigar; ++k) {
		int op = cigar[k] & BAM_CIGAR_MASK;

		if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CREF_SKIP) al=true;

		if (!al && op == BAM_CSOFT_CLIP) offs += cigar[k] >> BAM_CIGAR_SHIFT;

		if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP)
					l += cigar[k] >> BAM_CIGAR_SHIFT;

		if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP) {
			end += cigar[k] >> BAM_CIGAR_SHIFT;
			lend=l;
		}
	}

	start=c->pos-leftPos; // make relative to alignment of haplotypes to reference
	end-=leftPos;

	// lookup start and end in alignment of haplotype to reference

	for (int x=0;x<int(hap.ml.hpos.size());x++) if (hap.ml.hpos[x]==start) {
		alPos.push_back(hap.ml.hpos[x]-offs);
		break;
	}

	for (int x=int(hap.ml.hpos.size())-1;x>=0;x--) if (hap.ml.hpos[x]==end) {
		alPos.push_back(hap.ml.hpos[x]-lend);
		break;
	}


}

void DetInDel::computeLikelihoodsFaster(const vector<Haplotype> &haps, const vector<Read> & reads, vector<vector<MLAlignment> > & liks, uint32_t leftPos, uint32_t rightPos, vector<int> & onHap)
{
	liks.clear();
	liks=vector<vector<MLAlignment> >(haps.size(),vector<MLAlignment>(reads.size()));
	onHap = vector<int>(reads.size(),0); // records whether a read was aligned onto at least one haplotype

	const unsigned int kmer=4;

	for (size_t h=0;h<haps.size();h++) {
		const Haplotype & hap = haps[h];
		//cout << "Haplotype[" << h << "]: " << endl;
		HapHash hash(kmer, hap);
		for (size_t r=0;r<reads.size();r++) {
			// given BWA alignment of read to reference, estimate a number of likely alignments to the haplotype
			vector<int> alPos;
			computeHapPosition(hap, reads[r], alPos, leftPos);
		//	cout << "[" << r << ": " << alPos.size() ;


			ObservationModelS om(hap, reads[r], leftPos, params.obsParams);

			// align using guessed alignments and the haplotype hash
			liks[h][r]=om.align(hash);
		//	cout << "," << liks[h][r].ll << "] ";
			if (!liks[h][r].offHapHMQ) onHap[r]=1; // if on-haplotype with artificial high mapping quality

			/*
			seqan::Score<int> score(-1, -460, -100,-960);
			ObservationModelSeqAn om2(hap, reads[r], leftPos, params.obsParams, score);
			om2.align();
			*/

 		}
		//cout << "done" << endl;
	}

	// check HMQ off-haplotype reads

	// realign a couple of high-mapping quality reads to obtain new candidate indels
	// propose new set of candidate haplotypes by realigning all reads to the new set of candidate haplotypes


	// --bamFiles /Users/caa/Documents/workspace/DInDelFastProb/bamfiles.txt --output test --region 12036338-12036340 --maxReadLength 60 --tid 17 --maxHap 8 --showEmpirical  --minReadOverlap 20 --width 60 --maxLengthIndel 10 --ref /Users/caa/data/human_b36_female.Y.fa --pError 0.0005  --maxRead 2000 --computeMAP
}

double DetInDel::getPairPrior(const AlignedVariant & av1, const AlignedVariant & av2, int leftPos, const AlignedCandidates & candidateVariants)
{
	std::set<AlignedVariant> vars;  vars.insert(av1); vars.insert(av2);
	double ll = 0.0;
	BOOST_FOREACH(AlignedVariant avar, vars) {
		double lnf = 0.0;
		int type = avar.getType();
		const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());

		if (type==Variant::SNP) lnf = log(params.priorSNP); else if (type==Variant::DEL || type==Variant::INS) lnf = log(params.priorIndel);
		if (av==NULL) {
			ll += lnf;
		} else {
			double prior = av->getFreq();
			if (prior<0.0) ll += lnf; else ll+=log(prior);
		}
	}

	return ll;

}

double DetInDel::getHaplotypePrior(const Haplotype & h1, const Haplotype & h2, int leftPos, const AlignedCandidates & candidateVariants)
{
	// returns log prior probability of the pair of haplotypes, based on settings in params
	// one day maybe change prior for known SNPs
	double ll=0.0;

	// count indels
	typedef  map <int, AlignedVariant>::const_iterator AVIt;
	//hash_map <int, int> indels, snps;
	std::set <AlignedVariant> indels, snps;
	for (AVIt it=h1.indels.begin();it!=h1.indels.end();it++) if (it->second.getString().find("*REF")==string::npos && it->second.getString().find("=>")==string::npos ) {
			//indels[it->first]=1;
			indels.insert(it->second);
	}
	for (AVIt it=h2.indels.begin();it!=h2.indels.end();it++) if (it->second.getString().find("*REF")==string::npos && it->second.getString().find("=>")==string::npos ) {
		//indels[it->first]=1;
		indels.insert(it->second);
	}

	for (AVIt it=h1.snps.begin();it!=h1.snps.end();it++) if (it->second.getString().find("*REF")==string::npos && it->second.getString().find("=>D")==string::npos) {
		snps.insert(it->second);
		//snps[it->first]=1;
	}
	for (AVIt it=h2.snps.begin();it!=h2.snps.end();it++) if (it->second.getString().find("*REF")==string::npos && it->second.getString().find("=>D")==string::npos) {
		snps.insert(it->second);
		//snps[it->first]=1;
	}

	BOOST_FOREACH(AlignedVariant indel, indels) {
	//	cout << "indel: " << indel.getString() << " " << indel.getStartHap();
		const AlignedVariant *av = candidateVariants.findVariant(indel.getStartHap()+leftPos, indel.getType(), indel.getString());
		if (av==NULL) {
		//	cout << " not found. " << endl;
			ll += log(params.priorIndel);
		} else {
			double prior = av->getFreq();
		//	cout << " found: " << prior << " " << av->getStartHap() << " " << av->getFreq() << endl;
			if (prior<0.0) ll += log(params.priorIndel); else ll+=log(prior);
		}
	}
	BOOST_FOREACH(AlignedVariant indel, snps) {
	//	cout << "snp: " << indel.getString() << " " << indel.getStartHap();
		const AlignedVariant *av = candidateVariants.findVariant(indel.getStartHap()+leftPos, indel.getType(), indel.getString());
		if (av==NULL) {
		//	cout << " not found. " << endl;
			ll += log(params.priorIndel);
		} else {
			double prior = av->getFreq();
		//	cout << " found: " << prior << " " << av->getStartHap() << " " << av->getFreq() << endl;
			if (prior<0.0) ll += log(params.priorIndel); else ll+=log(prior);
		}
	}
	/*
	BOOST_FOREACH(AlignedVariant snp, snps) {
		const AlignedVariant *av = candidateVariants.findVariant(snp.getStartHap()+leftPos, snp.getString());
		if (av==NULL) ll += log(params.priorSNP); else {
			double prior = av->getFreq();
			if (prior<0.0) ll += log(params.priorSNP); else ll+=log(prior);
		}
	}
	*/
	/*
	int numIndels=int(indels.size());
	int numSNPs=int(snps.size());

	ll+=double(numIndels)*log(params.priorIndel);
	ll+=double(numSNPs)*log(params.priorSNP);
	*/
//	cout << "ll: " << ll << endl;
	return ll;
}

#define FILTERHAPS
#ifdef FILTERHAPS

void DetInDel::filterHaplotypes(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks,  vector<int> & filtered, map<pair<int, AlignedVariant>, VariantCoverage> & varCoverage, bool doFilter)
{

	const int debugfh = 0;
	int numFiltered = 0;
	int numHaps = int(haps.size());
	filtered = vector<int>(haps.size(),0);

	varCoverage.clear();


	typedef pair<int, AlignedVariant> PAV;
	map<PAV, vector< std::set<int> > > hVarCoverage; // coverage of each variant per haplotype, so that eventually only coverage of reads that were not filtered out are reported

	for (int h=0;h<int(haps.size());h++) {
		// check all reads aligned to this haplotype and select the ones that are not off-haplotype and aligned without indels and at most two high-quality mismatches
		std::set<int> selReads;
		for (size_t r=0;r<reads.size();r++) {
			int sel = 0;
			if (!liks[h][r].offHapHMQ && liks[h][r].numIndels == 0) { // && liks[h][r].numMismatch<=3) {
				selReads.insert(int(r));
				sel = 1;
			}
			if (debugfh) cout << "sel: " << "h: " << h << " " << bam1_qname(reads[r].getBam()) << " mpos: " << reads[r].matePos << " selected: " << sel << endl;

		}

		// check each variant in the haplotype

		bool allCovered = true; // all variants in haplotype should be covered by at least one read.
		for (map<int, AlignedVariant>::const_iterator it = haps[h].indels.begin();it!= haps[h].indels.end();it++) {
				const AlignedVariant & av = it->second;

				PAV pav(it->first, av);

				map<PAV,vector<std::set<int> > >::iterator pit  = hVarCoverage.find(pav);
				if (pit == hVarCoverage.end()) {
					hVarCoverage[pav] = vector< std::set<int> >(haps.size()*2);
				}

				if (av.getType() == Variant::INS || av.getType() == Variant::DEL) {
					int left = av.getLeftFlankRead() - params.obsParams.padCover;     // readFlankLeft is the first left unique base flanking the indel
					int right = av.getRightFlankRead() + params.obsParams.padCover;
					int leftV = av.getLeftFlankRead();
					int rightV = av.getRightFlankRead();


					int len = right-left+1;
					bool covered = false;
					int numdelcovered = 0;
					//cout << "left: " << left << " right: " << right << " len: " << len << endl;
					if (av.getType() == Variant::DEL) {
						// see if there is at least one read spanning the interval with at most one mismatch
						BOOST_FOREACH(int r, selReads) {
							std::set <int> c;
							int strand = 0;
							if (reads[r].isUnmapped()) {
								if (!reads[r].mateIsReverse()) strand = 1;
							} else {
								if (reads[r].isReverse()) strand = 1;
							}

							int nmm = 0;
							for (int b=0;b<=int(liks[h][r].hpos.size());b++) {
								int hb = liks[h][r].hpos[b];
								if (hb>=left && hb<=right) {
									c.insert(hb);
									if ( haps[h].seq[hb]!='N' && haps[h].seq[hb]!=reads[r].seq.seq[b]) nmm++;
								}

							}
							int cov = 0;
							if (int(c.size())>= len && nmm<=params.obsParams.maxMismatch) {
								cov = 1;
								numdelcovered++;
								hVarCoverage[pav][h+strand*numHaps].insert(r);
							}
							//cout << "RC" << bam1_qname(reads[r].getBam()) << " cov: " << cov << endl;
						}

						if (numdelcovered>=1) {
							covered = true;
						}
					} else if (av.getType() == Variant::INS) {
						// see if all bases in the haplotype from left to right are covered by at least one read that matches the haplotype without indels and at most 2 mismatches
						vector<int> hapBaseCovered(len, 0);
						vector<int> thisReadCovered(len, 0);
						int lenins = av.getSeq().size();

						BOOST_FOREACH(int r, selReads) {
							for (int x=0;x<len;x++) thisReadCovered[x]=0;
							int nmm = 0;
							std::set <int> c;
							
							// determine read strand
							int strand = 0;
							if (reads[r].isUnmapped()) {
								if (!reads[r].mateIsReverse()) strand = 1;
							} else {
								if (reads[r].isReverse()) strand = 1;
							}

							for (int b=0;b<=int(liks[h][r].hpos.size());b++) {
								int hb = liks[h][r].hpos[b];
								if (hb>=left && hb<=right) {
									// covered even if there is a mismatch
									thisReadCovered[hb-left]+=1;
									c.insert(hb);
									// count number of mismatches
									if (haps[h].seq[hb]!=reads[r].seq.seq[b]) nmm++;
								}
							}

						        bool thisread_covered = false;
							// for insertion <= 10 bp, whole insertion+padCover must be covered with at most one error by at least one read (ie not just covered by multiple reads together)
							if ( (lenins>10 && nmm<=params.obsParams.maxMismatch) || (lenins<=10 && int(c.size())>=len && nmm<=params.obsParams.maxMismatch)) {
								thisread_covered = true;
								for (size_t x=0;x<thisReadCovered.size();x++) {
									hapBaseCovered[x] += thisReadCovered[x];
									if (thisReadCovered[x]==0) {
										thisread_covered = false;
									}
									if (debugfh) cout << " " << hapBaseCovered[x];
								}
								if (thisread_covered) {
									hVarCoverage[pav][h+strand*numHaps].insert(r);
								}
							}
							

							if (0) cout << " hap " << h << " var: " << av.getString() << " len: " << len << " " << bam1_qname(reads[r].getBam()) << " nmm: " << nmm << " c.size(): " << c.size() << " mpos: " << reads[r].matePos << " covered: " << thisread_covered << endl;
							if (thisread_covered) covered=true;


						}
					}

					if (!covered) {
						allCovered = false;
						break;
					}
					if (debugfh) cout << "hap" << h << " var: " << av.getString() << " COVERED:" << covered << endl;

				}

		}
		if (doFilter) {
			if (!allCovered) {
				numFiltered++;
				filtered[h]=1;
			}
		}
		if (debugfh) cout << "Haplotype[" << h << "]: filtered " << filtered[h] << endl;

	}
	cout << "Filtered " << numFiltered << " haplotypes." << endl;
	// determine coverage of each variant
	for (map<PAV, vector <std::set<int> > >::const_iterator it = hVarCoverage.begin();it != hVarCoverage.end(); it++) {
		const PAV & pav = it->first;
		std::set<int> rf, rr; // forward and reverse strand reads
		for (int h=0;h<numHaps;h++) if (filtered[h]!=1) {
			rf.insert(hVarCoverage[pav][h].begin(), hVarCoverage[pav][h].end());
			rr.insert(hVarCoverage[pav][h+numHaps].begin(), hVarCoverage[pav][h+numHaps].end());
		}
		varCoverage[pav]=VariantCoverage(int(rf.size()), int(rr.size()));
	}


}
#endif

void DetInDel::estimateHaplotypeFrequenciesBayesEM(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks, vector<double> & hapFreqs, vector <HapEstResult > & posteriors,  uint32_t candPos, uint32_t leftPos,   uint32_t rightPos, OutputData & glfData, int index, const AlignedCandidates & candidateVariants, string program="all")

{

	// estimate haplotype frequencies using EM
	hapFreqs.clear();

	size_t nh=haps.size();
	size_t nr=reads.size();

	vector<double> rl(nh*nr,0.0); // read given haplotype likelihoods

	vector<double> z(nh*nr,0.0); // expectations of read-haplotype indicator variables
	vector<double> pi(nh); // haplotype frequencies
	vector<double> lpi(nh); // expectation of log frequencies
	vector<double> nk(nh,0.0), ak(nh,0.0); // counts for each haplotype

	hapFreqs=nk;

	int numUnmappedRealigned=0;
	int idx=0;
	int numReadOffAllHaps=0;
	for (size_t r=0;r<nr;r++) {
		int offallhap=1;
		for (size_t h=0;h<nh;h++) {
			// initialize read-haplotype likelihoods
			rl[idx]=liks[h][r].ll;
			if (!liks[h][r].offHap) offallhap=0;
			idx++;
		}
		if (offallhap) {
			numReadOffAllHaps++;

		}else {
			if (reads[r].isUnmapped()) numUnmappedRealigned++;
		}
	}


	// filter reads

	vector<int> filtered(nh, 0);
	map<pair<int, AlignedVariant>, VariantCoverage> varCoverage;

	//if (params.filterHaplotypes) {
	cout << "ALWAYS CALLING ::filterHaplotypes" << endl;
	filterHaplotypes(haps, reads,liks, filtered, varCoverage, params.filterHaplotypes);
	//}


	typedef pair<int, AlignedVariant> PAV;

	std::set< PAV > allVariants;
	map<int, std::set<PAV> > allVariantsByPos; //

	typedef map<int, AlignedVariant>::const_iterator It;
	typedef map<int, std::set<PAV> >::const_iterator PIt;

	for (size_t th=0;th<nh;th++) {
		const Haplotype & hap=haps[th];
		for (It it=hap.indels.begin();it!=hap.indels.end();it++) {
			if (!it->second.isRef() && !(it->second.isSNP() && it->second.getString()[3]=='D')) {
				allVariants.insert(PAV(it->first,it->second));
				allVariantsByPos[it->first].insert(PAV(it->first,it->second));
			}
		}
	}

	// set active variants, and divide into snps and indels
	vector< std::set< PAV > >  activeVariants, activeSNPs, activeIndels;

	int nav=0;
	int PRID=-1;
	if (program=="all") {
		std::set<PAV> snps, indels;
		BOOST_FOREACH(PAV pav, allVariants) {
			if (pav.second.isSNP()) {
				snps.insert(pav);
			} else if (pav.second.isIndel()) {
				indels.insert(pav);
			}
		}

		// both (double prior)
		activeVariants.push_back(allVariants);
		activeIndels.push_back(indels);
		activeSNPs.push_back(snps);
		nav++;
		PRID=1;
	} else if (program=="singlevariant") {
		std::set < std::set<PAV> > ssPAV;
		for (size_t h=0;h<haps.size();h++) if (filtered[h]==0) {
			const Haplotype & hap=haps[h];


			//cout << "hap[" << h << "].seq: " << hap.seq << endl;

			//cout << "vars:";
			std::set<PAV> act;
			for (It it=hap.indels.begin();it!=hap.indels.end();it++) {
				if (!it->second.isRef() && !(it->second.isSNP() && it->second.getString()[3]=='D')) {
					act.insert(PAV(it->first, it->second));
			//		cout << "[" << it->first << "," << it->second.getString() << "]";
				}
			}
			//cout << endl;
			ssPAV.insert(act);
		}

		nav=0;
		BOOST_FOREACH(std::set<PAV> s, ssPAV) {
			std::set<PAV> snps, indels;
			BOOST_FOREACH(PAV pav, s) {
				if (pav.second.isSNP()) {
					snps.insert(pav);
				} else if (pav.second.isIndel()) {
					indels.insert(pav);
				}
			}

			// both (double prior)
			activeVariants.push_back(s);
			activeIndels.push_back(indels);
			activeSNPs.push_back(snps);
			nav++;
		}

		PRID=2;
	} else if (program == "priorpersite") {
		nav = 0;

		// add reference haplotype
		activeVariants.push_back(std::set<PAV>());
		activeIndels.push_back(std::set<PAV>());
		activeSNPs.push_back(std::set<PAV>());



		for (map<int, std::set<PAV> >::const_iterator site_it = allVariantsByPos.begin();site_it!=allVariantsByPos.end();site_it++) {
			std::set<PAV> snps, indels;
			BOOST_FOREACH(PAV pav, site_it->second) {
				if (pav.second.isSNP()) snps.insert(pav);
				else if (pav.second.isIndel()) indels.insert(pav);
			}

			int maxStateSnp = (snps.size()==0)?1:2;
			int maxStateIndel = (indels.size()==0)?1:2;

			int prevNumActive = activeVariants.size();
			int sSnp = 1, sIndel = 1;
			//for (int sSnp = 0;sSnp<maxStateSnp;sSnp++) {
			//	for (int sIndel = 0; sIndel < maxStateIndel; sIndel++) {
			//
			//		if (sSnp == 1 || sIndel == 1) {
						// extend previous activeVariants

						for (int pna = 0;pna<prevNumActive;pna++) {
							std::set<PAV> av = activeVariants[pna];
							std::set<PAV> aIndels = activeIndels[pna];
							std::set<PAV> aSNPs = activeSNPs[pna];
							if (sSnp == 1) {
								av.insert(snps.begin(),snps.end());
								aSNPs.insert(snps.begin(),snps.end());
							}
							if (sIndel == 1) {
								av.insert(indels.begin(),indels.end());
								aIndels.insert(indels.begin(),indels.end());
							}

							activeVariants.push_back(av);
							activeIndels.push_back(aIndels);
							activeSNPs.push_back(aSNPs);
						}

			//		}
		//		}
		//	}

		}
		nav = activeVariants.size();

		PRID = 3;

	} else {
		cerr << "Unknown EM option" << endl;
		exit(1);
	}

	vector<int> compatible(nh,0);
	vector<double> logliks(nav,0.0);
	vector<double> logpriors(nav, 0.0);
	vector<double> post(nav,0.0);
	vector<double> freqs(nav*nh,0.0);

	// create matrix of which variant is active in which set
	idx=0;
	int nv=int(allVariants.size());
	vector<int> active(nav*nv,0), hapHasVar(nh*nv,0);

	//cout << "active: " << endl;
	BOOST_FOREACH(PAV pav, allVariants) {
	//	 cout << pav.first << " " << pav.second.getString() << " ";
		for (int a=0;a<nav;a++) {
			if (activeVariants[a].find(pav)!=activeVariants[a].end()) active[a*nv+idx]=1;
	//		cout << " " << active[a*nv+idx];
		}
	//	 cout << endl;
		for (size_t h=0;h<nh;h++) {
			It it=haps[h].indels.find(pav.first);
			if (it!=haps[h].indels.end() && it->second.getString()==pav.second.getString()) hapHasVar[h*nv+idx]=1;
//			cout << "[" <<  active[h*nv+idx] << " " << hapHasVar[h*nv+idx]<< " ]";

		}
//		cout << endl;
		idx++;
	}

	/*
	cout << "allVariants: ";
	BOOST_FOREACH(PAV pav, allVariants) {
		cout << " [" << pav.first << " " << pav.second.getString() << "]";
	}
	cout << endl;
	*/





	double logz=-HUGE_VAL;

	double a0=params.bayesa0;

	for (int th=0;th<nav;th++) {

		// set active variants

		double logprior=0.0;

		map <int, int> sites;
		BOOST_FOREACH(PAV pav, activeSNPs[th]) {
			sites[pav.first]=1;

			const AlignedVariant & avar = pav.second;
			const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
			int type = pav.second.getType();

			double lnf = 0.0;

			if (type==Variant::SNP) lnf = log(params.priorSNP); else if (type==Variant::DEL || type==Variant::INS) lnf = log(params.priorIndel);
			if (av==NULL) {
				logprior += lnf;
			} else {
				double prior = av->getFreq();
				if (prior<0.0) logprior += lnf; else logprior+=log(prior);
			}

		}
		BOOST_FOREACH(PAV pav, activeIndels[th]) {

			const AlignedVariant & avar = pav.second;
			const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
			int type = pav.second.getType();

			double lnf = 0.0;

			if (type==Variant::SNP) lnf = log(params.priorSNP); else if (type==Variant::DEL || type==Variant::INS) lnf = log(params.priorIndel);
			if (av==NULL) {
				logprior += lnf;
			} else {
				double prior = av->getFreq();
				if (prior<0.0) logprior += lnf; else logprior+=log(prior);
			}

			sites[pav.first]=2;
		}


		/*
		for (map<int,int>::const_iterator it=sites.begin();it!=sites.end();it++) {
			if (it->second==2) logprior+=log(params.priorIndel); else if (it->second==1) logprior+=log(params.priorSNP);
		}
		*/

		logpriors[th]=logprior;

//		cout << "Number of indels: " << ni << " number of SNPs: " << ns << endl;

		// check haplotypes

		int numah=0; // number of haplotypes for which frequencies will be estimated
		for (size_t h=0;h<nh;h++) {
			compatible[h]=1;
			if (filtered[h]!=0) {
				compatible[h]=0;
			} else {
				for (It it=haps[h].indels.begin();it!=haps[h].indels.end();it++) {
					if (!it->second.isRef() && !(it->second.isSNP() && it->second.getString()[3]=='D') && activeVariants[th].find(PAV(it->first,it->second))==activeVariants[th].end()) {
						// haplotype h has a non-ref variant that is not one of the active variants
						compatible[h]=0;
						break;
					}
				}
			}
			if (compatible[h]) numah++;
		}


		// run EM for this set of active variants
		bool converged=false;
		double tol=params.EMtol;

		double eOld=-HUGE_VAL, eNew;

		// initialize frequencies
		for (size_t h=0;h<nh;h++) if (compatible[h]) lpi[h]=log(1.0/double(numah)); else lpi[h]=-100;

		/*
		for (size_t r=0;r<nr;r++) {
				cout << "rl[" << r << "]:";
				for (size_t h=0;h<nh;h++) {
					cout << " " << rl[r*nh+h];
				}
				cout << endl;
			}
		*/
		double loglik, llNew, llOld=-HUGE_VAL;
		int iter=0;
		while (!converged) {
			//cout << endl << "EM[" << iter << "]:" << endl;
			// compute expectation of indicator variables
			for (size_t h=0;h<nh;h++) nk[h]=0.0;

			loglik=0.0;

			int idx=0;
			for (size_t r=0;r<nr;r++) {
				double lognorm=-HUGE_VAL;
				// compute responsibilities
				for (size_t h=0;h<nh;h++) {
					z[idx]=lpi[h]+(rl[idx]);
					lognorm=addLogs(lognorm, z[idx]);
					idx++;
				}
				idx-=nh;
				// normalize and compute counts
				for (size_t h=0;h<nh;h++) {
					z[idx]-=lognorm;
					z[idx]=exp(z[idx]);
					nk[h]+=z[idx];
					idx++;
				}
				loglik+=lognorm;

			}
			// compute frequencies
			//cout << "pi: ";

			double ahat=0.0;
			for (size_t h=0;h<nh;h++) if (compatible[h]) {
					ak[h]=nk[h]+a0; // a0 is Dirichlet prior parameter
					ahat+=ak[h];
			}
			double dahat=boost::math::digamma(ahat);

			for (size_t h=0;h<nh;h++) {

				// do variational bayes
				if (compatible[h]) {
					lpi[h]=boost::math::digamma(ak[h])-dahat;
					pi[h]=log((a0+nk[h])/(double(numah)*a0+double(nr)));
				} else {
					lpi[h]=-100;
					pi[h]=-100;
				}
			//	cout << " " << pi[h];
			//	zp+=exp(pi[h]);
			}
			//cout << " zp: " << zp << endl;


			idx=0;
			eNew=0.0;
			for (size_t r=0;r<nr;r++) {
				for (size_t h=0;h<nh;h++) {
				// compute responsibilities
					eNew+=z[idx]*( pi[h]+rl[idx]);
					idx++;
				}
			}
			//cout << "eOld: " << eOld << " " << " eNew: " << eNew; for (size_t h=0;h<nh;h++) { cout << " " << pi[h]; } cout << endl;
			/*
			for (size_t r=0;r<nr;r++) {
				cout << "z[" << r << "]:";
				for (size_t h=0;h<nh;h++) {
					cout << " " << z[r*nh+h];
				}
				cout << endl;
			}
			*/
			llNew=loglik;
			//cout << "loglik: " << loglik << endl;
			if (0 && llOld>llNew+1e-5)  {
				cerr << "ERROR: nr: " << nr << " eOld: " << eOld << " eNew: " << eNew << " diff: " << eOld-eNew << endl;
				cout << "ERROR: nr: " << nr << " eOld: " << eOld << " eNew: " << eNew << " diff: " << eOld-eNew << endl;
				cerr << "ERROR: nr: " << nr << " llOld: " << llOld << " eNew: " << llNew << " diff: " << llOld-llNew << endl;
				cout << "ERROR: nr: " << nr << " llOld: " << llOld << " eNew: " << llNew << " diff: " << llOld-llNew << endl;

				//throw string("EM Error in estimateHapFreqs");
				//iter=100;

			}
			converged=(fabs(eOld-eNew))<tol || iter>25;
			//cout << "iter: " << iter << " eOld: " << eOld << " eNew: " << eNew << endl;

			eOld=eNew;
			llOld=llNew;
			iter++;


		}


		// check sum

		double z=0.0;
		for (size_t y=0;y<nh;y++) {
			z+=exp(pi[y]);
		}

		if (0) {
		cout << "th: " << th << endl;
		for (size_t y=0;y<nh;y++) {
			cout << "[" << y << "," << exp(pi[y]) << "]";
		}
		cout << endl << endl;
		}

		logliks[th]=loglik;
		logz=addLogs(logz, logliks[th]+logprior);
		for (size_t h=0;h<nh;h++) { freqs[th*nh+h]=exp(pi[h])/z; }
		//for (size_t h=0;h<nh;h++) { cout << " " << freqs[th*nh+h]; } cout << endl;

		//cout << "loglik: " << loglik << " " << logliks[th] << " logprior: " << logprior << endl << endl;

	}

	for (int a=0;a<nav;a++) {
		post[a]=exp(logliks[a]+logpriors[a]-logz);
		//cout << "post[" << a << "]: " << post[a] << endl;
	}

	for (size_t h=0;h<nh;h++) {
		hapFreqs[h]=0.0;
	}

	for (int th=0;th<nav;th++) for (size_t h=0;h<nh;h++) {

		hapFreqs[h]+=exp(logliks[th]+logpriors[th]-logz)*freqs[th*nh+h];
	}

	/*
	cout << "hapFreqs:\n ";
	for (size_t th=0;th<nh;th++) {
		cout << "hapFreq[" << th << "]: " << hapFreqs[th] << endl;
		cout << "H" << th << ": " << logliks[th] << " " << logpriors[th] << " ";
		for (size_t h=0;h<nh;h++) {
			cout << " " << freqs[th*nh+h];
		}
		cout << endl;
	}
	*/

	cout << endl;

	// compute marginal posteriors for the individual variants
	vector< std::set<int> > readidx(myBams.size());
	for (int r=0;r<int(nr);r++) readidx[reads[r].poolID].insert(r);

	vector<double> prior(nh*nh,0.0);

	idx=-1;
	BOOST_FOREACH(PAV pav, allVariants) {
		idx++;

		double logp=-HUGE_VAL;
		double freq=0.0;
		for (int th=0;th<nav;th++) {
			if (active[th*nv+idx]) {
				logp=addLogs(logp, logliks[th]+logpriors[th]);
			}
		}

		for (size_t h=0;h<nh;h++) {
			if (hapHasVar[h*nv+idx]) {
				freq+=hapFreqs[h];
			}
		}

		logp-=logz;

		// compute marginalized haplotype frequencies
		vector<double> prior(nh*nh,0.0);

		bool doGLF=false; //(candPos==leftPos+pav.first)?true:false;

		const AlignedVariant & avar = pav.second;
		const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
		doGLF = (av==NULL)?false:true;

		//change this
if (params.outputGLF && doGLF) {
		map<int,int> otn; // old to new haplotype index
		// vector for mapping old haplotype index to new haplotype index
		vector<int> marsum(nv, 0);
		int s=1;
		for (int y=0;y<nv;y++) {
			if (y!=idx) {
				marsum[y]=s;
				s*=2;
			} else marsum[y]=0;
		//	cout << "marsum[" << y << "]: " << marsum[y] << endl;
		}


		// make a map of new marginalized states to the corresponding new index in the haplotype arrays.
		map<int, int> mar_states;
		for (int h=0;h<int(nh);h++) {
			int nidx=0;
			for (int v=0;v<nv;v++) nidx+=marsum[v]*hapHasVar[h*nv+v];
			map<int,int>::iterator it=mar_states.find(nidx);
			if (it!=mar_states.end()) otn[h]=it->second; else {
				int ns=int(mar_states.size());
				mar_states[nidx]=ns;
				otn[h]=ns;
			}
		//	cout << "oth["<<h<<"]: " << otn[h] << endl;
		}

		int nmarhap=mar_states.size();

		vector<double> marFreqs(nmarhap,0.0); //marginal frequencies
		for (size_t h=0;h<nh;h++) {
			int newh=otn[h];
			marFreqs[newh]+=hapFreqs[h];
		}
		// convert back to conditional frequencies/probabilities
		for (int h=0;h<nmarhap;h++) {
			if (marFreqs[h]<1e-16) marFreqs[h]=-50; else marFreqs[h]=log(marFreqs[h]);
		}
		//	cout << "marFreq[]: "; for (size_t x=0;x<nmarhap;x++) cout << " " << marFreqs[x]; cout << endl;

		// compute for every pair of haplotypes the likelihood of drawing it given priors on the variants and the estimated frequencies
		for (size_t h1=0;h1<nh;h1++) {
			for (size_t h2=h1;h2<nh;h2++) {
				prior[h1*nh+h2]=marFreqs[otn[h1]]+marFreqs[otn[h2]];
		//		cout << "prior: " << h1 << " " << h2 << ": " << prior[h1*nh+h2] << endl;
			}
		}
}
		// check which how many reads to a haplotype with this variant

		std::set<int> support;
		int totnf=0, totnr=0;
		double log5=log(0.5);
		if (1) for (size_t b=0;b<myBams.size();b++) {
			double msq=0.0;

			int nf=0;
			int nr=0;
			vector<double> lik(3,0.0);

			if (readidx[b].size()) {
				// compute RMS of mapping qualities

				int pos=pav.first;
				msq=0.0;
				int n=0;
				if (params.outputGLF && doGLF) {
				for (int i=0;i<3;i++) lik[i]=-HUGE_VAL;
				for (size_t h1=0;h1<nh;h1++) for (size_t h2=h1;h2<nh;h2++) {
					int genotype=hapHasVar[h1*nv+idx]+hapHasVar[h2*nv+idx];
					double pr=prior[h1*nh+h2];
				//	cout << "prior: " << h1 << " " << h2 << ": " << prior[h1*nh+h2];
					double ll=pr;
					BOOST_FOREACH(int r, readidx[b]) {
						ll+=log5+addLogs(rl[r*nh+h1],rl[r*nh+h2]);
					}

					lik[genotype]=addLogs(lik[genotype],ll);
				//	cout << " ll: " << ll << " " << lik[genotype] << endl;
				}
				}

				BOOST_FOREACH(int r, readidx[b]) {
					//cout << " " << 10*log10(1-reads[r].mapQual);

					size_t mlidx; double ml=-HUGE_VAL;
					std::set<size_t> mlis;
					for (size_t hi=0;hi<nh;hi++) {
						if (liks[hi][r].ll>=ml) {
							mlidx=hi;
							ml=liks[hi][r].ll;
						}
					}
					for (size_t hi=0;hi<nh;hi++) {
						if (liks[hi][r].ll>=ml-1e-7) {
							mlis.insert(hi);
						}
					}
					bool nrt=false, nft=false;

					map<int,bool>::const_iterator it;
					BOOST_FOREACH(size_t h, mlis) {
						bool covered=false;
						if (pav.second.isIndel()) {
							it=liks[h][r].hapIndelCovered.find(pos);
							if (it!=liks[h][r].hapIndelCovered.end() && it->second) covered=true;
						} else if (pav.second.isSNP()) {
							it=liks[h][r].hapSNPCovered.find(pos);
							if (it!=liks[h][r].hapSNPCovered.end() && it->second) covered=true;
						}
						if (covered) { // hapHasVar[] is to check whether haplotype truely has variant or only the reference
							if (hapHasVar[h*nv+idx]) {
								//if (pav.first+leftPos==43017596) {
								//	cout << "h: " << h << " r: " << r << " read: " << reads[r] << endl;
								//}


								if  (reads[r].onReverseStrand) nrt=true; else nft=true;
							}

						}
					}
					double mq=-10*log10(1.0-reads[r].mapQual);
					msq+=mq*mq;
					n++;
					if (nft) nf++;
					if (nrt) nr++;
				} // foreach read r
			//	cout << endl;
				if (n!=0) msq=sqrt(msq/double(n)); else msq=0.0;
				if (nf+nr>0) support.insert(b);
				totnf+=nf;
				totnr+=nr;
			}  // readidx[b].size()

			if (params.outputGLF && doGLF) {
				OutputData::Line line(glfData);
				line.set("msg", "ok");
				line.set("index", index);
				line.set("tid", params.tid);
				line.set("analysis_type",program);
				line.set("indidx",b);
				line.set("was_candidate_in_window",1);
				line.set("lpos",leftPos);
				line.set("rpos",rightPos);
				line.set("center_position",candPos);
				line.set("realigned_position",pav.first+leftPos);
				line.set("post_prob_variant", exp(logp));
				line.set("est_freq", freq);
				line.set("logZ", logz);
				line.set("nref_all", pav.second.getString());
				line.set("num_reads", readidx[b].size());
				line.set("msq",msq);
				line.set("num_cover_forward", nf);
				line.set("num_cover_reverse", nr);
				line.set("num_unmapped_realigned", numUnmappedRealigned);
				line.set("var_coverage_forward", varCoverage[pav].nf);
				line.set("var_coverage_reverse", varCoverage[pav].nr);

				if (b==0) {
					// output haplotypes and frequencies

					ostringstream os;
					bool ifh = true;
					for (size_t h=0;h<haps.size();h++) {
						if (hapFreqs[h]>1.0/double(2*reads.size())) {
							bool isfirst = true;
							if (!ifh) os << ";";
							ifh = false;
							int nvars = 0;
							for (map<int, AlignedVariant>::const_iterator it=haps[h].indels.begin();it!=haps[h].indels.end();it++) if (it->second.getString()!="*REF") {
								nvars++;
								if (!isfirst) os << ",";
								isfirst = false;
								os << leftPos+it->first << "," << it->second.getString();
							}
							if (nvars==0) os << "REF";
							os << ":" << hapFreqs[h];
						}
					}
					line.set("hapfreqs", os.str());

				}

				string likstring;

				for (int n=0;n<3;n++) {
					ostringstream o;
					o << lik[n];
					if (n==0) likstring.append("0/0:"); else if (n==1) likstring.append("0/1:"); else likstring.append("1/1:");
					likstring.append(o.str());
					if (n<2) likstring.append(";");
				}
				line.set("glf",likstring);
				glfData.output(line);
			}
			// glfOutput <<  PRID << " "  << b << " " << params.tid << " " << candPos << " " << pav.first+leftPos << " " << pav.second.getString() << " " << reads.size() <<  " " << msq << " "  << nf << " " << nr << " " << lik[0] << " " << lik[1] << " " << lik[2] << endl;


		} // foreach b
		posteriors.push_back(HapEstResult(pav.second, pav.first,exp(logp),freq, totnf, totnr));
	}


	cout << "candPos: " << candPos << " numReadOffAllHaps: " << numReadOffAllHaps << " logz: " << logz << endl;



    if (params.outputPooledLikelihoods) {

            // output which variants are active in which haplotype


            stringstream os; os << params.fileName << "." << params.tid << "." << candPos;
	    string oprefix = os.str();

	    string fname = oprefix;
	    fname.append(".hapvars");
            ofstream of(fname.c_str());
            if (!of.is_open()) {
                    throw string("Cannot open file ").append(fname).append(" for writing .hapvars file");
            }

            idx=0;
            BOOST_FOREACH(PAV pav, allVariants) {
                    stringstream o;
                    o << params.tid << " " << leftPos+pav.first << " " << pav.second.getString();
                    of << o.str() << string(50-o.str().length(),' ');
                    for (size_t h=0;h<nh;h++) {
                            of << " " << hapHasVar[h*nv+idx];
                    }

                    of << endl;
                    idx++;

            }

            of.close();


	    string prefix;
            stringstream os5; os5 << "EM " << params.tid << " " << candPos << " " << reads.size(); prefix.append(os5.str());

	    fname = oprefix;
	    fname.append(".hapfreqs");
	    of.open(fname.c_str());
	    outputHapsAndFreqs(&of,prefix,haps,hapFreqs, leftPos);
	    of.close();

            fname.clear();
            fname=oprefix;
	    oprefix.append(".liks");

	    cout << "fname: " << fname << endl;
            of.open(fname.c_str());
            if (!of.is_open()) {
                    throw string("Cannot open file ").append(fname).append(" for writing .liks file");
            }



            // output all likelihoods

            for (size_t r=0;r<nr;r++) {

                    of << r << " " << bam1_qname(reads[r].getBam()) << " " << log(1.0-reads[r].mapQual) << " " << reads[r].poolID;


                    for (size_t h=0;h<nh;h++) {
                            of << " " << liks[h][r].ll;
                    }


                    for (size_t h=0;h<nh;h++) {
                            of << " " << liks[h][r].offHap;
                    }
                    of << endl;
            }
	    of.close();

	    // output alignments
	    fname = oprefix;
	    fname.append(".alignments");
            cout << "fname: " << fname << endl;
            of.open(fname.c_str());
            if (!of.is_open()) {
                    throw string("Cannot open file ").append(fname).append(" for writing .liks file");
            }



	    for (size_t r=0;r<nr;r++) {
		cout << "###" << endl;
		cout << "read: " << bam1_qname(reads[r].getBam()) << " mpos: " << reads[r].matePos << endl;
		cout << "isUnmapped: " << reads[r].isUnmapped() << endl;
		// compute maximum alignment likelihood
		double lq = 0.0;
		for (size_t b=0;b<reads[r].size();b++) lq += log(reads[r].qual[b]);

		cout << "Max alignment loglik: " << lq << endl;

		double maxll = -HUGE_VAL;
		std::set <int> mlhaps;
		for (int h=nh-1;h>=0;h--) if (liks[h][r]>maxll) { maxll = liks[h][r]; }
		for (int h=nh-1;h>=0;h--) mlhaps.insert(h); //if (fabs(liks[h][r]-maxll)<0.01) mlhaps.insert(h);
		BOOST_FOREACH(int hidx, mlhaps) {
			cout << "r: " << r << " hidx: " << hidx << " maxll:" << maxll << endl;
			ObservationModelFBMaxErr obs(haps[hidx], reads[r], leftPos, params.obsParams);
			cout << string(50,' ') << haps[hidx].seq << endl;
			obs.printAlignment(50);
		}
	    }

            of.close();
    }
}

#ifndef DIPLOIDGLF
void DetInDel::diploidGLF(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks, vector<double> & hapFreqs, vector <HapEstResult > & posteriors,  uint32_t candPos, uint32_t leftPos, uint32_t rightPos,  OutputData & glfData, int index, const AlignedCandidates & candidateVariants, string program="all")

{
	size_t nh=haps.size();
	size_t nr=reads.size();

	vector<int> filtered(nh, 0);
	map<pair<int, AlignedVariant>, VariantCoverage> varCoverage;
	filterHaplotypes(haps, reads, liks, filtered, varCoverage, params.filterHaplotypes);

	vector<double> rl(nh*nr,0.0); // read given haplotype likelihoods

	// get read-hap likelihoods and get number of reads that are off all-haplotypes.

	int idx=0;
	int numReadOffAllHaps=0;
	for (size_t r=0;r<nr;r++) {
		int no=1;
		for (size_t h=0;h<nh;h++) {
			// initialize read-haplotype likelihoods
			rl[idx]=liks[h][r].ll;
			if (!liks[h][r].offHap) no=0;
			idx++;
		}
		if (no) {
			numReadOffAllHaps++;
		}
	}

	// get all variants

	typedef pair<int, AlignedVariant> PAV;
	const int VARSNP=1;
	const int VARINDEL=2;


	std::set< PAV > allVariants;
	map<int, std::set<PAV> > allVariantsByPos; //


	typedef map<int, AlignedVariant>::const_iterator It;
	typedef map<int, std::set<PAV> >::const_iterator PIt;

	vector<int> hap_num_indels(nh, 0);
	vector<int> hap_num_candidate_indels(nh, 0);
	vector<int> hap_num_snps(nh, 0);

	int ref_hap_idx = -1;
	for (size_t th=0;th<nh;th++) {
		const Haplotype & hap=haps[th];
		//cout << "hap[" << th << "]: ";
		hap_num_indels[th] = hap.countIndels();
		hap_num_snps[th] = hap.countSNPs();

		if (hap_num_indels[th] == 0 && hap_num_snps[th] == 0) {
			//if (ref_hap_idx != -1) cout << string("Already have ref-hap!") << " " << ref_hap_idx << endl;
			//if (ref_hap_idx!=-1) cout << "RH: " << haps[ref_hap_idx].seq << endl;
			//cout << "TH: " << haps[th].seq << endl;
			ref_hap_idx = th;
			//int h1 = th;
			//cout << "RRRR IN: " << hap_num_indels[h1] << " SNP: " << hap_num_snps[h1]; cout << " H1:"; for (It it=haps[h1].indels.begin();it!=haps[h1].indels.end();it++) cout << it->first << "," << it->second.getString() << ";" << endl;

		}
		if (hap_num_indels[th] != 0) {
			//check how many were candidates in the input file
			int nc = 0;
			for (It it=hap.indels.begin();it!=hap.indels.end();it++) {
				const AlignedVariant & avar = it->second;
				const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
				if (av!=NULL) nc+=1;
			}
			hap_num_candidate_indels[th] = nc;
		}
	


		for (It it=hap.indels.begin();it!=hap.indels.end();it++) {
			if (!it->second.isRef() && !(it->second.isSNP() && it->second.getString()[3]=='D')) {
				//cout << " " << it->first << "," << it->second.getString();
				allVariants.insert(PAV(it->first,it->second));
				allVariantsByPos[it->first].insert(PAV(it->first,it->second));
			}
		}
		//cout << endl;
	}

	idx=0;
	map<int,int> posToPosIdx;
	vector<int> varPositions;
	for (PIt pit=allVariantsByPos.begin();pit!=allVariantsByPos.end(); pit++) {
		posToPosIdx[pit->first]=idx;
		varPositions.push_back(pit->first);
		idx++;
	}

	int numVarPos = allVariantsByPos.size();
	int nv=int(allVariants.size());

	vector<int> hapVar(nh*numVarPos,0);
	vector<int> varType(nv+1);
	vector<PAV> variants(nv+1);
	idx=1;


	BOOST_FOREACH(PAV pav, allVariants) {
		int type=VARSNP;
		if (pav.second.isIndel()) type=VARINDEL;
		varType[idx]=type;
		int posIdx=posToPosIdx[pav.first];
		for (size_t h=0;h<nh;h++) {
			It it=haps[h].indels.find(pav.first);
			if (it!=haps[h].indels.end() && it->second.getString()==pav.second.getString()) hapVar[h*numVarPos+posIdx]=idx;
		}
		variants[idx]=pav;



		idx++;
	}



	/*
	cout << "allVariants: ";
	BOOST_FOREACH(PAV pav, allVariants) {
		cout << " [" << pav.first << " " << pav.second.getString() << "]";
	}
	cout << endl;
	*/

	// check which how many reads to a haplotype with this variant

	std::set<int> readidx;
	for (size_t r=0;r<nr;r++) readidx.insert(r);



	// compute marginal posteriors for the individual variants
	vector<double> prior(nh*nh,0.0), pairs_posterior(nh*nh, 0);
	// compute for every pair of haplotypes the likelihood of drawing it given priors on the variants and the estimated frequencies
	for (size_t h1=0;h1<nh;h1++) {
		for (size_t h2=h1;h2<nh;h2++) {
			prior[h1*nh+h2]=getHaplotypePrior(haps[h1],haps[h2], leftPos, candidateVariants);
		}
	}


	vector<int> max_indel_pair(2,-1);
	vector<int> max_noindel_pair(2,-1);

	double max_ll_indel = -HUGE_VAL;
	double max_ll_noindel = -HUGE_VAL;
	for (size_t h1=0;h1<nh;h1++) if (filtered[h1]==0) for (size_t h2=h1;h2<nh;h2++) if (filtered[h2]==0) {
		double ll=0.0;
		for (size_t r=0;r<reads.size();r++){
			ll+=log(0.5)+addLogs(rl[r*nh+h1],rl[r*nh+h2]);
		}
		// now we have the log likelihood, store posterior
		pairs_posterior[h1*nh+h2] = ll + prior[h1*nh+h2];

		// prinhaplotype pair
		/*		
		cout << "POST: " << h1 << " " << h2 << " " <<  pairs_posterior[h1*nh+h2] << " PR: " << prior[h1*nh+h2];
		
		cout << " IN: " << hap_num_indels[h1] << " SNP: " << hap_num_snps[h1]; cout << " H1:"; for (It it=haps[h1].indels.begin();it!=haps[h1].indels.end();it++) cout << it->first << "," << it->second.getString() << ";";
		cout << " IN: " << hap_num_indels[h2] << " SNP: " << hap_num_snps[h2]; cout << " H2:"; for (It it=haps[h2].indels.begin();it!=haps[h2].indels.end();it++) cout << it->first << "," << it->second.getString() << ";";
		cout << endl;
		*/
		
		if (pairs_posterior[h1*nh+h2]>max_ll_indel && (hap_num_candidate_indels[h1]>0 || hap_num_candidate_indels[h2]>0)) {
			max_ll_indel = pairs_posterior[h1*nh+h2];
			max_indel_pair[0] = h1;
			max_indel_pair[1] = h2;
		}
		if (pairs_posterior[h1*nh+h2]>max_ll_noindel && (hap_num_candidate_indels[h1]==0 && hap_num_candidate_indels[h2]==0)) {
			max_ll_noindel = pairs_posterior[h1*nh+h2];
			max_noindel_pair[0] = h1;
			max_noindel_pair[1] = h2;
		}

	}

	// output map-based variant calls
	
	double qual = 0.0;
	double ll_ref = max_ll_noindel;
	qual = - 10.0*( ll_ref - addLogs(max_ll_indel, ll_ref) )/log(10.0);
	cout << "ll_ref: " << ll_ref << " max_ll_indel: " << max_ll_indel << " qual: " << qual << endl;
	if (max_indel_pair[0]==-1 || max_indel_pair[1]==-1) throw string("Could not find indel allele");	
	if (1) {
		int numUnmappedRealigned = 0;
		int hx1 = max_indel_pair[0];
		int hx2 = max_indel_pair[1];
		for (size_t r=0;r<reads.size();r++) {
			if (reads[r].isUnmapped()) {
				if (liks[hx1][r].offHap == false || liks[hx2][r].offHap == false) {
					numUnmappedRealigned++;
				}
			}
		}

		map<int, std::set<AlignedVariant> > indels;
		for (int i=0;i<2;i++) {
			
			const Haplotype & hap = haps[ max_indel_pair[i] ];

			for (It it=hap.indels.begin();it!=hap.indels.end();it++) if (!it->second.isRef() || (it->second.isSNP() && it->second.getString()[3]=='D')){
				indels[it->first].insert(it->second);
			}
		}

		for (map<int, std::set<AlignedVariant> >::const_iterator it = indels.begin();it!=indels.end();it++) {
			
			double msq = 0;
			int numf=0, numr=0, n=0;
			int m =2;
			if (max_indel_pair[0]==max_indel_pair[1]) m = 1;
			for (int i=0;i<m;i++) {
				int h=max_indel_pair[i];
				It iter = haps[h].indels.find(it->first);
				if (iter != haps[h].indels.end() && iter->second.isIndel()) {
				
					for (int r=0;r<nr;r++) {
						bool covered=false, nft=false, nrt=false;

					map<int, bool>::const_iterator it2=liks[h][r].hapIndelCovered.find(it->first);
						if (it2!=liks[h][r].hapIndelCovered.end() && it2->second) covered=true;
					
						if (covered) { // hapHasVar[] is to check whether haplotype truely has variant or only the reference
							if  (reads[r].onReverseStrand) nrt=true; else nft=true;
							double mq=-10*log10(1.0-reads[r].mapQual);
							msq+=mq*mq;
							n++;
						}
						if (nft) numf++;
						if (nrt) numr++;
					}
				}
			}

			if (n!=0) msq=sqrt(msq/double(n)); else msq=0.0;
	
			int was_candidate = 0;
			
			// determine genotype
			const std::set< AlignedVariant> & alleles = it->second;
			string genotype;
			std::set<string> all_genotype;
			string nref_all;

			int vc_f = 0;
			int vc_r = 0;
			if (1) {
				const AlignedVariant & avar = *alleles.begin();
				const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
				if (av!=NULL) was_candidate=1;
				vc_f += varCoverage[PAV(it->first, avar)].nf;
				vc_r += varCoverage[PAV(it->first, avar)].nr;
			}
			
			string a1="*REF", a2="*REF";
			bool a1_ref=true, a2_ref=true;
			It ita1 = haps[hx1].indels.find(it->first);
			It ita2 = haps[hx2].indels.find(it->first);
					
			if (ita1 != haps[hx1].indels.end() && !ita1->second.isRef()) {
				a1 = ita1->second.getString();
				a1_ref=false;
			}
			if (ita2 != haps[hx2].indels.end() && !ita2->second.isRef()) {
				a2 = ita2->second.getString();
				a2_ref=false;
			}
			all_genotype.insert(a1);
			all_genotype.insert(a2);

			//cout << "a1: " << a1 << " a2: " << a2 << " a1ref " << a1_ref << " a2ref " << a2_ref << endl;

			if (a1_ref && a2_ref) throw string("genotyping error");

			if (a1==a2) {
				genotype = string("1/1");
				nref_all = a1;
			} else {
				
				if (a1_ref) { 
					genotype = string("0/1");
					nref_all = a2;
				} else if (a2_ref) {
					genotype = string("0/1");
					nref_all = a1;
				} else  {
					nref_all = a1+','+a2;
					genotype = string("1/2");
					
					if (1) {
						const AlignedVariant & avar = *alleles.rbegin();
						const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
						if (av!=NULL) was_candidate=1;
						vc_f += varCoverage[PAV(it->first, avar)].nf;
						vc_r += varCoverage[PAV(it->first, avar)].nr;
					}
				}
			}

			// determine genotype quality
			//cout << "POS: " << it->first << endl;
			double max_ll_altgeno = -HUGE_VAL;
			for (size_t h1=0;h1<nh;h1++) if (filtered[h1]==0) for (size_t h2=h1;h2<nh;h2++) if (filtered[h2]==0) {
				if (!( (h1==hx1 && h2 == hx2) || (h2==hx1 && h1 == hx2))) {
					std::set<string> _alt_genotype;
					 It it2=haps[h1].indels.find(it->first);
					 if (it2 == haps[h1].indels.end() || it2->second.isRef()) {
						 _alt_genotype.insert(string("*REF"));
					 } else {
						 _alt_genotype.insert(it2->second.getString());
					}
					it2=haps[h2].indels.find(it->first);
					 if (it2 == haps[h2].indels.end() || it2->second.isRef()) {
						 _alt_genotype.insert(string("*REF"));
					 } else {
						 _alt_genotype.insert(it2->second.getString());
					}
			//		 cout << "CALLED: " << h1 << " " << h2 << " geno: " <<  *all_genotype.begin() << " " << *all_genotype.rbegin() << " ALT: " << *_alt_genotype.begin() << " " << *_alt_genotype.rbegin() <<  " " << pairs_posterior[h1*nh+h2] << " " << max_ll_altgeno << endl;

				 	if (_alt_genotype != all_genotype && max_ll_altgeno<pairs_posterior[h1*nh+h2]) {
						 max_ll_altgeno = pairs_posterior[h1*nh+h2];
							
					}

				}
			}
			double genoqual = 0.0;
			genoqual = - 10.0*( max_ll_altgeno - addLogs(max_ll_indel, max_ll_altgeno) )/log(10.0);




	

			ostringstream glfs; glfs << genotype << ":" << genoqual;


	 	
			OutputData::Line line(glfData);
			line.set("msg", "ok");
			line.set("index", index);
			line.set("tid", params.tid);
			line.set("analysis_type",string("dip.map"));
			line.set("indidx",0);
			line.set("lpos",leftPos);
			line.set("rpos",rightPos);
			line.set("center_position",candPos);
			line.set("realigned_position",it->first+leftPos);
			line.set("was_candidate_in_window",was_candidate);
			line.set("qual", qual );
			//line.set("post_prob_variant", exp(logp));
			//line.set("est_freq", freq);
			line.set("nref_all", nref_all);
			line.set("num_reads", readidx.size());
			line.set("msq",msq);
			//line.set("numOffAll",numOffBoth);
			//line.set("num_indel",numMappedIndels);
			line.set("num_cover_forward", numf);
			line.set("num_cover_reverse", numr);
			line.set("var_coverage_forward", vc_f);
			line.set("var_coverage_reverse", vc_r);
			line.set("num_unmapped_realigned", numUnmappedRealigned);
			line.set("glf", glfs.str());
			//line.set("likrr", lik[0]);
			//line.set("likrn", lik[1]);
			//line.set("liknn", lik[2]);
			glfData.output(line);
		}
	}
	

	for (map<int, std::set<PAV> >::const_iterator it= allVariantsByPos.begin();it!=allVariantsByPos.end();it++)
	{
		bool has_variants_in_window = 0;
		BOOST_FOREACH(PAV pav, it->second) {
			const AlignedVariant & avar = pav.second;
			const AlignedVariant *av = candidateVariants.findVariant(avar.getStartHap()+leftPos, avar.getType(), avar.getString());
			//cout << "VAR "  << avar.getString() << endl;
			if (av!=NULL) {
				has_variants_in_window=1;
				break;
			}
		}



		std::set<int> support;
		int totnf=0, totnr=0;
		double log5=log(0.5);
		int nf=0;
		int nr=0;

		// compute RMS of mapping qualities

		int pos=it->first;
		int posIdx = posToPosIdx[pos];
		double msq=0.0;
		int n=0;

		//cout << endl;
		//cout << "pos: " << pos << " posIdx " << posIdx << endl;

		typedef std::set<int> IntGenotype;
		map<IntGenotype, double> genLiks;

		typedef map<IntGenotype,double>::iterator IGIt;

		double maxll=-HUGE_VAL;
		int hx1, hx2;

		for (size_t h1=0;h1<nh;h1++) if (filtered[h1]==0) for (size_t h2=h1;h2<nh;h2++) if (filtered[h2]==0) {
			IntGenotype genotype;
			int v1=hapVar[h1*numVarPos+posIdx];
			int v2=hapVar[h2*numVarPos+posIdx];

			genotype.insert(hapVar[h1*numVarPos+posIdx]);
			genotype.insert(hapVar[h2*numVarPos+posIdx]);

			double logPriorPos=0.0;
			//cerr << "FIX THIS!\n" << endl;
//			if ( (v1>0 && varType[v1]==VARSNP) || (v2>0 && varType[v2]==VARSNP)) { logPriorPos += log(params.priorSNP);  } else if ( (v1>0 && varType[v1]==VARINDEL) || (v2>0 && varType[v2]==VARINDEL) ) { logPriorPos+=log(params.priorIndel); };

			AlignedVariant av1, av2;
			if (v1) av1=variants[v1].second; else av1=AlignedVariant("*REF",-1);
			if (v2) av2=variants[v2].second; else av2=AlignedVariant("*REF",-1);

			logPriorPos = getPairPrior(av1,av2,leftPos, candidateVariants);
			double pr=prior[h1*nh+h2]-logPriorPos; // substract prior for this site to obtain likelihood

			//cout << "prior: " << h1 << " " << h2 << ": " << prior[h1*nh+h2] << " logPriorPos " << logPriorPos << endl;
			double ll=pr;
			for (size_t r=0;r<reads.size();r++){
				ll+=log5+addLogs(rl[r*nh+h1],rl[r*nh+h2]);
			}

			//cout << "genotype: " << *(genotype.begin()) << " " << *(genotype.rbegin()) << " lik: " << ll <<endl;


			IGIt igit = genLiks.find(genotype);
			if (igit==genLiks.end()) {
				genLiks[genotype]=ll;
			} else {
				genLiks[genotype]=addLogs(genLiks[genotype],ll);
			}

			if (ll>maxll) {
				maxll=ll;
				hx1=h1;
				hx2=h2;
			}

		}
		//cout << "hx1: " << hx1 << " hx2: " << hx2 << " postprob: " << maxll << endl;

		// see how many unmapped reads were realigned to the MAP haplotypes

		int numUnmappedRealigned = 0;
		for (size_t r=0;r<reads.size();r++) {
			if (reads[r].isUnmapped()) {
				if (liks[hx1][r].offHap == false || liks[hx2][r].offHap == false) {
					numUnmappedRealigned++;
				}
			}
		}

		if (params.outputPooledLikelihoods) {
			string ofLiksFile =  params.fileName;
			ofLiksFile.append(".check.txt");
			ofstream ofLiks(ofLiksFile.c_str());
			if (!ofLiks.is_open()) {
				throw string("Cannot open file for writing");
			}

			// output haplotypes
			//
			ofLiks << "HAPLOTYPES" << endl;
			for (size_t h=0;h<haps.size();h++) {
				ofLiks << h;
				stringstream varss;

				for(map<int, AlignedVariant>::const_iterator it = haps[h].indels.begin(); it != haps[h].indels.end(); it++) {
					varss << leftPos+it->first << "," << it->second.getString() << ";";
				}
				ofLiks << "\t" << varss.str() << endl;
			}

			ofLiks << "READS" << endl;


			for (size_t r=0;r<reads.size();r++) {
				int offBoth = 0;
				if (liks[hx1][r].offHap == true && liks[hx2][r].offHap == true) {
					offBoth =1;
				}
				ofLiks << r << "\t" << bam1_qname(reads[r].getBam()) << "\t" << reads[r].pos << "\t" << reads[r].mapQual;
				for (size_t h=0;h<haps.size();h++) {
					ofLiks << "\t" << liks[h][r].ll;
				}
				for (size_t h=0;h<haps.size();h++) {
					ofLiks << "\t" << int(liks[h][r].offHap);
				}
				ofLiks << endl;

			}
			ofLiks.close();
		}
#define DEBUGDIPLOIDGLF
#ifdef DEBUGDIPLOIDGLF
		if (params.outputPooledLikelihoods) {
			for (size_t r=0;r<reads.size();r++) {
				int mhx1 = 0;
				int mhx2 = 1;
				
				cout  << endl << "**READ** " << r << " " << bam1_qname(reads[r].getBam()) << " mapQual: " << reads[r].mapQual << " liks: " << liks[mhx1][r].ll << " " << liks[mhx2][r].ll << " unMapped: " << reads[r].isUnmapped() << endl;

				if (1) {
					/*
					cout << string(50,' '); cout << reads[r].seq.seq << endl;
					cout << haps[hx1].seq << endl;
					cout << haps[hx2].seq << endl;
					*/
					Read newread(reads[r]);

					/*
					newread.mapQual = 1.0 - 1e-20;
					newread.complement();
					newread.reverse();
					*/

					cout << "first: " << endl;
					ObservationModelFBMaxErr obs(haps[mhx1], newread, leftPos, params.obsParams);
					cout << string(50,' ') << haps[mhx1].seq << endl;
					obs.printAlignment(50);


					cout << "second: " << endl;
					ObservationModelFBMaxErr obs2(haps[mhx2], newread, leftPos, params.obsParams);
					cout << string(50,' ') << haps[mhx2].seq << endl;
					obs2.printAlignment(50);


				} else {
					int hidx = hx1; if (liks[hx2][r].ll>liks[hx1][r].ll) hidx = hx2;
					cout << "hidx: " << hidx << " hx1: " << hx1 << " hx2: " << hx2 << endl;
					ObservationModelFBMaxErr obs(haps[hidx], reads[r], leftPos, params.obsParams);
					cout << string(50,' ') << haps[hidx].seq << endl;
					obs.printAlignment(50);
				}
			}
		}

#endif




		double allmsq=0.0;
		int numMappedIndels=0;

		int nBQT=0, nmmBQT=0;
		double mLogBQ=0.0;
		int nMMLeft=0;
		int nMMRight=0;
		int numOffBoth =0;

		BOOST_FOREACH(int r, readidx) {
			double mq=-10*log10(1.0-reads[r].mapQual);
			allmsq+=(mq*mq);
			//cout << " " << 10*log10(1-reads[r].mapQual);

			int mlidx; double ml=-HUGE_VAL;
			std::set<int> mlis;

			if (liks[hx1][r].offHap && liks[hx2][r].offHap) numOffBoth++;

			if (liks[hx1][r].ll>=liks[hx2][r]) {
				mlidx=hx1;
				ml=liks[hx1][r].ll;
			} else {
				mlidx=hx2;
				ml=liks[hx2][r].ll;
			}

			mlis.insert(mlidx);

			bool nrt=false, nft=false;

			map<int,bool>::const_iterator it;
			BOOST_FOREACH(int h, mlis) {
				bool covered=false;
				numMappedIndels += int(liks[h][r].indels.size());
				nBQT+=liks[h][r].nBQT;
				nmmBQT+=liks[h][r].nmmBQT;
				mLogBQ+=liks[h][r].mLogBQ;
				if (liks[h][r].nMMLeft>=2) nMMLeft++;
				if (liks[h][r].nMMRight>=2) nMMRight++;


				map<int, AlignedVariant>::const_iterator hit=haps[h].indels.find(pos);
				if (hit->second.isIndel()) {
					it=liks[h][r].hapIndelCovered.find(pos);
					if (it!=liks[h][r].hapIndelCovered.end() && it->second) covered=true;
				} else if (hit->second.isSNP()) {
					it=liks[h][r].hapSNPCovered.find(pos);
					if (it!=liks[h][r].hapSNPCovered.end() && it->second) covered=true;
				}
				if (covered) { // hapHasVar[] is to check whether haplotype truely has variant or only the reference
					if  (reads[r].onReverseStrand) nrt=true; else nft=true;
					double mq=-10*log10(1.0-reads[r].mapQual);
					msq+=mq*mq;
					n++;
				}
			}
			if (nft) nf++;
			if (nrt) nr++;
		} // foreach read r
	//	cout << endl;

		if (n!=0) msq=sqrt(msq/double(n)); else msq=0.0;
		totnf+=nf;
		totnr+=nr;

		allmsq=(readidx.size()!=0)?sqrt(allmsq/double(readidx.size())):0;


		// recode variant indexes to glf/vcf indexes
		int nidx=1;
		map <int, int> toVCFidx;
		vector<string> alleles;
		alleles.push_back("R");
		toVCFidx[0]=0;

		ostringstream oAlleles;
		ostringstream oCovForward;
		ostringstream oCovReverse;
		int first=1;
		for (size_t h=0;h<nh;h++) {
			int v = hapVar[h*numVarPos+posIdx];
			if (v!=0) {
				map<int,int>::iterator tit = toVCFidx.find(v);
				if (tit==toVCFidx.end()) {
					toVCFidx[v]=nidx++;
					alleles.push_back(variants[v].second.getString());
					string str=(first==1) ? string(""):string(",");

					oAlleles << str << variants[v].second.getString();
					oCovForward << str << varCoverage[variants[v]].nf;
					oCovReverse << str << varCoverage[variants[v]].nr;
					first = 0;
				}
			}
		}


		// compute genotype posterior

		ostringstream o;

		first=1;
		for (map<IntGenotype, double>::iterator git=genLiks.begin();git!=genLiks.end();git++) {
			int v1 = *(git->first.begin());
			int v2 = *(git->first.rbegin());
			int a1 = toVCFidx[ *(git->first.begin()) ];
			int a2 = toVCFidx[ *(git->first.rbegin()) ];

			string str=(first==1) ? string(""):string(",");
			o << str << a1 << "/" << a2 << ":" << git->second;

			double logPrior=0.0;
			if ( (v1>0 && varType[v1]==VARSNP) || (v2>0 && varType[v2]==VARSNP)) { logPrior += log(params.priorSNP);  } else if ( (v1>0 && varType[v1]==VARINDEL) || (v2>0 && varType[v2]==VARINDEL) ) { logPrior+=log(params.priorIndel); };

			git->second -= logPrior;
			first = 0;
		}





		if (params.outputGLF) {
			OutputData::Line line(glfData);
			line.set("msg", "ok");
			line.set("index", index);
			line.set("tid", params.tid);
			line.set("analysis_type",program);
			line.set("indidx",0);
			line.set("lpos",leftPos);
			line.set("rpos",rightPos);
			line.set("center_position",candPos);
			line.set("realigned_position",pos+leftPos);
			line.set("was_candidate_in_window",has_variants_in_window);
			line.set("logZ", maxll );
			//line.set("post_prob_variant", exp(logp));
			//line.set("est_freq", freq);
			line.set("nBQT", nBQT);
			line.set("nmmBQT", nmmBQT);
			line.set("mLogBQ", mLogBQ/double(nBQT));
			line.set("nMMLeft", nMMLeft);
			line.set("nMMRight", nMMRight);
			line.set("nref_all", oAlleles.str());
			line.set("num_reads", readidx.size());
			line.set("msq",allmsq);
			line.set("numOffAll",numOffBoth);
			line.set("num_indel",numMappedIndels);
			line.set("num_cover_forward", nf);
			line.set("num_cover_reverse", nr);
			
			line.set("var_coverage_forward", oCovForward.str());
			line.set("var_coverage_reverse", oCovReverse.str());


			line.set("glf", o.str());
			line.set("num_unmapped_realigned", numUnmappedRealigned);
			//line.set("likrr", lik[0]);
			//line.set("likrn", lik[1]);
			//line.set("liknn", lik[2]);
			glfData.output(line);
		}
	// glfOutput <<  PRID << " "  << b << " " << params.tid << " " << candPos << " " << pav.first+leftPos << " " << pav.second.getString() << " " << reads.size() <<  " " << msq << " "  << nf << " " << nr << " " << lik[0] << " " << lik[1] << " " << lik[2] << endl;


  }

}
#endif

void DetInDel::estimateHaplotypeFrequencies(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks, vector<double> & hapFreqs)
{

	// estimate haplotype frequencies using EM
	hapFreqs.clear();

	size_t nh=haps.size();
	size_t nr=reads.size();




	vector<double> rl(nh*nr,0.0); // read given haplotype likelihoods

	vector<double> z(nh*nr,0.0); // expectations of read-haplotype indicator variables
	vector<double> pi(nh); // log of haplotype frequencies
	vector<double> nk(nh,0.0); // counts for each haplotype

	hapFreqs=nk;

	// initialize frequencies
	for (size_t h=0;h<nh;h++) pi[h]=1.0/double(nh);

	for (size_t h=0;h<nh;h++) for (size_t r=0;r<nr;r++) {
		// initialize read-haplotype likelihoods
		rl[h*nr+r]=liks[h][r].ll;

		// initialize expectations of indicator variables
		z[h*nr+r]=0.5;
	}


	bool converged=false;
	double tol=params.EMtol;

	double eOld=-HUGE_VAL, eNew;

	cout << "EM HapFreqs:";

	int iter=0;
	while (!converged) {

		// compute expectation of indicator variables
		for (size_t h=0;h<nh;h++) nk[h]=0.0;

		int idx=0;
		for (size_t r=0;r<nr;r++) {
			double lognorm=-HUGE_VAL;
			// compute responsibilities
			for (size_t h=0;h<nh;h++) {
				z[h*nr+r]=pi[h]+(rl[h*nr+r]);
				lognorm=addLogs(lognorm, z[h*nr+r]);
			}
			// normalize and compute counts
			for (size_t h=0;h<nh;h++) {
				z[nr*h+r]-=lognorm;
				z[nr*h+r]=exp(z[nr*h+r]);

				nk[h]+=z[nr*h+r];
			}
		}

		// compute frequencies

		for (size_t h=0;h<nh;h++) {
			double nph=nk[h]/nr;
			pi[h]=log(nph);
		}


		idx=0;
		eNew=0.0;
		for (size_t h=0;h<nh;h++) {

		for (size_t r=0;r<nr;r++) {
			// compute responsibilities
				eNew+=z[idx]*( pi[h]+rl[idx]);
				idx++;
			}
		}
		//cout << "[" << eNew << "]" << endl;
		//
		if (eOld>eNew) throw string("EM Error in estimateHapFreqs");
		converged=(fabs(eOld-eNew))<tol || iter>25;

		eOld=eNew;


		iter++;
	}

	for (size_t h=0;h<nh;h++) { cout << " " << exp(pi[h]); }
	cout << endl;

	// output haplotypes and estimated frequencies

	for (size_t h=0;h<nh;h++) hapFreqs[h]=exp(pi[h]);
}


void DetInDel::computePairLikelihoods(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks, vector<HapPairLik> & likPairs, bool usePrior, const AlignedCandidates & candidateVariants, int leftPos)
{
//	cout << "Computing pair likelihoods...\n";
	likPairs.clear();
	size_t lh=haps.size();
	likPairs.reserve(lh*(lh/2));
	//const double log10=log(10);
	double maxLL=-HUGE_VAL; int hpm, hmm;
	size_t midx;
	for (size_t hp=0;hp<lh;hp++) for (size_t hm=hp;hm<lh;hm++) {
		double ll=0.0;
		HapPairLik hpl;
		hpl.numIndFirst=0;
		hpl.numIndSecond=0;
		hpl.numOffBoth=0;
		hpl.numOffBothError=0.0;
		hpl.numFirst=0;
		hpl.numSecond=0;
		hpl.h1=hp;
		hpl.h2=hm;
		for (size_t r=0;r<reads.size();r++) {
			ll+=(addLogs(liks[hp][r].ll,liks[hm][r].ll)+log(.5));
			const MLAlignment & ml1=liks[hp][r];
			const MLAlignment & ml2=liks[hm][r];

			// record which indel was on which haplotype
			if (!ml1.offHap && ml1.ll>ml2.ll && ml1.indels.size()!=0) hpl.numIndFirst++;
			else if (!ml2.offHap && ml2.ll>ml1.ll && ml2.indels.size()!=0) hpl.numIndSecond++;
			if (ml1.offHapHMQ && ml2.offHapHMQ) { hpl.numOffBoth++; hpl.numOffBothError+=reads[r].mapQual; };
			if (ml1.ll>=ml2.ll) hpl.numFirst++;
			if (ml2.ll>=ml1.ll) hpl.numSecond++;

	//		determine read coverage of all the variants
	//		TODO this part is really slow!
			for (map<int, bool>::const_iterator it=ml1.hapIndelCovered.begin();it!=ml1.hapIndelCovered.end();it++) if (it->second) if (ml1.ll>=ml2.ll) { if (reads[r].onReverseStrand) hpl.hapIndelCoverage1[it->first].nr++; else hpl.hapIndelCoverage1[it->first].nf++; }
			for (map<int, bool>::const_iterator it=ml2.hapIndelCovered.begin();it!=ml2.hapIndelCovered.end();it++) if (it->second) if (ml2.ll>=ml1.ll) { if (reads[r].onReverseStrand) hpl.hapIndelCoverage2[it->first].nr++; else hpl.hapIndelCoverage2[it->first].nf++; }
			for (map<int, bool>::const_iterator it=ml1.hapSNPCovered.begin();it!=ml1.hapSNPCovered.end();it++) if (it->second) if (ml1.ll>=ml2.ll) { if (reads[r].onReverseStrand) hpl.hapSNPCoverage1[it->first].nr++; else hpl.hapSNPCoverage1[it->first].nf++; }
			for (map<int, bool>::const_iterator it=ml2.hapSNPCovered.begin();it!=ml2.hapSNPCovered.end();it++) if (it->second) if (ml2.ll>=ml1.ll) { if (reads[r].onReverseStrand) hpl.hapSNPCoverage2[it->first].nr++; else hpl.hapSNPCoverage2[it->first].nf++; }


		}
		if (usePrior) ll+=getHaplotypePrior(haps[hp], haps[hm], leftPos, candidateVariants);

		hpl.ll=ll;
		if (ll>maxLL) {
			maxLL=ll;
			hpm=hp;
			hmm=hm;
			midx=likPairs.size();
		}
	//	cout << "hp: " << hp << " hm: " << hm << " ll: " << ll << endl;
		likPairs.push_back(hpl);
	}


//	cout << "ML hap: " << hpm << " " << hmm << " midx: " << midx << endl;
	/*
	cout << haps[hpm] << endl;
	cout << "indels: "; for (map<int, AlignedVariant>::const_iterator it=haps[hpm].indels.begin();it!=haps[hpm].indels.end();it++) {
		cout << "[" << it->first << "," << it->second.getString() << "]";
	}
	cout << endl;
	cout << "coverage: ";
	for (map<int, VariantCoverage>::const_iterator it=likPairs[midx].hapIndelCoverage1.begin();it!=likPairs[midx].hapIndelCoverage1.end();it++) {
		cout << "[" << it->first << "," << it->second.nf << "," << it->second.nr << "]";
	}
	cout << endl;
	cout << haps[hmm] << endl;
	cout << "indels: "; for (map<int, AlignedVariant>::const_iterator it=haps[hmm].indels.begin();it!=haps[hmm].indels.end();it++) {
		cout << "[" << it->first << "," << it->second.getString() << "]";
	}
	cout << endl;
	for (map<int, VariantCoverage>::const_iterator it=likPairs[midx].hapIndelCoverage2.begin();it!=likPairs[midx].hapIndelCoverage2.end();it++) {
		cout << "[" << it->first << "," << it->second.nf << "," << it->second.nr << "]";
	}

	cout << endl;
	*/

	class SortFunc {
	public:
		static bool sortFunc(const HapPairLik & hpl1, const HapPairLik & hpl2)
		{
			// sort in decreasing order
			return hpl1.ll>hpl2.ll;
		}
	};
	sort(likPairs.begin(), likPairs.end(),SortFunc::sortFunc);
}

void DetInDel::statisticsHaplotypePair(const vector<Haplotype> & haps, const vector<Read> & reads, const vector<vector<MLAlignment> > & liks, HapPairLik & hpl, OutputData::Line & line)
{
	hpl.numIndFirst=0;
	hpl.numIndSecond=0;
	hpl.numOffBoth=0;
	hpl.numOffBothError=0.0;
	hpl.numFirst=0;
	hpl.numSecond=0;
	int hp=hpl.h1;
	int hm=hpl.h2;
	for (size_t r=0;r<reads.size();r++) {
		const MLAlignment & ml1=liks[hp][r];
		const MLAlignment & ml2=liks[hm][r];

		if (!ml1.offHap && ml1.ll>ml2.ll && ml1.indels.size()!=0) hpl.numIndFirst++;
		else if (!ml2.offHap && ml2.ll>ml1.ll && ml2.indels.size()!=0) hpl.numIndSecond++;
		if (ml1.offHapHMQ && ml2.offHapHMQ) { hpl.numOffBoth++; hpl.numOffBothError+=reads[r].mapQual; };
		if (ml1.ll>=ml2.ll) hpl.numFirst++;
		if (ml2.ll>=ml1.ll) hpl.numSecond++;

//		determine read coverage of all the variants
//		TODO this part is really slow!
		for (map<int, bool>::const_iterator it=ml1.hapIndelCovered.begin();it!=ml1.hapIndelCovered.end();it++) if (it->second) if (ml1.ll>=ml2.ll) { if (reads[r].onReverseStrand) hpl.hapIndelCoverage1[it->first].nr++; else hpl.hapIndelCoverage1[it->first].nf++; }
		for (map<int, bool>::const_iterator it=ml2.hapIndelCovered.begin();it!=ml2.hapIndelCovered.end();it++) if (it->second) if (ml2.ll>=ml1.ll) { if (reads[r].onReverseStrand) hpl.hapIndelCoverage2[it->first].nr++; else hpl.hapIndelCoverage2[it->first].nf++; }
		for (map<int, bool>::const_iterator it=ml1.hapSNPCovered.begin();it!=ml1.hapSNPCovered.end();it++) if (it->second) if (ml1.ll>=ml2.ll) { if (reads[r].onReverseStrand) hpl.hapSNPCoverage1[it->first].nr++; else hpl.hapSNPCoverage1[it->first].nf++; }
		for (map<int, bool>::const_iterator it=ml2.hapSNPCovered.begin();it!=ml2.hapSNPCovered.end();it++) if (it->second) if (ml2.ll>=ml1.ll) { if (reads[r].onReverseStrand) hpl.hapSNPCoverage2[it->first].nr++; else hpl.hapSNPCoverage2[it->first].nf++; }

		// record which indel was on which haplotype
	}

	line.set("num_off_hap", hpl.numOffBoth);
	line.set("num_mapped_to_first",hpl.numFirst);
	line.set("num_mapped_to_second",hpl.numSecond);
}



void parseRegionString(const string & region, int & start, int & end)
{
	string filtered;
	for(size_t x=0;x<region.size();x++) {
		char c=region[x];
		if (c=='-') filtered+=' ';
		else if (c!=',') filtered+=c;
	}
	istringstream is(filtered);
	string e; is >> e;
	if (!from_string(start,e,std::dec)) throw string("Cannot parse region for start!");
	is >> e;
	if (!from_string(end,e,std::dec)) throw string("Cannot parse region end!");
}

void getParameters(po::variables_map & vm, DetInDel::Parameters & params)
{
	params.maxHap=vm["maxHap"].as<uint32_t> ();
	params.maxReads=vm["maxRead"].as<uint32_t> ();
	params.width=vm["width"].as<uint32_t> ();
	params.mapQualThreshold=vm["mapQualThreshold"].as<double>();
	params.skipMaxHap=vm["skipMaxHap"].as<uint32_t>();
	//params.glfNumHap=vm["glfNumHap"].as<uint32_t>();
	params.inferenceMethod=vm["inferenceMethod"].as<string>();
	params.minReadOverlap=vm["minReadOverlap"].as<uint32_t>();
	params.maxReadLength=vm["maxReadLength"].as<uint32_t>();
	//params.scaleErr=vm["mapScaleError"].as<double>();
	//params.minCount=vm["minCount"].as<uint32_t>();
	params.maxHapReadProd=vm["maxHapReadProd"].as<uint32_t>();

	params.priorSNP=vm["priorSNP"].as<double>();
	params.priorIndel=vm["priorIndel"].as<double>();
	params.bayesa0=vm["bayesa0"].as<double>();
	params.bayesType=vm["bayesType"].as<string>();

	


	if (vm.count("ref")) {
		params.alignAgainstReference=true;
		params.refFileName=vm["ref"].as<string>();
	} else {
		params.alignAgainstReference=false;
	}

	params.obsParams.pError=vm["pError"].as<double>();
	params.obsParams.pMut=vm["pMut"].as<double>();

	//params.obsParams.baseQualThreshold=vm["baseQualThreshold"].as<double>();
	//	params.obsParams.fixedBaseQual=vm["fixedBaseQual"].as<double>();
	params.obsParams.maxLengthIndel=vm["maxLengthIndel"].as<int>();
	params.obsParams.maxLengthDel=params.obsParams.maxLengthIndel;
	params.obsParams.mapQualThreshold=vm["capMapQualThreshold"].as<double>();
	params.obsParams.capMapQualFast=vm["capMapQualFast"].as<double>();
	//params.obsParams.scaleErr=vm["obsScaleError"].as<double>();
	//params.obsParams.numE=vm["numE"].as<int>();
	params.obsParams.padCover = vm["flankRefSeq"].as<int>();
	params.obsParams.maxMismatch = vm["flankMaxMismatch"].as<int>();
	params.checkAllCIGARs=vm["checkAllCIGARs"].as<int>();

	params.varFileIsOneBased=vm.count("varFileIsOneBased")?true:false;
	params.outputRealignedBAM=vm.count("outputRealignedBAM")?true:false;
	params.analyzeLowFreq=vm.count("compareReadHap")?true:false;
	params.analyzeLowFreqDiffThreshold=vm["compareReadHapThreshold"].as<double>();
	params.showHapDist=vm.count("showEmpirical")?true:false;
	params.showCandHap=vm.count("showCandHap")?true:false;
	params.showReads=vm.count("showReads")?true:false;
	params.quiet=vm.count("quiet")?true:false;
	params.computeML=vm.count("computeML")?true:false;
	params.computeMAP=vm.count("computeMAP")?true:false;
	params.doDiploid=vm.count("doDiploid")?true:false;

	params.filterHaplotypes=vm.count("filterHaplotypes")?true:false;

	params.printCallsOnly=vm.count("printCallsOnly")?true:false;
	params.estimateHapFreqs=vm.count("doPooled")?true:false;
	params.outputPooledLikelihoods=vm.count("opl")?true:false;
	params.showHapAlignments=vm.count("showHapAlignments")?true:false;
	if (vm.count("filterReadAux")) params.filterReadAux=vm["filterReadAux"].as<string>();
	if (vm.count("processRealignedBAM")) params.processRealignedBAM=vm["processRealignedBAM"].as<string>();

	params.slower=vm.count("faster")?false:true;
	params.changeINStoN=vm.count("changeINStoN")?true:false;
	params.outputGLF=true;


	// removed options
/*
	params.outputRealignedBAM=vm.count("outputRealignedBAM")?true:false;
	params.obsParams.modelType=vm["modelType"].as<string>();
	params.mapUnmappedReads=vm.count("mapUnmapped")?true:false;
	params.obsParams.mapUnmappedReads=vm.count("mapUnmapped")?true:false;
	params.obsParams.pFirstgLO=vm["pFirstgLO"].as<double>();
	//params.numOutputTopHap=vm["numOutputTopHap"].as<int>();

*/

}



int smain(int argc, char *argv[])
{
	if (1) {
		Haplotype hap;
		Read read;

		//hap.seq=          "ATCGTGTAGCTCTCTGGCTGGCTAGCTGATTGGCTCTTGCC";
		//read.seq.seq=              "CTCTCTGGCTGGCTAGCGAT";
		Haplotype ref;
		//                 012345678901234567890123456789
		hap.seq=          "ATCGATTCGTGATATATATATTCAATGTAGTCGCTAG";
		read.seq.seq=     "ATCGATTCGTGATAATATTCAATGTAGTCGCTAG";


		//hap.seq=          "ATCGATTCGTGATATATATATTCAATGTAGTCGCTAG";
		//read.seq.seq=     "ATCGATTCGTGATATATATATAATTCAATGTAGTCGCTAG";

		//                 012345678901234567890123456789
		//hap.seq=          "ATCGATTCGTGTTTTTTCAATGTAGTCGCTAG";
		//read.seq.seq=     "ATCGATTCGTGTTTTTCAATGTAGTCGCTAG";

		read.mapQual=1-1e-16;

		ObservationModelParameters obsParams;
		read.setAllQual(0.99);

		ObservationModelFBMaxErr omfbe(hap, read, 0, obsParams);
		/*
		ObservationModelS oms(hap, read, 0, obsParams);
		HapHash hash(4, hap);

		oms.align(hash);
		*/

		double ll= omfbe.calcLikelihood();

		cout << "ll: " << ll << endl;
		cout << string(50,' ') << hap.seq << endl;
		omfbe.printAlignment(50);
	}
	if (0) {
		Haplotype hap;
		Read read;

		//hap.seq=          "ATCGTGTAGCTCTCTGGCTGGCTAGCTGATTGGCTCTTGCC";
		//read.seq.seq=              "CTCTCTGGCTGGCTAGCGAT";

		hap.seq=          "AAAATCACCAACACTTCATAATCTATTTTTTCCCCTGAGGAACTTCCTAAAATGAATAAAAAAAAACCCCAGCCACATCTGCATTTGCAAACAGGAAACTCTGCAAGCCATACTAAGACCAAAGCTTAGTT";
		read.seq.seq=     "CAAACAGGAAACTCTGCAAGCCATACTAAGACCAAAGCTTAGTTA";


		read.mapQual=1-1e-16;

		ObservationModelParameters obsParams;
		read.setAllQual(0.99);

		ObservationModelFBMaxErr omfbe(hap, read, 0, obsParams);
		/*
		ObservationModelS oms(hap, read, 0, obsParams);
		HapHash hash(4, hap);

		oms.align(hash);
		*/

		double ll= omfbe.calcLikelihood();

		cout << "ll: " << ll << endl;
		cout << string(50,' ') << hap.seq << endl;
		omfbe.printAlignment(50);
	}

	return 0;

}






#ifdef DINDEL
int main(int argc, char *argv[])
{
	po::options_description which("[Required] Program option");
	which.add_options()
	("analysis", po::value<string>()->default_value("indels"),"Analysis type:\n"
													          "getCIGARindels:  Extract indels from CIGARs of mapped reads, and infer libary insert size distributions\n"
															  "indels: infer indels\n"
															  "realignCandidates: Realign/reposition candidates in candidate file\n");

	po::options_description required("[Required] ");
	required.add_options()
	("ref", po::value<string>(),"fasta reference sequence (should be indexed with .fai file)")
	("outputFile", po::value<string>(),"file-prefix for output results");

	po::options_description baminput("[Required] BAM input. Choose one of the following");
	baminput.add_options()
	("bamFile",po::value<string>(), "read alignment file (should be indexed)")
	("bamFiles",po::value<string>(), "file containing filepaths for BAMs to be jointly analysed (not possible for --analysis==indels");


	po::options_description regioninput("[Required for analysis == getCIGARindels]: \nRegion to be considered for extraction of candidate indels.");
	regioninput.add_options()
	("region", po::value<string>(),"region to be analysed in format start-end, eg. 1000-2000")
	("tid", po::value<string>(),"target sequence (eg 'X') ");

	po::options_description varfileinput("[Required for analysis == indels]");
	varfileinput.add_options()
	("varFile", po::value<string>(), "file with candidate variants to be tested.")
	("varFileIsOneBased", "coordinates in varFile are one-based");

	po::options_description output_options("Output options");
	output_options.add_options()
	("outputRealignedBAM", "output BAM file with realigned reads")
	("processRealignedBAM", po::value<string>(),"ABSOLUTE path to script to process realigned BAM file")
	//("outputGLF", "outputGLF for individuals in each bam file")
	("quiet", "quiet output");
	//("printCallsOnly", "print only genotypes where call_lik_ref>0.0001 (only affects --single)");

	po::options_description single_analysis("parameters for analysis==indels option");
	single_analysis.add_options()
	("doDiploid", "analyze data assuming a diploid sequence")
	("doPooled", "estimate haplotype frequencies using Bayesian EM algorithm.\nMay be applied to single individual and pools.");

	po::options_description analysis_opt("General algorithm parameters");
	analysis_opt.add_options()
	//("mapUnmapped", "remap unmapped reads for which mate is mapped")
	("faster","use faster but less accurate ungapped read-haplotype alignment model")
	("filterHaplotypes","prefilter haplotypes based on coverage")
	("flankRefSeq",po::value<int>()->default_value(2),"#bases of reference sequence of indel region")
	("flankMaxMismatch",po::value<int>()->default_value(2),"max number of mismatches in indel region")
	("priorSNP", po::value<double>()->default_value(1.0/1000.0), "prior probability of a SNP site")
	("priorIndel", po::value<double>()->default_value(1.0/10000.0), "prior probability of a detected indel not being a sequencing error")
	("width", po::value<uint32_t>()->default_value(60), "number of bases to left and right of indel")
	("maxHap", po::value<uint32_t>()->default_value(8), "maximum number of haplotypes in likelihood computation")
	("maxRead", po::value<uint32_t>()->default_value(10000), "maximum number of reads in likelihood computation")
	("mapQualThreshold", po::value<double>()->default_value(0.99), "lower limit for read mapping quality")
	("capMapQualThreshold", po::value<double>()->default_value(100.0), "upper limit for read mapping quality in observationmodel_old (phred units)")
	("capMapQualFast", po::value<double>()->default_value(45.0), "cap mapping quality in alignment using fast ungapped method\n (WARNING: setting it too high (>50) might result in significant overcalling!)")
	("skipMaxHap", po::value<uint32_t>()->default_value(200), "skip computation if number of haplotypes exceeds this number")
	//("glfNumHap", po::value<uint32_t>()->default_value(5), "number of haplotypes per glf-class")
	//("numOutputTopHap", po::value<int>()->default_value(5), "number of haplotype pairs output to haplotype file")
	("minReadOverlap", po::value<uint32_t>()->default_value(20),"minimum overlap between read and haplotype")
	("maxReadLength", po::value<uint32_t>()->default_value(500),"maximum length of reads")
	("minCount", po::value<uint32_t>()->default_value(1), "minimum number of WS observations of indel")
	("maxHapReadProd",po::value<uint32_t>()->default_value(10000000), "skip if product of number of reads and haplotypes exceeds this value")
	("changeINStoN", "change sequence of inserted sequence to 'N', so that no penalty is incurred if a read mismatches the inserted sequence");
	po::options_description pooled_analysis("parameters for --pooled option");
	pooled_analysis.add_options()
	("bayesa0", po::value<double>()->default_value(0.001), "Dirichlet a0 parameter haplotype frequency prior")
	("bayesType",po::value<string>()->default_value("singlevariant"), "Bayesian EM program type (all or singlevariant or priorpersite)");


	po::options_description option_filter("General algorithm filtering options");
	option_filter.add_options()
	("checkAllCIGARs",po::value<int>()->default_value(1),"include all indels at the position of the call site")
	("filterReadAux", po::value<string>(), "match string for exclusion of reads based on auxilary information");


	po::options_description obsModel("Observation model parameters");
	obsModel.add_options()
	("pError", po::value<double>()->default_value(5e-4), "probability of a read indel")
	//("modelType", po::value<string>()->default_value("probabilistic"), "probabilistic/threshold")
	("pMut", po::value<double>()->default_value(1e-5), "probability of a mutation in the read")
	("maxLengthIndel", po::value<int>()->default_value(5), "maximum length of a _sequencing error_ indel in read [not for --faster option]");
	//("pFirstgLO",po::value<double>()->default_value(0.01),"probability of transition from off the haplotype to on the haplotype");

	po::options_description libParams("Library options");
	libParams.add_options()
	("libFile", po::value<string>(), "file with library insert histograms (as generated by --analysis getCIGARindels)");


	po::options_description miscAnalysis("Misc results analysis options");
	miscAnalysis.add_options()
	("compareReadHap",  "compare likelihood differences in reads against haplotypes")
	("compareReadHapThreshold", po::value<double>()->default_value(0.5), "difference threshold for viewing")
	("showEmpirical", "show empirical distribution over nucleotides")
	("showCandHap", "show candidate haplotypes for fast method")
	("showHapAlignments","show for each haplotype which reads map to it")
	("showReads","show reads")
	("inferenceMethod",po::value<string>()->default_value("empirical"), "inference method")
	("opl","output likelihoods for every read and haplotype");

	required.add(which).add(baminput).add(regioninput).add(varfileinput).add(output_options).add(single_analysis).add(analysis_opt).add(pooled_analysis).add(option_filter).add(obsModel).add(libParams).add(miscAnalysis);

	po::variables_map vm;

	try {
			po::store(po::parse_command_line(argc, argv, required), vm);
	} catch (boost::program_options::error) {
			cout << "Error parsing input options. Usage: \n\n" << required <<"\n";
			exit(1);
	}
	po::notify(vm);

	// analysis
	if (!(vm.count("analysis"))) {
		cerr << "Error: Specify which analysis (--analysis) is required." << endl;
		exit(1);
	}

	 // required
	if (!(vm.count("ref") && vm.count("outputFile"))) {
		cerr << "Error: One of the following options was not specified:  --ref --tid or --outputFile" << endl;
		exit(1);
	}

	if (vm.count("getCIGARindels") && vm.count("region") && !vm.count("tid")) {
		cerr << "--tid must be specified if analysis==getCIGARindels and --region option is used. " << endl;
		exit(1);
	}
//#define DEBUGGING
#ifndef DEBUGGING
	try {
#endif
		// extract required parameters
		string file;
		int multipleFiles=0;
		string analysis=vm["analysis"].as<string>();

		// baminput
		if (analysis=="indels" || analysis=="getCIGARindels") {
			if (!(vm.count("bamFile") || vm.count("bamFiles"))) {
					cerr << "Error: Specify either --bamFile or --bamFiles." << endl;
					exit(1);
			}

			if (vm.count("bamFile")) {
			  file=vm["bamFile"].as< string >();
			  cout << "Reading BAM file: " << file << endl;
			} else if (vm.count("bamFiles")) {
			  file=vm["bamFiles"].as<string>();
			  multipleFiles=1;
			}
		}

		string faFile=vm["ref"].as<string>();
		string outputFile=vm["outputFile"].as< string >();

		string modelType="probabilistic"; //vm["modelType"].as< string >();
		DetInDel::Parameters params(string("1"), outputFile, modelType);
		getParameters(vm, params);

		if (analysis=="getCIGARindels") {
			GetCandidatesFromCIGAR gcfc;
			string outputFile=vm["outputFile"].as< string >();// outputFile.append(".variants.txt");
			fasta::Fasta fa(faFile);
			if (vm.count("region")) {
				string tid=vm["tid"].as<string>();
				string region=vm["region"].as<string>();
				int start, end;
				parseRegionString(region, start, end);
				DetInDel detInDel(file, params, multipleFiles);
				const vector<MyBam *> &  bams = detInDel.getMyBams();

				cout << "Getting indels from CIGARs in mapped reads from region " << tid << ":" << start << "-" << end << endl;
				gcfc.getIndelFromCIGARRegion(bams,tid, start, end, outputFile, fa);

			} else {
				if (multipleFiles) {
					cerr << "Can extract the full set of indels from only BAM file at a time." << endl;
					exit(1);
				}
				gcfc.get(file, outputFile, faFile);
			}
		} else if (analysis=="indels") {
			if (!vm.count("varFile")) {
				cerr << "Please specify the file with the candidate variants." << endl;
				exit(1);
			}

			string varFile = vm["varFile"].as<string>();

			DetInDel detInDel(file, params, multipleFiles);

			if (vm.count("libFile")) {
				cout << "Detected library file..." << endl;
				detInDel.params.mapUnmappedReads=true;
				detInDel.params.obsParams.mapUnmappedReads=true;
				detInDel.addLibrary(vm["libFile"].as<string>());
			}
			detInDel.params.print();


			detInDel.detectIndels(varFile);
		} else if (analysis == "realignCandidates") {
			GetCandidatesFromCIGAR gcfc;
			string outputFile=vm["outputFile"].as< string >(); outputFile.append(".variants.txt");

			if (!vm.count("varFile")) {
				cerr << "Please specify the file with the candidate variants." << endl;
				exit(1);
			}

			string varFile = vm["varFile"].as<string>();

			if (varFile == outputFile) {
				cerr << "outputFile is same as variant file used for input!" << endl;
				exit(1);
			}

			gcfc.realignCandidateFile(varFile, params.varFileIsOneBased,outputFile, faFile);
		} else {
			cerr << "Unrecognized --analysis option." << endl;
			exit(1);
		}
#ifndef DEBUGGING
   }
   catch (string s) {
	cout << "Exception: " << s << endl;
    exit(1);
   }
#endif
   return 0;
}
#endif