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.ColumnSelection;
27 import jalview.datamodel.SearchResults;
28 import jalview.datamodel.SearchResults.Match;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.structure.SelectionListener;
35 import jalview.structure.SelectionSource;
36 import jalview.structure.SequenceListener;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.structure.VamsasSource;
39 import jalview.util.Comparison;
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 awt2swing.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.List;
54 import java.util.Vector;
56 public class SeqPanel extends Panel implements MouseMotionListener,
57 MouseListener, SequenceListener, SelectionListener
60 public SeqCanvas seqCanvas;
62 public AlignmentPanel ap;
64 protected int lastres;
66 protected int startseq;
68 protected AlignViewport av;
70 // if character is inserted or deleted, we will need to recalculate the
72 boolean seqEditOccurred = false;
74 ScrollThread scrollThread = null;
76 boolean mouseDragging = false;
78 boolean editingSeqs = false;
80 boolean groupEditing = false;
84 boolean changeEndSeq = false;
86 boolean changeStartSeq = false;
88 boolean changeEndRes = false;
90 boolean changeStartRes = false;
92 SequenceGroup stretchGroup = null;
94 StringBuffer keyboardNo1;
96 StringBuffer keyboardNo2;
98 boolean mouseWheelPressed = false;
100 Point lastMousePress;
102 EditCommand editCommand;
104 StructureSelectionManager ssm;
106 public SeqPanel(AlignViewport avp, AlignmentPanel p)
112 seqCanvas = new SeqCanvas(avp);
113 setLayout(new BorderLayout());
118 seqCanvas.addMouseMotionListener(this);
119 seqCanvas.addMouseListener(this);
120 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
121 ssm.addStructureViewerListener(this);
122 ssm.addSelectionListener(this);
129 if (editCommand != null && editCommand.getSize() > 0)
131 ap.alignFrame.addHistoryItem(editCommand);
132 av.firePropertyChange("alignment", null, av.getAlignment()
139 groupEditing = false;
147 seqCanvas.cursorY = getKeyboardNo1() - 1;
151 void setCursorColumn()
153 seqCanvas.cursorX = getKeyboardNo1() - 1;
157 void setCursorRowAndColumn()
159 if (keyboardNo2 == null)
161 keyboardNo2 = new StringBuffer();
165 seqCanvas.cursorX = getKeyboardNo1() - 1;
166 seqCanvas.cursorY = getKeyboardNo2() - 1;
171 void setCursorPosition()
173 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
175 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
179 void moveCursor(int dx, int dy)
181 seqCanvas.cursorX += dx;
182 seqCanvas.cursorY += dy;
183 if (av.hasHiddenColumns()
184 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
186 int original = seqCanvas.cursorX - dx;
187 int maxWidth = av.getAlignment().getWidth();
189 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
190 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
192 seqCanvas.cursorX += dx;
195 if (seqCanvas.cursorX >= maxWidth
196 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
198 seqCanvas.cursorX = original;
204 void scrollToVisible()
206 if (seqCanvas.cursorX < 0)
208 seqCanvas.cursorX = 0;
210 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
212 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
215 if (seqCanvas.cursorY < 0)
217 seqCanvas.cursorY = 0;
219 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
221 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
225 if (av.getWrapAlignment())
227 ap.scrollToWrappedVisible(seqCanvas.cursorX);
231 while (seqCanvas.cursorY < av.startSeq)
235 while (seqCanvas.cursorY + 1 > av.endSeq)
239 while (seqCanvas.cursorX < av.getColumnSelection()
240 .adjustForHiddenColumns(av.startRes))
243 if (!ap.scrollRight(false))
248 while (seqCanvas.cursorX > av.getColumnSelection()
249 .adjustForHiddenColumns(av.endRes))
251 if (!ap.scrollRight(true))
257 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
258 seqCanvas.cursorX, seqCanvas.cursorY);
263 void setSelectionAreaAtCursor(boolean topLeft)
265 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
267 if (av.getSelectionGroup() != null)
269 SequenceGroup sg = av.getSelectionGroup();
270 // Find the top and bottom of this group
271 int min = av.getAlignment().getHeight(), max = 0;
272 for (int i = 0; i < sg.getSize(); i++)
274 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
289 sg.setStartRes(seqCanvas.cursorX);
290 if (sg.getEndRes() < seqCanvas.cursorX)
292 sg.setEndRes(seqCanvas.cursorX);
295 min = seqCanvas.cursorY;
299 sg.setEndRes(seqCanvas.cursorX);
300 if (sg.getStartRes() > seqCanvas.cursorX)
302 sg.setStartRes(seqCanvas.cursorX);
305 max = seqCanvas.cursorY + 1;
310 // Only the user can do this
311 av.setSelectionGroup(null);
315 // Now add any sequences between min and max
317 for (int i = min; i < max; i++)
319 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
324 if (av.getSelectionGroup() == null)
326 SequenceGroup sg = new SequenceGroup();
327 sg.setStartRes(seqCanvas.cursorX);
328 sg.setEndRes(seqCanvas.cursorX);
329 sg.addSequence(sequence, false);
330 av.setSelectionGroup(sg);
332 ap.paintAlignment(false);
336 void insertGapAtCursor(boolean group)
338 groupEditing = group;
339 startseq = seqCanvas.cursorY;
340 lastres = seqCanvas.cursorX;
341 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
345 void deleteGapAtCursor(boolean group)
347 groupEditing = group;
348 startseq = seqCanvas.cursorY;
349 lastres = seqCanvas.cursorX + getKeyboardNo1();
350 editSequence(false, seqCanvas.cursorX);
354 void numberPressed(char value)
356 if (keyboardNo1 == null)
358 keyboardNo1 = new StringBuffer();
361 if (keyboardNo2 != null)
363 keyboardNo2.append(value);
367 keyboardNo1.append(value);
375 if (keyboardNo1 != null)
377 int value = Integer.parseInt(keyboardNo1.toString());
381 } catch (Exception x)
392 if (keyboardNo2 != null)
394 int value = Integer.parseInt(keyboardNo2.toString());
398 } catch (Exception x)
406 * Set status message in alignment panel
409 * aligned sequence object
413 * index of sequence in alignment
414 * @return position of res in sequence
416 void setStatusMessage(SequenceI sequence, int res, int seq)
418 // TODO remove duplication of identical gui method
419 StringBuilder text = new StringBuilder(32);
420 String seqno = seq == -1 ? "" : " " + (seq + 1);
421 text.append("Sequence" + seqno + " ID: " + sequence.getName());
423 String residue = null;
425 * Try to translate the display character to residue name (null for gap).
427 final String displayChar = String.valueOf(sequence.getCharAt(res));
428 if (av.getAlignment().isNucleotide())
430 residue = ResidueProperties.nucleotideName.get(displayChar);
433 text.append(" Nucleotide: ").append(residue);
438 residue = "X".equalsIgnoreCase(displayChar) ? "X"
439 : ResidueProperties.aa2Triplet.get(displayChar);
442 text.append(" Residue: ").append(residue);
449 pos = sequence.findPosition(res);
450 text.append(" (").append(Integer.toString(pos)).append(")");
452 // Object obj = null;
453 // if (av.getAlignment().isNucleotide())
455 // obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
459 // text.append(" Nucleotide: ");
464 // obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
467 // text.append(" Residue: ");
476 // text.append(obj + " (" + sequence.findPosition(res) + ")");
480 ap.alignFrame.setStatus(text.toString());
485 * Set the status bar message to highlight the first matched position in
490 private void setStatusMessage(SearchResults results)
492 List<Match> matches = results.getResults();
493 if (!matches.isEmpty())
495 Match m = matches.get(0);
496 SequenceI seq = m.getSequence();
497 int sequenceIndex = this.av.getAlignment().findIndex(seq);
500 * Convert position in sequence (base 1) to sequence character array index
503 int start = m.getStart() - 1;
504 setStatusMessage(seq, start, sequenceIndex);
508 public void mousePressed(MouseEvent evt)
510 lastMousePress = evt.getPoint();
512 // For now, ignore the mouseWheel font resizing on Macs
513 // As the Button2_mask always seems to be true
514 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
517 mouseWheelPressed = true;
521 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
523 if (evt.isControlDown() || evt.isAltDown())
531 doMousePressedDefineMode(evt);
535 int seq = findSeq(evt);
536 int res = findRes(evt);
538 if (seq < 0 || res < 0)
543 if ((seq < av.getAlignment().getHeight())
544 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
558 public void mouseClicked(MouseEvent evt)
560 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
561 if (evt.getClickCount() > 1)
563 if (av.getSelectionGroup() != null
564 && av.getSelectionGroup().getSize() == 1
565 && av.getSelectionGroup().getEndRes()
566 - av.getSelectionGroup().getStartRes() < 2)
568 av.setSelectionGroup(null);
571 SequenceFeature[] features = findFeaturesAtRes(sequence,
572 sequence.findPosition(findRes(evt)));
574 if (features != null && features.length > 0)
576 SearchResults highlight = new SearchResults();
577 highlight.addResult(sequence, features[0].getBegin(),
578 features[0].getEnd());
579 seqCanvas.highlightSearchResults(highlight);
581 if (features != null && features.length > 0)
583 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
584 { sequence }, features, false, ap);
586 seqCanvas.highlightSearchResults(null);
591 public void mouseReleased(MouseEvent evt)
593 mouseDragging = false;
594 mouseWheelPressed = false;
595 ap.paintAlignment(true);
599 doMouseReleasedDefineMode(evt);
607 int startWrapBlock = -1;
609 int wrappedBlock = -1;
611 int findRes(MouseEvent evt)
616 if (av.getWrapAlignment())
619 int hgap = av.getCharHeight();
620 if (av.getScaleAboveWrapped())
622 hgap += av.getCharHeight();
625 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
627 + seqCanvas.getAnnotationHeight();
631 x -= seqCanvas.LABEL_WEST;
633 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
639 wrappedBlock = y / cHeight;
640 wrappedBlock += av.getStartRes() / cwidth;
642 res = wrappedBlock * cwidth + x / av.getCharWidth();
647 res = (x / av.getCharWidth()) + av.getStartRes();
650 if (av.hasHiddenColumns())
652 res = av.getColumnSelection().adjustForHiddenColumns(res);
659 int findSeq(MouseEvent evt)
661 final int sqnum = findAlRow(evt);
662 return (sqnum < 0) ? 0 : sqnum;
668 * @return row in alignment that was selected (or -1 for column selection)
670 private int findAlRow(MouseEvent evt)
675 if (av.getWrapAlignment())
677 int hgap = av.getCharHeight();
678 if (av.getScaleAboveWrapped())
680 hgap += av.getCharHeight();
683 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
685 + seqCanvas.getAnnotationHeight();
689 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
698 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
699 .getAlignment().getHeight() - 1);
709 public void doMousePressed(MouseEvent evt)
712 int seq = findSeq(evt);
713 int res = findRes(evt);
715 if (seq < av.getAlignment().getHeight()
716 && res < av.getAlignment().getSequenceAt(seq).getLength())
718 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
719 // Find the residue's position in the sequence (res is the position
736 public void mouseOverSequence(SequenceI sequence, int index, int pos)
738 String tmp = sequence.hashCode() + index + "";
739 if (lastMessage == null || !lastMessage.equals(tmp))
741 ssm.mouseOverSequence(sequence, index, pos, av);
747 public void highlightSequence(SearchResults results)
749 if (av.isFollowHighlight())
751 if (ap.scrollToPosition(results, true))
753 ap.alignFrame.repaint();
756 setStatusMessage(results);
757 seqCanvas.highlightSearchResults(results);
762 public VamsasSource getVamsasSource()
764 return this.ap == null ? null : this.ap.av;
767 public void updateColours(SequenceI seq, int index)
769 System.out.println("update the seqPanel colours");
773 public void mouseMoved(MouseEvent evt)
775 int res = findRes(evt);
776 int seq = findSeq(evt);
778 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
787 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
788 if (res > sequence.getLength())
797 int respos = sequence.findPosition(res);
800 mouseOverSequence(sequence, res, respos);
803 StringBuilder text = new StringBuilder();
804 text.append("Sequence ").append(Integer.toString(seq + 1))
805 .append(" ID: ").append(sequence.getName());
808 final String ch = String.valueOf(sequence.getCharAt(res));
809 if (av.getAlignment().isNucleotide())
811 obj = ResidueProperties.nucleotideName.get(ch);
814 text.append(" Nucleotide: ").append(obj);
819 obj = "X".equalsIgnoreCase(ch) ? "X"
820 : ResidueProperties.aa2Triplet.get(ch);
823 text.append(" Residue: ").append(obj);
829 text.append(" (").append(Integer.toString(respos)).append(")");
832 ap.alignFrame.setStatus(text.toString());
834 StringBuilder tooltipText = new StringBuilder();
835 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
838 for (int g = 0; g < groups.length; g++)
840 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
842 if (!groups[g].getName().startsWith("JTreeGroup")
843 && !groups[g].getName().startsWith("JGroup"))
845 tooltipText.append(groups[g].getName()).append(" ");
847 if (groups[g].getDescription() != null)
849 tooltipText.append(groups[g].getDescription());
851 tooltipText.append("\n");
856 // use aa to see if the mouse pointer is on a
857 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
858 sequence.findPosition(res));
861 while (index < allFeatures.length)
863 SequenceFeature sf = allFeatures[index];
865 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
867 if (sf.getDescription() != null)
869 tooltipText.append(" " + sf.getDescription());
872 if (sf.getValue("status") != null)
874 String status = sf.getValue("status").toString();
875 if (status.length() > 0)
877 tooltipText.append(" (" + sf.getValue("status") + ")");
880 tooltipText.append("\n");
887 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
891 tooltip.setTip(tooltipText.toString());
895 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
897 Vector tmp = new Vector();
898 SequenceFeature[] features = sequence.getSequenceFeatures();
899 if (features != null)
901 for (int i = 0; i < features.length; i++)
903 if (av.getFeaturesDisplayed() == null
904 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
909 if (features[i].featureGroup != null
910 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
915 if ((features[i].getBegin() <= res)
916 && (features[i].getEnd() >= res))
918 tmp.addElement(features[i]);
923 features = new SequenceFeature[tmp.size()];
924 tmp.copyInto(features);
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[]
1073 message.append(" insert ");
1077 message.append(" delete ");
1080 message.append(Math.abs(startres - lastres) + " gaps.");
1081 ap.alignFrame.setStatus(message.toString());
1083 // Are we editing within a selection group?
1085 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1088 fixedColumns = true;
1090 // sg might be null as the user may only see 1 sequence,
1091 // but the sequence represents a group
1094 if (!av.isHiddenRepSequence(seq))
1100 sg = av.getRepresentedSequences(seq);
1103 fixedLeft = sg.getStartRes();
1104 fixedRight = sg.getEndRes();
1106 if ((startres < fixedLeft && lastres >= fixedLeft)
1107 || (startres >= fixedLeft && lastres < fixedLeft)
1108 || (startres > fixedRight && lastres <= fixedRight)
1109 || (startres <= fixedRight && lastres > fixedRight))
1115 if (fixedLeft > startres)
1117 fixedRight = fixedLeft - 1;
1120 else if (fixedRight < startres)
1122 fixedLeft = fixedRight;
1127 if (av.hasHiddenColumns())
1129 fixedColumns = true;
1130 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1131 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1133 if ((insertGap && startres > y1 && lastres < y1)
1134 || (!insertGap && startres < y2 && lastres > y2))
1140 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1141 // Selection spans a hidden region
1142 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1150 fixedRight = y2 - 1;
1157 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1158 .toArray(new SequenceI[0]);
1163 // If the user has selected the whole sequence, and is dragging to
1164 // the right, we can still extend the alignment and selectionGroup
1165 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1166 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1168 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1169 fixedRight = sg.getEndRes();
1172 // Is it valid with fixed columns??
1173 // Find the next gap before the end
1174 // of the visible region boundary
1175 boolean blank = false;
1176 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1180 for (SequenceI gs : groupSeqs)
1182 for (int j = 0; j < startres - lastres; j++)
1184 if (!Comparison.isGap(gs.getCharAt(fixedRight
1200 if (sg.getSize() == av.getAlignment().getHeight())
1202 if ((av.hasHiddenColumns() && startres < av
1203 .getColumnSelection().getHiddenBoundaryRight(startres)))
1209 int alWidth = av.getAlignment().getWidth();
1210 if (av.hasHiddenRows())
1212 int hwidth = av.getAlignment().getHiddenSequences()
1214 if (hwidth > alWidth)
1219 // We can still insert gaps if the selectionGroup
1220 // contains all the sequences
1221 sg.setEndRes(sg.getEndRes() + startres - lastres);
1222 fixedRight = alWidth + startres - lastres;
1233 else if (!insertGap)
1235 // / Are we able to delete?
1236 // ie are all columns blank?
1238 for (SequenceI gs : groupSeqs)
1240 for (int j = startres; j < lastres; j++)
1242 if (gs.getLength() <= j)
1247 if (!Comparison.isGap(gs.getCharAt(j)))
1249 // Not a gap, block edit not valid
1259 // dragging to the right
1260 if (fixedColumns && fixedRight != -1)
1262 for (int j = lastres; j < startres; j++)
1264 insertChar(j, groupSeqs, fixedRight);
1269 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1270 startres - lastres, av.getAlignment(), true);
1275 // dragging to the left
1276 if (fixedColumns && fixedRight != -1)
1278 for (int j = lastres; j > startres; j--)
1280 deleteChar(startres, groupSeqs, fixedRight);
1285 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1286 lastres - startres, av.getAlignment(), true);
1292 // ///Editing a single sequence///////////
1296 // dragging to the right
1297 if (fixedColumns && fixedRight != -1)
1299 for (int j = lastres; j < startres; j++)
1301 insertChar(j, new SequenceI[]
1302 { seq }, fixedRight);
1307 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1308 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1313 // dragging to the left
1314 if (fixedColumns && fixedRight != -1)
1316 for (int j = lastres; j > startres; j--)
1318 if (!Comparison.isGap(seq.getCharAt(startres)))
1323 deleteChar(startres, new SequenceI[]
1324 { seq }, fixedRight);
1329 // could be a keyboard edit trying to delete none gaps
1331 for (int m = startres; m < lastres; m++)
1333 if (!Comparison.isGap(seq.getCharAt(m)))
1342 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1343 { seq }, startres, max, av.getAlignment(), true);
1350 seqCanvas.repaint();
1353 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1355 int blankColumn = fixedColumn;
1356 for (int s = 0; s < seq.length; s++)
1358 // Find the next gap before the end of the visible region boundary
1359 // If lastCol > j, theres a boundary after the gap insertion
1361 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1363 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1365 // Theres a space, so break and insert the gap
1370 if (blankColumn <= j)
1372 blankColumn = fixedColumn;
1378 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1379 av.getAlignment(), true);
1381 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1386 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1389 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1392 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1393 av.getAlignment(), true);
1396 // ////////////////////////////////////////
1397 // ///Everything below this is for defining the boundary of the rubberband
1398 // ////////////////////////////////////////
1399 public void doMousePressedDefineMode(MouseEvent evt)
1401 if (scrollThread != null)
1403 scrollThread.running = false;
1404 scrollThread = null;
1407 int res = findRes(evt);
1408 int seq = findSeq(evt);
1410 startWrapBlock = wrappedBlock;
1417 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1419 if (sequence == null || res > sequence.getLength())
1424 stretchGroup = av.getSelectionGroup();
1426 if (stretchGroup == null)
1428 stretchGroup = av.getAlignment().findGroup(sequence);
1429 if (stretchGroup != null && res > stretchGroup.getStartRes()
1430 && res < stretchGroup.getEndRes())
1432 av.setSelectionGroup(stretchGroup);
1436 stretchGroup = null;
1440 else if (!stretchGroup.getSequences(null).contains(sequence)
1441 || stretchGroup.getStartRes() > res
1442 || stretchGroup.getEndRes() < res)
1444 stretchGroup = null;
1446 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1448 if (allGroups != null)
1450 for (int i = 0; i < allGroups.length; i++)
1452 if (allGroups[i].getStartRes() <= res
1453 && allGroups[i].getEndRes() >= res)
1455 stretchGroup = allGroups[i];
1460 av.setSelectionGroup(stretchGroup);
1463 // DETECT RIGHT MOUSE BUTTON IN AWT
1464 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1466 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1467 sequence.findPosition(res));
1469 Vector<String> links = null;
1470 if (allFeatures != null)
1472 for (int i = 0; i < allFeatures.length; i++)
1474 if (allFeatures[i].links != null)
1478 links = new Vector<String>();
1480 for (int j = 0; j < allFeatures[i].links.size(); j++)
1482 links.addElement(allFeatures[i].links.elementAt(j));
1487 APopupMenu popup = new APopupMenu(ap, null, links);
1489 popup.show(this, evt.getX(), evt.getY());
1495 seqCanvas.cursorX = findRes(evt);
1496 seqCanvas.cursorY = findSeq(evt);
1497 seqCanvas.repaint();
1501 // Only if left mouse button do we want to change group sizes
1503 if (stretchGroup == null)
1505 // define a new group here
1506 SequenceGroup sg = new SequenceGroup();
1507 sg.setStartRes(res);
1509 sg.addSequence(sequence, false);
1510 av.setSelectionGroup(sg);
1513 if (av.getConservationSelected())
1515 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1518 if (av.getAbovePIDThreshold())
1520 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1527 public void doMouseReleasedDefineMode(MouseEvent evt)
1529 if (stretchGroup == null)
1534 stretchGroup.recalcConservation(); // always do this - annotation has own
1536 if (stretchGroup.cs != null)
1538 stretchGroup.cs.alignmentChanged(stretchGroup,
1539 av.getHiddenRepSequences());
1541 if (stretchGroup.cs.conservationApplied())
1543 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1544 stretchGroup.getName());
1548 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1549 stretchGroup.getName());
1552 changeEndRes = false;
1553 changeStartRes = false;
1554 stretchGroup = null;
1555 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1556 ap.paintAlignment(true);
1560 public void doMouseDraggedDefineMode(MouseEvent evt)
1562 int res = findRes(evt);
1563 int y = findSeq(evt);
1565 if (wrappedBlock != startWrapBlock)
1570 if (stretchGroup == null)
1575 mouseDragging = true;
1577 if (y > av.getAlignment().getHeight())
1579 y = av.getAlignment().getHeight() - 1;
1582 if (res >= av.getAlignment().getWidth())
1584 res = av.getAlignment().getWidth() - 1;
1587 if (stretchGroup.getEndRes() == res)
1589 // Edit end res position of selected group
1590 changeEndRes = true;
1592 else if (stretchGroup.getStartRes() == res)
1594 // Edit start res position of selected group
1595 changeStartRes = true;
1605 if (res > (stretchGroup.getStartRes() - 1))
1607 stretchGroup.setEndRes(res);
1610 else if (changeStartRes)
1612 if (res < (stretchGroup.getEndRes() + 1))
1614 stretchGroup.setStartRes(res);
1618 int dragDirection = 0;
1624 else if (y < oldSeq)
1629 while ((y != oldSeq) && (oldSeq > -1)
1630 && (y < av.getAlignment().getHeight()))
1632 // This routine ensures we don't skip any sequences, as the
1633 // selection is quite slow.
1634 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1636 oldSeq += dragDirection;
1643 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1645 if (stretchGroup.getSequences(null).contains(nextSeq))
1647 stretchGroup.deleteSequence(seq, false);
1653 stretchGroup.addSequence(seq, false);
1656 stretchGroup.addSequence(nextSeq, false);
1665 if (res > av.endRes || res < av.startRes || y < av.startSeq
1671 if (scrollThread != null)
1673 scrollThread.setEvent(evt);
1676 seqCanvas.repaint();
1679 public void mouseEntered(MouseEvent e)
1686 if (scrollThread != null)
1688 scrollThread.running = false;
1689 scrollThread = null;
1693 public void mouseExited(MouseEvent e)
1695 if (av.getWrapAlignment())
1700 if (mouseDragging && scrollThread == null)
1702 scrollThread = new ScrollThread();
1706 void scrollCanvas(MouseEvent evt)
1710 if (scrollThread != null)
1712 scrollThread.running = false;
1713 scrollThread = null;
1715 mouseDragging = false;
1719 if (scrollThread == null)
1721 scrollThread = new ScrollThread();
1724 mouseDragging = true;
1725 scrollThread.setEvent(evt);
1730 // this class allows scrolling off the bottom of the visible alignment
1731 class ScrollThread extends Thread
1735 boolean running = false;
1737 public ScrollThread()
1742 public void setEvent(MouseEvent e)
1747 public void stopScrolling()
1761 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1763 running = ap.scrollUp(true);
1766 if (mouseDragging && evt.getY() >= getSize().height
1767 && av.getAlignment().getHeight() > av.getEndSeq())
1769 running = ap.scrollUp(false);
1772 if (mouseDragging && evt.getX() < 0)
1774 running = ap.scrollRight(false);
1777 else if (mouseDragging && evt.getX() >= getSize().width)
1779 running = ap.scrollRight(true);
1786 } catch (Exception ex)
1794 * modify current selection according to a received message.
1796 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1797 SelectionSource source)
1799 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1800 // handles selection messages...
1801 // TODO: extend config options to allow user to control if selections may be
1802 // shared between viewports.
1804 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1805 .getSequenceSetId().equals(av.getSequenceSetId()))))
1811 * Check for selection in a view of which this one is a dna/protein
1814 if (selectionFromTranslation(seqsel, colsel, source))
1819 // do we want to thread this ? (contention with seqsel and colsel locks, I
1821 // rules are: colsel is copied if there is a real intersection between
1822 // sequence selection
1823 boolean repaint = false, copycolsel = true;
1824 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1826 SequenceGroup sgroup = null;
1827 if (seqsel != null && seqsel.getSize() > 0)
1829 if (av.getAlignment() == null)
1832 .println("Selection message: alignviewport av SeqSetId="
1833 + av.getSequenceSetId() + " ViewId="
1835 + " 's alignment is NULL! returning immediatly.");
1838 sgroup = seqsel.intersect(av.getAlignment(),
1839 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1840 if ((sgroup == null || sgroup.getSize() == 0)
1841 && (colsel == null || colsel.size() == 0))
1843 // don't copy columns if the region didn't intersect.
1847 if (sgroup != null && sgroup.getSize() > 0)
1849 av.setSelectionGroup(sgroup);
1853 av.setSelectionGroup(null);
1855 repaint = av.isSelectionGroupChanged(true);
1858 && (av.getColumnSelection() == null || !av
1859 .isColSelChanged(true)))
1861 // the current selection is unset or from a previous message
1862 // so import the new colsel.
1863 if (colsel == null || colsel.size() == 0)
1865 if (av.getColumnSelection() != null)
1867 av.getColumnSelection().clear();
1872 // TODO: shift colSel according to the intersecting sequences
1873 if (av.getColumnSelection() == null)
1875 av.setColumnSelection(new ColumnSelection(colsel));
1879 av.getColumnSelection().setElementsFrom(colsel);
1882 repaint |= av.isColSelChanged(true);
1885 && av.hasHiddenColumns()
1886 && (av.getColumnSelection() == null || av.getColumnSelection()
1887 .getHiddenColumns() == null))
1889 System.err.println("Bad things");
1893 ap.scalePanelHolder.repaint();
1899 * scroll to the given row/column - or nearest visible location
1904 public void scrollTo(int row, int column)
1907 row = row < 0 ? ap.av.startSeq : row;
1908 column = column < 0 ? ap.av.startRes : column;
1909 ap.scrollTo(column, column, row, true, true);
1913 * scroll to the given row - or nearest visible location
1917 public void scrollToRow(int row)
1920 row = row < 0 ? ap.av.startSeq : row;
1921 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1925 * scroll to the given column - or nearest visible location
1929 public void scrollToColumn(int column)
1932 column = column < 0 ? ap.av.startRes : column;
1933 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1937 * If this panel is a cdna/protein translation view of the selection source,
1938 * tries to map the source selection to a local one, and returns true. Else
1945 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1946 ColumnSelection colsel, SelectionSource source)
1948 if (!(source instanceof AlignViewportI)) {
1951 final AlignViewportI sourceAv = (AlignViewportI) source;
1952 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1958 * Map sequence selection
1960 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1961 av.setSelectionGroup(sg);
1962 av.isSelectionGroupChanged(true);
1965 * Map column selection
1967 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1969 av.setColumnSelection(cs);
1970 av.isColSelChanged(true);
1972 ap.scalePanelHolder.repaint();