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.firePropertyChange("alignment", null,
136 av.getAlignment().getSequences());
142 groupEditing = false;
150 seqCanvas.cursorY = getKeyboardNo1() - 1;
151 scrollToVisible(true);
154 void setCursorColumn()
156 seqCanvas.cursorX = getKeyboardNo1() - 1;
157 scrollToVisible(true);
160 void setCursorRowAndColumn()
162 if (keyboardNo2 == null)
164 keyboardNo2 = new StringBuffer();
168 seqCanvas.cursorX = getKeyboardNo1() - 1;
169 seqCanvas.cursorY = getKeyboardNo2() - 1;
170 scrollToVisible(true);
174 void setCursorPosition()
176 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
178 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
179 scrollToVisible(true);
182 void moveCursor(int dx, int dy)
184 seqCanvas.cursorX += dx;
185 seqCanvas.cursorY += dy;
186 if (av.hasHiddenColumns() && !av.getAlignment().getHiddenColumns()
187 .isVisible(seqCanvas.cursorX))
189 int original = seqCanvas.cursorX - dx;
190 int maxWidth = av.getAlignment().getWidth();
192 while (!av.getAlignment().getHiddenColumns()
193 .isVisible(seqCanvas.cursorX) && seqCanvas.cursorX < maxWidth
194 && seqCanvas.cursorX > 0)
196 seqCanvas.cursorX += dx;
199 if (seqCanvas.cursorX >= maxWidth || !av.getAlignment()
200 .getHiddenColumns().isVisible(seqCanvas.cursorX))
202 seqCanvas.cursorX = original;
205 scrollToVisible(false);
209 * Scroll to make the cursor visible in the viewport.
212 * just jump to the location rather than scrolling
214 void scrollToVisible(boolean jump)
216 if (seqCanvas.cursorX < 0)
218 seqCanvas.cursorX = 0;
220 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
222 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
225 if (seqCanvas.cursorY < 0)
227 seqCanvas.cursorY = 0;
229 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
231 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
236 boolean repaintNeeded = true;
239 // only need to repaint if the viewport did not move, as otherwise it will
241 repaintNeeded = !av.getRanges().setViewportLocation(seqCanvas.cursorX,
246 if (av.getWrapAlignment())
248 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
252 av.getRanges().scrollToVisible(seqCanvas.cursorX,
256 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
257 seqCanvas.cursorX, seqCanvas.cursorY);
265 void setSelectionAreaAtCursor(boolean topLeft)
267 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
269 if (av.getSelectionGroup() != null)
271 SequenceGroup sg = av.getSelectionGroup();
272 // Find the top and bottom of this group
273 int min = av.getAlignment().getHeight(), max = 0;
274 for (int i = 0; i < sg.getSize(); i++)
276 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
291 sg.setStartRes(seqCanvas.cursorX);
292 if (sg.getEndRes() < seqCanvas.cursorX)
294 sg.setEndRes(seqCanvas.cursorX);
297 min = seqCanvas.cursorY;
301 sg.setEndRes(seqCanvas.cursorX);
302 if (sg.getStartRes() > seqCanvas.cursorX)
304 sg.setStartRes(seqCanvas.cursorX);
307 max = seqCanvas.cursorY + 1;
312 // Only the user can do this
313 av.setSelectionGroup(null);
317 // Now add any sequences between min and max
319 for (int i = min; i < max; i++)
321 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
326 if (av.getSelectionGroup() == null)
328 SequenceGroup sg = new SequenceGroup();
329 sg.setStartRes(seqCanvas.cursorX);
330 sg.setEndRes(seqCanvas.cursorX);
331 sg.addSequence(sequence, false);
332 av.setSelectionGroup(sg);
334 ap.paintAlignment(false, false);
338 void insertGapAtCursor(boolean group)
340 groupEditing = group;
341 startseq = seqCanvas.cursorY;
342 lastres = seqCanvas.cursorX;
343 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
347 void deleteGapAtCursor(boolean group)
349 groupEditing = group;
350 startseq = seqCanvas.cursorY;
351 lastres = seqCanvas.cursorX + getKeyboardNo1();
352 editSequence(false, seqCanvas.cursorX);
356 void numberPressed(char value)
358 if (keyboardNo1 == null)
360 keyboardNo1 = new StringBuffer();
363 if (keyboardNo2 != null)
365 keyboardNo2.append(value);
369 keyboardNo1.append(value);
377 if (keyboardNo1 != null)
379 int value = Integer.parseInt(keyboardNo1.toString());
383 } catch (Exception x)
394 if (keyboardNo2 != null)
396 int value = Integer.parseInt(keyboardNo2.toString());
400 } catch (Exception x)
408 * Set status message in alignment panel
411 * aligned sequence object
415 * index of sequence in alignment
417 void setStatusMessage(SequenceI sequence, int column, int seq)
419 // TODO remove duplication of identical gui method
420 StringBuilder text = new StringBuilder(32);
421 String seqno = seq == -1 ? "" : " " + (seq + 1);
422 text.append("Sequence" + seqno + " ID: " + sequence.getName());
424 String residue = null;
426 * Try to translate the display character to residue name (null for gap).
428 final String displayChar = String.valueOf(sequence.getCharAt(column));
429 if (av.getAlignment().isNucleotide())
431 residue = ResidueProperties.nucleotideName.get(displayChar);
434 text.append(" Nucleotide: ").append(residue);
439 residue = "X".equalsIgnoreCase(displayChar) ? "X"
440 : ("*".equals(displayChar) ? "STOP"
441 : ResidueProperties.aa2Triplet.get(displayChar));
444 text.append(" Residue: ").append(residue);
451 pos = sequence.findPosition(column);
452 text.append(" (").append(Integer.toString(pos)).append(")");
455 ap.alignFrame.statusBar.setText(text.toString());
459 * Set the status bar message to highlight the first matched position in
463 * @return true if results were matched, false if not
465 private boolean setStatusMessage(SearchResultsI results)
467 AlignmentI al = this.av.getAlignment();
468 int sequenceIndex = al.findIndex(results);
469 if (sequenceIndex == -1)
473 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
474 for (SearchResultMatchI m : results.getResults())
476 SequenceI seq = m.getSequence();
477 if (seq.getDatasetSequence() != null)
479 seq = seq.getDatasetSequence();
485 * Convert position in sequence (base 1) to sequence character array
488 int start = m.getStart() - m.getSequence().getStart();
489 setStatusMessage(seq, start, sequenceIndex);
497 public void mousePressed(MouseEvent evt)
499 lastMousePress = evt.getPoint();
501 // For now, ignore the mouseWheel font resizing on Macs
502 // As the Button2_mask always seems to be true
504 if (Platform.isWinMiddleButton(evt))
506 mouseWheelPressed = true;
510 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
512 if (evt.isControlDown() || evt.isAltDown())
520 doMousePressedDefineMode(evt);
524 int seq = findSeq(evt);
525 int res = findColumn(evt);
527 if (seq < 0 || res < 0)
532 if ((seq < av.getAlignment().getHeight())
533 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
548 public void mouseClicked(MouseEvent evt)
550 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
551 if (evt.getClickCount() > 1)
553 if (av.getSelectionGroup() != null
554 && av.getSelectionGroup().getSize() == 1
555 && av.getSelectionGroup().getEndRes()
556 - av.getSelectionGroup().getStartRes() < 2)
558 av.setSelectionGroup(null);
561 int column = findColumn(evt);
562 List<SequenceFeature> features = findFeaturesAtColumn(sequence,
565 if (!features.isEmpty())
567 SearchResultsI highlight = new SearchResults();
568 highlight.addResult(sequence, features.get(0).getBegin(),
569 features.get(0).getEnd());
570 seqCanvas.highlightSearchResults(highlight);
571 seqCanvas.getFeatureRenderer().amendFeatures(
572 Collections.singletonList(sequence), features, false, ap);
573 av.setSearchResults(null); // clear highlighting
574 seqCanvas.repaint(); // draw new/amended features
580 public void mouseReleased(MouseEvent evt)
582 boolean didDrag = mouseDragging; // did we come here after a drag
583 mouseDragging = false;
584 mouseWheelPressed = false;
588 doMouseReleasedDefineMode(evt, didDrag);
596 int startWrapBlock = -1;
598 int wrappedBlock = -1;
601 * Returns the aligned sequence position (base 0) at the mouse position, or
602 * the closest visible one
607 int findColumn(MouseEvent evt)
612 int startRes = av.getRanges().getStartRes();
613 if (av.getWrapAlignment())
616 int hgap = av.getCharHeight();
617 if (av.getScaleAboveWrapped())
619 hgap += av.getCharHeight();
622 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
623 + hgap + seqCanvas.getAnnotationHeight();
627 x = Math.max(0, x - seqCanvas.LABEL_WEST);
629 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
635 wrappedBlock = y / cHeight;
636 wrappedBlock += startRes / cwidth;
637 int startOffset = startRes % cwidth; // in case start is scrolled right
639 res = wrappedBlock * cwidth + startOffset
640 + +Math.min(cwidth - 1, x / av.getCharWidth());
644 res = (x / av.getCharWidth()) + startRes;
647 if (av.hasHiddenColumns())
649 res = av.getAlignment().getHiddenColumns()
650 .visibleToAbsoluteColumn(res);
657 int findSeq(MouseEvent evt)
659 final int sqnum = findAlRow(evt);
660 return (sqnum < 0) ? 0 : sqnum;
666 * @return row in alignment that was selected (or -1 for column selection)
668 private int findAlRow(MouseEvent evt)
673 if (av.getWrapAlignment())
675 int hgap = av.getCharHeight();
676 if (av.getScaleAboveWrapped())
678 hgap += av.getCharHeight();
681 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
682 + hgap + seqCanvas.getAnnotationHeight();
686 seq = Math.min((y % cHeight) / av.getCharHeight(),
687 av.getAlignment().getHeight() - 1);
696 (y / av.getCharHeight()) + av.getRanges().getStartSeq(),
697 av.getAlignment().getHeight() - 1);
707 public void doMousePressed(MouseEvent evt)
710 int seq = findSeq(evt);
711 int res = findColumn(evt);
713 if (seq < av.getAlignment().getHeight()
714 && res < av.getAlignment().getSequenceAt(seq).getLength())
716 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
717 // Find the residue's position in the sequence (res is the position
735 public void mouseOverSequence(SequenceI sequence, int index, int pos)
737 String tmp = sequence.hashCode() + index + "";
738 if (lastMessage == null || !lastMessage.equals(tmp))
740 ssm.mouseOverSequence(sequence, index, pos, av);
747 public void highlightSequence(SearchResultsI results)
749 if (av.isFollowHighlight())
751 // don't allow highlight of protein/cDNA to also scroll a complementary
752 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
753 // over residue to change abruptly, causing highlighted residue in panel 2
754 // to change, causing a scroll in panel 1 etc)
755 ap.setToScrollComplementPanel(false);
756 if (ap.scrollToPosition(results, true))
758 ap.alignFrame.repaint();
760 ap.setToScrollComplementPanel(true);
762 setStatusMessage(results);
763 seqCanvas.highlightSearchResults(results);
768 public VamsasSource getVamsasSource()
770 return this.ap == null ? null : this.ap.av;
774 public void updateColours(SequenceI seq, int index)
776 System.out.println("update the seqPanel colours");
781 public void mouseMoved(MouseEvent evt)
783 final int column = findColumn(evt);
784 int seq = findSeq(evt);
786 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
795 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
796 if (column > sequence.getLength())
805 final char ch = sequence.getCharAt(column);
806 boolean isGapped = Comparison.isGap(ch);
807 // find residue at column (or nearest if at a gap)
808 int respos = sequence.findPosition(column);
810 if (ssm != null && !isGapped)
812 mouseOverSequence(sequence, column, respos);
815 StringBuilder text = new StringBuilder();
816 text.append("Sequence ").append(Integer.toString(seq + 1))
817 .append(" ID: ").append(sequence.getName());
821 if (av.getAlignment().isNucleotide())
823 String base = ResidueProperties.nucleotideName
824 .get(String.valueOf(ch)); // BH 2020.04.07 just Eclipse warning
825 text.append(" Nucleotide: ").append(base == null ? ch : base);
829 String residue = (ch == 'x' || ch == 'X') ? "X"
830 : ResidueProperties.aa2Triplet.get(String.valueOf(ch));
831 text.append(" Residue: ").append(residue == null ? ch : residue);
833 text.append(" (").append(Integer.toString(respos)).append(")");
836 ap.alignFrame.statusBar.setText(text.toString());
838 StringBuilder tooltipText = new StringBuilder();
839 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
842 for (int g = 0; g < groups.length; g++)
844 if (groups[g].getStartRes() <= column
845 && groups[g].getEndRes() >= column)
847 if (!groups[g].getName().startsWith("JTreeGroup")
848 && !groups[g].getName().startsWith("JGroup"))
850 tooltipText.append(groups[g].getName()).append(" ");
852 if (groups[g].getDescription() != null)
854 tooltipText.append(groups[g].getDescription());
856 tooltipText.append("\n");
862 * add feature details to tooltip, including any that straddle
865 if (av.isShowSequenceFeatures())
867 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
869 for (SequenceFeature sf : allFeatures)
871 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
873 if (sf.getDescription() != null)
875 tooltipText.append(" " + sf.getDescription());
878 if (sf.getValue("status") != null)
880 String status = sf.getValue("status").toString();
881 if (status.length() > 0)
883 tooltipText.append(" (" + sf.getValue("status") + ")");
886 tooltipText.append("\n");
892 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
896 tooltip.setTip(tooltipText.toString());
901 * Returns features at the specified aligned column on the given sequence.
902 * Non-positional features are not included. If the column has a gap, then
903 * enclosing features are included (but not contact features).
910 List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
912 return seqCanvas.getFeatureRenderer().findFeaturesAtColumn(sequence, column);
918 * set when the current UI interaction has resulted in a change that requires
919 * overview shading to be recalculated. this could be changed to something
920 * more expressive that indicates what actually has changed, so selective
921 * redraws can be applied
923 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
926 public void mouseDragged(MouseEvent evt)
928 if (mouseWheelPressed)
930 int oldWidth = av.getCharWidth();
932 // Which is bigger, left-right or up-down?
933 if (Math.abs(evt.getY() - lastMousePress.y) > Math
934 .abs(evt.getX() - lastMousePress.x))
936 int fontSize = av.font.getSize();
938 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
942 else if (evt.getY() > lastMousePress.y)
953 new Font(av.font.getName(), av.font.getStyle(), fontSize),
955 av.setCharWidth(oldWidth);
959 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
961 av.setCharWidth(av.getCharWidth() - 1);
963 else if (evt.getX() > lastMousePress.x)
965 av.setCharWidth(av.getCharWidth() + 1);
968 if (av.getCharWidth() < 1)
976 FontMetrics fm = getFontMetrics(av.getFont());
977 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
979 lastMousePress = evt.getPoint();
981 ap.paintAlignment(false, false);
982 ap.annotationPanel.image = null;
988 doMouseDraggedDefineMode(evt);
992 int res = findColumn(evt);
999 if ((lastres == -1) || (lastres == res))
1004 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1006 // dragLeft, delete gap
1007 editSequence(false, res);
1011 editSequence(true, res);
1014 mouseDragging = true;
1015 if (scrollThread != null)
1017 scrollThread.setEvent(evt);
1022 synchronized void editSequence(boolean insertGap, int startres)
1025 int fixedRight = -1;
1026 boolean fixedColumns = false;
1027 SequenceGroup sg = av.getSelectionGroup();
1029 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1031 if (!groupEditing && av.hasHiddenRows())
1033 if (av.isHiddenRepSequence(seq))
1035 sg = av.getRepresentedSequences(seq);
1036 groupEditing = true;
1040 StringBuffer message = new StringBuffer();
1043 message.append(MessageManager.getString("action.edit_group"))
1045 if (editCommand == null)
1047 editCommand = new EditCommand(
1048 MessageManager.getString("action.edit_group"));
1053 message.append(MessageManager.getString("label.edit_sequence"))
1054 .append(" " + seq.getName());
1055 String label = seq.getName();
1056 if (label.length() > 10)
1058 label = label.substring(0, 10);
1060 if (editCommand == null)
1062 editCommand = new EditCommand(MessageManager
1063 .formatMessage("label.edit_params", new String[]
1070 message.append(" insert ");
1074 message.append(" delete ");
1077 message.append(Math.abs(startres - lastres) + " gaps.");
1078 ap.alignFrame.statusBar.setText(message.toString());
1080 // Are we editing within a selection group?
1081 if (groupEditing || (sg != null
1082 && sg.getSequences(av.getHiddenRepSequences()).contains(seq)))
1084 fixedColumns = true;
1086 // sg might be null as the user may only see 1 sequence,
1087 // but the sequence represents a group
1090 if (!av.isHiddenRepSequence(seq))
1096 sg = av.getRepresentedSequences(seq);
1099 fixedLeft = sg.getStartRes();
1100 fixedRight = sg.getEndRes();
1102 if ((startres < fixedLeft && lastres >= fixedLeft)
1103 || (startres >= fixedLeft && lastres < fixedLeft)
1104 || (startres > fixedRight && lastres <= fixedRight)
1105 || (startres <= fixedRight && lastres > fixedRight))
1111 if (fixedLeft > startres)
1113 fixedRight = fixedLeft - 1;
1116 else if (fixedRight < startres)
1118 fixedLeft = fixedRight;
1123 if (av.hasHiddenColumns())
1125 fixedColumns = true;
1126 int y1 = av.getAlignment().getHiddenColumns()
1127 .getNextHiddenBoundary(true, startres);
1128 int y2 = av.getAlignment().getHiddenColumns()
1129 .getNextHiddenBoundary(false, startres);
1131 if ((insertGap && startres > y1 && lastres < y1)
1132 || (!insertGap && startres < y2 && lastres > y2))
1138 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1139 // Selection spans a hidden region
1140 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1148 fixedRight = y2 - 1;
1155 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1156 .toArray(new SequenceI[0]);
1161 // If the user has selected the whole sequence, and is dragging to
1162 // the right, we can still extend the alignment and selectionGroup
1163 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1164 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1166 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1167 fixedRight = sg.getEndRes();
1170 // Is it valid with fixed columns??
1171 // Find the next gap before the end
1172 // of the visible region boundary
1173 boolean blank = false;
1174 for (; fixedRight > lastres; fixedRight--)
1178 for (SequenceI gs : groupSeqs)
1180 for (int j = 0; j < startres - lastres; j++)
1182 if (!jalview.util.Comparison
1183 .isGap(gs.getCharAt(fixedRight - j)))
1198 if (sg.getSize() == av.getAlignment().getHeight())
1200 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1202 .getNextHiddenBoundary(false, startres)))
1208 int alWidth = av.getAlignment().getWidth();
1209 if (av.hasHiddenRows())
1211 int hwidth = av.getAlignment().getHiddenSequences()
1213 if (hwidth > alWidth)
1218 // We can still insert gaps if the selectionGroup
1219 // contains all the sequences
1220 sg.setEndRes(sg.getEndRes() + startres - lastres);
1221 fixedRight = alWidth + startres - lastres;
1232 else if (!insertGap)
1234 // / Are we able to delete?
1235 // ie are all columns blank?
1237 for (SequenceI gs : groupSeqs)
1239 for (int j = startres; j < lastres; j++)
1241 if (gs.getLength() <= j)
1246 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1248 // Not a gap, block edit not valid
1258 // dragging to the right
1259 if (fixedColumns && fixedRight != -1)
1261 for (int j = lastres; j < startres; j++)
1263 insertChar(j, groupSeqs, fixedRight);
1268 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1269 startres - lastres, av.getAlignment(), true);
1274 // dragging to the left
1275 if (fixedColumns && fixedRight != -1)
1277 for (int j = lastres; j > startres; j--)
1279 deleteChar(startres, groupSeqs, fixedRight);
1284 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1285 lastres - startres, av.getAlignment(), true);
1291 // ///Editing a single sequence///////////
1295 // dragging to the right
1296 if (fixedColumns && fixedRight != -1)
1298 for (int j = lastres; j < startres; j++)
1300 insertChar(j, new SequenceI[] { seq }, fixedRight);
1305 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[] { seq },
1306 lastres, startres - lastres, av.getAlignment(), true);
1311 // dragging to the left
1312 if (fixedColumns && fixedRight != -1)
1314 for (int j = lastres; j > startres; j--)
1316 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1321 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1326 // could be a keyboard edit trying to delete none gaps
1328 for (int m = startres; m < lastres; m++)
1330 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1339 editCommand.appendEdit(Action.DELETE_GAP,
1341 { seq }, startres, max, av.getAlignment(), true);
1348 seqCanvas.repaint();
1351 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1353 int blankColumn = fixedColumn;
1354 for (int s = 0; s < seq.length; s++)
1356 // Find the next gap before the end of the visible region boundary
1357 // If lastCol > j, theres a boundary after the gap insertion
1359 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1361 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1363 // Theres a space, so break and insert the gap
1368 if (blankColumn <= j)
1370 blankColumn = fixedColumn;
1376 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1377 av.getAlignment(), true);
1379 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1384 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1387 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1390 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1391 av.getAlignment(), true);
1394 // ////////////////////////////////////////
1395 // ///Everything below this is for defining the boundary of the rubberband
1396 // ////////////////////////////////////////
1397 public void doMousePressedDefineMode(MouseEvent evt)
1399 if (scrollThread != null)
1401 scrollThread.threadRunning = false;
1402 scrollThread = null;
1405 int column = findColumn(evt);
1406 int seq = findSeq(evt);
1408 startWrapBlock = wrappedBlock;
1415 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1417 if (sequence == null || column > sequence.getLength())
1422 stretchGroup = av.getSelectionGroup();
1424 if (stretchGroup == null || !stretchGroup.contains(sequence, column))
1426 stretchGroup = av.getAlignment().findGroup(sequence, column);
1427 if (stretchGroup != null)
1429 // only update the current selection if the popup menu has a group to
1431 av.setSelectionGroup(stretchGroup);
1435 // DETECT RIGHT MOUSE BUTTON IN AWT
1436 if ((evt.getModifiers()
1437 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1439 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
1440 sequence.findPosition(column + 1));
1442 Vector<String> links = null;
1443 for (SequenceFeature sf : allFeatures)
1445 if (sf.links != null)
1449 links = new Vector<>();
1451 links.addAll(sf.links);
1454 APopupMenu popup = new APopupMenu(ap, null, links);
1456 popup.show(this, evt.getX(), evt.getY());
1462 seqCanvas.cursorX = findColumn(evt);
1463 seqCanvas.cursorY = findSeq(evt);
1464 seqCanvas.repaint();
1468 // Only if left mouse button do we want to change group sizes
1470 if (stretchGroup == null)
1472 // define a new group here
1473 SequenceGroup sg = new SequenceGroup();
1474 sg.setStartRes(column);
1475 sg.setEndRes(column);
1476 sg.addSequence(sequence, false);
1477 av.setSelectionGroup(sg);
1480 if (av.getConservationSelected())
1482 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1485 if (av.getAbovePIDThreshold())
1487 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1494 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1496 if (stretchGroup == null)
1500 // always do this - annotation has own state
1501 // but defer colourscheme update until hidden sequences are passed in
1502 boolean vischange = stretchGroup.recalcConservation(true);
1503 // here we rely on stretchGroup == av.getSelection()
1504 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1506 if (stretchGroup.cs != null)
1508 stretchGroup.cs.alignmentChanged(stretchGroup,
1509 av.getHiddenRepSequences());
1511 if (stretchGroup.cs.conservationApplied())
1513 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1514 stretchGroup.getName());
1516 if (stretchGroup.cs.getThreshold() > 0)
1518 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1519 stretchGroup.getName());
1522 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1523 ap.paintAlignment(needOverviewUpdate, needOverviewUpdate);
1524 needOverviewUpdate = false;
1525 changeEndRes = false;
1526 changeStartRes = false;
1527 stretchGroup = null;
1531 public void doMouseDraggedDefineMode(MouseEvent evt)
1533 int res = findColumn(evt);
1534 int y = findSeq(evt);
1536 if (wrappedBlock != startWrapBlock)
1541 if (stretchGroup == null)
1546 mouseDragging = true;
1548 if (y > av.getAlignment().getHeight())
1550 y = av.getAlignment().getHeight() - 1;
1553 if (res >= av.getAlignment().getWidth())
1555 res = av.getAlignment().getWidth() - 1;
1558 if (stretchGroup.getEndRes() == res)
1560 // Edit end res position of selected group
1561 changeEndRes = true;
1563 else if (stretchGroup.getStartRes() == res)
1565 // Edit start res position of selected group
1566 changeStartRes = true;
1576 if (res > (stretchGroup.getStartRes() - 1))
1578 stretchGroup.setEndRes(res);
1579 needOverviewUpdate |= av.isSelectionDefinedGroup();
1582 else if (changeStartRes)
1584 if (res < (stretchGroup.getEndRes() + 1))
1586 stretchGroup.setStartRes(res);
1587 needOverviewUpdate |= av.isSelectionDefinedGroup();
1591 int dragDirection = 0;
1597 else if (y < oldSeq)
1602 while ((y != oldSeq) && (oldSeq > -1)
1603 && (y < av.getAlignment().getHeight()))
1605 // This routine ensures we don't skip any sequences, as the
1606 // selection is quite slow.
1607 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1609 oldSeq += dragDirection;
1616 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1618 if (stretchGroup.getSequences(null).contains(nextSeq))
1620 stretchGroup.deleteSequence(seq, false);
1621 needOverviewUpdate |= av.isSelectionDefinedGroup();
1627 stretchGroup.addSequence(seq, false);
1630 stretchGroup.addSequence(nextSeq, false);
1631 needOverviewUpdate |= av.isSelectionDefinedGroup();
1640 if (res > av.getRanges().getEndRes()
1641 || res < av.getRanges().getStartRes()
1642 || y < av.getRanges().getStartSeq()
1643 || y > av.getRanges().getEndSeq())
1648 if ((scrollThread != null) && (scrollThread.isRunning()))
1650 scrollThread.setEvent(evt);
1653 seqCanvas.repaint();
1657 public void mouseEntered(MouseEvent e)
1664 if ((scrollThread != null) && (scrollThread.isRunning()))
1666 scrollThread.stopScrolling();
1667 scrollThread = null;
1672 public void mouseExited(MouseEvent e)
1674 if (av.getWrapAlignment())
1679 if (mouseDragging && scrollThread == null)
1681 scrollThread = new ScrollThread();
1685 void scrollCanvas(MouseEvent evt)
1689 if ((scrollThread != null) && (scrollThread.isRunning()))
1691 scrollThread.stopScrolling();
1692 scrollThread = null;
1694 mouseDragging = false;
1698 if (scrollThread == null)
1700 scrollThread = new ScrollThread();
1703 mouseDragging = true;
1704 scrollThread.setEvent(evt);
1709 // this class allows scrolling off the bottom of the visible alignment
1710 class ScrollThread extends Thread
1714 private volatile boolean threadRunning = true;
1716 public ScrollThread()
1721 public void setEvent(MouseEvent e)
1726 public void stopScrolling()
1728 threadRunning = false;
1731 public boolean isRunning()
1733 return threadRunning;
1739 while (threadRunning)
1745 if (mouseDragging && evt.getY() < 0
1746 && av.getRanges().getStartSeq() > 0)
1748 av.getRanges().scrollUp(true);
1751 if (mouseDragging && evt.getY() >= getSize().height && av
1752 .getAlignment().getHeight() > av.getRanges().getEndSeq())
1754 av.getRanges().scrollUp(false);
1757 if (mouseDragging && evt.getX() < 0)
1759 av.getRanges().scrollRight(false);
1762 else if (mouseDragging && evt.getX() >= getSize().width)
1764 av.getRanges().scrollRight(true);
1771 } catch (Exception ex)
1779 * modify current selection according to a received message.
1782 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1783 HiddenColumns hidden, SelectionSource source)
1785 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1786 // handles selection messages...
1787 // TODO: extend config options to allow user to control if selections may be
1788 // shared between viewports.
1789 if (av != null && (av == source || !av.followSelection
1790 || (source instanceof AlignViewport
1791 && ((AlignmentViewport) source).getSequenceSetId()
1792 .equals(av.getSequenceSetId()))))
1798 * Check for selection in a view of which this one is a dna/protein
1801 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1806 // do we want to thread this ? (contention with seqsel and colsel locks, I
1809 * only copy colsel if there is a real intersection between
1810 * sequence selection and this panel's alignment
1812 boolean repaint = false;
1813 boolean copycolsel = false;
1814 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1816 SequenceGroup sgroup = null;
1817 if (seqsel != null && seqsel.getSize() > 0)
1819 if (av.getAlignment() == null)
1821 System.out.println("Selection message: alignviewport av SeqSetId="
1822 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1823 + " 's alignment is NULL! returning immediatly.");
1826 sgroup = seqsel.intersect(av.getAlignment(),
1827 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1828 if ((sgroup != null && sgroup.getSize() > 0))
1833 if (sgroup != null && sgroup.getSize() > 0)
1835 av.setSelectionGroup(sgroup);
1839 av.setSelectionGroup(null);
1841 repaint = av.isSelectionGroupChanged(true);
1843 if (copycolsel && (av.getColumnSelection() == null
1844 || !av.isColSelChanged(true)))
1846 // the current selection is unset or from a previous message
1847 // so import the new colsel.
1848 if (colsel == null || colsel.isEmpty())
1850 if (av.getColumnSelection() != null)
1852 av.getColumnSelection().clear();
1857 // TODO: shift colSel according to the intersecting sequences
1858 if (av.getColumnSelection() == null)
1860 av.setColumnSelection(new ColumnSelection(colsel));
1864 av.getColumnSelection().setElementsFrom(colsel,
1865 av.getAlignment().getHiddenColumns());
1868 repaint |= av.isColSelChanged(true);
1870 if (copycolsel && av.hasHiddenColumns()
1871 && (av.getColumnSelection() == null))
1873 System.err.println("Bad things");
1877 ap.scalePanelHolder.repaint();
1883 * scroll to the given row/column - or nearest visible location
1888 public void scrollTo(int row, int column)
1891 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1892 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1893 ap.scrollTo(column, column, row, true, true);
1897 * scroll to the given row - or nearest visible location
1901 public void scrollToRow(int row)
1904 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1905 ap.scrollTo(ap.av.getRanges().getStartRes(),
1906 ap.av.getRanges().getStartRes(), row, true, true);
1910 * scroll to the given column - or nearest visible location
1914 public void scrollToColumn(int column)
1917 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1918 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true,
1923 * If this panel is a cdna/protein translation view of the selection source,
1924 * tries to map the source selection to a local one, and returns true. Else
1931 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1932 ColumnSelection colsel, HiddenColumns hidden,
1933 SelectionSource source)
1935 if (!(source instanceof AlignViewportI))
1939 final AlignViewportI sourceAv = (AlignViewportI) source;
1940 if (sourceAv.getCodingComplement() != av
1941 && av.getCodingComplement() != sourceAv)
1947 * Map sequence selection
1949 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1950 av.setSelectionGroup(sg);
1951 av.isSelectionGroupChanged(true);
1954 * Map column selection
1956 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1958 ColumnSelection cs = new ColumnSelection();
1959 HiddenColumns hs = new HiddenColumns();
1960 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1961 av.setColumnSelection(cs);
1962 av.getAlignment().setHiddenColumns(hs);
1964 ap.scalePanelHolder.repaint();