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