R/build_neighbour_rank_lists.R

Defines functions build_neighbour_rank_lists

Documented in build_neighbour_rank_lists

#' Build list of cluster 'neighbourhoods' connected by proximity
#'
#' @param query_cluster numeric/character, an identifier for the cluster being queried
#' @param rank_df data frame containining marker ranks in both maps.
#'
#' @return a list containining the complete neighbourhood for the query cluster
#' @export
#'
#' @examples
build_neighbour_rank_lists <- function(query_cluster, rank_df){

  # determine the ranks of the query cluster
  cluster_sub <- rank_df %>% filter(cluster == query_cluster)
  cluster_x_rank <- cluster_sub$x_rank
  cluster_y_rank <- cluster_sub$y_rank

  # find the clusters that are 1 rank away (+/-)
  x_neighbour <- rank_df$cluster[rank_df$x_rank == (cluster_x_rank + 1) | rank_df$x_rank == (cluster_x_rank -1)]
  y_neighbour <- rank_df$cluster[rank_df$y_rank == (cluster_y_rank + 1) | rank_df$y_rank == (cluster_y_rank -1)]

  # find the clusters that are 2 rank away (+/-)
  x_p1_neighbour <- rank_df$cluster[rank_df$x_rank == (cluster_x_rank + 2) | rank_df$x_rank == (cluster_x_rank -2)]
  y_p1_neighbour <- rank_df$cluster[rank_df$y_rank == (cluster_y_rank + 2) | rank_df$y_rank == (cluster_y_rank -2)]

  # grouping (5th!) element
  # Make a 5th element that lists the numbers shared in [x]&[y], [x+1]&[y], [x]&[y+1]
  x_y_shared <- x_neighbour[x_neighbour %in% y_neighbour]
  x_p1_y_shared <- x_p1_neighbour[x_p1_neighbour %in% y_neighbour]
  x_y_p1_shared <- x_neighbour[x_neighbour %in% y_p1_neighbour]
  fifth_element <- c(x_y_shared, x_p1_y_shared, x_y_p1_shared) %>% unique

  # output named list of neighbours
  list(marker_id = query_cluster, x_neighbour = x_neighbour, y_neighbour = y_neighbour, x_p1_neighbour = x_p1_neighbour, y_p1_neighbour = y_p1_neighbour, fifth_element = fifth_element)

}
ksamuk/syntR documentation built on Feb. 11, 2021, 4:46 a.m.