Skip to content

Commit

Permalink
Add customizability with kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOresten committed Nov 26, 2023
1 parent 5fe36b6 commit 2cdab03
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
43 changes: 33 additions & 10 deletions src/ribbon/render.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
function render!(
container,
segment::Segment{Loop},
colors::AbstractVector{<:RGB} = [colorant"red", colorant"yellow"],
colors::AbstractVector{<:RGB} = [colorant"red", colorant"yellow"];
radius = 0.25,
spline_quality = 20,
slice_quality = 20,
kwargs...
)
extended_segment = extend_segment(segment, 0:length(segment)+1)
startpoint = segment_startpoint(segment)
endpoint = segment_endpoint(segment)
controls = @view alphacarbon_coord_matrix(segment.backbone)[:, (isone(end) ? 1 : 2):end]
coords = [startpoint controls endpoint]
surface_vertices = tube_surface(coords, 0.25, spline_quality=20, tube_quality=20,
ghost_control_start=segment_startpoint(extended_segment), ghost_control_end=segment_endpoint(extended_segment))
surface_vertices = tube_surface(coords,
radius=radius, spline_quality=spline_quality, slice_quality=slice_quality,
ghost_control_start=segment_startpoint(extended_segment), ghost_control_end=segment_endpoint(extended_segment)
)
N = size(surface_vertices, 2)
color_matrix = expand_colors(colors, N)
surface!(container, eachslice(surface_vertices, dims=1)..., color=color_matrix)
Expand All @@ -20,13 +26,22 @@ end
function render!(
container,
segment::Segment{Helix},
colors::AbstractVector{<:RGB} = [colorant"lime", colorant"cyan"],
colors::AbstractVector{<:RGB} = [colorant"lime", colorant"cyan"];
helix_radius = 1.0,
helix_width = 1.0,
helix_thickness = 0.25,
spline_quality = 20,
slice_quality = 20,
kwargs...
)
startpoint = segment_startpoint(segment)
endpoint = segment_endpoint(segment)
controls = @view alphacarbon_coord_matrix(segment.backbone)[:, 2:end] # startpoint is first point instead. including first N *and* CA could mess with normals
coords = hcat(startpoint, controls, endpoint)
surface_vertices = helix_surface(coords, 1, spline_quality=20, tube_quality=20, x_elongation=0.25)
surface_vertices = helix_surface(coords,
radius=helix_radius, width_factor=helix_width, thickness_factor=helix_thickness,
spline_quality=spline_quality, slice_quality=slice_quality,
)
N = size(surface_vertices, 2)
color_matrix = expand_colors(colors, N)
surface!(container, eachslice(surface_vertices, dims=1)..., color=color_matrix)
Expand All @@ -37,15 +52,21 @@ end
function render!(
container,
segment::Segment{Strand},
colors::AbstractVector{<:RGB} = [colorant"blue", colorant"magenta"],
colors::AbstractVector{<:RGB} = [colorant"blue", colorant"magenta"];
strand_width = 2.0,
strand_thickness = 0.5,
spline_quality = 20,
kwargs...
)
startpoint = segment_startpoint(segment)
endpoint = segment_endpoint(segment)
oxygen_coords_side1 = @view oxygen_coord_matrix(segment.backbone)[:, 1:2:end-1]
oxygen_coords_side2 = @view oxygen_coord_matrix(segment.backbone)[:, 2:2:end]
coords1 = hcat(startpoint, oxygen_coords_side1, endpoint)
coords2 = hcat(startpoint, oxygen_coords_side2, endpoint)
surface_vertices = arrow_surface(coords1, coords2, width=1.1, thickness=0.5, spline_quality=20)
surface_vertices = arrow_surface(coords1, coords2,
width=strand_width, thickness=strand_thickness, spline_quality=spline_quality,
)
N = size(surface_vertices, 2)
color_matrix = expand_colors(colors, N)
surface!(container, eachslice(surface_vertices, dims=1)..., color=color_matrix)
Expand All @@ -56,12 +77,13 @@ end
function render!(
container,
chain::Chain,
colors::AbstractVector{<:RGB},
colors::AbstractVector{<:RGB};
kwargs...
)
@assert length(chain) == length(colors)
@assert !has_missing_ss(chain)
for segment in segments(chain)
render!(container, segment, colors[segment.range])
render!(container, segment, colors[segment.range]; kwargs...)
end

return container
Expand All @@ -72,6 +94,7 @@ function render!(
protein::Protein;
colorscheme::Union{ColorScheme, Symbol} = :jet,
color_vectors::AbstractVector{C} = [LinRange(0, 1, length(chain)) for chain in protein],
kwargs...
) where C <: AbstractVector{<:Union{Real, RGB}}
colorscheme isa Symbol && (colorscheme = colorschemes[colorscheme])
if eltype(C) <: Real
Expand All @@ -82,7 +105,7 @@ function render!(
@assert length(protein) == length(color_vectors)
@assert length.(protein) == length.(color_vectors)
for (chain, colors) in zip(protein, color_vectors)
render!(container, chain, colors)
render!(container, chain, colors; kwargs...)
end

return container
Expand Down
6 changes: 4 additions & 2 deletions src/ribbon/shapes/arrow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ end
# W is the width of the arrow head
# the length of the arrow head becomes 1 - l
# the shape is normalized such that length of the entire arrow is 1
arrow_function(l=0.5, w=0.5, W=1.0) = t -> t > l ? W*(t-1)/(l-1) : w
arrow_function(l=0.5, w=0.5, W=1.0) = let hw = w/2, hW = W/2
t -> t > l ? hW*(t-1)/(l-1) : hw
end

function arrow_surface(
points1::AbstractMatrix{T},
points2::AbstractMatrix{T};
width = 1.0,
thickness = 0.3,
spline_quality = 10,
spline_quality = 20,
) where T <: Real
half_thickness = thickness / 2
max_L = max(size(points1, 2), size(points2, 2))
Expand Down
16 changes: 8 additions & 8 deletions src/ribbon/shapes/helix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# normal depends on second derivative of the path
# does weird twist when second derivative changes too quickly
function helix_surface(
points::AbstractMatrix{T},
radius::Real = 0.5;
spline_quality = 10,
tube_quality = 20,
x_elongation = 1,
z_elongation = 1,
points::AbstractMatrix{T};
radius = 1.0,
width_factor = 1.0,
thickness_factor = 1.0,
spline_quality = 20,
slice_quality = 20,
) where T <: Real
path = spline(points, m=spline_quality, k=min(3, size(points, 2)-1))
N = size(path, 2)
tube_angles = LinRange(0, 2π, tube_quality)
tube_angles = LinRange(0, 2π, slice_quality)
surface_vertices = zeros(T, 3, N, length(tube_angles))
for idx in 1:N
three_path_points = eachcol(@view(path[:, min(max(begin,idx-1),end-2):min(max(begin,idx-1)+2,end)])) # understandable expressions are for the weak
Expand All @@ -21,7 +21,7 @@ function helix_surface(

b = cross(n, t)
for (jdx, v) in enumerate(tube_angles)
offset = radius .* (x_elongation*cos(v) .* n .+ z_elongation*sin(v) .* b)
offset = radius .* (thickness_factor*cos(v) .* n .+ width_factor*sin(v) .* b)
surface_vertices[:, idx, jdx] = path[:, idx] .- offset
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/ribbon/shapes/tube.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function tube_surface(
points::AbstractMatrix{T},
radius::Real = 0.5;
spline_quality = 10,
tube_quality = 20,
ghost_control_start = nothing, # don't seem to be working -- should make ends of the tube line up with the next segment so it looks nicer
points::AbstractMatrix{T};
radius = 1.0,
spline_quality = 20,
slice_quality = 20,
ghost_control_start = nothing,
ghost_control_end = nothing,
) where T <: Real

Expand All @@ -15,7 +15,7 @@ function tube_surface(
end

N = size(path, 2)
tube_angles = LinRange(0, 2π, tube_quality)
tube_angles = LinRange(0, 2π, slice_quality)
surface_vertices = zeros(T, 3, N, length(tube_angles))

# Precompute tangents and initialize normals
Expand Down

0 comments on commit 2cdab03

Please sign in to comment.