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.gui.PaintRefresher;
8 import jalview.structure.SelectionSource;
9 import jalview.structure.StructureSelectionManager;
10 import jalview.viewmodel.AlignmentViewport;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.InputEvent;
14 import java.awt.event.MouseEvent;
15 import java.util.HashSet;
18 import org.forester.archaeopteryx.MainFrame;
19 import org.forester.phylogeny.PhylogenyNode;
22 * Class for binding the Archaeopteryx tree viewer to the Jalview alignment that
23 * it originates from, meaning that selecting sequences in the tree viewer also
24 * selects them in the alignment view and vice versa.
26 * @author kjvanderheide
29 public final class JalviewAptxBinding implements JalviewTreeViewerBindingI
31 private org.forester.archaeopteryx.TreePanel treeView;
33 private AlignmentViewport parentAvport;
35 private final StructureSelectionManager ssm;
37 private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
39 private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
43 * @param archaeopteryx
45 * @param jalviewAlignmentViewport
46 * alignment viewport from which the tree was calculated.
48 * @param alignMappedToNodes
49 * map with sequences used to calculate the tree and matching tree
50 * nodes as key, value pair respectively.
52 * @param nodesMappedToAlign
53 * map with tree nodes and matching sequences used to calculate the
54 * tree as key, value pair respectively.
56 public JalviewAptxBinding(final MainFrame archaeopteryx,
57 final AlignmentViewport jalviewAlignmentViewport,
58 final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
59 final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
61 // deal with/prohibit null values here as that will cause problems
62 parentAvport = jalviewAlignmentViewport;
63 sequencesBoundToNodes = alignMappedToNodes;
64 nodesBoundToSequences = nodesMappedToAlign;
65 treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
66 ssm = parentAvport.getStructureSelectionManager();
67 ssm.addSelectionListener(this);
68 treeView.addMouseListener(this);
69 PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
74 public void actionPerformed(ActionEvent e)
79 public void mouseClicked(MouseEvent e)
84 public void mousePressed(final MouseEvent e)
86 showNodeSelectionOnAlign(e);
90 public void mouseReleased(MouseEvent e)
95 public void mouseEntered(MouseEvent e)
100 public void mouseExited(MouseEvent e)
106 public void selection(final SequenceGroup seqsel,
107 final ColumnSelection colsel, final HiddenColumns hidden,
108 final SelectionSource source)
110 if (source == parentAvport) // check if source is alignment from where the
113 treeView.setFoundNodes0(
114 new HashSet<Long>(seqsel.getSequences().size()));
116 for (SequenceI selectedSequence : seqsel.getSequences())
118 PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
119 if (matchingNode != null)
121 treeView.getFoundNodes0().add(matchingNode.getId());
125 PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
133 * If a node is selected in the tree panel this method highlights the
134 * corresponding sequence in the Jalview alignment view.
137 public void showNodeSelectionOnAlign(final MouseEvent e)
139 final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
142 SequenceI matchingSequence = nodesBoundToSequences.get(node);
143 if (matchingSequence != null)
146 if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) // shift is pressed
147 // (so multiple nodes
155 parentAvport.setSelectionGroup(null); // reset selection if shift
159 treeSelectionChanged(matchingSequence);
160 parentAvport.sendSelection(); // not actually needed?
161 PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
171 * Refactored from TreeCanvas.
174 * of the node selected in the tree viewer.
176 public void treeSelectionChanged(final SequenceI sequence)
178 SequenceGroup selected = parentAvport.getSelectionGroup();
180 if (selected == null)
182 selected = new SequenceGroup();
183 parentAvport.setSelectionGroup(selected);
186 selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
187 selected.addOrRemove(sequence, true);
191 public AlignmentViewport getParentAvport()
196 public void setParentAvport(final AlignmentViewport parentAvport)
198 this.parentAvport = parentAvport;