JAL-1551 spotlessApply
[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 import java.util.Iterator;
9
10 import jalview.api.AlignViewportI;
11 import jalview.datamodel.AlignmentAnnotation;
12 import jalview.datamodel.Annotation;
13 import jalview.datamodel.ColumnSelection;
14 import jalview.datamodel.ContactListI;
15 import jalview.datamodel.ContactMatrixI;
16 import jalview.datamodel.ContactRange;
17 import jalview.datamodel.HiddenColumns;
18 import jalview.renderer.api.AnnotationRowRendererI;
19
20 /**
21  * @author jprocter
22  *
23  */
24 public abstract class ContactMapRenderer implements AnnotationRowRendererI
25 {
26   /**
27    * bean holding colours for shading
28    * 
29    * @author jprocter
30    *
31    */
32   public class Shading
33   {
34     /**
35      * shown when no data available from map
36      */
37     Color no_data;
38
39     /**
40      * shown for region not currently visible - should normally not see this
41      */
42     Color hidden;
43
44     /**
45      * linear shading scheme min/max
46      */
47     Color maxColor, minColor;
48
49     /**
50      * linear shading scheme min/max for selected region
51      */
52     Color selMinColor, selMaxColor;
53
54     public Shading(Color no_data, Color hidden, Color maxColor,
55             Color minColor, Color selMinColor, Color selMaxColor)
56     {
57       super();
58       this.no_data = no_data;
59       this.hidden = hidden;
60       this.maxColor = maxColor;
61       this.minColor = minColor;
62       this.selMinColor = selMinColor;
63       this.selMaxColor = selMaxColor;
64     }
65
66   }
67
68   final Shading shade;
69
70   /**
71    * build an EBI-AlphaFold style renderer of PAE matrices
72    * 
73    * @return
74    */
75   public static ContactMapRenderer newPAERenderer()
76   {
77     return new ContactMapRenderer()
78     {
79       @Override
80       public Shading getShade()
81       {
82         return new Shading(Color.pink, Color.red,
83
84                 new Color(246, 252, 243), new Color(0, 60, 26),
85                 new Color(26, 0, 60), new Color(243, 246, 252));
86       }
87     };
88   }
89
90   /**
91    * 
92    * @return instance of Shading used to initialise the renderer
93    */
94   public abstract Shading getShade();
95
96   public ContactMapRenderer()
97   {
98     this.shade = getShade();
99   }
100
101   @Override
102   public void renderRow(Graphics g, int charWidth, int charHeight,
103           boolean hasHiddenColumns, AlignViewportI viewport,
104           HiddenColumns hiddenColumns, ColumnSelection columnSelection,
105           AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
106           int eRes, float min, float max, int y)
107   {
108     if (sRes > aa_annotations.length)
109     {
110       return;
111     }
112     eRes = Math.min(eRes, aa_annotations.length);
113
114     int x = 0, y2 = y;
115
116     g.setColor(shade.no_data);
117
118     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
119
120     int column;
121     int aaMax = aa_annotations.length - 1;
122     ContactMatrixI cm = viewport.getContactMatrix(_aa);
123     while (x < eRes - sRes)
124     {
125       column = sRes + x;
126       if (hasHiddenColumns)
127       {
128         column = hiddenColumns.visibleToAbsoluteColumn(column);
129       }
130       // TODO: highlight columns selected
131       boolean colsel = false;
132       if (columnSelection != null)
133       {
134         colsel = columnSelection.contains(column);
135       }
136
137       if (column > aaMax)
138       {
139         break;
140       }
141
142       if (aa_annotations[column] == null)
143       {
144         x++;
145         continue;
146       }
147       ContactListI contacts = viewport.getContactList(_aa, column);
148       if (contacts == null)
149       {
150         x++;
151         continue;
152       }
153       // ContactListI from viewport can map column -> group
154       Color gpcol = (cm == null) ? Color.white
155               : contacts.getColourForGroup(); // cm.getColourForGroup(cm.getGroupsFor(column));
156
157       // feature still in development - highlight or omit regions hidden in
158       // the alignment - currently marks them as red rows
159       boolean maskHiddenCols = false;
160       // TODO: optionally pass visible column mask to the ContactGeometry object
161       // so it maps
162       // only visible contacts to geometry
163       // Bean holding mapping from contact list to pixels
164       // TODO: allow bracketing/limiting of range on contacts to render (like
165       // visible column mask but more flexible?)
166
167       // COntactListI provides mapping for column -> cm-groupmapping
168       final ContactGeometry cgeom = new ContactGeometry(contacts,
169               _aa.graphHeight);
170
171       for (int ht = y2, eht = y2
172               - _aa.graphHeight; ht >= eht; ht -= cgeom.pixels_step)
173       {
174
175         ContactGeometry.contactInterval ci = cgeom.mapFor(y2 - ht,
176                 y2 - ht + cgeom.pixels_step);
177         // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
178         // cend = (int) Math.min(contact_height,
179         // Math.ceil(cstart + contacts_per_pixel * pixels_step));
180
181         Color col;
182         boolean rowsel = false, containsHidden = false;
183         if (columnSelection != null)
184         {
185           rowsel = cgeom.intersects(ci, columnSelection, hiddenColumns,
186                   maskHiddenCols);
187         }
188         // TODO: show selected region
189         if (colsel || rowsel)
190         {
191
192           col = getSelectedColorForRange(min, max, contacts, ci.cStart,
193                   ci.cEnd);
194           if (colsel && rowsel)
195           {
196             col = new Color(col.getBlue(), col.getGreen(), col.getRed());
197           }
198           else
199           {
200             col = new Color(col.getBlue(), col.getBlue(), col.getBlue());
201           }
202         }
203         else
204         {
205           col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd);
206         }
207         if (containsHidden)
208         {
209           col = shade.hidden;
210         }
211         if (gpcol != null && gpcol != Color.white)
212         {
213           // todo - could overlay group as a transparent rectangle ?
214           col = new Color(
215                   (int) (((float) (col.getRed() + gpcol.getRed())) / 2f),
216                   (int) (((float) (col.getGreen() + gpcol.getGreen()))
217                           / 2f),
218                   (int) (((float) (col.getBlue() + gpcol.getBlue())) / 2f));
219         }
220         g.setColor(col);
221
222         if (cgeom.pixels_step > 1)
223         {
224           g.fillRect(x * charWidth, ht, charWidth, 1 + cgeom.pixels_step);
225         }
226         else
227         {
228           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
229         }
230       }
231       x++;
232     }
233
234   }
235
236   Color shadeFor(float min, float max, float value)
237   {
238     return jalview.util.ColorUtils.getGraduatedColour(value, 0,
239             shade.minColor, max, shade.maxColor);
240   }
241
242   public Color getColorForRange(float min, float max, ContactListI cl,
243           int i, int j)
244   {
245     ContactRange cr = cl.getRangeFor(i, j);
246     // average for moment - probably more interested in maxIntProj though
247     return shadeFor(min, max, (float) cr.getMean());
248   }
249
250   public Color getSelectedColorForRange(float min, float max,
251           ContactListI cl, int i, int j)
252   {
253     ContactRange cr = cl.getRangeFor(i, j);
254     // average for moment - probably more interested in maxIntProj though
255     return jalview.util.ColorUtils.getGraduatedColour((float) cr.getMean(),
256             0, shade.selMinColor, max, shade.selMaxColor);
257   }
258
259 }