File: GraphFromFasta_MPI.cc

package info (click to toggle)
trinityrnaseq 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 212,452 kB
  • ctags: 5,067
  • sloc: perl: 45,552; cpp: 19,678; java: 11,865; sh: 1,485; makefile: 613; ansic: 427; python: 313; xml: 83
file content (2525 lines) | stat: -rw-r--r-- 78,688 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
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <map>
#include <fstream>

#include "base/CommandLineParser.h"
#include "analysis/DNAVector.h"
#include "aligns/KmerAlignCore.h"

#include <math.h>
#include "analysis/KmerTable.h"
#include "analysis/NonRedKmerTable.h"
#include "util/mutil.h"
#include <omp.h>
#include "analysis/DeBruijnGraph.h"
#include "analysis/sequenceUtil.h"

#define HUGE_DATASET
#ifdef MPI_ENABLED
#include<mpi.h>
#endif
#include <unistd.h>
#include <sys/resource.h>
void print_cpu_memory()
{
 struct rusage usage;
 getrusage (RUSAGE_SELF, &usage);
 // printf ("CPU time: %ld.%06ld sec user, %ld.%06ld sec system\n",
 //  usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
 //    usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);

 cerr<<"Max memory consumed  in GraphFromFasta "<<usage.ru_maxrss<<endl;
 

}



static float MIN_KMER_ENTROPY = 1.3;
static float MIN_WELD_ENTROPY = MIN_KMER_ENTROPY;  // min entropy for each half of a welding mer (kk)
static bool DEBUG = false;
static float MAX_RATIO_INTERNALLY_REPETITIVE = 0.85;

static bool REPORT_WELDS = false;
static int MAX_CLUSTER_SIZE = 100;
static int MIN_CONTIG_LENGTH = 24;


#include<sys/time.h>
static struct timeval start,end;
void timer_start(){

  gettimeofday(&start,NULL);
}


double timer_stop(){
  gettimeofday(&end,NULL);
  double time_taken = ((end.tv_usec-start.tv_usec) + 1000000*(end.tv_sec - start.tv_sec));
 time_taken = time_taken/1000;
  return time_taken;
}


// print nucleotide sequence 80 chars per line
void PrintSeq(const DNAVector & d) {
    int i;
    for (i=0; i<d.isize(); i++) {
        cout << d[i];
        if ((i+1) % 80 == 0)
            cout << endl;
    }
    cout << endl;
}


// wrapper around an integer vector
class Pool
{
public:
    
    

    Pool() {
        m_id = -1;
    }
    
    Pool(int id) {
        m_id = id;
    }

    Pool(const Pool& p) {
        m_id = p.m_id;
        m_index = p.m_index;
    }
    
    void push_back(int i) { add(i); }
    void add(int i) {m_index.push_back(i);}
    
    void add (Pool& p) {
        for (int i = 0; i < p.size(); i++) {
            if (! contains(p[i])) {
                add(p[i]);
            }
        }
    }
    
    void clear () {
        svec<int> newvec;
        m_index = newvec;
    }
    
    void exclude (Pool& p) {
        
        for (int i = 0; i < p.size(); i++) {
            if (p.contains(i)) {
                exclude(i);
            }
        }
    }
    
    void exclude (int i) {
        //FIXME:  there must be a more efficient way to do this.
        svec<int> new_mvec;
        for (int j = 0; j < m_index.size(); j++) {
            int val = m_index[j];
            if (val != i) {
                new_mvec.push_back(val);
            }
        }
        m_index = new_mvec;
    }
            

    int size() const {return m_index.isize();}
    int isize() const { return(size()); }
    
    int get(int i) const {return m_index[i];}

    int get_id() {
        return(m_id);
    }

    int operator [] (const int& i) {
        return(get(i));
    }

    bool contains(int id) {
        for (int i = 0; i < size(); i++) {
            if (get(i) == id) {
                return(true);
            }
        }
        return(false);
    }
    
    
    void sortvec() {
        sort(m_index.begin(), m_index.end());
    }
    

private:
    svec<int> m_index;
    int m_id;

};

// some prototypes
void add_iworm_link(map<int,Pool>& weld_reinforced_iworm_clusters, int iworm_index, int iworm_pool_addition);
void add_reciprocal_iworm_link(map<int,Pool>& weld_reinforced_iworm_clusters, int iworm_A, int iworm_B);




bool is_simple_repeat(const DNAVector& kmer) {
    
    // in the future, should just do SW alignment
    // for now, compare all half-kmers
    

    string best_left_kmer;
    string best_right_kmer;
    int best_left_pos = 0;
    int best_right_pos = 0;
    float max_repetitive_ratio = 0;


    int mid_kmer_length = kmer.isize()/2;
    
    for (int i = 0; i < mid_kmer_length; i++) {

        for (int j = i+1; j <= mid_kmer_length; j++) {
        
            int ref_kmer_pos = i;
            int internal_kmer_pos = j;
                    

            // compare half-kmers
            
            int bases_compared = 0;
            int bases_common = 0;
     
            stringstream left_kmer;
            stringstream right_kmer;
            

            while (internal_kmer_pos <= j + mid_kmer_length - 1) {
                bases_compared++;
                if (kmer[ref_kmer_pos] == kmer[internal_kmer_pos]) {
                    bases_common++;
                }
                
                //if (DEBUG) {
                    left_kmer << kmer[ref_kmer_pos];
                    right_kmer << kmer[internal_kmer_pos];
                    //}

                
                ref_kmer_pos++;
                internal_kmer_pos++;
            }
            
            float ratio_same = (float)bases_common/bases_compared;
            if (ratio_same > max_repetitive_ratio) {
                max_repetitive_ratio = ratio_same;
                best_left_kmer = left_kmer.str();
                best_right_kmer = right_kmer.str();
                best_left_pos = i;
                best_right_pos = j;
            }


            if ((! DEBUG) && ratio_same >= MAX_RATIO_INTERNALLY_REPETITIVE) {
                
                // quickest way of exiting this function.  No reason to capture the most repetitive kmer since we're not showing it.
                return(true);
            }
            
        }
           
    }

        
    if (DEBUG) {
        #pragma omp critical
        cout << "# " << kmer.AsString() << " most repetitive kmers:: (" << best_left_pos << "," << best_right_pos << ") " 
             << best_left_kmer << ", " << best_right_kmer << " ratioID: "  << max_repetitive_ratio << endl;
    }

    if (max_repetitive_ratio > MAX_RATIO_INTERNALLY_REPETITIVE) {
        
        if (DEBUG) {
            #pragma omp critical
            cout << "# " << kmer.AsString() << " is internally repetitive: (" << best_left_pos << "," << best_right_pos << ") " 
                 << best_left_kmer << ", " << best_right_kmer << " ratioID: "  << max_repetitive_ratio << endl;
        }
        
        return(true);
    }
    else {
        return(false); // not internally repetitive
    }
}




// used to determine complexity of a kmer
float compute_entropy(const DNAVector & kmer) {
    
    map<char,int> char_map;
    
    for (unsigned int i = 0; i < (unsigned int)kmer.isize(); i++) {
        
        char c = kmer[i];
        char_map[c]++;
    }
    
    float entropy = 0;
    
    char nucs[] = { 'G', 'A', 'T', 'C' };
    
    for (unsigned int i = 0; i < 4; i++) {
        
        char nuc = nucs[i];
        
        int count = char_map[nuc];
        
        float prob = (float)count / kmer.isize();
        
        if (prob > 0) {
            float val = prob * log(1/prob)/log((float)2);
            entropy += val;
        }
    }
    
    return(entropy);
}

bool IsSimple(const DNAVector & d) 
{
   
    // compute entropy
    float e = compute_entropy(d);

    //cout << d.AsString() << " has entropy: " << e << endl;
    if (e < MIN_KMER_ENTROPY) {
        
        //if (DEBUG) 
        //    cerr << "-Sequence declared low complexity: " << d.AsString() << " with entropy " << e << endl;
        
        return(true);
    }
    else {
        return false;
    }
    

    /*    old check
    
    int i;
    int k = 0;
    for (i=2; i<d.isize(); i++) {
        if (d[i] == d[i-2])
            k++;
    }
    double ratio = (double)k/(double)d.isize();
    if (ratio > 0.6) {

        if (DEBUG) 
            cerr << "-Sequence declared low complexity: " << d.AsString() << " with entropy " << e << endl;
    
        return true;
    }

    */
   
    
}


bool SimpleHalves(const DNAVector & d) {
    int len = d.isize();
    int mid_pos = int(len/2);

    DNAVector left;
    left.resize(mid_pos);
    for (int i = 0; i < mid_pos; i++) {
        left[i] = d[i];
    }
    
    DNAVector right;
    right.resize(len - mid_pos);
    int counter = 0;
    for (int i = mid_pos; i < len; i++) {
        right[counter] = d[i];
        
        counter++;
    }

    if (DEBUG) {
        #pragma omp critical
        {
            cout << "## Left half: " << left.AsString() << ", en: " << compute_entropy(left);
            cout << "\tRight half: " << right.AsString() << ", en: " << compute_entropy(right) << endl;
        }
    }
    
    return (compute_entropy(left) < MIN_WELD_ENTROPY 
            || 
            compute_entropy(right) < MIN_WELD_ENTROPY
            ||
            is_simple_repeat(left)
            ||
            is_simple_repeat(right)

            );
}
        


 

// shadows are contigs that are nearly identical but arise from sequencing error-containing kmers
bool IsShadow(const DNAVector & a, const DNAVector & b, int startA, int startB, int k) 
{
    //return false;
    
    
    int i;
    int n = 0;
    int nn = 0;
    int last = -1;
    int len = 0;
    for (i=startA; i<a.isize(); i++) {
        int x = i-startA + startB;
        
        if (x >= b.isize())
            break;
        len++;
        if (a[i] != b[x]) {     
            //cout << "Mismatch @ " << i << " and " << x << endl;
            if (last >= 0) {
                int dist = i-last;
                if (x > 3 && i > 3 && a[i-1] != b[x-1] && a[i-2] != b[x-2])
                    break;
                if (dist == k+1) {
                    n++;
                } else {
                    nn++;
                }
            }
            last = i;
        } else {
            //cout << i << " and " << x << endl;
        }
    }
    
    int expect = (int)(0.9*(double(len/(k+1)-1)));
    //cout << "Len: " << len << " Expect: " << expect << " Observed: " << n << " diss: " << nn << endl;
    if (n >= expect && n > 4 && nn < n/5) {
        return true;
    }
    return false;
    
}


// e.g., given the string:
// >a1;74093 K: 25 length: 8833
// extract "74093"
double Coverage(const string &s) 
{
    const char *nptr = std::strchr(s.c_str(), ';');
    if (nptr == NULL) // ';' not found
        return 1.0;
    
    char *endptr;
    double ret = std::strtod(++nptr, &endptr);
    if (endptr == nptr || ret < 1.0) // conversion not possible, or value < 1.0
        return 1.0;
    
    return ret;
}

bool IsGoodCoverage(double a, double b, double min_iso_ratio) 
{
    
    if (a > b) {
        double tmp = b;
        b = a;
        a = tmp;
    }
    
    if (a/b > min_iso_ratio) {
        return(true);
    }
    else {
        return(false);
    }
        
    /*  original way
    
    //return true;
    
    double dev_a = sqrt(a);
    double dev_b = sqrt(b);
    
    double mean = (a+b)/2;
    
    if (dev_b > dev_a) {
        double dev_tmp = dev_a;
        dev_a = dev_b;
        dev_b = dev_tmp;
        
        double tmp = a;
        a = b;
        b = tmp;
    }
    
    //cout << "Coverage check: " << a << " mean: " << mean << " dev: " << dev_a << endl;
    
    if ((a - mean) < 10*dev_a)
        return true;
    
    double ratio = a/b;
    if (ratio < 1.)
        ratio = 1./ratio;
    
    if (ratio < 100.)
        return true;
    else
        return false;


    */

}



class Welder
{
public:
    Welder(int k, int kk) {
        m_k = k;
        m_kk = kk;
        m_pTab = NULL;
    }
    
    void SetTable(NonRedKmerTable * p) {
        m_pTab = p;
    }
    

    // constructs the required weldable kmer from two contigs a,b and positions one and two
    void WeldableKmer(DNAVector & out, 
                      const DNAVector & a, int one, 
                      const DNAVector & b, int two) 
    {
        out.resize(m_kk);
        int flank = (m_kk - m_k)/2;
        
        int startA = one-flank;
        int stopA = one+m_k;
        int startB = two+m_k;
        int stopB = startB + flank;
        
        if (DEBUG) 
            cerr << "weldableKmer(" << startA << "," << stopA << "); (" << startB << "," << stopB << ")" << endl;
        

        if (startA < 0 || stopB >= b.isize()) {
            out.resize(0);
            if (DEBUG) 
                cerr << "range out of bounds" << endl;
            return;
        }
        
        int i;
        int j = 0;
        for (i=startA; i<stopA; i++) {
            out[j] = a[i];
            j++;
        }
        for (i=startB; i<stopB; i++) {
            out[j] = b[i];
            j++;
        }
        
        if (DEBUG)
            cerr << "\tweld candidate: " << out.AsString() << endl;

    }
    
    
    bool Weldable(const DNAVector & a, int one, const DNAVector & b, int two, int thresh, string& welding_kmer, int& weldable_kmer_read_count) 
    {
        int i;
        DNAVector d; // stores the required weldabler kmer of length kk
        WeldableKmer(d, a, one, b, two); // constructs teh weldable kmer, stores in (d)
        if (d.isize() == 0)
            return false;
        
        
        int count = m_pTab->GetCount(d, 0); // see if the weldable kmer exists among the reads and get read count.
        weldable_kmer_read_count = count;
        

        if (DEBUG) 
            cerr << "got weldable candidate: " << d.AsString() << " with count: " << weldable_kmer_read_count << endl;
        

        if (count >= thresh) {

            /* 
               if (SimpleHalves(d)) {
               cerr << "Error, halves of weldmer " << d.AsString() << " were found to fail the simple test....  FIXME" << endl;
               exit(3);
               } 
            */
            
            welding_kmer = d.AsString();

            return true;
            
        }
        else
            return false;
    }


    int weldmer_count_in_reads(const DNAVector weldmer) 
    {
        // weldmer created outside function and provided as input.
                
        int count = m_pTab->GetCount(weldmer, 0); // see if the weldable kmer exists among the reads and get read count.
        
        return(count);
    }

    
private:
    NonRedKmerTable * m_pTab;
    
    int m_k;
    int m_kk;
};

void Add(vecDNAVector & all, DNAVector & add, int & counter) 
{
    #pragma omp critical
    {
        all.push_back(add);
        counter++;
    }
    
}


void describe_poolings (svec<Pool>& pool_vec) {
    
    cerr << endl << "Pools described as follows:" << endl;
    
    for (unsigned int i=0; i<pool_vec.size(); i++) {
        int oldpool_id = i;
        
        stringstream old_pool_info;
        
        Pool& oldpool = pool_vec[oldpool_id];
        
        old_pool_info << "Pool [" << i << "] info: "; 
        for (int j=0; j<oldpool.size(); j++) {
            int iworm_id = oldpool.get(j);
            
            old_pool_info << iworm_id << " ";
        }
        old_pool_info << endl;
        
        cerr << old_pool_info.str();
    }
    
}

void describe_bubblings (vector<Pool>& pool_vec, map<int,Pool>& pool_idx_to_containment, int round) {
    
    for (unsigned int i=0; i<pool_vec.size(); i++) {

        stringstream old_pool_info;
        
        Pool& oldpool = pool_vec[i];
        
        old_pool_info << "Bubbling[" << round << "], Pool [" << oldpool.get_id() << "] size: " << oldpool.size() << ", links: "; 
        for (int j=0; j<oldpool.size(); j++) {
            int iworm_id = oldpool.get(j);
            
            old_pool_info << iworm_id << " ";
        }
        if (pool_idx_to_containment.find(oldpool.get_id()) != pool_idx_to_containment.end()) {
            old_pool_info << "; Containments: ";
            Pool& containment_pool = pool_idx_to_containment[oldpool.get_id()];
            for (int j = 0; j < containment_pool.size(); j++) {
                old_pool_info << containment_pool.get(j) << " ";
            }
        }
        
        old_pool_info << endl;
        
        cerr << old_pool_info.str();
    }
    
}


bool sort_pool_sizes_descendingly(const Pool& a, const Pool& b) {
    return (a.size() > b.size());
    
}

bool sort_pool_sizes_ascendingly(const Pool& a, const Pool& b) {
    return (a.size() < b.size());
}


svec<Pool> bubble_up_cluster_growth(map<int,Pool>& pool, map<int,bool>& ignore) {
    
    vector<Pool> pool_vec;
    for (map<int,Pool>::iterator it = pool.begin(); it != pool.end(); it++) {
        pool_vec.push_back(it->second);
    }
    
    sort (pool_vec.begin(), pool_vec.end(), sort_pool_sizes_ascendingly);
    
    //cerr << "After sorting: " << endl;
    //describe_poolings(pool_vec);
    

    //TODO: nothing ignored ends up as a containment.

    map<int,Pool> pool_idx_to_containment;
    map<int,int>  pool_idx_to_vec_idx;
    // init 
    for (int i = 0; i < pool_vec.size(); i++) {
        int id = pool_vec[i].get_id();
        Pool tmp(id);
        pool_idx_to_containment[id] = tmp;
        pool_idx_to_vec_idx[id] = i;
    }
    
    
    cerr << "now bubbling: " << endl << endl;
    bool bubbling = true;
    
    int bubble_round = 0;
    while (bubbling) {
        bubble_round++;
        bubbling = false;
        
        if (DEBUG) {
            describe_bubblings(pool_vec, pool_idx_to_containment, bubble_round);
            cerr << "bubbling up graph, round: " << bubble_round << endl;
        }
        
        for (int i = 0; i < pool_vec.size(); i++) {
            
            
            
            Pool& p = pool_vec[i];
            int id = p.get_id();
            
            // cerr << "Bubble_round: " << bubble_round << ", processing pool(" << i << "), with id: " << id << " and size: " << p.size() << endl;
            
            if (pool_idx_to_containment[id].size()) {
                // remove any entries in the pool that are already stored in the containment list.
                p.exclude(pool_idx_to_containment[id]);
            }
            
            if (p.size() > 0) {
                
                // cerr << "Processing pool: " << p.get_id() << " with size: " << p.size() << endl;
                
                // bubble upward
                // get other id:
                bool local_bubbled = false;
                int other_id = -1;
                
                for (int j = 0; j < p.size(); j++) {
                    

                    other_id = p[j];
                    if (other_id == id) { continue; }
                    
                    if (pool_idx_to_containment[other_id].size() + pool_idx_to_containment[id].size() + 2 <= MAX_CLUSTER_SIZE) {  // + 2 since neither self is included in its containment list

                        // move this id to the containment list of the 'other' 
                        pool_idx_to_containment[other_id].add(id);
                        
                        // add this id's containment list to the 'other'
                        pool_idx_to_containment[other_id].add(pool_idx_to_containment[id]);
                        
                        // remove 'other_id' from its own containment list.  (it's self-evident)
                        pool_idx_to_containment[other_id].exclude(other_id);
                        pool_vec[ pool_idx_to_vec_idx[ other_id ] ].exclude( id ); 
                    
                        local_bubbled = true;
                        break;
                    }
                }
                if (local_bubbled) {
                    // update links previously to (id) over to (other_id)

                    for (int j = 0; j < p.size(); j++) {
                        if (p[j] != other_id) {
                            // p[j] should contain id, since id linked to p[j] and all should be reciprocal
                            if (! pool_vec[ pool_idx_to_vec_idx[ p[j] ] ].contains( id )) {
                                

                                describe_bubblings(pool_vec, pool_idx_to_containment, -2);
                                cerr << "Error, " << p[j] << " doesn't contain: " << id << " but vice-versa was true." << endl;
                                exit(4);
                            }
                            
                            pool_vec[ pool_idx_to_vec_idx[ p[j] ] ].exclude( id );
                            // replace it with a link to the other_id, if not already linked.
                            if (! pool_vec[ pool_idx_to_vec_idx[ p[j] ] ].contains( other_id )) {
                                pool_vec[ pool_idx_to_vec_idx[ p[j] ] ].add( other_id );
                            }
                            // links must be reciprocal
                            if (! pool_vec[ pool_idx_to_vec_idx[ other_id ] ].contains( p[j] )) {
                                pool_vec[ pool_idx_to_vec_idx[ other_id ] ].add( p[j] );
                            } 
                            
                        }
                    }
                    
                    // clear out this pool
                    p.clear();
                    
                    // clear out the containment for this pool
                    pool_idx_to_containment[id].clear();
                    
                    
                    bubbling = true;
                }
            }
            
        }
        
    }
    
    cerr << "done bubbling.\n";


    // Some entries might be linked but not contained, or exist as singletons.
    map<int,bool> found_contained;
    for (map<int,Pool>::iterator it = pool_idx_to_containment.begin(); it != pool_idx_to_containment.end(); it++) {
        Pool& p = it->second;
        if (p.size() > 0) {
            found_contained[ p.get_id() ] = true; // include self since not in its own containment list.
            for (int i = 0; i < p.size(); i++) {
                int id = p.get(i);
                found_contained[id] = true;
            }
        }
    }

    

    
    svec<Pool> bubbled_up_pools;
    for (int i = 0; i < pool_vec.size(); i++) {
                
        Pool& p = pool_vec[i];
        int id = p.get_id();
        
        map<int,Pool>::iterator it = pool_idx_to_containment.find(id);

        if (it != pool_idx_to_containment.end() && (it->second).size() > 0 ) {
            // Entry (id) has a containment list.

            Pool containment_pool = it->second;
            // add (id) and its containment list to the final pool
            containment_pool.add(id);
            bubbled_up_pools.push_back(containment_pool);
        }
        else if (found_contained.find(id) == found_contained.end()) { // not part of any containment list
            
            if (DEBUG) 
                cerr << id << " is a loner, not contained." << endl;
        
            Pool loner;
            loner.add(id);
            bubbled_up_pools.push_back(loner);
        }
    }

        
    return(bubbled_up_pools);

}

    
// Single-linkage clustering of iworm contigs in pools
svec<Pool> sl_cluster_pools(map<int,Pool>& pool, map<int,bool>& ignore) {

    // Just pull out the ordered pools, initial order doesn't matter... they get trickled upward.
    svec<Pool> pool_vec;
    
    for (map<int,Pool>::iterator it = pool.begin(); it != pool.end(); it++) {
        pool_vec.push_back(it->second);
    }
        
    // init entries to loweset pool index they're found in.
    map<int,int> mapped;
    
    for (size_t i = 0; i < pool_vec.size(); i++) {
        
        Pool p = pool_vec[i];
        
        for (int j = 0; j < p.size(); j++) {
            int ele_id = p.get(j);
            
            if (mapped.find(ele_id) == mapped.end()) {
                
                mapped[ele_id] = i; // first time seeing it, so lowest pool index value
                
                // cerr << "Mapping: " << " ele: " << ele_id << " to pool " << i << endl;
                
            }
            
        }
    }
    
    int round = 0;
    bool remapped_flag = true;
    while (remapped_flag) {
        


        round++;
        cerr << "Transitive closure, round: " << round << endl;
        remapped_flag = false;
        
        
        if (DEBUG) {
            cerr << "Before remapping: " << endl;
            
            describe_poolings(pool_vec);
        }
        

        // do transitive closure on the poolings
        map<int,int> remapped = mapped;
        
        // trickle pool mappings upward
        
        for (unsigned int i=0; i<pool_vec.size(); i++) {
            int oldpool_id = i;
            
            Pool& oldpool = pool_vec[oldpool_id];
            
            int lowest_pool_map_id = oldpool_id;
            bool pool_reassignment_required = false;
            for (int j=0; j<oldpool.size(); j++) {
                int iworm_id = oldpool.get(j);
                int pool_mapping = remapped[iworm_id];
                
                
                if (pool_mapping != lowest_pool_map_id) {
                    pool_reassignment_required = true;
                }
                if (pool_mapping >= 0 && pool_mapping < lowest_pool_map_id) {
                    lowest_pool_map_id = pool_mapping;
                }
            }
            
            // reassign all members to lowest_pool_map_id (if same as pool_id, some entries may still be higher even if not less)
            if (pool_reassignment_required) {
                for (int j=0; j<oldpool.size(); j++) {
                    int iworm_id = oldpool.get(j);
                    remapped[iworm_id] = lowest_pool_map_id;
                    // cerr << "-remapping: iworm(" << iworm_id << ") to pool " << lowest_pool_map_id << endl; 
                }
            }
            
            if (lowest_pool_map_id < oldpool_id) {
                // reassign to lower pool
                Pool tmp;
                for (int j=0; j<oldpool.size(); j++) {
                    int iworm_id = oldpool.get(j);
                    
                    if (! pool_vec[lowest_pool_map_id].contains(iworm_id)) {

                        pool_vec[lowest_pool_map_id].add(iworm_id);
                    }
                    //#pragma omp critical
                    //cout << "RELOCATING: " << iworm_id << " from old pool " << oldpool_id << " to new pool " << lowest_pool_map_id << endl;
                }
                //cerr << "-clearing out old pool: " << oldpool_id << endl;
                pool_vec[oldpool_id] = tmp;
                remapped_flag = true;
            }    
        }
        mapped = remapped; // new assignments
        
        if (DEBUG) {
            cerr << "After remapping: " << endl;
            describe_poolings(pool_vec);
        }
        

    }
    
         
    
    //cerr << "...done (" << end_time - start_time << " seconds)" << endl;

    //------------------------------------------
    // Clustered inchworm contigs now defined.
    // Generate final output
    
    svec<Pool> nr_pools;
    
     for (int i=0; i < (int) pool_vec.size(); i++) {
        Pool & p = pool_vec[i];
        
        if (p.size() == 0) { continue; }
        
        p.sortvec();

        Pool nr_entries; // remove the redundant entries in p
        for (int j=0; j<p.size(); j++) {
            int z = p.get(j);
            
            if (ignore[z]) {
                continue;
            }
            

            if (j > 0 && p.get(j-1) == z) {
                // same entry ended up on the pool vec, already reported it.
                continue;
            }
            
            nr_entries.add(z);
        }
        nr_pools.push_back(nr_entries);
    }
    

    return(nr_pools);
    

}

void add_scaffolds_to_clusters(map<int,Pool>& iworm_clusters, string scaffolding_filename, vecDNAVector& dna, int min_glue_required, float glue_factor) {
    
    ifstream in (scaffolding_filename.c_str());
    
    string line;
    if (in.is_open()) {
        while (! in.eof()) {
            getline(in, line);
        
            if (line.length() == 0) { continue; }
            
            //cerr << line << endl;
            

            /* Format is like so:
            iwormA  idxA  iwormB idxB  count_pair_links

            a12;25 11 a8;4 17 41
            a14;13 13 a3;9 62 40
            a15;15 14 a26;8 25 33
            a13;23 12 a7;4 36 31

            */


            istringstream token (line);
            
            string iworm_acc_A, iworm_index_A_str, iworm_acc_B, iworm_index_B_str, count_str;
            token >> iworm_acc_A >> iworm_index_A_str >> iworm_acc_B >> iworm_index_B_str >> count_str;
            
            int iworm_index_A = atoi(iworm_index_A_str.c_str());
            int iworm_index_B = atoi(iworm_index_B_str.c_str());
            int pair_link_count = atoi(count_str.c_str());
            
            string iworm_A = dna.Name(iworm_index_A);
            string iworm_B = dna.Name(iworm_index_B);

            // verify that our names match up
            if (iworm_A.find(iworm_acc_A) == string::npos) {
                cerr << "Error, cannot locate acc: " << iworm_acc_A << " as substring of " << iworm_A << endl;
                exit(4);
            }
            if (iworm_B.find(iworm_acc_B) == string::npos) {
                cerr << "Error, cannot locate acc: " << iworm_acc_B << " as substring of " << iworm_B << endl;
                exit(4);
            }
            
            
            double coverage_A = Coverage(iworm_A);
            double coverage_B = Coverage(iworm_B);
            
            double higher_coverage_val = (coverage_A > coverage_B) ? coverage_A : coverage_B;
            int minCov = (int) (higher_coverage_val * glue_factor);
            if (minCov < min_glue_required) {
                minCov = min_glue_required;
            }
            
            
            if (pair_link_count >= minCov) {
               
                if (DEBUG)
                    cerr << "SCAFFOLD_ACCEPT: " << line << endl;
            
                // reciprocal linkage
                
                add_reciprocal_iworm_link(iworm_clusters, iworm_index_A, iworm_index_B);
                                
            }
            else {
                if (DEBUG) 
                    cerr << "SCAFFOLD_REJECT: " << line << endl;
            }
            
        }
        in.close();
    }
    else {
        cerr << "Error, cannot open file: " << scaffolding_filename;
        exit(3);
    }
    


}


void add_iworm_link(map<int,Pool>& weld_reinforced_iworm_clusters, int iworm_index, int iworm_pool_addition) {
    
    map<int,Pool>::iterator it = weld_reinforced_iworm_clusters.find(iworm_index);

    if (it == weld_reinforced_iworm_clusters.end()) {
        // add it
        Pool p(iworm_index);
        p.add(iworm_pool_addition);
        weld_reinforced_iworm_clusters[iworm_index] = p;
    }
    else {
        Pool& p = it->second;
        if (! p.contains(iworm_pool_addition)) {
            p.add(iworm_pool_addition);
        }
    }
}


void add_reciprocal_iworm_link(map<int,Pool>& weld_reinforced_iworm_clusters, int iworm_A, int iworm_B) {

    add_iworm_link(weld_reinforced_iworm_clusters, iworm_A, iworm_B);
    add_iworm_link(weld_reinforced_iworm_clusters, iworm_B, iworm_A);

}
    

void add_unclustered_iworm_contigs (svec<Pool>& clustered_pools, vecDNAVector& dna) {

    if (DEBUG) {
        cerr << "Adding unclustered iworm contigs." << endl;
    }

    map<int,bool> found;

    for (svec<Pool>::iterator it = clustered_pools.begin(); it != clustered_pools.end(); it++) {
        
        Pool& p = *it;
        
        for (size_t i = 0; i < p.size(); i++) {
            int id = p[i];
            found[id] = true;
            if (DEBUG)
                cerr << ":found iworm: " << id << " in cluster." << endl;
        }
    }
    

    // add in the missing entries
    for (size_t i = 0; i < dna.size(); i++) {
        if (found.find(i) == found.end()) {
            Pool p;
            p.add(i);
            clustered_pools.push_back(p);
            if (DEBUG)
                cerr << "-iworm: " << i << " added as unclustered." << endl;
        }
    }
    
}


int main(int argc,char** argv)
{
  int rank,numranks;
#ifdef MPI_ENABLED
  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numranks);
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
#else
  rank=0;
  numranks=1;
#endif


 
    
    commandArg<string> aStringCmmd("-i","input fasta");
    commandArg<string> readStringCmmd("-r","read fasta");
    commandArg<bool> strandCmmd("-strand","strand specific", false);
    commandArg<int> kCmmd("-k","k-mer size for pooling", 24);
    commandArg<int> kkCmmd("-kk","k-mer size for welding", 48);
    commandArg<int> threadsCmmd("-t", "number of threads (default: use OMP_NUM_THREADS env var)", -1);
    //commandArg<string> bStringCmmd("-o","output fasta");
    commandArg<string> scaffStringCmmd("-scaffolding", "scaffolded pairs of inchworm contigs", "");
    commandArg<double> glueFactorCmmd("-glue_factor", "fraction of max (iworm pair coverage) for read glue support", 0.05);
    commandArg<int> minGlueCmmd("-min_glue", "absolute min glue support required", 2);
    commandArg<double> minIsoRatioCmmd("-min_iso_ratio", "min ratio of (iworm pair coverage) for join", 0.05);
    commandArg<double> minKmerEntropyCmmd("-min_kmer_entropy", "min entropy value for kmers", MIN_KMER_ENTROPY); 
    commandArg<double> minWeldEntropyCmmd("-min_weld_entropy", "min entropy value for each half of a welding-(kk)mers", MIN_WELD_ENTROPY); 
    commandArg<double> maxRatioInternalRepeatCmmd("-max_ratio_internal_repeat", "maximum ratio identical bases in intra-kmer comparisons", MAX_RATIO_INTERNALLY_REPETITIVE);
    
    commandArg<bool> reportWeldsCmmd("-report_welds", "report the welding kmers", false);
    commandArg<bool> debugCmmd("-debug", "verbosely describes operations", false);
    commandArg<bool> noWeldsCmmd("-no_welds", "disable requirement for glue support", false);
    commandArg<int>  maxClusterSizeCmmd("-max_cluster_size", "max size for an inchworm cluster", MAX_CLUSTER_SIZE);
    commandArg<int>  minContigLengthCmmd("-min_contig_length", "min sum cluster contig length", MIN_CONTIG_LENGTH);

    commandLineParser P(argc,argv);
    P.SetDescription("Makes a graph out of a fasta");
    P.registerArg(aStringCmmd);
    P.registerArg(readStringCmmd);
    P.registerArg(strandCmmd);
    P.registerArg(kCmmd);
    P.registerArg(kkCmmd);
    P.registerArg(threadsCmmd);
    P.registerArg(scaffStringCmmd);
    P.registerArg(glueFactorCmmd);
    P.registerArg(minGlueCmmd);
    P.registerArg(minIsoRatioCmmd);
    P.registerArg(maxClusterSizeCmmd);
    
    P.registerArg(minKmerEntropyCmmd);
    P.registerArg(minWeldEntropyCmmd);

    P.registerArg(maxRatioInternalRepeatCmmd);
    P.registerArg(reportWeldsCmmd);
    P.registerArg(noWeldsCmmd);
    P.registerArg(minContigLengthCmmd);

    P.registerArg(debugCmmd);
    
    //P.registerArg(bStringCmmd);
    
    P.parse();
    
    cerr << "-----------------------------------------" << endl
         << "--- Chrysalis: GraphFromFasta -----------" << endl
         << "-- (cluster related inchworm contigs) ---" << endl
         << "-----------------------------------------" << endl << endl;
    


    string aString = P.GetStringValueFor(aStringCmmd); //inchworm contigs file
    bool sStrand = P.GetBoolValueFor(strandCmmd); // indicates strand-specific mode
    string readString = P.GetStringValueFor(readStringCmmd); // rna-seq reads file (strand-oriented if in strand-specific mode
    int k = P.GetIntValueFor(kCmmd); // kmer size for pooling, must be 24
    int kk = P.GetIntValueFor(kkCmmd); // kmer size for welding, default 48 = kmer + 1/2 kmer on each side of the kmer
    int num_threads = P.GetIntValueFor(threadsCmmd);
    REPORT_WELDS = P.GetBoolValueFor(reportWeldsCmmd);
    double glue_factor = P.GetDoubleValueFor(glueFactorCmmd);
    int min_glue_required = P.GetIntValueFor(minGlueCmmd);
    double min_iso_ratio = P.GetDoubleValueFor(minIsoRatioCmmd);
    bool bNoWeld = P.GetBoolValueFor(noWeldsCmmd);
    string scaffolding_filename = P.GetStringValueFor(scaffStringCmmd);
    
    MIN_KMER_ENTROPY = P.GetDoubleValueFor(minKmerEntropyCmmd);
    MIN_WELD_ENTROPY = P.GetDoubleValueFor(minWeldEntropyCmmd);
    MAX_RATIO_INTERNALLY_REPETITIVE = P.GetDoubleValueFor(maxRatioInternalRepeatCmmd);
    MAX_CLUSTER_SIZE = P.GetIntValueFor(maxClusterSizeCmmd);
    MIN_CONTIG_LENGTH = P.GetIntValueFor(minContigLengthCmmd);
    
    DEBUG = P.GetBoolValueFor(debugCmmd);

    if (min_glue_required < 1) {
        cerr << "-error, cannot have less than 1 read as glue.  Setting -min_glue to 1" << endl;
        min_glue_required = 1;
    }
    

    if (num_threads > 0) {
        cerr << "-setting num threads to: " << num_threads << endl;
        omp_set_num_threads(num_threads);
    }

    int real_num_threads;
    #pragma omp parallel
    {
        #pragma omp single
        {
            real_num_threads = omp_get_num_threads();
        }
    }
    cerr << "-running on " << real_num_threads << " threads" << endl;
    
    // read inchworm contigs into memory
    vecDNAVector dna;
    cerr << "GraphFromFasta: Reading file: " << aString << endl;
    dna.Read(aString, false, false, true, 1000000);
    cerr << "done!" << endl;
    
        
    size_t i, j; //TODO: remove this, use local vars instead
    
    if (k != 24) {
        cerr << "The only size of k supported is 24! Exiting!" << endl;
        return -1;
    }
        
        
    vecDNAVector crossover;
    
    NonRedKmerTable kmers(kk);
    Welder weld(k, kk); // decides if read support exists to weld two inchworm contigs together into the same component.


    int iworm_counter = 0;
    int total_iworm_contigs = dna.size();
    
    
    map<int,Pool> weld_reinforced_iworm_clusters;
    map<int,bool> toasted;
    
    if (scaffolding_filename.length() > 0) {
        // add scaffolding info to clusters
        add_scaffolds_to_clusters(weld_reinforced_iworm_clusters, scaffolding_filename, dna, min_glue_required, glue_factor);
    }

#ifdef MPI_ENABLED
    MPI_Barrier(MPI_COMM_WORLD);
#endif
    if (! bNoWeld) { // make this a function
        
        
                
        // decode inchworm contigs into kmer composition
        KmerAlignCore core;
        core.SetNumTables(2);
        TranslateBasesToNumberExact trans;
        core.SetTranslator(&trans);
        core.AddData(dna);
        core.SortAll();
            
        cerr << "Phase 1: Collecting candidate weldmers between iworm contig pairs sharing k-1mers" << endl;
        

        Welder weld(k, kk); // decides if read support exists to weld two inchworm contigs together into the same component.
        
#ifdef MPI_ENABLED
        int schedule_chunksize = dna.size() / (numranks) / 100;
#else
	int schedule_chunksize = dna.size() / (real_num_threads) / 100; 
#endif


        if(schedule_chunksize<1) schedule_chunksize=1;
        cerr << "-setting omp for schedule chunksize to " << schedule_chunksize << " for " << dna.size() << " iworm contigs" << endl;

        int counter = 0;
        
        double start_time = omp_get_wtime();
        
        iworm_counter = 0;


	int rank_chunksize;
	
	timer_start();

	int thread_chunksize=schedule_chunksize /(real_num_threads) / 10;
	int thread_count;

#ifdef MPI_ENABLED
	 for (int mpi_count=rank*schedule_chunksize; mpi_count<dna.isize(); mpi_count+=numranks*schedule_chunksize) {
	  if( (dna.isize()-mpi_count)<schedule_chunksize)
	    rank_chunksize=dna.isize()-mpi_count;
	  else
	    rank_chunksize=schedule_chunksize;

#pragma omp parallel for schedule(static) private(i,j,thread_count)
	  for(thread_count=0;thread_count<rank_chunksize;thread_count++){

	    i=mpi_count + thread_count;


	    DNAVector & d = dna[i]; // inchworm contig [i]
            
            #pragma omp atomic
            iworm_counter++;

            if (iworm_counter % 1000 == 0 || iworm_counter == dna.isize()-1) {
            #pragma omp critical
	      cerr << "\rProcessed: " << iworm_counter/(double)dna.isize()*100 << " % of iworm contigs on rank "<<rank   ;
            }
            
            for (j=0; j<=d.isize()-k; j++) {
                DNAVector sub; // a kmer
                sub.SetToSubOf(d, j, k);
            
                if (IsSimple(sub))
                    continue; // ignore kmers that are low complexity
            

                // find other inchworm contigs that have kmer matches
                svec<KmerAlignCoreRecord> matchesFW, matchesRC;   
            
                core.GetMatches(matchesFW, sub);
                if (!sStrand) {
                    sub.ReverseComplement();
                    core.GetMatches(matchesRC, sub);
                }
            
                int x;
            
                for (x=0; x<matchesFW.isize(); x++) {
                    int c = matchesFW[x].GetContig();
                    if (c == i)
                        continue;  // matches itself
                
                    DNAVector & dd = dna[c];        
                    int start = matchesFW[x].GetPosition();
                
                    if (DEBUG) {
                        cerr << "(Phase1: kmer match between " << dna.Name(i) << " and " << dna.Name(c)
                             << " at F positions: " << j << " and " << start << endl;
                    } 

                    DNAVector add;
                
                    weld.WeldableKmer(add, d, j, dd, start);
                    if (add.isize() > 0 && ! IsSimple(add) && (! SimpleHalves(add) )) {
                        Add(crossover, add, counter);      
                    }
                    weld.WeldableKmer(add, dd, start, d, j);
                    if (add.isize() > 0 && (! IsSimple(add)) && (! SimpleHalves(add) )) {
                        Add(crossover, add, counter);
                    }
                }
                for (x=0; x<matchesRC.isize(); x++) {
                    int c = matchesRC[x].GetContig();
                    if (c == i)
                        continue;
                    DNAVector dd = dna[c];
                    dd.ReverseComplement();
                
                    int start = dd.isize() - matchesRC[x].GetPosition() - k;
                    DNAVector add;
                
                    weld.WeldableKmer(add, d, j, dd, start); 
                    if (add.isize() > 0 && (! IsSimple(add)) && (! SimpleHalves(add)))
                        Add(crossover, add, counter);        
                
                
                    weld.WeldableKmer(add, dd, start, d, j);
                    if (add.isize() > 0 && ! IsSimple(add) && (! SimpleHalves(add)))
                        Add(crossover, add, counter);        
                }
            }
	  }
	 }
#else
#pragma omp parallel for schedule(dynamic, schedule_chunksize) private(j)
        for (i=0; i<dna.size(); i++) {
            DNAVector & d = dna[i]; // inchworm contig [i]
            
            #pragma omp atomic
            iworm_counter++;

            if (iworm_counter % 1000 == 0 || iworm_counter == dna.size()-1) {
            #pragma omp critical
                cerr << "\rProcessed: " << iworm_counter/(double)dna.size()*100 << " % of iworm contigs.    ";
            }
            
            for (j=0; j<=d.isize()-k; j++) {
                DNAVector sub; // a kmer
                sub.SetToSubOf(d, j, k);
            
                if (IsSimple(sub))
                    continue; // ignore kmers that are low complexity
            

                // find other inchworm contigs that have kmer matches
                svec<KmerAlignCoreRecord> matchesFW, matchesRC;   
            
                core.GetMatches(matchesFW, sub);
                if (!sStrand) {
                    sub.ReverseComplement();
                    core.GetMatches(matchesRC, sub);
                }
            
                int x;
            
                for (x=0; x<matchesFW.isize(); x++) {
                    int c = matchesFW[x].GetContig();
                    if (c == i)
                        continue;  // matches itself
                
                    DNAVector & dd = dna[c];        
                    int start = matchesFW[x].GetPosition();
                
                    if (DEBUG) {
                        cerr << "(Phase1: kmer match between " << dna.Name(i) << " and " << dna.Name(c)
                             << " at F positions: " << j << " and " << start << endl;
                    } 

                    DNAVector add;
                
                    weld.WeldableKmer(add, d, j, dd, start);
                    if (add.isize() > 0 && ! IsSimple(add) && (! SimpleHalves(add) )) {
                        Add(crossover, add, counter);      
                    }
                    weld.WeldableKmer(add, dd, start, d, j);
                    if (add.isize() > 0 && (! IsSimple(add)) && (! SimpleHalves(add) )) {
                        Add(crossover, add, counter);
                    }
                }
                for (x=0; x<matchesRC.isize(); x++) {
                    int c = matchesRC[x].GetContig();
                    if (c == i)
                        continue;
                    DNAVector dd = dna[c];
                    dd.ReverseComplement();
                
                    int start = dd.isize() - matchesRC[x].GetPosition() - k;
                    DNAVector add;
                
                    weld.WeldableKmer(add, d, j, dd, start); 
                    if (add.isize() > 0 && (! IsSimple(add)) && (! SimpleHalves(add)))
                        Add(crossover, add, counter);        
                
                
                    weld.WeldableKmer(add, dd, start, d, j);
                    if (add.isize() > 0 && ! IsSimple(add) && (! SimpleHalves(add)))
                        Add(crossover, add, counter);        
                }
            }
        }


#endif        
	 
	 
        double end_time = omp_get_wtime();
        cerr << endl << endl << "...done Phase 1. (" << end_time - start_time << " seconds)" << endl;

	cerr<< endl << endl << "done Phase 1 with my timers "<<timer_stop()<<" on rank "<<rank<<" (in milliseconds)"<<endl;
	cerr<<"Crossover size on rank "<<rank<<" is "<<crossover.size()<<std::endl;
        
        //crossover.resize(counter);
        
        
	timer_start();
#ifdef MPI_ENABLED
	


	char* combined_dnas_chars;

#ifdef HUGE_DATASET
	long* dnachars_counts, *crossover_counts;
	dnachars_counts = (long*)malloc(sizeof(long)*numranks);
	long crossover_size = crossover.isize();
	crossover_counts = (long*)malloc(sizeof(long)*numranks);
#else
	int* dnachars_counts, *crossover_counts;
	dnachars_counts = (int*)malloc(sizeof(int)*numranks);
	int crossover_size = crossover.isize();
	crossover_counts = (int*)malloc(sizeof(int)*numranks);
#endif


	  


	

	  



	 
	
	  DNAVector combined_dnavector;
	  for(int count=0;count<crossover.isize();count++)
	      combined_dnavector +=crossover[count];

#ifdef HUGE_DATASET
	  long dnachars_size = combined_dnavector.size();
#else
	  int dnachars_size = combined_dnavector.size();
#endif
	

	  cerr<<std::endl<<"Total dna chars  "<<combined_dnavector.size()<<" on rank "<<rank<<" with a total memory of "<<((double)(combined_dnavector.size()*sizeof(char))/(double)(1024*1024))<<" (MB)"<<std::endl;

	  char* dnas_chars_send;
	  svec<char>dnadata = combined_dnavector.Data();

	  dnas_chars_send = (char*)malloc(sizeof(char)*combined_dnavector.size());
	  for(long count=0;count<combined_dnavector.size();count++)
	    dnas_chars_send[count] = dnadata[count];
	    



	
#ifdef HUGE_DATASET
	  MPI_Allgather(&crossover_size,1,MPI_LONG,crossover_counts,1,MPI_LONG,MPI_COMM_WORLD);
          MPI_Allgather(&dnachars_size, 1,MPI_LONG,dnachars_counts, 1,MPI_LONG,MPI_COMM_WORLD);	  
#else
	  MPI_Allgather(&crossover_size,1,MPI_INT,crossover_counts,1,MPI_INT,MPI_COMM_WORLD);
          MPI_Allgather(&dnachars_size, 1,MPI_INT,dnachars_counts, 1,MPI_INT,MPI_COMM_WORLD);

#endif


	  

	
	  long long combined_crossover_counts=0;

	    for(int count=0;count<numranks;count++)
	      combined_crossover_counts += crossover_counts[count];
	  
	    long long combined_dnachars_counts=0;

	    for(int count=0;count<numranks;count++)
	      {
		if(rank==0)
		  cerr<<"DNA["<<count<<"]="<<dnachars_counts<<endl;
		combined_dnachars_counts += dnachars_counts[count];

	      }


	
	    cerr<<std::endl<<"Combined DNA characters to be exchanged "<<combined_dnachars_counts<<" on rank "<<rank<<" with a total memory of "<<((double)(combined_dnachars_counts*sizeof(char))/(double)(1024*1024))<<" (MB)"<<std::endl;

	    MPI_Barrier(MPI_COMM_WORLD);
	    combined_dnas_chars = (char*)malloc(sizeof(char)*combined_dnachars_counts);
	   


#ifdef HUGE_DATASET
	       long* dnachar_displs;
	       dnachar_displs = (long*)malloc(sizeof(long)*numranks);
#else
	       int *dnachar_displs;
	       dnachar_displs = (int*)malloc(sizeof(int)*numranks);
#endif


	    
	      dnachar_displs[0]=0;
	       for(int count=1;count<numranks;count++)
		 dnachar_displs[count] = dnachar_displs[count-1]+dnachars_counts[count-1];

	       cerr<<"Allocated memory for all DNA characters on rank "<<rank<<std::endl;

	       
#ifdef HUGE_DATASET
	    
	     
	       for(long count=0;count<dnachars_counts[rank];count++)
		 combined_dnas_chars[dnachar_displs[rank]+count]=dnas_chars_send[count];
	       
	       for(int count=0;count<numranks;count++)
		 {
		   void* buffer = &combined_dnas_chars[dnachar_displs[count]];
		   MPI_Bcast(buffer,dnachars_counts[count],MPI_CHAR,count,MPI_COMM_WORLD);
		   
		 }
	       

#else
	       MPI_Allgatherv(dnas_chars_send, dnachars_size, MPI_CHAR,  combined_dnas_chars, dnachars_counts, dnachar_displs,MPI_CHAR,MPI_COMM_WORLD);
#endif

	  free(dnas_chars_send);
	  

	  vecDNAVector combined_crossover;

	      int t=0;
	       for(long long count=0;count<combined_crossover_counts;count++)
		 { 

		   const string s(&combined_dnas_chars[t],kk);

		   //	   cerr<<"string = "<<s<<std::endl;
		   DNAVector dna_sequence;
		   dna_sequence.SetFromBases(s);
		   combined_crossover.push_back(dna_sequence);

		   t = t + kk;

		 }
	      
	       cerr<<"Subsequence on rank "<<rank<<" in beginning of combined DNA characters (8) = ";
	       for(int count=0;count<8;count++)
		 cerr<<combined_dnas_chars[count];

	       cerr<<std::endl;

	    
	        cerr<<"Subsequence on rank "<<rank<<" in end of combined DNA characters (8) = ";
	       for(int count=0;count<8;count++)
		 cerr<<combined_dnas_chars[combined_dnachars_counts-count];

	       cerr<<std::endl;


	       cerr<<"Size of combined crossover = "<<combined_crossover.isize()<<endl;
	       free(combined_dnas_chars);
	       free(crossover_counts);
	       free(dnachar_displs);
	       free(dnachars_counts);


	       
	       
	



#endif

	  if(rank==0)
	    cerr<< endl << endl << " Overhead of MPI steps  "<<timer_stop()<<" (in milliseconds)"<<endl;
 
        //-------------------------------------------------------------------------
        //------------  Now, do iworm clustering using welds ----------------------
        //-------------------------------------------------------------------------
#ifdef MPI_ENABLED        
        kmers.SetUp(combined_crossover);
#else
        kmers.SetUp(crossover);
#endif


        //cerr << "done setting up sorting structure." << endl;
    
        cerr << "Setting up reads for streaming..." << endl;
        DNAStringStreamFast seq;
        seq.ReadStream(readString);  //readString is the name of the file containing the reads
        
        cerr << "Identifying reads that support welding of iworm contigs..."<<endl;
	
        kmers.AddData(seq);
        cerr << endl << "Done!" << endl;
        
        weld.SetTable(&kmers);

#ifdef MPI_ENABLED
        MPI_Barrier(MPI_COMM_WORLD);
#endif

	start_time = omp_get_wtime();
        timer_start();
    
        //=================================================================
        // Cluster the inchworm contigs based on read-weld support
if(rank==0)
        cerr << "Phase 2: Reclustering iworm contigs using welds."  << endl;

#ifdef MPI_ENABLED

	svec<int> ind_leftFW;
	svec<int> ind_rightFW;
	svec<int> ind_leftRC;
	svec<int> ind_rightRC;	
	svec<int> ind_toasted;

	iworm_counter = 0; // reset 

         for (int mpi_count=rank*schedule_chunksize; mpi_count<dna.isize(); mpi_count+=numranks*schedule_chunksize) {
          if( (dna.isize()-mpi_count)<schedule_chunksize)
            rank_chunksize=dna.isize()-mpi_count;
          else
            rank_chunksize=schedule_chunksize;

#pragma omp parallel for schedule(static) private(j, thread_count,i)
          for(thread_count=0;thread_count<rank_chunksize;thread_count++) {

            i=mpi_count + thread_count;

	    #pragma omp atomic
            iworm_counter++;

            if (i % 100 == 0) {
                #pragma omp critical
                cerr << "\r[" << (iworm_counter/(float)dna.isize()*100) << "% done]                ";
            }	    
    
	    int cutoff = 0;
            DNAVector & d = dna[i];
            if (d.isize() < k) {

                if (DEBUG) {
                    cerr << "ignoring: " << dna.Name(i) << " since less than length: " << k << endl;
                }

                continue;
            }

	   // iterate through kmers of inchworm contig
	    for (j=0; j<=d.isize()-k; j++) {

                DNAVector sub;
                sub.SetToSubOf(d, j, k);

                svec<KmerAlignCoreRecord> matchesFW, matchesRC;

                core.GetMatches(matchesFW, sub);
                if (!sStrand) {
                    sub.ReverseComplement();
                    core.GetMatches(matchesRC, sub);
                }

                if (IsSimple(sub) && matchesFW.isize() + matchesRC.isize() > 1) {
                    if (DEBUG) {
                        cerr << "kmer: " << sub.AsString() << " ignored since either low complex and too many iworm matches"<< endl;
                    }

                    continue;
                }

                int x;

                double coverage = Coverage(dna.Name(i));

		// Process forward matching kmers
		 for (x=0; x<matchesFW.isize(); x++) {
                    int c = matchesFW[x].GetContig();

                    if (c == i) {
                        continue;
                    }


                    if (DEBUG) {
                        cerr << "kmer: " << sub.AsString() << " supports match between " << dna.Name(i) << " and " << dna.Name(c) << endl;
                    }


                    double coverage_other = Coverage(dna.Name(c));


                    double higher_coverage_val = (coverage > coverage_other) ? coverage : coverage_other;
                    int minCov = (int) (higher_coverage_val * glue_factor);
                    if (minCov < min_glue_required) {
                        minCov = min_glue_required;
                    }

                    DNAVector & dd = dna[c];
                    int start = matchesFW[x].GetPosition();


                    if (DEBUG) {
                        cerr << "Phase2: kmer match between " << dna.Name(i) << " and " << dna.Name(c)
                             << " at F positions: " << j << " and " << start << endl;
                    }


		    //cout << "TEST_WELD: " << I << " " << J << " ";
		     string welding_kmer;
                    int welding_kmer_read_count;
                    if (!bNoWeld
                    &&
                        !(weld.Weldable(d, j, dd, start, minCov, welding_kmer, welding_kmer_read_count)
                      ||
                          weld.Weldable(dd, start, d, j, minCov, welding_kmer, welding_kmer_read_count))) {

                        if (DEBUG) {
                            cerr << "\t no welding kmer avail " << welding_kmer << ", count: " << welding_kmer_read_count
                                 << ", min count required: " << minCov << endl;
                        }

                        continue;
                    }

                    if (IsShadow(d, dd, j, start, k) && coverage > 2*coverage_other) {
                        cerr << "Toasting shadow: " << dna.Name(c) << endl;

                        #pragma omp critical
                    //    toasted[c] = true;
                          ind_toasted.push_back(c);

                        continue;

                    } else if (!IsGoodCoverage(coverage, coverage_other, min_iso_ratio)) {
			//cerr << "Rejecting fw merge between " << dna.Name(i);
			//cerr << " and " << dna.Name(c) << endl;
			
			 continue;
                    }
		    // cerr << "Accept (fw)!!" << endl;

		    #pragma omp critical
                    {

                      //  add_reciprocal_iworm_link(weld_reinforced_iworm_clusters, i, c);
			ind_leftFW.push_back(i);
			ind_rightFW.push_back(c);
                    }

                    if (REPORT_WELDS) {
                    #pragma omp critical
                        cout << "#Welding: " << dna.Name(i) << " to " << dna.Name(c)
                             << " with " << welding_kmer << " found in " << welding_kmer_read_count << " reads" << endl;
                    }


                }

                if (sStrand) {
			// only doing the forward matches

		    if (DEBUG) {
                        cerr << " only procesing forward strand " << endl;
                    }

                    continue;   // superfluous since we didn't capture any rc matches
		}

		// Process the RC matches now
		for (x=0; x<matchesRC.isize(); x++) {
                    int c = matchesRC[x].GetContig();


                    if (c == i) {
			// ignore self matches
			continue;
                    }
		    if (DEBUG) {
                        cerr << "RCkmer: " << sub.AsString() << " supports match between " << dna.Name(i) << " and " << dna.Name(c) << endl;
                    }



                    double coverage_other = Coverage(dna.Name(c));

                    double higher_coverage_val = (coverage > coverage_other) ? coverage : coverage_other;
                    int minCov = (int) (higher_coverage_val * glue_factor);
                    if (minCov < min_glue_required) {
                        minCov = min_glue_required;
                    }

                    DNAVector dd = dna[c];
                    dd.ReverseComplement(); // revcomp a copy of the iworm[c] sequenc

		    int start = dd.isize() - matchesRC[x].GetPosition() - k; // reverse-complement the match coordinate

		    if (DEBUG) {
                        cerr << "kmer match between " << dna.Name(i) << " and " << dna.Name(c)
                             << " at R positions: " << j << " and " << start << endl;
                    }


                    string welding_kmer;
                    int welding_kmer_read_count;
                    if (!bNoWeld
                    &&
                        !(weld.Weldable(d, j, dd, start, minCov, welding_kmer, welding_kmer_read_count)
                      ||
                          weld.Weldable(dd, start, d, j, minCov, welding_kmer, welding_kmer_read_count))) {


                        if (DEBUG) {
                            cerr << "\t no welding kmer avail " << welding_kmer << ", count: " << welding_kmer_read_count
                                 << ", min count required: " << minCov << endl;
                        }

                        continue;
                    }

                    if (IsShadow(d, dd, j, start, k) && coverage > 2*coverage_other) {
                        cerr << "Toasting shadow: " << dna.Name(c) << endl;

                        #pragma omp critical
                        // toasted[c] = true;
			ind_toasted.push_back(c);

                        continue;

                    } else if (!IsGoodCoverage(coverage, coverage_other, min_iso_ratio)) {
			// cerr << "Rejecting rc merge between " << dna.Name(i);
			// cerr << " and " << dna.Name(c) << endl;
			continue;
                    } else if (!IsGoodCoverage(coverage, coverage_other, min_iso_ratio)) {
			// cerr << "Rejecting rc merge between " << dna.Name(i);
			// cerr << " and " << dna.Name(c) << endl;
			continue;
                    }
			//cerr << "Accept (rc)!!" << endl;


		   #pragma omp critical
                    {

                      //  add_reciprocal_iworm_link(weld_reinforced_iworm_clusters, i, c);
			ind_leftRC.push_back(i);
			ind_rightRC.push_back(c);
                    }

                    if (REPORT_WELDS) {
                    #pragma omp critical
                        cout << "#Welding: " << dna.Name(i) << " to " << dna.Name(c)
                             << " with " << welding_kmer << " found in " << welding_kmer_read_count << " reads" << endl;
                    }

		   //cout << "Mapped sequence " << dna.NameClean(c) << " to pool " << mapped[c] << " -" << endl;
	       }

            }

        }
	}


	end_time = omp_get_wtime();
	cerr << endl << endl << "...done Phase 2 on rank "<< rank << " (" << end_time - start_time << " seconds)" << endl;

	cerr<<"\nPhase 2 took "<<timer_stop()<<" on rank "<<rank<<" (in milliseconds)"<<endl;
        cerr << endl; // end of progress monitoring.


        MPI_Barrier(MPI_COMM_WORLD); 
  	timer_start();

	int* all_indexFW_sizes;
	int* all_indexRC_sizes;
	int* all_IndexFW_left;
	int* all_IndexFW_right;
	int* all_IndexRC_left;
	int* all_IndexRC_right;
	int* leftFW;
	int* rightFW;
	int* leftRC;
	int* rightRC;
	int numFW, numRC, numToasted;
	int* Ind_Toasted;
	int* all_Toasted_sizes;
	int* all_Index_Toasted;

	numToasted = (int)ind_toasted.size();
	Ind_Toasted = (int*)malloc(sizeof(int)*numToasted);
	for(int count=0; count<numToasted; count++)
		Ind_Toasted[count] = ind_toasted[count];	

	numFW = (int)ind_leftFW.size();
	if(!sStrand)	
	   numRC = (int)ind_leftRC.size();

	leftFW = (int*)malloc(sizeof(int)*numFW);
	rightFW = (int*)malloc(sizeof(int)*numFW);
	for(int count=0; count<numFW; count++) {
		leftFW[count]  = ind_leftFW[count];
		rightFW[count] = ind_rightFW[count]; 
	}

	if (!sStrand) {
	  leftRC = (int*)malloc(sizeof(int)*numRC);
          rightRC = (int*)malloc(sizeof(int)*numRC);
          for(int count=0; count<numRC; count++) {
                leftRC[count]  = ind_leftRC[count];
                rightRC[count] = ind_rightRC[count];
       	  } 
	}


	all_indexFW_sizes = (int*)malloc(sizeof(int)*numranks); 
	if (!sStrand) 
	  all_indexRC_sizes = (int*)malloc(sizeof(int)*numranks);

	all_Toasted_sizes =  (int*)malloc(sizeof(int)*numranks);


	MPI_Allgather(&numFW,1,MPI_INT,all_indexFW_sizes,1,MPI_INT,MPI_COMM_WORLD);
	if (!sStrand)
	   MPI_Allgather(&numRC,1,MPI_INT,all_indexRC_sizes,1,MPI_INT,MPI_COMM_WORLD);

	MPI_Allgather(&numToasted,1,MPI_INT,all_Toasted_sizes,1,MPI_INT,MPI_COMM_WORLD);


	int total_indexFW = 0;
	int total_indexRC = 0;
	int total_Toasted = 0;
        for(int count=0;count<numranks;count++)
		total_indexFW += all_indexFW_sizes[count];

         if (!sStrand) {
            for(int count=0;count<numranks;count++)
                total_indexRC += all_indexRC_sizes[count];
	   }

	   for(int count=0;count<numranks;count++)
		total_Toasted += all_Toasted_sizes[count];


	int* FW_displs;
	int* RC_displs;
	int* Toasted_displs;

		Toasted_displs = (int*)malloc(sizeof(int)*numranks);
		Toasted_displs[0]=0;
		for(int count=1;count<numranks;count++)
		   Toasted_displs[count] = Toasted_displs[count-1] + all_Toasted_sizes[count-1]; 


		FW_displs = (int*)malloc(sizeof(int)*numranks);

		FW_displs[0] = 0;
		for(int count=1;count<numranks;count++) 
		   FW_displs[count] =  FW_displs[count-1] + all_indexFW_sizes[count-1];

		all_IndexFW_left  = (int*)malloc(sizeof(int)*total_indexFW);
		all_IndexFW_right = (int*)malloc(sizeof(int)*total_indexFW);
		all_Index_Toasted = (int*)malloc(sizeof(int)*total_Toasted); 

		MPI_Allgatherv(leftFW, numFW,MPI_INT,all_IndexFW_left ,all_indexFW_sizes, FW_displs, MPI_INT,MPI_COMM_WORLD);
		MPI_Allgatherv(rightFW,numFW,MPI_INT,all_IndexFW_right,all_indexFW_sizes, FW_displs, MPI_INT,MPI_COMM_WORLD);
		MPI_Allgatherv(Ind_Toasted,numToasted,MPI_INT,all_Index_Toasted,all_Toasted_sizes, Toasted_displs, MPI_INT,MPI_COMM_WORLD);

		for(int count=0;count<total_indexFW;count++)
                        add_reciprocal_iworm_link(weld_reinforced_iworm_clusters, all_IndexFW_left[count], all_IndexFW_right[count] );

		for(int count=0; count<total_Toasted; count++) toasted[ all_Index_Toasted[count] ] = true;	

		free(FW_displs);
		free(Toasted_displs);
		free(all_indexFW_sizes);
		free(all_Toasted_sizes);
		free(all_IndexFW_left);
		free(all_IndexFW_right);		
		free(all_Index_Toasted);
		free(leftFW);
		free(rightFW);
		free(Ind_Toasted);

		if (!sStrand) {
		  RC_displs = (int*)malloc(sizeof(int)*numranks);
		  RC_displs[0] = 0;
		  for(int count=1;count<numranks;count++)
			RC_displs[count] =  RC_displs[count-1] + all_indexRC_sizes[count-1];

		
		  all_IndexRC_left  = (int*)malloc(sizeof(int)*total_indexRC);
                  all_IndexRC_right = (int*)malloc(sizeof(int)*total_indexRC);	

		  MPI_Allgatherv(leftRC, numRC,MPI_INT,all_IndexRC_left ,all_indexRC_sizes, RC_displs, MPI_INT,MPI_COMM_WORLD);
                  MPI_Allgatherv(rightRC,numRC,MPI_INT,all_IndexRC_right,all_indexRC_sizes, RC_displs, MPI_INT,MPI_COMM_WORLD);


		  for(int count=0;count<total_indexRC;count++)
                        add_reciprocal_iworm_link(weld_reinforced_iworm_clusters, all_IndexRC_left[count], all_IndexRC_right[count] );	

		  free(RC_displs);
		  free(all_indexRC_sizes);
		  free(all_IndexRC_left);
		  free(all_IndexRC_right);
		  free(leftRC);
		  free(rightRC);

		}

	   cerr<< endl << endl << " Overhead of MPI steps  "<<timer_stop()<<" (in milliseconds)"<<endl;	


	   svec<Pool> clustered_pools = bubble_up_cluster_growth(weld_reinforced_iworm_clusters, toasted);

	    if (DEBUG) {
        	cerr << "Final pool description: " << endl;
        	describe_poolings(clustered_pools);
    		}

    		add_unclustered_iworm_contigs(clustered_pools, dna);

	MPI_Barrier(MPI_COMM_WORLD);

	    int component_count = 0;

    	  for (i=0; i<clustered_pools.isize(); i++) {
        	Pool & p = clustered_pools[i];

        	if (p.size() == 0) { continue; }

        	p.sortvec();

        	int sum_iworm_length = 0;
        	for (unsigned int j = 0; j < p.size(); j++) {
            		int z = p.get(j);
            		sum_iworm_length += dna[z].isize();
        	}

        	if (sum_iworm_length < MIN_CONTIG_LENGTH) {
            		continue;
        	}

        	stringstream pool_info;
	if(rank==0) {
        	pool_info << "#POOL_INFO\t" << component_count << ":" << "\t";

		cout << "COMPONENT " << component_count << "\t" << p.size() << endl;
        	for (unsigned int j = 0; j < p.size(); j++) {
            		int z = p.get(j);

            		pool_info << z << " ";
            		cout << ">Component_" << component_count << " " << p.size() << " " << z << " [iworm" << dna.Name(z) << "]" << endl;
            		PrintSeq(dna[z]);
        	}
        	pool_info << endl;
          
		cout << pool_info.str();

        	cout << "END" << endl;
	   }
        	component_count++;

    	  }


#else   

        #pragma omp parallel for schedule(dynamic, schedule_chunksize) private(j)
        for (i=0; i<dna.size(); i++) {
            

            #pragma omp atomic
            iworm_counter++;
            
            if (i % 100 == 0) {
                #pragma omp critical
                cerr << "\r[" << (iworm_counter/(float)dna.size()*100) << "% done]                ";
            }
            
            
            int cutoff = 0;
            DNAVector & d = dna[i];
            if (d.isize() < k) {
            
                if (DEBUG) {
                    cerr << "ignoring: " << dna.Name(i) << " since less than length: " << k << endl;
                }
                
                continue;
            }
                            

            // iterate through kmers of inchworm contig
            for (j=0; j<=d.isize()-k; j++) {
            
                DNAVector sub;
                sub.SetToSubOf(d, j, k);
            
                svec<KmerAlignCoreRecord> matchesFW, matchesRC;   
            
                core.GetMatches(matchesFW, sub);
                if (!sStrand) {
                    sub.ReverseComplement();
                    core.GetMatches(matchesRC, sub);
                }
            
                if (IsSimple(sub) && matchesFW.isize() + matchesRC.isize() > 1) {
                    if (DEBUG) {
                        cerr << "kmer: " << sub.AsString() << " ignored since either low complex and too many iworm matches"<< endl;
                    }

                    continue;      
                }
            
                int x;
            
                double coverage = Coverage(dna.Name(i));
                        
                // Process forward matching kmers
                for (x=0; x<matchesFW.isize(); x++) {
                    int c = matchesFW[x].GetContig();
                    
                    if (c == i) {
                        continue;
                    }
                    
                    
                    if (DEBUG) {
                        cerr << "kmer: " << sub.AsString() << " supports match between " << dna.Name(i) << " and " << dna.Name(c) << endl;
                    }

                
                    double coverage_other = Coverage(dna.Name(c));
                
    
                    double higher_coverage_val = (coverage > coverage_other) ? coverage : coverage_other;
                    int minCov = (int) (higher_coverage_val * glue_factor);
                    if (minCov < min_glue_required) {
                        minCov = min_glue_required;
                    }
                
                    DNAVector & dd = dna[c];        
                    int start = matchesFW[x].GetPosition();
                
                
                    if (DEBUG) {
                        cerr << "Phase2: kmer match between " << dna.Name(i) << " and " << dna.Name(c)
                             << " at F positions: " << j << " and " << start << endl;
                    } 
                
                
                    //cout << "TEST_WELD: " << I << " " << J << " ";
                    string welding_kmer;
                    int welding_kmer_read_count;
                    if (!bNoWeld 
                    && 
                        !(weld.Weldable(d, j, dd, start, minCov, welding_kmer, welding_kmer_read_count) 
                      || 
                          weld.Weldable(dd, start, d, j, minCov, welding_kmer, welding_kmer_read_count))) {
                    
                        if (DEBUG) {
                            cerr << "\t no welding kmer avail " << welding_kmer << ", count: " << welding_kmer_read_count 
                                 << ", min count required: " << minCov << endl;
                        }
                        
                        continue;
                    }
                                
                    if (IsShadow(d, dd, j, start, k) && coverage > 2*coverage_other) {
                        cerr << "Toasting shadow: " << dna.Name(c) << endl;
                
                        #pragma omp critical
                        toasted[c] = true;
                    
                        continue;
               
                    } else if (!IsGoodCoverage(coverage, coverage_other, min_iso_ratio)) {
                        //cerr << "Rejecting fw merge between " << dna.Name(i);
                        //cerr << " and " << dna.Name(c) << endl;
                    
                        continue;
                    }
                    // cerr << "Accept (fw)!!" << endl;
                
                    #pragma omp critical
                    {
                        
                        add_reciprocal_iworm_link(weld_reinforced_iworm_clusters, i, c);
                        
                        
                    }
                
                    if (REPORT_WELDS) {
                    #pragma omp critical
                        cout << "#Welding: " << dna.Name(i) << " to " << dna.Name(c) 
                             << " with " << welding_kmer << " found in " << welding_kmer_read_count << " reads" << endl;
                    }


                }

                if (sStrand) {
                    // only doing the forward matches

                    if (DEBUG) {
                        cerr << " only procesing forward strand " << endl;
                    }
                

                    continue; // superfluous since we didn't capture any rc matches
                }
            
                // Process the RC matches now
                for (x=0; x<matchesRC.isize(); x++) {
                    int c = matchesRC[x].GetContig();


                    if (c == i) {
                        // ignore self matches
                        continue;
                    }
                    

                    if (DEBUG) {
                        cerr << "RCkmer: " << sub.AsString() << " supports match between " << dna.Name(i) << " and " << dna.Name(c) << endl;
                    }

                    
                                
                    double coverage_other = Coverage(dna.Name(c));
                
                    double higher_coverage_val = (coverage > coverage_other) ? coverage : coverage_other;
                    int minCov = (int) (higher_coverage_val * glue_factor);
                    if (minCov < min_glue_required) {
                        minCov = min_glue_required;
                    }
                
                    DNAVector dd = dna[c];
                    dd.ReverseComplement();  // revcomp a copy of the iworm[c] sequence
                
                    int start = dd.isize() - matchesRC[x].GetPosition() - k;  // reverse-complement the match coordinate


                    if (DEBUG) {
                        cerr << "kmer match between " << dna.Name(i) << " and " << dna.Name(c)
                             << " at R positions: " << j << " and " << start << endl;
                    } 
                
                
                    string welding_kmer;
                    int welding_kmer_read_count;
                    if (!bNoWeld 
                    && 
                        !(weld.Weldable(d, j, dd, start, minCov, welding_kmer, welding_kmer_read_count) 
                      || 
                          weld.Weldable(dd, start, d, j, minCov, welding_kmer, welding_kmer_read_count))) {


                        if (DEBUG) {
                            cerr << "\t no welding kmer avail " << welding_kmer << ", count: " << welding_kmer_read_count 
                                 << ", min count required: " << minCov << endl;
                        }
                        
                        continue;
                    }
                    
                    if (IsShadow(d, dd, j, start, k) && coverage > 2*coverage_other) {
                        cerr << "Toasting shadow: " << dna.Name(c) << endl;

                        #pragma omp critical
                        toasted[c] = true;
                    
                        continue;

                    } else if (!IsGoodCoverage(coverage, coverage_other, min_iso_ratio)) {
                        // cerr << "Rejecting rc merge between " << dna.Name(i);
                        // cerr << " and " << dna.Name(c) << endl;
                        continue;
                    } else if (!IsGoodCoverage(coverage, coverage_other, min_iso_ratio)) {
                        // cerr << "Rejecting rc merge between " << dna.Name(i);
                        // cerr << " and " << dna.Name(c) << endl;
                        continue;
                    }
                    //cerr << "Accept (rc)!!" << endl;


                
                    #pragma omp critical
                    {

                        add_reciprocal_iworm_link(weld_reinforced_iworm_clusters, i, c);

                        
                    }

                    if (REPORT_WELDS) {
                    #pragma omp critical
                        cout << "#Welding: " << dna.Name(i) << " to " << dna.Name(c) 
                             << " with " << welding_kmer << " found in " << welding_kmer_read_count << " reads" << endl;
                    }
                
                    //cout << "Mapped sequence " << dna.NameClean(c) << " to pool " << mapped[c] << " -" << endl;
                }
            
            }
 
	}

        end_time = omp_get_wtime();
	cerr << endl << endl << "...done Phase 2. (" << end_time - start_time << " seconds)" << endl;

        cerr<<"\nPhase 2 took "<<timer_stop()<< "(in milliseconds)"<<endl;
        cerr << endl; // end of progress monitoring.
	
    
    // sl_clustered_pools = sl_cluster_pools(weld_reinforced_iworm_clusters, toasted);
    
    svec<Pool> clustered_pools = bubble_up_cluster_growth(weld_reinforced_iworm_clusters, toasted);
    
    //-----------------------------------------------------------------------------------
    // Generate final output


    if (DEBUG) {
        cerr << "Final pool description: " << endl;
        describe_poolings(clustered_pools);
    }
    
    
    add_unclustered_iworm_contigs(clustered_pools, dna);
    
    int component_count = 0;
    
    for (i=0; i<clustered_pools.isize(); i++) {
        Pool & p = clustered_pools[i];
        
        if (p.size() == 0) { continue; }

        p.sortvec();
    
        int sum_iworm_length = 0;
        for (size_t j = 0; j < p.size(); j++) {
            int z = p.get(j);
            sum_iworm_length += dna[z].isize();
        }

        if (sum_iworm_length < MIN_CONTIG_LENGTH) {
            continue;
        }
        
        stringstream pool_info;                
        pool_info << "#POOL_INFO\t" << component_count << ":" << "\t";

        cout << "COMPONENT " << component_count << "\t" << p.size() << endl;
        for (size_t j = 0; j < p.size(); j++) {
            int z = p.get(j);
            
            pool_info << z << " ";
            cout << ">Component_" << component_count << " " << p.size() << " " << z << " [iworm" << dna.Name(z) << "]" << endl;
            PrintSeq(dna[z]);
        }
        pool_info << endl;
        cout << pool_info.str();
        
        cout << "END" << endl;
    

        component_count++;

    }

#endif

    }
    print_cpu_memory();
#ifdef MPI_ENABLED
    MPI_Finalize();
#endif

   
   return 0;
    
}