Search in sources :

Example 6 with NexusImporter

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

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

Example 8 with NexusImporter

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

the class LineageCountThroughTime method getLTT.

public static Variate[] getLTT(String treeFile, double minTime, double maxTime, int binCount, // number of trees to skip
int skip) throws IOException, ImportException {
    double delta = (maxTime - minTime) / (binCount - 1);
    BufferedReader reader = new BufferedReader(new FileReader(treeFile));
    String line = reader.readLine();
    TreeImporter importer;
    if (line.toUpperCase().startsWith("#NEXUS")) {
        importer = new NexusImporter(reader);
    } else {
        importer = new NewickImporter(reader, false);
    }
    int state = 0;
    while (importer.hasTree() && state < skip) {
        importer.importNextTree();
        state += 1;
    }
    // the age of the end of this group
    List<double[]> branchingTimes = new ArrayList<double[]>();
    state = 0;
    while (importer.hasTree()) {
        RootedTree tree = (RootedTree) importer.importNextTree();
        double[] bt = new double[tree.getInternalNodes().size()];
        int i = 0;
        for (Node node : tree.getInternalNodes()) {
            bt[i] = tree.getHeight(node);
            i++;
        }
        Arrays.sort(bt);
        branchingTimes.add(bt);
        state += 1;
    }
    Variate[] bins = new Variate[binCount];
    double height = 0;
    double n = branchingTimes.get(0).length;
    for (int k = 0; k < binCount; k++) {
        bins[k] = new Variate.D();
        if (height >= 0.0 && height <= maxTime) {
            for (state = 0; state < branchingTimes.size(); state++) {
                int index = 0;
                while (index < branchingTimes.get(state).length && branchingTimes.get(state)[index] < height) {
                    index += 1;
                }
                double lineageCount = 1;
                if (index < branchingTimes.get(state).length) {
                    lineageCount = n - index + 1;
                }
                bins[k].add(lineageCount);
            }
        }
        height += delta;
    }
    Variate xData = new Variate.D();
    Variate yDataMean = new Variate.D();
    Variate yDataMedian = new Variate.D();
    Variate yDataUpper = new Variate.D();
    Variate yDataLower = new Variate.D();
    double t = minTime;
    for (Variate bin : bins) {
        xData.add(t);
        if (bin.getCount() > 0) {
            yDataMean.add(bin.getMean());
            yDataMedian.add(bin.getQuantile(0.5));
            yDataLower.add(bin.getQuantile(0.025));
            yDataUpper.add(bin.getQuantile(0.975));
        } else {
            yDataMean.add(Double.NaN);
            yDataMedian.add(Double.NaN);
            yDataLower.add(Double.NaN);
            yDataUpper.add(Double.NaN);
        }
        t += delta;
    }
    return new Variate[] { xData, yDataMean };
}
Also used : NexusImporter(jebl.evolution.io.NexusImporter) Variate(dr.stats.Variate) Node(jebl.evolution.graphs.Node) ArrayList(java.util.ArrayList) RootedTree(jebl.evolution.trees.RootedTree) NewickImporter(jebl.evolution.io.NewickImporter) BufferedReader(java.io.BufferedReader) TreeImporter(jebl.evolution.io.TreeImporter) FileReader(java.io.FileReader)

Aggregations

NexusImporter (jebl.evolution.io.NexusImporter)8 RootedTree (jebl.evolution.trees.RootedTree)8 ImportException (jebl.evolution.io.ImportException)6 TreeImporter (jebl.evolution.io.TreeImporter)6 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 java.io (java.io)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