@@ -24,17 +24,18 @@ project_waypoints_coloured <- function(
2424 trajectory_projection_sd = sum(trajectory $ milestone_network $ length ) * 0.05 ,
2525 color_trajectory = " none"
2626) {
27- waypoints $ waypoint_network <- waypoints $ waypoint_network %> %
27+ wps <- waypoints
28+ wps $ waypoint_network <- wps $ waypoint_network %> %
2829 rename(
2930 milestone_id_from = .data $ from_milestone_id ,
3031 milestone_id_to = .data $ to_milestone_id
3132 )
3233
3334 assert_that(color_trajectory %in% c(" nearest" , " none" ))
34- assert_that(setequal(cell_positions $ cell_id , colnames(waypoints $ geodesic_distances )))
35+ assert_that(setequal(cell_positions $ cell_id , colnames(wps $ geodesic_distances )))
3536
36- # project waypoints to dimensionality reduction using kernel and geodesic distances
37- weights <- waypoints $ geodesic_distances %> % stats :: dnorm(sd = trajectory_projection_sd )
37+ # project wps to dimensionality reduction using kernel and geodesic distances
38+ weights <- wps $ geodesic_distances %> % stats :: dnorm(sd = trajectory_projection_sd )
3839 assert_that(all(! is.na(weights )))
3940
4041 weights <- weights / rowSums(weights )
@@ -54,34 +55,28 @@ project_waypoints_coloured <- function(
5455 y
5556 }
5657
57- if (! is.null(edge_positions )) {
58- approx_funs <-
59- edge_positions %> %
60- gather(" comp_name" , " comp_value" , starts_with(" comp_" )) %> %
61- group_by(.data $ from , .data $ to , .data $ comp_name ) %> %
62- summarise(
63- approx_fun = {
64- pct <- .data $ percentage
65- cv <- .data $ comp_value
66- list (function (x ) stats :: approx(pct , cv , x )$ y )
67- },
68- .groups = " drop"
69- )
58+ waypoint_positions <-
59+ if (! is.null(edge_positions )) {
60+ comp_names <- colnames(edge_positions ) %> % keep(function (x ) grepl(" comp_" , x ))
7061
71- waypoint_positions <-
72- waypoints $ progressions %> %
73- left_join(approx_funs , by = c(" from" , " to" )) %> %
74- mutate(
75- comp_value = map2_dbl(.data $ approx_fun , .data $ percentage , function (f , pct ) f(pct ))
76- ) %> %
77- spread(.data $ comp_name , .data $ comp_value ) %> %
78- select(.data $ waypoint_id , starts_with(" comp_" )) %> %
79- left_join(waypoints $ waypoints , " waypoint_id" )
80- } else {
81- waypoint_positions <- (weights %*% positions ) %> %
82- matrix_to_tibble(" waypoint_id" ) %> %
83- left_join(waypoints $ waypoints , " waypoint_id" )
84- }
62+ wps $ progressions %> %
63+ select(.data $ from , .data $ to ) %> %
64+ unique() %> %
65+ pmap_df(function (from , to ) {
66+ wp_progr <- wps $ progressions %> % filter(.data $ from == !! from , .data $ to == !! to )
67+ edge_pos <- edge_positions %> % filter(.data $ from == !! from , .data $ to == !! to )
68+ for (cn in comp_names ) {
69+ wp_progr [[cn ]] <- approx(edge_pos $ percentage , edge_pos [[cn ]], wp_progr $ percentage )$ y
70+ }
71+ wp_progr
72+ }) %> %
73+ select(.data $ waypoint_id , !! comp_names ) %> %
74+ left_join(wps $ waypoints , " waypoint_id" )
75+ } else {
76+ (weights %*% positions ) %> %
77+ matrix_to_tibble(" waypoint_id" ) %> %
78+ left_join(wps $ waypoints , " waypoint_id" )
79+ }
8580
8681
8782 # add color of closest cell
@@ -99,7 +94,7 @@ project_waypoints_coloured <- function(
9994
10095 segments <- left_join(
10196 waypoint_positions ,
102- waypoints $ progressions ,
97+ wps $ progressions ,
10398 by = " waypoint_id"
10499 ) %> %
105100 mutate(group = factor (paste0(.data $ from , " ---" , .data $ to ))) %> %
0 commit comments