Skip to content

Commit

Permalink
trying things
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio committed Apr 19, 2023
1 parent cc66832 commit 653c61e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
89 changes: 52 additions & 37 deletions src/makie.jl
Original file line number Diff line number Diff line change
@@ -1,70 +1,85 @@
function makie_sequence_logo!(
ax::Makie.Axis, weights::AbstractMatrix, letters::AbstractMatrix, colors::AbstractMatrix
ax::Makie.Axis,
weights::AbstractMatrix{<:Real},
letters::AbstractMatrix{<:AbstractChar},
colors::AbstractMatrix;
font=("Arial", :bold),
)
@assert size(weights) == size(letters) == size(colors)
x, y = compute_positions(weights)
for i in axes(weights, 1), j in axes(weights, 2)
Makie.text!(
ax, x[i,j], y[i,j];
text=string(letters[i,j]),
color=colors[i,j],
fontsize=Makie.Vec2f(1, abs(weights[i,j])),
#fontsize=Makie.Vec2f(1, 3),
align=(:center, :center),
markerspace=:data,
space=:data
)
end

# for i in axes(weights, 2)
# y_pos = y_neg = 0.0
# for k in axes(weights, 1)
# if weights[k,i] > 0
# y = y_pos
# y_pos += weights[k,i]
# elseif weights[k,i] < 0
# y = y_neg
# y_neg += weights[k,i]
# end

# Makie.text!(
# ax, i, y; text=string(letters[k,i]),
# textsize=abs(weights[k,i]), scale=Makie.Vec2(1/abs(weights[k,i]), 1),
# color=colors[k,i],
# markerspace=:data, align=(:center, :baseline), font="Arial bold"
# )
# Makie.scale
# end
# end

# Makie.text!(
# ax, vec(x), vec(y);
# text=vec(string.(letters)),
# #text=vec(letters),
# color=vec(colors),
# #font,
# fontsize=vec(Makie.Vec2f.(1, abs.(weights))),
# #fontsize=Makie.Vec2f(1, 2),
# align=(:center, :center),
# #justification=0,
# #overdraw=true,
# #space=:data,
# markerspace=:data
# )

# return nothing
return
end

function compute_positions(weights::AbstractMatrix{<:Real})
x = repeat((1:size(weights, 2))', size(weights, 1), 1)
y = zero(weights)
@assert size(x) == size(y) == size(weights)
for i in axes(weights, 2)
y_pos = y_neg = 0.0
for k in axes(weights, 1)
if weights[k,i] > 0
y[k,i] = y_pos + weights[k,i]/2
if weights[k,i] 0
y[k,i] = y_pos + weights[k,i] / 2
y_pos += weights[k,i]
elseif weights[k,i] < 0
y[k,i] = y_neg + weights[k,i]/2
y[k,i] = y_neg
y_neg += weights[k,i]
end
end
end

Makie.text!(
ax, vec(x), vec(y); text=vec(letters),
fontsize=vec([Makie.Vec2(1, abs(w)) for w in weights]),
color=vec(colors),
markerspace=:data, align=(:center, :baseline), font="Arial"
)
return x, y
end

function makie_sequence_logo!(
ax::Makie.Axis, weights::AbstractMatrix, letters::AbstractVector, colors::AbstractVector
ax::Makie.Axis,
weights::AbstractMatrix{<:Real},
letters::AbstractVector{<:AbstractChar},
colors::AbstractVector;
kwargs...
)
@assert size(weights, 1) == length(letters) == length(colors)
sorted_weights, sorted_letters, sorted_colors = sort_logo(weights, letters, colors)
return makie_sequence_logo!(ax, sorted_weights, sorted_letters, sorted_colors)
return makie_sequence_logo!(ax, sorted_weights, sorted_letters, sorted_colors; kwargs...)
end

function sort_logo(weights::AbstractMatrix, letters::AbstractVector, colors::AbstractVector)
function sort_logo(
weights::AbstractMatrix{<:Real},
letters::AbstractVector{<:AbstractChar},
colors::AbstractVector
)
@assert size(weights, 1) == length(letters) == length(colors)
sorted_weights = similar(weights)
sorted_letters = similar(letters, size(weights))
sorted_colors = similar(colors, size(weights))
for i in axes(weights, 2)
p = sortperm(weights[:,i])
p = sortperm(weights[:,i]; by=abs)
sorted_weights[:,i] .= weights[p,i]
sorted_letters[:,i] .= letters[p]
sorted_colors[:,i] .= colors[p]
Expand Down
22 changes: 17 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ using Statistics: mean
using LogExpFunctions: xlogx
import FASTX
using Base: front
import Makie, CairoMakie
import Makie
import CairoMakie

records = collect(FASTX.FASTA.Reader(open(__example_fasta())))
seqs = FASTX.sequence.(records)

NTs = "ACGU-"
NTs = "ACGU"

function onehot(s::AbstractString)
return reshape(collect(s), 1, length(s)) .== collect(NTs)
Expand All @@ -18,10 +19,21 @@ X = reshape(reduce(hcat, onehot.(seqs)), length(NTs), :, length(seqs))

p = reshape(mean(X; dims=3), size(X, 1), size(X, 2))
H = sum(-xlogx.(p) / log(2); dims=1)
cons = p .* (log2(5) .- H)
weights = p .* (log2(5) .- H)

weights .= mean(weights)

weights = [
1.0 1.0;
2.0 3.0
]
letters = ['A'; 'C']
colors = [:red; :blue]

fig = Makie.Figure()
ax = Makie.Axis(fig[1,1]; width=500, height=200)
makie_sequence_logo!(ax, cons, collect(NTs), [:red, :blue, :orange, :green, :black])
ax = Makie.Axis(fig[1,1]; width=1000, height=200)
tmp = makie_sequence_logo!(ax, weights, letters, colors)
Makie.resize_to_layout!(fig)
fig

Makie.Vec2f.(weights)

0 comments on commit 653c61e

Please sign in to comment.