f5c4c311f207f4e4b70f30543ab6cedb773ff257
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
1 /**
2  * 
3  */
4 package jalview.renderer;
5
6 import java.awt.Color;
7 import java.awt.Graphics;
8
9 import jalview.api.AlignViewportI;
10 import jalview.datamodel.AlignmentAnnotation;
11 import jalview.datamodel.Annotation;
12 import jalview.datamodel.ColumnSelection;
13 import jalview.datamodel.ContactListI;
14 import jalview.datamodel.ContactRange;
15 import jalview.datamodel.HiddenColumns;
16 import jalview.renderer.api.AnnotationRowRendererI;
17
18 /**
19  * @author jprocter
20  *
21  */
22 public class ContactMapRenderer implements AnnotationRowRendererI
23 {
24
25   @Override
26   public void renderRow(Graphics g, int charWidth, int charHeight,
27           boolean hasHiddenColumns, AlignViewportI viewport,
28           HiddenColumns hiddenColumns, ColumnSelection columnSelection,
29           AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
30           int eRes, float min, float max, int y)
31   {
32     if (sRes > aa_annotations.length)
33     {
34       return;
35     }
36     eRes = Math.min(eRes, aa_annotations.length);
37
38     int x = 0, y2 = y;
39
40     g.setColor(Color.pink);
41
42     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
43
44     int column;
45     int aaMax = aa_annotations.length - 1;
46     while (x < eRes - sRes)
47     {
48       column = sRes + x;
49       if (hasHiddenColumns)
50       {
51         column = hiddenColumns.visibleToAbsoluteColumn(column);
52       }
53       // TODO: highlight columns selected
54       boolean colsel = false;
55       if (columnSelection != null)
56       {
57         colsel = columnSelection.contains(column);
58       }
59
60       if (column > aaMax)
61       {
62         break;
63       }
64
65       if (aa_annotations[column] == null)
66       {
67         x++;
68         continue;
69       }
70       if (_aa.sequenceRef != null)
71       {
72         // get the sequence position for the column
73         column = _aa.sequenceRef.findPosition(column) - 1;
74       }
75       ContactListI contacts = viewport.getContactList(_aa, column);
76       if (contacts == null)
77       {
78         return;
79       }
80       int contact_height = contacts.getContactHeight();
81       // fractional number of contacts covering each pixel
82       double contacts_per_pixel = ((double) contact_height)
83               / ((double) _aa.graphHeight);
84
85       int pixels_step;
86
87       if (contacts_per_pixel >= 1)
88       {
89         // many contacts rendered per pixel
90         pixels_step = 1;
91       }
92       else
93       {
94         // pixel height for each contact
95         pixels_step = (int) Math
96                 .ceil(((double) _aa.graphHeight) / (double) contact_height);
97       }
98
99       int cstart = 0, cend;
100
101       for (int ht = y2,
102               eht = y2 - _aa.graphHeight; ht >= eht; ht -= pixels_step)
103       {
104         cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
105         cend = (int) Math.min(contact_height,
106                 Math.ceil(cstart + contacts_per_pixel * pixels_step));
107
108         // TODO show maximum colour for range - sort of done
109         // also need a 'getMaxPosForRange(start,end)' to accurately render
110         Color col;
111         boolean rowsel = false;
112         if (!colsel && columnSelection != null)
113         {
114           if (_aa.sequenceRef == null)
115           {
116             rowsel = columnSelection.intersects(cstart, cend);
117           }
118           else
119           {
120             // TODO check we have correctly mapped cstart to local sequence
121             // numbering
122             int s = _aa.sequenceRef.findIndex(cstart);
123             int e = _aa.sequenceRef.findIndex(cend);
124             if (s > 0 && s < _aa.sequenceRef.getLength())
125             {
126               rowsel = columnSelection.intersects(s, e);
127             }
128           }
129         }
130         // TODO: show selected region
131         if (colsel || rowsel)
132         {
133
134           col = getSelectedColorForRange(min, max, contacts, cstart, cend);
135           g.setColor(col);
136         }
137         else
138         {
139           col = getColorForRange(min, max, contacts, cstart, cend);
140           g.setColor(col);
141         }
142         if (pixels_step > 1)
143         {
144           g.fillRect(x * charWidth, ht, charWidth, 1 + pixels_step);
145         }
146         else
147         {
148           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
149         }
150       }
151       x++;
152     }
153
154   }
155
156   // Shading parameters
157   Color minColor = Color.white, maxColor = Color.green.darker().darker(),
158           selMaxColor = Color.magenta.darker();
159
160   Color shadeFor(float min, float max, float value)
161   {
162     return jalview.util.ColorUtils.getGraduatedColour(value, 0, minColor,
163             max, maxColor);
164   }
165
166   public Color getColorForRange(float min, float max, ContactListI cl,
167           int i, int j)
168   {
169     ContactRange cr = cl.getRangeFor(i, j);
170     // average for moment - probably more interested in maxIntProj though
171     return shadeFor(min, max, (float) cr.getMean());
172   }
173
174   public Color getSelectedColorForRange(float min, float max,
175           ContactListI cl, int i, int j)
176   {
177     ContactRange cr = cl.getRangeFor(i, j);
178     // average for moment - probably more interested in maxIntProj though
179     return jalview.util.ColorUtils.getGraduatedColour((float) cr.getMean(),
180             0, minColor, max, selMaxColor);
181   }
182
183 }