Commit 4e61437c authored by smorabit's avatar smorabit
Browse files

version 0.2.00 ST tutorial

parent d2ae4bf7
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
Package: hdWGCNA
Package: hdWGCNA
Title: hdWGCNA
Title: hdWGCNA
Version: 0.1.2.0001
Version: 0.2.00
Authors@R: c(
Authors@R: c(
    person("Sam", "Morabito", , "smorabit@uci.edu", role = c("aut", "cre"),
    person("Sam", "Morabito", , "smorabit@uci.edu", role = c("aut", "cre"),
           comment = c(ORCID = "0000-0002-7768-4856")),
           comment = c(ORCID = "0000-0002-7768-4856")),
    person("Swarup Lab", role = "fnd")
    person("Swarup Lab", role = "fnd")
  )
  )
Description: hdWGCNA is an R package for performing weighted gene co-expression network analysis in high dimensional data such as single-cell RNA-seq or spatial transcriptomics.
Description: hdWGCNA is an R package for performing weighted gene co-expression network analysis in high dimensional data such as single-cell RNA-seq or spatial transcriptomics.
License: MIT + file LICENSE
License: GNU GPLv3
Encoding: UTF-8
Encoding: UTF-8
LazyData: true
LazyData: true
Roxygen: list(markdown = TRUE)
Roxygen: list(markdown = TRUE)
+1 −1
Original line number Original line Diff line number Diff line
YEAR: 2022
YEAR: 2022
COPYRIGHT HOLDER: scWGCNA authors
COPYRIGHT HOLDER: hdWGCNA authors
+675 −21

File changed.

Preview size limit exceeded, changes collapsed.

+8 −1
Original line number Original line Diff line number Diff line
# hdWGCNA 0.2.00 (2022-09-23)
## Added
- `MetaspotsByGroups` to aggregate neighboring ST spots prior to network analysis.
- Tutorial for applying hdWGCNA to spatial transcriptomics datasets.

## Changes
- None

# hdWGCNA 0.1.2.0001 (2022-09-19)
# hdWGCNA 0.1.2.0001 (2022-09-19)
## Added
## Added
- None
- None
@@ -5,7 +13,6 @@
## Changes
## Changes
- networkType option in `TestSoftPowers`.
- networkType option in `TestSoftPowers`.



# hdWGCNA 0.1.2.0000 (2022-09-08)
# hdWGCNA 0.1.2.0000 (2022-09-08)
## Added
## Added
- Differential Module Eigengene (DME) tutorial
- Differential Module Eigengene (DME) tutorial

R/ConstructMetaspots.R

0 → 100644
+160 −0
Original line number Original line Diff line number Diff line

#' ConstructMetaspots
#'
#' Computes metaspots in a given Seurat object containing spatial transcriptomics data.
#' This function is called by MetaspotsByGroups and should NOT be run directly!
#' @param cur_seurat A Seurat object
#' @param mode "sum" or "average"
#' @param
#' @param
#' @keywords ST
#' @export
#' @examples
#'
ConstructMetaspots <- function(
  cur_seurat,
  mode = 'sum',
  assay = 'Spatial',
  slot = 'counts'
){
  # get expression matrix:
  X <- GetAssayData(cur_seurat, slot='counts')

  # boundaries
  row_range <- range(cur_seurat$row)
  col_range <- range(cur_seurat$col)

  # loop boundaries
  col_bounds <- col_range[1]:col_range[2]
  col_bounds <- col_bounds[which(1:length(col_bounds) %% 4 == 0)]

  # even or odd cols?
  if(all(col_bounds %% 2 == 0)){even = TRUE} else{even = FALSE}

  row_bounds <- row_range[1]:row_range[2]
  if(even){
    row_bounds <- row_bounds[row_bounds %% 2 == 0]
  } else{
    row_bounds <- row_bounds[row_bounds %% 2 != 0]
  }

  # note: even number rows go to even number cols etc
  coords <- cur_seurat@meta.data[,c('row', 'col')]

  # compute distances
  distances <- proxy::dist(coords, coords, method='euclidean')

  tmp <- distances[distances != 0]
  min1 <- min(tmp)

  bcs <- c()
  unique_cols <- unique(cur_seurat$col);
  unique_cols <- unique_cols[order(unique_cols)]
  combine_list <- list()

  tmp <- lapply(1:length(col_bounds), function(i){
    x = col_bounds[i]
    tmp <- lapply(1:length(row_bounds), function(j){
      y = row_bounds[j]
      cur_coords <- coords %>% subset(row == y & col == x)
      out <- c()
      if(nrow(cur_coords) > 0){

        cur_bc <- rownames(cur_coords)
        bcs <- c(bcs, cur_bc)

        # get cur distances:
        cur_dist <- distances[cur_bc,]
        cur_dist <- cur_dist[cur_dist != 0]
        cur_dist <- cur_dist[cur_dist == min1]

        # find neighbors that are close to this spot
        ix <- which(unique_cols == x)
        other_bcs <- coords %>% subset(col %in% unique_cols[c(ix-2, ix+2)] & row == y) %>% rownames
        cur_neighbors <- c(names(cur_dist), other_bcs)

        # update list of barcodes:
        combine_list[[cur_bc]] <- cur_neighbors
        if(length(cur_neighbors) < 2){
          return(c())
        }

        # aggregate expression profile for these spots:
        cur_X <- X[,c(cur_bc, cur_neighbors)]
        cur_X <- Matrix::rowSums(cur_X)

        if(mode == 'average'){
          cur_X <- cur_X / (length(cur_neighbors) + 1)
        }

        out <- cur_X

      } else{
        return()
      }
      list(cur_bc, cur_neighbors, out)

    })

    # get bc-neighbor results
    bcs <- unlist(lapply(1:length(tmp), function(k){tmp[[k]][[1]]}))
    cur_neighbors <- lapply(1:length(tmp), function(k){tmp[[k]][[2]]})
    cur_neighbors[sapply(cur_neighbors, is.null)] <- NULL
    names(cur_neighbors) <- bcs

    # combine expression results
    cur_X <- do.call(rbind, lapply(1:length(tmp), function(k){tmp[[k]][[3]]}))

    list(bcs, cur_neighbors, cur_X)

  })

  # get bc-neighbor results
  bcs <- unlist(lapply(1:length(tmp), function(k){tmp[[k]][[1]]}))

  cur_neighbors <- list()
  for(k in 1:length(tmp)){
    cur_neighbors <- c(cur_neighbors, tmp[[k]][[2]])
  }
  names(cur_neighbors) <- bcs

  # combine expression results
  agg_X <- do.call(rbind, lapply(1:length(tmp), function(k){tmp[[k]][[3]]}))

  # transpose expression matrix:
  agg_X <- t(agg_X)
  colnames(agg_X) <- bcs

  # get metadata:
  cur_meta <- cur_seurat@meta.data[bcs,]

  # make metaspot object:
  metaspot_obj <- CreateSeuratObject(
    counts = agg_X,
    meta = cur_meta,
    assay = assay
  )
  if(slot == 'scale.data'){
    metaspot_obj <- SeuratObject::SetAssayData(
      metaspot_obj,
      slot=slot,
      assay=assay,
      new.data=as.matrix(agg_X)
    )
  }

  # add neighbors:
  metaspot_obj@misc$spot_neighbors <- cur_neighbors

  # compute sparsity:
  agg_X[agg_X > 0] <- 1
  X[X > 0] <- 1
  density_agg <- sum(Matrix::colSums(agg_X) / (nrow(agg_X)*ncol(agg_X)))
  density_orig <- sum(Matrix::colSums(X) / (nrow(X)*ncol(X)))

  metaspot_obj@misc$density_agg <- density_agg
  metaspot_obj@misc$density_orig <- density_orig

  metaspot_obj

}
Loading