File: parasail_aligner.cpp

package info (click to toggle)
parasail 2.6.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,804 kB
  • sloc: ansic: 731,820; cpp: 3,989; python: 3,695; makefile: 1,219; sh: 108
file content (2898 lines) | stat: -rw-r--r-- 97,134 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
/**
 * @file parasail_query
 *
 * @author jeffrey.daily@gmail.com
 *
 * Copyright 2012 Pacific Northwest National Laboratory. All rights reserved.
 *
 * Reads fasta file of database sequences.
 * Optionally reads fasta file of query sequences.
 * Optionally filters inputs through suffix array.
 * Indexes input to learn length and end locations of each sequence.
 * Creates SA, LCP, and BWT.
 * Runs maximal pairs algorithm with the given minimum cutoff length.
 * For each sequence pair, performs alignment of user choice.
 * Output is csv of alignments between sequences.
 */
#include "config.h"

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(HAVE_GETOPT) && defined(HAVE_UNISTD_H)
#include <unistd.h>
#elif defined(HAVE_WINDOWS_H)
#include "wingetopt/src/getopt.h"
#endif
#if defined(HAVE_POLL)
#include <err.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#endif
#if defined(HAVE_WINDOWS_H)
#include <windows.h>
#endif
#if defined(HAVE_FILELENGTH)
#include <io.h>
#endif

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#ifdef _OPENMP
#include <omp.h>
#define THREAD_DOC "      threads: system-specific default, must be >= 1\n"
#else
#define THREAD_DOC "      threads: Warning: ignored; OpenMP was not supported by your compiler\n"
#endif

#include "parasail.h"
#include "parasail/io.h"

#include "sais.h"

#if HAVE_VARIADIC_MACROS
#define eprintf(STREAM, ...) fprintf(STREAM, __VA_ARGS__); fflush(STREAM)
#else
#define eprintf fprintf
#endif

#define GB 1.0E-9

/*
 * config.h above would normally define the correct restrict keyword, 
 * but to avoid conflicts we must wait to redefine it until after all headers.
 */
#if !defined(restrict) && defined(parasail_safe_restrict_cxx)
#define restrict parasail_safe_restrict_cxx
#endif

extern "C" size_t getMemorySize(void);

using ::std::bad_alloc;
using ::std::istringstream;
using ::std::make_pair;
using ::std::pair;
using ::std::set;
using ::std::size_t;
using ::std::stack;
using ::std::string;
using ::std::toupper;
using ::std::transform;
using ::std::vector;

typedef pair<int,int> Pair;

typedef set<Pair> PairSet;
typedef vector<Pair> PairVec;

struct quad {
    int lcp;
    int lb;
    int rb;
    vector<quad> children;

    quad()
        : lcp(0), lb(0), rb(INT_MAX), children() {}
    quad(int lcp, int lb, int rb)
        : lcp(lcp), lb(lb), rb(rb), children() {}
    quad(int lcp, int lb, int rb, vector<quad> children)
        : lcp(lcp), lb(lb), rb(rb), children(children) {}

    bool empty() { return rb == INT_MAX; }
};

inline static void pair_check(
        unsigned long &count_generated,
        PairSet &pairs,
        const int &i,
        const int &j,
        const int * const restrict SA,
        const unsigned char * const restrict BWT,
        const int * const restrict SID,
        const vector<int> &DB,
        const char &sentinal);

inline static void process(
        unsigned long &count_generated,
        PairSet &pairs,
        const quad &q,
        const int * const restrict SA,
        const unsigned char * const restrict BWT,
        const int * const restrict SID,
        const vector<int> &DB,
        const char &sentinal,
        const int &cutoff);

inline static void print_array(
        const char * filename_,
        const int * const restrict array,
        const char * const restrict s1, const int s1Len,
        const char * const restrict s2, const int s2Len);

inline static int self_score(
        const char * const restrict seq,
        int len,
        const parasail_matrix_t *matrix);

inline static void output_edges(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop);

inline static void output_graph(
        FILE *fop,
        int which,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        vector<vector<pair<int,float> > > &graph,
        unsigned long &edge_count,
        long long start,
        long long stop);

inline static void output_stats(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop);

inline static void output_basic(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop);

inline static void output_emboss(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases);

inline static void output_ssw(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases);

inline static void output_sam(
        FILE *fop,
        bool use_sam_header,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases);

inline static void output_trace(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases);

inline static void output_tables(
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop);

inline static void output(
        bool is_stats,
        bool is_table,
        bool is_trace,
        bool edge_output,
        bool use_emboss_format,
        bool use_ssw_format,
        bool use_sam_format,
        bool use_sam_header,
        FILE *fop,
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases);

inline static size_t parse_bytes(const char*);
static void set_signal_handler();

static void print_help(const char *progname, int status) {
    eprintf(stderr, "\nusage: %s "
            "[-a funcname] "
            "[-c cutoff] "
            "[-x] "
            "[-e gap_extend] "
            "[-o gap_open] "
            "[-m matrix] "
            "[-t threads] "
            "[-d] "
            "[-M match] "
            "[-X mismatch] "
            "[-k band size (for nw_banded)] "
            "[-l AOL] "
            "[-s SIM] "
            "[-i OS] "
            "[-v] "
            "[-V] "
            "-f file "
            "[-q query_file] "
            "[-g output_file] "
            "[-O output_format {EMBOSS,SAM,SAMH,SSW}] "
            "[-b batch_size] "
            "[-r memory_budget] "
            "[-C] "
            "[-A alphabet_aliases] "
            "\n\n",
            progname);
    eprintf(stderr, "Defaults:\n"
            "        funcname: sw_stats_striped_16\n"
            "          cutoff: 7, must be >= 1, exact match length cutoff\n"
            "              -x: if present, don't use suffix array filter\n"
            "      gap_extend: 1, must be >= 0\n"
            "        gap_open: 10, must be >= 0\n"
            "          matrix: blosum62\n"
            "              -d: if present, assume DNA alphabet ACGT\n"
            "           match: 1, must be >= 0\n"
            "        mismatch: 0, must be >= 0\n"
THREAD_DOC
            "             AOL: 80, must be 0 <= AOL <= 100, percent alignment length\n"
            "             SIM: 40, must be 0 <= SIM <= 100, percent exact matches\n"
            "              OS: 30, must be 0 <= OS <= 100, percent optimal score\n"
            "                                              over self score\n"
            "              -v: verbose output, report input parameters and timing\n"
            "              -V: verbose memory output, report memory use\n"
            "            file: no default, must be in FASTA format\n"
            "      query_file: no default, must be in FASTA format\n"
            "     output_file: parasail.csv\n"
            "   output_format: no default, must be one of {EMBOSS,SAM,SAMH,SSW}\n"
            "      batch_size: 0 (calculate based on memory budget),\n"
            "                  how many alignments before writing output\n"
            "   memory_budget: 2GB or half available from system query (%.3f GB)\n"
            "              -C: if present, use case sensitive alignments, matrices, etc.\n"
            "alphabet_aliases: traceback will treat these pairs of characters as matches,\n"
            "                  for example, 'TU' for one pair, or multiple pairs as 'XYab'\n",
        getMemorySize()/2.0*GB
            );
    exit(status);
}

#ifdef HAVE_POLL
static int stdin_has_data() {
    int timeout = 100; /* wait 100ms */
    struct pollfd fd;
    fd.fd = 0;
    fd.events = POLLIN;
    fd.revents = 0;
    int ret = poll(&fd, 1, timeout);
    return (ret > 0 && (fd.revents & POLLIN));
}
#elif defined(HAVE_WINDOWS_H)
#if !defined(HAVE_FILELENGTH)
#if defined(HAVE_STRUCT___STAT64) && defined(HAVE__FSTAT64)
#define STATBUF struct __stat64
#define FSTATFUNC _fstat64
#else
#define STATBUF struct stat
#define FSTATFUNC fstat
#endif
static long filelength(int fd) {
    int status;
    STATBUF buffer;
    status = FSTATFUNC(fd, &buffer);
    if (0 == status) {
        return buffer.st_size;
    }
    return -1;
}
#endif /* HAVE_FILELENGTH */
static int stdin_has_data() {
    int stdinHandle = fileno(stdin);
    long stdinFileLength = filelength(stdinHandle);
    if (stdinFileLength < 0) {
        return 0;
    }
    else if (stdinFileLength == 0) {
        int retval = fseek(stdin, 0, 0);
        if (retval < 0) {
            return 0;
        }
        else {
            return 1;
        }
    }
    return 1;
}
#endif

template <class info>
vector<long long> calc_batches(
        long long batch_size,
        bool verbose_memory,
        size_t memory_budget,
        const info *function_info,
        const PairVec &vpairs,
        const vector<long> &BEG,
        const vector<long> &END,
        size_t &memory_estimate)
{
    vector<long long> batches;
    batches.push_back(0);
    size_t result_size = sizeof(parasail_result_t);
    memory_estimate = 0;

    if (verbose_memory) {
        eprintf(stdout, "%20s: %.4f GB\n", "memory remaining", memory_budget*GB);
    }

    /* if user specified batch size on command line, we use it */
    if (0 != batch_size) {
        size_t batch_size_ = static_cast<size_t>(batch_size);
        for (size_t batch=batch_size_; batch<vpairs.size(); batch+=batch_size_) {
            batches.push_back(batch);
        }
        batches.push_back(vpairs.size());
        if (verbose_memory) {
            for (size_t batch=0; batch<batches.size()-1; ++batch) {
                long long start = batches[batch];
                long long stop = batches[batch+1];
                eprintf(stdout, "%20s: %lld-%lld\t%.4f GB\n", "batch",
                        start, stop, memory_estimate*GB);
            }
        }
        return batches;
    }

    if (function_info->is_table
            || function_info->is_rowcol
            || function_info->is_trace) {
        size_t multiplier = 1;
        if (function_info->is_table) {
            if (function_info->is_stats) {
                multiplier = 4;
                result_size += sizeof(parasail_result_extra_stats_t);
                result_size += sizeof(parasail_result_extra_stats_tables_t);
            }
            else {
                multiplier = 1;
                result_size += sizeof(parasail_result_extra_tables_t);
            }
        }
        else if (function_info->is_rowcol) {
            if (function_info->is_stats) {
                multiplier = 4;
                result_size += sizeof(parasail_result_extra_stats_t);
                result_size += sizeof(parasail_result_extra_stats_rowcols_t);
            }
            else {
                multiplier = 1;
                result_size += sizeof(parasail_result_extra_rowcols_t);
            }
        }
        else /* if (function_info->is_trace) */ {
            result_size += sizeof(parasail_result_extra_trace_t);
        }
        int i = vpairs[0].first;
        int j = vpairs[0].second;
        long i_beg = BEG[i];
        long i_end = END[i];
        long i_len = i_end-i_beg;
        long j_beg = BEG[j];
        long j_end = END[j];
        long j_len = j_end-j_beg;
        size_t current_size = 0;
        if (function_info->is_table) {
            current_size = sizeof(int) * multiplier * i_len * j_len;
        }
        else if (function_info->is_rowcol) {
            current_size = sizeof(int) * multiplier * (i_len + j_len);
        }
        else /* if (function_info->is_trace) */ {
            current_size = sizeof(int8_t) * multiplier * i_len * j_len;
        }
        memory_estimate = current_size;
        for (size_t index=1; index<vpairs.size(); ++index) {
            i = vpairs[index].first;
            j = vpairs[index].second;
            i_beg = BEG[i];
            i_end = END[i];
            i_len = i_end-i_beg;
            j_beg = BEG[j];
            j_end = END[j];
            j_len = j_end-j_beg;
            unsigned long local_size = 0;
            if (function_info->is_table) {
                local_size = sizeof(int) * multiplier * i_len * j_len;
            }
            else if (function_info->is_rowcol) {
                local_size = sizeof(int) * multiplier * (i_len + j_len);
            }
            else /* if (function_info->is_trace) */ {
                local_size = sizeof(int8_t) * multiplier * i_len * j_len;
            }
            if ((current_size + local_size) > memory_budget) {
                batches.push_back(index);
                if (current_size > memory_estimate) {
                    memory_estimate = current_size;
                }
                if (verbose_memory) {
                    long long start = batches[batches.size()-2];
                    long long stop = batches[batches.size()-1];
                    eprintf(stdout, "%20s: %lld-%lld\t%.4f GB\n", "batch",
                            start, stop, current_size*GB);
                }
                current_size = local_size;
            }
            else {
                current_size += local_size;
            }
        }
        if (current_size > memory_estimate) {
            memory_estimate = current_size;
        }
        batches.push_back(vpairs.size());
        if (verbose_memory) {
            long long start = batches[batches.size()-2];
            long long stop = batches[batches.size()-1];
            eprintf(stdout, "%20s: %lld-%lld\t%.4f GB\n", "batch",
                    start, stop, current_size*GB);
        }
    }
    else {
        if (function_info->is_stats) {
            result_size += sizeof(parasail_result_extra_stats_t);
        }
        /* the score, and perhaps stats */
        size_t results_per_batch = memory_budget / result_size;
        size_t how_many_batches = vpairs.size() / results_per_batch;
        for (size_t i=0; i<how_many_batches; ++i) {
            batches.push_back((i+1)*results_per_batch);
        }
        batches.push_back(vpairs.size());
        if (results_per_batch > vpairs.size()) {
            memory_estimate = result_size*vpairs.size();
        }
        else {
            memory_estimate = result_size*results_per_batch;
        }
        if (verbose_memory) {
            for (size_t batch=0; batch<batches.size()-1; ++batch) {
                long long start = batches[batch];
                long long stop = batches[batch+1];
                eprintf(stdout, "%20s: %lld-%lld\t%.4f GB\n", "batch",
                        start, stop, memory_estimate*GB);
            }
        }
    }

    return batches;
}


int main(int argc, char **argv) {
    FILE *fop = NULL;
    const char *fname = NULL;
    const char *qname = NULL;
    const char *oname = "parasail.csv";
    bool oname_from_user = false;
    parasail_sequences_t *sequences = NULL;
    parasail_sequences_t *queries = NULL;
    unsigned char *T = NULL;
    unsigned char *Q = NULL;
    int num_threads = -1;
    int *SA = NULL;
    int *LCP = NULL;
    unsigned char *BWT = NULL;
    int *SID = NULL;
    vector<long> BEG;
    vector<long> END;
    vector<int> DB;
    long n = 0;
    long t = 0;
    long q = 0;
    double start = 0;
    double finish = 0;
    long i = 0;
    long sid = 0;
    long sid_crossover = -1;
    char sentinal = 0;
    int cutoff = 7;
    bool use_filter = true;
    char *output_format = NULL;
    bool use_emboss_format = false;
    bool use_sam_format = false;
    bool use_sam_header = false;
    bool use_ssw_format = false;
    PairSet pairs;
    PairVec vpairs;
    unsigned long count_possible = 0;
    unsigned long count_generated = 0;
    unsigned long work = 0;
    int c = 0;
    const char *funcname = "sw_stats_striped_16";
    const parasail_function_info_t *function_info = NULL;
    const parasail_pfunction_info_t *pfunction_info = NULL;
    parasail_function_t *function = NULL;
    parasail_pfunction_t *pfunction = NULL;
    parasail_pcreator_t *pcreator = NULL;
    int is_banded = 0;
    int is_trace = 0;
    int kbandsize = 3;
    const char *matrixname = NULL;
    const parasail_matrix_t *matrix = NULL;
    bool matrix_needs_free = false;
    int gap_open = 10;
    int gap_extend = 1;
    int match = 1;
    int mismatch = 0;
    bool case_sensitive = false;
    const char *alphabet_aliases = NULL;
    bool use_dna = false;
    bool pairs_only = false;
    bool edge_output = false;
    bool graph_output = false;
    bool is_stats = true;
    bool is_table = false;
    bool has_query = false;
    const char *progname = "parasail_aligner";
    int AOL = 80;
    int SIM = 40;
    int OS = 30;
    bool verbose = false;
    bool verbose_memory = false;
    long long batch_size = 0;
    bool has_stdin = false;
    size_t memsize = 0; /* for temporary use */
    size_t memory_budget = 0;
    size_t bytes_used = 0;
    size_t profile_bits = 0;

    set_signal_handler();

    /* establish default memory budget */
    memory_budget = getMemorySize();
    if (0 == memory_budget) {
        /* error occurred, guess reasonable default */
        memory_budget = 2.0/GB;
    }
    else {
        /* use half available memory */
        memory_budget = memory_budget / 2;
    }

    /* Check arguments. */
    while ((c = getopt(argc, argv, "a:A:b:c:Cde:Ef:g:Ghi:k:l:m:M:o:O:pq:r:s:t:vVxX:")) != -1) {
        switch (c) {
            case 'a':
                funcname = optarg;
                break;
            case 'A':
                alphabet_aliases = optarg;
                break;
            case 'b':
                {
                    string numStr = optarg;
                    istringstream iss(numStr);
                    iss>>batch_size;
                    if (batch_size < 0) {
                    eprintf(stderr, "batch size must be >= 0\n");
                        print_help(progname, EXIT_FAILURE);
                    }
                }
                break;
            case 'c':
                cutoff = atoi(optarg);
                if (cutoff <= 0) {
                    eprintf(stderr, "cutoff must be > 0\n");
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'C':
                case_sensitive = true;
                break;
            case 'd':
                use_dna = true;
                break;
            case 'e':
                gap_extend = atoi(optarg);
                if (gap_extend < 0) {
                    eprintf(stderr, "gap extend must be >= 0\n");
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'E':
                edge_output = true;
                break;
            case 'f':
                fname = optarg;
                break;
            case 'g':
                oname = optarg;
                oname_from_user = true;
                break;
            case 'G':
                graph_output = true;
                break;
            case 'h':
                print_help(progname, EXIT_FAILURE);
                break;
            case 'i':
                OS = atoi(optarg);
                if (OS < 0 || OS > 100) {
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'k':
                kbandsize = atoi(optarg);
                if (kbandsize <= 0) {
                    eprintf(stderr, "band size must be > 0\n");
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'l':
                AOL = atoi(optarg);
                if (AOL < 0 || AOL > 100) {
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'm':
                matrixname = optarg;
                break;
            case 'M':
                match = atoi(optarg);
                if (match < 0) {
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'o':
                gap_open = atoi(optarg);
                if (gap_open < 0) {
                    eprintf(stderr, "gap open must be >= 0\n");
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 'O':
                output_format = optarg;
                break;
            case 'p':
                pairs_only = true;
                break;
            case 'q':
                qname = optarg;
                break;
            case 'r':
                memory_budget = parse_bytes(optarg);
                break;
            case 's':
                SIM = atoi(optarg);
                if (SIM < 0 || SIM > 100) {
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case 't':
                num_threads = atoi(optarg);
#ifdef _OPENMP
#else
                eprintf(stdout, "-t number of threads requested, but OpenMP was not found during configuration. Running without threads.");
#endif
                break;
            case 'v':
                verbose = true;
                break;
            case 'V':
                verbose_memory = true;
                break;
            case 'x':
                use_filter = false;
                break;
            case 'X':
                mismatch = atoi(optarg);
                if (mismatch < 0) {
                    print_help(progname, EXIT_FAILURE);
                }
                break;
            case '?':
                if (optopt == 'a'
                        || optopt == 'A'
                        || optopt == 'c'
                        || optopt == 'e'
                        || optopt == 'f'
                        || optopt == 'g'
                        || optopt == 'i'
                        || optopt == 'k'
                        || optopt == 'l'
                        || optopt == 'm'
                        || optopt == 'M'
                        || optopt == 'o'
                        || optopt == 'q'
                        || optopt == 's'
                        || optopt == 't'
                        || optopt == 'X'
                        ) {
                    eprintf(stderr,
                            "Option -%c requires an argument.\n",
                            optopt);
                }
                else if (isprint(optopt)) {
                    eprintf(stderr, "Unknown option `-%c'.\n",
                            optopt);
                }
                else {
                    eprintf(stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
                }
                print_help(progname, EXIT_FAILURE);
                break;
            default:
                eprintf(stderr, "default case in getopt\n");
                exit(EXIT_FAILURE);
        }
    }

    /* select the function */
    if (funcname) {
        if (NULL != strstr(funcname, "profile")) {
            pfunction_info = parasail_lookup_pfunction_info(funcname);
            if (NULL == pfunction_info) {
                eprintf(stderr, "Specified profile function not found.\n");
                exit(EXIT_FAILURE);
            }
            pfunction = pfunction_info->pointer;
            pcreator = pfunction_info->creator;
            if (NULL == strstr(funcname, "sat")) {
                profile_bits = atoi(pfunction_info->width);
            }
            else {
                profile_bits = 56; /* 8+16+32 */
            }
        }
        else {
            function_info = parasail_lookup_function_info(funcname);
            if (NULL == function_info && NULL != strstr(funcname, "nw_banded")) {
                is_banded = 1;
            }
            if (NULL == function_info && 0 == is_banded) {
                eprintf(stderr, "Specified function not found.\n");
                exit(EXIT_FAILURE);
            }
            function = function_info->pointer;
        }
    }
    else {
        eprintf(stderr, "No alignment function specified.\n");
        exit(EXIT_FAILURE);
    }

    has_stdin = stdin_has_data();
    if (has_stdin) {
        if (fname == NULL) {
            fname = "stdin";
        }
        else if (qname == NULL) {
            qname = "stdin";
            has_query = true;
        }
        else {
            eprintf(stderr, "input file, query file, and stdin detected; max inputs is 2\n");
            exit(EXIT_FAILURE);
        }
    }
    else {
        if (fname == NULL) {
            eprintf(stderr, "missing input file\n");
            print_help(progname, EXIT_FAILURE);
        }
        else if (qname != NULL) {
            has_query = true;
        }
    }

    is_trace = (NULL != strstr(funcname, "trace"));
    is_stats = (NULL != strstr(funcname, "stats"));
    is_table = (NULL != strstr(funcname, "table"));

    if (edge_output && graph_output) {
        eprintf(stderr, "Can only request one of edge or graph output.\n");
        exit(EXIT_FAILURE);
    }
    if ((edge_output || graph_output) && !is_stats) {
        eprintf(stderr, "Edge or graph output requested, but alignment function does not return statistics.\n");
        exit(EXIT_FAILURE);
    }
    if (graph_output && has_query) {
        eprintf(stderr, "Cannot specify a query file and output as a graph.\n");
        exit(EXIT_FAILURE);
    }

    if (NULL != output_format) {
        bool trace_warning = false;
        if (NULL != strstr(output_format, "SAMH")) {
            use_sam_format = true;
            use_sam_header = true;
            trace_warning = true;
        }
        else if (NULL != strstr(output_format, "SAM")) {
            use_sam_format = true;
            trace_warning = true;
        }
        else if (NULL != strstr(output_format, "EMBOSS")) {
            use_emboss_format = true;
            trace_warning = true;
        }
        else if (NULL != strstr(output_format, "SSW")) {
            use_ssw_format = true;
            trace_warning = true;
        }
        else {
            eprintf(stderr, "Unknown output format '%s'.\n", output_format);
            exit(EXIT_FAILURE);
        }
        if (!is_trace && trace_warning) {
            eprintf(stderr, "The selected output format '%s' requires an alignment function that returns a traceback.\n", output_format);
            exit(EXIT_FAILURE);
        }
    }
    else if (is_trace) {
        eprintf(stderr, "Please select trace output format.\n");
        exit(EXIT_FAILURE);
    }

    /* select the substitution matrix */
    if (NULL == matrixname && use_dna) {
        matrixname = "ACGT";
        if (case_sensitive) {
            matrix = parasail_matrix_create_case_sensitive("ACGT", match, -mismatch);
        }
        else {
            matrix = parasail_matrix_create("ACGT", match, -mismatch);
        }
        if (NULL == matrix) {
            eprintf(stderr, "Could not create DNA match/mismatch matrix.\n");
            exit(EXIT_FAILURE);
        }
        matrix_needs_free = true;
    }
    else {
        if (NULL == matrixname) {
            matrixname = "blosum62";
        }
        matrix = parasail_matrix_lookup(matrixname);
        if (NULL == matrix) {
            /* try as a filename */
            if (case_sensitive) {
                matrix = parasail_matrix_from_file_case_sensitive(matrixname);
            }
            else {
                matrix = parasail_matrix_from_file(matrixname);
            }
            if (NULL == matrix) {
                eprintf(stderr, "parasail_matrix_from_file(%s) failed.\n", matrixname);
                exit(EXIT_FAILURE);
            }
            matrix_needs_free = true;
        }
    }

    /* if matrix is PSSM, we disable the filter */
    if (matrix->type == PARASAIL_MATRIX_TYPE_PSSM) {
        use_filter = false;
        if (has_query) {
            eprintf(stderr, "A query file is not permitted with a pssm matrix.\n");
            exit(EXIT_FAILURE);
        }
    }

    if (is_trace && !oname_from_user) {
        oname = "stdout";
    }

    /* print the parameters for reference */
    if (verbose) {
        int major, minor, patch;
        parasail_version(&major, &minor, &patch);
        eprintf(stdout,
                "%20s: %d.%d.%d\n"
                "%20s: %s\n"
                "%20s: %d\n"
                "%20s: %s\n"
                "%20s: %s\n"
                "%20s: %s\n"
                "%20s: %d\n"
                "%20s: %d\n"
                "%20s: %s\n"
                "%20s: %d\n"
                "%20s: %d\n"
                "%20s: %d\n"
                "%20s: %s\n"
                "%20s: %s\n"
                "%20s: %s\n"
                "%20s: %lld\n"
                "%20s: %.4f GB\n",
                "parasail version", major, minor, patch,
                "funcname", funcname,
                "cutoff", cutoff,
                "case sensitive", case_sensitive ? "yes" : "no",
                "alphabet aliases", alphabet_aliases ? alphabet_aliases : "<no aliases>",
                "use filter", use_filter ? "yes" : "no",
                "gap_extend", gap_extend,
                "gap_open", gap_open,
                "matrix", matrixname,
                "AOL", AOL,
                "SIM", SIM,
                "OS", OS,
                "file", fname,
                "query", (NULL == qname) ? "<no query>" : qname,
                "output", oname,
                "batch_size", batch_size,
                "memory_budget", memory_budget*GB
                    );
        if (use_dna) {
            eprintf(stdout,
                    "%20s: %d\n"
                    "%20s: %d\n",
                    "match", match,
                    "mismatch", mismatch);
        }
    }
    if (verbose_memory) {
        eprintf(stdout, "%20s: %.4f GB\n", "memory budget", memory_budget*GB);
    }

    /* Best to know early whether we can open the output file. */
    if (oname_from_user || !is_trace) {
        if ((fop = fopen(oname, "w")) == NULL) {
            eprintf(stderr, "%s: Cannot open output file `%s': ",
                    progname, oname);
            perror("fopen");
            exit(EXIT_FAILURE);
        }
    }
    else {
        fop = stdout;
    }

    start = parasail_time();
    if (!has_query) {
        size_t count = 0;
        sequences = parasail_sequences_from_file(fname);
        if (NULL == sequences) {
            eprintf(stderr, "parasail_sequences_from_file(%s) failed.\n", fname);
            exit(EXIT_FAILURE);
        }
        T = (unsigned char*)parasail_sequences_pack(sequences, &count);
        if (NULL == T) {
            eprintf(stderr, "parasail_sequences_pack failed.\n");
            exit(EXIT_FAILURE);
        }
        n = count;
        if (is_trace) {
            /* This does not include name, comment, or qual strings. */
            bytes_used += sizeof(parasail_sequence_t) * sequences->l;
            bytes_used += sequences->characters;
        }
        else {
            /* If we aren't using tracebacks, we can discard sequences now. */
            parasail_sequences_free(sequences);
        }
        bytes_used += count;
    }
    else {
        size_t count = 0;
        sequences = parasail_sequences_from_file(fname);
        if (NULL == sequences) {
            eprintf(stderr, "parasail_sequences_from_file(%s) failed.\n", fname);
            exit(EXIT_FAILURE);
        }
        T = (unsigned char*)parasail_sequences_pack(sequences, &count);
        if (NULL == T) {
            eprintf(stderr, "parasail_sequences_pack failed.\n");
            exit(EXIT_FAILURE);
        }
        t = count;
        if (is_trace) {
            /* This does not include name, comment, or qual strings. */
            bytes_used += sizeof(parasail_sequence_t) * sequences->l;
            bytes_used += sequences->characters;
        }
        else {
            /* If we aren't using tracebacks, we can discard sequences now. */
            parasail_sequences_free(sequences);
        }
        bytes_used += count;
        queries = parasail_sequences_from_file(qname);
        if (NULL == queries) {
            eprintf(stderr, "parasail_sequences_from_file(%s) failed.\n", qname);
            exit(EXIT_FAILURE);
        }
        Q = (unsigned char*)parasail_sequences_pack(queries, &count);
        if (NULL == Q) {
            eprintf(stderr, "parasail_sequences_pack queries failed.\n");
            exit(EXIT_FAILURE);
        }
        q = count;
        if (is_trace) {
            /* This does not include name, comment, or qual strings. */
            bytes_used += sizeof(parasail_sequence_t) * queries->l;
            bytes_used += queries->characters;
        }
        else {
            /* If we aren't using tracebacks, we can discard queries now. */
            parasail_sequences_free(queries);
        }
        bytes_used += count;
        n = t+q;
        /* realloc T and copy Q into it */
        T = (unsigned char*)realloc(T, (n+1)*sizeof(unsigned char));
        if (T == NULL) {
            eprintf(stderr, "%s: Cannot reallocate memory.\n", progname);
            perror("realloc");
            exit(EXIT_FAILURE);
        }
        (void)memcpy(T+t, Q, q);
        free(Q);
    }
    T[n] = '\0';
    finish = parasail_time();
    if (verbose) {
        eprintf(stdout, "%20s: %.4f seconds\n", "read and pack time", finish-start);
    }
    if (verbose_memory) {
        eprintf(stdout, "%20s: %.4f GB\n", "read and pack memory", bytes_used*GB);
    }

    /* Allocate memory for sequence ID array. */
    if (use_filter) {
        memsize = (size_t)n * sizeof(int);
        SID = (int *)malloc(memsize);
        if(SID == NULL) {
            perror("malloc");
            eprintf(stderr, "%s: Cannot allocate suffix ID memory.\n", progname);
            eprintf(stderr, "Attempted %llu bytes. %llu already used.\n",
                    (unsigned long long)memsize,
                    (unsigned long long)bytes_used);
            exit(EXIT_FAILURE);
        }
        bytes_used += memsize;
    }

    /* determine sentinal */
    if (sentinal == 0) {
        long off = 0;
        while (!isgraph(T[n-off])) {
            ++off;
        }
        sentinal = T[n-off];
    }
    if (verbose) {
        eprintf(stdout, "%20s: %c\n", "sentinal", sentinal);
    }

    /* determine actual end of file (last char) */
    {
        long off = 0;
        while (!isgraph(T[n-off])) {
            ++off;
        }
        n = n - off + 1;
    }
    if (verbose) {
        eprintf(stdout, "%20s: %ld\n", "end of packed buffer", n);
    }

    /* scan T from left to count number of sequences */
    sid = 0;
    for (i=0; i<n; ++i) {
        if (T[i] == sentinal) {
            ++sid;
        }
    }
    if (0 == sid) { /* no sentinal found */
        eprintf(stderr, "no sentinal(%c) found in input\n", sentinal);
        exit(EXIT_FAILURE);
    }
    if (verbose) {
        eprintf(stdout, "%20s: %ld\n", "number of sequences", sid);
    }

    /* scan T from left to build sequence ID and end index */
    /* allocate vectors now that number of sequences is known */
    try {
        memsize = sizeof(long)*(sid+1);
        BEG.reserve(sid+1);
        bytes_used += memsize;
        END.reserve(sid+1);
        bytes_used += memsize;
        if (use_filter) {
            memsize = sizeof(int)*(sid+1);
            DB.reserve(sid+1);
            bytes_used += memsize;
        }
    } catch (const bad_alloc&) {
        eprintf(stderr, "Cannot allocate memory for vectors.\n");
        eprintf(stderr, "Attempted %llu bytes. %llu already used.\n",
                (unsigned long long)memsize,
                (unsigned long long)bytes_used);
        exit(EXIT_FAILURE);
    }
    sid = 0;
    BEG.push_back(0);
    if (use_filter) {
        /* look for mixed case also */
        char found_lower = 0;
        char found_upper = 0;
        for (i=0; i<n; ++i) {
            SID[i] = sid;
            if (T[i] == sentinal) {
                END.push_back(i);
                BEG.push_back(i+1);
                DB.push_back(i<t);
                if (-1 == sid_crossover && i>=t) {
                    sid_crossover = sid;
                }
                ++sid;
            }
            found_lower = found_lower || (T[i] >= 'a' && T[i] <= 'z');
            found_upper = found_upper || (T[i] >= 'A' && T[i] <= 'Z');
        }
        if (found_lower && found_upper) {
            /* make the whole thing upper case */
            if (!case_sensitive) {
                for (i=0; i<n; ++i) {
                    T[i] = toupper(T[i]);
                }
            }
        }
    }
    else {
        for (i=0; i<n; ++i) {
            if (T[i] == sentinal) {
                END.push_back(i);
                BEG.push_back(i+1);
                if (-1 == sid_crossover && i>=t) {
                    sid_crossover = sid;
                }
                ++sid;
            }
        }
    }

    /* if we don't have a query file, clear the DB flags */
    if (!has_query) {
        /* vector::clear() might not deallocate memory.
         * Use swap with temporary instead */
        vector<int>().swap(DB);
        sid_crossover = -1;
    }
    else if (verbose) {
        eprintf(stdout, "%20s: %ld\n", "number of queries", sid-sid_crossover);
        eprintf(stdout, "%20s: %ld\n", "number of db seqs", sid_crossover);
    }

    /* use the enhanced SA filter */
    if (use_filter) {
        size_t memsize_local = 0;
        /* Allocate memory for enhanced SA. */
        memsize = (size_t)(n+1) * sizeof(int); /* +1 for LCP */
        SA = (int *)malloc(memsize);
        if (SA == NULL) {
            perror("malloc");
            eprintf(stderr, "%s: Cannot allocate SA memory.\n", progname);
            eprintf(stderr, "Attempted %llu bytes. %llu already used.\n",
                    (unsigned long long)memsize,
                    (unsigned long long)bytes_used);
            exit(EXIT_FAILURE);
        }
        memsize_local += memsize;
        bytes_used += memsize;
        memsize = (size_t)(n+1) * sizeof(int); /* +1 for lcp tree */
        LCP = (int *)malloc(memsize);
        if (LCP == NULL) {
            perror("malloc");
            eprintf(stderr, "%s: Cannot allocate LCP memory.\n", progname);
            eprintf(stderr, "Attempted %llu bytes. %llu already used.\n",
                    (unsigned long long)memsize,
                    (unsigned long long)bytes_used);
            exit(EXIT_FAILURE);
        }
        memsize_local += memsize;
        bytes_used += memsize;
        memsize = (size_t)(n+1) * sizeof(unsigned char);
        BWT = (unsigned char *)malloc(memsize);
        if (BWT == NULL) {
            perror("malloc");
            eprintf(stderr, "%s: Cannot allocate BWT memory.\n", progname);
            eprintf(stderr, "Attempted %llu bytes. %llu already used.\n",
                    (unsigned long long)memsize,
                    (unsigned long long)bytes_used);
            exit(EXIT_FAILURE);
        }
        memsize_local += memsize;
        bytes_used += memsize;

        /* Construct the suffix and LCP arrays.
         * The following sais routine is from Fischer, with bugs fixed. */
        start = parasail_time();
        if(sais(T, SA, LCP, (int)n) != 0) {
            eprintf(stderr, "%s: Cannot allocate memory.\n", progname);
            exit(EXIT_FAILURE);
        }
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout,"%20s: %.4f seconds\n", "induced SA time", finish-start);
        }

        /* construct naive BWT: */
        start = parasail_time();
        for (i = 0; i < n; ++i) {
            BWT[i] = (SA[i] > 0) ? T[SA[i]-1] : sentinal;
        }
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "naive BWT time", finish-start);
        }

        /* "fix" the LCP array to clamp LCP's that are too long */
        start = parasail_time();
        for (i = 0; i < n; ++i) {
            int len = END[SID[SA[i]]] - SA[i]; /* don't include sentinal */
            if (LCP[i] > len) LCP[i] = len;
        }
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "clamp LCP time", finish-start);
        }

        /* The GSA we create will put all sentinels either at the beginning
         * or end of the SA. We don't want to count all of the terminals,
         * nor do we want to process them in our bottom-up traversal. */
        /* do the sentinels appear at the beginning or end of SA? */
        int bup_start = 1;
        int bup_stop = n;
        if (T[SA[0]] == sentinal) {
            /* sentinels at beginning */
            bup_start = sid+1;
            bup_stop = n;
        }
        else if (T[SA[n-1]] == sentinal) {
            /* sentinels at end */
            bup_start = 1;
            bup_stop = n-sid;
        }
        else {
            eprintf(stderr, "sentinels not found at beginning or end of SA\n");
            exit(EXIT_FAILURE);
        }

        /* DFS of enhanced SA, from Abouelhoda et al */
        start = parasail_time();
        count_generated = 0;
        LCP[n] = 0; /* doesn't really exist, but for the root */
        {
            stack<quad> the_stack;
            quad last_interval;
            the_stack.push(quad());
            for (i = bup_start; i <= bup_stop; ++i) {
                int lb = i - 1;
                while (LCP[i] < the_stack.top().lcp) {
                    the_stack.top().rb = i - 1;
                    last_interval = the_stack.top();
                    the_stack.pop();
                    process(count_generated, pairs, last_interval, SA, BWT, SID, DB, sentinal, cutoff);
                    lb = last_interval.lb;
                    if (LCP[i] <= the_stack.top().lcp) {
                        last_interval.children.clear();
                        the_stack.top().children.push_back(last_interval);
                        last_interval = quad();
                    }
                }
                if (LCP[i] > the_stack.top().lcp) {
                    if (!last_interval.empty()) {
                        last_interval.children.clear();
                        the_stack.push(quad(LCP[i],lb,INT_MAX,vector<quad>(1, last_interval)));
                        last_interval = quad();
                    }
                    else {
                        the_stack.push(quad(LCP[i],lb,INT_MAX));
                    }
                }
            }
            the_stack.top().rb = bup_stop - 1;
            process(count_generated, pairs, the_stack.top(), SA, BWT, SID, DB, sentinal, cutoff);
        }
        finish = parasail_time();
        if (!has_query) {
            count_possible = ((unsigned long)sid)*((unsigned long)sid-1)/2;
        } else {
            count_possible = (sid-sid_crossover)*sid_crossover;
        }
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "ESA time", finish-start);
            eprintf(stdout, "%20s: %lu\n", "possible pairs", count_possible);
            eprintf(stdout, "%20s: %lu\n", "generated pairs", count_generated);
        }

        /* Deallocate memory. */
        free(SID);
        free(SA);
        free(LCP);
        free(BWT);
        bytes_used -= (size_t)n * sizeof(int); /* SID */
        bytes_used -= memsize_local; /* SA,LCP,BWT */

        if (verbose) {
            eprintf(stdout, "%20s: %zu\n", "unique pairs", pairs.size());
        }
    }
    else {
        /* don't use enhanced SA filter -- generate all pairs */
        start = parasail_time();
        if (matrix->type == PARASAIL_MATRIX_TYPE_PSSM) {
            /* pssm matrix is itself a single query, so 1 to all comparison */
            for (int i=0; i<sid; ++i) {
                vpairs.push_back(make_pair(i,i));
            }
        }
        else if (!has_query) {
            /* no query file, so all against all comparison */
            for (int i=0; i<sid; ++i) {
                for (int j=i+1; j<sid; ++j) {
                    vpairs.push_back(make_pair(i,j));
                }
            }
        }
        else {
            /* query given, so only compare query against database */
            for (int i=sid_crossover; i<sid; ++i) {
                for (int j=0; j<sid_crossover; ++j) {
                    vpairs.push_back(make_pair(i,j));
                }
            }
        }
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "enumerate time", finish-start);
            eprintf(stdout, "%20s: %zu\n", "unique pairs", vpairs.size());
        }
    }

    if (!is_trace && pairs_only) {
        /* Done with input text. */
        free(T);
        if (vpairs.empty() && !pairs.empty()) {
            for (PairSet::iterator it=pairs.begin(); it!=pairs.end(); ++it) {
                int i = it->first;
                int j = it->second;
                eprintf(fop, "%d,%d\n", i, j);
            }
        }
        else if (!vpairs.empty() && pairs.empty()) {
            for (PairVec::iterator it=vpairs.begin(); it!=vpairs.end(); ++it) {
                int i = it->first;
                int j = it->second;
                eprintf(fop, "%d,%d\n", i, j);
            }
        }
        else {
            eprintf(stderr, "pairs and vpairs were empty\n");
            exit(EXIT_FAILURE);
        }
        fclose(fop);
        return 0;
    }

#ifdef _OPENMP
    if (-1 == num_threads) {
        num_threads = omp_get_max_threads();
    }
    else if (num_threads >= 1) {
        omp_set_num_threads(num_threads);
    }
    else {
        eprintf(stderr, "invalid number of threads chosen (%d)\n", num_threads);
        exit(EXIT_FAILURE);
    }
    if (verbose) {
        eprintf(stdout, "%20s: %d\n", "omp num threads", num_threads);
    }
#endif

    /* OpenMP can't iterate over an STL set. Convert to STL vector. */
    start = parasail_time();
    if (vpairs.empty()) {
        if (pairs.empty()) {
            if (use_filter) {
                eprintf(stderr, "no alignment work, either the filter removed all alignments or the input file(s) were empty\n");
                exit(EXIT_FAILURE);
            }
            else {
                eprintf(stderr, "no alignment work, perhaps the input file(s) were empty\n");
                exit(EXIT_FAILURE);
            }
        }
        vpairs.assign(pairs.begin(), pairs.end());
        /* set::clear() might not deallocate memory.
         * Use swap with temporary instead */
        PairSet().swap(pairs);
        /* don't increment bytes_used since memory use should remain same */
    }
    if (!pairs.empty()) {
        eprintf(stderr, "failed to free pair memory, continuing\n");
    }
    /* finally tally the pair memory */
    bytes_used += vpairs.size()*sizeof(Pair);
    /* pre-allocate result pointers */
    vector<parasail_result_t*> results(vpairs.size(),
            static_cast<parasail_result_t*>(NULL));
    bytes_used += vpairs.size()*sizeof(parasail_result_t*);
    finish = parasail_time();
    if (verbose) {
        eprintf(stdout, "%20s: %.4f seconds\n", "openmp prep time", finish-start);
    }
    if (verbose_memory) {
        eprintf(stdout, "%20s: %.4f GB\n", "openmp prep memory", bytes_used*GB);
    }

    /* create profiles, if necessary */
    vector<parasail_profile_t*> profiles;
    if (pfunction) {
        start = parasail_time();
        set<int> profile_indices_set;
        for (size_t index=0; index<vpairs.size(); ++index) {
            profile_indices_set.insert(vpairs[index].first);
        }
        vector<int> profile_indices(
                profile_indices_set.begin(),
                profile_indices_set.end());
        profiles.assign(sid, static_cast<parasail_profile_t*>(NULL));
        bytes_used += sizeof(parasail_profile_t*) * sid;
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "profile init", finish-start);
        }
        start = parasail_time();
#pragma omp parallel for schedule(guided)
        for (long long index=0; index<(long long)profile_indices.size(); ++index)
        {
            int i = profile_indices[index];
            long i_beg = BEG[i];
            long i_end = END[i];
            long i_len = i_end-i_beg;
            size_t local_mem = matrix->size * i_len * profile_bits;
            profiles[i] = pcreator((const char*)&T[i_beg], i_len, matrix);
#pragma omp atomic
            bytes_used += local_mem;
        }
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "profile creation", finish-start);
        }
        if (verbose_memory) {
            eprintf(stdout, "%20s: %.4f GB\n", "profile creation memory", bytes_used*GB);
        }
    }

    if (bytes_used > memory_budget) {
        eprintf(stderr, "memory budget exceeded prior to alignment phase\n");
        return 0;
    }

    /* align pairs */
    start = parasail_time();
    if (function) {
        size_t memory_estimate;
        long long vpairs_size = (long long)vpairs.size();
        vector<long long> batches = calc_batches(
                batch_size,
                verbose && verbose_memory,
                memory_budget-bytes_used, function_info,
                vpairs, BEG, END,
                memory_estimate);
        bytes_used += memory_estimate;
        vector<vector<pair<int,float> > > graph;
        unsigned long edge_count = 0;
        if (graph_output) {
            graph.resize(sid);
        }
        for (size_t batch=0; batch<batches.size()-1; ++batch) {
            long long start = batches[batch];
            long long stop = batches[batch+1];
            if (stop > vpairs_size) stop = vpairs_size;
#pragma omp parallel for schedule(guided)
            for (long long index=start; index<stop; ++index)
            {
                int i = vpairs[index].first;
                int j = vpairs[index].second;
                long i_beg = BEG[i];
                long i_end = END[i];
                long i_len = i_end-i_beg;
                long j_beg = BEG[j];
                long j_end = END[j];
                long j_len = j_end-j_beg;
                unsigned long local_work = i_len * j_len;
                parasail_result_t *result = function(
                        (const char*)&T[i_beg], i_len,
                        (const char*)&T[j_beg], j_len,
                        gap_open, gap_extend, matrix);
#pragma omp atomic
                work += local_work;
                results[index] = result;
            }
            if (graph_output) {
                output_graph(NULL, 0, T, AOL, SIM, OS, matrix, BEG,
                        END, vpairs, results, graph, edge_count, start,
                        stop);
            }
            else {
                output(is_stats, is_table, is_trace, edge_output,
                        use_emboss_format, use_ssw_format,
                        use_sam_format, use_sam_header, fop, has_query,
                        sid_crossover, T, AOL, SIM, OS, matrix,
                        BEG, END, vpairs, queries, sequences, results,
                        start, stop, case_sensitive, alphabet_aliases);
            }
            for (long long index=start; index<stop; ++index) {
                parasail_result_t *result = results[index];
                parasail_result_free(result);
            }
        }
        if (graph_output) {
            output_graph(fop, 0, T, AOL, SIM, OS, matrix, BEG,
                    END, vpairs, results, graph, edge_count, 0,
                    vpairs_size);
        }
    }
    else if (is_banded) {
        size_t memory_estimate;
        long long vpairs_size = (long long)vpairs.size();
        vector<long long> batches = calc_batches(
                batch_size,
                verbose && verbose_memory,
                memory_budget-bytes_used, function_info,
                vpairs, BEG, END,
                memory_estimate);
        bytes_used += memory_estimate;
        for (size_t batch=0; batch<batches.size()-1; ++batch) {
            long long start = batches[batch];
            long long stop = batches[batch+1];
            if (stop > vpairs_size) stop = vpairs_size;
#pragma omp parallel for schedule(guided)
            for (long long index=start; index<stop; ++index)
            {
                int i = vpairs[index].first;
                int j = vpairs[index].second;
                long i_beg = BEG[i];
                long i_end = END[i];
                long i_len = i_end-i_beg;
                long j_beg = BEG[j];
                long j_end = END[j];
                long j_len = j_end-j_beg;
                unsigned long local_work = i_len * j_len;
                parasail_result_t *result = parasail_nw_banded(
                        (const char*)&T[i_beg], i_len,
                        (const char*)&T[j_beg], j_len,
                        gap_open, gap_extend, kbandsize, matrix);
#pragma omp atomic
                work += local_work;
                results[index] = result;
            }
            output(is_stats, is_table, is_trace, edge_output,
                    use_emboss_format, use_ssw_format, use_sam_format,
                    use_sam_header, fop, has_query, sid_crossover, T, AOL,
                    SIM, OS, matrix, BEG, END, vpairs, queries, sequences,
                    results, start, stop, case_sensitive, alphabet_aliases);
            for (long long index=start; index<stop; ++index) {
                parasail_result_t *result = results[index];
                parasail_result_free(result);
            }
        }
    }
    else if (pfunction) {
        size_t memory_estimate;
        long long vpairs_size = (long long)vpairs.size();
        vector<long long> batches = calc_batches(
                batch_size,
                verbose && verbose_memory,
                memory_budget-bytes_used, pfunction_info,
                vpairs, BEG, END,
                memory_estimate);
        bytes_used += memory_estimate;
        vector<vector<pair<int,float> > > graph;
        unsigned long edge_count = 0;
        if (graph_output) {
            graph.resize(sid);
        }
        for (size_t batch=0; batch<batches.size()-1; ++batch) {
            long long start = batches[batch];
            long long stop = batches[batch+1];
            if (stop > vpairs_size) stop = vpairs_size;
#pragma omp parallel for schedule(guided)
            for (long long index=start; index<stop; ++index)
            {
                int i = vpairs[index].first;
                int j = vpairs[index].second;
                long j_beg = BEG[j];
                long j_end = END[j];
                long j_len = j_end-j_beg;
                parasail_profile_t *profile = profiles[i];
                if (NULL == profile) {
                    eprintf(stderr, "BAD PROFILE %d\n", i);
                    exit(EXIT_FAILURE);
                }
                unsigned long local_work = profile->s1Len * j_len;
                parasail_result_t *result = pfunction(
                        profile, (const char*)&T[j_beg], j_len,
                        gap_open, gap_extend);
#pragma omp atomic
                work += local_work;
                results[index] = result;
            }
            if (graph_output) {
                output_graph(NULL, 0, T, AOL, SIM, OS, matrix, BEG,
                        END, vpairs, results, graph, edge_count, start,
                        stop);
            }
            else {
                output(is_stats, is_table, is_trace, edge_output,
                        use_emboss_format, use_ssw_format,
                        use_sam_format, use_sam_header, fop, has_query,
                        sid_crossover, T, AOL, SIM, OS, matrix,
                        BEG, END, vpairs, queries, sequences, results,
                        start, stop, case_sensitive, alphabet_aliases);
            }
            for (long long index=start; index<stop; ++index) {
                parasail_result_t *result = results[index];
                parasail_result_free(result);
            }
        }
        if (graph_output) {
            output_graph(fop, 0, T, AOL, SIM, OS, matrix, BEG,
                    END, vpairs, results, graph, edge_count, 0,
                    vpairs_size);
        }
    }
    else {
        /* shouldn't get here */
        eprintf(stderr, "alignment function was not properly set (shouldn't happen)\n");
        exit(EXIT_FAILURE);
    }
    finish = parasail_time();
    if (verbose) {
        eprintf(stdout, "%20s: %lu cells\n", "work", work);
        eprintf(stdout, "%20s: %.4f seconds\n", "alignment time", finish-start);
        eprintf(stdout, "%20s: %.4f \n", "gcups", double(work)/(finish-start)/1000000000);
    }
    if (verbose_memory) {
        eprintf(stdout, "%20s: %.4f GB\n", "post-result memory", bytes_used*GB);
    }

    if (pfunction) {
        start = parasail_time();
#pragma omp parallel for schedule(guided)
        for (long long index=0; index<(long long)profiles.size(); ++index)
        {
            if (NULL != profiles[index]) {
                parasail_profile_free(profiles[index]);
            }
        }
        profiles.clear();
        finish = parasail_time();
        if (verbose) {
            eprintf(stdout, "%20s: %.4f seconds\n", "profile cleanup", finish-start);
        }
    }

    /* close output file */
    if (oname_from_user || !is_trace) {
        fclose(fop);
    }

    /* Done with input text. */
    free(T);

    /* Done with matrix, if it was not a built-in one. */
    if (matrix_needs_free) {
        // cast away const-ness to free */
        parasail_matrix_free(const_cast<parasail_matrix_t*>(matrix));
    }

    /* Done with sequences if we were using tracebacks. */
    if (is_trace) {
        parasail_sequences_free(sequences);
        if (has_query) {
            parasail_sequences_free(queries);
        }
    }

    return 0;
}


inline static void pair_check(
        unsigned long &count_generated,
        PairSet &pairs,
        const int &i,
        const int &j,
        const int * const restrict SA,
        const unsigned char * const restrict BWT,
        const int * const restrict SID,
        const vector<int> &DB,
        const char &sentinal)
{
    const int &sidi = SID[SA[i]];
    const int &sidj = SID[SA[j]];
    if (BWT[i] != BWT[j] || BWT[i] == sentinal) {
        if (DB.empty()) {
            if (sidi != sidj) {
                ++count_generated;
                if (sidi < sidj) {
                    pairs.insert(make_pair(sidi,sidj));
                }
                else {
                    pairs.insert(make_pair(sidj,sidi));
                }
            }
        }
        else {
            if (sidi != sidj && DB[sidi] != DB[sidj]) {
                ++count_generated;
                if (sidi > sidj) {
                    pairs.insert(make_pair(sidi,sidj));
                }
                else {
                    pairs.insert(make_pair(sidj,sidi));
                }
            }
        }
    }
}

/* try to reduce number of duplicate pairs generated */
/* we observe that l-intervals (i.e. internal nodes) always have at
 * least two children, but these children could be singleton
 * l-intervals, e.g., [i..j]=[1..1], in addition to l-intervals with
 * non-singleton ranges/quads. For each l-interval, we take the cross
 * product of its child l-intervals. Naively, we could take the cross
 * product of the entire lb/rb range of the l-interval, but this
 * generates too many duplicate pairs. Instead, the complexity should be
 * bounded by the number of exact matches...
 */
inline static void process(
        unsigned long &count_generated,
        PairSet &pairs,
        const quad &q,
        const int * const restrict SA,
        const unsigned char * const restrict BWT,
        const int * const restrict SID,
        const vector<int> &DB,
        const char &sentinal,
        const int &cutoff)
{
    const size_t n_children = q.children.size();
    size_t child_index = 0;

    if (q.lcp < cutoff) return;

    if (n_children) {
        for (int i=q.lb; i<=q.rb; ++i) {
            int j = i+1;
            if (child_index < n_children) {
                if (i >= q.children[child_index].lb) {
                    j = q.children[child_index].rb+1;
                    if (i >= q.children[child_index].rb) {
                        ++child_index;
                    }
                }
            }
            for (/*nope*/; j<=q.rb; ++j) {
                pair_check(count_generated, pairs, i, j, SA, BWT, SID, DB, sentinal);
            }
        }
    }
    else {
        for (int i=q.lb; i<=q.rb; ++i) {
            for (int j=i+1; j<=q.rb; ++j) {
                pair_check(count_generated, pairs, i, j, SA, BWT, SID, DB, sentinal);
            }
        }
    }
}

inline static void print_array(
        const char * filename_,
        const int * const restrict array,
        const char * const restrict s1, const int s1Len,
        const char * const restrict s2, const int s2Len)
{
    int i;
    int j;
    FILE *f = NULL;
    const char *filename = filename_;

    f = fopen(filename, "w");
    if (NULL == f) {
        eprintf(stderr, "fopen(\"%s\") error: %s\n", filename, strerror(errno));
        exit(-1);
    }
    fprintf(f, " ");
    for (j=0; j<s2Len; ++j) {
        fprintf(f, "%4c", s2[j]);
    }
    fprintf(f, "\n");
    for (i=0; i<s1Len; ++i) {
        fprintf(f, "%c", s1[i]);
        for (j=0; j<s2Len; ++j) {
            fprintf(f, "%4d", array[i*s2Len + j]);
        }
        fprintf(f, "\n");
    }
    fclose(f);
}

inline static int self_score(
        const char * const restrict seq,
        int len,
        const parasail_matrix_t *matrix)
{
    int score = 0;
    for (int i=0; i<len; ++i) {
        unsigned char mapped = matrix->mapper[(unsigned char)seq[i]];
        score += matrix->matrix[matrix->size*mapped+mapped];
    }
    return score;
}

inline static void output_edges(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop)
{
    unsigned long edge_count = 0;
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        long i_beg = BEG[i];
        long i_end = END[i];
        long i_len = i_end-i_beg;
        long j_beg = BEG[j];
        long j_end = END[j];
        long j_len = j_end-j_beg;

        if (has_query) {
            i = i - sid_crossover;
        }

        if (parasail_result_is_saturated(result)) {
            if (has_query) {
                fprintf(stderr, "query %d and ref %d saturated\n", i, j);
            } else {
                fprintf(stderr, "seq %d and seq %d saturated\n", i, j);
            }
            continue;
        }

        int score = parasail_result_get_score(result);
        int matches = parasail_result_get_matches(result);
        int length = parasail_result_get_length(result);
        int self_score_ = 0;
        int max_len = 0;
        int i_self_score = self_score(
                (const char*)&T[i_beg], i_len, matrix);
        int j_self_score = self_score(
                (const char*)&T[j_beg], j_len, matrix);

        if (i_len > j_len) {
            max_len = i_len;
            self_score_ = i_self_score;
        }
        else {
            max_len = j_len;
            self_score_ = j_self_score;
        }

        if ((length * 100 >= AOL * int(max_len))
                && (matches * 100 >= SIM * length)
                && (score * 100 >= OS * self_score_)) {
            ++edge_count;
            fprintf(fop, "%d,%d,%f,%f,%f\n",
                    i, j,
                    1.0*length/max_len,
                    1.0*matches/length,
                    1.0*score/self_score_);
        }
    }

    fprintf(stdout, "%20s: %lu\n", "edges count", edge_count);
}

inline static void output_graph(
        FILE *fop,
        int which,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        vector<vector<pair<int,float> > > &graph,
        unsigned long &edge_count,
        long long start,
        long long stop)
{
    if (NULL == fop) {
        for (long long index=start; index<stop; ++index) {
            parasail_result_t *result = results[index];
            int i = vpairs[index].first;
            int j = vpairs[index].second;
            long i_beg = BEG[i];
            long i_end = END[i];
            long i_len = i_end-i_beg;
            long j_beg = BEG[j];
            long j_end = END[j];
            long j_len = j_end-j_beg;

            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "seq %d and seq %d saturated\n", i, j);
                continue;
            }

            int score = parasail_result_get_score(result);
            int matches = parasail_result_get_matches(result);
            int length = parasail_result_get_length(result);
            int self_score_ = 0;
            int max_len = 0;
            int i_self_score = self_score(
                    (const char*)&T[i_beg], i_len, matrix);
            int j_self_score = self_score(
                    (const char*)&T[j_beg], j_len, matrix);

            if (i_len > j_len) {
                max_len = i_len;
                self_score_ = i_self_score;
            }
            else {
                max_len = j_len;
                self_score_ = j_self_score;
            }

            if ((length * 100 >= AOL * int(max_len))
                    && (matches * 100 >= SIM * length)
                    && (score * 100 >= OS * self_score_)) {
                float value;
                ++edge_count;
                switch (which) {
                    case 0:
                        value = 1.0*length/max_len;
                        break;
                    case 1:
                        value = 1.0*matches/length;
                        break;
                    case 2:
                        value = 1.0*score/self_score_;
                        break;
                }
                graph[i].push_back(make_pair(j,value));
                graph[j].push_back(make_pair(i,value));
            }
        }
    }
    else {
        fprintf(fop, "%lu %lu 1\n", (unsigned long)graph.size(), edge_count);
        for (size_t i=0; i<graph.size(); ++i) {
            if (graph[i].size() > 0) {
                fprintf(fop, "%d %f", graph[i][0].first+1, graph[i][0].second);
                for (size_t j=1; j<graph[i].size(); ++j) {
                    fprintf(fop, "  %d %f", graph[i][j].first+1, graph[i][j].second);
                }
                fprintf(fop, "\n");
            } else {
                fprintf(fop, "\n");
            }
        }
        fprintf(stdout, "%20s: %lu\n", "edges count", edge_count);
    }
}

inline static void output_stats(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop)
{
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        long i_beg = BEG[i];
        long i_end = END[i];
        long i_len = i_end-i_beg;
        long j_beg = BEG[j];
        long j_end = END[j];
        long j_len = j_end-j_beg;

        if (has_query) {
            i = i - sid_crossover;
        }

        if (parasail_result_is_saturated(result)) {
            if (has_query) {
                fprintf(stderr, "query %d and ref %d saturated\n", i, j);
            } else {
                fprintf(stderr, "seq %d and seq %d saturated\n", i, j);
            }
            continue;
        }

        eprintf(fop, "%d,%d,%ld,%ld,%d,%d,%d,%d,%d,%d\n",
                i,
                j,
                i_len,
                j_len,
                parasail_result_get_score(result),
                parasail_result_get_end_query(result),
                parasail_result_get_end_ref(result),
                parasail_result_get_matches(result),
                parasail_result_get_similar(result),
                parasail_result_get_length(result));
    }
}

inline static void output_basic(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop)
{
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        long i_beg = BEG[i];
        long i_end = END[i];
        long i_len = i_end-i_beg;
        long j_beg = BEG[j];
        long j_end = END[j];
        long j_len = j_end-j_beg;

        if (has_query) {
            i = i - sid_crossover;
        }

        if (parasail_result_is_saturated(result)) {
            if (has_query) {
                fprintf(stderr, "query %d and ref %d saturated\n", i, j);
            } else {
                fprintf(stderr, "seq %d and seq %d saturated\n", i, j);
            }
            continue;
        }

        eprintf(fop, "%d,%d,%ld,%ld,%d,%d,%d\n",
                i,
                j,
                i_len,
                j_len,
                parasail_result_get_score(result),
                parasail_result_get_end_query(result),
                parasail_result_get_end_ref(result));
    }
}

inline static void output_emboss(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases)
{
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;

        if (has_query) {
            i = i - sid_crossover;
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "query %d (%s) and ref %d (%s) saturated\n",
                        i, queries->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            parasail_traceback_generic_extra2(
                    queries->seqs[i].seq.s,
                    queries->seqs[i].seq.l,
                    sequences->seqs[j].seq.s,
                    sequences->seqs[j].seq.l,
                    queries->seqs[i].name.s,
                    sequences->seqs[j].name.s,
                    matrix,
                    result,
                    '|', ':', '.',
                    50,
                    14,
                    1,
                    7,
                    fop,
                    case_sensitive,
                    alphabet_aliases);
        }
        else {
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "seq %d (%s) and seq %d (%s) saturated\n",
                        i, sequences->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            parasail_traceback_generic_extra2(
                    matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? sequences->seqs[i].seq.s : matrix->query,
                    matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? sequences->seqs[i].seq.l : matrix->length,
                    sequences->seqs[j].seq.s,
                    sequences->seqs[j].seq.l,
                    matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? sequences->seqs[i].name.s : matrix->name,
                    sequences->seqs[j].name.s,
                    matrix,
                    result,
                    '|', ':', '.',
                    50,
                    14,
                    1,
                    7,
                    fop,
                    case_sensitive,
                    alphabet_aliases);
        }
    }
}

inline static void output_ssw(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases)
{
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        parasail_cigar_t *cigar = NULL;

        if (has_query) {
            i = i - sid_crossover;
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "query %d (%s) and ref %d (%s) saturated\n",
                        i, queries->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            fprintf(fop, "target_name: %s\n", sequences->seqs[j].name.s);
            fprintf(fop, "query_name: %s\n", queries->seqs[i].name.s);
            cigar = parasail_result_get_cigar_extra(result,
                    queries->seqs[i].seq.s,
                    queries->seqs[i].seq.l,
                    sequences->seqs[j].seq.s,
                    sequences->seqs[j].seq.l,
                    matrix,
                    case_sensitive,
                    alphabet_aliases);
        }
        else {
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "seq %d (%s) and seq %d (%s) saturated\n",
                        i, sequences->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            fprintf(fop, "target_name: %s\n", sequences->seqs[j].name.s);
            fprintf(fop, "query_name: %s\n", sequences->seqs[i].name.s);
            cigar = parasail_result_get_cigar_extra(result,
                    sequences->seqs[i].seq.s,
                    sequences->seqs[i].seq.l,
                    sequences->seqs[j].seq.s,
                    sequences->seqs[j].seq.l,
                    matrix,
                    case_sensitive,
                    alphabet_aliases);
        }

        fprintf(fop, "optimal_alignment_score: %d"
                "\tstrand: +"
                "\ttarget_begin: %d"
                "\ttarget_end: %d"
                "\tquery_begin: %d"
                "\tquery_end: %d\n",
                result->score,
                cigar->beg_ref+1,
                parasail_result_get_end_ref(result)+1,
                cigar->beg_query+1,
                parasail_result_get_end_query(result)+1);

        /* we only needed the cigar for beginning locations */
        parasail_cigar_free(cigar);

        if (has_query) {
            parasail_traceback_generic_extra2(
                    queries->seqs[i].seq.s,
                    queries->seqs[i].seq.l,
                    sequences->seqs[j].seq.s,
                    sequences->seqs[j].seq.l,
                    "Query:",
                    "Target:",
                    matrix,
                    result,
                    '|', '*', '*',
                    60,
                    10,
                    0,
                    7,
                    fop,
                    case_sensitive,
                    alphabet_aliases);
        }
        else {
            parasail_traceback_generic_extra2(
                    sequences->seqs[i].seq.s,
                    sequences->seqs[i].seq.l,
                    sequences->seqs[j].seq.s,
                    sequences->seqs[j].seq.l,
                    "Query:",
                    "Target:",
                    matrix,
                    result,
                    '|', '*', '*',
                    60,
                    10,
                    0,
                    7,
                    fop,
                    case_sensitive,
                    alphabet_aliases);
        }
    }
}

inline static void output_sam(
        FILE *fop,
        bool use_sam_header,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases)
{
    if (use_sam_header && has_query && 0 == start) {
        fprintf(fop, "@HD\tVN:1.4\tSO:queryname\n");
        for (size_t index=0; index<sequences->l; ++index) {
            parasail_sequence_t ref_seq = sequences->seqs[index];
            fprintf(fop, "@SQ\tSN:%s\tLN:%d\n",
                    ref_seq.name.s, (int32_t)ref_seq.seq.l);
        }
    }
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        parasail_sequence_t ref_seq;
        parasail_sequence_t read_seq;

        ref_seq = sequences->seqs[j];
        if (has_query) {
            i = i - sid_crossover;
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "query %d (%s) and ref %d (%s) saturated\n",
                        i, queries->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            read_seq = queries->seqs[i];
        }
        else {
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "seq %d (%s) and seq %d (%s) saturated\n",
                        i, sequences->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            read_seq = sequences->seqs[i];
        }

        fprintf(fop, "%s\t", read_seq.name.s);
        if (result->score == 0) {
            fprintf(fop, "4\t*\t0\t255\t*\t*\t0\t0\t*\t*\n");
        }
        else {
            int32_t c = 0;
            int32_t length = 0;
            uint32_t mapq = 255; /* not available */
            parasail_cigar_t *cigar = NULL;
            uint32_t mismatch = 0;

            cigar = parasail_result_get_cigar_extra(
                    result,
                    read_seq.seq.s, read_seq.seq.l,
                    ref_seq.seq.s, ref_seq.seq.l,
                    matrix,
                    case_sensitive,
                    alphabet_aliases);

            fprintf(fop, "0\t");
            fprintf(fop, "%s\t%d\t%d\t",
                    ref_seq.name.s, cigar->beg_ref + 1, mapq);
            if (parasail_result_is_sw(result)) {
                if (cigar->beg_query > 0) {
                    fprintf(fop, "%dS", cigar->beg_query);
                }
            }
            for (c=0; c<cigar->len; ++c) {
                char letter = parasail_cigar_decode_op(cigar->seq[c]);
                uint32_t length = parasail_cigar_decode_len(cigar->seq[c]);
                fprintf(fop, "%lu%c", (unsigned long)length, letter);
                if ('X' == letter || 'I' == letter || 'D' == letter) {
                    mismatch += length;
                }
            }

            length = read_seq.seq.l - result->end_query - 1;
            if (parasail_result_is_sw(result)) {
                if (length > 0) {
                    fprintf(fop, "%dS", length);
                }
            }
            fprintf(fop, "\t*\t0\t0\t");
            fprintf(fop, "%s", read_seq.seq.s);
            fprintf(fop, "\t");
            if (read_seq.qual.s) {
                fprintf (fop, "%s", read_seq.qual.s);
            }
            else {
                fprintf(fop, "*");
            }
            fprintf(fop, "\tAS:i:%d", result->score);
            fprintf(fop,"\tNM:i:%d\t", mismatch);
            fprintf(fop, "\n");

            parasail_cigar_free(cigar);
        }
    }
}

inline static void output_trace(
        FILE *fop,
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases)
{
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        long i_beg = BEG[i];
        long i_end = END[i];
        long i_len = i_end-i_beg;
        long j_beg = BEG[j];
        long j_end = END[j];
        long j_len = j_end-j_beg;
        parasail_cigar_t *cigar = NULL;
        char *cigar_string = NULL;

        if (has_query) {
            i = i - sid_crossover;
        }

        if (parasail_result_is_saturated(result)) {
            if (has_query) {
                fprintf(stderr, "query %d and ref %d saturated\n", i, j);
            } else {
                fprintf(stderr, "seq %d and seq %d saturated\n", i, j);
            }
            continue;
        }

        cigar = parasail_result_get_cigar_extra(
                result,
                (const char*)&T[i_beg], i_len,
                (const char*)&T[j_beg], j_len,
                matrix,
                case_sensitive,
                alphabet_aliases);
        cigar_string = parasail_cigar_decode(cigar);
        eprintf(fop, "%d,%d,%ld,%ld,%d,%d,%d,%s\n",
                i,
                j,
                i_len,
                j_len,
                parasail_result_get_score(result),
                parasail_result_get_end_query(result),
                parasail_result_get_end_ref(result),
                cigar_string);
        parasail_cigar_free(cigar);
        free(cigar_string);
    }
}

inline static void output_tables(
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop)
{
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        long i_beg = BEG[i];
        long i_end = END[i];
        long i_len = i_end-i_beg;
        long j_beg = BEG[j];
        long j_end = END[j];
        long j_len = j_end-j_beg;
        int *table = parasail_result_get_score_table(result);

        if (has_query) {
            i = i - sid_crossover;
        }

        char filename[256] = {'\0'};
        sprintf(filename, "parasail_%d_%d.txt", i, j);
        print_array(filename, table,
                (const char*)&T[i_beg], i_len,
                (const char*)&T[j_beg], j_len);
    }
}

inline static void output(
        bool is_stats,
        bool is_table,
        bool is_trace,
        bool edge_output,
        bool use_emboss_format,
        bool use_ssw_format,
        bool use_sam_format,
        bool use_sam_header,
        FILE *fop,
        bool has_query,
        long sid_crossover,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases)
{
    if (is_stats) {
        if (edge_output) {
            output_edges(fop, has_query, sid_crossover, T, AOL, SIM, OS, matrix, BEG, END, vpairs, results, start, stop);
        }
        else {
            output_stats(fop, has_query, sid_crossover, BEG, END, vpairs, results, start, stop);
        }
    }
    else if (is_trace) {
        if (use_emboss_format) {
            output_emboss(fop, has_query, sid_crossover, matrix, vpairs, queries, sequences, results, start, stop, case_sensitive, alphabet_aliases);
        }
        else if (use_ssw_format) {
            output_ssw(fop, has_query, sid_crossover, matrix, vpairs, queries, sequences, results, start, stop, case_sensitive, alphabet_aliases);
        }
        else if (use_sam_format) {
            output_sam(fop, use_sam_header, has_query, sid_crossover, matrix, vpairs, queries, sequences, results, start, stop, case_sensitive, alphabet_aliases);
        }
        else {
            output_trace(fop, has_query, sid_crossover, T, matrix, BEG, END, vpairs, results, start, stop, case_sensitive, alphabet_aliases);
        }
    }
    else {
        output_basic(fop, has_query, sid_crossover, BEG, END, vpairs, results, start, stop);
    }
    if (is_table) {
        output_tables(has_query, sid_crossover, T, BEG, END, vpairs, results, start, stop);
    }
}

inline static size_t parse_bytes(const char *value)
{
    size_t multiplier = 0;
    double base = 0;
    string unit;
    istringstream iss(value);

    iss >> base;
    if (!iss) {
        eprintf(stderr, "could not parse memory value\n");
        return 0;
    }
    iss >> unit;
    if (!iss || unit.empty()) {
        eprintf(stderr, "could not parse memory unit, default to bytes\n");
        unit = "b";
    }
    transform(unit.begin(), unit.end(), unit.begin(), ::tolower);

    if ("b" == unit
            || "byte" == unit
            || "bytes" == unit) {
        multiplier = 1ULL;
    }
    else if ("kb" == unit
            || "kbyte" == unit
            || "kbytes" == unit
            || "kilobyte" == unit
            || "kilobytes" == unit) {
        multiplier = 1000ULL;
    }
    else if ("mb" == unit
            || "mbyte" == unit
            || "mbytes" == unit
            || "megabyte" == unit
            || "megabytes" == unit) {
        multiplier = 1000ULL * 1000ULL;
    }
    else if ("gb" == unit
            || "gbyte" == unit
            || "gbytes" == unit
            || "gigabyte" == unit
            || "gigabytes" == unit) {
        multiplier = 1000ULL * 1000ULL * 1000ULL;
    }
    else {
        eprintf(stderr, "could not parse byte unit\n");
        return 0;
    }

    return static_cast<size_t>(multiplier*base);
}

#ifdef HAVE_SETUNHANDLEDEXCEPTIONFILTER
LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS * ExceptionInfo)
{
    switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
    {
    case EXCEPTION_ACCESS_VIOLATION:
        fputs("Error: EXCEPTION_ACCESS_VIOLATION\n", stderr);
        break;
    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
        fputs("Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED\n", stderr);
        break;
    case EXCEPTION_BREAKPOINT:
        fputs("Error: EXCEPTION_BREAKPOINT\n", stderr);
        break;
    case EXCEPTION_DATATYPE_MISALIGNMENT:
        fputs("Error: EXCEPTION_DATATYPE_MISALIGNMENT\n", stderr);
        break;
    case EXCEPTION_FLT_DENORMAL_OPERAND:
        fputs("Error: EXCEPTION_FLT_DENORMAL_OPERAND\n", stderr);
        break;
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
        fputs("Error: EXCEPTION_FLT_DIVIDE_BY_ZERO\n", stderr);
        break;
    case EXCEPTION_FLT_INEXACT_RESULT:
        fputs("Error: EXCEPTION_FLT_INEXACT_RESULT\n", stderr);
        break;
    case EXCEPTION_FLT_INVALID_OPERATION:
        fputs("Error: EXCEPTION_FLT_INVALID_OPERATION\n", stderr);
        break;
    case EXCEPTION_FLT_OVERFLOW:
        fputs("Error: EXCEPTION_FLT_OVERFLOW\n", stderr);
        break;
    case EXCEPTION_FLT_STACK_CHECK:
        fputs("Error: EXCEPTION_FLT_STACK_CHECK\n", stderr);
        break;
    case EXCEPTION_FLT_UNDERFLOW:
        fputs("Error: EXCEPTION_FLT_UNDERFLOW\n", stderr);
        break;
    case EXCEPTION_ILLEGAL_INSTRUCTION:
        fputs("Error: EXCEPTION_ILLEGAL_INSTRUCTION\n", stderr);
        break;
    case EXCEPTION_IN_PAGE_ERROR:
        fputs("Error: EXCEPTION_IN_PAGE_ERROR\n", stderr);
        break;
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
        fputs("Error: EXCEPTION_INT_DIVIDE_BY_ZERO\n", stderr);
        break;
    case EXCEPTION_INT_OVERFLOW:
        fputs("Error: EXCEPTION_INT_OVERFLOW\n", stderr);
        break;
    case EXCEPTION_INVALID_DISPOSITION:
        fputs("Error: EXCEPTION_INVALID_DISPOSITION\n", stderr);
        break;
    case EXCEPTION_NONCONTINUABLE_EXCEPTION:
        fputs("Error: EXCEPTION_NONCONTINUABLE_EXCEPTION\n", stderr);
        break;
    case EXCEPTION_PRIV_INSTRUCTION:
        fputs("Error: EXCEPTION_PRIV_INSTRUCTION\n", stderr);
        break;
    case EXCEPTION_SINGLE_STEP:
        fputs("Error: EXCEPTION_SINGLE_STEP\n", stderr);
        break;
    case EXCEPTION_STACK_OVERFLOW:
        fputs("Error: EXCEPTION_STACK_OVERFLOW\n", stderr);
        break;
    default:
        fputs("Error: Unrecognized Exception\n", stderr);
        break;
    }
    fflush(stderr);
    return EXCEPTION_EXECUTE_HANDLER;
}
void set_signal_handler()
{
    SetUnhandledExceptionFilter(windows_exception_handler);
}
#else
void posix_signal_handler(int sig, siginfo_t *siginfo, void *context)
{
    (void)context;
    switch (sig)
    {
    case SIGSEGV:
        fputs("Caught SIGSEGV: Segmentation Fault\n", stderr);
        break;
    case SIGINT:
        fputs("Caught SIGINT: Interactive attention signal, (usually ctrl+c)\n", stderr);
        break;
    case SIGFPE:
        switch (siginfo->si_code)
        {
        case FPE_INTDIV:
            fputs("Caught SIGFPE: (integer divide by zero)\n", stderr);
            break;
        case FPE_INTOVF:
            fputs("Caught SIGFPE: (integer overflow)\n", stderr);
            break;
        case FPE_FLTDIV:
            fputs("Caught SIGFPE: (floating-point divide by zero)\n", stderr);
            break;
        case FPE_FLTOVF:
            fputs("Caught SIGFPE: (floating-point overflow)\n", stderr);
            break;
        case FPE_FLTUND:
            fputs("Caught SIGFPE: (floating-point underflow)\n", stderr);
            break;
        case FPE_FLTRES:
            fputs("Caught SIGFPE: (floating-point inexact result)\n", stderr);
            break;
        case FPE_FLTINV:
            fputs("Caught SIGFPE: (floating-point invalid operation)\n", stderr);
            break;
        case FPE_FLTSUB:
            fputs("Caught SIGFPE: (subscript out of range)\n", stderr);
            break;
        default:
            fputs("Caught SIGFPE: Arithmetic Exception\n", stderr);
            break;
        }
    case SIGILL:
        switch (siginfo->si_code)
        {
        case ILL_ILLOPC:
            fputs("Caught SIGILL: (illegal opcode)\n", stderr);
            break;
        case ILL_ILLOPN:
            fputs("Caught SIGILL: (illegal operand)\n", stderr);
            break;
        case ILL_ILLADR:
            fputs("Caught SIGILL: (illegal addressing mode)\n", stderr);
            break;
        case ILL_ILLTRP:
            fputs("Caught SIGILL: (illegal trap)\n", stderr);
            break;
        case ILL_PRVOPC:
            fputs("Caught SIGILL: (privileged opcode)\n", stderr);
            break;
        case ILL_PRVREG:
            fputs("Caught SIGILL: (privileged register)\n", stderr);
            break;
        case ILL_COPROC:
            fputs("Caught SIGILL: (coprocessor error)\n", stderr);
            break;
        case ILL_BADSTK:
            fputs("Caught SIGILL: (internal stack error)\n", stderr);
            break;
        default:
            fputs("Caught SIGILL: Illegal Instruction\n", stderr);
            break;
        }
        break;
    case SIGTERM:
        fputs("Caught SIGTERM: a termination request was sent to the program\n", stderr);
        break;
    case SIGABRT:
        fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr);
        break;
    default:
        break;
    }
    fflush(stderr);
    _exit(EXIT_FAILURE);
}
void set_signal_handler()
{
    /* register our signal handlers */
    {
        struct sigaction sig_action = {};
        sig_action.sa_sigaction = posix_signal_handler;
        sigemptyset(&sig_action.sa_mask);
        sig_action.sa_flags = SA_SIGINFO;
        if (sigaction(SIGSEGV, &sig_action, NULL) != 0) { err(1, "sigaction"); }
        if (sigaction(SIGFPE, &sig_action, NULL) != 0) { err(1, "sigaction"); }
        if (sigaction(SIGINT, &sig_action, NULL) != 0) { err(1, "sigaction"); }
        if (sigaction(SIGILL, &sig_action, NULL) != 0) { err(1, "sigaction"); }
        if (sigaction(SIGTERM, &sig_action, NULL) != 0) { err(1, "sigaction"); }
        if (sigaction(SIGABRT, &sig_action, NULL) != 0) { err(1, "sigaction"); }
    }
}
#endif