Search in sources :

Example 1 with NexusImporter

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

the class TimeSlicer method readAndAnalyzeTrees.

// private List<Tree> importTrees(String treeFileName, int burnin) throws IOException, Importer.ImportException {
// 
// int totalTrees = 10000;
// 
// 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;
// 
// List<Tree> treeList = new ArrayList<Tree>();
// BufferedReader reader1 = new BufferedReader(new FileReader(treeFileName));
// 
// String line1 = reader1.readLine();
// TreeImporter importer1;
// if (line1.toUpperCase().startsWith("#NEXUS")) {
// importer1 = new NexusImporter(new FileReader(treeFileName));
// } else {
// importer1 = new NewickImporter(new FileReader(treeFileName));
// }
// totalTrees = 0;
// while (importer1.hasTree()) {
// Tree treeTime = importer1.importNextTree();
// 
// if (totalTrees > burnin)
// treeList.add(treeTime);
// 
// if (totalTrees > 0 && totalTrees % stepSize == 0) {
// progressStream.print("*");
// progressStream.flush();
// }
// totalTrees++;
// }
// return treeList;
// }
private void readAndAnalyzeTrees(String treeFileName, int burnin, int skipEvery, String[] traits, double[] slices, boolean impute, boolean trueNoise, Normalization normalize, boolean divideByBranchLength, BranchSet branchset, Set taxaSet) throws IOException, Importer.ImportException {
    int totalTrees = 10000;
    int totalStars = 0;
    progressStream.println("Reading and analyzing trees (bar assumes 10,000 trees)...");
    progressStream.println("0              25             50             75            100");
    progressStream.println("|--------------|--------------|--------------|--------------|");
    int stepSize = totalTrees / 60;
    if (stepSize < 1)
        stepSize = 1;
    BufferedReader reader1 = new BufferedReader(new FileReader(treeFileName));
    String line1 = reader1.readLine();
    TreeImporter importer1;
    if (line1.toUpperCase().startsWith("#NEXUS")) {
        importer1 = new NexusImporter(new FileReader(treeFileName));
    } else {
        importer1 = new NewickImporter(new FileReader(treeFileName));
    }
    totalTrees = 0;
    while (importer1.hasTree()) {
        Tree treeTime = importer1.importNextTree();
        if (totalTrees % skipEvery == 0) {
            treesRead++;
            if (totalTrees >= burnin) {
                analyzeTree(treeTime, traits, slices, impute, trueNoise, normalize, divideByBranchLength, branchset, taxaSet);
            }
        }
        if (totalTrees > 0 && totalTrees % stepSize == 0) {
            progressStream.print("*");
            totalStars++;
            if (totalStars % 61 == 0)
                progressStream.print("\n");
            progressStream.flush();
        }
        totalTrees++;
    }
    progressStream.print("\n");
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) NewickImporter(dr.evolution.io.NewickImporter) TreeImporter(dr.evolution.io.TreeImporter) Tree(dr.evolution.tree.Tree)

Example 2 with NexusImporter

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

the class SpeciesDelimitationAnalyser method readtrees.

void readtrees() throws IOException {
    clusters = new ArrayList<Cluster>(0);
    int totalTrees;
    FileReader fileReader = new FileReader(inputFileName);
    NexusImporter importer = new NexusImporter(fileReader);
    System.out.println("Reading trees...");
    try {
        taxonList = importer.parseTaxaBlock();
        totalTrees = 0;
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (totalTrees >= burnin) {
                Cluster sd = new Cluster(taxonList, tree, collapseheight);
                clusters.add(sd);
            }
            if (totalTrees > 0 && (totalTrees % 100 == 0)) {
                System.out.print("*");
                System.out.flush();
            }
            if (totalTrees > 0 && (totalTrees % 5000 == 0)) {
                System.out.println(" " + totalTrees);
                System.out.flush();
            }
            totalTrees++;
        }
        System.out.println("");
    } catch (Importer.ImportException e) {
        System.err.println("Error Parsing Input Tree: " + e.getMessage());
        return;
    }
    fileReader.close();
    if (totalTrees < 1) {
        System.err.println("No trees");
        return;
    }
    System.out.println("Total trees read: " + totalTrees);
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) Tree(dr.evolution.tree.Tree) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Example 3 with NexusImporter

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

the class NodeStateAnalyser 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 mrsd, double scale) {
    int count = 0;
    Map<String, List<Double>> heightMap = new HashMap<String, List<Double>>();
    Set<String> states = new TreeSet<String>();
    try {
        FileReader fileReader = new FileReader(new File(name));
        TreeImporter importer = new NexusImporter(fileReader);
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (count >= burnin) {
                for (int i = 0; i < tree.getInternalNodeCount(); i++) {
                    NodeRef node = tree.getInternalNode(i);
                    Object value = tree.getNodeAttribute(node, "state");
                    if (value != null) {
                        String state = value.toString();
                        List<Double> heights = heightMap.get(state);
                        if (heights == null) {
                            heights = new ArrayList<Double>();
                            heightMap.put(state, heights);
                            states.add(state);
                        }
                        double h = tree.getNodeHeight(node) * scale;
                        if (Double.isNaN(mrsd)) {
                            heights.add(h);
                        } else {
                            heights.add(mrsd - h);
                        }
                    } else {
                        System.out.println("Node missing state");
                    }
                }
            }
            count++;
        }
        boolean first = true;
        int maxCount = 0;
        for (String state : states) {
            if (first) {
                first = false;
            } else {
                System.out.print("\t");
            }
            System.out.print(state);
            List<Double> heights = heightMap.get(state);
            if (heights.size() > maxCount) {
                maxCount = heights.size();
            }
        }
        System.out.println();
        for (int i = 0; i < maxCount; i++) {
            first = true;
            for (String state : states) {
                if (first) {
                    first = false;
                } else {
                    System.out.print("\t");
                }
                List<Double> heights = heightMap.get(state);
                if (i < heights.size()) {
                    System.out.print(heights.get(i));
                }
            }
            System.out.println();
        }
    } 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) NodeRef(dr.evolution.tree.NodeRef) 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 4 with NexusImporter

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

the class TreeSummary method summarizeTrees.

private Tree summarizeTrees(int burnin, Tree consensusTree, CladeSystem cladeSystem, String inputFileName) throws IOException {
    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));
    try {
        while (importer.hasTree()) {
            Tree tree = importer.importNextTree();
            if (counter >= burnin) {
                cladeSystem.addSubTrees(tree);
            }
            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;
    }
    Tree bestTree = cladeSystem.getBestTree(consensusTree);
    return bestTree;
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TreeImporter(dr.evolution.io.TreeImporter) FileReader(java.io.FileReader) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer) TreeImporter(dr.evolution.io.TreeImporter)

Example 5 with NexusImporter

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

the class Defects method readNexusFile.

/**
 * @param fileName
 * @throws java.io.IOException
 */
private static Alignment readNexusFile(String fileName) throws java.io.IOException {
    Alignment alignment = null;
    TaxonList taxonList = null;
    try {
        FileReader reader = new FileReader(fileName);
        NexusImporter importer = new NexusImporter(reader);
        boolean done = false;
        while (!done) {
            try {
                NexusImporter.NexusBlock block = importer.findNextBlock();
                if (block == NexusImporter.TAXA_BLOCK) {
                    if (taxonList != null) {
                        throw new NexusImporter.MissingBlockException("TAXA block already defined");
                    }
                    taxonList = importer.parseTaxaBlock();
                } else if (block == NexusImporter.DATA_BLOCK) {
                    // A data block doesn't need a taxon block before it
                    // but if one exists then it will use it.
                    alignment = importer.parseDataBlock(taxonList);
                    if (taxonList == null) {
                        taxonList = alignment;
                    }
                } else if (block == NexusImporter.TREES_BLOCK) {
                // ignore tree block
                } else {
                // Ignore the block..
                }
            } catch (EOFException ex) {
                done = true;
            }
        }
    } catch (Importer.ImportException ime) {
        System.err.println("Error reading alignment: " + ime);
    }
    return alignment;
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TaxonList(dr.evolution.util.TaxonList) EOFException(java.io.EOFException) FileReader(java.io.FileReader) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Aggregations

NexusImporter (dr.evolution.io.NexusImporter)32 Tree (dr.evolution.tree.Tree)19 Importer (dr.evolution.io.Importer)15 TreeImporter (dr.evolution.io.TreeImporter)13 FileReader (java.io.FileReader)13 NewickImporter (dr.evolution.io.NewickImporter)10 TaxonList (dr.evolution.util.TaxonList)5 FlexibleTree (dr.evolution.tree.FlexibleTree)4 BufferedReader (java.io.BufferedReader)3 Parameter (dr.inference.model.Parameter)2 FileOutputStream (java.io.FileOutputStream)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)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 Alignment (dr.evolution.alignment.Alignment)1 ExtractPairs (dr.evolution.alignment.ExtractPairs)1