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