File: nxsdistancesblock.cpp

package info (click to toggle)
iqtree 1.5.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,780 kB
  • ctags: 11,529
  • sloc: cpp: 96,162; ansic: 59,874; python: 242; sh: 189; makefile: 45
file content (897 lines) | stat: -rw-r--r-- 24,662 bytes parent folder | download | duplicates (5)
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
//	Copyright (C) 1999-2003 Paul O. Lewis
//
//	This file is part of NCL (Nexus Class Library) version 2.0.
//
//	NCL is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	NCL is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with NCL; if not, write to the Free Software Foundation, Inc., 
//	59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

#include "ncl.h"

/*----------------------------------------------------------------------------------------------------------------------
|	Sets `id' to "DISTANCES", `taxa' to `t', `triangle' to `NxsDistancesBlockEnum::lower', `missing' to '?', `matrix' 
|	and `taxonPos' to NULL, `labels' and `diagonal' to true, `newtaxa' and `interleave' to false, and `ntax' and `nchar'
|	to 0. Assumes `t' is non-NULL.
*/
NxsDistancesBlock::NxsDistancesBlock(
  NxsTaxaBlock *t)	/* the NxsTaxaBlock that will keep track of taxon labels */
  : NxsBlock()
	{
	assert(t != NULL);
	taxa		= t;
	id = "DISTANCES";
	ntax		= 0;
	nchar		= 0;
	diagonal	= true;
	labels		= true;
	newtaxa		= false;
	interleave	= false;
	triangle	= NxsDistancesBlockEnum(lower);
	missing		= '?';
	matrix		= NULL;
	taxonPos	= NULL;
}

/*----------------------------------------------------------------------------------------------------------------------
|	Deletes `matrix' and `taxonPos' arrays.
*/
NxsDistancesBlock::~NxsDistancesBlock()
	{
	if (matrix != NULL)
		delete matrix;
	if (taxonPos != NULL)
		delete [] taxonPos;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Called when DIMENSIONS command needs to be parsed from within the DISTANCES block. Deals with everything after the 
|	token DIMENSIONS up to and including the semicolon that terminates the DIMENSIONS command.
*/
void NxsDistancesBlock::HandleDimensionsCommand(
  NxsToken &token)	/* the token used to read from `in' */
	{
	for (;;)
		{
		token.GetNextToken();

		// Token should either be ';' or the name of a subcommand
		//
		if (token.Equals(";"))
			break;

		else if (token.Equals("NEWTAXA"))
			{
			ntax = 0;
			newtaxa = 1;
			}

		else if (token.Equals("NTAX"))
			{
			if (!newtaxa)
				{
				errormsg = "Must specify NEWTAXA before NTAX if new taxa are being defined";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			// This should be the equals sign
			//
			token.GetNextToken();

			if (!token.Equals("="))
				{
				errormsg = "Expecting '=' but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			// This should be the number of taxa
			//
			token.GetNextToken();
			ntax = atoi(token.GetToken().c_str());
			}

		else if (token.Equals("NCHAR"))
			{
			// This should be the equals sign
			//
			token.GetNextToken();

			if (!token.Equals("="))
				{
				errormsg = "Expecting '=' but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			// This should be the number of characters
			//
			token.GetNextToken();
			nchar = atoi(token.GetToken().c_str());
			}
		}

	if (ntax == 0)
		ntax = taxa->GetNumTaxonLabels();
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Called when FORMAT command needs to be parsed from within the DISTANCES block. Deals with everything after the 
|	token FORMAT up to and including the semicolon that terminates the FORMAT command.
*/
void NxsDistancesBlock::HandleFormatCommand(
  NxsToken &token)	/* the token used to read from `in' */
	{
	for (;;)
		{
		// This should either be ';' or the name of a subcommand
		//
		token.GetNextToken();

		if (token.Equals(";"))
			break;

		else if (token.Equals("TRIANGLE"))
			{
			// This should be the equals sign
			//
			token.GetNextToken();

			if (!token.Equals("="))
				{
				errormsg = "Expecting '=' but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			// This should be LOWER, UPPER, or BOTH
			//
			token.GetNextToken();

			if (token.Equals("LOWER"))
				triangle = NxsDistancesBlockEnum(lower);
			else if (token.Equals("UPPER"))
				triangle = NxsDistancesBlockEnum(upper);
			else if (token.Equals("BOTH"))
				triangle = NxsDistancesBlockEnum(both);
			else
				{
				errormsg = "Expecting UPPER, LOWER, or BOTH but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}
			}

		else if (token.Equals("DIAGONAL"))
			{
			diagonal = 1;
			}

		else if (token.Equals("NODIAGONAL"))
			{
			diagonal = 0;
			}

		else if (token.Equals("LABELS"))
			{
			labels = 1;
			}

		else if (token.Equals("NOLABELS"))
			{
			labels = 0;
			}

		else if (token.Equals("INTERLEAVE"))
			{
			interleave = 1;
			}

		else if (token.Equals("NOINTERLEAVE"))
			{
			interleave = 0;
			}

		else if (token.Equals("MISSING"))
			{
			// This should be the equals sign
			//
			token.GetNextToken();

			if (!token.Equals("="))
				{
				errormsg = "Expecting '=' but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			// This should be the missing data symbol
			//
			token.GetNextToken();

			if (token.GetTokenLength() != 1)
				{
				errormsg = "Missing data symbol specified (";
				errormsg += token.GetToken();
				errormsg += ") is invalid (must be a single character)";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			missing = token.GetToken()[0];
			}

		else
			{
			errormsg = "Token specified (";
			errormsg += token.GetToken();
			errormsg += ") is an invalid subcommand for the FORMAT command";
			throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
			}
		}
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Called from within HandleMatrix, this function is used to deal with interleaved matrices. It is called once for 
|	each pass through the taxa. The local variable `jmax' records the number of columns read in the current interleaved 
|	page and is used to determine the offset used for j in subsequent pages.
*/
bool NxsDistancesBlock::HandleNextPass(
  NxsToken &token,	/* the token we are using for reading the data file */
  unsigned &offset)	/* the offset */
	{
	unsigned i, j, k, jmax = 0; 
	bool done = false;

	unsigned i_first = 0;
	if (triangle == NxsDistancesBlockEnum(lower))
		i_first = offset;

	unsigned i_last = ntax;

	for (i = i_first; i < i_last; i++)
		{
		// Deal with taxon label if provided. Here are the four situations we need to deal with:
		//   newtaxa  (offset > 0)  handled by
		//      0           0         case 1
		//      0           1         case 1
		//      1           0         case 2
		//      1           1         case 1
		//
		if (labels && (!newtaxa || offset > 0))
			{
			// Case 1: Expecting taxon labels, and also expecting them to already be in taxa
			//
			do
				{
				token.SetLabileFlagBit(NxsToken::newlineIsToken);
				token.GetNextToken();
				}
			while(token.AtEOL());

			try
				{
				// Look up position of taxon in NxsTaxaBlock list
				//
				k = taxa->FindTaxon(token.GetToken());

				// Array taxonPos is initialized to UINT_MAX and filled in as taxa are encountered
				//
				if (taxonPos[i] == UINT_MAX)
					{
					taxonPos[i] = k;
					}
				else if (taxonPos[i] != k)
					{
					errormsg = "Taxon labeled ";
					errormsg += token.GetToken();
					errormsg += " is out of order compared to previous interleave pages";
					throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
					}
				}

			catch (NxsTaxaBlock::NxsX_NoSuchTaxon)
				{
				errormsg = "Could not find ";
				errormsg += token.GetToken();
				errormsg += " among taxa previously defined";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}
			}

		else if (labels && newtaxa)
			{
			// Case 2: Expecting taxon labels, and also expecting taxa block to be empty
			//
			do
				{
				token.SetLabileFlagBit(NxsToken::newlineIsToken);
				token.GetNextToken();
				}
			while(token.AtEOL());

			taxa->AddTaxonLabel(token.GetToken());
			taxonPos[i] = i;
			}

		// Now deal with the row of distance values
		//
		unsigned true_j = 0;
		for (j = 0; j < ntax; j++)
			{
			if (i == ntax - 1 && j == ntax - 1)
				{
				done = true;
				}

			if ((i == ntax - 1) && (true_j == ntax - 1))
				{
				done = true;
				break;
				}

			if (i == ntax-1 && !diagonal && triangle == NxsDistancesBlockEnum(upper))
				{
				done = true;
				break;
				}

			if (!diagonal && triangle == NxsDistancesBlockEnum(lower) && j == ntax - offset - 1)
				{
				done = true;
				break;
				}

			token.SetLabileFlagBit(NxsToken::newlineIsToken);
			token.GetNextToken();

			if (token.AtEOL())
				{
				if (j > jmax)
					{
					jmax = j;
					if (!diagonal && triangle == NxsDistancesBlockEnum(upper) && i >= offset)
						jmax++;
					if (interleave && triangle == NxsDistancesBlockEnum(upper))
						i_last = jmax + offset;
					}
				break;
				}

			true_j = j + offset;
			if (triangle == NxsDistancesBlockEnum(upper) && i > offset)
				true_j += (i - offset);
			if (!diagonal && triangle == NxsDistancesBlockEnum(upper) && i >= offset)
				true_j++;

			if (true_j == ntax)
				{
				errormsg = "Too many distances specified in row just read in";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			string t = token.GetToken();
			if (token.GetTokenLength() == 1 && t[0] == missing)
				SetMissing(i, true_j);
			else
				SetDistance(i, true_j, atof(t.c_str()));
			}
		}

	offset += jmax;

	return done;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Called when MATRIX command needs to be parsed from within the DISTANCES block. Deals with everything after the 
|	token MATRIX up to and including the semicolon that terminates the MATRIX command.
*/
void NxsDistancesBlock::HandleMatrixCommand(
  NxsToken &token)	/* the token used to read from `in' */
	{
	unsigned i;
	unsigned prev_ntax = ntax;

	if (ntax == 0)
		ntax = taxa->GetNumTaxonLabels();

	if (ntax == 0)
		{
		errormsg = "MATRIX command cannot be read if NTAX is zero";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	if (triangle == NxsDistancesBlockEnum(both) && !diagonal)
		{
		errormsg = "Cannot specify NODIAGONAL and TRIANGLE=BOTH at the same time";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	if (newtaxa)
		taxa->Reset();

	// Allocate taxonPos array, deleting it first if previously allocated
	//
	if (taxonPos != NULL)
		{
		delete [] taxonPos;
		}

	taxonPos = new unsigned[ntax];
	
	for (i = 0; i < ntax; i++)
		taxonPos[i] = UINT_MAX;

	// Allocate matrix array, deleting it first if previously allocated
	//
	if (matrix != NULL)
		{
		assert(prev_ntax > 0);
		for (i = 0; i < prev_ntax; i++)
			delete [] matrix[i];
		delete [] matrix;
		}

	matrix = new NxsDistanceDatum*[ntax];
	for (i = 0; i < ntax; i++)
		matrix[i] = new NxsDistanceDatum[ntax];

	unsigned offset = 0;
	bool done = false;
	while (!done)
		{
		done = HandleNextPass(token, offset);
		}

	// Token should be equal to the terminating semicolon
	//
	token.GetNextToken();

	if (!token.Equals(";"))
		{
		errormsg = "Expecting ';' to terminate MATRIX command, but found ";
		errormsg += token.GetToken();
		errormsg += " instead";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Called when TAXLABELS command needs to be parsed from within the DISTANCES block. Deals with everything after the 
|	token TAXLABELS up to and including the semicolon that terminates the TAXLABELS command.
*/
void NxsDistancesBlock::HandleTaxlabelsCommand(
  NxsToken &token)	/* the token used to read from `in' */
	{
	if (!newtaxa)
		{
		errormsg = "NEWTAXA must have been specified in DIMENSIONS command to use the TAXLABELS command in a ";
		errormsg += id;
		errormsg += " block";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	if (ntax == 0)
		{
		errormsg = "NTAX must be specified before TAXLABELS command";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	for (unsigned i = 0; i < ntax; i++)
		{
        token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation + NxsToken::preserveUnderscores);
		token.GetNextToken();
		taxa->AddTaxonLabel(token.GetToken());
		}

	// This should be terminating semicolon
	//
	token.GetNextToken(); 

	if (!token.Equals(";"))
		{
		errormsg = "Expecting ';' to terminate TAXLABELS command, but found ";
		errormsg += token.GetToken();
		errormsg += " instead";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	// Some may object to setting newtaxa to false here, because then the
	// fact that new taxa were specified in this DISTANCES block rather than in
	// a preceding TAXA block is lost.  This will only be important if we wish to
	// recreate the original data file, which I don't anticipate anyone doing with
	// this code (too difficult to remember all comments, the order of blocks in
	// the file, etc.)
	//
	newtaxa = false;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	This function provides the ability to read everything following the block name (which is read by the NEXUS object)
|	to the end or endblock statement. Characters are read from the input stream in. Overrides the abstract virtual 
|	function in the base class.
*/
void NxsDistancesBlock::Read(
  NxsToken &token)	/* the token used to read from `in' */
	{
	isEmpty = false;

	// This should be the semicolon after the block name
	//
	token.GetNextToken(); 

	if (!token.Equals(";"))
		{
		errormsg = "Expecting ';' after ";
		errormsg += id;
		errormsg += " block name, but found ";
		errormsg += token.GetToken();
		errormsg += " instead";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	for (;;)
		{
		token.GetNextToken();

		if (token.Equals("DIMENSIONS"))
			{
			HandleDimensionsCommand(token);
			}

		else if (token.Equals("FORMAT"))
			{
			HandleFormatCommand(token);
			}

		else if (token.Equals("TAXLABELS"))
			{
			HandleTaxlabelsCommand(token);
			}

		else if (token.Equals("MATRIX"))
			{
			HandleMatrixCommand(token);
			}

		else if (token.Equals("END"))
			{
			// Get the semicolon following END
			//
			token.GetNextToken();

			if (!token.Equals(";"))
				{
				errormsg = "Expecting ';' to terminate the END command, but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			break;
			}

		else if (token.Equals("ENDBLOCK"))
			{
			// Get the semicolon following ENDBLOCK
			//
			token.GetNextToken();

			if (!token.Equals(";"))
				{
				errormsg = "Expecting ';' to terminate the ENDBLOCK command, but found ";
				errormsg += token.GetToken();
				errormsg += " instead";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}

			break;
			}

		else
			{
			SkippingCommand(token.GetToken());
			do
				{
				token.GetNextToken();
				}
			while (!token.AtEOF() && !token.Equals(";"));

			if (token.AtEOF())
				{
				errormsg = "Unexpected end of file encountered";
				throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
				}
			}
		}
	}

/*----------------------------------------------------------------------------------------------------------------------
|	This function outputs a brief report of the contents of this taxa block. Overrides the abstract virtual function in 
|	the base class.
*/
void NxsDistancesBlock::Report(
  ostream &out)	/* the output stream to which to write the report */
	{
	unsigned ntaxTotal = ntax;

	if (ntaxTotal == 0)
		ntaxTotal = taxa->GetNumTaxonLabels();

	out << endl;
	out << id << " block contains ";
	if (ntaxTotal == 0)
		{
		out << "no taxa" << endl;
		}
	else if (ntaxTotal == 1)
		out << "one taxon" << endl;
	else
		out << ntaxTotal << " taxa" << endl;

	if (IsLowerTriangular())
		out << "  Matrix is lower-triangular" << endl;
	else if (IsUpperTriangular())
		out << "  Matrix is upper-triangular" << endl;
	else
		out << "  Matrix is rectangular" << endl;

	if (IsInterleave())
		out << "  Matrix is interleaved" << endl;
	else 
		out << "  Matrix is non-interleaved" << endl;

	if (IsLabels())
		out << "  Taxon labels provided" << endl;
	else
		out << "  No taxon labels provided" << endl;

	if (IsDiagonal())
		out << "  Diagonal elements specified" << endl;
	else 
		out << "  Diagonal elements not specified" << endl;

	out << "  Missing data symbol is " << missing << endl;

	if (ntax == 0)
		return;

	out.setf(ios::fixed, ios::floatfield);
	out.setf(ios::showpoint);
	for (unsigned i = 0; i < ntax; i++)
		{
		if (labels)
			out << setw(20) << taxa->GetTaxonLabel(i);
		else
			out << "\t\t";

		for (unsigned j = 0; j < ntax; j++)
			{
			if (triangle == NxsDistancesBlockEnum(upper) && j < i)
				{
				out << setw(12) << " ";
				}
			else if (triangle == NxsDistancesBlockEnum(lower) && j > i)
				continue;
			else if (!diagonal && i == j)
				{
				out << setw(12) << " ";
				}
			else if (IsMissing(i, j))
				out << setw(12) << missing;
			else
				out << setw(12) << setprecision(5) << GetDistance(i, j);
			}

		out << endl;
		}
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Flushes taxonLabels and sets ntax to 0 in preparation for reading a new TAXA block.
*/
void NxsDistancesBlock::Reset()
	{
	// Reset base class data members that could have changed
	//
	errormsg.clear();
	isEnabled      = true;
	isEmpty        = true;
	isUserSupplied = false;

	if (matrix != NULL)
		{
		for (unsigned i = 0; i < ntax; i++)
			delete [] matrix[i];
		delete [] matrix;
		matrix = NULL;
		}

	if (taxonPos != NULL)
		delete [] taxonPos;
	taxonPos = NULL;

	ntax        = 0;
	nchar       = 0;
	diagonal    = true;
	labels      = true;
	newtaxa     = false;
	interleave  = false;
	missing     = '?';
	triangle    = NxsDistancesBlockEnum(lower);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of ntax.
*/
unsigned NxsDistancesBlock::GetNtax()
	{
	return ntax;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of nchar.
*/
unsigned NxsDistancesBlock::GetNchar()
	{
	return nchar;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of the (`i', `j')th element of `matrix'. Assumes `i' and `j' are both in the range [0..`ntax') 
|	and the distance stored at `matrix[i][j]' is not missing. Also assumes `matrix' is not NULL.
*/
double NxsDistancesBlock::GetDistance(
  unsigned i,	/* the row */
  unsigned j)	/* the column */
	{
	assert(i >= 0);
	assert(i < ntax);
	assert(j >= 0);
	assert(j < ntax);
	assert(matrix != NULL);

	return matrix[i][j].value;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of `missing'.
*/
char NxsDistancesBlock::GetMissingSymbol()
	{
	return missing;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of `triangle'.
*/
unsigned NxsDistancesBlock::GetTriangle()
	{
	return triangle;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns true if the value of `triangle' is NxsDistancesBlockEnum(both), false otherwise.
*/
bool NxsDistancesBlock::IsRectangular()
	{
	return (triangle == NxsDistancesBlockEnum(both) ? true : false);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns true if the value of triangle is NxsDistancesBlockEnum(upper), false otherwise.
*/
bool NxsDistancesBlock::IsUpperTriangular()
	{
	return (triangle == NxsDistancesBlockEnum(upper) ? true : false);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns true if the value of triangle is NxsDistancesBlockEnum(lower), false otherwise.
*/
bool NxsDistancesBlock::IsLowerTriangular()
	{
	return (triangle == NxsDistancesBlockEnum(lower) ? true : false);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of diagonal.
*/
bool NxsDistancesBlock::IsDiagonal()
	{
	return diagonal;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of interleave.
*/
bool NxsDistancesBlock::IsInterleave()
	{
	return interleave;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the value of labels.
*/
bool NxsDistancesBlock::IsLabels()
	{
	return labels;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns true if the (`i',`j')th distance is missing. Assumes `i' and `j' are both in the range [0..`ntax') and 
|	`matrix' is not NULL.
*/
bool NxsDistancesBlock::IsMissing(
  unsigned i,	/* the row */
  unsigned j)	/* the column */
	{
	assert(i >= 0);
	assert(i < ntax);
	assert(j >= 0);
	assert(j < ntax);
	assert(matrix != NULL);

	return (bool)(matrix[i][j].missing);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Sets the value of the (`i',`j')th matrix element to `d' and `missing' to false . Assumes `i' and `j' are both in 
|	the range [0..`ntax') and `matrix' is not NULL.
*/
void NxsDistancesBlock::SetDistance(
  unsigned i,	/* the row */
  unsigned j,	/* the column */
  double d)		/* the distance value */
	{
	assert(i >= 0);
	assert(i < ntax);
	assert(j >= 0);
	assert(j < ntax);
	assert(matrix != NULL);

	matrix[i][j].value = d;
	matrix[i][j].missing = false;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Sets the value of the (`i', `j')th `matrix' element to missing. Assumes `i' and `j' are both in the range 
|	[0..`ntax') and `matrix' is not NULL.
*/
void NxsDistancesBlock::SetMissing(
  unsigned i,	/* the row */
  unsigned j)	/* the column */
	{
	assert(i >= 0);
	assert(i < ntax);
	assert(j >= 0);
	assert(j < ntax);
	assert(matrix != NULL);

	matrix[i][j].missing = 1;
	matrix[i][j].value = 0.0;
	}

 /*----------------------------------------------------------------------------------------------------------------------
|	Sets `nchar' to `n'.
*/
void NxsDistancesBlock::SetNchar(
  unsigned n)	/* the number of characters */
	{
	nchar = n;
	}