Created
December 30, 2021 23:05
-
-
Save antagomir/d0eff35812b4dce44bf2521ab56e761d to your computer and use it in GitHub Desktop.
Visualize diversity for paired samples in miaverse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(mia) | |
library(dplyr) | |
library(tidyr) | |
# Get example data | |
library(microbiomeDataSets) | |
# tse <- LahtiMData() | |
x <- tse | |
# Agglomerate to Genus level | |
index <- "shannon" | |
x <- estimateDiversity(x, index=index, name="Signal") | |
# Speficy colData fields that determine the condition and sample pairs | |
pair_field <- "subject" | |
group_field <- "time" | |
# Rename the pair and group fields for routine handling | |
colData(x)$pair <- colData(x)[[pair_field]] | |
colData(x)$group <- as.factor(colData(x)[[group_field]]) | |
colData(x)$sample <- rownames(colData(x)) | |
# Indicate increase/decrease in signal | |
cdat <- colData(x) %>% as.data.frame() %>% | |
arrange(group) %>% | |
group_by(pair) %>% | |
mutate(change=diff(Signal)) %>% | |
DataFrame() | |
rownames(cdat) <- as.character(cdat$sample) | |
# Pick change to colData | |
# (note that row sorting had changed in cdat so we need to pick rows by name) | |
colData(x)$change <- cdat[rownames(colData(x)), "change"] | |
# Arrange to case-control pairs and rough check significance | |
tab <- colData(x) %>% as.data.frame() %>% | |
dplyr::select(pair, group, Signal) %>% | |
pivot_wider(names_from="group", values_from="Signal") | |
colnames(tab) <- c("pair", "control", "case") | |
tab <- tab %>% mutate(Difference=case-control) | |
# Table | |
knitr::kable(tab,digits=1) | |
library(scater) | |
p <- plotColData(x, "Signal", x="group") + | |
geom_line(data=as.data.frame(colData(x)), | |
aes(x=group, y=Signal, group=pair, color=change)) + | |
scale_color_gradient2(midpoint=0, | |
low="blue", mid="white", high="red", | |
space ="Lab") + | |
labs(title=index, | |
color="Change", | |
y="Value", | |
) + | |
geom_jitter(width=0) + | |
theme_bw(20) | |
print(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment