Search in sources :

Example 1 with TreeImporter

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

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

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

Example 4 with TreeImporter

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

the class TreeKMLGenerator method main.

public static void main(String[] args) {
    String inputTreeFile = args[0];
    RootedTree tree = null;
    try {
        TreeImporter importer = new NexusImporter(new FileReader(inputTreeFile));
        tree = (RootedTree) importer.importNextTree();
    } catch (ImportException e) {
        e.printStackTrace();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    TreeKMLGenerator generator = new TreeKMLGenerator(tree);
    Settings settings = new Settings(AnalysisType.CONTINUOUS);
    //        settings.getAltitudeTreeSettings().setTreeType(TreeType.ARC_TREE);
    //        settings.getAltitudeTreeSettings().getBranchStyle().setColorProperty("height");
    settings.getGroundTreeSettings().setTreeType(TreeType.SURFACE_TREE);
    settings.getGroundTreeSettings().getBranchStyle().setColorProperty("height");
    settings.setPlotAltitude(0);
    settings.setMostRecentDate(2003);
    //settings.setAgeCutOff(1995);
    settings.setTimeDivisionCount(0);
    settings.setTraitName("antigenic");
    settings.setLatitudeName("antigenic1");
    settings.setLongitudeName("antigenic2");
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(args[0] + ".kml"));
        Document doc = new Document(generator.generate(args[0], settings));
        try {
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            outputter.output(doc, out);
        } catch (IOException e) {
            System.err.println(e);
        }
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) NexusImporter(jebl.evolution.io.NexusImporter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ImportException(jebl.evolution.io.ImportException) RootedTree(jebl.evolution.trees.RootedTree) TreeImporter(jebl.evolution.io.TreeImporter) FileReader(java.io.FileReader)

Example 5 with TreeImporter

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

the class TreeGeoJSONGenerator method main.

public static void main(String[] args) {
    String inputTreeFile = args[0];
    RootedTree tree = null;
    try {
        TreeImporter importer = new NexusImporter(new FileReader(inputTreeFile));
        tree = (RootedTree) importer.importNextTree();
    } catch (ImportException e) {
        e.printStackTrace();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    TreeGeoJSONGenerator generator = new TreeGeoJSONGenerator(tree, null);
    try {
        generator.generate("", new PrintWriter(new File("output.geojson")));
    } catch (FileNotFoundException e) {
        //To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
}
Also used : ImportException(jebl.evolution.io.ImportException) NexusImporter(jebl.evolution.io.NexusImporter) RootedTree(jebl.evolution.trees.RootedTree) TreeImporter(jebl.evolution.io.TreeImporter)

Aggregations

NexusImporter (jebl.evolution.io.NexusImporter)6 TreeImporter (jebl.evolution.io.TreeImporter)6 RootedTree (jebl.evolution.trees.RootedTree)6 ImportException (jebl.evolution.io.ImportException)5 FileReader (java.io.FileReader)3 IOException (java.io.IOException)2 Arguments (dr.app.util.Arguments)1 LogFileTraces (dr.inference.trace.LogFileTraces)1 Variate (dr.stats.Variate)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 EmpiricalDemographicFunction (jebl.evolution.coalescent.EmpiricalDemographicFunction)1 Node (jebl.evolution.graphs.Node)1 NewickExporter (jebl.evolution.io.NewickExporter)1 NewickImporter (jebl.evolution.io.NewickImporter)1 CoalescentIntervalGenerator (jebl.evolution.treesimulation.CoalescentIntervalGenerator)1 IntervalGenerator (jebl.evolution.treesimulation.IntervalGenerator)1 TreeSimulator (jebl.evolution.treesimulation.TreeSimulator)1