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
503 if (Platform.isWinMiddleButton(evt))
505 mouseWheelPressed = true;
509 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
511 if (evt.isControlDown() || evt.isAltDown())
519 doMousePressedDefineMode(evt);
523 int seq = findSeq(evt);
524 int res = findColumn(evt);
526 if (seq < 0 || res < 0)
531 if ((seq < av.getAlignment().getHeight())
532 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
547 public void mouseClicked(MouseEvent evt)
549 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
550 if (evt.getClickCount() > 1)
552 if (av.getSelectionGroup() != null
553 && av.getSelectionGroup().getSize() == 1
554 && av.getSelectionGroup().getEndRes()
555 - av.getSelectionGroup().getStartRes() < 2)
557 av.setSelectionGroup(null);
560 int column = findColumn(evt);
561 List<SequenceFeature> features = findFeaturesAtColumn(sequence,
564 if (!features.isEmpty())
566 SearchResultsI highlight = new SearchResults();
567 highlight.addResult(sequence, features.get(0).getBegin(),
568 features.get(0).getEnd());
569 seqCanvas.highlightSearchResults(highlight);
570 seqCanvas.getFeatureRenderer().amendFeatures(
571 Collections.singletonList(sequence), features, false, ap);
572 av.setSearchResults(null); // clear highlighting
573 seqCanvas.repaint(); // draw new/amended features
579 public void mouseReleased(MouseEvent evt)
581 boolean didDrag = mouseDragging; // did we come here after a drag
582 mouseDragging = false;
583 mouseWheelPressed = false;
587 doMouseReleasedDefineMode(evt, didDrag);
595 int startWrapBlock = -1;
597 int wrappedBlock = -1;
600 * Returns the aligned sequence position (base 0) at the mouse position, or
601 * the closest visible one
606 int findColumn(MouseEvent evt)
611 int startRes = av.getRanges().getStartRes();
612 if (av.getWrapAlignment())
615 int hgap = av.getCharHeight();
616 if (av.getScaleAboveWrapped())
618 hgap += av.getCharHeight();
621 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
622 + hgap + seqCanvas.getAnnotationHeight();
626 x = Math.max(0, x - seqCanvas.LABEL_WEST);
628 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
634 wrappedBlock = y / cHeight;
635 wrappedBlock += startRes / cwidth;
636 int startOffset = startRes % cwidth; // in case start is scrolled right
638 res = wrappedBlock * cwidth + startOffset
639 + +Math.min(cwidth - 1, x / av.getCharWidth());
643 res = (x / av.getCharWidth()) + startRes;
646 if (av.hasHiddenColumns())
648 res = av.getAlignment().getHiddenColumns()
649 .visibleToAbsoluteColumn(res);
656 int findSeq(MouseEvent evt)
658 final int sqnum = findAlRow(evt);
659 return (sqnum < 0) ? 0 : sqnum;
665 * @return row in alignment that was selected (or -1 for column selection)
667 private int findAlRow(MouseEvent evt)
672 if (av.getWrapAlignment())
674 int hgap = av.getCharHeight();
675 if (av.getScaleAboveWrapped())
677 hgap += av.getCharHeight();
680 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
681 + hgap + seqCanvas.getAnnotationHeight();
685 seq = Math.min((y % cHeight) / av.getCharHeight(),
686 av.getAlignment().getHeight() - 1);
695 (y / av.getCharHeight()) + av.getRanges().getStartSeq(),
696 av.getAlignment().getHeight() - 1);
706 public void doMousePressed(MouseEvent evt)
709 int seq = findSeq(evt);
710 int res = findColumn(evt);
712 if (seq < av.getAlignment().getHeight()
713 && res < av.getAlignment().getSequenceAt(seq).getLength())
715 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
716 // Find the residue's position in the sequence (res is the position
734 public void mouseOverSequence(SequenceI sequence, int index, int pos)
736 String tmp = sequence.hashCode() + index + "";
737 if (lastMessage == null || !lastMessage.equals(tmp))
739 ssm.mouseOverSequence(sequence, index, pos, av);
746 public String highlightSequence(SearchResultsI results)
748 if (av.isFollowHighlight())
750 // don't allow highlight of protein/cDNA to also scroll a complementary
751 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
752 // over residue to change abruptly, causing highlighted residue in panel 2
753 // to change, causing a scroll in panel 1 etc)
754 ap.setToScrollComplementPanel(false);
755 if (ap.scrollToPosition(results, true))
757 ap.alignFrame.repaint();
759 ap.setToScrollComplementPanel(true);
761 setStatusMessage(results);
762 seqCanvas.highlightSearchResults(results);
767 public VamsasSource getVamsasSource()
769 return this.ap == null ? null : this.ap.av;
773 public void updateColours(SequenceI seq, int index)
775 jalview.bin.Console.outPrintln("update the seqPanel colours");
780 public void mouseMoved(MouseEvent evt)
782 final int column = findColumn(evt);
783 int seq = findSeq(evt);
785 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
794 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
795 if (column > sequence.getLength())
804 final char ch = sequence.getCharAt(column);
805 boolean isGapped = Comparison.isGap(ch);
806 // find residue at column (or nearest if at a gap)
807 int respos = sequence.findPosition(column);
809 if (ssm != null && !isGapped)
811 mouseOverSequence(sequence, column, respos);
814 StringBuilder text = new StringBuilder();
815 text.append("Sequence ").append(Integer.toString(seq + 1))
816 .append(" ID: ").append(sequence.getName());
820 if (av.getAlignment().isNucleotide())
822 String base = ResidueProperties.nucleotideName.get(ch);
823 text.append(" Nucleotide: ").append(base == null ? ch : base);
827 String residue = (ch == 'x' || ch == 'X') ? "X"
828 : ResidueProperties.aa2Triplet.get(String.valueOf(ch));
829 text.append(" Residue: ").append(residue == null ? ch : residue);
831 text.append(" (").append(Integer.toString(respos)).append(")");
834 ap.alignFrame.statusBar.setText(text.toString());
836 StringBuilder tooltipText = new StringBuilder();
837 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
840 for (int g = 0; g < groups.length; g++)
842 if (groups[g].getStartRes() <= column
843 && groups[g].getEndRes() >= column)
845 if (!groups[g].getName().startsWith("JTreeGroup")
846 && !groups[g].getName().startsWith("JGroup"))
848 tooltipText.append(groups[g].getName()).append(" ");
850 if (groups[g].getDescription() != null)
852 tooltipText.append(groups[g].getDescription());
854 tooltipText.append("\n");
860 * add feature details to tooltip, including any that straddle
863 if (av.isShowSequenceFeatures())
865 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
867 for (SequenceFeature sf : allFeatures)
869 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
871 if (sf.getDescription() != null)
873 tooltipText.append(" " + sf.getDescription());
876 if (sf.getValue("status") != null)
878 String status = sf.getValue("status").toString();
879 if (status.length() > 0)
881 tooltipText.append(" (" + sf.getValue("status") + ")");
884 tooltipText.append("\n");
890 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
894 tooltip.setTip(tooltipText.toString());
899 * Returns features at the specified aligned column on the given sequence.
900 * Non-positional features are not included. If the column has a gap, then
901 * enclosing features are included (but not contact features).
908 List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
910 return seqCanvas.getFeatureRenderer().findFeaturesAtColumn(sequence,
917 * set when the current UI interaction has resulted in a change that requires
918 * overview shading to be recalculated. this could be changed to something
919 * more expressive that indicates what actually has changed, so selective
920 * redraws can be applied
922 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
925 public void mouseDragged(MouseEvent evt)
927 if (mouseWheelPressed)
929 int oldWidth = av.getCharWidth();
931 // Which is bigger, left-right or up-down?
932 if (Math.abs(evt.getY() - lastMousePress.y) > Math
933 .abs(evt.getX() - lastMousePress.x))
935 int fontSize = av.font.getSize();
937 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
941 else if (evt.getY() > lastMousePress.y)
952 new Font(av.font.getName(), av.font.getStyle(), fontSize),
954 av.setCharWidth(oldWidth);
958 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
960 av.setCharWidth(av.getCharWidth() - 1);
962 else if (evt.getX() > lastMousePress.x)
964 av.setCharWidth(av.getCharWidth() + 1);
967 if (av.getCharWidth() < 1)
975 FontMetrics fm = getFontMetrics(av.getFont());
976 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
978 lastMousePress = evt.getPoint();
980 ap.paintAlignment(false, false);
981 ap.annotationPanel.image = null;
987 doMouseDraggedDefineMode(evt);
991 int res = findColumn(evt);
998 if ((lastres == -1) || (lastres == res))
1003 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1005 // dragLeft, delete gap
1006 editSequence(false, res);
1010 editSequence(true, res);
1013 mouseDragging = true;
1014 if (scrollThread != null)
1016 scrollThread.setEvent(evt);
1021 synchronized void editSequence(boolean insertGap, int startres)
1024 int fixedRight = -1;
1025 boolean fixedColumns = false;
1026 SequenceGroup sg = av.getSelectionGroup();
1028 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1030 if (!groupEditing && av.hasHiddenRows())
1032 if (av.isHiddenRepSequence(seq))
1034 sg = av.getRepresentedSequences(seq);
1035 groupEditing = true;
1039 StringBuffer message = new StringBuffer();
1042 message.append(MessageManager.getString("action.edit_group"))
1044 if (editCommand == null)
1046 editCommand = new EditCommand(
1047 MessageManager.getString("action.edit_group"));
1052 message.append(MessageManager.getString("label.edit_sequence"))
1053 .append(" " + seq.getName());
1054 String label = seq.getName();
1055 if (label.length() > 10)
1057 label = label.substring(0, 10);
1059 if (editCommand == null)
1061 editCommand = new EditCommand(MessageManager
1062 .formatMessage("label.edit_params", new String[]
1069 message.append(" insert ");
1073 message.append(" delete ");
1076 message.append(Math.abs(startres - lastres) + " gaps.");
1077 ap.alignFrame.statusBar.setText(message.toString());
1079 // Are we editing within a selection group?
1080 if (groupEditing || (sg != null
1081 && sg.getSequences(av.getHiddenRepSequences()).contains(seq)))
1083 fixedColumns = true;
1085 // sg might be null as the user may only see 1 sequence,
1086 // but the sequence represents a group
1089 if (!av.isHiddenRepSequence(seq))
1095 sg = av.getRepresentedSequences(seq);
1098 fixedLeft = sg.getStartRes();
1099 fixedRight = sg.getEndRes();
1101 if ((startres < fixedLeft && lastres >= fixedLeft)
1102 || (startres >= fixedLeft && lastres < fixedLeft)
1103 || (startres > fixedRight && lastres <= fixedRight)
1104 || (startres <= fixedRight && lastres > fixedRight))
1110 if (fixedLeft > startres)
1112 fixedRight = fixedLeft - 1;
1115 else if (fixedRight < startres)
1117 fixedLeft = fixedRight;
1122 if (av.hasHiddenColumns())
1124 fixedColumns = true;
1125 int y1 = av.getAlignment().getHiddenColumns()
1126 .getNextHiddenBoundary(true, startres);
1127 int y2 = av.getAlignment().getHiddenColumns()
1128 .getNextHiddenBoundary(false, startres);
1130 if ((insertGap && startres > y1 && lastres < y1)
1131 || (!insertGap && startres < y2 && lastres > y2))
1137 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1138 // Selection spans a hidden region
1139 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1147 fixedRight = y2 - 1;
1154 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1155 .toArray(new SequenceI[0]);
1160 // If the user has selected the whole sequence, and is dragging to
1161 // the right, we can still extend the alignment and selectionGroup
1162 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1163 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1165 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1166 fixedRight = sg.getEndRes();
1169 // Is it valid with fixed columns??
1170 // Find the next gap before the end
1171 // of the visible region boundary
1172 boolean blank = false;
1173 for (; fixedRight > lastres; fixedRight--)
1177 for (SequenceI gs : groupSeqs)
1179 for (int j = 0; j < startres - lastres; j++)
1181 if (!jalview.util.Comparison
1182 .isGap(gs.getCharAt(fixedRight - j)))
1197 if (sg.getSize() == av.getAlignment().getHeight())
1199 if ((av.hasHiddenColumns()
1200 && startres < av.getAlignment().getHiddenColumns()
1201 .getNextHiddenBoundary(false, startres)))
1207 int alWidth = av.getAlignment().getWidth();
1208 if (av.hasHiddenRows())
1210 int hwidth = av.getAlignment().getHiddenSequences()
1212 if (hwidth > alWidth)
1217 // We can still insert gaps if the selectionGroup
1218 // contains all the sequences
1219 sg.setEndRes(sg.getEndRes() + startres - lastres);
1220 fixedRight = alWidth + startres - lastres;
1231 else if (!insertGap)
1233 // / Are we able to delete?
1234 // ie are all columns blank?
1236 for (SequenceI gs : groupSeqs)
1238 for (int j = startres; j < lastres; j++)
1240 if (gs.getLength() <= j)
1245 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1247 // Not a gap, block edit not valid
1257 // dragging to the right
1258 if (fixedColumns && fixedRight != -1)
1260 for (int j = lastres; j < startres; j++)
1262 insertChar(j, groupSeqs, fixedRight);
1267 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1268 startres - lastres, av.getAlignment(), true);
1273 // dragging to the left
1274 if (fixedColumns && fixedRight != -1)
1276 for (int j = lastres; j > startres; j--)
1278 deleteChar(startres, groupSeqs, fixedRight);
1283 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1284 lastres - startres, av.getAlignment(), true);
1290 // ///Editing a single sequence///////////
1294 // dragging to the right
1295 if (fixedColumns && fixedRight != -1)
1297 for (int j = lastres; j < startres; j++)
1299 insertChar(j, new SequenceI[] { seq }, fixedRight);
1304 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[] { seq },
1305 lastres, startres - lastres, av.getAlignment(), true);
1310 // dragging to the left
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j > startres; j--)
1315 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1320 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1325 // could be a keyboard edit trying to delete none gaps
1327 for (int m = startres; m < lastres; m++)
1329 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1338 editCommand.appendEdit(Action.DELETE_GAP,
1340 { seq }, startres, max, av.getAlignment(), true);
1347 seqCanvas.repaint();
1350 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1352 int blankColumn = fixedColumn;
1353 for (int s = 0; s < seq.length; s++)
1355 // Find the next gap before the end of the visible region boundary
1356 // If lastCol > j, theres a boundary after the gap insertion
1358 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1360 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1362 // Theres a space, so break and insert the gap
1367 if (blankColumn <= j)
1369 blankColumn = fixedColumn;
1375 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1376 av.getAlignment(), true);
1378 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1383 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1386 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1389 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1390 av.getAlignment(), true);
1393 // ////////////////////////////////////////
1394 // ///Everything below this is for defining the boundary of the rubberband
1395 // ////////////////////////////////////////
1396 public void doMousePressedDefineMode(MouseEvent evt)
1398 if (scrollThread != null)
1400 scrollThread.threadRunning = false;
1401 scrollThread = null;
1404 int column = findColumn(evt);
1405 int seq = findSeq(evt);
1407 startWrapBlock = wrappedBlock;
1414 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1416 if (sequence == null || column > sequence.getLength())
1421 stretchGroup = av.getSelectionGroup();
1423 if (stretchGroup == null || !stretchGroup.contains(sequence, column))
1425 stretchGroup = av.getAlignment().findGroup(sequence, column);
1426 if (stretchGroup != null)
1428 // only update the current selection if the popup menu has a group to
1430 av.setSelectionGroup(stretchGroup);
1434 // DETECT RIGHT MOUSE BUTTON IN AWT
1435 if ((evt.getModifiersEx()
1436 & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK)
1438 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
1439 sequence.findPosition(column + 1));
1441 Vector<String> links = null;
1442 for (SequenceFeature sf : allFeatures)
1444 if (sf.links != null)
1448 links = new Vector<>();
1450 links.addAll(sf.links);
1453 APopupMenu popup = new APopupMenu(ap, null, links);
1455 popup.show(this, evt.getX(), evt.getY());
1461 seqCanvas.cursorX = findColumn(evt);
1462 seqCanvas.cursorY = findSeq(evt);
1463 seqCanvas.repaint();
1467 // Only if left mouse button do we want to change group sizes
1469 if (stretchGroup == null)
1471 // define a new group here
1472 SequenceGroup sg = new SequenceGroup();
1473 sg.setStartRes(column);
1474 sg.setEndRes(column);
1475 sg.addSequence(sequence, false);
1476 av.setSelectionGroup(sg);
1479 if (av.getConservationSelected())
1481 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1484 if (av.getAbovePIDThreshold())
1486 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1493 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1495 if (stretchGroup == null)
1499 // always do this - annotation has own state
1500 // but defer colourscheme update until hidden sequences are passed in
1501 boolean vischange = stretchGroup.recalcConservation(true);
1502 // here we rely on stretchGroup == av.getSelection()
1503 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1505 if (stretchGroup.cs != null)
1507 stretchGroup.cs.alignmentChanged(stretchGroup,
1508 av.getHiddenRepSequences());
1510 if (stretchGroup.cs.conservationApplied())
1512 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1513 stretchGroup.getName());
1515 if (stretchGroup.cs.getThreshold() > 0)
1517 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1518 stretchGroup.getName());
1521 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1522 ap.paintAlignment(needOverviewUpdate, needOverviewUpdate);
1523 needOverviewUpdate = false;
1524 changeEndRes = false;
1525 changeStartRes = false;
1526 stretchGroup = null;
1530 public void doMouseDraggedDefineMode(MouseEvent evt)
1532 int res = findColumn(evt);
1533 int y = findSeq(evt);
1535 if (wrappedBlock != startWrapBlock)
1540 if (stretchGroup == null)
1545 mouseDragging = true;
1547 if (y > av.getAlignment().getHeight())
1549 y = av.getAlignment().getHeight() - 1;
1552 if (res >= av.getAlignment().getWidth())
1554 res = av.getAlignment().getWidth() - 1;
1557 if (stretchGroup.getEndRes() == res)
1559 // Edit end res position of selected group
1560 changeEndRes = true;
1562 else if (stretchGroup.getStartRes() == res)
1564 // Edit start res position of selected group
1565 changeStartRes = true;
1575 if (res > (stretchGroup.getStartRes() - 1))
1577 stretchGroup.setEndRes(res);
1578 needOverviewUpdate |= av.isSelectionDefinedGroup();
1581 else if (changeStartRes)
1583 if (res < (stretchGroup.getEndRes() + 1))
1585 stretchGroup.setStartRes(res);
1586 needOverviewUpdate |= av.isSelectionDefinedGroup();
1590 int dragDirection = 0;
1596 else if (y < oldSeq)
1601 while ((y != oldSeq) && (oldSeq > -1)
1602 && (y < av.getAlignment().getHeight()))
1604 // This routine ensures we don't skip any sequences, as the
1605 // selection is quite slow.
1606 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1608 oldSeq += dragDirection;
1615 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1617 if (stretchGroup.getSequences(null).contains(nextSeq))
1619 stretchGroup.deleteSequence(seq, false);
1620 needOverviewUpdate |= av.isSelectionDefinedGroup();
1626 stretchGroup.addSequence(seq, false);
1629 stretchGroup.addSequence(nextSeq, false);
1630 needOverviewUpdate |= av.isSelectionDefinedGroup();
1639 if (res > av.getRanges().getEndRes()
1640 || res < av.getRanges().getStartRes()
1641 || y < av.getRanges().getStartSeq()
1642 || y > av.getRanges().getEndSeq())
1647 if ((scrollThread != null) && (scrollThread.isRunning()))
1649 scrollThread.setEvent(evt);
1652 seqCanvas.repaint();
1656 public void mouseEntered(MouseEvent e)
1663 if ((scrollThread != null) && (scrollThread.isRunning()))
1665 scrollThread.stopScrolling();
1666 scrollThread = null;
1671 public void mouseExited(MouseEvent e)
1673 if (av.getWrapAlignment())
1678 if (mouseDragging && scrollThread == null)
1680 scrollThread = new ScrollThread();
1684 void scrollCanvas(MouseEvent evt)
1688 if ((scrollThread != null) && (scrollThread.isRunning()))
1690 scrollThread.stopScrolling();
1691 scrollThread = null;
1693 mouseDragging = false;
1697 if (scrollThread == null)
1699 scrollThread = new ScrollThread();
1702 mouseDragging = true;
1703 scrollThread.setEvent(evt);
1708 // this class allows scrolling off the bottom of the visible alignment
1709 class ScrollThread extends Thread
1713 private volatile boolean threadRunning = true;
1715 public ScrollThread()
1720 public void setEvent(MouseEvent e)
1725 public void stopScrolling()
1727 threadRunning = false;
1730 public boolean isRunning()
1732 return threadRunning;
1738 while (threadRunning)
1744 if (mouseDragging && evt.getY() < 0
1745 && av.getRanges().getStartSeq() > 0)
1747 av.getRanges().scrollUp(true);
1750 if (mouseDragging && evt.getY() >= getSize().height && av
1751 .getAlignment().getHeight() > av.getRanges().getEndSeq())
1753 av.getRanges().scrollUp(false);
1756 if (mouseDragging && evt.getX() < 0)
1758 av.getRanges().scrollRight(false);
1761 else if (mouseDragging && evt.getX() >= getSize().width)
1763 av.getRanges().scrollRight(true);
1770 } catch (Exception ex)
1778 * modify current selection according to a received message.
1781 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1782 HiddenColumns hidden, SelectionSource source)
1784 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1785 // handles selection messages...
1786 // TODO: extend config options to allow user to control if selections may be
1787 // shared between viewports.
1788 if (av != null && (av == source || !av.followSelection
1789 || (source instanceof AlignViewport
1790 && ((AlignmentViewport) source).getSequenceSetId()
1791 .equals(av.getSequenceSetId()))))
1797 * Check for selection in a view of which this one is a dna/protein
1800 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1805 // do we want to thread this ? (contention with seqsel and colsel locks, I
1808 * only copy colsel if there is a real intersection between
1809 * sequence selection and this panel's alignment
1811 boolean repaint = false;
1812 boolean copycolsel = false;
1813 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1815 SequenceGroup sgroup = null;
1816 if (seqsel != null && seqsel.getSize() > 0)
1818 if (av.getAlignment() == null)
1820 jalview.bin.Console.outPrintln(
1821 "Selection message: alignviewport av SeqSetId="
1822 + av.getSequenceSetId() + " ViewId="
1824 + " 's alignment is NULL! returning immediatly.");
1827 sgroup = seqsel.intersect(av.getAlignment(),
1828 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1829 if ((sgroup != null && sgroup.getSize() > 0))
1834 if (sgroup != null && sgroup.getSize() > 0)
1836 av.setSelectionGroup(sgroup);
1840 av.setSelectionGroup(null);
1842 repaint = av.isSelectionGroupChanged(true);
1844 if (copycolsel && (av.getColumnSelection() == null
1845 || !av.isColSelChanged(true)))
1847 // the current selection is unset or from a previous message
1848 // so import the new colsel.
1849 if (colsel == null || colsel.isEmpty())
1851 if (av.getColumnSelection() != null)
1853 av.getColumnSelection().clear();
1858 // TODO: shift colSel according to the intersecting sequences
1859 if (av.getColumnSelection() == null)
1861 av.setColumnSelection(new ColumnSelection(colsel));
1865 av.getColumnSelection().setElementsFrom(colsel,
1866 av.getAlignment().getHiddenColumns());
1869 repaint |= av.isColSelChanged(true);
1871 if (copycolsel && av.hasHiddenColumns()
1872 && (av.getColumnSelection() == null))
1874 jalview.bin.Console.errPrintln("Bad things");
1878 ap.scalePanelHolder.repaint();
1884 * scroll to the given row/column - or nearest visible location
1889 public void scrollTo(int row, int column)
1892 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1893 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1894 ap.scrollTo(column, column, row, true, true);
1898 * scroll to the given row - or nearest visible location
1902 public void scrollToRow(int row)
1905 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1906 ap.scrollTo(ap.av.getRanges().getStartRes(),
1907 ap.av.getRanges().getStartRes(), row, true, true);
1911 * scroll to the given column - or nearest visible location
1915 public void scrollToColumn(int column)
1918 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1919 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true,
1924 * If this panel is a cdna/protein translation view of the selection source,
1925 * tries to map the source selection to a local one, and returns true. Else
1932 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1933 ColumnSelection colsel, HiddenColumns hidden,
1934 SelectionSource source)
1936 if (!(source instanceof AlignViewportI))
1940 final AlignViewportI sourceAv = (AlignViewportI) source;
1941 if (sourceAv.getCodingComplement() != av
1942 && av.getCodingComplement() != sourceAv)
1948 * Map sequence selection
1950 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1951 av.setSelectionGroup(sg);
1952 av.isSelectionGroupChanged(true);
1955 * Map column selection
1957 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1959 ColumnSelection cs = new ColumnSelection();
1960 HiddenColumns hs = new HiddenColumns();
1961 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1962 av.setColumnSelection(cs);
1963 av.getAlignment().setHiddenColumns(hs);
1965 ap.scalePanelHolder.repaint();