Search in sources :

Example 6 with Taxon

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

the class TaxonSetInputEditor method getContent.

private Component getContent(List<Taxon> taxonset) {
    m_taxonset = taxonset;
    m_taxonMap = new HashMap<>();
    m_lineageset = new ArrayList<>();
    for (Taxon taxonset2 : m_taxonset) {
        if (taxonset2 instanceof TaxonSet) {
            for (Taxon taxon : ((TaxonSet) taxonset2).taxonsetInput.get()) {
                m_lineageset.add(taxon);
                m_taxonMap.put(taxon.getID(), taxonset2.getID());
            }
        }
    }
    // set up table.
    // special features: background shading of rows
    // custom editor allowing only Date column to be edited.
    m_model = new DefaultTableModel();
    m_model.addColumn("Taxon");
    m_model.addColumn("Species/Population");
    taxonSetToModel();
    m_table = new JTable(m_model) {

        private static final long serialVersionUID = 1L;

        // method that induces table row shading
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            // even index, selected or not selected
            if (isCellSelected(Index_row, Index_col)) {
                comp.setBackground(Color.gray);
            } else if (Index_row % 2 == 0) {
                comp.setBackground(new Color(237, 243, 255));
            } else {
                comp.setBackground(Color.white);
            }
            return comp;
        }
    };
    // set up editor that makes sure only doubles are accepted as entry
    // and only the Date column is editable.
    m_table.setDefaultEditor(Object.class, new TableCellEditor() {

        JTextField m_textField = new JTextField();

        int m_iRow, m_iCol;

        @Override
        public boolean stopCellEditing() {
            m_table.removeEditor();
            String text = m_textField.getText();
            // Log.warning.println(text);
            m_model.setValueAt(text, m_iRow, m_iCol);
            // try {
            // Double.parseDouble(text);
            // } catch (Exception e) {
            // return false;
            // }
            modelToTaxonset();
            return true;
        }

        @Override
        public boolean isCellEditable(EventObject anEvent) {
            return m_table.getSelectedColumn() == 1;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowNr, int colNr) {
            if (!isSelected) {
                return null;
            }
            m_iRow = rowNr;
            m_iCol = colNr;
            m_textField.setText((String) value);
            return m_textField;
        }

        @Override
        public boolean shouldSelectCell(EventObject anEvent) {
            return false;
        }

        @Override
        public void removeCellEditorListener(CellEditorListener l) {
        }

        @Override
        public Object getCellEditorValue() {
            return null;
        }

        @Override
        public void cancelCellEditing() {
        }

        @Override
        public void addCellEditorListener(CellEditorListener l) {
        }
    });
    m_table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    m_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    int size = m_table.getFont().getSize();
    m_table.setRowHeight(20 * size / 13);
    m_table.getColumnModel().getColumn(0).setPreferredWidth(250 * size / 13);
    m_table.getColumnModel().getColumn(1).setPreferredWidth(250 * size / 13);
    JTableHeader header = m_table.getTableHeader();
    header.addMouseListener(new ColumnHeaderListener());
    JScrollPane pane = new JScrollPane(m_table);
    Box tableBox = Box.createHorizontalBox();
    tableBox.add(Box.createHorizontalGlue());
    tableBox.add(pane);
    tableBox.add(Box.createHorizontalGlue());
    Box box = Box.createVerticalBox();
    box.add(createFilterBox());
    box.add(tableBox);
    box.add(createButtonBox());
    return box;
}
Also used : JScrollPane(javax.swing.JScrollPane) TableCellRenderer(javax.swing.table.TableCellRenderer) Taxon(beast.evolution.alignment.Taxon) DefaultTableModel(javax.swing.table.DefaultTableModel) Color(java.awt.Color) JTableHeader(javax.swing.table.JTableHeader) CellEditorListener(javax.swing.event.CellEditorListener) Box(javax.swing.Box) TaxonSet(beast.evolution.alignment.TaxonSet) JTextField(javax.swing.JTextField) EventObject(java.util.EventObject) JTable(javax.swing.JTable) EventObject(java.util.EventObject) TableCellEditor(javax.swing.table.TableCellEditor) Component(java.awt.Component)

Example 7 with Taxon

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

the class TaxonSetInputEditor method newTaxonSet.

private TaxonSet newTaxonSet(String match) {
    if (getDoc().taxaset.containsKey(match)) {
        Taxon t = doc.taxaset.get(match);
        if (t instanceof TaxonSet) {
            TaxonSet set = (TaxonSet) t;
            set.taxonsetInput.get().clear();
            return set;
        } else {
        // TODO handle situation where taxon and set have same name (issue #135)
        }
    }
    TaxonSet set = new TaxonSet();
    set.setID(match);
    return set;
}
Also used : Taxon(beast.evolution.alignment.Taxon) TaxonSet(beast.evolution.alignment.TaxonSet)

Example 8 with Taxon

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

the class TaxonSetInputEditor method parseTrait.

void parseTrait(String trait) {
    Map<String, String> traitmap = new HashMap<>();
    for (String line : trait.split(",")) {
        String[] strs = line.split("=");
        if (strs.length == 2) {
            traitmap.put(strs[0].trim(), strs[1].trim());
        }
    }
    m_taxonset.clear();
    Set<Taxon> taxa = new HashSet<>();
    Set<String> taxonIDs = new HashSet<>();
    for (Alignment alignment : getDoc().alignments) {
        if (alignment instanceof FilteredAlignment) {
            alignment = ((FilteredAlignment) alignment).alignmentInput.get();
        }
        for (String id : alignment.getTaxaNames()) {
            if (!taxonIDs.contains(id)) {
                Taxon taxon = getDoc().getTaxon(id);
                taxa.add(taxon);
                taxonIDs.add(id);
            }
        }
    }
    HashMap<String, TaxonSet> map = new HashMap<>();
    List<String> unknowns = new ArrayList<>();
    for (Taxon taxon : taxa) {
        if (!(taxon instanceof TaxonSet)) {
            String match = traitmap.get(taxon.getID());
            if (match == null) {
                match = "UNKNOWN";
                unknowns.add(taxon.getID());
            }
            try {
                if (map.containsKey(match)) {
                    TaxonSet set = map.get(match);
                    set.taxonsetInput.setValue(taxon, set);
                } else {
                    TaxonSet set = newTaxonSet(match);
                    set.taxonsetInput.setValue(taxon, set);
                    map.put(match, set);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    // add taxon sets
    for (TaxonSet set : map.values()) {
        m_taxonset.add(set);
    }
    if (unknowns.size() > 0) {
        showMisMatchMessage(unknowns);
    }
}
Also used : HashMap(java.util.HashMap) Taxon(beast.evolution.alignment.Taxon) ArrayList(java.util.ArrayList) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) TaxonSet(beast.evolution.alignment.TaxonSet) PatternSyntaxException(java.util.regex.PatternSyntaxException) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) HashSet(java.util.HashSet)

Example 9 with Taxon

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

the class TaxonSetInputEditor method init.

@Override
public void init(Input<?> input, BEASTInterface beastObject, int itemNr, ExpandOption isExpandOption, boolean addButtons) {
    m_input = input;
    m_beastObject = beastObject;
    this.itemNr = itemNr;
    TaxonSet taxonset = (TaxonSet) m_input.get();
    if (taxonset == null) {
        return;
    }
    List<Taxon> taxonsets = new ArrayList<>();
    List<Taxon> taxa = taxonset.taxonsetInput.get();
    for (Taxon taxon : taxa) {
        taxonsets.add(taxon);
    }
    add(getContent(taxonsets));
    if (taxa.size() == 1 && taxa.get(0).getID().equals("Beauti2DummyTaxonSet") || taxa.size() == 0) {
        taxa.clear();
        try {
            // species is first character of taxon
            guessTaxonSets("(.).*", 0);
            for (Taxon taxonset2 : m_taxonset) {
                for (Taxon taxon : ((TaxonSet) taxonset2).taxonsetInput.get()) {
                    m_lineageset.add(taxon);
                    m_taxonMap.put(taxon.getID(), taxonset2.getID());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        taxonSetToModel();
        modelToTaxonset();
    }
}
Also used : Taxon(beast.evolution.alignment.Taxon) ArrayList(java.util.ArrayList) TaxonSet(beast.evolution.alignment.TaxonSet) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 10 with Taxon

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

the class MRCAPriorInputEditor method init.

@Override
public void init(Input<?> input, BEASTInterface beastObject, final int listItemNr, ExpandOption isExpandOption, boolean addButtons) {
    m_bAddButtons = addButtons;
    m_input = input;
    m_beastObject = beastObject;
    this.itemNr = listItemNr;
    Box itemBox = Box.createHorizontalBox();
    MRCAPrior prior = (MRCAPrior) beastObject;
    String text = prior.getID();
    JButton taxonButton = new JButton(text);
    // taxonButton.setMinimumSize(Base.PREFERRED_SIZE);
    // taxonButton.setPreferredSize(Base.PREFERRED_SIZE);
    itemBox.add(taxonButton);
    taxonButton.addActionListener(e -> {
        List<?> list = (List<?>) m_input.get();
        MRCAPrior prior2 = (MRCAPrior) list.get(itemNr);
        try {
            TaxonSet taxonset = prior2.taxonsetInput.get();
            List<Taxon> originalTaxa = new ArrayList<>();
            originalTaxa.addAll(taxonset.taxonsetInput.get());
            Set<Taxon> candidates = getTaxonCandidates(prior2);
            TaxonSetDialog dlg = new TaxonSetDialog(taxonset, candidates, doc);
            if (dlg.showDialog()) {
                if (dlg.taxonSet.taxonsetInput.get().size() == 0) {
                    JOptionPane.showMessageDialog(doc.beauti, "At least one taxon should be included in the taxon set", "Error specifying taxon set", JOptionPane.ERROR_MESSAGE);
                    taxonset.taxonsetInput.get().addAll(originalTaxa);
                    return;
                }
                prior2.taxonsetInput.setValue(dlg.taxonSet, prior2);
                int i = 1;
                String id = dlg.taxonSet.getID();
                while (doc.pluginmap.containsKey(dlg.taxonSet.getID()) && doc.pluginmap.get(dlg.taxonSet.getID()) != dlg.taxonSet) {
                    dlg.taxonSet.setID(id + i);
                    i++;
                }
                BEASTObjectPanel.addPluginToMap(dlg.taxonSet, doc);
                prior2.setID(dlg.taxonSet.getID() + ".prior");
            }
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        refreshPanel();
    });
    if (prior.distInput.getType() == null) {
        try {
            prior.distInput.setValue(new OneOnX(), prior);
            prior.distInput.setValue(null, prior);
        } catch (Exception e) {
        // TODO: handle exception
        }
    }
    List<BeautiSubTemplate> availableBEASTObjects = doc.getInputEditorFactory().getAvailableTemplates(prior.distInput, prior, null, doc);
    JComboBox<BeautiSubTemplate> comboBox = new JComboBox<>(availableBEASTObjects.toArray(new BeautiSubTemplate[] {}));
    comboBox.setName(text + ".distr");
    if (prior.distInput.get() != null) {
        String id = prior.distInput.get().getID();
        // id = BeautiDoc.parsePartition(id);
        id = id.substring(0, id.indexOf('.'));
        for (BeautiSubTemplate template : availableBEASTObjects) {
            if (template.classInput.get() != null && template.shortClassName.equals(id)) {
                comboBox.setSelectedItem(template);
            }
        }
    } else {
        comboBox.setSelectedItem(BeautiConfig.NULL_TEMPLATE);
    }
    comboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            @SuppressWarnings("unchecked") JComboBox<BeautiSubTemplate> comboBox = (JComboBox<BeautiSubTemplate>) e.getSource();
            BeautiSubTemplate template = (BeautiSubTemplate) comboBox.getSelectedItem();
            List<?> list = (List<?>) m_input.get();
            MRCAPrior prior = (MRCAPrior) list.get(itemNr);
            // }
            try {
                // BEASTObject beastObject2 =
                template.createSubNet(new PartitionContext(""), prior, prior.distInput, true);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            refreshPanel();
        }
    });
    itemBox.add(comboBox);
    JCheckBox isMonophyleticdBox = new JCheckBox(doc.beautiConfig.getInputLabel(prior, prior.isMonophyleticInput.getName()));
    isMonophyleticdBox.setName(text + ".isMonophyletic");
    isMonophyleticdBox.setSelected(prior.isMonophyleticInput.get());
    isMonophyleticdBox.setToolTipText(prior.isMonophyleticInput.getHTMLTipText());
    isMonophyleticdBox.addActionListener(new MRCAPriorActionListener(prior));
    itemBox.add(isMonophyleticdBox);
    JButton deleteButton = new SmallButton("-", true);
    deleteButton.setToolTipText("Delete this calibration");
    deleteButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Log.warning.println("Trying to delete a calibration");
            List<?> list = (List<?>) m_input.get();
            MRCAPrior prior = (MRCAPrior) list.get(itemNr);
            doc.disconnect(prior, "prior", "distribution");
            doc.disconnect(prior, "tracelog", "log");
            if (prior.onlyUseTipsInput.get()) {
                disableTipSampling(m_beastObject, doc);
            }
            doc.unregisterPlugin(prior);
            refreshPanel();
        }
    });
    itemBox.add(Box.createGlue());
    itemBox.add(deleteButton);
    add(itemBox);
}
Also used : ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JComboBox(javax.swing.JComboBox) Taxon(beast.evolution.alignment.Taxon) JComboBox(javax.swing.JComboBox) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) TaxonSet(beast.evolution.alignment.TaxonSet) InvocationTargetException(java.lang.reflect.InvocationTargetException) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) MRCAPrior(beast.math.distributions.MRCAPrior) SmallButton(beast.app.draw.SmallButton) OneOnX(beast.math.distributions.OneOnX)

Aggregations

Taxon (beast.evolution.alignment.Taxon)26 TaxonSet (beast.evolution.alignment.TaxonSet)18 ArrayList (java.util.ArrayList)7 Alignment (beast.evolution.alignment.Alignment)6 Tree (beast.evolution.tree.Tree)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)3 MRCAPrior (beast.math.distributions.MRCAPrior)3 Box (javax.swing.Box)3 BEASTInterface (beast.core.BEASTInterface)2 RealParameter (beast.core.parameter.RealParameter)2 TreeParser (beast.util.TreeParser)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 PrintStream (java.io.PrintStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JButton (javax.swing.JButton)2