2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 package jalview.appletgui;
26 import jalview.analysis.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
31 public class AlignViewport
40 boolean cursorMode = false;
42 boolean showJVSuffix = true;
43 boolean showText = true;
44 boolean showColourText = false;
45 boolean showBoxes = true;
46 boolean wrapAlignment = false;
47 boolean renderGaps = true;
48 boolean showSequenceFeatures = false;
49 boolean showAnnotation = true;
50 boolean showConservation = true;
51 boolean showQuality = true;
52 boolean showConsensus = true;
53 boolean upperCasebold = false;
55 boolean colourAppliesToAllGroups = true;
56 ColourSchemeI globalColourScheme = null;
57 boolean conservationColourSelected = false;
58 boolean abovePIDThreshold = false;
60 SequenceGroup selectionGroup;
66 Font font = new Font("SansSerif", Font.PLAIN, 10);
67 boolean validCharWidth = true;
70 ColumnSelection colSel = new ColumnSelection();
75 NJTree currentTree = null;
77 boolean scaleAboveWrapped = true;
78 boolean scaleLeftWrapped = true;
79 boolean scaleRightWrapped = true;
81 // The following vector holds the features which are
82 // currently visible, in the correct order or rendering
83 public Hashtable featuresDisplayed;
85 boolean hasHiddenColumns = false;
86 boolean hasHiddenRows = false;
87 boolean showHiddenMarkers = true;
90 public Hashtable [] hconsensus;
91 AlignmentAnnotation consensus;
92 AlignmentAnnotation conservation;
93 AlignmentAnnotation quality;
95 boolean autocalculateConsensus = true;
97 public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
99 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);
101 boolean ignoreGapsInConsensusCalculation = false;
103 jalview.bin.JalviewLite applet;
107 public AlignViewport(AlignmentI al, JalviewLite applet)
109 this.applet = applet;
112 this.endRes = al.getWidth() - 1;
114 this.endSeq = al.getHeight() - 1;
117 if(System.getProperty("os.name").startsWith("Mac"))
122 String param = applet.getParameter("showFullId");
125 showJVSuffix = Boolean.valueOf(param).booleanValue();
128 param = applet.getParameter("showAnnotation");
131 showAnnotation = Boolean.valueOf(param).booleanValue();
134 param = applet.getParameter("showConservation");
137 showConservation = Boolean.valueOf(param).booleanValue();
140 param = applet.getParameter("showQuality");
143 showQuality = Boolean.valueOf(param).booleanValue();
146 param = applet.getParameter("showConsensus");
149 showConsensus = Boolean.valueOf(param).booleanValue();
152 param = applet.getParameter("upperCase");
155 if(param.equalsIgnoreCase("bold"))
156 upperCasebold = true;
160 // We must set conservation and consensus before setting colour,
161 // as Blosum and Clustal require this to be done
162 updateConservation();
168 String colour = applet.getParameter("defaultColour");
172 colour = applet.getParameter("userDefinedColour");
174 colour = "User Defined";
179 globalColourScheme = ColourSchemeProperty.getColour(alignment, colour);
180 if (globalColourScheme != null)
182 globalColourScheme.setConsensus(hconsensus);
186 if(applet.getParameter("userDefinedColour")!=null)
188 ((UserColourScheme)globalColourScheme).parseAppletParameter(
189 applet.getParameter("userDefinedColour"));
196 public void showSequenceFeatures(boolean b)
198 showSequenceFeatures = b;
201 public boolean getShowSequenceFeatures()
203 return showSequenceFeatures;
207 public void updateConservation()
209 if(alignment.isNucleotide())
212 Conservation cons = new jalview.analysis.Conservation("All",
213 jalview.schemes.ResidueProperties.propHash, 3,
214 alignment.getSequences(), 0,
215 alignment.getWidth() - 1);
217 cons.verdict(false, ConsPercGaps);
219 int alWidth = alignment.getWidth();
220 Annotation[] annotations = new Annotation[alWidth];
221 Annotation[] qannotations = new Annotation[alWidth];
222 String sequence = cons.getConsSequence().getSequence();
223 float minR, minG, minB, maxR, maxG, maxB;
229 maxB = 0f - minB; // scalable range for colouring both Conservation and Quality
232 float qmin = cons.qualityRange[0].floatValue();
233 float qmax = cons.qualityRange[1].floatValue();
235 for (int i = 0; i < alWidth; i++)
240 value = Integer.parseInt(sequence.charAt(i) + "");
244 if (sequence.charAt(i) == '*')
248 if (sequence.charAt(i) == '+')
253 float vprop = value - min;
256 annotations[i] = new Annotation(sequence.charAt(i) + "",
258 new Color(minR + maxR * vprop,
260 minB + maxB * vprop));
262 value = ( (Double) cons.quality.elementAt(i)).floatValue();
263 vprop = value - qmin;
265 qannotations[i] = new Annotation(" ",
266 String.valueOf(value), ' ', value,
268 Color(minR + maxR * vprop,
270 minB + maxB * vprop));
273 if (conservation == null)
275 conservation = new AlignmentAnnotation("Conservation",
276 "Conservation of total alignment less than " +
277 ConsPercGaps + "% gaps",
279 0f, // cons.qualityRange[0].floatValue(),
280 11f, // cons.qualityRange[1].floatValue()
281 AlignmentAnnotation.BAR_GRAPH);
282 if (showConservation)
284 alignment.addAnnotation(conservation);
286 quality = new AlignmentAnnotation("Quality",
287 "Alignment Quality based on Blosum62 scores",
289 cons.qualityRange[0].floatValue(),
290 cons.qualityRange[1].floatValue(),
291 AlignmentAnnotation.BAR_GRAPH);
294 alignment.addAnnotation(quality);
299 conservation.annotations = annotations;
300 quality.annotations = qannotations;
301 quality.graphMax = cons.qualityRange[1].floatValue();
306 public void updateConsensus()
308 // this routine prevents vconsensus becoming a new object each time
309 // consenus is calculated. Important for speed of Blosum62
310 // and PID colouring of alignment
311 int aWidth = alignment.getWidth();
313 Annotation[] annotations = new Annotation[aWidth];
315 hconsensus = new Hashtable[aWidth];
316 AAFrequency.calculate(alignment.getSequencesArray(),
320 for (int i = 0; i < aWidth; i++)
323 if(ignoreGapsInConsensusCalculation)
324 value = ((Float)hconsensus[i].get(AAFrequency.PID_NOGAPS)).floatValue();
326 value = ((Float)hconsensus[i].get(AAFrequency.PID_GAPS)).floatValue();
328 String maxRes = hconsensus[i].get(AAFrequency.MAXRESIDUE).toString();
329 String mouseOver = maxRes;
330 if (maxRes.length() > 1)
332 mouseOver = "[" + maxRes + "] ";
337 mouseOver += (int) value + "%";
338 annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);
342 if (consensus == null)
344 consensus = new AlignmentAnnotation("Consensus",
345 "PID", annotations, 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
348 alignment.addAnnotation(consensus);
353 consensus.annotations = annotations;
356 if(globalColourScheme!=null)
357 globalColourScheme.setConsensus(hconsensus);
361 * get the consensus sequence as displayed under the PID consensus annotation row.
362 * @return consensus sequence as a new sequence object
365 * get the consensus sequence as displayed under the PID consensus annotation row.
366 * @return consensus sequence as a new sequence object
368 public SequenceI getConsensusSeq() {
373 StringBuffer seqs=new StringBuffer();
374 for (int i=0; i<consensus.annotations.length; i++) {
375 if (consensus.annotations[i]!=null) {
376 if (consensus.annotations[i].description.charAt(0) == '[')
377 seqs.append(consensus.annotations[i].description.charAt(1));
379 seqs.append(consensus.annotations[i].displayCharacter);
382 SequenceI sq = new Sequence("Consensus", seqs.toString());
383 sq.setDescription("Percentage Identity Consensus "+((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
386 public SequenceGroup getSelectionGroup()
388 return selectionGroup;
391 public void setSelectionGroup(SequenceGroup sg)
396 public boolean getConservationSelected()
398 return conservationColourSelected;
401 public void setConservationSelected(boolean b)
403 conservationColourSelected = b;
406 public boolean getAbovePIDThreshold()
408 return abovePIDThreshold;
411 public void setAbovePIDThreshold(boolean b)
413 abovePIDThreshold = b;
416 public int getStartRes()
421 public int getEndRes()
426 public int getStartSeq()
431 public void setGlobalColourScheme(ColourSchemeI cs)
433 globalColourScheme = cs;
436 public ColourSchemeI getGlobalColourScheme()
438 return globalColourScheme;
441 public void setStartRes(int res)
446 public void setStartSeq(int seq)
451 public void setEndRes(int res)
453 if (res > alignment.getWidth() - 1)
455 // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));
456 res = alignment.getWidth() - 1;
465 public void setEndSeq(int seq)
467 if (seq > alignment.getHeight())
469 seq = alignment.getHeight();
478 public int getEndSeq()
483 java.awt.Frame nullFrame;
484 public void setFont(Font f)
487 if(nullFrame == null)
489 nullFrame = new java.awt.Frame();
490 nullFrame.addNotify();
493 java.awt.FontMetrics fm = nullFrame.getGraphics().getFontMetrics(font);
494 setCharHeight(fm.getHeight());
495 charWidth = fm.charWidth('M');
499 Font f2 = new Font(f.getName(), Font.BOLD, f.getSize());
500 fm = nullFrame.getGraphics().getFontMetrics(f2);
501 charWidth = fm.stringWidth("MMMMMMMMMMM") / 10;
505 public Font getFont()
510 public int getCharWidth()
515 public void setCharHeight(int h)
520 public int getCharHeight()
525 public void setWrappedWidth(int w)
527 this.wrappedWidth = w;
530 public int getwrappedWidth()
535 public AlignmentI getAlignment()
540 public void setAlignment(AlignmentI align)
542 this.alignment = align;
545 public void setWrapAlignment(boolean state)
547 wrapAlignment = state;
550 public void setShowText(boolean state)
555 public void setRenderGaps(boolean state)
560 public boolean getColourText()
562 return showColourText;
565 public void setColourText(boolean state)
567 showColourText = state;
570 public void setShowBoxes(boolean state)
575 public boolean getWrapAlignment()
577 return wrapAlignment;
580 public boolean getShowText()
585 public boolean getShowBoxes()
590 public char getGapCharacter()
592 return getAlignment().getGapCharacter();
595 public void setGapCharacter(char gap)
597 if (getAlignment() != null)
599 getAlignment().setGapCharacter(gap);
603 public void setThreshold(int thresh)
608 public int getThreshold()
613 public void setIncrement(int inc)
618 public int getIncrement()
623 public void setHiddenColumns(ColumnSelection colsel)
625 this.colSel = colsel;
626 if(colSel.getHiddenColumns()!=null)
627 hasHiddenColumns = true;
630 public ColumnSelection getColumnSelection()
635 public void resetSeqLimits(int height)
637 setEndSeq(height / getCharHeight());
640 public void setCurrentTree(NJTree tree)
645 public NJTree getCurrentTree()
650 public void setColourAppliesToAllGroups(boolean b)
652 colourAppliesToAllGroups = b;
655 public boolean getColourAppliesToAllGroups()
657 return colourAppliesToAllGroups;
660 public boolean getShowJVSuffix()
665 public void setShowJVSuffix(boolean b)
670 public boolean getShowAnnotation()
672 return showAnnotation;
675 public void setShowAnnotation(boolean b)
680 public boolean getScaleAboveWrapped()
682 return scaleAboveWrapped;
685 public boolean getScaleLeftWrapped()
687 return scaleLeftWrapped;
690 public boolean getScaleRightWrapped()
692 return scaleRightWrapped;
695 public void setScaleAboveWrapped(boolean b)
697 scaleAboveWrapped = b;
700 public void setScaleLeftWrapped(boolean b)
702 scaleLeftWrapped = b;
705 public void setScaleRightWrapped(boolean b)
707 scaleRightWrapped = b;
710 public void setIgnoreGapsConsensus(boolean b)
712 ignoreGapsInConsensusCalculation = b;
714 if (globalColourScheme!=null)
716 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
717 ignoreGapsInConsensusCalculation);
723 * Property change listener for changes in alignment
725 * @param listener DOCUMENT ME!
727 public void addPropertyChangeListener(
728 java.beans.PropertyChangeListener listener)
730 changeSupport.addPropertyChangeListener(listener);
736 * @param listener DOCUMENT ME!
738 public void removePropertyChangeListener(
739 java.beans.PropertyChangeListener listener)
741 changeSupport.removePropertyChangeListener(listener);
745 * Property change listener for changes in alignment
747 * @param prop DOCUMENT ME!
748 * @param oldvalue DOCUMENT ME!
749 * @param newvalue DOCUMENT ME!
751 public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
753 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
758 public boolean getIgnoreGapsConsensus()
760 return ignoreGapsInConsensusCalculation;
762 public void hideSelectedColumns()
764 if (colSel.size() < 1)
767 colSel.hideSelectedColumns();
768 setSelectionGroup(null);
770 hasHiddenColumns = true;
773 public void invertColumnSelection()
776 for (int i = 0; i < alignment.getWidth(); i++)
780 if (colSel.contains(column))
781 colSel.removeElement(column);
783 colSel.addElement(column);
789 public void hideColumns(int start, int end)
792 colSel.hideColumns(start);
794 colSel.hideColumns(start, end);
796 hasHiddenColumns = true;
799 public void hideAllSelectedSeqs()
801 if (selectionGroup == null)
804 SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
808 setSelectionGroup(null);
811 public void hideSequence(SequenceI [] seq)
815 for (int i = 0; i < seq.length; i++)
816 alignment.getHiddenSequences().hideSequence(seq[i]);
818 hasHiddenRows = true;
819 firePropertyChange("alignment", null, alignment.getSequences());
823 public void showColumn(int col)
825 colSel.revealHiddenColumns(col);
826 if(colSel.getHiddenColumns()==null)
827 hasHiddenColumns = false;
830 public void showAllHiddenColumns()
832 colSel.revealAllHiddenColumns();
833 hasHiddenColumns = false;
836 public void showAllHiddenSeqs()
838 if(alignment.getHiddenSequences().getSize()>0)
840 if(selectionGroup==null)
842 selectionGroup = new SequenceGroup();
843 selectionGroup.setEndRes(alignment.getWidth()-1);
845 Vector tmp = alignment.getHiddenSequences().showAll();
846 for(int t=0; t<tmp.size(); t++)
848 selectionGroup.addSequence(
849 (SequenceI)tmp.elementAt(t), false
852 firePropertyChange("alignment", null, alignment.getSequences());
853 hasHiddenRows = false;
857 public int adjustForHiddenSeqs(int alignmentIndex)
859 return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);
863 * This method returns the a new SequenceI [] with
864 * the selection sequence and start and end points adjusted
867 public SequenceI[] getSelectionAsNewSequence()
869 SequenceI[] sequences;
871 if (selectionGroup == null)
872 sequences = alignment.getSequencesArray();
874 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
880 * This method returns the visible alignment as text, as
881 * seen on the GUI, ie if columns are hidden they will not
882 * be returned in the result.
883 * Use this for calculating trees, PCA, redundancy etc on views
884 * which contain hidden columns.
887 public jalview.datamodel.CigarArray getViewAsCigars(boolean selectedRegionOnly)
889 CigarArray selection=null;
890 SequenceI [] seqs= null;
892 int start = 0, end = 0;
893 if(selectedRegionOnly && selectionGroup!=null)
895 iSize = selectionGroup.getSize(false);
896 seqs = selectionGroup.getSequencesInOrder(alignment);
897 start = selectionGroup.getStartRes();
898 end = selectionGroup.getEndRes(); // inclusive for start and end in SeqCigar constructor
902 iSize = alignment.getHeight();
903 seqs = alignment.getSequencesArray();
904 end = alignment.getWidth()-1;
906 SeqCigar[] selseqs = new SeqCigar[iSize];
907 for(i=0; i<iSize; i++)
909 selseqs[i] = new SeqCigar(seqs[i], start, end);
911 selection=new CigarArray(selseqs);
912 // now construct the CigarArray operations
913 if (hasHiddenColumns) {
914 Vector regions = colSel.getHiddenColumns();
916 int hideStart, hideEnd;
918 for (int j = 0; last<end & j < regions.size(); j++)
920 region = (int[]) regions.elementAt(j);
921 hideStart = region[0];
923 // edit hidden regions to selection range
938 if (hideStart>hideEnd)
944 selection.addOperation(CigarArray.M, hideStart-last);
945 selection.addOperation(CigarArray.D, 1+hideEnd-hideStart);
948 // Final match if necessary.
950 selection.addOperation(CigarArray.M, end-last+1);
952 selection.addOperation(CigarArray.M, end-start+1);
957 * return a compact representation of the current alignment selection to
958 * pass to an analysis function
959 * @param selectedOnly boolean true to just return the selected view
960 * @return AlignmentView
962 jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly) {
964 // this is here because the AlignmentView constructor modifies the CigarArray
965 // object. Refactoring of Cigar and alignment view representation should
966 // be done to remove redundancy.
967 CigarArray aligview = getViewAsCigars(selectedOnly);
968 if (aligview!=null) {
969 return new AlignmentView(aligview,
970 (selectedOnly && selectionGroup!=null) ? selectionGroup.getStartRes() : 0);
975 * This method returns the visible alignment as text, as
976 * seen on the GUI, ie if columns are hidden they will not
977 * be returned in the result.
978 * Use this for calculating trees, PCA, redundancy etc on views
979 * which contain hidden columns.
982 public String [] getViewAsString(boolean selectedRegionOnly)
984 String [] selection = null;
985 SequenceI [] seqs= null;
987 int start = 0, end = 0;
988 if(selectedRegionOnly && selectionGroup!=null)
990 iSize = selectionGroup.getSize(false);
991 seqs = selectionGroup.getSequencesInOrder(alignment);
992 start = selectionGroup.getStartRes();
993 end = selectionGroup.getEndRes()+1;
997 iSize = alignment.getHeight();
998 seqs = alignment.getSequencesArray();
999 end = alignment.getWidth();
1002 selection = new String[iSize];
1005 for(i=0; i<iSize; i++)
1007 if (hasHiddenColumns)
1009 StringBuffer visibleSeq = new StringBuffer();
1010 Vector regions = colSel.getHiddenColumns();
1012 int blockStart = start, blockEnd=end;
1014 int hideStart, hideEnd;
1016 for (int j = 0; j < regions.size(); j++)
1018 region = (int[]) regions.elementAt(j);
1019 hideStart = region[0];
1020 hideEnd = region[1];
1022 if(hideStart < start)
1027 blockStart = Math.min(blockStart, hideEnd+1);
1028 blockEnd = Math.min(blockEnd, hideStart);
1030 if(blockStart>blockEnd)
1036 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
1038 blockStart = hideEnd+1;
1043 visibleSeq.append(seqs[i].getSequence(blockStart, end));
1045 selection[i] = visibleSeq.toString();
1049 selection[i] = seqs[i].getSequence(start, end);
1056 public boolean getShowHiddenMarkers()
1058 return showHiddenMarkers;
1061 public void setShowHiddenMarkers(boolean show)
1063 showHiddenMarkers = show;