Skip to content

Commit

Permalink
🎨 Remove library() from demo
Browse files Browse the repository at this point in the history
  • Loading branch information
heavywatal committed Jul 6, 2023
1 parent cc7006e commit f300818
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 91 deletions.
15 changes: 6 additions & 9 deletions demo/early.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(tidyverse)
library(tumopp)

.args = list(
"-D2 -k100 -Cmoore -Lconst -R256 -N256 -o Cmoore_Lconst",
"-D2 -k100 -Cmoore -Lstep -R256 -N256 -o Cmoore_Lstep",
Expand All @@ -9,8 +6,8 @@ library(tumopp)
"-D2 -k100 -Chex -Lstep -R256 -N256 -o Chex_Lstep",
"-D2 -k100 -Chex -Llinear -R256 -N256 -o Chex_Llinear"
)
results = tumopp(.args)
write_results(results)
results = tumopp::tumopp(.args)
# tumopp::write_results(results)

.add_clade_to_snapshots = function(population, graph, snapshots, ...) {
clade_info = tumopp:::sort_clades(population, graph, 4L)
Expand All @@ -20,13 +17,13 @@ write_results(results)

.plot_snapshot = function(data, limit) {
tumopp::plot_lattice2d(data, "clade", alpha = 1.0, limit = limit) +
scale_colour_brewer(palette = "Spectral", na.value = "grey50", guide = FALSE) +
theme_void()
ggplot2::scale_colour_brewer(palette = "Spectral", na.value = "grey50", guide = FALSE) +
ggplot2::theme_void()
}

.plot_snapshots = function(.tbl) {
.lim = tumopp::max_abs_xyz(.tbl)
tidyr::nest(.tbl, !"time")$data |>
tidyr::nest(.tbl, data = !"time")$data |>
parallel::mclapply(.plot_snapshot, limit = .lim)
}

Expand All @@ -47,7 +44,7 @@ magick_gif_animation = function(infiles, outfile = "animation.gif", delay = 15,
purrr::iwalk(.plt, \(.x, .y) {
.outfile = file.path(.pngdir, sprintf("snapshot_%03d.png", .y))
message(.outfile)
ggsave(.outfile, .x, width = 1, height = 1, scale = 6, dpi = 72)
ggplot2::ggsave(.outfile, .x, width = 1, height = 1, scale = 6, dpi = 72)
})
.infiles = file.path(.pngdir, "snapshot_*.png")
magick_gif_animation(.infiles, sprintf("%s/%s.gif", outdir, outdir), delay = 8)
Expand Down
50 changes: 23 additions & 27 deletions demo/rgl.R
Original file line number Diff line number Diff line change
@@ -1,71 +1,67 @@
# !/usr/bin/env Rscript
library(tidyverse)
library(rgl)
library(tumopp)

######## 1#########2#########3#########4#########5#########6#########7#########
## transformation

rgl::close3d()
rgl::open3d(windowRect = c(0, 0, 600, 600))
rgl::clear3d()
rgl::view3d(70, 5, 60)
axes3d()
rgl::axes3d()
.r = 2
tibble(x = seq(-.r, .r), y = x, z = x) |>
tibble::tibble(x = seq(-.r, .r), y = x, z = x) |>
tidyr::expand(x, y, z) |>
tumopp:::trans_coord_hcc() |>
# tumopp:::trans_coord_fcc() |>
dplyr::mutate(r = sqrt(x * x + y * y + z * z)) |>
with(spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
title3d("", "", "x", "y", "z")
with(rgl::spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
rgl::title3d("", "", "x", "y", "z")

######## 1#########2#########3#########4#########5#########6#########7#########
## minimum

.hex_xy = read_csv("x,y,z\n0,0,0\n1,0,0\n0,1,0\n1,0,-1")
.hex_xy = readr::read_csv(I("x,y,z\n0,0,0\n1,0,0\n0,1,0\n1,0,-1"))

rgl::close3d()
rgl::open3d(windowRect = c(0, 0, 600, 600))
rgl::clear3d()
rgl::view3d(20, 10, 60)
axes3d()
rgl::axes3d()
.hex_xy |>
trans_coord_hex() |>
with(spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
title3d("", "", "x", "y", "z")
tumopp::trans_coord_hex() |>
with(rgl::spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
rgl::title3d("", "", "x", "y", "z")

######## 1#########2#########3#########4#########5#########6#########7#########
## neighbors

.hex_xy = read_csv("x,y,z\n0,0,0\n0,1,0\n0,-1,0\n-1,0,0\n-1,1,0\n1,0,0\n1,-1,0")
.hex_xy = readr::read_csv(I("x,y,z\n0,0,0\n0,1,0\n0,-1,0\n-1,0,0\n-1,1,0\n1,0,0\n1,-1,0"))

rgl::close3d()
rgl::open3d(windowRect = c(0, 0, 600, 600))
rgl::clear3d()
rgl::view3d(20, 10, 60)
axes3d()
rgl::axes3d()
.hex_xy |>
bind_rows(.hex_xy |> dplyr::filter(x > 0 | (x == 0 & y == 0)) |> mutate(z = -1)) |>
bind_rows(.hex_xy |> dplyr::filter(x < 0 | (x == 0 & y == 0)) |> mutate(z = 1)) |>
dplyr::bind_rows(.hex_xy |> dplyr::filter(x > 0 | (x == 0 & y == 0)) |> dplyr::mutate(z = -1)) |>
dplyr::bind_rows(.hex_xy |> dplyr::filter(x < 0 | (x == 0 & y == 0)) |> dplyr::mutate(z = 1)) |>
print() |>
tumopp:::trans_coord_fcc() |>
with(spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
title3d("", "", "x", "y", "z")
with(rgl::spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
rgl::title3d("", "", "x", "y", "z")


rgl::close3d()
rgl::open3d(windowRect = c(0, 0, 600, 600))
rgl::clear3d()
rgl::view3d(40, 20, 60)
axes3d()
rgl::axes3d()
.hex_xy |>
bind_rows(.hex_xy |> mutate(z = -1) |> dplyr::filter(x < 0 | (x == 0 & y == 0))) |>
bind_rows(.hex_xy |> mutate(z = 1) |> dplyr::filter(x < 0 | (x == 0 & y == 0))) |>
dplyr::bind_rows(.hex_xy |> dplyr::mutate(z = -1) |> dplyr::filter(x < 0 | (x == 0 & y == 0))) |>
dplyr::bind_rows(.hex_xy |> dplyr::mutate(z = 1) |> dplyr::filter(x < 0 | (x == 0 & y == 0))) |>
print() |>
bind_rows(.hex_xy |> mutate(z = 5)) |>
bind_rows(.hex_xy |> mutate(z = 4) |> dplyr::filter(x > 0 | (x == 0 & y == 0))) |>
bind_rows(.hex_xy |> mutate(z = 6) |> dplyr::filter(x > 0 | (x == 0 & y == 0))) |>
dplyr::bind_rows(.hex_xy |> dplyr::mutate(z = 5)) |>
dplyr::bind_rows(.hex_xy |> dplyr::mutate(z = 4) |> dplyr::filter(x > 0 | (x == 0 & y == 0))) |>
dplyr::bind_rows(.hex_xy |> dplyr::mutate(z = 6) |> dplyr::filter(x > 0 | (x == 0 & y == 0))) |>
tumopp:::trans_coord_hcc() |>
with(spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
title3d("", "", "x", "y", "z")
with(rgl::spheres3d(x, y, z, color = "#009999", radius = 0.51, alpha = 0.6))
rgl::title3d("", "", "x", "y", "z")
31 changes: 13 additions & 18 deletions demo/run.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
library(tidyverse)
library(tumopp)

tumopp("-h")
result = tumopp("-D3 -Chex -N10000 -k1e9 -Lstep -Pmindrag")
tumopp::tumopp("-h")
result = tumopp::tumopp("-D3 -Chex -N10000 -k1e9 -Lstep -Pmindrag")

result$population[[1]] |>
filter_extant() |>
tumopp::filter_extant() |>
dplyr::filter(z == 0) |>
plot_lattice2d()
tumopp::plot_lattice2d()

result$population[[1]] |>
filter_extant() |>
add_surface(result$coord, result$dimensions) |>
tumopp::filter_extant() |>
tumopp::add_surface(result$coord, result$dimensions) |>
dplyr::filter(z == 0) |>
dplyr::filter(surface) |>
plot_lattice2d()
tumopp::plot_lattice2d()

if (result[["dimensions"]] > 2L) {
library(rgl)

rgl::close3d()
rgl::open3d(windowRect = c(0, 0, 600, 600))
result$population[[1]] |>
filter_extant() |>
add_surface(result$coord, result$dimensions) |>
tumopp::filter_extant() |>
tumopp::add_surface(result$coord, result$dimensions) |>
dplyr::filter(surface) |>
plot_tumor3d()
title3d("", "", "x", "y", "z")
tumopp::plot_tumor3d()
rgl::title3d("", "", "x", "y", "z")

.outfile = snapshot_surface(result$population[[1]])
.outfile = tumopp::snapshot_surface(result$population[[1]])
system(sprintf("open %s", .outfile))

writeWebGL(".", "rgl.html", snapshot = FALSE, width = 600, height = 600)
rgl::writeWebGL(".", "rgl.html", snapshot = FALSE, width = 600, height = 600)
} # fi 3D
72 changes: 35 additions & 37 deletions demo/sample.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
library(tidyverse)
library(tumopp)

# load_all()

(result = tumopp("-N40000 -D2 -Chex -k24 -Lconst"))
(result = tumopp::tumopp("-N40000 -D2 -Chex -k24 -Lconst"))
(population = result$population[[1L]])
(graph = result$graph[[1L]])
(extant = population |> filter_extant())
(extant = population |> tumopp::filter_extant())

(regions = sample_uniform_regions(extant, 8L, 100L))
(regions = tumopp::sample_uniform_regions(extant, 8L, 100L))
subgraph = tumopp::subtree(graph, unlist(regions$id))

extant |>
dplyr::left_join(tumopp:::tidy_regions(regions), by = "id") |>
plot_lattice2d(size = 0.3) +
geom_point(data = function(x) {
tumopp::plot_lattice2d(size = 0.3) +
ggplot2::geom_point(data = function(x) {
dplyr::filter(x, !is.na(region))
}, size = 0.3, alpha = 0.4) +
theme(axis.title = element_blank())
ggplot2::theme(axis.title = ggplot2::element_blank())

# #######1#########2#########3#########4#########5#########6#########7#########

mutated = mutate_clades(subgraph, mu = 1)
mutated = mutate_clades(subgraph, mu = -1)
mutated = mutate_clades(subgraph, segsites = 1000L)
mutated = tumopp:::mutate_clades(subgraph, mu = 1)
mutated = tumopp:::mutate_clades(subgraph, mu = -1)
mutated = tumopp:::mutate_clades(subgraph, segsites = 1000L)

.vaf = make_vaf(subgraph, regions$id, mu = -1) |> print()
.vaf = tumopp::make_vaf(subgraph, regions$id, mu = -1) |> print()
.tidy = .vaf |>
filter_detectable(0.05) |>
sort_vaf() |>
longer_vaf() |>
tumopp::filter_detectable(0.05) |>
tumopp::sort_vaf() |>
tumopp::longer_vaf() |>
print()

.tidy = make_longer_vaf(graph, regions$id, -1) |> print()

ggplot(.tidy) +
aes(sample, site) +
geom_tile(aes(fill = frequency)) +
scale_fill_distiller(palette = "Spectral", limit = c(0, 1), guide = FALSE) +
coord_cartesian(expand = FALSE)
.tidy = tumopp::make_longer_vaf(graph, regions$id, -1) |> print()

ggplot2::ggplot(.tidy) +
ggplot2::aes(sample, site) +
ggplot2::geom_tile(ggplot2::aes(fill = frequency)) +
ggplot2::scale_fill_distiller(palette = "Spectral", limit = c(0, 1), guide = FALSE) +
ggplot2::coord_cartesian(expand = FALSE)

ncell = 100L
testdf = tibble::tibble(mu = rep(c(1, 4, 16, 64), each = 200)) |>
dplyr::mutate(fst = wtl::mcmap_dbl(mu, \(x) tumopp::make_vaf(subgraph, regions$id, mu = x) |>
tumopp::dist_vaf(ncell) |>
Expand All @@ -51,27 +48,28 @@ testdf = tibble::tibble(mu = rep(c(1, 4, 16, 64), each = 200)) |>
xi = tumopp::make_vaf(subgraph, regions$id, mu = -1) |>
tumopp::dist_vaf(ncell) |>
tumopp::fst()
ggplot(testdf) +
aes(fst) +
geom_histogram(bins = 30) +
geom_vline(xintercept = xi) +
facet_wrap(vars(mu))
ggplot2::ggplot(testdf) +
ggplot2::aes(fst) +
ggplot2::geom_histogram(bins = 30) +
ggplot2::geom_vline(xintercept = xi) +
ggplot2::facet_wrap(ggplot2::vars(mu))


# #######1#########2#########3#########4#########5#########6#########7#########

distances = pairwise_distances(subgraph, regions) |> print()
distances = tumopp::pairwise_distances(subgraph, regions) |> print()

.xmax = max(distances$euclidean)
.ymax = max(max(distances$fst), 0.6)
distances |>
ggplot(aes(euclidean, fst)) +
geom_point() +
stat_smooth(method = lm, formula = y ~ x + 0) +
coord_cartesian(xlim = c(0, .xmax), ylim = c(0, .ymax))
ggplot2::ggplot() +
ggplot2::aes(euclidean, fst) +
ggplot2::geom_point() +
ggplot2::stat_smooth(method = lm, formula = y ~ x + 0) +
ggplot2::coord_cartesian(xlim = c(0, .xmax), ylim = c(0, .ymax))

m = dist_genealogy(subgraph, regions[["id"]])
fst(m)
gst(m)
m = tumopp::dist_genealogy(subgraph, regions[["id"]])
tumopp::fst(m)
tumopp::gst(m)

w = num_pairs(lengths(regions[["id"]]))
w = tumopp:::num_pairs(lengths(regions[["id"]]))

0 comments on commit f300818

Please sign in to comment.