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.ListIterator;
60 import java.util.Vector;
62 public class SeqPanel extends Panel implements MouseMotionListener,
63 MouseListener, SequenceListener, SelectionListener
66 public SeqCanvas seqCanvas;
68 public AlignmentPanel ap;
70 protected int lastres;
72 protected int startseq;
74 protected AlignViewport av;
76 // if character is inserted or deleted, we will need to recalculate the
78 boolean seqEditOccurred = false;
80 ScrollThread scrollThread = null;
82 boolean mouseDragging = false;
84 boolean editingSeqs = false;
86 boolean groupEditing = false;
90 boolean changeEndSeq = false;
92 boolean changeStartSeq = false;
94 boolean changeEndRes = false;
96 boolean changeStartRes = false;
98 SequenceGroup stretchGroup = null;
100 StringBuffer keyboardNo1;
102 StringBuffer keyboardNo2;
104 boolean mouseWheelPressed = false;
106 Point lastMousePress;
108 EditCommand editCommand;
110 StructureSelectionManager ssm;
112 public SeqPanel(AlignViewport avp, AlignmentPanel p)
116 seqCanvas = new SeqCanvas(avp);
117 setLayout(new BorderLayout());
122 seqCanvas.addMouseMotionListener(this);
123 seqCanvas.addMouseListener(this);
124 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
125 ssm.addStructureViewerListener(this);
126 ssm.addSelectionListener(this);
133 if (editCommand != null && editCommand.getSize() > 0)
135 ap.alignFrame.addHistoryItem(editCommand);
136 av.firePropertyChange("alignment", null, av.getAlignment()
143 groupEditing = false;
151 seqCanvas.cursorY = getKeyboardNo1() - 1;
155 void setCursorColumn()
157 seqCanvas.cursorX = getKeyboardNo1() - 1;
161 void setCursorRowAndColumn()
163 if (keyboardNo2 == null)
165 keyboardNo2 = new StringBuffer();
169 seqCanvas.cursorX = getKeyboardNo1() - 1;
170 seqCanvas.cursorY = getKeyboardNo2() - 1;
175 void setCursorPosition()
177 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
179 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
183 void moveCursor(int dx, int dy)
185 seqCanvas.cursorX += dx;
186 seqCanvas.cursorY += dy;
187 if (av.hasHiddenColumns()
188 && !av.getAlignment().getHiddenColumns()
189 .isVisible(seqCanvas.cursorX))
191 int original = seqCanvas.cursorX - dx;
192 int maxWidth = av.getAlignment().getWidth();
194 while (!av.getAlignment().getHiddenColumns()
195 .isVisible(seqCanvas.cursorX)
196 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
198 seqCanvas.cursorX += dx;
201 if (seqCanvas.cursorX >= maxWidth
202 || !av.getAlignment().getHiddenColumns()
203 .isVisible(seqCanvas.cursorX))
205 seqCanvas.cursorX = original;
211 void scrollToVisible()
213 if (seqCanvas.cursorX < 0)
215 seqCanvas.cursorX = 0;
217 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
219 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
222 if (seqCanvas.cursorY < 0)
224 seqCanvas.cursorY = 0;
226 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
228 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
232 if (av.getWrapAlignment())
234 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
238 ViewportRanges ranges = av.getRanges();
239 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
240 while (seqCanvas.cursorY < ranges.getStartSeq())
242 ranges.scrollUp(true);
244 while (seqCanvas.cursorY > ranges.getEndSeq())
246 ranges.scrollUp(false);
248 while (seqCanvas.cursorX < hidden.adjustForHiddenColumns(ranges
252 if (!ranges.scrollRight(false))
257 while (seqCanvas.cursorX > hidden.adjustForHiddenColumns(ranges
260 if (!ranges.scrollRight(true))
266 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
267 seqCanvas.cursorX, seqCanvas.cursorY);
272 void setSelectionAreaAtCursor(boolean topLeft)
274 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
276 if (av.getSelectionGroup() != null)
278 SequenceGroup sg = av.getSelectionGroup();
279 // Find the top and bottom of this group
280 int min = av.getAlignment().getHeight(), max = 0;
281 for (int i = 0; i < sg.getSize(); i++)
283 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
298 sg.setStartRes(seqCanvas.cursorX);
299 if (sg.getEndRes() < seqCanvas.cursorX)
301 sg.setEndRes(seqCanvas.cursorX);
304 min = seqCanvas.cursorY;
308 sg.setEndRes(seqCanvas.cursorX);
309 if (sg.getStartRes() > seqCanvas.cursorX)
311 sg.setStartRes(seqCanvas.cursorX);
314 max = seqCanvas.cursorY + 1;
319 // Only the user can do this
320 av.setSelectionGroup(null);
324 // Now add any sequences between min and max
326 for (int i = min; i < max; i++)
328 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
333 if (av.getSelectionGroup() == null)
335 SequenceGroup sg = new SequenceGroup();
336 sg.setStartRes(seqCanvas.cursorX);
337 sg.setEndRes(seqCanvas.cursorX);
338 sg.addSequence(sequence, false);
339 av.setSelectionGroup(sg);
341 ap.paintAlignment(false);
345 void insertGapAtCursor(boolean group)
347 groupEditing = group;
348 startseq = seqCanvas.cursorY;
349 lastres = seqCanvas.cursorX;
350 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
354 void deleteGapAtCursor(boolean group)
356 groupEditing = group;
357 startseq = seqCanvas.cursorY;
358 lastres = seqCanvas.cursorX + getKeyboardNo1();
359 editSequence(false, seqCanvas.cursorX);
363 void numberPressed(char value)
365 if (keyboardNo1 == null)
367 keyboardNo1 = new StringBuffer();
370 if (keyboardNo2 != null)
372 keyboardNo2.append(value);
376 keyboardNo1.append(value);
384 if (keyboardNo1 != null)
386 int value = Integer.parseInt(keyboardNo1.toString());
390 } catch (Exception x)
401 if (keyboardNo2 != null)
403 int value = Integer.parseInt(keyboardNo2.toString());
407 } catch (Exception x)
415 * Set status message in alignment panel
418 * aligned sequence object
422 * index of sequence in alignment
423 * @return position of column in sequence or -1 if at gap
425 void setStatusMessage(SequenceI sequence, int column, int seq)
427 // TODO remove duplication of identical gui method
428 StringBuilder text = new StringBuilder(32);
429 String seqno = seq == -1 ? "" : " " + (seq + 1);
430 text.append("Sequence" + seqno + " ID: " + sequence.getName());
432 String residue = null;
434 * Try to translate the display character to residue name (null for gap).
436 final String displayChar = String.valueOf(sequence.getCharAt(column));
437 if (av.getAlignment().isNucleotide())
439 residue = ResidueProperties.nucleotideName.get(displayChar);
442 text.append(" Nucleotide: ").append(residue);
447 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
448 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
452 text.append(" Residue: ").append(residue);
459 pos = sequence.findPosition(column);
460 text.append(" (").append(Integer.toString(pos)).append(")");
463 ap.alignFrame.statusBar.setText(text.toString());
467 * Set the status bar message to highlight the first matched position in
471 * @return true if results were matched, false if not
473 private boolean setStatusMessage(SearchResultsI results)
475 AlignmentI al = this.av.getAlignment();
476 int sequenceIndex = al.findIndex(results);
477 if (sequenceIndex == -1)
481 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
482 for (SearchResultMatchI m : results.getResults())
484 SequenceI seq = m.getSequence();
485 if (seq.getDatasetSequence() != null)
487 seq = seq.getDatasetSequence();
493 * Convert position in sequence (base 1) to sequence character array
496 int start = m.getStart() - m.getSequence().getStart();
497 setStatusMessage(seq, start, sequenceIndex);
505 public void mousePressed(MouseEvent evt)
507 lastMousePress = evt.getPoint();
509 // For now, ignore the mouseWheel font resizing on Macs
510 // As the Button2_mask always seems to be true
511 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
514 mouseWheelPressed = true;
518 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
520 if (evt.isControlDown() || evt.isAltDown())
528 doMousePressedDefineMode(evt);
532 int seq = findSeq(evt);
533 int res = findRes(evt);
535 if (seq < 0 || res < 0)
540 if ((seq < av.getAlignment().getHeight())
541 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
556 public void mouseClicked(MouseEvent evt)
558 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
559 if (evt.getClickCount() > 1)
561 if (av.getSelectionGroup() != null
562 && av.getSelectionGroup().getSize() == 1
563 && av.getSelectionGroup().getEndRes()
564 - av.getSelectionGroup().getStartRes() < 2)
566 av.setSelectionGroup(null);
569 int column = findRes(evt);
570 boolean isGapped = Comparison.isGap(sequence.getCharAt(column));
571 List<SequenceFeature> features = findFeaturesAtRes(sequence,
572 sequence.findPosition(column));
575 removeAdjacentFeatures(features, column + 1, sequence);
578 if (!features.isEmpty())
580 SearchResultsI highlight = new SearchResults();
581 highlight.addResult(sequence, features.get(0).getBegin(), features
583 seqCanvas.highlightSearchResults(highlight);
584 seqCanvas.getFeatureRenderer().amendFeatures(
585 Collections.singletonList(sequence), features, false, ap);
586 seqCanvas.highlightSearchResults(null);
592 public void mouseReleased(MouseEvent evt)
594 boolean didDrag = mouseDragging; // did we come here after a drag
595 mouseDragging = false;
596 mouseWheelPressed = false;
600 doMouseReleasedDefineMode(evt, didDrag);
608 int startWrapBlock = -1;
610 int wrappedBlock = -1;
612 int findRes(MouseEvent evt)
617 if (av.getWrapAlignment())
620 int hgap = av.getCharHeight();
621 if (av.getScaleAboveWrapped())
623 hgap += av.getCharHeight();
626 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
627 + hgap + seqCanvas.getAnnotationHeight();
631 x -= seqCanvas.LABEL_WEST;
633 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
639 wrappedBlock = y / cHeight;
640 wrappedBlock += av.getRanges().getStartRes() / cwidth;
642 res = wrappedBlock * cwidth + x / av.getCharWidth();
647 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
650 if (av.hasHiddenColumns())
652 res = av.getAlignment().getHiddenColumns()
653 .adjustForHiddenColumns(res);
660 int findSeq(MouseEvent evt)
662 final int sqnum = findAlRow(evt);
663 return (sqnum < 0) ? 0 : sqnum;
669 * @return row in alignment that was selected (or -1 for column selection)
671 private int findAlRow(MouseEvent evt)
676 if (av.getWrapAlignment())
678 int hgap = av.getCharHeight();
679 if (av.getScaleAboveWrapped())
681 hgap += av.getCharHeight();
684 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
685 + hgap + seqCanvas.getAnnotationHeight();
689 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
698 seq = Math.min((y / av.getCharHeight())
699 + av.getRanges().getStartSeq(),
701 .getAlignment().getHeight() - 1);
711 public void doMousePressed(MouseEvent evt)
714 int seq = findSeq(evt);
715 int res = findRes(evt);
717 if (seq < av.getAlignment().getHeight()
718 && res < av.getAlignment().getSequenceAt(seq).getLength())
720 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
721 // Find the residue's position in the sequence (res is the position
739 public void mouseOverSequence(SequenceI sequence, int index, int pos)
741 String tmp = sequence.hashCode() + index + "";
742 if (lastMessage == null || !lastMessage.equals(tmp))
744 ssm.mouseOverSequence(sequence, index, pos, av);
751 public void highlightSequence(SearchResultsI results)
753 if (av.isFollowHighlight())
755 // don't allow highlight of protein/cDNA to also scroll a complementary
756 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
757 // over residue to change abruptly, causing highlighted residue in panel 2
758 // to change, causing a scroll in panel 1 etc)
759 ap.setToScrollComplementPanel(false);
760 if (ap.scrollToPosition(results, true))
762 ap.alignFrame.repaint();
764 ap.setToScrollComplementPanel(true);
766 setStatusMessage(results);
767 seqCanvas.highlightSearchResults(results);
772 public VamsasSource getVamsasSource()
774 return this.ap == null ? null : this.ap.av;
778 public void updateColours(SequenceI seq, int index)
780 System.out.println("update the seqPanel colours");
785 public void mouseMoved(MouseEvent evt)
787 final int column = findRes(evt);
788 int seq = findSeq(evt);
790 if (seq >= av.getAlignment().getHeight() || seq < 0 || column < 0)
799 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
800 if (column > sequence.getLength())
809 final char ch = sequence.getCharAt(column);
810 boolean isGapped = Comparison.isGap(ch);
811 // find residue at column (or nearest if at a gap)
812 int respos = sequence.findPosition(column);
814 if (ssm != null && !isGapped)
816 mouseOverSequence(sequence, column, respos);
819 StringBuilder text = new StringBuilder();
820 text.append("Sequence ").append(Integer.toString(seq + 1))
821 .append(" ID: ").append(sequence.getName());
825 if (av.getAlignment().isNucleotide())
827 String base = ResidueProperties.nucleotideName.get(ch);
828 text.append(" Nucleotide: ").append(base == null ? ch : base);
832 String residue = (ch == 'x' || ch == 'X') ? "X"
833 : ResidueProperties.aa2Triplet
834 .get(String.valueOf(ch));
835 text.append(" Residue: ").append(residue == null ? ch : residue);
837 text.append(" (").append(Integer.toString(respos)).append(")");
840 ap.alignFrame.statusBar.setText(text.toString());
842 StringBuilder tooltipText = new StringBuilder();
843 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
846 for (int g = 0; g < groups.length; g++)
848 if (groups[g].getStartRes() <= column && groups[g].getEndRes() >= column)
850 if (!groups[g].getName().startsWith("JTreeGroup")
851 && !groups[g].getName().startsWith("JGroup"))
853 tooltipText.append(groups[g].getName()).append(" ");
855 if (groups[g].getDescription() != null)
857 tooltipText.append(groups[g].getDescription());
859 tooltipText.append("\n");
865 * add feature details to tooltip, including any that straddle
868 if (av.isShowSequenceFeatures())
870 List<SequenceFeature> allFeatures = findFeaturesAtRes(sequence,
874 removeAdjacentFeatures(allFeatures, column + 1, sequence);
876 for (SequenceFeature sf : allFeatures)
878 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
880 if (sf.getDescription() != null)
882 tooltipText.append(" " + sf.getDescription());
885 if (sf.getValue("status") != null)
887 String status = sf.getValue("status").toString();
888 if (status.length() > 0)
890 tooltipText.append(" (" + sf.getValue("status") + ")");
893 tooltipText.append("\n");
899 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
903 tooltip.setTip(tooltipText.toString());
907 List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
909 return seqCanvas.getFeatureRenderer().findFeaturesAtRes(sequence, res);
913 * Removes from the list of features any that start after, or end before, the
914 * given column position. This allows us to retain only those features
915 * adjacent to a gapped position that straddle the position. Contact features
916 * that 'straddle' the position are also removed, since they are not 'at' the
921 * alignment column (1..)
924 protected void removeAdjacentFeatures(List<SequenceFeature> features,
925 int column, SequenceI sequence)
927 // TODO should this be an AlignViewController method (shared by gui)?
928 ListIterator<SequenceFeature> it = features.listIterator();
931 SequenceFeature sf = it.next();
932 if (sf.isContactFeature()
933 || sequence.findIndex(sf.getBegin()) > column
934 || sequence.findIndex(sf.getEnd()) < column)
944 * set when the current UI interaction has resulted in a change that requires
945 * overview shading to be recalculated. this could be changed to something
946 * more expressive that indicates what actually has changed, so selective
947 * redraws can be applied
949 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
952 public void mouseDragged(MouseEvent evt)
954 if (mouseWheelPressed)
956 int oldWidth = av.getCharWidth();
958 // Which is bigger, left-right or up-down?
959 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
962 int fontSize = av.font.getSize();
964 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
968 else if (evt.getY() > lastMousePress.y)
979 new Font(av.font.getName(), av.font.getStyle(), fontSize),
981 av.setCharWidth(oldWidth);
985 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
987 av.setCharWidth(av.getCharWidth() - 1);
989 else if (evt.getX() > lastMousePress.x)
991 av.setCharWidth(av.getCharWidth() + 1);
994 if (av.getCharWidth() < 1)
1002 FontMetrics fm = getFontMetrics(av.getFont());
1003 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1005 lastMousePress = evt.getPoint();
1007 ap.paintAlignment(false);
1008 ap.annotationPanel.image = null;
1014 doMouseDraggedDefineMode(evt);
1018 int res = findRes(evt);
1025 if ((lastres == -1) || (lastres == res))
1030 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1032 // dragLeft, delete gap
1033 editSequence(false, res);
1037 editSequence(true, res);
1040 mouseDragging = true;
1041 if (scrollThread != null)
1043 scrollThread.setEvent(evt);
1048 synchronized void editSequence(boolean insertGap, int startres)
1051 int fixedRight = -1;
1052 boolean fixedColumns = false;
1053 SequenceGroup sg = av.getSelectionGroup();
1055 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1057 if (!groupEditing && av.hasHiddenRows())
1059 if (av.isHiddenRepSequence(seq))
1061 sg = av.getRepresentedSequences(seq);
1062 groupEditing = true;
1066 StringBuffer message = new StringBuffer();
1069 message.append(MessageManager.getString("action.edit_group")).append(
1071 if (editCommand == null)
1073 editCommand = new EditCommand(
1074 MessageManager.getString("action.edit_group"));
1079 message.append(MessageManager.getString("label.edit_sequence"))
1080 .append(" " + seq.getName());
1081 String label = seq.getName();
1082 if (label.length() > 10)
1084 label = label.substring(0, 10);
1086 if (editCommand == null)
1088 editCommand = new EditCommand(MessageManager.formatMessage(
1089 "label.edit_params", new String[] { label }));
1095 message.append(" insert ");
1099 message.append(" delete ");
1102 message.append(Math.abs(startres - lastres) + " gaps.");
1103 ap.alignFrame.statusBar.setText(message.toString());
1105 // Are we editing within a selection group?
1107 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1110 fixedColumns = true;
1112 // sg might be null as the user may only see 1 sequence,
1113 // but the sequence represents a group
1116 if (!av.isHiddenRepSequence(seq))
1122 sg = av.getRepresentedSequences(seq);
1125 fixedLeft = sg.getStartRes();
1126 fixedRight = sg.getEndRes();
1128 if ((startres < fixedLeft && lastres >= fixedLeft)
1129 || (startres >= fixedLeft && lastres < fixedLeft)
1130 || (startres > fixedRight && lastres <= fixedRight)
1131 || (startres <= fixedRight && lastres > fixedRight))
1137 if (fixedLeft > startres)
1139 fixedRight = fixedLeft - 1;
1142 else if (fixedRight < startres)
1144 fixedLeft = fixedRight;
1149 if (av.hasHiddenColumns())
1151 fixedColumns = true;
1152 int y1 = av.getAlignment().getHiddenColumns()
1153 .getHiddenBoundaryLeft(startres);
1154 int y2 = av.getAlignment().getHiddenColumns()
1155 .getHiddenBoundaryRight(startres);
1157 if ((insertGap && startres > y1 && lastres < y1)
1158 || (!insertGap && startres < y2 && lastres > y2))
1164 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1165 // Selection spans a hidden region
1166 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1174 fixedRight = y2 - 1;
1181 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1182 .toArray(new SequenceI[0]);
1187 // If the user has selected the whole sequence, and is dragging to
1188 // the right, we can still extend the alignment and selectionGroup
1189 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1190 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1192 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1193 fixedRight = sg.getEndRes();
1196 // Is it valid with fixed columns??
1197 // Find the next gap before the end
1198 // of the visible region boundary
1199 boolean blank = false;
1200 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1204 for (SequenceI gs : groupSeqs)
1206 for (int j = 0; j < startres - lastres; j++)
1208 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1224 if (sg.getSize() == av.getAlignment().getHeight())
1226 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1227 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1233 int alWidth = av.getAlignment().getWidth();
1234 if (av.hasHiddenRows())
1236 int hwidth = av.getAlignment().getHiddenSequences()
1238 if (hwidth > alWidth)
1243 // We can still insert gaps if the selectionGroup
1244 // contains all the sequences
1245 sg.setEndRes(sg.getEndRes() + startres - lastres);
1246 fixedRight = alWidth + startres - lastres;
1257 else if (!insertGap)
1259 // / Are we able to delete?
1260 // ie are all columns blank?
1262 for (SequenceI gs : groupSeqs)
1264 for (int j = startres; j < lastres; j++)
1266 if (gs.getLength() <= j)
1271 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1273 // Not a gap, block edit not valid
1283 // dragging to the right
1284 if (fixedColumns && fixedRight != -1)
1286 for (int j = lastres; j < startres; j++)
1288 insertChar(j, groupSeqs, fixedRight);
1293 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1294 startres - lastres, av.getAlignment(), true);
1299 // dragging to the left
1300 if (fixedColumns && fixedRight != -1)
1302 for (int j = lastres; j > startres; j--)
1304 deleteChar(startres, groupSeqs, fixedRight);
1309 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1310 lastres - startres, av.getAlignment(), true);
1316 // ///Editing a single sequence///////////
1320 // dragging to the right
1321 if (fixedColumns && fixedRight != -1)
1323 for (int j = lastres; j < startres; j++)
1325 insertChar(j, new SequenceI[] { seq }, fixedRight);
1330 editCommand.appendEdit(Action.INSERT_GAP,
1331 new SequenceI[] { seq }, lastres, startres - lastres,
1332 av.getAlignment(), true);
1337 // dragging to the left
1338 if (fixedColumns && fixedRight != -1)
1340 for (int j = lastres; j > startres; j--)
1342 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1347 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1352 // could be a keyboard edit trying to delete none gaps
1354 for (int m = startres; m < lastres; m++)
1356 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1365 editCommand.appendEdit(Action.DELETE_GAP,
1366 new SequenceI[] { seq }, startres, max,
1367 av.getAlignment(), true);
1374 seqCanvas.repaint();
1377 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1379 int blankColumn = fixedColumn;
1380 for (int s = 0; s < seq.length; s++)
1382 // Find the next gap before the end of the visible region boundary
1383 // If lastCol > j, theres a boundary after the gap insertion
1385 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1387 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1389 // Theres a space, so break and insert the gap
1394 if (blankColumn <= j)
1396 blankColumn = fixedColumn;
1402 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1403 av.getAlignment(), true);
1405 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1410 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1413 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1416 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1417 av.getAlignment(), true);
1420 // ////////////////////////////////////////
1421 // ///Everything below this is for defining the boundary of the rubberband
1422 // ////////////////////////////////////////
1423 public void doMousePressedDefineMode(MouseEvent evt)
1425 if (scrollThread != null)
1427 scrollThread.running = false;
1428 scrollThread = null;
1431 int res = findRes(evt);
1432 int seq = findSeq(evt);
1434 startWrapBlock = wrappedBlock;
1441 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1443 if (sequence == null || res > sequence.getLength())
1448 stretchGroup = av.getSelectionGroup();
1450 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1452 stretchGroup = av.getAlignment().findGroup(sequence, res);
1453 if (stretchGroup != null)
1455 // only update the current selection if the popup menu has a group to
1457 av.setSelectionGroup(stretchGroup);
1461 // DETECT RIGHT MOUSE BUTTON IN AWT
1462 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1464 List<SequenceFeature> allFeatures = findFeaturesAtRes(sequence,
1465 sequence.findPosition(res));
1467 Vector<String> links = null;
1468 for (SequenceFeature sf : allFeatures)
1470 if (sf.links != null)
1474 links = new Vector<String>();
1476 links.addAll(sf.links);
1479 APopupMenu popup = new APopupMenu(ap, null, links);
1481 popup.show(this, evt.getX(), evt.getY());
1487 seqCanvas.cursorX = findRes(evt);
1488 seqCanvas.cursorY = findSeq(evt);
1489 seqCanvas.repaint();
1493 // Only if left mouse button do we want to change group sizes
1495 if (stretchGroup == null)
1497 // define a new group here
1498 SequenceGroup sg = new SequenceGroup();
1499 sg.setStartRes(res);
1501 sg.addSequence(sequence, false);
1502 av.setSelectionGroup(sg);
1505 if (av.getConservationSelected())
1507 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1510 if (av.getAbovePIDThreshold())
1512 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1519 public void doMouseReleasedDefineMode(MouseEvent evt, boolean afterDrag)
1521 if (stretchGroup == null)
1525 // always do this - annotation has own state
1526 // but defer colourscheme update until hidden sequences are passed in
1527 boolean vischange = stretchGroup.recalcConservation(true);
1528 // here we rely on stretchGroup == av.getSelection()
1529 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup()
1531 if (stretchGroup.cs != null)
1533 stretchGroup.cs.alignmentChanged(stretchGroup,
1534 av.getHiddenRepSequences());
1536 if (stretchGroup.cs.conservationApplied())
1538 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1539 stretchGroup.getName());
1541 if (stretchGroup.cs.getThreshold() > 0)
1543 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1544 stretchGroup.getName());
1547 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1548 ap.paintAlignment(needOverviewUpdate);
1549 needOverviewUpdate = false;
1550 changeEndRes = false;
1551 changeStartRes = false;
1552 stretchGroup = null;
1556 public void doMouseDraggedDefineMode(MouseEvent evt)
1558 int res = findRes(evt);
1559 int y = findSeq(evt);
1561 if (wrappedBlock != startWrapBlock)
1566 if (stretchGroup == null)
1571 mouseDragging = true;
1573 if (y > av.getAlignment().getHeight())
1575 y = av.getAlignment().getHeight() - 1;
1578 if (res >= av.getAlignment().getWidth())
1580 res = av.getAlignment().getWidth() - 1;
1583 if (stretchGroup.getEndRes() == res)
1585 // Edit end res position of selected group
1586 changeEndRes = true;
1588 else if (stretchGroup.getStartRes() == res)
1590 // Edit start res position of selected group
1591 changeStartRes = true;
1601 if (res > (stretchGroup.getStartRes() - 1))
1603 stretchGroup.setEndRes(res);
1604 needOverviewUpdate |= av.isSelectionDefinedGroup();
1607 else if (changeStartRes)
1609 if (res < (stretchGroup.getEndRes() + 1))
1611 stretchGroup.setStartRes(res);
1612 needOverviewUpdate |= av.isSelectionDefinedGroup();
1616 int dragDirection = 0;
1622 else if (y < oldSeq)
1627 while ((y != oldSeq) && (oldSeq > -1)
1628 && (y < av.getAlignment().getHeight()))
1630 // This routine ensures we don't skip any sequences, as the
1631 // selection is quite slow.
1632 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1634 oldSeq += dragDirection;
1641 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1643 if (stretchGroup.getSequences(null).contains(nextSeq))
1645 stretchGroup.deleteSequence(seq, false);
1646 needOverviewUpdate |= av.isSelectionDefinedGroup();
1652 stretchGroup.addSequence(seq, false);
1655 stretchGroup.addSequence(nextSeq, false);
1656 needOverviewUpdate |= av.isSelectionDefinedGroup();
1665 if (res > av.getRanges().getEndRes()
1666 || res < av.getRanges().getStartRes()
1667 || y < av.getRanges().getStartSeq()
1668 || y > av.getRanges().getEndSeq())
1673 if (scrollThread != null)
1675 scrollThread.setEvent(evt);
1678 seqCanvas.repaint();
1682 public void mouseEntered(MouseEvent e)
1689 if (scrollThread != null)
1691 scrollThread.running = false;
1692 scrollThread = null;
1697 public void mouseExited(MouseEvent e)
1699 if (av.getWrapAlignment())
1704 if (mouseDragging && scrollThread == null)
1706 scrollThread = new ScrollThread();
1710 void scrollCanvas(MouseEvent evt)
1714 if (scrollThread != null)
1716 scrollThread.running = false;
1717 scrollThread = null;
1719 mouseDragging = false;
1723 if (scrollThread == null)
1725 scrollThread = new ScrollThread();
1728 mouseDragging = true;
1729 scrollThread.setEvent(evt);
1734 // this class allows scrolling off the bottom of the visible alignment
1735 class ScrollThread extends Thread
1739 boolean running = false;
1741 public ScrollThread()
1746 public void setEvent(MouseEvent e)
1751 public void stopScrolling()
1766 if (mouseDragging && evt.getY() < 0
1767 && av.getRanges().getStartSeq() > 0)
1769 running = av.getRanges().scrollUp(true);
1772 if (mouseDragging && evt.getY() >= getSize().height
1773 && av.getAlignment().getHeight() > av.getRanges()
1776 running = av.getRanges().scrollUp(false);
1779 if (mouseDragging && evt.getX() < 0)
1781 running = av.getRanges().scrollRight(false);
1784 else if (mouseDragging && evt.getX() >= getSize().width)
1786 running = av.getRanges().scrollRight(true);
1793 } catch (Exception ex)
1801 * modify current selection according to a received message.
1804 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1805 HiddenColumns hidden, SelectionSource source)
1807 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1808 // handles selection messages...
1809 // TODO: extend config options to allow user to control if selections may be
1810 // shared between viewports.
1812 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1813 .getSequenceSetId().equals(av.getSequenceSetId()))))
1819 * Check for selection in a view of which this one is a dna/protein
1822 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1827 // do we want to thread this ? (contention with seqsel and colsel locks, I
1830 * only copy colsel if there is a real intersection between
1831 * sequence selection and this panel's alignment
1833 boolean repaint = false;
1834 boolean copycolsel = false;
1835 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1837 SequenceGroup sgroup = null;
1838 if (seqsel != null && seqsel.getSize() > 0)
1840 if (av.getAlignment() == null)
1843 .println("Selection message: alignviewport av SeqSetId="
1844 + av.getSequenceSetId() + " ViewId="
1846 + " 's alignment is NULL! returning immediatly.");
1849 sgroup = seqsel.intersect(av.getAlignment(),
1850 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1851 if ((sgroup != null && sgroup.getSize() > 0))
1856 if (sgroup != null && sgroup.getSize() > 0)
1858 av.setSelectionGroup(sgroup);
1862 av.setSelectionGroup(null);
1864 repaint = av.isSelectionGroupChanged(true);
1867 && (av.getColumnSelection() == null || !av
1868 .isColSelChanged(true)))
1870 // the current selection is unset or from a previous message
1871 // so import the new colsel.
1872 if (colsel == null || colsel.isEmpty())
1874 if (av.getColumnSelection() != null)
1876 av.getColumnSelection().clear();
1881 // TODO: shift colSel according to the intersecting sequences
1882 if (av.getColumnSelection() == null)
1884 av.setColumnSelection(new ColumnSelection(colsel));
1888 av.getColumnSelection().setElementsFrom(colsel,
1889 av.getAlignment().getHiddenColumns());
1892 repaint |= av.isColSelChanged(true);
1895 && av.hasHiddenColumns()
1896 && (av.getColumnSelection() == null || av.getAlignment()
1897 .getHiddenColumns().getHiddenRegions() == null))
1899 System.err.println("Bad things");
1903 ap.scalePanelHolder.repaint();
1909 * scroll to the given row/column - or nearest visible location
1914 public void scrollTo(int row, int column)
1917 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1918 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1919 ap.scrollTo(column, column, row, true, true);
1923 * scroll to the given row - or nearest visible location
1927 public void scrollToRow(int row)
1930 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1931 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1932 .getStartRes(), row, true, true);
1936 * scroll to the given column - or nearest visible location
1940 public void scrollToColumn(int column)
1943 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1944 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1948 * If this panel is a cdna/protein translation view of the selection source,
1949 * tries to map the source selection to a local one, and returns true. Else
1956 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1957 ColumnSelection colsel, HiddenColumns hidden,
1958 SelectionSource source)
1960 if (!(source instanceof AlignViewportI))
1964 final AlignViewportI sourceAv = (AlignViewportI) source;
1965 if (sourceAv.getCodingComplement() != av
1966 && av.getCodingComplement() != sourceAv)
1972 * Map sequence selection
1974 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1975 av.setSelectionGroup(sg);
1976 av.isSelectionGroupChanged(true);
1979 * Map column selection
1981 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1983 ColumnSelection cs = new ColumnSelection();
1984 HiddenColumns hs = new HiddenColumns();
1985 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1986 av.setColumnSelection(cs);
1987 av.getAlignment().setHiddenColumns(hs);
1989 ap.scalePanelHolder.repaint();