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;
20 import org.forester.archaeopteryx.MainFrame;
21 import org.forester.phylogeny.PhylogenyMethods;
22 import org.forester.phylogeny.PhylogenyNode;
25 * Class for binding the Archaeopteryx tree viewer to the Jalview alignment that
26 * it originates from, meaning that selecting sequences in the tree viewer also
27 * selects them in the alignment view and vice versa.
29 * @author kjvanderheide
32 public final class JalviewBinding
33 implements ExternalTreeViewerBindingI<PhylogenyNode>
35 private org.forester.archaeopteryx.TreePanel treeView;
37 private AlignmentViewport parentAvport;
39 private final StructureSelectionManager ssm;
41 private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
43 private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
47 * @param archaeopteryx
49 * @param jalviewAlignmentViewport
50 * alignment viewport from which the tree was calculated.
52 * @param alignMappedToNodes
53 * map with sequences used to calculate the tree and matching tree
54 * nodes as key, value pair respectively.
56 * @param nodesMappedToAlign
57 * map with tree nodes and matching sequences used to calculate the
58 * tree as key, value pair respectively.
60 public JalviewBinding(final MainFrame archaeopteryx,
61 final AlignmentViewport jalviewAlignmentViewport,
62 final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
63 final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
65 // deal with/prohibit null values here as that will cause problems
66 parentAvport = jalviewAlignmentViewport;
67 sequencesBoundToNodes = alignMappedToNodes;
68 nodesBoundToSequences = nodesMappedToAlign;
69 treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
70 ssm = parentAvport.getStructureSelectionManager();
71 ssm.addSelectionListener(this);
72 treeView.addMouseListener(this);
73 PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
78 public void actionPerformed(ActionEvent e)
83 public void mouseClicked(MouseEvent e)
88 public void mousePressed(final MouseEvent e)
90 showNodeSelectionOnAlign(e);
94 public void mouseReleased(MouseEvent e)
99 public void mouseEntered(MouseEvent e)
104 public void mouseExited(MouseEvent e)
110 public void selection(final SequenceGroup seqsel,
111 final ColumnSelection colsel, final HiddenColumns hidden,
112 final SelectionSource source)
114 if (source == parentAvport) // check if source is alignment from where the
117 treeView.setFoundNodes0(
118 new HashSet<Long>(seqsel.getSequences().size()));
120 for (SequenceI selectedSequence : seqsel.getSequences())
122 PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
123 if (matchingNode != null)
125 treeView.getFoundNodes0().add(matchingNode.getId());
129 PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
138 public void showNodeSelectionOnAlign(final MouseEvent e)
140 final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
143 if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
144 // selection if shift
147 parentAvport.setSelectionGroup(null);
150 if (node.isInternal())
152 showMatchingChildSequences(node);
157 showMatchingSequence(node);
167 public void showMatchingSequence(final PhylogenyNode nodeToMatch)
169 SequenceI matchingSequence = nodesBoundToSequences.get(nodeToMatch);
170 if (matchingSequence != null)
172 treeSelectionChanged(matchingSequence);
173 parentAvport.sendSelection();
174 PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
179 public void showMatchingChildSequences(final PhylogenyNode parentNode)
181 final List<PhylogenyNode> childNodes = PhylogenyMethods
182 .getAllDescendants(parentNode);
183 // final BranchColor branchColor = new BranchColor();
185 for (PhylogenyNode childNode : childNodes)
187 SequenceI matchingSequence = nodesBoundToSequences.get(childNode);
188 if (matchingSequence != null)
190 treeSelectionChanged(matchingSequence);
195 parentAvport.sendSelection();
196 PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
200 * Refactored from TreeCanvas.
203 * of the node selected in the tree viewer.
206 public void treeSelectionChanged(final SequenceI sequence)
208 SequenceGroup selected = parentAvport.getSelectionGroup();
210 if (selected == null)
212 selected = new SequenceGroup();
213 parentAvport.setSelectionGroup(selected);
216 selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
217 selected.addOrRemove(sequence, true);
221 public AlignmentViewport getParentAvport()
226 public void setParentAvport(final AlignmentViewport parentAvport)
228 this.parentAvport = parentAvport;