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,
136 av.getAlignment().getSequences());
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() && !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;
208 void scrollToVisible()
210 if (seqCanvas.cursorX < 0)
212 seqCanvas.cursorX = 0;
214 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
216 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
219 if (seqCanvas.cursorY < 0)
221 seqCanvas.cursorY = 0;
223 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
225 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
229 if (av.getWrapAlignment())
231 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
235 ViewportRanges ranges = av.getRanges();
236 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
237 while (seqCanvas.cursorY < ranges.getStartSeq())
239 ranges.scrollUp(true);
241 while (seqCanvas.cursorY > ranges.getEndSeq())
243 ranges.scrollUp(false);
245 while (seqCanvas.cursorX < hidden
246 .adjustForHiddenColumns(ranges.getStartRes()))
249 if (!ranges.scrollRight(false))
254 while (seqCanvas.cursorX > hidden
255 .adjustForHiddenColumns(ranges.getEndRes()))
257 if (!ranges.scrollRight(true))
263 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
264 seqCanvas.cursorX, seqCanvas.cursorY);
269 void setSelectionAreaAtCursor(boolean topLeft)
271 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
273 if (av.getSelectionGroup() != null)
275 SequenceGroup sg = av.getSelectionGroup();
276 // Find the top and bottom of this group
277 int min = av.getAlignment().getHeight(), max = 0;
278 for (int i = 0; i < sg.getSize(); i++)
280 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
295 sg.setStartRes(seqCanvas.cursorX);
296 if (sg.getEndRes() < seqCanvas.cursorX)
298 sg.setEndRes(seqCanvas.cursorX);
301 min = seqCanvas.cursorY;
305 sg.setEndRes(seqCanvas.cursorX);
306 if (sg.getStartRes() > seqCanvas.cursorX)
308 sg.setStartRes(seqCanvas.cursorX);
311 max = seqCanvas.cursorY + 1;
316 // Only the user can do this
317 av.setSelectionGroup(null);
321 // Now add any sequences between min and max
323 for (int i = min; i < max; i++)
325 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
330 if (av.getSelectionGroup() == null)
332 SequenceGroup sg = new SequenceGroup();
333 sg.setStartRes(seqCanvas.cursorX);
334 sg.setEndRes(seqCanvas.cursorX);
335 sg.addSequence(sequence, false);
336 av.setSelectionGroup(sg);
338 ap.paintAlignment(false);
342 void insertGapAtCursor(boolean group)
344 groupEditing = group;
345 startseq = seqCanvas.cursorY;
346 lastres = seqCanvas.cursorX;
347 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
351 void deleteGapAtCursor(boolean group)
353 groupEditing = group;
354 startseq = seqCanvas.cursorY;
355 lastres = seqCanvas.cursorX + getKeyboardNo1();
356 editSequence(false, seqCanvas.cursorX);
360 void numberPressed(char value)
362 if (keyboardNo1 == null)
364 keyboardNo1 = new StringBuffer();
367 if (keyboardNo2 != null)
369 keyboardNo2.append(value);
373 keyboardNo1.append(value);
381 if (keyboardNo1 != null)
383 int value = Integer.parseInt(keyboardNo1.toString());
387 } catch (Exception x)
398 if (keyboardNo2 != null)
400 int value = Integer.parseInt(keyboardNo2.toString());
404 } catch (Exception x)
412 * Set status message in alignment panel
415 * aligned sequence object
419 * index of sequence in alignment
420 * @return position of column in sequence or -1 if at gap
422 void setStatusMessage(SequenceI sequence, int column, int seq)
424 // TODO remove duplication of identical gui method
425 StringBuilder text = new StringBuilder(32);
426 String seqno = seq == -1 ? "" : " " + (seq + 1);
427 text.append("Sequence" + seqno + " ID: " + sequence.getName());
429 String residue = null;
431 * Try to translate the display character to residue name (null for gap).
433 final String displayChar = String.valueOf(sequence.getCharAt(column));
434 if (av.getAlignment().isNucleotide())
436 residue = ResidueProperties.nucleotideName.get(displayChar);
439 text.append(" Nucleotide: ").append(residue);
444 residue = "X".equalsIgnoreCase(displayChar) ? "X"
445 : ("*".equals(displayChar) ? "STOP"
446 : ResidueProperties.aa2Triplet.get(displayChar));
449 text.append(" Residue: ").append(residue);
456 pos = sequence.findPosition(column);
457 text.append(" (").append(Integer.toString(pos)).append(")");
460 ap.alignFrame.statusBar.setText(text.toString());
464 * Set the status bar message to highlight the first matched position in
468 * @return true if results were matched, false if not
470 private boolean setStatusMessage(SearchResultsI results)
472 AlignmentI al = this.av.getAlignment();
473 int sequenceIndex = al.findIndex(results);
474 if (sequenceIndex == -1)
478 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
479 for (SearchResultMatchI m : results.getResults())
481 SequenceI seq = m.getSequence();
482 if (seq.getDatasetSequence() != null)
484 seq = seq.getDatasetSequence();
490 * Convert position in sequence (base 1) to sequence character array
493 int start = m.getStart() - m.getSequence().getStart();
494 setStatusMessage(seq, start, sequenceIndex);
502 public void mousePressed(MouseEvent evt)
504 lastMousePress = evt.getPoint();
506 // For now, ignore the mouseWheel font resizing on Macs
507 // As the Button2_mask always seems to be true
508 if ((evt.getModifiers()
509 & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
512 mouseWheelPressed = true;
516 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
518 if (evt.isControlDown() || evt.isAltDown())
526 doMousePressedDefineMode(evt);
530 int seq = findSeq(evt);
531 int res = findColumn(evt);
533 if (seq < 0 || res < 0)
538 if ((seq < av.getAlignment().getHeight())
539 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
554 public void mouseClicked(MouseEvent evt)
556 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
557 if (evt.getClickCount() > 1)
559 if (av.getSelectionGroup() != null
560 && av.getSelectionGroup().getSize() == 1
561 && av.getSelectionGroup().getEndRes()
562 - av.getSelectionGroup().getStartRes() < 2)
564 av.setSelectionGroup(null);
567 int column = findColumn(evt);
568 List<SequenceFeature> features = findFeaturesAtColumn(sequence,
571 if (!features.isEmpty())
573 SearchResultsI highlight = new SearchResults();
574 highlight.addResult(sequence, features.get(0).getBegin(),
575 features.get(0).getEnd());
576 seqCanvas.highlightSearchResults(highlight);
577 seqCanvas.getFeatureRenderer().amendFeatures(
578 Collections.singletonList(sequence), features, false, ap);
579 av.setSearchResults(null); // clear highlighting
580 seqCanvas.repaint(); // draw new/amended features
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 int startRes = av.getRanges().getStartRes();
619 if (av.getWrapAlignment())
622 int hgap = av.getCharHeight();
623 if (av.getScaleAboveWrapped())
625 hgap += av.getCharHeight();
628 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
629 + hgap + seqCanvas.getAnnotationHeight();
633 x = Math.max(0, x - seqCanvas.LABEL_WEST);
635 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
641 wrappedBlock = y / cHeight;
642 wrappedBlock += startRes / cwidth;
643 int startOffset = startRes % cwidth; // in case start is scrolled right
645 res = wrappedBlock * cwidth + startOffset
646 + +Math.min(cwidth - 1, x / av.getCharWidth());
650 res = (x / av.getCharWidth()) + startRes;
653 if (av.hasHiddenColumns())
655 res = av.getAlignment().getHiddenColumns()
656 .adjustForHiddenColumns(res);
663 int findSeq(MouseEvent evt)
665 final int sqnum = findAlRow(evt);
666 return (sqnum < 0) ? 0 : sqnum;
672 * @return row in alignment that was selected (or -1 for column selection)
674 private int findAlRow(MouseEvent evt)
679 if (av.getWrapAlignment())
681 int hgap = av.getCharHeight();
682 if (av.getScaleAboveWrapped())
684 hgap += av.getCharHeight();
687 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
688 + hgap + seqCanvas.getAnnotationHeight();
692 seq = Math.min((y % cHeight) / av.getCharHeight(),
693 av.getAlignment().getHeight() - 1);
702 (y / av.getCharHeight()) + av.getRanges().getStartSeq(),
703 av.getAlignment().getHeight() - 1);
713 public void doMousePressed(MouseEvent evt)
716 int seq = findSeq(evt);
717 int res = findColumn(evt);
719 if (seq < av.getAlignment().getHeight()
720 && res < av.getAlignment().getSequenceAt(seq).getLength())
722 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
723 // Find the residue's position in the sequence (res is the position
741 public void mouseOverSequence(SequenceI sequence, int index, int pos)
743 String tmp = sequence.hashCode() + index + "";
744 if (lastMessage == null || !lastMessage.equals(tmp))
746 ssm.mouseOverSequence(sequence, index, pos, av);
753 public void highlightSequence(SearchResultsI results)
755 if (av.isFollowHighlight())
757 // don't allow highlight of protein/cDNA to also scroll a complementary
758 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
759 // over residue to change abruptly, causing highlighted residue in panel 2
760 // to change, causing a scroll in panel 1 etc)
761 ap.setToScrollComplementPanel(false);
762 if (ap.scrollToPosition(results, true))
764 ap.alignFrame.repaint();
766 ap.setToScrollComplementPanel(true);
768 setStatusMessage(results);
769 seqCanvas.highlightSearchResults(results);
774 public VamsasSource getVamsasSource()
776 return this.ap == null ? null : this.ap.av;
780 public void updateColours(SequenceI seq, int index)
782 System.out.println("update the seqPanel colours");
787 public void mouseMoved(MouseEvent evt)
789 final int column = findColumn(evt);
790 int seq = findSeq(evt);
792 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
801 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
802 if (column > sequence.getLength())
811 final char ch = sequence.getCharAt(column);
812 boolean isGapped = Comparison.isGap(ch);
813 // find residue at column (or nearest if at a gap)
814 int respos = sequence.findPosition(column);
816 if (ssm != null && !isGapped)
818 mouseOverSequence(sequence, column, respos);
821 StringBuilder text = new StringBuilder();
822 text.append("Sequence ").append(Integer.toString(seq + 1))
823 .append(" ID: ").append(sequence.getName());
827 if (av.getAlignment().isNucleotide())
829 String base = ResidueProperties.nucleotideName.get(ch);
830 text.append(" Nucleotide: ").append(base == null ? ch : base);
834 String residue = (ch == 'x' || ch == 'X') ? "X"
835 : ResidueProperties.aa2Triplet.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
850 && groups[g].getEndRes() >= column)
852 if (!groups[g].getName().startsWith("JTreeGroup")
853 && !groups[g].getName().startsWith("JGroup"))
855 tooltipText.append(groups[g].getName()).append(" ");
857 if (groups[g].getDescription() != null)
859 tooltipText.append(groups[g].getDescription());
861 tooltipText.append("\n");
867 * add feature details to tooltip, including any that straddle
870 if (av.isShowSequenceFeatures())
872 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
874 for (SequenceFeature sf : allFeatures)
876 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
878 if (sf.getDescription() != null)
880 tooltipText.append(" " + sf.getDescription());
883 if (sf.getValue("status") != null)
885 String status = sf.getValue("status").toString();
886 if (status.length() > 0)
888 tooltipText.append(" (" + sf.getValue("status") + ")");
891 tooltipText.append("\n");
897 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
901 tooltip.setTip(tooltipText.toString());
906 * Returns features at the specified aligned column on the given sequence.
907 * Non-positional features are not included. If the column has a gap, then
908 * enclosing features are included (but not contact features).
915 List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
917 return seqCanvas.getFeatureRenderer().findFeaturesAtColumn(sequence, column);
923 * set when the current UI interaction has resulted in a change that requires
924 * overview shading to be recalculated. this could be changed to something
925 * more expressive that indicates what actually has changed, so selective
926 * redraws can be applied
928 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
931 public void mouseDragged(MouseEvent evt)
933 if (mouseWheelPressed)
935 int oldWidth = av.getCharWidth();
937 // Which is bigger, left-right or up-down?
938 if (Math.abs(evt.getY() - lastMousePress.y) > Math
939 .abs(evt.getX() - lastMousePress.x))
941 int fontSize = av.font.getSize();
943 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
947 else if (evt.getY() > lastMousePress.y)
958 new Font(av.font.getName(), av.font.getStyle(), fontSize),
960 av.setCharWidth(oldWidth);
964 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
966 av.setCharWidth(av.getCharWidth() - 1);
968 else if (evt.getX() > lastMousePress.x)
970 av.setCharWidth(av.getCharWidth() + 1);
973 if (av.getCharWidth() < 1)
981 FontMetrics fm = getFontMetrics(av.getFont());
982 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
984 lastMousePress = evt.getPoint();
986 ap.paintAlignment(false);
987 ap.annotationPanel.image = null;
993 doMouseDraggedDefineMode(evt);
997 int res = findColumn(evt);
1004 if ((lastres == -1) || (lastres == res))
1009 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1011 // dragLeft, delete gap
1012 editSequence(false, res);
1016 editSequence(true, res);
1019 mouseDragging = true;
1020 if (scrollThread != null)
1022 scrollThread.setEvent(evt);
1027 synchronized void editSequence(boolean insertGap, int startres)
1030 int fixedRight = -1;
1031 boolean fixedColumns = false;
1032 SequenceGroup sg = av.getSelectionGroup();
1034 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1036 if (!groupEditing && av.hasHiddenRows())
1038 if (av.isHiddenRepSequence(seq))
1040 sg = av.getRepresentedSequences(seq);
1041 groupEditing = true;
1045 StringBuffer message = new StringBuffer();
1048 message.append(MessageManager.getString("action.edit_group"))
1050 if (editCommand == null)
1052 editCommand = new EditCommand(
1053 MessageManager.getString("action.edit_group"));
1058 message.append(MessageManager.getString("label.edit_sequence"))
1059 .append(" " + seq.getName());
1060 String label = seq.getName();
1061 if (label.length() > 10)
1063 label = label.substring(0, 10);
1065 if (editCommand == null)
1067 editCommand = new EditCommand(MessageManager
1068 .formatMessage("label.edit_params", new String[]
1075 message.append(" insert ");
1079 message.append(" delete ");
1082 message.append(Math.abs(startres - lastres) + " gaps.");
1083 ap.alignFrame.statusBar.setText(message.toString());
1085 // Are we editing within a selection group?
1086 if (groupEditing || (sg != null
1087 && sg.getSequences(av.getHiddenRepSequences()).contains(seq)))
1089 fixedColumns = true;
1091 // sg might be null as the user may only see 1 sequence,
1092 // but the sequence represents a group
1095 if (!av.isHiddenRepSequence(seq))
1101 sg = av.getRepresentedSequences(seq);
1104 fixedLeft = sg.getStartRes();
1105 fixedRight = sg.getEndRes();
1107 if ((startres < fixedLeft && lastres >= fixedLeft)
1108 || (startres >= fixedLeft && lastres < fixedLeft)
1109 || (startres > fixedRight && lastres <= fixedRight)
1110 || (startres <= fixedRight && lastres > fixedRight))
1116 if (fixedLeft > startres)
1118 fixedRight = fixedLeft - 1;
1121 else if (fixedRight < startres)
1123 fixedLeft = fixedRight;
1128 if (av.hasHiddenColumns())
1130 fixedColumns = true;
1131 int y1 = av.getAlignment().getHiddenColumns()
1132 .getHiddenBoundaryLeft(startres);
1133 int y2 = av.getAlignment().getHiddenColumns()
1134 .getHiddenBoundaryRight(startres);
1136 if ((insertGap && startres > y1 && lastres < y1)
1137 || (!insertGap && startres < y2 && lastres > y2))
1143 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1144 // Selection spans a hidden region
1145 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1153 fixedRight = y2 - 1;
1160 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1161 .toArray(new SequenceI[0]);
1166 // If the user has selected the whole sequence, and is dragging to
1167 // the right, we can still extend the alignment and selectionGroup
1168 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1169 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1171 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1172 fixedRight = sg.getEndRes();
1175 // Is it valid with fixed columns??
1176 // Find the next gap before the end
1177 // of the visible region boundary
1178 boolean blank = false;
1179 for (; fixedRight > lastres; fixedRight--)
1183 for (SequenceI gs : groupSeqs)
1185 for (int j = 0; j < startres - lastres; j++)
1187 if (!jalview.util.Comparison
1188 .isGap(gs.getCharAt(fixedRight - j)))
1203 if (sg.getSize() == av.getAlignment().getHeight())
1205 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1206 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1212 int alWidth = av.getAlignment().getWidth();
1213 if (av.hasHiddenRows())
1215 int hwidth = av.getAlignment().getHiddenSequences()
1217 if (hwidth > alWidth)
1222 // We can still insert gaps if the selectionGroup
1223 // contains all the sequences
1224 sg.setEndRes(sg.getEndRes() + startres - lastres);
1225 fixedRight = alWidth + startres - lastres;
1236 else if (!insertGap)
1238 // / Are we able to delete?
1239 // ie are all columns blank?
1241 for (SequenceI gs : groupSeqs)
1243 for (int j = startres; j < lastres; j++)
1245 if (gs.getLength() <= j)
1250 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1252 // Not a gap, block edit not valid
1262 // dragging to the right
1263 if (fixedColumns && fixedRight != -1)
1265 for (int j = lastres; j < startres; j++)
1267 insertChar(j, groupSeqs, fixedRight);
1272 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1273 startres - lastres, av.getAlignment(), true);
1278 // dragging to the left
1279 if (fixedColumns && fixedRight != -1)
1281 for (int j = lastres; j > startres; j--)
1283 deleteChar(startres, groupSeqs, fixedRight);
1288 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1289 lastres - startres, av.getAlignment(), true);
1295 // ///Editing a single sequence///////////
1299 // dragging to the right
1300 if (fixedColumns && fixedRight != -1)
1302 for (int j = lastres; j < startres; j++)
1304 insertChar(j, new SequenceI[] { seq }, fixedRight);
1309 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[] { seq },
1310 lastres, startres - lastres, 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,
1345 { seq }, startres, max, 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.threadRunning = 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()
1441 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1443 List<SequenceFeature> allFeatures = findFeaturesAtColumn(sequence,
1444 sequence.findPosition(column + 1));
1446 Vector<String> links = null;
1447 for (SequenceFeature sf : allFeatures)
1449 if (sf.links != null)
1453 links = new Vector<String>();
1455 links.addAll(sf.links);
1458 APopupMenu popup = new APopupMenu(ap, null, links);
1460 popup.show(this, evt.getX(), evt.getY());
1466 seqCanvas.cursorX = findColumn(evt);
1467 seqCanvas.cursorY = findSeq(evt);
1468 seqCanvas.repaint();
1472 // Only if left mouse button do we want to change group sizes
1474 if (stretchGroup == null)
1476 // define a new group here
1477 SequenceGroup sg = new SequenceGroup();
1478 sg.setStartRes(column);
1479 sg.setEndRes(column);
1480 sg.addSequence(sequence, false);
1481 av.setSelectionGroup(sg);
1484 if (av.getConservationSelected())
1486 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1489 if (av.getAbovePIDThreshold())
1491 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1498 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1500 if (stretchGroup == null)
1504 // always do this - annotation has own state
1505 // but defer colourscheme update until hidden sequences are passed in
1506 boolean vischange = stretchGroup.recalcConservation(true);
1507 // here we rely on stretchGroup == av.getSelection()
1508 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1510 if (stretchGroup.cs != null)
1512 stretchGroup.cs.alignmentChanged(stretchGroup,
1513 av.getHiddenRepSequences());
1515 if (stretchGroup.cs.conservationApplied())
1517 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1518 stretchGroup.getName());
1520 if (stretchGroup.cs.getThreshold() > 0)
1522 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1523 stretchGroup.getName());
1526 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1527 ap.paintAlignment(needOverviewUpdate);
1528 needOverviewUpdate = false;
1529 changeEndRes = false;
1530 changeStartRes = false;
1531 stretchGroup = null;
1535 public void doMouseDraggedDefineMode(MouseEvent evt)
1537 int res = findColumn(evt);
1538 int y = findSeq(evt);
1540 if (wrappedBlock != startWrapBlock)
1545 if (stretchGroup == null)
1550 mouseDragging = true;
1552 if (y > av.getAlignment().getHeight())
1554 y = av.getAlignment().getHeight() - 1;
1557 if (res >= av.getAlignment().getWidth())
1559 res = av.getAlignment().getWidth() - 1;
1562 if (stretchGroup.getEndRes() == res)
1564 // Edit end res position of selected group
1565 changeEndRes = true;
1567 else if (stretchGroup.getStartRes() == res)
1569 // Edit start res position of selected group
1570 changeStartRes = true;
1580 if (res > (stretchGroup.getStartRes() - 1))
1582 stretchGroup.setEndRes(res);
1583 needOverviewUpdate |= av.isSelectionDefinedGroup();
1586 else if (changeStartRes)
1588 if (res < (stretchGroup.getEndRes() + 1))
1590 stretchGroup.setStartRes(res);
1591 needOverviewUpdate |= av.isSelectionDefinedGroup();
1595 int dragDirection = 0;
1601 else if (y < oldSeq)
1606 while ((y != oldSeq) && (oldSeq > -1)
1607 && (y < av.getAlignment().getHeight()))
1609 // This routine ensures we don't skip any sequences, as the
1610 // selection is quite slow.
1611 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1613 oldSeq += dragDirection;
1620 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1622 if (stretchGroup.getSequences(null).contains(nextSeq))
1624 stretchGroup.deleteSequence(seq, false);
1625 needOverviewUpdate |= av.isSelectionDefinedGroup();
1631 stretchGroup.addSequence(seq, false);
1634 stretchGroup.addSequence(nextSeq, false);
1635 needOverviewUpdate |= av.isSelectionDefinedGroup();
1644 if (res > av.getRanges().getEndRes()
1645 || res < av.getRanges().getStartRes()
1646 || y < av.getRanges().getStartSeq()
1647 || y > av.getRanges().getEndSeq())
1652 if ((scrollThread != null) && (scrollThread.isRunning()))
1654 scrollThread.setEvent(evt);
1657 seqCanvas.repaint();
1661 public void mouseEntered(MouseEvent e)
1668 if ((scrollThread != null) && (scrollThread.isRunning()))
1670 scrollThread.stopScrolling();
1671 scrollThread = null;
1676 public void mouseExited(MouseEvent e)
1678 if (av.getWrapAlignment())
1683 if (mouseDragging && scrollThread == null)
1685 scrollThread = new ScrollThread();
1689 void scrollCanvas(MouseEvent evt)
1693 if ((scrollThread != null) && (scrollThread.isRunning()))
1695 scrollThread.stopScrolling();
1696 scrollThread = null;
1698 mouseDragging = false;
1702 if (scrollThread == null)
1704 scrollThread = new ScrollThread();
1707 mouseDragging = true;
1708 scrollThread.setEvent(evt);
1713 // this class allows scrolling off the bottom of the visible alignment
1714 class ScrollThread extends Thread
1718 private volatile boolean threadRunning = true;
1720 public ScrollThread()
1725 public void setEvent(MouseEvent e)
1730 public void stopScrolling()
1732 threadRunning = false;
1735 public boolean isRunning()
1737 return threadRunning;
1743 while (threadRunning)
1749 if (mouseDragging && evt.getY() < 0
1750 && av.getRanges().getStartSeq() > 0)
1752 av.getRanges().scrollUp(true);
1755 if (mouseDragging && evt.getY() >= getSize().height && av
1756 .getAlignment().getHeight() > av.getRanges().getEndSeq())
1758 av.getRanges().scrollUp(false);
1761 if (mouseDragging && evt.getX() < 0)
1763 av.getRanges().scrollRight(false);
1766 else if (mouseDragging && evt.getX() >= getSize().width)
1768 av.getRanges().scrollRight(true);
1775 } catch (Exception ex)
1783 * modify current selection according to a received message.
1786 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1787 HiddenColumns hidden, SelectionSource source)
1789 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1790 // handles selection messages...
1791 // TODO: extend config options to allow user to control if selections may be
1792 // shared between viewports.
1793 if (av != null && (av == source || !av.followSelection
1794 || (source instanceof AlignViewport
1795 && ((AlignmentViewport) source).getSequenceSetId()
1796 .equals(av.getSequenceSetId()))))
1802 * Check for selection in a view of which this one is a dna/protein
1805 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1810 // do we want to thread this ? (contention with seqsel and colsel locks, I
1813 * only copy colsel if there is a real intersection between
1814 * sequence selection and this panel's alignment
1816 boolean repaint = false;
1817 boolean copycolsel = false;
1818 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1820 SequenceGroup sgroup = null;
1821 if (seqsel != null && seqsel.getSize() > 0)
1823 if (av.getAlignment() == null)
1825 System.out.println("Selection message: alignviewport av SeqSetId="
1826 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
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);
1847 if (copycolsel && (av.getColumnSelection() == null
1848 || !av.isColSelChanged(true)))
1850 // the current selection is unset or from a previous message
1851 // so import the new colsel.
1852 if (colsel == null || colsel.isEmpty())
1854 if (av.getColumnSelection() != null)
1856 av.getColumnSelection().clear();
1861 // TODO: shift colSel according to the intersecting sequences
1862 if (av.getColumnSelection() == null)
1864 av.setColumnSelection(new ColumnSelection(colsel));
1868 av.getColumnSelection().setElementsFrom(colsel,
1869 av.getAlignment().getHiddenColumns());
1872 repaint |= av.isColSelChanged(true);
1874 if (copycolsel && av.hasHiddenColumns()
1875 && (av.getColumnSelection() == 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(),
1910 ap.av.getRanges().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,
1927 * If this panel is a cdna/protein translation view of the selection source,
1928 * tries to map the source selection to a local one, and returns true. Else
1935 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1936 ColumnSelection colsel, HiddenColumns hidden,
1937 SelectionSource source)
1939 if (!(source instanceof AlignViewportI))
1943 final AlignViewportI sourceAv = (AlignViewportI) source;
1944 if (sourceAv.getCodingComplement() != av
1945 && av.getCodingComplement() != sourceAv)
1951 * Map sequence selection
1953 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1954 av.setSelectionGroup(sg);
1955 av.isSelectionGroupChanged(true);
1958 * Map column selection
1960 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1962 ColumnSelection cs = new ColumnSelection();
1963 HiddenColumns hs = new HiddenColumns();
1964 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1965 av.setColumnSelection(cs);
1966 av.getAlignment().setHiddenColumns(hs);
1968 ap.scalePanelHolder.repaint();