Spaniel

Rachel Queen

2020-10-27

About Spaniel

Spaniel is an R package designed to visualise results of Spatial Transcriptomics experiments. To install and load Spaniel:

BiocManager::install('Spaniel')
library(Spaniel)
library(Seurat)

Load data

The data used in this vignette was published in Science (AAAS) under the title “Visualization and analysis of gene expression in tissue sections by spatial transcriptomics” on July 1st 2016. A subset of the data that is used in this vignette is included in the package.

To download the full dataset vist:

www.spatialtranscriptomicsresearch.org/datasets/doi-10-1126science-aaf2403/

Expression Matrix

Spaniel requires a data frame of counts where the genes equate to the rows and the columns equate to a Spatial Transcriptomics spot/barcode.

The barcodes are named in the colnames of the data frame:

##  [1] "ACAAACCTACTCCGCTGT" "ACAAAGAGATATAGCCCT" "ACAACTATGGGTTGGCGG"
##  [4] "ACAACTTGTACGGACCCT" "ACAAGAAGCCTGTCCGCG" "ACAAGAATTGACAAGATC"
##  [7] "ACAAGCCGCGCTGTTACG" "ACACAGATCCTGTTCTGA" "ACACAGCGTTGTGGAATC"
## [10] "ACACTAAATGAGCCCTAC"

The genes are named in rownames of the data frame

##  [1] "Tulp4"   "Arhgap5" "Eef1a1"  "Map1a"   "Gsk3b"   "Qk"      "Tmod2"  
##  [8] "Glul"    "Gpm6b"   "Scd2"

Barcode Coordinates

Spaniel also requires the coordinates of the barcodes. In this example we will load the undjusted coordinates provided by spatial transcriptomics but adjusted coordinates can also be used. The barcodes need to be stored in a tab delimited text file. The first column of the file should contain the barcodes, the second coumn the x coordinate of the barcode and the third column the y coordinate.

It is not necessary to load this file in at this point but for demonstration purposes we will load it now and look at the first few lines of the file.

##                   V1 V2 V3
## 1 GTCCGATATGATTGCCGC 32  2
## 2 ATGAGCCGGGTTCATCTT 31  2
## 3 TGAGGCACTCTGTTGGGA 30  2
## 4 ATGATTAGTCGCCATTCG 29  2
## 5 ACTTGAGGGTAGATGTTT 28  2
## 6 ATGGCCAATACTGTTATC 27  2

Create a S4 object

Spaniel includes two functions to create S4 objects containing the counts and barcode information. These are the Seurat object: https://satijalab.org/seurat/ and a SingleCellExperiment object: https://bioconductor.org/packages/release/bioc/html/SingleCellExperiment.html

Creating a Seurat object

The createSeurat() function can be used to create a Seurat object. The count data is stored in the counts slot of the assay slot of the object, the barcodes are stored in the meta.data slot and the ProjectName and SectionNumber arguments can be used to add information about the Sample and position on slide to the project.name slot of the Seurat object.

The barcodes are stored in the metadata:

##                    orig.ident nCount_RNA nFeature_RNA               spot  x  y
## ACAAACCTACTCCGCTGT TestProj1_      68090          399 ACAAACCTACTCCGCTGT  5 14
## ACAAAGAGATATAGCCCT TestProj1_      32289          302 ACAAAGAGATATAGCCCT 26  9
## ACAACTATGGGTTGGCGG TestProj1_     110044          442 ACAACTATGGGTTGGCGG 16 16
## ACAACTTGTACGGACCCT TestProj1_       7401          159 ACAACTTGTACGGACCCT  2 28
## ACAAGAAGCCTGTCCGCG TestProj1_      15426          193 ACAAGAAGCCTGTCCGCG  9 29
## ACAAGAATTGACAAGATC TestProj1_        354           16 ACAAGAATTGACAAGATC  3  6

Corner of the count data:

## 10 x 5 sparse Matrix of class "dgCMatrix"
##         ACAAACCTACTCCGCTGT ACAAAGAGATATAGCCCT ACAACTATGGGTTGGCGG
## Tulp4                  166                  .                 86
## Arhgap5                 79                108                150
## Eef1a1                  59                  .                195
## Map1a                    .                  .                 23
## Gsk3b                   21                  4                186
## Qk                      31                 94                368
## Tmod2                   38                  6                177
## Glul                   100                 81                852
## Gpm6b                  167                189                685
## Scd2                   297                 78                386
##         ACAACTTGTACGGACCCT ACAAGAAGCCTGTCCGCG
## Tulp4                    .                  .
## Arhgap5                  .                  1
## Eef1a1                   4                  2
## Map1a                    .                  .
## Gsk3b                    .                  .
## Qk                       .                133
## Tmod2                    .                  .
## Glul                     1                  .
## Gpm6b                   24                169
## Scd2                     .                  5

Project name

## [1] "TestProj1_"

Creating a SingleCellExperiment object

Alternatively, the readSCE() function can be used to create a SingleCellExperiment object. This time the ProjectName and SectionNumber are added as a column of the colData of the object. The number of genes and counts per spot are calculated by the calculateQCMetrics() from the scater bioconductor package.

Barcodes are stored in the colData

## DataFrame with 5 rows and 5 columns
##                                  spot         x         y     project       sum
##                           <character> <integer> <integer> <character> <numeric>
## ACAAACCTACTCCGCTGT ACAAACCTACTCCGCTGT         5        14  TestProj1_     68090
## ACAAAGAGATATAGCCCT ACAAAGAGATATAGCCCT        26         9  TestProj1_     32289
## ACAACTATGGGTTGGCGG ACAACTATGGGTTGGCGG        16        16  TestProj1_    110044
## ACAACTTGTACGGACCCT ACAACTTGTACGGACCCT         2        28  TestProj1_      7401
## ACAAGAAGCCTGTCCGCG ACAAGAAGCCTGTCCGCG         9        29  TestProj1_     15426

Corner of the count data:

##         ACAAACCTACTCCGCTGT ACAAAGAGATATAGCCCT ACAACTATGGGTTGGCGG
## Tulp4                  166                  0                 86
## Arhgap5                 79                108                150
## Eef1a1                  59                  0                195
## Map1a                    0                  0                 23
## Gsk3b                   21                  4                186
## Qk                      31                 94                368
## Tmod2                   38                  6                177
## Glul                   100                 81                852
## Gpm6b                  167                189                685
## Scd2                   297                 78                386
##         ACAACTTGTACGGACCCT ACAAGAAGCCTGTCCGCG
## Tulp4                    0                  0
## Arhgap5                  0                  1
## Eef1a1                   4                  2
## Map1a                    0                  0
## Gsk3b                    0                  0
## Qk                       0                133
## Tmod2                    0                  0
## Glul                     1                  0
## Gpm6b                   24                169
## Scd2                     0                  5

Project name:

## [1] "TestProj1_"

Load image

The next step is to load the histological image into R. The image example has been scaled down in size, using photoshop, to increase the plotting speeds. The image is cropped to around te edges of the spots.

### Load histological image into R

imgFile <- file.path(system.file(package = "Spaniel"), 
                        "HE_Rep1_resized.jpg")

image <- parseImage(imgFile)

Spatial Transcriptomics using Seurat

The rest of this vignette will focus on using a Seurat workflow.

Quality Control

Assessing the number of genes and number of counts per spot is a useful quality control step. Spaniel allows QC metrics to be viewed on top of the histological image so that any quality issues can be pinpointed. Spots within the tissue region which have a low number of genes or counts may be due to experimental problems which should be addressed. Conversely spots which lie outside of the tissue and have a high number of counts or large number of genes may indicate that there is background contamination.

Visualisation

The plotting function allows the use of a binary filter to visualise which spots pass filtering thresholds. We create a filter to show spots which have greater than 280 genes and a minimum of 67500.

NOTE: The parameters are set for subset of counts used in this data set and will be lower the a dataset where the full count matrix is used.
The filter thresholds will be experiment specific and should be adjusted as necessary.

Filtering

Spots which don’t pass QC thresholds can then be filtered from the data:

Remove additional spots

After filtering some spots remain which fall outside of the tissue area. Spaniel includes a shiny apply to select these spots. This can be run by issuing the command:

This opens an interactive plot allowing you to click on any spots to be removed from downstream analysis. Once all the spots have been selected close the shiny app window. A list of spots is stored in a text file called points_to_remove.txt in the working directory. As this is an interactive step it is not run in this vignette. Instead we can load a list of points to remove and run the function removeSpots() to remove the points that fall outside of the tissue area.

Cluster Spots

A clustering analysis can now be performed using methods found in Seurat package. For more details see:

https://satijalab.org/seurat/get_started.html

seuratFiltered <- NormalizeData(object = seuratFiltered,
                                normalization.method = "LogNormalize",
                                scale.factor = 10000)

seuratFiltered <- FindVariableFeatures(object = seuratFiltered,
                                        selection.method = "vst",
                                        nfeatures = 2000)

all.genes <- rownames(x = seuratFiltered)


seuratFiltered <- ScaleData(object = seuratFiltered, features = all.genes)
seuratFiltered <- RunPCA(object = seuratFiltered,
                            features = VariableFeatures(object = seuratFiltered)
                        )


seuratFiltered <- FindNeighbors(object = seuratFiltered, dims = 1:10)
seuratFiltered <- FindClusters(object = seuratFiltered,
                                resolution = c(0.4, 0.5, 0.6, 0.8))

View Genes

spanielPlot() can be used to view gene expression an image. The normalised scaled data from the scale_data slot of a seurat object is shown.

NOTE: if this plot is used with a SingleCellExperiment object expression data will be taken from the logcounts slot.

View Clusters

It can also be useful to look at how different clustering resolutions can be viewed match up with the image. Again this can done using the spanielPlot(). The ClusterRes argument should match a name in the meta.data to plot.

NOTE: if this plot is used with a SingleCellExperiment object ClusterRes should match a column name within the colData.

ShinySpaniel

Using the RunShinySpaniel function in R

Spaniel includes a shiny app designed to share preprocessed data. The app is included as the runShinySpaniel function.

Mark Cluster Columns

The shiny Spaniel app has the option of visualising any cluster data on a plot. First the columns containing the different clustering solutions need to be marked in the meta.dat with the prefix “cluster_” so that Spaniel knows where to look for them.

Save data object

An example of processed data set can be found:

Save image object

An example of processed image file can be found:

Run shinySpaniel!

To launch the shiny Spaniel app issue the command:

Deploying ShinySpaniel

For sharing analysis it is also possible to deploy the ShinySpaniel app to Shiny.io or any other server running R Server. The standalone app is located:

spanielApp <- file.path(system.file(package = "Spaniel"), "ShinySpaniel" )

You can sign up for a shinyapps.io account:

https://www.shinyapps.io/

Sign into your account and follow steps 1 and and 2 in the getting started instructions to authorise your account in R.

Then run the following code:

library(rsconnect)
rsconnect::deployApp(spanielApp)