DiagPlot.R 34 KB
Newer Older
svolant's avatar
svolant committed
1
2
3
4
5
6
7
8
9
10
#@ This file contains all the functions for the 
#@ diagnostic plots of SHAMAN


##############################################################
##
##        Main function for the diagnostic plots
##
##############################################################

svolant's avatar
svolant committed
11
Plot_diag <- function(input,resDiff,tree,getTable=FALSE)
svolant's avatar
svolant committed
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
{
  
  VarInt = input$VarInt
  dds = resDiff$dds
  counts = resDiff$raw_counts
  if(input$CountsType=="Normalized") counts = resDiff$countsNorm
  target = resDiff$target
  normFactors = resDiff$normFactors
  
  ## Counts at the OTU level
  CT = resDiff$CT_noNorm
  if(input$CountsType=="Normalized") CT = resDiff$CT_Norm
  
  group = as.data.frame(target[,VarInt])
  rownames(group) = rownames(target)
  res = NULL
  
  if(ncol(group)>0 && nrow(counts)>0 && !getTable)
  { 
    colors = rep(c("#1f77b4","#aec7e8","#ff7f0e","#ffbb78", "#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5","#8c564b",
                   "#c49c94","#e377c2","#f7b6d2","#7f7f7f", "#c7c7c7","#bcbd22","#dbdb8d","#17becf","#9edae5"),ceiling(nrow(target)/20))
    
    if(input$DiagPlot=="barplotTot") res = barplotTot(input,counts,group = group, col=colors)
    if(input$DiagPlot=="barplotNul") res = barPlotNul(input,counts, group = group, col=colors)
    if(input$DiagPlot=="densityPlot") res = densityPlotTot(input,counts, group = group, col=colors)
    if(input$DiagPlot=="boxplotNorm") res = boxplotNorm(input,CT,group = group, col=colors)
    if(input$DiagPlot=="DispPlot") res = plotDispEsts(dds)
    if(input$DiagPlot=="MajTax") res = majTaxPlot(input,counts, group = group, col=colors)
    if(input$DiagPlot=="SfactorsVStot") res = diagSFactors(input,normFactors,resDiff$raw_counts) 
    if(input$DiagPlot=="pcaPlot") res = PCAPlot_meta(input,dds, group,  type.trans = input$TransType, col = colors)
svolant's avatar
svolant committed
42
43
44
    if(input$DiagPlot=="pcoaPlot") res = PCoAPlot_meta(input,dds, group,CT,tree, col = colors) 
    if(input$DiagPlot=="nmdsPlot") res = NMDSPlot(input, dds, group,CT,tree, col = colors) 
    if(input$DiagPlot=="clustPlot") res = HCPlot(input,dds,group,type.trans=input$TransType,counts,CT,tree,col=colors)
svolant's avatar
svolant committed
45
46
  }
  if(getTable && input$DiagPlot=="pcaPlot") res = Get_pca_table(input,dds, group,  type.trans = input$TransType)
svolant's avatar
svolant committed
47
48
  if(getTable && input$DiagPlot=="pcoaPlot") res = Get_pcoa_table(input,dds, group,CT,tree)$table
  if(getTable && input$DiagPlot=="nmdsPlot") res = Get_nmds_table(input,dds, group,CT,tree)$table
svolant's avatar
svolant committed
49
50
51
52
53
  return(res)
}



svolant's avatar
svolant committed
54
55
56
57
58
59
60

##############################################################
##
##        Permanova test
##
##############################################################

svolant's avatar
svolant committed
61
Perma_test_Diag <- function(input,resDiff,tree)
svolant's avatar
svolant committed
62
63
64
65
66
67
68
69
70
{
  
  VarInt = input$VarInt
  dds = resDiff$dds
  
  ## Counts at the OTU level
  CT = resDiff$CT_noNorm
  if(input$CountsType=="Normalized") CT = resDiff$CT_Norm
  
svolant's avatar
svolant committed
71
72
73
  target = resDiff$target
  normFactors = resDiff$normFactors
  
svolant's avatar
svolant committed
74
75
76
77
78
79
  group = as.data.frame(target[,VarInt])
  rownames(group) = rownames(target)
  res = NULL
  
  if(ncol(group)>0 && !is.null(dds))
  { 
svolant's avatar
svolant committed
80
81
    if(input$DiagPlot=="pcoaPlot") res = Get_pcoa_table(input,dds, group,CT,tree)$test$aov.tab$`Pr(>F)`[1]
    if(input$DiagPlot=="nmdsPlot") res = Get_nmds_table(input,dds, group,CT,tree)$test$aov.tab$`Pr(>F)`[1]
svolant's avatar
svolant committed
82
83
84
85
86
87
  }

  return(res)
}


svolant's avatar
svolant committed
88
89
90
91
92
93
94
95
##############################################################
##
##          Plot functions 
##
##############################################################


## Hierarchical clustering
svolant's avatar
svolant committed
96
HCPlot <- function (input,dds,group,type.trans,counts,CT,tree,col = c("lightblue", "orange", "MediumVioletRed", "SpringGreen")) 
svolant's avatar
svolant committed
97
98
99
100
101
102
103
104
105
106
107
108
109
{
  
  res = NULL
  
  ## Get the counts
  if (input$DistClust == "euclidean" && type.trans == "VST") counts <- assay(varianceStabilizingTransformation(dds))
  if (input$DistClust == "euclidean" && type.trans == "rlog") counts <- assay(rlogTransformation(dds))
  
  ## Get the group of leaf
  group = apply(group,1,paste, collapse = "-")    
  nb = length(unique((group)))
  
  ## Get the dendrogram
110
111
  if(input$DistClust=="sere") dist.counts.norm = as.dist(SEREcoef(counts.norm))
  else if(input$DistClust=="Unifrac"){
svolant's avatar
svolant committed
112
    tmp = UniFracDist(CT,tree)
113
114
115
116
117
118
119
    if(is.null(tree) || is.null(tmp)) dist.counts.norm = NULL
    else
    {
      dist.counts.norm = switch(input$DistClustUnifrac,
                                "WU" = as.dist(tmp[, , "d_1"]), 
                                "UWU" = as.dist(tmp[, , "d_UW"]),
                                "VAWU" = as.dist(tmp[, , "d_VAW"]))
svolant's avatar
svolant committed
120
    }
121
    
svolant's avatar
svolant committed
122
  }
123
124
125
126
127
128
129
  else if(input$DistClust  %in% getDistMethods()){
    dist = as.dist(distance(t(sweep(counts.norm,2,colSums(counts.norm),`/`)), method=input$DistClust))
    dist[is.na(dist)]=0.0
    dist.counts.norm = dist
  }
  else  dist.counts.norm = vegdist(t(counts.norm), method = input$DistClust)

svolant's avatar
svolant committed
130
  if(!is.null(dist))
svolant's avatar
svolant committed
131
  {
svolant's avatar
svolant committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    hc <- hclust(dist, method = "ward.D")
    
    dend = as.dendrogram(hc)
    
    ## Get the type of dendrogram
    type <- input$typeHculst
    
    dend <- set(dend, "labels_cex", input$cexLabelDiag)
    if(input$colorHC) labels_colors(dend)<-col[as.integer(as.factor(group))][order.dendrogram(dend)]
    if(type=="hori") 
    { 
      par(cex=input$cexTitleDiag,mar=c(6,6,4,5))
      res = plot(dend, main = "Cluster dendrogram",xlab = paste(input$DistClust,"distance, Ward criterion",sep=" "),cex=input$cexLabelDiag)
    }  
146
    else
svolant's avatar
svolant committed
147
148
149
150
    {
      par(cex=input$cexTitleDiag,mar=c(6,6,4,5))
      res = circlize_dendrogram(dend, labels_track_height = 0.2, dend_track_height = .3, main = "Cluster dendrogram",xlab = paste(input$DistClust,"distance, Ward criterion",sep=" "))
    }
svolant's avatar
svolant committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  }
  return(res)
}

## PCA Eigen value
Plot_diag_Eigen <- function(input,resDiff)
{
  colors = c("dodgerblue","firebrick1","MediumVioletRed","SpringGreen")
  VarInt = input$VarInt
  dds = resDiff$dds
  counts = resDiff$counts
  target = resDiff$target
  group = as.data.frame(target[,VarInt])
  
  ## If more than 4 levels for one factor
  maxFact =max(sapply(group,FUN=function(x) length(levels(x))))
  if(maxFact>=4) colors = rainbow(maxFact) 
  
  PCAPlot_meta(input,dds, group,  type.trans = input$TransType, col = colors, plot = "eigen") 
}


## PCOA Eigen value
svolant's avatar
svolant committed
174
Plot_diag_pcoaEigen = function(input,resDiff,tree)
svolant's avatar
svolant committed
175
176
177
178
179
180
181
{
  colors = c("SpringGreen","dodgerblue","black","firebrick1")
  VarInt = input$VarInt
  dds = resDiff$dds
  target = resDiff$target
  group = as.data.frame(target[,VarInt])
  rownames(group) = rownames(target)
svolant's avatar
svolant committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  CT = resDiff$CT_noNorm
  if(input$CountsType=="Normalized") CT = resDiff$CT_Norm
  PCoAPlot_meta(input,dds, group,CT=CT,tree, col = colors, plot = "eigen") 
}


## NMDS stress plot
Plot_diag_nmdsStress = function(input,resDiff,tree)
{
  VarInt = input$VarInt
  dds = resDiff$dds
  target = resDiff$target
  group = as.data.frame(target[,VarInt])
  rownames(group) = rownames(target)
  CT = resDiff$CT_noNorm
  if(input$CountsType=="Normalized") CT = resDiff$CT_Norm
  NMDSPlot(input,dds, group,CT=CT,tree=tree, plot = "stress") 
svolant's avatar
svolant committed
199
200
201
}


svolant's avatar
svolant committed
202
203


svolant's avatar
svolant committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
## Boxplot for the counts normalized/no normalized
boxplotNorm <- function(input,CT, group, col = c("lightblue","orange", "MediumVioletRed", "SpringGreen")) 
{
  
  ncol1 <- ncol(group) == 1
  par(cex=input$cexTitleDiag,mar=c(12,6,4,5))
  if(input$RemoveNullValue) CT[CT==0] = NA
  
  ### Boxplots of the counts
  my.boxplot(log2(CT+1), las = 2, pol.col = col[as.integer(group[,1])],
             pol.density = if (ncol1) {NULL}
             else {15}, 
             pol.angle = if (ncol1) {NULL}
             else {seq(0,160,length.out =nlevels(group[, 2]))[as.integer(group[, 2])]},
             main = paste(input$CountsType, "counts distribution"), ylab = expression(log[2] ~ ( count + 1)))
  legend("topright", levels(group[, 1]), fill = col[1:nlevels(group[,1])], bty = "n")
  if (!ncol1)  legend("topleft", levels(group[, 2]), density = 15,col = 1, angle = seq(0,160,length.out =nlevels(group[, 2]))[1:nlevels(group[, 2])], bty = "n")
  
}

## barplot total
barplotTot <- function(input,counts, group, cex.names = 1, col = c("lightblue","orange", "MediumVioletRed", "SpringGreen")) 
{
  
  ncol1 <- ncol(group) == 1
  par(cex=input$cexTitleDiag,mar=c(12,6,4,5))
  barplot(colSums(counts), cex.names = cex.names, main = "Total mapped read count per sample", ylab = "Total mapped read count", 
          ylim = c(0, max(colSums(counts)) * 1.2), density = if (ncol1) {NULL}
          else {15}, 
          angle = if (ncol1) {NULL}
          else {seq(0,160,length.out =nlevels(group[, 2]))[as.integer(group[, 2])]}, col = col[as.integer(group[, 1])], las = 2)
  legend("topright", levels(group[, 1]), fill = col[1:nlevels(group[,1])], bty = "n")
  if (!ncol1)  legend("topleft", levels(group[, 2]), density = 15,col = 1, angle = seq(0,160,length.out =nlevels(group[, 2]))[1:nlevels(group[, 2])], bty = "n")
  
}


## barplot Nul 
barPlotNul <-function (input,counts, group, cex.names = 1, col = c("lightblue","orange", "MediumVioletRed", "SpringGreen")) 
{
  
  percentage <- apply(counts, 2, function(x) {sum(x == 0)}) * 100/nrow(counts)
  percentage.allNull <- (nrow(counts) - nrow(removeNulCounts(counts))) * 100/nrow(counts)
  ncol1 <- ncol(group) == 1
  
  par(cex=input$cexTitleDiag,mar=c(12,6,4,5))
  
  barplot(percentage, las = 2, col = col[as.integer(group[,1])], 
          density = if (ncol1) {NULL}
          else {15}, 
          angle = if (ncol1) {NULL}
          else {seq(0,160,length.out =nlevels(group[, 2]))[as.integer(group[, 2])]},
          cex.names = cex.names, ylab = "Proportion of null counts", 
          main = "Proportion of null counts per sample", 
          ylim = c(0, 1.2 * ifelse(max(percentage) == 0, 1, max(percentage))))
  
  abline(h = percentage.allNull, lty = 2, lwd = 2)
  legend("topright", levels(group[, 1]), fill = col[1:nlevels(group[,1])], bty = "n")
  if (!ncol1) legend("topleft", levels(group[, 2]), density = 15, col = 1, angle = seq(0,160,length.out =nlevels(group[, 2]))[1:nlevels(group[, 2])], bty = "n")
}


## Plot density
densityPlotTot <-function (input,counts, group, col = c("lightblue", "orange", "MediumVioletRed", "SpringGreen")) 
{
  
  counts <- removeNulCounts(counts)
  ncol1 <- ncol(group) == 1
  par(cex=input$cexTitleDiag,mar=c(12,5,4,5))
  plot(density(log2(counts[, 1] + 1)), las = 1, lwd = 2, main = "Density of counts distribution", 
       xlab = expression(log[2] ~ (raw ~ count + 1)), 
       ylim = c(0, max(apply(counts, 2, function(x) {max(density(log2(x + 1))$y)})) * 1.05), 
       lty = if (ncol1) {1}
       else{rep(seq(1:6),ceiling(nlevels(group[, 2])/6))[as.integer(group[, 2])[1]]}, 
       col = col[as.integer(group[, 1])[1]])
  
  for (i in 2:ncol(counts)) 
  {
    lines(density(log2(counts[, i] + 1)), col = col[as.integer(group[,1])[i]], lwd = 2, 
          lty = if (ncol1) {1}
          else{rep(seq(1:6),ceiling(nlevels(group[, 2])/6))[as.integer(group[, 2])[i]]})
  }
  legend("topright", levels(group[, 1]), lty = 1, col = col[1:nlevels(group[,1])], lwd = 2, bty = "n")
  if (!ncol1) legend("topleft", levels(group[, 2]), lty = rep(seq(1:6),ceiling(nlevels(group[, 2])/6))[1:nlevels(group[, 2])], col = 1, lwd = 2, bty = "n")
  
}


## Table of maj. taxo
majTab <- function(input,counts,n)
{
  seqnames <- apply(counts, 2, function(x) {
    x <- sort(x, decreasing = TRUE)
    names(x)[1:n]
  })
  seqnames <- unique(unlist(as.character(seqnames)))
  sum <- apply(counts, 2, sum)
  counts <- counts[seqnames, ]
  sum <- matrix(sum, nrow(counts), ncol(counts), byrow = TRUE)
  p <- round(100 * counts/sum, digits = 3)
  return(p)
}


## Plot maj. taxo
majTaxPlot <-function (input,counts, n = 3, group, cex.names = 1, col = c("lightblue",  "orange", "MediumVioletRed", "SpringGreen")) 
{
  p = majTab(input,counts,n)
  maj <- apply(p, 2, max)
  seqname <- rownames(p)[apply(p, 2, which.max)]
  ncol1 <- ncol(group) == 1
  par(cex=input$cexTitleDiag,mar=c(12,6,4,5))
  x <- barplot(maj, col = col[as.integer(group[, 1])], main = "Proportion of mapped reads from\nmost expressed sequence",
               ylim = c(0, max(maj) * 1.2), cex.main = 1, 
               cex.names = cex.names, las = 2, ylab = "Proportion of mapped reads", 
               density = if (ncol1) {NULL}
               else {15}, 
               angle = if (ncol1) {NULL}
               else {seq(0,160,length.out =nlevels(group[, 2]))[as.integer(group[, 2])]})
  
  legend("topright", levels(group[, 1]), fill = col[1:nlevels(group[,1])], bty = "n")
  if (!ncol1) legend("topleft", levels(group[, 2]), density = 15, col = 1, 
                     angle = seq(0,160,length.out =nlevels(group[, 2]))[1:nlevels(group[, 2])], bty = "n")
  
  for (i in 1:length(seqname)) text(x[i], maj[i]/2, seqname[i], cex=input$cexLabelDiag, srt = 90, adj = 0)
}


## Get the SERE COEF
SEREcoef<-function(counts)
{
  counts = as.matrix(counts)
  sere <- matrix(0, ncol = ncol(counts), nrow = ncol(counts))
  for (i in 1:(ncol(counts)-1)) {
    for (j in (i+1):ncol(counts)) {
      sere[i, j] <- sigfun_Pearson_meta(counts[, c(i, j)])
    }
  }
  sere=sere+t(sere)
  colnames(sere) <- rownames(sere) <- colnames(counts)
  sere <- round(sere, digits = 3)
  
  return(sere) 
}


## function for the SERE coef
sigfun_Pearson_meta <- function(observed) {
  laneTotals <- colSums(observed)
  total <- sum(laneTotals)
  fullObserved <- observed[rowSums(observed) > 0, ]
  fullLambda <- rowSums(fullObserved)/total
  fullLhat <- fullLambda > 0
  fullExpected <- outer(fullLambda, laneTotals)
  fullKeep <- which(fullExpected > 0)
  oeFull <- (fullObserved[fullKeep] - fullExpected[fullKeep])^2/fullExpected[fullKeep]
  dfFull <- length(fullKeep) - sum(fullLhat != 0)
  sqrt(sum(oeFull)/dfFull)
}


## Plots of size factors
diagSFactors<-function (input,normFactors,counts) 
{
  geomeans <- exp(rowMeans(log(counts)))
  samples <- colnames(counts)
  par(cex=input$cexTitleDiag,mar=c(6,6,4,5))
  plot(normFactors, colSums(counts), pch = 19, las = 1,cex = ifelse(input$addLabelSFact,0,input$cexLabelDiag),
       ylab = "Total number of reads", xlab = "Size factors", 
       main = "Diagnostic: size factors vs total number of reads")
  if(input$addLabelSFact) text(normFactors,colSums(counts),labels = samples,cex=input$cexLabelDiag)
  abline(lm(colSums(counts) ~ normFactors + 0), lty = 2, col = "grey")
}


### PCoA
svolant's avatar
svolant committed
380
PCoAPlot_meta <-function (input, dds, group_init, CT,tree,col = c("SpringGreen","dodgerblue","black","firebrick1"), plot = "pcoa") 
svolant's avatar
svolant committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
{
  cval=c()
  time_set = 0
  # Set of shape
  shape=c(19,17,15,18)
  
  ## Var of interest
  VarInt  = input$VarInt
  
  ## Group
  group = as.character(apply(group_init,1,paste, collapse = "-"))
  
  ## Keep only some sample 
  val = c()
  for(i in 1:length(VarInt))
  { 
    Tinput = paste("input$","Mod",VarInt[i],sep="")
    expr=parse(text=Tinput)
    ## All the modalities for all the var of interest
    val = c(val,eval(expr))
  }
  if(length(VarInt)>1) Kval = apply(expand.grid(val,val),1,paste, collapse = "-")
  else Kval = val
  ind_kept = which(as.character(group)%in%Kval)
  
  ## Get the group corresponding to the modalities
  group = group[ind_kept]
  nb = length(unique((group)))
  group = as.factor(group)
  
  if(nlevels(group)!=0 && !is.null(input$PCaxe1) && !is.null(input$PCaxe2))
  { 
    ## Get the norm data
    counts.norm = as.data.frame(round(counts(dds)))
    if(input$CountsType=="Normalized") counts.norm = as.data.frame(round(counts(dds, normalized = TRUE)))
    # was removed
    counts.norm = counts.norm[,ind_kept]
svolant's avatar
svolant committed
418
    # print(head(counts.norm))
svolant's avatar
svolant committed
419
420
    ## Get the distance
    if(input$DistClust=="sere") dist.counts.norm = as.dist(SEREcoef(counts.norm))
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
421
    else if(input$DistClust=="Unifrac"){
422
423
424
425
426
427
      saveRDS(counts.norm,file="/home/aghozlan/workspace/shaman/count_matrix.rds")
      saveRDS(tree,file="/home/aghozlan/workspace/shaman/tree.rds")
      print(counts.norm)
      print(tree)
      #tmp = UniFracDist(CT,tree)
      tmp = UniFracDist(counts.norm,tree)
svolant's avatar
svolant committed
428
      if(is.null(tree) || is.null(tmp)) dist.counts.norm = NULL
429
      else
svolant's avatar
svolant committed
430
431
      {
        dist.counts.norm = switch(input$DistClustUnifrac,
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
432
433
                                  "WU" = as.dist(tmp[, , "d_1"]), 
                                  "UWU" = as.dist(tmp[, , "d_UW"]),
434
                                  "VAWU" = as.dist(tmp[, , "d_VAW"]))
svolant's avatar
svolant committed
435
      }
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
436
      
svolant's avatar
svolant committed
437
    }
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
438
439
440
441
    else if(input$DistClust  %in% getDistMethods()){
      dist = as.dist(distance(t(sweep(counts.norm,2,colSums(counts.norm),`/`)), method=input$DistClust))
      dist[is.na(dist)]=0.0
      dist.counts.norm = dist
442
    }
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
443
444
    else  dist.counts.norm = vegdist(t(counts.norm), method = input$DistClust)
    #"additive_symm"
svolant's avatar
svolant committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    if(!is.null(dist.counts.norm))
    {
      ## Do PCoA
      pco.counts.norm = dudi.pco(d = dist.counts.norm, scannf = FALSE,nf=ncol(counts.norm))
      
      ## Get eigen values
      eigen=(pco.counts.norm$eig/sum(pco.counts.norm$eig))*100
      
      ## xlim and ylim of the plot
      min = min(pco.counts.norm$li)
      max = max(pco.counts.norm$li)
      
      ## get condition set
      condition_set=val[which(val %in% unique(group_init$condition))]
      time_set=val[which(val %in% unique(group_init$time))]
svolant's avatar
svolant committed
460
      
svolant's avatar
svolant committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
      ## Colors
      if(length(col)<length(condition_set) * length(time_set))# && !input$colorgroup)
      {
        col = rainbow(length(condition_set) * length(time_set))
      }
      #else if(length(col)<length(condition_set) * length(time_set) && input$colorgroup){
      #  col = rep(col[1:length(condition_set)], length(time_set))
      #}
      if (length(time_set) == 1 && length(condition_set) <= 4){
        cval = apply(expand.grid(condition_set,time_set),1,paste, collapse = "-")
        cval = sort(cval)
      }
      
      # to reactivate
      #pco.counts.norm$li = pco.counts.norm$li[ind_kept,]
      if (plot == "pcoa"){
        par(cex=input$cexTitleDiag,mar=c(6,6,4,5))
        ## Plot axis, label and circles
        v_axes = c(as.numeric(gsub("PC","",input$PCaxe1)),as.numeric(gsub("PC","",input$PCaxe2)))
        
        plot(pco.counts.norm$li[v_axes], 
             xlab=paste(input$PCaxe1, ": ",round(eigen[v_axes[1]],1),"%") , 
             ylab=paste(input$PCaxe2, ": ",round(eigen[v_axes[2]],1),"%"),
             xlim=c(min+0.25*min,max+0.25*max), ylim=c(min-0.1,max+0.1), 
             cex.axis=1, cex.lab=1,lwd=2, type="n")
        
        title(main='Principal Coordinates Analysis ',cex.main=1.5)
        ## Add a subtitle
        par(font.main=3)
        if(input$DistClust=="Unifrac") title(main=paste("\n","\n",input$DistClustUnifrac,"distance",sep=" "),cex.main=1)
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
491
        else title(main=paste("\n","\n",input$DistClust,"distance",sep=" "),cex.main=1)
svolant's avatar
svolant committed
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
        
        # Set different shapes
        if(input$labelPCOA == "Group"){
          if(!is.null(cval)){
            for (i in 1:length(cval)){
              points(pco.counts.norm$li[which(group==cval[i]),v_axes],pch=shape[i],col=col[i], cex=input$cexpoint)
            }
            s.class(dfxy = pco.counts.norm$li[v_axes], fac = group, col = col, label = levels(group),
                    add.plot = TRUE, cpoint = 0, cell=input$cexcircle, clabel=input$cexLabelDiag,  cstar = input$cexstar)
          }else s.class(dfxy = pco.counts.norm$li[v_axes], fac = group, col = col, label = levels(group),
                        add.plot = TRUE, cpoint = input$cexpoint, cell=input$cexcircle, clabel=input$cexLabelDiag,  cstar = input$cexstar)
        }  
        else{
          s.label(pco.counts.norm$li, clabel = input$cexLabelDiag,boxes=FALSE, add.plot = TRUE)
          s.class(dfxy = pco.counts.norm$li, fac = group, col = col, label = levels(group), add.plot = TRUE, cpoint = 0, clabel = 0, cstar = input$cexstar, cell=input$cexcircle)
        }
      }else{
        v_axes = c(as.numeric(gsub("PC","",input$PCaxe1)),as.numeric(gsub("PC","",input$PCaxe2)))
        nbBar = max(7,max(v_axes))
        col = rep("grey",nbBar)
        col[v_axes] = "black"
        barplot(eigen[1:nbBar], xlab="Dimensions", ylab="Eigenvalues (%)", names.arg = 1:nbBar, col = col, ylim=c(0,max(eigen)+5), cex.axis=1.2, cex.lab=1.4,cex.names=1.2)
svolant's avatar
svolant committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
      }
    }
  }
  
}


### PCA
PCAPlot_meta <-function(input,dds, group_init, n = min(500, nrow(counts(dds))), type.trans = c("VST", "rlog"), 
                        col = c("lightblue", "orange", "MediumVioletRed", "SpringGreen"),plot="pca") 
{
  ## Var of interest
  VarInt  = input$VarInt
  
  group = as.character(apply(group_init,1,paste, collapse = "-"))
  group_init = group
  
  ## Keep only some sample 
  val = c()
  for(i in 1:length(VarInt))
  { 
    Tinput = paste("input$","Mod",VarInt[i],sep="")
    expr=parse(text=Tinput)
    ## All the modalities for all the var of interest
    val = c(val,eval(expr))
  }
  if(length(VarInt)>1) Kval = apply(expand.grid(val,val),1,paste, collapse = "-")
  else Kval = val
  ind_kept = which(as.character(group)%in%Kval)
  
svolant's avatar
svolant committed
544
  # save(val,Kval,dds,group_init,type.trans,VarInt,ind_kept,file="testLDA")
svolant's avatar
svolant committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  ## Get the group corresponding to the modalities
  group = group[ind_kept]
  nb = length(unique((group)))
  group = as.factor(group)
  
  ## To select the colors
  indgrp =as.integer(as.factor(group_init))[ind_kept]
  
  
  if(nlevels(group)!=0 && !is.null(input$PCaxe1) && !is.null(input$PCaxe2))
  { 
    type.trans <- type.trans[1]
    
    if (type.trans == "VST") counts.trans <- assay(varianceStabilizingTransformation(dds))
    else counts.trans <- assay(rlogTransformation(dds))
    counts.trans = counts.trans[,ind_kept]
    
    rv = apply(counts.trans, 1, var, na.rm = TRUE)
    pca = prcomp(t(counts.trans[order(rv, decreasing = TRUE),][1:n, ]))
    
    if(plot=="pca")
    { 
      prp <- pca$sdev^2 * 100/sum(pca$sdev^2)
      prp <- round(prp, 2)
      ncol1 <- ncol(group) == 1
svolant's avatar
svolant committed
570
571

      abs = range(pca$x[, as.numeric(gsub("PC","",input$PCaxe1))])
svolant's avatar
svolant committed
572
      abs = abs(abs[2] - abs[1])/25
svolant's avatar
svolant committed
573
      ord = range(pca$x[, as.numeric(gsub("PC","",input$PCaxe2))])
svolant's avatar
svolant committed
574
575
576
577
578
579
580
581
582
583
584
585
586
      ord = abs(ord[2] - ord[1])/25
      
      
      plot(pca$x[,as.numeric(gsub("PC","",input$PCaxe1))],pca$x[,as.numeric(gsub("PC","",input$PCaxe2))], las = 1, cex = input$cexTitleDiag, col = col[indgrp], 
           pch = 16,
           xlab = paste0(input$PCaxe1," (", prp[as.numeric(gsub("PC","",input$PCaxe1))], "%)"),
           ylab = paste0(input$PCaxe2," (", prp[as.numeric(gsub("PC","",input$PCaxe2))], "%)"),
           main = "Principal Component Analysis"
      )
      abline(h = 0, v = 0, lty = 2, col = "lightgray")
      text(pca$x[, as.numeric(gsub("PC","",input$PCaxe1))] - ifelse(pca$x[, as.numeric(gsub("PC","",input$PCaxe1))] > 0, abs, -abs), pca$x[,as.numeric(gsub("PC","",input$PCaxe2))] - ifelse(pca$x[,as.numeric(gsub("PC","",input$PCaxe2))] > 0, ord, -ord), colnames(counts.trans), col = col[indgrp],cex=input$cexLabelDiag)
      
    }
587
588
589
590
591
592
    if(plot=="eigen"){
      nbBar = max(7,max(c(as.numeric(gsub("PC","",input$PCaxe1)),as.numeric(gsub("PC","",input$PCaxe2)))))
      col = rep("grey",nbBar)
      eigen = pca$sdev[1:nbBar]^2
      col[c(as.numeric(gsub("PC","",input$PCaxe1)),as.numeric(gsub("PC","",input$PCaxe2)))] = "black"
      barplot(eigen, xlab="Dimensions", ylab="Eigenvalues (%)", names.arg = 1:nbBar, col = col, ylim=c(0,max(eigen)+5), cex.axis=1.2, cex.lab=1.4,cex.names=1.2)}
svolant's avatar
svolant committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
    
  }
}



##############################################################
##
##          Useful functions
##
##############################################################

## Remove nul counts
removeNulCounts <-function (counts) 
{
  return(counts[rowSums(counts) > 0, ])
}


## Color for the horizontal dendro
colLabdendo <- function(n,group) {
  
  group = apply(group,1,paste, collapse = "-")
  
  nb = length(unique((group)))
  namesGrp = names(group)
  
  if (is.leaf(n)) {
    a <- attributes(n)
    labCol <- rainbow(nb)[as.integer(as.factor(group))[which(namesGrp == a$label)]]
    attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
  }
  return(n)
}

## Create your own boxplots to get hatched boxplots
my.boxplot <- function(x, pol.col = 1, pol.density = NULL, pol.angle = 45,
                       bxp.pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5), ...){
  res <- boxplot(x, pars = bxp.pars, ...) # que boxplot se d�merde avec ses arguments
  n <- ncol(res$stats) # nombre de boxplots
  density <- if(is.null(pol.density)){NULL}else{rep(pol.density, length = n)}
  angle <- if(is.null(pol.angle)){NULL}else{rep(pol.angle, length = n)}
  col <- if(is.null(pol.col)){NULL}else{rep(pol.col, length = n)}
  # Ajout des textures
  ex <- bxp.pars$boxwex/2 
  for(i in 1:n){
    polygon(c(i - ex, i - ex, i + ex, i + ex),
            c(res$stats[2, i], res$stats[4, i], res$stats[4, i], res$stats[2, i]),
            density = density[i], angle = angle[i], col = col[i])
    segments(i-ex,res$stats[3,i],i+ex,res$stats[3,i],lwd=3,col="black",lend=1)
  }
}


### Get PCOA table (useful to get the number of axes)
svolant's avatar
svolant committed
648
Get_pcoa_table <-function (input, dds, group_init,CT,tree) 
svolant's avatar
svolant committed
649
650
651
652
653
{
  cval=c()
  time_set = 0
  # Set of shape
  shape=c(19,17,15,18)
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
654

svolant's avatar
svolant committed
655
656
657
658
659
660
661
662
  ## Var of interest
  VarInt  = input$VarInt
  
  ## Group
  group = as.character(apply(group_init,1,paste, collapse = "-"))
  
  ## Keep only some sample 
  val = c()
svolant's avatar
svolant committed
663
664
  permanova_test = NULL
  
svolant's avatar
svolant committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
  for(i in 1:length(VarInt))
  { 
    Tinput = paste("input$","Mod",VarInt[i],sep="")
    expr=parse(text=Tinput)
    ## All the modalities for all the var of interest
    val = c(val,eval(expr))
  }
  if(length(VarInt)>1) Kval = apply(expand.grid(val,val),1,paste, collapse = "-")
  else Kval = val
  ind_kept = which(as.character(group)%in%Kval)
  
  ## Get the group corresponding to the modalities
  group = group[ind_kept]
  nb = length(unique((group)))
  group = as.factor(group)
  
  if(nlevels(group)!=0)
  { 
    ## Get the norm data
    counts.norm = as.data.frame(round(counts(dds)))
    if(input$CountsType=="Normalized") counts.norm = as.data.frame(round(counts(dds, normalized = TRUE)))
    # was removed
    counts.norm = counts.norm[,ind_kept]
    
    ## Get the distance
    if(input$DistClust=="sere") dist.counts.norm = as.dist(SEREcoef(counts.norm))
691
    else if(input$DistClust=="Unifrac") {
692
      tmp = UniFracDist(counts.norm,tree)
svolant's avatar
svolant committed
693
      if(is.null(tree) || is.null(tmp)) dist.counts.norm = NULL
694
695
      else{
          dist.counts.norm = switch(input$DistClustUnifrac,
svolant's avatar
svolant committed
696
697
                                "WU" = as.dist(tmp[, , "d_1"]), 
                                "UWU" = as.dist(tmp[, , "d_UW"]),
698
                                "VAWU" = as.dist(tmp[, , "d_VAW"]))
svolant's avatar
svolant committed
699
700
      }
    }
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
701
702
703
704
705
    else if(input$DistClust  %in% getDistMethods()){
      dist = as.dist(distance(t(sweep(counts.norm,2,colSums(counts.norm),`/`)), method=input$DistClust))
      dist[is.na(dist)]=0.0
      dist.counts.norm = dist
    }
706
707
708
    else {
      dist.counts.norm = vegdist(t(counts.norm), method = input$DistClust)
    }
svolant's avatar
svolant committed
709
    
svolant's avatar
svolant committed
710
711
712
713
714
715
716
717
718
    if(!is.null(dist.counts.norm)){ 
      ## To get always the same result 
      set.seed(666)
      permanova_test = adonis(dist.counts.norm~group)
  
      ## Do PCoA
      pco.counts.norm = dudi.pco(d = dist.counts.norm, scannf = FALSE,nf=ncol(counts.norm))
      return(list(table = pco.counts.norm$li,test=permanova_test))
    } else return(list(table = NULL,test=NULL))
svolant's avatar
svolant committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
    
  }
}

### Get PCA table (useful to get the number of axes)
Get_pca_table <-function(input,dds, group_init, n = min(500, nrow(counts(dds))), type.trans = c("VST", "rlog")) 
{
  ## Var of interest
  VarInt  = input$VarInt
  
  group = as.character(apply(group_init,1,paste, collapse = "-"))
  group_init = group
  
  ## Keep only some sample 
  val = c()
  for(i in 1:length(VarInt))
  { 
    Tinput = paste("input$","Mod",VarInt[i],sep="")
    expr=parse(text=Tinput)
    ## All the modalities for all the var of interest
    val = c(val,eval(expr))
  }
  if(length(VarInt)>1) Kval = apply(expand.grid(val,val),1,paste, collapse = "-")
  else Kval = val
  ind_kept = which(as.character(group)%in%Kval)
  
  ## Get the group corresponding to the modalities
  group = group[ind_kept]
  nb = length(unique((group)))
  group = as.factor(group)
  
  ## To select the colors
  indgrp =as.integer(as.factor(group_init))[ind_kept]
  
  
  if(nlevels(group)!=0)
  { 
    type.trans <- type.trans[1]
    
    if (type.trans == "VST") counts.trans <- assay(varianceStabilizingTransformation(dds))
    else counts.trans <- assay(rlogTransformation(dds))
    counts.trans = counts.trans[,ind_kept]
    
    rv = apply(counts.trans, 1, var, na.rm = TRUE)
    pca = prcomp(t(counts.trans[order(rv, decreasing = TRUE),][1:n, ]))
    return(pca$x)
  }
}


svolant's avatar
svolant committed
769
Get_nmds_table <-function(input,dds, group_init,CT,tree) 
svolant's avatar
svolant committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
{
  ## Var of interest
  VarInt  = input$VarInt
  
  group = as.character(apply(group_init,1,paste, collapse = "-"))
  group_init = group
  
  ## Keep only some sample 
  val = c()
  for(i in 1:length(VarInt))
  { 
    Tinput = paste("input$","Mod",VarInt[i],sep="")
    expr=parse(text=Tinput)
    ## All the modalities for all the var of interest
    val = c(val,eval(expr))
  }
  if(length(VarInt)>1) Kval = apply(expand.grid(val,val),1,paste, collapse = "-")
  else Kval = val
  ind_kept = which(as.character(group)%in%Kval)
  
  ## Get the group corresponding to the modalities
  group = group[ind_kept]
  nb = length(unique((group)))
  group = as.factor(group)
  
  ## To select the colors
  indgrp =as.integer(as.factor(group_init))[ind_kept]
  
  
  if(nlevels(group)!=0)
  { 
    ## Get the norm data
    counts.norm = as.data.frame(round(counts(dds)))
    if(input$CountsType=="Normalized") counts.norm = as.data.frame(round(counts(dds, normalized = TRUE)))
    # was removed
    counts.norm = counts.norm[,ind_kept]
    
    ## Get the distance
svolant's avatar
svolant committed
808
    if(input$DistClust!="sere" && input$DistClust!="Unifrac") dist.counts.norm = vegdist(t(counts.norm), method = input$DistClust)
svolant's avatar
svolant committed
809
    if(input$DistClust=="sere") dist.counts.norm = as.dist(SEREcoef(counts.norm))
svolant's avatar
svolant committed
810
811
812
813
814
815
816
817
818
    if(input$DistClust=="Unifrac") {
      tmp = UniFracDist(CT,tree)
      if(is.null(tree) || is.null(tmp)) dist.counts.norm = NULL
      if(!is.null(tree)) {dist.counts.norm = switch(input$DistClustUnifrac,
                                                    "WU" = as.dist(tmp[, , "d_1"]), 
                                                    "UWU" = as.dist(tmp[, , "d_UW"]),
                                                    "VAWU" = as.dist(tmp[, , "d_VAW"])
      )
      }
svolant's avatar
svolant committed
819
    
svolant's avatar
svolant committed
820
    }
svolant's avatar
svolant committed
821
    
svolant's avatar
svolant committed
822
823
824
825
826
827
828
829
830
831
832
833
834
    if(!is.null(dist.counts.norm)){ 
      
      ## To get always the same result 
      set.seed(666)
      permanova_test = adonis(dist.counts.norm~group)
      
      ## Do NMDS
      # nmds.counts.norm = metaMDS(dist.counts.norm,k=min(round((nrow(counts.norm)-1)/2-1),round(ncol(counts.norm)/2)), trymax = 1)
      nmds.counts.norm = metaMDS(dist.counts.norm,k=3, trymax = 1)
      proj = nmds.counts.norm$points
      return(list(table = proj,test=permanova_test,nmds=nmds.counts.norm))
      
    } else return(list(table = NULL,test=NULL))
svolant's avatar
svolant committed
835
836
837
    
  }
}
svolant's avatar
svolant committed
838
839
840
841
842
843





### NMDS
svolant's avatar
svolant committed
844
NMDSPlot <-function (input, dds, group_init, CT,tree,col = c("SpringGreen","dodgerblue","black","firebrick1"),plot="nmds") 
svolant's avatar
svolant committed
845
846
847
848
849
{
  ## Var of interest
  VarInt  = input$VarInt
  
  group = as.character(apply(group_init,1,paste, collapse = "-"))
svolant's avatar
svolant committed
850
  group_init = group
svolant's avatar
svolant committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
  
  ## Keep only some sample 
  val = c()
  for(i in 1:length(VarInt))
  { 
    Tinput = paste("input$","Mod",VarInt[i],sep="")
    expr=parse(text=Tinput)
    ## All the modalities for all the var of interest
    val = c(val,eval(expr))
  }
  if(length(VarInt)>1) Kval = apply(expand.grid(val,val),1,paste, collapse = "-")
  else Kval = val
  ind_kept = which(as.character(group)%in%Kval)
  
  ## Get the group corresponding to the modalities
  group = group[ind_kept]
  nb = length(unique((group)))
  group = as.factor(group)
  
svolant's avatar
svolant committed
870
871
872
873
  ## To select the colors
  indgrp =as.integer(as.factor(group_init))[ind_kept]
  
  
svolant's avatar
svolant committed
874
875
  if(nlevels(group)!=0 && !is.null(input$PCaxe1) && !is.null(input$PCaxe2))
  { 
svolant's avatar
svolant committed
876
    
svolant's avatar
svolant committed
877
878
879
880
881
882
883
    ## Get the norm data
    counts.norm = as.data.frame(round(counts(dds)))
    if(input$CountsType=="Normalized") counts.norm = as.data.frame(round(counts(dds, normalized = TRUE)))
    # was removed
    counts.norm = counts.norm[,ind_kept]
    
    ## Get the distance
svolant's avatar
svolant committed
884
    if(input$DistClust!="sere" && input$DistClust!="Unifrac") dist.counts.norm = vegdist(t(counts.norm), method = input$DistClust)
svolant's avatar
svolant committed
885
    if(input$DistClust=="sere") dist.counts.norm = as.dist(SEREcoef(counts.norm))
svolant's avatar
svolant committed
886
887
888
889
890
891
892
893
894
    if(input$DistClust=="Unifrac") {
      tmp = UniFracDist(CT,tree)
      if(is.null(tree) || is.null(tmp)) dist.counts.norm = NULL
      if(!is.null(tree)) {dist.counts.norm = switch(input$DistClustUnifrac,
                                                    "WU" = as.dist(tmp[, , "d_1"]), 
                                                    "UWU" = as.dist(tmp[, , "d_UW"]),
                                                    "VAWU" = as.dist(tmp[, , "d_VAW"])
      )
      }
svolant's avatar
svolant committed
895
    
svolant's avatar
svolant committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
    }

    if(!is.null(dist.counts.norm)){ 
      
      ## Do NMDS
      # nmds.counts.norm = metaMDS(dist.counts.norm,k=min(round((nrow(counts.norm)-1)/2-1),round(ncol(counts.norm)/2)), trymax = 25)
      nmds.counts.norm = metaMDS(dist.counts.norm,k=3, trymax = 25)
      if(plot =="nmds") {

        proj = nmds.counts.norm$points
        
        ## xlim and ylim of the plot
        min = min(proj); max = max(proj)
        abs = range(proj[, as.numeric(gsub("PC","",input$PCaxe1))])
        abs = abs(abs[2] - abs[1])/25
        ord = range(proj[, as.numeric(gsub("PC","",input$PCaxe2))])
        ord = abs(ord[2] - ord[1])/25
        
        plot(proj[,as.numeric(gsub("PC","",input$PCaxe1))],proj[,as.numeric(gsub("PC","",input$PCaxe2))], las = 1, cex = input$cexTitleDiag, col = col[indgrp], 
             pch = 16,
             xlab = paste("MDS",gsub("PC","",input$PCaxe1)),
             ylab = paste("MDS",gsub("PC","",input$PCaxe2))
        )
        title(main= "Non-metric multidimensional scaling",cex.main=1.5)
        par(font.main=3)
        if(input$DistClust=="Unifrac") title(main=paste("\n","\n",input$DistClustUnifrac,"distance",sep=" "),cex.main=1)
922
        else title(main=paste("\n","\n",input$DistClust,"distance",sep=" "),cex.main=1)
svolant's avatar
svolant committed
923
924
925
926
927
        abline(h = 0, v = 0, lty = 2, col = "lightgray")
        text(proj[,as.numeric(gsub("PC","",input$PCaxe1))] - ifelse(proj[,as.numeric(gsub("PC","",input$PCaxe1))] > 0, abs, -abs), proj[,as.numeric(gsub("PC","",input$PCaxe2))] - ifelse(proj[,as.numeric(gsub("PC","",input$PCaxe2))] > 0, ord, -ord), colnames(counts.norm), col = col[indgrp],cex=input$cexLabelDiag)
      }
      if(plot=="stress") stressplot(nmds.counts.norm,pch=16,p.col="black")
    }
svolant's avatar
svolant committed
928
929
930
931
932
933
  }
  
}



svolant's avatar
svolant committed
934
935
936
937
938
939
940
941
942
943
944
945
### Unifrac distance 
UniFracDist<-function(counts,tree)
{
  counts_otu = round(t(as.matrix(counts)),0)
  # unifracs = vegdist(counts_otu) ## Default value
  unifracs = NULL
  if(!is.null(counts_otu) && !is.null(tree)){
    unifracs <- GUniFrac(counts_otu, tree, alpha=c(0, 0.5, 1))$unifracs
  }
  return(unifracs) 
}

svolant's avatar
svolant committed
946