Search in sources :

Example 26 with NexusImporter

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

the class ConditionalCladeFrequency method report.

/**
     * Creates the report. The estimated posterior of the given tree is printed.
     *
     * @throws IOException if general I/O error occurs
     */
public void report(Reader r) throws IOException, Importer.ImportException {
    System.err.println("making report");
    ArrayList<Tree> referenceTrees = new ArrayList<Tree>();
    BufferedReader reader = new BufferedReader(r);
    String line = reader.readLine();
    if (line.toUpperCase().startsWith("#NEXUS")) {
        NexusImporter importer = new NexusImporter(reader);
        Tree[] trees = importer.importTrees(null);
        for (Tree tree : trees) {
            referenceTrees.add(tree);
            SimpleTree sTree = new SimpleTree(tree);
            System.out.println("Estimated marginal posterior by condiational clade frequencies:");
            System.out.println(getTreeProbability(sTree) + "\t\t" + sTree);
        }
    } else {
        throw new RuntimeException("Could not read reference tree. Only Nexus format is supported.");
    }
    System.out.flush();
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) BufferedReader(java.io.BufferedReader) SimpleTree(dr.evolution.tree.SimpleTree) Tree(dr.evolution.tree.Tree) SimpleTree(dr.evolution.tree.SimpleTree)

Example 27 with NexusImporter

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

the class EmpiricalTreeDistributionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    final String fileName = xo.getStringAttribute(FILE_NAME);
    // default is random tree
    int startingTree = xo.getAttribute(STARTING_TREE, -1);
    // default is random draw
    boolean iterate = xo.getAttribute(ITERATE, false);
    if (iterate && startingTree < 0) {
        startingTree = 0;
    }
    Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'");
    TaxonList taxa = (TaxonList) xo.getChild(TaxonList.class);
    final File file = FileHelpers.getFile(fileName);
    Tree[] trees = null;
    NexusImporter importer = null;
    try {
        FileReader reader = new FileReader(file);
        importer = new NexusImporter(reader);
        if (!iterate) {
            // Re-order taxon numbers to original TaxonList order
            trees = importer.importTrees(taxa, true);
            reader.close();
        }
    } catch (FileNotFoundException e) {
        throw new XMLParseException(e.getMessage());
    } catch (IOException e) {
        throw new XMLParseException(e.getMessage());
    } catch (Importer.ImportException e) {
        throw new XMLParseException(e.getMessage());
    }
    if (iterate) {
        Logger.getLogger("dr.evomodel").info("    Iterate over each tree from file, " + fileName);
        return new EmpiricalTreeDistributionModel(importer, startingTree);
    } else {
        Logger.getLogger("dr.evomodel").info("    Randomly jump between " + trees.length + " trees from file, " + fileName);
        return new EmpiricalTreeDistributionModel(trees, startingTree);
    }
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TaxonList(dr.evolution.util.TaxonList) EmpiricalTreeDistributionModel(dr.evomodel.tree.EmpiricalTreeDistributionModel) Tree(dr.evolution.tree.Tree) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Example 28 with NexusImporter

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

the class AlignmentScore method main.

public static void main(String[] args) throws java.io.IOException, Importer.ImportException {
    NexusImporter importer = new NexusImporter(new FileReader(args[0]));
    Alignment alignment = importer.importAlignment();
    ExtractPairs pairs = new ExtractPairs(alignment);
    Parameter muParam = new Parameter.Default(1.0);
    Parameter kappaParam = new Parameter.Default(1.0);
    kappaParam.addBounds(new Parameter.DefaultBounds(100.0, 0.0, 1));
    muParam.addBounds(new Parameter.DefaultBounds(1.0, 1.0, 1));
    Parameter freqParam = new Parameter.Default(alignment.getStateFrequencies());
    FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqParam);
    SubstitutionModel substModel = new HKY(kappaParam, freqModel);
    SiteModel siteModel = new GammaSiteModel(substModel, muParam, null, 1, null);
    ScoreMatrix scoreMatrix = new ScoreMatrix(siteModel, 0.1);
    double threshold = 0.1;
    List<PairDistance> pairDistances = new ArrayList<PairDistance>();
    Set<Integer> sequencesUsed = new HashSet<Integer>();
    List<Integer> allGaps = new ArrayList<Integer>();
    for (int i = 0; i < alignment.getSequenceCount(); i++) {
        for (int j = i + 1; j < alignment.getSequenceCount(); j++) {
            Alignment pairAlignment = pairs.getPairAlignment(i, j);
            if (pairAlignment != null) {
                SitePatterns patterns = new SitePatterns(pairAlignment);
                double distance = getGeneticDistance(scoreMatrix, patterns);
                if (distance < threshold) {
                    List gaps = new ArrayList();
                    GapUtils.getGapSizes(pairAlignment, gaps);
                    pairDistances.add(new PairDistance(i, j, distance, gaps, pairAlignment.getSiteCount()));
                    System.out.print(".");
                } else {
                    System.out.print("*");
                }
            } else {
                System.out.print("x");
            }
        }
        System.out.println();
    }
    Collections.sort(pairDistances);
    int totalLength = 0;
    for (PairDistance pairDistance : pairDistances) {
        Integer x = pairDistance.x;
        Integer y = pairDistance.y;
        if (!sequencesUsed.contains(x) && !sequencesUsed.contains(y)) {
            allGaps.addAll(pairDistance.gaps);
            sequencesUsed.add(x);
            sequencesUsed.add(y);
            System.out.println("Added pair (" + x + "," + y + ") d=" + pairDistance.distance + " L=" + pairDistance.alignmentLength);
            totalLength += pairDistance.alignmentLength;
        }
    }
    printFrequencyTable(allGaps);
    System.out.println("total length=" + totalLength);
}
Also used : ExtractPairs(dr.evolution.alignment.ExtractPairs) FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) SitePatterns(dr.evolution.alignment.SitePatterns) NexusImporter(dr.evolution.io.NexusImporter) SubstitutionModel(dr.oldevomodel.substmodel.SubstitutionModel) Alignment(dr.evolution.alignment.Alignment) HKY(dr.oldevomodel.substmodel.HKY) Parameter(dr.inference.model.Parameter) FileReader(java.io.FileReader)

Example 29 with NexusImporter

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

the class TestParsimony method main.

public static void main(String[] args) throws Exception {
    // read alignments
    NexusImporter importer = new NexusImporter(new FileReader(alignmentFileName));
    Alignment alignment = importer.importAlignment();
    // alignment = new GapStrippedAlignment(alignment);
    // read trees
    Tree tree = null;
    importer = new NexusImporter(new FileReader(treeFileName));
    try {
        tree = importer.importTree(alignment);
    //((FlexibleTree)tree).resolveTree();
    } catch (Exception e) {
        System.err.println("Exception attempting to read tree: " + e.getMessage());
        System.exit(0);
    }
    //testParsimony(new FitchParsimony(alignment, false), alignment, tree);
    System.out.println("single:");
    testParsimony(new SankoffParsimony(alignment), alignment, tree);
    System.out.println("paired:");
    testPairedSites(alignment, tree);
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) FileReader(java.io.FileReader)

Aggregations

NexusImporter (dr.evolution.io.NexusImporter)29 Tree (dr.evolution.tree.Tree)17 Importer (dr.evolution.io.Importer)12 FileReader (java.io.FileReader)12 TreeImporter (dr.evolution.io.TreeImporter)10 NewickImporter (dr.evolution.io.NewickImporter)9 TaxonList (dr.evolution.util.TaxonList)4 FlexibleTree (dr.evolution.tree.FlexibleTree)3 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