Search in sources :

Example 61 with Alignment

use of beast.evolution.alignment.Alignment in project bacter by tgvaughan.

the class ACGLikelihoodSlow method calculateLogP.

@Override
public double calculateLogP() {
    logP = 0.0;
    for (Region region : acg.getRegions(locus)) {
        Alignment margAlign = createMarginalAlignment(alignment, acg, region);
        Tree margTree = new Tree(new MarginalTree(acg, region).getRoot());
        TreeLikelihood treeLikelihood = new TreeLikelihood();
        treeLikelihood.initByName("data", margAlign, "tree", margTree, "siteModel", siteModel, "useAmbiguities", useAmbiguitiesInput.get());
        logP += treeLikelihood.calculateLogP();
    }
    return logP;
}
Also used : Alignment(beast.evolution.alignment.Alignment) TreeLikelihood(beast.evolution.likelihood.TreeLikelihood) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) Region(bacter.Region) Tree(beast.evolution.tree.Tree) MarginalTree(bacter.MarginalTree) MarginalTree(bacter.MarginalTree)

Example 62 with Alignment

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

the class BeautiAlignmentProvider method parseBeast1XML.

private static BEASTInterface parseBeast1XML(String ID, String xml) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    doc.normalize();
    NodeList alignments = doc.getElementsByTagName("alignment");
    Alignment alignment = new Alignment();
    alignment.dataTypeInput.setValue("nucleotide", alignment);
    // parse first alignment
    org.w3c.dom.Node node = alignments.item(0);
    String dataTypeName = node.getAttributes().getNamedItem("dataType").getNodeValue();
    int totalCount = 4;
    if (dataTypeName == null) {
        alignment.dataTypeInput.setValue("integer", alignment);
    } else if (dataTypeName.toLowerCase().equals("dna") || dataTypeName.toLowerCase().equals("nucleotide")) {
        alignment.dataTypeInput.setValue("nucleotide", alignment);
        totalCount = 4;
    } else if (dataTypeName.toLowerCase().equals("aminoacid") || dataTypeName.toLowerCase().equals("protein")) {
        alignment.dataTypeInput.setValue("aminoacid", alignment);
        totalCount = 20;
    } else {
        alignment.dataTypeInput.setValue("integer", alignment);
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        org.w3c.dom.Node child = children.item(i);
        if (child.getNodeName().equals("sequence")) {
            Sequence sequence = new Sequence();
            // find the taxon
            String taxon = "";
            NodeList sequenceChildren = child.getChildNodes();
            for (int j = 0; j < sequenceChildren.getLength(); j++) {
                org.w3c.dom.Node child2 = sequenceChildren.item(j);
                if (child2.getNodeName().equals("taxon")) {
                    taxon = child2.getAttributes().getNamedItem("idref").getNodeValue();
                }
            }
            String data = child.getTextContent();
            sequence.initByName("totalcount", totalCount, "taxon", taxon, "value", data);
            sequence.setID("seq_" + taxon);
            alignment.sequenceInput.setValue(sequence, alignment);
        }
    }
    alignment.setID(ID);
    alignment.initAndValidate();
    return alignment;
}
Also used : InputSource(org.xml.sax.InputSource) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Sequence(beast.evolution.alignment.Sequence) Document(org.w3c.dom.Document)

Example 63 with Alignment

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

the class AlignmentListInputEditor method splitItem.

// replaceItem
void splitItem() {
    int[] selected = getTableRowSelection();
    if (selected.length == 0) {
        JOptionPane.showMessageDialog(this, "Select partitions to split, before hitting the split button");
        return;
    }
    String[] options = { "{1,2} + 3", "{1,2} + 3 frame 2", "{1,2} + 3 frame 3", "1 + 2 + 3", "1 + 2 + 3 frame 2", "1 + 2 + 3 frame 3" };
    String option = (String) JOptionPane.showInputDialog(null, "Split selected alignments into partitions", "Option", JOptionPane.WARNING_MESSAGE, null, options, "1 + 2 + 3");
    if (option == null) {
        return;
    }
    String[] filters = null;
    String[] ids = null;
    if (option.equals(options[0])) {
        filters = new String[] { "1::3,2::3", "3::3" };
        ids = new String[] { "_1,2", "_3" };
    } else if (option.equals(options[1])) {
        filters = new String[] { "1::3,3::3", "2::3" };
        ids = new String[] { "_1,2", "_3" };
    } else if (option.equals(options[2])) {
        filters = new String[] { "2::3,3::3", "1::3" };
        ids = new String[] { "_1,2", "_3" };
    } else if (option.equals(options[3])) {
        filters = new String[] { "1::3", "2::3", "3::3" };
        ids = new String[] { "_1", "_2", "_3" };
    } else if (option.equals(options[4])) {
        filters = new String[] { "2::3", "3::3", "1::3" };
        ids = new String[] { "_1", "_2", "_3" };
    } else if (option.equals(options[5])) {
        filters = new String[] { "3::3", "1::3", "2::3" };
        ids = new String[] { "_1", "_2", "_3" };
    } else {
        return;
    }
    for (int i = selected.length - 1; i >= 0; i--) {
        int rowNr = selected[i];
        Alignment alignment = alignments.remove(rowNr);
        getDoc().delAlignmentWithSubnet(alignment);
        try {
            List<Alignment> newAlignments = new ArrayList<>();
            for (int j = 0; j < filters.length; j++) {
                FilteredAlignment f = new FilteredAlignment();
                f.initByName("data", alignment, "filter", filters[j], "dataType", alignment.dataTypeInput.get());
                f.setID(alignment.getID() + ids[j]);
                getDoc().addAlignmentWithSubnet(f, getDoc().beautiConfig.partitionTemplate.get());
                newAlignments.add(f);
            }
            alignments.addAll(newAlignments);
            partitionCount = alignments.size();
            tableData = null;
            initTableData();
            if (newAlignments.size() == 2) {
                link(TREE_COLUMN, alignments.size() - 1, alignments.size() - 2);
            } else {
                link(TREE_COLUMN, alignments.size() - 2, alignments.size() - 3);
                tableData = null;
                initTableData();
                link(TREE_COLUMN, alignments.size() - 1, alignments.size() - 2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    refreshPanel();
}
Also used : FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) ArrayList(java.util.ArrayList) FilteredAlignment(beast.evolution.alignment.FilteredAlignment)

Example 64 with Alignment

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

the class Beauti method addAlignmentProviderMenus.

private void addAlignmentProviderMenus(JMenu fileMenu) {
    List<BeautiAlignmentProvider> providers = doc.beautiConfig.alignmentProvider;
    for (BeautiAlignmentProvider provider : providers) {
        AbstractAction action = new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    setCursor(new Cursor(Cursor.WAIT_CURSOR));
                    // get user-specified alignments
                    List<BEASTInterface> beastObjects = provider.getAlignments(doc);
                    if (beastObjects != null) {
                        for (BEASTInterface o : beastObjects) {
                            if (o instanceof Alignment) {
                                try {
                                    BeautiDoc.createTaxonSet((Alignment) o, doc);
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }
                    }
                    doc.connectModel();
                    doc.fireDocHasChanged();
                    if (beastObjects != null) {
                        for (BEASTInterface o : beastObjects) {
                            if (o instanceof MRCAPrior) {
                                doc.addMRCAPrior((MRCAPrior) o);
                            }
                        }
                    }
                    a_save.setEnabled(true);
                    a_saveas.setEnabled(true);
                } catch (Exception exx) {
                    exx.printStackTrace();
                    String text = "Something went wrong importing the alignment:\n";
                    JTextArea textArea = new JTextArea(text);
                    textArea.setColumns(30);
                    textArea.setLineWrap(true);
                    textArea.setWrapStyleWord(true);
                    textArea.append(exx.getMessage());
                    textArea.setSize(textArea.getPreferredSize().width, 1);
                    textArea.setOpaque(false);
                    JOptionPane.showMessageDialog(null, textArea, "Error importing alignment", JOptionPane.WARNING_MESSAGE);
                }
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        };
        String providerInfo = provider.toString().replaceAll("Add ", "Add partition for ");
        action.putValue(Action.SHORT_DESCRIPTION, providerInfo);
        action.putValue(Action.LONG_DESCRIPTION, providerInfo);
        action.putValue(Action.NAME, provider.toString());
        fileMenu.add(action);
    }
}
Also used : Alignment(beast.evolution.alignment.Alignment) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) MRCAPrior(beast.math.distributions.MRCAPrior) BEASTInterface(beast.core.BEASTInterface) Cursor(java.awt.Cursor) AbstractAction(javax.swing.AbstractAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 65 with Alignment

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

the class BeautiConfig method selectAlignments.

/**
 * if fileArray == null, then use getAlignments(doc)
 * @param doc
 * @param parent
 * @param fileArray
 * @return a list of alignments based on the user selected alignment provider
 */
public List<BEASTInterface> selectAlignments(BeautiDoc doc, JComponent parent, File[] fileArray) {
    List<BeautiAlignmentProvider> providers = alignmentProvider;
    BeautiAlignmentProvider selectedProvider = null;
    if (providers.size() == 1) {
        selectedProvider = providers.get(0);
    } else {
        selectedProvider = (BeautiAlignmentProvider) JOptionPane.showInputDialog(parent, "Select what to add", "Add partition", JOptionPane.QUESTION_MESSAGE, null, providers.toArray(), providers.get(0));
        if (selectedProvider == null) {
            return null;
        }
    }
    List<BEASTInterface> beastObjects = selectedProvider.getAlignments(doc, fileArray);
    // create taxon sets, if any
    if (beastObjects != null) {
        for (BEASTInterface o : beastObjects) {
            if (o instanceof Alignment) {
                try {
                    BeautiDoc.createTaxonSet((Alignment) o, doc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    doc.connectModel();
    if (beastObjects != null) {
        for (BEASTInterface o : beastObjects) {
            if (o instanceof MRCAPrior) {
                doc.addMRCAPrior((MRCAPrior) o);
            }
        }
    }
    return beastObjects;
}
Also used : Alignment(beast.evolution.alignment.Alignment) MRCAPrior(beast.math.distributions.MRCAPrior) BEASTInterface(beast.core.BEASTInterface)

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