c4f308e3572564debd3074a31cb7f88f4319a913
[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.datamodel.HiddenColumns;
13 import jalview.renderer.api.AnnotationRowRendererI;
14
15 import java.awt.Color;
16 import java.awt.Graphics;
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, HiddenColumns hiddenColumns,
28           ColumnSelection columnSelection, AlignmentAnnotation _aa,
29           Annotation[] aa_annotations, int sRes, int eRes, float min,
30           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
54       if (column > aaMax)
55       {
56         break;
57       }
58
59       if (aa_annotations[column] == null)
60       {
61         x++;
62         continue;
63       }
64       if (_aa.sequenceRef != null)
65       {
66         // get the sequence position for the column
67         column = _aa.sequenceRef.findPosition(column) - 1;
68       }
69       ContactListI contacts = viewport.getContactList(_aa, column);
70       if (contacts == null)
71       {
72         return;
73       }
74       int contact_height = contacts.getContactHeight();
75       // fractional number of contacts covering each pixel
76       double contacts_per_pixel = ((double) contact_height)
77               / ((double) _aa.graphHeight);
78
79       int pixels_step;
80
81       if (contacts_per_pixel >= 1)
82       {
83         // many contacts rendered per pixel
84         pixels_step = 1;
85       }
86       else
87       {
88         // pixel height for each contact
89         pixels_step = (int) Math
90                 .ceil(((double) _aa.graphHeight) / (double) contact_height);
91       }
92
93       int cstart = 0, cend;
94
95       for (int ht = y2,
96               eht = y2 - _aa.graphHeight; ht >= eht; ht -= pixels_step)
97       {
98         cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
99         cend = (int) Math.min(contact_height,
100                 Math.ceil(cstart + contacts_per_pixel * pixels_step));
101
102         // TODO show maximum colour for range - sort of done
103         // also need a 'getMaxPosForRange(start,end)' to accurately render
104         Color col = getColorForRange(min, max, contacts, cstart, cend);
105
106         {
107           g.setColor(col);
108         }
109         if (pixels_step > 1)
110         {
111           g.fillRect(x * charWidth, ht, charWidth, 1 + pixels_step);
112         }
113         else
114         {
115           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
116         }
117       }
118       x++;
119     }
120
121   }
122
123   Color minColor = Color.white, maxColor = Color.magenta;
124
125   Color shadeFor(float min, float max, float value)
126   {
127     return jalview.util.ColorUtils.getGraduatedColour(value, 0, minColor,
128             max, maxColor);
129   }
130
131   public Color getColorForRange(float min, float max, ContactListI cl,
132           int i, int j)
133   {
134     ContactRange cr = cl.getRangeFor(i, j);
135     // average for moment - probably more interested in maxIntProj though
136     return shadeFor(min, max, (float) cr.getMean());
137   }
138
139 }