2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
24 import jalview.analysis.*;
25 import jalview.api.AlignCalcManagerI;
26 import jalview.api.AlignViewportI;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.structure.SelectionSource;
31 import jalview.structure.VamsasSource;
32 import jalview.viewmodel.AlignmentViewport;
33 import jalview.workers.ConservationThread;
34 import jalview.workers.ConsensusThread;
36 public class AlignViewport extends AlignmentViewport implements AlignViewportI, SelectionSource, VamsasSource
46 boolean cursorMode = false;
48 boolean showJVSuffix = true;
50 boolean showText = true;
52 boolean showColourText = false;
54 boolean showBoxes = true;
56 boolean wrapAlignment = false;
58 boolean renderGaps = true;
60 boolean showSequenceFeatures = false;
62 boolean showAnnotation = true;
64 boolean showConservation = true;
66 boolean showQuality = true;
68 boolean showConsensus = true;
70 boolean upperCasebold = false;
72 boolean colourAppliesToAllGroups = true;
74 boolean conservationColourSelected = false;
76 boolean abovePIDThreshold = false;
84 Font font = new Font("SansSerif", Font.PLAIN, 10);
86 boolean validCharWidth = true;
92 NJTree currentTree = null;
94 boolean scaleAboveWrapped = true;
96 boolean scaleLeftWrapped = true;
98 boolean scaleRightWrapped = true;
100 // The following vector holds the features which are
101 // currently visible, in the correct order or rendering
102 public Hashtable featuresDisplayed;
105 boolean showHiddenMarkers = true;
107 public jalview.bin.JalviewLite applet;
109 Hashtable sequenceColours;
113 Stack historyList = new Stack();
115 Stack redoList = new Stack();
117 public void finalize() {
124 public AlignViewport(AlignmentI al, JalviewLite applet)
126 calculator = new jalview.workers.AlignCalcManager();
127 this.applet = applet;
129 // we always pad gaps
130 this.setPadGaps(true);
132 this.endRes = al.getWidth() - 1;
134 this.endSeq = al.getHeight() - 1;
137 // get the width and height scaling factors if they were specified
138 String param = applet.getParameter("widthScale");
143 widthScale = new Float(param).floatValue();
144 } catch (Exception e)
147 if (widthScale <= 1.0)
150 .println("Invalid alignment character width scaling factor ("
151 + widthScale + "). Ignoring.");
157 .println("Alignment character width scaling factor is now "
161 param = applet.getParameter("heightScale");
166 heightScale = new Float(param).floatValue();
167 } catch (Exception e)
170 if (heightScale <= 1.0)
173 .println("Invalid alignment character height scaling factor ("
174 + heightScale + "). Ignoring.");
180 .println("Alignment character height scaling factor is now "
187 MAC = new jalview.util.Platform().isAMac();
191 showJVSuffix = applet.getDefaultParameter("showFullId", showJVSuffix);
193 showAnnotation = applet.getDefaultParameter("showAnnotation", showAnnotation);
195 showConservation = applet.getDefaultParameter("showConservation", showConservation);
197 showQuality = applet.getDefaultParameter("showQuality", showQuality);
199 showConsensus = applet.getDefaultParameter("showConsensus", showConsensus);
201 showUnconserved = applet.getDefaultParameter("showUnconserved", showUnconserved);
203 String param = applet.getParameter("upperCase");
206 if (param.equalsIgnoreCase("bold"))
208 upperCasebold = true;
211 sortByTree = applet.getDefaultParameter("sortByTree", sortByTree);
213 followHighlight = applet.getDefaultParameter("automaticScrolling",followHighlight);
214 followSelection = followHighlight;
216 showSequenceLogo = applet.getDefaultParameter("showSequenceLogo", showSequenceLogo);
218 normaliseSequenceLogo = applet.getDefaultParameter("normaliseSequenceLogo", normaliseSequenceLogo);
220 showGroupConsensus = applet.getDefaultParameter("showGroupConsensus", showGroupConsensus);
222 showGroupConservation = applet.getDefaultParameter("showGroupConservation", showGroupConservation);
224 showConsensusHistogram = applet.getDefaultParameter("showConsensusHistogram", showConsensusHistogram);
230 String colour = applet.getParameter("defaultColour");
234 colour = applet.getParameter("userDefinedColour");
237 colour = "User Defined";
243 globalColourScheme = ColourSchemeProperty.getColour(alignment,
245 if (globalColourScheme != null)
247 globalColourScheme.setConsensus(hconsensus);
251 if (applet.getParameter("userDefinedColour") != null)
253 ((UserColourScheme) globalColourScheme).parseAppletParameter(applet
254 .getParameter("userDefinedColour"));
257 if (hconsensus == null)
259 if (!alignment.isNucleotide())
261 conservation = new AlignmentAnnotation("Conservation",
262 "Conservation of total alignment less than " + getConsPercGaps()
263 + "% gaps", new Annotation[1], 0f, 11f,
264 AlignmentAnnotation.BAR_GRAPH);
265 conservation.hasText = true;
266 conservation.autoCalculated = true;
268 if (showConservation)
270 alignment.addAnnotation(conservation);
275 quality = new AlignmentAnnotation("Quality",
276 "Alignment Quality based on Blosum62 scores",
277 new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
278 quality.hasText = true;
279 quality.autoCalculated = true;
281 alignment.addAnnotation(quality);
284 if (alignment.hasRNAStructure())
286 strucConsensus = new AlignmentAnnotation("StrucConsensus", "PID",
287 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
288 strucConsensus.hasText = true;
289 strucConsensus.autoCalculated = true;
293 consensus = new AlignmentAnnotation("Consensus", "PID",
294 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
295 consensus.hasText = true;
296 consensus.autoCalculated = true;
300 alignment.addAnnotation(consensus);
301 if (strucConsensus!=null)
303 alignment.addAnnotation(strucConsensus);
310 public void showSequenceFeatures(boolean b)
312 showSequenceFeatures = b;
315 public boolean getShowSequenceFeatures()
317 return showSequenceFeatures;
322 * get the consensus sequence as displayed under the PID consensus annotation
325 * @return consensus sequence as a new sequence object
327 public SequenceI getConsensusSeq()
329 if (consensus == null)
331 updateConsensus(null);
333 if (consensus == null)
337 StringBuffer seqs = new StringBuffer();
338 for (int i = 0; i < consensus.annotations.length; i++)
340 if (consensus.annotations[i] != null)
342 if (consensus.annotations[i].description.charAt(0) == '[')
344 seqs.append(consensus.annotations[i].description.charAt(1));
348 seqs.append(consensus.annotations[i].displayCharacter);
352 SequenceI sq = new Sequence("Consensus", seqs.toString());
353 sq.setDescription("Percentage Identity Consensus "
354 + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
358 public boolean getConservationSelected()
360 return conservationColourSelected;
363 public void setConservationSelected(boolean b)
365 conservationColourSelected = b;
368 public boolean getAbovePIDThreshold()
370 return abovePIDThreshold;
373 public void setAbovePIDThreshold(boolean b)
375 abovePIDThreshold = b;
378 public int getStartRes()
383 public int getEndRes()
388 public int getStartSeq()
393 public void setStartRes(int res)
398 public void setStartSeq(int seq)
403 public void setEndRes(int res)
405 if (res > alignment.getWidth() - 1)
407 // log.System.out.println(" Corrected res from " + res + " to maximum " +
408 // (alignment.getWidth()-1));
409 res = alignment.getWidth() - 1;
418 public void setEndSeq(int seq)
420 if (seq > alignment.getHeight())
422 seq = alignment.getHeight();
431 public int getEndSeq()
436 java.awt.Frame nullFrame;
438 protected FeatureSettings featureSettings = null;
440 private float heightScale = 1, widthScale = 1;
442 public void setFont(Font f)
445 if (nullFrame == null)
447 nullFrame = new java.awt.Frame();
448 nullFrame.addNotify();
451 java.awt.FontMetrics fm = nullFrame.getGraphics().getFontMetrics(font);
452 setCharHeight((int) (heightScale * fm.getHeight()));
453 charWidth = (int) (widthScale * fm.charWidth('M'));
457 Font f2 = new Font(f.getName(), Font.BOLD, f.getSize());
458 fm = nullFrame.getGraphics().getFontMetrics(f2);
459 charWidth = (int) (widthScale * (fm.stringWidth("MMMMMMMMMMM") / 10));
463 public Font getFont()
468 public int getCharWidth()
473 public void setCharHeight(int h)
478 public int getCharHeight()
483 public void setWrappedWidth(int w)
485 this.wrappedWidth = w;
488 public int getwrappedWidth()
493 public AlignmentI getAlignment()
498 public void setAlignment(AlignmentI align)
500 this.alignment = align;
503 public void setWrapAlignment(boolean state)
505 wrapAlignment = state;
508 public void setShowText(boolean state)
513 public void setRenderGaps(boolean state)
518 public boolean getColourText()
520 return showColourText;
523 public void setColourText(boolean state)
525 showColourText = state;
528 public void setShowBoxes(boolean state)
533 public boolean getWrapAlignment()
535 return wrapAlignment;
538 public boolean getShowText()
543 public boolean getShowBoxes()
548 public char getGapCharacter()
550 return getAlignment().getGapCharacter();
553 public void setGapCharacter(char gap)
555 if (getAlignment() != null)
557 getAlignment().setGapCharacter(gap);
561 public void setThreshold(int thresh)
566 public int getThreshold()
571 public void setIncrement(int inc)
576 public int getIncrement()
581 public void resetSeqLimits(int height)
583 setEndSeq(height / getCharHeight());
586 public void setCurrentTree(NJTree tree)
591 public NJTree getCurrentTree()
596 public void setColourAppliesToAllGroups(boolean b)
598 colourAppliesToAllGroups = b;
601 public boolean getColourAppliesToAllGroups()
603 return colourAppliesToAllGroups;
606 public boolean getShowJVSuffix()
611 public void setShowJVSuffix(boolean b)
616 public boolean getShowAnnotation()
618 return showAnnotation;
621 public void setShowAnnotation(boolean b)
626 public boolean getScaleAboveWrapped()
628 return scaleAboveWrapped;
631 public boolean getScaleLeftWrapped()
633 return scaleLeftWrapped;
636 public boolean getScaleRightWrapped()
638 return scaleRightWrapped;
641 public void setScaleAboveWrapped(boolean b)
643 scaleAboveWrapped = b;
646 public void setScaleLeftWrapped(boolean b)
648 scaleLeftWrapped = b;
651 public void setScaleRightWrapped(boolean b)
653 scaleRightWrapped = b;
656 public void setIgnoreGapsConsensus(boolean b)
658 ignoreGapsInConsensusCalculation = b;
659 updateConsensus(null);
660 if (globalColourScheme != null)
662 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
663 ignoreGapsInConsensusCalculation);
672 public boolean getShowHiddenMarkers()
674 return showHiddenMarkers;
677 public void setShowHiddenMarkers(boolean show)
679 showHiddenMarkers = show;
682 public Color getSequenceColour(SequenceI seq)
684 if (sequenceColours == null || !sequenceColours.containsKey(seq))
690 return (Color) sequenceColours.get(seq);
694 public void setSequenceColour(SequenceI seq, Color col)
696 if (sequenceColours == null)
698 sequenceColours = new Hashtable();
703 sequenceColours.remove(seq);
707 sequenceColours.put(seq, col);
711 boolean centreColumnLabels;
713 public boolean getCentreColumnLabels()
715 return centreColumnLabels;
718 public void updateSequenceIdColours()
720 Vector groups = alignment.getGroups();
721 for (int ig = 0, igSize = groups.size(); ig < igSize; ig++)
723 SequenceGroup sg = (SequenceGroup) groups.elementAt(ig);
724 if (sg.idColour != null)
726 Vector sqs = sg.getSequences(getHiddenRepSequences());
727 for (int s = 0, sSize = sqs.size(); s < sSize; s++)
729 this.setSequenceColour((SequenceI) sqs.elementAt(s), sg.idColour);
735 public boolean followHighlight = true;
737 public boolean getFollowHighlight()
739 return followHighlight;
742 public boolean followSelection = true;
745 * @return true if view selection should always follow the selections
746 * broadcast by other selection sources
748 public boolean getFollowSelection()
750 return followSelection;
752 public void sendSelection()
754 jalview.structure.StructureSelectionManager
755 .getStructureSelectionManager(applet).sendSelection(
756 new SequenceGroup(getSelectionGroup()),
757 new ColumnSelection(getColumnSelection()), this);
764 * synthesize a column selection if none exists so it covers the given
765 * selection group. if wholewidth is false, no column selection is made if the
766 * selection group covers the whole alignment width.
771 public void expandColSelection(SequenceGroup sg, boolean wholewidth)
775 && (sgs = sg.getStartRes()) >= 0
776 && sg.getStartRes() <= (sge = sg.getEndRes())
777 && (colSel == null || colSel.getSelected() == null || colSel
778 .getSelected().size() == 0))
780 if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
787 colSel = new ColumnSelection();
789 for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
791 colSel.addElement(cspos);
797 public boolean hasHiddenColumns()
799 return hasHiddenColumns;
802 public boolean isNormaliseSequenceLogo()
804 return normaliseSequenceLogo;
807 public void setNormaliseSequenceLogo(boolean state)
809 normaliseSequenceLogo = state;
814 * @return true if alignment characters should be displayed
816 public boolean isValidCharWidth()
818 return validCharWidth;