diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml new file mode 100644 index 00000000..f57192b5 --- /dev/null +++ b/.github/workflows/pkgdown.yaml @@ -0,0 +1,57 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +name: pkgdown + +on: + push: + branches: [website] + pull_request: + branches: [website] + release: + types: [published] + workflow_dispatch: + +jobs: + pkgdown: + runs-on: ubuntu-latest + # Only restrict concurrency for non-PR jobs + concurrency: + group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + + - name: Checkout repo for workflow access + uses: actions/checkout@v3 + + - name: Setup pandoc + uses: r-lib/actions/setup-pandoc@v2 + + - name: Setup R environment + uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - name: Set up dependencies (general) + uses: r-lib/actions/setup-r-dependencies@v2 + env: + _R_CHECK_FORCE_SUGGESTS: false + _R_CHECK_RD_XREFS: false + with: + dependencies: '"hard"' # do not use suggested dependencies + extra-packages: any::pkgdown, any::scattermore, any::exactextractr, local::. + needs: website + + - name: Build site + run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) + shell: Rscript {0} + + - name: Deploy to GitHub pages 🚀 + if: github.event_name != 'pull_request' + uses: JamesIves/github-pages-deploy-action@v4.4.1 + with: + clean: false + branch: gh-pages + folder: docs diff --git a/_pkgdown.yml b/_pkgdown.yml new file mode 100644 index 00000000..1b21ca3e --- /dev/null +++ b/_pkgdown.yml @@ -0,0 +1,441 @@ +url: https://drieslab.github.io/GiottoClass/ + + +authors: + Ruben Dries: + href: https://www.drieslab.com + Guo-Cheng Yuan: + href: https://labs.icahn.mssm.edu/yuanlab/ + Joselyn C. Chávez-Fuentes: + href: https://josschavezf.github.io + + + +template: + bootstrap: 5 + bslib: + bg: "#FFFFFF" + fg: "#000000" + primary: "#7E69AE" + code_font: {google: "JetBrains Mono"} + +navbar: + bg: light + structure: + left: + - intro + - reference + - articles + - news + right: + - suite + - github + components: + reference: + text: Documentation + href: reference/index.html + articles: + text: Articles + menu: + - text: Spatial Geometry Objects + href: articles/spatial_geometries.html + - text: Images + href: articles/image_tools.html + news: + text: News + href: news/index.html + suite: + text: Suite + href: https://drieslab.github.io/Giotto/ + github: + icon: fab fa-github fa-lg + href: https://github.com/drieslab/GiottoClass/ + aria-label: github + +news: + cran_dates: false + +home: + sidebar: + structure: [links, license, community, citation, authors, dev] + +reference: +- title: Giotto + desc: Create a Giotto Object +- contents: + - createGiottoObject + - createGiottoObjectSubcellular +- title: Updaters + desc: Objects version compatibility v3.0 and up +- contents: + - updateGiottoObject + - updateGiottoPointsObject + - updateGiottoPolygonObject +- title: Ingestion +- subtitle: Read + desc: Read data in and create Giotto-native objects +- contents: + - readCellMetadata + - readFeatMetadata + - readPolygonData + - readFeatData + - readExprData + - readExprMatrix + - readSpatLocsData + - readSpatNetData + - readSpatEnrichData + - readDimReducData + - readNearestNetData +- subtitle: evaluate + desc: Evaluate and format data for usage with Giotto +- contents: + - evaluate_input +- subtitle: Create + desc: Create Giotto-native objects +- contents: + - createCellMetaObj + - createDimObj + - createExprObj + - createFeatMetaObj + - createGiottoPoints + - createGiottoPolygon + - createNearestNetObj + - createSpatEnrObj + - createSpatLocsObj + - createSpatNetObj + - createGiottoImage + - createGiottoLargeImage + - createGiottoLargeImageList +- title: Access +- subtitle: Getters + desc: Get data from a Giotto Object +- contents: + - getCellMetadata + - getFeatureMetadata + - getPolygonInfo + - getFeatureInfo + - getExpression + - getSpatialLocations + - getSpatialNetwork + - getSpatialEnrichment + - getDimReduction + - getMultiomics + - getNearestNetwork + - getGiottoImage +- subtitle: Setters + desc: Set (and replace) Giotto-native data objects in a Giotto Object +- contents: + - setGiotto + - setCellMetadata + - setFeatureMetadata + - setPolygonInfo + - setFeatureInfo + - setExpression + - setSpatialLocations + - setSpatialNetwork + - setSpatialEnrichment + - setDimReduction + - setMultiomics + - setNearestNetwork + - setGiottoImage +- subtitle: Append + desc: Add information to the Giotto Object +- contents: + - addCellMetadata + - addFeatMetadata + - addGiottoImage + - addGiottoImageMG + - addGiottoLargeImage +- title: Gobject Contents +- subtitle: Print + desc: Print Giotto object slot contents +- contents: + - showGiottoCellMetadata + - showGiottoFeatMetadata + - showGiottoSpatialInfo + - showGiottoFeatInfo + - showGiottoExpression + - showGiottoSpatLocs + - showGiottoSpatNetworks + - showGiottoSpatEnrichments + - showGiottoDimRed + - showGiottoNearestNetworks + - showGiottoImageNames +- subtitle: Return available data + desc: Return slot contents as data.table with nested locations +- contents: + - list_cell_metadata + - list_dim_reductions + - list_expression + - list_feat_metadata + - list_feature_info + - list_images + - list_nearest_networks + - list_spatial_enrichments + - list_spatial_grids + - list_spatial_info + - list_spatial_locations + - list_spatial_networks +- subtitle: Return names of available data + desc: List slot contents as character vector. The spat_unit and/or feat_type usually must be provided. +- contents: + - list_cell_id_names + - list_feat_id_names + - list_dim_reductions_names + - list_expression_names + - list_feature_info_names + - list_images_names + - list_nearest_networks_names + - list_spatial_enrichments_names + - list_spatial_grids_names + - list_spatial_info_names + - list_spatial_locations_names + - list_spatial_networks_names +- title: Annotate + desc: Giotto object metdata and annotation tools +- contents: + - pDataDT + - fDataDT + - annotateGiotto + - removeCellAnnotation + - removeFeatAnnotation + - annotateSpatialNetwork + - calculateMetaTable + - calculateMetaTableCells + - calculateSpatCellMetadataProportions +- title: Instructions + desc: Set instructions for Giotto Object behavior +- contents: + - instructions-generic + - createGiottoInstructions + - showGiottoInstructions + - readGiottoInstructions + - changeGiottoInstructions + - replaceGiottoInstructions +- title: Subset + desc: Subsetting the Giotto Object +- contents: + - subsetGiotto + - subsetGiottoLocs + - subsetGiottoLocsMulti + - subsetGiottoLocsSubcellular +- title: Join + desc: Joining multiple Giotto Objects +- contents: + - joinGiottoObjects +- title: Poly Generation + desc: Creation of polygon shapes and arrays +- contents: + - hexVertices + - rectVertices + - circleVertices + - makePseudoVisium + - polyStamp + - tessellate + - triGrid + - orthoGrid +- title: Interoperability +- subtitle: From + desc: Convert from other frameworks to Giotto +- contents: + - matches("ToGiotto") + - giottoMasterToSuite +- subtitle: To + desc: Convert Giotto Object to other frameworks +- contents: + - starts_with("giottoTo") + - createBentoAdata +- title: Aggregate +- subtitle: Polygon + desc: Polygon and spatial unit aggregation and combination +- contents: + - aggregateStacks + - aggregateStacksExpression + - aggregateStacksLocations + - aggregateStacksPolygonOverlaps + - aggregateStacksPolygons + - combineToMultiPolygon +- subtitle: Feature + desc: Calculate spatial features overlapped by polygons +- contents: + - calculateOverlap + - calculateOverlapRaster + - calculateOverlapPolygonImages + - calculateOverlapSerial + - calculateOverlapParallel + - overlaps-generic +- subtitle: Matrix + desc: Convert overlapped features to raw expression matrix +- contents: + - overlapImagesToMatrix + - overlapToMatrix + - overlapToMatrixMultiPoly +- subtitle: Centroids + desc: Centroid calculation +- contents: + - addSpatialCentroidLocations + - addSpatialCentroidLocationsLayer + - centroids-generic +- title: Combine + desc: Create combined table of metadata and other results and data +- contents: + - combineCellData + - combineFeatureData + - combineFeatureOverlapData + - combineMetadata + - combineSpatialCellFeatureInfo + - combineSpatialCellMetadataInfo +- title: Metafeatures + desc: Metafeature creation +- contents: + - createMetafeats +- title: Classes + desc: GiottoClass defined classes +- contents: + - ends_with("-class") +- title: Spatial network + desc: Spatial network creation and tools +- contents: + - createSpatialNetwork + - createSpatialDelaunayNetwork + - createSpatialFeaturesKNNnetwork + - createSpatialKNNnetwork + - createSpatialWeightMatrix + - spat_net_to_igraph +- title: Spatial +- subtitle: Manipulation + desc: Spatial manipulation and terra-based functions +- contents: + - ext + - flip + - crop + - transpose-generic + - spin + - spatShift + - wrap + - rescale + - rescalePolygons +- subtitle: Querying + desc: Spatial querying + contents: + - spatQueryGiottoPolygons +- title: NN Networks + desc: Nearest neighbor networks for analysis of dimension reduction information + contents: + - createNearestNetwork + - addNetworkLayout +- title: Object generics + desc: Standard generics for Giotto exported classes +- contents: + - plot-generic + - extract-methods + - rbind-generic + - show,giotto-method + - row-plus-colnames-generic + - dimnames + - dims-generic + - copy +- subtitle: Hierarchical + desc: Hierarchical tagging generics for Giotto exported classes +- contents: + - activeFeatType-generic + - activeSpatUnit-generic + - featType-generic + - spatUnit-generic + - prov-generic + - objName-generic + - spatIDs-generic + - overlaps-generic +- title: Save/Load + desc: Saving and loading of the Giotto Object +- contents: + - loadGiotto + - saveGiotto + - reconnectGiottoImage +- title: As + desc: As coercion functions +- contents: + - as.points + - as.polygons + - as.data.table + - r_spatial_conversions +- title: Python + desc: Giotto python environment +- contents: + - installGiottoEnvironment + - checkGiottoEnvironment + - checkPythonPackage + - removeGiottoEnvironment +- title: Coordinate stitching + desc: Coordinate stitching +- contents: + - stitchFieldCoordinates + - stitchTileCoordinates +- title: Images + desc: Image tools +- contents: + - stitchGiottoLargeImage + - convertGiottoLargeImageToMG + - estimateImageBg + - distGiottoImage + - plot_auto_largeImage_resample + - add_img_array_alpha + - changeImageBg + - writeGiottoLargeImage + - updateGiottoImage + - updateGiottoImageMG + - updateGiottoLargeImage +- title: History + desc: Functions for logging Giotto Object history +- contents: + - objHistory + - update_giotto_params + - showProcessingSteps +- title: Utilities + desc: Utility functions that are used in other code +- contents: + - polygon_to_raster + - smoothGiottoPolygons +- title: Deprecated + desc: Deprecated and/or less supported older functions. Some of these functions are here for compatibility and internal purposes. +- contents: + - getSpatialGrid + - get_NearestNetwork + - get_dimReduction + - get_distance + - get_expression_values + - get_feature_info + - get_giottoImage + - get_multiomics + - get_polygon_info + - get_spatialGrid + - get_spatialNetwork + - get_spatial_enrichment + - get_spatial_locations + - createSpatialDefaultGrid + - createSpatialGrid + - setSpatialGrid + - set_NearestNetwork + - set_dimReduction + - set_expression_values + - set_feature_info + - set_giottoImage + - set_multiomics + - set_polygon_info + - set_spatialGrid + - set_spatialNetwork + - set_spatial_enrichment + - set_spatial_locations + - generate_grid + - cropGiottoLargeImage + - plotGiottoImage + - showGiottoSpatGrids + - showGrids + - showNetworks + - addGiottoPoints + - addGiottoPoints3D + - addGiottoPolygons + - annotateSpatialGrid + - annotate_spatlocs_with_spatgrid_2D + - annotate_spatlocs_with_spatgrid_3D + diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..04c55859 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,14 @@ +comment: false + +coverage: + status: + project: + default: + target: auto + threshold: 1% + informational: true + patch: + default: + target: auto + threshold: 1% + informational: true diff --git a/vignettes/image_tools.Rmd b/vignettes/image_tools.Rmd new file mode 100644 index 00000000..617c32be --- /dev/null +++ b/vignettes/image_tools.Rmd @@ -0,0 +1,104 @@ +--- +title: "Giotto image tools" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Giotto image tools} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +# Giotto Image Tools + +## 1. Overview + +Giotto uses `giottoLargeImage` to represent images and raster information. This +is an S4 class built based on the *terra* `SpatRaster`. + +An older class called `giottoImage` based on the *magick* package also exists, +but is currently being phased out. Ideally, `giottoLargeImages` will also +be renamed to `giottoImage` afterwards, simplifying the naming scheme. + +The object structure of `giottoLargeImage` +``` +giottoLargeImage +\- name (object name) +\- raster_object (terra raster object) +\- extent (current spatial extent) +\- overall_extent (spatial extent of original image - experimental) +\- scale_factor (pixels per coordinate unit - experimental) +\- resolution (coordinate units covered per pixel) +\- max_intensity (approximate maximum intensity value) +\- min_intensity (approximate minimum intensity value) +\- max_window (value to set as maximum intensity in color scaling) +\- colors (vector of color mappings provided as hex codes) +\- is_int (whether values are integers) +\- file_path (filepath to the image) +\- OS_platform (operating system) +``` + + +## 2. Sampling + +Spatial images are often extremely large files. The high detail (resolution), +large spatial region captured, and precision of recorded values (bitdepth) +often results in files in the 10s of gigabytes. This makes the full size images +difficult to work with. One way to get around this is to perform regular +sampling of the image, touching only the values of the original image that are +needed generate a downscaled representation. This is implemented in *terra* +as `spatSample()`. + +When plotting, *Giotto* optimizes the speed of this sampling by striking a balance +between `terra::crop()` and `terra::spatSample()` to try to prevent sampling from +unnecessary regions, but also avoid large crop operations on the fullsize image, +which can be very costly. This is done through `plot_auto_largeImage_resample()` + +`giottoLargeImages` can also be resampled to other image formats (`magick` and +`EBImage`) in addition to `data.table` and `array` using `GiottoClass:::.spatraster_sample_values()` + +These two functions are experimental and they will be exported using more common +function names in the future. + +## 3 Color scaling +`distGiottoImage()` can be used to look at the values present within a +`giottoLargeImage`. It is often the case that the values recorded within an +image do not map to the full set of values allowed by its bitdepth. +*Giotto* plots images by guessing the bitdepth based on the estimated maximum +value detected. This mapping may not always be optimal. + +```{r} +library(GiottoClass) +gimg <- GiottoData::loadSubObjectMini("giottoLargeImage", idx = 2) +gimg <- GiottoClass:::.update_giotto_image(gimg) # update older images that are missing slots + +plot(gimg) +distGiottoImage(giottoLargeImage = gimg) +``` + +Since there are few values beyond 70, try setting `max_window` to 70 in so +that the color mapping better represents the available information. + +```{r} +gimg@max_window <- 70 +plot(gimg) +# plot(gimg, max_intensity = 150) # can be used without setting the slot +``` + + +The colormap to use for an image can also be edited. The default for a greyscale +image is a monochrome black to white. Other color scales can be supplied. +For monochrome colorscales: `getMonochromeColors()` +```{r} +gimg@colors <- getMonochromeColors("green") +plot(gimg) +viridis_colors <- hcl.colors(256, palette = "viridis") +plot(gimg, col = viridis_colors) # can be used without setting the slot +``` + +### TODOs: + +- provide accessors for these slots +- make *ggplot*-based plotting also obey these settings + +```{r} +sessionInfo() +``` diff --git a/vignettes/spatial_geometries.Rmd b/vignettes/spatial_geometries.Rmd new file mode 100644 index 00000000..e4880770 --- /dev/null +++ b/vignettes/spatial_geometries.Rmd @@ -0,0 +1,197 @@ +--- +title: "Giotto spatial geometry classes" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Giotto spatial geometry classes} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +## 1. Overview + +*GiottoClass* represents spatial polygons and points respectively as S4 classes +`giottoPolygon` and `giottoPoints`. These objects are built on top of *terra* +`SpatVectors` in order to represent spatial biology. They also respond to many +of terra's generics, with some enhancements. Both objects have a +`spatVector` slot that contains the main information. This common slot is inherited +from the `VIRTUAL` `terraVectData` class, however this naming may be subject to +change as other data representations are added. + + +The structures of these classes are: + +``` +terraVectData +\- spatVector + +giottoPolygon +\- spatVector (spatial polygons SpatVector) +\- spatVectorCentroids (centroids points SpatVector) +\- overlaps (list of overlapped features info) +\- name (object name/spat_unit) +\- unique_ID_cache (cache of polygon IDs) + +giottoPoints +\- spatVector (spatial points SpatVector) +\- networks (feature network) +\- feat_type (object name/feat_type) +\- unique_ID_cache (cache of feature IDs) +``` +Of note are the `unique_ID_cache` slots which are used to get around the memory +usage incurred when frequently accessing the `SpatVectors` attributes. The caches +should be updated after modifications to the object. This is automatically +performed when using `[]` subsetting. + + +## 2.1 Polygon object creation + +`createGiottoPolygon()` accepts a number of inputs, including filepaths to spatial +files such as .wkt, .shp, or .GeoJSON or mask image files of common formats such +as .tif or .png. After reading in these files, they are passed to the respective +methods for `SpatVector` and `SpatRaster` (mask image). + +`data.frame-like` inputs are also allowed as long as they are formatted similarly +to *terra's* `matrix` representation of polygon geometries, with at least the +following columns. + +- `geom`: integer identifier for each polygon +- `part`: integer identifier for each part of the polygon, in the case of a multi polygon +- `x`: numeric x coordinate of a polygon vertex +- `y`: numeric y coordinate of a polygon vertex +- `hole`: integer identifier for each hole within a polygon +- `poly_ID`: standard Giotto identifier for the polygon. Akin to cell_ID + +Additional columns can be included as other attributes, but a set of `poly_ID`s +are required. + +*For de-novo generation of simple polygon arrays, see the documentation for +`polyStamp()` and `tessellate()`* + +## 2.2 Points object creation + +There are relatively fewer input methods for `giottoPoints` since they currently +tend to be provided as flat files as opposed to spatial-specific formats. +`createGiottoPoints()` works with +with `data.frame-like` inputs with at least columns for x-coordinate, y-coordinate, +and ID information. The first two detected numeric columns are expected to be +x and y respectively. The first character column is assumed to be the feat_IDs. +*terra* `SpatVector` inputs are also accepted. + +A helpful parameter is `split_keyword` which accepts a list of regex keywords +to pull matched features into separate `giottoPoints` objects. This is useful +in situations where multiple feature modalities and/or QC targets are provided +together and would interfere with each other if analyzed together. (see examples +in `createGiottoPoints()` documentation) + + +## 3. Subsetting + +`giottoPoints` and `giottoPolygons` can be subset either by logical vectors +or by ID. +```{r} +library(GiottoClass) +gpoly <- GiottoData::loadSubObjectMini("giottoPolygon") +gpoints <- GiottoData::loadSubObjectMini("giottoPoints") + +# full objects +print(gpoly) +print(gpoints) + +# subsets +plot(gpoly) +plot(gpoly[c("100210519278873141813371229408401071444", "101161259912191124732236989250178928032", "101488859781016188084173008420811094152")]) +print(gpoly[c("100210519278873141813371229408401071444", "101161259912191124732236989250178928032", "101488859781016188084173008420811094152")]) +plot(gpoly[c(T,F)]) + +plot(gpoints) +plot(gpoints[c("Fn1")]) +print(gpoints[c("Fn1")]) +``` + +`$` extraction can be used to pull attributes of these objects as vectors +```{r} +head(gpoly$poly_ID) +``` + +## 4. Conversion to data.table +*GiottoClass* has a set of functions for converting `giottoPoints` and `giottoPolygon` +objects between spatial classes using `as.sf()`, `as.sp()`, `as.stars()`, and +`as.terra()`. +*terra*-based `giottoPoints` and `giottoPolygon` objects can also +be converted into `data.table` for geometry manipulation. This method largely +piggybacks off *terra*'s `as.data.frame()` and adds support for `geom = "XY"` +for polygon geometry. + +```{r} +gpoly_dt <- data.table::as.data.table(gpoly, geom = "XY") +gpoints_dt <- data.table::as.data.table(gpoints, geom = "XY") + +print(gpoly_dt) +print(gpoints_dt) +``` + +These tables can then be either re-ingested using `createGiottoPolygon()` and +`createGiottoPoints()` or converted to `SpatVector` using `as.polygons()` or +`as.points()` +```{r} +print(as.polygons(gpoly_dt)) + +# gpoints currently still requires addition of geom, part, and hole cols +gpoints_dt[, geom := 1:.N] +gpoints_dt[, part := 1] +gpoints_dt[, hole := 0] +print(as.points(gpoints_dt)) +``` + +## 5. Centroids +Centroids information are carried by `giottoPolygon` objects in the `spatVectorCentroids` +slot. The `centroids()` generic from *terra* pulls from this slot if the +information already exists. Otherwise, it calculates and returns a set of +centroids for the polygons as `SpatVector` points. The `append_gpolygon` param +makes it so that the `giottoPolygon` with the centroids info appended is returned +instead. + + +## 6.1 Overlaps +Overlaps are sets of features overlapped by the polygons. `calculateOverlaps()` +is a generic function that performs this overlapping between polygons and points +and polygons and raster (intensity) data. +```{r} +gpoly@overlaps <- NULL # reset overlaps info +gpoly <- calculateOverlap(gpoly, gpoints, verbose = FALSE) + +gimg <- GiottoData::loadSubObjectMini("giottoLargeImage") +gpoly <- calculateOverlap(gpoly, gimg, verbose = FALSE, progress = FALSE) +``` + + +Overlaps are stored as a `list` under the `giottoPolygon` `overlaps` slot, +separated by modalities. This list can be retrieved using `overlaps()`. +In the case of overlapped points geometries, the list items are points `SpatVector` +objects. The `poly_ID` column tracks which polygon is overlapping the point +feature as designated by `feat_ID` and `feat_ID_uniq`. `NA` values mean that +the feature was not overlapped. + +For overlaps intensities, the results are stored as `data.tables` in a nested +`list` called `intensity` under the main `overlaps` `list`. +```{r} +print(gpoly@overlaps$rna) +print(gpoly@overlaps$intensity) +``` + +## 6.2 Overlaps to matrix +Overlap results can be converted into expression matrices using `overlapToMatrix()` +```{r} +# points, rna modality (default) +rna_mat <- overlapToMatrix(gpoly) +print(rna_mat[1:3,1:3]) + +# intensity, dapi_z0 +intens_mat <- overlapToMatrix(gpoly, type = "intensity", feat_info = "dapi_z0") +print(intens_mat[1, 1:3, drop = FALSE]) +``` + +```{r} +sessionInfo() +``` +