/** * @method Execute [void:public] * @param token [NexusToken&] the token object used to grab Nexus tokens * @param notifyStartStop [bool] if true, ExecuteStarting and ExecuteStopping will be called (true by default) * * Reads the Nexus data file from the input stream provided by token. This function * is responsible for reading through the name of a each block. Once * it has read a block name, it searches blockList for a block object * to handle reading the remainder of the block's contents. The block * object is responsible for reading the end or endblock command as well * as the trailing semicolon. This function also handles reading * comments that are outside of blocks, as well as the initial #NEXUS * keyword. The notifyStartStop argument is provided in case you do not * wish the ExecuteStart and ExecuteStop functions to be called. These functions * are primarily used for creating and destroying a dialog box to show progress, * and nested Execute calls can thus cause problems (e.g., a dialog box is * destroyed when the inner Execute calls ExecuteStop and the outer Execute * still expects the dialog box to be available). Specifying notifyStartStop * false for all the nested Execute calls thus allows the outermost Execute call * to control creation and destruction of the dialog box. */ void Nexus::Execute( NexusToken& token, bool notifyStartStop /* = true */ ) { char id_str[256]; bool disabledBlock = false; nxsstring errormsg; try { token.GetNextToken(); } catch( XNexus x ) { NexusError( token.errormsg, 0, 0, 0 ); return; } if( !token.Equals("#NEXUS") ) { errormsg = "Expecting #NEXUS to be the first token in the file, but found "; errormsg += token.GetToken(); errormsg += " instead"; NexusError( errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn() ); return; } if( notifyStartStop ) ExecuteStarting(); for(;;) { token.SetLabileFlagBit( NexusToken::saveCommandComments ); token.GetNextToken(); if( token.AtEOF() ) break; if( token.Equals("BEGIN") ) { disabledBlock = false; token.GetNextToken(); NexusBlock* curr; for( curr = blockList; curr != NULL; curr = curr->next ) { if( !token.Equals( curr->GetID() ) ) continue; if( !curr->IsEnabled() ) { disabledBlock = true; SkippingDisabledBlock( token.GetToken() ); continue; } strcpy( id_str, curr->GetID().c_str() ); EnteringBlock( id_str /*curr->GetID()*/ ); curr->Reset(); try { curr->Read( token ); } catch( XNexus x ) { NexusError( curr->errormsg, x.pos, x.line, x.col ); curr->Reset(); return; } ExitingBlock( id_str /*curr->GetID()*/ ); break; } if( curr == NULL ) { token.BlanksToUnderscores(); nxsstring currBlock = token.GetToken(); if( !disabledBlock ) SkippingBlock( currBlock ); for(;;) { token.GetNextToken(); if( token.Equals("END") || token.Equals("ENDBLOCK") ) { token.GetNextToken(); if( !token.Equals(";") ) { errormsg = "Expecting ';' after END or ENDBLOCK command, but found "; errormsg += token.GetToken(); errormsg += " instead"; NexusError( errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn() ); return; } break; } if( token.AtEOF() ) { errormsg = "Encountered end of file before END or ENDBLOCK in block "; errormsg += currBlock; NexusError( errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn() ); return; } } } // if token not found amongst known block IDs } // if token equals BEGIN else if( token.Equals("&SHOWALL") ) { NexusBlock* curr; for( curr = blockList; curr != NULL; curr = curr->next ) { DebugReportBlock(*curr); } } else if( token.Equals("&LEAVE") ) { break; } } // for(;;) if( notifyStartStop ) ExecuteStopping(); }
void BullShell::ParseCodLikeStartValCommand(CodLikeStartOpts &opts, NexusToken& token) const { opts.aaFreqs.clear(); DblVector blankRow(21, 1.0/20.0); blankRow[20] = 0.0; //zero out the freq of the stop codon. opts.genetic_code = GenCode(MITO); token.GetNextToken(); opts.gtrParams.assign(N_MUT_PARAMS, .25); while (token.GetToken()!=";") { if (token.Abbreviation("CURREntbranchlengths") || token.Abbreviation("CURRBranchlengths")) { token.GetNextToken(); opts.currbrlen = true; if (token.Equals("=")) { token.GetNextToken(); if (token.Abbreviation("No")) opts.currbrlen = false; else if (!token.Abbreviation("Yes")) throw XBull("Expecting YES or NO after CurrentBranchLengths = option to CodLikeStartVal command"); token.GetNextToken(); } } else if (token.Abbreviation("GEneticCode")) { SkipEqualsIfNext(token); if (token.Abbreviation("Mitochondrial")) opts.genetic_code = GenCode(MITO); else if (token.Abbreviation("Nuclear")) opts.genetic_code = GenCode(NUCLEAR); else throw XBull("Expecting either Mito or Nuclear code"); } else if (token.Abbreviation("BASEfreq")) { SkipEqualsIfNext(token); if (!token.Equals("(")) throw XBull("Expecting ( after basefreq option to CodLikeStartVal command"); for (unsigned i = 0; i < 3; i++) { token.GetNextToken(); opts.gtrParams[i] = atof(token.GetToken().c_str()); if (opts.gtrParams[i] <= 0.0) throw XBull("basefreqs must be > 0.0 CodLikeStartVal command"); } if (opts.gtrParams[1] + opts.gtrParams[2] + opts.gtrParams[0] >= 1.0) throw XBull("Sum of A, C, and G must be <1.0 CodLikeStartVal command"); token.GetNextToken(); if (!token.Equals(")")) throw XBull("Expecting ) after basefreq option to CodLikeStartVal command"); } else if (token.Abbreviation("RMATrix")) { SkipEqualsIfNext(token); if (!token.Equals("(")) throw XBull("Expecting ( after rmatrix option to CodLikeStartVal command"); for (int i = 0; i < 6; i++) { token.GetNextToken(); opts.gtrParams[3 + i] = atof(token.GetToken().c_str()); if (opts.gtrParams[3 + i] <= 0.0) throw XBull("rmatrix must be >0.0 CodLikeStartVal command"); } token.GetNextToken(); if (!token.Equals(")")) throw XBull("Expecting ) after rmatrix option to CodLikeStartVal command"); } # ifdef ALLOWMULTIHITS else if (token.Abbreviation("DOublehit")) { //not a great name, but allows multiplier abbreviation to work SkipEqualsIfNext(token); opts.gtrParams[MULTI_HIT_PARAM_INDEX] = atof(token.GetToken().c_str()); if (opts.gtrParams[MULTI_HIT_PARAM_INDEX] < 0.0) throw XBull("DOublehit (MultipleHitProb) must be >= 0.0 CodLikeStartVal command"); } # endif else if (token.Abbreviation("AAFreq")) { SkipEqualsIfNext(token); if (!token.Equals("(")) throw XBull("Expecting ( after aafreq option to CodLikeStartVal command"); token.GetNextToken(); long tmp; if (!NxsString::to_long(token.GetTokenAsCStr(), &tmp)) throw XBull("Expecting number of codons after aafreq=( option to CodLikeStartVal command"); if (tmp < 1) throw XBull("number of codons must be >0 in CodLikeStartVal command"); unsigned naa = (unsigned) tmp; opts.aaFreqs.assign(naa, blankRow); for (unsigned ii = 0; ii < naa; ii++) { token.GetNextToken(); if (!token.Equals("(")) throw XBull("Expecting ( for next site in aafreq option to CodLikeStartVal command", token); DblVector & row = opts.aaFreqs[ii]; for (int j=0; j < 20; j++) { token.SetLabileFlagBit(NxsToken::hyphenNotPunctuation); token.GetNextToken(); row[j] = atof(token.GetToken().c_str()); if (row[j]<0.0) throw XBull("each amino acid freq must be >0.0 CodLikeStartVal command", token); } token.GetNextToken(); if (!token.Equals(")")) throw XBull("Expecting ) for next site in aafreq option to CodLikeStartVal command", token); } token.GetNextToken(); if (!token.Equals(")")) throw XBull("Expecting ) after aafreq option to CodLikeStartVal command"); } else if (token.Abbreviation("MUlt")) { SkipEqualsIfNext(token); if (!token.Equals("(")) throw XBull("Expecting ( after mult option to CodLikeStartVal command"); if (opts.aaFreqs.size() < 1) throw XBull("number of codons must be specified (in the AAFreq subcommand) before the mult option is used in CodLikeStartVal command"); opts.multipliers.assign(opts.aaFreqs.size(), 1.0); for (unsigned ii = 0; ii < opts.aaFreqs.size(); ii++) { token.GetNextToken(); opts.multipliers[ii] = atof(token.GetToken().c_str()); if (opts.multipliers[ii] < 0.0) throw XBull("multiplier must be > 0.0 CodLikeStartVal command"); } token.GetNextToken(); if (!token.Equals(")")) throw XBull("Expecting ) after mult option to CodLikeStartVal command"); } else if (token.Abbreviation("TReescale")) { SkipEqualsIfNext(token); opts.treeScale = atof(token.GetToken().c_str()); if (opts.treeScale < 0.0) throw XBull("The Tree scaling must be >0.0 CodLikeStartVal command"); } token.GetNextToken(); } }
/** * @method HandleNextPass [int:protected] * @param token [NexusToken&] the token we are using for reading the data file * @param offset [int&] the offset (see below) * * Called from within HandleMatrix, this function is used to deal with interleaved * matrices. It is called once for each pass through the taxa. * * <P>The local variable jmax records the number of columns read in the current * interleaved page and is used to determine the offset used for j in subsequent * pages. */ int DistancesBlock::HandleNextPass( NexusToken& token, int& offset ) { int i, j, k, jmax = 0, done = 0; int i_first = 0; if( triangle == lower ) i_first = offset; int i_last = ntax; for( i = i_first; i < i_last; i++ ) { // Deal with taxon label if provided // if( labels && ( !newtaxa || offset>0 ) ) { do { token.SetLabileFlagBit( NexusToken::newlineIsToken ); token.GetNextToken(); } while( token.AtEOL() ); try { k = taxa.FindTaxon( token.GetToken() ); if( taxonPos[i]==-1 ) taxonPos[i] = k; else if( taxonPos[i] != k ) { errormsg = "Taxon labeled "; errormsg += token.GetToken(); errormsg += " is out of order compared to previous interleave pages"; throw XNexus( errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn() ); } } catch (std::out_of_range) { errormsg = "Could not find "; errormsg += token.GetToken(); errormsg += " among taxa previously defined"; throw XNexus( errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn() ); } } else if( labels && newtaxa ) { do { token.SetLabileFlagBit( NexusToken::newlineIsToken ); token.GetNextToken(); } while( token.AtEOL() ); taxa.AddTaxonLabel( token.GetToken() ); taxonPos[i] = i; } // Now deal with the row of distance values // int true_j = 0; for( j = 0; j < ntax; j++ ) { if( i == ntax-1 && j == ntax-1 ) { done = 1; } if( i == ntax-1 && true_j == ntax-1 ) { done = 1; break; } if( i == ntax-1 && !diagonal && triangle==upper ) { done = 1; break; } if( !diagonal && triangle==lower && j == ntax-offset-1) { done = 1; break; } token.SetLabileFlagBit( NexusToken::newlineIsToken ); token.GetNextToken(); if( token.AtEOL() ) { if( j > jmax ) { jmax = j; if( !diagonal && triangle == upper && i >= offset ) jmax++; if( interleave && triangle == upper ) i_last = jmax + offset; } break; } true_j = j+offset; if( triangle==upper && i > offset ) true_j += ( i - offset ); if( !diagonal && triangle==upper && i >= offset ) true_j++; if( true_j == ntax ) { errormsg = "Too many distances specified in row just read in"; throw XNexus( errormsg, token.GetFilePosition(), token.GetFileLine(), token.GetFileColumn() ); } std::string t = token.GetToken(); if( token.GetTokenLength() == 1 && t[0] == missing ) SetMissing( i, true_j ); else SetDistance( i, true_j, atof( t.c_str() ) ); } } offset += jmax; if( done ) return 1; else return 0; }