daisybio / daisybio/cyanus

Access medians per conditions

Open
#6 1 comment 0 reactions 0 assignees View on GitHub
good first issue
Dominant language
R
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Description

In case you want to access the exact median expressions per condition (or other stats), we have prepared a script for you. First, download your sce object using the button on the upper right. Then, modify your paths:
```{r}
path_to_sce <- "~/Downloads/sce.rds"
path_to_output <- "~/Downloads/medians_per_condition.csv"
condition <- "condition"
sample_id <- "sample_id"
```
Install and load packages
```{r}
install.packages("data.table")
install.packages("ggplot2")
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")

BiocManager::install("SingleCellExperiment")

library(SingleCellExperiment)
library(data.table)
library(ggplot2)
```
Read in your SCE object
```{r}
sce <- readRDS(path_to_sce)
```
Extract the expression data and add metadata information
```{r}
exprs_dt <- data.table(t(assays(sce)$exprs))
exprs_dt[, (condition) := colData(sce)[, condition]]
exprs_dt[, (sample_id) := colData(sce)[, sample_id]]
# reformat for easier calculation
exprs_dt <- melt(exprs_dt, id.vars = c(condition, sample_id), variable.name = "marker", value.name = "exprs")
```
Calculate medians per condition
```{r}
median_dt <- exprs_dt[, median(exprs), by=c(condition, "marker")]
colnames(median_dt) <- c(condition, "marker", "median")
print(median_dt)
# export
fwrite(median_dt, path_to_output)
```
Sanity check: reproduce boxplot from CYANUS
```{r}
# calculate medians per condition per sample id
median_per_sample_dt <- exprs_dt[, median(exprs), by=c(condition, sample_id, "marker")]
colnames(median_per_sample_dt) <- c(condition, sample_id, "marker", "median")

ggplot()+
geom_boxplot(data = median_per_sample_dt, aes(x = get(condition), y = median, color = get(condition)))+
geom_point(data = median_dt, aes(x = get(condition), y = median))+
facet_wrap(~marker, scales = "free_y")+
theme_bw()
```
**Attention**: the median per marker per condition is not the same as the median over the medians per marker per condition per sample id. This is why the black points do not match the boxplot lines exactly.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.