-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgiotto_object_tutorial.R
288 lines (198 loc) · 9.29 KB
/
giotto_object_tutorial.R
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# ------------------------------------------------------#
# #
# Giotto Suite: A multi-scale and technology-agnostic #
# spatial omics analysis framework #
# #
# Jiaji George Chen, Joselyn Cristina Chavez-Fuentes, #
# Matthew O'Brien, Irzam Sarfraz, Iqra Amin, #
# Eddie Ruiz, Pratishtha Guckhool, Guo-Cheng Yuan, #
# Ruben Dries #
# #
# 8/2/2023 #
# #
# #
# ------------------------------------------------------#
# Tutorial: Giotto object structure and manipulation
# start with basic aggregation
# and workflow diagram
# what do you do when there are datasets with more than one set of polys?
# turns out this particular dataset actually does come with multiple layers of annotations that are different from each other
#
# This can be seen both in the values that are carried in the points and also in the polys provided
# Ensure Giotto Suite is installed. -------------------------- #
i_p = installed.packages()
if(!"Giotto" %in% i_p) devtools::install_github("drieslab/Giotto@suite")
# Ensure Giotto Data is installed
if(!"GiottoData" %in% i_p) devtools::install_github("drieslab/GiottoData")
library(data.table)
library(Giotto)
library(GiottoData)
# Ensure the Python environment for Giotto has been installed
genv_exists = checkGiottoEnvironment()
if(!genv_exists){
# The following command need only be run once to install the Giotto environment
installGiottoEnvironment()
}
# ------------------------------------------------------------ #
# Create a Giotto object - General method with multiple layers
# The Giotto object holds spatial-omic data and facilitates its analysis and
# visualization.
#
# Data used: a subset of a Vizgen MERSCOPE mouse brain dataset to show flexiblity
# of framework
## provide path to vizgen folder
data_path = system.file('/Mini_datasets/Vizgen/Raw/', package = 'GiottoData')
## 0.1 path to transcripts ####
# --------------------------- #
## each transcript has x, y and z coordinate
tx_path = paste0(data_path, '/', 'vizgen_transcripts.gz')
tx_dt = data.table::fread(tx_path)
## 0.2 path to cell boundaries folder ####
# -------------------------------------- #
## vizgen already provides segmentation information in .hdf5 files
## Here I have already converted the hdf5 files to a simple data.table format
boundary_path = paste0(data_path, '/cell_boundaries/')
z0_polygon_DT = fread(paste0(boundary_path, '/', 'z0_polygons.gz'))
z1_polygon_DT = fread(paste0(boundary_path, '/', 'z1_polygons.gz'))
z0_polygons = createGiottoPolygonsFromDfr(name = 'z0', segmdfr = z0_polygon_DT)
z1_polygons = createGiottoPolygonsFromDfr(name = 'z1', segmdfr = z1_polygon_DT)
plot(z0_polygons)
plot(z1_polygons)
# 1. create dataset with transcript and polygon information ####
# ------------------------------------------------------------------------ #
# Giotto objects should need some form of expression information in order to
# perform analyses
# This can be an expression matrix (with spatial locations)
# or simple feature detections with some polygon annotations
#
# Note the nested inputs that define feature type and spatial unit
viz = createGiottoObject(feat_info = list('rna' = tx_dt[,.(global_x, -global_y, gene, global_z)]),
spatial_info = list('z0' = z0_polygons,
'z1' = z1_polygons))
viz # directly returns quick summary of attached info
# add some visualizations
# calculate centroid for each polygon ( = cell)
# this can/will be used when aggregating for example counts to cells
viz = addSpatialCentroidLocations(gobject = viz,
poly_info = paste0('z',0:1),
provenance = list('z0', 'z1'),
return_gobject = TRUE)
showGiottoSpatLocs(viz) # show functions provide a more detailed look at specific slots
#Subcellular data can then be immediately plotted
spatInSituPlotPoints(viz,
feats = list('rna' = c("Htr1b", "Ackr1", "Epha7")),
feats_color_code = c("Htr1b" = 'green', 'Ackr1' = 'blue', 'Epha7' = 'red'),
largeImage_name = 'dapi_z0',
point_size = 0.5,
plot_method = 'ggplot',
show_polygon = TRUE,
use_overlap = F,
polygon_feat_type = 'z1',
polygon_color = 'cyan',
polygon_bg_color = 'cyan',
polygon_line_size = 0.2,
coord_fix_ratio = TRUE,
background_color = 'black')
# 3. aggregate information to matrix: polygons and transcripts ####
# --------------------------------------------------------------- #
# we will use the z0 polygon information
# we can set activeSpatUnit() or specify this for each command
# activeSpatUnit(viz) = 'z0' # now you don't need to think about setting spat_unit each time
# note the feat_subset_column and feat_subset_ids
# explanation giotto points and polyons and attached values
tx_z0 = getFeatureInfo(viz)
viz = calculateOverlapRaster(viz,
spatial_info = 'z0',
feat_info = 'rna',
feat_subset_column = 'global_z',
feat_subset_ids = 0)
viz = overlapToMatrix(viz,
poly_info = 'z0',
feat_info = 'rna',
name = 'raw')
viz = calculateOverlapRaster(viz,
spatial_info = 'z1',
feat_info = 'rna',
feat_subset_column = 'global_z',
feat_subset_ids = 1)
viz = overlapToMatrix(viz,
poly_info = 'z1',
feat_info = 'rna',
name = 'raw')
showGiottoExpression(viz)
# combine the calculated data for z1 and z0 into a new spatial unit called
# 'aggregate'
viz = aggregateStacks(gobject = viz,
spat_units = c('z0', 'z1'),
feat_type = 'rna',
values = 'raw',
summarize_expression = 'sum',
summarize_locations = 'mean',
new_spat_unit = 'aggregate')
# From here normal analysis can continue
activeSpatUnit(viz) = 'aggregate'
viz = filterGiotto(viz, expression_threshold = 1, feat_det_in_min_cells = 1, min_det_feats_per_cell = 1)
viz = normalizeGiotto(viz)
viz = addStatistics(viz)
spatInSituPlotPoints(viz,
show_polygon = T,
polygon_feat_type = 'aggregate',
spat_unit = 'aggregate',
polygon_color = 'white',
polygon_line_size = 0.1,
polygon_fill_as_factor = F,
polygon_fill = 'nr_feats',
polygon_alpha = 1)
viz = createSpatialNetwork(viz, method = 'kNN', k = 8)
viz = spatialAutoCorLocal(viz, method = 'moran', data_to_use = 'expression')
spatPlot2D(viz,
spat_enr_names = 'expr_moran',
cell_color = featIDs(viz)[1],
color_as_factor = FALSE)
# issues with meta-based enrs
activeFeatType(viz) = 'rna'
activeSpatUnit(viz) = 'cell'
## Generate polygons and binning
# Bin values using tessellate.
tx = getFeatureInfo(viz, feat_type = 'rna', return_giottoPoints = TRUE)
e = ext(tx) # get extent (spatial bounding box)
hexbin = tessellate(e, shape = 'hexagon', radius = 10, name = 'hexbin')
viz = setPolygonInfo(viz, hexbin)
pseudo_vis = makePseudoVisium(e)
viz = setPolygonInfo(viz, pseudo_vis, name = 'pseudo_visium')
viz = spatQueryGiottoPolygons(viz,
filters = list(pseudo_visium = 'all', hexbin = 'all'))
viz = calculateOverlapRaster(viz,
spatial_info = 'hexbin',
feat_info = 'rna')
viz = overlapToMatrix(viz,
poly_info = 'hexbin',
feat_info = 'rna',
name = 'raw')
activeSpatUnit(viz) = 'hexbin'
viz = filterGiotto(viz, expression_threshold = 1, feat_det_in_min_cells = 1, min_det_feats_per_cell = 1)
viz = normalizeGiotto(viz)
viz = addStatistics(viz)
viz = addSpatialCentroidLocations(viz,
poly_info = 'hexbin',
init_metadata = FALSE)
spatInSituPlotPoints(viz,
polygon_feat_type = 'hexbin',
polygon_line_size = 0.2,
polygon_fill = 'nr_feats',
polygon_fill_as_factor = FALSE,
point_size = 0.5,
feats = list(rna = featIDs(viz)[1:4]))
# linked spatial queries
# further explanation about the dataset and z stacks
# workflow diagram
## Completed Giotto Objects (short)
## which technologies
## easy to play with
##
viz2 = GiottoData::loadGiottoMini('vizgen')
viz2
# spatial manipulation of certain objects
# autocor
# create pre-gen html with images
# follow this up with pre-loaded objects