@@ -24,64 +24,55 @@ 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 )
38- assert_that(all(! is.na(weights )))
39-
40- weights <- weights / rowSums(weights )
41- positions <- cell_positions %> %
42- select(.data $ cell_id , .data $ comp_1 , .data $ comp_2 ) %> %
43- slice(match(colnames(weights ), .data $ cell_id )) %> %
44- column_to_rownames(" cell_id" ) %> %
45- as.matrix()
37+ # calculate positions
38+ waypoint_positions <-
39+ if (! is.null(edge_positions )) {
40+ comp_names <- colnames(edge_positions ) %> % keep(function (x ) grepl(" comp_" , x ))
4641
47- # make sure weights and positions have the same cell_ids in the same order
48- assert_that(all.equal(colnames(weights ), rownames(positions )))
42+ wps $ progressions %> %
43+ select(.data $ from , .data $ to ) %> %
44+ unique() %> %
45+ pmap_df(function (from , to ) {
46+ wp_progr <- wps $ progressions %> % filter(.data $ from == !! from , .data $ to == !! to )
47+ edge_pos <- edge_positions %> % filter(.data $ from == !! from , .data $ to == !! to )
48+ for (cn in comp_names ) {
49+ wp_progr [[cn ]] <- approx(edge_pos $ percentage , edge_pos [[cn ]], wp_progr $ percentage )$ y
50+ }
51+ wp_progr
52+ }) %> %
53+ select(.data $ waypoint_id , !! comp_names ) %> %
54+ left_join(wps $ waypoints , " waypoint_id" )
55+ } else {
56+ # project wps to dimensionality reduction using kernel and geodesic distances
57+ weights <- wps $ geodesic_distances %> % stats :: dnorm(sd = trajectory_projection_sd )
58+ assert_that(all(! is.na(weights )))
4959
50- # calculate positions
51- matrix_to_tibble <- function ( x , rownames_column ) {
52- y <- as_tibble( x )
53- y [[ rownames_column ]] <- rownames( x )
54- y
55- }
60+ weights <- weights / rowSums( weights )
61+ positions <- cell_positions % > %
62+ select( .data $ cell_id , .data $ comp_1 , .data $ comp_2 ) % > %
63+ slice(match(colnames( weights ), .data $ cell_id )) % > %
64+ column_to_rownames( " cell_id " ) % > %
65+ as.matrix()
5666
57- if (! is.null(edge_positions )) {
58- approx_funs <-
59- edge_positions %> %
60- gather(.data $ comp_name , .data $ 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- )
67+ # make sure weights and positions have the same cell_ids in the same order
68+ assert_that(all.equal(colnames(weights ), rownames(positions )))
7069
71- waypoint_position <-
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- }
70+ (weights %*% positions ) %> %
71+ as.data.frame() %> %
72+ rownames_to_column(" waypoint_id" ) %> %
73+ left_join(wps $ waypoints , " waypoint_id" ) %> %
74+ as_tibble()
75+ }
8576
8677
8778 # add color of closest cell
@@ -99,7 +90,7 @@ project_waypoints_coloured <- function(
9990
10091 segments <- left_join(
10192 waypoint_positions ,
102- waypoints $ progressions ,
93+ wps $ progressions ,
10394 by = " waypoint_id"
10495 ) %> %
10596 mutate(group = factor (paste0(.data $ from , " ---" , .data $ to ))) %> %
0 commit comments