Search in sources :

Example 1 with ImportException

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

the class ContinuousTreeToKML method main.

public static void main(String[] args) {
    // altitutude of the root of the 3D trees
    double altitude = 0;
    // required to convert heights to calendar dates
    double mostRecentDate = 2010;
    String coordinateLabel = "loc";
    boolean makeTreeSlices = false;
    double[] sliceTimes = null;
    double treeSliceBranchWidth = 3;
    // shows complete branch for slice if time is more recent than the branch's midpoint
    boolean showBranchAtMidPoint = false;
    Arguments arguments = new Arguments(new Arguments.Option[] { new Arguments.StringOption(ANNOTATION, "location annotation label", "specifies the label used for location coordinates annotation [default=location]"), new Arguments.RealOption(ALTITUDE, "specifies the altitude of the root of the 3D tree [default=no 3D tree]"), new Arguments.RealOption(MRSD, "specifies the most recent sampling data in fractional years to rescale time [default=2010]"), new Arguments.StringOption(SLICES, "time", "specifies a slice time-list [default=none]"), new Arguments.StringOption(SLICEMIDPOINT, falseTrue, false, "shows complete branch for sliced tree if time is more recent than the branch's midpoint [default=false"), new Arguments.Option(HELP, "option to print this message") });
    try {
        arguments.parseArguments(args);
    } catch (Arguments.ArgumentException ae) {
        progressStream.println(ae);
        printUsage(arguments);
        System.exit(1);
    }
    if (args.length == 0 || arguments.hasOption(HELP)) {
        printUsage(arguments);
        System.exit(0);
    }
    if (arguments.hasOption(MRSD)) {
        mostRecentDate = arguments.getRealOption(MRSD);
    }
    if (arguments.hasOption(ALTITUDE)) {
        altitude = arguments.getRealOption(ALTITUDE);
    }
    String annotationLabel = arguments.getStringOption(ANNOTATION);
    if (annotationLabel != null) {
        coordinateLabel = annotationLabel;
    }
    String sliceString = arguments.getStringOption(SLICES);
    if (sliceString != null) {
        makeTreeSlices = true;
        try {
            sliceTimes = DiscreteTreeToKML.parseVariableLengthDoubleArray(sliceString);
        } catch (Arguments.ArgumentException ae) {
            System.err.println("error reading slice heights");
            ae.printStackTrace();
            return;
        }
        makeTreeSlices = true;
    }
    if (arguments.hasOption(SLICEBW)) {
        treeSliceBranchWidth = arguments.getRealOption(SLICEBW);
    }
    String midpointString = arguments.getStringOption(SLICEMIDPOINT);
    if (midpointString != null && midpointString.compareToIgnoreCase("true") == 0) {
        showBranchAtMidPoint = true;
    }
    final String[] args2 = arguments.getLeftoverArguments();
    String inputFileName = null;
    String outputFileName = null;
    switch(args2.length) {
        case 0:
            printUsage(arguments);
            System.exit(1);
        case 1:
            inputFileName = args2[0];
            outputFileName = inputFileName + ".kml";
            break;
        case 2:
            inputFileName = args2[0];
            outputFileName = args2[1];
            break;
        default:
            {
                System.err.println("Unknown option: " + args2[2]);
                System.err.println();
                printUsage(arguments);
                System.exit(1);
            }
    }
    RootedTree tree = null;
    try {
        TreeImporter importer = new NexusImporter(new FileReader(inputFileName));
        tree = (RootedTree) importer.importNextTree();
    } catch (ImportException e) {
        e.printStackTrace();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    ContinuousKML exporter = new ContinuousKML(tree, inputFileName, altitude, mostRecentDate, coordinateLabel);
    try {
        BufferedWriter out1 = new BufferedWriter(new FileWriter(outputFileName));
        StringBuffer buffer = new StringBuffer();
        //we write the general tree stuff, but when making slices we do not include everything in the buffer compilation
        exporter.writeTreeToKML();
        if (makeTreeSlices) {
            for (int i = 0; i < sliceTimes.length; i++) {
                //                    System.out.println(sliceTimes[i]);
                exporter.writeTreeToKML(sliceTimes[i], treeSliceBranchWidth, showBranchAtMidPoint);
            }
        }
        exporter.compileBuffer(buffer, makeTreeSlices);
        out1.write(buffer.toString());
        out1.close();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}
Also used : NexusImporter(jebl.evolution.io.NexusImporter) Arguments(dr.app.util.Arguments) ImportException(jebl.evolution.io.ImportException) RootedTree(jebl.evolution.trees.RootedTree) TreeImporter(jebl.evolution.io.TreeImporter)

Example 2 with ImportException

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

the class DiscreteTreeToKML method readTree.

private static RootedTree readTree(String inString) throws Arguments.ArgumentException {
    RootedTree tree;
    try {
        TreeImporter importer = new NexusImporter(new FileReader(inString));
        tree = (RootedTree) importer.importNextTree();
    } catch (ImportException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return tree;
}
Also used : ImportException(jebl.evolution.io.ImportException) NexusImporter(jebl.evolution.io.NexusImporter) RootedTree(jebl.evolution.trees.RootedTree) TreeImporter(jebl.evolution.io.TreeImporter) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 3 with ImportException

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

the class TreeSpaceFrame method loadTrees.

private TreeLineages loadTrees(InputFile inputFile) throws IOException {
    PrintStream progressStream = System.out;
    int totalTrees = 10000;
    int totalTreesUsed = 0;
    progressStream.println("Reading trees (bar assumes 10,000 trees)...");
    progressStream.println("0              25             50             75            100");
    progressStream.println("|--------------|--------------|--------------|--------------|");
    int stepSize = totalTrees / 60;
    if (stepSize < 1)
        stepSize = 1;
    TreeLineages treeLineages = new TreeLineages();
    FileReader fileReader = new FileReader(inputFile.getFile());
    jebl.evolution.io.NexusImporter importer = new NexusImporter(fileReader);
    try {
        totalTrees = 0;
        while (importer.hasTree()) {
            RootedTree tree = (RootedTree) importer.importNextTree();
            if (totalTrees >= inputFile.getBurnin()) {
                treeLineages.addTree(tree);
                totalTreesUsed += 1;
            }
            if (totalTrees > 0 && totalTrees % stepSize == 0) {
                progressStream.print("*");
                progressStream.flush();
            }
            totalTrees++;
        }
    } catch (ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
        return null;
    }
    fileReader.close();
    progressStream.println();
    progressStream.println();
    if (totalTrees < 1) {
        System.err.println("No trees");
        return null;
    }
    if (totalTreesUsed <= 1) {
        if (inputFile.getBurnin() > 0) {
            System.err.println("No trees to use: burnin too high");
            return null;
        }
    }
    progressStream.println("Total trees read: " + totalTrees);
    if (inputFile.getBurnin() > 0) {
        progressStream.println("Ignoring first " + inputFile.getBurnin() + " trees.");
    }
    treeLineages.setupTrees();
    return treeLineages;
}
Also used : ImportException(jebl.evolution.io.ImportException) NexusImporter(jebl.evolution.io.NexusImporter) RootedTree(jebl.evolution.trees.RootedTree) java.io(java.io) NexusImporter(jebl.evolution.io.NexusImporter)

Example 4 with ImportException

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

the class SkylineReconstructor method main.

public static void main(String[] argv) {
    Variate x = null;
    List<Variate> plots = new ArrayList<Variate>();
    for (int i = 1; i <= 200; i++) {
        String stem = "sim" + (i < 10 ? "00" : (i < 100 ? "0" : "")) + i;
        try {
            SkylineReconstructor skyline = new SkylineReconstructor(new File(stem + ".log"), new File(stem + ".trees"), 1000000, 200, 0.0, 150000, 0.0);
            if (x == null) {
                x = skyline.getXData();
            }
            plots.add(skyline.getYDataMean());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ImportException e) {
            e.printStackTrace();
        } catch (TraceException e) {
            e.printStackTrace();
        }
        if (i % 10 == 0) {
            System.err.println("Read " + i);
        }
    }
    for (int i = 0; i < x.getCount(); i++) {
        System.out.print(x.get(i));
        for (Variate y : plots) {
            System.out.print("\t" + y.get(i));
        }
        System.out.println();
    }
}
Also used : ImportException(jebl.evolution.io.ImportException) TraceException(dr.inference.trace.TraceException) Variate(dr.stats.Variate) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 5 with ImportException

use of jebl.evolution.io.ImportException 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

ImportException (jebl.evolution.io.ImportException)8 NexusImporter (jebl.evolution.io.NexusImporter)6 RootedTree (jebl.evolution.trees.RootedTree)6 TreeImporter (jebl.evolution.io.TreeImporter)5 IOException (java.io.IOException)4 Arguments (dr.app.util.Arguments)2 TraceException (dr.inference.trace.TraceException)2 FileReader (java.io.FileReader)2 LogFileTraces (dr.inference.trace.LogFileTraces)1 Variate (dr.stats.Variate)1 java.io (java.io)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 EmpiricalDemographicFunction (jebl.evolution.coalescent.EmpiricalDemographicFunction)1 NewickExporter (jebl.evolution.io.NewickExporter)1 CoalescentIntervalGenerator (jebl.evolution.treesimulation.CoalescentIntervalGenerator)1 IntervalGenerator (jebl.evolution.treesimulation.IntervalGenerator)1