JAL-2348 contact map renderer refactored from hacked version of matrix 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.renderer.api.AnnotationRowRendererI;
12
13 import java.awt.Color;
14 import java.awt.Font;
15 import java.awt.Graphics;
16
17 /**
18  * @author jprocter
19  *
20  */
21 public class ContactMapRenderer implements AnnotationRowRendererI
22 {
23   /*
24    * 
25    *     // TODO Auto-generated method stub
26       void drawProfileDensity(Graphics g, AlignmentAnnotation _aa,
27               Annotation[] aa_annotations, int sRes, int eRes, float min,
28               float max, int y)
29       {
30
31    * 
32    * 
33    */
34   @Override
35   public void renderRow(Graphics g, int charWidth, int charHeight,
36           boolean hasHiddenColumns, AlignViewportI viewport,
37           ColumnSelection columnSelection, AlignmentAnnotation _aa,
38           Annotation[] aa_annotations, int sRes, int eRes, float min,
39           float max, int y)
40   {
41     if (sRes > aa_annotations.length)
42     {
43       return;
44     }
45     Font ofont = g.getFont();
46     eRes = Math.min(eRes, aa_annotations.length);
47
48     int x = 0, y2 = y;
49
50     g.setColor(Color.pink);
51
52     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
53
54     int column;
55     int aaMax = aa_annotations.length - 1;
56     while (x < eRes - sRes)
57     {
58       column = sRes + x;
59       if (hasHiddenColumns)
60       {
61         column = columnSelection.adjustForHiddenColumns(column);
62       }
63
64       if (column > aaMax)
65       {
66         break;
67       }
68
69       if (aa_annotations[column] == null)
70       {
71         x++;
72         continue;
73       }
74       /*
75        * {profile type, #values, total count, char1, pct1, char2, pct2...}
76        */
77       ContactListI contacts = viewport.getContactList(_aa, column);
78
79       if (contacts == null)
80       {
81         return;
82       }
83
84
85       int scale = Math
86               .max(1, _aa.graphHeight / contacts.getContactHeight());
87       int step = _aa.graphHeight / scale;
88       int valuesProcessed = 0;
89       // profl[1] is the number of values in the profile
90       for (int stp = 0, ht = y2, eht = y2 + _aa.graphHeight; ht < eht; ht += scale, stp++)
91       {
92         valuesProcessed = stp * step;
93         g.setColor(contacts.getColorForScore(stp * step));
94
95         if (scale > 1)
96         {
97           g.fillRect(x * charWidth, ht, charWidth, scale);
98         } else {
99           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
100         }
101       }
102     }
103     x++;
104
105   }
106 }