0514d1550f22cf54ec74803acfd12bd7149b1532
[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     /**
55      * 
56      * @param no_data
57      *          - colour when no data available
58      * @param hidden
59      *          - colour if this row is hidden
60      * @param maxColor
61      *          - colour for maximum value of contact
62      * @param minColor
63      *          - colour for minimum value of contact
64      * @param selMinColor
65      *          - min colour if the contact has been selected
66      * @param selMaxColor
67      *          - max colour if contact is selected
68      */
69     public Shading(Color no_data, Color hidden, Color maxColor,
70             Color minColor, Color selMinColor, Color selMaxColor)
71     {
72       super();
73       this.no_data = no_data;
74       this.hidden = hidden;
75       this.maxColor = maxColor;
76       this.minColor = minColor;
77       this.selMinColor = selMinColor;
78       this.selMaxColor = selMaxColor;
79     }
80
81   }
82
83   final Shading shade;
84
85   /**
86    * build an EBI-AlphaFold style renderer of PAE matrices
87    * 
88    * @return
89    */
90   public static ContactMapRenderer newPAERenderer()
91   {
92     return new ContactMapRenderer()
93     {
94       @Override
95       public Shading getShade()
96       {
97         return new Shading(Color.pink, Color.red,
98
99                 new Color(247, 252, 245), new Color(0, 68, 28),
100                 new Color(28, 0, 68), new Color(245, 247, 252));
101       }
102     };
103   }
104
105   /**
106    * 
107    * @return instance of Shading used to initialise the renderer
108    */
109   public abstract Shading getShade();
110
111   public ContactMapRenderer()
112   {
113     this.shade = getShade();
114   }
115
116   @Override
117   public void renderRow(Graphics g, int charWidth, int charHeight,
118           boolean hasHiddenColumns, AlignViewportI viewport,
119           HiddenColumns hiddenColumns, ColumnSelection columnSelection,
120           AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
121           int eRes, float min, float max, int y)
122   {
123     if (sRes > aa_annotations.length)
124     {
125       return;
126     }
127     eRes = Math.min(eRes, aa_annotations.length);
128
129     int x = 0, topY = y;
130
131     // uncomment below to render whole area of matrix as pink
132     // g.setColor(shade.no_data);
133     // g.fillRect(x, topY-_aa.height, (eRes - sRes) * charWidth,
134     // _aa.graphHeight);
135
136     boolean showGroups = _aa.isShowGroupsForContactMatrix();
137     int column;
138     int aaMax = aa_annotations.length - 1;
139     ContactMatrixI cm = viewport.getContactMatrix(_aa);
140     if (cm == null)
141     {
142       return;
143     }
144     while (x < eRes - sRes)
145     {
146       column = sRes + x;
147       if (hasHiddenColumns)
148       {
149         column = hiddenColumns.visibleToAbsoluteColumn(column);
150       }
151       // TODO: highlight columns selected
152       boolean colsel = false;
153       if (columnSelection != null)
154       {
155         colsel = columnSelection.contains(column);
156       }
157
158       if (column > aaMax)
159       {
160         break;
161       }
162
163       if (aa_annotations[column] == null)
164       {
165         x++;
166         continue;
167       }
168       ContactListI contacts = viewport.getContactList(_aa, column);
169       if (contacts == null)
170       {
171         x++;
172         continue;
173       }
174       // ContactListI from viewport can map column -> group
175       Color gpcol = (cm == null) ? Color.white
176               : contacts.getColourForGroup(); // cm.getColourForGroup(cm.getGroupsFor(column));
177       // feature still in development - highlight or omit regions hidden in
178       // the alignment - currently marks them as red rows
179       boolean maskHiddenCols = false;
180       // TODO: optionally pass visible column mask to the ContactGeometry object
181       // so it maps
182       // only visible contacts to geometry
183       // Bean holding mapping from contact list to pixels
184       // TODO: allow bracketing/limiting of range on contacts to render (like
185       // visible column mask but more flexible?)
186
187       // COntactListI provides mapping for column -> cm-groupmapping
188       final ContactGeometry cgeom = new ContactGeometry(contacts,
189               _aa.graphHeight);
190
191       for (int ht = 0, botY = topY
192               - _aa.height; ht < _aa.graphHeight; ht += cgeom.pixels_step)
193       {
194         ContactGeometry.contactInterval ci = cgeom.mapFor(ht);
195         // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
196         // cend = (int) Math.min(contact_height,
197         // Math.ceil(cstart + contacts_per_pixel * pixels_step));
198
199         Color col;
200         boolean rowsel = false;
201         boolean containsHidden = false;
202         if (columnSelection != null)
203         {
204           rowsel = cgeom.intersects(ci, columnSelection, hiddenColumns,
205                   maskHiddenCols);
206         }
207         // TODO: show selected region
208         if (colsel || rowsel)
209         {
210
211           col = getSelectedColorForRange(min, max, contacts, ci.cStart,
212                   ci.cEnd);
213           if (colsel && rowsel)
214           {
215             col = new Color(col.getBlue(), col.getGreen(), col.getRed());
216           }
217           else
218           {
219             col = new Color(col.getBlue(), col.getBlue(), col.getBlue());
220           }
221         }
222         else
223         {
224           col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd);
225         }
226         if (containsHidden)
227         {
228           col = shade.hidden;
229         }
230         if (showGroups && gpcol != null && gpcol != Color.white)
231         {
232           // todo - could overlay group as a transparent rectangle ?
233           col = new Color(
234                   (int) (((float) (col.getRed() + gpcol.getRed())) / 2f),
235                   (int) (((float) (col.getGreen() + gpcol.getGreen()))
236                           / 2f),
237                   (int) (((float) (col.getBlue() + gpcol.getBlue())) / 2f));
238         }
239         g.setColor(col);
240         if (cgeom.pixels_step > 1)
241         {
242           g.fillRect(x * charWidth, botY + ht, charWidth,
243                   cgeom.pixels_step);
244         }
245         else
246         {
247           g.drawLine(x * charWidth, botY + ht, (x + 1) * charWidth,
248                   botY + ht);
249         }
250       }
251       x++;
252     }
253
254   }
255
256   Color shadeFor(float min, float max, float value)
257   {
258     return jalview.util.ColorUtils.getGraduatedColour(value, 0,
259             shade.minColor, max, shade.maxColor);
260   }
261
262   public Color getColorForRange(float min, float max, ContactListI cl,
263           int i, int j)
264   {
265     ContactRange cr = cl.getRangeFor(i, j);
266     // average for moment - probably more interested in maxIntProj though
267     return shadeFor(min, max, (float) cr.getMean());
268   }
269
270   public Color getSelectedColorForRange(float min, float max,
271           ContactListI cl, int i, int j)
272   {
273     ContactRange cr = cl.getRangeFor(i, j);
274     // average for moment - probably more interested in maxIntProj though
275     return jalview.util.ColorUtils.getGraduatedColour((float) cr.getMin(),
276             0, shade.selMinColor, max, shade.selMaxColor);
277   }
278
279 }