Skip to content

Commit

Permalink
spatial
Browse files Browse the repository at this point in the history
  • Loading branch information
Arezou Rahimi committed Jun 15, 2024
1 parent 84e57a2 commit 99e866b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 45 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Depends:
LazyData: true
Imports:
fields,
FNN,
ggplot2,
Matrix,
methods,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(run.DOT.lowresolution)
export(setup.ref)
export(setup.srt)
exportClasses(Dot)
importFrom(FNN,get.knn)
importFrom(Matrix,colMeans)
importFrom(Matrix,colSums)
importFrom(Matrix,rowMeans)
Expand Down
1 change: 1 addition & 0 deletions R/DOT-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom FNN get.knn
#' @importFrom Matrix colMeans
#' @importFrom Matrix colSums
#' @importFrom Matrix rowMeans
Expand Down
4 changes: 2 additions & 2 deletions R/dot.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ setup.srt <- function(srt_data, srt_coords = NULL, th.spatial = 0.84, th.nonspat

if(th.spatial > 0)
{
s$P <- get_pairs(s, th.spatial, th.nonspatial, nrow(s$X), verbose = verbose)
s$P <- get_pairs(s, th.spatial, th.nonspatial, nrow(s$X), radius = radius, verbose = verbose)
}

gc()
Expand Down Expand Up @@ -439,7 +439,7 @@ run.DOT.lowresolution <- function(object, ratios_weight = 0,
l_sp <- l_I * sparsity_coef / max_size
l_C <- inner_params[3] * S / C

if(is.null(object@srt$P))
if(is.null(object@srt$P) || nrow(object@srt$P) == 0)
{
l_S <- 0
}else
Expand Down
47 changes: 17 additions & 30 deletions R/process.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,32 +159,18 @@ get_de_genes <- function(ref_centroids, ref_ratios, max_genes, verbose = FALSE)
#' An internal method for computing the spatial neighborhood radius
#' @param coordinates Coordinates of spots
#' @param neighbors An internal parameter
#' @param sample_size An internal parameter
#' @param th.quantile An internal parameter
#' @return A numeric value
#' @keywords internal
#' @noRd
#'
find_radius <- function(coordinates, neighbors = 8, sample_size = 2000, th.quantile = 0.9)
find_radius <- function(coordinates, neighbors = 8, th.quantile = 0.9)
{
N <- nrow(coordinates)
if(N > sample_size)
{
sample_spots <- sample(1:N, sample_size)
dists <- fields::rdist(coordinates[sample_spots, ], coordinates)
}else
{
dists <- as.matrix(stats::dist(coordinates))
}
min_dists <- matrix(NA, nrow = nrow(dists), ncol = neighbors)
for(r in 1:nrow(dists))
{
min_dists[r, ] <- sort(dists[r, which(dists[r,]>0)])[1:neighbors]
}
knn <- FNN::get.knn(coordinates, neighbors)

mid_dists <- as.vector(min_dists)
knn <- as.vector(knn$nn.dist)

radius <- as.numeric(stats::quantile(min_dists, th.quantile)) # 99%
radius <- as.numeric(stats::quantile(knn, th.quantile)) # 90%

return(radius)
}
Expand All @@ -208,8 +194,7 @@ find_spatial_pairs <- function(nrm_X, coordinates, radius = "auto", neighbors =
message("Computing spatial radius")
if(radius == "auto")
{
radius <- find_radius(coordinates, neighbors = neighbors,
sample_size = 1000, th.quantile = th.quantile)
radius <- find_radius(coordinates, neighbors = neighbors, th.quantile = th.quantile)
radius <- radius * 1.05
# print(radius)
gc()
Expand Down Expand Up @@ -282,9 +267,11 @@ get_pairs <- function(srt, th.spatial, th.nonspatial, max_size, radius = 'auto',
srtp <- rbind(srtp, extra_pairs)
}

if(nrow(srtp) == 0 & verbose)
if(nrow(srtp) == 0)
{
message("No pairs satisfied the thresholds.")
if(verbose)
message("No pairs satisfied the thresholds.")

srtp <- NULL
}

Expand Down Expand Up @@ -366,14 +353,14 @@ draw_maps <- function(spatial, weights, normalize = TRUE, ncol = 4, trans = 'ide
ggplot2::scale_color_viridis_c(legend_title, option = viridis_option, trans = trans)+
ggplot2::xlab("") + ggplot2::ylab("")+
ggplot2::theme(axis.text.x = ggplot2::element_blank(),
axis.ticks.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.ticks.y = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
plot.background = ggplot2::element_rect(fill = "transparent", color = NA),
strip.background = ggplot2::element_rect(fill = 'transparent'),
panel.background = ggplot2::element_rect(fill = background)
axis.ticks.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.ticks.y = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
plot.background = ggplot2::element_rect(fill = "transparent", color = NA),
strip.background = ggplot2::element_rect(fill = 'transparent'),
panel.background = ggplot2::element_rect(fill = background)
)+
ggplot2::facet_wrap(~celltype, ncol = ncol)

Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# DOT: Flexible Feature Transfer to Spatial Omics <img src="man/figures/DOT_icon.png" align="right" height = "139">

<!-- badges: start -->
[![R-CMD-check](https://github.com/saezlab/DOT/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/saezlab/DOT/actions/workflows/R-CMD-check.yaml)
[![R-CMD-check](https://github.com/saezlab/DOT/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/saezlab/DOT/actions/workflows/R-CMD-check.yaml)
[![GitHub issues](https://img.shields.io/github/issues/saezlab/DOT)](https://github.com/saezlab/DOT/issues)

<!-- badges: end -->

## Overview

`DOT` is a method for transferring cell features from a reference single-cell RNA-seq data to spots/cells in spatial omics. It operates by optimizing a combination of multiple objectives using a Frank-Wolfe algorithm to produce a high quality transfer. Apart from transferring cell types/states to spatial omics, `DOT` can be used for transferring other relevant categorical or continuous features from one set of omics to another, such as estimating the expression of missinng genes or transferring transcription factor/pathway activities.
## Overview
`DOT` is a method for transferring cell features from a reference single-cell RNA-seq data to spots/cells in spatial omics. It operates by optimizing a combination of multiple objectives using a Frank-Wolfe algorithm to produce a high quality transfer. Apart from transferring cell types/states to spatial omics, `DOT` can be used for transferring other relevant categorical or continuous features from one set of omics to another, such as estimating the expression of missinng genes or transferring transcription factor/pathway activities.


<p align="center" width="100%">
<img src="man/figures/overview.png" align="center" width="65%">
</p>

For more information about how this package has been used with real data and expected outputs, please check the following link:

- [DOT's general usage](https://saezlab.github.io/DOT/articles/general.html)
<img src="man/figures/overview.png" align="center" width="65%">
</p>
For more information about how this package has been used with real data and expected outputs, please check the following link:
- [DOT's general usage](https://saezlab.github.io/DOT/articles/general.html)

## Installation
`DOT` is an R package which you can install from [GitHub](https://github.com/) with:
Expand All @@ -38,6 +38,6 @@ Installation takes less than five minutes. The sample dataset provided can be ru
Operating system tested on: macOS Monterey 12.4

## Citation
If you use **DOT** for your research please cite the [following preprint](https://arxiv.org/abs/2301.01682):
If you use **DOT** for your research please cite the [following article](https://doi.org/10.1038/s41467-024-48868-z):

> Rahimi, Arezou, Luis A. Vale-Silva, Maria Faelth Savitski, Jovan Tanevski, and Julio Saez-Rodriguez. "DOT: A flexible multi-objective optimization framework for transferring features across single-cell and spatial omics." arXiv preprint arXiv:2301.01682 (2023).
> Rahimi, A., Vale-Silva, L.A., Fälth Savitski, M. et al. DOT: a flexible multi-objective optimization framework for transferring features across single-cell and spatial omics. Nat Commun 15, 4994 (2024). https://doi.org/10.1038/s41467-024-48868-z.

0 comments on commit 99e866b

Please sign in to comment.