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.ArrayList;
58 import java.util.Collections;
59 import java.util.List;
60 import java.util.ListIterator;
61 import java.util.Vector;
63 public class SeqPanel extends Panel implements MouseMotionListener,
64 MouseListener, SequenceListener, SelectionListener
67 public SeqCanvas seqCanvas;
69 public AlignmentPanel ap;
71 protected int lastres;
73 protected int startseq;
75 protected AlignViewport av;
77 // if character is inserted or deleted, we will need to recalculate the
79 boolean seqEditOccurred = false;
81 ScrollThread scrollThread = null;
83 boolean mouseDragging = false;
85 boolean editingSeqs = false;
87 boolean groupEditing = false;
91 boolean changeEndSeq = false;
93 boolean changeStartSeq = false;
95 boolean changeEndRes = false;
97 boolean changeStartRes = false;
99 SequenceGroup stretchGroup = null;
101 StringBuffer keyboardNo1;
103 StringBuffer keyboardNo2;
105 boolean mouseWheelPressed = false;
107 Point lastMousePress;
109 EditCommand editCommand;
111 StructureSelectionManager ssm;
113 public SeqPanel(AlignViewport avp, AlignmentPanel p)
117 seqCanvas = new SeqCanvas(avp);
118 setLayout(new BorderLayout());
123 seqCanvas.addMouseMotionListener(this);
124 seqCanvas.addMouseListener(this);
125 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
126 ssm.addStructureViewerListener(this);
127 ssm.addSelectionListener(this);
134 if (editCommand != null && editCommand.getSize() > 0)
136 ap.alignFrame.addHistoryItem(editCommand);
137 av.firePropertyChange("alignment", null, av.getAlignment()
144 groupEditing = false;
152 seqCanvas.cursorY = getKeyboardNo1() - 1;
156 void setCursorColumn()
158 seqCanvas.cursorX = getKeyboardNo1() - 1;
162 void setCursorRowAndColumn()
164 if (keyboardNo2 == null)
166 keyboardNo2 = new StringBuffer();
170 seqCanvas.cursorX = getKeyboardNo1() - 1;
171 seqCanvas.cursorY = getKeyboardNo2() - 1;
176 void setCursorPosition()
178 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
180 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
184 void moveCursor(int dx, int dy)
186 seqCanvas.cursorX += dx;
187 seqCanvas.cursorY += dy;
188 if (av.hasHiddenColumns()
189 && !av.getAlignment().getHiddenColumns()
190 .isVisible(seqCanvas.cursorX))
192 int original = seqCanvas.cursorX - dx;
193 int maxWidth = av.getAlignment().getWidth();
195 while (!av.getAlignment().getHiddenColumns()
196 .isVisible(seqCanvas.cursorX)
197 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
199 seqCanvas.cursorX += dx;
202 if (seqCanvas.cursorX >= maxWidth
203 || !av.getAlignment().getHiddenColumns()
204 .isVisible(seqCanvas.cursorX))
206 seqCanvas.cursorX = original;
212 void scrollToVisible()
214 if (seqCanvas.cursorX < 0)
216 seqCanvas.cursorX = 0;
218 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
220 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
223 if (seqCanvas.cursorY < 0)
225 seqCanvas.cursorY = 0;
227 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
229 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
233 if (av.getWrapAlignment())
235 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
239 ViewportRanges ranges = av.getRanges();
240 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
241 while (seqCanvas.cursorY < ranges.getStartSeq())
243 ranges.scrollUp(true);
245 while (seqCanvas.cursorY > ranges.getEndSeq())
247 ranges.scrollUp(false);
249 while (seqCanvas.cursorX < hidden.adjustForHiddenColumns(ranges
253 if (!ranges.scrollRight(false))
258 while (seqCanvas.cursorX > hidden.adjustForHiddenColumns(ranges
261 if (!ranges.scrollRight(true))
267 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
268 seqCanvas.cursorX, seqCanvas.cursorY);
273 void setSelectionAreaAtCursor(boolean topLeft)
275 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
277 if (av.getSelectionGroup() != null)
279 SequenceGroup sg = av.getSelectionGroup();
280 // Find the top and bottom of this group
281 int min = av.getAlignment().getHeight(), max = 0;
282 for (int i = 0; i < sg.getSize(); i++)
284 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
299 sg.setStartRes(seqCanvas.cursorX);
300 if (sg.getEndRes() < seqCanvas.cursorX)
302 sg.setEndRes(seqCanvas.cursorX);
305 min = seqCanvas.cursorY;
309 sg.setEndRes(seqCanvas.cursorX);
310 if (sg.getStartRes() > seqCanvas.cursorX)
312 sg.setStartRes(seqCanvas.cursorX);
315 max = seqCanvas.cursorY + 1;
320 // Only the user can do this
321 av.setSelectionGroup(null);
325 // Now add any sequences between min and max
327 for (int i = min; i < max; i++)
329 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
334 if (av.getSelectionGroup() == null)
336 SequenceGroup sg = new SequenceGroup();
337 sg.setStartRes(seqCanvas.cursorX);
338 sg.setEndRes(seqCanvas.cursorX);
339 sg.addSequence(sequence, false);
340 av.setSelectionGroup(sg);
342 ap.paintAlignment(false);
346 void insertGapAtCursor(boolean group)
348 groupEditing = group;
349 startseq = seqCanvas.cursorY;
350 lastres = seqCanvas.cursorX;
351 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
355 void deleteGapAtCursor(boolean group)
357 groupEditing = group;
358 startseq = seqCanvas.cursorY;
359 lastres = seqCanvas.cursorX + getKeyboardNo1();
360 editSequence(false, seqCanvas.cursorX);
364 void numberPressed(char value)
366 if (keyboardNo1 == null)
368 keyboardNo1 = new StringBuffer();
371 if (keyboardNo2 != null)
373 keyboardNo2.append(value);
377 keyboardNo1.append(value);
385 if (keyboardNo1 != null)
387 int value = Integer.parseInt(keyboardNo1.toString());
391 } catch (Exception x)
402 if (keyboardNo2 != null)
404 int value = Integer.parseInt(keyboardNo2.toString());
408 } catch (Exception x)
416 * Set status message in alignment panel
419 * aligned sequence object
423 * index of sequence in alignment
424 * @return position of column in sequence or -1 if at gap
426 void setStatusMessage(SequenceI sequence, int column, int seq)
428 // TODO remove duplication of identical gui method
429 StringBuilder text = new StringBuilder(32);
430 String seqno = seq == -1 ? "" : " " + (seq + 1);
431 text.append("Sequence" + seqno + " ID: " + sequence.getName());
433 String residue = null;
435 * Try to translate the display character to residue name (null for gap).
437 final String displayChar = String.valueOf(sequence.getCharAt(column));
438 if (av.getAlignment().isNucleotide())
440 residue = ResidueProperties.nucleotideName.get(displayChar);
443 text.append(" Nucleotide: ").append(residue);
448 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
449 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
453 text.append(" Residue: ").append(residue);
460 pos = sequence.findPosition(column);
461 text.append(" (").append(Integer.toString(pos)).append(")");
464 ap.alignFrame.statusBar.setText(text.toString());
468 * Set the status bar message to highlight the first matched position in
472 * @return true if results were matched, false if not
474 private boolean setStatusMessage(SearchResultsI results)
476 AlignmentI al = this.av.getAlignment();
477 int sequenceIndex = al.findIndex(results);
478 if (sequenceIndex == -1)
482 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
483 for (SearchResultMatchI m : results.getResults())
485 SequenceI seq = m.getSequence();
486 if (seq.getDatasetSequence() != null)
488 seq = seq.getDatasetSequence();
494 * Convert position in sequence (base 1) to sequence character array
497 int start = m.getStart() - m.getSequence().getStart();
498 setStatusMessage(seq, start, sequenceIndex);
506 public void mousePressed(MouseEvent evt)
508 lastMousePress = evt.getPoint();
510 // For now, ignore the mouseWheel font resizing on Macs
511 // As the Button2_mask always seems to be true
512 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
515 mouseWheelPressed = true;
519 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
521 if (evt.isControlDown() || evt.isAltDown())
529 doMousePressedDefineMode(evt);
533 int seq = findSeq(evt);
534 int res = findRes(evt);
536 if (seq < 0 || res < 0)
541 if ((seq < av.getAlignment().getHeight())
542 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
557 public void mouseClicked(MouseEvent evt)
559 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
560 if (evt.getClickCount() > 1)
562 if (av.getSelectionGroup() != null
563 && av.getSelectionGroup().getSize() == 1
564 && av.getSelectionGroup().getEndRes()
565 - av.getSelectionGroup().getStartRes() < 2)
567 av.setSelectionGroup(null);
570 int column = findRes(evt);
571 boolean isGapped = Comparison.isGap(sequence.getCharAt(column));
572 List<SequenceFeature> features = findFeaturesAtRes(sequence,
573 sequence.findPosition(column));
576 removeAdjacentFeatures(features, column + 1, sequence);
579 if (!features.isEmpty())
581 SearchResultsI highlight = new SearchResults();
582 highlight.addResult(sequence, features.get(0).getBegin(), features
584 seqCanvas.highlightSearchResults(highlight);
585 seqCanvas.getFeatureRenderer().amendFeatures(
586 Collections.singletonList(sequence), features, false, ap);
588 seqCanvas.highlightSearchResults(null);
594 public void mouseReleased(MouseEvent evt)
596 boolean didDrag = mouseDragging; // did we come here after a drag
597 mouseDragging = false;
598 mouseWheelPressed = false;
602 doMouseReleasedDefineMode(evt, didDrag);
610 int startWrapBlock = -1;
612 int wrappedBlock = -1;
614 int findRes(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 = findRes(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 = findRes(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 = findFeaturesAtRes(sequence,
875 sequence.findPosition(column));
878 removeAdjacentFeatures(allFeatures, column + 1, sequence);
880 for (SequenceFeature sf : allFeatures)
882 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
884 if (sf.getDescription() != null)
886 tooltipText.append(" " + sf.getDescription());
889 if (sf.getValue("status") != null)
891 String status = sf.getValue("status").toString();
892 if (status.length() > 0)
894 tooltipText.append(" (" + sf.getValue("status") + ")");
897 tooltipText.append("\n");
903 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
907 tooltip.setTip(tooltipText.toString());
912 * Removes from the list of features any that start after, or end before, the
913 * given column position. This allows us to retain only those features
914 * adjacent to a gapped position that straddle the position. Contact features
915 * that 'straddle' the position are also removed, since they are not 'at' the
920 * alignment column (1..)
923 protected void removeAdjacentFeatures(List<SequenceFeature> features,
924 int column, SequenceI sequence)
926 // TODO should this be an AlignViewController method (shared by gui)?
927 ListIterator<SequenceFeature> it = features.listIterator();
930 SequenceFeature sf = it.next();
931 if (sf.isContactFeature()
932 || sequence.findIndex(sf.getBegin()) > column
933 || sequence.findIndex(sf.getEnd()) < column)
940 List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
942 List<SequenceFeature> result = new ArrayList<>();
943 SequenceFeature[] features = sequence.getSequenceFeatures();
944 if (features != null)
946 for (int i = 0; i < features.length; i++)
948 if (av.getFeaturesDisplayed() == null
949 || !av.getFeaturesDisplayed().isVisible(
950 features[i].getType()))
955 if (features[i].featureGroup != null
956 && !seqCanvas.fr.checkGroupVisibility(
957 features[i].featureGroup, false))
962 if ((features[i].getBegin() <= res)
963 && (features[i].getEnd() >= res))
965 result.add(features[i]);
976 * set when the current UI interaction has resulted in a change that requires
977 * overview shading to be recalculated. this could be changed to something
978 * more expressive that indicates what actually has changed, so selective
979 * redraws can be applied
981 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
984 public void mouseDragged(MouseEvent evt)
986 if (mouseWheelPressed)
988 int oldWidth = av.getCharWidth();
990 // Which is bigger, left-right or up-down?
991 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
994 int fontSize = av.font.getSize();
996 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
1000 else if (evt.getY() > lastMousePress.y)
1011 new Font(av.font.getName(), av.font.getStyle(), fontSize),
1013 av.setCharWidth(oldWidth);
1017 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
1019 av.setCharWidth(av.getCharWidth() - 1);
1021 else if (evt.getX() > lastMousePress.x)
1023 av.setCharWidth(av.getCharWidth() + 1);
1026 if (av.getCharWidth() < 1)
1034 FontMetrics fm = getFontMetrics(av.getFont());
1035 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1037 lastMousePress = evt.getPoint();
1039 ap.paintAlignment(false);
1040 ap.annotationPanel.image = null;
1046 doMouseDraggedDefineMode(evt);
1050 int res = findRes(evt);
1057 if ((lastres == -1) || (lastres == res))
1062 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1064 // dragLeft, delete gap
1065 editSequence(false, res);
1069 editSequence(true, res);
1072 mouseDragging = true;
1073 if (scrollThread != null)
1075 scrollThread.setEvent(evt);
1080 synchronized void editSequence(boolean insertGap, int startres)
1083 int fixedRight = -1;
1084 boolean fixedColumns = false;
1085 SequenceGroup sg = av.getSelectionGroup();
1087 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1089 if (!groupEditing && av.hasHiddenRows())
1091 if (av.isHiddenRepSequence(seq))
1093 sg = av.getRepresentedSequences(seq);
1094 groupEditing = true;
1098 StringBuffer message = new StringBuffer();
1101 message.append(MessageManager.getString("action.edit_group")).append(
1103 if (editCommand == null)
1105 editCommand = new EditCommand(
1106 MessageManager.getString("action.edit_group"));
1111 message.append(MessageManager.getString("label.edit_sequence"))
1112 .append(" " + seq.getName());
1113 String label = seq.getName();
1114 if (label.length() > 10)
1116 label = label.substring(0, 10);
1118 if (editCommand == null)
1120 editCommand = new EditCommand(MessageManager.formatMessage(
1121 "label.edit_params", new String[] { label }));
1127 message.append(" insert ");
1131 message.append(" delete ");
1134 message.append(Math.abs(startres - lastres) + " gaps.");
1135 ap.alignFrame.statusBar.setText(message.toString());
1137 // Are we editing within a selection group?
1139 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1142 fixedColumns = true;
1144 // sg might be null as the user may only see 1 sequence,
1145 // but the sequence represents a group
1148 if (!av.isHiddenRepSequence(seq))
1154 sg = av.getRepresentedSequences(seq);
1157 fixedLeft = sg.getStartRes();
1158 fixedRight = sg.getEndRes();
1160 if ((startres < fixedLeft && lastres >= fixedLeft)
1161 || (startres >= fixedLeft && lastres < fixedLeft)
1162 || (startres > fixedRight && lastres <= fixedRight)
1163 || (startres <= fixedRight && lastres > fixedRight))
1169 if (fixedLeft > startres)
1171 fixedRight = fixedLeft - 1;
1174 else if (fixedRight < startres)
1176 fixedLeft = fixedRight;
1181 if (av.hasHiddenColumns())
1183 fixedColumns = true;
1184 int y1 = av.getAlignment().getHiddenColumns()
1185 .getHiddenBoundaryLeft(startres);
1186 int y2 = av.getAlignment().getHiddenColumns()
1187 .getHiddenBoundaryRight(startres);
1189 if ((insertGap && startres > y1 && lastres < y1)
1190 || (!insertGap && startres < y2 && lastres > y2))
1196 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1197 // Selection spans a hidden region
1198 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1206 fixedRight = y2 - 1;
1213 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1214 .toArray(new SequenceI[0]);
1219 // If the user has selected the whole sequence, and is dragging to
1220 // the right, we can still extend the alignment and selectionGroup
1221 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1222 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1224 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1225 fixedRight = sg.getEndRes();
1228 // Is it valid with fixed columns??
1229 // Find the next gap before the end
1230 // of the visible region boundary
1231 boolean blank = false;
1232 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1236 for (SequenceI gs : groupSeqs)
1238 for (int j = 0; j < startres - lastres; j++)
1240 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1256 if (sg.getSize() == av.getAlignment().getHeight())
1258 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1259 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1265 int alWidth = av.getAlignment().getWidth();
1266 if (av.hasHiddenRows())
1268 int hwidth = av.getAlignment().getHiddenSequences()
1270 if (hwidth > alWidth)
1275 // We can still insert gaps if the selectionGroup
1276 // contains all the sequences
1277 sg.setEndRes(sg.getEndRes() + startres - lastres);
1278 fixedRight = alWidth + startres - lastres;
1289 else if (!insertGap)
1291 // / Are we able to delete?
1292 // ie are all columns blank?
1294 for (SequenceI gs : groupSeqs)
1296 for (int j = startres; j < lastres; j++)
1298 if (gs.getLength() <= j)
1303 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1305 // Not a gap, block edit not valid
1315 // dragging to the right
1316 if (fixedColumns && fixedRight != -1)
1318 for (int j = lastres; j < startres; j++)
1320 insertChar(j, groupSeqs, fixedRight);
1325 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1326 startres - lastres, av.getAlignment(), true);
1331 // dragging to the left
1332 if (fixedColumns && fixedRight != -1)
1334 for (int j = lastres; j > startres; j--)
1336 deleteChar(startres, groupSeqs, fixedRight);
1341 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1342 lastres - startres, av.getAlignment(), true);
1348 // ///Editing a single sequence///////////
1352 // dragging to the right
1353 if (fixedColumns && fixedRight != -1)
1355 for (int j = lastres; j < startres; j++)
1357 insertChar(j, new SequenceI[] { seq }, fixedRight);
1362 editCommand.appendEdit(Action.INSERT_GAP,
1363 new SequenceI[] { seq }, lastres, startres - lastres,
1364 av.getAlignment(), true);
1369 // dragging to the left
1370 if (fixedColumns && fixedRight != -1)
1372 for (int j = lastres; j > startres; j--)
1374 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1379 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1384 // could be a keyboard edit trying to delete none gaps
1386 for (int m = startres; m < lastres; m++)
1388 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1397 editCommand.appendEdit(Action.DELETE_GAP,
1398 new SequenceI[] { seq }, startres, max,
1399 av.getAlignment(), true);
1406 seqCanvas.repaint();
1409 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1411 int blankColumn = fixedColumn;
1412 for (int s = 0; s < seq.length; s++)
1414 // Find the next gap before the end of the visible region boundary
1415 // If lastCol > j, theres a boundary after the gap insertion
1417 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1419 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1421 // Theres a space, so break and insert the gap
1426 if (blankColumn <= j)
1428 blankColumn = fixedColumn;
1434 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1435 av.getAlignment(), true);
1437 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1442 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1445 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1448 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1449 av.getAlignment(), true);
1452 // ////////////////////////////////////////
1453 // ///Everything below this is for defining the boundary of the rubberband
1454 // ////////////////////////////////////////
1455 public void doMousePressedDefineMode(MouseEvent evt)
1457 if (scrollThread != null)
1459 scrollThread.running = false;
1460 scrollThread = null;
1463 int res = findRes(evt);
1464 int seq = findSeq(evt);
1466 startWrapBlock = wrappedBlock;
1473 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1475 if (sequence == null || res > sequence.getLength())
1480 stretchGroup = av.getSelectionGroup();
1482 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1484 stretchGroup = av.getAlignment().findGroup(sequence, res);
1485 if (stretchGroup != null)
1487 // only update the current selection if the popup menu has a group to
1489 av.setSelectionGroup(stretchGroup);
1493 // DETECT RIGHT MOUSE BUTTON IN AWT
1494 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1496 List<SequenceFeature> allFeatures = findFeaturesAtRes(sequence,
1497 sequence.findPosition(res));
1499 Vector<String> links = null;
1500 for (SequenceFeature sf : allFeatures)
1502 if (sf.links != null)
1506 links = new Vector<>();
1508 for (int j = 0; j < sf.links.size(); j++)
1510 links.addElement(sf.links.elementAt(j));
1514 APopupMenu popup = new APopupMenu(ap, null, links);
1516 popup.show(this, evt.getX(), evt.getY());
1522 seqCanvas.cursorX = findRes(evt);
1523 seqCanvas.cursorY = findSeq(evt);
1524 seqCanvas.repaint();
1528 // Only if left mouse button do we want to change group sizes
1530 if (stretchGroup == null)
1532 // define a new group here
1533 SequenceGroup sg = new SequenceGroup();
1534 sg.setStartRes(res);
1536 sg.addSequence(sequence, false);
1537 av.setSelectionGroup(sg);
1540 if (av.getConservationSelected())
1542 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1545 if (av.getAbovePIDThreshold())
1547 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1554 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1556 if (stretchGroup == null)
1560 // always do this - annotation has own state
1561 // but defer colourscheme update until hidden sequences are passed in
1562 boolean vischange = stretchGroup.recalcConservation(true);
1563 // here we rely on stretchGroup == av.getSelection()
1564 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1566 if (stretchGroup.cs != null)
1568 stretchGroup.cs.alignmentChanged(stretchGroup,
1569 av.getHiddenRepSequences());
1571 if (stretchGroup.cs.conservationApplied())
1573 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1574 stretchGroup.getName());
1576 if (stretchGroup.cs.getThreshold() > 0)
1578 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1579 stretchGroup.getName());
1582 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1583 ap.paintAlignment(needOverviewUpdate);
1584 needOverviewUpdate = false;
1585 changeEndRes = false;
1586 changeStartRes = false;
1587 stretchGroup = null;
1591 public void doMouseDraggedDefineMode(MouseEvent evt)
1593 int res = findRes(evt);
1594 int y = findSeq(evt);
1596 if (wrappedBlock != startWrapBlock)
1601 if (stretchGroup == null)
1606 mouseDragging = true;
1608 if (y > av.getAlignment().getHeight())
1610 y = av.getAlignment().getHeight() - 1;
1613 if (res >= av.getAlignment().getWidth())
1615 res = av.getAlignment().getWidth() - 1;
1618 if (stretchGroup.getEndRes() == res)
1620 // Edit end res position of selected group
1621 changeEndRes = true;
1623 else if (stretchGroup.getStartRes() == res)
1625 // Edit start res position of selected group
1626 changeStartRes = true;
1636 if (res > (stretchGroup.getStartRes() - 1))
1638 stretchGroup.setEndRes(res);
1639 needOverviewUpdate |= av.isSelectionDefinedGroup();
1642 else if (changeStartRes)
1644 if (res < (stretchGroup.getEndRes() + 1))
1646 stretchGroup.setStartRes(res);
1647 needOverviewUpdate |= av.isSelectionDefinedGroup();
1651 int dragDirection = 0;
1657 else if (y < oldSeq)
1662 while ((y != oldSeq) && (oldSeq > -1)
1663 && (y < av.getAlignment().getHeight()))
1665 // This routine ensures we don't skip any sequences, as the
1666 // selection is quite slow.
1667 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1669 oldSeq += dragDirection;
1676 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1678 if (stretchGroup.getSequences(null).contains(nextSeq))
1680 stretchGroup.deleteSequence(seq, false);
1681 needOverviewUpdate |= av.isSelectionDefinedGroup();
1687 stretchGroup.addSequence(seq, false);
1690 stretchGroup.addSequence(nextSeq, false);
1691 needOverviewUpdate |= av.isSelectionDefinedGroup();
1700 if (res > av.getRanges().getEndRes()
1701 || res < av.getRanges().getStartRes()
1702 || y < av.getRanges().getStartSeq()
1703 || y > av.getRanges().getEndSeq())
1708 if (scrollThread != null)
1710 scrollThread.setEvent(evt);
1713 seqCanvas.repaint();
1717 public void mouseEntered(MouseEvent e)
1724 if (scrollThread != null)
1726 scrollThread.running = false;
1727 scrollThread = null;
1732 public void mouseExited(MouseEvent e)
1734 if (av.getWrapAlignment())
1739 if (mouseDragging && scrollThread == null)
1741 scrollThread = new ScrollThread();
1745 void scrollCanvas(MouseEvent evt)
1749 if (scrollThread != null)
1751 scrollThread.running = false;
1752 scrollThread = null;
1754 mouseDragging = false;
1758 if (scrollThread == null)
1760 scrollThread = new ScrollThread();
1763 mouseDragging = true;
1764 scrollThread.setEvent(evt);
1769 // this class allows scrolling off the bottom of the visible alignment
1770 class ScrollThread extends Thread
1774 boolean running = false;
1776 public ScrollThread()
1781 public void setEvent(MouseEvent e)
1786 public void stopScrolling()
1801 if (mouseDragging && evt.getY() < 0
1802 && av.getRanges().getStartSeq() > 0)
1804 running = av.getRanges().scrollUp(true);
1807 if (mouseDragging && evt.getY() >= getSize().height
1808 && av.getAlignment().getHeight() > av.getRanges()
1811 running = av.getRanges().scrollUp(false);
1814 if (mouseDragging && evt.getX() < 0)
1816 running = av.getRanges().scrollRight(false);
1819 else if (mouseDragging && evt.getX() >= getSize().width)
1821 running = av.getRanges().scrollRight(true);
1828 } catch (Exception ex)
1836 * modify current selection according to a received message.
1839 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1840 HiddenColumns hidden, SelectionSource source)
1842 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1843 // handles selection messages...
1844 // TODO: extend config options to allow user to control if selections may be
1845 // shared between viewports.
1847 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1848 .getSequenceSetId().equals(av.getSequenceSetId()))))
1854 * Check for selection in a view of which this one is a dna/protein
1857 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1862 // do we want to thread this ? (contention with seqsel and colsel locks, I
1865 * only copy colsel if there is a real intersection between
1866 * sequence selection and this panel's alignment
1868 boolean repaint = false;
1869 boolean copycolsel = false;
1870 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1872 SequenceGroup sgroup = null;
1873 if (seqsel != null && seqsel.getSize() > 0)
1875 if (av.getAlignment() == null)
1878 .println("Selection message: alignviewport av SeqSetId="
1879 + av.getSequenceSetId() + " ViewId="
1881 + " 's alignment is NULL! returning immediatly.");
1884 sgroup = seqsel.intersect(av.getAlignment(),
1885 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1886 if ((sgroup != null && sgroup.getSize() > 0))
1891 if (sgroup != null && sgroup.getSize() > 0)
1893 av.setSelectionGroup(sgroup);
1897 av.setSelectionGroup(null);
1899 repaint = av.isSelectionGroupChanged(true);
1902 && (av.getColumnSelection() == null || !av
1903 .isColSelChanged(true)))
1905 // the current selection is unset or from a previous message
1906 // so import the new colsel.
1907 if (colsel == null || colsel.isEmpty())
1909 if (av.getColumnSelection() != null)
1911 av.getColumnSelection().clear();
1916 // TODO: shift colSel according to the intersecting sequences
1917 if (av.getColumnSelection() == null)
1919 av.setColumnSelection(new ColumnSelection(colsel));
1923 av.getColumnSelection().setElementsFrom(colsel,
1924 av.getAlignment().getHiddenColumns());
1927 repaint |= av.isColSelChanged(true);
1930 && av.hasHiddenColumns()
1931 && (av.getColumnSelection() == null))
1933 System.err.println("Bad things");
1937 ap.scalePanelHolder.repaint();
1943 * scroll to the given row/column - or nearest visible location
1948 public void scrollTo(int row, int column)
1951 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1952 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1953 ap.scrollTo(column, column, row, true, true);
1957 * scroll to the given row - or nearest visible location
1961 public void scrollToRow(int row)
1964 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1965 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1966 .getStartRes(), row, true, true);
1970 * scroll to the given column - or nearest visible location
1974 public void scrollToColumn(int column)
1977 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1978 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1982 * If this panel is a cdna/protein translation view of the selection source,
1983 * tries to map the source selection to a local one, and returns true. Else
1990 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1991 ColumnSelection colsel, HiddenColumns hidden,
1992 SelectionSource source)
1994 if (!(source instanceof AlignViewportI))
1998 final AlignViewportI sourceAv = (AlignViewportI) source;
1999 if (sourceAv.getCodingComplement() != av
2000 && av.getCodingComplement() != sourceAv)
2006 * Map sequence selection
2008 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2009 av.setSelectionGroup(sg);
2010 av.isSelectionGroupChanged(true);
2013 * Map column selection
2015 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2017 ColumnSelection cs = new ColumnSelection();
2018 HiddenColumns hs = new HiddenColumns();
2019 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
2020 av.setColumnSelection(cs);
2021 av.getAlignment().setHiddenColumns(hs);
2023 ap.scalePanelHolder.repaint();