Skip to content

Commit

Permalink
Merge pull request #1 from skleinbo/v0.3
Browse files Browse the repository at this point in the history
Release V0.3
  • Loading branch information
skleinbo committed Oct 17, 2022
2 parents 4ee6d53 + feb347f commit 1086186
Show file tree
Hide file tree
Showing 10 changed files with 1,652 additions and 173 deletions.
18 changes: 13 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
name = "VisualGrowthDynamics"
uuid = "2bd58e1e-1b99-47b5-a98e-b1d95625cfec"
authors = ["Stephan Kleinboelting <skleinbo@thp.uni-koeln.de>"]
version = "0.2.1"
version = "0.3.0"

[deps]
AbstractPlotting = "537997a7-5e4e-5d89-9595-2241ea00577e"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
GrowthDynamics = "708a6a61-856c-5d7f-acbf-fcf0902d75eb"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Makie = "≥ 0.11"
julia = "1"
FileIO = "1.15"
GLMakie = "0.6"
GeometryBasics = "0.4"
GrowthDynamics = "0.6"
Makie = "0.17"
MeshIO = "0.4"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
96 changes: 96 additions & 0 deletions assets/fccvertex.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
v 0.0 1.4142135 0.0
v -0.70710677 0.70710677 -0.70710677
v 0.70710677 0.70710677 -0.70710677
v -0.70710677 0.70710677 -0.70710677
v 0.0 0.0 -1.4142135
v 0.70710677 0.70710677 -0.70710677
v 0.70710677 0.70710677 -0.70710677
v 0.0 0.0 -1.4142135
v 1.4142135 0.0 0.0
v 0.0 0.0 -1.4142135
v 0.70710677 -0.70710677 -0.70710677
v 1.4142135 0.0 0.0
v 0.0 0.0 -1.4142135
v -0.70710677 -0.70710677 -0.70710677
v 0.70710677 -0.70710677 -0.70710677
v -0.70710677 -0.70710677 -0.70710677
v 0.0 -1.4142135 0.0
v 0.70710677 -0.70710677 -0.70710677
v -0.70710677 0.70710677 -0.70710677
v -1.4142135 0.0 0.0
v 0.0 0.0 -1.4142135
v -1.4142135 0.0 0.0
v -0.70710677 -0.70710677 -0.70710677
v 0.0 0.0 -1.4142135
v 0.0 0.0 1.4142135
v -0.70710677 0.70710677 0.70710677
v 0.70710677 0.70710677 0.70710677
v -0.70710677 0.70710677 0.70710677
v 0.0 1.4142135 0.0
v 0.70710677 0.70710677 0.70710677
v 0.70710677 -0.70710677 0.70710677
v 0.0 0.0 1.4142135
v 1.4142135 0.0 0.0
v 0.0 0.0 1.4142135
v 0.70710677 0.70710677 0.70710677
v 1.4142135 0.0 0.0
v 0.0 -1.4142135 0.0
v -0.70710677 -0.70710677 0.70710677
v 0.70710677 -0.70710677 0.70710677
v -0.70710677 -0.70710677 0.70710677
v 0.0 0.0 1.4142135
v 0.70710677 -0.70710677 0.70710677
v -0.70710677 -0.70710677 0.70710677
v -1.4142135 0.0 0.0
v 0.0 0.0 1.4142135
v -1.4142135 0.0 0.0
v -0.70710677 0.70710677 0.70710677
v 0.0 0.0 1.4142135
v -0.70710677 -0.70710677 -0.70710677
v -1.4142135 0.0 0.0
v 0.0 -1.4142135 0.0
v -1.4142135 0.0 0.0
v -0.70710677 -0.70710677 0.70710677
v 0.0 -1.4142135 0.0
v 0.70710677 -0.70710677 -0.70710677
v 0.0 -1.4142135 0.0
v 1.4142135 0.0 0.0
v 0.0 -1.4142135 0.0
v 0.70710677 -0.70710677 0.70710677
v 1.4142135 0.0 0.0
v 0.70710677 0.70710677 0.70710677
v 0.0 1.4142135 0.0
v 1.4142135 0.0 0.0
v 0.0 1.4142135 0.0
v 0.70710677 0.70710677 -0.70710677
v 1.4142135 0.0 0.0
v -0.70710677 0.70710677 0.70710677
v -1.4142135 0.0 0.0
v 0.0 1.4142135 0.0
v -1.4142135 0.0 0.0
v -0.70710677 0.70710677 -0.70710677
v 0.0 1.4142135 0.0
f 1 2 3
f 4 5 6
f 7 8 9
f 10 11 12
f 13 14 15
f 16 17 18
f 19 20 21
f 22 23 24
f 25 26 27
f 28 29 30
f 31 32 33
f 34 35 36
f 37 38 39
f 40 41 42
f 43 44 45
f 46 47 48
f 49 50 51
f 52 53 54
f 55 56 57
f 58 59 60
f 61 62 63
f 64 65 66
f 67 68 69
f 70 71 72
139 changes: 73 additions & 66 deletions src/ColorFunctions.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module ColorFunctions
using Colors
import ..Lattices: intersect_lattice_with_plane, dimension
using LightGraphs
import ..Lattices: intersect_lattice_with_plane, dimension, density
import Graphs: has_edge, neighborhood, nv, SimpleDiGraph, vertices
import GrowthDynamics.Lattices: neighbors
import GrowthDynamics.TumorConfigurations: gindex, TumorConfiguration

is_leaf(g::SimpleDiGraph, s) = !mapreduce(|, vertices(g)) do in; has_edge(g, in, s) end

mutable struct ColorMapping
inner::Dict{Any, Color}
full::Dict{Any, Color}
mutable struct ColorMapping{T}
inner::Vector{T}
full::Dict{T, <:Colorant}
end
ColorMapping(seed::Dict{<:Any, <:Color}) = ColorMapping(seed, copy(seed))
min_colors(palette) = ColorMapping(Dict([0 =>colorant"white", 1 => palette[1]]))
ColorMapping(seed::Dict{T, <:Colorant}) where T = ColorMapping(collect(keys(seed)), copy(seed))
min_colors(palette) = ColorMapping(Dict([0=>colorant"transparent"]))

default_palette = distinguishable_colors(256, colorant"midnightblue")
default_palette = distinguishable_colors(256, Colors.JULIA_LOGO_COLORS[:blue])

"""
lighten(C, p)
Expand All @@ -27,79 +29,84 @@ module ColorFunctions
convert(T, HSV(hsv.h, min(hsv.s * (1-0*p), 1.0), min(hsv.v * (1+p), 1.0) ))
end

function color_phylo!(C::ColorMapping, state; depth=1,
palette=default_palette, transition=(x::Color, d)->x)

P = state.Phylogeny
"""
color_lineages!(C, state; roots=[1])
Assign to each genotype in `roots` a distinct color from `palette`;
assign all children the same color as their root.
Previous assignments are kept.
Expects a `ColorMapping` as its first argument.
# Additional keyword arguments
* `palette`: vector of colors for the root nodes.
"""
function color_lineages!(C::ColorMapping{T}, state;
roots=[one(T)],
palette=default_palette,
default=colorant"transparent",
kwargs...) where T

P = state.phylogeny

# Indices of root nodes
iroots = gindex.(Ref(state.meta), roots)
# no. of root colors in use
j = length(C.inner)
# root colors
inner_colors = filter(x->x[1] in C.inner, pairs(C.full))
C.full = Dict(inner_colors)

## Find all vertices which are at most `depth` removed from the root.
## Each of which is going to be assigned its own color.
## They form the 'inner' part.
if depth == -1
depth = nv(P)
end
ring = neighborhood(P, 1, depth, dir=:in)
# How many colors are currently in use?
j = length(unique(values(C.inner))) - 1

### Prune inner colors ###
# Assign genotypes to indices.
inner_gs = map(x->state.meta.genotypes[x], ring)
# If a genotype is no longer present, remove it from the color-map.
for k in keys(C.inner)
if k!=0 && !(k in inner_gs)
delete!(C.inner, k)
j -= 1
# @info "$k pruned"
end
end
# Assign colors to new genotypes within the inner ring.
for ringv in ring
g = state.meta.genotypes[ringv]
if !haskey(C.inner ,g)
push!(C.inner, g => palette[j%length(palette)+1])
for (root, iroot) in zip(roots, iroots)
if !in(root, C.inner)
push!(C.inner, root)
C.full[root] = current_color = palette[(j-1)%length(palette)+1]
else
current_color = C.full[root]
end
j += 1
end

### Build a new _full_ color-map from the inner colors.
C.full = copy(C.inner)
# @info C.inner
for ringv in ring
nb = neighborhood(P, ringv, nv(P), dir=:in)
g_parent = state.meta.genotypes[ringv]
for (d,n) in enumerate(nb)
g = state.meta.genotypes[n]
newcolor = range(C.inner[g_parent], colorant"white", length=round(Int, 1.5*length(nb)))[d]
#oc = RGBA(C.inner[g_parent])
#newcolor = RGBA(oc.r, oc.g, oc.b, clamp( oc.alpha*(1-0.3), 0., 1.))
push!(C.full, g => newcolor)
# Fill in descendants with the same color
for child in neighborhood(P, iroot, nv(P), dir=:in)
g_child = state.meta[child, :genotype]
C.full[g_child] = current_color
end
j += 1
end

map(reshape(state.lattice.data, length(state.lattice.data))) do s
C.full[s]
map(state.lattice.data) do s
get(C.full, s, default)
end
end
color_phylo(state; depth=1, palette=default_palette, kwargs...) = color_phylo!(min_colors(palette), state; depth=depth, kwargs...)
color_lineages(state; palette=default_palette, kwargs...) = color_lineages!(min_colors(palette), state; kwargs...)

function color_depth!(C::ColorMapping, state; depth=2, kwargs...)
inner = neighborhood(state.phylogeny, 1, depth, dir=:in)
ginner = state.meta[inner, :genotypes]
color_lineages!(C, state; roots=ginner, kwargs...)
end
color_depth(state; depth=2, palette=default_palette, kwargs...) = color_depth!(min_colors(palette), state; depth, palette, kwargs...)

function color_phylo_by_genomic_distance!(C::ColorMapping, state; depth=1,
palette=reverse(colormap("Blues", 20))
palette=reverse(colormap("Blues", 20)),
default=colorant"transparent"
)
P = state.Phylogeny
P = state.phylogeny

C.full = copy(C.inner)
inner_colors = filter(x->x[1] in C.inner, pairs(C.full))
C.full = Dict(inner_colors)

for j in 2:length(state.meta.genotypes)
l = length(state.meta.snps[j])
# push!(C.full, state.meta.genotypes[j] => palette[l%length(palette)+1])
push!(C.full, state.meta.genotypes[j] => palette[min(l, 20)] )
for j in 2:length(state.meta)
l = length(state.meta[j, :snps])
push!(C.full, state.meta[j, :genotype] => palette[min(l, 20)] )
end

map(reshape(state.lattice.data, state.lattice.Na^2)) do s
C.full[s]
map(state.lattice.data) do s
get(C.full, s , default)
end
end
color_phylo_by_genomic_distance(state; depth=1, palette=default_palette, kwargs...) = color_phylo_by_genomic_distance!(min_colors(palette), state; depth, palette, kwargs...)

function color_generic(state;palette=rand(RGB,maximum(state.lattice.data)))
v0 = state.lattice.data
map(v0) do x
Expand All @@ -108,7 +115,7 @@ module ColorFunctions
else
RGBA{Float32}(palette[x])
end
end |> X->reshape(X, length(v0))
end
end

function color_genotype(state, g; color=colorant"blue")
Expand All @@ -120,7 +127,7 @@ module ColorFunctions
else
color
end
end |> X->reshape(X, length(v0))
end
end

function color_neighbors(state, I::CartesianIndex)
Expand Down Expand Up @@ -156,7 +163,7 @@ module ColorFunctions
function hide!(v, A::BitArray)
for i in eachindex(v)
if !A[i]
v[i] = RGBA{Float32}(0., 0., 0., 0.)
v[i] = colorant"transparent"
end
end
v
Expand Down
Loading

0 comments on commit 1086186

Please sign in to comment.