Search in sources :

Example 6 with TreeImporter

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

the class TreeAnnotator method summarizeTrees.

private Tree summarizeTrees(int burnin, CladeSystem cladeSystem, String inputFileName) throws /*, boolean useSumCladeCredibility */
IOException {
    Tree bestTree = null;
    double bestScore = Double.NEGATIVE_INFINITY;
    progressStream.println("Analyzing " + totalTreesUsed + " trees...");
    progressStream.println("0              25             50             75            100");
    progressStream.println("|--------------|--------------|--------------|--------------|");
    int stepSize = totalTrees / 60;
    if (stepSize < 1)
        stepSize = 1;
    int counter = 0;
    int bestTreeNumber = 0;
    TreeImporter importer = new NexusImporter(new FileReader(inputFileName), true);
    try {
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (counter >= burnin) {
                double score = scoreTree(tree, cladeSystem);
                //                    progressStream.println(score);
                if (score > bestScore) {
                    bestTree = tree;
                    bestScore = score;
                    bestTreeNumber = counter + 1;
                }
            }
            if (counter > 0 && counter % stepSize == 0) {
                progressStream.print("*");
                progressStream.flush();
            }
            counter++;
        }
    } catch (Importer.ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
        return null;
    }
    progressStream.println();
    progressStream.println();
    progressStream.println("Best tree: " + bestTree.getId() + " (tree number " + bestTreeNumber + ")");
    //        if (useSumCladeCredibility) {
    //            progressStream.println("Highest Sum Clade Credibility: " + bestScore);
    //        } else {
    progressStream.println("Highest Log Clade Credibility: " + bestScore);
    return bestTree;
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TreeImporter(dr.evolution.io.TreeImporter) FileReader(java.io.FileReader) NexusImporter(dr.evolution.io.NexusImporter) NewickImporter(dr.evolution.io.NewickImporter) Importer(dr.evolution.io.Importer) TreeImporter(dr.evolution.io.TreeImporter)

Example 7 with TreeImporter

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

the class TreeLengthFinder method report.

/**
     * Recursively analyzes trees files.
     *
     * @param name       the file to analyze (if this is a directory then the files within it are analyzed)
     * @param burnin     the burnin to use
     */
private void report(String name, int burnin) {
    double treeLength = 0.0;
    int count = 0;
    try {
        FileReader fileReader = new FileReader(new File(name));
        TreeImporter importer = new NexusImporter(fileReader);
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (count >= burnin) {
                treeLength += TreeLength.FACTORY.createStatistic().getSummaryStatistic(tree)[0];
            }
            count++;
        }
        treeLength /= (count - burnin);
        System.out.println(name + "\t" + burnin + "\t" + treeLength);
    } catch (Importer.ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
    } catch (IOException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
    }
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TreeImporter(dr.evolution.io.TreeImporter) Tree(dr.evolution.tree.Tree) NexusImporter(dr.evolution.io.NexusImporter) TreeImporter(dr.evolution.io.TreeImporter) Importer(dr.evolution.io.Importer)

Example 8 with TreeImporter

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

the class TreeSpaceFrame method processTrees1.

private int processTrees1(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;
    CladeSystem cladeSystem = document.getCladeSystem();
    FileReader fileReader = new FileReader(inputFile.getFile());
    TreeImporter importer = new dr.evolution.io.NexusImporter(fileReader);
    try {
        totalTrees = 0;
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (totalTrees >= inputFile.getBurnin()) {
                cladeSystem.add(tree, true);
                totalTreesUsed += 1;
            }
            if (totalTrees > 0 && totalTrees % stepSize == 0) {
                progressStream.print("*");
                progressStream.flush();
            }
            totalTrees++;
        }
    } catch (Importer.ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
        return 0;
    }
    fileReader.close();
    progressStream.println();
    progressStream.println();
    if (totalTrees < 1) {
        System.err.println("No trees");
        return 0;
    }
    if (totalTreesUsed <= 1) {
        if (inputFile.getBurnin() > 0) {
            System.err.println("No trees to use: burnin too high");
            return 0;
        }
    }
    cladeSystem.normalizeClades(totalTreesUsed);
    progressStream.println("Total trees read: " + totalTrees);
    if (inputFile.getBurnin() > 0) {
        progressStream.println("Ignoring first " + inputFile.getBurnin() + " trees.");
    }
    int cladeCount = cladeSystem.getCladeMap().keySet().size();
    progressStream.println("Total unique clades: " + cladeCount);
    progressStream.println();
    progressStream.println("Processing trees for correlated clades:");
    fileReader = new FileReader(inputFile.getFile());
    importer = new dr.evolution.io.NexusImporter(fileReader);
    try {
        totalTrees = 0;
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (totalTrees >= inputFile.getBurnin()) {
                cladeSystem.addCooccurances(tree);
            }
            if (totalTrees > 0 && totalTrees % stepSize == 0) {
                progressStream.print("*");
                progressStream.flush();
            }
            totalTrees++;
        }
    } catch (Importer.ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
        return 0;
    }
    fileReader.close();
    progressStream.println();
    progressStream.println();
    double THRESHOLD = 0.05;
    PrintWriter writer = new PrintWriter("clade_co-occurance.txt");
    writer.println("source\tsize\ttarget\tco-occurence");
    java.util.List<CladeSystem.Clade> allClades = cladeSystem.getClades();
    for (CladeSystem.Clade clade1 : allClades) {
        String name1;
        int card1 = clade1.bits.cardinality();
        if (card1 == 1) {
            name1 = clade1.label;
        } else {
            name1 = "clade" + (clade1.index + 1);
        }
        if (clade1.parents != null) {
            for (CladeSystem.Clade clade2 : clade1.parents.keySet()) {
                String name2;
                int card2 = clade2.bits.cardinality();
                name2 = "clade" + (clade2.index + 1);
                double value = clade1.parents.get(clade2);
                value /= totalTreesUsed;
                if (value > THRESHOLD) {
                    if (card1 > card2) {
                        writer.println(name1 + "_" + card1 + "\t" + card1 + "\t" + name2 + "_" + card2 + "\t" + value);
                    } else {
                        writer.println(name2 + "_" + card2 + "\t" + card2 + "\t" + name1 + "_" + card1 + "\t" + value);
                    }
                }
            }
        }
    }
    writer.close();
    writer = new PrintWriter("clade_frequency.txt");
    writer.println("source\tsize\tfrequency");
    for (CladeSystem.Clade clade1 : allClades) {
        String name1;
        int card1 = clade1.bits.cardinality();
        if (card1 == 1) {
            name1 = clade1.label;
        } else {
            name1 = "clade" + (clade1.index + 1);
        }
        double value = clade1.count;
        value /= totalTreesUsed;
        if (value > THRESHOLD) {
            writer.println(name1 + "_" + card1 + "\t" + card1 + "\t" + value);
        }
    }
    writer.close();
    progressStream.println();
    cladePlotter.setCladeSystem(cladeSystem);
    return totalTreesUsed;
}
Also used : NexusImporter(jebl.evolution.io.NexusImporter) java.io(java.io) TreeImporter(dr.evolution.io.TreeImporter) RootedTree(jebl.evolution.trees.RootedTree) Tree(dr.evolution.tree.Tree) Importer(dr.evolution.io.Importer) TreeImporter(dr.evolution.io.TreeImporter) NexusImporter(jebl.evolution.io.NexusImporter)

Example 9 with TreeImporter

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

the class GetNSCountsFromTrees method analyze.

private void analyze(File inputFile, int burnin, BranchSet branchSet, List<Set> inclusionSets, List<Set> exclusionSets, double[] siteList) {
    if (summary) {
        resultsStream.print("tree" + SEP + "cN" + SEP + "uN" + SEP + "cS" + SEP + "uS" + SEP + "cNrate" + SEP + "cSrate" + SEP + "dN/dS" + "\n");
    } else {
        resultsStream.print("tree" + SEP + "branch" + SEP + "N/S" + SEP + "site" + SEP + "height/date" + SEP + "fromState" + SEP + "toState");
        if (branchInfo) {
            resultsStream.print(SEP + "branchLength" + SEP + "branchCN/S" + SEP + "branchUN/S" + "\n");
        } else {
            resultsStream.print("\n");
        }
    }
    int totalTrees = 10000;
    int totalStars = 0;
    System.out.println("Reading and analyzing trees (bar assumes 10,000 trees)...");
    System.out.println("0              25             50             75            100");
    System.out.println("|--------------|--------------|--------------|--------------|");
    int stepSize = totalTrees / 60;
    if (stepSize < 1)
        stepSize = 1;
    int count = 0;
    int treeUsed = 1;
    try {
        FileReader fileReader = new FileReader(inputFile);
        TreeImporter importer = new NexusImporter(fileReader);
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (count >= burnin) {
                getNSCounts(tree, treeUsed, branchSet, inclusionSets, exclusionSets, siteList);
                treeUsed++;
            }
            count++;
            if (totalTrees > 0 && totalTrees % stepSize == 0) {
                System.out.print("*");
                totalStars++;
                if (totalStars % 61 == 0)
                    System.out.print("\n");
                System.out.flush();
            }
            totalTrees++;
        }
        System.out.print("\n");
    } catch (Importer.ImportException e) {
        progressStream.println("Error Parsing Input Tree: " + e.getMessage());
    } catch (IOException e) {
        progressStream.println("Error Parsing Input Tree: " + e.getMessage());
    }
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TreeImporter(dr.evolution.io.TreeImporter) Tree(dr.evolution.tree.Tree) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer) TreeImporter(dr.evolution.io.TreeImporter)

Example 10 with TreeImporter

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

TreeImporter (dr.evolution.io.TreeImporter)12 NexusImporter (dr.evolution.io.NexusImporter)10 Importer (dr.evolution.io.Importer)8 Tree (dr.evolution.tree.Tree)8 NewickImporter (dr.evolution.io.NewickImporter)6 FileReader (java.io.FileReader)3 ArrayList (java.util.ArrayList)2 JTreeDisplay (dr.app.gui.tree.JTreeDisplay)1 JTreePanel (dr.app.gui.tree.JTreePanel)1 SquareTreePainter (dr.app.gui.tree.SquareTreePainter)1 TreeSummaryStatistic (dr.app.treestat.statistics.TreeSummaryStatistic)1 TreeExporter (dr.evolution.io.TreeExporter)1 FlexibleTree (dr.evolution.tree.FlexibleTree)1 NodeRef (dr.evolution.tree.NodeRef)1 GammaSiteModel (dr.oldevomodel.sitemodel.GammaSiteModel)1 SiteModel (dr.oldevomodel.sitemodel.SiteModel)1 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)1 HKY (dr.oldevomodel.substmodel.HKY)1 java.awt (java.awt)1 PrinterJob (java.awt.print.PrinterJob)1