-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTIMA_PostAlignment.Rmd
More file actions
172 lines (140 loc) · 5.47 KB
/
STIMA_PostAlignment.Rmd
File metadata and controls
172 lines (140 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: "Post-alignment slices adaptations"
author: "Victor Gaya"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
reticulate:
python_engine: python
vignette: >
%\VignetteIndexEntry{Post-alignment slices adaptations}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## Loading required packages
```{r, eval=FALSE}
#| label: packages
#remove.packages("STIMA")
#remotes::install_github("vagm110901/STIMA")
library(STIMA)
library(tools)
library(reticulate)
```
## Input data
```{r, eval=FALSE}
#| label: folder
# Define the folder where raw data is located
inputDir <- "/storage/gge/Victor/cell2spine/fullexp/results"
```
## Diferenciate crop / non-crop
```{r, eval=FALSE}
#| label: list_files
# Listing files
files <- list.files(path = inputDir, full.names = TRUE, recursive = FALSE)
rds_files <- rds_files <- files[grepl("\\.rds$", files)]
rds_groups <- list(
nocrop = rds_files[!grepl("crop", rds_files)],
crop = rds_files[grepl("crop", rds_files)]
)
```
# Split the RDS files
```{r, eval=FALSE}
#| label: split_rds
# Define the save directory
saveDir <- paste0(inputDir,"/split")
if (!dir.exists(saveDir)) {
dir.create(saveDir, recursive = TRUE)}
# We read each rds file
for (group in names(rds_groups)) {
for (rds_file in rds_groups[[group]]) {
listAligned <- readRDS(rds_file)
paciente_merge_aligned <- listAligned$alignedObj
info <- paciente_merge_aligned@meta.data[["info"]][[1]]
print(info)
# Split the samples
paciente_aligned.list <- SplitObject(paciente_merge_aligned, split.by = "slice")
# change the split.by value for the metadata information used to split the objects
# Delete the reference one
paciente_aligned.list$reference <- NULL
for (i in seq_along(paciente_aligned.list)){
slice <- paciente_aligned.list[[i]]@meta.data[["slice"]][[1]]
print(slice)
# Image name and key
image_name <- names(paciente_aligned.list[[i]])[[2]]
image_key <- paciente_aligned.list[[i]]@images[[1]]@key
# Change the name and the key to the aligned image (optional)
img <- paciente_aligned.list[[i]]@images[[2]]
paciente_aligned.list[[i]]@images[[paste0(image_name, "_aligned")]] <- img
paciente_aligned.list[[i]]@images[[2]] <- NULL
paciente_aligned.list[[i]]@images[[2]]@key <- paste0(image_key, "_aligned_")
# Save separatley crop and non-crop slices
if (group == "crop") {
saveName <- paste0(saveDir, "/", slice, "_aligned_crop.rds")
} else {saveName <- paste0(saveDir, "/", slice, "_aligned.rds")}
saveRDS(paciente_aligned.list[[i]], file = saveName)
}
}
}
```
# Save RDS files to AnnData format
## Save the csv files from RDS
```{r, eval=FALSE}
#| label: CSV_files
STIMA::saveSeurat_forAnnData_fromFolder(saveDir)
```
## Create the AnnData files
### Loading required packages
```{python, eval=FALSE}
#| label: py_packages
import os
import pandas as pd
import numpy as np
import scanpy as sc
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from scipy.sparse import csr_matrix
from skimage import io
from skimage.draw import disk
import anndata as ad
```
```{python, eval=FALSE}
#| label: AnnData_creation
carpetaData = "./tempfiles/"
carpetaSave = "./adata/"
slice_indices = sorted(set(f.split("slice")[1].split(".")[0] for f in os.listdir(carpetaData) if "slice" in f))
for i in slice_indices:
i = str(i)
# Load expression matrices and metadata for each slice
expression = pd.read_csv(f'{carpetaData}expression_matrix_slice{i}.csv', index_col=0)
cell_metadata = pd.read_csv(f'{carpetaData}cell_metadata_slice{i}.csv', index_col=0)
gene_metadata = pd.read_csv(f'{carpetaData}gene_metadata{i}.csv', index_col=0)
# Load spatial coordinates (imagerow, imagecol)
image_coords = pd.read_csv(f'{carpetaData}image_coordinates_slice{i}.csv', index_col=0)
#image_coords[["col", "row"]] = image_coords[["row", "col"]]
image_coords[["imagerow", "imagecol"]] = image_coords[["imagecol", "imagerow"]]
spatial_coords = image_coords[['imagerow', 'imagecol']] # adjust by columns names
scale_factors = pd.read_csv(f'{carpetaData}scale_factors_slice{i}.csv', index_col=0)
image_coords_selected = image_coords.iloc[:,:]
cell_metadata = cell_metadata.join(image_coords_selected, how="left")
# Load the histological spatial image
spatial_image = io.imread(f'{carpetaData}spatial_image_slice{i}.png')
# Create AnnData object
adata = sc.AnnData(X=expression.values, obs=cell_metadata, var=gene_metadata)
# Include Spatial coordinates
adata.obsm['spatial'] = spatial_coords.values
#adata.obsm['rgb'] = spatial_image
# Associate image to the AnnData object
image_name = str(cell_metadata.name.iloc[0])
adata.uns['spatial'] = {image_name: {}}
adata.uns['spatial'][image_name]['images'] = {}
adata.uns['spatial'][image_name]['images'] = {'lowres': spatial_image}
adata.uns['spatial'][image_name]['scalefactors'] = {
'tissue_hires_scalef': float(scale_factors.tissue_hires_scalef.iloc[0]),
'tissue_lowres_scalef': float(scale_factors.tissue_lowres_scalef.iloc[0]),
'spot_diameter_fullres': float(scale_factors.spot_diameter_fullres.iloc[0]),
'fiducial_diameter_fullres': float(scale_factors.fiducial_diameter_fullres.iloc[0])}
adata.var_names = adata.var['x']
adata.X = csr_matrix(adata.X)
# Save the AnnData as h5ad format
adata.write(f'{carpetaSave}{i}.h5ad')
```