File: nxsassumptionsblock.cpp

package info (click to toggle)
iqtree 1.6.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,140 kB
  • sloc: cpp: 111,752; ansic: 53,619; python: 242; sh: 195; makefile: 52
file content (535 lines) | stat: -rw-r--r-- 16,908 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
//	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 = "ASSUMPTIONS", charBlockPtr = NULL, and taxa = t. Assumes taxa is non-NULL.
*/
NxsAssumptionsBlock::NxsAssumptionsBlock(
  NxsTaxaBlock *t)	/* pointer to the taxa block */
	{
	assert(t);
	taxa			= t;
	charBlockPtr	= NULL;
	id				= "ASSUMPTIONS";
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Nothing needs to be done in the destructor.
*/
NxsAssumptionsBlock::~NxsAssumptionsBlock()
{
}

/*----------------------------------------------------------------------------------------------------------------------
|	Makes data member taxa point to 'tb'. Assumes tb is non-NULL.
*/
void NxsAssumptionsBlock::ReplaceTaxaBlockPtr(
  NxsTaxaBlock *tb)	/* pointer to new NxsTaxaBlock object */
	{
	assert(tb);
	taxa = tb;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the number of character sets stored.
*/
int NxsAssumptionsBlock::GetNumCharSets()
	{
	return (int)charsets.size();
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Erases 'names' vector, then fills 'names' with the names of all stored character sets.
*/
void NxsAssumptionsBlock::GetCharSetNames(
  NxsStringVector &names)	/* the vector in which to store the names */
	{
	names.erase(names.begin(), names.end());
	NxsUnsignedSetMap::const_iterator i;
	for (i = charsets.begin(); i != charsets.end(); i++)
	names.push_back((*i).first);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns reference to character set having name 'nm'.
*/
NxsUnsignedSet &NxsAssumptionsBlock::GetCharSet(
  NxsString nm)	/* the name of the character set to return */
	{
	return charsets[nm];
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns name of default character set. If returned string has zero length, then no default character set was defined
|	in the data set.
*/
NxsString NxsAssumptionsBlock::GetDefCharSetName()
	{
	return def_charset;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the number of taxon sets stored.
*/
int NxsAssumptionsBlock::GetNumTaxSets()
	{
	return (int)taxsets.size();
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Erases 'names' vector, then fills 'names' with the names of all stored taxon sets.
*/
void NxsAssumptionsBlock::GetTaxSetNames(
  NxsStringVector &names)	/* the vector in which to store the names */
	{
	names.erase(names.begin(), names.end());
	NxsUnsignedSetMap::const_iterator i;
	for (i = taxsets.begin(); i != taxsets.end(); i++)
		names.push_back((*i).first);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns reference to taxon set having name 'nm'.
*/
NxsUnsignedSet &NxsAssumptionsBlock::GetTaxSet(
  NxsString nm)	/* the name of the taxon set to return */
	{
	return taxsets[nm];
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns name of default taxon set. If returned string has zero length, then no default taxon set was defined in the
|	data set.
*/
NxsString NxsAssumptionsBlock::GetDefTaxSetName()
	{
	return def_taxset;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns the number of exclusion sets stored.
*/
int NxsAssumptionsBlock::GetNumExSets()
	{
	return (int)exsets.size();
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Erases names, then fills names with the names of all stored exclusion sets.
*/
void NxsAssumptionsBlock::GetExSetNames(
  NxsStringVector &names)	/* the vector in which to store the names */
	{
	names.erase(names.begin(), names.end());
	NxsUnsignedSetMap::const_iterator i;
	for (i = exsets.begin(); i != exsets.end(); i++)
		names.push_back((*i).first);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns reference to exclusion set having name 'nm'.
*/
NxsUnsignedSet &NxsAssumptionsBlock::GetExSet(
  NxsString nm)	/* the name of the exclusion set to return */
	{
	return exsets[nm];
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Returns name of default exclusion set. If returned string has zero length, then no default exclusion set was defined
|	in the data set.
*/
NxsString NxsAssumptionsBlock::GetDefExSetName()
	{
	return def_exset;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Applies exclusion set having name 'nm' by calling the ApplyExset method of the NxsCharactersBlock or 
|	NxsCharactersBlock-derived object stored in the charBlockPtr pointer (which will be whichever block last called the 
|	NxsAssumptionsBlock::SetCallback method).
*/
void NxsAssumptionsBlock::ApplyExSet(
  NxsString nm)	/* the name of the exclusion set to apply */
	{
	assert(charBlockPtr);
	charBlockPtr->ApplyExset(exsets[nm]);
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Reads and stores information contained in the command CHARSET within an ASSUMPTIONS block.
*/
void NxsAssumptionsBlock::HandleCharset(
  NxsToken &token)	/* the token used to read from in */
	{
	bool asterisked = false;

	// Next token should be either an asterisk or the name of a charset
	//
	token.GetNextToken();

	if (token.Equals("*"))
		{
		asterisked = true;
		token.GetNextToken();
		}

	// Token now stored should be the name of a charset
	//
	NxsString charset_name = token.GetToken();

	// Now grab the equals sign
	//
	token.GetNextToken();
	if (!token.Equals("="))
		{
		errormsg = "Expecting '=' in CHARSET definition but found ";
		errormsg += token.GetToken();
		errormsg += " instead";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	assert(charBlockPtr);
	NxsCharactersBlock &charBlock = *charBlockPtr;
	NxsUnsignedSet s;
	int totalChars = charBlock.GetNCharTotal();
	NxsSetReader(token, totalChars, s, charBlock, NxsSetReader::charset).Run();

	charsets[charset_name] = s;

	if (asterisked)
		def_charset = charset_name;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Called when the END or ENDBLOCK command needs to be parsed from within the ASSUMPTIONS block. Basically just checks
|	to make sure the next token in the data file is a semicolon.
*/
void NxsAssumptionsBlock::HandleEndblock(
  NxsToken &token)	/* the token used to read from in */
	{
	// Get the semicolon following END or ENDBLOCK token
	//
	token.GetNextToken();

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

/*----------------------------------------------------------------------------------------------------------------------
|	Reads and stores information contained in the command EXSET within an ASSUMPTIONS block. If EXSET keyword is 
|	followed by an asterisk, last read NxsCharactersBlock or NxsCharactersBlock-derived object is notified of the 
|	characters to be excluded (its ApplyExset function is called).
*/
void NxsAssumptionsBlock::HandleExset(
  NxsToken &token)	/* the token used to read from in */
	{
	bool asterisked = false;

	// Next token should be either an asterisk or the name of an exset
	//
	token.GetNextToken();

	if (token.Equals("*"))
		{
		asterisked = true;
		token.GetNextToken();
		}

	// Token now stored should be the name of an exset
	//
	NxsString exset_name = token.GetToken();

	// Now grab the equals sign
	//
	token.GetNextToken();
	if (!token.Equals("="))
		{
		errormsg = "Expecting '=' in EXSET definition but found ";
		errormsg += token.GetToken();
		errormsg += " instead";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	assert(charBlockPtr);
	NxsCharactersBlock &charBlock = *charBlockPtr;
	NxsUnsignedSet s;
	int totalChars = charBlock.GetNCharTotal();
	NxsSetReader(token, totalChars, s, charBlock, NxsSetReader::charset).Run();

	exsets[exset_name] = s;

	if (asterisked)
		{
		def_exset = exset_name;
		charBlock.ApplyExset(s);
		}
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Reads and stores information contained in the command TAXSET within an ASSUMPTIONS block.
*/
void NxsAssumptionsBlock::HandleTaxset(
  NxsToken &token)	/* the token used to read from in */
	{
	bool asterisked = false;

	// Next token should be either an asterisk or the name of a taxset
	//
	token.GetNextToken();

	if (token.Equals("*"))
		{
		asterisked = true;
		token.GetNextToken();
		}

	// Token now stored should be the name of a taxset
	//
	NxsString taxset_name = token.GetToken();

	// Now grab the equals sign
	//
	token.GetNextToken();
	if (!token.Equals("="))
		{
		errormsg = "Expecting '=' in TAXSET definition but found ";
		errormsg += token.GetToken();
		errormsg += " instead";
		throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn());
		}

	NxsUnsignedSet s;
	int totalTaxa = taxa->GetNumTaxonLabels();
	NxsSetReader(token, totalTaxa, s, *this, NxsSetReader::taxset).Run();

	taxsets[taxset_name] = s;

	if (asterisked)
		def_taxset = taxset_name;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	This function provides the ability to read everything following the block name (which is read by the NxsReader 
|	object) to the end or ENDBLOCK statement. Characters are read from the input stream in. Overrides the pure virtual
|	function in the base class.
*/
void NxsAssumptionsBlock::Read(
  NxsToken &token)	/* the token used to read from in */
	{
	isEmpty = false;
	isUserSupplied = true;

	// 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("EXSET"))
			{
			HandleExset(token);
			}
		else if (token.Equals("TAXSET"))
			{
			HandleTaxset(token);
			}
		else if (token.Equals("CHARSET"))
			{
			HandleCharset(token);
			}
		else if (token.Equals("END"))
			{
			HandleEndblock(token);
			break;
			}
		else if (token.Equals("ENDBLOCK"))
			{
			HandleEndblock(token);
			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());
				}
			}
		}	// for(;;)
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Prepares for reading a new ASSUMPTIONS block. Overrides the pure virtual function in the base class.
*/
void NxsAssumptionsBlock::Reset()
	{
	exsets.erase(exsets.begin(), exsets.end());
	taxsets.erase(taxsets.begin(), taxsets.end());
	charsets.erase(charsets.begin(), charsets.end());
	def_taxset.clear();
	def_charset.clear();
	def_exset.clear();
	errormsg.clear();
	isEnabled		= true;
	isEmpty			= true;
	isUserSupplied	= false;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	This function outputs a brief report of the contents of this ASSUMPTIONS block. Overrides the pure virtual function
|	in the base class.
*/
void NxsAssumptionsBlock::Report(
  ostream &out)	/* the output stream to which to write the report */
	{
	out << endl;
	out << id << " block contains the following:" << endl;

	if (charsets.empty())
		out << "  No character sets were defined" << endl;
	else
		{
		NxsUnsignedSetMap::const_iterator charsets_iter = charsets.begin();
		if (charsets.size() == 1)
			{
			out << "  1 character set defined:" << endl;
			out << "    " << (*charsets_iter).first << endl;
			}
		else
			{
			out << "  " << charsets.size() << " character sets defined:" << endl;
			for (; charsets_iter != charsets.end(); charsets_iter++)
				{
				NxsString nm = (*charsets_iter).first;
				out << "    " << nm;
				if (nm == def_charset)
					out << " (default)";
				out << endl;
				}
			}
		}	// if (charsets.empty()) ... else

	if (taxsets.empty())
		out << "  No taxon sets were defined" << endl;
	else
		{
		NxsUnsignedSetMap::const_iterator taxsets_iter = taxsets.begin();
		if (taxsets.size() == 1)
			{
			out << "  1 taxon set defined:" << endl;
			out << "    " << (*taxsets_iter).first << endl;
			}
		else
			{
			out << "  " << taxsets.size() << " taxon sets defined:" << endl;
			for (; taxsets_iter != taxsets.end(); taxsets_iter++)
				{
				NxsString nm = (*taxsets_iter).first;
				out << "    " << nm;
				if (nm == def_taxset)
					out << " (default)";
				out << endl;
				}
			}
		}	// if (taxsets.empty()) ... else

	if (exsets.empty())
		out << "  No exclusion sets were defined" << endl;
	else
		{
		NxsUnsignedSetMap::const_iterator exsets_iter = exsets.begin();
		if (exsets.size() == 1)
			{
			out << "  1 exclusion set defined:" << endl;
			out << "    " << (*exsets_iter).first << endl;
			}
		else
			{
			out << "  " << exsets.size() << " exclusion sets defined:" << endl;
			for (; exsets_iter != exsets.end(); exsets_iter++)
				{
				NxsString nm = (*exsets_iter).first;
				out << "    " << nm;
				if (nm == def_exset)
				out << " (default)";
				out << endl;
				}
			}
		}

	out << endl;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	A CHARACTERS, DATA, or ALLELES block can call this function to specify that it is to receive notification when the 
|	current taxon or character set changes (e.g., an "EXSET *" command is read or a program requests that one of the 
|	predefined taxon sets, character sets, or exsets be applied). Normally, a NxsCharactersBlock-derived object calls 
|	this function upon entering its MATRIX command, since when that happens it becomes the primary data-containing block.
*/
void NxsAssumptionsBlock::SetCallback(
  NxsCharactersBlock* p)	/* the object to be called in the event of a change in character status */
	{
	charBlockPtr = p;
	}

/*----------------------------------------------------------------------------------------------------------------------
|	Converts a taxon label to a number corresponding to the taxon's position within the list maintained by the 
|	NxsTaxaBlock object. This method overrides the virtual function of the same name in the NxsBlock base class. If s 
|	is not a valid taxon label, returns the value 0.
*/
unsigned NxsAssumptionsBlock::TaxonLabelToNumber(
  NxsString s)	/* the taxon label to convert */
	{
	int i;
	try
		{
		i = 1 + taxa->FindTaxon(s);
		}
	catch(NxsTaxaBlock::NxsX_NoSuchTaxon)
		{
		i = 0;
		}

	return i;
	}