# load packages
library(SPATA2)
library(tidyverse)

The center of SPATA2 is an S4-object of class spata2. This vignette explains the basis of it’s structure and more importantly the most basic functions to create one as well as to manipulate it.

1. Create a spata2 object

The easiest way to create one from scratch with your data is via the initiateSpataObject_*()functions.

1.1 From 10X Visium folders

Behind the scenes initiateSpataObject_10X() loads the necessary data file as well as the histology image from the folder you obtain from 10X Visium. It performs a variety of computations e.g. count normalization, dimensionality reduction, clustering while relying on gold-standard functions of the Seurat-package.

object_t275 <-
  initiateSpataObject_10X(
    directory_10X = "path/to/a/10Xvisium-folder", # the directory from which to load the data
    sample_name = "gbm-275"
    )

1.2 With a matrix and coordinates

There are two indispensable inputs needed in order to set up a spata2 object. As long as you provide those two you can use SPATA2 to analyze and visualize data deriving from all kinds of experiments.

  • A matrix in which the rows correspond to the gene names and the columns correspond to the unique identifiers of every distinct location (we refer to these locations as barcode-spots). Every cell of the matrix subsequently corresponds to the expression values. This matrix can either be an unprocessed count matrix or an already processed matrix which we refer to as expression matrix. The example is a 10x10 subset of an already processed matrix.
##            AAACAAGTATCTCCCA-1 AAACACCAATAACTGC-1 AAACAGAGCGACTCCT-1
## AL627309.1         0.00000000          1.0000000         0.03219694
## AL669831.2         0.12571837          0.7143180         0.06505426
## AL669831.5         0.07783532          0.8040467         0.14062911
## FAM87B             0.13916475          0.7292328         0.07296636
## LINC00115          0.25666142          0.6883596         0.00000000
## FAM41C             1.00000000          0.6477934         0.13263546
## AL645608.1         0.15857889          0.7022333         0.04904714
## SAMD11             0.13929504          0.6638247         0.17781843
## NOC2L              0.41453276          0.0000000         1.00000000
## KLHL17             0.05927105          0.6688244         0.43051485
##            AAACAGCTTTCAGAAG-1 AAACAGGGTCTATATT-1 AAACAGTGTTCCTGGG-1
## AL627309.1         0.00000000          0.5636165          0.8875665
## AL669831.2         0.12651164          0.5663211          0.7204619
## AL669831.5         0.28041277          0.9790490          0.8132117
## FAM87B             0.13134633          0.5841412          0.7210713
## LINC00115          0.03239111          0.0000000          0.2144870
## FAM41C             1.00000000          1.0000000          1.0000000
## AL645608.1         0.12102254          0.5137110          0.7141826
## SAMD11             0.25566425          0.7021117          0.6646854
## NOC2L              0.69096436          0.9859252          0.0000000
## KLHL17             0.32551350          0.6557542          0.2472023
##            AAACATTTCCCGGATT-1 AAACCGGGTAGGTACC-1 AAACCGTTCGTCCAGG-1
## AL627309.1          1.0000000         0.02437061          0.6291549
## AL669831.2          0.7785258         0.16050206          0.6618059
## AL669831.5          0.6796621         0.37621856          1.0000000
## FAM87B              0.7926872         0.16628669          0.6805608
## LINC00115           0.2490788         0.00000000          0.5193033
## FAM41C              0.8483957         0.53188622          0.5971984
## AL645608.1          0.7718748         0.14916640          0.6540970
## SAMD11              0.7857686         0.25079273          0.5973341
## NOC2L               0.0000000         1.00000000          0.0000000
## KLHL17              0.5935567         0.22997498          0.6117876
##            AAACCTAAGCAGCCGG-1
## AL627309.1          0.6054902
## AL669831.2          0.3943090
## AL669831.5          1.0000000
## FAM87B              0.4356422
## LINC00115           0.0000000
## FAM41C              0.9148817
## AL645608.1          0.3334440
## SAMD11              0.4980708
## NOC2L               0.5031803
## KLHL17              0.4672352
  • A coordinate data.frame that contains at least a column called barcodes that is identical in it’s content to the column names of the input matrix as well as a x- and a y-column referring to the spatial coordinates of each barcode-spot. (The example below shows a subset that corresponds to the matrix above.)
##              barcodes      x      y
## 1  AAACAAGTATCTCCCA-1 434.76 373.80
## 2  AAACACCAATAACTGC-1 131.76 428.76
## 3  AAACAGAGCGACTCCT-1 407.16 145.20
## 4  AAACAGCTTTCAGAAG-1  96.00 327.00
## 5  AAACAGGGTCTATATT-1 110.40 352.56
## 6  AAACAGTGTTCCTGGG-1 218.64 518.16
## 7  AAACATTTCCCGGATT-1 416.04 443.40
## 8  AAACCGGGTAGGTACC-1 165.36 321.12
## 9  AAACCGTTCGTCCAGG-1 215.88 384.96
## 10 AAACCTAAGCAGCCGG-1 364.80 468.48

Depending on whether your input matrix has already undergone processing steps you need to decide whether you want to initiate the spata2 object with either initiateSpataObject_CountMtr() or initiateSpataObject_ExprMtr(). As with any other function run ?initiateSpataObject_*() in your console to obtain a detailed report on what is going on in the background and how to manipulate the way your data is read in, processed and stored.

object_t275 <- 
 initiateSpataObject_CountMtr(
   coords_df = example_coords_df, # your coordinate data.frame
   count_mtr = example_count_mtr, # a matrix with unprocessed count values
   sample_name = "gbm_1",
   image = example_image
   )

object_t275 <- 
  initiateSpataObject_ExprMtr(
    coords_df = example_coords_df, # your coordinate data.frame
    expr_mtr = example_expr_mtr, # a matrix with already processed (e.g. normalized) values
    sample_name = "gbm_1",
    image = example_image
    )

2. Saving and loading a spata2 object

As with any other object created in R you can save spata2 objects with the function base::saveRDS(). However, SPATA2 provides some functionalities to make working with your objects as convenient as possible. The object itself contains a slot in which you can store the directory under which you want to save it every time you make progress worth saving (after time consuming computations for example). You can use getDirectoryInstructions() to obtain a list of directories that are currently stored in your spata2 object. (Click here to see how to create spata2 objects from Seurat, Giotto objects or AnnData structures.)

getDirectoryInstructions(object = object_t275)
## $cell_data_set
## [1] "not defined"
## 
## $seurat_object
## [1] "not defined"
## 
## $spata_object
## [1] "not defined"

Use setSpataDir() to adjust the default directory under which the object of interest is supposed to be stored.

# set/change the current default directory
object_t275 <- setSpataDir(object_t275, dir = "dir/to/spata_objects/T275.RDS")

getDirectoryInstructions(object = object_t275)

If a default directory exists you can save the object without any further input using saveSpataObject().

# quickly save the `spata2` object under the default directory 
saveSpataObject(object = object_t275)