1 package jalview.ext.archaeopteryx;
3 import jalview.datamodel.ColumnSelection;
4 import jalview.datamodel.HiddenColumns;
5 import jalview.datamodel.SequenceGroup;
6 import jalview.datamodel.SequenceI;
7 import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
8 import jalview.gui.PaintRefresher;
9 import jalview.structure.SelectionSource;
10 import jalview.structure.StructureSelectionManager;
11 import jalview.viewmodel.AlignmentViewport;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.InputEvent;
15 import java.awt.event.MouseEvent;
16 import java.util.HashSet;
17 import java.util.List;
21 import javax.swing.JTabbedPane;
22 import javax.swing.SwingUtilities;
23 import javax.swing.event.ChangeEvent;
24 import javax.swing.event.ChangeListener;
26 import org.forester.archaeopteryx.MainFrame;
27 import org.forester.phylogeny.Phylogeny;
28 import org.forester.phylogeny.PhylogenyMethods;
29 import org.forester.phylogeny.PhylogenyNode;
32 * Class for binding the Archaeopteryx tree viewer to the Jalview alignment that
33 * it originates from, meaning that selecting sequences in the tree viewer also
34 * selects them in the alignment view and vice versa.
36 * @author kjvanderheide
39 public final class JalviewBinding
40 implements ExternalTreeViewerBindingI<PhylogenyNode>
42 private final MainFrame aptxFrame;
44 private org.forester.archaeopteryx.TreePanel currentTreeView;
46 private final AlignmentViewport parentAvport;
48 private JTabbedPane treeTabs;
50 private final StructureSelectionManager ssm;
52 private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
54 private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
58 * @param archaeopteryx
60 * @param jalviewAlignmentViewport
61 * alignment viewport from which the tree was calculated.
63 * @param alignMappedToNodes
64 * map with sequences used to calculate the tree and matching tree
65 * nodes as key, value pair respectively.
67 * @param nodesMappedToAlign
68 * map with tree nodes and matching sequences used to calculate the
69 * tree as key, value pair respectively.
71 public JalviewBinding(final MainFrame archaeopteryx,
72 final AlignmentViewport jalviewAlignmentViewport,
73 final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
74 final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
76 // deal with/prohibit null values here as that will cause problems
77 aptxFrame = archaeopteryx;
78 parentAvport = jalviewAlignmentViewport;
79 sequencesBoundToNodes = alignMappedToNodes;
80 nodesBoundToSequences = nodesMappedToAlign;
82 treeTabs = aptxFrame.getMainPanel().getTabbedPane();
83 ssm = parentAvport.getStructureSelectionManager();
85 // archaeopteryx.getMainPanel().getControlPanel().setColorBranches(true);
87 ssm.addSelectionListener(this);
89 int tabCount = treeTabs.getTabCount();
91 for (int i = 0; i < tabCount; i++)
93 // roundabout way to select each tree because getComponentAt(i) requires
94 // casting to TreePanel which doesn't work
95 treeTabs.setSelectedIndex(i);
96 currentTreeView = aptxFrame.getMainPanel().getCurrentTreePanel();
97 currentTreeView.addMouseListener(this);
98 PaintRefresher.Register(currentTreeView,
99 parentAvport.getSequenceSetId());
103 treeTabs.addChangeListener(new ChangeListener()
107 public void stateChanged(ChangeEvent e)
110 SwingUtilities.invokeLater(new Runnable()
115 * Resend the selection to the tree view when tabs get switched, this
116 * has to be buried in invokeLater as Forester first resets the tree
117 * view on switching tabs, without invokeLater this would get called
118 * before Forester resets which would nullify the selection.
122 currentTreeView = aptxFrame.getMainPanel()
123 .getCurrentTreePanel();
124 parentAvport.sendSelection();
136 public void actionPerformed(ActionEvent e)
141 public void mouseClicked(MouseEvent e)
143 SwingUtilities.invokeLater(new Runnable() {
147 * invokeLater so that this always runs after Forester's mouseClicked
152 final PhylogenyNode node = currentTreeView.findNode(e.getX(),
156 if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
157 // selection if shift
161 parentAvport.setSelectionGroup(null);
164 showNodeSelectionOnAlign(node);
180 public void mousePressed(final MouseEvent e)
185 public void mouseReleased(MouseEvent e)
190 public void mouseEntered(MouseEvent e)
195 public void mouseExited(MouseEvent e)
201 public void selection(final SequenceGroup seqsel,
202 final ColumnSelection colsel, final HiddenColumns hidden,
203 final SelectionSource source)
205 if (source == parentAvport) // check if source is alignment from where the
208 currentTreeView.setFoundNodes0(
209 new HashSet<Long>(seqsel.getSequences().size()));
211 for (SequenceI selectedSequence : seqsel.getSequences())
213 PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
214 if (matchingNode != null)
216 currentTreeView.getFoundNodes0().add(matchingNode.getId());
228 * Partially refactored from TreeCanvas
230 public void partitionTree(final MouseEvent e)
233 int lineLength = currentTreeView.getHeight();
235 Phylogeny tree = currentTreeView.getPhylogeny();
236 double treeHeight = tree.calculateHeight(true);
242 int viewWidth = currentTreeView.getWidth();
244 // treeView.validate();
246 // System.out.println("selection");
247 // System.out.println(x);
248 // System.out.println("-------------");
249 // System.out.println("width");
250 // System.out.println(viewWidth);
260 public void showNodeSelectionOnAlign(final PhylogenyNode node)
263 if (node.isInternal())
265 showMatchingChildSequences(node);
270 showMatchingSequence(node);
280 public void showMatchingSequence(final PhylogenyNode nodeToMatch)
282 SequenceI matchingSequence = nodesBoundToSequences.get(nodeToMatch);
283 if (matchingSequence != null)
285 long nodeId = nodeToMatch.getId();
286 addOrRemoveInSet(currentTreeView.getFoundNodes0(), nodeId);
287 treeSelectionChanged(matchingSequence);
288 parentAvport.sendSelection();
294 public void showMatchingChildSequences(final PhylogenyNode parentNode)
296 List<PhylogenyNode> childNodes = PhylogenyMethods
297 .getAllDescendants(parentNode);
300 for (PhylogenyNode childNode : childNodes)
302 // childNode.getBranchData().setBranchColor(new BranchColor(Color.BLUE));
304 SequenceI matchingSequence = nodesBoundToSequences.get(childNode);
305 if (matchingSequence != null)
307 long nodeId = childNode.getId();
308 addOrRemoveInSet(currentTreeView.getFoundNodes0(), nodeId);
310 treeSelectionChanged(matchingSequence);
315 parentAvport.sendSelection();
321 * Refactored from TreeCanvas.
324 * of the node selected in the tree viewer.
327 public void treeSelectionChanged(final SequenceI sequence)
329 if (!parentAvport.isClosed()) // alignment view could be closed
331 SequenceGroup selected = parentAvport.getSelectionGroup();
333 if (selected == null)
335 selected = new SequenceGroup();
336 parentAvport.setSelectionGroup(selected);
339 selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
340 selected.addOrRemove(sequence, true);
344 public void sortByTree_actionPerformed() {
345 // parentAvport.mirrorCommand(command, undo, ssm, source);
348 // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
354 * sort the associated alignment view by the current tree.
359 // public void sortByTree_actionPerformed()// modify for Aptx
362 // // if (treeCanvas.applyToAllViews)
364 // final ArrayList<CommandI> commands = new ArrayList<>();
365 // for (AlignmentPanel ap : PaintRefresher
366 // .getAssociatedPanels(parentAvport.getSequenceSetId()))
368 // commands.add(sortAlignmentIn(ap.av.getAlignPanel()));
370 // av.getAlignPanel().alignFrame.addHistoryItem(new CommandI()
374 // public void undoCommand(AlignmentI[] views)
376 // for (CommandI tsort : commands)
378 // tsort.undoCommand(views);
383 // public int getSize()
385 // return commands.size();
389 // public String getDescription()
391 // return "Tree Sort (many views)";
395 // public void doCommand(AlignmentI[] views)
398 // for (CommandI tsort : commands)
400 // tsort.doCommand(views);
404 // for (AlignmentPanel ap : PaintRefresher
405 // .getAssociatedPanels(av.getSequenceSetId()))
407 // // ensure all the alignFrames refresh their GI after adding an undo item
408 // ap.alignFrame.updateEditMenuBar();
413 // treeCanvas.ap.alignFrame
414 // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
423 * @param objectToCheck
425 public static <E> void addOrRemoveInSet(Set<E> set, E objectToCheck)
427 if (set.contains(objectToCheck))
429 set.remove(objectToCheck);
433 set.add(objectToCheck);
438 public AlignmentViewport getParentAvport()
443 public org.forester.archaeopteryx.TreePanel getTreeView()
445 return currentTreeView;