Search in sources :

Example 1 with TreeExporter

use of dr.evolution.io.TreeExporter in project beast-mcmc by beast-dev.

the class GetAncestralSequenceFromSplitTrait method analyze.

/**
     * Recursively analyzes log files.
     *
     * @param treeAnnotatorInputFile       the file to analyze (if this is a directory then the files within it are analyzed)
     * @throws dr.inference.trace.TraceException
     *          if the trace file is in the wrong format or corrupted
     */
private void analyze(File treeAnnotatorInputFile) throws TraceException {
    try {
        FileReader fileReader = new FileReader(treeAnnotatorInputFile);
        TreeImporter importer = new NexusImporter(fileReader);
        FlexibleTree tree = (FlexibleTree) importer.importNextTree();
        for (int i = 0; i < tree.getNodeCount(); i++) {
            Hashtable<Integer, State> states = new Hashtable<Integer, State>();
            for (Iterator<String> j = tree.getNodeAttributeNames(tree.getNode(i)); j.hasNext(); ) {
                String name = j.next();
                if (name.indexOf("states_") >= 0) {
                    Integer d = Integer.parseInt(name.replaceFirst("states_", "").replaceFirst("\\..+", ""));
                    //= new State(name.);
                    State s;
                    if (states.containsKey(d)) {
                        s = states.get(d);
                    } else {
                        s = new State(d);
                    }
                    if (name.matches("states_" + d + ".prob")) {
                        Object o = tree.getNodeAttribute(tree.getNode(i), name);
                        double probability = (Double) o;
                        s.setProbability(probability);
                    } else if (name.matches("states_" + d)) {
                        Object o = tree.getNodeAttribute(tree.getNode(i), name);
                        String value = (String) o;
                        s.setState(value.replaceAll("\"", ""));
                    } else if (name.matches("states_" + d + ".set.prob")) {
                        /* Not necessary but lets parse it anyways */
                        Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name);
                        double[] probabilities = new double[o.length];
                        for (int k = 0; k < o.length; k++) {
                            probabilities[k] = (Double) o[k];
                        }
                        s.setProbabilities(probabilities);
                    } else if (name.matches("states_" + d + ".set")) {
                        /* Not necessary but lets parse it anyways */
                        Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name);
                        String[] set = new String[o.length];
                        for (int k = 0; k < o.length; k++) {
                            set[k] = ((String) o[k]).replaceAll("\"", "");
                        }
                        s.setSet(set);
                    }
                    //                        }
                    states.put(d, s);
                }
            }
            State[] statesArray = states.values().toArray(new State[states.size()]);
            Arrays.sort(statesArray);
            /* Set the default length to the number of characters that it would need */
            StringBuffer sb = new StringBuffer(statesArray.length * statesArray[0].getState().length());
            for (State s : statesArray) {
                sb.append(s.getState());
            }
            tree.setNodeAttribute(tree.getNode(i), "seq", sb.toString());
        }
        /* Export the new tree with the new sequences */
        TreeExporter exporter = new NexusExporter(System.out);
        exporter.exportTree(tree);
        System.out.println("Begin trees;");
        System.out.println("\ttree max_tree = " + tree.toString());
        System.out.println("End;");
    } catch (IOException e) {
        System.err.println("Error Parsing Input log: " + e.getMessage());
    } catch (Importer.ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
    }
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) FlexibleTree(dr.evolution.tree.FlexibleTree) TreeExporter(dr.evolution.io.TreeExporter) TreeImporter(dr.evolution.io.TreeImporter) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer) TreeImporter(dr.evolution.io.TreeImporter)

Aggregations

Importer (dr.evolution.io.Importer)1 NexusImporter (dr.evolution.io.NexusImporter)1 TreeExporter (dr.evolution.io.TreeExporter)1 TreeImporter (dr.evolution.io.TreeImporter)1 FlexibleTree (dr.evolution.tree.FlexibleTree)1