/
nexusparsercharactersblock.cpp
1963 lines (1699 loc) · 78.8 KB
/
nexusparsercharactersblock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
/*------------------------------------------------------------------------------------------------------
* Matrix Data Editor (MaDE)
*
* Copyright (c) 2012-2013, Alan R.T. Spencer
*
* This program 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 3 of the License,
* or (at your option) any later version.
*
* This program 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 3 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see http://www.gnu.org/licenses/.
*-----------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------
* Modified from work by Paul O. Lewis and Mark T. Holder. Available from http://sourceforge.net/p/ncl/
* under a GNU General Public License version 2.0 (GPLv2) Licence.
*
* Nexus Class Libray (NCL)
*
* Copyright (c) 2003-2012, Paul O. Lewis
* Copyright (c) 2007-2012, Mark T. Holder
*
* This program 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.
*
* This program 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 this program; if not,
* write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*-----------------------------------------------------------------------------------------------------*/
#include "nexusparser.h"
NexusParserCharactersBlock::NexusParserCharactersBlock(NexusParserReader *pointer, NexusParserTaxaBlock *tBlock, NexusParserAssumptionsBlock *aBlock)
{
Q_ASSERT(tBlock != NULL);
Q_ASSERT(aBlock != NULL);
setNexusParserReader(pointer);
blockID = "CHARACTERS";
taxaBlock = tBlock;
assumptionsBlock = aBlock;
reset();
}
NexusParserCharactersBlock::~NexusParserCharactersBlock()
{
reset();
if(symbols.count() > 0) {
symbols.clear();
}
}
// Reset
void NexusParserCharactersBlock::reset()
{
NexusParserBlock::reset();
ncharTotal = 0;
nchar = 0;
nextCharacterID = 0;
characterList.clear();
newchar = true;
ntaxTotal = 0;
ntax = 0;
newtaxa = false;
interleaving = false;
transposing = false;
respectingCase = false;
labels = true;
tokens = false;
datatype = NexusParserCharactersBlock::standard;
missing = nexusParser->defaultMissingCharacter;
gap = nexusParser->defaultGapCharacter;
matchchar = nexusParser->defaultMatchCharacter;
resetSymbols();
equatesList.clear();
items.clear();
items.append("STATES");
statesFormat = STATES_PRESENT;
}
/*------------------------------------------------------------------------------------/
* Read Function
*-----------------------------------------------------------------------------------*/
// This function provides the ability to read everything following the block name (which is read by the NexusParserReader
// 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 NexusParserCharactersBlock::read(NexusParserToken &token)
{
isEmpty = false;
demandEndSemicolon(token, QString("BEGIN %1").arg(blockID));
// Get Taxa Number (nTax)
ntax = taxaBlock->getNumTaxonLabels();
for(;;)
{
token.getNextToken();
NexusParserBlock::NexusParserCommandResult result = handleBasicBlockCommands(token);
if (result == NexusParserBlock::NexusParserCommandResult(STOP_PARSING_BLOCK)){
return;
}
if (result != NexusParserBlock::NexusParserCommandResult(HANDLED_COMMAND)) {
if (token.equals("DIMENSIONS")) {
handleDimensions(token, "NEWTAXA", "NTAX", "NCHAR");
} else if (token.equals("FORMAT")) {
handleFormat(token);
} else if (token.equals("ELIMINATE")) {
handleEliminate(token);
} else if (token.equals("TAXLABELS")) {
handleTaxlabels(token);
} else if (token.equals("CHARSTATELABELS")) {
handleCharstatelabels(token);
} else if (token.equals("CHARLABELS")) {
handleCharlabels(token);
} else if (token.equals("STATELABELS")) {
handleStatelabels(token);
} else if (token.equals("MATRIX")) {
handleMatrix(token);
} else {
skippingCommand(token.getToken());
do {
token.getNextToken();
} while (!token.getAtEndOfFile() && !token.equals(";"));
if (token.getAtEndOfFile()){
errorMessage = "Quick, hide under the desk we have a problem! Unexpected end of file encountered";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
}
}
}
}
/*------------------------------------------------------------------------------------/
* handleXXXXXX Functions
*-----------------------------------------------------------------------------------*/
// Called when DIMENSIONS command needs to be parsed from within the CHARACTERS block. Deals with everything after
// the token DIMENSIONS up to and including the semicolon that terminates the DIMENSIONs command. `newtaxaLabel',
// `ntaxLabel' and `ncharLabel' are simply "NEWTAXA", "NTAX" and "NCHAR" for this class, but may be different for
// derived classes that use `newtaxa', `ntax' and `nchar' for other things (e.g., ntax is number of populations in
// an ALLELES block)
void NexusParserCharactersBlock::handleDimensions(NexusParserToken &token, QString newtaxaLabel, QString ntaxLabel, QString ncharLabel)
{
nexusParser->logMesssage(
QString("%1 BLOCK: found command \"DIMENSIONS\" on line %2, now looking for \"%3\" or \"%4\" or \"%5\"...")
.arg(blockID)
.arg(token.getFileLine())
.arg(newtaxaLabel)
.arg(ntaxLabel)
.arg(ncharLabel)
);
for (;;)
{
token.getNextToken();
if (token.equals(newtaxaLabel)) {
newtaxa = true;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"%2\" on line %3.")
.arg(blockID)
.arg(newtaxaLabel)
.arg(token.getFileLine())
);
} else if (token.equals(ntaxLabel)){
demandEquals(token, "in DIMENSIONS command");
ntax = demandPositiveInt(token, ntaxLabel);
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"%2\" on line %3. NTAX = %4.")
.arg(blockID)
.arg(ntaxLabel)
.arg(token.getFileLine())
.arg(ntax)
);
if (newtaxa) {
ntaxTotal = ntax;
} else {
ntaxTotal = taxaBlock->getNumTaxonLabels();
if (ntaxTotal < ntax){
errorMessage = ntaxLabel;
errorMessage += " in ";
errorMessage += blockID;
errorMessage += " block must be less than or equal to NTAX in TAXA block\nNote: one circumstance that can cause this error is \nforgetting to specify ";
errorMessage += ntaxLabel;
errorMessage += " in DIMENSIONS command when \na TAXA block has not been provided";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
}
} else if (token.equals(ncharLabel)){
demandEquals(token, "in DIMENSIONS command");
nchar = demandPositiveInt(token, ncharLabel);
ncharTotal = nchar;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"%2\" on line %3. NCHAR = %4.")
.arg(blockID)
.arg(ncharLabel)
.arg(token.getFileLine())
.arg(nchar)
);
} else if (token.equals(";")){
break;
}
}
if (newtaxa) {
taxaBlock->reset();
}
}
// Called when FORMAT command needs to be parsed from within the CHARACTERS block. Deals with everything after the
// token FORMAT up to and including the semicolon that terminates the FORMAT command.
void NexusParserCharactersBlock::handleFormat(NexusParserToken &token)
{
nexusParser->logMesssage(
QString("%1 BLOCK: found command \"FORMAT\" on line %2...")
.arg(blockID)
.arg(token.getFileLine())
);
bool standardDataTypeAssumed = false;
bool ignoreCaseAssumed = false;
for (;;)
{
token.getNextToken();
if (token.equals("DATATYPE")){
demandEquals(token, "after keyword DATATYPE");
// This should be one of the following: STANDARD, DNA, RNA, NUCLEOTIDE, PROTEIN, or CONTINUOUS
token.getNextToken();
if (token.equals("STANDARD")) {
datatype = standard;
} else if (token.equals("DNA")) {
datatype = dna;
} else if (token.equals("RNA")) {
datatype = rna;
} else if (token.equals("NUCLEOTIDE")) {
datatype = nucleotide;
} else if (token.equals("PROTEIN")) {
datatype = protein;
} else if (token.equals("CONTINUOUS")) {
datatype = continuous;
statesFormat = INDIVIDUALS;
items.append("AVERAGE");
} else {
errorMessage = token.getToken();
errorMessage += " is not a valid DATATYPE within a ";
errorMessage += blockID;
errorMessage += " block";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"DATATYPE\" on line %2. DATATYPE = %3.")
.arg(blockID)
.arg(token.getFileLine())
.arg(token.getToken())
);
if (standardDataTypeAssumed && datatype != standard) {
throw NexusParserException("DATATYPE must be specified first in FORMAT command",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
resetSymbols();
if (datatype == continuous) {
tokens = true;
}
} else if (token.equals("RESPECTCASE")) {
if (ignoreCaseAssumed) {
throw NexusParserException("RESPECTCASE must be specified before MISSING, GAP, SYMBOLS, and MATCHCHAR in FORMAT command",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"RESPECTCASE\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
respectingCase = true;
} else if (token.equals("MISSING")) {
demandEquals(token, "after keyword MISSING");
// This should be the missing data symbol (single character)
token.getNextToken();
if (token.getTokenLength() != 1){
errorMessage = "MISSING symbol should be a single character, but ";
errorMessage += token.getToken();
errorMessage += " was specified";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
} else if (token.isPunctuationToken() && !token.isPlusMinusToken()){
errorMessage = "MISSING symbol specified cannot be a punctuation token (";
errorMessage += token.getToken();
errorMessage += " was specified)";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
} else if (token.isWhitespaceToken()) {
errorMessage = "MISSING symbol specified cannot be a whitespace character (";
errorMessage += token.getToken();
errorMessage += " was specified)";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
missing = token.getToken().at(0);
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"MISSING\" on line %2. MISSING = %3.")
.arg(blockID)
.arg(token.getFileLine())
.arg(missing)
);
ignoreCaseAssumed = true;
standardDataTypeAssumed = true;
} else if (token.equals("GAP")) {
demandEquals(token, "after keyword GAP");
// This should be the gap symbol (single character)
token.getNextToken();
if (token.getTokenLength() != 1) {
errorMessage = "GAP symbol should be a single character, but ";
errorMessage += token.getToken();
errorMessage += " was specified";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
} else if (token.isPunctuationToken() && !token.isPlusMinusToken()) {
errorMessage = "GAP symbol specified cannot be a punctuation token (";
errorMessage += token.getToken();
errorMessage += " was specified)";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
} else if (token.isWhitespaceToken()) {
errorMessage = "GAP symbol specified cannot be a whitespace character (";
errorMessage += token.getToken();
errorMessage += " was specified)";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
gap = token.getToken().at(0);
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"GAP\" on line %2. GAP = %3.")
.arg(blockID)
.arg(token.getFileLine())
.arg(gap)
);
ignoreCaseAssumed = true;
standardDataTypeAssumed = true;
} else if (token.equals("SYMBOLS")) {
if (datatype == NexusParserCharactersBlock::continuous) {
throw NexusParserException("SYMBOLS subcommand not allowed for DATATYPE=CONTINUOUS",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
int numDefStates;
int maxNewStates;
switch(datatype){
case NexusParserCharactersBlock::dna:
case NexusParserCharactersBlock::rna:
case NexusParserCharactersBlock::nucleotide:
numDefStates = 4;
maxNewStates = NEXUS_MAX_STATES-numDefStates;
break;
case NexusParserCharactersBlock::protein:
numDefStates = 21;
maxNewStates = NEXUS_MAX_STATES-numDefStates;
break;
default:
numDefStates = 0;
symbols.clear();
maxNewStates = NEXUS_MAX_STATES-numDefStates;
break;
}
demandEquals(token, " after keyword SYMBOLS");
// This should be the symbols list
token.setLabileFlagBit(NexusParserToken::doubleQuotedToken);
token.getNextToken();
token.stripWhitespace();
int numNewSymbols = token.getTokenLength();
if (numNewSymbols > maxNewStates){
errorMessage = "SYMBOLS defines ";
errorMessage += numNewSymbols;
errorMessage += " new states but only ";
errorMessage += maxNewStates;
errorMessage += " new states allowed for this DATATYPE";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
QString t = token.getToken();
int tlen = t.size();
// Check to make sure user has not used any symbols already in the default symbols list for this data type
for (int i = 0; i < tlen; i++)
{
if (isInSymbols(t.at(i))){
errorMessage = "The character ";
errorMessage += t[i];
errorMessage += " defined in SYMBOLS has already been predefined for this DATATYPE";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
}
// If we've made it this far, go ahead and add the user-defined symbols to the end of the list of predefined symbols
QString symbolsString;
for (int i = 0; i < tlen; i++)
{
symbols.append(t.at(i));
symbolsString.append(t.at(i));
}
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"SYMBOLS\" on line %2. SYMBOLS = %3.")
.arg(blockID)
.arg(token.getFileLine())
.arg(symbolsString)
);
ignoreCaseAssumed = true;
standardDataTypeAssumed = true;
} else if (token.equals("EQUATE")) {
if (datatype == NexusParserCharactersBlock::continuous) {
throw NexusParserException("EQUATE subcommand not allowed for DATATYPE=CONTINUOUS",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
demandEquals(token, "after keyword EQUATE");
// This should be a double-quote character
token.getNextToken();
if (!token.equals("\"")) {
errorMessage = "Expecting '\"' after keyword EQUATE but found ";
errorMessage += token.getToken();
errorMessage += " instead";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
// Loop until second double-quote character is encountered
for (;;)
{
token.getNextToken();
if (token.equals("\"")) {
break;
}
// If token is not a double-quote character, then it must be the equate symbol (i.e., the
// character to be replaced in the data matrix)
if (token.getTokenLength() != 1)
{
errorMessage = "Expecting single-character EQUATE symbol but found ";
errorMessage += token.getToken();
errorMessage += " instead";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
// Check for bad choice of equate symbol
QString t = token.getToken();
QChar ch = t.at(0);
bool badEquateSymbol = false;
// The character '^' cannot be an equate symbol
if (ch == QChar('^')) {
badEquateSymbol = true;
}
// Equate symbols cannot be punctuation (except for + and -)
if (token.isPunctuationToken() && !token.isPlusMinusToken()){
badEquateSymbol = true;
}
// Equate symbols cannot be same as matchchar, missing, or gap
if (ch == missing || ch == matchchar || ch == gap){
badEquateSymbol = true;
}
// Equate symbols cannot be one of the state symbols currently defined
if (isInSymbols(ch)){
badEquateSymbol = true;
}
if (badEquateSymbol){
errorMessage = "EQUATE symbol specified (";
errorMessage += token.getToken();
errorMessage += ") is not valid; must not be same as missing, matchchar, gap, state symbols, or any of the following: ()[]{}/\\,;:=*'\"`<>^";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
QString symbol = token.getToken();
demandEquals(token, " in EQUATE definition");
// This should be the token to be substituted in for the equate symbol
token.setLabileFlagBit(NexusParserToken::parentheticalToken);
token.setLabileFlagBit(NexusParserToken::curlyBracketedToken);
token.getNextToken();
QString equivalent = token.getToken();
// Add the new equate association to the equates list
equateAdd(symbol, equivalent);
}
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"EQUATE\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("MATCHCHAR")) {
demandEquals(token, "after keyword MATCHCHAR");
// This should be the matchchar symbol (single character)
token.getNextToken();
if (token.getTokenLength() != 1){
errorMessage = "MATCHCHAR symbol should be a single character, but ";
errorMessage += token.getToken();
errorMessage += " was specified";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
} else if (token.isPunctuationToken() && !token.isPlusMinusToken()){
errorMessage = "MATCHCHAR symbol specified cannot be a punctuation token (";
errorMessage += token.getToken();
errorMessage += " was specified) ";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
} else if (token.isWhitespaceToken()){
errorMessage = "MATCHCHAR symbol specified cannot be a whitespace character (";
errorMessage += token.getToken();
errorMessage += " was specified)";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
matchchar = token.getToken().at(0);
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"MATCHCHAR\" on line %2. MATCHCHAR = %3.")
.arg(blockID)
.arg(token.getFileLine())
.arg(matchchar)
);
ignoreCaseAssumed = true;
standardDataTypeAssumed = true;
} else if (token.equals("LABELS")) {
labels = true;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"LABELS\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("NOLABELS")) {
labels = false;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"NOLABELS\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("TRANSPOSE")) {
transposing = true;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"TRANSPOSE\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("INTERLEAVE")) {
interleaving = true;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"INTERLEAVE\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("ITEMS")){
demandEquals(token, "after keyword ITEMS");
items.clear();
// This should be STATES (no other item is supported at this time)
token.getNextToken();
if (datatype == NexusParserCharactersBlock::continuous){
QString str;
if (token.equals("(")){
token.getNextToken();
while (!token.equals(")")){
str = token.getToken().toUpper();
items.append(str);
token.getNextToken();
}
} else {
str = token.getToken().toUpper();
items.append(str);
}
} else {
if (!token.equals("STATES")) {
throw NexusParserException("Sorry, only ITEMS=STATES is supported for discrete datatypes at this time",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
items.clear();
items.append("STATES");
}
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"ITEMS\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("STATESFORMAT")) {
demandEquals(token, "after keyword STATESFORMAT");
// This should be STATESPRESENT (no other statesformat is supported at this time)
token.getNextToken();
if (token.equals("STATESPRESENT")) {
statesFormat = STATES_PRESENT;
} else {
if (datatype == NexusParserCharactersBlock::continuous) {
if (token.equals("INDIVIDUALS")) {
statesFormat = INDIVIDUALS;
} else {
throw NexusParserException("Sorry, only STATESFORMAT=STATESPRESENT or STATESFORMAT=INDIVIDUALS are supported for continuous datatypes at this time",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
} else {
throw NexusParserException("Sorry, only STATESFORMAT=STATESPRESENT supported for discrete datatypes at this time",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
}
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"STATESFORMAT\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("TOKENS")) {
tokens = true;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"TOKENS\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals("NOTOKENS")) {
if (datatype == NexusParserCharactersBlock::continuous) {
throw NexusParserException("NOTOKENS is not allowed for the CONTINUOUS datatype",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
tokens = false;
nexusParser->logMesssage(
QString("%1 BLOCK: found subcommand \"NOTOKENS\" on line %2.")
.arg(blockID)
.arg(token.getFileLine())
);
standardDataTypeAssumed = true;
} else if (token.equals(";")) {
break;
}
}
// Perform some last checks before leaving the FORMAT command
if (!tokens && datatype == continuous) {
throw NexusParserException("TOKENS must be defined for DATATYPE=CONTINUOUS",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
if (tokens && (datatype == dna || datatype == rna || datatype == nucleotide)) {
throw NexusParserException("TOKENS not allowed for the DATATYPEs DNA, RNA, or NUCLEOTIDE",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
}
// Called when ELIMINATE command needs to be parsed from within the CHARACTERS block. Deals with everything after the
// token ELIMINATE up to and including the semicolon that terminates the ELIMINATE command. Any character numbers
// or ranges of character numbers specified are stored in the NxsUnsignedSet `eliminated', which remains empty until
// an ELIMINATE command is processed. Note that like all sets the character ranges are adjusted so that their offset
// is 0. For example, given "eliminate 4-7;" in the data file, the eliminate array would contain the values 3, 4, 5
// and 6 (not 4, 5, 6 and 7). It is assumed that the ELIMINATE command comes before character labels and/or character
// state labels have been specified; an error message is generated if the user attempts to use ELIMINATE after a
// CHARLABELS, CHARSTATELABELS, or STATELABELS command.
void NexusParserCharactersBlock::handleEliminate(NexusParserToken &token)
{
nexusParser->logMesssage(
QString("%1 BLOCK: found command \"ELIMINATE\" on line %2...")
.arg(blockID)
.arg(token.getFileLine())
);
// Construct an object of type NexusParserSetReader, then call its run function
// to store the set in the eliminated set
NexusParserSetReader setReader(token, ncharTotal, eliminated, *this, NexusParserSetReader::charset);
setReader.run();
Q_ASSERT(eliminated.count() <= ncharTotal);
nchar = ncharTotal - eliminated.count();
if (nchar != ncharTotal && (characterList.count() > 0)) {
throw NexusParserException("The ELIMINATE command must appear before character (or character state) labels are specified", token.getFilePosition(), token.getFileLine(), token.getFileColumn());
}
if (!charPos.isEmpty()) {
throw NexusParserException("Only one ELIMINATE command is allowed, and it must appear before the MATRIX command", token.getFilePosition(), token.getFileLine(), token.getFileColumn());
}
// Creates a charPos Map containing all characters (thoses that have been eliminated have a value of INT_MAX)
buildCharPosArray(true);
}
// Called when TAXLABELS command needs to be parsed from within the CHARACTERS block. Deals with everything after the
// token TAXLABELS up to and including the semicolon that terminates the TAXLABELS command.
void NexusParserCharactersBlock::handleTaxlabels(NexusParserToken &token)
{
nexusParser->logMesssage(
QString("%1 BLOCK: found command \"TAXLABELS\" on line %2...")
.arg(blockID)
.arg(token.getFileLine())
);
if (!newtaxa){
errorMessage = "NEWTAXA must have been specified in DIMENSIONS command to use the TAXLABELS command in a ";
errorMessage += blockID;
errorMessage += " block";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
for (;;)
{
token.getNextToken();
// Token should either be ';' or the name of a taxon
if (token.equals(";")){
break;
} else {
// Check to make sure user is not trying to read in more
// taxon labels than there are taxa
if (taxaBlock->getNumTaxonLabels() > ntaxTotal){
throw NexusParserException("Number of taxon labels exceeds NTAX specified in DIMENSIONS command",
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
// Add to NexusParserTaxaBlock Class
taxaBlock->taxonAdd(token.getToken());
}
}
// NB: Some may object to setting newtaxa to false here, because then the fact that new taxa were
// specified in this CHARACTERS 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.) - better to
// get a copy of the file data from the QString NexusParserToken::nexusData and output it somewhere.
newtaxa = false;
}
// Called when CHARSTATELABELS command needs to be parsed from within the CHARACTERS block. Deals with everything
// after the token CHARSTATELABELS up to and including the semicolon that terminates the CHARSTATELABELS command.
// Resulting `charLabels' list will store labels only for characters that have not been eliminated, and likewise for
// `charStates'. Specifically, `charStates[0]' refers to the vector of character state labels for the first
// non-eliminated character.
void NexusParserCharactersBlock::handleCharstatelabels(NexusParserToken &token)
{
nexusParser->logMesssage(
QString("%1 BLOCK: found command \"CHARSTATELABELS\" on line %2...")
.arg(blockID)
.arg(token.getFileLine())
);
int currChar = 0;
bool semicolonFoundInInnerLoop = false;
bool tokenAlreadyRead = false;
characterList.clear();
// If there has been no ELIMINATE command build a new charPos map - will be a simple map where key === value.
if (charPos.isEmpty()){
buildCharPosArray();
}
for (;;) // loop #1
{
// End of CHARSTATELABELS found therefore break loop.
if (semicolonFoundInInnerLoop){
break;
}
// Get next token if needed
if (tokenAlreadyRead) {
tokenAlreadyRead = false;
} else {
token.getNextToken();
}
// End of CHARSTATELABELS found therefore break loop.
if (token.equals(";")) {
break;
}
// Token should be the character number (i.e. 1... ncharTotal); create a new association
int n = token.getToken().toInt();
if (n < 1 || n > ncharTotal || n <= currChar) {
errorMessage = "Invalid character number (";
errorMessage += token.getToken();
errorMessage += ") found in CHARSTATELABELS command (either out of range or not interpretable as an integer)";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
// If n is not the next character after currChar, need to add some dummy labels to characterList to fill the gap
while (n - currChar > 1) {
currChar++;
QString characterLabel = QString("Character %1").arg(n);
if (!isEliminated(currChar - 1)) {
characterAdd(characterLabel, false);
nexusParser->logMesssage(
QString("%1 BLOCK: created character label [C%2] -> %3")
.arg(blockID)
.arg(n)
.arg(characterLabel)
);
} else {
characterAdd(characterLabel, true);
nexusParser->logMesssage(
QString("%1 BLOCK: created character label [C%2] [ELIMINATED] -> %3")
.arg(blockID)
.arg(n)
.arg(characterLabel)
);
}
}
currChar++;
Q_ASSERT(n == currChar);
// Token should be the character label
token.getNextToken();
QString characterLabel = token.getToken();
if (!isEliminated(currChar - 1)) {
characterAdd(characterLabel, false);
nexusParser->logMesssage(
QString("%1 BLOCK: extracted character label [C%2] -> %3")
.arg(blockID)
.arg(n)
.arg(characterLabel)
);
} else {
characterAdd(characterLabel, true);
nexusParser->logMesssage(
QString("%1 BLOCK: extracted character label [C%2] [ELIMINATED] -> %3")
.arg(blockID)
.arg(n)
.arg(characterLabel)
);
}
// Token should be a slash character if state labels were provided for this character; otherwise,
// token should be one of the following:
// 1) the comma separating information for different characters, in which case we read in the
// next token (which should be the next character number)
// 2) the semicolon indicating the end of the command
token.getNextToken();
if (!token.equals("/")){
if (!token.equals(",") && !token.equals(";")){
errorMessage = "Expecting a comma or semicolon here, but found (";
errorMessage += token.getToken();
errorMessage += ") instead";
throw NexusParserException(errorMessage,
token.getFilePosition(),
token.getFileLine(),
token.getFileColumn());
}
// If ',' get next character number
if (token.equals(",")){
token.getNextToken();