|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "ncl.h" |
|
|
#include <functional> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NxsCharactersBlock::NxsCharactersBlock( |
|
|
NxsTaxaBlock *tb, |
|
|
NxsAssumptionsBlock *ab) |
|
|
: NxsBlock() |
|
|
{ |
|
|
assert(tb != NULL); |
|
|
assert(ab != NULL); |
|
|
|
|
|
taxa = tb; |
|
|
assumptionsBlock = ab; |
|
|
id = "CHARACTERS"; |
|
|
|
|
|
|
|
|
|
|
|
matrix = NULL; |
|
|
charPos = NULL; |
|
|
taxonPos = NULL; |
|
|
activeTaxon = NULL; |
|
|
activeChar = NULL; |
|
|
symbols = NULL; |
|
|
|
|
|
Reset(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NxsCharactersBlock::~NxsCharactersBlock() |
|
|
{ |
|
|
Reset(); |
|
|
|
|
|
if (symbols != NULL) |
|
|
delete [] symbols; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::ApplyDelset( |
|
|
NxsUnsignedSet &delset) |
|
|
{ |
|
|
assert(activeTaxon != NULL); |
|
|
assert(taxonPos != NULL); |
|
|
|
|
|
unsigned num_deleted = 0; |
|
|
unsigned k; |
|
|
|
|
|
NxsUnsignedSet::const_iterator i; |
|
|
for (i = delset.begin(); i != delset.end(); i++) |
|
|
{ |
|
|
k = taxonPos[*i]; |
|
|
if (k == UINT_MAX) |
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (activeTaxon[k] == true) |
|
|
num_deleted++; |
|
|
activeTaxon[k] = false; |
|
|
} |
|
|
|
|
|
return num_deleted; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::ApplyExset( |
|
|
NxsUnsignedSet &exset) |
|
|
{ |
|
|
assert(activeChar != NULL); |
|
|
assert(charPos != NULL); |
|
|
|
|
|
int num_excluded = 0; |
|
|
unsigned k; |
|
|
|
|
|
NxsUnsignedSet::const_iterator i; |
|
|
for (i = exset.begin(); i != exset.end(); i++) |
|
|
{ |
|
|
k = charPos[*i]; |
|
|
if (k == UINT_MAX) |
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (activeChar[k] == true) |
|
|
num_excluded++; |
|
|
activeChar[k] = false; |
|
|
} |
|
|
|
|
|
return num_excluded; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::ApplyIncludeset( |
|
|
NxsUnsignedSet &inset) |
|
|
{ |
|
|
assert(activeChar != NULL); |
|
|
assert(charPos != NULL); |
|
|
|
|
|
unsigned num_included = 0; |
|
|
unsigned k; |
|
|
|
|
|
NxsUnsignedSet::const_iterator i; |
|
|
for (i = inset.begin(); i != inset.end(); i++) |
|
|
{ |
|
|
k = charPos[*i]; |
|
|
if (k == UINT_MAX) |
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (activeChar[k] == false) |
|
|
num_included++; |
|
|
activeChar[k] = true; |
|
|
} |
|
|
|
|
|
return num_included; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::ApplyRestoreset( |
|
|
NxsUnsignedSet &restoreset) |
|
|
{ |
|
|
assert(activeTaxon != NULL); |
|
|
assert(taxonPos != NULL); |
|
|
|
|
|
unsigned num_restored = 0; |
|
|
unsigned k; |
|
|
|
|
|
NxsUnsignedSet::const_iterator i; |
|
|
for (i = restoreset.begin(); i != restoreset.end(); i++) |
|
|
{ |
|
|
k = taxonPos[*i]; |
|
|
if (k == UINT_MAX) |
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (activeTaxon[k] == false) |
|
|
num_restored++; |
|
|
activeTaxon[k] = true; |
|
|
} |
|
|
|
|
|
return num_restored; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::BuildCharPosArray( |
|
|
bool check_eliminated) |
|
|
{ |
|
|
assert(charPos == NULL); |
|
|
|
|
|
charPos = new unsigned[ncharTotal]; |
|
|
|
|
|
unsigned k = 0; |
|
|
for (unsigned j = 0; j < ncharTotal; j++) |
|
|
{ |
|
|
if (check_eliminated && IsEliminated(j)) |
|
|
charPos[j] = UINT_MAX; |
|
|
else |
|
|
charPos[j] = k++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::CharLabelToNumber( |
|
|
NxsString s) |
|
|
{ |
|
|
NxsStringVector::const_iterator iter = find(charLabels.begin(), charLabels.end(), s); |
|
|
|
|
|
unsigned k = 1; |
|
|
if (iter != charLabels.end()) |
|
|
k += (iter - charLabels.begin()); |
|
|
else |
|
|
k = 0; |
|
|
|
|
|
return k; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::Consume( |
|
|
NxsCharactersBlock &other) |
|
|
{ |
|
|
ntax = other.ntax; |
|
|
ntaxTotal = other.ntaxTotal; |
|
|
nchar = other.nchar; |
|
|
ncharTotal = other.ncharTotal; |
|
|
|
|
|
newtaxa = other.newtaxa; |
|
|
newchar = other.newchar; |
|
|
|
|
|
formerly_datablock = true; |
|
|
respectingCase = other.respectingCase; |
|
|
transposing = other.transposing; |
|
|
interleaving = other.interleaving; |
|
|
tokens = other.tokens; |
|
|
labels = other.labels; |
|
|
|
|
|
missing = other.missing; |
|
|
gap = other.gap; |
|
|
matchchar = other.matchchar; |
|
|
|
|
|
datatype = other.datatype; |
|
|
|
|
|
if (symbols != NULL) |
|
|
delete [] symbols; |
|
|
symbols = other.symbols; |
|
|
other.symbols = NULL; |
|
|
|
|
|
if (charPos != NULL) |
|
|
delete [] charPos; |
|
|
charPos = other.charPos; |
|
|
other.charPos = NULL; |
|
|
|
|
|
if (taxonPos != NULL) |
|
|
delete [] taxonPos; |
|
|
taxonPos = other.taxonPos; |
|
|
other.taxonPos = NULL; |
|
|
|
|
|
if (activeChar != NULL) |
|
|
delete [] activeChar; |
|
|
activeChar = other.activeChar; |
|
|
other.activeChar = NULL; |
|
|
|
|
|
if (activeTaxon != NULL) |
|
|
delete [] activeTaxon; |
|
|
activeTaxon = other.activeTaxon; |
|
|
other.activeTaxon = NULL; |
|
|
|
|
|
if (matrix != NULL) |
|
|
delete matrix; |
|
|
matrix = other.matrix; |
|
|
other.matrix = NULL; |
|
|
|
|
|
equates.clear(); |
|
|
int size = other.equates.size(); |
|
|
if (size > 0) |
|
|
{ |
|
|
NxsStringMap::const_iterator i; |
|
|
for (i = other.equates.begin(); i != other.equates.end(); ++i) |
|
|
equates[(*i).first] = (*i).second; |
|
|
other.equates.clear(); |
|
|
} |
|
|
|
|
|
eliminated.clear(); |
|
|
size = eliminated.size(); |
|
|
if (size > 0) |
|
|
{ |
|
|
NxsUnsignedSet::const_iterator i; |
|
|
for (i = other.eliminated.begin(); i != other.eliminated.end(); i++) |
|
|
eliminated.insert(*i); |
|
|
other.eliminated.clear(); |
|
|
} |
|
|
|
|
|
charLabels.clear(); |
|
|
size = charLabels.size(); |
|
|
if (size > 0) |
|
|
{ |
|
|
NxsStringVector::const_iterator i; |
|
|
for (i = other.charLabels.begin(); i != other.charLabels.end(); i++) |
|
|
charLabels.push_back((*i)); |
|
|
other.charLabels.clear(); |
|
|
} |
|
|
|
|
|
charStates.clear(); |
|
|
size = charStates.size(); |
|
|
if (size > 0) |
|
|
{ |
|
|
NxsStringVectorMap::const_iterator i; |
|
|
for (i = other.charStates.begin(); i != other.charStates.end(); i++) |
|
|
charStates[ (*i).first ] = (*i).second; |
|
|
other.charStates.clear(); |
|
|
} |
|
|
|
|
|
isEmpty = false; |
|
|
isUserSupplied = other.isUserSupplied; |
|
|
other.Reset(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::DebugShowMatrix( |
|
|
ostream &out, |
|
|
bool use_matchchar, |
|
|
const char *marginText) |
|
|
{ |
|
|
assert(charPos != NULL); |
|
|
assert(taxonPos != NULL); |
|
|
|
|
|
unsigned i, k; |
|
|
unsigned width = taxa->GetMaxTaxonLabelLength(); |
|
|
unsigned first_taxon = UINT_MAX; |
|
|
|
|
|
for (i = 0; i < ntaxTotal; i++) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (taxonPos[i] == UINT_MAX) |
|
|
continue; |
|
|
else |
|
|
{ |
|
|
if (first_taxon == UINT_MAX) |
|
|
first_taxon = i; |
|
|
|
|
|
if (marginText != NULL) |
|
|
out << marginText; |
|
|
|
|
|
NxsString currTaxonLabel = taxa->GetTaxonLabel(taxonPos[i]); |
|
|
out << currTaxonLabel; |
|
|
|
|
|
|
|
|
|
|
|
unsigned currTaxonLabelLen = currTaxonLabel.size(); |
|
|
unsigned diff = width - currTaxonLabelLen; |
|
|
for (k = 0; k < diff+5; k++) |
|
|
out << ' '; |
|
|
} |
|
|
|
|
|
for (unsigned currChar = 0; currChar < ncharTotal; currChar++) |
|
|
{ |
|
|
unsigned j = charPos[currChar]; |
|
|
if (j == UINT_MAX) |
|
|
continue; |
|
|
ShowStateLabels(out, i, j, (use_matchchar ? first_taxon : UINT_MAX)); |
|
|
} |
|
|
|
|
|
out << endl; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::GetMaxObsNumStates() |
|
|
{ |
|
|
unsigned max = 2; |
|
|
for (unsigned j = 0; j < nchar; j++) |
|
|
{ |
|
|
unsigned ns = GetObsNumStates(j); |
|
|
if (ns <= max) |
|
|
continue; |
|
|
max = ns; |
|
|
} |
|
|
|
|
|
return max; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::GetNumActiveChar() |
|
|
{ |
|
|
unsigned num_active_char = 0; |
|
|
for (unsigned i = 0; i < nchar; i++) |
|
|
{ |
|
|
if (activeChar[i] == false) |
|
|
continue; |
|
|
num_active_char++; |
|
|
} |
|
|
|
|
|
return num_active_char; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::GetNumActiveTaxa() |
|
|
{ |
|
|
unsigned num_active_taxa = 0; |
|
|
for (unsigned i = 0; i < ntax; i++) |
|
|
{ |
|
|
if (activeTaxon[i] == false) |
|
|
continue; |
|
|
num_active_taxa++; |
|
|
} |
|
|
|
|
|
return num_active_taxa; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::GetOrigCharIndex( |
|
|
unsigned j) |
|
|
{ |
|
|
unsigned k = j; |
|
|
while (k < ncharTotal && charPos[k] < j) |
|
|
k++; |
|
|
|
|
|
assert(k < ncharTotal); |
|
|
return k; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::GetOrigTaxonIndex( |
|
|
unsigned i) |
|
|
{ |
|
|
assert(taxonPos != NULL); |
|
|
|
|
|
unsigned k = i; |
|
|
while (k < ntaxTotal && taxonPos[k] < i) |
|
|
k++; |
|
|
|
|
|
assert(k < ntaxTotal); |
|
|
return k; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NxsString NxsCharactersBlock::GetStateLabel( |
|
|
unsigned i, |
|
|
unsigned j) |
|
|
{ |
|
|
NxsString s = " "; |
|
|
NxsStringVectorMap::const_iterator cib = charStates.find(i); |
|
|
if (cib != charStates.end() && static_cast<unsigned>(j) < (*cib).second.size()) |
|
|
{ |
|
|
s = (*cib).second[j]; |
|
|
} |
|
|
|
|
|
return s; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool NxsCharactersBlock::IsEliminated( |
|
|
unsigned origCharIndex) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (eliminated.empty()) |
|
|
return false; |
|
|
|
|
|
NxsUnsignedSet::const_iterator found = eliminated.find(origCharIndex); |
|
|
if (found == eliminated.end()) |
|
|
return false; |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool NxsCharactersBlock::IsInSymbols( |
|
|
char ch) |
|
|
{ |
|
|
assert(symbols != NULL); |
|
|
unsigned symbolsLength = strlen(symbols); |
|
|
bool found = false; |
|
|
for (unsigned i = 0; i < symbolsLength; i++) |
|
|
{ |
|
|
char char_in_symbols = (respectingCase ? symbols[i] : (char)toupper(symbols[i])); |
|
|
char char_in_question = (respectingCase ? ch : (char)toupper(ch)); |
|
|
if (char_in_symbols != char_in_question) |
|
|
continue; |
|
|
found = true; |
|
|
break; |
|
|
} |
|
|
|
|
|
return found; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleCharlabels( |
|
|
NxsToken &token) |
|
|
{ |
|
|
unsigned num_labels_read = 0; |
|
|
charLabels.clear(); |
|
|
|
|
|
if (charPos == NULL) |
|
|
BuildCharPosArray(); |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (token.Equals(";")) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
else |
|
|
{ |
|
|
num_labels_read++; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (num_labels_read > ncharTotal) |
|
|
{ |
|
|
errormsg = "Number of character labels exceeds NCHAR specified in DIMENSIONS command"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (!IsEliminated(num_labels_read - 1)) |
|
|
charLabels.push_back(token.GetToken()); |
|
|
} |
|
|
} |
|
|
|
|
|
newchar = false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleCharstatelabels( |
|
|
NxsToken &token) |
|
|
{ |
|
|
unsigned currChar = 0; |
|
|
bool semicolonFoundInInnerLoop = false; |
|
|
bool tokenAlreadyRead = false; |
|
|
bool save = true; |
|
|
|
|
|
charStates.clear(); |
|
|
charLabels.clear(); |
|
|
|
|
|
if (charPos == NULL) |
|
|
BuildCharPosArray(); |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
save = true; |
|
|
|
|
|
if (semicolonFoundInInnerLoop) |
|
|
break; |
|
|
|
|
|
if (tokenAlreadyRead) |
|
|
tokenAlreadyRead = false; |
|
|
else |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.Equals(";")) |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
int n = atoi(token.GetToken().c_str()); |
|
|
|
|
|
if (n < 1 || n > (int)ncharTotal || n <= (int)currChar) |
|
|
{ |
|
|
errormsg = "Invalid character number ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") found in CHARSTATELABELS command (either out of range or not interpretable as an integer)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (n - currChar > 1) |
|
|
{ |
|
|
currChar++; |
|
|
if (!IsEliminated(currChar - 1)) |
|
|
charLabels.push_back(" "); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
currChar++; |
|
|
assert(n == (int)currChar); |
|
|
if (IsEliminated(currChar-1)) |
|
|
save = false; |
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
|
|
|
|
|
|
if (save) |
|
|
charLabels.push_back(token.GetToken()); |
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!token.Equals("/")) |
|
|
{ |
|
|
if (!token.Equals(",") && !token.Equals(";")) |
|
|
{ |
|
|
errormsg = "Expecting a comma or semicolon here, but found ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
if (token.Equals(",")) |
|
|
token.GetNextToken(); |
|
|
tokenAlreadyRead = true; |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.Equals(";")) |
|
|
{ |
|
|
semicolonFoundInInnerLoop = true; |
|
|
break; |
|
|
} |
|
|
|
|
|
if (token.Equals(",")) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
|
|
|
if (save) |
|
|
{ |
|
|
|
|
|
|
|
|
NxsString cslabel = token.GetToken(); |
|
|
unsigned k = GetCharPos(n - 1); |
|
|
charStates[k].push_back(cslabel); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
newchar = false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleDimensions( |
|
|
NxsToken &token, |
|
|
NxsString newtaxaLabel, |
|
|
NxsString ntaxLabel, |
|
|
NxsString ncharLabel) |
|
|
{ |
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.Equals(newtaxaLabel)) |
|
|
{ |
|
|
newtaxa = true; |
|
|
} |
|
|
else if (token.Equals(ntaxLabel)) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after "; |
|
|
errormsg += ntaxLabel; |
|
|
errormsg += " in DIMENSIONS command, but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
ntax = atoi(token.GetToken().c_str()); |
|
|
if (ntax <= 0) |
|
|
{ |
|
|
errormsg = ntaxLabel; |
|
|
errormsg += " must be a number greater than 0"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (newtaxa) |
|
|
ntaxTotal = ntax; |
|
|
else |
|
|
{ |
|
|
ntaxTotal = taxa->GetNumTaxonLabels(); |
|
|
if (ntaxTotal < ntax) |
|
|
{ |
|
|
errormsg = ntaxLabel; |
|
|
errormsg += " in "; |
|
|
errormsg += id; |
|
|
errormsg += " block must be less than or equal to NTAX in TAXA block"; |
|
|
errormsg += "\nNote: one circumstance that can cause this error is "; |
|
|
errormsg += "\nforgetting to specify "; |
|
|
errormsg += ntaxLabel; |
|
|
errormsg += " in DIMENSIONS command when "; |
|
|
errormsg += "\na TAXA block has not been provided"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
} |
|
|
} |
|
|
else if (token.Equals(ncharLabel)) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after "; |
|
|
errormsg += ncharLabel; |
|
|
errormsg += " in DIMENSIONS command, but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
nchar = atoi(token.GetToken().c_str()); |
|
|
if (nchar <= 0) |
|
|
{ |
|
|
errormsg = ncharLabel; |
|
|
errormsg += " must be a number greater than 0"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
ncharTotal = nchar; |
|
|
} |
|
|
else if (token.Equals(";")) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if (newtaxa) |
|
|
taxa->Reset(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleEliminate( |
|
|
NxsToken &token) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
NxsSetReader(token, ncharTotal, eliminated, *this, NxsSetReader::charset).Run(); |
|
|
|
|
|
nchar = ncharTotal - eliminated.size(); |
|
|
|
|
|
if (nchar != ncharTotal && (charLabels.size() > 0 || charStates.size() > 0)) |
|
|
{ |
|
|
errormsg = "The ELIMINATE command must appear before character\n(or character state) labels are specified"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (charPos != NULL) |
|
|
{ |
|
|
errormsg = "Only one ELIMINATE command is allowed, and it must appear before the MATRIX command"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
BuildCharPosArray(true); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleEndblock( |
|
|
NxsToken &token, |
|
|
NxsString charToken) |
|
|
{ |
|
|
|
|
|
|
|
|
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()); |
|
|
} |
|
|
|
|
|
if (charLabels.empty() && !charStates.empty()) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (unsigned k = 0; k < ncharTotal; k++) |
|
|
{ |
|
|
NxsString nm = charToken; |
|
|
nm += " "; |
|
|
nm += (k+1); |
|
|
charLabels.push_back(nm.c_str()); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleFormat( |
|
|
NxsToken &token) |
|
|
{ |
|
|
bool standardDataTypeAssumed = false; |
|
|
bool ignoreCaseAssumed = false; |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.Equals("DATATYPE")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword DATATYPE but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
else |
|
|
{ |
|
|
errormsg = token.GetToken(); |
|
|
errormsg += " is not a valid DATATYPE within a "; |
|
|
errormsg += id; |
|
|
errormsg += " block"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ResetSymbols(); |
|
|
|
|
|
if (datatype == continuous) |
|
|
tokens = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("RESPECTCASE")) |
|
|
{ |
|
|
if (ignoreCaseAssumed) |
|
|
{ |
|
|
errormsg = "RESPECTCASE must be specified before MISSING, GAP, SYMBOLS, and MATCHCHAR in FORMAT command"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
standardDataTypeAssumed = true; |
|
|
respectingCase = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("MISSING")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword MISSING but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.GetTokenLength() != 1) |
|
|
{ |
|
|
errormsg = "MISSING symbol should be a single character, but "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
else if (token.IsPunctuationToken() && !token.IsPlusMinusToken()) |
|
|
{ |
|
|
errormsg = "MISSING symbol specified cannot be a punctuation token ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
else if (token.IsWhitespaceToken()) |
|
|
{ |
|
|
errormsg = "MISSING symbol specified cannot be a whitespace character ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
missing = token.GetToken()[0]; |
|
|
|
|
|
ignoreCaseAssumed = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("GAP")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword GAP but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.GetTokenLength() != 1) |
|
|
{ |
|
|
errormsg = "GAP symbol should be a single character, but "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
else if (token.IsPunctuationToken() && !token.IsPlusMinusToken()) |
|
|
{ |
|
|
errormsg = "GAP symbol specified cannot be a punctuation token ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
else if (token.IsWhitespaceToken()) |
|
|
{ |
|
|
errormsg = "GAP symbol specified cannot be a whitespace character ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
gap = token.GetToken()[0]; |
|
|
|
|
|
ignoreCaseAssumed = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("SYMBOLS")) |
|
|
{ |
|
|
if (datatype == NxsCharactersBlock::continuous) |
|
|
{ |
|
|
errormsg = "SYMBOLS subcommand not allowed for DATATYPE=CONTINUOUS"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
int numDefStates; |
|
|
int maxNewStates; |
|
|
switch(datatype) |
|
|
{ |
|
|
case NxsCharactersBlock::dna: |
|
|
case NxsCharactersBlock::rna: |
|
|
case NxsCharactersBlock::nucleotide: |
|
|
numDefStates = 4; |
|
|
maxNewStates = NCL_MAX_STATES-4; |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::protein: |
|
|
numDefStates = 21; |
|
|
maxNewStates = NCL_MAX_STATES-21; |
|
|
break; |
|
|
|
|
|
default: |
|
|
numDefStates = 0; |
|
|
symbols[0] = '\0'; |
|
|
maxNewStates = NCL_MAX_STATES; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword SYMBOLS but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.SetLabileFlagBit(NxsToken::doubleQuotedToken); |
|
|
token.GetNextToken(); |
|
|
|
|
|
token.StripWhitespace(); |
|
|
unsigned numNewSymbols = token.GetTokenLength(); |
|
|
|
|
|
if ((int)numNewSymbols > maxNewStates) |
|
|
{ |
|
|
errormsg = "SYMBOLS defines "; |
|
|
errormsg += numNewSymbols; |
|
|
errormsg += " new states but only "; |
|
|
errormsg += maxNewStates; |
|
|
errormsg += " new states allowed for this DATATYPE"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
NxsString t = token.GetToken(); |
|
|
unsigned tlen = t.size(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NxsString told = t; |
|
|
t=""; |
|
|
for (unsigned i = 0; i < tlen; i++) |
|
|
{ |
|
|
if (!IsInSymbols(told[i]) && told[i] > 32) |
|
|
{ |
|
|
t += told[i]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
strcpy(symbols+numDefStates, t.c_str()); |
|
|
|
|
|
ignoreCaseAssumed = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("EQUATE")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword EQUATE but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("\"")) |
|
|
{ |
|
|
errormsg = "Expecting '\"' after keyword EQUATE but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
if (token.Equals("\"")) |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (token.GetTokenLength() != 1) |
|
|
{ |
|
|
errormsg = "Expecting single-character EQUATE symbol but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NxsString t = token.GetToken(); |
|
|
char ch = t[0]; |
|
|
bool badEquateSymbol = false; |
|
|
|
|
|
|
|
|
|
|
|
if (ch == '^') |
|
|
badEquateSymbol = true; |
|
|
|
|
|
|
|
|
|
|
|
if (token.IsPunctuationToken() && !token.IsPlusMinusToken()) |
|
|
badEquateSymbol = true; |
|
|
|
|
|
|
|
|
|
|
|
if (ch == missing || ch == matchchar || ch == gap) |
|
|
badEquateSymbol = true; |
|
|
|
|
|
|
|
|
|
|
|
if (IsInSymbols(ch)) |
|
|
badEquateSymbol = true; |
|
|
|
|
|
if (badEquateSymbol) |
|
|
{ |
|
|
errormsg = "EQUATE symbol specified ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") is not valid; must not be same as missing, \nmatchchar, gap, state symbols, or any of the following: ()[]{}/\\,;:=*'\"`<>^"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
NxsString k = token.GetToken(); |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' in EQUATE definition but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.SetLabileFlagBit(NxsToken::parentheticalToken); |
|
|
token.SetLabileFlagBit(NxsToken::curlyBracketedToken); |
|
|
token.GetNextToken(); |
|
|
NxsString v = token.GetToken(); |
|
|
|
|
|
|
|
|
|
|
|
equates[k] = v; |
|
|
} |
|
|
|
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("MATCHCHAR")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword MATCHCHAR but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.GetTokenLength() != 1) |
|
|
{ |
|
|
errormsg = "MATCHCHAR symbol should be a single character, but "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
else if (token.IsPunctuationToken() && !token.IsPlusMinusToken()) |
|
|
{ |
|
|
errormsg = "MATCHCHAR symbol specified cannot be a punctuation token ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified) "; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
else if (token.IsWhitespaceToken()) |
|
|
{ |
|
|
errormsg = "MATCHCHAR symbol specified cannot be a whitespace character ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " was specified)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
matchchar = token.GetToken()[0]; |
|
|
|
|
|
ignoreCaseAssumed = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("LABELS")) |
|
|
{ |
|
|
labels = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("NOLABELS")) |
|
|
{ |
|
|
labels = false; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("TRANSPOSE")) |
|
|
{ |
|
|
transposing = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("INTERLEAVE")) |
|
|
{ |
|
|
interleaving = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("ITEMS")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg += "Expecting '=' after keyword ITEMS but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("STATES")) |
|
|
{ |
|
|
errormsg = "Sorry, only ITEMS=STATES supported at this time"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("STATESFORMAT")) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("=")) |
|
|
{ |
|
|
errormsg = "Expecting '=' after keyword STATESFORMAT but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals("STATESPRESENT")) |
|
|
{ |
|
|
errormsg = "Sorry, only STATESFORMAT=STATESPRESENT supported at this time"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("TOKENS")) |
|
|
{ |
|
|
tokens = true; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals("NOTOKENS")) |
|
|
{ |
|
|
tokens = false; |
|
|
standardDataTypeAssumed = true; |
|
|
} |
|
|
|
|
|
else if (token.Equals(";")) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
else |
|
|
{ |
|
|
errormsg = "Expecting ';' but found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " at the end of FORMAT command"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!tokens && datatype == continuous) |
|
|
{ |
|
|
errormsg = "TOKENS must be defined for DATATYPE=CONTINUOUS"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (tokens && (datatype == dna || datatype == rna || datatype == nucleotide)) |
|
|
{ |
|
|
errormsg = "TOKENS not allowed for the DATATYPEs DNA, RNA, or NUCLEOTIDE"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool NxsCharactersBlock::HandleNextState( |
|
|
NxsToken &token, |
|
|
unsigned i, |
|
|
unsigned j) |
|
|
{ |
|
|
|
|
|
|
|
|
if (!tokens) |
|
|
{ |
|
|
token.SetLabileFlagBit(NxsToken::parentheticalToken); |
|
|
token.SetLabileFlagBit(NxsToken::curlyBracketedToken); |
|
|
token.SetLabileFlagBit(NxsToken::singleCharacterToken); |
|
|
} |
|
|
|
|
|
if (interleaving) |
|
|
token.SetLabileFlagBit(NxsToken::newlineIsToken); |
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (interleaving && token.AtEOL()) |
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
if (token.AtEOF()) |
|
|
{ |
|
|
errormsg = "Unexpected end of file encountered"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assert(token.GetTokenLength() > 0); |
|
|
|
|
|
|
|
|
|
|
|
if (j < 0) |
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
NxsString skey = NxsString(token.GetToken(true)); |
|
|
|
|
|
NxsStringMap::iterator p = equates.find(skey); |
|
|
if (p != equates.end()) |
|
|
{ |
|
|
NxsString sval = (*p).second; |
|
|
token.ReplaceToken(sval.c_str()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!tokens && token.GetTokenLength() == 1) |
|
|
{ |
|
|
char ch = token.GetToken()[0]; |
|
|
|
|
|
|
|
|
|
|
|
if (ch == missing) |
|
|
{ |
|
|
matrix->SetMissing(i, j); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else if (matchchar != '\0' && ch == matchchar) |
|
|
{ |
|
|
matrix->CopyStatesFromFirstTaxon(i, j); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else if (gap != '\0' && ch == gap) |
|
|
{ |
|
|
matrix->SetGap(i, j); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else |
|
|
{ |
|
|
int p = PositionInSymbols(ch); |
|
|
if (p < 0) |
|
|
{ |
|
|
errormsg = "State specified ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") for taxon "; |
|
|
errormsg += (i+1); |
|
|
errormsg += ", character "; |
|
|
errormsg += (j+1); |
|
|
errormsg += ", not found in list of valid symbols"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
matrix->AddState(i, j, p); |
|
|
matrix->SetPolymorphic(i, j, 0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else if (!tokens && token.GetTokenLength() > 1) |
|
|
{ |
|
|
|
|
|
|
|
|
NxsString t = token.GetToken(); |
|
|
unsigned tlen = t.size(); |
|
|
unsigned poly = (t[0] == '('); |
|
|
assert(poly || t[0] == '{'); |
|
|
assert((poly && t[tlen-1] == ')') || (!poly && t[tlen-1] == '}')); |
|
|
|
|
|
unsigned first_nonblank = 1; |
|
|
while (t[first_nonblank] == ' ' || t[first_nonblank] == '\t') |
|
|
first_nonblank++; |
|
|
|
|
|
unsigned last_nonblank = tlen - 2; |
|
|
while (t[last_nonblank] == ' ' || t[last_nonblank] == '\t') |
|
|
last_nonblank--; |
|
|
|
|
|
if (t[first_nonblank] == '~' || t[last_nonblank] == '~') |
|
|
{ |
|
|
errormsg = token.GetToken(); |
|
|
errormsg += " does not represent a valid range of states"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
unsigned k = 1; |
|
|
char *pFirst = symbols; |
|
|
bool tildeFound = false; |
|
|
for (;;) |
|
|
{ |
|
|
if (t[k] == ')' || t[k] == '}') |
|
|
break; |
|
|
|
|
|
if (t[k] == ' ' || t[k] == '\t') |
|
|
{ |
|
|
k++; |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (t[k] == '~') |
|
|
{ |
|
|
tildeFound = true; |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
if (tildeFound) |
|
|
{ |
|
|
|
|
|
|
|
|
pFirst++; |
|
|
while (*pFirst != '\0' && *pFirst != t[k]) |
|
|
{ |
|
|
int p = PositionInSymbols(*pFirst); |
|
|
if (p < 0) |
|
|
{ |
|
|
errormsg = "State specified ("; |
|
|
errormsg += *pFirst; |
|
|
errormsg += ") for taxon "; |
|
|
errormsg += (i+1); |
|
|
errormsg += ", character "; |
|
|
errormsg += (j+1); |
|
|
errormsg += ", not found in list of valid symbols"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
matrix->AddState(i, j, p); |
|
|
pFirst++; |
|
|
} |
|
|
|
|
|
tildeFound = false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
int p = PositionInSymbols(t[k]); |
|
|
if (p < 0) |
|
|
{ |
|
|
errormsg = "State specified ("; |
|
|
errormsg += t[k]; |
|
|
errormsg += ") for taxon "; |
|
|
errormsg += (i+1); |
|
|
errormsg += ", character "; |
|
|
errormsg += (j+1); |
|
|
errormsg += ", not found in list of valid symbols"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
pFirst = (symbols + p); |
|
|
matrix->AddState(i, j, p); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
k++; |
|
|
} |
|
|
|
|
|
matrix->SetPolymorphic(i, j, poly); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
int polymorphism = token.Equals("("); |
|
|
int uncertainty = token.Equals("LEFT_SQUIGGLY"); |
|
|
|
|
|
if (!uncertainty && !polymorphism) |
|
|
{ |
|
|
int k = HandleTokenState(token, j); |
|
|
matrix->AddState(i, j, k); |
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
bool tildeFound = false; |
|
|
unsigned first = UINT_MAX; |
|
|
unsigned last; |
|
|
for (;;) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
token.SetLabileFlagBit(NxsToken::tildeIsPunctuation); |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (polymorphism && token.Equals(")")) |
|
|
{ |
|
|
if (tildeFound) |
|
|
{ |
|
|
errormsg = "Range of states still being specified when ')' encountered"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
else if (uncertainty && token.Equals("RIGHT_SQUIGGLY")) |
|
|
{ |
|
|
if (tildeFound) |
|
|
{ |
|
|
errormsg = "Range of states still being specified when '}' encountered"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
else if (token.Equals("~")) |
|
|
{ |
|
|
if (first == UINT_MAX) |
|
|
{ |
|
|
errormsg = "Tilde character ('~') cannot precede token indicating beginning of range"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
tildeFound = true; |
|
|
} |
|
|
|
|
|
else if (tildeFound) |
|
|
{ |
|
|
|
|
|
|
|
|
last = HandleTokenState(token, j); |
|
|
|
|
|
if (last <= first) |
|
|
{ |
|
|
errormsg = "Last state in specified range ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") must be greater than the first"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
for (unsigned k = first+1; k <= last; k++) |
|
|
matrix->AddState(i, j, k); |
|
|
|
|
|
tildeFound = false; |
|
|
first = UINT_MAX; |
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
first = HandleTokenState(token, j); |
|
|
matrix->AddState(i, j, first); |
|
|
} |
|
|
} |
|
|
|
|
|
if (polymorphism) |
|
|
matrix->SetPolymorphic(i, j, 1); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::HandleTokenState( |
|
|
NxsToken &token, |
|
|
unsigned j) |
|
|
{ |
|
|
|
|
|
|
|
|
if (charStates.find(j) == charStates.end()) |
|
|
{ |
|
|
errormsg = "No states were defined for character "; |
|
|
errormsg += (1 + GetOrigCharIndex(j)); |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NxsStringVectorMap::const_iterator bagIter = charStates.find(j); |
|
|
NxsStringVector::const_iterator ci_begin = (*bagIter).second.begin(); |
|
|
NxsStringVector::const_iterator ci_end = (*bagIter).second.end(); |
|
|
NxsString t = token.GetToken(respectingCase); |
|
|
NxsStringVector::const_iterator cit; |
|
|
if (respectingCase) |
|
|
cit = find(ci_begin, ci_end, t); |
|
|
else |
|
|
cit = find_if (ci_begin, ci_end, |
|
|
[=] (const NxsString& s) { return s.EqualsCaseInsensitive(t); } ); |
|
|
|
|
|
if (cit == ci_end) |
|
|
{ |
|
|
errormsg = "Character state "; |
|
|
errormsg += t; |
|
|
errormsg += " not defined for character "; |
|
|
errormsg += (1 + GetOrigCharIndex(j)); |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned k = (cit - ci_begin); |
|
|
return k; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleStdMatrix( |
|
|
NxsToken &token) |
|
|
{ |
|
|
assert(charPos != NULL); |
|
|
assert(taxonPos != NULL); |
|
|
|
|
|
unsigned i = 0, j, currChar = 0; |
|
|
unsigned firstChar = 0; |
|
|
unsigned lastChar = ncharTotal; |
|
|
unsigned nextFirst = 0; |
|
|
int page = 0; |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < ntax; i++) |
|
|
{ |
|
|
if (labels) |
|
|
{ |
|
|
|
|
|
|
|
|
token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation + NxsToken::preserveUnderscores); |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (page == 0 && newtaxa) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (taxa->IsAlreadyDefined(token.GetToken())) |
|
|
{ |
|
|
errormsg = "Data for this taxon ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") has already been saved"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
taxa->AddTaxonLabel(token.GetToken()); |
|
|
|
|
|
|
|
|
|
|
|
taxonPos[i] = i; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned positionInTaxaBlock; |
|
|
try |
|
|
{ |
|
|
positionInTaxaBlock = taxa->FindTaxon(token.GetToken()); |
|
|
} |
|
|
catch(NxsTaxaBlock::NxsX_NoSuchTaxon) |
|
|
{ |
|
|
if (token.Equals(";") && i == 0) |
|
|
{ |
|
|
errormsg = "Unexpected ; (after only "; |
|
|
errormsg += currChar; |
|
|
errormsg += " characters were read)"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
errormsg = "Could not find taxon named "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " among stored taxon labels"; |
|
|
} |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (page == 0) |
|
|
{ |
|
|
|
|
|
|
|
|
if (taxonPos[positionInTaxaBlock] != UINT_MAX) |
|
|
{ |
|
|
errormsg = "Data for this taxon ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") has already been saved"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (positionInTaxaBlock != i) |
|
|
{ |
|
|
errormsg = "Relative order of taxa must be the same in both the TAXA and CHARACTERS blocks"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
taxonPos[i] = positionInTaxaBlock; |
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
if (taxonPos[positionInTaxaBlock] != i) |
|
|
{ |
|
|
errormsg = "Ordering of taxa must be identical to that in first interleave page"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
if (page == 0) |
|
|
taxonPos[i] = i; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (currChar = firstChar; currChar < lastChar; currChar++) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
j = charPos[currChar]; |
|
|
|
|
|
|
|
|
|
|
|
bool ok = HandleNextState(token, i, j); |
|
|
if (interleaving && !ok) |
|
|
{ |
|
|
if (lastChar < ncharTotal && j != lastChar) |
|
|
{ |
|
|
errormsg = "Each line within an interleave page must comprise the same number of characters"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
nextFirst = currChar; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastChar = currChar; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
firstChar = nextFirst; |
|
|
lastChar = ncharTotal; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (currChar == ncharTotal) |
|
|
break; |
|
|
|
|
|
page++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleTransposedMatrix( |
|
|
NxsToken &token) |
|
|
{ |
|
|
assert(charPos != NULL); |
|
|
assert(taxonPos != NULL); |
|
|
|
|
|
unsigned i = 0, j, currChar; |
|
|
unsigned firstTaxon = 0; |
|
|
unsigned lastTaxon = ntaxTotal; |
|
|
unsigned nextFirst = 0; |
|
|
int page = 0; |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (currChar = 0; currChar < ncharTotal; currChar++) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
j = charPos[currChar]; |
|
|
|
|
|
if (labels) |
|
|
{ |
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (page == 0 && newchar) |
|
|
{ |
|
|
|
|
|
|
|
|
NxsString s = token.GetToken(); |
|
|
NxsStringVector::const_iterator iter = find(charLabels.begin(), charLabels.end(), s); |
|
|
|
|
|
bool charLabelFound = (iter != charLabels.end()); |
|
|
if (charLabelFound) |
|
|
{ |
|
|
errormsg = "Data for this character ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") has already been saved"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
charLabels.push_back(token.GetToken()); |
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
NxsString s = token.GetToken(); |
|
|
NxsStringVector::const_iterator iter = find(charLabels.begin(), charLabels.end(), s); |
|
|
|
|
|
if (iter == charLabels.end()) |
|
|
{ |
|
|
errormsg = "Could not find character named "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " among stored character labels"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
unsigned positionInCharLabelsList = (iter - charLabels.begin()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (positionInCharLabelsList != currChar) |
|
|
{ |
|
|
if (page == 0) |
|
|
{ |
|
|
errormsg = "Data for this character ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") has already been saved"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
else |
|
|
{ |
|
|
errormsg = "Ordering of characters must be identical to that in first interleave page"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = firstTaxon; i < lastTaxon; i++) |
|
|
{ |
|
|
if (page == 0) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
taxonPos[i] = i; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ok = HandleNextState(token, i, j); |
|
|
if (interleaving && !ok) |
|
|
{ |
|
|
if (lastTaxon < ntaxTotal && i != lastTaxon) |
|
|
{ |
|
|
errormsg = "Each line within an interleave page must comprise the same number of taxa"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
nextFirst = i; |
|
|
|
|
|
|
|
|
|
|
|
lastTaxon = i; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
firstTaxon = nextFirst; |
|
|
lastTaxon = ntaxTotal; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (i == ntaxTotal) |
|
|
break; |
|
|
|
|
|
page++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleMatrix( |
|
|
NxsToken &token) |
|
|
{ |
|
|
unsigned i, j; |
|
|
|
|
|
if (ntax == 0) |
|
|
{ |
|
|
errormsg = "Must precede "; |
|
|
errormsg += id; |
|
|
errormsg += " block with a TAXA block or specify NEWTAXA and NTAX in the DIMENSIONS command"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (ntaxTotal == 0) |
|
|
ntaxTotal = taxa->GetNumTaxonLabels(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert(nchar >= 0); |
|
|
|
|
|
if (datatype == NxsCharactersBlock::continuous) |
|
|
{ |
|
|
errormsg = "Sorry, continuous character matrices have not yet been implemented"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
if (matrix != NULL) |
|
|
delete matrix; |
|
|
matrix = new NxsDiscreteMatrix(ntax, nchar); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
activeTaxon = new bool[ntax]; |
|
|
for (i = 0; i < ntax; i++) |
|
|
activeTaxon[i] = true; |
|
|
|
|
|
activeChar = new bool[nchar]; |
|
|
for (j = 0; j < nchar; j++) |
|
|
activeChar[j] = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (charPos == NULL) |
|
|
BuildCharPosArray(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (taxonPos != NULL) |
|
|
delete [] taxonPos; |
|
|
taxonPos = new unsigned[ntaxTotal]; |
|
|
|
|
|
for (i = 0; i < ntaxTotal; i++) |
|
|
taxonPos[i] = UINT_MAX; |
|
|
|
|
|
if (transposing) |
|
|
HandleTransposedMatrix(token); |
|
|
else |
|
|
HandleStdMatrix(token); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assumptionsBlock->SetCallback(this); |
|
|
|
|
|
|
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (!token.Equals(";")) |
|
|
{ |
|
|
errormsg = "Expecting ';' at the end of the MATRIX command; found "; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += " instead"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleStatelabels( |
|
|
NxsToken &token) |
|
|
{ |
|
|
bool semicolonFoundInInnerLoop = false; |
|
|
|
|
|
charStates.clear(); |
|
|
|
|
|
if (charPos == NULL) |
|
|
BuildCharPosArray(); |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
if (semicolonFoundInInnerLoop) |
|
|
break; |
|
|
|
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.Equals(";")) |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
unsigned n = atoi(token.GetToken().c_str()); |
|
|
|
|
|
if (n < 1 || n > ncharTotal) |
|
|
{ |
|
|
errormsg = "Invalid character number ("; |
|
|
errormsg += token.GetToken(); |
|
|
errormsg += ") found in STATELABELS command (either out of range or not interpretable as an integer)"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
|
|
|
if (token.Equals(";")) |
|
|
{ |
|
|
semicolonFoundInInnerLoop = true; |
|
|
break; |
|
|
} |
|
|
|
|
|
if (token.Equals(",")) |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
if (!IsEliminated(n - 1)) |
|
|
{ |
|
|
unsigned k = GetCharPos(n - 1); |
|
|
charStates[k].push_back(token.GetToken()); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::HandleTaxlabels( |
|
|
NxsToken &token) |
|
|
{ |
|
|
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()); |
|
|
} |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation + NxsToken::preserveUnderscores); |
|
|
token.GetNextToken(); |
|
|
|
|
|
|
|
|
|
|
|
if (token.Equals(";")) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
if (taxa->GetNumTaxonLabels() > ntaxTotal) |
|
|
{ |
|
|
errormsg = "Number of taxon labels exceeds NTAX specified in DIMENSIONS command"; |
|
|
throw NxsException(errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn()); |
|
|
} |
|
|
|
|
|
taxa->AddTaxonLabel(token.GetToken()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newtaxa = false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned NxsCharactersBlock::PositionInSymbols( |
|
|
char ch) |
|
|
{ |
|
|
assert(symbols != NULL); |
|
|
unsigned symbolsLength = strlen(symbols); |
|
|
bool found = false; |
|
|
unsigned i; |
|
|
for (i = 0; i < symbolsLength; i++) |
|
|
{ |
|
|
char char_in_symbols = (respectingCase ? symbols[i] : (char)toupper(symbols[i])); |
|
|
char char_in_question = (respectingCase ? ch : (char)toupper(ch)); |
|
|
if (char_in_symbols != char_in_question) |
|
|
continue; |
|
|
found = true; |
|
|
break; |
|
|
} |
|
|
return (found ? i : UINT_MAX); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::Read( |
|
|
NxsToken &token) |
|
|
{ |
|
|
isEmpty = false; |
|
|
isUserSupplied = true; |
|
|
|
|
|
|
|
|
|
|
|
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()); |
|
|
} |
|
|
|
|
|
ntax = taxa->GetNumTaxonLabels(); |
|
|
|
|
|
for (;;) |
|
|
{ |
|
|
token.GetNextToken(); |
|
|
|
|
|
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 if (token.Equals("END")) |
|
|
{ |
|
|
HandleEndblock(token, "Character"); |
|
|
break; |
|
|
} |
|
|
else if (token.Equals("ENDBLOCK")) |
|
|
{ |
|
|
HandleEndblock(token, "Character"); |
|
|
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()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::Report( |
|
|
ostream &out) |
|
|
{ |
|
|
out << endl; |
|
|
out << id << " block contains "; |
|
|
if (ntax == 0) |
|
|
out << "no taxa"; |
|
|
else if (ntax == 1) |
|
|
out << "one taxon"; |
|
|
else |
|
|
out << ntax << " taxa"; |
|
|
out << " and "; |
|
|
if (nchar == 0) |
|
|
out << "no characters"; |
|
|
else if (nchar == 1) |
|
|
out << "one character"; |
|
|
else |
|
|
out << nchar << " characters"; |
|
|
out << endl; |
|
|
|
|
|
if (formerly_datablock) |
|
|
{ |
|
|
out << "Originally read in as a DATA block" << endl; |
|
|
out << endl; |
|
|
} |
|
|
|
|
|
switch(datatype) |
|
|
{ |
|
|
case NxsCharactersBlock::dna: |
|
|
out << " Data type is \"DNA\"" << endl; |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::rna: |
|
|
out << " Data type is \"RNA\"" << endl; |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::nucleotide: |
|
|
out << " Data type is \"nucleotide\"" << endl; |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::protein: |
|
|
out << " Data type is \"protein\"" << endl; |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::continuous: |
|
|
out << " Data type is \"continuous\"" << endl; |
|
|
break; |
|
|
|
|
|
default: |
|
|
out << " Data type is \"standard\"" << endl; |
|
|
} |
|
|
|
|
|
if (respectingCase) |
|
|
out << " Respecting case" << endl; |
|
|
else |
|
|
out << " Ignoring case" << endl; |
|
|
|
|
|
if (tokens) |
|
|
out << " Multicharacter tokens allowed in data matrix" << endl; |
|
|
else |
|
|
out << " Data matrix entries are expected to be single symbols" << endl; |
|
|
|
|
|
if (labels && transposing) |
|
|
out << " Character labels are expected on left side of matrix" << endl; |
|
|
else if (labels && !transposing) |
|
|
out << " Taxon labels are expected on left side of matrix" << endl; |
|
|
else |
|
|
out << " No labels are expected on left side of matrix" << endl; |
|
|
|
|
|
if (charLabels.size() > 0) |
|
|
{ |
|
|
out << " Character and character state labels:" << endl; |
|
|
for (unsigned k = 0; k < nchar; k++) |
|
|
{ |
|
|
if (charLabels[k].length() == 0) |
|
|
out << '\t' << (1 + GetOrigCharIndex(k)) << '\t' << "(no label provided for this character)" << endl; |
|
|
else |
|
|
out << '\t' << (1 + GetOrigCharIndex(k)) << '\t' << charLabels[k] << endl; |
|
|
|
|
|
|
|
|
|
|
|
NxsStringVectorMap::const_iterator cib = charStates.find(k); |
|
|
if (cib != charStates.end()) |
|
|
{ |
|
|
int ns = (*cib).second.size(); |
|
|
for (int m = 0; m < ns; m++) |
|
|
{ |
|
|
out << "\t\t" << (*cib).second[m] << endl; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (transposing && interleaving) |
|
|
out << " Matrix transposed and interleaved" << endl; |
|
|
else if (transposing && !interleaving) |
|
|
out << " Matrix transposed but not interleaved" << endl; |
|
|
else if (!transposing && interleaving) |
|
|
out << " Matrix interleaved but not transposed" << endl; |
|
|
else |
|
|
out << " Matrix neither transposed nor interleaved" << endl; |
|
|
|
|
|
out << " Missing data symbol is '" << missing << '\'' << endl; |
|
|
|
|
|
if (matchchar != '\0') |
|
|
out << " Match character is '" << matchchar << '\'' << endl; |
|
|
else |
|
|
out << " No match character specified" << endl; |
|
|
|
|
|
if (gap != '\0') |
|
|
out << " Gap character specified is '" << gap << '\'' << endl; |
|
|
else |
|
|
out << " No gap character specified" << endl; |
|
|
|
|
|
out << " Valid symbols are: " << symbols << endl; |
|
|
|
|
|
int numEquateMacros = equates.size(); |
|
|
if (numEquateMacros > 0) |
|
|
{ |
|
|
out << " Equate macros in effect:" << endl; |
|
|
typedef NxsStringMap::const_iterator CI; |
|
|
for (CI i = equates.begin(); i != equates.end(); ++i) |
|
|
{ |
|
|
out << " " << (*i).first << " = " << (*i).second << endl; |
|
|
} |
|
|
} |
|
|
else |
|
|
out << " No equate macros have been defined" << endl; |
|
|
|
|
|
if (ncharTotal == nchar) |
|
|
out << " No characters were eliminated" << endl; |
|
|
else |
|
|
{ |
|
|
out << " The following characters were eliminated:" << endl; |
|
|
NxsUnsignedSet::const_iterator k; |
|
|
for (k = eliminated.begin(); k != eliminated.end(); k++) |
|
|
{ |
|
|
out << " " << ((*k)+1) << endl; |
|
|
} |
|
|
} |
|
|
|
|
|
out << " The following characters have been excluded:" << endl; |
|
|
unsigned k; |
|
|
unsigned nx = 0; |
|
|
for (k = 0; k < nchar; k++) |
|
|
{ |
|
|
if (activeChar[k]) |
|
|
continue; |
|
|
out << " " << (k+1) << endl; |
|
|
nx++; |
|
|
} |
|
|
|
|
|
if (nx == 0) |
|
|
out << " (no characters excluded)" << endl; |
|
|
|
|
|
out << " The following taxa have been deleted:" << endl; |
|
|
nx = 0; |
|
|
for (k = 0; k < ntax; k++) |
|
|
{ |
|
|
if (activeTaxon[k]) |
|
|
continue; |
|
|
out << " " << (k+1) << endl; |
|
|
nx++; |
|
|
} |
|
|
|
|
|
if (nx == 0) |
|
|
out << " (no taxa deleted)" << endl; |
|
|
|
|
|
out << " Data matrix:" << endl; |
|
|
DebugShowMatrix(out, false, " "); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::Reset() |
|
|
{ |
|
|
|
|
|
|
|
|
errormsg.clear(); |
|
|
isEnabled = true; |
|
|
isEmpty = true; |
|
|
isUserSupplied = false; |
|
|
|
|
|
ntax = 0; |
|
|
ntaxTotal = 0; |
|
|
nchar = 0; |
|
|
ncharTotal = 0; |
|
|
newchar = true; |
|
|
newtaxa = false; |
|
|
interleaving = false; |
|
|
transposing = false; |
|
|
respectingCase = false; |
|
|
formerly_datablock = false; |
|
|
labels = true; |
|
|
tokens = false; |
|
|
datatype = NxsCharactersBlock::standard; |
|
|
missing = '?'; |
|
|
gap = '\0'; |
|
|
matchchar = '\0'; |
|
|
|
|
|
ResetSymbols(); |
|
|
|
|
|
charLabels.clear(); |
|
|
charStates.clear(); |
|
|
equates.clear(); |
|
|
eliminated.clear(); |
|
|
|
|
|
if (matrix != NULL) |
|
|
{ |
|
|
delete matrix; |
|
|
matrix = NULL; |
|
|
} |
|
|
|
|
|
if (charPos != NULL) |
|
|
{ |
|
|
delete [] charPos; |
|
|
charPos = NULL; |
|
|
} |
|
|
|
|
|
if (taxonPos != NULL) |
|
|
{ |
|
|
delete [] taxonPos; |
|
|
taxonPos = NULL; |
|
|
} |
|
|
|
|
|
if (activeTaxon != NULL) |
|
|
{ |
|
|
delete [] activeTaxon; |
|
|
activeTaxon = NULL; |
|
|
} |
|
|
|
|
|
if (activeChar != NULL) |
|
|
{ |
|
|
delete [] activeChar; |
|
|
activeChar = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::ResetSymbols() |
|
|
{ |
|
|
|
|
|
|
|
|
if (symbols == NULL) |
|
|
{ |
|
|
symbols = new char[NCL_MAX_STATES+1]; |
|
|
symbols[0] = '0'; |
|
|
symbols[1] = '1'; |
|
|
symbols[2] = '\0'; |
|
|
} |
|
|
|
|
|
switch(datatype) |
|
|
{ |
|
|
case NxsCharactersBlock::dna: |
|
|
strcpy(symbols, "ACGT"); |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::rna: |
|
|
strcpy(symbols, "ACGU"); |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::nucleotide: |
|
|
strcpy(symbols, "ACGT"); |
|
|
break; |
|
|
|
|
|
case NxsCharactersBlock::protein: |
|
|
strcpy(symbols, "ACDEFGHIKLMNPQRSTVWY*XU"); |
|
|
break; |
|
|
|
|
|
default: |
|
|
strcpy(symbols, "01"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
equates.clear(); |
|
|
if (datatype == NxsCharactersBlock::dna || datatype == NxsCharactersBlock::rna || datatype == NxsCharactersBlock::nucleotide) |
|
|
{ |
|
|
equates[ NxsString("R") ] = NxsString("{AG}"); |
|
|
equates[ NxsString("Y") ] = NxsString("{CT}"); |
|
|
equates[ NxsString("M") ] = NxsString("{AC}"); |
|
|
equates[ NxsString("K") ] = NxsString("{GT}"); |
|
|
equates[ NxsString("S") ] = NxsString("{CG}"); |
|
|
equates[ NxsString("W") ] = NxsString("{AT}"); |
|
|
equates[ NxsString("H") ] = NxsString("{ACT}"); |
|
|
equates[ NxsString("B") ] = NxsString("{CGT}"); |
|
|
equates[ NxsString("V") ] = NxsString("{ACG}"); |
|
|
equates[ NxsString("D") ] = NxsString("{AGT}"); |
|
|
equates[ NxsString("N") ] = NxsString("{ACGT}"); |
|
|
equates[ NxsString("X") ] = NxsString("{ACGT}"); |
|
|
} |
|
|
else if (datatype == NxsCharactersBlock::protein) |
|
|
{ |
|
|
equates[ NxsString("B") ] = NxsString("{DN}"); |
|
|
equates[ NxsString("Z") ] = NxsString("{EQ}"); |
|
|
equates[ NxsString("J") ] = NxsString("{IL}"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::ShowStateLabels( |
|
|
ostream &out, |
|
|
unsigned i, |
|
|
unsigned j, |
|
|
unsigned first_taxon) |
|
|
{ |
|
|
if (tokens) |
|
|
{ |
|
|
unsigned n = matrix->GetNumStates(i, j); |
|
|
if (n == 0 && matrix->IsGap(i, j)) |
|
|
out << gap; |
|
|
else if (n == 0 && matrix->IsMissing(i, j)) |
|
|
out << missing; |
|
|
else if (n == 1) |
|
|
{ |
|
|
int s = matrix->GetState(i, j); |
|
|
bool use_matchchar = false; |
|
|
if (first_taxon != UINT_MAX && i > first_taxon) |
|
|
{ |
|
|
int firsts = matrix->GetState(first_taxon, j); |
|
|
if (firsts == s) |
|
|
use_matchchar = true; |
|
|
} |
|
|
if (use_matchchar) |
|
|
{ |
|
|
|
|
|
|
|
|
out << " ."; |
|
|
} |
|
|
else |
|
|
{ |
|
|
NxsStringVectorMap::const_iterator ci = charStates.find(j); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ci == charStates.end()) |
|
|
out << " " << s << "[<-no label found]"; |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
out << " " << (*ci).second[s]; |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
if (matrix->IsPolymorphic(i, j)) |
|
|
out << " ("; |
|
|
else |
|
|
out << " {"; |
|
|
for (unsigned k = 0; k < n; k++) |
|
|
{ |
|
|
unsigned s = matrix->GetState(i, j, k); |
|
|
NxsStringVectorMap::const_iterator ci = charStates.find(j); |
|
|
if (ci == charStates.end()) |
|
|
out << " " << s << "[<-no label found]"; |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
out << " " << (*ci).second[s]; |
|
|
} |
|
|
} |
|
|
if (matrix->IsPolymorphic(i, j)) |
|
|
out << ')'; |
|
|
else |
|
|
out << '}'; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
if (first_taxon != UINT_MAX && i > first_taxon) |
|
|
{ |
|
|
char s[NCL_MAX_STATES + 3]; |
|
|
WriteStates(matrix->GetDiscreteDatum(i, j), s, NCL_MAX_STATES + 3); |
|
|
|
|
|
char ss[NCL_MAX_STATES + 3]; |
|
|
WriteStates(matrix->GetDiscreteDatum(first_taxon, j), ss, NCL_MAX_STATES + 3); |
|
|
|
|
|
if (strcmp(s, ss) == 0) |
|
|
out << '.'; |
|
|
else |
|
|
ShowStates(out, i, j); |
|
|
} |
|
|
else |
|
|
ShowStates(out, i, j); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void NxsCharactersBlock::WriteStates( |
|
|
NxsDiscreteDatum &d, |
|
|
char *s, |
|
|
unsigned slen) |
|
|
{ |
|
|
assert(s != NULL); |
|
|
assert(slen > 1); |
|
|
|
|
|
if (matrix->IsMissing(d)) |
|
|
{ |
|
|
s[0] = missing; |
|
|
s[1] = '\0'; |
|
|
} |
|
|
else if (matrix->IsGap(d)) |
|
|
{ |
|
|
s[0] = gap; |
|
|
s[1] = '\0'; |
|
|
} |
|
|
else |
|
|
{ |
|
|
assert(symbols != NULL); |
|
|
unsigned numStates = matrix->GetNumStates(d); |
|
|
unsigned symbolListLen = strlen(symbols); |
|
|
unsigned numCharsNeeded = numStates; |
|
|
if (numStates > 1) |
|
|
numCharsNeeded += 2; |
|
|
assert(slen > numCharsNeeded); |
|
|
|
|
|
if (numStates == 1) { |
|
|
unsigned v = matrix->GetState(d); |
|
|
assert(v < symbolListLen); |
|
|
s[0] = (v<symbolListLen) ? symbols[v] : '\0'; |
|
|
s[1] = '\0'; |
|
|
} |
|
|
else { |
|
|
|
|
|
|
|
|
unsigned i = 0; |
|
|
if (matrix->IsPolymorphic(d)) |
|
|
s[i++] = '('; |
|
|
else |
|
|
s[i++] = '{'; |
|
|
for (unsigned k = 0; k < numStates; k++) |
|
|
{ |
|
|
unsigned v = matrix->GetState(d, k); |
|
|
assert(v < symbolListLen); |
|
|
s[i++] = symbols[v]; |
|
|
s[i] = '\0'; |
|
|
} |
|
|
if (matrix->IsPolymorphic(d)) |
|
|
s[i++] = ')'; |
|
|
else |
|
|
s[i++] = '}'; |
|
|
s[i] = '\0'; |
|
|
} |
|
|
} |
|
|
} |
|
|
|