---
title: "seqFISH Spatial Data Workflow"
author: "Dario Righelli"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
    BiocStyle::html_document:
        toc: true
vignette: >
    %\VignetteIndexEntry{seqFISH Spatial Data Workflow}
    %\VignetteEncoding{UTF-8}
    %\VignetteEngine{knitr::rmarkdown}
editor_options: 
    chunk_output_type: console
---

# Introduction

The SpatialExperiment package provides classes and methods for single cell 
spatial data handling.

This vignettes shows how to create a SpatialExperiment class by loading a 
seqFISH spatial dataset.

The seqFISH data are part of the [BIRS Biointegration Workshop Hackathon](https://github.com/BIRSBiointegration/Hackathon/tree/master/seqFISH) 
previously published in [Zhu et al. 2018](https://www.nature.com/articles/nbt.4260).


# Installation 

```{r, eval=FALSE}
if(!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("SpatialExperiment")
```


# Loading data and libraries

We stored the provided seqFish and scRNA-seq data inside the SpatialExperiment
external data seqFISH folder.

```{r, message=FALSE}
library(SpatialExperiment)
```


## seqFISH data

```{r}
fishCoordFile <- system.file(file.path("extdata", "seqFISH",
                            "fcortex.coordinates.txt"), 
                            package="SpatialExperiment")
fishCoordinates <- read.table(fishCoordFile, header=FALSE, sep=" ")
colnames(fishCoordinates) <- c("Cell_ID", "Irrelevant", "x", "y")

fishCellLabsFile <- system.file(file.path("extdata", "seqFISH", 
                            "seqfish_cell_labels.tsv"),
                            package="SpatialExperiment")
fishCellLabels <- read.table(file=fishCellLabsFile, header=FALSE, sep="\t")
colnames(fishCellLabels) <- c("Cell_ID", "cluster", "class", "classID", 
                                "Irrelevant", "Prob")
fishFeatCountsFile <- system.file(file.path("extdata", "seqFISH",
                            "seqfish_normalized_cortex_b2_testing.txt"), 
                            package="SpatialExperiment")
fishFeaturesCounts <- read.table(file=fishFeatCountsFile, 
                                header=FALSE, sep="\t", row.names=1)
```


# SpatialExperiment package


## Class handling

We can load the data inside the SpatialExperiment class.
We then use the show method for looking at the class description.

```{r}
se <- SpatialExperiment(rowData=rownames(fishFeaturesCounts),
                        colData=fishCellLabels,
                        assays=SimpleList(counts=as.matrix(fishFeaturesCounts)),
                        spatialCoords=fishCoordinates)
show(se)
```


## Spatial Coordinates

With the aid of the spatialCoords methods we can get and set the spatial coordinates.

### Getter

```{r}
spatialCoords(se)
```

### Setter

We create a fake fish coordinates data frame and overwrite the old one,
showing that the y coordinates are identical to the x ones.

```{r}
fakeFishCoords <- cbind(fishCoordinates[,c(1:3)], fishCoordinates[,3])
colnames(fakeFishCoords) <- c("Cell_ID", "Irrelevant", "x", "y")
spatialCoords(se) <- fakeFishCoords
spatialCoords(se)
```

```{r}
sessionInfo()
```

