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
26 import jalview.analysis.*;
\r
27 import jalview.bin.*;
\r
28 import jalview.datamodel.*;
\r
29 import jalview.schemes.*;
\r
31 public class AlignViewport
\r
40 boolean cursorMode = false;
\r
42 boolean showJVSuffix = true;
\r
43 boolean showText = true;
\r
44 boolean showColourText = false;
\r
45 boolean showBoxes = true;
\r
46 boolean wrapAlignment = false;
\r
47 boolean renderGaps = true;
\r
48 boolean showSequenceFeatures = false;
\r
49 boolean showAnnotation = true;
\r
50 boolean showConservation = true;
\r
51 boolean showQuality = true;
\r
52 boolean showConsensus = true;
\r
54 boolean colourAppliesToAllGroups = true;
\r
55 ColourSchemeI globalColourScheme = null;
\r
56 boolean conservationColourSelected = false;
\r
57 boolean abovePIDThreshold = false;
\r
59 SequenceGroup selectionGroup;
\r
65 Font font = new Font("SansSerif", Font.PLAIN, 10);
\r
66 boolean validCharWidth = true;
\r
67 AlignmentI alignment;
\r
69 ColumnSelection colSel = new ColumnSelection();
\r
74 NJTree currentTree = null;
\r
76 boolean scaleAboveWrapped = true;
\r
77 boolean scaleLeftWrapped = true;
\r
78 boolean scaleRightWrapped = true;
\r
80 // The following vector holds the features which are
\r
81 // currently visible, in the correct order or rendering
\r
82 Hashtable featuresDisplayed;
\r
84 boolean hasHiddenColumns = false;
\r
85 boolean hasHiddenRows = false;
\r
86 boolean showHiddenMarkers = true;
\r
89 public Vector vconsensus;
\r
90 AlignmentAnnotation consensus;
\r
91 AlignmentAnnotation conservation;
\r
92 AlignmentAnnotation quality;
\r
94 boolean autocalculateConsensus = true;
\r
96 public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
\r
98 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);
\r
100 boolean ignoreGapsInConsensusCalculation = false;
\r
102 jalview.bin.JalviewLite applet;
\r
104 public AlignViewport(AlignmentI al, JalviewLite applet)
\r
106 this.applet = applet;
\r
109 this.endRes = al.getWidth() - 1;
\r
111 this.endSeq = al.getHeight() - 1;
\r
114 if (applet != null)
\r
116 String param = applet.getParameter("showFullId");
\r
119 showJVSuffix = Boolean.valueOf(param).booleanValue();
\r
122 param = applet.getParameter("showAnnotation");
\r
125 showAnnotation = Boolean.valueOf(param).booleanValue();
\r
128 param = applet.getParameter("showConservation");
\r
131 showConservation = Boolean.valueOf(param).booleanValue();
\r
134 param = applet.getParameter("showQuality");
\r
137 showQuality = Boolean.valueOf(param).booleanValue();
\r
140 param = applet.getParameter("showConsensus");
\r
143 showConsensus = Boolean.valueOf(param).booleanValue();
\r
146 // We must set conservation and consensus before setting colour,
\r
147 // as Blosum and Clustal require this to be done
\r
148 updateConservation();
\r
152 if (applet != null)
\r
154 String colour = applet.getParameter("defaultColour");
\r
158 colour = applet.getParameter("userDefinedColour");
\r
160 colour = "User Defined";
\r
165 globalColourScheme = ColourSchemeProperty.getColour(alignment, colour);
\r
166 if (globalColourScheme != null)
\r
168 globalColourScheme.setConsensus(vconsensus);
\r
172 if(applet.getParameter("userDefinedColour")!=null)
\r
174 ((UserColourScheme)globalColourScheme).parseAppletParameter(
\r
175 applet.getParameter("userDefinedColour"));
\r
182 public void showSequenceFeatures(boolean b)
\r
184 showSequenceFeatures = b;
\r
187 public boolean getShowSequenceFeatures()
\r
189 return showSequenceFeatures;
\r
193 public void updateConservation()
\r
195 if(alignment.isNucleotide())
\r
198 Conservation cons = new jalview.analysis.Conservation("All",
\r
199 jalview.schemes.ResidueProperties.propHash, 3,
\r
200 alignment.getSequences(), 0,
\r
201 alignment.getWidth() - 1);
\r
203 cons.verdict(false, ConsPercGaps);
\r
204 cons.findQuality();
\r
205 int alWidth = alignment.getWidth();
\r
206 Annotation[] annotations = new Annotation[alWidth];
\r
207 Annotation[] qannotations = new Annotation[alWidth];
\r
208 String sequence = cons.getConsSequence().getSequence();
\r
209 float minR, minG, minB, maxR, maxG, maxB;
\r
213 maxR = 1.0f - minR;
\r
214 maxG = 0.9f - minG;
\r
215 maxB = 0f - minB; // scalable range for colouring both Conservation and Quality
\r
218 float qmin = cons.qualityRange[0].floatValue();
\r
219 float qmax = cons.qualityRange[1].floatValue();
\r
221 for (int i = 0; i < alWidth; i++)
\r
226 value = Integer.parseInt(sequence.charAt(i) + "");
\r
228 catch (Exception ex)
\r
230 if (sequence.charAt(i) == '*')
\r
234 if (sequence.charAt(i) == '+')
\r
239 float vprop = value - min;
\r
242 annotations[i] = new Annotation(sequence.charAt(i) + "",
\r
244 new Color(minR + maxR * vprop,
\r
245 minG + maxG * vprop,
\r
246 minB + maxB * vprop));
\r
248 value = ( (Double) cons.quality.elementAt(i)).floatValue();
\r
249 vprop = value - qmin;
\r
251 qannotations[i] = new Annotation(" ",
\r
252 String.valueOf(value), ' ', value,
\r
254 Color(minR + maxR * vprop,
\r
255 minG + maxG * vprop,
\r
256 minB + maxB * vprop));
\r
259 if (conservation == null)
\r
261 conservation = new AlignmentAnnotation("Conservation",
\r
262 "Conservation of total alignment less than " +
\r
263 ConsPercGaps + "% gaps",
\r
265 0f, // cons.qualityRange[0].floatValue(),
\r
266 11f, // cons.qualityRange[1].floatValue()
\r
267 AlignmentAnnotation.BAR_GRAPH);
\r
268 if (showConservation)
\r
270 alignment.addAnnotation(conservation);
\r
272 quality = new AlignmentAnnotation("Quality",
\r
273 "Alignment Quality based on Blosum62 scores",
\r
275 cons.qualityRange[0].floatValue(),
\r
276 cons.qualityRange[1].floatValue(),
\r
277 AlignmentAnnotation.BAR_GRAPH);
\r
280 alignment.addAnnotation(quality);
\r
285 conservation.annotations = annotations;
\r
286 quality.annotations = qannotations;
\r
287 quality.graphMax = cons.qualityRange[1].floatValue();
\r
292 public void updateConsensus()
\r
294 Annotation[] annotations = new Annotation[alignment.getWidth()];
\r
296 // this routine prevents vconsensus becoming a new object each time
\r
297 // consenus is calculated. Important for speed of Blosum62
\r
298 // and PID colouring of alignment
\r
299 if (vconsensus == null)
\r
301 vconsensus = alignment.getAAFrequency();
\r
305 Vector temp = alignment.getAAFrequency();
\r
306 vconsensus.removeAllElements();
\r
307 Enumeration e = temp.elements();
\r
308 while (e.hasMoreElements())
\r
310 vconsensus.addElement(e.nextElement());
\r
313 Hashtable hash = null;
\r
314 for (int i = 0; i < alignment.getWidth(); i++)
\r
316 hash = (Hashtable) vconsensus.elementAt(i);
\r
318 if(ignoreGapsInConsensusCalculation)
\r
319 value = ((Float)hash.get("pid_nogaps")).floatValue();
\r
321 value = ((Float)hash.get("pid_gaps")).floatValue();
\r
323 String maxRes = hash.get("maxResidue").toString();
\r
324 String mouseOver = hash.get("maxResidue") + " ";
\r
325 if (maxRes.length() > 1)
\r
327 mouseOver = "[" + maxRes + "] ";
\r
332 mouseOver += (int) value + "%";
\r
333 annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);
\r
337 if (consensus == null)
\r
339 consensus = new AlignmentAnnotation("Consensus",
\r
340 "PID", annotations, 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
\r
343 alignment.addAnnotation(consensus);
\r
348 consensus.annotations = annotations;
\r
351 if(globalColourScheme!=null)
\r
352 globalColourScheme.setConsensus(vconsensus);
\r
356 public SequenceGroup getSelectionGroup()
\r
358 return selectionGroup;
\r
361 public void setSelectionGroup(SequenceGroup sg)
\r
363 selectionGroup = sg;
\r
366 public boolean getConservationSelected()
\r
368 return conservationColourSelected;
\r
371 public void setConservationSelected(boolean b)
\r
373 conservationColourSelected = b;
\r
376 public boolean getAbovePIDThreshold()
\r
378 return abovePIDThreshold;
\r
381 public void setAbovePIDThreshold(boolean b)
\r
383 abovePIDThreshold = b;
\r
386 public int getStartRes()
\r
391 public int getEndRes()
\r
396 public int getStartSeq()
\r
401 public void setGlobalColourScheme(ColourSchemeI cs)
\r
403 globalColourScheme = cs;
\r
406 public ColourSchemeI getGlobalColourScheme()
\r
408 return globalColourScheme;
\r
411 public void setStartRes(int res)
\r
413 this.startRes = res;
\r
416 public void setStartSeq(int seq)
\r
418 this.startSeq = seq;
\r
421 public void setEndRes(int res)
\r
423 if (res > alignment.getWidth() - 1)
\r
425 // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));
\r
426 res = alignment.getWidth() - 1;
\r
435 public void setEndSeq(int seq)
\r
437 if (seq > alignment.getHeight())
\r
439 seq = alignment.getHeight();
\r
448 public int getEndSeq()
\r
453 java.awt.Frame nullFrame;
\r
454 public void setFont(Font f)
\r
457 if(nullFrame == null)
\r
459 nullFrame = new java.awt.Frame();
\r
460 nullFrame.addNotify();
\r
463 java.awt.FontMetrics fm = nullFrame.getGraphics().getFontMetrics(font);
\r
464 setCharHeight(fm.getHeight());
\r
465 setCharWidth(fm.charWidth('M'));
\r
468 public Font getFont()
\r
473 public void setCharWidth(int w)
\r
475 this.charWidth = w;
\r
478 public int getCharWidth()
\r
483 public void setCharHeight(int h)
\r
485 this.charHeight = h;
\r
488 public int getCharHeight()
\r
493 public void setWrappedWidth(int w)
\r
495 this.wrappedWidth = w;
\r
498 public int getwrappedWidth()
\r
500 return wrappedWidth;
\r
503 public AlignmentI getAlignment()
\r
508 public void setAlignment(AlignmentI align)
\r
510 this.alignment = align;
\r
513 public void setWrapAlignment(boolean state)
\r
515 wrapAlignment = state;
\r
518 public void setShowText(boolean state)
\r
523 public void setRenderGaps(boolean state)
\r
525 renderGaps = state;
\r
528 public boolean getColourText()
\r
530 return showColourText;
\r
533 public void setColourText(boolean state)
\r
535 showColourText = state;
\r
538 public void setShowBoxes(boolean state)
\r
543 public boolean getWrapAlignment()
\r
545 return wrapAlignment;
\r
548 public boolean getShowText()
\r
553 public boolean getShowBoxes()
\r
558 public char getGapCharacter()
\r
560 return getAlignment().getGapCharacter();
\r
563 public void setGapCharacter(char gap)
\r
565 if (getAlignment() != null)
\r
567 getAlignment().setGapCharacter(gap);
\r
571 public void setThreshold(int thresh)
\r
573 threshold = thresh;
\r
576 public int getThreshold()
\r
581 public void setIncrement(int inc)
\r
586 public int getIncrement()
\r
591 public void setHiddenColumns(ColumnSelection colsel)
\r
593 this.colSel = colsel;
\r
594 if(colSel.getHiddenColumns()!=null)
\r
595 hasHiddenColumns = true;
\r
598 public ColumnSelection getColumnSelection()
\r
603 public void resetSeqLimits(int height)
\r
605 setEndSeq(height / getCharHeight());
\r
608 public void setCurrentTree(NJTree tree)
\r
610 currentTree = tree;
\r
613 public NJTree getCurrentTree()
\r
615 return currentTree;
\r
618 public void setColourAppliesToAllGroups(boolean b)
\r
620 colourAppliesToAllGroups = b;
\r
623 public boolean getColourAppliesToAllGroups()
\r
625 return colourAppliesToAllGroups;
\r
628 public boolean getShowJVSuffix()
\r
630 return showJVSuffix;
\r
633 public void setShowJVSuffix(boolean b)
\r
638 public boolean getShowAnnotation()
\r
640 return showAnnotation;
\r
643 public void setShowAnnotation(boolean b)
\r
645 showAnnotation = b;
\r
648 public boolean getScaleAboveWrapped()
\r
650 return scaleAboveWrapped;
\r
653 public boolean getScaleLeftWrapped()
\r
655 return scaleLeftWrapped;
\r
658 public boolean getScaleRightWrapped()
\r
660 return scaleRightWrapped;
\r
663 public void setScaleAboveWrapped(boolean b)
\r
665 scaleAboveWrapped = b;
\r
668 public void setScaleLeftWrapped(boolean b)
\r
670 scaleLeftWrapped = b;
\r
673 public void setScaleRightWrapped(boolean b)
\r
675 scaleRightWrapped = b;
\r
678 public void setIgnoreGapsConsensus(boolean b)
\r
680 ignoreGapsInConsensusCalculation = b;
\r
682 if (globalColourScheme!=null)
\r
684 globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
\r
685 ignoreGapsInConsensusCalculation);
\r
691 * Property change listener for changes in alignment
\r
693 * @param listener DOCUMENT ME!
\r
695 public void addPropertyChangeListener(
\r
696 java.beans.PropertyChangeListener listener)
\r
698 changeSupport.addPropertyChangeListener(listener);
\r
704 * @param listener DOCUMENT ME!
\r
706 public void removePropertyChangeListener(
\r
707 java.beans.PropertyChangeListener listener)
\r
709 changeSupport.removePropertyChangeListener(listener);
\r
713 * Property change listener for changes in alignment
\r
715 * @param prop DOCUMENT ME!
\r
716 * @param oldvalue DOCUMENT ME!
\r
717 * @param newvalue DOCUMENT ME!
\r
719 public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
\r
721 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
\r
726 public boolean getIgnoreGapsConsensus()
\r
728 return ignoreGapsInConsensusCalculation;
\r
730 public void hideSelectedColumns()
\r
732 if (colSel.size() < 1)
\r
735 colSel.hideSelectedColumns();
\r
736 setSelectionGroup(null);
\r
738 hasHiddenColumns = true;
\r
741 public void invertColumnSelection()
\r
744 for (int i = 0; i < alignment.getWidth(); i++)
\r
748 if (colSel.contains(column))
\r
749 colSel.removeElement(column);
\r
751 colSel.addElement(column);
\r
757 public void hideColumns(int start, int end)
\r
760 colSel.hideColumns(start);
\r
762 colSel.hideColumns(start, end);
\r
763 setSelectionGroup(null);
\r
764 hasHiddenColumns = true;
\r
767 public void hideSequence(SequenceI seq)
\r
771 alignment.getHiddenSequences().hideSequence(seq);
\r
772 hasHiddenRows = true;
\r
776 public void showSequence(int index)
\r
778 alignment.getHiddenSequences().showSequence(index);
\r
780 if(alignment.getHiddenSequences().getSize()<1)
\r
781 hasHiddenRows = false;
\r
784 public void showColumn(int col)
\r
786 colSel.revealHiddenColumns(col);
\r
787 if(colSel.getHiddenColumns()==null)
\r
788 hasHiddenColumns = false;
\r
791 public void showAllHiddenColumns()
\r
793 colSel.revealAllHiddenColumns();
\r
794 hasHiddenColumns = false;
\r
797 public void showAllHiddenSeqs()
\r
799 if(alignment.getHiddenSequences().getSize()>0)
\r
801 alignment.getHiddenSequences().showAll();
\r
802 hasHiddenRows = false;
\r
806 public int adjustForHiddenSeqs(int alignmentIndex)
\r
808 return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);
\r
812 * This method returns the a new SequenceI [] with
\r
813 * the selection sequence and start and end points adjusted
\r
816 public SequenceI[] getSelectionAsNewSequence()
\r
818 SequenceI[] sequences;
\r
820 if (selectionGroup == null)
\r
821 sequences = alignment.getSequencesArray();
\r
823 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
\r
829 * This method returns the visible alignment as text, as
\r
830 * seen on the GUI, ie if columns are hidden they will not
\r
831 * be returned in the result.
\r
832 * Use this for calculating trees, PCA, redundancy etc on views
\r
833 * which contain hidden columns.
\r
836 public jalview.datamodel.CigarArray getViewAsCigars(boolean selectedRegionOnly)
\r
838 CigarArray selection=null;
\r
839 SequenceI [] seqs= null;
\r
841 int start = 0, end = 0;
\r
842 if(selectedRegionOnly && selectionGroup!=null)
\r
844 iSize = selectionGroup.getSize(false);
\r
845 seqs = selectionGroup.getSequencesInOrder(alignment);
\r
846 start = selectionGroup.getStartRes();
\r
847 end = selectionGroup.getEndRes(); // inclusive for start and end in SeqCigar constructor
\r
851 iSize = alignment.getHeight();
\r
852 seqs = alignment.getSequencesArray();
\r
853 end = alignment.getWidth()-1;
\r
855 SeqCigar[] selseqs = new SeqCigar[iSize];
\r
856 for(i=0; i<iSize; i++)
\r
858 selseqs[i] = new SeqCigar(seqs[i], start, end);
\r
860 selection=new CigarArray(selseqs);
\r
861 // now construct the CigarArray operations
\r
862 if (hasHiddenColumns) {
\r
863 Vector regions = colSel.getHiddenColumns();
\r
865 int hideStart, hideEnd;
\r
867 for (int j = 0; last<end & j < regions.size(); j++)
\r
869 region = (int[]) regions.elementAt(j);
\r
870 hideStart = region[0];
\r
871 hideEnd = region[1];
\r
872 // edit hidden regions to selection range
\r
873 if(hideStart<last) {
\r
874 if (hideEnd > last)
\r
887 if (hideStart>hideEnd)
\r
890 * form operations...
\r
892 if (last<hideStart)
\r
893 selection.addOperation(CigarArray.M, hideStart-last);
\r
894 selection.addOperation(CigarArray.D, 1+hideEnd-hideStart);
\r
897 // Final match if necessary.
\r
899 selection.addOperation(CigarArray.M, end-last);
\r
901 selection.addOperation(CigarArray.M, end-start);
\r
906 * return a compact representation of the current alignment selection to
\r
907 * pass to an analysis function
\r
908 * @param selectedOnly boolean true to just return the selected view
\r
909 * @return AlignmentView
\r
911 jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly) {
\r
913 // this is here because the AlignmentView constructor modifies the CigarArray
\r
914 // object. Refactoring of Cigar and alignment view representation should
\r
915 // be done to remove redundancy.
\r
916 CigarArray aligview = getViewAsCigars(selectedOnly);
\r
917 if (aligview!=null)
\r
918 return new AlignmentView(aligview);
\r
922 * This method returns the visible alignment as text, as
\r
923 * seen on the GUI, ie if columns are hidden they will not
\r
924 * be returned in the result.
\r
925 * Use this for calculating trees, PCA, redundancy etc on views
\r
926 * which contain hidden columns.
\r
929 public String [] getViewAsString(boolean selectedRegionOnly)
\r
931 String [] selection = null;
\r
932 SequenceI [] seqs= null;
\r
934 int start = 0, end = 0;
\r
935 if(selectedRegionOnly && selectionGroup!=null)
\r
937 iSize = selectionGroup.getSize(false);
\r
938 seqs = selectionGroup.getSequencesInOrder(alignment);
\r
939 start = selectionGroup.getStartRes();
\r
940 end = selectionGroup.getEndRes()+1;
\r
944 iSize = alignment.getHeight();
\r
945 seqs = alignment.getSequencesArray();
\r
946 end = alignment.getWidth();
\r
949 selection = new String[iSize];
\r
952 for(i=0; i<iSize; i++)
\r
954 if (hasHiddenColumns)
\r
956 StringBuffer visibleSeq = new StringBuffer();
\r
957 Vector regions = colSel.getHiddenColumns();
\r
959 int blockStart = start, blockEnd=end;
\r
961 int hideStart, hideEnd;
\r
963 for (int j = 0; j < regions.size(); j++)
\r
965 region = (int[]) regions.elementAt(j);
\r
966 hideStart = region[0];
\r
967 hideEnd = region[1];
\r
969 if(hideStart < start)
\r
974 blockStart = Math.min(blockStart, hideEnd+1);
\r
975 blockEnd = Math.min(blockEnd, hideStart);
\r
977 if(blockStart>blockEnd)
\r
983 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
\r
985 blockStart = hideEnd+1;
\r
990 visibleSeq.append(seqs[i].getSequence(blockStart, end));
\r
992 selection[i] = visibleSeq.toString();
\r
996 selection[i] = seqs[i].getSequence(start, end);
\r
1003 public boolean getShowHiddenMarkers()
\r
1005 return showHiddenMarkers;
\r
1008 public void setShowHiddenMarkers(boolean show)
\r
1010 showHiddenMarkers = show;
\r