use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class BeagleTreeLikelihood method main.
public static void main(String[] args) {
try {
MathUtils.setSeed(666);
System.out.println("Test case 1: simulateOnePartition");
int sequenceLength = 1000;
ArrayList<Partition> partitionsList = new ArrayList<Partition>();
// create tree
NewickImporter importer = new NewickImporter("(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
Tree tree = importer.importTree(null);
TreeModel treeModel = new TreeModel(tree);
// create Frequency Model
Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25, 0.25, 0.25 });
FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqs);
// create branch model
Parameter kappa1 = new Parameter.Default(1, 1);
Parameter kappa2 = new Parameter.Default(1, 1);
HKY hky1 = new HKY(kappa1, freqModel);
HKY hky2 = new HKY(kappa2, freqModel);
HomogeneousBranchModel homogenousBranchSubstitutionModel = new HomogeneousBranchModel(hky1);
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
substitutionModels.add(hky1);
substitutionModels.add(hky2);
List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>();
freqModels.add(freqModel);
Parameter epochTimes = new Parameter.Default(1, 20);
// create branch rate model
Parameter rate = new Parameter.Default(1, 0.001);
BranchRateModel branchRateModel = new StrictClockBranchRates(rate);
// create site model
GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
BranchModel homogeneousBranchModel = new HomogeneousBranchModel(hky1);
BranchModel epochBranchModel = new EpochBranchModel(treeModel, substitutionModels, epochTimes);
// create partition
Partition partition1 = new //
Partition(//
treeModel, //
homogenousBranchSubstitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
0, // to
sequenceLength - 1, // every
1);
partitionsList.add(partition1);
// feed to sequence simulator and generate data
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
Alignment alignment = simulator.simulate(false, false);
BeagleTreeLikelihood nbtl = new BeagleTreeLikelihood(alignment, treeModel, homogeneousBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, false);
System.out.println("nBTL(homogeneous) = " + nbtl.getLogLikelihood());
nbtl = new BeagleTreeLikelihood(alignment, treeModel, epochBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, false);
System.out.println("nBTL(epoch) = " + nbtl.getLogLikelihood());
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
// END: try-catch block
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class BranchAssignmentModelParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing branch assignment branch model.");
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
String annotation = xo.getStringAttribute(ANNOTATION);
LinkedHashMap<Integer, SubstitutionModel> modelIndexMap = new LinkedHashMap<Integer, SubstitutionModel>();
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
if (xoc.getName().equals(ASSIGNMENT)) {
Integer index = null;
if (xoc.hasAttribute(ANNOTATION_VALUE)) {
index = xoc.getIntegerAttribute(ANNOTATION_VALUE);
}
SubstitutionModel model = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
modelIndexMap.put(index, model);
}
}
}
SubstitutionModel baseModel = (SubstitutionModel) xo.getElementFirstChild(BASE_MODEL);
return new BranchAssignmentModel(treeModel, annotation, modelIndexMap, baseModel);
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class BranchSpecificBranchModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing clade-specific branch model.");
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
BranchSpecificBranchModel branchModel = new BranchSpecificBranchModel(tree, substitutionModel);
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
if (xoc.getName().equals(CLADE)) {
double stemWeight = xoc.getAttribute(STEM_WEIGHT, 0.0);
substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
if (taxonList.getTaxonCount() == 1) {
throw new XMLParseException("A clade must be defined by at least two taxa");
}
try {
branchModel.addClade(taxonList, substitutionModel, stemWeight);
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
}
} else if (xoc.getName().equals(EXTERNAL_BRANCHES)) {
substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
try {
branchModel.addExternalBranches(taxonList, substitutionModel);
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
}
} else if (xoc.getName().equals(BACKBONE)) {
substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
try {
branchModel.addBackbone(taxonList, substitutionModel);
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
}
}
}
}
return branchModel;
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class ExternalInternalBranchModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing external-internal branch model.");
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
SubstitutionModel internalSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
SubstitutionModel externalSubstitutionModel = (SubstitutionModel) xo.getElementFirstChild(EXTERNAL_BRANCHES);
return new ExternalInternalBranchModel(tree, externalSubstitutionModel, internalSubstitutionModel);
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class RandomBranchAssignmentModelParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing random assignment branch model.");
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
XMLObject cxo = xo.getChild(MODELS);
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
for (int i = 0; i < cxo.getChildCount(); i++) {
SubstitutionModel substModel = (SubstitutionModel) cxo.getChild(i);
substitutionModels.add(substModel);
}
return new RandomBranchAssignmentModel(treeModel, substitutionModels);
}
Aggregations