Search in sources :

Example 1 with PartitionData

use of dr.app.beauti.options.PartitionData in project beast-mcmc by beast-dev.

the class OldTreesPanel method createTree.

private void createTree() {
    if (generateTreeDialog == null) {
        generateTreeDialog = new GenerateTreeDialog(frame);
    }
    int result = generateTreeDialog.showDialog(options);
    if (result != JOptionPane.CANCEL_OPTION) {
        GenerateTreeDialog.MethodTypes methodType = generateTreeDialog.getMethodType();
        PartitionData partition = generateTreeDialog.getDataPartition();
        Patterns patterns = new Patterns(partition.getAlignment());
        DistanceMatrix distances = new F84DistanceMatrix(patterns);
        Tree tree;
        TemporalRooting temporalRooting;
        switch(methodType) {
            case NJ:
                tree = new NeighborJoiningTree(distances);
                temporalRooting = new TemporalRooting(tree);
                tree = temporalRooting.findRoot(tree, TemporalRooting.RootingFunction.CORRELATION);
                break;
            case UPGMA:
                tree = new UPGMATree(distances);
                temporalRooting = new TemporalRooting(tree);
                break;
            default:
                throw new IllegalArgumentException("unknown method type");
        }
        tree.setId(generateTreeDialog.getName());
        options.userTrees.add(tree);
        treesTableModel.fireTableDataChanged();
        int row = options.userTrees.size() - 1;
        treesTable.getSelectionModel().setSelectionInterval(row, row);
    }
    fireTreePriorsChanged();
}
Also used : F84DistanceMatrix(dr.evolution.distance.F84DistanceMatrix) NeighborJoiningTree(dr.evolution.tree.NeighborJoiningTree) PartitionData(dr.app.beauti.options.PartitionData) UPGMATree(dr.evolution.tree.UPGMATree) NeighborJoiningTree(dr.evolution.tree.NeighborJoiningTree) Tree(dr.evolution.tree.Tree) TemporalRooting(dr.app.pathogen.TemporalRooting) F84DistanceMatrix(dr.evolution.distance.F84DistanceMatrix) DistanceMatrix(dr.evolution.distance.DistanceMatrix) UPGMATree(dr.evolution.tree.UPGMATree) Patterns(dr.evolution.alignment.Patterns)

Example 2 with PartitionData

use of dr.app.beauti.options.PartitionData 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<CharSet> charSets = new ArrayList<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, null);
                } 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 (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, "default", partition);
                    partition.setPartitionSubstitutionModel(pm);
                // options.addPartitionSubstitutionModel(pm);
                }
            }
            options.dataPartitions.add(partition);
        }
    }
    calculateHeights(options);
}
Also used : ArrayList(java.util.ArrayList) CharSet(dr.app.beauti.util.CharSet) 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)

Aggregations

PartitionData (dr.app.beauti.options.PartitionData)2 Tree (dr.evolution.tree.Tree)2 PartitionSubstitutionModel (dr.app.beauti.options.PartitionSubstitutionModel)1 CharSet (dr.app.beauti.util.CharSet)1 NexusApplicationImporter (dr.app.beauti.util.NexusApplicationImporter)1 TemporalRooting (dr.app.pathogen.TemporalRooting)1 Alignment (dr.evolution.alignment.Alignment)1 CharSetAlignment (dr.evolution.alignment.CharSetAlignment)1 ConvertAlignment (dr.evolution.alignment.ConvertAlignment)1 Patterns (dr.evolution.alignment.Patterns)1 DistanceMatrix (dr.evolution.distance.DistanceMatrix)1 F84DistanceMatrix (dr.evolution.distance.F84DistanceMatrix)1 Importer (dr.evolution.io.Importer)1 NexusImporter (dr.evolution.io.NexusImporter)1 NeighborJoiningTree (dr.evolution.tree.NeighborJoiningTree)1 UPGMATree (dr.evolution.tree.UPGMATree)1 dr.evolution.util (dr.evolution.util)1 ArrayList (java.util.ArrayList)1