forked from jfmrod/hpc-clust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpc-clust.cpp
560 lines (462 loc) · 19.3 KB
/
hpc-clust.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
#include <eutils/emain.h>
#include <eutils/estrarray.h>
#include <eutils/efile.h>
#include <eutils/eheap.h>
#include <eutils/etime.h>
#include <eutils/etimer.h>
#include <eutils/eoption.h>
//#include <eutils/eparalgor.h>
#include <eutils/esystem.h>
#include "hpc-clust-config.h"
#include "cluster-common.h"
#include "eseqclusteravg.h"
eblockarray<eseqdist> dists;
eseqcluster clcluster; // complete linkage
eseqclustersingle slcluster; // single linkage
eseqclusteravg alcluster; // avg linkage
long partsFinished=0;
long partsTotal=10000;
estrarray arr;
unsigned totaldists;
int seqlen=0;
bool ignoreUnique=false;
float radixKey(eblockarray<eseqdist>& dists,long int i)
{
return(dists[i].dist);
}
estr args2str(int argvc,char **argv)
{
estr tmpstr;
int i;
for (i=0; i<argvc; ++i)
tmpstr+=estr(argv[i])+" ";
tmpstr.del(-1);
return(tmpstr);
}
emutex mutex;
void help()
{
printf("HPC-CLUST v%s\n",HPC_CLUST_PACKAGE_VERSION);
printf("by Joao F. Matias Rodrigues and Christian von Mering\n");
printf("Institute of Molecular Life Sciences, University of Zurich, Switzerland\n");
printf("Matias Rodrigues JF, Mering C von. HPC-CLUST: Distributed hierarchical clustering for very large sets of nucleotide sequences. Bioinformatics. 2013.\n");
printf("\n");
printf("Usage:\n");
printf(" %s [...] <-sl true|-cl true|-al true> alignedseqs.fa\n",efile(argv[0]).basename()._str);
printf("\n");
printf("Clusters a set of multiple aligned sequences in fasta or stockholm format to a given threshold.\n");
printf("Example: hpc-clust -nthreads 4 -t 0.8 -dfunc gap -sl true alignedseqs.fa\n");
printf("\n");
printf("Optional arguments:\n");
printf("%10s %s\n","-t","distance threshold until which to do the clustering [default: 0.9]");
printf("%10s %s\n","-dfunc","distance function to use: gap, nogap, tamura [default: gap]");
printf("%10s %s\n","-nthreads","number of threads to use [default: 4]");
printf("%10s %s\n","-ofile","output filename [defaults to input filename] + \".sl\",\".cl\", or \".al\" extension");
printf("\n");
printf("One or more is required:\n");
printf("%10s %s\n","-sl true","perform single-linkage clustering");
printf("%10s %s\n","-cl true","perform complete-linkage clustering");
printf("%10s %s\n","-al true","perform average-linkage clustering");
printf("\n");
printf("After clustering:\n");
printf("%10s %s\n","-makeotus <alignment> <mergelog> <threshold>","generate an OTU file at a given threshold");
printf("%10s %s\n","-makeotus_mothur <alignment> <mergelog> <threshold>","generate a MOTHUR compatible OTU file at a given threshold");
printf("%10s %s\n","-makereps <alignment> <otu>","generate a fasta file of OTU representatives. Sequences chosen have the minimum average distance to other sequences in the OTU.");
printf("\n");
printf("Report bugs to: [email protected]\n");
printf("http://meringlab.org/software/hpc-clust/\n");
exit(0);
}
typedef float (*t_dfunc)(const estr&,const estr&,int);
class edistfunc
{
public:
efunc calcfunc;
t_dfunc calcfunc_single;
edistfunc(const efunc& _calcfunc,t_dfunc _calcfunc_single): calcfunc(_calcfunc),calcfunc_single(_calcfunc_single) {}
};
eoption<edistfunc> dfuncpart;
eoption<edistfunc> dfunc;
etaskman taskman;
int nthreads=4;
int winlen=70;
float t=0.90;
void finduniq(ebasicarray<INDTYPE>& uniqind,earray<ebasicarray<INDTYPE> >& dupslist)
{
ebasicstrhashof<long> duphash;
ebasicstrhashof<long>::iter it;
if (!ignoreUnique){
duphash.reserve(arr.size());
for (long i=0; i<arr.size(); ++i){
if (i%1000==0)
fprintf(stderr,"\r%li/%li",i,(long)arr.size());
it=duphash.get(arr.values(i));
if (it==duphash.end())
{ uniqind.add(i); duphash.add(arr.values(i),uniqind.size()-1); dupslist.add(ebasicarray<INDTYPE>(i)); }
else
dupslist[it.value()].add(i);
}
fprintf(stderr,"\r%li\n",(long)arr.size());
}else{
uniqind.init(arr.size());
for (long i=0; i<uniqind.size(); ++i)
uniqind[i]=i;
}
cout << endl;
}
void actionMakeOtusMothur()
{
estrarray uarr;
eseqclusterData cdata;
ldieif(argvc<4,"syntax: "+efile(argv[0]).basename()+" -makeotus_mothur <alignment> <mergelog> <cutoff>");
cout << "# loading seqs file: " << argv[1] << endl;
load_seqs(argv[1],uarr);
cdata.load(argv[2],uarr.size());
float t=estr(argv[3]).f();
earray<eintarray> otuarr;
cdata.getOTU(t,otuarr,uarr.size());
cout << "label\tnumOtus";
for (long i=0; i<otuarr.size(); ++i)
cout << "\tOTU" << i;
cout << endl;
cout << (1.0-t) << "\t" << otuarr.size();
for (long i=0; i<otuarr.size(); ++i){
// cout << ">OTU" << i << " otu_size="<< otuarr[i].size() << endl;
cout << "\t" << uarr.keys(otuarr[i][0]);
for (long j=1; j<otuarr[i].size(); ++j)
cout << "," << uarr.keys(otuarr[i][j]);
}
cout << endl;
exit(0);
}
void actionMakeOtus()
{
estrarray uarr;
eseqclusterData cdata;
ldieif(argvc<4,"syntax: "+efile(argv[0]).basename()+" -makeotus <alignment> <mergelog> <cutoff>");
cout << "# loading seqs file: " << argv[1] << endl;
load_seqs(argv[1],uarr);
cdata.load(argv[2],uarr.size());
float t=estr(argv[3]).f();
earray<eintarray> otuarr;
cdata.getOTU(t,otuarr,uarr.size());
for (long i=0; i<otuarr.size(); ++i){
cout << ">OTU" << i << " otu_size="<< otuarr[i].size() << endl;
for (long j=0; j<otuarr[i].size(); ++j)
cout << uarr.keys(otuarr[i][j]) << endl;
}
exit(0);
}
void actionMakeReps()
{
ldieif(argvc<3,"syntax: "+efile(argv[0]).basename()+" -makereps <alignment> <otu>");
estrhashof<INDTYPE> seqind;
estrarray uarr;
cout << "# loading seqs file: " << argv[1] << endl;
load_seqs_compressed(argv[1],arr,seqind,seqlen);
load_seqs(argv[1],uarr);
earray<ebasicarray<INDTYPE> > otus;
efile f;
estr line;
estrarray parts;
f.open(argv[2],"r");
while (!f.eof()){
f.readln(line);
if (line.len()==0 || line[0]=='#') continue;
if (line[0]=='>'){
otus.add(ebasicarray<INDTYPE>());
continue;
}
ldieif(otus.size()==0,"first entry not start of OTU or missing '>'");
parts=line.explode("\t");
ldieif(parts.size()==0,"array empty: "+line);
ldieif(!seqind.exists(parts[0]),"sequence not found: "+parts[0]);
otus[otus.size()-1].add(seqind[parts[0]]);
}
cerr << endl;
ebasicarray<INDTYPE> uniqind;
earray<ebasicarray<INDTYPE> > dupslist;
finduniq(uniqind,dupslist);
eintarray uniqmask;
uniqmask.init(arr.size(),0);
for (long i=0; i<uniqind.size(); ++i)
uniqmask[uniqind[i]]=1;
// ebasicarray<INDTYPE> uniqind;
taskman.createThread(nthreads);
efloatarray avgdist;
for (long j=0; j<otus.size(); ++j){
// cout << "# computing distances for otu "<< j << " size: " << otus[j].size() << endl;
if (otus[j].size()==1){
cout << ">OTU" << j << " " << arr.keys(otus[j][0]) << " avg_id=1.0 otu_size=1" << endl;
cout << uarr.values(otus[j][0]) << endl;
continue;
}
uniqind.clear();
for (long l=0; l<otus[j].size(); ++l){
if (uniqmask[otus[j][l]]==1)
uniqind.add(otus[j][l]);
}
// uniqind=otus[j];
avgdist.init(arr.size(),0.0);
dists.clear();
partsTotal=10000;
if (partsTotal>(uniqind.size()-1l)*uniqind.size()/20l) partsTotal=(uniqind.size()-1l)*uniqind.size()/20l; // make fewer tasks if to few calculations per task
if (partsTotal<=0) partsTotal=1;
taskman.clear();
for (long i=0; i<partsTotal; ++i)
taskman.addTask(dfunc.value().calcfunc,evararray(mutex,uniqind,arr,dists,(const int&)seqlen,(const long int&)i,(const long int&)partsTotal,(const float&)0.0,(const int&)winlen));
taskman.wait();
for (long i=0; i<dists.size(); ++i){
avgdist[dists[i].x]+=dists[i].dist;
avgdist[dists[i].y]+=dists[i].dist;
}
long k=0;
for (long i=1; i<avgdist.size(); ++i)
if (avgdist[k]<avgdist[i]) k=i;
// cout << "OTU" << j << " " << otus[j].size() << " " << arr.keys(k) << " " << avgdist[k]/(otus[j].size()-1) << " " << dists.size() << endl;
cout << ">OTU" << j << " " << arr.keys(k) << " avg_id=" << avgdist[k]/(otus[j].size()-1) << " otu_size=" << otus[j].size() << endl;
cout << uarr.values(k) << endl;
}
cerr << endl;
exit(0);
}
void actionMakePart()
{
ldieif(argvc<3,"syntax: "+efile(argv[0]).basename()+" -makepart <alignment> <cutoff>");
cout << "# loading seqs file: " << argv[1] << endl;
load_seqs_compressed(argv[1],arr,seqlen);
t=estr(argv[2]).f();
ebasicarray<INDTYPE> uniqind;
earray<ebasicarray<INDTYPE> > dupslist;
finduniq(uniqind,dupslist);
cout << "# unique seqs: " << uniqind.size() << endl;
ebasicarray<INDTYPE> otuid;
otuid.reserve(uniqind.size());
for (long i=0l; i<uniqind.size(); ++i)
otuid.add(i);
cout << "# computing partitions. threshold: " << t << endl;
if (partsTotal>(arr.size()-1l)*arr.size()/20l) partsTotal=(arr.size()-1l)*arr.size()/20l; // make fewer tasks if to few calculations per task
// partsTotal=1;
for (long i=0; i<partsTotal; ++i)
taskman.addTask(dfuncpart.value().calcfunc,evararray(mutex,uniqind,arr,otuid,(const int&)seqlen,(const long int&)i,(const long int&)partsTotal,(const float&)t,(const int&)winlen));
taskman.createThread(nthreads);
taskman.wait();
cout << endl;
ebasicarray<INDTYPE> newotuid;
earray<ebasicarray<INDTYPE> > otus;
newotuid.init(otuid.size(),-1l);
long otucount=0;
for (long i=0; i<otuid.size(); ++i){
if (newotuid[otuid[i]]==-1l){
newotuid[otuid[i]]=otucount;
otus.add(ebasicarray<INDTYPE>());
++otucount;
}
otuid[i]=newotuid[otuid[i]];
otus[otuid[i]].add(i);
}
cout << "# partitions: " << otus.size() << endl;
for (long i=0; i<otus.size(); ++i){
cout << otus[i].size() << ":";
for (long j=0; j<otus[i].size(); ++j){
// cout << " " << uniqind[otus[i][j]];
for (long k=0; k<dupslist[otus[i][j]].size(); ++k)
cout << " " << dupslist[otus[i][j]][k];
}
cout << endl;
}
exit(0);
}
int emain()
{
bool cl=false;
bool sl=false;
bool al=false;
bool cdist=false;
epregister(cl);
epregister(sl);
epregister(al);
epregister(cdist);
epregisterFunc(help);
dfuncpart.choice=0;
dfuncpart.add("gap",edistfunc(part_calc_dists_u<estrarray,eseqdist,dist_compressed2>,dist_compressed2));
dfuncpart.add("nogap",edistfunc(part_calc_dists_u<estrarray,eseqdist,dist_nogap_compressed2>,dist_nogap_compressed2));
dfuncpart.add("gap2",edistfunc(part_calc_dists_u<estrarray,eseqdist,dist_compressed>,dist_compressed));
dfuncpart.add("nogap2",edistfunc(part_calc_dists_u<estrarray,eseqdist,dist_nogap_compressed>,dist_nogap_compressed));
dfuncpart.add("nogapsingle",edistfunc(part_calc_dists_u<estrarray,eseqdist,dist_nogapsingle_compressed>,dist_nogapsingle_compressed));
dfuncpart.add("tamura",edistfunc(part_calc_dists_u<estrarray,eseqdist,dist_tamura_compressed>,dist_tamura_compressed));
epregister(dfuncpart);
dfunc.choice=0;
dfunc.add("gap",edistfunc(t_calc_dists_u<estrarray,eseqdist,eblockarray<eseqdist>,dist_compressed2>,dist_compressed2));
dfunc.add("nogap",edistfunc(t_calc_dists_u<estrarray,eseqdist,eblockarray<eseqdist>,dist_nogap_compressed2>,dist_nogap_compressed2));
dfunc.add("gap2",edistfunc(t_calc_dists_u<estrarray,eseqdist,eblockarray<eseqdist>,dist_compressed>,dist_compressed));
dfunc.add("nogap2",edistfunc(t_calc_dists_u<estrarray,eseqdist,eblockarray<eseqdist>,dist_nogap_compressed>,dist_nogap_compressed));
dfunc.add("nogapsingle",edistfunc(t_calc_dists_u<estrarray,eseqdist,eblockarray<eseqdist>,dist_nogapsingle_compressed>,dist_nogapsingle_compressed));
dfunc.add("tamura",edistfunc(t_calc_dists_u<estrarray,eseqdist,eblockarray<eseqdist>,dist_tamura_compressed>,dist_tamura_compressed));
epregisterClass(eoption<edistfunc>);
epregisterClassMethod4(eoption<edistfunc>,operator=,int,(const estr& val),"=");
epregister(dfunc);
epregister(winlen);
estr ofile;
estr dfile;
estr dupfile;
epregister(dupfile);
epregister(ignoreUnique);
epregister(t);
epregister(nthreads);
epregister(ofile);
epregister(dfile);
epregister(ignoreMemThres);
getParser()->actions.add("makereps",actionMakeReps);
getParser()->actions.add("makeotus",actionMakeOtus);
getParser()->actions.add("makeotus_mothur",actionMakeOtusMothur);
getParser()->actions.add("makepart",actionMakePart);
eparseArgs(argvc,argv);
// cout << "# initializing identity lookup table" << endl;
// initLookupTable();
if(argvc<2) {
cout << "syntax: "+efile(argv[0]).basename()+" <-sl true|-cl true|-al true> <seqali>" << endl;
cout << "\""+efile(argv[0]).basename()+ " --help\" for more help" << endl;
exit(-1);
}
if(!cl && !sl && !al) {
cout << "syntax: "+efile(argv[0]).basename()+" <-sl true|-cl true|-al true> <seqali>" << endl;
cout << "please choose at least one clustering method <-sl true|-cl true|-al true>" << endl;
cout << "\""+efile(argv[0]).basename()+ " --help\" for more help" << endl;
exit(-1);
}
cout << "# " << date() << endl;
cout << "# " << args2str(argvc,argv) << endl;
cout << "# system RAM: " << getSystem()->getTotalRam()/1024 << "Mb" << endl;
cout << "# free system RAM: " << (getSystem()->getFreeRam()+getSystem()->getBufferRam())/1024 << "Mb" << endl;
cout << "# process memory limit: " << ((getSystem()->getMemLimit()&0x3fffffffffffff)==0x3fffffffffffff?estr("unlimited"):estr(getSystem()->getMemLimit()/1024/1024)+"Mb") << endl;
warnMemThres=MIN(MIN(getSystem()->getTotalRam(),getSystem()->getMemLimit()/1024),getSystem()->getFreeRam()+getSystem()->getBufferRam())*0.6/1024;
exitMemThres=MIN(MIN(getSystem()->getTotalRam(),getSystem()->getMemLimit()/1024),getSystem()->getFreeRam()+getSystem()->getBufferRam())*0.65/1024;
cout << "# warning memory threshold: " << warnMemThres << "Mb" << endl;
cout << "# exit memory threshold: " << exitMemThres << "Mb" << endl;
cout << "# distance function: " << dfunc.key() << endl;
if (ofile.len()==0)
ofile=argv[1];
epregisterClass(eseqdist);
epregisterClassSerializeMethod(eseqdist);
epregisterClassProperty(eseqdist,dist);
epregisterClassProperty(eseqdist,x);
epregisterClassProperty(eseqdist,y);
epregisterClass(ebasicarray<eseqdist>);
epregisterClassInheritance(ebasicarray<eseqdist>,ebasearray);
epregisterClassMethod(ebasicarray<eseqdist>,subset);
epregisterClassSerializeMethod(ebasicarray<eseqdist>);
long i,j;
cout << "# loading seqs file: " << argv[1] << endl;
load_seqs_compressed(argv[1],arr,seqlen);
#ifndef HPC_CLUST_USE_LONGIND
ldieif(arr.size() > (2l<<31),"To cluster more than 2 million sequences please recompile hpc-clust with the --enable-longind flag.");
#endif
ebasicarray<INDTYPE> uniqind;
earray<ebasicarray<INDTYPE> > dupslist;
finduniq(uniqind,dupslist);
cout << "# unique seqs: " << uniqind.size() << endl;
if (dupfile.len()){
efile dupf(dupfile,"w");
for (i=0; i<dupslist.size(); ++i){
dupf.write(estr(dupslist[i][0])+" "+estr(dupslist[i].size()));
for (j=1; j<dupslist[i].size(); ++j)
dupf.write(estr(" ")+dupslist[i][j]);
dupf.write("\n");
}
dupf.close();
}
long maxdists=uniqind.size()*(uniqind.size()-1)/2;
long maxmem=maxdists*sizeof(eseqdist)/1024/1024;
cout << "# maximum number of distance pairs: " << maxdists << " (" << maxmem << "Mb)" << endl;
if (maxmem > warnMemThres){
cout << "# WARNING: Number of sequences provided may require more memory than is currently available on this system." << endl;
cout << "# Please monitor the memory usage of this program and check the log at the end. This program will" << endl;
cout << "# automatically exit if it reaches the exitMemThres value shown above. You can force the program" << endl;
cout << "# to ignore this threshold using the argument: -ignoreMemThres true" << endl;
cout << "# Memory requirements can be reduced by increasing the clustering threshold, or reducing the number" << endl;
cout << "# of sequences to be clustered. For more information and tips on optimizing hpc-clust memory" << endl;
cout << "# usage please refer to the documentation." << endl;
}
float dtime,stime;
etimer t1;
t1.reset();
efile df(dfile);
cout << "# computing distances" << endl;
// if ((arr.size()-1l)*arr.size()/2l/partsTotal > 10000l) partsTotal=(arr.size()-1l)*arr.size()/2l/10000l; // make more tasks if too many calculations per task
if (partsTotal>(arr.size()-1l)*arr.size()/20l) partsTotal=(arr.size()-1l)*arr.size()/20l; // make fewer tasks if to few calculations per task
// cout << "partsTotal: " << partsTotal << endl;
cerr << endl; // needed for keeping track of the progress
for (i=0; i<partsTotal; ++i)
taskman.addTask(dfunc.value().calcfunc,evararray(mutex,uniqind,arr,dists,(const int&)seqlen,(const long int&)i,(const long int&)partsTotal,(const float&)t,(const int&)winlen));
taskman.createThread(nthreads);
taskman.wait();
cerr << endl;
dtime=t1.lap()*0.001;
cout << "# time calculating distances: " << dtime << endl;
cout << "# distances within threshold: " << dists.size() << endl;
cout << "# number of tasks: " << taskman.tasks.size() << endl;
fradix256sort<eblockarray<eseqdist>,radixKey>(dists);
cout << "# number of tasks: " << taskman.tasks.size() << endl;
stime=t1.lap()*0.001;
if (dfile.len()){
cout << "# saving distances to file: "<<dfile << endl;
for (i=0; i<dists.size(); ++i)
df.write(estr(arr.keys(dists[i].x))+"\t"+arr.keys(dists[i].y)+"\t"+(1.0-dists[i].dist)+"\n");
/*
for (i=0; i<dupslist.size(); ++i){
for (j=1; j<dupslist[i].size(); ++j)
df.write(estr(dupslist[i][0])+" "+dupslist[i][j]+" 1.0\n");
}
*/
df.close();
}
// }else{
// cout << "# loading distances from file: "<<dfile << endl;
/*
estr str;
df.read(str);
ldieif(mindists.unserial(str,0)==-1,"problem loading distance file: "+dfile);
df.close();
*/
// }
totaldists=dists.size();
cout << "# time sorting distances: " << stime << endl;
cout << "# initializing cluster"<<endl;
if (cl)
clcluster.init(arr.size(),ofile+".cl",argv[1],dupslist);
if (sl)
slcluster.init(arr.size(),ofile+".sl",argv[1],dupslist);
if (al)
alcluster.init(arr.size(),ofile+".al",argv[1],dupslist,t,dfunc.value().calcfunc_single,arr,seqlen);
cout << "# starting clustering"<<endl;
t1.reset();
for (i=dists.size()-1; i>=0; --i){
if (cl)
clcluster.add(dists[i]);
if (al)
alcluster.add(dists[i]);
if (sl)
slcluster.add(dists[i]);
}
if (al)
alcluster.finalize();
float clustime=t1.lap()*0.001;
cout << "# time calculating distances: " << dtime << endl;
cout << "# time sorting distances: " << stime << endl;
cout << "# time clustering: " << clustime << endl;
cout << "# total time: " << dtime+clustime+stime << endl;
cout << "# distances within threshold: " << totaldists << endl;
if (cdist){
efile fsl,fcl,fal;
if (sl) fsl.open(ofile+".sl.dist","w");
if (cl) fcl.open(ofile+".cl.dist","w");
if (al) fal.open(ofile+".cl.dist","w");
for (i=dists.size()-1; i>=0; --i){
if (sl) fsl.write(estr(dists[i].x)+" "+dists[i].y+" "+dists[i].dist+" "+slcluster.clusterData.getMergeDistance(dists[i].x,dists[i].y)+"\n");
if (cl) fcl.write(estr(dists[i].x)+" "+dists[i].y+" "+dists[i].dist+" "+clcluster.clusterData.getMergeDistance(dists[i].x,dists[i].y)+"\n");
if (al) fal.write(estr(dists[i].x)+" "+dists[i].y+" "+dists[i].dist+" "+alcluster.clusterData.getMergeDistance(dists[i].x,dists[i].y)+"\n");
}
}
return(0);
}