Commit 27b17bde authored by smorabit's avatar smorabit
Browse files

small change to module connectivity to allow the user to select the slot & assay

parent b332e406
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -881,6 +881,9 @@ AvgModuleExpr <- function(seurat_obj, n_genes = 25, wgcna_name=NULL, ...){
ModuleConnectivity <- function(
  seurat_obj,
  harmonized=TRUE,
  assay = NULL,
  slot = NULL,
  use_metacells = FALSE,
  wgcna_name = NULL,
  ...
){
@@ -888,17 +891,23 @@ ModuleConnectivity <- function(
  # set as active assay if wgcna_name is not given
  if(is.null(wgcna_name)){wgcna_name <- seurat_obj@misc$active_wgcna}

  if(use_metacells){
    warning('use_metacells option is not implemented yet.')
  }

  # get module df, wgcna genes, and wgcna params:
  modules <- GetModules(seurat_obj, wgcna_name)
  MEs <- GetMEs(seurat_obj, harmonized, wgcna_name)
  genes_use <- GetWGCNAGenes(seurat_obj, wgcna_name)
  params <- GetWGCNAParams(seurat_obj, wgcna_name)

  # datExpr for full expression dataset
  if(is.null(assay)){assay <- params$metacell_assay}
  if(is.null(slot)){slot <- params$metacell_slot}

  datExpr <- t(GetAssayData(
    seurat_obj,
    assay=params$metacell_assay,
    slot=params$metacell_slot
    assay=assay,
    slot=slot
  ))[,genes_use]

  print('datExpr')
@@ -915,6 +924,7 @@ ModuleConnectivity <- function(
  )

  # add module color to the kMEs table
  modules <- modules[,1:3]
  kMEs <- cbind(modules, kMEs)
  colnames(kMEs) <- c(colnames(modules), paste0("kME_", colnames(MEs)))

+1 −1
Original line number Diff line number Diff line
@@ -403,7 +403,7 @@ seurat_obj <- ModuleConnectivity(seurat_obj)

For convenience, we re-name the scWGCNA modules to indicate that they are from
the inhibitory neuron group. More information about renaming modules can be
found in the [module customization tutorial](articles/customization.html).
found in the [module customization tutorial](customization.html).

```{r eval=FALSE}

+10 −44
Original line number Diff line number Diff line
@@ -111,6 +111,9 @@ seurat_query <- ProjectModules(
As you can see it only takes running one function to project co-expression modules
from one dataset into another using scWGCNA. Optionally, we can also compute the
hub gene expression scores and the intramodular connectivity for the projected modules.
Note that if we do not run the `ModuleConnectivity` function on the query dataset,
the *kME* values in the module assignment table `GetModules(seurat_query)` are the
*kME* values from the reference dataset.


<details> <summary> Code </summary>
@@ -123,54 +126,11 @@ seurat_query <- ModuleExprScore(
)

# compute intramodular connectivity
seurat_query <- ModuleConnectivity(seurat_query)
seurat_query <- ModuleConnectivity(seurat_query, assay="RNA", slot="data")

```
</details>

```{r eval=FALSE, echo=FALSE}

################################################################################
# TODO: update ModuleConnectivity function to allow the user to
# select the assay rather than getting straight from the params.
# also allow to do it on the metacells object.
################################################################################

seurat_query <- readRDS(file=paste0(data_dir, 'Swarup_2021.rds'))
seurat_query <- subset(seurat_query, Diagnosis == 'Control')

# Project modules from query to reference dataset
seurat_query <- ProjectModules(
  seurat_obj = seurat_query,
  seurat_ref = seurat_ref,
  scale_genes = TRUE,
  # vars.to.regress = c(), # optionally regress covariates when running ScaleData
  wgcna_name_proj="Zhou_projected", # name of the new scWGCNA experiment in the query dataset
  wgcna_name = "train" # name of the scWGCNA experiment in the ref dataset
)

modules <- GetModules(seurat_query)
MEs <- GetMEs(seurat_query)
genes_use <- GetWGCNAGenes(seurat_query)
params <- GetWGCNAParams(seurat_query)

# kME values are the same because the modules table is just the same
datExpr <- t(GetAssayData(
  seurat_query,
  assay="RNA",
  slot="data"
))[,genes_use]
dim(datExpr)

kMEs <- WGCNA::signedKME(
  datExpr,
  MEs,
  outputColumnName = "kME",
  corFnc = "bicor"
)

```

We can extract the projected module eigengenes using the `GetMEs` function.

```{r eval=FALSE}
@@ -179,4 +139,10 @@ projected_hMEs <- GetModules(seurat_query)

```

The projected modules can be used in most of the downstream scWGCNA analysis
tasks and visualization functions, such as [module trait correlation](module_trait_correlation.html),
or.

## Visualization

Compare the hub gene UMAP between the projected and the query (use gganimate!!)