Commit 18f3bf32 authored by smorabit's avatar smorabit
Browse files

fix ResetModuleNames

parent 0f4222a6
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
@@ -1208,21 +1208,6 @@ ResetModuleNames <- function(
    seurat_obj <- SetEnrichrTable(seurat_obj, enrich_table, wgcna_name)
  }

  # update ROC info:
  # THIS DOES NOT UPDATE THE ROC OBJECTS THEMSELVES!!!
  roc_data <- GetROCData(seurat_obj, wgcna_name)
  if(!is.null(roc_data)){
    roc_data$roc$module <- factor(
      new_mod_df[match(roc_data$roc$module, new_mod_df$old),'new'],
      levels = as.character(new_mod_df$new)
    )
    roc_data$conf$module <- factor(
      new_mod_df[match(roc_data$conf$module, new_mod_df$old),'new'],
      levels = as.character(new_mod_df$new)
    )
    seurat_obj <- SetROCData(seurat_obj, roc_data, wgcna_name)
  }

  # update motif overlap
  overlap_df <- GetMotifOverlap(seurat_obj, wgcna_name)
  if(!is.null(overlap_df)){
+0 −76
Original line number Diff line number Diff line
@@ -1424,82 +1424,6 @@ OverlapBarPlot <- function(

}


#' ROCCurves
#'
#' Makes barplots from Enrichr data
#'
#' @param seurat_obj A Seurat object
#' @param dbs List of EnrichR databases
#' @param max_genes Max number of genes to include per module, ranked by kME.
#' @param wgcna_name The name of the hdWGCNA experiment in the seurat_obj@misc slot
#' @keywords scRNA-seq
#' @export
#' @examples
#' ROCCurves
ROCCurves <- function(
  seurat_obj,
  roc_df=NULL,
  conf_df=NULL,
  wgcna_name=NULL
){

  if(is.null(wgcna_name)){wgcna_name <- seurat_obj@misc$active_wgcna}

  # get Modules
  modules <- GetModules(seurat_obj)
  mods <- levels(modules$module)
  mods <- mods[mods != 'grey']

  # get module colors:
  mod_colors <- modules %>% subset(module %in% mods) %>%
    select(c(module, color)) %>%
    distinct %>%
    arrange(module) %>% .$color

  # get the ROC info from seurat obj:
  if(is.null(roc_df) | is.null(conf_df)){
    roc_df <- GetROCData(seurat_obj, wgcna_name)$roc
    conf_df <- GetROCData(seurat_obj, wgcna_name)$conf
  }

  # plot the ROC curve
  roc_df <- roc_df %>% group_by(module) %>% arrange(sensitivity)
  conf_df <- conf_df %>% group_by(module) %>% arrange(sensitivity)
  auc_df <- distinct(roc_df[,c('module', 'auc')])

  # set factor levels for modules:
  roc_df$module <- factor(as.character(roc_df$module), levels=mods)
  conf_df$module <- factor(as.character(conf_df$module), levels=mods)
  auc_df$module <- factor(as.character(auc_df$module), levels=mods)

  p <- roc_df %>% ggplot(
    aes(x=specificity, y=sensitivity, color=module, fill=module),
  ) +
    geom_line() +
    geom_ribbon(
      data=conf_df,
      aes(x = sensitivity, ymin=lo, ymax=hi, fill=module),
      inherit.aes=FALSE, alpha=0.4
    ) +
    scale_color_manual(values = unlist(mod_colors)) +
    scale_fill_manual(values = unlist(mod_colors)) +
    scale_x_continuous(breaks = c(0, 0.5, 1), labels=c("0", "0.5", "1")) +
    scale_y_continuous(breaks = c(0, 0.5, 1), labels=c("0", "0.5", "1")) +
    xlab("1 - Specificity (FPR)") + ylab("Sensitivity (TPR)") +
    geom_text(
      data = auc_df,
      aes(color=module),
      x=0.75, y=0.1, label=paste0("AUC: ", format(auc_df$auc, digits=2)),
      inherit.aes=FALSE, size=4, color='black'
    )

  p

}



#' Displays the top n TFs in a set of modules as a bar plot
#'
#' @param seurat_obj A Seurat object
+0 −96
Original line number Diff line number Diff line
@@ -185,99 +185,3 @@ wrap_plots(plot_list, ncol=6)
```

<img src="figures/projection/module_preservation_all.png" width="700" height="700">

<!--
## Binary classification analysis

Explain the binary classification analysis.

### Transfer cell states from reference to query

```{r eval=FALSE}

# add SCANVI predicted labels from Zhou et al:
predicted_labels <- read.csv(paste0('../data/Swarup_2021_predicted_obs.csv'))
predicted_labels <- predicted_labels %>% subset(X %in% colnames(seurat_query))
seurat_query$predictions <- predicted_labels$predictions

# setup cell cluster labels for ROC computation
seurat_ref$roc_group <- factor(
  as.character(seurat_ref$annotation),
  levels = levels(seurat_ref$annotation)
)
seurat_query$roc_group <- factor(
  as.character(seurat_query$predictions),
  levels = levels(seurat_ref$roc_group)
)

```

Now we visualize the transferred cell states on the UMAP:

<details> <summary> Code </summary>

```{r eval=FALSE}

p1 <- DimPlot(seurat_ref, group.by='roc_group', label=TRUE, repel=TRUE) +
   umap_theme() +
   ggtitle('Zhou') +
   NoLegend()

p2 <- DimPlot(seurat_query, group.by='roc_group', label=TRUE, repel=TRUE) +
   umap_theme() +
   ggtitle('Morabito & Miyoshi') +
   NoLegend()

p1 | p2

```

</details>

<img src="figures/projection/umap_transfer_labels.png" width="600" height="600">


### Compute ROC

```{r eval=FALSE}

# compute ROC
seurat_ref <- ComputeROC(
  seurat_ref,
  seurat_test = seurat_query,
  group.by = 'roc_group'
)

# head the ROC table:
roc_data <- GetROCData(seurat_obj)
head(roc_data$roc)

```


<details> <summary> Output </summary>
```
specificity sensitivity module   color       auc
1   1.0000000           1 INH-M1 darkred 0.9948718
2   0.9333333           1 INH-M1 darkred 0.9948718
3   0.8666667           1 INH-M1 darkred 0.9948718
4   0.8000000           1 INH-M1 darkred 0.9948718
5   0.7333333           1 INH-M1 darkred 0.9948718

6   0.6666667           1 INH-M1 darkred 0.9948718
```
</details>

### Plot ROC curves

```{r eval=FALSE}

# plot the ROC Curves
p <- ROCCurves(seurat_obj=seurat_obj)

# plot the ROC curves faceted by module
p + facet_wrap(~module, ncol=6) + NoLegend()

```

<img src="figures/projection/Zhou_ROC.png" width="700" height="700"> -->