Advertisement
Guest User

Untitled

a guest
Mar 5th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. ---
  2. title: "buildconstellation"
  3. date: 2021-03-03
  4. params:
  5. name: NULL
  6. args_list: NULL
  7. groups.col: NULL
  8. colors.use : NULL
  9. max_size: 20
  10. n.pcs: 20
  11. run.knn: TRUE
  12. k: 15
  13. frac.th: 0.05
  14. out.dir: '../out'
  15. output: html_document
  16. ---
  17.  
  18. ```{r}
  19. knitr::opts_knit$set(progress = TRUE, verbose = FALSE)
  20. ```
  21.  
  22. ```{r}
  23. name <- params$name
  24. groups.col <- params$groups.col
  25. colors.use <- params$colors.use
  26. max_size <- params$max_size
  27.  
  28. n.pcs <- params$n.pcs
  29. k <- params$k
  30. frac.th <- params$frac.th
  31. run.knn <- params$run.knn
  32.  
  33. out.dir <- params$out.dir
  34.  
  35. args_list <- params$args_list
  36.  
  37. cells.cl.df <- args_list$cells_cl_df
  38. rd.dat <- args_list$reductions
  39. cl <- args_list$cl
  40. cl.numeric <- args_list$cl_numeric
  41. ```
  42.  
  43. Extra functions
  44.  
  45. ```{r functions}
  46. source_rmd("scrattch.hicat_fxns.Rmd")
  47. ```
  48.  
  49. ## 1. Build dataframes for constellation plots.
  50.  
  51. ```{r cl_and_cl_numeric, eval=TRUE, include=TRUE}
  52. # cl and cl numeric can also be passed as a param to the Rmd.
  53.  
  54. if(is.null(cl) & is.null(cl.numeric)) {
  55.  
  56. message("No cl and c;.numeric provided")
  57. # Cluster, or area-celltype combination.
  58. cl <- cells.cl.df[[groups.col]] %>% as.factor %>%
  59. set_names(cells.cl.df$cell.name)
  60. # 128 clusters in all _combo2_ cells
  61. # 77 clusters in all _combo2_ & ExN lineage cells.
  62.  
  63. cl.numeric <- as.numeric(cl) %>% purrr::set_names(nm = names(cl))
  64. }
  65. ```
  66.  
  67. 2
  68.  
  69. ```{r cl.df, eval=TRUE, include=TRUE}
  70.  
  71. cl.df <- get_cl_df(cl)
  72.  
  73. cl.df$area <- str_split_fixed(cl.df$cluster_label, "-", 2)[ ,2] %>% tolower
  74.  
  75. cl.df$clade <- str_split_fixed(cl.df$cluster_label, "-", 2)[ ,1] %>% tolower
  76. # Add clade_id, clade_color to cl.df
  77. # cl.df <- cl.df %>% left_join(clade.cols)
  78. cl.df <- cl.df %>% left_join(colors.use, by = c("clade" = "celltype"))
  79. # %>% rename(cluster_color = "colors.use")
  80. # rm(group.cols)
  81.  
  82. # cells.cl.df: Add cluster_id column from cl.df; remove unused columns.
  83. cells.cl.df <- cells.cl.df %>% select(-cluster_label) %>% rename(cluster_label = groups.col) %>%
  84. left_join(
  85. # %>% select(cell.name, groups.col, combined.cluster.2),
  86. cl.df, by = "cluster_label") %>%
  87.  
  88. # Requires cells.cl.df (metadata) to have column being used for groups
  89. # named 'cluster_label' to match with cl_df during join.
  90. mutate(cluster_id = as.factor(cluster_id))
  91. ```
  92.  
  93. ## 4 Find cluster centers from UMAP coordinates
  94.  
  95. ```{r rd.cl.center}
  96.  
  97. rd.cl.center <- get_RD_cl_center(rd.dat = rd.dat$umap, cl)
  98. message("Got rd.cl.center")
  99. ```
  100.  
  101. ```{r update-rd.cl.center}
  102. rd.cl.center %<>%
  103. as.data.frame %>%
  104. set_names(c("x", "y")) %>%
  105. add_column(cl = cl.df$cluster_id, .before = "x") %>%
  106. # add_column preserves rownames.
  107. # but moving rownames to column cluster_label anyway bc of left_join below.
  108. # Needs to be cl (not cl_id) or else you get error:
  109. # Error in `$<-.data.frame`(`*tmp*`, "edge.frac.within", value = numeric(0)) :
  110. # replacement has 0 rows, data has 26
  111. rownames_to_column("cluster_label")
  112.  
  113. message("Updated rd.cl.center")
  114. ```
  115.  
  116. ## 5 Join `cl.df` and `rd.cl.center` into `cl.center.df` for input into `get_KNN_graph`.
  117.  
  118. ```{r cl.center.df}
  119. cl.center.df <- left_join(rd.cl.center, cl.df,
  120. by = c("cluster_label"))
  121. ```
  122.  
  123. ## 6 Get knn and cluster counts
  124.  
  125. Calls `knn.cl` in `scrattch.hicat_fxns.Rmd`
  126.  
  127. ```{r knn.result}
  128. if(run.knn == TRUE) {
  129.  
  130. message("Running KNN")
  131.  
  132. knn.result <- RANN::nn2(data = rd.dat$pca[, 1:n.pcs], k = k)
  133.  
  134. } else {knn.result <- knn.result}
  135. ```
  136.  
  137. ```{r knn.cl}
  138.  
  139. knn.cl <- get_knn_graph(knn.result = knn.result,
  140. rd.dat = rd.dat$umap,
  141. cl.df = cl.df,
  142. cl = cl.numeric,
  143. cl.numeric = cl.numeric,
  144. knn.outlier.th = 2,
  145. outlier.frac.t = 0.5)
  146.  
  147. # rm(rd.dat, ncx.clusters)
  148. ```
  149.  
  150. # 2. Make constellation plot
  151.  
  152. ```{r knn_cl_df_filter}
  153.  
  154. # Keep only cells whose $frac (fraction of cells in cluster with nearest neighbors in a different cluster) >= 0.05.
  155. # Defined in `get_knn_graph`:
  156. # knn.cl.df$frac = knn.cl.df$Freq / knn.cl.df$cl.from.total
  157. # 10% : 213 edges
  158. knn.cl.df.filter <- knn.cl$knn.cl.df %>% dplyr::filter(frac >= frac.th) %>%
  159. mutate(cl.from = as.numeric(cl.from), cl.to = as.numeric(cl.to))
  160. # cl.to, cl.from numeric or factor?
  161. # Need to be numeric for getting the rows where cl.to == cl.from (knn.cl.df$same)
  162. ```
  163.  
  164. ```{r plot-constellation}
  165. # Plot only edges between ExN lineage clusters.
  166. # knn.cl.df %<>% filter_at(vars(cl.from.label, cl.to.label),
  167. # all_vars(str_detect(., "RG|IPC|Neuron|OPC|Dividing"))
  168. # )
  169.  
  170. # cl.center.df$cluster_label %<>% str_remove("_combo2")
  171. message("Plotting")
  172.  
  173. cl.plot <- plot_constellation(knn.cl.df = knn.cl.df.filter,
  174. cl.center.df = cl.center.df,
  175. out.dir = out.dir,
  176. node.label = "cluster_label", # name of column in cl.center.df with cluster/node names
  177. exxageration = 0.4, curved = TRUE,
  178. plot.parts = FALSE, plot.hull = NULL,
  179. plot.height = 40, plot.width = 40,
  180. node.dodge = TRUE,
  181. label.size = 3,
  182. max_size = max_size)
  183.  
  184. ```
  185.  
  186. ```{r}
  187. result <- lst(args_list, results = lst(cl.plot, knn.result, cl.center.df, knn.cl, knn.cl.df.filter))
  188. # rm(cl.plot, knn.result, cl.center.df, knn.cl, knn.cl.df.filter)
  189. st <- format(Sys.time(), "%Y%m%d_%H%M_")
  190. write_rds(result, path = file.path(out.dir, paste0(st, name, "_constellation_tables.rds")))
  191. ```
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement