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;
54 boolean colourAppliesToAllGroups = true;
55 ColourSchemeI globalColourScheme = null;
56 boolean conservationColourSelected = false;
57 boolean abovePIDThreshold = false;
59 SequenceGroup selectionGroup;
65 Font font = new Font("SansSerif", Font.PLAIN, 10);
66 boolean validCharWidth = true;
69 ColumnSelection colSel = new ColumnSelection();
74 NJTree currentTree = null;
76 boolean scaleAboveWrapped = true;
77 boolean scaleLeftWrapped = true;
78 boolean scaleRightWrapped = true;
80 // The following vector holds the features which are
81 // currently visible, in the correct order or rendering
82 Hashtable featuresDisplayed;
84 boolean hasHiddenColumns = false;
85 boolean hasHiddenRows = false;
86 boolean showHiddenMarkers = true;
89 public Vector vconsensus;
90 AlignmentAnnotation consensus;
91 AlignmentAnnotation conservation;
92 AlignmentAnnotation quality;
94 boolean autocalculateConsensus = true;
96 public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
98 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);
100 boolean ignoreGapsInConsensusCalculation = false;
102 jalview.bin.JalviewLite applet;
106 public AlignViewport(AlignmentI al, JalviewLite applet)
108 this.applet = applet;
111 this.endRes = al.getWidth() - 1;
113 this.endSeq = al.getHeight() - 1;
116 if(System.getProperty("os.name").startsWith("Mac"))
121 String param = applet.getParameter("showFullId");
124 showJVSuffix = Boolean.valueOf(param).booleanValue();
127 param = applet.getParameter("showAnnotation");
130 showAnnotation = Boolean.valueOf(param).booleanValue();
133 param = applet.getParameter("showConservation");
136 showConservation = Boolean.valueOf(param).booleanValue();
139 param = applet.getParameter("showQuality");
142 showQuality = Boolean.valueOf(param).booleanValue();
145 param = applet.getParameter("showConsensus");
148 showConsensus = Boolean.valueOf(param).booleanValue();
151 // We must set conservation and consensus before setting colour,
152 // as Blosum and Clustal require this to be done
153 updateConservation();
159 String colour = applet.getParameter("defaultColour");
163 colour = applet.getParameter("userDefinedColour");
165 colour = "User Defined";
170 globalColourScheme = ColourSchemeProperty.getColour(alignment, colour);
171 if (globalColourScheme != null)
173 globalColourScheme.setConsensus(vconsensus);
177 if(applet.getParameter("userDefinedColour")!=null)
179 ((UserColourScheme)globalColourScheme).parseAppletParameter(
180 applet.getParameter("userDefinedColour"));
187 public void showSequenceFeatures(boolean b)
189 showSequenceFeatures = b;
192 public boolean getShowSequenceFeatures()
194 return showSequenceFeatures;
198 public void updateConservation()
200 if(alignment.isNucleotide())
203 Conservation cons = new jalview.analysis.Conservation("All",
204 jalview.schemes.ResidueProperties.propHash, 3,
205 alignment.getSequences(), 0,
206 alignment.getWidth() - 1);
208 cons.verdict(false, ConsPercGaps);
210 int alWidth = alignment.getWidth();
211 Annotation[] annotations = new Annotation[alWidth];
212 Annotation[] qannotations = new Annotation[alWidth];
213 String sequence = cons.getConsSequence().getSequence();
214 float minR, minG, minB, maxR, maxG, maxB;
220 maxB = 0f - minB; // scalable range for colouring both Conservation and Quality
223 float qmin = cons.qualityRange[0].floatValue();
224 float qmax = cons.qualityRange[1].floatValue();
226 for (int i = 0; i < alWidth; i++)
231 value = Integer.parseInt(sequence.charAt(i) + "");
235 if (sequence.charAt(i) == '*')
239 if (sequence.charAt(i) == '+')
244 float vprop = value - min;
247 annotations[i] = new Annotation(sequence.charAt(i) + "",
249 new Color(minR + maxR * vprop,
251 minB + maxB * vprop));
253 value = ( (Double) cons.quality.elementAt(i)).floatValue();
254 vprop = value - qmin;
256 qannotations[i] = new Annotation(" ",
257 String.valueOf(value), ' ', value,
259 Color(minR + maxR * vprop,
261 minB + maxB * vprop));
264 if (conservation == null)
266 conservation = new AlignmentAnnotation("Conservation",
267 "Conservation of total alignment less than " +
268 ConsPercGaps + "% gaps",
270 0f, // cons.qualityRange[0].floatValue(),
271 11f, // cons.qualityRange[1].floatValue()
272 AlignmentAnnotation.BAR_GRAPH);
273 if (showConservation)
275 alignment.addAnnotation(conservation);
277 quality = new AlignmentAnnotation("Quality",
278 "Alignment Quality based on Blosum62 scores",
280 cons.qualityRange[0].floatValue(),
281 cons.qualityRange[1].floatValue(),
282 AlignmentAnnotation.BAR_GRAPH);
285 alignment.addAnnotation(quality);
290 conservation.annotations = annotations;
291 quality.annotations = qannotations;
292 quality.graphMax = cons.qualityRange[1].floatValue();
297 public void updateConsensus()
299 Annotation[] annotations = new Annotation[alignment.getWidth()];
301 // this routine prevents vconsensus becoming a new object each time
302 // consenus is calculated. Important for speed of Blosum62
303 // and PID colouring of alignment
304 if (vconsensus == null)
306 vconsensus = alignment.getAAFrequency();
310 Vector temp = alignment.getAAFrequency();
311 vconsensus.removeAllElements();
312 Enumeration e = temp.elements();
313 while (e.hasMoreElements())
315 vconsensus.addElement(e.nextElement());
318 Hashtable hash = null;
319 for (int i = 0; i < alignment.getWidth(); i++)
321 hash = (Hashtable) vconsensus.elementAt(i);
323 if(ignoreGapsInConsensusCalculation)
324 value = ((Float)hash.get("pid_nogaps")).floatValue();
326 value = ((Float)hash.get("pid_gaps")).floatValue();
328 String maxRes = hash.get("maxResidue").toString();
329 String mouseOver = hash.get("maxResidue") + " ";
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(vconsensus);
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');
498 public Font getFont()
503 public int getCharWidth()
508 public void setCharHeight(int h)
513 public int getCharHeight()
518 public void setWrappedWidth(int w)
520 this.wrappedWidth = w;
523 public int getwrappedWidth()
528 public AlignmentI getAlignment()
533 public void setAlignment(AlignmentI align)
535 this.alignment = align;
538 public void setWrapAlignment(boolean state)
540 wrapAlignment = state;
543 public void setShowText(boolean state)
548 public void setRenderGaps(boolean state)
553 public boolean getColourText()
555 return showColourText;
558 public void setColourText(boolean state)
560 showColourText = state;
563 public void setShowBoxes(boolean state)
568 public boolean getWrapAlignment()
570 return wrapAlignment;
573 public boolean getShowText()
578 public boolean getShowBoxes()
583 public char getGapCharacter()
585 return getAlignment().getGapCharacter();
588 public void setGapCharacter(char gap)
590 if (getAlignment() != null)
592 getAlignment().setGapCharacter(gap);
596 public void setThreshold(int thresh)
601 public int getThreshold()
606 public void setIncrement(int inc)
611 public int getIncrement()
616 public void setHiddenColumns(ColumnSelection colsel)
618 this.colSel = colsel;
619 if(colSel.getHiddenColumns()!=null)
620 hasHiddenColumns = true;
623 public ColumnSelection getColumnSelection()
628 public void resetSeqLimits(int height)
630 setEndSeq(height / getCharHeight());
633 public void setCurrentTree(NJTree tree)
638 public NJTree getCurrentTree()
643 public void setColourAppliesToAllGroups(boolean b)
645 colourAppliesToAllGroups = b;
648 public boolean getColourAppliesToAllGroups()
650 return colourAppliesToAllGroups;
653 public boolean getShowJVSuffix()
658 public void setShowJVSuffix(boolean b)
663 public boolean getShowAnnotation()
665 return showAnnotation;
668 public void setShowAnnotation(boolean b)
673 public boolean getScaleAboveWrapped()
675 return scaleAboveWrapped;
678 public boolean getScaleLeftWrapped()
680 return scaleLeftWrapped;
683 public boolean getScaleRightWrapped()
685 return scaleRightWrapped;
688 public void setScaleAboveWrapped(boolean b)
690 scaleAboveWrapped = b;
693 public void setScaleLeftWrapped(boolean b)
695 scaleLeftWrapped = b;
698 public void setScaleRightWrapped(boolean b)
700 scaleRightWrapped = b;
703 public void setIgnoreGapsConsensus(boolean b)
705 ignoreGapsInConsensusCalculation = b;
707 if (globalColourScheme!=null)
709 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
710 ignoreGapsInConsensusCalculation);
716 * Property change listener for changes in alignment
718 * @param listener DOCUMENT ME!
720 public void addPropertyChangeListener(
721 java.beans.PropertyChangeListener listener)
723 changeSupport.addPropertyChangeListener(listener);
729 * @param listener DOCUMENT ME!
731 public void removePropertyChangeListener(
732 java.beans.PropertyChangeListener listener)
734 changeSupport.removePropertyChangeListener(listener);
738 * Property change listener for changes in alignment
740 * @param prop DOCUMENT ME!
741 * @param oldvalue DOCUMENT ME!
742 * @param newvalue DOCUMENT ME!
744 public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
746 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
751 public boolean getIgnoreGapsConsensus()
753 return ignoreGapsInConsensusCalculation;
755 public void hideSelectedColumns()
757 if (colSel.size() < 1)
760 colSel.hideSelectedColumns();
761 setSelectionGroup(null);
763 hasHiddenColumns = true;
766 public void invertColumnSelection()
769 for (int i = 0; i < alignment.getWidth(); i++)
773 if (colSel.contains(column))
774 colSel.removeElement(column);
776 colSel.addElement(column);
782 public void hideColumns(int start, int end)
785 colSel.hideColumns(start);
787 colSel.hideColumns(start, end);
789 hasHiddenColumns = true;
792 public void hideSequence(SequenceI seq)
796 alignment.getHiddenSequences().hideSequence(seq);
797 hasHiddenRows = true;
798 firePropertyChange("alignment", null, alignment.getSequences());
802 public void hideAllSelectedSeqs()
804 if (selectionGroup == null)
807 SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
809 for (int i = 0; i < seqs.length; i++)
811 alignment.getHiddenSequences().hideSequence(seqs[i]);
813 firePropertyChange("alignment", null, alignment.getSequences());
814 hasHiddenRows = true;
815 setSelectionGroup(null);
818 public void showColumn(int col)
820 colSel.revealHiddenColumns(col);
821 if(colSel.getHiddenColumns()==null)
822 hasHiddenColumns = false;
825 public void showAllHiddenColumns()
827 colSel.revealAllHiddenColumns();
828 hasHiddenColumns = false;
831 public void showAllHiddenSeqs()
833 if(alignment.getHiddenSequences().getSize()>0)
835 if(selectionGroup==null)
837 selectionGroup = new SequenceGroup();
838 selectionGroup.setEndRes(alignment.getWidth()-1);
840 Vector tmp = alignment.getHiddenSequences().showAll();
841 for(int t=0; t<tmp.size(); t++)
843 selectionGroup.addSequence(
844 (SequenceI)tmp.elementAt(t), false
847 firePropertyChange("alignment", null, alignment.getSequences());
848 hasHiddenRows = false;
852 public int adjustForHiddenSeqs(int alignmentIndex)
854 return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);
858 * This method returns the a new SequenceI [] with
859 * the selection sequence and start and end points adjusted
862 public SequenceI[] getSelectionAsNewSequence()
864 SequenceI[] sequences;
866 if (selectionGroup == null)
867 sequences = alignment.getSequencesArray();
869 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
875 * This method returns the visible alignment as text, as
876 * seen on the GUI, ie if columns are hidden they will not
877 * be returned in the result.
878 * Use this for calculating trees, PCA, redundancy etc on views
879 * which contain hidden columns.
882 public jalview.datamodel.CigarArray getViewAsCigars(boolean selectedRegionOnly)
884 CigarArray selection=null;
885 SequenceI [] seqs= null;
887 int start = 0, end = 0;
888 if(selectedRegionOnly && selectionGroup!=null)
890 iSize = selectionGroup.getSize(false);
891 seqs = selectionGroup.getSequencesInOrder(alignment);
892 start = selectionGroup.getStartRes();
893 end = selectionGroup.getEndRes(); // inclusive for start and end in SeqCigar constructor
897 iSize = alignment.getHeight();
898 seqs = alignment.getSequencesArray();
899 end = alignment.getWidth()-1;
901 SeqCigar[] selseqs = new SeqCigar[iSize];
902 for(i=0; i<iSize; i++)
904 selseqs[i] = new SeqCigar(seqs[i], start, end);
906 selection=new CigarArray(selseqs);
907 // now construct the CigarArray operations
908 if (hasHiddenColumns) {
909 Vector regions = colSel.getHiddenColumns();
911 int hideStart, hideEnd;
913 for (int j = 0; last<end & j < regions.size(); j++)
915 region = (int[]) regions.elementAt(j);
916 hideStart = region[0];
918 // edit hidden regions to selection range
933 if (hideStart>hideEnd)
939 selection.addOperation(CigarArray.M, hideStart-last);
940 selection.addOperation(CigarArray.D, 1+hideEnd-hideStart);
943 // Final match if necessary.
945 selection.addOperation(CigarArray.M, end-last);
947 selection.addOperation(CigarArray.M, end-start);
952 * return a compact representation of the current alignment selection to
953 * pass to an analysis function
954 * @param selectedOnly boolean true to just return the selected view
955 * @return AlignmentView
957 jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly) {
959 // this is here because the AlignmentView constructor modifies the CigarArray
960 // object. Refactoring of Cigar and alignment view representation should
961 // be done to remove redundancy.
962 CigarArray aligview = getViewAsCigars(selectedOnly);
964 return new AlignmentView(aligview);
968 * This method returns the visible alignment as text, as
969 * seen on the GUI, ie if columns are hidden they will not
970 * be returned in the result.
971 * Use this for calculating trees, PCA, redundancy etc on views
972 * which contain hidden columns.
975 public String [] getViewAsString(boolean selectedRegionOnly)
977 String [] selection = null;
978 SequenceI [] seqs= null;
980 int start = 0, end = 0;
981 if(selectedRegionOnly && selectionGroup!=null)
983 iSize = selectionGroup.getSize(false);
984 seqs = selectionGroup.getSequencesInOrder(alignment);
985 start = selectionGroup.getStartRes();
986 end = selectionGroup.getEndRes()+1;
990 iSize = alignment.getHeight();
991 seqs = alignment.getSequencesArray();
992 end = alignment.getWidth();
995 selection = new String[iSize];
998 for(i=0; i<iSize; i++)
1000 if (hasHiddenColumns)
1002 StringBuffer visibleSeq = new StringBuffer();
1003 Vector regions = colSel.getHiddenColumns();
1005 int blockStart = start, blockEnd=end;
1007 int hideStart, hideEnd;
1009 for (int j = 0; j < regions.size(); j++)
1011 region = (int[]) regions.elementAt(j);
1012 hideStart = region[0];
1013 hideEnd = region[1];
1015 if(hideStart < start)
1020 blockStart = Math.min(blockStart, hideEnd+1);
1021 blockEnd = Math.min(blockEnd, hideStart);
1023 if(blockStart>blockEnd)
1029 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
1031 blockStart = hideEnd+1;
1036 visibleSeq.append(seqs[i].getSequence(blockStart, end));
1038 selection[i] = visibleSeq.toString();
1042 selection[i] = seqs[i].getSequence(start, end);
1049 public boolean getShowHiddenMarkers()
1051 return showHiddenMarkers;
1054 public void setShowHiddenMarkers(boolean show)
1056 showHiddenMarkers = show;