Search in sources :

Example 11 with Importer

use of dr.evolution.io.Importer 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 12 with Importer

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

the class DebugTreeImporter method main.

//args[0]: saved dump file
//args[1]: new XML file
//args[2]: new tree (Newick format)
//args[3]: output file
//e.g.: dump.trump.160 worobey-con-sc-resume-162-taxa.xml newtree.nwk restart.dump
public static void main(String[] args) {
    if (args.length != 4) {
        throw new RuntimeException("Incorrect number of arguments.");
    }
    try {
        FileReader fileIn = new FileReader(args[2]);
        BufferedReader in = new BufferedReader(fileIn);
        String fullTree = in.readLine();
        NewickImporter importer = new NewickImporter(fullTree);
        Tree tree = importer.importNextTree();
        //assume rootHeight in dumpfile is ALWAYS followed by all the nodeHeights
        System.out.println("root height: " + tree.getNodeHeight(tree.getRoot()));
        FileReader dumpFileIn = new FileReader(args[0]);
        BufferedReader dumpIn = new BufferedReader(dumpFileIn);
        FileWriter dumpFileOut = new FileWriter(args[3]);
        BufferedWriter dumpOut = new BufferedWriter(dumpFileOut);
        String original = dumpIn.readLine();
        String[] fields = original.split("\t");
        //write new rootHeight to new output dump file
        while (!fields[0].equals("treeModel.rootHeight")) {
            if (DEBUG) {
                System.out.println(original);
            }
            dumpOut.write(original + "\n");
            original = dumpIn.readLine();
            fields = original.split("\t");
        }
        fields[2] = Double.toString(tree.getNodeHeight(tree.getRoot()));
        for (int i = 0; i < fields.length; i++) {
            dumpOut.write(fields[i] + "\t");
        }
        dumpOut.write("\n");
        //write all new node heights to new output dump file
        int nodeCount = tree.getNodeCount();
        if (DEBUG) {
            System.out.println(nodeCount + " nodes found in tree.");
        }
        for (int i = 0; i < (nodeCount - 1); i++) {
            if (DEBUG) {
                System.out.println(tree.getNode(i).getNumber() + "\t" + tree.getNodeHeight(tree.getNode(i)));
            }
            dumpOut.write(tree.getNode(i).getNumber() + "\t1\t" + tree.getNodeHeight(tree.getNode(i)) + "\n");
        }
        //skip all the node heights in the original dump file
        //no clue as to how many there are ...
        //best I can tell only node heights have integers as parameter names
        original = dumpIn.readLine();
        if (DEBUG) {
            System.out.println(original);
        }
        fields = original.split("\t");
        while (isInteger(fields[0])) {
            original = dumpIn.readLine();
            fields = original.split("\t");
        }
        while (!fields[0].equals("treeModel")) {
            dumpOut.write(original + "\n");
            original = dumpIn.readLine();
            if (DEBUG) {
                System.out.println(original);
            }
            fields = original.split("\t");
        }
        dumpOut.write(fields[0] + "\t" + fullTree);
        dumpOut.flush();
        dumpOut.close();
    } catch (FileNotFoundException fnfe) {
        throw new RuntimeException("Tree file not found.");
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to read file: " + ioe.getMessage());
    } catch (Importer.ImportException ie) {
        throw new RuntimeException("Unable to import tree: " + ie.getMessage());
    }
}
Also used : NewickImporter(dr.evolution.io.NewickImporter) Tree(dr.evolution.tree.Tree) Importer(dr.evolution.io.Importer) NewickImporter(dr.evolution.io.NewickImporter)

Example 13 with Importer

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

the class BeautiTesterConfig method importFromFile.

public void importFromFile(String fileName, BeautiOptions options, boolean translate) {
    TaxonList taxa = null;
    Alignment alignment = null;
    Tree tree = null;
    PartitionSubstitutionModel model = null;
    java.util.List<NexusApplicationImporter.CharSet> charSets = new ArrayList<NexusApplicationImporter.CharSet>();
    try {
        FileReader reader = new FileReader(fileName);
        NexusApplicationImporter importer = new NexusApplicationImporter(reader);
        boolean done = false;
        while (!done) {
            try {
                NexusImporter.NexusBlock block = importer.findNextBlock();
                if (block == NexusImporter.TAXA_BLOCK) {
                    if (taxa != null) {
                        throw new NexusImporter.MissingBlockException("TAXA block already defined");
                    }
                    taxa = importer.parseTaxaBlock();
                } else if (block == NexusImporter.CALIBRATION_BLOCK) {
                    if (taxa == null) {
                        throw new NexusImporter.MissingBlockException("TAXA or DATA block must be defined before a CALIBRATION block");
                    }
                    importer.parseCalibrationBlock(taxa);
                } else if (block == NexusImporter.CHARACTERS_BLOCK) {
                    if (taxa == null) {
                        throw new NexusImporter.MissingBlockException("TAXA block must be defined before a CHARACTERS block");
                    }
                    if (alignment != null) {
                        throw new NexusImporter.MissingBlockException("CHARACTERS or DATA block already defined");
                    }
                    alignment = importer.parseCharactersBlock(options.taxonList);
                } else if (block == NexusImporter.DATA_BLOCK) {
                    if (alignment != null) {
                        throw new NexusImporter.MissingBlockException("CHARACTERS or DATA block already defined");
                    }
                    // A data block doesn't need a taxon block before it
                    // but if one exists then it will use it.
                    alignment = importer.parseDataBlock(options.taxonList);
                    if (taxa == null) {
                        taxa = alignment;
                    }
                } else if (block == NexusImporter.TREES_BLOCK) {
                    if (taxa == null) {
                        throw new NexusImporter.MissingBlockException("TAXA or DATA block must be defined before a TREES block");
                    }
                    if (tree != null) {
                        throw new NexusImporter.MissingBlockException("TREES block already defined");
                    }
                    Tree[] trees = importer.parseTreesBlock(taxa);
                    if (trees.length > 0) {
                        tree = trees[0];
                    }
                } else if (block == NexusApplicationImporter.PAUP_BLOCK) {
                    model = importer.parsePAUPBlock(options, charSets);
                } else if (block == NexusApplicationImporter.MRBAYES_BLOCK) {
                    model = importer.parseMrBayesBlock(options, charSets);
                } else if (block == NexusApplicationImporter.ASSUMPTIONS_BLOCK) {
                    importer.parseAssumptionsBlock(charSets);
                } else {
                // Ignore the block..
                }
            } catch (EOFException ex) {
                done = true;
            }
        }
        // Allow the user to load taxa only (perhaps from a tree file) so that they can sample from a prior...
        if (alignment == null && taxa == null) {
            throw new NexusImporter.MissingBlockException("TAXON, DATA or CHARACTERS block is missing");
        }
    } catch (FileNotFoundException fnfe) {
        System.err.println("File not found: " + fnfe);
        System.exit(1);
    } catch (Importer.ImportException ime) {
        System.err.println("Error parsing imported file: " + ime);
        System.exit(1);
    } catch (IOException ioex) {
        System.err.println("File I/O Error: " + ioex);
        System.exit(1);
    } catch (Exception ex) {
        System.err.println("Fatal exception: " + ex);
        System.exit(1);
    }
    if (options.taxonList == null) {
        // This is the first partition to be loaded...
        options.taxonList = new Taxa(taxa);
        // check the taxon names for invalid characters
        boolean foundAmp = false;
        for (int i = 0; i < taxa.getTaxonCount(); i++) {
            String name = taxa.getTaxon(i).getId();
            if (name.indexOf('&') >= 0) {
                foundAmp = true;
            }
        }
        if (foundAmp) {
            System.err.println("One or more taxon names include an illegal character ('&').");
            return;
        }
        // make sure they all have dates...
        for (int i = 0; i < taxa.getTaxonCount(); i++) {
            if (taxa.getTaxonAttribute(i, "date") == null) {
                java.util.Date origin = new java.util.Date(0);
                dr.evolution.util.Date date = dr.evolution.util.Date.createTimeSinceOrigin(0.0, Units.Type.YEARS, origin);
                taxa.getTaxon(i).setAttribute("date", date);
            }
        }
    } else {
        // This is an additional partition so check it uses the same taxa
        java.util.List<String> oldTaxa = new ArrayList<String>();
        for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {
            oldTaxa.add(options.taxonList.getTaxon(i).getId());
        }
        java.util.List<String> newTaxa = new ArrayList<String>();
        for (int i = 0; i < taxa.getTaxonCount(); i++) {
            newTaxa.add(taxa.getTaxon(i).getId());
        }
        if (!(oldTaxa.containsAll(newTaxa) && oldTaxa.size() == newTaxa.size())) {
            System.err.println("This file contains different taxa from the previously loaded");
            return;
        }
    }
    String fileNameStem = dr.app.util.Utils.trimExtensions(fileName, new String[] { "NEX", "NEXUS", "TRE", "TREE" });
    if (alignment != null) {
        if (translate) {
            alignment = new ConvertAlignment(AminoAcids.INSTANCE, GeneticCode.UNIVERSAL, alignment);
        }
        java.util.List<PartitionData> partitions = new ArrayList<PartitionData>();
        if (charSets != null && charSets.size() > 0) {
            for (NexusApplicationImporter.CharSet charSet : charSets) {
                partitions.add(new PartitionData(options, charSet.getName(), fileName, new CharSetAlignment(charSet, alignment)));
            }
        } else {
            partitions.add(new PartitionData(options, fileNameStem, fileName, alignment));
        }
        for (PartitionData partition : partitions) {
            if (model != null) {
                partition.setPartitionSubstitutionModel(model);
            //                    options.addPartitionSubstitutionModel(model);
            } else {
                for (PartitionSubstitutionModel pm : options.getPartitionSubstitutionModels()) {
                    if (pm.getDataType() == alignment.getDataType()) {
                        partition.setPartitionSubstitutionModel(pm);
                    }
                }
                if (partition.getPartitionSubstitutionModel() == null) {
                    PartitionSubstitutionModel pm = new PartitionSubstitutionModel(options, partition);
                    partition.setPartitionSubstitutionModel(pm);
                //                        options.addPartitionSubstitutionModel(pm);
                }
            }
            options.dataPartitions.add(partition);
        }
    }
    calculateHeights(options);
}
Also used : ArrayList(java.util.ArrayList) NexusApplicationImporter(dr.app.beauti.util.NexusApplicationImporter) ConvertAlignment(dr.evolution.alignment.ConvertAlignment) CharSetAlignment(dr.evolution.alignment.CharSetAlignment) Alignment(dr.evolution.alignment.Alignment) PartitionData(dr.app.beauti.options.PartitionData) ConvertAlignment(dr.evolution.alignment.ConvertAlignment) Tree(dr.evolution.tree.Tree) PartitionSubstitutionModel(dr.app.beauti.options.PartitionSubstitutionModel) NexusImporter(dr.evolution.io.NexusImporter) NexusApplicationImporter(dr.app.beauti.util.NexusApplicationImporter) Importer(dr.evolution.io.Importer) NexusImporter(dr.evolution.io.NexusImporter) CharSetAlignment(dr.evolution.alignment.CharSetAlignment) dr.evolution.util(dr.evolution.util)

Example 14 with Importer

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

the class SPPathDifferenceMetric method main.

public static void main(String[] args) {
    try {
        NewickImporter importer = new NewickImporter("(('A':1.2,'B':0.8):0.5,('C':0.8,'D':1.0):1.1)");
        Tree treeOne = importer.importNextTree();
        System.out.println("tree 1: " + treeOne);
        importer = new NewickImporter("((('A':0.8,'B':1.4):0.3,'C':0.7):0.9,'D':1.0)");
        Tree treeTwo = importer.importNextTree();
        System.out.println("tree 2: " + treeTwo + "\n");
        double metric = (new SPPathDifferenceMetric().getMetric(treeOne, treeTwo));
        System.out.println("path difference = " + metric);
        //Additional test for comparing a collection of trees against a (fixed) focal tree
        SPPathDifferenceMetric fixed = new SPPathDifferenceMetric(treeOne);
        metric = fixed.getMetric(treeTwo);
        System.out.println("path difference = " + metric);
    } catch (Importer.ImportException ie) {
        System.err.println(ie);
    } catch (IOException ioe) {
        System.err.println(ioe);
    }
}
Also used : NewickImporter(dr.evolution.io.NewickImporter) IOException(java.io.IOException) Importer(dr.evolution.io.Importer) NewickImporter(dr.evolution.io.NewickImporter)

Example 15 with Importer

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

the class TreeTraceAnalysis method report.

/**
     * @param minCladeProbability clades with at least this posterior probability will be included in report.
     * @throws IOException if general I/O error occurs
     */
public void report(double minCladeProbability, double credSetProbability, int minNT) throws IOException {
    System.err.println("making report");
    final int fieldWidth = 14;
    NumberFormatter formatter = new NumberFormatter(6);
    formatter.setPadding(true);
    formatter.setFieldWidth(fieldWidth);
    final int nTreeSet = treeSet.size();
    int totalTrees = treeSet.getSumFrequency();
    System.out.println();
    System.out.println("burnIn=" + burnin);
    System.out.println("total trees used =" + totalTrees);
    System.out.println();
    System.out.println((Math.round(credSetProbability * 100.0)) + "% credible set (" + nTreeSet + " unique trees, " + totalTrees + " total):");
    System.out.println("Count\tPercent\tTree");
    int credSet = (int) (credSetProbability * totalTrees);
    int sumFreq = 0;
    int skipped = 0;
    NumberFormatter nf = new NumberFormatter(8);
    for (int i = 0; i < nTreeSet; i++) {
        final int freq = treeSet.getFrequency(i);
        boolean show = true;
        if (minNT > 0 && freq <= minNT) {
            show = false;
            skipped += 1;
        }
        final double prop = ((double) freq) / totalTrees;
        if (show) {
            System.out.print(freq);
            System.out.print("\t" + nf.formatDecimal(prop * 100.0, 2) + "%");
        }
        sumFreq += freq;
        final double sumProp = ((double) sumFreq) / totalTrees;
        if (show) {
            System.out.print("\t" + nf.formatDecimal(sumProp * 100.0, 2) + "%");
            String newickTree = treeSet.get(i);
            if (freq > 100) {
                // calculate conditional average node heights
                Tree meanTree = analyzeTree(newickTree);
                System.out.println("\t" + TreeUtils.newick(meanTree));
            } else {
                System.out.println("\t" + newickTree);
            }
        }
        if (sumFreq >= credSet) {
            if (skipped > 0) {
                System.out.println();
                System.out.println("... (" + skipped + ") trees.");
            }
            System.out.println();
            System.out.println("95% credible set has " + (i + 1) + " trees.");
            break;
        }
    }
    System.out.println();
    System.out.println(Math.round(minCladeProbability * 100.0) + "%-rule clades (" + cladeSet.size() + " unique clades):");
    final int nCladeSet = cladeSet.size();
    for (int i = 0; i < nCladeSet; i++) {
        final int freq = cladeSet.getFrequency(i);
        final double prop = ((double) freq) / totalTrees;
        if (prop >= minCladeProbability) {
            System.out.print(freq);
            System.out.print("\t" + nf.formatDecimal(prop * 100.0, 2) + "%");
            System.out.print("\t" + cladeSet.getMeanNodeHeight(i));
            System.out.println("\t" + cladeSet.getClade(i));
        }
    }
    System.out.flush();
    System.out.println("Clade credible sets:");
    int fiveCredSet = (5 * totalTrees) / 100;
    int halfCredSet = (50 * totalTrees) / 100;
    sumFreq = 0;
    assert nTreeSet == treeSet.size();
    final CladeSet tempCladeSet = new CladeSet();
    for (int nt = 0; nt < nTreeSet; nt++) {
        sumFreq += treeSet.getFrequency(nt);
        String newickTree = treeSet.get(nt);
        NewickImporter importer = new NewickImporter(new StringReader(newickTree));
        try {
            Tree tree = importer.importNextTree();
            tempCladeSet.add(tree);
        } catch (Importer.ImportException e) {
            System.err.println("Err");
        }
        if (sumFreq >= fiveCredSet) {
            System.out.println();
            System.out.println("5% credible set has " + tempCladeSet.getCladeCount() + " clades.");
            // don't do it more than once
            fiveCredSet = totalTrees + 1;
        }
        if (sumFreq >= halfCredSet) {
            System.out.println();
            System.out.println("50% credible set has " + tempCladeSet.getCladeCount() + " clades.");
            // don't do it more than once
            halfCredSet = totalTrees + 1;
        }
    }
    System.out.flush();
}
Also used : NewickImporter(dr.evolution.io.NewickImporter) StringReader(java.io.StringReader) NumberFormatter(dr.util.NumberFormatter) NewickImporter(dr.evolution.io.NewickImporter) Importer(dr.evolution.io.Importer)

Aggregations

Importer (dr.evolution.io.Importer)20 NexusImporter (dr.evolution.io.NexusImporter)13 Tree (dr.evolution.tree.Tree)10 TreeImporter (dr.evolution.io.TreeImporter)8 NewickImporter (dr.evolution.io.NewickImporter)7 TaxonList (dr.evolution.util.TaxonList)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 FlexibleTree (dr.evolution.tree.FlexibleTree)2 PartitionData (dr.app.beauti.options.PartitionData)1 PartitionSubstitutionModel (dr.app.beauti.options.PartitionSubstitutionModel)1 NexusApplicationImporter (dr.app.beauti.util.NexusApplicationImporter)1 Alignment (dr.evolution.alignment.Alignment)1 CharSetAlignment (dr.evolution.alignment.CharSetAlignment)1 ConvertAlignment (dr.evolution.alignment.ConvertAlignment)1 TreeExporter (dr.evolution.io.TreeExporter)1 NodeRef (dr.evolution.tree.NodeRef)1 dr.evolution.util (dr.evolution.util)1 Taxon (dr.evolution.util.Taxon)1