File: ClusterTree.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,868 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 15
file content (1018 lines) | stat: -rw-r--r-- 33,423 bytes parent folder | download | duplicates (6)
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
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include "ClusterTree.h"
#include "../general/utils.h"
#include "dayhoff.h"
#include "RandomGenerator.h"
#include <math.h>
#include <sstream>
#include "../general/OutputFile.h"

namespace clustalw
{

ClusterTree::ClusterTree()
 : numSeqs(0),
   firstSeq(0),
   lastSeq(0)
{
    bootstrapPrompt = "\nEnter name for bootstrap output file  ";
    bootstrapFileTypeMsg = "Bootstrap output";
}



/** ****************************************************************************************
 *                          The rest are private functions.                                *
 *                                                                                         *
 *                                                                                         *
 *                                                                                         *
 *******************************************************************************************/
 
 
 
void ClusterTree::distanceMatrixOutput(ofstream* outFile, clustalw::DistMatrix* matToPrint,
                                        clustalw::Alignment *alignPtr)
{
    if(outFile == NULL || !outFile->is_open())
    {
        clustalw::utilityObject->error("Cannot output the distance matrix, file is not open\n");        
        return;
    }
     
    int i, j;
    int _maxNames = alignPtr->getMaxNames();
    (*outFile) << setw(6) << lastSeq - firstSeq + 1;
    
    for (i = 1; i <= lastSeq - firstSeq + 1; i++)
    {
        (*outFile) << "\n" << left << setw(_maxNames) << alignPtr->getName(i) << " ";
        for (j = 1; j <= lastSeq - firstSeq + 1; j++)
        {
            (*outFile) << " " << setw(6) << setprecision(3) << fixed << (*matToPrint)(i, j);
            if (j % 8 == 0)
            {
                if (j != lastSeq - firstSeq + 1)
                {
                    (*outFile) << "\n";
                }
                if (j != lastSeq - firstSeq + 1)
                {
                    (*outFile) << "          ";
                }
            }
        }
    }
}

void ClusterTree::overspillMessage(int overspill,int totalDists)
{
    std::ostringstream ssOverSpill;
    std::ostringstream ssTotalDists;
    string message;

    ssOverSpill << overspill;
    message += ssOverSpill.str();
    message += " of the distances out of a total of ";
    ssTotalDists << totalDists;
    message += ssTotalDists.str();
    message += "\n were out of range for the distance correction.\n"
      "\n SUGGESTIONS: 1) remove the most distant sequences"
      "\n           or 2) use the PHYLIP package"
      "\n           or 3) turn off the correction."
      "\n Note: Use option 3 with caution! With this degree"
      "\n of divergence you will have great difficulty"
      "\n getting robust and reliable trees.\n\n";

    clustalw::utilityObject->warning(message.c_str()); 
}

/*
 * The function treeGapDelete flags all the positions that have a gap in any sequence.
 *
 */
// NOTE there is something wrong with using _lenFirstSeq. But this is what old clustal does.
void ClusterTree::treeGapDelete(clustalw::Alignment *alignPtr)
{
    int seqn;
    int posn;
    int _maxAlnLength = alignPtr->getMaxAlnLength();
    int _lenFirstSeq = alignPtr->getSeqLength(firstSeq);
    int _gapPos1 = clustalw::userParameters->getGapPos1();
    int _gapPos2 = clustalw::userParameters->getGapPos2();
    
    treeGaps.resize(_maxAlnLength + 1);
    
    for (posn = 1; posn <= _lenFirstSeq; ++posn)
    {
        treeGaps[posn] = 0;
        for (seqn = 1; seqn <= lastSeq - firstSeq + 1; ++seqn)
        {
            const vector<int>* _seqM = alignPtr->getSequence(seqn + firstSeq - 1);
            
            if(posn > alignPtr->getSeqLength(seqn + firstSeq - 1))
            {
                break; // Dont read locations that cannot be read!
            }
            if (((*_seqM)[posn] == _gapPos1) || ((*_seqM)[posn] == _gapPos2))
            {
                treeGaps[posn] = 1;
                break;
            }
        }
    }
}

int ClusterTree::dnaDistanceMatrix(ofstream* treeFile, clustalw::Alignment *alignPtr)
{
    int m, n;
    int j, i;
    int res1, res2;
    int overspill = 0;
    double p, q, e, a, b, k;
    
    treeGapDelete(alignPtr); // flag positions with gaps (tree_gaps[i] = 1 ) 

    if (verbose)
    {
        (*treeFile) << "\n";
        (*treeFile) <<  "\n DIST   = percentage divergence (/100)";
        (*treeFile) << "\n p      = rate of transition (A <-> G; C <-> T)";
        (*treeFile) << "\n q      = rate of transversion";
        (*treeFile) << "\n Length = number of sites used in comparison";
        (*treeFile) << "\n";
        if (clustalw::userParameters->getTossGaps())
        {
            (*treeFile) << "\n All sites with gaps (in any sequence) deleted!";
            (*treeFile) << "\n";
        }
        if (clustalw::userParameters->getKimura())
        {
            (*treeFile) << "\n Distances corrected by Kimura's 2 parameter model:";
            (*treeFile) << "\n\n Kimura, M. (1980)";
            (*treeFile) << " A simple method for estimating evolutionary ";
            (*treeFile) << "rates of base";
            (*treeFile) << "\n substitutions through comparative studies of ";
            (*treeFile) << "nucleotide sequences.";
            (*treeFile) << "\n J. Mol. Evol., 16, 111-120.";
            (*treeFile) << "\n\n";
        }
    }
    
    int _numSeqs = alignPtr->getNumSeqs();
    quickDistMat.reset(new clustalw::DistMatrix(_numSeqs + 1));
    int _lenFirstSeq = alignPtr->getSeqLength(firstSeq);
    int _gapPos1 = clustalw::userParameters->getGapPos1();
    int _gapPos2 = clustalw::userParameters->getGapPos2();
    int lenSeqM, lenSeqN;
     // for every pair of sequence 
    for (m = 1; m < lastSeq - firstSeq + 1; ++m)
    {
        const vector<int>* _seqM = alignPtr->getSequence(m + firstSeq - 1);
        lenSeqM = alignPtr->getSeqLength(m + firstSeq - 1);
        
        for (n = m + 1; n <= lastSeq - firstSeq + 1; ++n)
        {
            const vector<int>* _seqN = alignPtr->getSequence(n + firstSeq - 1);
            lenSeqN = alignPtr->getSeqLength(n + firstSeq - 1);
            
            p = q = e = 0.0;
            quickDistMat->SetAt(m, n, 0.0);
            quickDistMat->SetAt(n ,m, 0.0);
            
            for (i = 1; i <= _lenFirstSeq; ++i)
            {
                j = bootPositions[i];
                if (clustalw::userParameters->getTossGaps() && (treeGaps[j] > 0))
                {
                    goto skip;
                }
                // gap position 
/** *******************************************************************************
 * BUG!!!!!!!      NOTE this was found for protein. Presuming the same here       *
 * NOTE: the following if statements were coded in so as to produce               *
 * the same distance results as the old clustal. Old clustal compares             *
 * up to the length of the first sequence. If this is longer than the             *
 * other sequences, then the -3 and 0's are compared at the end of the            *
 * array. These should not be compared, but I need to stick to this to            *
 * produce the same results as the old version for testing!                       *
 **********************************************************************************/
                if(j > lenSeqM)
                {
                    if(j == lenSeqM + 1)
                    {
                        res1 = -3;
                    }
                    else
                    {    
                        res1 = 0;
                    }
                }
                else
                { 
                    res1 = (*_seqM)[j];
                }
                if(j > lenSeqN)
                {
                    if(j == lenSeqN + 1)
                    {
                        res2 = -3;
                    }
                    else
                    {    
                        res2 = 0;
                    }
                }
                else
                {
                    res2 = (*_seqN)[j];
                }
                if ((res1 == _gapPos1) || (res1 == _gapPos2) || (res2 == _gapPos1)
                    || (res2 == _gapPos2))
                {
                    goto skip;
                }
                // gap in a seq
                if (!clustalw::userParameters->getUseAmbiguities())
                {
                    if (isAmbiguity(res1) || isAmbiguity(res2))
                    {
                        goto skip;
                    }
                }
                // ambiguity code in a seq
                e = e+1.0;
                if (res1 != res2)
                {
                    if (transition(res1, res2))
                    {
                        p = p + 1.0;
                    }
                    else
                    {
                        q = q + 1.0;
                    }
                }
                skip: ;
            }


            // Kimura's 2 parameter correction for multiple substitutions

            if (!clustalw::userParameters->getKimura())
            {
                if (e == 0)
                {
                    cerr << "\n WARNING: sequences " << m << " and " << n 
                         << " are non-overlapping\n";
                    k = 0.0;
                    p = 0.0;
                    q = 0.0;
                }
                else
                {
                    k = (p + q) / e;
                    if (p > 0.0)
                    {
                        p = p / e;
                    }
                    else
                    {
                        p = 0.0;
                    }
                    if (q > 0.0)
                    {
                        q = q / e;
                    }
                    else
                    {
                        q = 0.0;
                    }
                }
                quickDistMat->SetAt(m, n, k);
                quickDistMat->SetAt(n ,m, k);
                if (verbose) 
                {
                    (*treeFile) << setw(4) << m << " vs." << setw(4) << n << ":  DIST =  " 
                                << setw(4) << fixed << setprecision(4)
                                << k << "; p = " << fixed << setprecision(4) << p << "; q = "
                                << fixed << setprecision(4) << q << "; length = " << setw(6)
                                << fixed << setprecision(0) << e << "\n"; 
                }
            }
            else
            {
                if (e == 0)
                {
                    cerr << "\n WARNING: sequences " << m << " and " << n 
                         << " are non-overlapping\n";;
                    p = 0.0;
                    q = 0.0;
                }
                else
                {
                    if (p > 0.0)
                    {
                        p = p / e;
                    }
                    else
                    {
                        p = 0.0;
                    }
                    if (q > 0.0)
                    {
                        q = q / e;
                    }
                    else
                    {
                        q = 0.0;
                    }
                }

                if (((2.0 *p) + q) == 1.0)
                {
                    a = 0.0;
                }
                else
                {
                    a = 1.0 / (1.0 - (2.0 *p) - q);
                }

                if (q == 0.5)
                {
                    b = 0.0;
                }
                else
                {
                    b = 1.0 / (1.0 - (2.0 *q));
                }

                // watch for values going off the scale for the correction.
                if ((a <= 0.0) || (b <= 0.0))
                {
                    overspill++;
                    k = 3.5; // arbitrary high score
                }
                else
                {
                    k = 0.5 * log(a) + 0.25 * log(b);
                }
                quickDistMat->SetAt(m, n, k);
                quickDistMat->SetAt(n ,m, k);
                if (verbose)
                // if screen output 
                {
                    (*treeFile) << setw(4) << m << " vs." << setw(4) << n 
                                << ":  DIST =  " << fixed << setprecision(4)
                                << k << "; p = " << fixed <<  setprecision(4) << p << "; q = "
                                << fixed << setprecision(4) << q << "; length = " << setw(6)
                                << fixed << setprecision(0) << e << "\n"; 
                }

            }
        }
    }
    return overspill; // return the number of off-scale values
}

int ClusterTree::protDistanceMatrix(ofstream* treeFile, clustalw::Alignment *alignPtr)
{
    int m, n;
    int j, i;
    int res1, res2;
    int overspill = 0;
    double p, e, k, tableEntry;

    treeGapDelete(alignPtr); // flag positions with gaps (tree_gaps[i] = 1 ) 

    if (verbose)
    {
        (*treeFile) <<  "\n";
        (*treeFile) << "\n DIST   = percentage divergence (/100)";
        (*treeFile) << "\n Length = number of sites used in comparison";
        (*treeFile) << "\n\n";
        if (clustalw::userParameters->getTossGaps())
        {
            (*treeFile) << "\n All sites with gaps (in any sequence) deleted";
            (*treeFile) << "\n";
        }
        if (clustalw::userParameters->getKimura())
        {
            (*treeFile) <<
                "\n Distances up to 0.75 corrected by Kimura's empirical method:";
            (*treeFile) << "\n\n Kimura, M. (1983)";
            (*treeFile) << " The Neutral Theory of Molecular Evolution.";
            (*treeFile) <<
                "\n Page 75. Cambridge University Press, Cambridge, England.";
            (*treeFile) << "\n\n";
        }
    }
    int _numSeqs = alignPtr->getNumSeqs();
    int _lenSeq1 = alignPtr->getSeqLength(1);
    quickDistMat.reset(new clustalw::DistMatrix(_numSeqs + 1));
    int _gapPos1 = clustalw::userParameters->getGapPos1();
    int _gapPos2 = clustalw::userParameters->getGapPos2();
    int lenSeqM, lenSeqN;
    // for every pair of sequence 
    for (m = 1; m < _numSeqs; ++m)
    {
        const vector<int>* _seqM = alignPtr->getSequence(m);
        lenSeqM = alignPtr->getSeqLength(m);
        for (n = m + 1; n <= _numSeqs; ++n)
        {
            const vector<int>* _seqN = alignPtr->getSequence(n);
            lenSeqN = alignPtr->getSeqLength(n);
            p = e = 0.0;
            quickDistMat->SetAt(m, n, 0.0);
            quickDistMat->SetAt(n ,m, 0.0);
            for (i = 1; i <= _lenSeq1; ++i) // It may be this here!
            {
                j = bootPositions[i];
                if (clustalw::userParameters->getTossGaps() && (treeGaps[j] > 0))
                {
                    goto skip;
                }
                // gap position
/** *******************************************************************************
 * BUG!!!!!!!                                                                     *
 * NOTE: the following if statements were coded in so as to produce               *
 * the same distance results as the old clustal. Old clustal compares             *
 * up to the length of the first sequence. If this is longer than the             *
 * other sequences, then the -3 and 0's are compared at the end of the            *
 * array. These should not be compared, but I need to stick to this to            *
 * produce the same results as the old version for testing!                       *
 **********************************************************************************/
                if(j > lenSeqM)
                {
                    if(j == lenSeqM + 1)
                    {
                        res1 = -3;
                    }
                    else
                    {    
                        res1 = 0;
                    }
                }
                else
                { 
                    res1 = (*_seqM)[j];
                }
                if(j > lenSeqN)
                {
                    if(j == lenSeqN + 1)
                    {
                        res2 = -3;
                    }
                    else
                    {    
                        res2 = 0;
                    }
                }
                else
                {
                    res2 = (*_seqN)[j];
                }
                if ((res1 == _gapPos1) || (res1 == _gapPos2) || (res2 == _gapPos1)
                    || (res2 == _gapPos2))
                {
                    goto skip;
                }
                // gap in a seq
                e = e + 1.0;
                if (res1 != res2)
                {
                    p = p + 1.0;
                }
                skip: ;
            }

            if (p <= 0.0)
            {
                k = 0.0;
            }
            else
            {
                k = p / e;
            }

            if (clustalw::userParameters->getKimura())
            {
                if (k < 0.75)
                {
                    // use Kimura's formula
                    if (k > 0.0)
                    {
                        k =  - log(1.0 - k - (k * k / 5.0));
                    }
                }
                else
                {
                    if (k > 0.930)
                    {
                        overspill++;
                        k = 10.0; // arbitrarily set to 1000%
                    }
                    else // dayhoff_pams is from dayhoff.h file
                    {
                        tableEntry = (k * 1000.0) - 750.0;
                        k = (double)dayhoff_pams[(int)tableEntry];
                        k = k / 100.0;
                    }
                }
            }

            quickDistMat->SetAt(m, n, k);
            quickDistMat->SetAt(n ,m, k);
            if (verbose)
            {
                (*treeFile) << setw(4) << m << " vs." << setw(4) << n 
                            << "  DIST = " << fixed << setprecision(4)
                            << k << ";  length = " << setw(6)
                            << setprecision(0) << e << "\n";
            }
        }
    }
    return overspill;
}

bool ClusterTree::isAmbiguity(int c)
{
    int i;
    char codes[] = "ACGTU";

    if (clustalw::userParameters->getUseAmbiguities() == true)
    {
        return false;
    }

    for (i = 0; i < 5; i++)
        if (clustalw::userParameters->getAminoAcidCode(c) == codes[i])
        {
            return false;
        }

    return true;
}

/*
 * The function calcPercIdentity calculates the percent identity of the sequences
 * and outputs it to a the file pfile. NOTE this is not used at the moment. It was in
 * the old code, but there was no way to access it from the menu. This may change. 
 */
void ClusterTree::calcPercIdentity(ofstream* pfile, clustalw::Alignment *alignPtr)
{
    clustalw::DistMatrix percentMat;
  
    float ident;
    int nmatch;
  
    int val1, val2;
  
    int i,j,k, length_longest;
    int length_shortest;
    
    int rs = 0, rl = 0;
    // findout sequence length, longest and shortest;
    length_longest = 0;
    length_shortest = 0;

    int _numSeqs = alignPtr->getNumSeqs();
    int _seqLength;
    for (i = 1; i <= _numSeqs; i++) 
    {
        _seqLength = alignPtr->getSeqLength(i);
        if (length_longest < _seqLength)
        {
            length_longest = _seqLength;
            rs = i;
        }
        if (length_shortest > _seqLength) 
        {
            length_shortest = _seqLength;
            rl = i;
        }
    } 

    percentMat.ResizeRect(_numSeqs + 1);
    nmatch = 0;
    int _lenSeqI, _lenSeqJ;
    int _maxAA = clustalw::userParameters->getMaxAA();
    
    for (i = 1; i <= _numSeqs; i++) 
    {
        const vector<int>* _seqI = alignPtr->getSequence(i);
        _lenSeqI = alignPtr->getSeqLength(i);
        
        for (j = i; j <= _numSeqs; j++) 
        {
            const vector<int>* _seqJ = alignPtr->getSequence(j);
            _lenSeqJ = alignPtr->getSeqLength(j);
            
            cout << "\n           " << alignPtr->getName(j) << " ";
            ident = 0;
            nmatch = 0;
            for(k = 1; k <= length_longest; k++) 
            {
                if((k > _lenSeqI) || (k > _lenSeqJ))
                {
                    break;
                }
                val1 = (*_seqI)[k];
                val2 = (*_seqJ)[k];

                if (((val1 < 0) || (val1 > _maxAA)) || ((val2 < 0) || (val2 > _maxAA)))
                { 
                    continue; // residue = '-';
                }
                if (val1 == val2) 
                {
                    ident++ ;
                    nmatch++;
                }
                else 
                {
                    nmatch++ ;
                }
            }
            ident = ident/nmatch * 100.0 ;
            percentMat.SetAt(i, j, ident);
            percentMat.SetAt(j, i, ident);
        }
    }

    int _maxNameSize = alignPtr->getMaxNames();
    
    (*pfile) << "#\n#\n#  Percent Identity  Matrix - created by Clustal"
             << clustalw::userParameters->getRevisionLevel() << " \n#\n#\n";
    for(i = 1; i <= _numSeqs; i++) 
    {
        (*pfile) << "\n " << right << setw(5) << i << ": "; 
        (*pfile) << left << setw(_maxNameSize) << alignPtr->getName(i);
        
        for(j = 1; j <= _numSeqs; j++) 
        {
            (*pfile) << setw(8) << right << fixed << setprecision(0) << percentMat(i, j);
        }
    }
    (*pfile) << "\n";

}

void ClusterTree::compareTree(PhyloTree* tree1, PhyloTree* tree2, vector<int>* hits, int n)
{
    int i, j, k;
    int nhits1, nhits2;

    for (i = 1; i <= n - 3; i++)
    {
        for (j = 1; j <= n - 3; j++)
        {
            nhits1 = 0;
            nhits2 = 0;
            for (k = 1; k <= n; k++)
            {
                if (tree1->treeDesc[i][k] == tree2->treeDesc[j][k])
                {
                    nhits1++;
                }
                if (tree1->treeDesc[i][k] != tree2->treeDesc[j][k])
                {
                    nhits2++;
                }
            }
            if ((nhits1 == lastSeq - firstSeq + 1) || (nhits2 == lastSeq -
                firstSeq + 1))
            {
                (*hits)[i]++;
            }
        }
    }
}

/**
 * NOTE this will go into the OutputFile class and will not be needed here anymore.
 *
 */
/*string ClusterTree::getOutputFileName(const string prompt, string path, 
                                      const string fileExtension)
{
    string temp;
    string _fileName; // Will return this name.
    string message;
    _fileName = path + fileExtension;

    if(_fileName.compare(clustalw::userParameters->getSeqName()) == 0) 
    {
        cout << "Output file name is the same as input file.\n";
        if (clustalw::userParameters->getMenuFlag()) 
        {
            message = "\n\nEnter new name to avoid overwriting  [" + _fileName + "]: ";
            clustalw::utilityObject->getStr(message, temp);
            if(temp != "")
            {
                _fileName = temp;
            }
        }
    }
    else if (clustalw::userParameters->getMenuFlag()) 
    {

        message = prompt + " [" + _fileName + "]";
        clustalw::utilityObject->getStr(message, temp);
        if(temp != "")
        {
            _fileName = temp;
        }
    }   
    return _fileName;

}*/

bool ClusterTree::transition(int base1, int base2)
{
// assumes that the bases of DNA sequences have been translated as
// a,A = 0;   c,C = 1;   g,G = 2;   t,T,u,U = 3;  N = 4;
// a,A = 0;   c,C = 2;   g,G = 6;   t,T,u,U =17;
// A <--> G  and  T <--> C  are transitions;  all others are transversions.
    if (((base1 == 0) && (base2 == 6)) || ((base1 == 6) && (base2 == 0)))
    {
        return true;
    }
    // A <--> G 
    if (((base1 == 17) && (base2 == 2)) || ((base1 == 2) && (base2 == 17)))
    {
        return true;
    }
    // T <--> C 
    return false;
}

/**
 * This function is used to open all the bootstrap tree files. It opens them with the 
 * correct message prompt.
 */
bool ClusterTree::openFilesForBootstrap(clustalw::OutputFile* clustalFile, clustalw::OutputFile* phylipFile,
                         clustalw::OutputFile* nexusFile, clustalw::TreeNames* treeNames, string* path)
{
    if (clustalw::userParameters->getOutputTreeClustal())
    {
        if(!clustalFile || !clustalFile->openFile(&(treeNames->clustalName), 
                                  bootstrapPrompt, path, "njb", bootstrapFileTypeMsg))
        {
            return false;
        } 
    }   
        
    if (clustalw::userParameters->getOutputTreePhylip())
    {     
        if(!phylipFile || !phylipFile->openFile(&(treeNames->phylipName), 
                                bootstrapPrompt, path, "phb", bootstrapFileTypeMsg))
        {
            return false;
        }                     
    }    

    if (clustalw::userParameters->getOutputTreeNexus())
    {
        if(!nexusFile || !nexusFile->openFile(&(treeNames->nexusName), 
                                bootstrapPrompt, path, "treb", bootstrapFileTypeMsg))
        {
            return false;
        }                   
    }        
    return true;
}                         

bool ClusterTree::openFilesForTreeFromAlignment(clustalw::OutputFile* clustalFile, 
    clustalw::OutputFile* phylipFile, clustalw::OutputFile* distFile, clustalw::OutputFile* nexusFile, clustalw::OutputFile* pimFile, 
                            clustalw::TreeNames* treeNames, string* path)
{
    if (clustalw::userParameters->getOutputTreeClustal())
    {
        if(!clustalFile || !clustalFile->openFile(&(treeNames->clustalName), 
                                  "\nEnter name for CLUSTAL    tree output file  ",
                                  path, "nj", "Phylogenetic tree"))
        {
            return false;
        } 
    }
        
    if (clustalw::userParameters->getOutputTreePhylip())
    {     
        if(!phylipFile || !phylipFile->openFile(&(treeNames->phylipName), 
                             "\nEnter name for PHYLIP     tree output file  ", path, "ph",
                             "Phylogenetic tree"))
        {
            return false;
        }
    }

    if (clustalw::userParameters->getOutputTreeDistances())
    {   
        if(!distFile || !distFile->openFile(&(treeNames->distName), 
                               "\nEnter name for distance matrix output file  ",
                               path, "dst", "Distance matrix"))
        {
            return false;
        }         
    }

    if (clustalw::userParameters->getOutputTreeNexus())
    {
        if(!nexusFile || !nexusFile->openFile(&(treeNames->nexusName), 
                                "\nEnter name for NEXUS tree output file  ", path,
                                     "tre", "Nexus tree"))
        {
            return false;
        }
    }

    if (clustalw::userParameters->getOutputPim())
    {       
        if(!pimFile || !pimFile->openFile(&(treeNames->pimName), 
                            "\nEnter name for % Identity matrix output file  ", path, "pim", 
                            "perc identity"))
        {
            return false;
        }                       
    }
    return true;
}

int ClusterTree::calcQuickDistMatForAll(ofstream* clustalFile, ofstream* phylipFile, 
                                  ofstream* nexusFile, ofstream* pimFile, ofstream* distFile,
                                  clustalw::Alignment* alignPtr)
{
    int overspill = 0;
    bool _DNAFlag = clustalw::userParameters->getDNAFlag();
    
    overspill = calcQuickDistMatForSubSet(clustalFile, phylipFile, nexusFile, alignPtr);    

    if (pimFile && clustalw::userParameters->getOutputPim())
    {
        verbose = false; // Turn off file output
        if (_DNAFlag)
        {
            calcPercIdentity(pimFile, alignPtr);
        }
        else
        {
            calcPercIdentity(pimFile, alignPtr);
        }
    }


    if (distFile && clustalw::userParameters->getOutputTreeDistances())
    {
        verbose = false; // Turn off file output
        if (_DNAFlag)
        {
            overspill = dnaDistanceMatrix(distFile, alignPtr);
        }
        else
        {
            overspill = protDistanceMatrix(distFile, alignPtr);
        }
        distanceMatrixOutput(distFile, quickDistMat.get(),
                             alignPtr);
    }
    return overspill;
}                    

int ClusterTree::calcQuickDistMatForSubSet(ofstream* clustalFile, ofstream* phylipFile, 
                                          ofstream* nexusFile, clustalw::Alignment* alignPtr, 
                                          bool inBootLoop)
{
    int overspill = 0;
    bool _DNAFlag = clustalw::userParameters->getDNAFlag();
    
    if (clustalFile && clustalw::userParameters->getOutputTreeClustal())
    {
        if(!inBootLoop)
        {
            verbose = true; // Turn on file output
        }
        else
        {
            verbose = false; // Turn off when we are in the loop in bootstrap!
        }   
        if (_DNAFlag)
        {
            overspill = dnaDistanceMatrix(clustalFile, alignPtr);
        }
        else
        {
            overspill = protDistanceMatrix(clustalFile, alignPtr);
        }
    }

    if (phylipFile && clustalw::userParameters->getOutputTreePhylip())
    {
        verbose = false; // Turn off file output 
        if (_DNAFlag)
        {
            overspill = dnaDistanceMatrix(phylipFile, alignPtr);
        }
        else
        {
            overspill = protDistanceMatrix(phylipFile, alignPtr);
        }
    }

    if (nexusFile && clustalw::userParameters->getOutputTreeNexus())
    {
        verbose = false; // Turn off file output 
        if (_DNAFlag)
        {
            overspill = dnaDistanceMatrix(nexusFile, alignPtr);
        }
        else
        {
            overspill = protDistanceMatrix(nexusFile, alignPtr);
        }
    }
    return overspill;    
}

void ClusterTree::printBootstrapHeaderToClustalFile(ofstream* clustalFile)
{
    if(clustalFile)
    {
        (*clustalFile) << "\n\n\t\t\tBootstrap Confidence Limits\n\n";
        (*clustalFile) << "\n Random number generator seed = " 
                       << setw(7)
                       << clustalw::userParameters->getBootRanSeed() << "\n";
        (*clustalFile) << "\n Number of bootstrap trials   = " << setw(7)
                       << clustalw::userParameters->getBootNumTrials() << "\n";
        (*clustalFile) << "\n\n Diagrammatic representation of the above tree: \n";
        (*clustalFile) << "\n Each row represents 1 tree cycle;";
        (*clustalFile) << " defining 2 groups.\n";
        (*clustalFile) << "\n Each column is 1 sequence; ";
        (*clustalFile) << "the stars in each line show 1 group; ";
        (*clustalFile) << "\n the dots show the other\n";
        (*clustalFile) << "\n Numbers show occurrences in bootstrap samples.";
    }
}

void ClusterTree::promptForBoolSeedAndNumTrials()
{
    if (clustalw::userParameters->getMenuFlag())
    {
        unsigned int tempSeed;
        tempSeed = clustalw::utilityObject->getInt(
                "\n\nEnter seed no. for random number generator ", 1, 1000,
        clustalw::userParameters->getBootRanSeed());
        clustalw::userParameters->setBootRanSeed(tempSeed);

        clustalw::userParameters->setBootNumTrials(
                     clustalw::utilityObject->getInt("\n\nEnter number of bootstrap trials ", 
                        1, 10000, clustalw::userParameters->getBootNumTrials()));    
    }
}

void ClusterTree::printErrorMessageForBootstrap(int totalOverspill, int totalDists, 
                                                int nfails)
{
    cerr << "\n";
    cerr << "\n WARNING: " << totalOverspill 
         << " of the distances out of a total of " 
         << totalDists << " times" << clustalw::userParameters->getBootNumTrials();
    cerr << "\n were out of range for the distance correction.";
    cerr << "\n This affected " << nfails << " out of " 
         << clustalw::userParameters->getBootNumTrials() << " bootstrap trials.";
    cerr << "\n This may not be fatal but you have been warned!" << "\n";
    cerr << "\n SUGGESTIONS: 1) turn off the correction";
    cerr << "\n           or 2) remove the most distant sequences";
    cerr << "\n           or 3) use the PHYLIP package.\n\n";
            
    if (clustalw::userParameters->getMenuFlag())
    {
        string dummy;
        clustalw::utilityObject->getStr(string("Press [RETURN] to continue"), dummy);
    }
}

bool ClusterTree::checkIfConditionsMet(int numSeqs, int min)
{
    if (clustalw::userParameters->getEmpty())
    {
        clustalw::utilityObject->error("You must load an alignment first");
        return false;
    }

    if (numSeqs < min)
    {
        clustalw::utilityObject->error("Alignment has only %d sequences", numSeqs);
        return false;
    }
    
    return true;
}
                                 
}