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.viewmodel.AlignmentViewport;
46 import jalview.viewmodel.ViewportRanges;
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, av.getAlignment()
142 groupEditing = false;
150 seqCanvas.cursorY = getKeyboardNo1() - 1;
154 void setCursorColumn()
156 seqCanvas.cursorX = getKeyboardNo1() - 1;
160 void setCursorRowAndColumn()
162 if (keyboardNo2 == null)
164 keyboardNo2 = new StringBuffer();
168 seqCanvas.cursorX = getKeyboardNo1() - 1;
169 seqCanvas.cursorY = getKeyboardNo2() - 1;
174 void setCursorPosition()
176 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
178 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
182 void moveCursor(int dx, int dy)
184 seqCanvas.cursorX += dx;
185 seqCanvas.cursorY += dy;
186 if (av.hasHiddenColumns()
187 && !av.getAlignment().getHiddenColumns()
188 .isVisible(seqCanvas.cursorX))
190 int original = seqCanvas.cursorX - dx;
191 int maxWidth = av.getAlignment().getWidth();
193 while (!av.getAlignment().getHiddenColumns()
194 .isVisible(seqCanvas.cursorX)
195 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
197 seqCanvas.cursorX += dx;
200 if (seqCanvas.cursorX >= maxWidth
201 || !av.getAlignment().getHiddenColumns()
202 .isVisible(seqCanvas.cursorX))
204 seqCanvas.cursorX = original;
210 void scrollToVisible()
212 if (seqCanvas.cursorX < 0)
214 seqCanvas.cursorX = 0;
216 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
218 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
221 if (seqCanvas.cursorY < 0)
223 seqCanvas.cursorY = 0;
225 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
227 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
231 if (av.getWrapAlignment())
233 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
237 ViewportRanges ranges = av.getRanges();
238 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
239 while (seqCanvas.cursorY < ranges.getStartSeq())
241 ranges.scrollUp(true);
243 while (seqCanvas.cursorY > ranges.getEndSeq())
245 ranges.scrollUp(false);
247 while (seqCanvas.cursorX < hidden.adjustForHiddenColumns(ranges
251 if (!ranges.scrollRight(false))
256 while (seqCanvas.cursorX > hidden.adjustForHiddenColumns(ranges
259 if (!ranges.scrollRight(true))
265 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
266 seqCanvas.cursorX, seqCanvas.cursorY);
271 void setSelectionAreaAtCursor(boolean topLeft)
273 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
275 if (av.getSelectionGroup() != null)
277 SequenceGroup sg = av.getSelectionGroup();
278 // Find the top and bottom of this group
279 int min = av.getAlignment().getHeight(), max = 0;
280 for (int i = 0; i < sg.getSize(); i++)
282 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
297 sg.setStartRes(seqCanvas.cursorX);
298 if (sg.getEndRes() < seqCanvas.cursorX)
300 sg.setEndRes(seqCanvas.cursorX);
303 min = seqCanvas.cursorY;
307 sg.setEndRes(seqCanvas.cursorX);
308 if (sg.getStartRes() > seqCanvas.cursorX)
310 sg.setStartRes(seqCanvas.cursorX);
313 max = seqCanvas.cursorY + 1;
318 // Only the user can do this
319 av.setSelectionGroup(null);
323 // Now add any sequences between min and max
325 for (int i = min; i < max; i++)
327 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
332 if (av.getSelectionGroup() == null)
334 SequenceGroup sg = new SequenceGroup();
335 sg.setStartRes(seqCanvas.cursorX);
336 sg.setEndRes(seqCanvas.cursorX);
337 sg.addSequence(sequence, false);
338 av.setSelectionGroup(sg);
340 ap.paintAlignment(false);
344 void insertGapAtCursor(boolean group)
346 groupEditing = group;
347 startseq = seqCanvas.cursorY;
348 lastres = seqCanvas.cursorX;
349 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
353 void deleteGapAtCursor(boolean group)
355 groupEditing = group;
356 startseq = seqCanvas.cursorY;
357 lastres = seqCanvas.cursorX + getKeyboardNo1();
358 editSequence(false, seqCanvas.cursorX);
362 void numberPressed(char value)
364 if (keyboardNo1 == null)
366 keyboardNo1 = new StringBuffer();
369 if (keyboardNo2 != null)
371 keyboardNo2.append(value);
375 keyboardNo1.append(value);
383 if (keyboardNo1 != null)
385 int value = Integer.parseInt(keyboardNo1.toString());
389 } catch (Exception x)
400 if (keyboardNo2 != null)
402 int value = Integer.parseInt(keyboardNo2.toString());
406 } catch (Exception x)
414 * Set status message in alignment panel
417 * aligned sequence object
421 * index of sequence in alignment
422 * @return position of column in sequence or -1 if at gap
424 void setStatusMessage(SequenceI sequence, int column, int seq)
426 // TODO remove duplication of identical gui method
427 StringBuilder text = new StringBuilder(32);
428 String seqno = seq == -1 ? "" : " " + (seq + 1);
429 text.append("Sequence" + seqno + " ID: " + sequence.getName());
431 String residue = null;
433 * Try to translate the display character to residue name (null for gap).
435 final String displayChar = String.valueOf(sequence.getCharAt(column));
436 if (av.getAlignment().isNucleotide())
438 residue = ResidueProperties.nucleotideName.get(displayChar);
441 text.append(" Nucleotide: ").append(residue);
446 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
447 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
451 text.append(" Residue: ").append(residue);
458 pos = sequence.findPosition(column);
459 text.append(" (").append(Integer.toString(pos)).append(")");
462 ap.alignFrame.statusBar.setText(text.toString());
466 * Set the status bar message to highlight the first matched position in
470 * @return true if results were matched, false if not
472 private boolean setStatusMessage(SearchResultsI results)
474 AlignmentI al = this.av.getAlignment();
475 int sequenceIndex = al.findIndex(results);
476 if (sequenceIndex == -1)
480 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
481 for (SearchResultMatchI m : results.getResults())
483 SequenceI seq = m.getSequence();
484 if (seq.getDatasetSequence() != null)
486 seq = seq.getDatasetSequence();
492 * Convert position in sequence (base 1) to sequence character array
495 int start = m.getStart() - m.getSequence().getStart();
496 setStatusMessage(seq, start, sequenceIndex);
504 public void mousePressed(MouseEvent evt)
506 lastMousePress = evt.getPoint();
508 // For now, ignore the mouseWheel font resizing on Macs
509 // As the Button2_mask always seems to be true
510 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
513 mouseWheelPressed = true;
517 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
519 if (evt.isControlDown() || evt.isAltDown())
527 doMousePressedDefineMode(evt);
531 int seq = findSeq(evt);
532 int res = findColumn(evt);
534 if (seq < 0 || res < 0)
539 if ((seq < av.getAlignment().getHeight())
540 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
555 public void mouseClicked(MouseEvent evt)
557 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
558 if (evt.getClickCount() > 1)
560 if (av.getSelectionGroup() != null
561 && av.getSelectionGroup().getSize() == 1
562 && av.getSelectionGroup().getEndRes()
563 - av.getSelectionGroup().getStartRes() < 2)
565 av.setSelectionGroup(null);
568 int column = findColumn(evt);
569 List<SequenceFeature> features = findFeaturesAtColumn(sequence,
572 if (!features.isEmpty())
574 SearchResultsI highlight = new SearchResults();
575 highlight.addResult(sequence, features.get(0).getBegin(), features
577 seqCanvas.highlightSearchResults(highlight);
578 seqCanvas.getFeatureRenderer().amendFeatures(
579 Collections.singletonList(sequence), features, false, ap);
580 seqCanvas.highlightSearchResults(null);
586 public void mouseReleased(MouseEvent evt)
588 boolean didDrag = mouseDragging; // did we come here after a drag
589 mouseDragging = false;
590 mouseWheelPressed = false;
594 doMouseReleasedDefineMode(evt, didDrag);
602 int startWrapBlock = -1;
604 int wrappedBlock = -1;
607 * Returns the aligned sequence position (base 0) at the mouse position, or
608 * the closest visible one
613 int findColumn(MouseEvent evt)
618 if (av.getWrapAlignment())
621 int hgap = av.getCharHeight();
622 if (av.getScaleAboveWrapped())
624 hgap += av.getCharHeight();
627 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
628 + hgap + seqCanvas.getAnnotationHeight();
632 x -= seqCanvas.LABEL_WEST;
634 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
640 wrappedBlock = y / cHeight;
641 wrappedBlock += av.getRanges().getStartRes() / cwidth;
643 res = wrappedBlock * cwidth + x / av.getCharWidth();
648 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
651 if (av.hasHiddenColumns())
653 res = av.getAlignment().getHiddenColumns()
654 .adjustForHiddenColumns(res);
661 int findSeq(MouseEvent evt)
663 final int sqnum = findAlRow(evt);
664 return (sqnum < 0) ? 0 : sqnum;
670 * @return row in alignment that was selected (or -1 for column selection)
672 private int findAlRow(MouseEvent evt)
677 if (av.getWrapAlignment())
679 int hgap = av.getCharHeight();
680 if (av.getScaleAboveWrapped())
682 hgap += av.getCharHeight();
685 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
686 + hgap + seqCanvas.getAnnotationHeight();
690 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
699 seq = Math.min((y / av.getCharHeight())
700 + av.getRanges().getStartSeq(),
702 .getAlignment().getHeight() - 1);
712 public void doMousePressed(MouseEvent evt)
715 int seq = findSeq(evt);
716 int res = findColumn(evt);
718 if (seq < av.getAlignment().getHeight()
719 && res < av.getAlignment().getSequenceAt(seq).getLength())
721 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
722 // Find the residue's position in the sequence (res is the position
740 public void mouseOverSequence(SequenceI sequence, int index, int pos)
742 String tmp = sequence.hashCode() + index + "";
743 if (lastMessage == null || !lastMessage.equals(tmp))
745 ssm.mouseOverSequence(sequence, index, pos, av);
752 public void highlightSequence(SearchResultsI results)
754 if (av.isFollowHighlight())
756 // don't allow highlight of protein/cDNA to also scroll a complementary
757 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
758 // over residue to change abruptly, causing highlighted residue in panel 2
759 // to change, causing a scroll in panel 1 etc)
760 ap.setToScrollComplementPanel(false);
761 if (ap.scrollToPosition(results, true))
763 ap.alignFrame.repaint();
765 ap.setToScrollComplementPanel(true);
767 setStatusMessage(results);
768 seqCanvas.highlightSearchResults(results);
773 public VamsasSource getVamsasSource()
775 return this.ap == null ? null : this.ap.av;
779 public void updateColours(SequenceI seq, int index)
781 System.out.println("update the seqPanel colours");
786 public void mouseMoved(MouseEvent evt)
788 final int column = findColumn(evt);
789 int seq = findSeq(evt);
791 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
800 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
801 if (column > sequence.getLength())
810 final char ch = sequence.getCharAt(column);
811 boolean isGapped = Comparison.isGap(ch);
812 // find residue at column (or nearest if at a gap)
813 int respos = sequence.findPosition(column);
815 if (ssm != null && !isGapped)
817 mouseOverSequence(sequence, column, respos);
820 StringBuilder text = new StringBuilder();
821 text.append("Sequence ").append(Integer.toString(seq + 1))
822 .append(" ID: ").append(sequence.getName());
826 if (av.getAlignment().isNucleotide())
828 String base = ResidueProperties.nucleotideName.get(ch);
829 text.append(" Nucleotide: ").append(base == null ? ch : base);
833 String residue = (ch == 'x' || ch == 'X') ? "X"
834 : ResidueProperties.aa2Triplet
835 .get(String.valueOf(ch));
836 text.append(" Residue: ").append(residue == null ? ch : residue);
838 text.append(" (").append(Integer.toString(respos)).append(")");
841 ap.alignFrame.statusBar.setText(text.toString());
843 StringBuilder tooltipText = new StringBuilder();
844 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
847 for (int g = 0; g < groups.length; g++)
849 if (groups[g].getStartRes() <= column && groups[g].getEndRes() >= column)
851 if (!groups[g].getName().startsWith("JTreeGroup")
852 && !groups[g].getName().startsWith("JGroup"))
854 tooltipText.append(groups[g].getName()).append(" ");
856 if (groups[g].getDescription() != null)
858 tooltipText.append(groups[g].getDescription());
860 tooltipText.append("\n");
866 * add feature details to tooltip, including any that straddle
869 if (av.isShowSequenceFeatures())
871 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
873 for (SequenceFeature sf : allFeatures)
875 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
877 if (sf.getDescription() != null)
879 tooltipText.append(" " + sf.getDescription());
882 if (sf.getValue("status") != null)
884 String status = sf.getValue("status").toString();
885 if (status.length() > 0)
887 tooltipText.append(" (" + sf.getValue("status") + ")");
890 tooltipText.append("\n");
896 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
900 tooltip.setTip(tooltipText.toString());
905 * Returns features at the specified aligned column on the given sequence.
906 * Non-positional features are not included. If the column has a gap, then
907 * enclosing features are included (but not contact features).
914 List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
916 return seqCanvas.getFeatureRenderer().findFeaturesAtColumn(sequence, column);
922 * set when the current UI interaction has resulted in a change that requires
923 * overview shading to be recalculated. this could be changed to something
924 * more expressive that indicates what actually has changed, so selective
925 * redraws can be applied
927 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
930 public void mouseDragged(MouseEvent evt)
932 if (mouseWheelPressed)
934 int oldWidth = av.getCharWidth();
936 // Which is bigger, left-right or up-down?
937 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
940 int fontSize = av.font.getSize();
942 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
946 else if (evt.getY() > lastMousePress.y)
957 new Font(av.font.getName(), av.font.getStyle(), fontSize),
959 av.setCharWidth(oldWidth);
963 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
965 av.setCharWidth(av.getCharWidth() - 1);
967 else if (evt.getX() > lastMousePress.x)
969 av.setCharWidth(av.getCharWidth() + 1);
972 if (av.getCharWidth() < 1)
980 FontMetrics fm = getFontMetrics(av.getFont());
981 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
983 lastMousePress = evt.getPoint();
985 ap.paintAlignment(false);
986 ap.annotationPanel.image = null;
992 doMouseDraggedDefineMode(evt);
996 int res = findColumn(evt);
1003 if ((lastres == -1) || (lastres == res))
1008 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1010 // dragLeft, delete gap
1011 editSequence(false, res);
1015 editSequence(true, res);
1018 mouseDragging = true;
1019 if (scrollThread != null)
1021 scrollThread.setEvent(evt);
1026 synchronized void editSequence(boolean insertGap, int startres)
1029 int fixedRight = -1;
1030 boolean fixedColumns = false;
1031 SequenceGroup sg = av.getSelectionGroup();
1033 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1035 if (!groupEditing && av.hasHiddenRows())
1037 if (av.isHiddenRepSequence(seq))
1039 sg = av.getRepresentedSequences(seq);
1040 groupEditing = true;
1044 StringBuffer message = new StringBuffer();
1047 message.append(MessageManager.getString("action.edit_group")).append(
1049 if (editCommand == null)
1051 editCommand = new EditCommand(
1052 MessageManager.getString("action.edit_group"));
1057 message.append(MessageManager.getString("label.edit_sequence"))
1058 .append(" " + seq.getName());
1059 String label = seq.getName();
1060 if (label.length() > 10)
1062 label = label.substring(0, 10);
1064 if (editCommand == null)
1066 editCommand = new EditCommand(MessageManager.formatMessage(
1067 "label.edit_params", new String[] { label }));
1073 message.append(" insert ");
1077 message.append(" delete ");
1080 message.append(Math.abs(startres - lastres) + " gaps.");
1081 ap.alignFrame.statusBar.setText(message.toString());
1083 // Are we editing within a selection group?
1085 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1088 fixedColumns = true;
1090 // sg might be null as the user may only see 1 sequence,
1091 // but the sequence represents a group
1094 if (!av.isHiddenRepSequence(seq))
1100 sg = av.getRepresentedSequences(seq);
1103 fixedLeft = sg.getStartRes();
1104 fixedRight = sg.getEndRes();
1106 if ((startres < fixedLeft && lastres >= fixedLeft)
1107 || (startres >= fixedLeft && lastres < fixedLeft)
1108 || (startres > fixedRight && lastres <= fixedRight)
1109 || (startres <= fixedRight && lastres > fixedRight))
1115 if (fixedLeft > startres)
1117 fixedRight = fixedLeft - 1;
1120 else if (fixedRight < startres)
1122 fixedLeft = fixedRight;
1127 if (av.hasHiddenColumns())
1129 fixedColumns = true;
1130 int y1 = av.getAlignment().getHiddenColumns()
1131 .getHiddenBoundaryLeft(startres);
1132 int y2 = av.getAlignment().getHiddenColumns()
1133 .getHiddenBoundaryRight(startres);
1135 if ((insertGap && startres > y1 && lastres < y1)
1136 || (!insertGap && startres < y2 && lastres > y2))
1142 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1143 // Selection spans a hidden region
1144 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1152 fixedRight = y2 - 1;
1159 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1160 .toArray(new SequenceI[0]);
1165 // If the user has selected the whole sequence, and is dragging to
1166 // the right, we can still extend the alignment and selectionGroup
1167 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1168 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1170 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1171 fixedRight = sg.getEndRes();
1174 // Is it valid with fixed columns??
1175 // Find the next gap before the end
1176 // of the visible region boundary
1177 boolean blank = false;
1178 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1182 for (SequenceI gs : groupSeqs)
1184 for (int j = 0; j < startres - lastres; j++)
1186 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1202 if (sg.getSize() == av.getAlignment().getHeight())
1204 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1205 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1211 int alWidth = av.getAlignment().getWidth();
1212 if (av.hasHiddenRows())
1214 int hwidth = av.getAlignment().getHiddenSequences()
1216 if (hwidth > alWidth)
1221 // We can still insert gaps if the selectionGroup
1222 // contains all the sequences
1223 sg.setEndRes(sg.getEndRes() + startres - lastres);
1224 fixedRight = alWidth + startres - lastres;
1235 else if (!insertGap)
1237 // / Are we able to delete?
1238 // ie are all columns blank?
1240 for (SequenceI gs : groupSeqs)
1242 for (int j = startres; j < lastres; j++)
1244 if (gs.getLength() <= j)
1249 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1251 // Not a gap, block edit not valid
1261 // dragging to the right
1262 if (fixedColumns && fixedRight != -1)
1264 for (int j = lastres; j < startres; j++)
1266 insertChar(j, groupSeqs, fixedRight);
1271 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1272 startres - lastres, av.getAlignment(), true);
1277 // dragging to the left
1278 if (fixedColumns && fixedRight != -1)
1280 for (int j = lastres; j > startres; j--)
1282 deleteChar(startres, groupSeqs, fixedRight);
1287 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1288 lastres - startres, av.getAlignment(), true);
1294 // ///Editing a single sequence///////////
1298 // dragging to the right
1299 if (fixedColumns && fixedRight != -1)
1301 for (int j = lastres; j < startres; j++)
1303 insertChar(j, new SequenceI[] { seq }, fixedRight);
1308 editCommand.appendEdit(Action.INSERT_GAP,
1309 new SequenceI[] { seq }, lastres, startres - lastres,
1310 av.getAlignment(), true);
1315 // dragging to the left
1316 if (fixedColumns && fixedRight != -1)
1318 for (int j = lastres; j > startres; j--)
1320 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1325 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1330 // could be a keyboard edit trying to delete none gaps
1332 for (int m = startres; m < lastres; m++)
1334 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1343 editCommand.appendEdit(Action.DELETE_GAP,
1344 new SequenceI[] { seq }, startres, max,
1345 av.getAlignment(), true);
1352 seqCanvas.repaint();
1355 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1357 int blankColumn = fixedColumn;
1358 for (int s = 0; s < seq.length; s++)
1360 // Find the next gap before the end of the visible region boundary
1361 // If lastCol > j, theres a boundary after the gap insertion
1363 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1365 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1367 // Theres a space, so break and insert the gap
1372 if (blankColumn <= j)
1374 blankColumn = fixedColumn;
1380 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1381 av.getAlignment(), true);
1383 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1388 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1391 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1394 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1395 av.getAlignment(), true);
1398 // ////////////////////////////////////////
1399 // ///Everything below this is for defining the boundary of the rubberband
1400 // ////////////////////////////////////////
1401 public void doMousePressedDefineMode(MouseEvent evt)
1403 if (scrollThread != null)
1405 scrollThread.running = false;
1406 scrollThread = null;
1409 int column = findColumn(evt);
1410 int seq = findSeq(evt);
1412 startWrapBlock = wrappedBlock;
1419 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1421 if (sequence == null || column > sequence.getLength())
1426 stretchGroup = av.getSelectionGroup();
1428 if (stretchGroup == null || !stretchGroup.contains(sequence, column))
1430 stretchGroup = av.getAlignment().findGroup(sequence, column);
1431 if (stretchGroup != null)
1433 // only update the current selection if the popup menu has a group to
1435 av.setSelectionGroup(stretchGroup);
1439 // DETECT RIGHT MOUSE BUTTON IN AWT
1440 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1442 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
1443 sequence.findPosition(column + 1));
1445 Vector<String> links = null;
1446 for (SequenceFeature sf : allFeatures)
1448 if (sf.links != null)
1452 links = new Vector<String>();
1454 links.addAll(sf.links);
1457 APopupMenu popup = new APopupMenu(ap, null, links);
1459 popup.show(this, evt.getX(), evt.getY());
1465 seqCanvas.cursorX = findColumn(evt);
1466 seqCanvas.cursorY = findSeq(evt);
1467 seqCanvas.repaint();
1471 // Only if left mouse button do we want to change group sizes
1473 if (stretchGroup == null)
1475 // define a new group here
1476 SequenceGroup sg = new SequenceGroup();
1477 sg.setStartRes(column);
1478 sg.setEndRes(column);
1479 sg.addSequence(sequence, false);
1480 av.setSelectionGroup(sg);
1483 if (av.getConservationSelected())
1485 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1488 if (av.getAbovePIDThreshold())
1490 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1497 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1499 if (stretchGroup == null)
1503 // always do this - annotation has own state
1504 // but defer colourscheme update until hidden sequences are passed in
1505 boolean vischange = stretchGroup.recalcConservation(true);
1506 // here we rely on stretchGroup == av.getSelection()
1507 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1509 if (stretchGroup.cs != null)
1511 stretchGroup.cs.alignmentChanged(stretchGroup,
1512 av.getHiddenRepSequences());
1514 if (stretchGroup.cs.conservationApplied())
1516 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1517 stretchGroup.getName());
1519 if (stretchGroup.cs.getThreshold() > 0)
1521 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1522 stretchGroup.getName());
1525 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1526 ap.paintAlignment(needOverviewUpdate);
1527 needOverviewUpdate = false;
1528 changeEndRes = false;
1529 changeStartRes = false;
1530 stretchGroup = null;
1534 public void doMouseDraggedDefineMode(MouseEvent evt)
1536 int res = findColumn(evt);
1537 int y = findSeq(evt);
1539 if (wrappedBlock != startWrapBlock)
1544 if (stretchGroup == null)
1549 mouseDragging = true;
1551 if (y > av.getAlignment().getHeight())
1553 y = av.getAlignment().getHeight() - 1;
1556 if (res >= av.getAlignment().getWidth())
1558 res = av.getAlignment().getWidth() - 1;
1561 if (stretchGroup.getEndRes() == res)
1563 // Edit end res position of selected group
1564 changeEndRes = true;
1566 else if (stretchGroup.getStartRes() == res)
1568 // Edit start res position of selected group
1569 changeStartRes = true;
1579 if (res > (stretchGroup.getStartRes() - 1))
1581 stretchGroup.setEndRes(res);
1582 needOverviewUpdate |= av.isSelectionDefinedGroup();
1585 else if (changeStartRes)
1587 if (res < (stretchGroup.getEndRes() + 1))
1589 stretchGroup.setStartRes(res);
1590 needOverviewUpdate |= av.isSelectionDefinedGroup();
1594 int dragDirection = 0;
1600 else if (y < oldSeq)
1605 while ((y != oldSeq) && (oldSeq > -1)
1606 && (y < av.getAlignment().getHeight()))
1608 // This routine ensures we don't skip any sequences, as the
1609 // selection is quite slow.
1610 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1612 oldSeq += dragDirection;
1619 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1621 if (stretchGroup.getSequences(null).contains(nextSeq))
1623 stretchGroup.deleteSequence(seq, false);
1624 needOverviewUpdate |= av.isSelectionDefinedGroup();
1630 stretchGroup.addSequence(seq, false);
1633 stretchGroup.addSequence(nextSeq, false);
1634 needOverviewUpdate |= av.isSelectionDefinedGroup();
1643 if (res > av.getRanges().getEndRes()
1644 || res < av.getRanges().getStartRes()
1645 || y < av.getRanges().getStartSeq()
1646 || y > av.getRanges().getEndSeq())
1651 if (scrollThread != null)
1653 scrollThread.setEvent(evt);
1656 seqCanvas.repaint();
1660 public void mouseEntered(MouseEvent e)
1667 if (scrollThread != null)
1669 scrollThread.running = false;
1670 scrollThread = null;
1675 public void mouseExited(MouseEvent e)
1677 if (av.getWrapAlignment())
1682 if (mouseDragging && scrollThread == null)
1684 scrollThread = new ScrollThread();
1688 void scrollCanvas(MouseEvent evt)
1692 if (scrollThread != null)
1694 scrollThread.running = false;
1695 scrollThread = null;
1697 mouseDragging = false;
1701 if (scrollThread == null)
1703 scrollThread = new ScrollThread();
1706 mouseDragging = true;
1707 scrollThread.setEvent(evt);
1712 // this class allows scrolling off the bottom of the visible alignment
1713 class ScrollThread extends Thread
1717 boolean running = false;
1719 public ScrollThread()
1724 public void setEvent(MouseEvent e)
1729 public void stopScrolling()
1744 if (mouseDragging && evt.getY() < 0
1745 && av.getRanges().getStartSeq() > 0)
1747 running = av.getRanges().scrollUp(true);
1750 if (mouseDragging && evt.getY() >= getSize().height
1751 && av.getAlignment().getHeight() > av.getRanges()
1754 running = av.getRanges().scrollUp(false);
1757 if (mouseDragging && evt.getX() < 0)
1759 running = av.getRanges().scrollRight(false);
1762 else if (mouseDragging && evt.getX() >= getSize().width)
1764 running = 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.
1790 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1791 .getSequenceSetId().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)
1821 .println("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);
1845 && (av.getColumnSelection() == null || !av
1846 .isColSelChanged(true)))
1848 // the current selection is unset or from a previous message
1849 // so import the new colsel.
1850 if (colsel == null || colsel.isEmpty())
1852 if (av.getColumnSelection() != null)
1854 av.getColumnSelection().clear();
1859 // TODO: shift colSel according to the intersecting sequences
1860 if (av.getColumnSelection() == null)
1862 av.setColumnSelection(new ColumnSelection(colsel));
1866 av.getColumnSelection().setElementsFrom(colsel,
1867 av.getAlignment().getHiddenColumns());
1870 repaint |= av.isColSelChanged(true);
1873 && av.hasHiddenColumns()
1874 && (av.getColumnSelection() == null || av.getAlignment()
1875 .getHiddenColumns().getHiddenRegions() == null))
1877 System.err.println("Bad things");
1881 ap.scalePanelHolder.repaint();
1887 * scroll to the given row/column - or nearest visible location
1892 public void scrollTo(int row, int column)
1895 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1896 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1897 ap.scrollTo(column, column, row, true, true);
1901 * scroll to the given row - or nearest visible location
1905 public void scrollToRow(int row)
1908 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1909 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1910 .getStartRes(), row, true, true);
1914 * scroll to the given column - or nearest visible location
1918 public void scrollToColumn(int column)
1921 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1922 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1926 * If this panel is a cdna/protein translation view of the selection source,
1927 * tries to map the source selection to a local one, and returns true. Else
1934 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1935 ColumnSelection colsel, HiddenColumns hidden,
1936 SelectionSource source)
1938 if (!(source instanceof AlignViewportI))
1942 final AlignViewportI sourceAv = (AlignViewportI) source;
1943 if (sourceAv.getCodingComplement() != av
1944 && av.getCodingComplement() != sourceAv)
1950 * Map sequence selection
1952 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1953 av.setSelectionGroup(sg);
1954 av.isSelectionGroupChanged(true);
1957 * Map column selection
1959 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1961 ColumnSelection cs = new ColumnSelection();
1962 HiddenColumns hs = new HiddenColumns();
1963 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1964 av.setColumnSelection(cs);
1965 av.getAlignment().setHiddenColumns(hs);
1967 ap.scalePanelHolder.repaint();