Example #1
0
/**
 * @method HandleLog [void:protected]
 * @param token [NexusToken&] the token used to read from in
 * @throws XBull
 *
 * Called when the LOG command needs to be parsed
 * from within the BullShell block.
 */
void BullShell::HandleLog( NexusToken& token )
{
	bool starting = false;
	bool stopping = false;
	bool appending = false;
	bool replacing = false;
	std::string logfname;
	errormsg.clear();
	// Retrieve all tokens for this command, stopping only in the event
	// of a semicolon or an unrecognized keyword
	//
	for (token.GetNextToken(); !token.Equals(";"); token.GetNextToken()) {
		if ( token.Abbreviation("STOp") )
			stopping = true;
		else if ( token.Abbreviation("STArt") )
			starting = true;
		else if ( token.Abbreviation("Replace") )
			replacing = true;
		else if ( token.Abbreviation("Append") )
			appending = true;
		else if ( token.Abbreviation("File") ) {
			DemandEquals(token, "after file subcommand of Log command");
			token.GetNextToken();
			logfname = token.GetTokenReference();
		}
		else {
			errormsg << "Unexpected keyword (" << token.GetToken() << ") encountered reading LOG command";
			throw XBull( errormsg, token);
		}
   }
   
   // Check for incompatible combinations of keywords
   if ( stopping && ( starting || appending || replacing || !logfname.empty() ) ) {
		errormsg = "Cannot specify STOP with any of the following START, APPEND, REPLACE, FILE";
		throw XBull( errormsg, token);
   }
   
   if ( appending && replacing ) {
		errormsg = "Cannot specify APPEND and REPLACE at the same time";
		throw XBull( errormsg, token);
   }

   if ( stopping ) {
		ioObject.stopLogging();
		kernel.logEachStep=false;
		return;
   }
   
	if (logfname.empty() ) {
		errormsg << "Must provide a file name when opening a log file\ne.g., log file=doofus.txt start replace;";
		throw XBull(errormsg, token);
	}	
	if (!appending && !replacing && FileExists(logfname.c_str())) {
		errormsg << "File " << logfname << " exists.\n APPEND or REPLACE must be specified in the LOG command.";
		throw XBull( errormsg, token);
	}
	ioObject.startLogging(logfname,  appending);	
}
Example #2
0
SimulateOpts BullShell::ParseSimulateCommand(NexusToken& token ) const {	
	token.GetNextToken();
	int nOutputs = 0;
	int nSimChars = 0;
	SimulateOpts simOpts;
	const unsigned nTrees = kernel.getNumTrees();
	for (; token.GetToken() != ";"; token.GetNextToken()) { 
		if (token.Abbreviation("NReps")) {
			DemandEquals(token, "after NReps subcommand of Simulate command");
			simOpts.nReps = DemandPositiveInt(token, "after NReps subcommand of Simulate command");
		}
		if (token.Abbreviation("Seed")) {
			DemandEquals(token, "after Seed subcommand of Simulate command");
			simOpts.seed = DemandPositiveInt(token, "after Seed subcommand of Simulate command");
		}
		else if (token.Abbreviation("COLLapsefile")) {
			DemandEquals(token, "after CollapseFile subcommand of Simulate command");
			token.GetNextToken();
			simOpts.collapsedTreeFilename = token.GetTokenReference();
		}
		else if (token.Abbreviation("NChars")) {
			DemandEquals(token, "after NChars subcommand of Simulate command");
			simOpts.nSimChars = DemandPositiveInt(token, "after NChars subcommand of Simulate command");
		}
		else if (token.Abbreviation("Concat")) {
			DemandEquals(token, "after Concat subcommand of GetTrees command");
			simOpts.concatenations  = DemandPositiveInt(token, "after Concat subcommand of GetTrees command");
		}
		else if (token.Abbreviation("Verbose")) {
			simOpts.verbose = true;
		}
		else if (token.Abbreviation("NOUTput")) {
			DemandEquals(token, "after NOutput subcommand of Simulate command");
			nOutputs = DemandPositiveInt(token, "after NOutput subcommand of Simulate command");
			if (nOutputs < 1) {
				errormsg="nOutputs must be > 0";
				throw XBull( errormsg, token);
			}
		}
		else if (token.Abbreviation("Paupblockfile")) {
			DemandEquals(token, "after Paupblockfile subcommand of Simulate");
			token.GetNextToken();
			ReadNexusVector(&(simOpts.paupBlockFilename), token);
		}
		else if (token.Abbreviation("OVERWrite"))
			simOpts.overwrite = true;
		else if (token.Abbreviation("File")) {
			DemandEquals(token, "after File subcommand of Simulate");
			token.GetNextToken();
			ReadNexusVector(&(simOpts.outputFilenames), token);
		}
		else if (token.Abbreviation("TAg")) {
			DemandEquals(token, "after Tag subcommand of Simulate command");
			token.GetNextToken();
			simOpts.tagname = token.GetTokenReference();
		}
		else if (token.Abbreviation("AUTOmatic"))
			simOpts.automatic = true;
		else if (token.Abbreviation("OUTputtypes")) {
			DemandEquals(token, "after OutputTypes subcommand of Simulate");
			token.GetNextToken();
			EncodingType et;
			if (token.Equals("(")) {
				token.GetNextToken();
				while (!token.Equals(")")) {
					et = InterpretOutputType(token);
					simOpts.outTypes.push_back(et);
					token.GetNextToken();
				}
			}
			else {
				et = InterpretOutputType(token);
				simOpts.outTypes.push_back(et);
			}
		}
		else {
			Tree * temptree;
			long tn;
			if (NxsString::to_long(token.GetTokenAsCStr(), &tn)) {
				if ((unsigned) tn > nTrees || tn < 1) {
					errormsg = "Tree ";
					errormsg += token.GetToken();
					errormsg += " unknown.	simulation aborted.";
					throw XBull( errormsg, token);
				}
				temptree = kernel.getTree(tn - 1);
			}
			else {
				try {
					temptree = kernel.FindTreeFromName(token.GetToken());
				}
				catch (NoSuchTree) {
					errormsg << "Tree " << token.GetToken() << " unknown.";
					throw XBull( errormsg, token);
				}
			}
			simOpts.treeNames.push_back(token.GetToken()); //keeps track of what the user called the tree
			simOpts.treeAlias.push_back(temptree);
		}	
	}


	if (simOpts.outTypes.size() == 0){
		simOpts.outTypes.push_back(EncodingType(DNANoGap));
	}
	if ((nOutputs > 0) && simOpts.outTypes.size() != (unsigned) nOutputs) {
		errormsg = "The number of OutputTypes in a Simulate command must match the number of specified in the NOutput option";
		throw XBull( errormsg, token);
	}
	if (simOpts.automatic) {
		if (simOpts.tagname.empty()) {
			errormsg = "The Tag option of the Simulate command must be used if the Automatic naming mode is requested.";
			throw XBull( errormsg, token);
		}
		if (!simOpts.outputFilenames.empty()) {
			errormsg = "The FILE option of the Simulate command cannot be used if the Automatic naming mode is requested (use the Tag instead of file).";
			throw XBull( errormsg, token);
		}
		simOpts.outputFilenames.clear();
		std::vector<EncodingType>::const_iterator ot = simOpts.outTypes.begin();
		for (; ot != simOpts.outTypes.end(); ot++) {
			string ofname = EncodingTypeToString(*ot);
			ofname.append(simOpts.tagname);
			simOpts.outputFilenames.push_back(ofname);
		}
	}
	else if (simOpts.outTypes.size() != simOpts.outputFilenames.size()) {
		errormsg = "The number of OutputTypes in a Simulate command must match the number of Files specified";
		throw XBull( errormsg, token);
	}
	if ((!simOpts.paupBlockFilename.empty()) && simOpts.paupBlockFilename.size() != simOpts.outTypes.size()) {
		errormsg = "The number of PaupBlockFile in a Simulate command must match the number of OutputTypes specified";
		throw XBull( errormsg, token);
	}
	

	//TEMPORARY lots of the interaction with SSettings assumes that you are using SSRFCodonSubModel
	if (!kernel.hasModel()) {
		errormsg="You can't simulate a codon model without first declaring the models using codLikeStartVal";
		throw XBull( errormsg, token);
	}
	
	if ( simOpts.concatenations == 0) {
		if (nSimChars == 0) {
			errormsg="Either the number of characters or the number of concatenations must be specified";
			throw XBull( errormsg, token);
		}
		const unsigned naa = kernel.getModelConstRef().getNumAASites();
		const unsigned ndna = 3*naa;
		if (nSimChars % ndna) {
			errormsg="The number of characters must be a multiple of the number of amino acids";
			throw XBull( errormsg, token);
		}
		simOpts.concatenations = ndna/nSimChars;
	}
	return simOpts;
}
Example #3
0
GetTreesOpts BullShell::ParseGetTrees(NexusToken& token ) const {
	errormsg.clear();
	if (kernel.getNumTaxa() == 0) {
		errormsg="You can't get trees without an active set of taxa";
		throw XBull( errormsg, token);
	}
	GetTreesOpts gto;
	bool readingMostRecent = false;
	gto.fromTree = -1;
	gto.toTree = -1;
	gto.mode = 7;
	std::string filePref;

	token.GetNextToken();
	while (token.GetToken()!=";") {
		if (token.Abbreviation("STOREBrlens")) {
			token.GetNextToken();
			gto.storeBrLensFromFile=true;
			if (token.GetToken() == "=") {
				token.GetNextToken();
				if (token.Abbreviation("No"))
					gto.storeBrLensFromFile=false;
				else if (!token.Abbreviation("Yes")) {
					errormsg="Expecting YES or NO after CurrentBranchLengths = option to LSCORE command";
					throw XBull(errormsg, token);
				}
			}
		}
		else if (token.Abbreviation("FRom")) {
			DemandEquals(token, "after From subcommand of GetTrees command");
			gto.fromTree = DemandPositiveInt(token, "after From subcommand of GetTrees command");
			if (gto.fromTree < 0) {
				errormsg="Expecting positive integer after FROM option of GETTREES";
				throw XBull(errormsg, token);
			}	
		}
		else if (token.Abbreviation("To")) {
			DemandEquals(token, "after To subcommand of GetTrees command");
			gto.toTree = DemandPositiveInt(token, "after To subcommand of GetTrees command");
			if (gto.toTree < 0) {
				errormsg="Expecting positive integer after TO option of GETTREES";
				throw XBull(errormsg, token);
			}	
		}
		else if (token.Abbreviation("REplace"))
			gto.mode=3;
		else if (token.Abbreviation("Mode")) {
			DemandEquals(token, "after Mode subcommand of GetTrees command");
			gto.mode = DemandPositiveInt(token, "after Mode subcommand of GetTrees command");
			if (gto.mode!=3 && gto.mode!=7) {
				errormsg="Right now bull only get trees with mode 3 or mode 7";
				throw XBull(errormsg, token);
			}	
		}
		else if (token.Abbreviation("FIle")) {
			DemandEquals(token, "after File subcommand of GetTrees command");
			token.GetNextToken();
			gto.filename = token.GetTokenReference();
		}
		else if (token.Abbreviation("PREfix")) {
			token.GetNextToken();
			if (token.GetToken() == "=")	
				token.GetNextToken();
			filePref = token.GetToken();
		}
		else if (token.Abbreviation("MOStrecent"))
			readingMostRecent=true;
		else {
			errormsg = "Unrecognized subcommand (";
			errormsg << token.GetTokenReference() << ") in GetTrees command.";
			throw XBull(errormsg, token);
		}
		token.GetNextToken();
	}
	
	if (readingMostRecent) {
		int highNum = GetHighestFileNum(filePref);
		if (highNum < 0) {
			errormsg << "Couldn't open file " << filePref << "0";
			throw XBull(errormsg, token);
		}  
		gto.filename = filePref;
		gto.filename += highNum;
	}
	if (gto.fromTree > -1 && gto.toTree > gto.fromTree) {
		errormsg << "The FromTree setting (" << gto.fromTree << ") cannot be less than the ToTree setting (" << gto.toTree << ")";
		throw XBull(errormsg, token);
	}
		
	gto.fromTree = (gto.fromTree < 1 ? -1 : gto.fromTree - 1);
	gto.toTree = (gto.toTree < 1 ? -1 : gto.toTree - 1);
	return gto;
}