use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class TraceCorrelationAssert method createPrimateTreeModel.
//************************** data ****************************
protected TreeModel createPrimateTreeModel() {
SimpleNode[] nodes = new SimpleNode[10];
for (int n = 0; n < 10; n++) {
nodes[n] = new SimpleNode();
}
// nodes[0].setHeight(0);
// human
nodes[0].setTaxon(taxa[0]);
// chimp
nodes[1].setTaxon(taxa[1]);
// bonobo
nodes[2].setTaxon(taxa[2]);
nodes[3].setHeight(0.010772);
nodes[3].addChild(nodes[1]);
nodes[3].addChild(nodes[2]);
nodes[4].setHeight(0.024003);
nodes[4].addChild(nodes[0]);
nodes[4].addChild(nodes[3]);
// gorilla
nodes[5].setTaxon(taxa[3]);
nodes[6].setHeight(0.036038);
nodes[6].addChild(nodes[4]);
nodes[6].addChild(nodes[5]);
// orangutan
nodes[7].setTaxon(taxa[4]);
nodes[8].setHeight(0.069125);
nodes[8].addChild(nodes[6]);
nodes[8].addChild(nodes[7]);
// siamang
nodes[9].setTaxon(taxa[5]);
SimpleNode root = new SimpleNode();
root.setHeight(0.099582);
root.addChild(nodes[8]);
root.addChild(nodes[9]);
Tree tree = new SimpleTree(root);
tree.setUnits(Units.Type.YEARS);
//treeModel
return new TreeModel(tree);
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class MainFrame method generateNumberOfSimulations.
// END: doExport
// threading, UI, exceptions handling
private void generateNumberOfSimulations(final File outFile) {
setBusy();
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
ArrayList<TreeModel> simulatedTreeModelList = new ArrayList<TreeModel>();
// Executed in background thread
public Void doInBackground() {
try {
if (BeagleSequenceSimulatorApp.VERBOSE) {
Utils.printPartitionDataList(dataList);
System.out.println();
}
long startingSeed = dataList.startingSeed;
for (int i = 0; i < dataList.simulationsCount; i++) {
String fullPath = Utils.getMultipleWritePath(outFile, dataList.outputFormat.toString().toLowerCase(), i);
PrintWriter writer = new PrintWriter(new FileWriter(fullPath));
ArrayList<Partition> partitionsList = new ArrayList<Partition>();
for (PartitionData data : dataList) {
if (data.record == null) {
writer.close();
throw new RuntimeException("Set data in Partitions tab for " + (partitionsList.size() + 1) + " partition.");
} else {
TreeModel treeModel = data.createTreeModel();
simulatedTreeModelList.add(treeModel);
// create partition
Partition partition = new Partition(//
treeModel, //
data.createBranchModel(), //
data.createSiteRateModel(), //
data.createClockRateModel(), //
data.createFrequencyModel(), // from
data.from - 1, // to
data.to - 1, // every
data.every);
if (data.ancestralSequenceString != null) {
partition.setRootSequence(data.createAncestralSequence());
}
partitionsList.add(partition);
}
}
if (dataList.setSeed) {
MathUtils.setSeed(startingSeed);
startingSeed += 1;
}
beagleSequenceSimulator = new BeagleSequenceSimulator(partitionsList);
SimpleAlignment alignment = beagleSequenceSimulator.simulate(dataList.useParallel, dataList.outputAncestralSequences);
alignment.setOutputType(dataList.outputFormat);
// if (dataList.outputFormat == SimpleAlignment.OutputType.NEXUS) {
// alignment.setOutputType(dataList.outputFormat);
// } else if(dataList.outputFormat == SimpleAlignment.OutputType.XML) {
// alignment.setOutputType(dataList.outputFormat);
// }else {
// //
// }
writer.println(alignment.toString());
writer.close();
}
// END: simulationsCount loop
} catch (Exception e) {
Utils.handleException(e);
setStatus("Exception occured.");
setIdle();
}
return null;
}
// END: doInBackground
// Executed in event dispatch thread
public void done() {
// LinkedHashMap<Integer, LinkedHashMap<NodeRef, int[]>> partitionSequencesMap = beagleSequenceSimulator.getPartitionSequencesMap();
terminalPanel.setText(Utils.partitionDataListToString(dataList, simulatedTreeModelList));
setStatus("Generated " + Utils.getSiteCount(dataList) + " sites.");
setIdle();
}
};
worker.execute();
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class TransmissionWilsonBaldingB method proposeTree.
public void proposeTree() {
TreeModel tree = c2cLikelihood.getTreeModel();
BranchMapModel branchMap = c2cLikelihood.getBranchMap();
NodeRef i;
double oldMinAge, newMinAge, newRange, oldRange, newAge, q;
// choose a random eligible node
final int nodeCount = tree.getNodeCount();
do {
i = tree.getNode(MathUtils.nextInt(nodeCount));
} while (!eligibleForMove(i, tree, branchMap));
final NodeRef iP = tree.getParent(i);
//this one can go anywhere
NodeRef j = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
NodeRef jP = tree.getParent(j);
while ((jP != null && tree.getNodeHeight(jP) <= tree.getNodeHeight(i)) || (i == j)) {
j = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
jP = tree.getParent(j);
}
if (iP == tree.getRoot() || j == tree.getRoot()) {
throw new RuntimeException("Root changes not allowed!");
}
if (jP == iP || j == iP || jP == i)
throw new RuntimeException("move failed");
final NodeRef CiP = getOtherChild(tree, iP, i);
NodeRef PiP = tree.getParent(iP);
if (resampleInfectionTimes) {
AbstractCase iCase = branchMap.get(i.getNumber());
AbstractCase iPCase = branchMap.get(iP.getNumber());
AbstractCase CiPCase = branchMap.get(CiP.getNumber());
AbstractCase PiPCase = null;
if (PiP != null) {
PiPCase = branchMap.get(PiP.getNumber());
}
if (iCase != iPCase) {
iCase.setInfectionBranchPosition(MathUtils.nextDouble());
}
// what happens between PiP and CiP
if (PiPCase == null || CiPCase != PiPCase) {
CiPCase.setInfectionBranchPosition(MathUtils.nextDouble());
}
// what happens between k and j
AbstractCase jCase = branchMap.get(j.getNumber());
jCase.setInfectionBranchPosition(MathUtils.nextDouble());
}
newMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(j));
newRange = tree.getNodeHeight(jP) - newMinAge;
newAge = newMinAge + (MathUtils.nextDouble() * newRange);
oldMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(CiP));
oldRange = tree.getNodeHeight(PiP) - oldMinAge;
q = newRange / Math.abs(oldRange);
if (branchMap.get(PiP.getNumber()) != branchMap.get(CiP.getNumber())) {
q *= 0.5;
}
if (branchMap.get(jP.getNumber()) != branchMap.get(j.getNumber())) {
q *= 2;
}
tree.beginTreeEdit();
if (j == tree.getRoot()) {
// 1. remove edges <iP, CiP>
tree.removeChild(iP, CiP);
tree.removeChild(PiP, iP);
// 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
tree.addChild(iP, j);
tree.addChild(PiP, CiP);
// iP is the new root
tree.setRoot(iP);
} else if (iP == tree.getRoot()) {
// 1. remove edges <k, j>, <iP, CiP>, <PiP, iP>
tree.removeChild(jP, j);
tree.removeChild(iP, CiP);
// 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
tree.addChild(iP, j);
tree.addChild(jP, iP);
//CiP is the new root
tree.setRoot(CiP);
} else {
// 1. remove edges <k, j>, <iP, CiP>, <PiP, iP>
tree.removeChild(jP, j);
tree.removeChild(iP, CiP);
tree.removeChild(PiP, iP);
// 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
tree.addChild(iP, j);
tree.addChild(jP, iP);
tree.addChild(PiP, CiP);
}
tree.setNodeHeight(iP, newAge);
tree.endTreeEdit();
//
logq = Math.log(q);
if (MathUtils.nextInt(2) == 0) {
branchMap.set(iP.getNumber(), branchMap.get(jP.getNumber()), true);
} else {
branchMap.set(iP.getNumber(), branchMap.get(j.getNumber()), true);
}
if (DEBUG) {
c2cLikelihood.getTreeModel().checkPartitions();
}
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class TransmissionExchangeOperatorB method doOperation.
public double doOperation() {
TreeModel tree = c2cLikelihood.getTreeModel();
double hr = exchange();
final int tipCount = tree.getExternalNodeCount();
assert tree.getExternalNodeCount() == tipCount : "Lost some tips";
return hr;
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class TransmissionExchangeOperatorA method doOperation.
public double doOperation() {
TreeModel tree = c2cLikelihood.getTreeModel();
double hr = exchange();
final int tipCount = tree.getExternalNodeCount();
assert tree.getExternalNodeCount() == tipCount : "Lost some tips";
return hr;
}
Aggregations