Exemplo n.º 1
0
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();
	}

}
Exemplo n.º 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;
}