JAL-2349 colour shade computation refactored to annotation row renderer
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
1 /**
2  * 
3  */
4 package jalview.renderer;
5
6 import jalview.api.AlignViewportI;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.ColumnSelection;
10 import jalview.datamodel.ContactListI;
11 import jalview.datamodel.ContactRange;
12 import jalview.renderer.api.AnnotationRowRendererI;
13
14 import java.awt.Color;
15 import java.awt.Graphics;
16
17 /**
18  * @author jprocter
19  *
20  */
21 public class ContactMapRenderer implements AnnotationRowRendererI
22 {
23
24   @Override
25   public void renderRow(Graphics g, int charWidth, int charHeight,
26           boolean hasHiddenColumns, AlignViewportI viewport,
27           ColumnSelection columnSelection, AlignmentAnnotation _aa,
28           Annotation[] aa_annotations, int sRes, int eRes, float min,
29           float max, int y)
30   {
31     if (sRes > aa_annotations.length)
32     {
33       return;
34     }
35     eRes = Math.min(eRes, aa_annotations.length);
36
37     int x = 0, y2 = y;
38
39     g.setColor(Color.pink);
40
41     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
42
43     int column;
44     int aaMax = aa_annotations.length - 1;
45     while (x < eRes - sRes)
46     {
47       column = sRes + x;
48       if (hasHiddenColumns)
49       {
50         column = columnSelection.adjustForHiddenColumns(column);
51       }
52
53       if (column > aaMax)
54       {
55         break;
56       }
57
58       if (aa_annotations[column] == null)
59       {
60         x++;
61         continue;
62       }
63       /*
64        * {profile type, #values, total count, char1, pct1, char2, pct2...}
65        */
66       ContactListI contacts = viewport.getContactList(_aa, column);
67       min = _aa.graphMin;
68       max = _aa.graphMax;
69       if (contacts == null)
70       {
71         return;
72       }
73
74       // cell height to render
75       double scale = (_aa.graphHeight < contacts.getContactHeight()) ? 1
76               : (((double) _aa.graphHeight) / (double) contacts
77                       .getContactHeight());
78       int cstart, cend = -1;
79       for (int ht = y2, eht = y2 - _aa.graphHeight; ht >= eht; ht -= scale)
80       {
81         cstart = cend + 1;
82         cend = -1
83                 + (contacts.getContactHeight() * (ht - eht) / _aa.graphHeight);
84         // TODO show maximum colour for range - sort of done
85         // also need a 'getMaxPosForRange(start,end)'
86         g.setColor(getColorForRange(contacts, cstart, cend));
87
88         if (scale > 1)
89         {
90           g.fillRect(x * charWidth, ht, charWidth, 1 + (int) scale);
91         }
92         else
93         {
94           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
95         }
96       }
97       x++;
98     }
99
100   }
101
102   Color minColor = Color.white, maxColor = Color.magenta;
103
104   float min, max;
105
106   Color shadeFor(float value)
107   {
108     return jalview.util.ColorUtils.getGraduatedColour(value, 0, minColor,
109             max, maxColor);
110   }
111
112   public Color getColorForRange(ContactListI cl, int i, int j)
113   {
114     ContactRange cr = cl.getRangeFor(i, j);
115     // average for moment - probably more interested in maxIntProj though
116     return shadeFor((float) cr.getMean());
117   }
118
119 }