Search in sources :

Example 1 with NewickExporter

use of jebl.evolution.io.NewickExporter in project beast-mcmc by beast-dev.

the class CoalGenApp method simulate.

public static void simulate(String inputFileName, String treesFileName, String outputFileName) throws IOException, TraceException, Importer.ImportException {
    File logFile = new File(inputFileName);
    System.out.println("Loading trace file: " + inputFileName);
    LogFileTraces traces = new LogFileTraces(inputFileName, logFile);
    traces.loadTraces();
    traces.setBurnIn(0);
    System.out.println(traces.getStateCount() + " states loaded");
    System.out.println();
    System.out.println("Opening trees file: " + treesFileName);
    //        BufferedReader reader = new BufferedReader(new FileReader(treesFileName));
    System.out.println("Simulating...");
    System.out.println("0              25             50             75            100");
    System.out.println("|--------------|--------------|--------------|--------------|");
    int stepSize = traces.getStateCount() / 60;
    if (stepSize < 1)
        stepSize = 1;
    PrintWriter writer = new PrintWriter(new FileWriter(outputFileName));
    FileReader fileReader = new FileReader(treesFileName);
    TreeImporter importer = new NexusImporter(fileReader);
    EmpiricalDemographicFunction demo = null;
    IntervalGenerator intervals = null;
    TreeSimulator sim = null;
    CoalGenData data = new CoalGenData();
    data.traces = traces;
    // const stepwise bsp
    data.setDemographicModel(7);
    data.setupSkyline();
    double[] popSizes = new double[data.popSizeCount];
    double[] groupSizes = new double[data.groupSizeCount];
    NewickExporter exporter = new NewickExporter(writer);
    int count = 0;
    try {
        while (importer.hasTree()) {
            RootedTree inTree = (RootedTree) importer.importNextTree();
            if (sim == null) {
                setSamplingTimes(inTree);
                sim = new TreeSimulator(inTree.getTaxa(), "date");
            }
            data.getNextSkyline(popSizes, groupSizes);
            double[] times = getTimes(inTree, groupSizes);
            demo = new EmpiricalDemographicFunction(popSizes, times, true);
            intervals = new CoalescentIntervalGenerator(demo);
            RootedTree outTree = sim.simulate(intervals);
            exporter.exportTree(outTree);
            writer.println();
            writer.flush();
            if (count > 0 && count % stepSize == 0) {
                System.out.print("*");
                System.out.flush();
            }
            count++;
        }
    } catch (ImportException e) {
        e.printStackTrace();
    }
    fileReader.close();
    writer.close();
}
Also used : TreeSimulator(jebl.evolution.treesimulation.TreeSimulator) NexusImporter(jebl.evolution.io.NexusImporter) NewickExporter(jebl.evolution.io.NewickExporter) ImportException(jebl.evolution.io.ImportException) CoalescentIntervalGenerator(jebl.evolution.treesimulation.CoalescentIntervalGenerator) RootedTree(jebl.evolution.trees.RootedTree) EmpiricalDemographicFunction(jebl.evolution.coalescent.EmpiricalDemographicFunction) LogFileTraces(dr.inference.trace.LogFileTraces) TreeImporter(jebl.evolution.io.TreeImporter) IntervalGenerator(jebl.evolution.treesimulation.IntervalGenerator) CoalescentIntervalGenerator(jebl.evolution.treesimulation.CoalescentIntervalGenerator)

Aggregations

LogFileTraces (dr.inference.trace.LogFileTraces)1 EmpiricalDemographicFunction (jebl.evolution.coalescent.EmpiricalDemographicFunction)1 ImportException (jebl.evolution.io.ImportException)1 NewickExporter (jebl.evolution.io.NewickExporter)1 NexusImporter (jebl.evolution.io.NexusImporter)1 TreeImporter (jebl.evolution.io.TreeImporter)1 RootedTree (jebl.evolution.trees.RootedTree)1 CoalescentIntervalGenerator (jebl.evolution.treesimulation.CoalescentIntervalGenerator)1 IntervalGenerator (jebl.evolution.treesimulation.IntervalGenerator)1 TreeSimulator (jebl.evolution.treesimulation.TreeSimulator)1