Skip to content

Commit

Permalink
Add the code wiht the solution to the spatialLIBD::vis_clus() exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
lcolladotor committed Jul 6, 2023
1 parent 804e2c6 commit decf332
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
47 changes: 42 additions & 5 deletions 17_spatialLIBD_intro.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ----"download_10x_data"-------------------------------------
## ----"download_10x_data"----------------------------------------------------
## Download and save a local cache of the data provided by 10x Genomics
bfc <- BiocFileCache::BiocFileCache()
lymph.url <-
Expand All @@ -14,7 +14,7 @@ lymph.url <-
lymph.data <- sapply(lymph.url, BiocFileCache::bfcrpath, x = bfc)


## ----"extract_files"-----------------------------------------
## ----"extract_files"--------------------------------------------------------
## Extract the files to a temporary location
## (they'll be deleted once you close your R session)
xx <- sapply(lymph.data, utils::untar, exdir = file.path(tempdir(), "outs"))
Expand All @@ -40,7 +40,7 @@ list.files(lymph.dirs)
## 1.4G genes/genes.gtf


## ----"use_gencode_gtf"---------------------------------------
## ----"use_gencode_gtf"------------------------------------------------------
## Download the Gencode v32 GTF file and cache it
gtf_cache <- BiocFileCache::bfcrpath(
bfc,
Expand All @@ -54,7 +54,7 @@ gtf_cache <- BiocFileCache::bfcrpath(
gtf_cache


## ----wrapper_functions---------------------------------------
## ----wrapper_functions------------------------------------------------------
## Import the data as a SpatialExperiment object using wrapper functions
## provided by spatialLIBD
library("spatialLIBD")
Expand All @@ -73,7 +73,7 @@ spe_wrapper
lobstr::obj_size(spe_wrapper)


## ----"run_shiny_app_wrapper"---------------------------------
## ----"run_shiny_app_wrapper"------------------------------------------------
## Run our shiny app
if (interactive()) {
vars <- colnames(colData(spe_wrapper))
Expand All @@ -90,3 +90,40 @@ if (interactive()) {
)
}


## ---------------------------------------------------------------------------
## Basic spatial graph visualizing a discrete variable that we provided to
## the argument "clustervar". Here we chose to visualize
## "10x_kmeans_10_clusters" which contains the clustering results from the
## K-means algorithm when k = 10.
## We are plotting the one sample we have called "lymph". This is the same
## name we chose earlier when we ran spatialLIBD::read10xVisiumWrapper().
vis_clus(
spe = spe_wrapper,
sampleid = "lymph",
clustervar = "10x_kmeans_10_clusters"
)

## Next we ignore the histology image and stop plotting it by setting
## "spatial = FALSE"
vis_clus(
spe = spe_wrapper,
sampleid = "lymph",
clustervar = "10x_kmeans_10_clusters",
spatial = FALSE
)

## Finally, we use the "colors" argument to specify our own colors. We use
## the general setNames() R function to create a named vector that has as
## values the colors and as names the same names we have for our "clustervar".
vis_clus(
spe = spe_wrapper,
sampleid = "lymph",
clustervar = "10x_kmeans_10_clusters",
spatial = FALSE,
colors = setNames(Polychrome::palette36.colors(10), 1:10)
)

## Here we can see the vector we provided as input to "colors":
setNames(Polychrome::palette36.colors(10), 1:10)

54 changes: 54 additions & 0 deletions 17_spatialLIBD_intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,57 @@ font-family: sans-serif;
* Use custom colors of your choice. See https://emilhvitfeldt.github.io/r-color-palettes/discrete.html to choose a color palette.
- For example, use the `Polychrome::palette36()` colors.
</div>

<style>
p.solution {
background-color: #C093D6;
padding: 9px;
border: 1px solid black;
border-radius: 10px;
font-family: sans-serif;
}
</style>

<p class="solution">
**Solution 1**:
The function for plotting discrete variables is `spatialLIBD::vis_clus()` which is documented at http://research.libd.org/spatialLIBD/reference/vis_clus.html. After reading the documentation website for this function, we are ready to write the code to solve our exercise problem.
</p>


```{r}
## Basic spatial graph visualizing a discrete variable that we provided to
## the argument "clustervar". Here we chose to visualize
## "10x_kmeans_10_clusters" which contains the clustering results from the
## K-means algorithm when k = 10.
## We are plotting the one sample we have called "lymph". This is the same
## name we chose earlier when we ran spatialLIBD::read10xVisiumWrapper().
vis_clus(
spe = spe_wrapper,
sampleid = "lymph",
clustervar = "10x_kmeans_10_clusters"
)
## Next we ignore the histology image and stop plotting it by setting
## "spatial = FALSE"
vis_clus(
spe = spe_wrapper,
sampleid = "lymph",
clustervar = "10x_kmeans_10_clusters",
spatial = FALSE
)
## Finally, we use the "colors" argument to specify our own colors. We use
## the general setNames() R function to create a named vector that has as
## values the colors and as names the same names we have for our "clustervar".
vis_clus(
spe = spe_wrapper,
sampleid = "lymph",
clustervar = "10x_kmeans_10_clusters",
spatial = FALSE,
colors = setNames(Polychrome::palette36.colors(10), 1:10)
)
## Here we can see the vector we provided as input to "colors":
setNames(Polychrome::palette36.colors(10), 1:10)
```

0 comments on commit decf332

Please sign in to comment.