Search in sources :

Example 96 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class AlignmentGenerator method writeAlignment.

/**
     * Generate an alignment block from these beast options
     *
     * @param alignment the alignment to write
     * @param writer    the writer
     */
private void writeAlignment(Alignment alignment, XMLWriter writer) {
    writer.writeText("");
    writer.writeComment("The sequence alignment (each sequence refers to a taxon above).", "ntax=" + alignment.getTaxonCount() + " nchar=" + alignment.getSiteCount());
    if (options.samplePriorOnly) {
        writer.writeComment("Null sequences generated in order to sample from the prior only.");
    }
    if (getAlignmentDataTypeDescription(alignment) != null) {
        writer.writeOpenTag(AlignmentParser.ALIGNMENT, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, alignment.getId()), new Attribute.Default<String>(DataType.DATA_TYPE, getAlignmentDataTypeDescription(alignment)) });
    } else {
        writer.writeOpenTag(AlignmentParser.ALIGNMENT, new Attribute.Default<String>(XMLParser.ID, alignment.getId()));
        writer.writeIDref(DataType.DATA_TYPE, getAlignmentDataTypeIdref(alignment));
    }
    for (int i = 0; i < alignment.getTaxonCount(); i++) {
        Taxon taxon = alignment.getTaxon(i);
        writer.writeOpenTag(SequenceParser.SEQUENCE);
        writer.writeIDref(TaxonParser.TAXON, taxon.getId());
        if (!options.samplePriorOnly) {
            //                writer.checkText(alignment.getAlignedSequenceString(i));
            writer.writeText(alignment.getAlignedSequenceString(i));
        //                System.out.println(taxon.getId() + ": \n" + alignment.getAlignedSequenceString(i));
        //                System.out.println("len = " + alignment.getAlignedSequenceString(i).length() + "\n");
        } else {
            // generate a codon in case there is codon partitioning
            writer.writeText(NULL_SEQUENCE);
        }
        writer.writeCloseTag(SequenceParser.SEQUENCE);
    }
    writer.writeCloseTag(AlignmentParser.ALIGNMENT);
}
Also used : Attribute(dr.util.Attribute) Taxon(dr.evolution.util.Taxon)

Example 97 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class BeastGenerator method checkOptions.

/**
     * Checks various options to check they are valid. Throws IllegalArgumentExceptions with
     * descriptions of the problems.
     *
     * @throws IllegalArgumentException if there is a problem with the current settings
     */
public void checkOptions() throws GeneratorException {
    try {
        if (options.contains(Microsatellite.INSTANCE)) {
            // clear all masks
            for (PartitionPattern partitionPattern : options.getPartitionPattern()) {
                partitionPattern.getPatterns().clearMask();
            }
            // set mask
            for (PartitionTreeModel model : options.getPartitionTreeModels()) {
                // if a tree only has 1 data partition, which mostly mean unlinked trees
                if (options.getDataPartitions(model).size() == 1) {
                    PartitionPattern partition = (PartitionPattern) options.getDataPartitions(model).get(0);
                    Patterns patterns = partition.getPatterns();
                    for (int i = 0; i < patterns.getTaxonCount(); i++) {
                        int state = patterns.getPatternState(i, 0);
                        // mask ? from data
                        if (state < 0) {
                            patterns.addMask(i);
                        }
                    }
                //                        System.out.println("mask set = " + patterns.getMaskSet() + " in partition " + partition.getName());
                }
            }
        }
        //++++++++++++++++ Taxon List ++++++++++++++++++
        TaxonList taxonList = options.taxonList;
        Set<String> ids = new HashSet<String>();
        ids.add(TaxaParser.TAXA);
        ids.add(AlignmentParser.ALIGNMENT);
        ids.add(TraitData.TRAIT_SPECIES);
        if (taxonList != null) {
            if (taxonList.getTaxonCount() < 2) {
                throw new GeneratorException("BEAST requires at least two taxa to run.");
            }
            for (int i = 0; i < taxonList.getTaxonCount(); i++) {
                Taxon taxon = taxonList.getTaxon(i);
                if (ids.contains(taxon.getId())) {
                    throw new GeneratorException("A taxon has the same id," + taxon.getId() + MESSAGE_CAL);
                }
                ids.add(taxon.getId());
            }
        }
        //++++++++++++++++ Taxon Sets ++++++++++++++++++
        for (PartitionTreeModel model : options.getPartitionTreeModels()) {
            // should be only 1 calibrated internal node with a proper prior and monophyletic for each tree at moment
            if (model.getPartitionTreePrior().getNodeHeightPrior() == TreePriorType.YULE_CALIBRATION) {
                if (// invalid node calibration
                options.treeModelOptions.isNodeCalibrated(model) < 0)
                    throw new GeneratorException(MESSAGE_CAL_YULE);
                if (options.treeModelOptions.isNodeCalibrated(model) > 0) {
                    // internal node calibration
                    List taxonSetsList = options.getKeysFromValue(options.taxonSetsTreeModel, model);
                    if (taxonSetsList.size() != 1 || !options.taxonSetsMono.get(taxonSetsList.get(0))) {
                        // 1 tmrca per tree && monophyletic
                        throw new GeneratorException(MESSAGE_CAL_YULE, BeautiFrame.TAXON_SETS);
                    }
                }
            }
        }
        for (Taxa taxa : options.taxonSets) {
            // AR - we should allow single taxon taxon sets...
            if (// && !options.taxonSetsIncludeStem.get(taxa)
            taxa.getTaxonCount() < 1) {
                throw new GeneratorException("Taxon set, " + taxa.getId() + ", should contain \n" + "at least one taxa. Please go back to Taxon Sets \n" + "panel to correct this.", BeautiFrame.TAXON_SETS);
            }
            if (ids.contains(taxa.getId())) {
                throw new GeneratorException("A taxon set has the same id," + taxa.getId() + MESSAGE_CAL, BeautiFrame.TAXON_SETS);
            }
            ids.add(taxa.getId());
        }
        //++++++++++++++++ *BEAST ++++++++++++++++++
        if (options.useStarBEAST) {
            if (!options.traitExists(TraitData.TRAIT_SPECIES))
                throw new GeneratorException("A trait labelled \"species\" is required for *BEAST species designations." + "\nPlease create or import the species designations in the Traits table.", BeautiFrame.TRAITS);
            // should be only 1 calibrated internal node with monophyletic at moment
            if (options.getPartitionTreePriors().get(0).getNodeHeightPrior() == TreePriorType.SPECIES_YULE_CALIBRATION) {
                if (options.speciesSets.size() != 1 || !options.speciesSetsMono.get(options.speciesSets.get(0))) {
                    throw new GeneratorException(MESSAGE_CAL_YULE, BeautiFrame.TAXON_SETS);
                }
            }
            for (Taxa species : options.speciesSets) {
                if (species.getTaxonCount() < 2) {
                    throw new GeneratorException("Species set, " + species.getId() + ",\n should contain" + "at least two species. \nPlease go back to Species Sets panel to select included species.", BeautiFrame.TAXON_SETS);
                }
                if (ids.contains(species.getId())) {
                    throw new GeneratorException("A species set has the same id," + species.getId() + MESSAGE_CAL, BeautiFrame.TAXON_SETS);
                }
                ids.add(species.getId());
            }
            int tId = options.starBEASTOptions.getEmptySpeciesIndex();
            if (tId >= 0) {
                throw new GeneratorException("The taxon " + options.taxonList.getTaxonId(tId) + " has NULL value for \"species\" trait", BeautiFrame.TRAITS);
            }
        }
        //        if (options.isShareSameTreePrior()) {
        if (options.getPartitionTreeModels().size() > 1) {
            //TODO not allowed multi-prior yet
            for (PartitionTreePrior prior : options.getPartitionTreePriors()) {
                if (prior.getNodeHeightPrior() == TreePriorType.GMRF_SKYRIDE) {
                    throw new GeneratorException("For the Skyride, tree model/tree prior combination not implemented by BEAST." + "\nThe Skyride is only available for a single tree model partition in this release.", BeautiFrame.TREES);
                }
            }
        }
        //+++++++++++++++ Starting tree ++++++++++++++++
        for (PartitionTreeModel model : options.getPartitionTreeModels()) {
            if (model.getStartingTreeType() == StartingTreeType.USER) {
                if (model.getUserStartingTree() == null) {
                    throw new GeneratorException("Please select a starting tree in " + BeautiFrame.TREES + " panel, " + "\nwhen choosing user specified starting tree option.", BeautiFrame.TREES);
                }
            }
        }
        //++++++++++++++++ Random local clock model validation ++++++++++++++++++
        for (PartitionClockModel model : options.getPartitionClockModels()) {
            // 1 random local clock CANNOT have different tree models
            if (model.getClockType() == ClockType.RANDOM_LOCAL_CLOCK) {
                // || AUTOCORRELATED_LOGNORMAL
                PartitionTreeModel treeModel = null;
                for (AbstractPartitionData pd : options.getDataPartitions(model)) {
                    // only the PDs linked to this tree model
                    if (treeModel != null && treeModel != pd.getPartitionTreeModel()) {
                        throw new GeneratorException("A single random local clock cannot be applied to multiple trees.", BeautiFrame.CLOCK_MODELS);
                    }
                    treeModel = pd.getPartitionTreeModel();
                }
            }
        }
        //++++++++++++++++ Tree Model ++++++++++++++++++
        for (PartitionTreeModel model : options.getPartitionTreeModels()) {
            int numOfTaxa = -1;
            for (AbstractPartitionData pd : options.getDataPartitions(model)) {
                if (pd.getTaxonCount() > 0) {
                    if (numOfTaxa > 0) {
                        if (numOfTaxa != pd.getTaxonCount()) {
                            throw new GeneratorException("Partitions with different taxa cannot share the same tree.", BeautiFrame.DATA_PARTITIONS);
                        }
                    } else {
                        numOfTaxa = pd.getTaxonCount();
                    }
                }
            }
        }
        //++++++++++++++++ Prior Bounds ++++++++++++++++++
        for (Parameter param : options.selectParameters()) {
            if (param.getInitial() != Double.NaN) {
                if (param.isTruncated && (param.getInitial() < param.truncationLower || param.getInitial() > param.truncationUpper)) {
                    throw new GeneratorException("Parameter \"" + param.getName() + "\":" + "\ninitial value " + param.getInitial() + " is NOT in the range [" + param.truncationLower + ", " + param.truncationUpper + "]," + "\nor this range is wrong. Please check the Prior panel.", BeautiFrame.PRIORS);
                } else if (param.priorType == PriorType.UNIFORM_PRIOR && (param.getInitial() < param.uniformLower || param.getInitial() > param.uniformUpper)) {
                    throw new GeneratorException("Parameter \"" + param.getName() + "\":" + "\ninitial value " + param.getInitial() + " is NOT in the range [" + param.uniformLower + ", " + param.uniformUpper + "]," + "\nor this range is wrong. Please check the Prior panel.", BeautiFrame.PRIORS);
                }
                if (param.isNonNegative && param.getInitial() < 0.0) {
                    throw new GeneratorException("Parameter \"" + param.getName() + "\":" + "\ninitial value " + param.getInitial() + " should be non-negative. Please check the Prior panel.", BeautiFrame.PRIORS);
                }
                if (param.isZeroOne && (param.getInitial() < 0.0 || param.getInitial() > 1.0)) {
                    throw new GeneratorException("Parameter \"" + param.getName() + "\":" + "\ninitial value " + param.getInitial() + " should lie in the interval [0, 1]. Please check the Prior panel.", BeautiFrame.PRIORS);
                }
            }
        }
        checkComponentOptions();
    // add other tests and warnings here
    // Speciation model with dated tips
    // Sampling rates without dated tips or priors on rate or nodes
    } catch (Exception e) {
        // catch any other exceptions here and rethrow to generate messages
        throw new GeneratorException(e.getMessage());
    }
}
Also used : TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) IOException(java.io.IOException) Taxa(dr.evolution.util.Taxa) TaxonList(dr.evolution.util.TaxonList) Patterns(dr.evolution.alignment.Patterns)

Example 98 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class ViewAligmentPanel method paintComponent.

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);
    //		Graphics2D g2d = (Graphics2D) g;
    g.setFont(new Font("SansSerif", Font.PLAIN, FONT_SIZE));
    String sequenceString = partitionData.getAlignment().getAlignedSequenceString(0);
    for (int i = 0; i < sequenceString.length(); i++) {
        if (i == 0 || ((i + 1) % 10) == 0) {
            if (i == 0) {
                g.drawString(Integer.toString(i + 1), TAXON_NAME_LEN + i * (FONT_SIZE + GAP), Y_BOADER - (FONT_SIZE + 3 * GAP));
            } else if (i < 100) {
                g.drawString(Integer.toString(i + 1), TAXON_NAME_LEN + i * (FONT_SIZE + GAP) - GAP, Y_BOADER - (FONT_SIZE + 3 * GAP));
            } else {
                g.drawString(Integer.toString(i + 1), TAXON_NAME_LEN + i * (FONT_SIZE + GAP) - 2 * GAP, Y_BOADER - (FONT_SIZE + 3 * GAP));
            }
            g.drawLine(TAXON_NAME_LEN + i * (FONT_SIZE + GAP) + GAP, Y_BOADER - (FONT_SIZE + 2 * GAP), TAXON_NAME_LEN + i * (FONT_SIZE + GAP) + GAP, Y_BOADER - (FONT_SIZE + GAP));
        }
    }
    for (int i = 0; i < partitionData.getAlignment().getTaxonCount(); i++) {
        Taxon taxon = partitionData.getAlignment().getTaxon(i);
        g.drawString(taxon.getId(), 0, Y_BOADER + i * (FONT_SIZE + GAP));
        sequenceString = partitionData.getAlignment().getAlignedSequenceString(i);
        int charSpace = TAXON_NAME_LEN;
        for (char c : sequenceString.toCharArray()) {
            g.drawString(Character.toString(c), charSpace, Y_BOADER + i * (FONT_SIZE + GAP));
            charSpace = charSpace + FONT_SIZE + GAP;
        }
    }
}
Also used : Taxon(dr.evolution.util.Taxon)

Example 99 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class TaxaEditor method loadTaxaFile.

// END: ListenLoadTaxaFile
private void loadTaxaFile(File file) {
    try {
        Taxa taxa = new Taxa();
        String[] lines = Utils.loadStrings(file.getAbsolutePath());
        Taxon taxon;
        for (int i = 0; i < lines.length; i++) {
            String[] line = lines[i].split("\\s+");
            taxon = new Taxon(line[TaxaEditorTableModel.NAME_INDEX]);
            taxon.setAttribute(Utils.ABSOLUTE_HEIGHT, Double.valueOf(line[TaxaEditorTableModel.HEIGHT_INDEX]));
            taxa.addTaxon(taxon);
        }
        // END: i loop
        updateTable(taxa);
    } catch (Exception e) {
        Utils.handleException(e);
    }
// END: try-catch block
}
Also used : Taxa(dr.evolution.util.Taxa) Taxon(dr.evolution.util.Taxon)

Example 100 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class TaxaEditorTableModel method addEmptyRow.

// END: Constructor
public void addEmptyRow() {
    String name = "";
    Taxon taxon = new Taxon(name);
    taxon.setAttribute(Utils.ABSOLUTE_HEIGHT, (Double) 0.0);
    taxaSet.addTaxon(taxon);
    this.fireTableDataChanged();
}
Also used : Taxon(dr.evolution.util.Taxon)

Aggregations

Taxon (dr.evolution.util.Taxon)151 Taxa (dr.evolution.util.Taxa)31 ArrayList (java.util.ArrayList)24 TaxonList (dr.evolution.util.TaxonList)19 NodeRef (dr.evolution.tree.NodeRef)18 Date (dr.evolution.util.Date)18 Tree (dr.evolution.tree.Tree)15 Sequence (dr.evolution.sequence.Sequence)12 TreeModel (dr.evomodel.tree.TreeModel)12 Parameter (dr.inference.model.Parameter)12 Attribute (dr.util.Attribute)11 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)10 Patterns (dr.evolution.alignment.Patterns)9 NewickImporter (dr.evolution.io.NewickImporter)7 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)7 Microsatellite (dr.evolution.datatype.Microsatellite)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 PatternList (dr.evolution.alignment.PatternList)4 DataType (dr.evolution.datatype.DataType)4