2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
25 import java.awt.event.*;
\r
27 import jalview.datamodel.*;
\r
28 import jalview.schemes.*;
\r
30 public class SeqPanel
\r
34 public SeqCanvas seqCanvas;
\r
35 public AlignmentPanel ap;
\r
37 protected int lastres;
\r
38 protected int startseq;
\r
42 protected AlignViewport av;
\r
44 // if character is inserted or deleted, we will need to recalculate the conservation
\r
45 int seqEditOccurred = -1;
\r
47 ScrollThread scrollThread = null;
\r
48 boolean mouseDragging = false;
\r
50 boolean editingSeqs = false;
\r
51 boolean groupEditing = false;
\r
53 public SeqPanel(AlignViewport avp, AlignmentPanel p)
\r
57 seqCanvas = new SeqCanvas(avp);
\r
58 setLayout(new BorderLayout());
\r
63 seqCanvas.addMouseMotionListener(new MouseMotionAdapter()
\r
65 public void mouseMoved(MouseEvent evt)
\r
67 if (av.getWrapAlignment())
\r
74 public void mouseDragged(MouseEvent evt)
\r
76 if (av.getWrapAlignment())
\r
82 doMouseDragged(evt);
\r
86 doMouseDraggedDefineMode(evt);
\r
91 seqCanvas.addMouseListener(new MouseAdapter()
\r
93 public void mouseReleased(MouseEvent evt)
\r
95 if (av.getWrapAlignment())
\r
101 doMouseReleased(evt);
\r
105 doMouseReleasedDefineMode(evt);
\r
110 public void mousePressed(MouseEvent evt)
\r
112 if (av.getWrapAlignment())
\r
116 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
\r
118 if (evt.isAltDown() || evt.isControlDown())
\r
120 groupEditing = true;
\r
123 editingSeqs = true;
\r
124 doMousePressed(evt);
\r
128 doMousePressedDefineMode(evt);
\r
132 public void mouseExited(MouseEvent evt)
\r
134 if (av.getWrapAlignment() || editingSeqs)
\r
138 doMouseExitedDefineMode(evt);
\r
142 public void mouseEntered(MouseEvent evt)
\r
144 if (av.getWrapAlignment() || editingSeqs)
\r
148 doMouseEnteredDefineMode(evt);
\r
152 seqCanvas.repaint();
\r
155 public void doMouseReleased(MouseEvent evt)
\r
158 if (seqEditOccurred > -1)
\r
160 editOccurred(seqEditOccurred);
\r
165 seqEditOccurred = -1;
\r
166 editingSeqs = false;
\r
167 groupEditing = false;
\r
171 public void doMousePressed(MouseEvent evt)
\r
173 ap.alignFrame.addHistoryItem(new HistoryItem(
\r
174 "Edit Sequence", av.alignment, HistoryItem.EDIT));
\r
178 int x = evt.getX();
\r
179 int y = evt.getY();
\r
181 res = x / av.getCharWidth() + av.getStartRes();
\r
182 seq = y / av.getCharHeight() + av.getStartSeq();
\r
184 if (seq < av.getAlignment().getHeight() &&
\r
185 res < av.getAlignment().getSequenceAt(seq).getLength())
\r
187 //char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
\r
188 // Find the residue's position in the sequence (res is the position
\r
189 // in the alignment
\r
203 public void doMouseMoved(MouseEvent evt)
\r
205 int res = 0, seq = 0;
\r
206 int x = evt.getX();
\r
207 int y = evt.getY();
\r
208 if (av.wrapAlignment)
\r
210 y -= 2 * av.charHeight;
\r
211 int chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
213 res = (int) ( (y / chunkHeight) * (getSize().width / av.charWidth)) +
\r
214 x / av.getCharWidth() + av.getStartRes();
\r
217 seq = y / av.getCharHeight() + av.getStartSeq();
\r
222 res = x / av.getCharWidth() + av.getStartRes();
\r
223 seq = y / av.getCharHeight() + av.getStartSeq();
\r
226 if (seq >= av.getAlignment().getHeight())
\r
231 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
\r
232 if (res > sequence.getLength())
\r
237 Object obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
\r
241 aa = obj.toString();
\r
244 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +
\r
245 sequence.getName());
\r
248 text.append(" Residue: " + aa + " (" +
\r
249 av.getAlignment().getSequenceAt(seq).findPosition(res) + ")");
\r
252 ap.alignFrame.statusBar.setText(text.toString());
\r
254 // use aa to see if the mouse pointer is on a
\r
255 if (av.showSequenceFeatures)
\r
257 Vector features = sequence.getSequenceFeatures();
\r
258 Enumeration e = features.elements();
\r
259 StringBuffer sbuffer = new StringBuffer();
\r
261 while (e.hasMoreElements())
\r
263 SequenceFeature sf = (SequenceFeature) e.nextElement();
\r
264 if (sf.getStart() <= sequence.findPosition(res) &&
\r
265 sf.getEnd() >= sequence.findPosition(res))
\r
267 if (sbuffer.length() > 0)
\r
269 sbuffer.append("; ");
\r
271 sbuffer.append(sf.getType() + " " + sf.getDescription());
\r
272 if (sf.getStatus().length() > 0)
\r
274 sbuffer.append(" (" + sf.getStatus() + ")");
\r
283 public void doMouseDragged(MouseEvent evt)
\r
286 // If we're dragging we're editing
\r
287 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
\r
293 if (lastres == -1 || lastres == res)
\r
298 boolean dragRight = true;
\r
299 if (res < av.getAlignment().getWidth() && res < lastres)
\r
304 if (res != lastres)
\r
309 SequenceGroup sg = av.getSelectionGroup();
\r
319 sg.setEndRes(sg.getEndRes() + (res - lastres));
\r
325 /// Are we able to delete?
\r
326 // ie are all columns blank?
\r
327 boolean deleteAllowed = false;
\r
328 for (int s = 0; s < sg.getSize(); s++)
\r
330 SequenceI seq = sg.getSequenceAt(s);
\r
331 for (int j = res; j < lastres; j++)
\r
333 if (seq.getSequence().length() <= j)
\r
338 if (!jalview.util.Comparison.isGap(seq.getSequence().charAt(j)))
\r
340 // Not a gap, block edit not valid
\r
342 deleteAllowed = false;
\r
345 deleteAllowed = true;
\r
349 if (!deleteAllowed)
\r
355 sg.setEndRes(sg.getEndRes() - (lastres - res));
\r
358 for (int i = 0; i < sg.getSize(); i++)
\r
360 SequenceI s = sg.getSequenceAt(i);
\r
361 int k = av.alignment.findIndex(s);
\r
366 for (int j = lastres; j < res; j++)
\r
375 for (int j = res; j < lastres; j++)
\r
377 if (s.getLength() > j)
\r
379 deleteChar(res, k);
\r
385 else /////Editing a single sequence///////////
\r
387 if (res < av.getAlignment().getWidth() && res > lastres)
\r
389 // dragging to the right
\r
390 for (int j = lastres; j < res; j++)
\r
392 insertChar(j, startseq);
\r
395 else if (res < av.getAlignment().getWidth() && res < lastres)
\r
397 // dragging to the left
\r
398 for (int j = lastres; j > res; j--)
\r
400 if (jalview.util.Comparison.isGap(
\r
401 av.alignment.getSequenceAt(startseq).getSequence().charAt(res)))
\r
404 deleteChar(res, startseq);
\r
419 seqCanvas.repaint();
\r
422 public void drawChars(int seqstart, int seqend, int start)
\r
424 seqCanvas.drawPanel(seqCanvas.gg, start, av.getEndRes(), seqstart, seqend,
\r
425 av.getStartRes(), av.getStartSeq(), 0);
\r
426 seqCanvas.repaint();
\r
429 public void insertChar(int j, int seq)
\r
431 av.alignment.getSequenceAt(seq).insertCharAt(j, av.getGapCharacter());
\r
432 seqEditOccurred = seq;
\r
435 public void deleteChar(int j, int seq)
\r
438 av.alignment.getSequenceAt(seq).deleteCharAt(j);
\r
439 seqEditOccurred = seq;
\r
440 av.alignment.getWidth();
\r
444 void editOccurred(int i)
\r
446 if (endEdit == startEdit)
\r
448 ap.alignFrame.historyList.pop();
\r
449 ap.alignFrame.updateEditMenuBar();
\r
452 av.updateConservation();
\r
453 av.updateConsensus();
\r
456 ColourSchemeI cs = av.getGlobalColourScheme();
\r
457 if (cs instanceof ConservationColourScheme)
\r
459 ConservationColourScheme ccs = (ConservationColourScheme) cs;
\r
460 if (ccs.cs instanceof ClustalxColourScheme)
\r
462 jalview.analysis.Conservation c = new jalview.analysis.Conservation
\r
464 ResidueProperties.propHash, 3,
\r
465 av.alignment.getSequences(), 0,
\r
466 av.alignment.getWidth() - 1);
\r
468 c.verdict(false, av.ConsPercGaps);
\r
470 ClustalxColourScheme cxs = (ClustalxColourScheme) ccs.cs;
\r
471 cxs.resetClustalX(av.alignment.getSequences(), av.alignment.getWidth());
\r
472 ccs = new ConservationColourScheme(c, cxs);
\r
473 av.setGlobalColourScheme(ccs);
\r
477 if (cs instanceof ClustalxColourScheme)
\r
479 ( (ClustalxColourScheme) cs).resetClustalX(av.alignment.getSequences(),
\r
480 av.alignment.getWidth());
\r
481 av.setGlobalColourScheme(cs);
\r
486 //////////////////////////////////////////
\r
487 /////Everything below this is for defining the boundary of the rubberband
\r
488 //////////////////////////////////////////
\r
490 public void doMousePressedDefineMode(MouseEvent evt)
\r
492 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
\r
493 int seq = evt.getY() / av.getCharHeight() + av.getStartSeq();
\r
496 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
\r
498 if (sequence == null || res > sequence.getLength())
\r
503 stretchGroup = av.getSelectionGroup();
\r
505 if (stretchGroup == null)
\r
507 stretchGroup = av.alignment.findGroup(sequence);
\r
508 if (stretchGroup != null && res > stretchGroup.getStartRes() &&
\r
509 res < stretchGroup.getEndRes())
\r
511 av.setSelectionGroup(stretchGroup);
\r
515 stretchGroup = null;
\r
519 else if (!stretchGroup.sequences.contains(sequence)
\r
520 || stretchGroup.getStartRes() > res
\r
521 || stretchGroup.getEndRes() < res)
\r
523 stretchGroup = null;
\r
525 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
\r
527 if (allGroups != null)
\r
529 for (int i = 0; i < allGroups.length; i++)
\r
531 if (allGroups[i].getStartRes() <= res &&
\r
532 allGroups[i].getEndRes() >= res)
\r
534 stretchGroup = allGroups[i];
\r
535 av.setSelectionGroup(stretchGroup);
\r
542 if (stretchGroup == null)
\r
544 // define a new group here
\r
545 SequenceGroup sg = new SequenceGroup();
\r
546 sg.setStartRes(res);
\r
548 sg.addSequence(sequence, false);
\r
549 av.setSelectionGroup(sg);
\r
552 if (av.getConservationSelected())
\r
554 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
\r
557 if (av.getAbovePIDThreshold())
\r
559 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
\r
565 // DETECT RIGHT MOUSE BUTTON IN AWT
\r
566 else if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
\r
567 InputEvent.BUTTON3_MASK)
\r
569 APopupMenu popup = new APopupMenu(ap, null);
\r
571 popup.show(this, evt.getX(), evt.getY());
\r
574 if (stretchGroup != null && stretchGroup.getEndRes() == res)
\r
576 // Edit end res position of selected group
\r
577 changeEndRes = true;
\r
580 else if (stretchGroup != null && stretchGroup.getStartRes() == res)
\r
582 // Edit end res position of selected group
\r
583 changeStartRes = true;
\r
588 boolean changeEndSeq = false;
\r
589 boolean changeStartSeq = false;
\r
590 boolean changeEndRes = false;
\r
591 boolean changeStartRes = false;
\r
592 SequenceGroup stretchGroup = null;
\r
594 public void doMouseReleasedDefineMode(MouseEvent evt)
\r
598 stretchGroup.recalcConservation();
\r
599 mouseDragging = false;
\r
602 if (stretchGroup == null)
\r
607 if (stretchGroup.cs instanceof ConservationColourScheme)
\r
609 ConservationColourScheme ccs = (ConservationColourScheme) stretchGroup.cs;
\r
610 stretchGroup.cs = ccs;
\r
611 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
\r
612 stretchGroup.getName());
\r
616 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
\r
617 stretchGroup.getName());
\r
620 changeEndRes = false;
\r
621 changeStartRes = false;
\r
622 stretchGroup = null;
\r
623 seqCanvas.repaint();
\r
627 boolean remove = false;
\r
628 public void doMouseDraggedDefineMode(MouseEvent evt)
\r
630 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
\r
631 int y = evt.getY() / av.getCharHeight() + av.getStartSeq();
\r
633 if (stretchGroup == null)
\r
638 if (res > av.alignment.getWidth())
\r
640 res = av.alignment.getWidth() - 1;
\r
643 if (stretchGroup.getEndRes() == res)
\r
645 // Edit end res position of selected group
\r
646 changeEndRes = true;
\r
649 else if (stretchGroup.getStartRes() == res)
\r
651 // Edit start res position of selected group
\r
652 changeStartRes = true;
\r
655 if (res < av.getStartRes())
\r
657 res = av.getStartRes();
\r
659 else if (res > av.getEndRes())
\r
661 res = av.getEndRes();
\r
666 if (res > stretchGroup.getStartRes() - 1)
\r
668 stretchGroup.setEndRes(res);
\r
671 else if (changeStartRes)
\r
673 if (res < stretchGroup.getEndRes() + 1)
\r
675 stretchGroup.setStartRes(res);
\r
679 int dragDirection = 0;
\r
684 else if (y < oldSeq)
\r
686 dragDirection = -1;
\r
689 while (y != oldSeq && oldSeq > 0 && y < av.alignment.getHeight())
\r
691 // This routine ensures we don't skip any sequences, as the
\r
692 // selection is quite slow.
\r
693 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
\r
695 oldSeq += dragDirection;
\r
696 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
\r
698 if (stretchGroup.sequences.contains(nextSeq))
\r
700 stretchGroup.deleteSequence(seq, false);
\r
706 stretchGroup.addSequence(seq, false);
\r
708 stretchGroup.addSequence(nextSeq, false);
\r
712 mouseDragging = true;
\r
713 if (scrollThread != null)
\r
715 scrollThread.setEvent(evt);
\r
718 seqCanvas.repaint();
\r
721 public void doMouseEnteredDefineMode(MouseEvent e)
\r
723 if (scrollThread != null)
\r
725 scrollThread.running = false;
\r
729 public void doMouseExitedDefineMode(MouseEvent e)
\r
731 if (av.getWrapAlignment())
\r
738 scrollThread = new ScrollThread();
\r
743 // this class allows scrolling off the bottom of the visible alignment
\r
748 boolean running = false;
\r
749 public ScrollThread()
\r
754 public void setEvent(MouseEvent e)
\r
759 public void stopScrolling()
\r
772 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
\r
774 running = ap.scrollUp(true);
\r
777 if (mouseDragging && evt.getY() >= getSize().height &&
\r
778 av.alignment.getHeight() > av.getEndSeq())
\r
780 running = ap.scrollUp(false);
\r
783 if (mouseDragging && evt.getX() < 0)
\r
785 running = ap.scrollRight(true);
\r
788 else if (mouseDragging && evt.getX() >= getSize().width)
\r
790 running = ap.scrollRight(false);
\r
798 catch (Exception ex)
\r