JAL-2349 fixed rendering method and switched to using color from contact range to...
[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.Graphics;
15
16 /**
17  * @author jprocter
18  *
19  */
20 public class ContactMapRenderer implements AnnotationRowRendererI
21 {
22
23   @Override
24   public void renderRow(Graphics g, int charWidth, int charHeight,
25           boolean hasHiddenColumns, AlignViewportI viewport,
26           ColumnSelection columnSelection, AlignmentAnnotation _aa,
27           Annotation[] aa_annotations, int sRes, int eRes, float min,
28           float max, int y)
29   {
30     if (sRes > aa_annotations.length)
31     {
32       return;
33     }
34     eRes = Math.min(eRes, aa_annotations.length);
35
36     int x = 0, y2 = y;
37
38     g.setColor(Color.pink);
39
40     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
41
42     int column;
43     int aaMax = aa_annotations.length - 1;
44     while (x < eRes - sRes)
45     {
46       column = sRes + x;
47       if (hasHiddenColumns)
48       {
49         column = columnSelection.adjustForHiddenColumns(column);
50       }
51
52       if (column > aaMax)
53       {
54         break;
55       }
56
57       if (aa_annotations[column] == null)
58       {
59         x++;
60         continue;
61       }
62       /*
63        * {profile type, #values, total count, char1, pct1, char2, pct2...}
64        */
65       ContactListI contacts = viewport.getContactList(_aa, column);
66
67       if (contacts == null)
68       {
69         return;
70       }
71
72       // cell height to render
73       double scale = (_aa.graphHeight < contacts.getContactHeight()) ? 1
74               : (((double) _aa.graphHeight) / (double) contacts
75                       .getContactHeight());
76       int cstart, cend = -1;
77       for (int ht = y2, eht = y2 - _aa.graphHeight; ht >= eht; ht -= scale)
78       {
79         cstart = cend + 1;
80         cend = -1
81                 + (contacts.getContactHeight() * (ht - eht) / _aa.graphHeight);
82         // TODO show maximum colour for range - sort of done
83         // also need a 'getMaxPosForRange(start,end)'
84         g.setColor(contacts.getColorForRange(cstart, cend));
85
86         if (scale > 1)
87         {
88           g.fillRect(x * charWidth, ht, charWidth, 1 + (int) scale);
89         }
90         else
91         {
92           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
93         }
94       }
95       x++;
96     }
97
98   }
99 }