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 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 -= seqCanvas.LABEL_WEST;
635 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
641 wrappedBlock = y / cHeight;
642 wrappedBlock += av.getRanges().getStartRes() / cwidth;
644 res = wrappedBlock * cwidth + x / av.getCharWidth();
649 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
652 if (av.hasHiddenColumns())
654 res = av.getAlignment().getHiddenColumns()
655 .adjustForHiddenColumns(res);
662 int findSeq(MouseEvent evt)
664 final int sqnum = findAlRow(evt);
665 return (sqnum < 0) ? 0 : sqnum;
671 * @return row in alignment that was selected (or -1 for column selection)
673 private int findAlRow(MouseEvent evt)
678 if (av.getWrapAlignment())
680 int hgap = av.getCharHeight();
681 if (av.getScaleAboveWrapped())
683 hgap += av.getCharHeight();
686 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
687 + hgap + seqCanvas.getAnnotationHeight();
691 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
700 seq = Math.min((y / av.getCharHeight())
701 + av.getRanges().getStartSeq(),
703 .getAlignment().getHeight() - 1);
713 public void doMousePressed(MouseEvent evt)
716 int seq = findSeq(evt);
717 int res = findRes(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 = findRes(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
836 .get(String.valueOf(ch));
837 text.append(" Residue: ").append(residue == null ? ch : residue);
839 text.append(" (").append(Integer.toString(respos)).append(")");
842 ap.alignFrame.statusBar.setText(text.toString());
844 StringBuilder tooltipText = new StringBuilder();
845 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
848 for (int g = 0; g < groups.length; g++)
850 if (groups[g].getStartRes() <= column && 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 = findFeaturesAtRes(sequence,
873 sequence.findPosition(column));
876 removeAdjacentFeatures(allFeatures, column + 1, sequence);
878 for (SequenceFeature sf : allFeatures)
880 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
882 if (sf.getDescription() != null)
884 tooltipText.append(" " + sf.getDescription());
887 if (sf.getValue("status") != null)
889 String status = sf.getValue("status").toString();
890 if (status.length() > 0)
892 tooltipText.append(" (" + sf.getValue("status") + ")");
895 tooltipText.append("\n");
901 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
905 tooltip.setTip(tooltipText.toString());
910 * Removes from the list of features any that start after, or end before, the
911 * given column position. This allows us to retain only those features
912 * adjacent to a gapped position that straddle the position.
916 * alignment column (1..)
919 protected void removeAdjacentFeatures(List<SequenceFeature> features,
920 int column, SequenceI sequence)
922 // TODO should this be an AlignViewController method (shared by gui)?
923 ListIterator<SequenceFeature> it = features.listIterator();
926 SequenceFeature sf = it.next();
927 if (sequence.findIndex(sf.getBegin()) > column
928 || sequence.findIndex(sf.getEnd()) < column)
935 List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
937 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
938 SequenceFeature[] features = sequence.getSequenceFeatures();
939 if (features != null)
941 for (int i = 0; i < features.length; i++)
943 if (av.getFeaturesDisplayed() == null
944 || !av.getFeaturesDisplayed().isVisible(
945 features[i].getType()))
950 if (features[i].featureGroup != null
951 && !seqCanvas.fr.checkGroupVisibility(
952 features[i].featureGroup, false))
957 if ((features[i].getBegin() <= res)
958 && (features[i].getEnd() >= res))
960 result.add(features[i]);
971 * set when the current UI interaction has resulted in a change that requires
972 * overview shading to be recalculated. this could be changed to something
973 * more expressive that indicates what actually has changed, so selective
974 * redraws can be applied
976 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
979 public void mouseDragged(MouseEvent evt)
981 if (mouseWheelPressed)
983 int oldWidth = av.getCharWidth();
985 // Which is bigger, left-right or up-down?
986 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
989 int fontSize = av.font.getSize();
991 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
995 else if (evt.getY() > lastMousePress.y)
1006 new Font(av.font.getName(), av.font.getStyle(), fontSize),
1008 av.setCharWidth(oldWidth);
1012 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
1014 av.setCharWidth(av.getCharWidth() - 1);
1016 else if (evt.getX() > lastMousePress.x)
1018 av.setCharWidth(av.getCharWidth() + 1);
1021 if (av.getCharWidth() < 1)
1029 FontMetrics fm = getFontMetrics(av.getFont());
1030 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1032 lastMousePress = evt.getPoint();
1034 ap.paintAlignment(false);
1035 ap.annotationPanel.image = null;
1041 doMouseDraggedDefineMode(evt);
1045 int res = findRes(evt);
1052 if ((lastres == -1) || (lastres == res))
1057 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1059 // dragLeft, delete gap
1060 editSequence(false, res);
1064 editSequence(true, res);
1067 mouseDragging = true;
1068 if (scrollThread != null)
1070 scrollThread.setEvent(evt);
1075 synchronized void editSequence(boolean insertGap, int startres)
1078 int fixedRight = -1;
1079 boolean fixedColumns = false;
1080 SequenceGroup sg = av.getSelectionGroup();
1082 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1084 if (!groupEditing && av.hasHiddenRows())
1086 if (av.isHiddenRepSequence(seq))
1088 sg = av.getRepresentedSequences(seq);
1089 groupEditing = true;
1093 StringBuffer message = new StringBuffer();
1096 message.append(MessageManager.getString("action.edit_group")).append(
1098 if (editCommand == null)
1100 editCommand = new EditCommand(
1101 MessageManager.getString("action.edit_group"));
1106 message.append(MessageManager.getString("label.edit_sequence"))
1107 .append(" " + seq.getName());
1108 String label = seq.getName();
1109 if (label.length() > 10)
1111 label = label.substring(0, 10);
1113 if (editCommand == null)
1115 editCommand = new EditCommand(MessageManager.formatMessage(
1116 "label.edit_params", new String[] { label }));
1122 message.append(" insert ");
1126 message.append(" delete ");
1129 message.append(Math.abs(startres - lastres) + " gaps.");
1130 ap.alignFrame.statusBar.setText(message.toString());
1132 // Are we editing within a selection group?
1134 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1137 fixedColumns = true;
1139 // sg might be null as the user may only see 1 sequence,
1140 // but the sequence represents a group
1143 if (!av.isHiddenRepSequence(seq))
1149 sg = av.getRepresentedSequences(seq);
1152 fixedLeft = sg.getStartRes();
1153 fixedRight = sg.getEndRes();
1155 if ((startres < fixedLeft && lastres >= fixedLeft)
1156 || (startres >= fixedLeft && lastres < fixedLeft)
1157 || (startres > fixedRight && lastres <= fixedRight)
1158 || (startres <= fixedRight && lastres > fixedRight))
1164 if (fixedLeft > startres)
1166 fixedRight = fixedLeft - 1;
1169 else if (fixedRight < startres)
1171 fixedLeft = fixedRight;
1176 if (av.hasHiddenColumns())
1178 fixedColumns = true;
1179 int y1 = av.getAlignment().getHiddenColumns()
1180 .getHiddenBoundaryLeft(startres);
1181 int y2 = av.getAlignment().getHiddenColumns()
1182 .getHiddenBoundaryRight(startres);
1184 if ((insertGap && startres > y1 && lastres < y1)
1185 || (!insertGap && startres < y2 && lastres > y2))
1191 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1192 // Selection spans a hidden region
1193 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1201 fixedRight = y2 - 1;
1208 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1209 .toArray(new SequenceI[0]);
1214 // If the user has selected the whole sequence, and is dragging to
1215 // the right, we can still extend the alignment and selectionGroup
1216 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1217 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1219 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1220 fixedRight = sg.getEndRes();
1223 // Is it valid with fixed columns??
1224 // Find the next gap before the end
1225 // of the visible region boundary
1226 boolean blank = false;
1227 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1231 for (SequenceI gs : groupSeqs)
1233 for (int j = 0; j < startres - lastres; j++)
1235 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1251 if (sg.getSize() == av.getAlignment().getHeight())
1253 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1254 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1260 int alWidth = av.getAlignment().getWidth();
1261 if (av.hasHiddenRows())
1263 int hwidth = av.getAlignment().getHiddenSequences()
1265 if (hwidth > alWidth)
1270 // We can still insert gaps if the selectionGroup
1271 // contains all the sequences
1272 sg.setEndRes(sg.getEndRes() + startres - lastres);
1273 fixedRight = alWidth + startres - lastres;
1284 else if (!insertGap)
1286 // / Are we able to delete?
1287 // ie are all columns blank?
1289 for (SequenceI gs : groupSeqs)
1291 for (int j = startres; j < lastres; j++)
1293 if (gs.getLength() <= j)
1298 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1300 // Not a gap, block edit not valid
1310 // dragging to the right
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j < startres; j++)
1315 insertChar(j, groupSeqs, fixedRight);
1320 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1321 startres - lastres, av.getAlignment(), true);
1326 // dragging to the left
1327 if (fixedColumns && fixedRight != -1)
1329 for (int j = lastres; j > startres; j--)
1331 deleteChar(startres, groupSeqs, fixedRight);
1336 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1337 lastres - startres, av.getAlignment(), true);
1343 // ///Editing a single sequence///////////
1347 // dragging to the right
1348 if (fixedColumns && fixedRight != -1)
1350 for (int j = lastres; j < startres; j++)
1352 insertChar(j, new SequenceI[] { seq }, fixedRight);
1357 editCommand.appendEdit(Action.INSERT_GAP,
1358 new SequenceI[] { seq }, lastres, startres - lastres,
1359 av.getAlignment(), true);
1364 // dragging to the left
1365 if (fixedColumns && fixedRight != -1)
1367 for (int j = lastres; j > startres; j--)
1369 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1374 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1379 // could be a keyboard edit trying to delete none gaps
1381 for (int m = startres; m < lastres; m++)
1383 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1392 editCommand.appendEdit(Action.DELETE_GAP,
1393 new SequenceI[] { seq }, startres, max,
1394 av.getAlignment(), true);
1401 seqCanvas.repaint();
1404 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1406 int blankColumn = fixedColumn;
1407 for (int s = 0; s < seq.length; s++)
1409 // Find the next gap before the end of the visible region boundary
1410 // If lastCol > j, theres a boundary after the gap insertion
1412 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1414 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1416 // Theres a space, so break and insert the gap
1421 if (blankColumn <= j)
1423 blankColumn = fixedColumn;
1429 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1430 av.getAlignment(), true);
1432 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1437 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1440 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1443 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1444 av.getAlignment(), true);
1447 // ////////////////////////////////////////
1448 // ///Everything below this is for defining the boundary of the rubberband
1449 // ////////////////////////////////////////
1450 public void doMousePressedDefineMode(MouseEvent evt)
1452 if (scrollThread != null)
1454 scrollThread.running = false;
1455 scrollThread = null;
1458 int res = findRes(evt);
1459 int seq = findSeq(evt);
1461 startWrapBlock = wrappedBlock;
1468 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1470 if (sequence == null || res > sequence.getLength())
1475 stretchGroup = av.getSelectionGroup();
1477 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1479 stretchGroup = av.getAlignment().findGroup(sequence, res);
1480 if (stretchGroup != null)
1482 // only update the current selection if the popup menu has a group to
1484 av.setSelectionGroup(stretchGroup);
1488 // DETECT RIGHT MOUSE BUTTON IN AWT
1489 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1491 List<SequenceFeature> allFeatures = findFeaturesAtRes(sequence,
1492 sequence.findPosition(res));
1494 Vector<String> links = null;
1495 for (SequenceFeature sf : allFeatures)
1497 if (sf.links != null)
1501 links = new Vector<String>();
1503 for (int j = 0; j < sf.links.size(); j++)
1505 links.addElement(sf.links.elementAt(j));
1509 APopupMenu popup = new APopupMenu(ap, null, links);
1511 popup.show(this, evt.getX(), evt.getY());
1517 seqCanvas.cursorX = findRes(evt);
1518 seqCanvas.cursorY = findSeq(evt);
1519 seqCanvas.repaint();
1523 // Only if left mouse button do we want to change group sizes
1525 if (stretchGroup == null)
1527 // define a new group here
1528 SequenceGroup sg = new SequenceGroup();
1529 sg.setStartRes(res);
1531 sg.addSequence(sequence, false);
1532 av.setSelectionGroup(sg);
1535 if (av.getConservationSelected())
1537 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1540 if (av.getAbovePIDThreshold())
1542 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1549 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1551 if (stretchGroup == null)
1555 // always do this - annotation has own state
1556 // but defer colourscheme update until hidden sequences are passed in
1557 boolean vischange = stretchGroup.recalcConservation(true);
1558 // here we rely on stretchGroup == av.getSelection()
1559 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1561 if (stretchGroup.cs != null)
1563 stretchGroup.cs.alignmentChanged(stretchGroup,
1564 av.getHiddenRepSequences());
1566 if (stretchGroup.cs.conservationApplied())
1568 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1569 stretchGroup.getName());
1571 if (stretchGroup.cs.getThreshold() > 0)
1573 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1574 stretchGroup.getName());
1577 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1578 ap.paintAlignment(needOverviewUpdate);
1579 needOverviewUpdate = false;
1580 changeEndRes = false;
1581 changeStartRes = false;
1582 stretchGroup = null;
1586 public void doMouseDraggedDefineMode(MouseEvent evt)
1588 int res = findRes(evt);
1589 int y = findSeq(evt);
1591 if (wrappedBlock != startWrapBlock)
1596 if (stretchGroup == null)
1601 mouseDragging = true;
1603 if (y > av.getAlignment().getHeight())
1605 y = av.getAlignment().getHeight() - 1;
1608 if (res >= av.getAlignment().getWidth())
1610 res = av.getAlignment().getWidth() - 1;
1613 if (stretchGroup.getEndRes() == res)
1615 // Edit end res position of selected group
1616 changeEndRes = true;
1618 else if (stretchGroup.getStartRes() == res)
1620 // Edit start res position of selected group
1621 changeStartRes = true;
1631 if (res > (stretchGroup.getStartRes() - 1))
1633 stretchGroup.setEndRes(res);
1634 needOverviewUpdate |= av.isSelectionDefinedGroup();
1637 else if (changeStartRes)
1639 if (res < (stretchGroup.getEndRes() + 1))
1641 stretchGroup.setStartRes(res);
1642 needOverviewUpdate |= av.isSelectionDefinedGroup();
1646 int dragDirection = 0;
1652 else if (y < oldSeq)
1657 while ((y != oldSeq) && (oldSeq > -1)
1658 && (y < av.getAlignment().getHeight()))
1660 // This routine ensures we don't skip any sequences, as the
1661 // selection is quite slow.
1662 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1664 oldSeq += dragDirection;
1671 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1673 if (stretchGroup.getSequences(null).contains(nextSeq))
1675 stretchGroup.deleteSequence(seq, false);
1676 needOverviewUpdate |= av.isSelectionDefinedGroup();
1682 stretchGroup.addSequence(seq, false);
1685 stretchGroup.addSequence(nextSeq, false);
1686 needOverviewUpdate |= av.isSelectionDefinedGroup();
1695 if (res > av.getRanges().getEndRes()
1696 || res < av.getRanges().getStartRes()
1697 || y < av.getRanges().getStartSeq()
1698 || y > av.getRanges().getEndSeq())
1703 if (scrollThread != null)
1705 scrollThread.setEvent(evt);
1708 seqCanvas.repaint();
1712 public void mouseEntered(MouseEvent e)
1719 if (scrollThread != null)
1721 scrollThread.running = false;
1722 scrollThread = null;
1727 public void mouseExited(MouseEvent e)
1729 if (av.getWrapAlignment())
1734 if (mouseDragging && scrollThread == null)
1736 scrollThread = new ScrollThread();
1740 void scrollCanvas(MouseEvent evt)
1744 if (scrollThread != null)
1746 scrollThread.running = false;
1747 scrollThread = null;
1749 mouseDragging = false;
1753 if (scrollThread == null)
1755 scrollThread = new ScrollThread();
1758 mouseDragging = true;
1759 scrollThread.setEvent(evt);
1764 // this class allows scrolling off the bottom of the visible alignment
1765 class ScrollThread extends Thread
1769 boolean running = false;
1771 public ScrollThread()
1776 public void setEvent(MouseEvent e)
1781 public void stopScrolling()
1796 if (mouseDragging && evt.getY() < 0
1797 && av.getRanges().getStartSeq() > 0)
1799 running = av.getRanges().scrollUp(true);
1802 if (mouseDragging && evt.getY() >= getSize().height
1803 && av.getAlignment().getHeight() > av.getRanges()
1806 running = av.getRanges().scrollUp(false);
1809 if (mouseDragging && evt.getX() < 0)
1811 running = av.getRanges().scrollRight(false);
1814 else if (mouseDragging && evt.getX() >= getSize().width)
1816 running = av.getRanges().scrollRight(true);
1823 } catch (Exception ex)
1831 * modify current selection according to a received message.
1834 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1835 HiddenColumns hidden, SelectionSource source)
1837 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1838 // handles selection messages...
1839 // TODO: extend config options to allow user to control if selections may be
1840 // shared between viewports.
1842 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1843 .getSequenceSetId().equals(av.getSequenceSetId()))))
1849 * Check for selection in a view of which this one is a dna/protein
1852 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1857 // do we want to thread this ? (contention with seqsel and colsel locks, I
1860 * only copy colsel if there is a real intersection between
1861 * sequence selection and this panel's alignment
1863 boolean repaint = false;
1864 boolean copycolsel = false;
1865 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1867 SequenceGroup sgroup = null;
1868 if (seqsel != null && seqsel.getSize() > 0)
1870 if (av.getAlignment() == null)
1873 .println("Selection message: alignviewport av SeqSetId="
1874 + av.getSequenceSetId() + " ViewId="
1876 + " 's alignment is NULL! returning immediatly.");
1879 sgroup = seqsel.intersect(av.getAlignment(),
1880 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1881 if ((sgroup != null && sgroup.getSize() > 0))
1886 if (sgroup != null && sgroup.getSize() > 0)
1888 av.setSelectionGroup(sgroup);
1892 av.setSelectionGroup(null);
1894 repaint = av.isSelectionGroupChanged(true);
1897 && (av.getColumnSelection() == null || !av
1898 .isColSelChanged(true)))
1900 // the current selection is unset or from a previous message
1901 // so import the new colsel.
1902 if (colsel == null || colsel.isEmpty())
1904 if (av.getColumnSelection() != null)
1906 av.getColumnSelection().clear();
1911 // TODO: shift colSel according to the intersecting sequences
1912 if (av.getColumnSelection() == null)
1914 av.setColumnSelection(new ColumnSelection(colsel));
1918 av.getColumnSelection().setElementsFrom(colsel,
1919 av.getAlignment().getHiddenColumns());
1922 repaint |= av.isColSelChanged(true);
1925 && av.hasHiddenColumns()
1926 && (av.getColumnSelection() == null || av.getAlignment()
1927 .getHiddenColumns().getHiddenRegions() == null))
1929 System.err.println("Bad things");
1933 ap.scalePanelHolder.repaint();
1939 * scroll to the given row/column - or nearest visible location
1944 public void scrollTo(int row, int column)
1947 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1948 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1949 ap.scrollTo(column, column, row, true, true);
1953 * scroll to the given row - or nearest visible location
1957 public void scrollToRow(int row)
1960 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1961 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1962 .getStartRes(), row, true, true);
1966 * scroll to the given column - or nearest visible location
1970 public void scrollToColumn(int column)
1973 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1974 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1978 * If this panel is a cdna/protein translation view of the selection source,
1979 * tries to map the source selection to a local one, and returns true. Else
1986 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1987 ColumnSelection colsel, HiddenColumns hidden,
1988 SelectionSource source)
1990 if (!(source instanceof AlignViewportI))
1994 final AlignViewportI sourceAv = (AlignViewportI) source;
1995 if (sourceAv.getCodingComplement() != av
1996 && av.getCodingComplement() != sourceAv)
2002 * Map sequence selection
2004 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2005 av.setSelectionGroup(sg);
2006 av.isSelectionGroupChanged(true);
2009 * Map column selection
2011 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2013 ColumnSelection cs = new ColumnSelection();
2014 HiddenColumns hs = new HiddenColumns();
2015 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
2016 av.setColumnSelection(cs);
2017 av.getAlignment().setHiddenColumns(hs);
2019 ap.scalePanelHolder.repaint();