-
Notifications
You must be signed in to change notification settings - Fork 0
/
H&Eplot.Rmd
183 lines (159 loc) · 5.74 KB
/
H&Eplot.Rmd
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
171
172
173
174
175
176
177
178
179
180
181
182
---
title: "Untitled"
author: "Matei Teleman"
date: "2024-09-23"
output: html_document
---
```{r}
hires_scalef <- 0.0797
lowres_scalef <- 0.00797
positions_in_tissue$imagerow_hires <- positions_in_tissue$pxl_row_in_fullres * hires_scalef
positions_in_tissue$imagecol_hires <- positions_in_tissue$pxl_col_in_fullres * hires_scalef
```
```{r}
# Get the minimum and maximum coordinates in high-resolution image coordinates
x_min <- min(positions_in_tissue$imagecol_hires)
x_max <- max(positions_in_tissue$imagecol_hires)
y_min <- min(positions_in_tissue$imagerow_hires)
y_max <- max(positions_in_tissue$imagerow_hires)
print(paste("Adjusted x_min:", x_min))
print(paste("Adjusted x_max:", x_max))
print(paste("Adjusted y_min:", y_min))
print(paste("Adjusted y_max:", y_max))
# Get the minimum and maximum coordinates in high-resolution image coordinates
# Get image dimensions
img_height <- dim(img)[1]
# Adjust y indices for image array (invert y-axis)
# y_min_img <- img_height - y_max + 1
# y_max_img <- img_height - y_min + 1
# x_min_img <- img_height - x_max + 1
# x_max_img <- img_height - x_min + 1
# Round indices to integers
x_min_img <- max(1, round(x_min))
x_max_img <- min(dim(img)[2], round(x_max))
y_min_img <- max(1, round(y_min_img))
y_max_img <- min(dim(img)[1], round(y_max_img))
```
```{r}
# Subset the image
cropped_img <- img[y_min_img:y_max_img, x_min_img:x_max_img, ]
cropped_img <- img[0:1810, 3236:5201, ]
plot(as.raster(cropped_img))
```
```{r}
ggplot() +
annotation_raster(as.raster(cropped_img), xmin = 3236, xmax = 5201, ymin = 0, ymax = 1810) +
geom_polygon(
data = fg_mc$polygons,
mapping = aes(x = x * hires_scalef, y = -y * hires_scalef + 1810, group = membership),
alpha = 0,
color = "black",
linetype = "solid"
) +
coord_fixed() +
theme_minimal()
```
```{r}
SpatialDimPlot(so_16,pt.size.factor = 0) +
geom_polygon(
data = fg_mc$polygons,
mapping = aes(x = x, y = y, group = membership),
alpha = 0,
color = "black",
linetype = "solid"
)
```
```{r}
test <- SpatialExperiment::read10xVisium(samples = "/Users/admin/Documents/These/data/VisiumHDColonCancer/binned_outputs/square_016um",type = c("HDF5", "sparse"),data = c("filtered", "raw"),images = "hires",load = TRUE)
```
```{r}
poly_8um <- readRDS("/Users/admin/Documents/GitHub/SuperSpot/figures/visium_hd/MC_spl_g16_fg.rds")$polygons
poly_8um$imagecol <- poly_8um$x
poly_8um$imagerow <- poly_8um$y
poly_8um$x <- poly_8um$imagerow
poly_8um$y <- poly_8um$imagecol
ggplot() +
annotation_raster(as.raster(cropped_img), xmin = 3236, xmax = 5201, ymin = 0, ymax = 1810) +
geom_polygon(
data = poly_8um,
mapping = aes(x = x * hires_scalef, y = -y * hires_scalef + 1810, group = membership),
alpha = 0,
color = "black",
linetype = "solid"
) +
coord_fixed() +
theme_minimal()
```
```{r}
positions<- arrow::read_parquet("/Users/admin/Documents/These/data/VisiumHDMouseBrain/binned_outputs/square_002um/spatial/tissue_positions.parquet") %>% column_to_rownames("barcode")
positions_in_tissue <- subset(positions, in_tissue == 1)
hires_scalef <- 0.2505533
lowres_scalef <- 0.02505533
positions_in_tissue$imagerow_hires <- positions_in_tissue$pxl_row_in_fullres * hires_scalef
positions_in_tissue$imagecol_hires <- positions_in_tissue$pxl_col_in_fullres * hires_scalef
```
```{r}
# Get the minimum and maximum coordinates in high-resolution image coordinates
x_min <- min(positions_in_tissue$imagecol_hires)
x_max <- max(positions_in_tissue$imagecol_hires)
y_min <- min(positions_in_tissue$imagerow_hires)
y_max <- max(positions_in_tissue$imagerow_hires)
print(paste("Adjusted x_min:", x_min))
print(paste("Adjusted x_max:", x_max))
print(paste("Adjusted y_min:", y_min))
print(paste("Adjusted y_max:", y_max))
# Get the minimum and maximum coordinates in high-resolution image coordinates
# Get image dimensions
img <- readPNG("/Users/admin/Documents/These/data/VisiumHDMouseBrain/binned_outputs/square_002um/spatial/tissue_hires_image.png")
img_height <- dim(img)[1]
# Adjust y indices for image array (invert y-axis)
# y_min_img <- img_height - y_max + 1
# y_max_img <- img_height - y_min + 1
# x_min_img <- img_height - x_max + 1
# x_max_img <- img_height - x_min + 1
# Round indices to integers
x_min_img <- max(1, round(x_min))
x_max_img <- min(dim(img)[2], round(x_max))
y_min_img <- max(1, round(y_min_img))
y_max_img <- min(dim(img)[1], round(y_max_img))
```
```{r}
cropped_img <- img[round(y_min):round(y_max), round(x_min):round(x_max), ]
plot(as.raster(cropped_img))
```
```{r}
poly_8um <- readRDS("/Users/admin/Documents/These/data/VisiumHDMouseBrain/MC_spl_g16_fg.rds")$polygons
poly_8um$imagecol <- poly_8um$x
poly_8um$imagerow <- poly_8um$y
poly_8um$x <- poly_8um$imagerow
poly_8um$y <- poly_8um$imagecol
ggplot() +
annotation_raster(as.raster(cropped_img), xmin = round(x_min), xmax = round(x_max), ymin = round(y_min), ymax = round(y_max)) +
geom_polygon(
data = poly_8um,
mapping = aes(x = x * hires_scalef, y = -y * hires_scalef + round(y_max) + round(y_min), group = membership),
alpha = 0,
color = "black",
linetype = "solid",
linewidth = 0.1
) +
coord_fixed() +
theme_minimal()
poly_16um <- readRDS("/Users/admin/Documents/These/data/VisiumHDMouseBrain/MC_spl_g64_fg.rds")$polygons
poly_16um$imagecol <- poly_16um$x
poly_16um$imagerow <- poly_16um$y
poly_16um$x <- poly_16um$imagerow
poly_16um$y <- poly_16um$imagecol
ggplot() +
annotation_raster(as.raster(cropped_img), xmin = round(x_min), xmax = round(x_max), ymin = round(y_min), ymax = round(y_max)) +
geom_polygon(
data = poly_16um,
mapping = aes(x = x * hires_scalef, y = -y * hires_scalef + round(y_max) + round(y_min), group = membership),
alpha = 0,
color = "black",
linetype = "solid",
linewidth = 0.1
) +
coord_fixed() +
theme_minimal()
```