File: dnaclust.cpp

package info (click to toggle)
dnaclust 3-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 720 kB
  • sloc: cpp: 3,630; sh: 516; makefile: 64
file content (2188 lines) | stat: -rw-r--r-- 69,572 bytes parent folder | download | duplicates (3)
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
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <limits>
#include <stack>
#include <fstream>
// #include <boost/bind.hpp>
// #include <boost/date_time/posix_time/posix_time.hpp>
// #include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/program_options.hpp>		
#include <stdint.h>
//#include <boost/thread.hpp>
//#include <boost/thread/condition_variable.hpp>
#include <pthread.h>
namespace po = boost::program_options;

#include "ternary_sort.hpp"
#include "fasta.hpp"

#define MATRIX_RESIZABLE 1
#include "multi_dim.hpp"
#include "utility.hpp"


typedef char CharType;
typedef const CharType * ConstCharPointer;
typedef std::vector<ConstCharPointer> ConstCharPointerVector;
typedef int PositionType;
typedef int SequenceNumber;
typedef intptr_t Index;

uint16_t running_threads = 0;
size_t num_threads = 1;
int threads_completed = 0;
pthread_mutex_t running_mutex;

struct thread_args
 {
    PositionType l;
    PositionType r;
    PositionType pos;
    Index intervalIndex;
    uint16_t tableIndex;
    int globalOffset;
};

SequenceNumber numberOfInputSequences;

bool outputMultipleAlignment = false;
bool leftGapsAllowed = false;
bool noKmerFilter = true;
bool kmerFilter = false;
#if OLD_CODE
bool approximateFilter = false;
double approximationFactor;
#endif // OLD_CODE
bool approximateFilter2 = false;
bool noOverlap = false;
bool recruit_only = false;
bool useFullQueryHeader = false;

ConstCharPointerVector sortedStrings;

ConstCharPointerVector lexicographicSequencePointers;
ConstCharPointerVector headerPointers;
typedef std::vector<PositionType> PositionVector;
PositionVector headerLengths;
PositionVector sequenceLengths;


ConstCharPointer querySequencePointer;
PositionType querySequenceLength;
ConstCharPointer queryHeaderPointer;
PositionType queryHeaderLength;


const CharType endOfStringChar = '\0';
const CharType invalidChar = '^';
const CharType gapChar = '-';

const char HEADER_INDICATOR = '%';
const char CLUSTER_INDICATOR = '#';

typedef int32_t CostType;
const CostType MAX_COST = std::numeric_limits<CostType>::max() / 2;

struct Context {
  int id;
};

const size_t MAX_THREADS = 60;
pthread_t searchWorkers[MAX_THREADS];
struct Context searchWorkersContext[MAX_THREADS];
struct thread_args searchWorkersArgs[MAX_THREADS];
pthread_mutex_t searchWorkersMutexes[MAX_THREADS];
pthread_cond_t searchWorkersConds[MAX_THREADS];
pthread_cond_t searchWorkersCompleted;
pthread_mutex_t allThreadsMutex;

const size_t MAX_LENGTH = 4500;
std::vector< std::vector<CharType> > word(MAX_THREADS, std::vector<CharType>(MAX_LENGTH + 1));
std::vector< std::vector<PositionType> > wordMinLength(MAX_THREADS, std::vector<PositionType>(MAX_LENGTH + 1));
std::vector< std::vector<CostType> > minCost(MAX_THREADS, std::vector<CostType>(MAX_LENGTH + 1));
std::vector< std::vector<PositionType> > left(MAX_THREADS, std::vector<PositionType>(MAX_LENGTH + 1));
std::vector< std::vector<PositionType> > right(MAX_THREADS, std::vector<PositionType>(MAX_LENGTH + 1));

CostType radious;

float DEFAULT_SIMILARITY = 0.99;
float similarity;

float DEFAULT_MISMATCHES = -1.0;
float mismatches;

bool printInvertedIndex = false;
bool assignAmbiguous = false;
std::map< std::string, std::vector<std::string> > final_clusters;
std::map< std::string, std::vector<std::string> > inverted_index;
std::map< std::string, int > center_nonambig_counts;

template<typename T>
std::ostream & operator<<(std::ostream & os, std::vector<T> vec)
{
    os<<"{ ";
    std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(os, " "));
    os<<"}";
    return os;
}

template <typename BaseType, typename ExponentType>
BaseType power(const BaseType base, const ExponentType exponent)
{
  BaseType result = 1;
  for (ExponentType i = 0; i < exponent; ++i)
    result *= base;
  return result;
}


const int MAXIMUM_K_MER_LENGTH = 6;
#if OLD_CODE
const int DEFAULT_K_MER_LENGTH = 4;
#endif // OLD_CODE
int k_mer_length;
int random_seed = 0;

enum Nucleotides{A, C, G, T, NUMBER_OF_NUCLEOTIDES};

typedef int16_t KmerNumber;

#if FIXED_K
const KmerNumber NUMBER_OF_K_MERS = power(static_cast<int>(NUMBER_OF_NUCLEOTIDES), K_MER_LENGTH);
#endif // FIXED_K
KmerNumber number_of_k_mers;

// Exception thrown when program gets an unkown nucleotide letter.
class UnknownNucleotide : public std::exception
{
public:
  UnknownNucleotide(CharType nucleotide) throw()
    : nucleotide_(nucleotide)
  { }

  virtual const char *what() const throw()
  {
    std::ostringstream messageStream;
    messageStream << "Sequence contains unknown nucleotide: " << nucleotide_ << ".";
    return messageStream.str().c_str();
  }

private:
  const CharType nucleotide_;
};

// Return an integer number in range 0..(NUMBER_OF_NUCLEOTIDES - 1) for a given nucleotide character.
int numberFromNucleotide(const CharType nucleotide)
{
  switch(tolower(nucleotide)) {
  case 'a':
    return A;
  case 'c':
    return C;
  case 'g':
    return G;
  case 't':
    return T;
  default:
    return A;
    throw UnknownNucleotide(nucleotide);
  }
}

typedef uint16_t KmerCount;
const KmerCount MAX_K_MER_COUNT = std::numeric_limits<KmerCount>::max();

// k-mer spectrum is a vector of integers counting how many of each k-mer there are in a sequence.
typedef std::vector<KmerCount> KmerSpectrum;

// Count the number of each k-mer in a sequence and return a vector of counts.
KmerSpectrum countKmers(ConstCharPointer sequence, PositionType length)
{
  KmerSpectrum spectrum(number_of_k_mers);

  if (length >= k_mer_length) {
    int i = 0;
    int kMerNumber = 0;
    for (; i < k_mer_length; ++i) {
      kMerNumber *= NUMBER_OF_NUCLEOTIDES;
      kMerNumber += numberFromNucleotide(sequence[i]);
    }
    
    ++spectrum[kMerNumber];
    
    for (; i < length; ++i) {
      kMerNumber *= NUMBER_OF_NUCLEOTIDES;
      kMerNumber %= number_of_k_mers;
      kMerNumber += numberFromNucleotide(sequence[i]);
      
      if (spectrum[kMerNumber] < MAX_K_MER_COUNT)
	++spectrum[kMerNumber];
    }
      
    
  }
  
  return spectrum;
}



typedef std::vector<Index> IndexVector;

IndexVector lexicographicIndexOfSortedStringsIndexes;

typedef multi_dim::Matrix<KmerCount> CountMatrix;
CountMatrix spectrumMatrix;

IndexVector lexicographicIndexOfSpectrumIndexes;


IndexVector spectrumSearchResults;

KmerSpectrum querySpectrum;
KmerSpectrum queryLowSpectrum;

IndexVector spectrumSplitGuide;
PositionVector spectrumRemainingGuide;

/*
  Search for potential matches based on their k-mer spectrum.
  The query's k-mer spectrum should in global variable 'querySpectrum'.
  The k-mer spectrums for the sequences to be clustered are in global variable 'specturms'.
  Results will be in global variable ''.
*/




typedef std::pair<KmerSpectrum, Index> SpectrumIndexPair;
typedef std::vector<SpectrumIndexPair> SpectrumIndexVector;
    
SpectrumIndexVector spectrumIndexes;


enum BackPointer {LEFT, DOWN, DOWN_LEFT, ROOT};

typedef multi_dim::Matrix<CostType> DpTable;

DpTable *tables[MAX_THREADS];
//DpTable table(MAX_LENGTH + 1, MAX_LENGTH + 1);

typedef multi_dim::Matrix<BackPointer> BackPointerTable;

BackPointerTable backTable(MAX_LENGTH + 1, MAX_LENGTH + 1);


inline const CostType cost(const CharType c1, const CharType c2)
{
  return (c1 != c2) ? 1 : 0;
}

#if COST_MATRIX
namespace cost_matrix {
  const size_t MAX_CHAR = 256;
  typedef multi_dim::Array2D<CostType, MAX_CHAR, MAX_CHAR> CostMatrix;

  CostMatrix cost;
  
}
#endif // COST_MATRIX

void initialize()
{
  // initialize() implementation depends on whether we allow gaps on both ends.
  
  for(uint16_t i = 0; i < num_threads; ++i){
    if (mismatches >= 0)
      left[i][0] = - static_cast<PositionType>(mismatches);
    else
      left[i][0] = - static_cast<PositionType>(floor(MAX_LENGTH * (1 - similarity)));
    right[i][0] = std::numeric_limits<PositionType>::max() / 2;
    std::fill(word[i].begin(), word[i].end(), invalidChar);

    tables[i] = new DpTable(MAX_LENGTH + 1, MAX_LENGTH + 1);
    (*tables[i])(0, 0) = 0;
    backTable(0, 0) = ROOT;

    for (size_t j = 1; j <  MAX_LENGTH + 1; ++j) {
      (*tables[i])(0, j) = leftGapsAllowed ? 0 : (*tables[i])(0, j - 1) + cost(gapChar, invalidChar);    
      backTable(0, j) = LEFT;
    }
  }
} // void initialize()


typedef std::vector<PositionType> AlignmentFunction;
typedef boost::tuple<CostType, AlignmentFunction> AlignmentCostAndFunction;


AlignmentCostAndFunction findAlignmentFunction(const PositionType len1, const PositionType len2, const PositionType pos2)
{

  AlignmentFunction alignmentFunc(len2 + 1);

  // TODO(cmhill): Remove
  CostType minCost = 0; // table(len1, pos2);


  PositionType i = len1;
  PositionType j = len2;

  assert(minCost <= radious);

  while (j > pos2){
    alignmentFunc[j] = i;
    --j;
  }

  while (backTable(i, j) != ROOT){

    switch(backTable(i, j)){
    case DOWN:
      alignmentFunc[j] = i;
      --i;
      break;
    case LEFT:
      alignmentFunc[j] = i;
      --j;
      break;
    case DOWN_LEFT:
      alignmentFunc[j] = i;
      --i;
      --j;
      break;
    case ROOT:
    default:
      std::stringstream errorStream;
      errorStream << "Invalid back pointer value; backTable(" << i << "," << j << ")=" << backTable(i, j) << ".";
      throw std::logic_error(errorStream.str());
    } // switch
  } // while (backTable(i, j) != ROOT)

  alignmentFunc[j] = i;
    
  return boost::make_tuple(minCost, alignmentFunc);
} // findAlignmentFunction2

struct SearchResult
{
  SequenceNumber number;
  CostType cost;
  AlignmentFunction func;
};

typedef std::vector<SearchResult> SearchResultVector;

SearchResultVector searchResults;

std::vector< SearchResultVector > threadSearchResults(MAX_THREADS);
std::vector< PositionVector > sortedStringsLengthsMinTree(MAX_THREADS);
std::vector< PositionVector > sortedStringsLengthsMaxTree(MAX_THREADS);
int resultsCounter[MAX_THREADS];

template <class Iterator1, class Iterator2>
Iterator2 buildMaxTree(Iterator1 first, Iterator1 last, Iterator2 intervalIterator)
{
  if (last > first) {
    if (last == first + 1) {
      *intervalIterator = *first;
    } else {
      Iterator1 mid = first + (last - 1 - first) / 2 + 1;
      *intervalIterator = std::max(*buildMaxTree(first,  mid, intervalIterator + 1),
				   *buildMaxTree(mid,   last, intervalIterator + 2 * (mid - first)));

    }
  }
  return intervalIterator;
}


template <class Iterator1, class Iterator2>
Iterator2 buildMinTree(Iterator1 first, Iterator1 last, Iterator2 intervalIterator)
{
  if (last > first) {
    if (last == first + 1) {
      *intervalIterator = *first;
    } else {
      Iterator1 mid = first + (last - 1 - first) / 2 + 1;
      *intervalIterator = std::min(*buildMinTree(first,  mid, intervalIterator + 1),
				   *buildMinTree(mid,   last, intervalIterator + 2 * (mid - first)));

    }
  }
  return intervalIterator;
}


/*
  The implementation of search function is seperated in another file.
  The reason for this is to reuse the code to make two search functions:
  The first search function does not keep track of back pointers and would not find the alignment but is supposedly faster.
  The second search function will find the alignment.
*/

#define ALIGN_WITH_BACKPOINTER 0
#include "search_include.cpp"

#undef ALIGN_WITH_BACKPOINTER
#define ALIGN_WITH_BACKPOINTER 1
#include "search_include.cpp"



template <class InputIterator>
std::string firstWord(InputIterator first, InputIterator last)
{
  return std::string(first, std::find_if(first, last, isspace));
}


// Using std::vector<char> instead of std::vector<bool> .
typedef std::vector<char> BoolVector;
BoolVector marked;
BoolVector markedGrey;
int numberOfMarked;
int remainingAtLastDbUpdate;

sequence::Fasta inputFasta;
sequence::Fasta predeterminedCentersFasta;

bool lessSpectrumIndexPair(const SpectrumIndexPair &x, const SpectrumIndexPair &y, KmerNumber dimension)
{
  return x.first[dimension] < y.first[dimension];
}

typedef std::vector<KmerCount> KmerCountVector;

template <class RandomAccessIterator>
RandomAccessIterator median(RandomAccessIterator first, RandomAccessIterator last)
{
  RandomAccessIterator mid = first + ((last - first) / 2);
  std::nth_element(first, mid, last);
  return mid;
}

typedef const KmerCount * const ConstKmerCountArray;
typedef KmerCount * const KmerCountArray;

Index twoMeans(const Index l, const Index r)
{

#if MY_DEBUG
  // debug.
  std::cerr << "\n\ttwoMeans(" << l << ",\t" << r  << ")\n" << std::flush;
#endif // MY_DEBUG

#if OLD_CODE
  std::sort(spectrumIndexes.begin() + l, spectrumIndexes.begin() + r);
#endif // OLD_CODE

#if OLD_CODE
  typedef std::vector<float> FloatVector;
  FloatVector center1(spectrumIndexes[l].first.begin(), spectrumIndexes[l].first.end());
  FloatVector center2(center1.size());
#endif // OLD_CODE

  KmerCountVector center1(spectrumIndexes[l].first.begin(), spectrumIndexes[l].first.end());
  KmerCountVector center2(center1.size());


  {
    Index i = l + 1;
    while (spectrumIndexes[l].first == spectrumIndexes[i].first)
      ++i;
    
    std::copy(spectrumIndexes[i].first.begin(), spectrumIndexes[i].first.end(), center2.begin());
  }

  assert(center1 != center2);

  typedef std::vector<int> IntVector;
  IntVector clusterNumber(r - l, 0);
  clusterNumber.front() = 1;
  clusterNumber.back() = 0;

  int clustersChanged = 100000000;


  // debug.
  int clusterChanges = 0;


  KmerCountVector counts1;
  KmerCountVector counts2;

  counts1.reserve(r - l);
  counts2.reserve(r - l);

  while((clustersChanged > 0) && ((clusterChanges <= 1) || (clustersChanged > (r - l) / 10))) {

#if MY_DEBUG
    // debug.
    std::cerr << '.' << std::flush;
    if ((l == 215699) && (r == 216534)) {
      for (Index i = l; i < r; ++i)
	std::cerr << (clusterNumber[i - l]);
      std::cerr << '\n';
    }
#endif // MY_DEBUG
    ++clusterChanges;


    clustersChanged = 0;
    
    for (Index i = l; i < r; ++i) {
      int d1 = 0;
      int d2 = 0;

      ConstKmerCountArray spectrum = spectrumIndexes[i].first.data();
      for (KmerNumber j = 0; j < number_of_k_mers; ++j) {
	d1 += abs(static_cast<int>(center1[j]) - static_cast<int>(spectrum[j]));
	d2 += abs(static_cast<int>(center2[j]) - static_cast<int>(spectrum[j]));
      }

      if (d1 != d2) {
	int newClusterNumber = (d1 < d2) ? 1 : 0;
	if (newClusterNumber != clusterNumber[i - l]) {
	  clusterNumber[i - l] = newClusterNumber;
	  ++clustersChanged;
	} // if
      }
    } // for

#if OLD_CODE

    {

      int sizeOfCluster1 = 0;
      int sizeOfCluster2 = 0;

      for (Index i = l; i < r; ++i) 
	if (clusterNumber[i - l] == 1) 
	  ++sizeOfCluster1;
	else 
	  ++sizeOfCluster2;


      if (sizeOfCluster1 == 0) {
	
	// debug.
	std::cerr << "sizeOfCluster1 == 0\n" << std::flush;

	float maxD2 = 0;
	Index maxIndex = -1;
	for (Index i = l; i < r; ++i) {
	  float d2 = 0;
	  for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j) {
	    d2 += fabsf(center2[j] - spectrumIndexes[i].first[j]);
	  }
	  if (d2 > maxD2) {
	    maxD2 = d2;
	    maxIndex = i;
	  }
	} // for

	assert(l <= maxIndex);
	assert(maxIndex < r);

	clusterNumber[maxIndex - l] = 1;
	clustersChanged = true;
      }
      if (sizeOfCluster2 == 0) {

	// debug.
	std::cerr << "sizeOfCluster2 == 0\n" << std::flush;

	float maxD1 = 0;
	Index maxIndex = -1;
	for (Index i = l; i < r; ++i) {
	  float d1 = 0;
	  for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j) {
	    d1 += fabsf(center1[j] - spectrumIndexes[i].first[j]);
	  }
	  if (d1 > maxD1) {
	    maxD1 = d1;
	    maxIndex = i;
	  }
	} // for

	assert(l <= maxIndex);
	assert(maxIndex < r);

	clusterNumber[maxIndex - l] = 0;
	clustersChanged = true;
      }
	

    }
#endif // OLD_CODE
 

    if (clustersChanged) { // Find new centers.

#if OLD_CODE
      int sizeOfCluster1 = 0;
      int sizeOfCluster2 = 0;

      std::fill(center1.begin(), center1.end(), 0);
      std::fill(center2.begin(), center2.end(), 0);

      for (Index i = l; i < r; ++i) {
	if (clusterNumber[i - l] == 1) {
	  ++sizeOfCluster1;
	  for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j)
	    center1[j] += spectrumIndexes[i].first[j];
	} else {
	  ++sizeOfCluster2;
	  for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j)
	    center2[j] += spectrumIndexes[i].first[j];
	}

      }

      assert(sizeOfCluster1 > 0);
      assert(sizeOfCluster2 > 0);

      for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j) {
	center1[j] /= sizeOfCluster1;
	center2[j] /= sizeOfCluster2;
      } // for
#endif // OLD_CODE


#if MY_DEBUG
      std::cerr << '(' << std::flush;
#endif // MY_DEBUG

      for (KmerNumber j = 0; j < number_of_k_mers; ++j) {

	counts1.clear();
	counts2.clear();

	for (Index i = l; i < r; ++i) {
	  if (clusterNumber[i - l] == 1) {
	    counts1.push_back(spectrumIndexes[i].first[j]);
	  } else {
	    counts2.push_back(spectrumIndexes[i].first[j]);
	  } // if (clusterNumber[i - l] == 1)
	} // for (Index i

#if MY_DEBUG
	assert(counts1.size() > 0);
	assert(counts2.size() > 0);
#endif // MY_DEBUG

	center1[j] = *median(counts1.begin(), counts1.end());
	center2[j] = *median(counts2.begin(), counts2.end());

      } // for (KmerNumber j

#if MY_DEBUG
      std::cerr << ')' << std::flush;
#endif // MY_DEBUG

    } // Find new center.


  } // while

#if MY_DEBUG
  std::cerr << 'S' << std::flush;
#endif // MY_DEBUG

  { // Split.
    Index i = l;
    Index j = r - 1;

    KmerCountVector temp(number_of_k_mers);
    
    while (j > i) {
      while (clusterNumber[i - l] == 1)
	++i;
      while (clusterNumber[j - l] == 0)
	--j;
      if (j > i) {
	std::swap(spectrumIndexes[i], spectrumIndexes[j]);
	/*
	{
	  // std::swap(spectrumIndexes[i].first, spectrumIndexes[j].first);
	  std::copy(spectrumIndexes[i].first.begin(), spectrumIndexes[i].first.end(), temp.begin());
	  std::copy(spectrumIndexes[j].first.begin(), spectrumIndexes[j].first.end(), spectrumIndexes[i].first.begin());
	  std::copy(temp.begin(), temp.end(), spectrumIndexes[i].first.begin());

	  std::swap(spectrumIndexes[i].second, spectrumIndexes[j].second);
	}
	*/
	std::swap(clusterNumber[i - l], clusterNumber[j - l]);
	++i;
	--j;
      } // if
    } // while

    // debug.
    
  } // Split.

#if MY_DEBUG
  std::cerr << 'R' << std::flush;
#endif // MY_DEBUG

  Index result = l;
  while (clusterNumber[result - l] == 1)
    ++result;

  for (Index i = result; i < r; ++i)
    assert(clusterNumber[i - l] == 0);

#if MY_DEBUG
  // debug.
  std::cerr << "clusterChanges=" << clusterChanges << ". done.\n" << std::flush;
#endif // MY_DEBUG
#if MY_DEBUG
  std::cerr << 'Q' << std::flush;
#endif // MY_DEBUG

  return result;
  
  
} // twoMeans


namespace cluster_information
{
  Index *l = 0;
  Index *r = 0;
  Index *sizeOfLeftCluster = 0;


  Index *numberOfKmers = 0;
  KmerCount **allMinCounts = 0;
  KmerCount **allMaxCounts = 0;

  int **counts = 0;
}



const KmerCount MAX_COUNT = std::numeric_limits<KmerCount>::max();
const KmerCount MIN_COUNT = 0;

const Index NULL_INDEX = -1;


void clusterSort(const Index l, const Index r, const Index index)
{

#if MY_DEBUG
  if (r - l > 10000)
    std::cerr << "clusterSort(" << l << ",\t" << r << ") " << std::flush;
#endif // MY_DEBUG

#if OLD_CODE
  ClusterInformation &info = clusters_new[index];

  clusters_new[index].l = l;
  clusters_new[index].r = r;

  KmerCountArray minCounts = clusters_new[index].minCounts;
  KmerCountArray maxCounts = clusters_new[index].maxCounts;

  std::fill_n(minCounts, number_of_k_mers, MAX_COUNT);
  std::fill_n(maxCounts, number_of_k_mers, MIN_COUNT);

  for (Index i = l; i < r; ++i)
    for (KmerNumber j = 0; j < number_of_k_mers; ++j) {
      KmerCount count = spectrumIndexes[i].first[j];

      if (count < minCounts[j])
	minCounts[j] = count;
      if (count > maxCounts[j])
	maxCounts[j] = count;
    }


#endif // OLD_CODE

#if MY_DEBUG
  std::cerr << '1' << std::flush;
#endif // MY_DEBUG

  {
    // Set 'cluster_infomation' of this cluster.
    // \for_all k \in {1...k_mer_length}: cluster_information::allMinCounts[k][index * cluster_information::numberOfKmers[k] ... index * cluster_information::numberOfKmers[k] + cluster_information::numberOfKmers[k] - 1]
    

    for (int k = 0; k < k_mer_length; ++k) {
      std::fill_n(&cluster_information::allMinCounts[k][index * cluster_information::numberOfKmers[k]], cluster_information::numberOfKmers[k], MAX_COUNT);
      std::fill_n(&cluster_information::allMaxCounts[k][index * cluster_information::numberOfKmers[k]], cluster_information::numberOfKmers[k], MIN_COUNT);
    }

#if MY_DEBUG
    std::cerr << '2' << std::flush;
#endif // MY_DEBUG

    for (Index i = l; i < r; ++i) {


      for (int k = 0; k < k_mer_length; ++k)
	std::fill_n(cluster_information::counts[k], cluster_information::numberOfKmers[k], 0);
    
      std::copy (spectrumIndexes[i].first.begin(), spectrumIndexes[i].first.end(), cluster_information::counts[k_mer_length - 1]);

      for (int k = k_mer_length - 1; k > 0; --k)
	for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j) {
	  int j_prime = j / static_cast<int>(NUMBER_OF_NUCLEOTIDES);
	  cluster_information::counts[k - 1][j_prime] += cluster_information::counts[k][j];
	}

      for (int k = 0; k < k_mer_length; ++k)
	for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j) {
	  KmerCount count = MAX_COUNT;
	  if (cluster_information::counts[k][j] < MAX_COUNT)
	    count = cluster_information::counts[k][j];

	  KmerCount &minCount = cluster_information::allMinCounts[k][index * cluster_information::numberOfKmers[k] + j];
	  KmerCount &maxCount = cluster_information::allMaxCounts[k][index * cluster_information::numberOfKmers[k] + j];

	  if (minCount > count)
	    minCount = count;
	  if (maxCount < count)
	    maxCount = count;
	}
    } // for (Index i = l; i < r; ++i)


    cluster_information::l[index] = l;
    cluster_information::r[index] = r;

  }

#if MY_DEBUG
  {

    std::cerr << '\n';

    for (int k = 0; k < k_mer_length; ++k) {
      for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j) {
	std::cerr << static_cast<int>(cluster_information::allMinCounts[k][index * cluster_information::numberOfKmers[k] + j]) << '\t';
      }
      std::cerr << '\n';
      for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j) {
	std::cerr << static_cast<int>(cluster_information::allMaxCounts[k][index * cluster_information::numberOfKmers[k] + j]) << '\t';
      }
      std::cerr << '\n';
      

    }

    std::cerr << std::flush;

  }

  std::cerr << '3' << std::flush;
#endif // MY_DEBUG


#if MEDIAN_COUNTS
  // Calculate 'medianCounts' and 'averageDeviations'.
  KmerCountArray medianCounts = info.medianCounts;
  {
    //    KmerCountArray averageDeviations = info.averageDeviations;

    KmerCountVector counts(r - l);
    KmerCount count;

    for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j) {
      for (Index i = l; i < r; ++i) {
	count = spectrumIndexes[i].first[j];
	counts[i - l] = count;
      } // for (Index i = l; i < r; ++i)
    
      medianCounts[j] = *median(counts.begin(), counts.end());

#if OLD_CODE
      int deviation = 0;

      for (Index i = l; i < r; ++i) {
	count = spectrumIndexes[i].first[j];
	deviation += abs(medianCounts[j] - count);
      } // for (Index i = l; i < r; ++i)

      averageDeviations[j] = deviation / (r - l);
#endif // OLD_CODE

    } // for (KmerNumber k = 0; k < NUMBER_OF_K_MERS; ++k)
  } // Calculate 'medianCounts' and 'averageDeviations'.
#endif // MEDIAN_COUNTS

#if OLD_CODE
  // Store indexes of different new counts.
  info.numberOfDifferentCounts = 0;
  for (KmerNumber j = 0; j < number_of_k_mers; ++j) 
    if ((minCounts[j] > prevMinCounts[j]) || 
	(maxCounts[j] < prevMaxCounts[j])

#if MEDIAN_COUNTS
	||
	(medianCounts[j] != prevMedianCounts[j])
#endif // MEDIAN_COUNTS

	) {
      info.indexOfDifferentCounts[info.numberOfDifferentCounts] = j;
      ++info.numberOfDifferentCounts;
    }

#endif // OLD_CODE

#if MY_DEBUG
  std::cerr << "l=" << l << "\tr=" << r << "\ti=" << index << '\n' << std::flush;
  for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j)
    std::cerr << static_cast<int>(minCounts[j]) << '\t';
  std::cerr << '\n' << std::flush;
  for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j)
    std::cerr << static_cast<int>(maxCounts[j]) << '\t';
  std::cerr << '\n' << std::flush;
#endif // MY_DEBUG

  bool allEqual = true;

  {
    for (KmerNumber j = 0; j < number_of_k_mers; ++j) {

      KmerCount &minCount = cluster_information::allMinCounts[k_mer_length - 1][index * cluster_information::numberOfKmers[k_mer_length - 1] + j];
      KmerCount &maxCount = cluster_information::allMaxCounts[k_mer_length - 1][index * cluster_information::numberOfKmers[k_mer_length - 1] + j];

      if (minCount < maxCount)
	allEqual = false;
    }
  }


  if (allEqual) {

#if OLD_CODE
    clusters_new[index].sizeOfLeftCluster = NULL_INDEX;
#endif // OLD_CODE

    cluster_information::sizeOfLeftCluster[index] = NULL_INDEX;

#if MY_DEBUG
    std::cerr << ".\n" << std::flush;
#endif // MY_DEBUG

    return;
  }

#if MY_DEBUG
  std::cerr << '4' << std::flush;
#endif // MY_DEBUG

  Index mid = twoMeans(l, r);

#if MY_DEBUG
  std::cerr << ".\n" << std::flush;
#endif // MY_DEBUG

#if OLD_CODE
  clusters_new[index].sizeOfLeftCluster = mid - l;
#endif // OLD_CODE

  cluster_information::sizeOfLeftCluster[index] = mid - l;

  clusterSort(l, mid, index + 1);
  clusterSort(mid, r, index + 2 * (mid - l));
}

IndexVector clusterSearchResults;

#if OLD_CODE
typedef const ClusterInformation * ConstClusterInfoPointer;
#endif // OLD_CODE

typedef const KmerCount * const ConstKmerCountArray;

typedef int Threshold;

Threshold maxMoreThreshold;
Threshold minMoreThreshold;



namespace layered {

  int layeredMaxMoreThresholds[MAXIMUM_K_MER_LENGTH];
  int layeredMinMoreThresholds[MAXIMUM_K_MER_LENGTH];

  int **allQueryCounts = 0;

  int layeredClusterSearch(Index index, int k)
  {
    
#if MY_DEBUG
    std::cerr << "layeredClusterSearch(" << index << ", " << k << ")\n" << std::flush;
#endif // MY_DEBUG

    int maxMore = 0;
    int minMore = 0;

    int numberOfKmers = cluster_information::numberOfKmers[k];
    KmerCount *minCounts = &cluster_information::allMinCounts[k][index * numberOfKmers];
    KmerCount *maxCounts = &cluster_information::allMaxCounts[k][index * numberOfKmers];

    int minMoreThreshold = layeredMinMoreThresholds[k];
    int maxMoreThreshold = layeredMaxMoreThresholds[k];


    int *queryCounts = allQueryCounts[k];

#if MY_DEBUG
    for (Index i = 0; i < numberOfKmers; ++i) 
      std::cerr << queryCounts[i] << '\t';
    std::cerr << '\n';
    for (Index i = 0; i < numberOfKmers; ++i) 
      std::cerr << static_cast<int>(minCounts[i]) << '\t';
    std::cerr << '\n';
    for (Index i = 0; i < numberOfKmers; ++i) 
      std::cerr << static_cast<int>(maxCounts[i]) << '\t';
    std::cerr << '\n' << std::flush;
#endif // MY_DEBUG

    {
      // Compute 'minMore' and 'maxMore'.

      int minCount;
      int maxCount;
      int queryCount;

      for (Index i = 0; i < numberOfKmers; ++i) {

	maxCount = maxCounts[i];
	queryCount = queryCounts[i];
	
	if (maxCount > queryCount) {
	  maxMore += maxCount - queryCount;
	  
	  minCount = minCounts[i];
	  if (minCount > queryCount) { 
	    minMore += minCount - queryCount;

	    if (minMore > minMoreThreshold) {
#if MY_DEBUG								
	      std::cerr << "minMore=" << minMore << " > " << layeredMinMoreThresholds[k] << "=layeredMinMoreThresholds[k]\n" << std::flush;
#endif // MY_DEBUG				
	      return 0;
	    } // if (minMore > minMoreThreshold)


	    // Second type of approximate filter
	    if (approximateFilter2 && minMore * (cluster_information::r[index] - cluster_information::l[index]) > minMoreThreshold)
	      return 0;

	  } // if (minCount > queryCount)

	} // if (maxMore > queryCount)
	  
#if OLD_CODE
	if (minCounts[i] > queryCounts[i])
	  minMore += static_cast<int>(minCounts[i]) - queryCounts[i];
	if (maxCounts[i] > queryCounts[i])
	  maxMore += static_cast<int>(maxCounts[i]) - queryCounts[i];
#endif // OLD_CODE
      } // for (Index i = 0; i < numberOfKmers; ++i)
    }

    if (maxMore <= maxMoreThreshold) {
#if MY_DEBUG
      std::cerr << "maxMore=" << maxMore << " <= " << layeredMaxMoreThresholds[k] << "=layeredMaxMoreThresholds[k]\n" << std::flush;
#endif // MY_DEBUG
      if (k + 1 < k_mer_length) {
	return layeredClusterSearch(index, k + 1);
      }

#if MY_DEBUG
      std::cerr << "Return results:\n" << std::flush;
#endif // MY_DEBUG

      for (Index i = cluster_information::l[index]; i < cluster_information::r[index]; ++i)
	clusterSearchResults.push_back(i);
      return cluster_information::r[index] - cluster_information::l[index];
    }


    return (layeredClusterSearch(index + 1, k) +
	    layeredClusterSearch(index + 2 * cluster_information::sizeOfLeftCluster[index], k));

  }

} // namespace layered


void *threaded_search_without_backpointer(void *read_args) {
    struct thread_args *args = static_cast<struct thread_args *>(read_args);

    const PositionType l = args->l;
    const PositionType r = args->r;
    PositionType pos = args->pos;
    const Index intervalIndex = args->intervalIndex;
    const uint16_t tableIndex = args->tableIndex;
    const int globalOffset = args->globalOffset;
    search_without_backpointer(l, r, pos, intervalIndex, tableIndex, globalOffset);
    return NULL;
}


void * threadWorker(void *context) {
  int id = (static_cast<struct Context *>(context))->id;

  pthread_mutex_lock(&searchWorkersMutexes[id]);
  while (1) {
    // Then wait for more work.
    pthread_cond_wait(&searchWorkersConds[id], &searchWorkersMutexes[id]);

    // Received work:
#if CHRIS_DEBUG
    std::cout<< "[THREAD " << id << "] received work.  Table index: " << searchWorkersArgs[id].tableIndex << "\n";
    flush(std::cout);
#endif // CHRIS_DEBUG

    
    threaded_search_without_backpointer(static_cast<void *>(&searchWorkersArgs[id]));
    pthread_mutex_lock(&allThreadsMutex);
    
    ++threads_completed;

    if (threads_completed >= running_threads) {
      pthread_cond_signal(&searchWorkersCompleted);
    }
    pthread_mutex_unlock(&allThreadsMutex);
  }

  return NULL;
}


/*
  Input: 'queryString', 'queryLength', 'queryHeader', 'queryHeaderLength' should be set.
  Database: 'sortedStrings' contains the strings to be clusterd in lexicographically sorted order. After making a cluster the sequences are removed from this vector.
  Output: Prints the cluster to STDOUT. The format of the output is determined by the value of global boolean variable 'outputMultipleAlignment'.
*/

void makeCluster()
{
#if MY_DEBUG
  std::cerr << "makeCluster()\n" << std::flush;
#endif // MY_DEBUG

  radious = static_cast<CostType>(floor((1 - similarity) * querySequenceLength));
  if (mismatches >= 0)
    radious = static_cast<CostType>(mismatches);

  if (noOverlap)
    radious *= 2;

  if (! noKmerFilter) {
    querySpectrum = countKmers(querySequencePointer, querySequenceLength);
  } // if (! noKmerFilter)



  if (! noKmerFilter) {   
    // Set allQueryCounts

    std::copy(querySpectrum.begin(), querySpectrum.end(), layered::allQueryCounts[k_mer_length - 1]);
    
    for (int k = 0; k < k_mer_length - 1; ++k)
      std::fill_n(layered::allQueryCounts[k], cluster_information::numberOfKmers[k], 0);


    for (int k = k_mer_length - 1; k > 0; --k)
      for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j) {
	int j_prime = j / static_cast<int>(NUMBER_OF_NUCLEOTIDES);
	layered::allQueryCounts[k - 1][j_prime] += layered::allQueryCounts[k][j];
      }


#if MY_DEBUG
    for (int k = 0; k < k_mer_length; ++k) {
      for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j)
	std::cerr << layered::allQueryCounts[k][j] << '\t';
      std::cerr << '\n' << std::flush;
    }
#endif // MY_DEBUG

    for (int k = 0; k < k_mer_length - 1; ++k)
      for (int j = 0; j < cluster_information::numberOfKmers[k]; ++j)
	if (layered::allQueryCounts[k][j] > MAX_COUNT)
	  layered::allQueryCounts[k][j] = MAX_COUNT;


  } // if (! noKmerFilter)

#if OLD_CODE
  callSplitSearch();
#endif // OLD_CODE

#if MY_DEBUG
  std::cerr << "splitSearchResults.size() == " << splitSearchResults.size() << '\n' << std::flush;
#endif // MY_DEBUG


#if MY_DEBUG
  naiveSearchResults.clear();
  
  naiveSpectrumSearch(radious * K_MER_LENGTH);

  assert(naiveSearchResults == splitSearchResults);

  std::cerr << "navieSearchResults == splitSearchResults\n" << std::flush;
#endif // MY_DEBUG

#if MY_DEBUG
  for (int i = 0; i < NUMBER_OF_K_MERS; ++i)
    std::cerr << static_cast<int>(querySpectrum[i]) << '\t';
  std::cerr << '\n' << std::flush;

  for (int j = 0; j < splitSearchResults.size(); ++j) {
    for (int i = 0; i < NUMBER_OF_K_MERS; ++i)
      std::cerr << static_cast<int>(spectrumIndexes[splitSearchResults[j]].first[i]) << '\t';
    std::cerr << '\n';
  }
#endif // MY_DEBUG

  clusterSearchResults.clear();

  if (! noKmerFilter) {
    // Set k-mer filter thresholds
    {
      for (int i = 0; i < k_mer_length; ++i)
	if (radious == 0) {
	  layered::layeredMaxMoreThresholds[i] = 0;
	  layered::layeredMinMoreThresholds[i] = 0;
	} else {
	  // radious * sqrt(i + 1)
	  // Approximation minMoreThreshold <= maxMoreThreshold
	  layered::layeredMaxMoreThresholds[i] = std::max(radious, static_cast<CostType>(querySequenceLength * 0.04)) * (i + 1);
#if OLD_CODE
	  layered::layeredMinMoreThresholds[i] = (approximateFilter ? static_cast<int>(radious * exp(approximationFactor * log(i + 1)))  : (radious * (i + 1)));
#endif // OLD_CODE
	  layered::layeredMinMoreThresholds[i] = radious * (i + 1);
	}
    } 

#if MY_DEBUG
    std::cerr << "Before layeredClusterSearch(0, 0)\n" << std::flush;
#endif //MY_DEBUG

    layered::layeredClusterSearch(0, 0);

#if MY_DEBUG
    for (int i = 0; i < static_cast<int>(clusterSearchResults.size()); ++i)
      std::cerr << clusterSearchResults[i] << '\t';
    std::cerr << '\n' << std::flush;

    exit(EXIT_FAILURE);
#endif // MY_DEBUG


#if NEW_CODE
    clusterSearch(clusters.data());
#endif // NEW_CODE
  } // if (! noKmerFilter)

  IndexVector unmarkeds;


  if (! noKmerFilter) { 
    // Remove already marked sequences.
    unmarkeds.reserve(clusterSearchResults.size());

    for(IndexVector::const_iterator it = clusterSearchResults.begin(); it != clusterSearchResults.end(); ++it){
      Index lexicographicIndex = spectrumIndexes[*it].second;
      if (!marked[lexicographicIndex])
	unmarkeds.push_back(lexicographicIndex);
    }
    const Index numberOfUnmarkeds = unmarkeds.size();
  
    std::sort(unmarkeds.begin(), unmarkeds.end());

#if MY_DEBUG							
    std::cout << "Query:\t" << querySequencePointer << '\n';
  
    for (Index i = 0; i < numberOfUnmarkeds; ++i)
      std::cout << lexicographicSequencePointers[unmarkeds[i]] << '\n';
#endif // MY_DEBUG

    sortedStrings.resize(numberOfUnmarkeds);

    PositionVector sortedStringsLengths(sortedStrings.size());
  
    for (Index i = 0; i < numberOfUnmarkeds; ++i) {
      sortedStrings[i] = lexicographicSequencePointers[unmarkeds[i]];
      sortedStringsLengths[i] = sequenceLengths[unmarkeds[i]];
    }

    searchResults.clear();

    for(uint16_t i = 0; i < num_threads; ++i){
      word[i][1] = invalidChar;
      wordMinLength[i][1] = MAX_LENGTH + 1;
    }

    
    size_t num_threads_to_use = num_threads;
    int interval_size = static_cast<int>(ceil(sortedStrings.size() / static_cast<double>(num_threads)));//sortedStrings.size() / num_threads_to_use;

    if (sortedStrings.size() < num_threads) {
      num_threads_to_use = 1;
      interval_size = sortedStrings.size();
    }

#if CHRIS_DEBUG
      std::cout << "Size: " <<  sortedStrings.size() << "\n";
      //std::cout << "Building first trees for table: " << i << " from: " <<  << " to " << end << ", interval_size " << chunkIndexes[i].second << "\n" << std::flush;
#endif // CHRIS_DEBUG

    running_threads = 0;
    for (unsigned int i = 0, currentTable = 0; i < sortedStrings.size(); i += interval_size, ++currentTable) {
      unsigned int end = i + interval_size;
      if (i + interval_size > sortedStrings.size()) {
        end = sortedStrings.size();
      }
      else {
        end = i + interval_size;
      }

      sortedStringsLengthsMinTree[currentTable].resize(2 * (end - i));
      sortedStringsLengthsMaxTree[currentTable].resize(2 * (end - i));

#if CHRIS_DEBUG
        std::cout << "Kmer: Building second trees from: " << i << " to " << end << "\n" << std::flush; 
        std::cout << "Kmer: Size of sorted string min/max: " << sortedStringsLengthsMinTree[currentTable].size() << ", " 
            << sortedStringsLengthsMaxTree[currentTable].size() << "\n" << std::flush;
#endif // CHRIS_DEBUG

      buildMinTree(sortedStringsLengths.begin() + i, sortedStringsLengths.begin() + end, sortedStringsLengthsMinTree[currentTable].begin());
      buildMaxTree(sortedStringsLengths.begin() + i, sortedStringsLengths.begin() + end, sortedStringsLengthsMaxTree[currentTable].begin());
    }

    if (numberOfUnmarkeds) {
      if (outputMultipleAlignment)
	search(0, numberOfUnmarkeds - 1, 0, 0, 0, 0);
      else {
      
      running_threads = 0;
      threads_completed = 0;

#if CHRIS_DEBUG
        std::cout << "Size: " << sortedStrings.size() << ", numberOfUnmarkeds: " << numberOfUnmarkeds 
        << ", interval_size" << interval_size<< "\n" <<std::flush;
#endif // CHRIS_DEBUG

      for (int i = 0; i < numberOfUnmarkeds; i += interval_size) {
        ++running_threads;
        pthread_mutex_lock(&searchWorkersMutexes[running_threads-1]);

        struct thread_args *args = &searchWorkersArgs[running_threads-1];
        args->l = 0;

        if (i + interval_size > numberOfUnmarkeds) {
          args->r = (numberOfUnmarkeds - 1) - i;
        }
        else {
          args->r = interval_size - 1;
        }
        args->pos = 0;
        args->intervalIndex = 0;
        args->tableIndex = running_threads-1;
        args->globalOffset = i;

#if CHRIS_DEBUG
        std::cout << "  Assigning work to thread: " << i << ", l: " << args->l
            << ", r: " << args->r << "\n" <<std::flush;
#endif // CHRIS_DEBUG

        pthread_cond_signal(&searchWorkersConds[running_threads-1]);
        pthread_mutex_unlock(&searchWorkersMutexes[running_threads-1]);

      }

      // Wait for all threads to complete.
      pthread_mutex_lock(&allThreadsMutex);
      while (threads_completed < running_threads) {
        pthread_cond_wait(&searchWorkersCompleted, &allThreadsMutex);
      }
      pthread_mutex_unlock(&allThreadsMutex);

      for(int i = 0; i < running_threads; i++)  {
        searchResults.insert(searchResults.end(), threadSearchResults[i].begin(), threadSearchResults[i].end());
        threadSearchResults[i].clear();
      }
    }

	//search_without_backpointer(0, numberOfUnmarkeds - 1, 0, 0, 0, 0);
    }

  } // if (! noKmerFilter)
  else {
    // No k-mer filter

    for(uint16_t i = 0; i < num_threads; ++i){
      word[i][1] = invalidChar;
      wordMinLength[i][1] = MAX_LENGTH + 1;
    }
    searchResults.clear();

    if (outputMultipleAlignment)
      search(0, sortedStrings.size() - 1, 0, 0, 0, 0);
    else {
      int num_threads_to_use = num_threads;
      int interval_size = static_cast<int>(ceil(sortedStrings.size() / static_cast<double>(num_threads)));
      
      if (sortedStrings.size() < num_threads) {
        num_threads_to_use = 1;
        interval_size = sortedStrings.size();
      }

      running_threads = 0;
      threads_completed = 0;

      for (unsigned int i = 0; i < sortedStrings.size(); i += interval_size) {
        ++running_threads;

        // Create the thread variables.
        pthread_mutex_lock(&searchWorkersMutexes[running_threads - 1]);

        struct thread_args *args = &searchWorkersArgs[running_threads - 1];
        args->l = 0;

        if (i + interval_size > sortedStrings.size()) {
          args->r = (sortedStrings.size() - 1) - i;
        }
        else {
          args->r = interval_size - 1;
        }
        args->pos = 0;
        args->intervalIndex = 0;
        args->tableIndex = running_threads-1;
        args->globalOffset = i;

#if CHRIS_DEBUG
        std::cout << "Assigning work to thread: " << i << ", l: " << args->l
            << ", r: " << args->r << "\n" <<std::flush;
#endif // CHRIS_DEBUG


        pthread_cond_signal(&searchWorkersConds[running_threads-1]);
        pthread_mutex_unlock(&searchWorkersMutexes[running_threads-1]);
      }

      // Wait for all threads to complete.
      pthread_mutex_lock(&allThreadsMutex);
      while (threads_completed < running_threads) {
        pthread_cond_wait(&searchWorkersCompleted, &allThreadsMutex);
      }
      pthread_mutex_unlock(&allThreadsMutex);

      for(int i = 0; i < running_threads; i++)  {
        searchResults.insert(searchResults.end(), threadSearchResults[i].begin(), threadSearchResults[i].end());
        threadSearchResults[i].clear();
      }

      //search_without_backpointer(0, sortedStrings.size() - 1, 0, 0);
    }
  }


  { // Output cluster alignment.
	

    // Output cluster size.
    if (outputMultipleAlignment) {
      std::cout << CLUSTER_INDICATOR << searchResults.size() + 1 << '\n';
    }


    typedef std::vector<PositionType> CountsVector;


    // This vector will contain the maximum number of gaps that happen after position [i] in query string in ANY of the alignments to search results.
    CountsVector gapCounts(querySequenceLength);
	
	
    if (outputMultipleAlignment) { // Compute gapCounts[0..].

      for (SearchResultVector::const_iterator resultIterator = searchResults.begin(); resultIterator != searchResults.end(); ++resultIterator) {
	for (PositionType i = 0; i < static_cast<PositionType>(resultIterator->func.size() - 1); ++i)
	  gapCounts[i] = std::max(gapCounts[i], resultIterator->func[i + 1] - resultIterator->func[i] - 1);

      } // for
    } // if (outputMultipleAlignment)

    // This string will contain the query sequence with appropriate number of gaps inserted at each position to produce a multiple alignment of the cluster.
    std::string clusterCenterSequenceWithGaps;

    if (outputMultipleAlignment) { // Compute clusterCenterSequenceWithGaps.
      for (PositionType positionInQuery = 0; positionInQuery < querySequenceLength; ++positionInQuery) {
	clusterCenterSequenceWithGaps.append(gapCounts[positionInQuery], gapChar);
	clusterCenterSequenceWithGaps.push_back(querySequencePointer[positionInQuery]);
      } // for
    } // if (outputMultipleAlignment)

    // Output cluster center sequence with gaps from multiple alignment.
    if (outputMultipleAlignment) {
      std::cout << '>' << queryHeaderPointer << '\n'
		<< clusterCenterSequenceWithGaps << '\n';
    } else {
      if (useFullQueryHeader) {
        if (!assignAmbiguous) {
          std::cout << queryHeaderPointer << '\t';
        } else {
          if (!printInvertedIndex)
            center_nonambig_counts[queryHeaderPointer] = 1;
        }
      } else {
        if (!assignAmbiguous) {
          std::cout << firstWord(queryHeaderPointer, queryHeaderPointer + queryHeaderLength) << '\t';
        } else {
          if (!printInvertedIndex)
            center_nonambig_counts[firstWord(queryHeaderPointer, queryHeaderPointer + queryHeaderLength)] = 1;
        }
      }
    } 
	
    // Output multiple alignment of sequences in current cluster.
    for (SearchResultVector::const_iterator resultIterator = searchResults.begin(); resultIterator != searchResults.end(); ++resultIterator) {

      Index lexicographicIndex;
      if (! noKmerFilter) {
	lexicographicIndex = unmarkeds[resultIterator->number];
      } else {
	lexicographicIndex = lexicographicIndexOfSortedStringsIndexes[resultIterator->number];
      }
      ConstCharPointer s = sortedStrings[resultIterator->number];

      if (!assignAmbiguous)
        markedGrey[lexicographicIndex] = true;

      if ((!noOverlap) || resultIterator->cost < radious / 2) {
      
	if (!marked[lexicographicIndex]) {
    if (!assignAmbiguous) {
  	  marked[lexicographicIndex] = true;
  	  ++numberOfMarked;
    }

	  ConstCharPointer headerPointer = headerPointers[lexicographicIndex];
	  PositionType headerLength = headerLengths[lexicographicIndex];

	  if (outputMultipleAlignment) {
	    std::string alignedSequence; 
	    for (PositionType i = 0; i < static_cast<PositionType>(gapCounts.size()); ++i) {
	      for (PositionType j = resultIterator->func[i + 1] - resultIterator->func[i]; j < gapCounts[i] + 1; ++j)
		alignedSequence.push_back(gapChar);
	      for (PositionType j = resultIterator->func[i]; j < resultIterator->func[i + 1]; ++j)
		alignedSequence.push_back(s[j]);
	    } // for (PositionType i = 0; i < gapCounts.size() - 1; ++i)

	    std::cout << '>' << headerPointer << '\n'
		      << alignedSequence << '\n';
	  } else {
      if (useFullQueryHeader) {
        if (!assignAmbiguous) {
          std::cout << headerPointer << '\t';
        }
        else {
          if (printInvertedIndex) {
            std::cout << headerPointer << '\t' << queryHeaderPointer << '\n';
          } else {
            inverted_index[headerPointer].push_back(queryHeaderPointer);
            if (inverted_index[headerPointer].size() == 1) {
              center_nonambig_counts[queryHeaderPointer] += 1;
            } else if (inverted_index[headerPointer].size() == 2) {
              center_nonambig_counts[inverted_index[headerPointer][0]] -= 1;
            }
          }
        }

      } else {
        if (!assignAmbiguous) {
          std::cout << firstWord(headerPointer, headerPointer + headerLength) << '\t';
        } else {
          if (printInvertedIndex) {
            std::cout << firstWord(headerPointer, headerPointer + headerLength) << '\t' 
                << firstWord(queryHeaderPointer, queryHeaderPointer + queryHeaderLength) << '\n';
          } else {
            inverted_index[firstWord(headerPointer, headerPointer + headerLength)].push_back(
                firstWord(queryHeaderPointer, queryHeaderPointer + queryHeaderLength));

            //inverted_index[firstWord(headerPointer, headerPointer + headerLength)].push_back(
            //  firstWord(queryHeaderPointer, queryHeaderPointer + queryHeaderLength));
            if (inverted_index[firstWord(headerPointer, headerPointer + headerLength)].size() == 1) {
              center_nonambig_counts[firstWord(queryHeaderPointer, queryHeaderPointer + queryHeaderLength)] += 1;
            } else if (inverted_index[firstWord(headerPointer, headerPointer + headerLength)].size() == 2) {
              //std::cout << inverted_index[firstWord(headerPointer, headerPointer + headerLength)];
              //std::cout << "Prev index: " << inverted_index[firstWord(headerPointer, headerPointer + headerLength)][0] << "+";
              center_nonambig_counts[inverted_index[firstWord(headerPointer, headerPointer + headerLength)][0]] -= 1;
            }
          }
        }
      }

	  }
	    
	} // if (!marked[lexicographicIndex])

      } // if ((!noOverlap) || resultIterator->cost < radious / 2)

    } // for
	
    if (!assignAmbiguous)
      std::cout << '\n';

  } // Output cluster alignment.

}


int main(int argc, char *argv[])
{

  { // Parse command line options.
    
    const std::string PROGRAM_NAME = "dnaclust";
    const std::string USAGE_DESCRIPTION =
      "  The output is written to STDOUT."
      " Each line will contain the ids of the sequences in each cluster, and the first id of each line is the cluster representative.\n"
      "  Example: To cluster a set of 16S rRNA fragments at 0.98 similarity use:\n"
      "  ./dnaclust file.fasta -l -s 0.98 > clusters \n"
      "  You can optionally specify a k-mer length for the filter."
      " The longer k-mers use more memory. "
      " Also the filter will be more specific with longer k-mers."
      " The default log_4(median length) should be good for most cases.\n"
      ;
#if OLD_CODE
	  "  For the fastest running time please specify k-mer length around log_4(sequence-length).\n"
	  "  Example: To cluster a set of 16S RNA fragments of length around 250 at 0.98 similarity use.\n"
	  "  ./dnaclust file.fasta -l -s 0.98 -k 3 > clusters \n"
#endif // OLD_CODE


    po::options_description desc("Options");

    std::string inputFileName;
    std::string predeterminedCentersFileName;
    
    desc.add_options()
      ("help,h", "produce help message")
      ("similarity,s", po::value<float>(&similarity)->default_value(DEFAULT_SIMILARITY), "set similarity between cluster center and cluster sequences")
      ("input-file,i", po::value<std::string>(&inputFileName), "input file")
      ("predetermined-cluster-centers,p", po::value<std::string>(&predeterminedCentersFileName), "file containing predetermined cluster centers")
      ("recruit-only,r", po::bool_switch(&recruit_only), "when used with 'predetermined-cluster-centers' option, only clusters the input sequences that are similar to the predetermined centers")
      //("multiple-alignment,m", po::bool_switch(&outputMultipleAlignment)->default_value(false), "produce multiple alignment for each cluster")
      ("header,d", "output header line indicating run options")
      ("left-gaps-allowed,l", "allow for gaps on the left of shorter string in semi-global alignment")
      ("k-mer-length,k", po::value<int>(&k_mer_length)/*->default_value(DEFAULT_K_MER_LENGTH) */, "length of k-mer for filtering")
#if OLD_CODE
      ("approximate-filter,a", po::bool_switch(&approximateFilter)->default_value(false), "use faster approximate k-mer filter")
      ("approximation-factor,f", po::value<double>(&approximationFactor)->default_value(0.5), "determine amount of approximation, by a floating point value between zero and one. smaller values result in more approximation.")
      ("approximate-filter2", po::bool_switch(&approximateFilter2)->default_value(false), "use second type of approximate k-mer filter")
#endif // OLC_CODE
      ("approximate-filter", po::bool_switch(&approximateFilter2)->default_value(false), "use faster approximate k-mer filter")
      ("k-mer-filter", po::bool_switch(&kmerFilter)->default_value(false), "use k-mer filter")
      ("no-k-mer-filter", po::bool_switch(&noKmerFilter)->default_value(true), "do not use k-mer filter")
      ("no-overlap", po::bool_switch(&noOverlap)->default_value(false), "cluster some of sequences such that the cluster centers are at distance at least two times the radius of the clusters")
      ("threads,t", po::value<size_t>(&num_threads), "number of threads")
      ("use-full-query-header,u", po::bool_switch(&useFullQueryHeader)->default_value(false), "use the full query header instead of the first word")
      ("mismatches,m", po::value<float>(&mismatches)->default_value(DEFAULT_MISMATCHES), "number of mismatches allowed from cluster center")
      ("assign-ambiguous,a", po::bool_switch(&assignAmbiguous), "assign ambiguous reads to clusters based on abundances of non-ambiguous reads")
      ("random-seed,e", po::value<int>(&random_seed), "Seed for random number generator")
      ("print-inverted-index", po::bool_switch(&printInvertedIndex), "Print mapping from sequence to each center")
      ;

    po::positional_options_description pod;
    pod.add("input-file", -1);

    typedef std::vector<std::string> StringVector;
    StringVector options(argv, argv + argc);
    
    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(desc).positional(pod).run(), vm);
    notify(vm);
    
    if(vm.count("help") || 
       (inputFileName.empty()))
      {
	std::cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] FastaFile\n"
		  << USAGE_DESCRIPTION
		  << desc << '\n';
	exit(EXIT_FAILURE);
      }

    if(kmerFilter)
      noKmerFilter = false;

    std::ifstream inputFile(inputFileName.c_str());
    inputFile >> inputFasta;

    numberOfInputSequences = inputFasta.size();

    // Initialize all threads.
    for (size_t i = 0; i < MAX_THREADS; ++i) {
      pthread_mutex_init(&searchWorkersMutexes[i], NULL);
      pthread_cond_init(&searchWorkersConds[i], NULL);

      //struct thread_args *args = &args_array[running_threads-1];
      searchWorkersContext[i].id = i;
      pthread_create(&searchWorkers[i], NULL, threadWorker, static_cast<void *>(&searchWorkersContext[i]));
    }
    pthread_mutex_init(&allThreadsMutex, NULL);
    pthread_cond_init(&searchWorkersCompleted, NULL);
    pthread_mutex_init(&running_mutex, NULL);

    if (vm.count("predetermined-cluster-centers")) {
      std::ifstream predeterminedCentersFile(predeterminedCentersFileName.c_str());
      predeterminedCentersFile >> predeterminedCentersFasta;
    }


    if (vm.count("header")){ // Output command line options at the first line of output.
      std::cout << HEADER_INDICATOR;
      for (StringVector::const_iterator i = options.begin(); i != options.end(); ++i)
	std::cout << ' ' << *i;
      std::cout << '\n';
    }

    if (vm.count("left-gaps-allowed")) {
      leftGapsAllowed = true;
    }

    if (vm.count("k-mer-length") == 0) {
      // Try to guess appropriate k-mer length based on median sequence length.

      typedef std::vector<size_t> size_vector;
      size_vector sizes;
      for (sequence::Fasta::const_iterator i = inputFasta.begin(); i != inputFasta.end(); ++i)
	sizes.push_back(i->sequence.length());
      size_t median_size = *median(sizes.begin(), sizes.end());
      k_mer_length = static_cast<int>(floor(log(median_size) / log((double) NUMBER_OF_NUCLEOTIDES)));
    }

    // Initializing this at the beginning is crucial.
    number_of_k_mers = power(static_cast<int>(NUMBER_OF_NUCLEOTIDES), k_mer_length);

  }


  {

    { // Calculate global vectors indexed by 'lexicographical index': lexicographicalSequencePointers, sequenceLengths, headerPointers, headerLengths

      typedef std::pair<ConstCharPointer, Index> PointerIndexPair;
      typedef std::vector<PointerIndexPair> PointerIndexVector;
    
      PointerIndexVector pointerFastaIndexes(numberOfInputSequences);
      lexicographicSequencePointers.resize(numberOfInputSequences);

      for (Index i = 0; i < numberOfInputSequences; ++i) {
	lexicographicSequencePointers[i] = inputFasta[i].sequence.c_str();

	pointerFastaIndexes[i].first = inputFasta[i].sequence.c_str();
	pointerFastaIndexes[i].second = i;
      }

      ternary_sort::ternarySort(lexicographicSequencePointers.data(), numberOfInputSequences);

      std::sort(pointerFastaIndexes.begin(), pointerFastaIndexes.end());
    
      PointerIndexVector pointerLexicographicIndexes(numberOfInputSequences);

      for (Index i = 0; i < numberOfInputSequences; ++i) {
	pointerLexicographicIndexes[i].first = lexicographicSequencePointers[i];
	pointerLexicographicIndexes[i].second = i;
      }

      std::sort(pointerLexicographicIndexes.begin(), pointerLexicographicIndexes.end());

      IndexVector fastaIndexOfLexicographicIndexes(numberOfInputSequences);
      IndexVector lexicographicIndexOfFastaIndexes(numberOfInputSequences);

      for (Index i = 0; i < numberOfInputSequences; ++i) {
	assert(pointerFastaIndexes[i].first == pointerLexicographicIndexes[i].first);
      
	fastaIndexOfLexicographicIndexes[pointerLexicographicIndexes[i].second] = pointerFastaIndexes[i].second;
	lexicographicIndexOfFastaIndexes[pointerFastaIndexes[i].second] = pointerLexicographicIndexes[i].second;

      }

      headerPointers.resize(numberOfInputSequences);
      headerLengths.resize(numberOfInputSequences);
      sequenceLengths.resize(numberOfInputSequences);

      for (Index i = 0; i < numberOfInputSequences; ++i) {
	Index fastaIndex = fastaIndexOfLexicographicIndexes[i];
	headerPointers[i] = inputFasta[fastaIndex].header.c_str();
	headerLengths[i] = inputFasta[fastaIndex].header.length();
	sequenceLengths[i] = inputFasta[fastaIndex].sequence.length();
      }
    } // Calculate global vectors indexed by 'lexicographical index': lexicographicalSequencePointers, sequenceLengths, headerPointers, headerLengths



    typedef std::pair<PositionType, Index> LengthIndexPair;
    typedef std::vector<LengthIndexPair> LengthIndexVector;
#if MY_DEBUG
    typedef std::pair<std::pair<PositionType, ConstCharPointer>, Index> LengthIndexDebugPair;
    typedef std::vector<LengthIndexDebugPair> LengthIndexVector;
#endif // MY_DEBUG
    LengthIndexVector lengthLexicographicIndexes(numberOfInputSequences);
    
    for (Index i = 0; i < numberOfInputSequences; ++i) {

      lengthLexicographicIndexes[i].first = sequenceLengths[i];
      lengthLexicographicIndexes[i].second = i;
#if MY_DEBUG
      // Assume i is fastaIndex;
      Index lexicographicIndex = lexicographicIndexOfFastaIndexes[i];
      lengthLexicographicIndexes[i].first.first = sequenceLengths[lexicographicIndex];
      lengthLexicographicIndexes[i].first.second = lexicographicSequencePointers[lexicographicIndex];
      lengthLexicographicIndexes[i].second = lexicographicIndex;
#endif // MY_DEBUG
    }


    std::sort(lengthLexicographicIndexes.begin(), lengthLexicographicIndexes.end(), std::greater<LengthIndexPair>());
#if MY_DEBUG
    std::sort(lengthLexicographicIndexes.begin(), lengthLexicographicIndexes.end(), std::greater<LengthIndexDebugPair>());
#endif // MY_DEBUG

    if (! noKmerFilter) { 

      spectrumIndexes.resize(numberOfInputSequences);

      for (Index i = 0; i < numberOfInputSequences; ++i) {
	spectrumIndexes[i].first = countKmers(lexicographicSequencePointers[i], sequenceLengths[i]);
	spectrumIndexes[i].second = i;
      }


      
      { // Sorting the k-mer spectrums in a clever way.
	
	{ // Allocate cluster_information
	  cluster_information::l = new Index[2 * numberOfInputSequences];
	  cluster_information::r = new Index[2 * numberOfInputSequences];
	  cluster_information::sizeOfLeftCluster = new Index[2 * numberOfInputSequences];

	  cluster_information::numberOfKmers = new Index[k_mer_length];
	  for (int i = 0; i < k_mer_length; ++i)
	    cluster_information::numberOfKmers[i] = power(static_cast<int>(NUMBER_OF_NUCLEOTIDES), i + 1);

	  cluster_information::allMinCounts = new KmerCount *[k_mer_length];
	  for (int i = 0; i < k_mer_length; ++i)
	    cluster_information::allMinCounts[i] = new KmerCount[cluster_information::numberOfKmers[i] * numberOfInputSequences * 2];

	  cluster_information::allMaxCounts = new KmerCount *[k_mer_length];
	  for (int i = 0; i < k_mer_length; ++i)
	    cluster_information::allMaxCounts[i] = new KmerCount[cluster_information::numberOfKmers[i] * numberOfInputSequences * 2];

	  cluster_information::counts = new int *[k_mer_length];
	  for(int i = 0; i < k_mer_length; ++i)
	    cluster_information::counts[i] = new int[cluster_information::numberOfKmers[i]];

	}

	// Allocate allQueryCounts
	{
	  layered::allQueryCounts = new int *[k_mer_length];
	  for (int i = 0; i < k_mer_length; ++i)
	    layered::allQueryCounts[i] = new int[cluster_information::numberOfKmers[i]];
	}

#if MY_DEBUG
	std::cerr << "Before clusterSort()\n" << std::flush;
#endif // MY_DEBUG

	{
	  clusterSort(0, numberOfInputSequences, 0);


#if MY_DEBUG
	  std::cerr << "After clusterSort.\n" << std::flush;
#endif // MY_DEBUG

	  remainingAtLastDbUpdate = numberOfInputSequences;
	}

      }



#if MY_DEBUG
      for (Index i = 0; i < numberOfInputSequences; ++i) {
	std::cout << i << ":\t";
	for (KmerNumber j = 0; j < NUMBER_OF_K_MERS; ++j)
	  std::cout << static_cast<int>(spectrumIndexes[i].first[j]) << '\t';
	std::cout << '\n';
      }

    exit(EXIT_FAILURE);
#endif // MY_DEBUG

    } // if (! noKmerFilter)

    if (noKmerFilter) {
      sortedStrings = lexicographicSequencePointers;
      lexicographicIndexOfSortedStringsIndexes.resize(sortedStrings.size());
      for (Index i = 0; i < static_cast<Index>(sortedStrings.size()); ++i)
	lexicographicIndexOfSortedStringsIndexes[i] = i;

      // Go through each thread and resize all the threads
      int num_threads_to_use = num_threads;
      int interval_size = static_cast<int>(ceil(sortedStrings.size() / static_cast<double>(num_threads)));//sortedStrings.size() / num_threads_to_use;

      if (sortedStrings.size() < num_threads) {
        num_threads_to_use = 1; //sortedStrings.size(); //1; //sortedStrings.size();
        interval_size = sortedStrings.size();//1;//sortedStrings.size();
      }

      //std::cout << "Using threads: " <<  num_threads_to_use << "\n";
      //flush(std::cout);
      
      
      running_threads = 0;
      //std::cout << "Sorted strings size: " << sortedStrings.size() << "\n"; 
      //flush(std::cout);

#if CHRIS_DEBUG
       std::cout << "Size: " <<  sortedStrings.size() << "\n";
      //std::cout << "Building first trees for table: " << i << " from: " << chunkIndexes[i].first << " to " << end << ", interval_size " << chunkIndexes[i].second << "\n" << std::flush;
#endif // CHRIS_DEBUG

      for (unsigned int i = 0, currentTable = 0; i < sortedStrings.size(); i += interval_size, ++currentTable) {
        unsigned int end = i + interval_size;
        if (i + interval_size > sortedStrings.size()) {
          end = sortedStrings.size();
        }
        else {
          end = i + interval_size;
        }

        sortedStringsLengthsMinTree[currentTable].resize(2 * (end - i));
        sortedStringsLengthsMaxTree[currentTable].resize(2 * (end - i));
  
#if CHRIS_DEBUG
        std::cout << "Building first trees from: " << i << " to " << end << "\n"; 
#endif // CHRIS_DEBUG

        
        buildMinTree(sequenceLengths.begin() + i, sequenceLengths.begin() + end, sortedStringsLengthsMinTree[currentTable].begin());
        buildMaxTree(sequenceLengths.begin() + i, sequenceLengths.begin() + end, sortedStringsLengthsMaxTree[currentTable].begin());
      }

      remainingAtLastDbUpdate = numberOfInputSequences;
    }

    marked.resize(numberOfInputSequences);
    std::fill(marked.begin(), marked.end(), false);

    markedGrey.resize(numberOfInputSequences);
    std::fill(markedGrey.begin(), markedGrey.end(), false);
    
    numberOfMarked = 0;
    

    // This MUST be called before any call to any of the search functions.
    initialize();
    

    // Loop over predetermined cluster centers.
    for (sequence::Fasta::const_iterator predeterminedIterator = predeterminedCentersFasta.begin(); predeterminedIterator != predeterminedCentersFasta.end();  ++predeterminedIterator) {
      querySequencePointer = predeterminedIterator->sequence.c_str();
      querySequenceLength = predeterminedIterator->sequence.length();
      queryHeaderPointer = predeterminedIterator->header.c_str();
      queryHeaderLength = predeterminedIterator->header.length();

      makeCluster();

    } // for


    if (recruit_only) {
      // If we are assigning ambiguous reads based on abundance, handle it here.
      if (assignAmbiguous) {
        /* initialize random seed: */
        if (random_seed == 0) {
          srand(time(NULL));
        } else {
          srand(random_seed);
        }
//        std::cout << "********" << "\n";
      
        typedef std::map< std::string, std::vector<std::string> >::const_iterator MapIterator;
        for (MapIterator iter = inverted_index.begin(); iter != inverted_index.end(); iter++)
        {
            //std::cout << "Key: " << iter->first << std::endl << "Values:" << std::endl;

            if (iter->second.size() == 1) {
              final_clusters[iter->second[0]].push_back(iter->first);
            } 
            else{
              std::vector<std::string> bins;
              typedef std::vector<std::string>::const_iterator ListIterator;
              for (ListIterator center_iter = iter->second.begin(); center_iter != iter->second.end(); center_iter++) {
                  for (int z = 0; z < center_nonambig_counts[*center_iter]; ++z) {
                    bins.push_back(*center_iter);
                  }
              }

              int index = rand() % bins.size();

#if ABUN_DEBUG
              std::cout << "Bins: " << bins << ", selected index: " << index << 
                  "(" << bins[index] << ")" << "\n";
#endif // ABUN_DEBUG

              final_clusters[bins[index]].push_back(iter->first);
  //            std::cout << " " << *list_iter << std::endl;
            }
        }

        for (MapIterator iter = final_clusters.begin(); iter != final_clusters.end(); iter++) {
          std::cout << iter->first << "\t";

          typedef std::vector<std::string>::const_iterator ListIterator;
          for (ListIterator seq_iter = iter->second.begin(); seq_iter != iter->second.end(); seq_iter++) {
            std::cout << *seq_iter << "\t";
          }

          std::cout << "\n";
        }
      }

      return EXIT_SUCCESS;
    }

#if TIMING
    boost::posix_time::ptime beginning = boost::posix_time::second_clock::local_time();
#endif // TIMING

    for (Index i = 0; i < numberOfInputSequences; ++i) {

      Index lexicographicIndex = lengthLexicographicIndexes[i].second;
      if (!marked[lexicographicIndex]) {
	if (!markedGrey[lexicographicIndex]) {
	  markedGrey[lexicographicIndex] = true;
	  marked[lexicographicIndex] = true;
	  ++numberOfMarked;


	if ((numberOfInputSequences - numberOfMarked < remainingAtLastDbUpdate * 0.5) && (remainingAtLastDbUpdate > numberOfInputSequences * 0.05) && (numberOfInputSequences - numberOfMarked > 1)) {
	  if (! noKmerFilter) {

	    Index i = 0;
	    Index j = remainingAtLastDbUpdate - 1;
	  
	    while (i < j) {
	      while (!marked[spectrumIndexes[i].second])
		++i;
	      while (marked[spectrumIndexes[j].second])
		--j;
	      
	      if (i < j) {
		std::swap(spectrumIndexes[i], spectrumIndexes[j]);

		++i;
		--j;
	      } // if (i < j)
	    } // while (i < j)

	    remainingAtLastDbUpdate = numberOfInputSequences - numberOfMarked;

#if TIMING								
	    std::cerr << (boost::posix_time::second_clock::local_time() - beginning)  << '\t' << "Database update.\n" << std::flush;
#endif // TIMING
							
	    clusterSort(0, remainingAtLastDbUpdate, 0);
	    
#if TIMING								
	    std::cerr << (boost::posix_time::second_clock::local_time() - beginning)  << '\t' << "...done.\n" << std::flush;
#endif // TIMING				
	  } // if (! noKmerFilter)
	  else { // No k-mer filter

	    ConstCharPointerVector newSortedStrings;
	    PositionVector newSortedStringsLengths;
	    IndexVector newLexicographicIndexOfSortedStringsIndexes;
	  

	    for (Index i = 0; i < static_cast<Index>(sortedStrings.size()); ++i) {
	      Index lexicographicIndex = lexicographicIndexOfSortedStringsIndexes[i];
	      if (! marked[lexicographicIndex]){
		newSortedStrings.push_back(lexicographicSequencePointers[lexicographicIndex]);
		newLexicographicIndexOfSortedStringsIndexes.push_back(lexicographicIndex);
		newSortedStringsLengths.push_back(sequenceLengths[lexicographicIndex]);
	      }
	    }
					 
	    sortedStrings.swap(newSortedStrings);
	    lexicographicIndexOfSortedStringsIndexes.swap(newLexicographicIndexOfSortedStringsIndexes);
	  
      size_t num_threads_to_use = num_threads;
      int interval_size = static_cast<int>(ceil(sortedStrings.size() / static_cast<double>(num_threads)));//sortedStrings.size() / num_threads_to_use;

      if (sortedStrings.size() < num_threads) {
        num_threads_to_use = 1;//sortedStrings.size(); //1;
        interval_size = sortedStrings.size();//1; //sortedStrings.size();
      }
      
#if CHRIS_DEBUG
       std::cout << "Size: " <<  sortedStrings.size() << "\n";
      //std::cout << "Building first trees for table: " << i << " from: " << chunkIndexes[i].first << " to " << end << ", interval_size " << chunkIndexes[i].second << "\n" << std::flush;
#endif // CHRIS_DEBUG
      
      for (unsigned int i = 0, currentTable = 0; i < sortedStrings.size() ; i += interval_size, ++currentTable) {

        unsigned int end = i + interval_size;
        if (i + interval_size > sortedStrings.size()) {
          end = sortedStrings.size();
        }
        else {
          end = i + interval_size;
        }
        sortedStringsLengthsMinTree[currentTable].resize(2 * (end - i));
        sortedStringsLengthsMaxTree[currentTable].resize(2 * (end - i));

#if CHRIS_DEBUG
        std::cout << "Building second trees from: " << i << " to " << end << "\n" << std::flush; 
        std::cout << "Size of sorted string min/max: " << sortedStringsLengthsMinTree[currentTable].size() << ", " 
            << sortedStringsLengthsMaxTree[currentTable].size() << "\n" << std::flush;
        //std::cout << "Building first trees for table: " << i << " from: " << chunkIndexes[i].first << " to " << end << ", interval_size " << chunkIndexes[i].second << "\n" << std::flush;
#endif // CHRIS_DEBUG
        buildMinTree(newSortedStringsLengths.begin() + i, newSortedStringsLengths.begin() + end, sortedStringsLengthsMinTree[currentTable].begin());
        buildMaxTree(newSortedStringsLengths.begin() + i, newSortedStringsLengths.begin() + end, sortedStringsLengthsMaxTree[currentTable].begin());
      }

	    remainingAtLastDbUpdate = numberOfInputSequences - numberOfMarked;
	  } // else
	} // if ((numberOfInputSequences - numberOfMarked < remainingAtLastDbUpdate * 0.5) && (remainingAtLastDbUpdate > numberOfInputSequences * 0.05))
	querySequencePointer = lexicographicSequencePointers[lexicographicIndex];
	querySequenceLength = sequenceLengths[lexicographicIndex];
	queryHeaderPointer = headerPointers[lexicographicIndex];
	queryHeaderLength = headerLengths[lexicographicIndex];

	makeCluster();
	
	} // if (!markedGrey[lexicographicIndex])
      } // if (!marked[lexicographicIndex])


    } // for (Index i = 0; i < numberOfInputSequences; ++i)

    return EXIT_SUCCESS;
  }
} // main