-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixomics_ilesplmicro.Rmd
215 lines (174 loc) · 6.91 KB
/
mixomics_ilesplmicro.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
---
title: "mixomics_ilesplmicro"
author: "Carolina Monzo"
date: "9/2/2021"
output: html_document
---
```{r}
suppressPackageStartupMessages(library(mixOmics))
suppressPackageStartupMessages(library(dplyr))
theme_set(theme_bw())
setwd("~/workspace/CM_IgSeq_tmp/analysis/plots/diablo_ALDR_ILESPLmi/")
path <- "~/workspace/CM_IgSeq_tmp/analysis/plots/diablo_ALDR_ILESPLmi/"
metadata <- read.table("~/workspace/16S_final/CM_16S_cross-sectionalF245/metadata/metadata_F245_16S.csv", sep = ";", header = T, row.names = 1, stringsAsFactors = F, comment.char = "")
ile <- read.table("~/workspace/CM_IgSeq_tmp/analysis/relCounts_Clones_ILE.csv", sep = ";", header = T, row.names = 1, stringsAsFactors = F)
colnames(ile) <- gsub("\\.", "-", colnames(ile))
spl <- read.table("~/workspace/CM_IgSeq_tmp/analysis/relCounts_Clones_SPL.csv", sep = ";", header = T, row.names = 1, stringsAsFactors = F)
colnames(spl) <- gsub("\\.", "-", colnames(spl))
microbiome <- read.table("~/workspace/16S_final/CM_16S_cross-sectionalF245/analysis/seqtab_merge3/mergedQC/norm-CLEAN_ASVs_counts_merged_20210615.tsv", sep = "\t", header = T, row.names = 1, stringsAsFactors = F, comment.char = "")
colnames(microbiome) <- gsub("\\.", "-", colnames(microbiome))
# Keep only samples where we also have igseq
microbiome <- microbiome[, colnames(microbiome) %in% colnames(ile)]
ile <- ile[, colnames(ile) %in% colnames(microbiome)]
ile[is.na(ile)] <- 0
spl <- spl[, colnames(spl) %in% colnames(microbiome)]
spl[is.na(spl)] <- 0
ile <- ile[, order(colnames(ile))]
spl <- spl[, order(colnames(spl))]
microbiome <- microbiome[, order(colnames(microbiome))]
metadata <- metadata[metadata$ID %in% colnames(microbiome), ]
# Analysing only differences between diets
metadata <- metadata[metadata$Months == 24,]
##IF all diets, comment
#metadata <- metadata[metadata$Treatment %in% c("AL_lifelong", "DR_lifelong"), ]
ile <- ile[, colnames(ile) %in% metadata$ID]
spl <- spl[, colnames(spl) %in% metadata$ID]
microbiome <- microbiome[, colnames(microbiome) %in% metadata$ID]
#
diet <- metadata[, c("Treatment")]
months <- metadata[, c("Months")]
Y <- diet
# Removing clones only found in one sample as they dont add variability
ile <- t(ile)
presabsile <- ile
presabsile[presabsile>0] <-1
presabsile <- presabsile[, which(colSums(presabsile) > 1)]
ile <- ile[, colnames(ile) %in% colnames(presabsile)]
spl <- t(spl)
presabspl <- spl
presabspl[presabspl>0] <-1
presabspl <- presabspl[, which(colSums(presabspl) > 1)]
spl <- spl[, colnames(spl) %in% colnames(presabspl)]
spl<-as.data.frame(spl)
ile<-as.data.frame(ile)
microbiome <- as.data.frame(microbiome)
```
```{r}
# Now that we have the same order of columns, transpose and put together
data = list(ile = ile, spl = spl,
microbiome = t(microbiome))
# check dimension
lapply(data, dim)
```
```{r}
design = matrix(0.1, ncol = length(data), nrow = length(data),
dimnames = list(names(data), names(data)))
diag(design) = 0
design
```
```{r}
sgccda.res = block.splsda(X = data, Y = Y, ncomp = 5,
design = design)
set.seed(333) # for reproducibility, only when the `cpus' argument is not used
# this code takes a couple of min to run
perf.diablo = perf(sgccda.res, validation = 'Mfold', folds = 4, nrepeat = 10)
#perf.diablo # lists the different outputs
pdf(paste0(path,"PCcontributions.pdf"))
plot(perf.diablo)
dev.off()
```
```{r}
perf.diablo$choice.ncomp$WeightedVote
```
```{r}
# By looking at the PC plots, this combination is most logical
ncomp = perf.diablo$choice.ncomp$WeightedVote["Overall.ER", "max.dist"]
```
```{r}
#Tuning
test.keepX = list(ile = c(5:9, seq(10, 18, 2), seq(20,30,5)), microbiome = c(5:9, seq(10, 18, 2), seq(20,30,5)), spl = c(5:9, seq(10, 18, 2), seq(20,30,5)))
BPPARAM <- BiocParallel::SnowParam(workers = max(parallel::detectCores()-1, 2))
tune.TCGA = tune.block.splsda(X = data, Y = Y, ncomp = ncomp, test.keepX = test.keepX, design = design, validation = 'Mfold', folds = 4, nrepeat = 1, dist = "max.dist", BPPARAM = BPPARAM)
list.keepX = tune.TCGA$choice.keepX
list.keepX
```
```{r}
# Final model
sgccda.res = block.splsda(X = data, Y = Y, ncomp = ncomp,
keepX = list.keepX, design = design)
# List the functions related to the object
sgccda.res
## Y has been included automatically in the design, so that the covariance between each block's component and the outcome is maximised
```
```{r}
sgccda.res$design
```
```{r}
# Extract the selected variables
varmicrobiome <- selectVar(sgccda.res, block = 'microbiome', comp = 1)$microbiome$name
varspl <- selectVar(sgccda.res, block = 'spl', comp = 1)$spl$name
varile <- selectVar(sgccda.res, block = 'ile', comp = 1)$ile$name
```
```{r}
#check whether the correlation between components from each data set has been maximized as specified in the design matrix
## For all diets
col.per.group <- c("darkgreen", "gold", "dodgerblue", "red")
names(col.per.group) <- levels(Y)
# for ALDR
#col.per.group <- c("dodgerblue", "red")
#names(col.per.group) <- levels(Y)
pdf(paste0(path,"datasets_correlation_new.pdf"))
plotDiablo(sgccda.res, ncomp = 1, col.per.group = col.per.group)
dev.off()
```
```{r}
pdf(paste0(path,"PCindiv.pdf"))
plotIndiv(sgccda.res, ind.names = FALSE, legend = TRUE, title = 'DIABLO', ellipse = TRUE, col.per.group = col.per.group)
dev.off()
```
```{r}
pdf(paste0(path,"PCcombinedArrows.pdf"))
plotArrow(sgccda.res, ind.names = FALSE, legend = TRUE, title = 'DIABLO', col.per.group = col.per.group, ellipse = TRUE)
dev.off()
```
```{r}
pdf(paste0(path,"VarContributions.pdf"))
plotLoadings(sgccda.res, comp = 1, contrib = 'max', method = 'median', legend.color = col.per.group, legend = FALSE)
dev.off()
```
```{r}
network.res <- network(sgccda.res, save = "pdf", name.save = "myNetwork")
# Save for cytoscape
library(igraph)
write.graph(network.res$gR, file = "myNetwork.gml", format = "gml")
```
```{r}
# Calculate performance of the model
set.seed(123)# for reproducibility, only when the `cpus' argument is not used
perf.diablo = perf(sgccda.res, validation = 'Mfold', M = 4, nrepeat = 10,
dist = 'max.dist')
#perf.diablo # lists the different outputs
# Performance with Majority vote
perf.diablo$MajorityVote.error.rate
pdf(paste0(path,"AUROC_microbiome.pdf"))
auc.splsda = auroc(sgccda.res, roc.block = "microbiome", roc.comp = 1)
dev.off()
pdf(paste0(path,"AUROC_ile.pdf"))
auc.splsda = auroc(sgccda.res, roc.block = "ile", roc.comp = 1)
dev.off()
pdf(paste0(path,"AUROC_spl.pdf"))
auc.splsda = auroc(sgccda.res, roc.block = "spl", roc.comp = 1)
dev.off()
```
```{r}
pdf(paste0(path, "cimDiablo.pdf"))
cimDiablo(sgccda.res, color.blocks= c('lightblue', "darkgreen", 'chocolate3'), color.Y = col.per.group, margins=c(10,5))
dev.off()
```
```{r}
pdf(paste0(path,"circos.pdf"))
circosPlot(sgccda.res, cutoff = 0.7, line = TRUE,
color.blocks= c('lightblue', "darkgreen", 'chocolate3'),
color.cor = c("lightgreen","grey20"), size.labels = 1.5, color.Y = col.per.group)
dev.off()
```