2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.SearchResultMatchI;
30 import jalview.datamodel.SearchResults;
31 import jalview.datamodel.SearchResultsI;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
36 import jalview.schemes.ResidueProperties;
37 import jalview.structure.SelectionListener;
38 import jalview.structure.SelectionSource;
39 import jalview.structure.SequenceListener;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.structure.VamsasSource;
42 import jalview.util.Comparison;
43 import jalview.util.MappingUtils;
44 import jalview.util.MessageManager;
45 import jalview.util.Platform;
46 import jalview.viewmodel.AlignmentViewport;
48 import java.awt.BorderLayout;
50 import java.awt.FontMetrics;
51 import java.awt.Panel;
52 import java.awt.Point;
53 import java.awt.event.InputEvent;
54 import java.awt.event.MouseEvent;
55 import java.awt.event.MouseListener;
56 import java.awt.event.MouseMotionListener;
57 import java.util.Collections;
58 import java.util.List;
59 import java.util.Vector;
61 public class SeqPanel extends Panel implements MouseMotionListener,
62 MouseListener, SequenceListener, SelectionListener
65 public SeqCanvas seqCanvas;
67 public AlignmentPanel ap;
69 protected int lastres;
71 protected int startseq;
73 protected AlignViewport av;
75 // if character is inserted or deleted, we will need to recalculate the
77 boolean seqEditOccurred = false;
79 ScrollThread scrollThread = null;
81 boolean mouseDragging = false;
83 boolean editingSeqs = false;
85 boolean groupEditing = false;
89 boolean changeEndSeq = false;
91 boolean changeStartSeq = false;
93 boolean changeEndRes = false;
95 boolean changeStartRes = false;
97 SequenceGroup stretchGroup = null;
99 StringBuffer keyboardNo1;
101 StringBuffer keyboardNo2;
103 boolean mouseWheelPressed = false;
105 Point lastMousePress;
107 EditCommand editCommand;
109 StructureSelectionManager ssm;
111 public SeqPanel(AlignViewport avp, AlignmentPanel p)
115 seqCanvas = new SeqCanvas(avp);
116 setLayout(new BorderLayout());
121 seqCanvas.addMouseMotionListener(this);
122 seqCanvas.addMouseListener(this);
123 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
124 ssm.addStructureViewerListener(this);
125 ssm.addSelectionListener(this);
132 if (editCommand != null && editCommand.getSize() > 0)
134 ap.alignFrame.addHistoryItem(editCommand);
135 av.notifyAlignment();
141 groupEditing = false;
149 seqCanvas.cursorY = getKeyboardNo1() - 1;
150 scrollToVisible(true);
153 void setCursorColumn()
155 seqCanvas.cursorX = getKeyboardNo1() - 1;
156 scrollToVisible(true);
159 void setCursorRowAndColumn()
161 if (keyboardNo2 == null)
163 keyboardNo2 = new StringBuffer();
167 seqCanvas.cursorX = getKeyboardNo1() - 1;
168 seqCanvas.cursorY = getKeyboardNo2() - 1;
169 scrollToVisible(true);
173 void setCursorPosition()
175 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
177 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
178 scrollToVisible(true);
181 void moveCursor(int dx, int dy)
183 seqCanvas.cursorX += dx;
184 seqCanvas.cursorY += dy;
185 if (av.hasHiddenColumns() && !av.getAlignment().getHiddenColumns()
186 .isVisible(seqCanvas.cursorX))
188 int original = seqCanvas.cursorX - dx;
189 int maxWidth = av.getAlignment().getWidth();
191 while (!av.getAlignment().getHiddenColumns()
192 .isVisible(seqCanvas.cursorX) && seqCanvas.cursorX < maxWidth
193 && seqCanvas.cursorX > 0)
195 seqCanvas.cursorX += dx;
198 if (seqCanvas.cursorX >= maxWidth || !av.getAlignment()
199 .getHiddenColumns().isVisible(seqCanvas.cursorX))
201 seqCanvas.cursorX = original;
204 scrollToVisible(false);
208 * Scroll to make the cursor visible in the viewport.
211 * just jump to the location rather than scrolling
213 void scrollToVisible(boolean jump)
215 if (seqCanvas.cursorX < 0)
217 seqCanvas.cursorX = 0;
219 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
221 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
224 if (seqCanvas.cursorY < 0)
226 seqCanvas.cursorY = 0;
228 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
230 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
235 boolean repaintNeeded = true;
238 // only need to repaint if the viewport did not move, as otherwise it will
240 repaintNeeded = !av.getRanges().setViewportLocation(seqCanvas.cursorX,
245 if (av.getWrapAlignment())
247 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
251 av.getRanges().scrollToVisible(seqCanvas.cursorX,
255 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
256 seqCanvas.cursorX, seqCanvas.cursorY);
264 void setSelectionAreaAtCursor(boolean topLeft)
266 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
268 if (av.getSelectionGroup() != null)
270 SequenceGroup sg = av.getSelectionGroup();
271 // Find the top and bottom of this group
272 int min = av.getAlignment().getHeight(), max = 0;
273 for (int i = 0; i < sg.getSize(); i++)
275 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
290 sg.setStartRes(seqCanvas.cursorX);
291 if (sg.getEndRes() < seqCanvas.cursorX)
293 sg.setEndRes(seqCanvas.cursorX);
296 min = seqCanvas.cursorY;
300 sg.setEndRes(seqCanvas.cursorX);
301 if (sg.getStartRes() > seqCanvas.cursorX)
303 sg.setStartRes(seqCanvas.cursorX);
306 max = seqCanvas.cursorY + 1;
311 // Only the user can do this
312 av.setSelectionGroup(null);
316 // Now add any sequences between min and max
318 for (int i = min; i < max; i++)
320 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
325 if (av.getSelectionGroup() == null)
327 SequenceGroup sg = new SequenceGroup();
328 sg.setStartRes(seqCanvas.cursorX);
329 sg.setEndRes(seqCanvas.cursorX);
330 sg.addSequence(sequence, false);
331 av.setSelectionGroup(sg);
333 ap.paintAlignment(false, false);
337 void insertGapAtCursor(boolean group)
339 groupEditing = group;
340 startseq = seqCanvas.cursorY;
341 lastres = seqCanvas.cursorX;
342 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
346 void deleteGapAtCursor(boolean group)
348 groupEditing = group;
349 startseq = seqCanvas.cursorY;
350 lastres = seqCanvas.cursorX + getKeyboardNo1();
351 editSequence(false, seqCanvas.cursorX);
355 void numberPressed(char value)
357 if (keyboardNo1 == null)
359 keyboardNo1 = new StringBuffer();
362 if (keyboardNo2 != null)
364 keyboardNo2.append(value);
368 keyboardNo1.append(value);
376 if (keyboardNo1 != null)
378 int value = Integer.parseInt(keyboardNo1.toString());
382 } catch (Exception x)
393 if (keyboardNo2 != null)
395 int value = Integer.parseInt(keyboardNo2.toString());
399 } catch (Exception x)
407 * Set status message in alignment panel
410 * aligned sequence object
414 * index of sequence in alignment
416 void setStatusMessage(SequenceI sequence, int column, int seq)
418 // TODO remove duplication of identical gui method
419 StringBuilder text = new StringBuilder(32);
420 String seqno = seq == -1 ? "" : " " + (seq + 1);
421 text.append("Sequence" + seqno + " ID: " + sequence.getName());
423 String residue = null;
425 * Try to translate the display character to residue name (null for gap).
427 final String displayChar = String.valueOf(sequence.getCharAt(column));
428 if (av.getAlignment().isNucleotide())
430 residue = ResidueProperties.nucleotideName.get(displayChar);
433 text.append(" Nucleotide: ").append(residue);
438 residue = "X".equalsIgnoreCase(displayChar) ? "X"
439 : ("*".equals(displayChar) ? "STOP"
440 : ResidueProperties.aa2Triplet.get(displayChar));
443 text.append(" Residue: ").append(residue);
450 pos = sequence.findPosition(column);
451 text.append(" (").append(Integer.toString(pos)).append(")");
454 ap.alignFrame.statusBar.setText(text.toString());
458 * Set the status bar message to highlight the first matched position in
462 * @return true if results were matched, false if not
464 private boolean setStatusMessage(SearchResultsI results)
466 AlignmentI al = this.av.getAlignment();
467 int sequenceIndex = al.findIndex(results);
468 if (sequenceIndex == -1)
472 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
473 for (SearchResultMatchI m : results.getResults())
475 SequenceI seq = m.getSequence();
476 if (seq.getDatasetSequence() != null)
478 seq = seq.getDatasetSequence();
484 * Convert position in sequence (base 1) to sequence character array
487 int start = m.getStart() - m.getSequence().getStart();
488 setStatusMessage(seq, start, sequenceIndex);
496 public void mousePressed(MouseEvent evt)
498 lastMousePress = evt.getPoint();
500 // For now, ignore the mouseWheel font resizing on Macs
501 // As the Button2_mask always seems to be true
502 if (Platform.isWinMiddleButton(evt))
504 mouseWheelPressed = true;
508 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
510 if (evt.isControlDown() || evt.isAltDown())
518 doMousePressedDefineMode(evt);
522 int seq = findSeq(evt);
523 int res = findColumn(evt);
525 if (seq < 0 || res < 0)
530 if ((seq < av.getAlignment().getHeight())
531 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
546 public void mouseClicked(MouseEvent evt)
548 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
549 if (evt.getClickCount() > 1)
551 if (av.getSelectionGroup() != null
552 && av.getSelectionGroup().getSize() == 1
553 && av.getSelectionGroup().getEndRes()
554 - av.getSelectionGroup().getStartRes() < 2)
556 av.setSelectionGroup(null);
559 int column = findColumn(evt);
560 List<SequenceFeature> features = findFeaturesAtColumn(sequence,
563 if (!features.isEmpty())
565 SearchResultsI highlight = new SearchResults();
566 highlight.addResult(sequence, features.get(0).getBegin(),
567 features.get(0).getEnd());
568 seqCanvas.highlightSearchResults(highlight);
569 seqCanvas.getFeatureRenderer().amendFeatures(
570 Collections.singletonList(sequence), features, false, ap);
571 av.setSearchResults(null); // clear highlighting
572 seqCanvas.repaint(); // draw new/amended features
578 public void mouseReleased(MouseEvent evt)
580 boolean didDrag = mouseDragging; // did we come here after a drag
581 mouseDragging = false;
582 mouseWheelPressed = false;
586 doMouseReleasedDefineMode(evt, didDrag);
594 int startWrapBlock = -1;
596 int wrappedBlock = -1;
599 * Returns the aligned sequence position (base 0) at the mouse position, or
600 * the closest visible one
605 int findColumn(MouseEvent evt)
610 int startRes = av.getRanges().getStartRes();
611 if (av.getWrapAlignment())
614 int hgap = av.getCharHeight();
615 if (av.getScaleAboveWrapped())
617 hgap += av.getCharHeight();
620 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
621 + hgap + seqCanvas.getAnnotationHeight();
625 x = Math.max(0, x - seqCanvas.LABEL_WEST);
627 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
633 wrappedBlock = y / cHeight;
634 wrappedBlock += startRes / cwidth;
635 int startOffset = startRes % cwidth; // in case start is scrolled right
637 res = wrappedBlock * cwidth + startOffset
638 + +Math.min(cwidth - 1, x / av.getCharWidth());
642 res = (x / av.getCharWidth()) + startRes;
645 if (av.hasHiddenColumns())
647 res = av.getAlignment().getHiddenColumns()
648 .visibleToAbsoluteColumn(res);
655 int findSeq(MouseEvent evt)
657 final int sqnum = findAlRow(evt);
658 return (sqnum < 0) ? 0 : sqnum;
664 * @return row in alignment that was selected (or -1 for column selection)
666 private int findAlRow(MouseEvent evt)
671 if (av.getWrapAlignment())
673 int hgap = av.getCharHeight();
674 if (av.getScaleAboveWrapped())
676 hgap += av.getCharHeight();
679 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
680 + hgap + seqCanvas.getAnnotationHeight();
684 seq = Math.min((y % cHeight) / av.getCharHeight(),
685 av.getAlignment().getHeight() - 1);
694 (y / av.getCharHeight()) + av.getRanges().getStartSeq(),
695 av.getAlignment().getHeight() - 1);
705 public void doMousePressed(MouseEvent evt)
708 int seq = findSeq(evt);
709 int res = findColumn(evt);
711 if (seq < av.getAlignment().getHeight()
712 && res < av.getAlignment().getSequenceAt(seq).getLength())
714 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
715 // Find the residue's position in the sequence (res is the position
733 public void mouseOverSequence(SequenceI sequence, int index, int pos)
735 String tmp = sequence.hashCode() + index + "";
736 if (lastMessage == null || !lastMessage.equals(tmp))
738 ssm.mouseOverSequence(sequence, index, pos, av);
745 public String highlightSequence(SearchResultsI results)
747 if (av.isFollowHighlight())
749 // don't allow highlight of protein/cDNA to also scroll a complementary
750 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
751 // over residue to change abruptly, causing highlighted residue in panel 2
752 // to change, causing a scroll in panel 1 etc)
753 ap.setToScrollComplementPanel(false);
754 if (ap.scrollToPosition(results, true))
756 ap.alignFrame.repaint();
758 ap.setToScrollComplementPanel(true);
760 setStatusMessage(results);
761 seqCanvas.highlightSearchResults(results);
766 public VamsasSource getVamsasSource()
768 return this.ap == null ? null : this.ap.av;
772 public void updateColours(SequenceI seq, int index)
774 System.out.println("update the seqPanel colours");
779 public void mouseMoved(MouseEvent evt)
781 final int column = findColumn(evt);
782 int seq = findSeq(evt);
784 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
793 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
794 if (column > sequence.getLength())
803 final char ch = sequence.getCharAt(column);
804 boolean isGapped = Comparison.isGap(ch);
805 // find residue at column (or nearest if at a gap)
806 int respos = sequence.findPosition(column);
808 if (ssm != null && !isGapped)
810 mouseOverSequence(sequence, column, respos);
813 StringBuilder text = new StringBuilder();
814 text.append("Sequence ").append(Integer.toString(seq + 1))
815 .append(" ID: ").append(sequence.getName());
819 if (av.getAlignment().isNucleotide())
821 String base = ResidueProperties.nucleotideName.get(ch);
822 text.append(" Nucleotide: ").append(base == null ? ch : base);
826 String residue = (ch == 'x' || ch == 'X') ? "X"
827 : ResidueProperties.aa2Triplet.get(String.valueOf(ch));
828 text.append(" Residue: ").append(residue == null ? ch : residue);
830 text.append(" (").append(Integer.toString(respos)).append(")");
833 ap.alignFrame.statusBar.setText(text.toString());
835 StringBuilder tooltipText = new StringBuilder();
836 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
839 for (int g = 0; g < groups.length; g++)
841 if (groups[g].getStartRes() <= column
842 && groups[g].getEndRes() >= column)
844 if (!groups[g].getName().startsWith("JTreeGroup")
845 && !groups[g].getName().startsWith("JGroup"))
847 tooltipText.append(groups[g].getName()).append(" ");
849 if (groups[g].getDescription() != null)
851 tooltipText.append(groups[g].getDescription());
853 tooltipText.append("\n");
859 * add feature details to tooltip, including any that straddle
862 if (av.isShowSequenceFeatures())
864 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
866 for (SequenceFeature sf : allFeatures)
868 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
870 if (sf.getDescription() != null)
872 tooltipText.append(" " + sf.getDescription());
875 if (sf.getValue("status") != null)
877 String status = sf.getValue("status").toString();
878 if (status.length() > 0)
880 tooltipText.append(" (" + sf.getValue("status") + ")");
883 tooltipText.append("\n");
889 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
893 tooltip.setTip(tooltipText.toString());
898 * Returns features at the specified aligned column on the given sequence.
899 * Non-positional features are not included. If the column has a gap, then
900 * enclosing features are included (but not contact features).
907 List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
909 return seqCanvas.getFeatureRenderer().findFeaturesAtColumn(sequence,
916 * set when the current UI interaction has resulted in a change that requires
917 * overview shading to be recalculated. this could be changed to something
918 * more expressive that indicates what actually has changed, so selective
919 * redraws can be applied
921 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
924 public void mouseDragged(MouseEvent evt)
926 if (mouseWheelPressed)
928 int oldWidth = av.getCharWidth();
930 // Which is bigger, left-right or up-down?
931 if (Math.abs(evt.getY() - lastMousePress.y) > Math
932 .abs(evt.getX() - lastMousePress.x))
934 int fontSize = av.font.getSize();
936 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
940 else if (evt.getY() > lastMousePress.y)
951 new Font(av.font.getName(), av.font.getStyle(), fontSize),
953 av.setCharWidth(oldWidth);
957 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
959 av.setCharWidth(av.getCharWidth() - 1);
961 else if (evt.getX() > lastMousePress.x)
963 av.setCharWidth(av.getCharWidth() + 1);
966 if (av.getCharWidth() < 1)
974 FontMetrics fm = getFontMetrics(av.getFont());
975 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
977 lastMousePress = evt.getPoint();
979 ap.paintAlignment(false, false);
980 ap.annotationPanel.image = null;
986 doMouseDraggedDefineMode(evt);
990 int res = findColumn(evt);
997 if ((lastres == -1) || (lastres == res))
1002 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1004 // dragLeft, delete gap
1005 editSequence(false, res);
1009 editSequence(true, res);
1012 mouseDragging = true;
1013 if (scrollThread != null)
1015 scrollThread.setEvent(evt);
1020 synchronized void editSequence(boolean insertGap, int startres)
1023 int fixedRight = -1;
1024 boolean fixedColumns = false;
1025 SequenceGroup sg = av.getSelectionGroup();
1027 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1029 if (!groupEditing && av.hasHiddenRows())
1031 if (av.isHiddenRepSequence(seq))
1033 sg = av.getRepresentedSequences(seq);
1034 groupEditing = true;
1038 StringBuffer message = new StringBuffer();
1041 message.append(MessageManager.getString("action.edit_group"))
1043 if (editCommand == null)
1045 editCommand = new EditCommand(
1046 MessageManager.getString("action.edit_group"));
1051 message.append(MessageManager.getString("label.edit_sequence"))
1052 .append(" " + seq.getName());
1053 String label = seq.getName();
1054 if (label.length() > 10)
1056 label = label.substring(0, 10);
1058 if (editCommand == null)
1060 editCommand = new EditCommand(MessageManager
1061 .formatMessage("label.edit_params", new String[]
1068 message.append(" insert ");
1072 message.append(" delete ");
1075 message.append(Math.abs(startres - lastres) + " gaps.");
1076 ap.alignFrame.statusBar.setText(message.toString());
1078 // Are we editing within a selection group?
1079 if (groupEditing || (sg != null
1080 && sg.getSequences(av.getHiddenRepSequences()).contains(seq)))
1082 fixedColumns = true;
1084 // sg might be null as the user may only see 1 sequence,
1085 // but the sequence represents a group
1088 if (!av.isHiddenRepSequence(seq))
1094 sg = av.getRepresentedSequences(seq);
1097 fixedLeft = sg.getStartRes();
1098 fixedRight = sg.getEndRes();
1100 if ((startres < fixedLeft && lastres >= fixedLeft)
1101 || (startres >= fixedLeft && lastres < fixedLeft)
1102 || (startres > fixedRight && lastres <= fixedRight)
1103 || (startres <= fixedRight && lastres > fixedRight))
1109 if (fixedLeft > startres)
1111 fixedRight = fixedLeft - 1;
1114 else if (fixedRight < startres)
1116 fixedLeft = fixedRight;
1121 if (av.hasHiddenColumns())
1123 fixedColumns = true;
1124 int y1 = av.getAlignment().getHiddenColumns()
1125 .getNextHiddenBoundary(true, startres);
1126 int y2 = av.getAlignment().getHiddenColumns()
1127 .getNextHiddenBoundary(false, startres);
1129 if ((insertGap && startres > y1 && lastres < y1)
1130 || (!insertGap && startres < y2 && lastres > y2))
1136 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1137 // Selection spans a hidden region
1138 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1146 fixedRight = y2 - 1;
1153 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1154 .toArray(new SequenceI[0]);
1159 // If the user has selected the whole sequence, and is dragging to
1160 // the right, we can still extend the alignment and selectionGroup
1161 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1162 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1164 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1165 fixedRight = sg.getEndRes();
1168 // Is it valid with fixed columns??
1169 // Find the next gap before the end
1170 // of the visible region boundary
1171 boolean blank = false;
1172 for (; fixedRight > lastres; fixedRight--)
1176 for (SequenceI gs : groupSeqs)
1178 for (int j = 0; j < startres - lastres; j++)
1180 if (!jalview.util.Comparison
1181 .isGap(gs.getCharAt(fixedRight - j)))
1196 if (sg.getSize() == av.getAlignment().getHeight())
1198 if ((av.hasHiddenColumns()
1199 && startres < av.getAlignment().getHiddenColumns()
1200 .getNextHiddenBoundary(false, startres)))
1206 int alWidth = av.getAlignment().getWidth();
1207 if (av.hasHiddenRows())
1209 int hwidth = av.getAlignment().getHiddenSequences()
1211 if (hwidth > alWidth)
1216 // We can still insert gaps if the selectionGroup
1217 // contains all the sequences
1218 sg.setEndRes(sg.getEndRes() + startres - lastres);
1219 fixedRight = alWidth + startres - lastres;
1230 else if (!insertGap)
1232 // / Are we able to delete?
1233 // ie are all columns blank?
1235 for (SequenceI gs : groupSeqs)
1237 for (int j = startres; j < lastres; j++)
1239 if (gs.getLength() <= j)
1244 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1246 // Not a gap, block edit not valid
1256 // dragging to the right
1257 if (fixedColumns && fixedRight != -1)
1259 for (int j = lastres; j < startres; j++)
1261 insertChar(j, groupSeqs, fixedRight);
1266 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1267 startres - lastres, av.getAlignment(), true);
1272 // dragging to the left
1273 if (fixedColumns && fixedRight != -1)
1275 for (int j = lastres; j > startres; j--)
1277 deleteChar(startres, groupSeqs, fixedRight);
1282 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1283 lastres - startres, av.getAlignment(), true);
1289 // ///Editing a single sequence///////////
1293 // dragging to the right
1294 if (fixedColumns && fixedRight != -1)
1296 for (int j = lastres; j < startres; j++)
1298 insertChar(j, new SequenceI[] { seq }, fixedRight);
1303 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[] { seq },
1304 lastres, startres - lastres, av.getAlignment(), true);
1309 // dragging to the left
1310 if (fixedColumns && fixedRight != -1)
1312 for (int j = lastres; j > startres; j--)
1314 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1319 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1324 // could be a keyboard edit trying to delete none gaps
1326 for (int m = startres; m < lastres; m++)
1328 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1337 editCommand.appendEdit(Action.DELETE_GAP,
1339 { seq }, startres, max, av.getAlignment(), true);
1346 seqCanvas.repaint();
1349 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1351 int blankColumn = fixedColumn;
1352 for (int s = 0; s < seq.length; s++)
1354 // Find the next gap before the end of the visible region boundary
1355 // If lastCol > j, theres a boundary after the gap insertion
1357 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1359 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1361 // Theres a space, so break and insert the gap
1366 if (blankColumn <= j)
1368 blankColumn = fixedColumn;
1374 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1375 av.getAlignment(), true);
1377 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1382 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1385 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1388 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1389 av.getAlignment(), true);
1392 // ////////////////////////////////////////
1393 // ///Everything below this is for defining the boundary of the rubberband
1394 // ////////////////////////////////////////
1395 public void doMousePressedDefineMode(MouseEvent evt)
1397 if (scrollThread != null)
1399 scrollThread.threadRunning = false;
1400 scrollThread = null;
1403 int column = findColumn(evt);
1404 int seq = findSeq(evt);
1406 startWrapBlock = wrappedBlock;
1413 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1415 if (sequence == null || column > sequence.getLength())
1420 stretchGroup = av.getSelectionGroup();
1422 if (stretchGroup == null || !stretchGroup.contains(sequence, column))
1424 stretchGroup = av.getAlignment().findGroup(sequence, column);
1425 if (stretchGroup != null)
1427 // only update the current selection if the popup menu has a group to
1429 av.setSelectionGroup(stretchGroup);
1433 // DETECT RIGHT MOUSE BUTTON IN AWT
1434 if ((evt.getModifiersEx()
1435 & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK)
1437 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
1438 sequence.findPosition(column + 1));
1440 Vector<String> links = null;
1441 for (SequenceFeature sf : allFeatures)
1443 if (sf.links != null)
1447 links = new Vector<>();
1449 links.addAll(sf.links);
1452 APopupMenu popup = new APopupMenu(ap, null, links);
1454 popup.show(this, evt.getX(), evt.getY());
1460 seqCanvas.cursorX = findColumn(evt);
1461 seqCanvas.cursorY = findSeq(evt);
1462 seqCanvas.repaint();
1466 // Only if left mouse button do we want to change group sizes
1468 if (stretchGroup == null)
1470 // define a new group here
1471 SequenceGroup sg = new SequenceGroup();
1472 sg.setStartRes(column);
1473 sg.setEndRes(column);
1474 sg.addSequence(sequence, false);
1475 av.setSelectionGroup(sg);
1478 if (av.getConservationSelected())
1480 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1483 if (av.getAbovePIDThreshold())
1485 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1492 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1494 if (stretchGroup == null)
1498 // always do this - annotation has own state
1499 // but defer colourscheme update until hidden sequences are passed in
1500 boolean vischange = stretchGroup.recalcConservation(true);
1501 // here we rely on stretchGroup == av.getSelection()
1502 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1504 if (stretchGroup.cs != null)
1506 stretchGroup.cs.alignmentChanged(stretchGroup,
1507 av.getHiddenRepSequences());
1509 if (stretchGroup.cs.conservationApplied())
1511 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1512 stretchGroup.getName());
1514 if (stretchGroup.cs.getThreshold() > 0)
1516 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1517 stretchGroup.getName());
1520 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1521 ap.paintAlignment(needOverviewUpdate, needOverviewUpdate);
1522 needOverviewUpdate = false;
1523 changeEndRes = false;
1524 changeStartRes = false;
1525 stretchGroup = null;
1529 public void doMouseDraggedDefineMode(MouseEvent evt)
1531 int res = findColumn(evt);
1532 int y = findSeq(evt);
1534 if (wrappedBlock != startWrapBlock)
1539 if (stretchGroup == null)
1544 mouseDragging = true;
1546 if (y > av.getAlignment().getHeight())
1548 y = av.getAlignment().getHeight() - 1;
1551 if (res >= av.getAlignment().getWidth())
1553 res = av.getAlignment().getWidth() - 1;
1556 if (stretchGroup.getEndRes() == res)
1558 // Edit end res position of selected group
1559 changeEndRes = true;
1561 else if (stretchGroup.getStartRes() == res)
1563 // Edit start res position of selected group
1564 changeStartRes = true;
1574 if (res > (stretchGroup.getStartRes() - 1))
1576 stretchGroup.setEndRes(res);
1577 needOverviewUpdate |= av.isSelectionDefinedGroup();
1580 else if (changeStartRes)
1582 if (res < (stretchGroup.getEndRes() + 1))
1584 stretchGroup.setStartRes(res);
1585 needOverviewUpdate |= av.isSelectionDefinedGroup();
1589 int dragDirection = 0;
1595 else if (y < oldSeq)
1600 while ((y != oldSeq) && (oldSeq > -1)
1601 && (y < av.getAlignment().getHeight()))
1603 // This routine ensures we don't skip any sequences, as the
1604 // selection is quite slow.
1605 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1607 oldSeq += dragDirection;
1614 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1616 if (stretchGroup.getSequences(null).contains(nextSeq))
1618 stretchGroup.deleteSequence(seq, false);
1619 needOverviewUpdate |= av.isSelectionDefinedGroup();
1625 stretchGroup.addSequence(seq, false);
1628 stretchGroup.addSequence(nextSeq, false);
1629 needOverviewUpdate |= av.isSelectionDefinedGroup();
1638 if (res > av.getRanges().getEndRes()
1639 || res < av.getRanges().getStartRes()
1640 || y < av.getRanges().getStartSeq()
1641 || y > av.getRanges().getEndSeq())
1646 if ((scrollThread != null) && (scrollThread.isRunning()))
1648 scrollThread.setEvent(evt);
1651 seqCanvas.repaint();
1655 public void mouseEntered(MouseEvent e)
1662 if ((scrollThread != null) && (scrollThread.isRunning()))
1664 scrollThread.stopScrolling();
1665 scrollThread = null;
1670 public void mouseExited(MouseEvent e)
1672 if (av.getWrapAlignment())
1677 if (mouseDragging && scrollThread == null)
1679 scrollThread = new ScrollThread();
1683 void scrollCanvas(MouseEvent evt)
1687 if ((scrollThread != null) && (scrollThread.isRunning()))
1689 scrollThread.stopScrolling();
1690 scrollThread = null;
1692 mouseDragging = false;
1696 if (scrollThread == null)
1698 scrollThread = new ScrollThread();
1701 mouseDragging = true;
1702 scrollThread.setEvent(evt);
1707 // this class allows scrolling off the bottom of the visible alignment
1708 class ScrollThread extends Thread
1712 private volatile boolean threadRunning = true;
1714 public ScrollThread()
1719 public void setEvent(MouseEvent e)
1724 public void stopScrolling()
1726 threadRunning = false;
1729 public boolean isRunning()
1731 return threadRunning;
1737 while (threadRunning)
1743 if (mouseDragging && evt.getY() < 0
1744 && av.getRanges().getStartSeq() > 0)
1746 av.getRanges().scrollUp(true);
1749 if (mouseDragging && evt.getY() >= getSize().height && av
1750 .getAlignment().getHeight() > av.getRanges().getEndSeq())
1752 av.getRanges().scrollUp(false);
1755 if (mouseDragging && evt.getX() < 0)
1757 av.getRanges().scrollRight(false);
1760 else if (mouseDragging && evt.getX() >= getSize().width)
1762 av.getRanges().scrollRight(true);
1769 } catch (Exception ex)
1777 * modify current selection according to a received message.
1780 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1781 HiddenColumns hidden, SelectionSource source)
1783 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1784 // handles selection messages...
1785 // TODO: extend config options to allow user to control if selections may be
1786 // shared between viewports.
1787 if (av != null && (av == source || !av.followSelection
1788 || (source instanceof AlignViewport
1789 && ((AlignmentViewport) source).getSequenceSetId()
1790 .equals(av.getSequenceSetId()))))
1796 * Check for selection in a view of which this one is a dna/protein
1799 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1804 // do we want to thread this ? (contention with seqsel and colsel locks, I
1807 * only copy colsel if there is a real intersection between
1808 * sequence selection and this panel's alignment
1810 boolean repaint = false;
1811 boolean copycolsel = false;
1812 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1814 SequenceGroup sgroup = null;
1815 if (seqsel != null && seqsel.getSize() > 0)
1817 if (av.getAlignment() == null)
1819 System.out.println("Selection message: alignviewport av SeqSetId="
1820 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1821 + " 's alignment is NULL! returning immediatly.");
1824 sgroup = seqsel.intersect(av.getAlignment(),
1825 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1826 if ((sgroup != null && sgroup.getSize() > 0))
1831 if (sgroup != null && sgroup.getSize() > 0)
1833 av.setSelectionGroup(sgroup);
1837 av.setSelectionGroup(null);
1839 repaint = av.isSelectionGroupChanged(true);
1841 if (copycolsel && (av.getColumnSelection() == null
1842 || !av.isColSelChanged(true)))
1844 // the current selection is unset or from a previous message
1845 // so import the new colsel.
1846 if (colsel == null || colsel.isEmpty())
1848 if (av.getColumnSelection() != null)
1850 av.getColumnSelection().clear();
1855 // TODO: shift colSel according to the intersecting sequences
1856 if (av.getColumnSelection() == null)
1858 av.setColumnSelection(new ColumnSelection(colsel));
1862 av.getColumnSelection().setElementsFrom(colsel,
1863 av.getAlignment().getHiddenColumns());
1866 repaint |= av.isColSelChanged(true);
1868 if (copycolsel && av.hasHiddenColumns()
1869 && (av.getColumnSelection() == null))
1871 System.err.println("Bad things");
1875 ap.scalePanelHolder.repaint();
1881 * scroll to the given row/column - or nearest visible location
1886 public void scrollTo(int row, int column)
1889 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1890 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1891 ap.scrollTo(column, column, row, true, true);
1895 * scroll to the given row - or nearest visible location
1899 public void scrollToRow(int row)
1902 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1903 ap.scrollTo(ap.av.getRanges().getStartRes(),
1904 ap.av.getRanges().getStartRes(), row, true, true);
1908 * scroll to the given column - or nearest visible location
1912 public void scrollToColumn(int column)
1915 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1916 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true,
1921 * If this panel is a cdna/protein translation view of the selection source,
1922 * tries to map the source selection to a local one, and returns true. Else
1929 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1930 ColumnSelection colsel, HiddenColumns hidden,
1931 SelectionSource source)
1933 if (!(source instanceof AlignViewportI))
1937 final AlignViewportI sourceAv = (AlignViewportI) source;
1938 if (sourceAv.getCodingComplement() != av
1939 && av.getCodingComplement() != sourceAv)
1945 * Map sequence selection
1947 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1948 av.setSelectionGroup(sg);
1949 av.isSelectionGroupChanged(true);
1952 * Map column selection
1954 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1956 ColumnSelection cs = new ColumnSelection();
1957 HiddenColumns hs = new HiddenColumns();
1958 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1959 av.setColumnSelection(cs);
1960 av.getAlignment().setHiddenColumns(hs);
1962 ap.scalePanelHolder.repaint();