Skip to contents

Calculate subcellular points/feature info or image values overlapped by polygon annotations. This provides a summary of the spatial data overlapped by the polygon which can be further processed to become an expression matrix.

Usage

# S4 method for giotto,missing
calculateOverlap(
  x,
  name_overlap = NULL,
  spatial_info = NULL,
  feat_info = NULL,
  image_names = NULL,
  poly_subset_ids = NULL,
  return_gobject = TRUE,
  verbose = TRUE,
  ...
)

# S4 method for giottoPolygon,giottoPoints
calculateOverlap(
  x,
  y,
  name_overlap = NULL,
  poly_subset_ids = NULL,
  feat_subset_column = NULL,
  feat_subset_ids = NULL,
  count_info_column = NULL,
  return_gpolygon = TRUE,
  verbose = TRUE,
  ...
)

# S4 method for giottoPolygon,giottoLargeImage
calculateOverlap(
  x,
  y,
  name_overlap = NULL,
  poly_subset_ids = NULL,
  return_gpolygon = TRUE,
  verbose = TRUE,
  ...
)

# S4 method for giottoPolygon,SpatRaster
calculateOverlap(
  x,
  y,
  name_overlap = NULL,
  poly_subset_ids = NULL,
  return_gpolygon = TRUE,
  verbose = TRUE,
  ...
)

# S4 method for SpatVector,SpatRaster
calculateOverlap(x, y, poly_subset_ids = NULL, verbose = TRUE, ...)

# S4 method for SpatVector,SpatVector
calculateOverlap(
  x,
  y,
  poly_subset_ids = NULL,
  feat_subset_column = NULL,
  feat_subset_ids = NULL,
  count_info_column = NULL,
  verbose = TRUE
)

Arguments

x

Object with spatial annotations: giottoPolygon, or SpatVector polygons. Can also be a giotto object

name_overlap

name for the overlap results (default to feat_info parameter)

spatial_info

character. Name polygon information

feat_info

character. Name of vector feature information to overlap

image_names

character vector. Name(s) of the image feature information to overlap

poly_subset_ids

character vector. (optional) Specific poly_IDs to use

return_gobject

return giotto object (default: TRUE)

verbose

be verbose

...

additional params to pass to methods.

y

Object with features to overlap: giottoPoints, giottoLargeImage, SpatVector points or SpatRaster

feat_subset_column

character. (optional) feature info attribute to subset feature points on when performing overlap calculation.

feat_subset_ids

(optional) values matched against in feat_subset_column in order to subset feature points when performing overlap calculation.

count_info_column

character. (optional) column with count information. Useful in cases when more than one detection is reported per point.

return_gpolygon

default = TRUE. Whether to return the entire giottoPolygon provided to x, but with the overlaps information appended or as a bare terra SpatVector

Value

Usually an object of the same class as x, with the overlaps information appended. return_* logical params usually allow return of a lower-level representation of the results instead. Only the SpatVector,SpatRaster method is different in that it returns a data.table.

Details

feat_subset_column, feat_subset_ids, and count_info_column are specific to overlaps on feature points info, and should not be provided when overlapping image data. These three params can also be passed to the giotto method through the ... param when working with overlaps on feature points info.

Examples

g <- GiottoData::loadGiottoMini("vizgen")
#> 1. read Giotto object
#> 2. read Giotto feature information
#> [1] "/home/runner/work/_temp/Library/GiottoData//Mini_datasets/Vizgen/VizgenObject//Features/rna_feature_spatVector.shp"
#> [1] "rna"
#> 3. read Giotto spatial information
#> 
#> 3.1 read Giotto spatial shape information
#> [1] "aggregate_spatInfo_spatVector.shp" "z0_spatInfo_spatVector.shp"       
#> [3] "z1_spatInfo_spatVector.shp"       
#> aggregate
#> z0
#> z1
#> 
#> 3.2 read Giotto spatial centroid information
#> aggregate
#> z0
#> z1
#> 
#> 3.3 read Giotto spatial overlap information
#> [1] "rna_aggregate_spatInfo_spatVectorOverlaps.shp"
#> [2] "rna_z0_spatInfo_spatVectorOverlaps.shp"       
#> [3] "rna_z1_spatInfo_spatVectorOverlaps.shp"       
#> [1] "poly_ID"      "feat_ID"      "feat_ID_uniq" "stack"       
#> aggregate and rna
#> [1] "poly_ID"      "feat_ID"      "feat_ID_uniq"
#> z0 and rna
#> [1] "poly_ID"      "feat_ID"      "feat_ID_uniq"
#> z1 and rna
#> 
#> 4. read Giotto image information
#> 
#> no external python path or giotto environment was specified, will check if a
#>  default python path is available
#> 
#> A default python path was found: /usr/bin/python3 and will be used
#>  If this is not the correct python path, either
#> 
#> 1. use installGiottoEnvironment() to install a local miniconda python
#>  environment along with required modules
#> 
#> 2. provide an existing python path to python_path to use your own python path
#>  which has all modules installed
#> Set options("giotto.use_conda" = FALSE) if python functionalities are not
#>  needed
#> Warning: module: pandas was not found with python path: /usr/bin/python3
#> Warning: module: igraph was not found with python path: /usr/bin/python3
#> Warning: module: leidenalg was not found with python path: /usr/bin/python3
#> Warning: module: community was not found with python path: /usr/bin/python3
#> Warning: module: networkx was not found with python path: /usr/bin/python3
#> Warning: module: sklearn was not found with python path: /usr/bin/python3
#> Warning: module: pandas was not found with python path: /usr/bin/python3
#> Warning: module: igraph was not found with python path: /usr/bin/python3
#> Warning: module: leidenalg was not found with python path: /usr/bin/python3
#> Warning: module: community was not found with python path: /usr/bin/python3
#> Warning: module: networkx was not found with python path: /usr/bin/python3
#> Warning: module: sklearn was not found with python path: /usr/bin/python3
gpoly <- getPolygonInfo(g,
    polygon_name = "aggregate",
    return_giottoPolygon = TRUE
)
gpoints <- getFeatureInfo(g, return_giottoPoints = TRUE)
gimg <- getGiottoImage(g, image_type = "largeImage")

gpoly@overlaps <- NULL
overlaps(gpoly) # Should now be NULL
#> NULL

# detections from 2 z-layers are provided
table(gpoints$global_z)
#> 
#>     0     1 
#> 35889 43872 

# calculate all transcripts overlapped
out_all <- calculateOverlap(gpoly, gpoints)
#> 1. convert polygon to raster
#> 2. overlap raster and points
#> 3. add polygon information
#> 4. add points information
#> 5. create overlap polygon information
overlaps_all <- overlaps(out_all)
overlaps_all$rna
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 79761, 3  (geometries, attributes)
#>  extent      : 6400.037, 6900.032, -5149.983, -4699.979  (xmin, xmax, ymin, ymax)
#>  coord. ref. :  
#>  names       : poly_ID feat_ID feat_ID_uniq
#>  type        :   <chr>   <chr>        <int>
#>  values      :      NA    Mlc1            1
#>                     NA  Gprc5b            2
#>                     NA    Gfap            3

# calculate z1 only
out_z1 <- calculateOverlap(gpoly, gpoints,
    feat_subset_column = "global_z",
    feat_subset_ids = c(1)
)
#> 1. convert polygon to raster
#> 2. overlap raster and points
#> 3. add polygon information
#> 4. add points information
#> 5. create overlap polygon information
overlaps_z1 <- overlaps(out_z1)
overlaps_z1$rna
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 43872, 3  (geometries, attributes)
#>  extent      : 6400.043, 6900.03, -5149.983, -4699.984  (xmin, xmax, ymin, ymax)
#>  coord. ref. :  
#>  names       : poly_ID feat_ID feat_ID_uniq
#>  type        :   <chr>   <chr>        <int>
#>  values      :      NA  Gabbr2        36198
#>                     NA    Gfap        36199
#>                     NA    Gfap        36200

# overlap image to get sum intensities per cell
out_img <- calculateOverlap(gpoly, gimg)
#> Start image extract
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
#> End image extract
overlaps_img <- overlaps(out_img)
overlaps_img$intensity
#> $dapi_z0
#>                                          poly_ID mini_dataset_dapi_z0
#>                                           <char>                <num>
#>       1: 100210519278873141813371229408401071444                   18
#>       2: 100210519278873141813371229408401071444                   18
#>       3: 100210519278873141813371229408401071444                   18
#>       4: 100210519278873141813371229408401071444                   18
#>       5: 100210519278873141813371229408401071444                   18
#>      ---                                                             
#> 3170620:  98561957902191275233320065611022298397                   98
#> 3170621:  98561957902191275233320065611022298397                   98
#> 3170622:  98561957902191275233320065611022298397                   88
#> 3170623:  98561957902191275233320065611022298397                   88
#> 3170624:  98561957902191275233320065611022298397                   88
#> 

# giotto method
# calculate z0 overlaps and return as gobject
out_g <- calculateOverlap(g,
    feat_subset_column = "global_z",
    feat_subset_ids = 0
)
#> 1. convert polygon to raster
#> 2. overlap raster and points
#> 3. add polygon information
#> 4. add points information
#> 5. create overlap polygon information
#> Warning: module: pandas was not found with python path: /usr/bin/python3
#> Warning: module: igraph was not found with python path: /usr/bin/python3
#> Warning: module: leidenalg was not found with python path: /usr/bin/python3
#> Warning: module: community was not found with python path: /usr/bin/python3
#> Warning: module: networkx was not found with python path: /usr/bin/python3
#> Warning: module: sklearn was not found with python path: /usr/bin/python3
overlaps(getPolygonInfo(out_g, return_giottoPolygon = TRUE))
#> No polygon information named "cell" discovered.
#>  Selecting first available (" z0 ")
#> $rna
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 35889, 3  (geometries, attributes)
#>  extent      : 6400.037, 6900.032, -5149.983, -4699.979  (xmin, xmax, ymin, ymax)
#>  coord. ref. :  
#>  names       : poly_ID feat_ID feat_ID_uniq
#>  type        :   <chr>   <chr>        <int>
#>  values      :      NA    Mlc1            1
#>                     NA  Gprc5b            2
#>                     NA    Gfap            3
#> 

# note that z0 and z1 nrows match that from the table of global z values.
# With points overlaps, all points are returned, but non-overlapped points
# only have an `NA` value for the `poly_ID` column. Overlapped points will
# have the `poly_ID` of their overlapping polygon.