forked from mothur/mothur
/
hcluster.cpp
807 lines (681 loc) · 24.9 KB
/
hcluster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
* hcluster.cpp
* Mothur
*
* Created by westcott on 10/13/09.
* Copyright 2009 Schloss Lab. All rights reserved.
*
*/
#include "hcluster.h"
#include "rabundvector.hpp"
#include "listvector.hpp"
/***********************************************************************/
HCluster::HCluster(RAbundVector* rav, ListVector* lv, string ms, string d, NameAssignment* n, float c) : rabund(rav), list(lv), method(ms), distfile(d), nameMap(n), cutoff(c) {
try {
m = MothurOut::getInstance();
mapWanted = false;
exitedBreak = false;
numSeqs = list->getNumSeqs();
//initialize cluster array
for (int i = 0; i < numSeqs; i++) {
clusterNode temp(1, -1, i);
clusterArray.push_back(temp);
}
if ((method == "furthest") || (method == "nearest")) {
m->openInputFile(distfile, filehandle);
}else{
processFile();
}
}
catch(exception& e) {
m->errorOut(e, "HCluster", "HCluster");
exit(1);
}
}
/***********************************************************************/
void HCluster::clusterBins(){
try {
//cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(clusterArray[smallRow].smallChild) << '\t' << rabund->get(clusterArray[smallCol].smallChild);
rabund->set(clusterArray[smallCol].smallChild, rabund->get(clusterArray[smallRow].smallChild)+rabund->get(clusterArray[smallCol].smallChild));
rabund->set(clusterArray[smallRow].smallChild, 0);
rabund->setLabel(toString(smallDist));
//cout << '\t' << rabund->get(clusterArray[smallRow].smallChild) << '\t' << rabund->get(clusterArray[smallCol].smallChild) << endl;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "clusterBins");
exit(1);
}
}
/***********************************************************************/
void HCluster::clusterNames(){
try {
///cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(clusterArray[smallRow].smallChild) << '\t' << list->get(clusterArray[smallCol].smallChild);
if (mapWanted) { updateMap(); }
list->set(clusterArray[smallCol].smallChild, list->get(clusterArray[smallRow].smallChild)+','+list->get(clusterArray[smallCol].smallChild));
list->set(clusterArray[smallRow].smallChild, "");
list->setLabel(toString(smallDist));
//cout << '\t' << list->get(clusterArray[smallRow].smallChild) << '\t' << list->get(clusterArray[smallCol].smallChild) << endl;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "clusterNames");
exit(1);
}
}
/***********************************************************************/
int HCluster::getUpmostParent(int node){
try {
while (clusterArray[node].parent != -1) {
node = clusterArray[node].parent;
}
return node;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "getUpmostParent");
exit(1);
}
}
/***********************************************************************/
void HCluster::printInfo(){
try {
cout << "link table" << endl;
for (itActive = activeLinks.begin(); itActive!= activeLinks.end(); itActive++) {
cout << itActive->first << " = " << itActive->second << endl;
}
cout << endl;
for (int i = 0; i < linkTable.size(); i++) {
cout << i << '\t';
for (it = linkTable[i].begin(); it != linkTable[i].end(); it++) {
cout << it->first << '-' << it->second << '\t' ;
}
cout << endl;
}
cout << endl << "clusterArray" << endl;
for (int i = 0; i < clusterArray.size(); i++) {
cout << i << '\t' << clusterArray[i].numSeq << '\t' << clusterArray[i].parent << '\t' << clusterArray[i].smallChild << endl;
}
cout << endl;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "getUpmostParent");
exit(1);
}
}
/***********************************************************************/
int HCluster::makeActive() {
try {
int linkValue = 1;
itActive = activeLinks.find(smallRow);
it2Active = activeLinks.find(smallCol);
if ((itActive == activeLinks.end()) && (it2Active == activeLinks.end())) { //both are not active so add them
int size = linkTable.size();
map<int, int> temp; map<int, int> temp2;
//add link to eachother
temp[smallRow] = 1; // 1 2
temp2[smallCol] = 1; // 1 0 1
// 2 1 0
linkTable.push_back(temp);
linkTable.push_back(temp2);
//add to activeLinks
activeLinks[smallRow] = size;
activeLinks[smallCol] = size+1;
}else if ((itActive != activeLinks.end()) && (it2Active == activeLinks.end())) { //smallRow is active, smallCol is not
int size = linkTable.size();
int alreadyActiveRow = itActive->second;
map<int, int> temp;
//add link to eachother
temp[smallRow] = 1; // 6 2 3 5
linkTable.push_back(temp); // 6 0 1 2 0
linkTable[alreadyActiveRow][smallCol] = 1; // 2 1 0 1 1
// 3 2 1 0 0
// 5 0 1 0 0
//add to activeLinks
activeLinks[smallCol] = size;
}else if ((itActive == activeLinks.end()) && (it2Active != activeLinks.end())) { //smallCol is active, smallRow is not
int size = linkTable.size();
int alreadyActiveCol = it2Active->second;
map<int, int> temp;
//add link to eachother
temp[smallCol] = 1; // 6 2 3 5
linkTable.push_back(temp); // 6 0 1 2 0
linkTable[alreadyActiveCol][smallRow] = 1; // 2 1 0 1 1
// 3 2 1 0 0
// 5 0 1 0 0
//add to activeLinks
activeLinks[smallRow] = size;
}else { //both are active so add one
int row = itActive->second;
int col = it2Active->second;
linkTable[row][smallCol]++;
linkTable[col][smallRow]++;
linkValue = linkTable[row][smallCol];
}
return linkValue;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "makeActive");
exit(1);
}
}
/***********************************************************************/
void HCluster::updateArrayandLinkTable() {
try {
//if cluster was made update clusterArray and linkTable
int size = clusterArray.size();
//add new node
clusterNode temp(clusterArray[smallRow].numSeq + clusterArray[smallCol].numSeq, -1, clusterArray[smallCol].smallChild);
clusterArray.push_back(temp);
//update child nodes
clusterArray[smallRow].parent = size;
clusterArray[smallCol].parent = size;
if (method == "furthest") {
//update linkTable by merging clustered rows and columns
int rowSpot = activeLinks[smallRow];
int colSpot = activeLinks[smallCol];
//fix old rows
for (int i = 0; i < linkTable.size(); i++) {
//check if they are in map
it = linkTable[i].find(smallRow);
it2 = linkTable[i].find(smallCol);
if ((it!=linkTable[i].end()) && (it2!=linkTable[i].end())) { //they are both there
linkTable[i][size] = linkTable[i][smallRow]+linkTable[i][smallCol];
linkTable[i].erase(smallCol); //delete col row
linkTable[i].erase(smallRow); //delete col row
}else if ((it==linkTable[i].end()) && (it2!=linkTable[i].end())) { //only col
linkTable[i][size] = linkTable[i][smallCol];
linkTable[i].erase(smallCol); //delete col
}else if ((it!=linkTable[i].end()) && (it2==linkTable[i].end())) { //only row
linkTable[i][size] = linkTable[i][smallRow];
linkTable[i].erase(smallRow); //delete col
}
}
//merge their values
for (it = linkTable[rowSpot].begin(); it != linkTable[rowSpot].end(); it++) {
it2 = linkTable[colSpot].find(it->first); //does the col also have this
if (it2 == linkTable[colSpot].end()) { //not there so add it
linkTable[colSpot][it->first] = it->second;
}else { //merge them
linkTable[colSpot][it->first] = it->second + it2->second;
}
}
linkTable[colSpot].erase(size);
linkTable.erase(linkTable.begin()+rowSpot); //delete row
//update activerows
activeLinks.erase(smallRow);
activeLinks.erase(smallCol);
activeLinks[size] = colSpot;
//adjust everybody elses spot since you deleted - time vs. space
for (itActive = activeLinks.begin(); itActive != activeLinks.end(); itActive++) {
if (itActive->second > rowSpot) { activeLinks[itActive->first]--; }
}
}
}
catch(exception& e) {
m->errorOut(e, "HCluster", "updateArrayandLinkTable");
exit(1);
}
}
/***********************************************************************/
double HCluster::update(int row, int col, float distance){
try {
bool cluster = false;
smallRow = row;
smallCol = col;
smallDist = distance;
//find upmost parent of row and col
smallRow = getUpmostParent(smallRow);
smallCol = getUpmostParent(smallCol);
//you don't want to cluster with yourself
if (smallRow != smallCol) {
if ((method == "furthest") || (method == "nearest")) {
//can we cluster???
if (method == "nearest") { cluster = true; }
else{ //assume furthest
//are they active in the link table
int linkValue = makeActive(); //after this point this nodes info is active in linkTable
if (linkValue == (clusterArray[smallRow].numSeq * clusterArray[smallCol].numSeq)) { cluster = true; }
}
if (cluster) {
updateArrayandLinkTable();
clusterBins();
clusterNames();
}
}else {
cluster = true;
updateArrayandLinkTable();
clusterBins();
clusterNames();
combineFile();
}
}
return cutoff;
//printInfo();
}
catch(exception& e) {
m->errorOut(e, "HCluster", "update");
exit(1);
}
}
/***********************************************************************/
void HCluster::setMapWanted(bool ms) {
try {
mapWanted = ms;
//initialize map
for (int i = 0; i < list->getNumBins(); i++) {
//parse bin
string names = list->get(i);
while (names.find_first_of(',') != -1) {
//get name from bin
string name = names.substr(0,names.find_first_of(','));
//save name and bin number
seq2Bin[name] = i;
names = names.substr(names.find_first_of(',')+1, names.length());
}
//get last name
seq2Bin[names] = i;
}
}
catch(exception& e) {
m->errorOut(e, "HCluster", "setMapWanted");
exit(1);
}
}
/***********************************************************************/
void HCluster::updateMap() {
try {
//update location of seqs in smallRow since they move to smallCol now
string names = list->get(clusterArray[smallRow].smallChild);
while (names.find_first_of(',') != -1) {
//get name from bin
string name = names.substr(0,names.find_first_of(','));
//save name and bin number
seq2Bin[name] = clusterArray[smallCol].smallChild;
names = names.substr(names.find_first_of(',')+1, names.length());
}
//get last name
seq2Bin[names] = clusterArray[smallCol].smallChild;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "updateMap");
exit(1);
}
}
//**********************************************************************************************************************
vector<seqDist> HCluster::getSeqs(){
try {
vector<seqDist> sameSeqs;
if ((method == "furthest") || (method == "nearest")) {
sameSeqs = getSeqsFNNN();
}else{
sameSeqs = getSeqsAN();
}
return sameSeqs;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "getSeqs");
exit(1);
}
}
//**********************************************************************************************************************
vector<seqDist> HCluster::getSeqsFNNN(){
try {
string firstName, secondName;
float distance, prevDistance;
vector<seqDist> sameSeqs;
prevDistance = -1;
//if you are not at the beginning of the file
if (exitedBreak) {
sameSeqs.push_back(next);
prevDistance = next.dist;
exitedBreak = false;
}
//get entry
while (!filehandle.eof()) {
filehandle >> firstName >> secondName >> distance; m->gobble(filehandle);
//save first one
if (prevDistance == -1) { prevDistance = distance; }
map<string,int>::iterator itA = nameMap->find(firstName);
map<string,int>::iterator itB = nameMap->find(secondName);
if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
//using cutoff
if (distance > cutoff) { break; }
if (distance != -1) { //-1 means skip me
//are the distances the same
if (distance == prevDistance) { //save in vector
seqDist temp(itA->second, itB->second, distance);
sameSeqs.push_back(temp);
exitedBreak = false;
}else{
next.seq1 = itA->second;
next.seq2 = itB->second;
next.dist = distance;
exitedBreak = true;
break;
}
}
}
//rndomize matching dists
random_shuffle(sameSeqs.begin(), sameSeqs.end());
return sameSeqs;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "getSeqsFNNN");
exit(1);
}
}
//**********************************************************************************************************************
vector<seqDist> HCluster::getSeqsAN(){
try {
int firstName, secondName;
float prevDistance;
vector<seqDist> sameSeqs;
prevDistance = -1;
m->openInputFile(distfile, filehandle, "no error");
//is the smallest value in mergedMin or the distfile?
float mergedMinDist = 10000;
float distance = 10000;
if (mergedMin.size() > 0) { mergedMinDist = mergedMin[0].dist; }
if (!filehandle.eof()) {
filehandle >> firstName >> secondName >> distance; m->gobble(filehandle);
//save first one
if (prevDistance == -1) { prevDistance = distance; }
if (distance != -1) { //-1 means skip me
seqDist temp(firstName, secondName, distance);
sameSeqs.push_back(temp);
}else{ distance = 10000; }
}
if (mergedMinDist < distance) { //get minimum distance from mergedMin
//remove distance we saved from file
sameSeqs.clear();
prevDistance = mergedMinDist;
for (int i = 0; i < mergedMin.size(); i++) {
if (mergedMin[i].dist == prevDistance) {
sameSeqs.push_back(mergedMin[i]);
}else { break; }
}
}else{ //get minimum from file
//get entry
while (!filehandle.eof()) {
filehandle >> firstName >> secondName >> distance; m->gobble(filehandle);
if (prevDistance == -1) { prevDistance = distance; }
if (distance != -1) { //-1 means skip me
//are the distances the same
if (distance == prevDistance) { //save in vector
seqDist temp(firstName, secondName, distance);
sameSeqs.push_back(temp);
}else{
break;
}
}
}
}
filehandle.close();
//randomize matching dists
random_shuffle(sameSeqs.begin(), sameSeqs.end());
//can only return one value since once these are merged the other distances in sameSeqs may have changed
vector<seqDist> temp;
if (sameSeqs.size() > 0) { temp.push_back(sameSeqs[0]); }
return temp;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "getSeqsAN");
exit(1);
}
}
/***********************************************************************/
int HCluster::combineFile() {
try {
//int bufferSize = 64000; //512k - this should be a variable that the user can set to optimize code to their hardware
//char* inputBuffer;
//inputBuffer = new char[bufferSize];
//size_t numRead;
string tempDistFile = distfile + ".temp";
ofstream out;
m->openOutputFile(tempDistFile, out);
//FILE* in;
//in = fopen(distfile.c_str(), "rb");
ifstream in;
m->openInputFile(distfile, in, "no error");
int first, second;
float dist;
vector< map<int, float> > smallRowColValues;
smallRowColValues.resize(2); //0 = row, 1 = col
int count = 0;
//go through file pulling out distances related to rows merging
//if mergedMin contains distances add those back into file
//bool done = false;
//partialDist = "";
//while ((numRead = fread(inputBuffer, 1, bufferSize, in)) != 0) {
//cout << "number of char read = " << numRead << endl;
//cout << inputBuffer << endl;
//if (numRead < bufferSize) { done = true; }
//parse input into individual distances
//int spot = 0;
//string outputString = "";
//while(spot < numRead) {
//cout << "spot = " << spot << endl;
// seqDist nextDist = getNextDist(inputBuffer, spot, bufferSize);
//you read a partial distance
// if (nextDist.seq1 == -1) { break; }
while (!in.eof()) {
//first = nextDist.seq1; second = nextDist.seq2; dist = nextDist.dist;
//cout << "next distance = " << first << '\t' << second << '\t' << dist << endl;
//since file is sorted and mergedMin is sorted
//you can put the smallest distance from each through the code below and keep the file sorted
in >> first >> second >> dist; m->gobble(in);
if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempDistFile); return 0; }
//while there are still values in mergedMin that are smaller than the distance read from file
while (count < mergedMin.size()) {
//is the distance in mergedMin smaller than from the file
if (mergedMin[count].dist < dist) {
//is this a distance related to the columns merging?
//if yes, save in memory
if ((mergedMin[count].seq1 == smallRow) && (mergedMin[count].seq2 == smallCol)) { //do nothing this is the smallest distance from last time
}else if (mergedMin[count].seq1 == smallCol) {
smallRowColValues[1][mergedMin[count].seq2] = mergedMin[count].dist;
}else if (mergedMin[count].seq2 == smallCol) {
smallRowColValues[1][mergedMin[count].seq1] = mergedMin[count].dist;
}else if (mergedMin[count].seq1 == smallRow) {
smallRowColValues[0][mergedMin[count].seq2] = mergedMin[count].dist;
}else if (mergedMin[count].seq2 == smallRow) {
smallRowColValues[0][mergedMin[count].seq1] = mergedMin[count].dist;
}else { //if no, write to temp file
//outputString += toString(mergedMin[count].seq1) + '\t' + toString(mergedMin[count].seq2) + '\t' + toString(mergedMin[count].dist) + '\n';
//if (mergedMin[count].dist < cutoff) {
out << mergedMin[count].seq1 << '\t' << mergedMin[count].seq2 << '\t' << mergedMin[count].dist << endl;
//}
}
count++;
}else{ break; }
}
//is this a distance related to the columns merging?
//if yes, save in memory
if ((first == smallRow) && (second == smallCol)) { //do nothing this is the smallest distance from last time
}else if (first == smallCol) {
smallRowColValues[1][second] = dist;
}else if (second == smallCol) {
smallRowColValues[1][first] = dist;
}else if (first == smallRow) {
smallRowColValues[0][second] = dist;
}else if (second == smallRow) {
smallRowColValues[0][first] = dist;
}else { //if no, write to temp file
//outputString += toString(first) + '\t' + toString(second) + '\t' + toString(dist) + '\n';
//if (dist < cutoff) {
out << first << '\t' << second << '\t' << dist << endl;
//}
}
}
//out << outputString;
//if(done) { break; }
//}
//fclose(in);
in.close();
//if values in mergedMin are larger than the the largest in file then
while (count < mergedMin.size()) {
//is this a distance related to the columns merging?
//if yes, save in memory
if ((mergedMin[count].seq1 == smallRow) && (mergedMin[count].seq2 == smallCol)) { //do nothing this is the smallest distance from last time
}else if (mergedMin[count].seq1 == smallCol) {
smallRowColValues[1][mergedMin[count].seq2] = mergedMin[count].dist;
}else if (mergedMin[count].seq2 == smallCol) {
smallRowColValues[1][mergedMin[count].seq1] = mergedMin[count].dist;
}else if (mergedMin[count].seq1 == smallRow) {
smallRowColValues[0][mergedMin[count].seq2] = mergedMin[count].dist;
}else if (mergedMin[count].seq2 == smallRow) {
smallRowColValues[0][mergedMin[count].seq1] = mergedMin[count].dist;
}else { //if no, write to temp file
//if (mergedMin[count].dist < cutoff) {
out << mergedMin[count].seq1 << '\t' << mergedMin[count].seq2 << '\t' << mergedMin[count].dist << endl;
//}
}
count++;
}
out.close();
mergedMin.clear();
//rename tempfile to distfile
m->mothurRemove(distfile);
rename(tempDistFile.c_str(), distfile.c_str());
//cout << "remove = "<< renameOK << " rename = " << ok << endl;
//merge clustered rows averaging the distances
map<int, float>::iterator itMerge;
map<int, float>::iterator it2Merge;
for(itMerge = smallRowColValues[0].begin(); itMerge != smallRowColValues[0].end(); itMerge++) {
//does smallRowColValues[1] have a distance to this seq too?
it2Merge = smallRowColValues[1].find(itMerge->first);
float average;
if (it2Merge != smallRowColValues[1].end()) { //if yes, then average
//average
if (method == "average") {
int total = clusterArray[smallRow].numSeq + clusterArray[smallCol].numSeq;
average = ((clusterArray[smallRow].numSeq * itMerge->second) + (clusterArray[smallCol].numSeq * it2Merge->second)) / (float) total;
}else { //weighted
average = ((itMerge->second * 1.0) + (it2Merge->second * 1.0)) / (float) 2.0;
}
smallRowColValues[1].erase(it2Merge);
seqDist temp(clusterArray[smallRow].parent, itMerge->first, average);
mergedMin.push_back(temp);
}else {
//can't find value so update cutoff
if (cutoff > itMerge->second) { cutoff = itMerge->second; }
}
}
//update cutoff
for(itMerge = smallRowColValues[1].begin(); itMerge != smallRowColValues[1].end(); itMerge++) {
if (cutoff > itMerge->second) { cutoff = itMerge->second; }
}
//sort merged values
sort(mergedMin.begin(), mergedMin.end(), compareSequenceDistance);
return 0;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "combineFile");
exit(1);
}
}
/***********************************************************************
seqDist HCluster::getNextDist(char* buffer, int& index, int size){
try {
seqDist next;
int indexBefore = index;
string first, second, distance;
first = ""; second = ""; distance = "";
int tabCount = 0;
//cout << "partial = " << partialDist << endl;
if (partialDist != "") { //read what you can, you know it is less than a whole distance.
for (int i = 0; i < partialDist.size(); i++) {
if (tabCount == 0) {
if (partialDist[i] == '\t') { tabCount++; }
else { first += partialDist[i]; }
}else if (tabCount == 1) {
if (partialDist[i] == '\t') { tabCount++; }
else { second += partialDist[i]; }
}else if (tabCount == 2) {
distance += partialDist[i];
}
}
partialDist = "";
}
//try to get another distance
bool gotDist = false;
while (index < size) {
if ((buffer[index] == 10) || (buffer[index] == 13)) { //newline in unix or windows
gotDist = true;
//m->gobble space
while (index < size) {
if (isspace(buffer[index])) { index++; }
else { break; }
}
break;
}else{
if (tabCount == 0) {
if (buffer[index] == '\t') { tabCount++; }
else { first += buffer[index]; }
}else if (tabCount == 1) {
if (buffer[index] == '\t') { tabCount++; }
else { second += buffer[index]; }
}else if (tabCount == 2) {
distance += buffer[index];
}
index++;
}
}
//there was not a whole distance in the buffer, ie. buffer = "1 2 0.01 2 3 0."
//then you want to save the partial distance.
if (!gotDist) {
for (int i = indexBefore; i < size; i++) {
partialDist += buffer[i];
}
index = size + 1;
next.seq1 = -1; next.seq2 = -1; next.dist = 0.0;
}else{
int firstname, secondname;
float dist;
convert(first, firstname);
convert(second, secondname);
convert(distance, dist);
next.seq1 = firstname; next.seq2 = secondname; next.dist = dist;
}
return next;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "getNextDist");
exit(1);
}
}
***********************************************************************/
int HCluster::processFile() {
try {
string firstName, secondName;
float distance;
ifstream in;
m->openInputFile(distfile, in, "no error");
ofstream out;
string outTemp = distfile + ".temp";
m->openOutputFile(outTemp, out);
//get entry
while (!in.eof()) {
if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outTemp); return 0; }
in >> firstName >> secondName >> distance; m->gobble(in);
map<string,int>::iterator itA = nameMap->find(firstName);
map<string,int>::iterator itB = nameMap->find(secondName);
if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
//using cutoff
if (distance > cutoff) { break; }
if (distance != -1) { //-1 means skip me
out << itA->second << '\t' << itB->second << '\t' << distance << endl;
}
}
in.close();
out.close();
m->mothurRemove(distfile);
rename(outTemp.c_str(), distfile.c_str());
return 0;
}
catch(exception& e) {
m->errorOut(e, "HCluster", "processFile");
exit(1);
}
}
/***********************************************************************/