---
title: "Comparison of FC and CRP clustering"
author:
- name: "Nathan Constantine-Cooke"
url: https://scholar.google.com/citations?user=2emHWR0AAAAJ&hl=en&oi=ao
corresponding: true
affiliations:
- ref: CGEM
- ref: HGU
- name: "Karla Monterrubio-Gómez"
url: https://scholar.google.com/citations?user=YmyxSXAAAAAJ&hl=en
affiliations:
- ref: HGU
- ref: CGEM
- name: "Catalina A. Vallejos"
url: https://scholar.google.com/citations?user=lkdrwm0AAAAJ&hl=en&oi=ao
affiliations:
- ref: HGU
#comments:
# giscus:
# repo: quarto-dev/quarto-docs
---
```{R}
suppressPackageStartupMessages (library (libdr))
suppressPackageStartupMessages (library (ComplexHeatmap))
suppressPackageStartupMessages (library (tidyverse))
library (ggalluvial)
library (lcmm)
library (patchwork)
library (gameR)
library (pander)
library (libdr)
set.seed (123 )
prefix <- "/Volumes/igmm/cvallejo-predicct/libdr/"
########################
### Load FC models ###
########################
# set the number of groups
G.fcal <- numeric ()
models.fcal <- list ()
G.cands <- seq (2 , 10 )
for (G.cand in G.cands) {
file.name <- paste0 (prefix, "cache/fcal/ncs/fcal-" , G.cand, ".RDS" )
if (file.exists (file.name)) {
G.fcal <- c (G.fcal, G.cand)
models.fcal[[G.cand]] <- readRDS (file.name)
}
}
rm (G.cand, G.cands)
dict <- readRDS (paste0 (prefix, "processed/dict.RDS" ))
####################################################################
### Load CRP moving average (no autocorrelation structure) models ###
####################################################################
G.crp <- numeric ()
models.crp <- list ()
G.cands <- seq (2 , 10 )
for (G.cand in G.cands) {
file.name <- paste0 (prefix, "cache/crp-ma/crp-" , G.cand, ".RDS" )
if (file.exists (file.name)) {
G.crp <- c (G.crp, G.cand)
models.crp[[G.cand]] <- readRDS (file.name)
}
}
rm (G.cand, G.cands)
```
```{R}
#| column: page
#| fig-width: 14
#| fig-height: 10
fc.remapping <- function (x) {
plyr:: mapvalues (x,
from = seq (1 , 8 ),
to = c (7 , 6 , 4 , 8 , 1 , 5 , 2 , 3 ))
}
crp.remapping <- function (x) {
plyr:: mapvalues (x,
from = seq (1 , 8 ),
to = c (2 , 3 , 1 , 4 , 5 , 7 , 6 , 8 ))
}
col.vec <- c (
viridis:: viridis (8 ),
viridis:: inferno (8 )
)
fcal.pprob <- models.fcal[[8 ]]$ pprob
crp.pprob <- models.crp[[8 ]]$ pprob
fcal.ids <- fcal.pprob$ ids
crp.ids <- crp.pprob$ ids
# Only IDs in both FCAL and CRP models
ids.comb <- fcal.ids[fcal.ids %in% crp.ids]
ids.comb <- ids.comb[order (ids.comb)]
fc.classes <- data.frame (fcal.pprob[, c (1 , 2 )], type = "FC" )
fc.classes$ class <- fc.remapping (fc.classes$ class)
fc.classes$ class <- as.factor (paste0 ("FC" , fc.classes$ class))
crp.classes <- data.frame (crp.pprob[, c (1 , 2 )], type = "CRP" )
crp.classes$ class <- crp.remapping (crp.classes$ class)
crp.classes$ class <- as.factor (paste0 ("CRP" , crp.classes$ class))
fc.classes$ overlap <- ifelse (fc.classes$ ids %in% ids.comb, TRUE , FALSE )
crp.classes$ overlap <- ifelse (crp.classes$ ids %in% ids.comb, TRUE , FALSE )
p_overlap_fc <- fc.classes %>%
mutate (overlap = factor (
overlap, levels = c (TRUE , FALSE ),
labels = c ("In the overlap cohort" , "Not in the overlap cohort" ))) %>%
plotCat ("overlap" , class = "class" )
p_overlap_crp <- crp.classes %>%
mutate (overlap = factor (
overlap, levels = c (TRUE , FALSE ),
labels = c ("In the overlap cohort" , "Not in the overlap cohort" ))) %>%
plotCat ("overlap" , class = "class" )
p <- wrap_elements (p_overlap_fc) +
wrap_elements (p_overlap_crp) +
plot_annotation (tag_levels = "A" ) &
theme (plot.tag = element_text (size = 20 , face = "bold" ))
ggsave ("plots/overlap.pdf" , p, width = 14 , height = 10 )
ggsave ("plots/overlap.png" , p, width = 14 , height = 10 )
p
```
```{R}
#| column: page
#| fig-width: 12
#| fig-height: 11
aux <- table (crp.classes$ class, crp.classes$ overlap)
# Numbers for the text
aux[8 ,2 ] / (aux[8 ,1 ] + aux[8 ,2 ])
sum (aux[1 : 7 ,2 ]) / sum (aux[1 : 7 ,1 ] + aux[1 : 7 ,2 ])
classes <- rbind (fc.classes, crp.classes)
classes$ class <- as.factor (classes$ class)
classes$ type <- as.factor (classes$ type)
classes$ type <- relevel (classes$ type, "FC" )
classes <- merge (classes,
dict[, c ("ids" , "diagnosis" )],
by = "ids" , all.x = TRUE ,
all.y = FALSE )
p1 <- classes %>%
subset (overlap == TRUE ) %>%
ggplot (aes (
x = type,
stratum = class,
alluvium = ids,
fill = class,
label = class
)) +
geom_flow () +
geom_stratum (alpha = 1 ) +
geom_text (stat = "stratum" , size = 3 ) +
theme_minimal () +
theme (axis.line = element_line (colour = "gray" )) +
xlab ("Biomarker" ) +
scale_fill_manual (values = col.vec)
p2 <- classes %>%
subset (overlap == TRUE ) %>%
filter (diagnosis == "Crohn's Disease" ) %>%
ggplot (aes (
x = type,
stratum = class,
alluvium = ids,
fill = class,
label = class
)) +
geom_flow () +
geom_stratum (alpha = 1 ) +
geom_text (stat = "stratum" , size = 3 ) +
theme_minimal () +
theme (axis.line = element_line (colour = "gray" )) +
xlab ("Biomarker" ) +
scale_fill_manual (values = col.vec)
p3 <- classes %>%
subset (overlap == TRUE ) %>%
filter (diagnosis == "Ulcerative Colitis" ) %>%
ggplot (aes (
x = type,
stratum = class,
alluvium = ids,
fill = class,
label = class
)) +
geom_flow () +
geom_stratum (alpha = 1 ) +
geom_text (stat = "stratum" , size = 3 ) +
theme_minimal () +
theme (axis.line = element_line (colour = "gray" )) +
xlab ("Biomarker" ) +
scale_fill_manual (values = col.vec)
p <- p1 + (p2 / p3) +
plot_annotation (tag_levels = "A" ) +
plot_layout (guides = "collect" ) &
theme (legend.position = "none" ,
plot.tag = element_text (size = 20 , face = "bold" )) &
guides (fill = guide_legend (nrow = 1 ))
p
ggsave ("paper/big-comp.png" , p, width = 12 , height = 11 )
ggsave ("paper/big-comp.pdf" , p, width = 12 , height = 11 )
```
Calculate some numbers in the overlap
```{r}
tab1 <- classes.wide <- classes %>%
subset (overlap == TRUE ) %>%
pivot_wider (names_from = type, values_from = class)
tab2 <- classes.wide %>%
subset (FC == "FC1" ) %>%
count (CRP) %>%
mutate (percentage = (n/ sum (n) * 100 ))
tab3 <- classes.wide %>%
subset (CRP %in% c ("CRP1" , "CRP2" )) %>%
count (FC) %>%
mutate (percentage = (n/ sum (n) * 100 ))
tab4 <- classes.wide %>%
subset (CRP %in% c ("CRP8" )) %>%
count (FC) %>%
mutate (percentage = (n/ sum (n) * 100 ))
tab5 <- classes.wide %>%
subset (diagnosis == "Ulcerative Colitis" ) %>%
subset (FC %in% c ("FC7" )) %>%
count (CRP) %>%
mutate (percentage = (n/ sum (n) * 100 ))
```
```{R}
#| include: false
ids.shared <- intersect (models.fcal[[8 ]]$ pprob$ ids, models.crp[[8 ]]$ pprob$ ids)
fc.df <- subset (models.fcal[[8 ]]$ pprob, ids %in% ids.shared)[,c (1 , 2 )]
colnames (fc.df) <- c ("ids" , "FCcluster" )
crp.df <- subset (models.crp[[8 ]]$ pprob, ids %in% ids.shared)[,c (1 , 2 )]
colnames (crp.df) <- c ("ids" , "CRPcluster" )
mapping <- merge (fc.df, crp.df, by = "ids" )
tab <- with (mapping, table (FCcluster, CRPcluster))
rowSums (tab)
colSums (tab)
```
Out of subjects paired in FC, how many are also paired in CRP?
```{R}
paired <- 0
doublePaired <- 0
for (i in fc.df$ ids) {
for (j in fc.df$ ids) {
if (i != j) {
if (subset (fc.df, ids == i)$ FCcluster == subset (fc.df, ids == j)$ FCcluster){
paired <- paired + 1
if (subset (crp.df, ids == i)$ CRPcluster == subset (crp.df, ids == j)$ CRPcluster) {
doublePaired <- doublePaired + 1
}
}
}
}
}
doublePaired / paired
```
Out of subjects paired in CRP, how many are also paired in FC?
```{R}
paired <- 0
doublePaired <- 0
for (i in crp.df$ ids) {
for (j in crp.df$ ids) {
if (i != j) {
if (subset (crp.df, ids == i)$ CRPcluster == subset (crp.df, ids == j)$ CRPcluster){
paired <- paired + 1
if (subset (fc.df, ids == i)$ FCcluster == subset (fc.df, ids == j)$ FCcluster) {
doublePaired <- doublePaired + 1
}
}
}
}
}
doublePaired / paired
```
## Reuse {.appendix}
Licensed by
<a href="https://creativecommons.org/licenses/by/4.0/">CC BY</a>
unless otherwise stated.
## {.appendix}
<div class = "center">
<img class = "center" src="../../images/MRC_HGU_Edinburgh RGB.png" alt="MRC Human Genetics Unit logo" height = 50px>
<img src="../../images/cgem-logo.png" alt="Centre for Genomic & Experimental Medicine logo" height = 50px>
</div>
---
## Session info {.appendix}
```{R Session info}
pander(sessionInfo())
```