Skip to content

Commit

Permalink
name chunks in rmarkdown
Browse files Browse the repository at this point in the history
  • Loading branch information
csangara committed Jan 26, 2024
1 parent d3df359 commit adf6fa6
Show file tree
Hide file tree
Showing 125 changed files with 554 additions and 513 deletions.
28 changes: 14 additions & 14 deletions vignettes/circos.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Circos plot visualization to show active ligand-target links between interacting cells"
title: "HNSCC case study + double circos visualization"
author: "Robin Browaeys & Chananchida Sang-aram"
date: "2023-07-20"
output: rmarkdown::html_vignette
Expand All @@ -23,9 +23,9 @@ knitr::opts_chunk$set(
)
```

This vignette shows how to visualize the output of a NicheNet analysis in a circos plot. This is an additional demonstration to [Seurat Wrapper + Circos visualization](seurat_wrapper_circos.md). Here, we will use a normal matrix as input instead of a Seurat object.
This vignette shows how to visualize the output of a NicheNet analysis in a circos plot. This is an additional demonstration to [Seurat Wrapper + circos visualization](seurat_wrapper_circos.md). Here, we will use a normal matrix as input instead of a Seurat object.

We will use the same dataset as from [NicheNet's ligand activity analysis on a gene set of interest: predict active ligands and their target genes](ligand_activity_geneset.md) @puram_single-cell_2017. In contrast to the basic vignette, we will look at communication between multiple cell types. More specifically, we will predict which ligands expressed by both CAFs and endothelial cells can induce the p-EMT program in neighboring malignant cells.
We will use the same dataset as from [NicheNet's ligand activity analysis on a gene set of interest](ligand_activity_geneset.md) @puram_single-cell_2017. In contrast to the basic vignette, we will look at communication between multiple cell types. More specifically, we will predict which ligands expressed by both CAFs and endothelial cells can induce the p-EMT program in neighboring malignant cells.

### Load packages

Expand Down Expand Up @@ -220,19 +220,19 @@ vis_circos_obj <- prepare_circos_visualization(circos_links,

Render the circos plot where all links have the same transparency. Here, only the widths of the blocks that indicate each target gene is proportional the ligand-target regulatory potential (~prior knowledge supporting the regulatory interaction).

```{r, fig.width=8, fig.height=8}
```{r ligand-target-circos, fig.width=8, fig.height=8}
draw_circos_plot(vis_circos_obj, transparency = FALSE, args.circos.text = list(cex = 0.5))
```

Render the circos plot where the degree of transparency determined by the regulatory potential value of a ligand-target interaction.

```{r, fig.width=8, fig.height=8}
```{r ligand-target-circos-transparent, fig.width=8, fig.height=8}
draw_circos_plot(vis_circos_obj, transparency = TRUE, args.circos.text = list(cex = 0.5))
```

To create a legend for the circos plot, we can use the `ComplexHeatmap::Legend` function and creating a gTree object from it with `grid::grid.grabExpr`. As the circos plot is drawn on base R graphics (i.e., it is not a ggplot object), we will get the plot using `recordPlot()`.

```{r, fig.show='hide'}
```{r ligand-target-circos-unused, fig.show='hide'}
par(bg = "transparent")
# Default celltype order
Expand All @@ -245,24 +245,24 @@ circos_legend <- ComplexHeatmap::Legend(
type = "point",
grid_height = unit(3, "mm"),
grid_width = unit(3, "mm"),
labels_gp = gpar(fontsize = 8)
labels_gp = grid::gpar(fontsize = 8)
)
circos_legend_grob <- grid::grid.grabExpr(draw(circos_legend))
circos_legend_grob <- grid::grid.grabExpr(ComplexHeatmap::draw(circos_legend))
draw_circos_plot(vis_circos_obj, transparency = TRUE, args.circos.text = list(cex = 0.5))
p_circos_no_legend <- recordPlot()
```

We can combine the circos plot and the legend using `cowplot::plot_grid`.
```{r fig.width=8, fig.height=8}
```{r ligand-target-circos-with-legend, fig.width=8, fig.height=8}
cowplot::plot_grid(p_circos_no_legend, circos_legend_grob, rel_widths = c(1, 0.1))
```


We can save this plot to an svg file.

```{r}
```{r, eval=FALSE}
svg("ligand_target_circos.svg", width = 10, height = 10)
cowplot::plot_grid(p_circos_no_legend, circos_legend_grob, rel_widths = c(1, 0.1))
dev.off()
Expand Down Expand Up @@ -292,14 +292,14 @@ vis_circos_receptor_obj <- prepare_circos_visualization(lr_network_top_df,

When drawing the plot, the argument `link.visible` = TRUE is also necessary for making all links visible, since no cutoff is used to filter out ligand-receptor interactions.

```{r}
```{r ligand-receptor-circos}
draw_circos_plot(vis_circos_receptor_obj, transparency = FALSE,
link.visible = TRUE, args.circos.text = list(cex = 0.8))
```

Just as above, if `transparency = TRUE`, the degree of transparency is determined by the prior interaction weight of the ligand-receptor interaction.

```{r}
```{r ligand-receptor-circos-transparent}
draw_circos_plot(vis_circos_receptor_obj, transparency = TRUE,
link.visible = TRUE, args.circos.text = list(cex = 0.8))
```
Expand Down Expand Up @@ -388,7 +388,7 @@ vis_circos_obj_subset <- prepare_circos_visualization(circos_links_subset,

Finally, we create the plot by adding an extra layer in `preAllocateTracks`, and we add a `for` loop at the end to draw the outer layer.

```{r, fig.width=8, fig.height=8}
```{r double-circos-method-1, fig.width=8, fig.height=8}
circos.par(gap.degree = vis_circos_obj_subset$gaps)
chordDiagram(vis_circos_obj_subset$links_circle,
Expand Down Expand Up @@ -500,7 +500,7 @@ vis_circos_obj2 <- prepare_circos_visualization(circos_links %>% select(-target_

The general idea here is similar - adding an extra layer in `preAllocateTracks` and a `for` loop at the end to draw the outer layer - but the function allows for much cleaner code.

```{r, fig.width=8, fig.height=8}
```{r double-circos-method-2, fig.width=8, fig.height=8}
circos.par(gap.degree = vis_circos_obj2$gaps)
chordDiagram(vis_circos_obj2$links_circle,
directional = 1,
Expand Down
28 changes: 12 additions & 16 deletions vignettes/circos.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Circos plot visualization to show active ligand-target links between
interacting cells
HNSCC case study + double circos visualization
================
Robin Browaeys & Chananchida Sang-aram
2023-07-20
Expand All @@ -10,12 +9,11 @@ rmarkdown::render("vignettes/circos.Rmd", output_format = "github_document")

This vignette shows how to visualize the output of a NicheNet analysis
in a circos plot. This is an additional demonstration to [Seurat
Wrapper + Circos visualization](seurat_wrapper_circos.md). Here, we will
Wrapper + circos visualization](seurat_wrapper_circos.md). Here, we will
use a normal matrix as input instead of a Seurat object.

We will use the same dataset as from [NicheNet’s ligand activity
analysis on a gene set of interest: predict active ligands and their
target genes](ligand_activity_geneset.md) Puram et al. (2017). In
analysis on a gene set of interest](ligand_activity_geneset.md) Puram et al. (2017). In
contrast to the basic vignette, we will look at communication between
multiple cell types. More specifically, we will predict which ligands
expressed by both CAFs and endothelial cells can induce the p-EMT
Expand Down Expand Up @@ -278,7 +276,7 @@ supporting the regulatory interaction).
draw_circos_plot(vis_circos_obj, transparency = FALSE, args.circos.text = list(cex = 0.5))
```

![](circos_files/figure-gfm/unnamed-chunk-12-1.png)<!-- -->
![](circos_files/figure-gfm/ligand-target-circos-1.png)<!-- -->

Render the circos plot where the degree of transparency determined by
the regulatory potential value of a ligand-target interaction.
Expand All @@ -287,7 +285,7 @@ the regulatory potential value of a ligand-target interaction.
draw_circos_plot(vis_circos_obj, transparency = TRUE, args.circos.text = list(cex = 0.5))
```

![](circos_files/figure-gfm/unnamed-chunk-13-1.png)<!-- -->
![](circos_files/figure-gfm/ligand-target-circos-transparent-1.png)<!-- -->

To create a legend for the circos plot, we can use the
`ComplexHeatmap::Legend` function and creating a gTree object from it
Expand All @@ -308,10 +306,10 @@ circos_legend <- ComplexHeatmap::Legend(
type = "point",
grid_height = unit(3, "mm"),
grid_width = unit(3, "mm"),
labels_gp = gpar(fontsize = 8)
labels_gp = grid::gpar(fontsize = 8)
)

circos_legend_grob <- grid::grid.grabExpr(draw(circos_legend))
circos_legend_grob <- grid::grid.grabExpr(ComplexHeatmap::draw(circos_legend))

draw_circos_plot(vis_circos_obj, transparency = TRUE, args.circos.text = list(cex = 0.5))
p_circos_no_legend <- recordPlot()
Expand All @@ -324,16 +322,14 @@ We can combine the circos plot and the legend using
cowplot::plot_grid(p_circos_no_legend, circos_legend_grob, rel_widths = c(1, 0.1))
```

![](circos_files/figure-gfm/unnamed-chunk-15-1.png)<!-- -->
![](circos_files/figure-gfm/ligand-target-circos-with-legend-1.png)<!-- -->

We can save this plot to an svg file.

``` r
svg("ligand_target_circos.svg", width = 10, height = 10)
cowplot::plot_grid(p_circos_no_legend, circos_legend_grob, rel_widths = c(1, 0.1))
dev.off()
## png
## 2
```

### Visualize ligand-receptor interactions of the prioritized ligands in a circos plot
Expand Down Expand Up @@ -369,7 +365,7 @@ draw_circos_plot(vis_circos_receptor_obj, transparency = FALSE,
link.visible = TRUE, args.circos.text = list(cex = 0.8))
```

![](circos_files/figure-gfm/unnamed-chunk-18-1.png)<!-- -->
![](circos_files/figure-gfm/ligand-receptor-circos-1.png)<!-- -->

Just as above, if `transparency = TRUE`, the degree of transparency is
determined by the prior interaction weight of the ligand-receptor
Expand All @@ -380,7 +376,7 @@ draw_circos_plot(vis_circos_receptor_obj, transparency = TRUE,
link.visible = TRUE, args.circos.text = list(cex = 0.8))
```

![](circos_files/figure-gfm/unnamed-chunk-19-1.png)<!-- -->
![](circos_files/figure-gfm/ligand-receptor-circos-transparent-1.png)<!-- -->

### Adding an outer track to the circos plot (ligand-receptor-target circos plot)

Expand Down Expand Up @@ -598,7 +594,7 @@ for (group in unique(targets_filtered$type)){
}
```

![](circos_files/figure-gfm/unnamed-chunk-22-1.png)<!-- -->
![](circos_files/figure-gfm/double-circos-method-1-1.png)<!-- -->

``` r

Expand Down Expand Up @@ -691,7 +687,7 @@ for (target_gene_group in unique(target_gene_groups$target_type)){
}
```

![](circos_files/figure-gfm/unnamed-chunk-25-1.png)<!-- -->
![](circos_files/figure-gfm/double-circos-method-2-1.png)<!-- -->

``` r

Expand Down
Binary file added vignettes/images/nichenet_prior_model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions vignettes/ligand_activity_geneset.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ We will use the top 30 ligands to predict active target genes and construct an a

Active target genes are defined as genes in the gene set of interest that have the highest regulatory potential for each top-ranked ligand. These top targets of each ligand are based on the prior model. Specifically, the function get_weighted_ligand_target_links will return genes that are in the gene set of interest and are the top `n` targets of a ligand (default: `n = 200`).

```{r, fig.width=8, fig.height=6}
```{r ligand-target-heatmap, fig.width=8, fig.height=6}
active_ligand_target_links_df <- best_upstream_ligands %>%
lapply(get_weighted_ligand_target_links,
geneset = geneset_oi,
Expand All @@ -212,7 +212,7 @@ p_ligand_target_network

We can also look at which receptors of the receiver cell population (malignant cells) can potentially bind to the prioritized ligands from the sender cell population (CAFs).

```{r, fig.width=9, fig.height=6}
```{r ligand-receptor-heatmap, fig.width=9, fig.height=6}
ligand_receptor_links_df <- get_weighted_ligand_receptor_links(
best_upstream_ligands, expressed_receptors,
Expand Down Expand Up @@ -247,7 +247,7 @@ vis_ligand_aupr <- ligand_activities %>% filter(test_ligand %in% best_upstream_l
column_to_rownames("test_ligand") %>% select(aupr_corrected) %>% arrange(aupr_corrected) %>% as.matrix(ncol = 1)
```

```{r, fig.width=5, fig.height=6}
```{r ligand-activity-heatmap, fig.width=5, fig.height=6}
p_ligand_aupr <- make_heatmap_ggplot(vis_ligand_aupr,
"Prioritized CAF-ligands", "Ligand activity",
color = "darkorange", legend_title = "AUPR") +
Expand Down Expand Up @@ -280,7 +280,7 @@ vis_ligand_tumor_expression <- aggregated_expression_matrix_CAF[rev(best_upstrea
```

```{r, fig.width=9, fig.height=6}
```{r ligand-expression-heatmap, fig.width=9, fig.height=6}
color <- colorRampPalette(rev(brewer.pal(n = 7, name ="RdYlBu")))(100)
p_ligand_tumor_expression <- make_heatmap_ggplot(vis_ligand_tumor_expression,
"Prioritized CAF-ligands", "Tumor",
Expand Down Expand Up @@ -310,7 +310,7 @@ vis_target_tumor_expression_scaled <- aggregated_expression_matrix_target %>% t(
.[order_tumors, order_targets]
```

```{r, fig.width=9, fig.height=6}
```{r target-expression-heatmap, fig.width=9, fig.height=6}
p_target_tumor_scaled_expression <- make_threecolor_heatmap_ggplot(vis_target_tumor_expression_scaled,
"Tumor", "Target",
low_color = color[1], mid_color = color[50], mid = 0.5,
Expand All @@ -322,7 +322,7 @@ p_target_tumor_scaled_expression

#### Combine the different heatmaps in one overview figure

```{r, fig.width=15, fig.height=9}
```{r summary-vis, fig.width=15, fig.height=9}
figures_without_legend = plot_grid(
p_ligand_aupr + theme(legend.position = "none"),
Expand Down
Loading

0 comments on commit adf6fa6

Please sign in to comment.