JAL-4124 matrix is saved in dataset, with forward references made from alignments
[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, topY = y;
113
114     // uncomment below to render whole area of matrix as pink
115     // g.setColor(shade.no_data);
116     // g.fillRect(x, topY-_aa.height, (eRes - sRes) * charWidth, _aa.graphHeight);
117     
118     boolean showGroups = _aa.isShowGroupsForContactMatrix();
119     int column;
120     int aaMax = aa_annotations.length - 1;
121     ContactMatrixI cm = viewport.getContactMatrix(_aa);
122     if (cm==null)
123     {
124       return;
125     }
126     while (x < eRes - sRes)
127     {
128       column = sRes + x;
129       if (hasHiddenColumns)
130       {
131         column = hiddenColumns.visibleToAbsoluteColumn(column);
132       }
133       // TODO: highlight columns selected
134       boolean colsel = false;
135       if (columnSelection != null)
136       {
137         colsel = columnSelection.contains(column);
138       }
139
140       if (column > aaMax)
141       {
142         break;
143       }
144
145       if (aa_annotations[column] == null)
146       {
147         x++;
148         continue;
149       }
150       ContactListI contacts = viewport.getContactList(_aa, column);
151       if (contacts == null)
152       {
153         x++;
154         continue;
155       }
156       // ContactListI from viewport can map column -> group
157       Color gpcol = (cm == null) ? Color.white
158               : contacts.getColourForGroup(); // cm.getColourForGroup(cm.getGroupsFor(column));
159       // feature still in development - highlight or omit regions hidden in
160       // the alignment - currently marks them as red rows
161       boolean maskHiddenCols = false;
162       // TODO: optionally pass visible column mask to the ContactGeometry object
163       // so it maps
164       // only visible contacts to geometry
165       // Bean holding mapping from contact list to pixels
166       // TODO: allow bracketing/limiting of range on contacts to render (like
167       // visible column mask but more flexible?)
168
169       // COntactListI provides mapping for column -> cm-groupmapping
170       final ContactGeometry cgeom = new ContactGeometry(contacts,
171               _aa.graphHeight);
172
173       for (int ht = 0, botY = topY
174               - _aa.height; ht < _aa.graphHeight; ht += cgeom.pixels_step)
175       {
176         ContactGeometry.contactInterval ci = cgeom.mapFor(ht,
177                 ht + cgeom.pixels_step);
178         // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
179         // cend = (int) Math.min(contact_height,
180         // Math.ceil(cstart + contacts_per_pixel * pixels_step));
181
182         Color col;
183         boolean rowsel = false, containsHidden = false;
184         if (columnSelection != null)
185         {
186           rowsel = cgeom.intersects(ci, columnSelection, hiddenColumns,
187                   maskHiddenCols);
188         }
189         // TODO: show selected region
190         if (colsel || rowsel)
191         {
192
193           col = getSelectedColorForRange(min, max, contacts, ci.cStart,
194                   ci.cEnd);
195           if (colsel && rowsel)
196           {
197             col = new Color(col.getBlue(), col.getGreen(), col.getRed());
198           }
199           else
200           {
201             col = new Color(col.getBlue(), col.getBlue(), col.getBlue());
202           }
203         }
204         else
205         {
206           col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd);
207         }
208         if (containsHidden)
209         {
210           col = shade.hidden;
211         }
212         if (showGroups && gpcol != null && gpcol != Color.white)
213         {
214           // todo - could overlay group as a transparent rectangle ?
215           col = new Color(
216                   (int) (((float) (col.getRed() + gpcol.getRed())) / 2f),
217                   (int) (((float) (col.getGreen() + gpcol.getGreen()))
218                           / 2f),
219                   (int) (((float) (col.getBlue() + gpcol.getBlue())) / 2f));
220         }
221         g.setColor(col);
222         if (cgeom.pixels_step > 1)
223         {
224           g.fillRect(x * charWidth, botY+ht, charWidth, 1 + cgeom.pixels_step);
225         }
226         else
227         {
228           g.drawLine(x * charWidth, botY+ht, (x + 1) * charWidth, botY+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 }