Get cells located within the polygons area

getCellsFromPolygon(
  gobject,
  polygon_name = "selections",
  spat_unit = "cell",
  spat_loc_name = "raw",
  polygons = NULL
)

Arguments

gobject

A Giotto object

polygon_name

name of polygon selections

spat_unit

spatial unit, default = 'cell'

spat_loc_name

name of spatial locations to use, default = 'raw'

polygons

character. A vector with polygon names to extract cells from. If NULL, cells from all polygons are retrieved

Value

A terra 'SpatVector' with cell ID, x y coordinates, and polygon ID where each cell is located in.

Examples

## Plot interactive polygons
g <- GiottoData::loadGiottoMini("visium")
#> 1. read Giotto object
#> 2. read Giotto feature information
#> 3. read Giotto spatial information
#> 3.1 read Giotto spatial shape information
#> cell_spatInfo_spatVector.shp
#> cell
#> 
#> 3.2 read Giotto spatial centroid information
#> cell
#> 
#> 3.3 read Giotto spatial overlap information
#> No overlaps were found, overlap loading will be
#>  skipped
#> 
#> 4. read Giotto image information
#> a giotto python environment was found
#> Using python path:
#>  "/Users/yuanlab/Library/r-miniconda/envs/giotto_env/bin/pythonw"
my_polygon_coords <- data.frame(poly_ID = rep("polygon1", 3),
sdimx = c(5477, 5959, 4720), sdimy = c(-4125, -2808, -5202))

## Add polygon coordinates to Giotto object
my_giotto_polygons <- createGiottoPolygonsFromDfr(my_polygon_coords,
name = "selections")
#>   Selecting col "poly_ID" as poly_ID column
#>   Selecting cols "sdimx" and "sdimy" as x and y respectively
g <- addGiottoPolygons(
    gobject = g,
    gpolygons = list(my_giotto_polygons)
)

## Add polygon IDs to cell metadata
addPolygonCells(g)
#> 
#> These column names were already used: in_tissue nr_feats perc_feats total_expr
#>  leiden_clus custom_leiden
#>  and will be overwritten
#> An object of class giotto 
#> >Active spat_unit:  cell 
#> >Active feat_type:  rna 
#> [SUBCELLULAR INFO]
#> polygons      : cell selections 
#> [AGGREGATE INFO]
#> expression -----------------------
#>   [cell][rna] raw normalized scaled
#> spatial locations ----------------
#>   [cell] raw
#> spatial networks -----------------
#>   [cell] Delaunay_network spatial_network
#> spatial enrichments --------------
#>   [cell][rna] cluster_metagene DWLS
#> dim reduction --------------------
#>   [cell][rna] pca custom_pca umap custom_umap tsne
#> nearest neighbor networks --------
#>   [cell][rna] sNN.pca custom_NN
#> attached images ------------------
#> images      : alignment image 
#> 
#> 
#> Use objHistory() to see steps and params used

## Get only cells from polygon 1
getCellsFromPolygon(g)
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 17, 4  (geometries, attributes)
#>  extent      : 4720, 5959, -5202, -2808  (xmin, xmax, ymin, ymax)
#>  coord. ref. :  
#>  names       : sdimx sdimy            cell_ID  poly_ID
#>  type        : <int> <int>              <chr>    <chr>
#>  values      :  5477 -4125 AAAGGGATGTAGCAAG-1 polygon1
#>                 5959 -2808 AAATGGCATGTCTTGT-1 polygon1
#>                 4720 -5202 AAATGGTCAATGTGCC-1 polygon1