Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SheridJeff committed Aug 7, 2024
2 parents d1a4c5f + 9ca20aa commit d0617a1
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 55 deletions.
22 changes: 11 additions & 11 deletions 02_session3.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -492,25 +492,25 @@ These transforms are normally done automatically when using:
```
# convenience function params
load_images = list(
img1 = "[img_path1.tif]",
img2 = "[img_path2.tif]",
img3 = "..."
img1 = "[img_path1.tif]",
img2 = "[img_path2.tif]",
img3 = "..."
),
load_aligned_images = list(
aligned_img = c(
"[path to image.tif]",
"[path to magealignment.csv]"
)
aligned_img = c(
"[path to image.tif]",
"[path to magealignment.csv]"
)
)
# importer params
x$load_image(path = "[img_path1.tif]", name = "img1")
x$load_image(path = "[img_path2.tif]", name = "img2")
...
x$load_aligned_image(
path = "[path to image.tif]",
imagealignment_path = "[path to magealignment.csv]",
name = "aligned_img"
path = "[path to image.tif]",
imagealignment_path = "[path to magealignment.csv]",
name = "aligned_img"
)
```

Expand Down Expand Up @@ -947,7 +947,7 @@ knitr::include_graphics("img/02_session3/cc_enrich2.png")

Another thing we can do is create a "pseudovisium" dataset by tessellating across this dataset using the same layout and resolution as a Visium capture array.

`makePseudoVisium` generates a Visium array of circular polygons across the spatial extent provided.
`makePseudoVisium()` generates a Visium array of circular polygons across the spatial extent provided.

Here we use `ext()` with the `prefer` arg pointing to the polygon and points data and `all_data = TRUE`, meaning that the combined spatial extent of those two data types will be returned, giving a good measure of where all the data in the object is at the moment.

Expand Down
4 changes: 4 additions & 0 deletions 03_session2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Luckily, the above operations are all affine transformation, and they can be con
knitr::include_graphics("img/03_session2/matrix.png")
```

```{r, echo=FALSE}
knitr::include_graphics("img/03_session2/wiki_affine.png")
```

To perform the linear transform, the xy coordinates just need to be matrix multiplied by the 2x2 affine matrix. The resulting values should then be added to the translate values.

```{r, echo=FALSE}
Expand Down
33 changes: 12 additions & 21 deletions 03_session4.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Load a Giotto mini Visium object, which will be used for demonstrating interoper
```{r, eval = FALSE}
gobject <- GiottoData::loadGiottoMini("visium")
```
*Load a Giotto mini Visium object, which will be used for demonstrating interoperability.*

## Seurat

Expand All @@ -35,10 +36,8 @@ To convert Giotto object to Seurat V5 object, we first load required libraries a

```{r eval=FALSE}
library(Seurat)
library(SeuratData)
library(ggplot2)
library(patchwork)
library(dplyr)
```

Now we convert the Giotto object to a Seurat V5 object and create violin and spatial feature plots to visualize the RNA count data.
Expand All @@ -49,13 +48,11 @@ gToS <- giottoToSeuratV5(gobject = gobject,
plot1 <- VlnPlot(gToS,
features = "nCount_rna",
pt.size = 0.1) +
NoLegend()
pt.size = 0.1) + NoLegend()
plot2 <- SpatialFeaturePlot(gToS,
features = "nCount_rna",
pt.size.factor = 2) +
theme(legend.position = "right")
pt.size.factor = 2) + theme(legend.position = "right")
wrap_plots(plot1, plot2)
```
Expand All @@ -77,7 +74,9 @@ gToS <- SCTransform(gToS, assay = "rna", verbose = FALSE)
We perform Principal Component Analysis (PCA), find neighbors, and run UMAP for dimensionality reduction and clustering on the transformed Seurat object:

```{r, eval=FALSE}
gToS <- RunPCA(gToS, assay = "SCT")
gToS <- FindVariableFeatures(gToS, assay = "rna")
gToS <- RunPCA(gToS, assay = "rna")
gToS <- FindNeighbors(gToS, reduction = "pca", dims = 1:30)
Expand Down Expand Up @@ -139,7 +138,7 @@ spat_cor_netw_DT <- detectSpatialCorFeats(
subset_feats = ext_spatial_genes)
top10_genes <- showSpatialCorFeats(spat_cor_netw_DT,
feats = "Dsp",
feats = "'Rims3",
show_top_feats = 10)
spat_cor_netw_DT <- clusterSpatialCorFeats(spat_cor_netw_DT,
Expand Down Expand Up @@ -172,10 +171,6 @@ To start the conversion of a Giotto mini Visium object to a SpatialExperiment ob

```{r eval=FALSE}
library(SpatialExperiment)
library(ggspavis)
library(pheatmap)
library(scater)
library(scran)
library(nnSVG)
```

Expand Down Expand Up @@ -218,6 +213,8 @@ spe <- nnSVG(spe, X = X)
rowData(spe)[order(rowData(spe)$rank)[1:10], ]$feat_ID
```

[nnSvgProcessedspe](https://zenodo.org/records/13255338)

### Conversion of SpatialExperiment object back to Giotto

We then convert the processed SpatialExperiment object back into a Giotto object for further downstream analysis using the Giotto suite. This is done using the `spatialExperimentToGiotto` function, where we explicitly specify the spatial network from the SpatialExperiment object.
Expand Down Expand Up @@ -279,12 +276,7 @@ First, we specify the directory where the results will be saved. Additionally, w

```{r eval=FALSE}
# Specify path to which results may be saved
results_directory <- "results/03_session4/giotto_anndata_conversion/"
instrs <- showGiottoInstructions(gobject)
mini_gobject <- replaceGiottoInstructions(gobject = gobject,
instructions = instrs)
results_directory <- paste0(getwd(),'/giotto_anndata_conversion/')
```

#### Create Default kNN Network
Expand Down Expand Up @@ -339,6 +331,8 @@ adata$write("results/03_session4/cell_rna_converted_gobject2.h5ad")
processed_file_path <- "results/03_session4/cell_rna_converted_gobject2.h5ad"
```

[ProceesedAnnData](https://zenodo.org/records/13255338)

### Convert AnnData to Giotto

Finally, we convert the processed AnnData object back into a Giotto object for further analysis using Giotto.
Expand Down Expand Up @@ -369,9 +363,6 @@ knitr::include_graphics("img/03_session4/5.png")
```{r eval=FALSE}
mini_gobject <- loadGiottoMini(dataset = "vizgen",
python_path = NULL)
mini_gobject <- replaceGiottoInstructions(gobject = mini_gobject,
instructions = instrs)
```


Expand Down
39 changes: 21 additions & 18 deletions 03_session5.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ knitr::include_graphics("img/03_session5/0-ONTraCWorkFlow.png")

[ONTraC GitHub repository](https://github.com/gyuanlab/ONTraC)

[PPT](https://docs.google.com/presentation/d/1XlRIJDLNrSYP4rd5qsF3bvMMlgqN8VUiaZIO8jF0cto/edit?usp=sharing)

### Introduction to MERFISH

MERFISH is a massively multiplexed single-molecule imaging technology for spatially resolved transcriptomics capable of simultaneously measuring the copy number and spatial distribution of hundreds to tens of thousands of RNA species in individual cells. For further information, please visit the official [website](https://vizgen.com/technology/#merfish).
Expand All @@ -33,10 +35,10 @@ knitr::include_graphics("img/03_session5/0-MERFISH.png")
options(timeout=Inf) # In case of network interrupt
data_path <- file.path("data","03_session5")
dir.create(data_path)
dir.create(data_path, recursive=T)
results_folder <- file.path("results","03_session5")
dir.create(results_folder)
dir.create(results_folder, recursive=T)
```

### Dataset
Expand Down Expand Up @@ -96,16 +98,17 @@ instructions <- createGiottoInstructions(
)
## create Giotto object from expression counts. This file contains 61 slices here.
giotto_all_slices_obj <- anndataToGiotto("data/03_session5/counts.h5ad")
giotto_all_slices_obj <- anndataToGiotto(file.path(data_path, "counts.h5ad"))
## load meta_data
meta_df <- read.csv(file.path(data_path, "cell_labels.csv"),
meta_df <- read.csv(file.path(data_path, "cell_labels.csv"),
colClasses = "character") # as the cell IDs are 30 digit numbers, set the type as character to avoid the limitation of R in handling larger integers
colnames(meta_df)[[1]] <- "cell_ID"
### we use two slices here to speed up
slice1_cells <- meta_df[meta_df$slice_id == "mouse2_slice229",]$cell_ID
slice2_cells <- meta_df[meta_df$slice_id == "mouse2_slice300",]$cell_ID
selected_cells <- c(slice1_cells, slice2_cells)
## subset giotto obj by cell ID
giotto_slice1_obj <- subsetGiotto(gobject = giotto_all_slices_obj,
Expand Down Expand Up @@ -176,15 +179,15 @@ giotto_obj <- joinGiottoObjects(gobject_list = list(giotto_slice1_obj,
join_method = "z_stack")
## save giotto obj
saveGiotto(giotto_obj,
foldername = file.path(results_folder, "giotto_merfish_subset"))
# saveGiotto
saveGiotto(gobject = giotto_obj, foldername = "gobject", dir=results_folder)
```


If you facing network issue when downloading the raw dataset. Please download the processing giotto obj from [Zenodo](https://zenodo.org/communities/gw2024/), unzip and move it to `results` folder

```{r, eval=FALSE}
giotto_obj <- loadGiotto(path_to_folder = file.path(results_folder, "giotto_merfish_subset"))
giotto_obj <- loadGiotto(path_to_folder = file.path(results_folder, "gobject"))
```

#### Spatial distribution of cell type
Expand All @@ -208,7 +211,7 @@ knitr::include_graphics("img/03_session5/1-spatialCellTypeDis.png")
# annotation directly
ONTraC_input <- getONTraCv1Input(gobject = giotto_obj,
cell_type = "subclass",
output_path = data_path,
output_path = results_folder,
spat_unit = "cell",
feat_type = "rna",
verbose = TRUE)
Expand Down Expand Up @@ -259,7 +262,7 @@ source ~/.bash_profile
conda activate ONTraC
ONTraC --meta-input data/03_session5/ONTraC_dataset_input.csv --preprocessing-dir data/03_session5/preprocessing_dir --GNN-dir data/03_session5/GNN_dir --NTScore-dir data/03_session5/NTScore_dir --device cpu --epochs 1000 -s 42 --patience 100 --min-delta 0.001 --min-epochs 50 --lr 0.03 --hidden-feats 4 -k 6 --modularity-loss-weight 1 --regularization-loss-weight 0.1 --purity-loss-weight 100 --beta 0.3 --equal-space 2>&1 | tee data/03_session5/merfish_subset.log
ONTraC -d results/03_session5/ONTraC_dataset_input.csv --preprocessing-dir results/03_session5/preprocessing_dir --GNN-dir results/03_session5/GNN_dir --NTScore-dir results/03_session5/NTScore_dir --device cuda --epochs 1000 -s 42 --patience 100 --min-delta 0.001 --min-epochs 50 --lr 0.03 --hidden-feats 4 -k 6 --modularity-loss-weight 0.3 --purity-loss-weight 300 --regularization-loss-weight 0.3 --beta 0.03 2>&1 | tee results/03_session5/merfish_subset.log
```

### Visualization
Expand All @@ -274,18 +277,18 @@ giotto_obj <- loadOntraCResults(gobject = giotto_obj,
The NTScore and binarized niche cluster info were stored in cell metadata

```{r, eval = FALSE}
head(pDataDT(giotto_obj, spat_unit = "cell", feat_type = "niche cluster"))
head(pDataDT(giotto_obj, spat_unit = "cell", feat_type = "rna"))
```

```{r, eval = FALSE}
# cell_ID sample_id slice_id class_label subclass label list_ID NicheCluster NTScore
# <char> <char> <char> <char> <char> <char> <char> <int> <num>
# 1: mouse2_slice229-100101435705986292663283283043431511315 mouse2_sample6 mouse2_slice229 Glutamatergic L6 CT L6_CT_5 mouse2_slice229 2 0.7998957
# 2: mouse2_slice229-100104370212612969023746137269354247741 mouse2_sample6 mouse2_slice229 Other OPC OPC mouse2_slice229 0 0.2003027
# 3: mouse2_slice229-100128078183217482733448056590230529739 mouse2_sample6 mouse2_slice229 Glutamatergic L2/3 IT L23_IT_4 mouse2_slice229 0 0.2350597
# 4: mouse2_slice229-100209662400867003194056898065587980841 mouse2_sample6 mouse2_slice229 Other Oligo Oligo_1 mouse2_slice229 1 0.3990417
# 5: mouse2_slice229-100218038012295593766653119076639444055 mouse2_sample6 mouse2_slice229 Glutamatergic L2/3 IT L23_IT_4 mouse2_slice229 0 0.2910255
# 6: mouse2_slice229-100252992997994275968450436343196667192 mouse2_sample6 mouse2_slice229 Other Astro Astro_2 mouse2_slice229 2 0.8007477
# cell_ID sample_id slice_id class_label subclass label list_ID NicheCluster NTScore
# <char> <char> <char> <char> <char> <char> <char> <int> <num>
# 1: mouse2_slice229-100101435705986292663283283043431511315 mouse2_sample6 mouse2_slice229 Glutamatergic L6 CT L6_CT_5 mouse2_slice229 3 0.2002081
# 2: mouse2_slice229-100104370212612969023746137269354247741 mouse2_sample6 mouse2_slice229 Other OPC OPC mouse2_slice229. 1 0.7999791
# 3: mouse2_slice229-100128078183217482733448056590230529739 mouse2_sample6 mouse2_slice229 Glutamatergic L2/3 IT L23_IT_4 mouse2_slice229 1 0.7662198
# 4: mouse2_slice229-100209662400867003194056898065587980841 mouse2_sample6 mouse2_slice229 Other Oligo Oligo_1 mouse2_slice229 5 0.6010420
# 5: mouse2_slice229-100218038012295593766653119076639444055 mouse2_sample6 mouse2_slice229 Glutamatergic L2/3 IT L23_IT_4 mouse2_slice229 1 0.7132024
# 6: mouse2_slice229-100252992997994275968450436343196667192 mouse2_sample6 mouse2_slice229 Other Astro Astro_2 mouse2_slice229 3 0.1980136
```

The probability matrix of each cell assigned to each niche cluster and
Expand Down
Loading

0 comments on commit d0617a1

Please sign in to comment.