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 av.setSearchResults(null); // clear highlighting
581 seqCanvas.repaint(); // draw new/amended features
587 public void mouseReleased(MouseEvent evt)
589 boolean didDrag = mouseDragging; // did we come here after a drag
590 mouseDragging = false;
591 mouseWheelPressed = false;
595 doMouseReleasedDefineMode(evt, didDrag);
603 int startWrapBlock = -1;
605 int wrappedBlock = -1;
608 * Returns the aligned sequence position (base 0) at the mouse position, or
609 * the closest visible one
614 int findColumn(MouseEvent evt)
619 int startRes = av.getRanges().getStartRes();
620 if (av.getWrapAlignment())
623 int hgap = av.getCharHeight();
624 if (av.getScaleAboveWrapped())
626 hgap += av.getCharHeight();
629 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
630 + hgap + seqCanvas.getAnnotationHeight();
634 x = Math.max(0, x - seqCanvas.LABEL_WEST);
636 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
642 wrappedBlock = y / cHeight;
643 wrappedBlock += startRes / cwidth;
644 int startOffset = startRes % cwidth; // in case start is scrolled right
646 res = wrappedBlock * cwidth + startOffset
647 + +Math.min(cwidth - 1, x / av.getCharWidth());
651 res = (x / av.getCharWidth()) + startRes;
654 if (av.hasHiddenColumns())
656 res = av.getAlignment().getHiddenColumns()
657 .adjustForHiddenColumns(res);
664 int findSeq(MouseEvent evt)
666 final int sqnum = findAlRow(evt);
667 return (sqnum < 0) ? 0 : sqnum;
673 * @return row in alignment that was selected (or -1 for column selection)
675 private int findAlRow(MouseEvent evt)
680 if (av.getWrapAlignment())
682 int hgap = av.getCharHeight();
683 if (av.getScaleAboveWrapped())
685 hgap += av.getCharHeight();
688 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
689 + hgap + seqCanvas.getAnnotationHeight();
693 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
702 seq = Math.min((y / av.getCharHeight())
703 + av.getRanges().getStartSeq(),
705 .getAlignment().getHeight() - 1);
715 public void doMousePressed(MouseEvent evt)
718 int seq = findSeq(evt);
719 int res = findColumn(evt);
721 if (seq < av.getAlignment().getHeight()
722 && res < av.getAlignment().getSequenceAt(seq).getLength())
724 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
725 // Find the residue's position in the sequence (res is the position
743 public void mouseOverSequence(SequenceI sequence, int index, int pos)
745 String tmp = sequence.hashCode() + index + "";
746 if (lastMessage == null || !lastMessage.equals(tmp))
748 ssm.mouseOverSequence(sequence, index, pos, av);
755 public void highlightSequence(SearchResultsI results)
757 if (av.isFollowHighlight())
759 // don't allow highlight of protein/cDNA to also scroll a complementary
760 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
761 // over residue to change abruptly, causing highlighted residue in panel 2
762 // to change, causing a scroll in panel 1 etc)
763 ap.setToScrollComplementPanel(false);
764 if (ap.scrollToPosition(results, true))
766 ap.alignFrame.repaint();
768 ap.setToScrollComplementPanel(true);
770 setStatusMessage(results);
771 seqCanvas.highlightSearchResults(results);
776 public VamsasSource getVamsasSource()
778 return this.ap == null ? null : this.ap.av;
782 public void updateColours(SequenceI seq, int index)
784 System.out.println("update the seqPanel colours");
789 public void mouseMoved(MouseEvent evt)
791 final int column = findColumn(evt);
792 int seq = findSeq(evt);
794 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
803 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
804 if (column > sequence.getLength())
813 final char ch = sequence.getCharAt(column);
814 boolean isGapped = Comparison.isGap(ch);
815 // find residue at column (or nearest if at a gap)
816 int respos = sequence.findPosition(column);
818 if (ssm != null && !isGapped)
820 mouseOverSequence(sequence, column, respos);
823 StringBuilder text = new StringBuilder();
824 text.append("Sequence ").append(Integer.toString(seq + 1))
825 .append(" ID: ").append(sequence.getName());
829 if (av.getAlignment().isNucleotide())
831 String base = ResidueProperties.nucleotideName.get(ch);
832 text.append(" Nucleotide: ").append(base == null ? ch : base);
836 String residue = (ch == 'x' || ch == 'X') ? "X"
837 : ResidueProperties.aa2Triplet
838 .get(String.valueOf(ch));
839 text.append(" Residue: ").append(residue == null ? ch : residue);
841 text.append(" (").append(Integer.toString(respos)).append(")");
844 ap.alignFrame.statusBar.setText(text.toString());
846 StringBuilder tooltipText = new StringBuilder();
847 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
850 for (int g = 0; g < groups.length; g++)
852 if (groups[g].getStartRes() <= column && groups[g].getEndRes() >= column)
854 if (!groups[g].getName().startsWith("JTreeGroup")
855 && !groups[g].getName().startsWith("JGroup"))
857 tooltipText.append(groups[g].getName()).append(" ");
859 if (groups[g].getDescription() != null)
861 tooltipText.append(groups[g].getDescription());
863 tooltipText.append("\n");
869 * add feature details to tooltip, including any that straddle
872 if (av.isShowSequenceFeatures())
874 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
876 for (SequenceFeature sf : allFeatures)
878 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
880 if (sf.getDescription() != null)
882 tooltipText.append(" " + sf.getDescription());
885 if (sf.getValue("status") != null)
887 String status = sf.getValue("status").toString();
888 if (status.length() > 0)
890 tooltipText.append(" (" + sf.getValue("status") + ")");
893 tooltipText.append("\n");
899 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
903 tooltip.setTip(tooltipText.toString());
908 * Returns features at the specified aligned column on the given sequence.
909 * Non-positional features are not included. If the column has a gap, then
910 * enclosing features are included (but not contact features).
917 List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
919 return seqCanvas.getFeatureRenderer().findFeaturesAtColumn(sequence, column);
925 * set when the current UI interaction has resulted in a change that requires
926 * overview shading to be recalculated. this could be changed to something
927 * more expressive that indicates what actually has changed, so selective
928 * redraws can be applied
930 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
933 public void mouseDragged(MouseEvent evt)
935 if (mouseWheelPressed)
937 int oldWidth = av.getCharWidth();
939 // Which is bigger, left-right or up-down?
940 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
943 int fontSize = av.font.getSize();
945 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
949 else if (evt.getY() > lastMousePress.y)
960 new Font(av.font.getName(), av.font.getStyle(), fontSize),
962 av.setCharWidth(oldWidth);
966 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
968 av.setCharWidth(av.getCharWidth() - 1);
970 else if (evt.getX() > lastMousePress.x)
972 av.setCharWidth(av.getCharWidth() + 1);
975 if (av.getCharWidth() < 1)
983 FontMetrics fm = getFontMetrics(av.getFont());
984 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
986 lastMousePress = evt.getPoint();
988 ap.paintAlignment(false);
989 ap.annotationPanel.image = null;
995 doMouseDraggedDefineMode(evt);
999 int res = findColumn(evt);
1006 if ((lastres == -1) || (lastres == res))
1011 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1013 // dragLeft, delete gap
1014 editSequence(false, res);
1018 editSequence(true, res);
1021 mouseDragging = true;
1022 if (scrollThread != null)
1024 scrollThread.setEvent(evt);
1029 synchronized void editSequence(boolean insertGap, int startres)
1032 int fixedRight = -1;
1033 boolean fixedColumns = false;
1034 SequenceGroup sg = av.getSelectionGroup();
1036 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1038 if (!groupEditing && av.hasHiddenRows())
1040 if (av.isHiddenRepSequence(seq))
1042 sg = av.getRepresentedSequences(seq);
1043 groupEditing = true;
1047 StringBuffer message = new StringBuffer();
1050 message.append(MessageManager.getString("action.edit_group")).append(
1052 if (editCommand == null)
1054 editCommand = new EditCommand(
1055 MessageManager.getString("action.edit_group"));
1060 message.append(MessageManager.getString("label.edit_sequence"))
1061 .append(" " + seq.getName());
1062 String label = seq.getName();
1063 if (label.length() > 10)
1065 label = label.substring(0, 10);
1067 if (editCommand == null)
1069 editCommand = new EditCommand(MessageManager.formatMessage(
1070 "label.edit_params", new String[] { label }));
1076 message.append(" insert ");
1080 message.append(" delete ");
1083 message.append(Math.abs(startres - lastres) + " gaps.");
1084 ap.alignFrame.statusBar.setText(message.toString());
1086 // Are we editing within a selection group?
1088 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1091 fixedColumns = true;
1093 // sg might be null as the user may only see 1 sequence,
1094 // but the sequence represents a group
1097 if (!av.isHiddenRepSequence(seq))
1103 sg = av.getRepresentedSequences(seq);
1106 fixedLeft = sg.getStartRes();
1107 fixedRight = sg.getEndRes();
1109 if ((startres < fixedLeft && lastres >= fixedLeft)
1110 || (startres >= fixedLeft && lastres < fixedLeft)
1111 || (startres > fixedRight && lastres <= fixedRight)
1112 || (startres <= fixedRight && lastres > fixedRight))
1118 if (fixedLeft > startres)
1120 fixedRight = fixedLeft - 1;
1123 else if (fixedRight < startres)
1125 fixedLeft = fixedRight;
1130 if (av.hasHiddenColumns())
1132 fixedColumns = true;
1133 int y1 = av.getAlignment().getHiddenColumns()
1134 .getHiddenBoundaryLeft(startres);
1135 int y2 = av.getAlignment().getHiddenColumns()
1136 .getHiddenBoundaryRight(startres);
1138 if ((insertGap && startres > y1 && lastres < y1)
1139 || (!insertGap && startres < y2 && lastres > y2))
1145 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1146 // Selection spans a hidden region
1147 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1155 fixedRight = y2 - 1;
1162 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1163 .toArray(new SequenceI[0]);
1168 // If the user has selected the whole sequence, and is dragging to
1169 // the right, we can still extend the alignment and selectionGroup
1170 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1171 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1173 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1174 fixedRight = sg.getEndRes();
1177 // Is it valid with fixed columns??
1178 // Find the next gap before the end
1179 // of the visible region boundary
1180 boolean blank = false;
1181 for (; fixedRight > lastres; fixedRight--)
1185 for (SequenceI gs : groupSeqs)
1187 for (int j = 0; j < startres - lastres; j++)
1189 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1205 if (sg.getSize() == av.getAlignment().getHeight())
1207 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1208 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1214 int alWidth = av.getAlignment().getWidth();
1215 if (av.hasHiddenRows())
1217 int hwidth = av.getAlignment().getHiddenSequences()
1219 if (hwidth > alWidth)
1224 // We can still insert gaps if the selectionGroup
1225 // contains all the sequences
1226 sg.setEndRes(sg.getEndRes() + startres - lastres);
1227 fixedRight = alWidth + startres - lastres;
1238 else if (!insertGap)
1240 // / Are we able to delete?
1241 // ie are all columns blank?
1243 for (SequenceI gs : groupSeqs)
1245 for (int j = startres; j < lastres; j++)
1247 if (gs.getLength() <= j)
1252 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1254 // Not a gap, block edit not valid
1264 // dragging to the right
1265 if (fixedColumns && fixedRight != -1)
1267 for (int j = lastres; j < startres; j++)
1269 insertChar(j, groupSeqs, fixedRight);
1274 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1275 startres - lastres, av.getAlignment(), true);
1280 // dragging to the left
1281 if (fixedColumns && fixedRight != -1)
1283 for (int j = lastres; j > startres; j--)
1285 deleteChar(startres, groupSeqs, fixedRight);
1290 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1291 lastres - startres, av.getAlignment(), true);
1297 // ///Editing a single sequence///////////
1301 // dragging to the right
1302 if (fixedColumns && fixedRight != -1)
1304 for (int j = lastres; j < startres; j++)
1306 insertChar(j, new SequenceI[] { seq }, fixedRight);
1311 editCommand.appendEdit(Action.INSERT_GAP,
1312 new SequenceI[] { seq }, lastres, startres - lastres,
1313 av.getAlignment(), true);
1318 // dragging to the left
1319 if (fixedColumns && fixedRight != -1)
1321 for (int j = lastres; j > startres; j--)
1323 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1328 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1333 // could be a keyboard edit trying to delete none gaps
1335 for (int m = startres; m < lastres; m++)
1337 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1346 editCommand.appendEdit(Action.DELETE_GAP,
1347 new SequenceI[] { seq }, startres, max,
1348 av.getAlignment(), true);
1355 seqCanvas.repaint();
1358 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1360 int blankColumn = fixedColumn;
1361 for (int s = 0; s < seq.length; s++)
1363 // Find the next gap before the end of the visible region boundary
1364 // If lastCol > j, theres a boundary after the gap insertion
1366 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1368 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1370 // Theres a space, so break and insert the gap
1375 if (blankColumn <= j)
1377 blankColumn = fixedColumn;
1383 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1384 av.getAlignment(), true);
1386 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1391 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1394 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1397 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1398 av.getAlignment(), true);
1401 // ////////////////////////////////////////
1402 // ///Everything below this is for defining the boundary of the rubberband
1403 // ////////////////////////////////////////
1404 public void doMousePressedDefineMode(MouseEvent evt)
1406 if (scrollThread != null)
1408 scrollThread.running = false;
1409 scrollThread = null;
1412 int column = findColumn(evt);
1413 int seq = findSeq(evt);
1415 startWrapBlock = wrappedBlock;
1422 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1424 if (sequence == null || column > sequence.getLength())
1429 stretchGroup = av.getSelectionGroup();
1431 if (stretchGroup == null || !stretchGroup.contains(sequence, column))
1433 stretchGroup = av.getAlignment().findGroup(sequence, column);
1434 if (stretchGroup != null)
1436 // only update the current selection if the popup menu has a group to
1438 av.setSelectionGroup(stretchGroup);
1442 // DETECT RIGHT MOUSE BUTTON IN AWT
1443 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1445 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
1446 sequence.findPosition(column + 1));
1448 Vector<String> links = null;
1449 for (SequenceFeature sf : allFeatures)
1451 if (sf.links != null)
1455 links = new Vector<String>();
1457 links.addAll(sf.links);
1460 APopupMenu popup = new APopupMenu(ap, null, links);
1462 popup.show(this, evt.getX(), evt.getY());
1468 seqCanvas.cursorX = findColumn(evt);
1469 seqCanvas.cursorY = findSeq(evt);
1470 seqCanvas.repaint();
1474 // Only if left mouse button do we want to change group sizes
1476 if (stretchGroup == null)
1478 // define a new group here
1479 SequenceGroup sg = new SequenceGroup();
1480 sg.setStartRes(column);
1481 sg.setEndRes(column);
1482 sg.addSequence(sequence, false);
1483 av.setSelectionGroup(sg);
1486 if (av.getConservationSelected())
1488 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1491 if (av.getAbovePIDThreshold())
1493 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1500 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1502 if (stretchGroup == null)
1506 // always do this - annotation has own state
1507 // but defer colourscheme update until hidden sequences are passed in
1508 boolean vischange = stretchGroup.recalcConservation(true);
1509 // here we rely on stretchGroup == av.getSelection()
1510 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1512 if (stretchGroup.cs != null)
1514 stretchGroup.cs.alignmentChanged(stretchGroup,
1515 av.getHiddenRepSequences());
1517 if (stretchGroup.cs.conservationApplied())
1519 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1520 stretchGroup.getName());
1522 if (stretchGroup.cs.getThreshold() > 0)
1524 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1525 stretchGroup.getName());
1528 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1529 ap.paintAlignment(needOverviewUpdate);
1530 needOverviewUpdate = false;
1531 changeEndRes = false;
1532 changeStartRes = false;
1533 stretchGroup = null;
1537 public void doMouseDraggedDefineMode(MouseEvent evt)
1539 int res = findColumn(evt);
1540 int y = findSeq(evt);
1542 if (wrappedBlock != startWrapBlock)
1547 if (stretchGroup == null)
1552 mouseDragging = true;
1554 if (y > av.getAlignment().getHeight())
1556 y = av.getAlignment().getHeight() - 1;
1559 if (res >= av.getAlignment().getWidth())
1561 res = av.getAlignment().getWidth() - 1;
1564 if (stretchGroup.getEndRes() == res)
1566 // Edit end res position of selected group
1567 changeEndRes = true;
1569 else if (stretchGroup.getStartRes() == res)
1571 // Edit start res position of selected group
1572 changeStartRes = true;
1582 if (res > (stretchGroup.getStartRes() - 1))
1584 stretchGroup.setEndRes(res);
1585 needOverviewUpdate |= av.isSelectionDefinedGroup();
1588 else if (changeStartRes)
1590 if (res < (stretchGroup.getEndRes() + 1))
1592 stretchGroup.setStartRes(res);
1593 needOverviewUpdate |= av.isSelectionDefinedGroup();
1597 int dragDirection = 0;
1603 else if (y < oldSeq)
1608 while ((y != oldSeq) && (oldSeq > -1)
1609 && (y < av.getAlignment().getHeight()))
1611 // This routine ensures we don't skip any sequences, as the
1612 // selection is quite slow.
1613 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1615 oldSeq += dragDirection;
1622 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1624 if (stretchGroup.getSequences(null).contains(nextSeq))
1626 stretchGroup.deleteSequence(seq, false);
1627 needOverviewUpdate |= av.isSelectionDefinedGroup();
1633 stretchGroup.addSequence(seq, false);
1636 stretchGroup.addSequence(nextSeq, false);
1637 needOverviewUpdate |= av.isSelectionDefinedGroup();
1646 if (res > av.getRanges().getEndRes()
1647 || res < av.getRanges().getStartRes()
1648 || y < av.getRanges().getStartSeq()
1649 || y > av.getRanges().getEndSeq())
1654 if (scrollThread != null)
1656 scrollThread.setEvent(evt);
1659 seqCanvas.repaint();
1663 public void mouseEntered(MouseEvent e)
1670 if (scrollThread != null)
1672 scrollThread.running = false;
1673 scrollThread = null;
1678 public void mouseExited(MouseEvent e)
1680 if (av.getWrapAlignment())
1685 if (mouseDragging && scrollThread == null)
1687 scrollThread = new ScrollThread();
1691 void scrollCanvas(MouseEvent evt)
1695 if (scrollThread != null)
1697 scrollThread.running = false;
1698 scrollThread = null;
1700 mouseDragging = false;
1704 if (scrollThread == null)
1706 scrollThread = new ScrollThread();
1709 mouseDragging = true;
1710 scrollThread.setEvent(evt);
1715 // this class allows scrolling off the bottom of the visible alignment
1716 class ScrollThread extends Thread
1720 boolean running = false;
1722 public ScrollThread()
1727 public void setEvent(MouseEvent e)
1732 public void stopScrolling()
1747 if (mouseDragging && evt.getY() < 0
1748 && av.getRanges().getStartSeq() > 0)
1750 running = av.getRanges().scrollUp(true);
1753 if (mouseDragging && evt.getY() >= getSize().height
1754 && av.getAlignment().getHeight() > av.getRanges()
1757 running = av.getRanges().scrollUp(false);
1760 if (mouseDragging && evt.getX() < 0)
1762 running = av.getRanges().scrollRight(false);
1765 else if (mouseDragging && evt.getX() >= getSize().width)
1767 running = av.getRanges().scrollRight(true);
1774 } catch (Exception ex)
1782 * modify current selection according to a received message.
1785 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1786 HiddenColumns hidden, SelectionSource source)
1788 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1789 // handles selection messages...
1790 // TODO: extend config options to allow user to control if selections may be
1791 // shared between viewports.
1793 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1794 .getSequenceSetId().equals(av.getSequenceSetId()))))
1800 * Check for selection in a view of which this one is a dna/protein
1803 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1808 // do we want to thread this ? (contention with seqsel and colsel locks, I
1811 * only copy colsel if there is a real intersection between
1812 * sequence selection and this panel's alignment
1814 boolean repaint = false;
1815 boolean copycolsel = false;
1816 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1818 SequenceGroup sgroup = null;
1819 if (seqsel != null && seqsel.getSize() > 0)
1821 if (av.getAlignment() == null)
1824 .println("Selection message: alignviewport av SeqSetId="
1825 + av.getSequenceSetId() + " ViewId="
1827 + " 's alignment is NULL! returning immediatly.");
1830 sgroup = seqsel.intersect(av.getAlignment(),
1831 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1832 if ((sgroup != null && sgroup.getSize() > 0))
1837 if (sgroup != null && sgroup.getSize() > 0)
1839 av.setSelectionGroup(sgroup);
1843 av.setSelectionGroup(null);
1845 repaint = av.isSelectionGroupChanged(true);
1848 && (av.getColumnSelection() == null || !av
1849 .isColSelChanged(true)))
1851 // the current selection is unset or from a previous message
1852 // so import the new colsel.
1853 if (colsel == null || colsel.isEmpty())
1855 if (av.getColumnSelection() != null)
1857 av.getColumnSelection().clear();
1862 // TODO: shift colSel according to the intersecting sequences
1863 if (av.getColumnSelection() == null)
1865 av.setColumnSelection(new ColumnSelection(colsel));
1869 av.getColumnSelection().setElementsFrom(colsel,
1870 av.getAlignment().getHiddenColumns());
1873 repaint |= av.isColSelChanged(true);
1876 && av.hasHiddenColumns()
1877 && (av.getColumnSelection() == null))
1879 System.err.println("Bad things");
1883 ap.scalePanelHolder.repaint();
1889 * scroll to the given row/column - or nearest visible location
1894 public void scrollTo(int row, int column)
1897 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1898 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1899 ap.scrollTo(column, column, row, true, true);
1903 * scroll to the given row - or nearest visible location
1907 public void scrollToRow(int row)
1910 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1911 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1912 .getStartRes(), row, true, true);
1916 * scroll to the given column - or nearest visible location
1920 public void scrollToColumn(int column)
1923 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1924 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1928 * If this panel is a cdna/protein translation view of the selection source,
1929 * tries to map the source selection to a local one, and returns true. Else
1936 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1937 ColumnSelection colsel, HiddenColumns hidden,
1938 SelectionSource source)
1940 if (!(source instanceof AlignViewportI))
1944 final AlignViewportI sourceAv = (AlignViewportI) source;
1945 if (sourceAv.getCodingComplement() != av
1946 && av.getCodingComplement() != sourceAv)
1952 * Map sequence selection
1954 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1955 av.setSelectionGroup(sg);
1956 av.isSelectionGroupChanged(true);
1959 * Map column selection
1961 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1963 ColumnSelection cs = new ColumnSelection();
1964 HiddenColumns hs = new HiddenColumns();
1965 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1966 av.setColumnSelection(cs);
1967 av.getAlignment().setHiddenColumns(hs);
1969 ap.scalePanelHolder.repaint();