a504b4dcae59ecf00f22c895b619f36551a92260
[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       // cell height to render
86       double scale = (_aa.graphHeight < contacts.getContactHeight()) ? 1
87               : (((double) _aa.graphHeight) / (double) contacts
88                       .getContactHeight());
89       // distance between contact map per cell
90       int step = (int) ((contacts.getContactHeight()) * scale / _aa.graphHeight);
91
92       // profl[1] is the number of values in the profile
93       for (int stp = 0, ht = y2, eht = y2 - _aa.graphHeight; ht > eht; ht -= scale, stp++)
94       {
95         g.setColor(contacts
96                 .getColorForScore((int) (stp * step * ((double) _aa.graphHeight / (double) contacts
97                         .getContactHeight()))));
98
99         if (scale > 1)
100         {
101           g.fillRect(x * charWidth, ht, charWidth, 1 + (int) scale);
102         } else {
103           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
104         }
105       }
106       x++;
107     }
108
109   }
110 }