Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
ContactMapRenderer | 40 | 59 | 27 | ||
ContactMapRenderer.Shading | 48 | 7 | 1 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
7 | * Jalview is free software: you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation, either version 3 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * Jalview is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty | |
14 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | * PURPOSE. See the GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Jalview. If not, see <http://www.gnu.org/licenses/>. | |
19 | * The Jalview Authors are detailed in the 'AUTHORS' file. | |
20 | */ | |
21 | package jalview.renderer; | |
22 | ||
23 | import java.awt.Color; | |
24 | import java.awt.Graphics; | |
25 | ||
26 | import jalview.api.AlignViewportI; | |
27 | import jalview.datamodel.AlignmentAnnotation; | |
28 | import jalview.datamodel.Annotation; | |
29 | import jalview.datamodel.ColumnSelection; | |
30 | import jalview.datamodel.ContactListI; | |
31 | import jalview.datamodel.ContactMatrixI; | |
32 | import jalview.datamodel.ContactRange; | |
33 | import jalview.datamodel.HiddenColumns; | |
34 | import jalview.renderer.api.AnnotationRowRendererI; | |
35 | ||
36 | /** | |
37 | * @author jprocter | |
38 | * | |
39 | */ | |
40 | public abstract class ContactMapRenderer implements AnnotationRowRendererI | |
41 | { | |
42 | /** | |
43 | * bean holding colours for shading | |
44 | * | |
45 | * @author jprocter | |
46 | * | |
47 | */ | |
48 | public class Shading | |
49 | { | |
50 | /** | |
51 | * shown when no data available from map | |
52 | */ | |
53 | Color no_data; | |
54 | ||
55 | /** | |
56 | * shown for region not currently visible - should normally not see this | |
57 | */ | |
58 | Color hidden; | |
59 | ||
60 | /** | |
61 | * linear shading scheme min/max | |
62 | */ | |
63 | Color maxColor, minColor; | |
64 | ||
65 | /** | |
66 | * linear shading scheme min/max for selected region | |
67 | */ | |
68 | Color selMinColor, selMaxColor; | |
69 | ||
70 | /** | |
71 | * | |
72 | * @param no_data | |
73 | * - colour when no data available | |
74 | * @param hidden | |
75 | * - colour if this row is hidden | |
76 | * @param maxColor | |
77 | * - colour for maximum value of contact | |
78 | * @param minColor | |
79 | * - colour for minimum value of contact | |
80 | * @param selMinColor | |
81 | * - min colour if the contact has been selected | |
82 | * @param selMaxColor | |
83 | * - max colour if contact is selected | |
84 | */ | |
85 | 139 | public Shading(Color no_data, Color hidden, Color maxColor, |
86 | Color minColor, Color selMinColor, Color selMaxColor) | |
87 | { | |
88 | 139 | super(); |
89 | 139 | this.no_data = no_data; |
90 | 139 | this.hidden = hidden; |
91 | 139 | this.maxColor = maxColor; |
92 | 139 | this.minColor = minColor; |
93 | 139 | this.selMinColor = selMinColor; |
94 | 139 | this.selMaxColor = selMaxColor; |
95 | } | |
96 | ||
97 | } | |
98 | ||
99 | final Shading shade; | |
100 | ||
101 | /** | |
102 | * build an EBI-AlphaFold style renderer of PAE matrices | |
103 | * | |
104 | * @return | |
105 | */ | |
106 | 139 | public static ContactMapRenderer newPAERenderer() |
107 | { | |
108 | 139 | return new ContactMapRenderer() |
109 | { | |
110 | 139 | @Override |
111 | public Shading getShade() | |
112 | { | |
113 | 139 | return new Shading(Color.pink, Color.red, |
114 | ||
115 | new Color(247, 252, 245), new Color(0, 68, 28), | |
116 | new Color(28, 0, 68), new Color(245, 247, 252)); | |
117 | } | |
118 | }; | |
119 | } | |
120 | ||
121 | /** | |
122 | * | |
123 | * @return instance of Shading used to initialise the renderer | |
124 | */ | |
125 | public abstract Shading getShade(); | |
126 | ||
127 | 139 | public ContactMapRenderer() |
128 | { | |
129 | 139 | this.shade = getShade(); |
130 | } | |
131 | ||
132 | 139 | @Override |
133 | public void renderRow(Graphics g, int charWidth, int charHeight, | |
134 | boolean hasHiddenColumns, AlignViewportI viewport, | |
135 | HiddenColumns hiddenColumns, ColumnSelection columnSelection, | |
136 | AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes, | |
137 | int eRes, float min, float max, int y) | |
138 | { | |
139 | 139 | if (sRes > aa_annotations.length) |
140 | { | |
141 | 0 | return; |
142 | } | |
143 | 139 | eRes = Math.min(eRes, aa_annotations.length); |
144 | ||
145 | 139 | int x = 0, topY = y; |
146 | ||
147 | // uncomment below to render whole area of matrix as pink | |
148 | // g.setColor(shade.no_data); | |
149 | // g.fillRect(x, topY-_aa.height, (eRes - sRes) * charWidth, | |
150 | // _aa.graphHeight); | |
151 | ||
152 | 139 | boolean showGroups = _aa.isShowGroupsForContactMatrix(); |
153 | 139 | int column; |
154 | 139 | int aaMax = aa_annotations.length - 1; |
155 | 139 | ContactMatrixI cm = viewport.getContactMatrix(_aa); |
156 | 139 | if (cm == null) |
157 | { | |
158 | 0 | return; |
159 | } | |
160 | 8223 | while (x < eRes - sRes) |
161 | { | |
162 | 8084 | column = sRes + x; |
163 | 8084 | if (hasHiddenColumns) |
164 | { | |
165 | 0 | column = hiddenColumns.visibleToAbsoluteColumn(column); |
166 | } | |
167 | // TODO: highlight columns selected | |
168 | 8084 | boolean colsel = false; |
169 | 8084 | if (columnSelection != null) |
170 | { | |
171 | 8084 | colsel = columnSelection.contains(column); |
172 | } | |
173 | ||
174 | 8084 | if (column > aaMax) |
175 | { | |
176 | 0 | break; |
177 | } | |
178 | ||
179 | 8084 | if (aa_annotations[column] == null) |
180 | { | |
181 | 4532 | x++; |
182 | 4532 | continue; |
183 | } | |
184 | 3552 | ContactListI contacts = viewport.getContactList(_aa, column); |
185 | 3552 | if (contacts == null) |
186 | { | |
187 | 0 | x++; |
188 | 0 | continue; |
189 | } | |
190 | // ContactListI from viewport can map column -> group | |
191 | 3552 | Color gpcol = (cm == null) ? Color.white |
192 | : contacts.getColourForGroup(); // cm.getColourForGroup(cm.getGroupsFor(column)); | |
193 | // feature still in development - highlight or omit regions hidden in | |
194 | // the alignment - currently marks them as red rows | |
195 | 3552 | boolean maskHiddenCols = false; |
196 | // TODO: optionally pass visible column mask to the ContactGeometry object | |
197 | // so it maps | |
198 | // only visible contacts to geometry | |
199 | // Bean holding mapping from contact list to pixels | |
200 | // TODO: allow bracketing/limiting of range on contacts to render (like | |
201 | // visible column mask but more flexible?) | |
202 | ||
203 | // COntactListI provides mapping for column -> cm-groupmapping | |
204 | 3552 | final ContactGeometry cgeom = new ContactGeometry(contacts, |
205 | _aa.graphHeight); | |
206 | ||
207 | 3552 | for (int ht = 0, botY = topY |
208 | 145628 | - _aa.height; ht < _aa.graphHeight; ht += cgeom.pixels_step) |
209 | { | |
210 | 142079 | ContactGeometry.contactInterval ci = cgeom.mapFor(ht); |
211 | // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel); | |
212 | // cend = (int) Math.min(contact_height, | |
213 | // Math.ceil(cstart + contacts_per_pixel * pixels_step)); | |
214 | ||
215 | 142069 | Color col; |
216 | 142071 | boolean rowsel = false; |
217 | 142077 | boolean containsHidden = false; |
218 | 142078 | if (columnSelection != null) |
219 | { | |
220 | 142080 | rowsel = cgeom.intersects(ci, columnSelection, hiddenColumns, |
221 | maskHiddenCols); | |
222 | } | |
223 | // TODO: show selected region | |
224 | 142080 | if (colsel || rowsel) |
225 | { | |
226 | ||
227 | 0 | col = getSelectedColorForRange(min, max, contacts, ci.cStart, |
228 | ci.cEnd); | |
229 | 0 | if (colsel && rowsel) |
230 | { | |
231 | 0 | col = new Color(col.getBlue(), col.getGreen(), col.getRed()); |
232 | } | |
233 | else | |
234 | { | |
235 | 0 | col = new Color(col.getBlue(), col.getBlue(), col.getBlue()); |
236 | } | |
237 | } | |
238 | else | |
239 | { | |
240 | 142080 | col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd); |
241 | } | |
242 | 142079 | if (containsHidden) |
243 | { | |
244 | 0 | col = shade.hidden; |
245 | } | |
246 | 142079 | if (showGroups && gpcol != null && gpcol != Color.white) |
247 | { | |
248 | // todo - could overlay group as a transparent rectangle ? | |
249 | 0 | col = new Color((int) ((col.getRed() + gpcol.getRed()) / 2f), |
250 | (int) ((col.getGreen() + gpcol.getGreen()) / 2f), | |
251 | (int) ((col.getBlue() + gpcol.getBlue()) / 2f)); | |
252 | } | |
253 | 142080 | g.setColor(col); |
254 | 142080 | if (cgeom.pixels_step > 1) |
255 | { | |
256 | 0 | g.fillRect(x * charWidth, botY + ht, charWidth, |
257 | cgeom.pixels_step); | |
258 | } | |
259 | else | |
260 | { | |
261 | 142078 | g.drawLine(x * charWidth, botY + ht, (x + 1) * charWidth, |
262 | botY + ht); | |
263 | } | |
264 | } | |
265 | 3552 | x++; |
266 | } | |
267 | ||
268 | } | |
269 | ||
270 | 142076 | Color shadeFor(float min, float max, float value) |
271 | { | |
272 | 142076 | return jalview.util.ColorUtils.getGraduatedColour(value, 0, |
273 | shade.minColor, max, shade.maxColor); | |
274 | } | |
275 | ||
276 | 142079 | public Color getColorForRange(float min, float max, ContactListI cl, |
277 | int i, int j) | |
278 | { | |
279 | 142079 | ContactRange cr = cl.getRangeFor(i, j); |
280 | // average for moment - probably more interested in maxIntProj though | |
281 | 142076 | return shadeFor(min, max, (float) cr.getMean()); |
282 | } | |
283 | ||
284 | 0 | public Color getSelectedColorForRange(float min, float max, |
285 | ContactListI cl, int i, int j) | |
286 | { | |
287 | 0 | ContactRange cr = cl.getRangeFor(i, j); |
288 | // average for moment - probably more interested in maxIntProj though | |
289 | 0 | return jalview.util.ColorUtils.getGraduatedColour((float) cr.getMin(), |
290 | 0, shade.selMinColor, max, shade.selMaxColor); | |
291 | } | |
292 | ||
293 | } |