Spatial Measures
介绍
空间转录组学和图像分析领域中,研究人员试图回答的许多问题都与空间相关。观测值的坐标通常会缩放到当前使用的图像分辨率。以Visium平台为例,如果将10X Visium输出中的tissue_lowres_image.png替换为tissue_hires_image.png,条形码斑点的坐标会相应缩放,以确保坐标与图像保持对齐。因此,坐标可用于回答相对空间问题,但不能用于绝对度量。例如,“坏死区域的面积约为400像素”这一说法是没有意义的。如果以像素为单位,面积将始终取决于图像分辨率。本教程将解释SPATA2如何通过像素比例因子计算Visium平台从像素到SI单位的转换,并提供关于如何利用SI距离进行工作的示例。
library(SPATA2) library(tidyverse)# load SPATA2 inbuilt example data (and rename an image for this tutorial) object_t275 <- loadExampleObject("UKF275T") %>% renameImage(img_name = "image1", new_img_name = "lowres")text_size <- theme(text = element_text(size = 17.5)) plotImage(object_t275, img_name = "lowres") + text_size + labs(subtitle = "Normal resolution")# very_low_res image has been registered using `registerImage()` plotImage(object_t275, img_name = "very_low_res") + text_size + labs(subtitle = "Very low resolution")2. 像素比例因子
要以绝对数值回答问题,必须将距离转换为国际单位制(SI)单位,即微米、毫米、厘米等。这是可能的,因为大多数空间组学研究都带有真实值。
2.1 Visium
在10X Visium的情况下,此真实值是两个相邻条形码点之间的中心到中心距离,该距离始终为100微米。利用此信息,可通过像素比例因子计算实际距离和面积。该比例因子由initiateSpataObjectVisium()自动计算。(如果您在SPATA2对象中注册了多张图像,请注意,默认情况下会使用活动图像的像素比例因子。)
# how many um is one pixel in side lengths getPixelScaleFactor(object_t275, unit = "um")## [1] 13.68328 ## attr(,"unit") ## [1] "um/px"# how many pixel is one um in side lengths getPixelScaleFactor(object_t275, unit = "um", switch = TRUE)## [1] 0.07308187 ## attr(,"unit") ## [1] "px/um"# by default the pixel scale factor of the active image is used getImageNames(object_t275)## [1] "lowres" "very_low_res"# which is image 'lowres' in this example activeImage(object_t275)## [1] "lowres"# how many um is one pixel in side lengths in case of an even lower resolution? getPixelScaleFactor(object_t275, unit = "um", img_name = "very_low_res")## [1] 78.94202 ## attr(,"unit") ## [1] "um/px"2.2 MERFISH and Co
像素比例因子这一术语源于SPATA2最初仅被认为适用于Visium数据集的时期。后来,其应用范围已扩展到其他平台,例如MERFISH,该平台提供以微米为单位的细胞坐标。如果平台提供的观测坐标单位为国际单位制(SI)单位,则理论上不需要像素比例因子。但为了使SPATA2代码正常运行,SPATA2object中必须存在该比例因子。因此,它会由相应的initiateSpataObject*()函数自动设置。例如,在MERFISH数据中,x坐标和y坐标以微米(um)为单位提供。因此,通过initiateSpataObjectMERFISH()函数初始化的SPATA2对象的像素比例因子始终为1毫米/像素。
3. 距离的运用
多个函数的参数以某种方式涉及距离度量。SPATA2 使用 units 包来处理距离。除非文档中有其他说明,否则可以将距离以像素或 SI 单位提供。在后台,输入会被转换为像素并与当前分辨率对齐。4.1 距离转换部分给出了如何进行此操作的一些示例。
3.1 处理距离输入和输出
validUnitsOfLength()## nanometer micrometer millimeter centimeter decimeter meter pixel ## "nm" "um" "mm" "cm" "dm" "m" "px"# wrappers around units::set_units() as_micrometer(input = "4mm")## 4000 [um]as_millimeter(input = "4cm")## 40 [mm]### convert from si -> pixel # example 1: a simple string works as_pixel(input = "4mm", object = object_t275)## [1] 292.3275 ## attr(,"unit") ## [1] "px"# example 2: a unit object works, too unit_input <- units::set_units(x = 4, value = "mm") unit_input## 4 [mm]class(unit_input)## [1] "units"as_pixel(input = unit_input, object = object_t275)## [1] 292.3275 ## attr(,"unit") ## [1] "px"### convert from pixel -> si # example 1: simple numeric input is interpreted as pixel as_millimeter( input = c(100, 200, 300), object = object_t275 )## Units: [mm] ## [1] 1.368328 2.736657 4.104985# example 2: strings with px suffix work, too as_micrometer( input = c("50px", "200px", "800px"), object = object_t275 )## Units: [um] ## [1] 684.1642 2736.6568 10946.62713.2 Examples
以下是一些实际距离可能发挥作用的例子。
# specifying x- and y-range while handling images xrange = c("2.5mm", "6.5mm") yrange = c("0.5mm", "4.5mm") # where to set the breaks is a measure of distance, too breaks <- str_c(0:8, "mm") # vector of valid distance inputs print(breaks)## [1] "0mm" "1mm" "2mm" "3mm" "4mm" "5mm" "6mm" "7mm" "8mm"axes_add_on <- ggpLayerAxesSI(object_t275, unit = "mm", breaks = breaks) rect_add_on <- ggpLayerRect( object = object_t275, xrange = xrange, yrange = yrange ) plotImage(object = object_t275) + rect_add_on + axes_add_on + text_sizeplotImage( object = object_t275, xrange = xrange, # crop the image with distance input yrange = yrange # crop the image with distance input ) + text_sizeplotImage(object_t275) + ggpLayerAxesSI(object_t275, unit = "mm", breaks = breaks) + ggpLayerSpatAnnOutline(object_t275, ids = "img_ann_1", fill = "orange") + ggpLayerHorizonSAS(object_t275, id = "img_ann_1", distance = "1mm") + ggpLayerScaleBarSI(object_t275, sb_dist = "1mm", sb_pos = c("5.5mm", "7.5mm"), text_size = 15, text_nudge_y = 10) + text_size4. 与区域相关的工作
除了距离之外,还可以计算面积。这在例如图像注释的面积与生物学问题相关时会变得有趣。
4.1 处理区域输入和输出
validUnitsOfArea()## [1] "nm2" "um2" "mm2" "cm2" "dm2" "m2" "px"validUnitsOfAreaSI()## [1] "nm2" "um2" "mm2" "cm2" "dm2" "m2"getPixelScaleFactor(object_t275, unit = "um2", img_name = "lowres")## [1] 187.2323 ## attr(,"unit") ## [1] "um2/px"# more um2 per pixel in the very low resolution image # -> pixels are smaller getPixelScaleFactor(object_t275, unit = "um2", img_name = "very_low_res")## [1] 6231.843 ## attr(,"unit") ## [1] "um2/px"# numeric input is interpreted as pixel as_millimeter2(input = c(200, 400, 4000, 50000), object = object_t275)## Units: [mm^2] ## [1] 0.03744645 0.07489290 0.74892903 9.36161289# if character, different units can be specified as input as_centimeter2(input = c("4mm2", "400px"), object = object_t275)## Units: [cm^2] ## [1] 0.040000000 0.0007489294.2 Examples
# process the diet example object object_t313 <- identifyTissueOutline(example_data$object_UKF313T_diet)# set ids of example spatial annotations ids <- c("necrotic_area", "necrotic_edge", "necrotic_edge2") plotSpatialAnnotations(object_t313, ids = ids, nrow = 1)# process the diet example object object_mouse <- loadExampleObject("LMU_MCI") object_mouse <- identifyTissueOutline(object_mouse)# get tissue area by tisse section getTissueArea(object_mouse, unit = "mm2")## Units: [mm^2] ## tissue_section_1 tissue_section_2 ## 6.349445 7.945489getTissueArea(object_t313, unit = "mm2")## 29.67623 [mm^2]# left plotImage(object_t313, unit = "mm") + ggpLayerTissueOutline(object_t313)# right plotImage(object_mouse, unit = "mm") + ggpLayerTissueOutline(object_mouse)# use the three necrotic ids from above spat_ann_areas <- getSpatAnnArea(object = object_t313, ids = ids, unit = "mm2") print(spat_ann_areas)## Units: [mm^2] ## necrotic_area necrotic_edge necrotic_edge2 ## 6.1849516 0.4618869 0.6252351threshold <- units::set_units(x = 1, value = "mm2") # keep only those with an area smaller than 1mm2 print(spat_ann_areas[spat_ann_areas < threshold])## Units: [mm^2] ## necrotic_edge necrotic_edge2 ## 0.4618869 0.6252351