-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
style.R
416 lines (391 loc) · 14.5 KB
/
style.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#' Specify Styling for Sphere
#'
#' Styling options for the sphere representation. Used inside
#' \code{m_add_style()} and \code{m_set_style()}.
#'
#' @param hidden Boolean - do not show atom. Default \code{FALSE}.
#' @param radius Override van der waals radius.
#' @param scale Scale radius by specified amount.
#' @param colorScheme Specify scheme to color the atoms by. Default is
#' "default". Other choices are "Carbon", "ssPyMOL", "ssJmol", "Jmol",
#' "default", "amino",
#' "shapely", "nucleic", "chain", "chainHetatm", "prop".
#' @param color Discrete, fixed coloring, overrides any colorScheme.
#' @param opacity Opacity of spheres, 0 being invisible. Must be the same for
#' all atoms in the model.
#' @param colorfunc Allows the user to provide a function for setting the
#' colorSchemes, written in javascript.
#' \href{https://3dmol.csb.pitt.edu/doc/types.html#ColorschemeSpec}{Official
#' Documentation}
#'
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_sphere(radius = 0.5)) %>%
#' m_zoom_to()
#' @export
m_style_sphere <- function(scale = 1,
colorScheme = NULL,
color = NULL,
radius = NULL,
hidden = FALSE,
opacity = 1,
colorfunc = NULL) {
sphere_style <- list(
hidden = hidden,
radius = radius,
scale = scale,
colorscheme = colorScheme,
color = color,
opacity = opacity,
colorfunc = colorfunc
) %>% .cleanup_nulls()
class(sphere_style) <- "SphereStyleSpec"
list(sphere = sphere_style)
}
#' Specifying Style for Ball and Stick
#'
#' Styling options for ball and stick representations.
#'
#' @param sphereScale Relative scaling of sphere atomic radii.
#' @param colorScheme Specify scheme to color the atoms by. Default is
#' "default". Other choices are "Carbon", "ssPyMOL", "ssJmol", "Jmol",
#' "default", "amino", "shapely", "nucleic", "chain", "chainHetatm", "prop".
#' @param color Specific coloring for everything in selection, overrides any
#' specified colorScheme.
#' @param sphereRadius Set absolute radius for all spheres in selection
#' (overriding the atomic raddii)l
#' @param stickRadius Set absolute radius for all sticks in the selection.
#' @param hidden Boolean - do not show atoms in selection. Default \code{FALSE}
#' @param opacity Opacity of spheres and sticks, 0 being invisible, 1 being
#' opaque. Must be the same for all atoms in the model.
#' @param colorfunc Allows the user to provide a function for setting the
#' colorSchemes, written in javascript.
#' \href{https://3dmol.csb.pitt.edu/doc/types.html#ColorschemeSpec}{Official
#' Documentation}
#'
#' @export
#'
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_ballnstick()) %>%
#' m_zoom_to()
m_style_ballnstick <- function(sphereScale = 0.3,
colorScheme = NULL,
color = NULL,
sphereRadius = NULL,
stickRadius = 0.25,
hidden = FALSE,
opacity = 1,
colorfunc = NULL) {
ballnstick_style <- c(
m_style_sphere(
scale = sphereScale,
colorScheme = colorScheme,
color = color,
opacity = opacity,
hidden = hidden,
colorfunc = colorfunc
),
m_style_stick(
radius = stickRadius,
hidden = hidden,
colorScheme = colorScheme,
color = color,
opacity = opacity,
colorfunc = colorfunc
)
)
class(ballnstick_style) <- "BallnStickStyleSpec"
ballnstick_style
}
#' Specify Styling for Cartoon
#'
#' Styling options for the cartoon representation. Used inside
#' \code{m_add_style()} and \code{m_set_style()}.
#'
#' @param color Block color values. Strand color, may specify as 'spectrum'
#' which will apply
#' reversed gradient based on residue number.
#' @param style style of cartoon rendering ("trace", "oval", "rectangle"
#' (default), "parabola", "edged").
#' @param ribbon whether to use constant strand width, disregarding secondary
#' structure; use thickness to adjust radius.
#' @param arrows whether to add arrows showing beta-sheet directionality;
#' does not apply to trace or ribbon.
#' @param tubes whether to display alpha helices as simple cylinders; does not
#' apply to trace.
#' @param thickness cartoon strand thickness, default is 0.4.
#' @param width cartoon strand width, default is secondary structure-dependent;
#' does not apply to trace or ribbon.
#' @param opacity set opacity from 0-1; transparency is set per-chain with a
#' warning outputted in the event of ambiguity.
#' @param colorfunc Allows the user to provide a function for setting the
#' colorSchemes, written in javascript.
#' \href{https://3dmol.csb.pitt.edu/doc/types.html#ColorschemeSpec}{Official
#' Documentation}
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_cartoon(color = "spectrum")) %>%
#' m_zoom_to()
#' @export
m_style_cartoon <- function(color = NULL,
style = "rectangle",
ribbon = FALSE,
arrows = TRUE,
tubes = FALSE,
thickness = 0.4,
width = NULL,
opacity = 1,
colorfunc = NULL) {
cartoon_style <- list(
color = color,
style = style,
ribbon = ribbon,
arrows = arrows,
tubes = tubes,
thickness = thickness,
width = width,
opacity = opacity,
colorfunc = colorfunc
) %>% .cleanup_nulls()
class(cartoon_style) <- "CartoonStyleSpec"
list(cartoon = cartoon_style)
}
#' Specify Styling for Stick
#'
#' Styling options for the stick representation. Used inside
#' \code{m_add_style()} and \code{m_set_style()}.
#'
#' @param hidden Do not show.
#' @param radius Radius of sticks.
#' @param singleBonds Draw all bonds as single bonds if \code{TRUE}.
#' @param colorScheme Specify scheme to color the atoms by. Default is
#' "default". Other choices are "Carbon", "ssPyMOL", "ssJmol", "Jmol",
#' "default", "amino",
#' "shapely", "nucleic", "chain", "chainHetatm", "prop".
#' @param color Fixed coloring, overrides colorScheme.
#' @param opacity Opacity, must be the same for all atoms in the model.
#' @param colorfunc Allows the user to provide a function for setting the
#' colorSchemes, written in javascript.
#' \href{https://3dmol.csb.pitt.edu/doc/types.html#ColorschemeSpec}{Official
#' Documentation}
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_stick(opacity = 0.4)) %>%
#' m_zoom_to()
#' @export
m_style_stick <- function(radius = 0.3,
singleBonds = FALSE,
colorScheme = NULL,
color = NULL,
opacity = 1,
hidden = FALSE,
colorfunc = NULL) {
stick_style <- list(
hidden = hidden,
radius = radius,
singleBonds = singleBonds,
colorscheme = colorScheme,
color = color,
opacity = opacity,
colorfunc = colorfunc
) %>% .cleanup_nulls()
class(stick_style) <- "StickStyleSpec"
list(stick = stick_style)
}
#' Specify Styling for Labels
#'
#' Styling options for the labels. Used inside \code{m_add_label()},
#' \code{m_add_res_labels()} and \code{m_add_property_labels()}.
#'
#' @param font Font name, default sans-serif.
#' @param fontSize Height of text, default 18.
#' @param fontColor Font color, default white.
#' @param fontOpacity Font opacity, default 1.
#' @param borderThickness Line width of border around label, default 0.
#' @param borderColor Color of border, default backgroundColor.
#' @param borderOpacity Opacity of border, default 1.
#' @param backgroundColor Color of background, default black.
#' @param backgroundOpacity Opacity of background, default 1.
#' @param position x,y,z coordinates for label (for custom positioning).
#' @param inFront Logical, if \code{TRUE} always put in front of model.
#' @param showBackground Logical, show background rounded rectangle, default
#' \code{TRUE}.
#' @param fixed Logical, setes the label to change with the model when zooming.
#' @param alignment String, how to orient the label with respect to position:
#' 'topLeft' (default), 'topCenter', 'topRight', 'centerLeft', 'center',
#' 'centerRight', 'bottomLeft', 'bottomCenter', 'bottomRight'.
#' @param frame If set, only display in this frame of an animation.
#'
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_stick()) %>%
#' m_add_res_labels(style = m_style_label(
#' fontSize = 14,
#' backgroundColor = "green"
#' )) %>%
#' m_zoom_to()
#' @export
m_style_label <- function(font = "sans-serif",
fontSize = 18,
fontColor = "white",
fontOpacity = 1,
backgroundColor = "black",
backgroundOpacity = 1,
borderOpacity = 1,
borderThickness = 0,
borderColor = backgroundColor,
inFront = TRUE,
showBackground = TRUE,
fixed = FALSE,
alignment = c(
"topLeft", "topCenter", "topRight",
"centerLeft", "center", "centerRight",
"bottomLeft", "bottomCenter",
"bottomRight"
),
position = NULL,
frame = NULL) {
alignment <- match.arg(alignment)
if (length(alignment) > 1) {
alignment <- alignment[1]
}
label_style <- list(
font = font,
fontSize = fontSize,
fontColor = fontColor,
fontOpacity = fontOpacity,
backgroundColor = backgroundColor,
backgroundOpacity = backgroundOpacity,
borderColor = borderColor,
borderThickness = borderThickness,
borderOpacity = borderOpacity,
position = position,
inFront = inFront,
showBackground = showBackground,
fixed = fixed,
alignment = alignment,
frame = frame
) %>% .cleanup_nulls()
class(label_style) <- "LabelSpec"
label_style
}
#' Specify Styling for Lines
#'
#' Styling options for the line representation. Used inside
#' \code{m_add_style()} and \code{m_set_style()}. Can also be used for styling
#' when adding individual lines with \code{m_add_line()}.
#'
#' @param colorScheme Specify scheme to color the atoms by. Default is
#' "default". Other choices are "Carbon", "ssPyMOL", "ssJmol", "Jmol",
#' "default", "amino",
#' "shapely", "nucleic", "chain", "chainHetatm", "prop".
#' @param color Fixed coloring, overrides \code{colorScheme}.
#' @param opacity Opacity, must be the same for all atoms in the model.
#' @param hidden Logical, do not show line.
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_line(color = "blue")) %>%
#' m_zoom_to()
#' @export
m_style_line <- function(colorScheme = NULL,
color = NULL,
opacity = 1,
hidden = FALSE) {
line_style <- list(
colorscheme = colorScheme,
color = color,
opacity = opacity,
hidden = hidden
) %>% .cleanup_nulls()
class(line_style) <- "LineStyleSpec"
list(line = line_style)
}
#' Specify Styling for Surface
#'
#' Styling options for the surface representation. Used inside
#' \code{m_add_surface()}.
#'
#' @param opacity Opacity, 0 for transparent, 1 for opaque.
#' @param colorScheme Specify scheme to color the atoms by. Default is
#' "default". Other choices are "Carbon", "ssPyMOL", "ssJmol", "Jmol",
#' "default", "amino",
#' "shapely", "nucleic", "chain", "chainHetatm", "prop".
#' @param color Fixed coloring, overrides colorScheme.
#' @examples
#' r3dmol() %>%
#' m_add_model(data = pdb_1j72, format = "pdb") %>%
#' m_set_style(style = m_style_stick()) %>%
#' m_add_surface(style = m_style_surface(opacity = 0.4)) %>%
#' m_zoom_to()
#' @export
m_style_surface <- function(opacity = 1,
colorScheme = NULL,
color = NULL) {
surface_style <- list(
opacity = opacity,
colorscheme = colorScheme,
color = color
) %>% .cleanup_nulls()
class(surface_style) <- "SurfaceStyleSpec"
surface_style
}
#' Specify Styling for Generic Shapes
#'
#' Styling options for the various shapes. Used inside \code{m_add_sphere()},
#' \code{m_add_arrow()}, \code{m_add_cylinder()} etc.
#'
#' @param color Solid color values.
#' @param opacity Transparency value. 1 for opaque, 0 for invisible.
#' @param wireframe Draw as wireframe, not solid surface.
#' @param hidden If true, do not display object.
#' @param frame If set, only display in this frame of an animation.
#' @param clickable If true, user can click on object to trigger callback.
#' @param callback Function to call on click.
#' @param hoverable Logical, enabling hover_callback and unhover_callback
#' functions to be called. Set hoverDuration in the viewer_spec() of r3dmol().
#' @param hover_callback Function to be called upon hover.
#' @param unhover_callback Function to be called upon hover stopping.
#' @examples
#' library(r3dmol)
#' \dontrun{
#' r3dmol() %>%
#' m_add_model(data = m_fetch_pdb("1bna")) %>%
#' m_add_sphere(
#' center = m_sel(resi = 1),
#' spec = m_shape_spec(color = "green", wireframe = TRUE)
#' ) %>%
#' m_zoom_to(sel = m_sel(resi = 1))
#' }
#' @export
m_shape_spec <- function(color = NULL,
opacity = 1.0,
wireframe = FALSE,
hidden = FALSE,
frame = NULL,
clickable = FALSE,
callback = NULL,
hoverable = FALSE,
hover_callback = NULL,
unhover_callback = NULL) {
shape_spec <- list(
color = color,
alpha = opacity,
wireframe = wireframe,
hidden = hidden,
frame = frame,
clickable = clickable,
callback = callback,
hoverable = hoverable,
hover_callback = hover_callback,
unhover_callback = unhover_callback
) %>% .cleanup_nulls()
class(shape_spec) <- "ShapeSpec"
shape_spec
}