Search in sources :

Example 1 with TreeSummaryStatistic

use of dr.app.treestat.statistics.TreeSummaryStatistic in project beast-mcmc by beast-dev.

the class TreeStatFrame method processTreeFile.

protected void processTreeFile(File inFile, File outFile) throws IOException, Importer.ImportException {
    processTreeFileAction.setEnabled(false);
    BufferedReader r = new BufferedReader(new FileReader(inFile));
    String line = r.readLine();
    r.close();
    final ProgressMonitorInputStream in = new ProgressMonitorInputStream(this, "Reading " + inFile.getName(), new FileInputStream(inFile));
    in.getProgressMonitor().setMillisToDecideToPopup(0);
    in.getProgressMonitor().setMillisToPopup(0);
    final Reader reader = new InputStreamReader(new BufferedInputStream(in));
    //        final Reader reader = new FileReader(inFile);
    final TreeImporter importer;
    if (line.toUpperCase().startsWith("#NEXUS")) {
        importer = new NexusImporter(reader);
    } else {
        reader.close();
        importer = new NewickImporter(reader);
    }
    final Tree firstTree = importer.importNextTree();
    boolean isUltrametric = TreeUtils.isUltrametric(firstTree);
    boolean isBinary = TreeUtils.isBinary(firstTree);
    boolean stop = false;
    // check that the trees conform with the requirements of the selected statistics
    for (int i = 0; i < treeStatData.statistics.size(); i++) {
        TreeSummaryStatistic tss = (TreeSummaryStatistic) treeStatData.statistics.get(i);
        String label = tss.getSummaryStatisticName();
        if (!isUltrametric && !tss.allowsNonultrametricTrees()) {
            if (JOptionPane.showConfirmDialog(this, "Warning: These trees may not be ultrametric and this is\na requirement of the " + label + " statistic. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                stop = true;
                break;
            }
            // don't ask the question again...
            isUltrametric = true;
        }
        if (!isBinary && !tss.allowsPolytomies()) {
            if (JOptionPane.showConfirmDialog(this, "Warning: These trees may not be strictly bifurcating and this is\na requirement of the " + label + " statistic. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                stop = true;
                break;
            }
            // don't ask the question again...
            isBinary = true;
        }
    }
    if (stop) {
        processTreeFileAction.setEnabled(true);
        return;
    }
    final PrintWriter writer = new PrintWriter(new FileWriter(outFile));
    //        Thread readThread = new Thread() {
    //            public void run() {
    Tree tree = firstTree;
    writer.print("state");
    for (int i = 0; i < treeStatData.statistics.size(); i++) {
        TreeSummaryStatistic tss = (TreeSummaryStatistic) treeStatData.statistics.get(i);
        int dim = tss.getStatisticDimensions(tree);
        for (int j = 0; j < dim; j++) {
            writer.print("\t" + tss.getStatisticLabel(tree, j));
        }
    }
    writer.println();
    state = 0;
    do {
        writer.print(state);
        for (int i = 0; i < treeStatData.statistics.size(); i++) {
            TreeSummaryStatistic tss = (TreeSummaryStatistic) treeStatData.statistics.get(i);
            double[] stats = tss.getSummaryStatistic(tree);
            for (int j = 0; j < stats.length; j++) {
                writer.print("\t" + stats[j]);
            }
        }
        writer.println();
        state += 1;
        final int currentState = state;
        in.getProgressMonitor().setNote("Processing Tree " + currentState + "...");
        //                    EventQueue.invokeLater(
        //                            new Runnable() {
        //                                public void run() {
        //                                    progressLabel.setText("Processing Tree " + currentState + "...");
        //                                }
        //                            });
        //                    try {
        tree = importer.importNextTree();
    //                    } catch (final IOException e) {
    //                        EventQueue.invokeLater(
    //                                new Runnable() {
    //                                    public void run() {
    //                                        JOptionPane.showMessageDialog(TreeStatFrame.this, "File I/O Error: " + e.getMessage(),
    //                                                "File I/O Error",
    //                                                JOptionPane.ERROR_MESSAGE);
    //                                    }
    //                                });
    //                    } catch (final Importer.ImportException e) {
    //                        EventQueue.invokeLater(
    //                                new Runnable() {
    //                                    public void run() {
    //                                        JOptionPane.showMessageDialog(TreeStatFrame.this, "Error importing tree: " + e.getMessage(),
    //                                                "Tree Import Error",
    //                                                JOptionPane.ERROR_MESSAGE);
    //                                    }
    //                                });
    //                    }
    } while (tree != null);
    //            }
    //        };
    //
    //        readThread.start();
    //        while (readThread.isAlive()) {
    //            Thread.yield();
    //        }
    reader.close();
    writer.close();
    progressLabel.setText("" + state + " trees processed.");
    processTreeFileAction.setEnabled(true);
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TreeSummaryStatistic(dr.app.treestat.statistics.TreeSummaryStatistic) NewickImporter(dr.evolution.io.NewickImporter) TreeImporter(dr.evolution.io.TreeImporter) Tree(dr.evolution.tree.Tree)

Aggregations

TreeSummaryStatistic (dr.app.treestat.statistics.TreeSummaryStatistic)1 NewickImporter (dr.evolution.io.NewickImporter)1 NexusImporter (dr.evolution.io.NexusImporter)1 TreeImporter (dr.evolution.io.TreeImporter)1 Tree (dr.evolution.tree.Tree)1