Commit d3bfbd68 authored by smorabit's avatar smorabit
Browse files

bugfixes

parent e926942b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
Package: hdWGCNA
Title: hdWGCNA
Version: 0.1.1.9008
Version: 0.1.1.9009
Authors@R: c(
    person("Sam", "Morabito", , "smorabit@uci.edu", role = c("aut", "cre"),
           comment = c(ORCID = "0000-0002-7768-4856")),
+129 −0
Original line number Diff line number Diff line

```{r eval=FALSE}
library(Seurat)
library(tidyverse)
library(cowplot)
library(patchwork)
library(WGCNA)

library(Matrix)
library(viridis)
library(harmony)
library(RColorBrewer)
library(ggpubr)
library(tictoc)
library(RColorBrewer)
library(Hmisc)
library(corrplot)
library(enrichR)
library(GeneOverlap)
library(grid)
library(gridExtra)
library(igraph)
library(ggrepel)
#library(hdWGCNA)
enableWGCNAThreads(nThreads = 8)
theme_set(theme_cowplot())
set.seed(12345)


# spatial plotting functions
source('/dfs7/swaruplab/smorabit/analysis/scWGCNA/bin/spatial_functions.R')
source("/pub/smorabit/hdWGCNA/bin/spatial_functions.R")

#devtools::install_github('smorabit/hdWGCNA', ref='dev')
library(hdWGCNA)

setwd('/dfs7/swaruplab/smorabit/analysis/scWGCNA/')

# output directories
data_dir <- "data/"
fig_dir <- 'figures/'



# re-load scWGCNA dataset:
load(file='data/Zhou_color_scheme.rda')


# reload process ed with all cell types
# seurat_obj <-  readRDS(file=paste0(data_dir, 'Zhou_scWGCNA_all_celltypes.rds'))


load('/dfs7/swaruplab/smorabit/analysis/AD_NucSeq_2019/batch_correction/liger/update/celltype-analysis/data/color_scheme.rda')
cp <- unlist(color_scheme_snRNA_celltype)
cp[names(cp) == 'EX'] <- 'turquoise'


setwd('/dfs7/swaruplab/smorabit/analysis/scWGCNA/project_modules/')

seurat_zhou <- readRDS(file= '/dfs7/swaruplab/smorabit/analysis/scWGCNA/brain_iterative/data/Zhou_scWGCNA_all_celltypes.rds')


```

Project Bulk RNA-seq modules into single-cell dataset

```{r eval=FALSE}

# load Morabito & miyoshi et al dataset
seurat_obj <- readRDS("/dfs7/swaruplab/smorabit/analysis/scWGCNA/data/Swarup_2021.rds")

# load consensus modules from
consensus_modules <- read.csv("/dfs7/swaruplab/smorabit/analysis/scWGCNA/data/hmg_cWGCNA_modules.csv")

consensus_modules <- consensus_modules %>%
  dplyr::select(c(-Ensembl.Gene.ID)) %>%
  dplyr::rename(c(gene_name = GeneSymbol, module = ModuleLabels, color = Initially.Assigned.Module.Color)) %>%
   dplyr::select(c(gene_name, module, color))

consensus_modules <- subset(consensus_modules, gene_name %in% rownames(seurat_obj))

# remove duplicate gene names
consensus_modules <- consensus_modules[match(unique(consensus_modules$gene_name), consensus_modules$gene_name),]

seurat_obj <- FindVariableFeatures(seurat_obj, nfeatures=2000)
seurat_obj <- ScaleData(seurat_obj)

seurat_obj <- ProjectModules(
  seurat_obj,
  modules = consensus_modules,
  group.by.vars = "Batch",
  seurat_ref = NULL,
  wgcna_name = "None",
  wgcna_name_proj = 'HMG_2020'
)

saveRDS(seurat_obj, file = 'data/Swarup_2021_hmg_projected.rds')


plot_list <- ModuleFeaturePlot(seurat_obj, order='shuffle', raster=TRUE, raster_dpi=400, alpha=1, restrict_range=FALSE, raster_scale=0.25)

pdf("figures/featureplot_hmg_projected.pdf",height=9, width=16)
wrap_plots(plot_list, ncol=6)
dev.off()


# get hMEs from seurat object
MEs <- GetMEs(seurat_obj, harmonized=TRUE)
mods <- colnames(MEs); mods <- mods[mods != 'grey']

# add hMEs to Seurat meta-data:
seurat_obj@meta.data <- cbind(seurat_obj@meta.data, MEs)

# plot with Seurat's DotPlot function
p <- DotPlot(seurat_obj, features=mods, group.by = 'annotation', dot.min=0.25)

# flip the x/y axes, rotate the axis labels, and change color scheme:
p <- p +
  coord_flip() +
  RotatedAxis() +
  scale_color_gradient2(high='red', mid='grey95', low='blue')

# plot output
pdf("figures/dotplot_hmg_projected.pdf",height=6, width=12)
p
dev.off()


```
+8 −0
Original line number Diff line number Diff line
# hdWGCNA 0.1.1.9009 (2022-07-23)
## Added
- None

## Changes
- Bugfix in `ProjectModules`.
- Bugfix in `MetacellsByGroups`

# hdWGCNA 0.1.1.9008 (2022-07-21)
## Added
- New tutorial for consensus co-expression network analysis.
+13 −3
Original line number Diff line number Diff line
@@ -648,6 +648,8 @@ ModuleEigengenes <- function(
  wgcna_name=NULL, ...
){

  print('in here')

  # set as active assay if wgcna_name is not given
  if(is.null(wgcna_name)){wgcna_name <- seurat_obj@misc$active_wgcna}

@@ -1112,7 +1114,8 @@ OverlapModulesDEGs <- function(
#' @examples
#' ProjectModules
ProjectModules <- function(
  seurat_obj, seurat_ref,
  seurat_obj,
  seurat_ref,
  modules=NULL,
  group.by.vars=NULL,
  gene_mapping=NULL, # table mapping genes from species 1 to species 2
@@ -1135,13 +1138,16 @@ ProjectModules <- function(
    }
  }

  # cast "modules" to a factor
  if(!is.factor(modules$module)){
    modules$module <- as.factor(modules$module)
  }

  # are we mapping gene names?
  if(!is.null(gene_mapping)){
    modules <- TransferModuleGenome(modules, gene_mapping, genome1_col, genome2_col)
  }

  print(head(modules))

  # get genes that overlap between WGCNA genes & seurat_obj genes:
  gene_names <- modules$gene_name
  genes_use <- intersect(gene_names, rownames(seurat_obj))
@@ -1152,6 +1158,8 @@ ProjectModules <- function(

  # subset modules by genes in this seurat object:
  modules <- modules %>% subset(gene_name %in% genes_use)
  print(head(modules))


  # setup new seurat obj for wgcna:
  if(!(wgcna_name_proj %in% names(seurat_obj@misc))){
@@ -1163,6 +1171,7 @@ ProjectModules <- function(
  }

  # project modules:
  print('project modules')
  seurat_obj <- ModuleEigengenes(
    seurat_obj,
    group.by.vars=group.by.vars,
@@ -1172,6 +1181,7 @@ ProjectModules <- function(
    wgcna_name = wgcna_name_proj,
    ...
  )
  print('done')

  seurat_obj

+16 −4
Original line number Diff line number Diff line
@@ -282,8 +282,10 @@ MetacellsByGroups <- function(
  groupings <- groupings[order(groupings)]

  # remove groups that are too small:
  group_counts <- table(seurat_obj$metacell_grouping) >= min_cells
  group_counts <- table(seurat_obj$metacell_grouping) < min_cells
  if(any(group_counts)){
    warning(paste0("Removing the following groups that did not meet min_cells: ", paste(names(group_counts)[group_counts], collapse=', ')))
  }
  groupings <- groupings[table(seurat_obj$metacell_grouping) >= min_cells]

  if(length(groupings) == 0 ){
@@ -315,21 +317,31 @@ MetacellsByGroups <- function(
  )
  names(metacell_list) <- groupings

  print('done making metacells')

  # remove NULL
  remove <- which(sapply(metacell_list, is.null))
  if(length(remove) > 1){
    metacell_list <- metacell_list[-remove]
  }

  # get the run stats:
  run_stats <- as.data.frame(do.call(rbind, lapply(metacell_list, function(x){x@misc$run_stats})))
  rownames(run_stats) <- 1:nrow(run_stats)
  for(i in 1:length(group.by)){
    run_stats[[group.by[i]]] <- do.call(rbind, strsplit(run_stats$name, '#'))[,i]
    run_stats[[group.by[i]]] <- do.call(rbind, strsplit(as.character(run_stats$name), '#'))[,i]
  }

  # combine metacell objects
  print(length(metacell_list))
  if(length(metacell_list) > 1){
    metacell_obj <- merge(metacell_list[[1]], metacell_list[2:length(metacell_list)])
  } else{
    metacell_obj <- metacell_list[[1]]
  }

  print('metacell shape')


  # set idents for metacell object:
  Idents(metacell_obj) <- metacell_obj@meta.data[[ident.group]]
Loading