Skip to content

Commit

Permalink
Created Ribbon module
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOresten committed Nov 14, 2023
1 parent 454e70a commit 275ad98
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
9 changes: 3 additions & 6 deletions src/ProtPlot.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
module ProtPlot

using Backboner
import Backboner
export Backboner

using GLMakie
using Colors, ColorSchemes
using LinearAlgebra

include("utils/utils.jl")
include("ribbon/ribbon.jl")

export Ribbon

end
13 changes: 12 additions & 1 deletion src/ribbon/ribbon.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module Ribbon

using Backboner
using GLMakie
using Colors
using ColorSchemes
using LinearAlgebra

include("utils/utils.jl")
include("segment.jl")
include("shapes.jl")
include("render.jl")
include("render.jl")

end
11 changes: 11 additions & 0 deletions src/ribbon/segment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function segments(chain::Chain)
end


# utilities

function segment_startpoint(segment::Segment, A=1) # nitrogen
return segment.backbone.coords[:,A,1] # first residue
end
Expand All @@ -96,4 +98,13 @@ function segment_endpoint(segment::Segment, A=1) # nitrogen
else
return segment.chain.backbone[:,A,segment_stop+1] # next residue
end
end

function remove_singleton_strands!(chain::Chain)
ssvector = chain.ssvector
for i in 2:length(ssvector)-1
if ssvector[i-1] != Strand && ssvector[i] == Strand && ssvector[i+1] != Strand
ssvector[i] = Loop
end
end
end
14 changes: 8 additions & 6 deletions src/ribbon/shapes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ function tube_surface(
surface_vertices = zeros(T, 3, N, length(tube_angles))

# Precompute tangents and initialize normals
tangents = [path_tangent(path[:, max(1, i-1)], path[:, i], path[:, min(N, i+1)]) for i in 1:N]
tangents = stack(path_tangent(path[:, max(1, i-1)], path[:, i], path[:, min(N, i+1)]) for i in 1:N)
normals = zeros(T, 3, N)

# Initial normal vector (arbitrary, but not aligned with the first tangent)
normals[:, 1] = abs(tangents[1][1]) < 0.9 ? [1, 0, 0] : [0, 1, 0]
normals[:, 1] = abs(tangents[1, 1]) < 0.9 ? [1, 0, 0] : [0, 1, 0]

# Propagate the normal vector along the path
for idx in 2:N
prev_normal = normals[:, idx-1]
t = tangents[idx]
t = tangents[:, idx]
projected_normal = prev_normal - dot(t, prev_normal) * t
if norm(projected_normal) < 1e-5
projected_normal = cross(tangents[idx - 1], t)
projected_normal = cross(tangents[:, idx - 1], t)
end
normals[:, idx] = normalize!(projected_normal)
end

# Generate surface vertices
for idx in 1:N
t = tangents[idx]
t = tangents[:, idx]
n = normals[:, idx]
b = cross(n, t)

Expand Down Expand Up @@ -108,6 +108,8 @@ end
# 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

# TODO: just use oxygen atoms for calculating paths

function arrow_surface(
points1::AbstractMatrix{T},
points2::AbstractMatrix{T};
Expand Down Expand Up @@ -135,7 +137,7 @@ function arrow_surface(
arrow_head_length = 3.0
arrow_body_length = length_of_path - arrow_head_length
l = findfirst(>(arrow_body_length), cumulative_length_of_path) / N
arrow = arrow_function(l, width, width*1.8)
arrow = arrow_function(l, width, width*2.0)

surface_vertices = zeros(T, 3, N, 5)
for idx in 1:N
Expand Down
11 changes: 0 additions & 11 deletions src/utils/utils.jl → src/ribbon/utils/colors.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
include("splines.jl")

function remove_singleton_strands!(chain::Chain)
ssvector = chain.ssvector
for i in 2:length(ssvector)-1
if ssvector[i-1] != Strand && ssvector[i] == Strand && ssvector[i+1] != Strand
ssvector[i] = Loop
end
end
end

# expand vector to have length N while keeping the same discrete color gradient
function expand_colors(colors::Vector, N::Integer)
L = length(colors)
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/ribbon/utils/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include("splines.jl")
include("colors.jl")

0 comments on commit 275ad98

Please sign in to comment.