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
19 package jalview.gui;
\r
21 import jalview.analysis.*;
\r
23 import jalview.bin.*;
\r
25 import jalview.datamodel.*;
\r
27 import jalview.schemes.*;
\r
38 * @version $Revision$
\r
40 public class AlignViewport
\r
46 boolean showJVSuffix = true;
\r
47 boolean showText = true;
\r
48 boolean showColourText = false;
\r
49 boolean showBoxes = true;
\r
50 boolean wrapAlignment = false;
\r
51 boolean renderGaps = true;
\r
52 boolean showSequenceFeatures = false;
\r
53 boolean showAnnotation = true;
\r
54 boolean showConservation = true;
\r
55 boolean showQuality = true;
\r
56 boolean showIdentity = true;
\r
57 boolean colourAppliesToAllGroups = true;
\r
58 ColourSchemeI globalColourScheme = null;
\r
59 boolean conservationColourSelected = false;
\r
60 boolean abovePIDThreshold = false;
\r
61 SequenceGroup selectionGroup;
\r
64 boolean validCharWidth;
\r
67 AlignmentI alignment;
\r
68 ColumnSelection colSel = new ColumnSelection();
\r
71 NJTree currentTree = null;
\r
72 boolean scaleAboveWrapped = false;
\r
73 boolean scaleLeftWrapped = true;
\r
74 boolean scaleRightWrapped = true;
\r
75 boolean hasHiddenColumns = false;
\r
76 boolean hasHiddenRows = false;
\r
78 boolean cursorMode = false;
\r
80 // The following vector holds the features which are
\r
81 // currently visible, in the correct order or rendering
\r
82 Hashtable featuresDisplayed = null;
\r
85 /** DOCUMENT ME!! */
\r
86 public Vector vconsensus;
\r
87 AlignmentAnnotation consensus;
\r
88 AlignmentAnnotation conservation;
\r
89 AlignmentAnnotation quality;
\r
90 boolean autoCalculateConsensus = true;
\r
92 /** DOCUMENT ME!! */
\r
93 public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
\r
95 // JBPNote Prolly only need this in the applet version.
\r
96 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);
\r
98 boolean ignoreGapsInConsensusCalculation = false;
\r
100 boolean isDataset = false;
\r
102 boolean antiAlias = false;
\r
104 boolean padGaps = false;
\r
107 public AlignViewport(AlignmentI al, boolean dataset)
\r
109 isDataset = dataset;
\r
114 * Creates a new AlignViewport object.
\r
116 * @param al DOCUMENT ME!
\r
118 public AlignViewport(AlignmentI al)
\r
127 this.endRes = alignment.getWidth() - 1;
\r
129 this.endSeq = alignment.getHeight() - 1;
\r
131 antiAlias = Cache.getDefault("ANTI_ALIAS", false);
\r
133 showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
\r
134 showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
\r
135 showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
\r
137 showQuality = Cache.getDefault("SHOW_QUALITY", true);
\r
138 showIdentity = Cache.getDefault("SHOW_IDENTITY", true);
\r
140 autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
\r
142 padGaps = Cache.getDefault("PAD_GAPS", false);
\r
144 String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
\r
145 String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "") ;
\r
146 String fontSize = Cache.getDefault("FONT_SIZE", "10");
\r
150 if (fontStyle.equals("bold"))
\r
154 else if (fontStyle.equals("italic"))
\r
159 setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
\r
162 alignment.setGapCharacter( Cache.getDefault("GAP_SYMBOL", "-").charAt(0) );
\r
165 // We must set conservation and consensus before setting colour,
\r
166 // as Blosum and Clustal require this to be done
\r
167 if(vconsensus==null && !isDataset)
\r
169 updateConservation();
\r
173 if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
\r
175 globalColourScheme = ColourSchemeProperty.getColour(alignment,
\r
176 jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
\r
178 if (globalColourScheme instanceof UserColourScheme)
\r
180 globalColourScheme = UserDefinedColours.loadDefaultColours();
\r
181 ((UserColourScheme)globalColourScheme).setThreshold(0, getIgnoreGapsConsensus());
\r
184 if (globalColourScheme != null)
\r
186 globalColourScheme.setConsensus(vconsensus);
\r
196 * @param b DOCUMENT ME!
\r
198 public void setShowSequenceFeatures(boolean b)
\r
200 showSequenceFeatures = b;
\r
203 public boolean getShowSequenceFeatures()
\r
205 return showSequenceFeatures;
\r
211 public void updateConservation()
\r
213 if(alignment.isNucleotide())
\r
217 Conservation cons = new jalview.analysis.Conservation("All",
\r
218 jalview.schemes.ResidueProperties.propHash, 3,
\r
219 alignment.getSequences(), 0, alignment.getWidth() - 1);
\r
221 cons.verdict(false, ConsPercGaps);
\r
222 cons.findQuality();
\r
224 int alWidth = alignment.getWidth();
\r
225 Annotation[] annotations = new Annotation[alWidth];
\r
226 Annotation[] qannotations = new Annotation[alWidth];
\r
227 String sequence = cons.getConsSequence().getSequence();
\r
237 maxR = 1.0f - minR;
\r
238 maxG = 0.9f - minG;
\r
239 maxB = 0f - minB; // scalable range for colouring both Conservation and Quality
\r
243 float qmin = cons.qualityRange[0].floatValue();
\r
244 float qmax = cons.qualityRange[1].floatValue();
\r
246 for (int i = 0; i < alWidth; i++)
\r
252 value = Integer.parseInt(sequence.charAt(i) + "");
\r
254 catch (Exception ex)
\r
256 if (sequence.charAt(i) == '*')
\r
261 if (sequence.charAt(i) == '+')
\r
267 float vprop = value - min;
\r
269 annotations[i] = new Annotation(sequence.charAt(i) + "",
\r
270 String.valueOf(value), ' ', value,
\r
271 new Color(minR + (maxR * vprop),
\r
272 minG + (maxG * vprop),
\r
273 minB + (maxB * vprop)));
\r
276 value = ( (Double) cons.quality.get(i)).floatValue();
\r
277 vprop = value - qmin;
\r
279 qannotations[i] = new Annotation(" ", String.valueOf(value), ' ',
\r
281 new Color(minR + (maxR * vprop),
\r
282 minG + (maxG * vprop),
\r
283 minB + (maxB * vprop)));
\r
286 if (conservation == null)
\r
288 conservation = new AlignmentAnnotation("Conservation",
\r
289 "Conservation of total alignment less than " +
\r
290 ConsPercGaps + "% gaps",
\r
291 annotations, 0f, // cons.qualityRange[0].floatValue(),
\r
292 11f, // cons.qualityRange[1].floatValue()
\r
293 AlignmentAnnotation.BAR_GRAPH);
\r
295 if (showConservation)
\r
297 alignment.addAnnotation(conservation);
\r
300 quality = new AlignmentAnnotation("Quality",
\r
301 "Alignment Quality based on Blosum62 scores",
\r
303 cons.qualityRange[0].floatValue(),
\r
304 cons.qualityRange[1].floatValue(),
\r
305 AlignmentAnnotation.BAR_GRAPH);
\r
309 alignment.addAnnotation(quality);
\r
314 conservation.annotations = annotations;
\r
315 quality.annotations = qannotations;
\r
316 quality.graphMax = cons.qualityRange[1].floatValue();
\r
319 catch (OutOfMemoryError error)
\r
321 javax.swing.SwingUtilities.invokeLater(new Runnable()
\r
325 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
326 "Out of memory calculating conservation!!"
\r
328 "\nSee help files for increasing Java Virtual Machine memory."
\r
330 javax.swing.JOptionPane.WARNING_MESSAGE);
\r
334 System.out.println("Conservation calculation: " + error);
\r
343 public void updateConsensus()
\r
346 Annotation[] annotations = new Annotation[alignment.getWidth()];
\r
348 // this routine prevents vconsensus becoming a new object each time
\r
349 // consenus is calculated. Important for speed of Blosum62
\r
350 // and PID colouring of alignment
\r
351 if (vconsensus == null)
\r
353 vconsensus = alignment.getAAFrequency();
\r
357 Vector temp = alignment.getAAFrequency();
\r
358 vconsensus.clear();
\r
360 Enumeration e = temp.elements();
\r
362 while (e.hasMoreElements())
\r
364 vconsensus.add(e.nextElement());
\r
368 Hashtable hash = null;
\r
370 for (int i = 0; i < alignment.getWidth(); i++)
\r
372 hash = (Hashtable) vconsensus.elementAt(i);
\r
375 if (ignoreGapsInConsensusCalculation)
\r
376 value = ( (Float) hash.get("pid_nogaps")).floatValue();
\r
378 value = ( (Float) hash.get("pid_gaps")).floatValue();
\r
380 String maxRes = hash.get("maxResidue").toString();
\r
381 String mouseOver = hash.get("maxResidue") + " ";
\r
383 if (maxRes.length() > 1)
\r
385 mouseOver = "[" + maxRes + "] ";
\r
389 mouseOver += ( (int) value + "%");
\r
390 annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);
\r
393 if (consensus == null)
\r
395 consensus = new AlignmentAnnotation("Consensus", "PID",
\r
396 annotations, 0f, 100f,AlignmentAnnotation.BAR_GRAPH);
\r
400 alignment.addAnnotation(consensus);
\r
405 consensus.annotations = annotations;
\r
408 if (globalColourScheme != null)
\r
409 globalColourScheme.setConsensus(vconsensus);
\r
411 }catch(OutOfMemoryError error)
\r
413 javax.swing.SwingUtilities.invokeLater(new Runnable()
\r
417 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
418 "Out of memory calc45ulating consensus!!"
\r
420 "\nSee help files for increasing Java Virtual Machine memory."
\r
422 javax.swing.JOptionPane.WARNING_MESSAGE);
\r
427 System.out.println("Consensus calculation: " + error);
\r
436 * @return DOCUMENT ME!
\r
438 public SequenceGroup getSelectionGroup()
\r
440 return selectionGroup;
\r
446 * @param sg DOCUMENT ME!
\r
448 public void setSelectionGroup(SequenceGroup sg)
\r
450 selectionGroup = sg;
\r
456 * @return DOCUMENT ME!
\r
458 public boolean getConservationSelected()
\r
460 return conservationColourSelected;
\r
466 * @param b DOCUMENT ME!
\r
468 public void setConservationSelected(boolean b)
\r
470 conservationColourSelected = b;
\r
476 * @return DOCUMENT ME!
\r
478 public boolean getAbovePIDThreshold()
\r
480 return abovePIDThreshold;
\r
486 * @param b DOCUMENT ME!
\r
488 public void setAbovePIDThreshold(boolean b)
\r
490 abovePIDThreshold = b;
\r
496 * @return DOCUMENT ME!
\r
498 public int getStartRes()
\r
506 * @return DOCUMENT ME!
\r
508 public int getEndRes()
\r
516 * @return DOCUMENT ME!
\r
518 public int getStartSeq()
\r
526 * @param cs DOCUMENT ME!
\r
528 public void setGlobalColourScheme(ColourSchemeI cs)
\r
530 globalColourScheme = cs;
\r
536 * @return DOCUMENT ME!
\r
538 public ColourSchemeI getGlobalColourScheme()
\r
540 return globalColourScheme;
\r
546 * @param res DOCUMENT ME!
\r
548 public void setStartRes(int res)
\r
550 this.startRes = res;
\r
556 * @param seq DOCUMENT ME!
\r
558 public void setStartSeq(int seq)
\r
560 this.startSeq = seq;
\r
566 * @param res DOCUMENT ME!
\r
568 public void setEndRes(int res)
\r
570 if (res > (alignment.getWidth() - 1))
\r
572 // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));
\r
573 res = alignment.getWidth() - 1;
\r
587 * @param seq DOCUMENT ME!
\r
589 public void setEndSeq(int seq)
\r
591 if (seq > alignment.getHeight())
\r
593 seq = alignment.getHeight();
\r
607 * @return DOCUMENT ME!
\r
609 public int getEndSeq()
\r
617 * @param f DOCUMENT ME!
\r
619 public void setFont(Font f)
\r
623 Container c = new Container();
\r
625 java.awt.FontMetrics fm = c.getFontMetrics(font);
\r
626 setCharHeight(fm.getHeight());
\r
627 setCharWidth(fm.charWidth('M'));
\r
628 validCharWidth = true;
\r
634 * @return DOCUMENT ME!
\r
636 public Font getFont()
\r
644 * @param w DOCUMENT ME!
\r
646 public void setCharWidth(int w)
\r
648 this.charWidth = w;
\r
654 * @return DOCUMENT ME!
\r
656 public int getCharWidth()
\r
664 * @param h DOCUMENT ME!
\r
666 public void setCharHeight(int h)
\r
668 this.charHeight = h;
\r
674 * @return DOCUMENT ME!
\r
676 public int getCharHeight()
\r
684 * @param w DOCUMENT ME!
\r
686 public void setWrappedWidth(int w)
\r
688 this.wrappedWidth = w;
\r
694 * @return DOCUMENT ME!
\r
696 public int getWrappedWidth()
\r
698 return wrappedWidth;
\r
705 * @return DOCUMENT ME!
\r
707 public AlignmentI getAlignment()
\r
715 * @param align DOCUMENT ME!
\r
717 public void setAlignment(AlignmentI align)
\r
719 this.alignment = align;
\r
725 * @param state DOCUMENT ME!
\r
727 public void setWrapAlignment(boolean state)
\r
729 wrapAlignment = state;
\r
735 * @param state DOCUMENT ME!
\r
737 public void setShowText(boolean state)
\r
745 * @param state DOCUMENT ME!
\r
747 public void setRenderGaps(boolean state)
\r
749 renderGaps = state;
\r
755 * @return DOCUMENT ME!
\r
757 public boolean getColourText()
\r
759 return showColourText;
\r
765 * @param state DOCUMENT ME!
\r
767 public void setColourText(boolean state)
\r
769 showColourText = state;
\r
775 * @param state DOCUMENT ME!
\r
777 public void setShowBoxes(boolean state)
\r
785 * @return DOCUMENT ME!
\r
787 public boolean getWrapAlignment()
\r
789 return wrapAlignment;
\r
795 * @return DOCUMENT ME!
\r
797 public boolean getShowText()
\r
805 * @return DOCUMENT ME!
\r
807 public boolean getShowBoxes()
\r
815 * @return DOCUMENT ME!
\r
817 public char getGapCharacter()
\r
819 return getAlignment().getGapCharacter();
\r
825 * @param gap DOCUMENT ME!
\r
827 public void setGapCharacter(char gap)
\r
829 if (getAlignment() != null)
\r
831 getAlignment().setGapCharacter(gap);
\r
838 * @param thresh DOCUMENT ME!
\r
840 public void setThreshold(int thresh)
\r
842 threshold = thresh;
\r
848 * @return DOCUMENT ME!
\r
850 public int getThreshold()
\r
858 * @param inc DOCUMENT ME!
\r
860 public void setIncrement(int inc)
\r
868 * @return DOCUMENT ME!
\r
870 public int getIncrement()
\r
878 * @param y DOCUMENT ME!
\r
880 * @return DOCUMENT ME!
\r
882 public int getIndex(int y)
\r
885 int starty = getStartSeq();
\r
886 int endy = getEndSeq();
\r
888 for (int i = starty; i <= endy; i++)
\r
890 if ((i < alignment.getHeight()) &&
\r
891 (alignment.getSequenceAt(i) != null))
\r
893 int y2 = y1 + getCharHeight();
\r
895 if ((y >= y1) && (y <= y2))
\r
914 * @return DOCUMENT ME!
\r
916 public ColumnSelection getColumnSelection()
\r
925 * @param tree DOCUMENT ME!
\r
927 public void setCurrentTree(NJTree tree)
\r
929 currentTree = tree;
\r
935 * @return DOCUMENT ME!
\r
937 public NJTree getCurrentTree()
\r
939 return currentTree;
\r
945 * @param b DOCUMENT ME!
\r
947 public void setColourAppliesToAllGroups(boolean b)
\r
949 colourAppliesToAllGroups = b;
\r
955 * @return DOCUMENT ME!
\r
957 public boolean getColourAppliesToAllGroups()
\r
959 return colourAppliesToAllGroups;
\r
965 * @return DOCUMENT ME!
\r
967 public boolean getShowJVSuffix()
\r
969 return showJVSuffix;
\r
975 * @param b DOCUMENT ME!
\r
977 public void setShowJVSuffix(boolean b)
\r
986 * @return DOCUMENT ME!
\r
988 public boolean getShowAnnotation()
\r
990 return showAnnotation;
\r
996 * @param b DOCUMENT ME!
\r
998 public void setShowAnnotation(boolean b)
\r
1000 showAnnotation = b;
\r
1006 * @return DOCUMENT ME!
\r
1008 public boolean getScaleAboveWrapped()
\r
1010 return scaleAboveWrapped;
\r
1016 * @return DOCUMENT ME!
\r
1018 public boolean getScaleLeftWrapped()
\r
1020 return scaleLeftWrapped;
\r
1026 * @return DOCUMENT ME!
\r
1028 public boolean getScaleRightWrapped()
\r
1030 return scaleRightWrapped;
\r
1036 * @param b DOCUMENT ME!
\r
1038 public void setScaleAboveWrapped(boolean b)
\r
1040 scaleAboveWrapped = b;
\r
1046 * @param b DOCUMENT ME!
\r
1048 public void setScaleLeftWrapped(boolean b)
\r
1050 scaleLeftWrapped = b;
\r
1056 * @param b DOCUMENT ME!
\r
1058 public void setScaleRightWrapped(boolean b)
\r
1060 scaleRightWrapped = b;
\r
1064 * Property change listener for changes in alignment
\r
1066 * @param listener DOCUMENT ME!
\r
1068 public void addPropertyChangeListener(
\r
1069 java.beans.PropertyChangeListener listener)
\r
1071 changeSupport.addPropertyChangeListener(listener);
\r
1077 * @param listener DOCUMENT ME!
\r
1079 public void removePropertyChangeListener(
\r
1080 java.beans.PropertyChangeListener listener)
\r
1082 changeSupport.removePropertyChangeListener(listener);
\r
1086 * Property change listener for changes in alignment
\r
1088 * @param prop DOCUMENT ME!
\r
1089 * @param oldvalue DOCUMENT ME!
\r
1090 * @param newvalue DOCUMENT ME!
\r
1092 public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
\r
1094 changeSupport.firePropertyChange(prop, oldvalue, newvalue);
\r
1097 public void setIgnoreGapsConsensus(boolean b)
\r
1099 ignoreGapsInConsensusCalculation = b;
\r
1100 updateConsensus();
\r
1101 if(globalColourScheme!=null)
\r
1103 globalColourScheme.setThreshold(globalColourScheme.getThreshold(), ignoreGapsInConsensusCalculation);
\r
1107 public boolean getIgnoreGapsConsensus()
\r
1109 return ignoreGapsInConsensusCalculation;
\r
1112 public void setDataset(boolean b)
\r
1117 public boolean isDataset()
\r
1122 public void hideSequence(SequenceI seq)
\r
1124 alignment.getHiddenSequences().hideSequence(seq);
\r
1125 hasHiddenRows = true;
\r
1128 public void showSequence(int index)
\r
1130 alignment.getHiddenSequences().showSequence(index);
\r
1132 if(alignment.getHiddenSequences().getSize()<1)
\r
1133 hasHiddenRows = false;
\r
1136 public void showAllHiddenSeqs()
\r
1138 alignment.getHiddenSequences().showAll();
\r
1139 hasHiddenRows = false;
\r
1142 public int adjustForHiddenSeqs(int alignmentIndex)
\r
1144 return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);
\r
1148 * This method returns the a new SequenceI [] with
\r
1149 * the selection sequence and start and end points adjusted
\r
1150 * @return String[]
\r
1152 public SequenceI[] getSelectionAsNewSequence()
\r
1154 SequenceI[] sequences;
\r
1156 if (selectionGroup == null)
\r
1157 sequences = alignment.getSequencesArray();
\r
1159 sequences = selectionGroup.getSelectionAsNewSequences(alignment);
\r
1166 * This method returns the visible selected area as text, as
\r
1167 * seen on the GUI, ie if columns are hidden they will not
\r
1168 * be returned in the result.
\r
1169 * Use this for calculating trees, PCA, redundancy etc on views
\r
1170 * which contain hidden columns.
\r
1171 * @return String[]
\r
1173 public String [] getSelectionAsString()
\r
1175 String [] selection = null;
\r
1176 SequenceI [] seqs= null;
\r
1178 int start = 0, end = 0;
\r
1179 if(selectionGroup!=null)
\r
1181 iSize = selectionGroup.getSize(false);
\r
1182 seqs = selectionGroup.getSequencesInOrder(alignment);
\r
1183 start = selectionGroup.getStartRes();
\r
1184 end = selectionGroup.getEndRes()+1;
\r
1188 iSize = alignment.getHeight();
\r
1189 seqs = alignment.getSequencesArray();
\r
1190 end = alignment.getWidth();
\r
1193 selection = new String[iSize];
\r
1196 for(i=0; i<iSize; i++)
\r
1198 if (hasHiddenColumns)
\r
1200 StringBuffer visibleSeq = new StringBuffer();
\r
1201 Vector regions = colSel.getHiddenColumns();
\r
1203 int blockStart = start, blockEnd=end;
\r
1205 int hideStart, hideEnd;
\r
1207 for (int j = 0; j < regions.size(); j++)
\r
1209 region = (int[]) regions.elementAt(j);
\r
1210 hideStart = region[0];
\r
1211 hideEnd = region[1];
\r
1213 if(hideStart < start)
\r
1218 blockStart = Math.min(blockStart, hideEnd+1);
\r
1219 blockEnd = Math.min(blockEnd, hideStart);
\r
1221 if(blockStart>blockEnd)
\r
1227 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
\r
1229 blockStart = hideEnd+1;
\r
1233 if(end>blockStart)
\r
1234 visibleSeq.append(seqs[i].getSequence(blockStart, end));
\r
1236 selection[i] = visibleSeq.toString();
\r
1240 selection[i] = seqs[i].getSequence(start, end);
\r
1243 // System.out.println(seqs[i].getName()+"\t"+ selection[i]);
\r