Viewport now looks at preferences when loading up
[jalview.git] / src / jalview / gui / AlignViewport.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import jalview.analysis.*;\r
5 import jalview.analysis.NJTree;\r
6 import jalview.datamodel.*;\r
7 import jalview.schemes.*;\r
8 import java.util.*;\r
9 import jalview.bin.Cache;\r
10 \r
11 public class AlignViewport\r
12 {\r
13   int startRes;\r
14   int endRes;\r
15 \r
16   int startSeq;\r
17   int endSeq;\r
18 \r
19   boolean showFullId = false;\r
20   boolean showText=true;\r
21   boolean showColourText=false;\r
22   boolean showBoxes=true;\r
23   boolean wrapAlignment=false;\r
24   boolean renderGaps = true;\r
25   boolean showSequenceFeatures = false;\r
26   boolean showAnnotation = true;\r
27   boolean showConservation = true;\r
28   boolean showQuality = true;\r
29   boolean showIdentity = true;\r
30 \r
31   boolean colourAppliesToAllGroups = true;\r
32   ColourSchemeI globalColourScheme = null;\r
33   boolean conservationColourSelected = false;\r
34   boolean abovePIDThreshold = false;\r
35 \r
36   SequenceGroup selectionGroup = new SequenceGroup();\r
37 \r
38   RendererI renderer = new SequenceRenderer(this);\r
39 \r
40   int             charHeight;\r
41   int             charWidth;\r
42   int             chunkWidth;\r
43   int             chunkHeight;\r
44 \r
45   Font            font = new Font("SansSerif",Font.PLAIN,10);\r
46   AlignmentI      alignment;\r
47 \r
48   ColumnSelection colSel = new ColumnSelection();\r
49 \r
50   int threshold;\r
51   int increment;\r
52 \r
53   NJTree currentTree = null;\r
54 \r
55   boolean scaleAboveWrapped = false;\r
56   boolean scaleLeftWrapped  = true;\r
57   boolean scaleRightWrapped = true;\r
58 \r
59 \r
60   public AlignViewport(AlignmentI al)\r
61   {\r
62     setAlignment(al);\r
63     this.startRes = 0;\r
64     this.endRes = al.getWidth()-1;\r
65     this.startSeq = 0;\r
66     this.endSeq = al.getHeight()-1;\r
67 \r
68     updateFromPreferences();\r
69 \r
70   }\r
71 \r
72   public void updateFromPreferences()\r
73   {\r
74     String string = Cache.getProperty("SHOW_FULL_ID");\r
75     if(string!=null)\r
76       showFullId = Boolean.valueOf(string).booleanValue();\r
77 \r
78     string = Cache.getProperty("SHOW_ANNOTATIONS");\r
79         if(string!=null)\r
80       showAnnotation = Boolean.valueOf(string).booleanValue();\r
81 \r
82     string = Cache.getProperty("SHOW_CONSERVATION");\r
83         if(string!=null)\r
84       showConservation = Boolean.valueOf(string).booleanValue();\r
85     string = Cache.getProperty("SHOW_QUALITY");\r
86         if(string!=null)\r
87       showQuality = Boolean.valueOf(string).booleanValue();\r
88     string = Cache.getProperty("SHOW_IDENTITY");\r
89         if(string!=null)\r
90       showIdentity = Boolean.valueOf(string).booleanValue();\r
91 \r
92     string = Cache.getProperty("SHOW_FULL_ID");\r
93       if(string!=null)\r
94         showFullId = Boolean.valueOf(string).booleanValue();\r
95 \r
96   String fontName = Cache.getProperty("FONT_NAME");\r
97   String fontStyle= Cache.getProperty("FONT_STYLE");\r
98   String fontSize = Cache.getProperty("FONT_SIZE");\r
99   if(fontName!=null && fontStyle!=null && fontSize!=null)\r
100       setFont( new Font(fontName,Integer.parseInt(fontStyle),Integer.parseInt(fontSize)) );\r
101   else\r
102       setFont( font );\r
103 \r
104     string = Cache.getProperty("GAP_SYMBOL");\r
105     if(string!=null)\r
106       alignment.setGapCharacter( string.charAt(0) );\r
107 \r
108 \r
109     // We must set conservation and consensus before setting colour,\r
110     // as Blosum and Clustal require this to be done\r
111     updateConservation();\r
112     updateConsensus();\r
113     string = Cache.getProperty("DEFAULT_COLOUR");\r
114     if(string!=null)\r
115     {\r
116       globalColourScheme = ColourSchemeProperty.getColour(alignment, string);\r
117       if(globalColourScheme!=null)\r
118         globalColourScheme.setConsensus( vconsensus );\r
119    }\r
120 \r
121  }\r
122 \r
123  public void showSequenceFeatures(boolean b)\r
124  {\r
125    showSequenceFeatures = b;\r
126  }\r
127 \r
128   public Vector vconsensus;\r
129   AlignmentAnnotation consensus;\r
130   AlignmentAnnotation conservation;\r
131   AlignmentAnnotation quality;\r
132 \r
133   public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!\r
134 \r
135   public void updateConservation()\r
136   {\r
137     Conservation cons = new jalview.analysis.Conservation("All",\r
138         jalview.schemes.ResidueProperties.propHash, 3,\r
139         alignment.getSequences(), 0,\r
140         alignment.getWidth()-1);\r
141     cons.calculate();\r
142     cons.verdict(false, ConsPercGaps);\r
143     cons.findQuality();\r
144     int alWidth = alignment.getWidth();\r
145     Annotation [] annotations = new Annotation[alWidth];\r
146     Annotation [] qannotations = new Annotation[alWidth];\r
147     String sequence = cons.getConsSequence().getSequence();\r
148     float minR,minG,minB, maxR,maxG,maxB;\r
149     minR = 0.3f;\r
150     minG = 0.0f;\r
151     minB = 0f;\r
152     maxR = 1.0f-minR; maxG=0.9f-minG; maxB=0f-minB; // scalable range for colouring both Conservation and Quality\r
153     float min = 0f;\r
154     float max = 11f;\r
155     float qmin = cons.qualityRange[0].floatValue();\r
156     float qmax = cons.qualityRange[1].floatValue();\r
157 \r
158     for (int i = 0; i < alWidth; i++)\r
159     {\r
160       float value = 0;\r
161       try\r
162         {\r
163           value = Integer.parseInt(sequence.charAt(i) + "");\r
164         }\r
165       catch (Exception ex)\r
166         {\r
167           if (sequence.charAt(i) == '*') value = 11;\r
168           if (sequence.charAt(i) == '+') value = 10;\r
169         }\r
170       float vprop = value-min;\r
171       vprop/=max;\r
172       annotations[i] = new Annotation(sequence.charAt(i) + "",\r
173                                       "Conservation graph", ' ', value, new Color(minR+maxR*vprop, minG+maxG*vprop, minB+maxB*vprop));\r
174       // Quality calc\r
175       value = ((Double) cons.quality.get(i)).floatValue();\r
176       vprop = value - qmin;\r
177       vprop/=qmax;\r
178       qannotations[i] = new Annotation(" ",\r
179                                       String.valueOf(value), ' ', value, new Color(minR+maxR*vprop, minG+maxG*vprop, minB+maxB*vprop));\r
180     }\r
181 \r
182     if(conservation==null)\r
183     {\r
184       conservation = new AlignmentAnnotation("Conservation",\r
185                                              "Conservation of total alignment less than "+ConsPercGaps+"% gaps",\r
186                                              annotations,\r
187                                              0f, // cons.qualityRange[0].floatValue(),\r
188                                              11f, // cons.qualityRange[1].floatValue()\r
189                                              1);\r
190       if(showConservation)\r
191       alignment.addAnnotation(conservation);\r
192       quality = new AlignmentAnnotation("Quality",\r
193                                         "Alignment Quality based on Blosum62 scores",\r
194                                         qannotations,\r
195                                         cons.qualityRange[0].floatValue(),\r
196                                         cons.qualityRange[1].floatValue(),\r
197                                         1);\r
198       if(showQuality)\r
199         alignment.addAnnotation(quality);\r
200     }\r
201     else {\r
202       conservation.annotations = annotations;\r
203       quality.annotations = qannotations;\r
204       quality.graphMax = cons.qualityRange[1].floatValue();\r
205     }\r
206 \r
207 \r
208   }\r
209 \r
210   public void updateConsensus()\r
211   {\r
212     Annotation [] annotations = new Annotation[alignment.getWidth()];\r
213 \r
214     // this routine prevents vconsensus becoming a new object each time\r
215     // consenus is calculated. Important for speed of Blosum62\r
216     // and PID colouring of alignment\r
217     if(vconsensus == null)\r
218         vconsensus = alignment.getAAFrequency();\r
219     else\r
220     {\r
221         Vector temp = alignment.getAAFrequency();\r
222         vconsensus.clear();\r
223         Enumeration e = temp.elements();\r
224         while(e.hasMoreElements())\r
225         {\r
226           vconsensus.add(e.nextElement());\r
227         }\r
228     }\r
229     Hashtable hash = null;\r
230     for (int i = 0; i<alignment.getWidth(); i++)\r
231     {\r
232         hash = (Hashtable) vconsensus.elementAt(i);\r
233         float value = Float.parseFloat(hash.get("maxCount").toString());\r
234         value /= Float.parseFloat(hash.get("size").toString());\r
235 \r
236         value *= 100;\r
237         String maxRes = hash.get("maxResidue")+" ";\r
238         String mouseOver = hash.get("maxResidue")+" ";\r
239         if(maxRes.length()>2)\r
240         {\r
241           mouseOver = "["+maxRes+"] ";\r
242           maxRes = "+ ";\r
243         }\r
244 \r
245         mouseOver += (int)value+"%";\r
246         annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
247 \r
248     }\r
249 \r
250      if(consensus==null)\r
251      {\r
252        consensus = new AlignmentAnnotation("% Identity",\r
253                                            "PID", annotations, 0f, 100f, 1);\r
254        if(showIdentity)\r
255          alignment.addAnnotation(consensus);\r
256      }\r
257      else\r
258        consensus.annotations = annotations;\r
259 \r
260   }\r
261 \r
262 \r
263   public SequenceGroup getSelectionGroup()\r
264   {\r
265     return selectionGroup;\r
266   }\r
267 \r
268   public void setSelectionGroup(SequenceGroup sg)\r
269   {\r
270     selectionGroup = sg;\r
271   }\r
272 \r
273 \r
274  public boolean getConservationSelected()\r
275  {\r
276    return conservationColourSelected;\r
277  }\r
278 \r
279  public void setConservationSelected(boolean b)\r
280  {\r
281    conservationColourSelected = b;\r
282  }\r
283 \r
284  public boolean getAbovePIDThreshold()\r
285  {\r
286    return abovePIDThreshold;\r
287  }\r
288 \r
289  public void setAbovePIDThreshold(boolean b)\r
290  {\r
291    abovePIDThreshold = b;\r
292  }\r
293 \r
294   public int getStartRes() {\r
295     return startRes;\r
296   }\r
297 \r
298   public int getEndRes() {\r
299     return endRes;\r
300   }\r
301 \r
302   public int getStartSeq() {\r
303     return startSeq;\r
304   }\r
305 \r
306   public void setGlobalColourScheme(ColourSchemeI cs)\r
307   {\r
308      globalColourScheme = cs;\r
309   }\r
310 \r
311   public ColourSchemeI getGlobalColourScheme()\r
312   {\r
313     return globalColourScheme;\r
314   }\r
315 \r
316 \r
317   public void setStartRes(int res) {\r
318     this.startRes = res;\r
319   }\r
320   public void setStartSeq(int seq) {\r
321     this.startSeq = seq;\r
322   }\r
323   public void setEndRes(int res) {\r
324     if (res > alignment.getWidth()-1) {\r
325       System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
326        res = alignment.getWidth()-1;\r
327     }\r
328     if (res < 0) {\r
329       res = 0;\r
330     }\r
331     this.endRes = res;\r
332   }\r
333   public void setEndSeq(int seq) {\r
334     if (seq > alignment.getHeight()) {\r
335       seq = alignment.getHeight();\r
336     }\r
337     if (seq < 0) {\r
338       seq = 0;\r
339     }\r
340     this.endSeq = seq;\r
341   }\r
342   public int getEndSeq() {\r
343     return endSeq;\r
344   }\r
345 \r
346   public void setFont(Font f) {\r
347     font = f;\r
348     javax.swing.JFrame temp = new javax.swing.JFrame();\r
349     temp.addNotify();\r
350     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
351     setCharHeight(fm.getHeight());\r
352     setCharWidth(fm.charWidth('M'));\r
353   }\r
354 \r
355   public Font getFont() {\r
356     return font;\r
357   }\r
358   public void setCharWidth(int w) {\r
359     this.charWidth = w;\r
360   }\r
361   public int getCharWidth() {\r
362     return charWidth;\r
363   }\r
364   public void setCharHeight(int h) {\r
365     this.charHeight = h;\r
366   }\r
367   public int getCharHeight() {\r
368     return charHeight;\r
369   }\r
370   public void setChunkWidth(int w) {\r
371     this.chunkWidth = w;\r
372   }\r
373   public int getChunkWidth() {\r
374     return chunkWidth;\r
375   }\r
376   public void setChunkHeight(int h) {\r
377     this.chunkHeight = h;\r
378   }\r
379   public int getChunkHeight() {\r
380     return chunkHeight;\r
381   }\r
382   public AlignmentI getAlignment() {\r
383     return alignment;\r
384   }\r
385   public void setAlignment(AlignmentI align) {\r
386     this.alignment = align;\r
387   }\r
388 \r
389   public void setWrapAlignment(boolean state) {\r
390     wrapAlignment = state;\r
391   }\r
392   public void setShowText(boolean state) {\r
393     showText = state;\r
394   }\r
395 \r
396   public void setRenderGaps(boolean state){\r
397     renderGaps = state;\r
398     if(renderer instanceof SequenceRenderer)\r
399     {\r
400       SequenceRenderer sr = (SequenceRenderer)renderer;\r
401       sr.renderGaps(state);\r
402     }\r
403   }\r
404 \r
405 \r
406   public boolean getColourText()\r
407   {\r
408     return showColourText;\r
409   }\r
410 \r
411   public void setColourText(boolean state)\r
412   {\r
413     showColourText = state;\r
414   }\r
415 \r
416   public void setShowBoxes(boolean state) {\r
417     showBoxes = state;\r
418   }\r
419 \r
420   public boolean getWrapAlignment() {\r
421       return wrapAlignment;\r
422   }\r
423   public boolean getShowText() {\r
424     return showText;\r
425   }\r
426   public boolean getShowBoxes() {\r
427     return showBoxes;\r
428   }\r
429 \r
430   public char getGapCharacter() {\r
431     return getAlignment().getGapCharacter();\r
432   }\r
433   public void setGapCharacter(char gap) {\r
434     if (getAlignment() != null) {\r
435       getAlignment().setGapCharacter(gap);\r
436     }\r
437   }\r
438   public void setThreshold(int thresh) {\r
439     threshold = thresh;\r
440   }\r
441   public int getThreshold() {\r
442     return threshold;\r
443   }\r
444   public void setIncrement(int inc) {\r
445     increment = inc;\r
446   }\r
447   public int getIncrement() {\r
448     return increment;\r
449   }\r
450   public int getIndex(int y) {\r
451     int y1     = 0;\r
452     int starty = getStartSeq();\r
453     int endy   = getEndSeq();\r
454 \r
455     for (int i = starty; i <= endy; i++) {\r
456       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null) {\r
457         int y2 = y1 + getCharHeight();\r
458 \r
459         if (y>=y1 && y <=y2) {\r
460           return i;\r
461         }\r
462         y1  = y2;\r
463       } else {\r
464         return -1;\r
465       }\r
466     }\r
467     return -1;\r
468   }\r
469 \r
470   public ColumnSelection getColumnSelection() {\r
471     return colSel;\r
472   }\r
473 \r
474   public void resetSeqLimits(int height) {\r
475     setEndSeq(height/getCharHeight());\r
476   }\r
477   public void setCurrentTree(NJTree tree) {\r
478       currentTree = tree;\r
479   }\r
480   public NJTree getCurrentTree() {\r
481     return currentTree;\r
482   }\r
483 \r
484     public void setRenderer(RendererI rend) {\r
485         this.renderer = rend;\r
486     }\r
487 \r
488     public RendererI getRenderer() {\r
489         return renderer;\r
490     }\r
491 \r
492   public void setColourAppliesToAllGroups(boolean b)\r
493   {   colourAppliesToAllGroups = b; }\r
494 \r
495   public boolean getColourAppliesToAllGroups()\r
496   {return colourAppliesToAllGroups; }\r
497 \r
498   public boolean getShowFullId()\r
499   {\r
500     return showFullId;\r
501   }\r
502 \r
503   public void setShowFullId(boolean b)\r
504   {\r
505     showFullId = b;\r
506   }\r
507 \r
508   public boolean getShowAnnotation()\r
509   {\r
510     return showAnnotation;\r
511   }\r
512 \r
513   public void setShowAnnotation(boolean b)\r
514   {\r
515     showAnnotation = b;\r
516   }\r
517 \r
518   public boolean getScaleAboveWrapped()\r
519   { return scaleAboveWrapped;}\r
520 \r
521   public boolean getScaleLeftWrapped()\r
522   { return scaleLeftWrapped; }\r
523 \r
524   public boolean getScaleRightWrapped()\r
525   { return scaleRightWrapped; }\r
526 \r
527   public void setScaleAboveWrapped(boolean b)\r
528   { scaleAboveWrapped = b; }\r
529 \r
530   public void setScaleLeftWrapped(boolean b)\r
531   { scaleLeftWrapped = b; }\r
532 \r
533   public void setScaleRightWrapped(boolean b)\r
534   { scaleRightWrapped = b; }\r
535 \r
536 \r
537 }\r