knitr::opts_chunk$set(warning=FALSE, message=FALSE, include = TRUE,
                        fig.height = 8, fig.width = 8, fig.align = "center",

About Spaniel

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


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:

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.

### read in test data
counts <- readRDS(file.path(system.file(package = "Spaniel"), 

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


The genes are named in rownames of the data frame


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.

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

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.

barcodes <- read.csv(barcodesFile, sep = "\t", header = FALSE)

Create a S4 object

Spaniel includes two functions to create S4 objects containing the counts and barcode information. These are the Seurat object: and a SingleCellExperiment object:

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 slot and the ProjectName and SectionNumber arguments can be used to add information about the Sample and position on slide to the slot of the Seurat object.

seuratObj <- createSeurat(counts,
                        projectName = "TestProj", 
                        sectionNumber = 1

The barcodes are stored in the metadata:


Corner of the count data:

GetAssayData(seuratObj, "counts")[1:10, 1:5]

Project name


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.

sce <- createSCE(counts = counts,
                barcodeFile = barcodesFile, 
                projectName = "TestProj", 
                sectionNumber = 1)

Barcodes are stored in the colData


Corner of the count data:

counts(sce)[1:10, 1:5]

Project name:


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"), 

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.


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.

minGenes <- 280
minUMI <- 67500

filter <- seuratObj$nCount_RNA > minUMI &
            seuratObj$nFeature_RNA > minGenes 

spanielPlot(object = seuratObj, 
        grob = image, 
        plotType = "NoGenes", 
        showFilter = filter)


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

seuratFiltered <- subset(x = seuratObj, subset = nFeature_RNA > minGenes &
                                nCount_RNA > minUMI)

spanielPlot(object = seuratFiltered, 
        grob = image, 
        plotType = "NoGenes") 

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:

selectSpots(seuratFiltered, image)

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.

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

seuratFiltered <- removeSpots(seuratFiltered, 
                                pointsToRemove = spotsToRemove)

spanielPlot(object = seuratFiltered, 
        grob = image, 
        plotType = "NoGenes") 

Cluster Spots

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

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.

gene = "Nrgn"
spanielPlot(object = seuratFiltered, grob = image, 
        plotType = "Gene", 
        gene = gene)

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 to plot.

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

spanielPlot(object = seuratFiltered, 
        grob = image, 
        plotType = "Cluster", 
        clusterRes = "RNA_snn_res.0.8"


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.

seuratFiltered <- markClusterCol(seuratFiltered, "res")

Save data object

saveRDS(seuratFiltered, "data.rds")

An example of processed data set can be found:

file.path(system.file(package = "Spaniel"), "extdata/SeuratData.rds" )

Save image object

saveRDS(image, "image.rds")

An example of processed image file can be found:

file.path(system.file(package = "Spaniel"), "extdata/image.rds" )

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 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 account:

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:


Try the Spaniel package in your browser

Any scripts or data that you put into this service are public.

Spaniel documentation built on Nov. 8, 2020, 5:54 p.m.