Commit e926942b authored by smorabit's avatar smorabit
Browse files

added consensus tutorial

parent 5c35cc88
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
Package: hdWGCNA
Title: hdWGCNA
Version: 0.1.1.9006
Version: 0.1.1.9008
Authors@R: c(
    person("Sam", "Morabito", , "smorabit@uci.edu", role = c("aut", "cre"),
           comment = c(ORCID = "0000-0002-7768-4856")),
+9 −0
Original line number Diff line number Diff line
# hdWGCNA 0.1.1.9008 (2022-07-21)
## Added
- New tutorial for consensus co-expression network analysis.

## Changes
- `ModuleNetworkPlot` and `RunModuleUMAP` now checks if `ModuleConnectivity` has been computed in order to throw a more informative error.
- `GetTOM` checks if the TOM file exists in order to throw a more informative error.


# hdWGCNA 0.1.1.9007 (2022-07-20)
## Added
- None
+40 −19
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ SetupForWGCNA <- function(
TestSoftPowers <- function(
  seurat_obj,
  powers=c(seq(1,10,by=1), seq(12,30, by=2)),
  setDatExpr = TRUE,
  setDatExpr = FALSE,
  use_metacells = TRUE,
  group.by=NULL, group_name=NULL
){
@@ -221,7 +221,7 @@ TestSoftPowers <- function(
TestSoftPowersConsensus <- function(
  seurat_obj,
  powers=c(seq(1,10,by=1), seq(12,30, by=2)),
  setDatExpr = TRUE,
  setDatExpr = FALSE,
  use_metacells = TRUE,
  group.by=NULL, group_name=NULL,
  multi.group.by = NULL,
@@ -291,7 +291,7 @@ ConstructNetwork <- function(
  seurat_obj, soft_power=NULL, min_power=3,
  tom_outdir="TOM",
  use_metacells=TRUE,
  setDatExpr=TRUE, group.by=NULL,
  setDatExpr=FALSE, group.by=NULL,
  group_name=NULL,
  tom_name = NULL,
  consensus = FALSE,
@@ -309,6 +309,22 @@ ConstructNetwork <- function(

  wgcna_name <- seurat_obj@misc$active_wgcna


  # suffix for the tom
  if(is.null(tom_name)){
    tom_name <- gsub(' ', '_', group_name)
  }

  # tom file name
  renamed <- paste0(tom_outdir, '/', tom_name, '_TOM.rda')
  if(file.exists(renamed)){
    if(overwrite_tom){
      warning(paste0('Overwriting TOM ', renamed))
    } else{
      stop(paste0("TOM ", renamed, " already exists. Set overwrite_tom = TRUE or change tom_name to proceed."))
    }
  }

  # constructing network on multiple datasets (consensus WGCNA)
  if(consensus){

@@ -349,21 +365,21 @@ ConstructNetwork <- function(
    if(is.null(group_name)){
      group_name <- 'all'
    }

    # suffix for the tom
    if(is.null(tom_name)){
      tom_name <- gsub(' ', '_', group_name)
    }

    # tom file name
    renamed <- paste0(tom_outdir, '/', tom_name, '_TOM.rda')
    if(file.exists(renamed)){
      if(overwrite_tom){
        warning(paste0('Overwriting TOM ', renamed))
      } else{
        stop(paste0("TOM ", renamed, " already exists. Set overwrite_tom = TRUE or change tom_name to proceed."))
      }
    }
    #
    # # suffix for the tom
    # if(is.null(tom_name)){
    #   tom_name <- gsub(' ', '_', group_name)
    # }
    #
    # # tom file name
    # renamed <- paste0(tom_outdir, '/', tom_name, '_TOM.rda')
    # if(file.exists(renamed)){
    #   if(overwrite_tom){
    #     warning(paste0('Overwriting TOM ', renamed))
    #   } else{
    #     stop(paste0("TOM ", renamed, " already exists. Set overwrite_tom = TRUE or change tom_name to proceed."))
    #   }
    # }

    nSets = 1
    setLabels = gsub(' ', '_', group_name)
@@ -898,7 +914,7 @@ ModuleConnectivity <- function(
  #modules <- subset(modules, module != 'grey')
  #MEs <- MEs[,colnames(MEs) != 'grey']

  if(is.null(assay)){assay <- seurat_obj@active.assay}
  if(is.null(assay)){assay <- DefaultAssay(seurat_obj)}

  if(!is.null(group.by)){
    cells.use <- seurat_obj@meta.data %>% subset(get(group.by) %in% group_name) %>% rownames
@@ -1673,6 +1689,11 @@ RunModuleUMAP <- function(
  modules <- GetModules(seurat_obj, wgcna_name)
  mods <- levels(modules$module)

  # check if we have eigengene-based connectivities:
  if(!all(paste0('kME_', as.character(mods)) %in% colnames(modules))){
    stop('Eigengene-based connectivity (kME) not found. Did you run ModuleEigengenes and ModuleConnectivity?')
  }

  if(exclude_grey){
    mods <- mods[mods != 'grey']
    modules <- subset(modules, module != 'grey')
+32 −8
Original line number Diff line number Diff line
@@ -197,11 +197,6 @@ SetDatExpr <- function(
  genes_use <- GetWGCNAGenes(seurat_obj, wgcna_name)
  modules <- GetModules(seurat_obj, wgcna_name)

  if(is.null(assay)){
    assay <- params$metacell_assay
    warning(paste0('assay not specified, trying to use assay ', assay))
  }

  # use metacells or whole seurat object?
  if(use_metacells){
    s_obj <- GetMetacellObject(seurat_obj, wgcna_name)
@@ -209,6 +204,12 @@ SetDatExpr <- function(
    s_obj <- seurat_obj
  }


  if(is.null(assay)){
    assay <- DefaultAssay(s_obj)
    warning(paste0('assay not specified, trying to use assay ', assay))
  }

  # check the assay:
  if(!(assay %in% names(s_obj@assays))){
    stop("Assay not found. Check names(seurat_obj@assays) or names(GetMetacellObject(seurat_obj)@assays)")
@@ -257,7 +258,6 @@ SetDatExpr <- function(
    datExpr <- datExpr[,gene_list]
  }


  if(return_seurat){

    # update the WGCNA gene list:
@@ -301,7 +301,7 @@ GetDatExpr <- function(seurat_obj, wgcna_name=NULL){
#' @param use_metacells A logical determining if we use the metacells (TRUE) or the full expression matrix (FALSE)
#' @param group.by A string containing the name of a column in the Seurat object with cell groups (clusters, cell types, etc). If NULL (default), hdWGCNA uses the Seurat Idents as the group.
#' @param multi.group.by A string containing the name of a column in the Seurat object with groups for consensus WGCNA (dataset, sample, condition, etc)
#' @param multi_groups A character vecrtor containing the names of
#' @param multi_groups A character vecrtor containing the names of groups to select
#' @param wgcna_name A string containing the name of the WGCNA slot in seurat_obj@misc. Default = NULL which retrieves the currently active WGCNA data
#' @keywords scRNA-seq
#' @export
@@ -315,6 +315,8 @@ SetMultiExpr <- function(
  multi.group.by = NULL,
  multi_groups = NULL,
  wgcna_name=NULL,
  assay=NULL,
  slot='data',
  ...
){

@@ -322,8 +324,22 @@ SetMultiExpr <- function(
  if(is.null(wgcna_name)){wgcna_name <- seurat_obj@misc$active_wgcna}

  # get the WGCNA genes:
  params <- GetWGCNAParams(seurat_obj, wgcna_name)
  gene_names <- GetWGCNAGenes(seurat_obj, wgcna_name)

  # use metacells or whole seurat object?
  if(use_metacells){
    s_obj <- GetMetacellObject(seurat_obj, wgcna_name)
  } else{
    s_obj <- seurat_obj
  }

  # get assay
  if(is.null(assay)){
    assay <- DefaultAssay(s_obj)
    warning(paste0('assay not specified, trying to use assay ', assay))
  }

  # get the different groups present if not specified by the user:
  if(is.null(multi_groups)){
    multi_groups <- unique(seurat_obj@meta.data[[multi.group.by]])
@@ -343,7 +359,10 @@ SetMultiExpr <- function(
      multi.group.by = multi.group.by,
      multi_group_name = x,
      return_seurat = FALSE,
      wgcna_name = wgcna_name
      use_metacells = use_metacells,
      wgcna_name = wgcna_name,
      assay = assay,
      slot = slot
    ) %>% as.matrix
  })
  datExpr_list
@@ -804,6 +823,11 @@ GetTOM <- function(seurat_obj, wgcna_name=NULL){

  # load TOM
  tom_files <- GetNetworkData(seurat_obj, wgcna_name)$TOMFiles

  if(!file.exists(tom_files[[1]])){
    stop(paste0("TOM file ", tom_files[[1]], ' not found. Please update path to TOM file.'))
  }

  load(tom_files[[1]])

  TOM <- as.matrix(consTomDS)
+10 −1
Original line number Diff line number Diff line
@@ -572,7 +572,9 @@ ModuleFeaturePlot<- function(
    }

    # aspect ratio:
    if(is.numeric(plot_ratio)){
      p <- p + coord_fixed(ratio = plot_ratio)
    }

    # UCell?
    if(!ucell){
@@ -834,12 +836,19 @@ ModuleNetworkPlot <- function(
  MEs <- GetMEs(seurat_obj, wgcna_name)
  modules <- GetModules(seurat_obj, wgcna_name)



  # using all modules?
  if(mods == 'all'){
    mods <- levels(modules$module)
    mods <- mods[mods != 'grey']
  }

  # check if we have eigengene-based connectivities:
  if(!all(paste0('kME_', as.character(mods)) %in% colnames(modules))){
    stop('Eigengene-based connectivity (kME) not found. Did you run ModuleEigengenes and ModuleConnectivity?')
  }

  # create output folder
  if(!dir.exists(outdir)){dir.create(outdir)}

Loading