Search in sources :

Example 21 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class AlignmentListInputEditor method init.

@Override
@SuppressWarnings("unchecked")
public void init(Input<?> input, BEASTInterface beastObject, int itemNr, ExpandOption isExpandOption, boolean addButtons) {
    this.itemNr = itemNr;
    if (input.get() instanceof List) {
        alignments = (List<Alignment>) input.get();
    } else {
        // we just have a single Alignment
        alignments = new ArrayList<>();
        alignments.add((Alignment) input.get());
    }
    linkButtons = new ArrayList<>();
    unlinkButtons = new ArrayList<>();
    partitionCount = alignments.size();
    // override BoxLayout in superclass
    setLayout(new BorderLayout());
    add(createLinkButtons(), BorderLayout.NORTH);
    add(createListBox(), BorderLayout.CENTER);
    // Box box = Box.createVerticalBox();
    // box.add(Box.createVerticalStrut(STRUT_SIZE));
    // box.add(createLinkButtons());
    // box.add(Box.createVerticalStrut(STRUT_SIZE));
    // box.add(createListBox());
    // box.add(Box.createVerticalStrut(STRUT_SIZE));
    // box.add(Box.createVerticalGlue());
    // add(box, BorderLayout.CENTER);
    Color focusColor = UIManager.getColor("Focus.color");
    Border focusBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, focusColor);
    new FileDrop(null, scrollPane, focusBorder, new FileDrop.Listener() {

        @Override
        public void filesDropped(java.io.File[] files) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    addItem(files);
                }
            });
        }
    });
    // end FileDrop.Listener
    // this should place the add/remove/split buttons at the bottom of the window.
    add(createAddRemoveSplitButtons(), BorderLayout.SOUTH);
    updateStatus();
}
Also used : Color(java.awt.Color) FileDrop(beast.app.util.FileDrop) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) BorderLayout(java.awt.BorderLayout) List(java.util.List) ArrayList(java.util.ArrayList) Border(javax.swing.border.Border) File(java.io.File)

Example 22 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class AlignmentListInputEditor method initTableData.

void initTableData() {
    this.likelihoods = new GenericTreeLikelihood[partitionCount];
    if (tableData == null) {
        tableData = new Object[partitionCount][NR_OF_COLUMNS];
    }
    CompoundDistribution likelihoods = (CompoundDistribution) doc.pluginmap.get("likelihood");
    for (int i = 0; i < partitionCount; i++) {
        Alignment data = alignments.get(i);
        // partition name
        tableData[i][NAME_COLUMN] = data;
        // alignment name
        if (data instanceof FilteredAlignment) {
            tableData[i][FILE_COLUMN] = ((FilteredAlignment) data).alignmentInput.get();
        } else {
            tableData[i][FILE_COLUMN] = data;
        }
        // # taxa
        tableData[i][TAXA_COLUMN] = data.getTaxonCount();
        // # sites
        tableData[i][SITES_COLUMN] = data.getSiteCount();
        // Data type
        tableData[i][TYPE_COLUMN] = data.getDataType();
        // site model
        GenericTreeLikelihood likelihood = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(i);
        assert (likelihood != null);
        this.likelihoods[i] = likelihood;
        tableData[i][SITEMODEL_COLUMN] = getPartition(likelihood.siteModelInput);
        // clock model
        tableData[i][CLOCKMODEL_COLUMN] = getPartition(likelihood.branchRateModelInput);
        // tree
        tableData[i][TREE_COLUMN] = getPartition(likelihood.treeInput);
        // useAmbiguities
        tableData[i][USE_AMBIGUITIES_COLUMN] = null;
        try {
            if (hasUseAmbiguitiesInput(i)) {
                tableData[i][USE_AMBIGUITIES_COLUMN] = likelihood.getInputValue("useAmbiguities");
            }
        } catch (Exception e) {
        // ignore
        }
    }
}
Also used : CompoundDistribution(beast.core.util.CompoundDistribution) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) FilteredAlignment(beast.evolution.alignment.FilteredAlignment)

Example 23 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class AlignmentListInputEditor method replaceItem.

private void replaceItem(Alignment alignment) {
    BeautiAlignmentProvider provider = new BeautiAlignmentProvider();
    List<BEASTInterface> list = provider.getAlignments(doc);
    List<Alignment> alignments = new ArrayList<>();
    for (BEASTInterface o : list) {
        if (o instanceof Alignment) {
            alignments.add((Alignment) o);
        }
    }
    Alignment replacement = null;
    if (alignments.size() > 1) {
        JComboBox<Alignment> jcb = new JComboBox<Alignment>(alignments.toArray(new Alignment[] {}));
        JOptionPane.showMessageDialog(null, jcb, "Select a replacement alignment", JOptionPane.QUESTION_MESSAGE);
        replacement = (Alignment) jcb.getSelectedItem();
    } else if (alignments.size() == 1) {
        replacement = alignments.get(0);
    }
    if (replacement != null) {
        if (!replacement.getDataType().getClass().getName().equals(alignment.getDataType().getClass().getName())) {
            JOptionPane.showMessageDialog(null, "Data types do not match, so alignment cannot be replaced: " + replacement.getID() + " " + replacement.getDataType().getClass().getName() + " != " + alignment.getID() + " " + alignment.getDataType().getClass().getName());
            return;
        }
        // replace alignment
        Set<BEASTInterface> outputs = new LinkedHashSet<>();
        outputs.addAll(alignment.getOutputs());
        for (BEASTInterface o : outputs) {
            for (Input<?> input : o.listInputs()) {
                if (input.get() == alignment) {
                    input.setValue(replacement, o);
                    replacement.getOutputs().add(o);
                } else if (input.get() instanceof List) {
                    @SuppressWarnings("rawtypes") List inputlist = (List) input.get();
                    int i = inputlist.indexOf(alignment);
                    if (i >= 0) {
                        inputlist.set(i, replacement);
                        replacement.getOutputs().add(o);
                    }
                }
            }
        }
        int i = doc.alignments.indexOf(alignment);
        doc.alignments.set(i, replacement);
        refreshPanel();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JComboBox(javax.swing.JComboBox) ArrayList(java.util.ArrayList) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) BEASTInterface(beast.core.BEASTInterface) List(java.util.List) ArrayList(java.util.ArrayList)

Example 24 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class BeagleTreeLikelihood method setStates.

/**
 * Sets the partials from a sequence in an alignment.
 *
 * @param beagle        beagle
 * @param nodeIndex     nodeIndex
 * @param taxon         the taxon
 */
protected final void setStates(Beagle beagle, int nodeIndex, int taxon) {
    Alignment data = dataInput.get();
    int i;
    int[] states = new int[patternCount];
    for (i = 0; i < patternCount; i++) {
        int code = data.getPattern(taxon, i);
        int[] statesForCode = data.getDataType().getStatesForCode(code);
        if (statesForCode.length == 1)
            states[i] = statesForCode[0];
        else
            // Causes ambiguous states to be ignored.
            states[i] = code;
    }
    beagle.setTipStates(nodeIndex, states);
}
Also used : Alignment(beast.evolution.alignment.Alignment)

Example 25 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class Frequencies method estimateFrequencies.

// update
/**
 * Estimate from sequence alignment.
 * This version matches the implementation in Beast 1 & PAUP  *
 */
void estimateFrequencies() {
    Alignment alignment = dataInput.get();
    DataType dataType = alignment.getDataType();
    int stateCount = alignment.getMaxStateCount();
    freqs = new double[stateCount];
    Arrays.fill(freqs, 1.0 / stateCount);
    int attempts = 0;
    double difference;
    do {
        double[] tmpFreq = new double[stateCount];
        double total = 0.0;
        for (int i = 0; i < alignment.getPatternCount(); i++) {
            int[] pattern = alignment.getPattern(i);
            double weight = alignment.getPatternWeight(i);
            for (int value : pattern) {
                int[] codes = dataType.getStatesForCode(value);
                double sum = 0.0;
                for (int codeIndex : codes) {
                    sum += freqs[codeIndex];
                }
                for (int codeIndex : codes) {
                    double tmp = (freqs[codeIndex] * weight) / sum;
                    tmpFreq[codeIndex] += tmp;
                    total += tmp;
                }
            }
        }
        difference = 0.0;
        for (int i = 0; i < stateCount; i++) {
            difference += Math.abs((tmpFreq[i] / total) - freqs[i]);
            freqs[i] = tmpFreq[i] / total;
        }
        attempts++;
    } while (difference > 1E-8 && attempts < 1000);
    // Alignment alignment = m_data.get();
    // m_fFreqs = new double[alignment.getMaxStateCount()];
    // for (int i = 0; i < alignment.getPatternCount(); i++) {
    // int[] pattern = alignment.getPattern(i);
    // double weight = alignment.getPatternWeight(i);
    // DataType dataType = alignment.getDataType();
    // for (int value : pattern) {
    // if (value < 4) {
    // int [] codes = dataType.getStatesForCode(value);
    // for (int codeIndex : codes) {
    // m_fFreqs[codeIndex] += weight / codes.length;
    // }
    // }
    // //                if (value < m_fFreqs.length) { // ignore unknowns
    // //                    m_fFreqs[value] += weight;
    // //                }
    // }
    // }
    // // normalize
    // double sum = 0;
    // for (double f : m_fFreqs) {
    // sum += f;
    // }
    // for (int i = 0; i < m_fFreqs.length; i++) {
    // m_fFreqs[i] /= sum;
    // }
    Log.info.println("Starting frequencies: " + Arrays.toString(freqs));
}
Also used : Alignment(beast.evolution.alignment.Alignment) DataType(beast.evolution.datatype.DataType)

Aggregations

Alignment (beast.evolution.alignment.Alignment)102 Test (org.junit.Test)43 Sequence (beast.evolution.alignment.Sequence)31 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)30 Tree (beast.evolution.tree.Tree)29 SiteModel (beast.evolution.sitemodel.SiteModel)27 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)22 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)20 ArrayList (java.util.ArrayList)17 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)17 Frequencies (beast.evolution.substitutionmodel.Frequencies)13 RealParameter (beast.core.parameter.RealParameter)12 TaxonSet (beast.evolution.alignment.TaxonSet)11 BEASTInterface (beast.core.BEASTInterface)10 ClusterTree (beast.util.ClusterTree)9 Taxon (beast.evolution.alignment.Taxon)6 HKY (beast.evolution.substitutionmodel.HKY)6 Node (beast.evolution.tree.Node)6 TreeParser (beast.util.TreeParser)6 IOException (java.io.IOException)6