use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class BeagleSeqSimTest method simulateTwoPartitions.
// END: simulateOnePartition
static void simulateTwoPartitions() {
try {
System.out.println("Test case 3: simulateTwoPartitions");
MathUtils.setSeed(666);
int sequenceLength = 11;
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);
DefaultTreeModel treeModel = new DefaultTreeModel(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 substitution model
Parameter kappa = new Parameter.Default(1, 10);
HKY hky = new HKY(kappa, freqModel);
HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(hky);
// create site model
GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
// create branch rate model
BranchRateModel branchRateModel = new DefaultBranchRateModel();
// create partition
Partition partition1 = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
0, // to
3, // every
1);
// create partition
Partition Partition = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
4, // to
sequenceLength - 1, // every
1);
Sequence ancestralSequence = new Sequence();
ancestralSequence.appendSequenceString("TCAAGTG");
Partition.setRootSequence(ancestralSequence);
partitionsList.add(partition1);
partitionsList.add(Partition);
// feed to sequence simulator and generate data
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
System.out.println(simulator.simulate(simulateInPar, false).toString());
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
// END: try-catch block
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class BeagleSeqSimTest method simulateRandomBranchAssignment.
// END: main
static void simulateRandomBranchAssignment() {
try {
System.out.println("Test case I dunno which: simulate random branch assignments");
MathUtils.setSeed(666);
int sequenceLength = 10;
ArrayList<Partition> partitionsList = new ArrayList<Partition>();
File treeFile = new File("/home/filip/Dropbox/BeagleSequenceSimulator/SimTree/SimTree.figtree");
Tree tree = Utils.importTreeFromFile(treeFile);
DefaultTreeModel treeModel = new DefaultTreeModel(tree);
// create Frequency Model
Parameter freqs = new Parameter.Default(Utils.UNIFORM_CODON_FREQUENCIES);
FrequencyModel freqModel = new FrequencyModel(Codons.UNIVERSAL, freqs);
// create base subst model
Parameter omegaParameter = new Parameter.Default("omega", 1, 1.0);
Parameter kappaParameter = new Parameter.Default("kappa", 1, 1.0);
GY94CodonModel baseSubModel = new GY94CodonModel(Codons.UNIVERSAL, omegaParameter, kappaParameter, freqModel);
RandomBranchModel substitutionModel = new RandomBranchModel(treeModel, baseSubModel, 0.25, false, -1);
// create site model
GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
// create branch rate model
BranchRateModel branchRateModel = new DefaultBranchRateModel();
// create partition
Partition partition1 = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
0, // to
sequenceLength - 1, // every
1);
// Sequence ancestralSequence = new Sequence();
// ancestralSequence.appendSequenceString("TCAAGTGAGG");
// partition1.setRootSequence(ancestralSequence);
partitionsList.add(partition1);
// feed to sequence simulator and generate data
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
SimpleAlignment alignment = simulator.simulate(simulateInPar, true);
// alignment.setOutputType(SimpleAlignment.OutputType.NEXUS);
alignment.setOutputType(SimpleAlignment.OutputType.XML);
System.out.println(alignment.toString());
} catch (Exception e) {
e.printStackTrace();
}
// END: try-catch
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class Coevolve method runModel2.
private void runModel2(PatternList patternList, PrintWriter pw, Tree tree, SubstitutionModel substModel, final Parameter betaParameter) {
final Parameter muParameter = new Parameter.Default(1.0);
muParameter.setId("mu");
SiteModel siteModel = new GammaSiteModel(substModel, muParameter, null, 1, null);
DefaultTreeModel treeModel = new DefaultTreeModel(tree);
final TreeLikelihood treeLikelihood = new TreeLikelihood(patternList, treeModel, siteModel, null, null, false, false, true, false, false);
treeLikelihood.setId("likelihood");
MultivariateFunction function = new MultivariateFunction() {
public double evaluate(double[] argument) {
betaParameter.setParameterValue(0, argument[0]);
betaParameter.setParameterValue(1, argument[1]);
muParameter.setParameterValue(0, argument[2]);
double lnL = -treeLikelihood.getLogLikelihood();
// System.err.println("" + argument[0] + "\t" + argument[1] + "\t" + argument[2] + "\t" + lnL);
return lnL;
}
public int getNumArguments() {
return 3;
}
public double getLowerBound(int n) {
return 0.0;
}
public double getUpperBound(int n) {
return 100.0;
}
};
MultivariateMinimum optimizer = new ConjugateGradientSearch();
double lnL = optimizer.findMinimum(function, new double[] { 1.0, 1.0, 1.0 }, 6, 6);
pw.write(betaParameter.getParameterValue(0) + "\t");
pw.write(betaParameter.getParameterValue(1) + "\t");
pw.write(muParameter.getParameterValue(0) + "\t");
pw.write(lnL + "\n");
pw.flush();
System.out.println("" + betaParameter.getParameterValue(0) + "\t" + betaParameter.getParameterValue(1) + "\t" + muParameter.getParameterValue(0) + "\t" + lnL);
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class AncestralSequenceAnnotator method sampleTree.
private FlexibleTree sampleTree(Tree tree, PatternList alignment, GammaSiteRateModel siteModel, BranchRateModel rateModel) {
FlexibleTree flexTree = new FlexibleTree(tree, true);
flexTree.adoptTreeModelOrdering();
FlexibleTree finalTree = new FlexibleTree(tree);
finalTree.adoptTreeModelOrdering();
TreeModel treeModel = new DefaultTreeModel(tree);
// Turn off noisy logging by TreeLikelihood constructor
Logger logger = Logger.getLogger("dr.evomodel");
boolean useParentHandlers = logger.getUseParentHandlers();
logger.setUseParentHandlers(false);
// AncestralStateTreeLikelihood likelihood = new AncestralStateTreeLikelihood(
// alignment,
// treeModel,
// siteModel,
// rateModel,
// false, true,
// alignment.getDataType(),
// TAG,
// false);
AncestralStateBeagleTreeLikelihood likelihood = new AncestralStateBeagleTreeLikelihood(alignment, treeModel, new HomogeneousBranchModel(siteModel.getSubstitutionModel()), siteModel, rateModel, null, false, PartialsRescalingScheme.DEFAULT, true, null, alignment.getDataType(), TAG, false, true);
// PatternList patternList, TreeModel treeModel,
// BranchSiteModel branchSiteModel,
// SiteRateModel siteRateModel,
// BranchRateModel branchRateModel,
// boolean useAmbiguities,
// PartialsRescalingScheme scalingScheme,
// Map<Set<String>, Parameter> partialsRestrictions,
// final DataType dataType,
// final String tag,
// SubstitutionModel substModel,
// boolean useMAP,
// boolean returnML) {
// PatternList patternList, TreeModel treeModel,
// SiteModel siteModel, BranchRateModel branchRateModel,
// boolean useAmbiguities, boolean storePartials,
// final DataType dataType,
// final String tag,
// boolean forceRescaling,
// boolean useMAP,
// boolean returnML) {
logger.setUseParentHandlers(useParentHandlers);
// Sample internal nodes
likelihood.makeDirty();
double logLikelihood = likelihood.getLogLikelihood();
System.out.println("The new and old Likelihood (this value should be roughly the same, debug?): " + logLikelihood + ", " + Double.parseDouble(tree.getAttribute(LIKELIHOOD).toString()));
if (Double.parseDouble(tree.getAttribute(LIKELIHOOD).toString()) != logLikelihood) {
/* Newly written check, not sure if this is correct. May need to round values at least */
// throw new RuntimeException("The values of likelihood are not identical");
}
// System.err.printf("New logLikelihood = %4.1f\n", logLikelihood);
flexTree.setAttribute(LIKELIHOOD, logLikelihood);
TreeTrait ancestralStates = likelihood.getTreeTrait(TAG);
for (int i = 0; i < treeModel.getNodeCount(); i++) {
NodeRef node = treeModel.getNode(i);
String sample = ancestralStates.getTraitString(treeModel, node);
String oldSeq = (String) flexTree.getNodeAttribute(flexTree.getNode(i), SEQ_STRING);
if (oldSeq != null) {
char[] seq = (sample.substring(1, sample.length() - 1)).toCharArray();
int length = oldSeq.length();
for (int j = 0; j < length; j++) {
if (oldSeq.charAt(j) == GAP)
seq[j] = GAP;
}
String newSeq = new String(seq);
// if( newSeq.contains("MMMMMMM") ) {
// System.err.println("bad = "+newSeq);
// System.exit(-1);
// }
finalTree.setNodeAttribute(finalTree.getNode(i), NEW_SEQ, newSeq);
}
// Taxon taxon = finalTree.getNodeTaxon(finalTree.getNode(i));
// System.err.println("node: "+(taxon == null ? "null" : taxon.getId())+" "+
// finalTree.getNodeAttribute(finalTree.getNode(i),NEW_SEQ));
}
return finalTree;
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class NarrowExchangeTest method testNarrow.
/**
* Test method for {@link dr.evomodel.operators.ExchangeOperator#narrow()}.
*/
public void testNarrow() throws IOException, ImportException {
// probability of picking B node is 1/(2n-4) = 1/6
// probability of swapping it with C is 1/1
// total = 1/6
System.out.println("Test 1: Forward");
String treeMatch = "((((A,C),B),D),E);";
int count = 0;
int reps = 100000;
for (int i = 0; i < reps; i++) {
DefaultTreeModel treeModel = new DefaultTreeModel("treeModel", tree5);
ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.NARROW, treeModel, 1);
operator.doOperation();
String tree = TreeUtils.newickNoLengths(treeModel);
if (tree.equals(treeMatch)) {
count += 1;
}
}
double p_1 = (double) count / (double) reps;
System.out.println("Number of proposals:\t" + count);
System.out.println("Number of tries:\t" + reps);
System.out.println("Number of ratio:\t" + p_1);
System.out.println("Number of expected ratio:\t" + 1.0 / 6.0);
assertExpectation(1.0 / 6.0, p_1, reps);
}
Aggregations