Advertisement
Guest User

Untitled

a guest
Mar 5th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. source_rmd("~/cse-phd/second-trimester/neocortex/constellation_plots/R/scrattch.hicat_fxns.Rmd")
  2.  
  3. buildConstellation <- function(args_list,
  4. groups.col, colors.use,
  5. run.knn, n.pcs, k, frac.th,
  6. out.dir) {
  7.  
  8.  
  9. cells.cl.df <- args_list$cells_cl_df
  10. rd.dat <- args_list$reductions
  11. cl <- args_list$cl
  12. cl.numeric <- args_list$cl_numeric
  13.  
  14. # 1. Build dataframes for constellation plots.
  15.  
  16. # 1.1 cl_and_cl_numeric
  17. # cl and cl numeric can also be passed as a param to the Rmd.
  18.  
  19. if(is.null(cl) & is.null(cl.numeric)) {
  20.  
  21. # Cluster, or area-celltype combination.
  22. cl <- cells.cl.df[[groups.col]] %>% as.factor %>%
  23. set_names(cells.cl.df$cell.name)
  24. # 128 clusters in all _combo2_ cells
  25. # 77 clusters in all _combo2_ & ExN lineage cells.
  26.  
  27. cl.numeric <- as.numeric(cl) %>% set_names(nm = names(cl))
  28. }
  29.  
  30. ## 2 cl.df
  31.  
  32. cl.df <- get_cl_df(cl)
  33.  
  34. cl.df$area <- str_split_fixed(cl.df$cluster_label, "-", 2)[ ,2] %>% tolower
  35.  
  36. cl.df$clade <- str_split_fixed(cl.df$cluster_label, "-", 2)[ ,1] %>% tolower
  37. # Add clade_id, clade_color to cl.df
  38. # cl.df <- cl.df %>% left_join(clade.cols)
  39.  
  40.  
  41. cl.df <- cl.df %>% left_join(colors.use, by = c("clade" = "celltype"))
  42.  
  43. # %>% rename(cluster_color = "colors.use")
  44. # rm(group.cols)
  45.  
  46. # cells.cl.df: Add cluster_id column from cl.df; remove unused columns.
  47. cells.cl.df <- cells.cl.df %>% select(-cluster_label) %>% rename(cluster_label = groups.col) %>%
  48. left_join(
  49. # %>% select(cell.name, groups.col, combined.cluster.2),
  50. cl.df, by = "cluster_label") %>%
  51.  
  52. # Requires cells.cl.df (metadata) to have column being used for groups
  53. # named 'cluster_label' to match with cl_df during join.
  54. mutate(cluster_id = as.factor(cluster_id))
  55.  
  56. # ----------------------------------------------
  57. ## 4 Find cluster centroids from UMAP coordinates
  58. ## rd.cl.center
  59.  
  60. rd.cl.center <- get_RD_cl_center(rd.dat = rd.dat$umap, cl)
  61. message("Got rd.cl.center")
  62.  
  63. # update-rd.cl.center
  64.  
  65. rd.cl.center %<>%
  66. as.data.frame %>%
  67. set_names(c("x", "y")) %>%
  68. add_column(cl = cl.df$cluster_id, .before = "x") %>%
  69. # add_column preserves rownames.
  70. # but moving rownames to column cluster_label anyway bc of left_join below.
  71. # Needs to be cl (not cl_id) or else you get error:
  72. # Error in `$<-.data.frame`(`*tmp*`, "edge.frac.within", value = numeric(0)) :
  73. # replacement has 0 rows, data has 26
  74. rownames_to_column("cluster_label")
  75.  
  76. message("Updated rd.cl.center")
  77.  
  78. ## 5 Join `cl.df` and `rd.cl.center` into `cl.center.df` for input into `get_KNN_graph`.
  79. ## cl.center.df
  80. cl.center.df <- left_join(rd.cl.center, cl.df,
  81. by = c("cluster_label"))
  82.  
  83.  
  84. # 6 Get knn and cluster counts
  85. # Calls `knn.cl` in scrattch.hicat_fxns.Rmd
  86. # knn.result
  87. if(run.knn == TRUE) {
  88.  
  89. knn.result <<- RANN::nn2(data = rd.dat$pca[, 1:n.pcs], k = k)
  90.  
  91. }else{knn.result <- knn.result}
  92.  
  93. knn.cl <- get_knn_graph(knn.result = knn.result,
  94. rd.dat = rd.dat$umap,
  95. cl.df = cl.df,
  96. cl = cl.numeric,
  97. cl.numeric = cl.numeric,
  98. knn.outlier.th = 2,
  99. outlier.frac.t = 0.5)
  100.  
  101. # rm(rd.dat, ncx.clusters)
  102.  
  103. # -----------------------------------------------------------------------------
  104. # 2. Make constellation plot
  105.  
  106. # knn_cl_df_filter
  107.  
  108. # Keep only cells whose $frac >= 0.05.
  109. # frac = fraction of cells in cluster with nearest neighbors in a different cluster.
  110. # Defined in `get_knn_graph`:
  111. # knn.cl.df$frac = knn.cl.df$Freq / knn.cl.df$cl.from.total
  112. # 10% : 213 edges
  113. knn.cl.df.filter <- knn.cl$knn.cl.df %>% dplyr::filter(frac >= frac.th) %>%
  114. mutate(cl.from = as.numeric(cl.from), cl.to = as.numeric(cl.to))
  115.  
  116. # cl.to, cl.from numeric or factor?
  117. # Need to be numeric for getting the rows where cl.to == cl.from (knn.cl.df$same)
  118.  
  119.  
  120. ## plot-constellation
  121.  
  122. # To plot only edges between ExN lineage clusters:
  123. # knn.cl.df %<>% filter_at(vars(cl.from.label, cl.to.label),
  124. # all_vars(str_detect(., "RG|IPC|Neuron|OPC|Dividing")))
  125.  
  126. cl.plot <- plot_constellation(knn.cl.df = knn.cl.df.filter,
  127. cl.center.df = cl.center.df,
  128. out.dir = out.dir,
  129. node.label = "cluster_label", # name of column in cl.center.df with cluster/node names
  130. exxageration = 0.4, curved = TRUE,
  131. plot.parts = FALSE, plot.hull = NULL,
  132. plot.height = 40, plot.width = 40,
  133. node.dodge = TRUE,
  134. label.size = 3, max_size = 25)
  135.  
  136. return(lst(cl.center.df, knn.result, cl.plot))
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement