use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class LineageSpecificBranchModelParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
FrequencyModel rootFrequencyModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
XMLObject cxo = xo.getChild(MODELS);
for (int i = 0; i < cxo.getChildCount(); i++) {
SubstitutionModel substModel = (SubstitutionModel) cxo.getChild(i);
substitutionModels.add(substModel);
}
//END: models loop
// TODO: check if categories numbering starts from zero
Parameter categories = (Parameter) xo.getElementFirstChild(CATEGORIES);
return new //provider,
LineageSpecificBranchModel(//provider,
treeModel, //provider,
rootFrequencyModel, //provider,
substitutionModels, categories);
}
use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class HiddenMarkovRatesTest method testHiddenRates.
public void testHiddenRates() {
final int index = 0;
double[] freqs = getBinaryFreqs(index);
FrequencyModel binaryFreqModel = new FrequencyModel(TwoStates.INSTANCE, freqs);
int relativeTo = 0;
Parameter ratesParameter = new Parameter.Default(0);
GeneralSubstitutionModel binaryModel = new GeneralSubstitutionModel("binary", TwoStates.INSTANCE, binaryFreqModel, ratesParameter, relativeTo);
UniformizedSubstitutionModel uSM = new UniformizedSubstitutionModel(binaryModel, MarkovJumpsType.REWARDS);
uSM.setSaveCompleteHistory(true);
double[] rewardRegister = new double[] { 0.0, 1.0 };
uSM.setRegistration(rewardRegister);
final double[] hkyFreqs = getHKYFreqs(index);
FrequencyModel hkyFreqModel = new FrequencyModel(Nucleotides.INSTANCE, hkyFreqs);
final double kappa = getKappa(index);
final HKY hky = new HKY(kappa, hkyFreqModel);
final double length = getLength(index);
double[] resultCompleteHistory = new double[16];
final int replicates = getNumberReplicates(index);
double result = 0.0;
for (int r = 0; r < replicates; ++r) {
result += oneCompleteHistoryReplicate(resultCompleteHistory, hky, uSM, length);
}
result /= replicates;
normalize(resultCompleteHistory, replicates);
System.out.println("Averaged probabilities");
System.out.println(result);
System.out.println(new Vector(resultCompleteHistory));
System.out.println();
double[] intermediate = new double[16];
hky.getTransitionProbabilities(result, intermediate);
System.out.println("Intermediate using above average reward");
System.out.println(result);
System.out.println(new Vector(intermediate));
System.out.println();
double[] resultExpected = new double[16];
UniformizedSubstitutionModel expectedUSM = new UniformizedSubstitutionModel(binaryModel, MarkovJumpsType.REWARDS, replicates);
expectedUSM.setRegistration(rewardRegister);
result = oneCompleteHistoryReplicate(resultExpected, hky, expectedUSM, length);
System.out.println("Averaged reward");
System.out.println(result);
System.out.println(new Vector(resultExpected));
System.out.println();
double[] originalProbs = new double[16];
hky.getTransitionProbabilities(length, originalProbs);
System.out.println("Original probabilities");
System.out.println(new Vector(originalProbs));
System.out.println();
}
use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class TN93Test method testTN93.
public void testTN93() {
Parameter kappa1 = new Parameter.Default(5.0);
Parameter kappa2 = new Parameter.Default(2.0);
double[] pi = new double[] { 0.40, 0.20, 0.30, 0.10 };
double time = 0.1;
FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, pi);
TN93 tn = new TN93(kappa1, kappa2, freqModel);
EigenDecomposition decomp = tn.getEigenDecomposition();
Vector eval = new Vector(decomp.getEigenValues());
System.out.println("Eval = " + eval);
double[] probs = new double[16];
tn.getTransitionProbabilities(time, probs);
System.out.println("new probs = " + new Vector(probs));
// check against old implementation
dr.oldevomodel.substmodel.FrequencyModel oldFreq = new dr.oldevomodel.substmodel.FrequencyModel(Nucleotides.INSTANCE, pi);
dr.oldevomodel.substmodel.TN93 oldTN = new dr.oldevomodel.substmodel.TN93(kappa1, kappa2, oldFreq);
double[] oldProbs = new double[16];
oldTN.getTransitionProbabilities(time, oldProbs);
System.out.println("old probs = " + new Vector(oldProbs));
assertEquals(probs, oldProbs, 10E-6);
}
use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class CompleteHistorySimulator method simulate.
// END: toString
/**
* perform the actual sequence generation
*
* @return alignment containing randomly generated sequences for the nodes in the
* leaves of the tree
*/
public void simulate() {
double[] lambda = new double[stateCount * stateCount];
if (!branchSpecificLambda) {
// Assumes a single generator for whole tree
siteModel.getSubstitutionModel().getInfinitesimalMatrix(lambda);
}
NodeRef root = tree.getRoot();
double[] categoryProbs = siteModel.getCategoryProportions();
int[] category = new int[nReplications];
for (int i = 0; i < nReplications; i++) {
category[i] = MathUtils.randomChoicePDF(categoryProbs);
}
FrequencyModel frequencyModel = siteModel.getSubstitutionModel().getFrequencyModel();
int[] seq = new int[nReplications];
for (int i = 0; i < nReplications; i++) {
seq[i] = MathUtils.randomChoicePDF(frequencyModel.getFrequencies());
}
setDataType(siteModel.getSubstitutionModel().getDataType());
traverse(root, seq, category, this, lambda);
}
use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class PartitionParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
int from = 0;
int to = -1;
int every = xo.getAttribute(EVERY, 1);
if (xo.hasAttribute(FROM)) {
from = xo.getIntegerAttribute(FROM) - 1;
if (from < 0) {
throw new XMLParseException("Illegal 'from' attribute in patterns element");
}
}
if (xo.hasAttribute(TO)) {
to = xo.getIntegerAttribute(TO) - 1;
if (to < 0 || to < from) {
throw new XMLParseException("Illegal 'to' attribute in patterns element");
}
}
if (every <= 0) {
throw new XMLParseException("Illegal 'every' attribute in patterns element");
}
// END: every check
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
Sequence rootSequence = (Sequence) xo.getChild(Sequence.class);
BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
if (rateModel == null) {
rateModel = new DefaultBranchRateModel();
}
BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class);
if (branchModel == null) {
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
branchModel = new HomogeneousBranchModel(substitutionModel);
}
Partition partition = new Partition(tree, branchModel, siteModel, rateModel, freqModel, from, to, every);
if (rootSequence != null) {
partition.setRootSequence(rootSequence);
}
return partition;
}
Aggregations