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