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.SearchResults;
29 import jalview.datamodel.SearchResults.Match;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.schemes.ResidueProperties;
35 import jalview.structure.SelectionListener;
36 import jalview.structure.SelectionSource;
37 import jalview.structure.SequenceListener;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.structure.VamsasSource;
40 import jalview.util.MappingUtils;
41 import jalview.util.MessageManager;
42 import jalview.viewmodel.AlignmentViewport;
44 import java.awt.BorderLayout;
46 import java.awt.FontMetrics;
47 import java.awt.Panel;
48 import java.awt.Point;
49 import java.awt.event.InputEvent;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
52 import java.awt.event.MouseMotionListener;
53 import java.util.Vector;
55 public class SeqPanel extends Panel implements MouseMotionListener,
56 MouseListener, SequenceListener, SelectionListener
59 public SeqCanvas seqCanvas;
61 public AlignmentPanel ap;
63 protected int lastres;
65 protected int startseq;
67 protected AlignViewport av;
69 // if character is inserted or deleted, we will need to recalculate the
71 boolean seqEditOccurred = false;
73 ScrollThread scrollThread = null;
75 boolean mouseDragging = false;
77 boolean editingSeqs = false;
79 boolean groupEditing = false;
83 boolean changeEndSeq = false;
85 boolean changeStartSeq = false;
87 boolean changeEndRes = false;
89 boolean changeStartRes = false;
91 SequenceGroup stretchGroup = null;
93 StringBuffer keyboardNo1;
95 StringBuffer keyboardNo2;
97 boolean mouseWheelPressed = false;
101 EditCommand editCommand;
103 StructureSelectionManager ssm;
105 public SeqPanel(AlignViewport avp, AlignmentPanel p)
109 seqCanvas = new SeqCanvas(avp);
110 setLayout(new BorderLayout());
115 seqCanvas.addMouseMotionListener(this);
116 seqCanvas.addMouseListener(this);
117 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
118 ssm.addStructureViewerListener(this);
119 ssm.addSelectionListener(this);
126 if (editCommand != null && editCommand.getSize() > 0)
128 ap.alignFrame.addHistoryItem(editCommand);
129 av.firePropertyChange("alignment", null, av.getAlignment()
136 groupEditing = false;
144 seqCanvas.cursorY = getKeyboardNo1() - 1;
148 void setCursorColumn()
150 seqCanvas.cursorX = getKeyboardNo1() - 1;
154 void setCursorRowAndColumn()
156 if (keyboardNo2 == null)
158 keyboardNo2 = new StringBuffer();
162 seqCanvas.cursorX = getKeyboardNo1() - 1;
163 seqCanvas.cursorY = getKeyboardNo2() - 1;
168 void setCursorPosition()
170 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
172 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
176 void moveCursor(int dx, int dy)
178 seqCanvas.cursorX += dx;
179 seqCanvas.cursorY += dy;
180 if (av.hasHiddenColumns()
181 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
183 int original = seqCanvas.cursorX - dx;
184 int maxWidth = av.getAlignment().getWidth();
186 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
187 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
189 seqCanvas.cursorX += dx;
192 if (seqCanvas.cursorX >= maxWidth
193 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
195 seqCanvas.cursorX = original;
201 void scrollToVisible()
203 if (seqCanvas.cursorX < 0)
205 seqCanvas.cursorX = 0;
207 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
209 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
212 if (seqCanvas.cursorY < 0)
214 seqCanvas.cursorY = 0;
216 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
218 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
222 if (av.getWrapAlignment())
224 ap.scrollToWrappedVisible(seqCanvas.cursorX);
228 while (seqCanvas.cursorY < av.startSeq)
232 while (seqCanvas.cursorY + 1 > av.endSeq)
236 while (seqCanvas.cursorX < av.getColumnSelection()
237 .adjustForHiddenColumns(av.startRes))
240 if (!ap.scrollRight(false))
245 while (seqCanvas.cursorX > av.getColumnSelection()
246 .adjustForHiddenColumns(av.endRes))
248 if (!ap.scrollRight(true))
254 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
255 seqCanvas.cursorX, seqCanvas.cursorY);
260 void setSelectionAreaAtCursor(boolean topLeft)
262 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
264 if (av.getSelectionGroup() != null)
266 SequenceGroup sg = av.getSelectionGroup();
267 // Find the top and bottom of this group
268 int min = av.getAlignment().getHeight(), max = 0;
269 for (int i = 0; i < sg.getSize(); i++)
271 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
286 sg.setStartRes(seqCanvas.cursorX);
287 if (sg.getEndRes() < seqCanvas.cursorX)
289 sg.setEndRes(seqCanvas.cursorX);
292 min = seqCanvas.cursorY;
296 sg.setEndRes(seqCanvas.cursorX);
297 if (sg.getStartRes() > seqCanvas.cursorX)
299 sg.setStartRes(seqCanvas.cursorX);
302 max = seqCanvas.cursorY + 1;
307 // Only the user can do this
308 av.setSelectionGroup(null);
312 // Now add any sequences between min and max
314 for (int i = min; i < max; i++)
316 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
321 if (av.getSelectionGroup() == null)
323 SequenceGroup sg = new SequenceGroup();
324 sg.setStartRes(seqCanvas.cursorX);
325 sg.setEndRes(seqCanvas.cursorX);
326 sg.addSequence(sequence, false);
327 av.setSelectionGroup(sg);
329 ap.paintAlignment(false);
333 void insertGapAtCursor(boolean group)
335 groupEditing = group;
336 startseq = seqCanvas.cursorY;
337 lastres = seqCanvas.cursorX;
338 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
342 void deleteGapAtCursor(boolean group)
344 groupEditing = group;
345 startseq = seqCanvas.cursorY;
346 lastres = seqCanvas.cursorX + getKeyboardNo1();
347 editSequence(false, seqCanvas.cursorX);
351 void numberPressed(char value)
353 if (keyboardNo1 == null)
355 keyboardNo1 = new StringBuffer();
358 if (keyboardNo2 != null)
360 keyboardNo2.append(value);
364 keyboardNo1.append(value);
372 if (keyboardNo1 != null)
374 int value = Integer.parseInt(keyboardNo1.toString());
378 } catch (Exception x)
389 if (keyboardNo2 != null)
391 int value = Integer.parseInt(keyboardNo2.toString());
395 } catch (Exception x)
403 * Set status message in alignment panel
406 * aligned sequence object
410 * index of sequence in alignment
411 * @return position of res in sequence
413 void setStatusMessage(SequenceI sequence, int res, int seq)
415 // TODO remove duplication of identical gui method
416 StringBuilder text = new StringBuilder(32);
417 String seqno = seq == -1 ? "" : " " + (seq + 1);
418 text.append("Sequence" + seqno + " ID: " + sequence.getName());
420 String residue = null;
422 * Try to translate the display character to residue name (null for gap).
424 final String displayChar = String.valueOf(sequence.getCharAt(res));
425 if (av.getAlignment().isNucleotide())
427 residue = ResidueProperties.nucleotideName.get(displayChar);
430 text.append(" Nucleotide: ").append(residue);
435 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
436 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
440 text.append(" Residue: ").append(residue);
447 pos = sequence.findPosition(res);
448 text.append(" (").append(Integer.toString(pos)).append(")");
451 ap.alignFrame.statusBar.setText(text.toString());
455 * Set the status bar message to highlight the first matched position in
459 * @return true if results were matched, false if not
461 private boolean setStatusMessage(SearchResults results)
463 AlignmentI al = this.av.getAlignment();
464 int sequenceIndex = al.findIndex(results);
465 if (sequenceIndex == -1)
469 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
470 for (Match m : results.getResults())
472 SequenceI seq = m.getSequence();
473 if (seq.getDatasetSequence() != null)
475 seq = seq.getDatasetSequence();
481 * Convert position in sequence (base 1) to sequence character array
484 int start = m.getStart() - m.getSequence().getStart();
485 setStatusMessage(seq, start, sequenceIndex);
493 public void mousePressed(MouseEvent evt)
495 lastMousePress = evt.getPoint();
497 // For now, ignore the mouseWheel font resizing on Macs
498 // As the Button2_mask always seems to be true
499 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
502 mouseWheelPressed = true;
506 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
508 if (evt.isControlDown() || evt.isAltDown())
516 doMousePressedDefineMode(evt);
520 int seq = findSeq(evt);
521 int res = findRes(evt);
523 if (seq < 0 || res < 0)
528 if ((seq < av.getAlignment().getHeight())
529 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
544 public void mouseClicked(MouseEvent evt)
546 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
547 if (evt.getClickCount() > 1)
549 if (av.getSelectionGroup() != null
550 && av.getSelectionGroup().getSize() == 1
551 && av.getSelectionGroup().getEndRes()
552 - av.getSelectionGroup().getStartRes() < 2)
554 av.setSelectionGroup(null);
557 SequenceFeature[] features = findFeaturesAtRes(sequence,
558 sequence.findPosition(findRes(evt)));
560 if (features != null && features.length > 0)
562 SearchResults highlight = new SearchResults();
563 highlight.addResult(sequence, features[0].getBegin(),
564 features[0].getEnd());
565 seqCanvas.highlightSearchResults(highlight);
567 if (features != null && features.length > 0)
569 seqCanvas.getFeatureRenderer().amendFeatures(
570 new SequenceI[] { sequence }, features, false, ap);
572 seqCanvas.highlightSearchResults(null);
578 public void mouseReleased(MouseEvent evt)
580 mouseDragging = false;
581 mouseWheelPressed = false;
582 ap.paintAlignment(true);
586 doMouseReleasedDefineMode(evt);
594 int startWrapBlock = -1;
596 int wrappedBlock = -1;
598 int findRes(MouseEvent evt)
603 if (av.getWrapAlignment())
606 int hgap = av.getCharHeight();
607 if (av.getScaleAboveWrapped())
609 hgap += av.getCharHeight();
612 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
613 + hgap + seqCanvas.getAnnotationHeight();
617 x -= seqCanvas.LABEL_WEST;
619 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
625 wrappedBlock = y / cHeight;
626 wrappedBlock += av.getStartRes() / cwidth;
628 res = wrappedBlock * cwidth + x / av.getCharWidth();
633 res = (x / av.getCharWidth()) + av.getStartRes();
636 if (av.hasHiddenColumns())
638 res = av.getColumnSelection().adjustForHiddenColumns(res);
645 int findSeq(MouseEvent evt)
647 final int sqnum = findAlRow(evt);
648 return (sqnum < 0) ? 0 : sqnum;
654 * @return row in alignment that was selected (or -1 for column selection)
656 private int findAlRow(MouseEvent evt)
661 if (av.getWrapAlignment())
663 int hgap = av.getCharHeight();
664 if (av.getScaleAboveWrapped())
666 hgap += av.getCharHeight();
669 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
670 + hgap + seqCanvas.getAnnotationHeight();
674 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
683 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
684 .getAlignment().getHeight() - 1);
694 public void doMousePressed(MouseEvent evt)
697 int seq = findSeq(evt);
698 int res = findRes(evt);
700 if (seq < av.getAlignment().getHeight()
701 && res < av.getAlignment().getSequenceAt(seq).getLength())
703 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
704 // Find the residue's position in the sequence (res is the position
722 public void mouseOverSequence(SequenceI sequence, int index, int pos)
724 String tmp = sequence.hashCode() + index + "";
725 if (lastMessage == null || !lastMessage.equals(tmp))
727 ssm.mouseOverSequence(sequence, index, pos, av);
734 public void highlightSequence(SearchResults results)
736 if (av.isFollowHighlight())
738 if (ap.scrollToPosition(results, true))
740 ap.alignFrame.repaint();
743 setStatusMessage(results);
744 seqCanvas.highlightSearchResults(results);
749 public VamsasSource getVamsasSource()
751 return this.ap == null ? null : this.ap.av;
755 public void updateColours(SequenceI seq, int index)
757 System.out.println("update the seqPanel colours");
762 public void mouseMoved(MouseEvent evt)
764 int res = findRes(evt);
765 int seq = findSeq(evt);
767 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
776 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
777 if (res > sequence.getLength())
786 int respos = sequence.findPosition(res);
789 mouseOverSequence(sequence, res, respos);
792 StringBuilder text = new StringBuilder();
793 text.append("Sequence ").append(Integer.toString(seq + 1))
794 .append(" ID: ").append(sequence.getName());
797 final String ch = String.valueOf(sequence.getCharAt(res));
798 if (av.getAlignment().isNucleotide())
800 obj = ResidueProperties.nucleotideName.get(ch);
803 text.append(" Nucleotide: ").append(obj);
808 obj = "X".equalsIgnoreCase(ch) ? "X" : ResidueProperties.aa2Triplet
812 text.append(" Residue: ").append(obj);
818 text.append(" (").append(Integer.toString(respos)).append(")");
821 ap.alignFrame.statusBar.setText(text.toString());
823 StringBuilder tooltipText = new StringBuilder();
824 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
827 for (int g = 0; g < groups.length; g++)
829 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
831 if (!groups[g].getName().startsWith("JTreeGroup")
832 && !groups[g].getName().startsWith("JGroup"))
834 tooltipText.append(groups[g].getName()).append(" ");
836 if (groups[g].getDescription() != null)
838 tooltipText.append(groups[g].getDescription());
840 tooltipText.append("\n");
845 // use aa to see if the mouse pointer is on a
846 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
847 sequence.findPosition(res));
850 while (index < allFeatures.length)
852 SequenceFeature sf = allFeatures[index];
854 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
856 if (sf.getDescription() != null)
858 tooltipText.append(" " + sf.getDescription());
861 if (sf.getValue("status") != null)
863 String status = sf.getValue("status").toString();
864 if (status.length() > 0)
866 tooltipText.append(" (" + sf.getValue("status") + ")");
869 tooltipText.append("\n");
876 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
880 tooltip.setTip(tooltipText.toString());
884 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
886 Vector tmp = new Vector();
887 SequenceFeature[] features = sequence.getSequenceFeatures();
888 if (features != null)
890 for (int i = 0; i < features.length; i++)
892 if (av.getFeaturesDisplayed() == null
893 || !av.getFeaturesDisplayed().isVisible(
894 features[i].getType()))
899 if (features[i].featureGroup != null
900 && !seqCanvas.fr.checkGroupVisibility(
901 features[i].featureGroup, false))
906 if ((features[i].getBegin() <= res)
907 && (features[i].getEnd() >= res))
909 tmp.addElement(features[i]);
914 features = new SequenceFeature[tmp.size()];
915 tmp.copyInto(features);
923 * set when the current UI interaction has resulted in a change that requires
924 * overview shading to be recalculated. this could be changed to something
925 * more expressive that indicates what actually has changed, so selective
926 * redraws can be applied
928 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
931 public void mouseDragged(MouseEvent evt)
933 if (mouseWheelPressed)
935 int oldWidth = av.getCharWidth();
937 // Which is bigger, left-right or up-down?
938 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
941 int fontSize = av.font.getSize();
943 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
947 else if (evt.getY() > lastMousePress.y)
957 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
958 av.setCharWidth(oldWidth);
962 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
964 av.setCharWidth(av.getCharWidth() - 1);
966 else if (evt.getX() > lastMousePress.x)
968 av.setCharWidth(av.getCharWidth() + 1);
971 if (av.getCharWidth() < 1)
979 FontMetrics fm = getFontMetrics(av.getFont());
980 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
982 lastMousePress = evt.getPoint();
984 ap.paintAlignment(false);
985 ap.annotationPanel.image = null;
991 doMouseDraggedDefineMode(evt);
995 int res = findRes(evt);
1002 if ((lastres == -1) || (lastres == res))
1007 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1009 // dragLeft, delete gap
1010 editSequence(false, res);
1014 editSequence(true, res);
1017 mouseDragging = true;
1018 if (scrollThread != null)
1020 scrollThread.setEvent(evt);
1025 synchronized void editSequence(boolean insertGap, int startres)
1028 int fixedRight = -1;
1029 boolean fixedColumns = false;
1030 SequenceGroup sg = av.getSelectionGroup();
1032 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1034 if (!groupEditing && av.hasHiddenRows())
1036 if (av.isHiddenRepSequence(seq))
1038 sg = av.getRepresentedSequences(seq);
1039 groupEditing = true;
1043 StringBuffer message = new StringBuffer();
1046 message.append(MessageManager.getString("action.edit_group")).append(
1048 if (editCommand == null)
1050 editCommand = new EditCommand(
1051 MessageManager.getString("action.edit_group"));
1056 message.append(MessageManager.getString("label.edit_sequence"))
1057 .append(" " + seq.getName());
1058 String label = seq.getName();
1059 if (label.length() > 10)
1061 label = label.substring(0, 10);
1063 if (editCommand == null)
1065 editCommand = new EditCommand(MessageManager.formatMessage(
1066 "label.edit_params", new String[] { label }));
1072 message.append(" insert ");
1076 message.append(" delete ");
1079 message.append(Math.abs(startres - lastres) + " gaps.");
1080 ap.alignFrame.statusBar.setText(message.toString());
1082 // Are we editing within a selection group?
1084 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1087 fixedColumns = true;
1089 // sg might be null as the user may only see 1 sequence,
1090 // but the sequence represents a group
1093 if (!av.isHiddenRepSequence(seq))
1099 sg = av.getRepresentedSequences(seq);
1102 fixedLeft = sg.getStartRes();
1103 fixedRight = sg.getEndRes();
1105 if ((startres < fixedLeft && lastres >= fixedLeft)
1106 || (startres >= fixedLeft && lastres < fixedLeft)
1107 || (startres > fixedRight && lastres <= fixedRight)
1108 || (startres <= fixedRight && lastres > fixedRight))
1114 if (fixedLeft > startres)
1116 fixedRight = fixedLeft - 1;
1119 else if (fixedRight < startres)
1121 fixedLeft = fixedRight;
1126 if (av.hasHiddenColumns())
1128 fixedColumns = true;
1129 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1130 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1132 if ((insertGap && startres > y1 && lastres < y1)
1133 || (!insertGap && startres < y2 && lastres > y2))
1139 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1140 // Selection spans a hidden region
1141 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1149 fixedRight = y2 - 1;
1156 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1157 .toArray(new SequenceI[0]);
1162 // If the user has selected the whole sequence, and is dragging to
1163 // the right, we can still extend the alignment and selectionGroup
1164 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1165 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1167 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1168 fixedRight = sg.getEndRes();
1171 // Is it valid with fixed columns??
1172 // Find the next gap before the end
1173 // of the visible region boundary
1174 boolean blank = false;
1175 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1179 for (SequenceI gs : groupSeqs)
1181 for (int j = 0; j < startres - lastres; j++)
1183 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1199 if (sg.getSize() == av.getAlignment().getHeight())
1201 if ((av.hasHiddenColumns() && startres < av
1202 .getColumnSelection().getHiddenBoundaryRight(startres)))
1208 int alWidth = av.getAlignment().getWidth();
1209 if (av.hasHiddenRows())
1211 int hwidth = av.getAlignment().getHiddenSequences()
1213 if (hwidth > alWidth)
1218 // We can still insert gaps if the selectionGroup
1219 // contains all the sequences
1220 sg.setEndRes(sg.getEndRes() + startres - lastres);
1221 fixedRight = alWidth + startres - lastres;
1232 else if (!insertGap)
1234 // / Are we able to delete?
1235 // ie are all columns blank?
1237 for (SequenceI gs : groupSeqs)
1239 for (int j = startres; j < lastres; j++)
1241 if (gs.getLength() <= j)
1246 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1248 // Not a gap, block edit not valid
1258 // dragging to the right
1259 if (fixedColumns && fixedRight != -1)
1261 for (int j = lastres; j < startres; j++)
1263 insertChar(j, groupSeqs, fixedRight);
1268 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1269 startres - lastres, av.getAlignment(), true);
1274 // dragging to the left
1275 if (fixedColumns && fixedRight != -1)
1277 for (int j = lastres; j > startres; j--)
1279 deleteChar(startres, groupSeqs, fixedRight);
1284 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1285 lastres - startres, av.getAlignment(), true);
1291 // ///Editing a single sequence///////////
1295 // dragging to the right
1296 if (fixedColumns && fixedRight != -1)
1298 for (int j = lastres; j < startres; j++)
1300 insertChar(j, new SequenceI[] { seq }, fixedRight);
1305 editCommand.appendEdit(Action.INSERT_GAP,
1306 new SequenceI[] { seq }, lastres, startres - lastres,
1307 av.getAlignment(), true);
1312 // dragging to the left
1313 if (fixedColumns && fixedRight != -1)
1315 for (int j = lastres; j > startres; j--)
1317 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1322 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1327 // could be a keyboard edit trying to delete none gaps
1329 for (int m = startres; m < lastres; m++)
1331 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1340 editCommand.appendEdit(Action.DELETE_GAP,
1341 new SequenceI[] { seq }, startres, max,
1342 av.getAlignment(), true);
1349 seqCanvas.repaint();
1352 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1354 int blankColumn = fixedColumn;
1355 for (int s = 0; s < seq.length; s++)
1357 // Find the next gap before the end of the visible region boundary
1358 // If lastCol > j, theres a boundary after the gap insertion
1360 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1362 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1364 // Theres a space, so break and insert the gap
1369 if (blankColumn <= j)
1371 blankColumn = fixedColumn;
1377 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1378 av.getAlignment(), true);
1380 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1385 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1388 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1391 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1392 av.getAlignment(), true);
1395 // ////////////////////////////////////////
1396 // ///Everything below this is for defining the boundary of the rubberband
1397 // ////////////////////////////////////////
1398 public void doMousePressedDefineMode(MouseEvent evt)
1400 if (scrollThread != null)
1402 scrollThread.running = false;
1403 scrollThread = null;
1406 int res = findRes(evt);
1407 int seq = findSeq(evt);
1409 startWrapBlock = wrappedBlock;
1416 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1418 if (sequence == null || res > sequence.getLength())
1423 stretchGroup = av.getSelectionGroup();
1425 if (stretchGroup == null)
1427 stretchGroup = av.getAlignment().findGroup(sequence);
1428 if (stretchGroup != null && res > stretchGroup.getStartRes()
1429 && res < stretchGroup.getEndRes())
1431 av.setSelectionGroup(stretchGroup);
1435 stretchGroup = null;
1439 else if (!stretchGroup.getSequences(null).contains(sequence)
1440 || stretchGroup.getStartRes() > res
1441 || stretchGroup.getEndRes() < res)
1443 stretchGroup = null;
1445 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1447 if (allGroups != null)
1449 for (int i = 0; i < allGroups.length; i++)
1451 if (allGroups[i].getStartRes() <= res
1452 && allGroups[i].getEndRes() >= res)
1454 stretchGroup = allGroups[i];
1459 av.setSelectionGroup(stretchGroup);
1462 // DETECT RIGHT MOUSE BUTTON IN AWT
1463 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1465 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1466 sequence.findPosition(res));
1468 Vector<String> links = null;
1469 if (allFeatures != null)
1471 for (int i = 0; i < allFeatures.length; i++)
1473 if (allFeatures[i].links != null)
1477 links = new Vector<String>();
1479 for (int j = 0; j < allFeatures[i].links.size(); j++)
1481 links.addElement(allFeatures[i].links.elementAt(j));
1486 APopupMenu popup = new APopupMenu(ap, null, links);
1488 popup.show(this, evt.getX(), evt.getY());
1494 seqCanvas.cursorX = findRes(evt);
1495 seqCanvas.cursorY = findSeq(evt);
1496 seqCanvas.repaint();
1500 // Only if left mouse button do we want to change group sizes
1502 if (stretchGroup == null)
1504 // define a new group here
1505 SequenceGroup sg = new SequenceGroup();
1506 sg.setStartRes(res);
1508 sg.addSequence(sequence, false);
1509 av.setSelectionGroup(sg);
1512 if (av.getConservationSelected())
1514 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1517 if (av.getAbovePIDThreshold())
1519 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1526 public void doMouseReleasedDefineMode(MouseEvent evt)
1528 if (stretchGroup == null)
1532 // always do this - annotation has own state
1533 // but defer colourscheme update until hidden sequences are passed in
1534 boolean vischange = stretchGroup.recalcConservation(true);
1535 // here we rely on stretchGroup == av.getSelection()
1536 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1537 if (stretchGroup.cs != null)
1539 stretchGroup.cs.alignmentChanged(stretchGroup,
1540 av.getHiddenRepSequences());
1542 if (stretchGroup.cs.conservationApplied())
1544 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1545 stretchGroup.getName());
1549 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1550 stretchGroup.getName());
1553 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1554 ap.paintAlignment(needOverviewUpdate);
1555 needOverviewUpdate = false;
1556 changeEndRes = false;
1557 changeStartRes = false;
1558 stretchGroup = null;
1562 public void doMouseDraggedDefineMode(MouseEvent evt)
1564 int res = findRes(evt);
1565 int y = findSeq(evt);
1567 if (wrappedBlock != startWrapBlock)
1572 if (stretchGroup == null)
1577 mouseDragging = true;
1579 if (y > av.getAlignment().getHeight())
1581 y = av.getAlignment().getHeight() - 1;
1584 if (res >= av.getAlignment().getWidth())
1586 res = av.getAlignment().getWidth() - 1;
1589 if (stretchGroup.getEndRes() == res)
1591 // Edit end res position of selected group
1592 changeEndRes = true;
1594 else if (stretchGroup.getStartRes() == res)
1596 // Edit start res position of selected group
1597 changeStartRes = true;
1607 if (res > (stretchGroup.getStartRes() - 1))
1609 stretchGroup.setEndRes(res);
1610 needOverviewUpdate |= av.isSelectionDefinedGroup();
1613 else if (changeStartRes)
1615 if (res < (stretchGroup.getEndRes() + 1))
1617 stretchGroup.setStartRes(res);
1618 needOverviewUpdate |= av.isSelectionDefinedGroup();
1622 int dragDirection = 0;
1628 else if (y < oldSeq)
1633 while ((y != oldSeq) && (oldSeq > -1)
1634 && (y < av.getAlignment().getHeight()))
1636 // This routine ensures we don't skip any sequences, as the
1637 // selection is quite slow.
1638 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1640 oldSeq += dragDirection;
1647 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1649 if (stretchGroup.getSequences(null).contains(nextSeq))
1651 stretchGroup.deleteSequence(seq, false);
1652 needOverviewUpdate |= av.isSelectionDefinedGroup();
1658 stretchGroup.addSequence(seq, false);
1661 stretchGroup.addSequence(nextSeq, false);
1662 needOverviewUpdate |= av.isSelectionDefinedGroup();
1671 if (res > av.endRes || res < av.startRes || y < av.startSeq
1677 if (scrollThread != null)
1679 scrollThread.setEvent(evt);
1682 seqCanvas.repaint();
1686 public void mouseEntered(MouseEvent e)
1693 if (scrollThread != null)
1695 scrollThread.running = false;
1696 scrollThread = null;
1701 public void mouseExited(MouseEvent e)
1703 if (av.getWrapAlignment())
1708 if (mouseDragging && scrollThread == null)
1710 scrollThread = new ScrollThread();
1714 void scrollCanvas(MouseEvent evt)
1718 if (scrollThread != null)
1720 scrollThread.running = false;
1721 scrollThread = null;
1723 mouseDragging = false;
1727 if (scrollThread == null)
1729 scrollThread = new ScrollThread();
1732 mouseDragging = true;
1733 scrollThread.setEvent(evt);
1738 // this class allows scrolling off the bottom of the visible alignment
1739 class ScrollThread extends Thread
1743 boolean running = false;
1745 public ScrollThread()
1750 public void setEvent(MouseEvent e)
1755 public void stopScrolling()
1770 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1772 running = ap.scrollUp(true);
1775 if (mouseDragging && evt.getY() >= getSize().height
1776 && av.getAlignment().getHeight() > av.getEndSeq())
1778 running = ap.scrollUp(false);
1781 if (mouseDragging && evt.getX() < 0)
1783 running = ap.scrollRight(false);
1786 else if (mouseDragging && evt.getX() >= getSize().width)
1788 running = ap.scrollRight(true);
1795 } catch (Exception ex)
1803 * modify current selection according to a received message.
1806 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1807 SelectionSource source)
1809 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1810 // handles selection messages...
1811 // TODO: extend config options to allow user to control if selections may be
1812 // shared between viewports.
1814 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1815 .getSequenceSetId().equals(av.getSequenceSetId()))))
1821 * Check for selection in a view of which this one is a dna/protein
1824 if (selectionFromTranslation(seqsel, colsel, source))
1829 // do we want to thread this ? (contention with seqsel and colsel locks, I
1832 * only copy colsel if there is a real intersection between
1833 * sequence selection and this panel's alignment
1835 boolean repaint = false;
1836 boolean copycolsel = false;
1837 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1839 SequenceGroup sgroup = null;
1840 if (seqsel != null && seqsel.getSize() > 0)
1842 if (av.getAlignment() == null)
1845 .println("Selection message: alignviewport av SeqSetId="
1846 + av.getSequenceSetId() + " ViewId="
1848 + " 's alignment is NULL! returning immediatly.");
1851 sgroup = seqsel.intersect(av.getAlignment(),
1852 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1853 if ((sgroup != null && sgroup.getSize() > 0))
1858 if (sgroup != null && sgroup.getSize() > 0)
1860 av.setSelectionGroup(sgroup);
1864 av.setSelectionGroup(null);
1866 repaint = av.isSelectionGroupChanged(true);
1869 && (av.getColumnSelection() == null || !av
1870 .isColSelChanged(true)))
1872 // the current selection is unset or from a previous message
1873 // so import the new colsel.
1874 if (colsel == null || colsel.isEmpty())
1876 if (av.getColumnSelection() != null)
1878 av.getColumnSelection().clear();
1883 // TODO: shift colSel according to the intersecting sequences
1884 if (av.getColumnSelection() == null)
1886 av.setColumnSelection(new ColumnSelection(colsel));
1890 av.getColumnSelection().setElementsFrom(colsel);
1893 repaint |= av.isColSelChanged(true);
1896 && av.hasHiddenColumns()
1897 && (av.getColumnSelection() == null || av.getColumnSelection()
1898 .getHiddenColumns() == null))
1900 System.err.println("Bad things");
1904 ap.scalePanelHolder.repaint();
1910 * scroll to the given row/column - or nearest visible location
1915 public void scrollTo(int row, int column)
1918 row = row < 0 ? ap.av.startSeq : row;
1919 column = column < 0 ? ap.av.startRes : column;
1920 ap.scrollTo(column, column, row, true, true);
1924 * scroll to the given row - or nearest visible location
1928 public void scrollToRow(int row)
1931 row = row < 0 ? ap.av.startSeq : row;
1932 ap.scrollTo(ap.av.startRes, ap.av.startRes, 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.startRes : column;
1944 ap.scrollTo(column, column, ap.av.startSeq, 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, SelectionSource source)
1959 if (!(source instanceof AlignViewportI))
1963 final AlignViewportI sourceAv = (AlignViewportI) source;
1964 if (sourceAv.getCodingComplement() != av
1965 && av.getCodingComplement() != sourceAv)
1971 * Map sequence selection
1973 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1974 av.setSelectionGroup(sg);
1975 av.isSelectionGroupChanged(true);
1978 * Map column selection
1980 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1982 av.setColumnSelection(cs);
1984 ap.scalePanelHolder.repaint();