VisuPlot.R 35.3 KB
Newer Older
svolant's avatar
svolant committed
1
2
3
4
#@ This file contains all the functions for the 
#@ visualisation plots of SHAMAN


5
6
7
##                       ##
##        Barplot ####
##                       ##
svolant's avatar
svolant committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Plot_Visu_Barplot <- function(input,resDiff)
{
  
  ## Get Input for BarPlot
  VarInt = input$VisuVarInt
  ind_taxo = input$selectTaxoPlot
  
  tmp_combined = GetDataToPlot(input,resDiff,VarInt,ind_taxo)
  counts_tmp_combined = tmp_combined$counts
  nbKept = length(ind_taxo)
  SamplesNames = tmp_combined$namesCounts
  
  if(nbKept>1) namesTax = colnames(counts_tmp_combined)
22
  else if(nbKept==1) namesTax = ind_taxo
svolant's avatar
svolant committed
23
24
  
  dataNull = data.frame(x=c(0,0),y=c(1,2))
25
26

  plotd3 = nvd3Plot(x ~ y , data = dataNull, type = "multiBarChart", id = 'barplotTaxoNyll',height = input$heightVisu,width=if(input$modifwidthVisu){input$widthVisu})
svolant's avatar
svolant committed
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
  gg = NULL
  
  if(!is.null(counts_tmp_combined) && nrow(counts_tmp_combined)>0 && length(VarInt)>0)
  { 
    
    ## Create the data frame for the plot function
    dataBarPlot_mat = c()
    tmp_mat = matrix(0,ncol=3,nrow=nbKept)
    tmp_counts = c()
    
    for(i in 1:(nrow(counts_tmp_combined)))
    {
      ## Taxo
      tmp_mat[1:nbKept,1] = namesTax
      
      ## Counts
      
      tmpProp = counts_tmp_combined[i,]
      if(input$CountsOrProp=="prop")
      { 
        tmpProp = round(tmpProp/sum(tmpProp),3)
        tmpProp = as.numeric(tmpProp/sum(tmpProp) * 100)
      }
      tmp_counts = c(tmp_counts,tmpProp)      
      
      ## Meta data
      tmp_mat[1:nbKept,3] = as.character(rep(SamplesNames[i],nbKept))
      
      ## Conbined the sample
      dataBarPlot_mat = rbind(dataBarPlot_mat,tmp_mat)
    }
    
    
    ## Add numeric vector to the dataframe
    dataBarPlot_mat = as.data.frame(dataBarPlot_mat)
    
    colnames(dataBarPlot_mat) = c("Taxonomy","Proportions","AllVar")
    dataBarPlot_mat[,2] = tmp_counts
    if(input$SensPlotVisu == "Vertical") Sens = "multiBarChart"
66
    else Sens = "multiBarHorizontalChart"
svolant's avatar
svolant committed
67
    
68
69
70
    XRotate = input$rotateXLabel

    plotd3 <- nvd3Plot(Proportions ~ AllVar | Taxonomy, data = dataBarPlot_mat, type = Sens, id = 'barplotTaxo', height = input$heightVisu, width=if(input$modifwidthVisu){input$widthVisu})
svolant's avatar
svolant committed
71
    plotd3$chart(stacked = TRUE)
72
73
74
75
76
77
    if(input$SensPlotVisu == "Vertical") {
      plotd3$chart(reduceXTicks = FALSE)
      plotd3$xAxis(rotateLabels = XRotate)
    }
   
    
svolant's avatar
svolant committed
78
    
79
    ##                                 ##
svolant's avatar
svolant committed
80
    ## Same plot in ggplot2 for export
81
    ##                                 ##
svolant's avatar
svolant committed
82
83
84
85
86
    
    tax.colors=rep(c("#1f77b4","#aec7e8","#ff7f0e","#ffbb78", "#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5","#8c564b",
                     "#c49c94","#e377c2","#f7b6d2","#7f7f7f", "#c7c7c7","#bcbd22","#dbdb8d","#17becf","#9edae5"),ceiling(nbKept/20))
    
    dataBarPlot_mat$Taxonomy = factor(dataBarPlot_mat$Taxonomy,levels = namesTax)
svolant's avatar
svolant committed
87
    dataBarPlot_mat$AllVar = factor(dataBarPlot_mat$AllVar,levels = unique(dataBarPlot_mat$AllVar))
svolant's avatar
svolant committed
88
89
    
    gg= ggplot(dataBarPlot_mat, aes(x=AllVar, y=Proportions, fill=Taxonomy)) 
90
    gg= gg +geom_bar(position=input$positionBarPlot, stat="identity")
svolant's avatar
svolant committed
91
92
93
94
95
96
97
98
99
100
101
    gg= gg + theme_bw()+ scale_fill_manual(values=tax.colors)
    gg = gg +theme(panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank()) 
    if(input$CountsOrProp=="prop") gg = gg+labs(y="Relative abundance (%)",x="")
    if(input$CountsOrProp=="counts") gg = gg+labs(y="Abundance",x="")
    if(input$SensPlotVisu == "Horizontal") gg = gg + coord_flip()
  } 
  return(list(plotd3=plotd3,gg=gg))
}



102
103
104
##                       ##
##          HEATMAP ####
##                       ##
svolant's avatar
svolant committed
105
106
107
108
109
110
111
112
113
114
115
116
117
Plot_Visu_Heatmap <- function(input,resDiff,export=FALSE){
  
  VarInt = input$VisuVarInt
  ind_taxo = input$selectTaxoPlot
  
  counts_tmp_combined = GetDataToPlot(input,resDiff,VarInt,ind_taxo)$counts
  
  if(!is.null(counts_tmp_combined) && nrow(counts_tmp_combined)>0)
  { 
    ## Transform to log2
    counts_tmp_combined = log2(GetDataToPlot(input,resDiff,VarInt,ind_taxo)$counts+1)
    
    col <- switch(input$colors,
118
                  "green-blue"=colorRampPalette(brewer.pal(9,"GnBu"))(256),
svolant's avatar
svolant committed
119
120
121
122
123
124
125
                  "blue-white-red"=colorRampPalette(rev(brewer.pal(9, "RdBu")))(200),
                  "purple-white-orange"=colorRampPalette(rev(brewer.pal(9, "PuOr")))(200),
                  "red-yellow-green"= colorRampPalette(rev(brewer.pal(9,"RdYlGn")))(200))
    
    ## Transpose matrix if Horizontal
    if(input$SensPlotVisu=="Horizontal") counts_tmp_combined = t(as.matrix(counts_tmp_combined))
    
svolant's avatar
svolant committed
126
127
128
129
130
    if(!export) {plot = d3heatmap(counts_tmp_combined, dendrogram = "none", Rowv = (input$SortHeatRow == "Yes"), 
                                  Colv = (input$SortHeatColumn == "Yes"), na.rm = TRUE, width=ifelse(input$modifwidthVisu,input$widthVisu, "100%"), 
                                  height = input$heightVisu, show_grid = FALSE, colors = col, scale = input$scaleHeatmap, cexRow = as.numeric(input$LabelSizeHeatmap), margins=c(12,30), 
                                  cexCol=as.numeric(input$LabelSizeHeatmap), offsetCol=input$LabelColOffsetHeatmap, offsetRow=input$LabelRowOffsetHeatmap)
    }
131
132
133
134
135
136
    if(export){ 
      dendrogram="none"
      if(input$SortHeatColumn == "Yes" && input$SortHeatRow == "Yes" ) dendrogram ="both"
      else if(input$SortHeatColumn == "Yes") dendrogram ="column"
      else if(input$SortHeatRow == "Yes") dendrogram ="row"
      plot = heatmap.2(counts_tmp_combined, dendrogram = dendrogram, Rowv = (input$SortHeatRow == "Yes"), 
svolant's avatar
svolant committed
137
138
139
140
                                 Colv = (input$SortHeatColumn == "Yes"), na.rm = TRUE, density.info="none", margins=c(as.numeric(input$lowerMargin),as.numeric(input$rightMargin)),trace="none",
                                 srtCol=45, col = col, scale = input$scaleHeatmap, cexRow = input$LabelSizeHeatmap,cexCol =input$LabelSizeHeatmap, 
                                 offsetCol=input$LabelColOffsetHeatmap,offsetRow=input$LabelRowOffsetHeatmap,symm=FALSE,symkey=FALSE,symbreaks=FALSE)
    }
svolant's avatar
svolant committed
141
142
143
144
145
146
    return(plot)
  }
}



147
148
149
##                       ##
##          BOXPLOTS ####
##                       ##
svolant's avatar
svolant committed
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
Plot_Visu_Boxplot <- function(input,resDiff,alpha=0.7){
  
  gg = NULL
  
  ## Colors
  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(resDiff$target)/20))
  
  ## Get Input for BoxPlot
  VarInt = input$VisuVarInt
  ind_taxo = input$selectTaxoPlot
  
  tmp_merge = GetDataToPlot(input,resDiff,VarInt,ind_taxo,aggregate=FALSE)
  counts_tmp_combined = tmp_merge$counts
  levelsMod = tmp_merge$levelsMod
  nbKept = length(ind_taxo)
  
  if(!is.null(counts_tmp_combined) && nrow(counts_tmp_combined)>0 && !is.null(levelsMod))
  { 
    
    if(input$typeDataBox == "Relative") counts_tmp_combined = tmp_merge$prop_all
    else counts_tmp_combined = log2(counts_tmp_combined+1)
    if(nbKept==1) colnames(counts_tmp_combined)=ind_taxo
    
    ## Create the data frame for the plot function
    dataBarPlot_mat = c()
    tmp_mat = matrix(0,ncol=3,nrow=nbKept)
    tmp_counts = c()
    
    for(i in 1:(nrow(counts_tmp_combined)))
    {
      ## Taxo
      tmp_mat[1:nbKept,1] = colnames(counts_tmp_combined)
      
      ## Counts        
      tmpProp = counts_tmp_combined[i,]
      tmp_counts = c(tmp_counts,tmpProp)      
      
      ## Meta data
      tmp_mat[1:nbKept,3] = as.character(rownames(counts_tmp_combined)[i],nbKept)
      
      ## Conbined the sample
      dataBarPlot_mat = rbind(dataBarPlot_mat,tmp_mat)
    }
    
    
    dataBarPlot_mat = as.data.frame(dataBarPlot_mat)
    
    colnames(dataBarPlot_mat) = c("Taxonomy","Value","Samples")
    dataBarPlot_mat[,2] = tmp_counts
    
    if(is.null(input$BoxColorBy) || length(VarInt)<=1){ dataBarPlot_mat$Colors = dataBarPlot_mat$Samples}
    if(!is.null(input$BoxColorBy) && length(VarInt)>1)
    { 
      tmp = strsplit(as.character(dataBarPlot_mat$Samples),"-")
      ind = which(VarInt%in%input$BoxColorBy)
      dataBarPlot_mat$Colors = rapply(tmp, function(x) paste(x[ind],collapse ="-"), how = "unlist")
    }
    dataBarPlot_mat$Samples = factor(dataBarPlot_mat$Samples,levels=levelsMod)
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
209
    dataBarPlot_mat$Colors = factor(dataBarPlot_mat$Colors,levels=levelsMod)
svolant's avatar
svolant committed
210
211
212
213
214
215
216
217
218
219
220
221
    gg = ggplot(dataBarPlot_mat,aes(x=Samples,y=Value,fill=Colors))  + geom_boxplot(alpha=alpha) +theme_bw()
    gg = gg  +theme(axis.text=element_text(size=16,face="bold"),axis.title=element_text(size=18,face="bold"),panel.background = element_blank(),
                    panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.x=element_blank(), axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5)) 
    gg = gg + ylab(paste(input$typeDataBox, "abundance")) +scale_fill_manual(values = colors) + guides(fill=FALSE)
    if(input$CheckAddPointsBox) gg = gg + geom_point(position=position_jitterdodge(dodge.width=0.9))
    if(input$SensPlotVisu=="Horizontal") gg = gg + coord_flip()
    if(nbKept>1) gg = gg + facet_wrap(~ Taxonomy,scales = input$ScaleBoxplot)
  }
  
  return(gg)
}

222
223
224
##                       ##
##          KRONA ####
##                       ##
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
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
Plot_Visu_Krona <- function(input,resDiff,CT_OTU,taxo_table){
  
  res = NULL
  ## Get Input for Krona
  VarInt = input$VisuVarInt
  ind_taxo = input$selectTaxoPlot
  
  ## Removed column with only 1 modality
  ind = which(apply(taxo_table,2,FUN = function(x) length(unique(x[!is.na(x)])))==1)
  if(length(ind)>0) taxo_table = taxo_table[,-ind]
  
  #print(counts_tmp_combined)
  if(nrow(CT_OTU)>0 && !is.null(CT_OTU) && nrow(taxo_table)>0 && !is.null(taxo_table))
  { 
    tmp = CreateTableTree(input,resDiff,CT_OTU,taxo_table,VarInt)
    
    if(nrow(tmp$counts)>0 && !is.null(tmp$counts))
    {
      merge_dat = c()
      for(cond in tmp$levelsMod){
        merge_dat = rbind(merge_dat, cbind(cond, merge(round(tmp$counts[cond,]), taxo_table, by="row.names")))
      }
      # Reorder columns
      merge_dat=merge_dat[, c(3,1,4:dim(merge_dat)[2],2)]
      # Remove zero counts 
      # Required for Krona
      res = merge_dat[merge_dat[,1]>0,]
252
253
      #print(input$TaxoSelect)
      #print(res)
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
254
255
256
257
    }
  }
  return(res)
}
svolant's avatar
svolant committed
258

259
260
261
##                       ##
##      Phylo PLOT ####
##                       ##
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
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
Plot_Visu_Phylotree = function(input, resDiff, CT_OTU, taxo_table, treeseq){
  res = NULL
  VarInt = input$VisuVarInt
  ind_taxo = input$selectTaxoPlot
  
  ## Removed column with only 1 modality
  ind = which(apply(taxo_table,2,FUN = function(x) length(unique(x[!is.na(x)])))==1)
  if(length(ind)>0) taxo_table = taxo_table[,-ind]
  
  #print(counts_tmp_combined)
  if(nrow(CT_OTU)>0 && !is.null(CT_OTU) && nrow(taxo_table)>0 && !is.null(taxo_table))
  { 
    tmp = CreateTableTree(input,resDiff,CT_OTU,taxo_table,VarInt)
    
    if(nrow(tmp$counts)>0 && !is.null(tmp$counts))
    {
      #merge_dat = c()
      #for(cond in tmp$levelsMod){
      #  merge_dat = rbind(merge_dat, cbind(cond, merge(round(tmp$counts[cond,]), taxo_table, by="row.names")))
      #}
      # Reorder columns
      #merge_dat=merge_dat[, c(3,1,4:dim(merge_dat)[2],2)]
      # Remove zero counts 
      # Required for Krona
      #res = merge_dat[merge_dat[,1]>0,]
      counts=round(t(tmp$counts))
      data = as.data.frame(rbind(c("#name",colnames(counts)),cbind(rownames(counts),counts)))
      res = PhyloTreeMetaR(treeseq, data)
    }
  }
  #if(input$TransDataPhyloTree =="log2") data = cbind(target,log2(t(counts)+1),div)
  #else if(input$TransDataPhyloTree =="none") data = cbind(target,t(counts),div)
  #print(counts)
  return(res)
}
  
svolant's avatar
svolant committed
298

299
300
301
##                       ##
##      SCATTER PLOT ####
##                       ##
svolant's avatar
svolant committed
302
303
304
305
306
307
308
309
310
Plot_Visu_Scatterplot<- function(input,resDiff,export=FALSE,lmEst = FALSE,CorEst=FALSE){
  
  plot = NULL
  regCoef = NULL
  Rsq = NULL
  cor.est = NULL
  cor.pvalue = NULL
  div = NULL
  dds = resDiff$dds
311
312
313
314
315
316
  
  if(input$NormOrRaw=="norm")
  {counts = as.data.frame(round(counts(dds, normalized = TRUE)))}
  else
  {counts = as.data.frame(round(counts(dds, normalized = FALSE)))}

svolant's avatar
svolant committed
317
  target = as.data.frame(resDiff$target)
318
319
  #print("target")
  #print(target)
svolant's avatar
svolant committed
320
321
  ## Get the diversity values
  tmp_div = Plot_Visu_Diversity(input,resDiff,ForScatter=TRUE)$dataDiv
322
323
  #print("tmp_div")
  #print(tmp_div)
svolant's avatar
svolant committed
324
325
326
327
328
329
330
331
332
  
  if(!is.null(tmp_div)){
    div = cbind(round(tmp_div$value[tmp_div$diversity =="Alpha"],3),
                round(tmp_div$value[tmp_div$diversity =="Shannon"],3),
                round(tmp_div$value[tmp_div$diversity =="Inv.Simpson"],3),
                round(tmp_div$value[tmp_div$diversity =="Simpson"],3))
    colnames(div) = c("Alpha div","Shannon div","Inv.Simpson div","Simpson div")
  }
  if(input$TransDataScatter =="log2") data = cbind(target,log2(t(counts)+1),div)
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
333
  else if(input$TransDataScatter =="none") data = cbind(target,t(counts),div)
334

svolant's avatar
svolant committed
335
336
337
338
339
340
  ## Get Input for ScatterPlot
  Xvar = input$Xscatter
  Yvar = input$Yscatter
  ColBy = input$ColorBy
  PchBy = input$PchBy
  PointSize = input$PointSize
svolant's avatar
svolant committed
341
342
  NamesData = colnames(data)
  x_var = NULL; y_var = NULL; col_var = NULL; symbol_var = NULL; size_var = NULL
svolant's avatar
svolant committed
343
  
svolant's avatar
svolant committed
344
345
346
347
348
  if (!is.null(Xvar)) {if(Xvar%in%NamesData){x_var = data[,Xvar]}}
  if (!is.null(Yvar)) {if(Yvar%in%NamesData){ y_var = data[,Yvar]}}
  if (!is.null(ColBy)) {if(ColBy%in%NamesData){ col_var = data[,ColBy]}}
  if (!is.null(PchBy)) {if(PchBy%in%NamesData){ symbol_var = data[,PchBy]}}
  if (!is.null(PointSize)) {if(PointSize%in%NamesData){ size_var = data[,PointSize]}}
svolant's avatar
svolant committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  
  if(!export && !input$AddRegScatter && !lmEst && !CorEst && !is.null(x_var) && !is.null(y_var)){
    plot = scatterD3(x = x_var,
                     y = y_var,
                     lab = rownames(data),
                     xlab = Xvar,
                     ylab = Yvar,
                     col_var = col_var,
                     col_lab = ColBy,
                     symbol_var = symbol_var,
                     symbol_lab = PchBy,
                     size_var = size_var,
                     size_lab = PointSize,
                     key_var = rownames(data),
                     height = input$heightVisu,
364
                     width=if(input$modifwidthVisu){input$widthVisu},
svolant's avatar
svolant committed
365
366
367
368
369
370
371
372
373
374
375
376
377
                     point_opacity = 0.7,
                     labels_size = input$SizeLabelScatter,
                     transitions = TRUE)
    return(plot)
  }
  
  if(export || input$AddRegScatter){
    if(!lmEst && !CorEst){
      col_var = if (ColBy== "None" || is.null(ColBy)) 1 else data[,ColBy]
      symbol_var = if (PchBy == "None" || is.null(PchBy)) factor(rep(1,nrow(data))) else data[,PchBy]
      size_var = if (PointSize == "None" || is.null(PointSize))  1 else data[,PointSize]
      
      plot = ggplot(data, aes(x = x_var, y = y_var)) + geom_point(aes(color=col_var,size =size_var,shape = symbol_var),alpha=0.7) +theme_bw()
378
      if(input$AddRegScatter) plot = plot + geom_smooth(method="lm")
svolant's avatar
svolant committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
      if(input$SizeLabelScatter!=0) plot = plot + geom_text(aes(label=rownames(data),color=col_var,size=as.numeric(input$SizeLabelScatter)/10),vjust = 0,nudge_y =0.05)
      plot = plot + xlab(Xvar) + ylab(Yvar)
      
      return(plot)
    }
  }
  if(lmEst && !CorEst)
  {
    res = lm(y_var~x_var)
    sumRes = summary(res)
    regCoef = sumRes$coefficients 
    rownames(regCoef) = c("Intercept",Xvar)
    Rsq = sumRes$r.squared
    return(list(regCoef=regCoef,Rsq = Rsq))
  }
  if(CorEst)
  {
396
397
398
399
400
401
    #typesTarget = sapply(target,class)
    #print(typesTarget)
    #numInd = (typesTarget=="numeric")[1:ncol(target)]
    #print(numInd)
    typesTarget = sapply(target,is.numeric)
    numInd=which(typesTarget[2:ncol(target)])
svolant's avatar
svolant committed
402
403
404
    if(any(numInd)) data = cbind(target[,numInd],log2(t(counts)+1),div)
    if(!any(numInd)) data = cbind(log2(t(counts)+1),div)
    
405
    cor.est = round(cor(as.matrix(data),method = input$CorMeth),3)
svolant's avatar
svolant committed
406
407
408
409
410
411
412
    #cor.pvalue = cor.test(data,method = input$CorMeth)
    return(list(cor.est=cor.est))
  }
}



413
414
415
##                       ##
##      Diversity ####
##                       ##
svolant's avatar
svolant committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
Plot_Visu_Diversity <- function(input,resDiff,ForScatter=FALSE){
  gg = NULL
  dataTmp = NULL
  dds = resDiff$dds
  counts = round(counts(dds, normalized = TRUE))
  
  ## Get Input for the plot
  if(!ForScatter)
  {
    VarInt = input$VisuVarInt
    VarIntBoxDiv = input$VarBoxDiv 
    VarIntDivCol = input$VarDivCol
    ind_taxo = rownames(counts)
    tmp = GetDataToPlot(input,resDiff,VarInt,ind_taxo,aggregate=FALSE,rarefy = TRUE)
    counts_tmp_combined = tmp$counts
    targetInt = tmp$targetInt
    levelsMod = tmp$levelsMod
  }
  if(ForScatter)
  {
    counts_tmp_combined = t(counts)
    targetInt = resDiff$target
    targetInt$AllVar = targetInt[,1]
    levelsMod = NULL
  }
  
  if(nrow(counts_tmp_combined)>0 && !is.null(counts_tmp_combined) && !is.null(targetInt))
  { 
    sqrt.nb = sqrt(table(targetInt$AllVar))
445
    # save(counts_tmp_combined,targetInt,file = "testDiv.RData")
svolant's avatar
svolant committed
446
    alpha <- tapply(TaxoNumber(counts_tmp_combined), targetInt$AllVar, mean)
447
448
    ci.alpha.down = pmax(alpha - 1.96*tapply(TaxoNumber(counts_tmp_combined), targetInt$AllVar, sd)/sqrt.nb,0)
    ci.alpha.up = alpha + 1.96*tapply(TaxoNumber(counts_tmp_combined), targetInt$AllVar, sd)/sqrt.nb
svolant's avatar
svolant committed
449
    
450
451
452
    shan <- tapply(vegan::diversity(counts_tmp_combined, index = "shannon"), targetInt$AllVar, mean)
    ci.shan.down = pmax(shan - 1.96*tapply(vegan::diversity(counts_tmp_combined, index = "shannon"), targetInt$AllVar, sd)/sqrt.nb,0)
    ci.shan.up = shan + 1.96*tapply(vegan::diversity(counts_tmp_combined, index = "shannon"), targetInt$AllVar, sd)/sqrt.nb
svolant's avatar
svolant committed
453
    
454
455
456
    simpson <- tapply(vegan::diversity(counts_tmp_combined, index = "simpson"), targetInt$AllVar, mean)
    ci.simpson.down = pmax(simpson - 1.96*tapply(vegan::diversity(counts_tmp_combined, index = "simpson"), targetInt$AllVar, sd)/sqrt.nb,0)
    ci.simpson.up = simpson + 1.96*tapply(vegan::diversity(counts_tmp_combined, index = "simpson"), targetInt$AllVar, sd)/sqrt.nb
svolant's avatar
svolant committed
457
    
458
459
460
    invsimpson <- tapply(vegan::diversity(counts_tmp_combined, index = "invsimpson"), targetInt$AllVar, mean)
    ci.invsimpson.down = pmax(invsimpson - 1.96*tapply(vegan::diversity(counts_tmp_combined, index = "invsimpson"), targetInt$AllVar, sd)/sqrt.nb,0)
    ci.invsimpson.up = invsimpson + 1.96*tapply(vegan::diversity(counts_tmp_combined, index = "invsimpson"), targetInt$AllVar, sd)/sqrt.nb
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    
    gamma <- TaxoNumber(counts_tmp_combined, targetInt$AllVar)
    beta = gamma/alpha - 1
    nb = length(alpha)
    
    dataTmp = data.frame(value=c(alpha,beta,gamma,shan,simpson,invsimpson),
                         ci.down=c(ci.alpha.down,beta,gamma,ci.shan.down,ci.simpson.down,ci.invsimpson.down),
                         ci.up=c(ci.alpha.up,beta,gamma,ci.shan.up,ci.simpson.up,ci.invsimpson.up),
                         diversity = c(rep("Alpha",nb),rep("Beta",nb),rep("Gamma",nb),rep("Shannon",nb),rep("Simpson",nb),rep("Inv.Simpson",nb)),
                         Var = as.character(rep(names(alpha),6)))
    
    if(!ForScatter)
    {                  
      dataTmp = dataTmp[dataTmp$diversity%in%input$WhichDiv,]
      
      ## Order of the modalities
      dataTmp$Var = factor(dataTmp$Var,levels = levelsMod)
      
      tmp.mat = matrix(unlist((lapply(as.matrix(as.character(dataTmp$Var)),strsplit,"-"))),ncol=length(VarInt),byrow = T)
      tmp.level = matrix(unlist((lapply(as.matrix(as.character(levelsMod)),strsplit,"-"))),ncol=length(VarInt),byrow = T)
      
      indVar = VarInt%in%VarIntBoxDiv
      if(length(which(indVar))>=1){
        if(length(which(indVar))>=2){
          tmp.levelX = apply(tmp.level[,which(indVar)],1,paste,collapse = "-")
          dataTmp$VarX = factor(apply(tmp.mat[,which(indVar)],1,paste,collapse = "-"),levels = unique(tmp.levelX))
        }
        if(length(which(indVar))==1){
          tmp.levelX = tmp.level[,which(indVar)]
          dataTmp$VarX = factor(tmp.mat[,which(indVar)],levels = unique(tmp.levelX))
        }
      }
      
      if(is.null(VarIntBoxDiv)) dataTmp$VarX = tmp.mat[,1]
      dataTmp$VarCol = dataTmp$VarX
      
      if(length(which(!indVar))>=1){
        if(length(which(!indVar))>=2){
          tmp.levelCol = apply(tmp.level[,which(!indVar)],1,paste,collapse = "-")
          dataTmp$VarCol = factor(apply(tmp.mat[,which(!indVar)],1,paste,collapse = "-"),levels = unique(tmp.levelCol))
        }
        if(length(which(!indVar))==1){ 
          tmp.levelCol = tmp.level[,which(!indVar)]
          dataTmp$VarCol = factor(tmp.mat[,which(!indVar)],levels = unique(tmp.levelCol))
        }
      }
      
      
      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(targetInt)/20))
      
      gg = ggplot(dataTmp, aes(x=VarX, y=value, fill=VarCol)) 
      gg = gg + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5), legend.title=element_blank())
      gg = gg + geom_bar(stat = "identity",width=0.4,position = position_dodge(width=0.5),alpha=0.8) 
      if(input$DivAddError=="Add") gg = gg + geom_errorbar(aes(ymin=ci.down, ymax=ci.up,color=VarCol,width=.2),position = position_dodge(width=0.5))
      if(input$SensPlotVisu=="Horizontal") gg = gg + coord_flip() + facet_wrap(~ diversity,scales="fixed")
      if(input$SensPlotVisu=="Vertical") gg = gg + facet_wrap(~ diversity,scales=input$DivScale)
      gg = gg + xlab(paste(VarIntBoxDiv,collapse ="-"))+ ylab("Diversity")
      gg = gg + scale_fill_manual(values = colors[1:length(unique(dataTmp[,7]))]) + scale_color_manual(values = colors[1:length(unique(dataTmp[,7]))])
    }
    
    ## Get interactivity
    #ff = ggplotly(gg)
  }
525
  
svolant's avatar
svolant committed
526
527
528
529
530
  return(list(plot=gg,dataDiv = dataTmp))
  
}


531
532
533
##                       ##
##       RAREFACTION ####
##                       ##
svolant's avatar
svolant committed
534
535
536
537
538
539
Plot_Visu_Rarefaction <- function(input,resDiff,xlim,ylim,ylab="Species"){
  
  PlotRare = NULL
  dds = resDiff$dds
  
  ## Taxo in columns
540
541
542
543
544
  
  if(input$NormOrRaw=="norm")
  {counts = t(round(counts(dds, normalized = TRUE)))}
  else
  {counts = t(round(counts(dds, normalized = FALSE)))}
svolant's avatar
svolant committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
  
  if(nrow(counts)>0 && !is.null(counts))
  { 
    max <- max(rowSums(counts))
    raremax <- min(rowSums(counts))
    #PlotRare = rarefaction_curve(counts, step = 10, taxo = "Taxonomy level") 
    options(warn=-1)
    PlotRare = rarecurve(counts, step = max(1,ceiling(max/60)),sample=raremax, col = "blue", cex = 0.9,xlim=xlim,ylim=ylim, ylab=ylab) 
    options(warn=0)
  }
  return(PlotRare)
}




561
##                                      ##
svolant's avatar
svolant committed
562
##
563
##          Useful functions ####
svolant's avatar
svolant committed
564
##
565
##                                      ##
svolant's avatar
svolant committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

## Get the non-zero taxo by sample  
TaxoNumber <-  function (x, groups, mar = 1) 
{
  if (!missing(groups)) 
  {
    if (length(groups) == 1) groups = rep(groups, nrow(x))
    x = aggregate(x, list(groups), max)
    rownames(x) = x[, 1]; x = x[, -1]
  }
  if (length(dim(x)) > 1) res = apply(x > 0, mar, sum)
  else res = sum(x > 0)
}


## Modified version of expand.grid
expand.grid2.list <- function(listInput)
{
  n = length(listInput)
  if(is.list(listInput) && n>1)
  {
    l1 = listInput[[1]]
    l2 = listInput[[2]]
    res = c()
    
    for(i in l1){
      for(j in l2){ 
        res = rbind(res,paste(i,j,sep = "-"))
      }
    }
    listInput[[1]] = res
    listInput = listInput[-2]
    if(length(listInput)>1 && is.list(listInput)) res = expand.grid2.list(listInput)
  }
  else res = listInput
  return(res)
}


## Put the data in the right format to be plot
606
GetDataToPlot <- function(input,resDiff,VarInt,ind_taxo,sec_variable = NULL, aggregate=TRUE,rarefy=FALSE)
svolant's avatar
svolant committed
607
{
608
609
610
611
  sec_variable_added_to_VarInt <- FALSE
  if(!is.null(sec_variable)){if(!is.element(sec_variable,VarInt)){VarInt <- c(VarInt,sec_variable)
                                                                  sec_variable_added_to_VarInt <- TRUE}}
  
svolant's avatar
svolant committed
612
613
614
  dds = resDiff$dds
  val = c()
  list.val = list()
615
616
617
618
  if(input$NormOrRaw=="norm")
  {counts = as.data.frame(round(counts(dds, normalized = TRUE)))}
  else
  {counts = as.data.frame(round(counts(dds, normalized = FALSE)))}
svolant's avatar
svolant committed
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
  if(rarefy) {set.seed(1234); counts = t(rrarefy(t(counts), min(colSums(counts))))}
  
  target = resDiff$target
  counts_tmp_combined = NULL
  prop_tmp_combined = NULL
  targetInt = NULL
  namesCounts = NULL
  levelsMod = NULL
  prop_all=NULL
  ## Select a subset within the taxonomy level (default is the 12 most abundant)
  nbKept = length(ind_taxo)
  Taxonomy = rownames(counts)
  
  if (length(VarInt)>0 && nbKept>0)
  { 
    
    ## Get the modalities to keep
    for(i in 1:length(VarInt))
    { 
      ## Replace "-" by "." 
      target[,VarInt[i]] =  gsub("-",".",target[,VarInt[i]])
      
      Tinput = paste("input$","ModVisu",VarInt[i],sep="")
      expr=parse(text=Tinput)
      ## All the modalities for all the var of interest
      val = c(val,eval(expr))
645
646
647
648
      if(sec_variable_added_to_VarInt){mod <- eval(expr)
                                      if(is.null(mod)){mod <- as.character(unique(as.factor(target[,VarInt[i]])))}
                                      list.val[[i]] = mod}
      else{list.val[[i]] = eval(expr)}
svolant's avatar
svolant committed
649
650
651
652
653
654
    }
    if (!is.null(val) && !is.null(list.val))
    {
      
      ## Create the variable to plot
      targetInt = as.data.frame(target[,VarInt])
svolant's avatar
svolant committed
655
      rownames(targetInt)=rownames(target) 
svolant's avatar
svolant committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
      ## Combining the Varint
      if(length(VarInt)>1){targetInt$AllVar = apply(targetInt,1,paste, collapse = "-"); targetInt$AllVar = factor(targetInt$AllVar,levels =  expand.grid2.list(list.val))}
      if(length(VarInt)<=1){targetInt$AllVar = target[,VarInt]; targetInt$AllVar = factor(targetInt$AllVar,levels = val)}
      colnames(targetInt) = c(VarInt,"AllVar")
      
      ## Keep only the selected modalities
      ind_kept = which(!is.na(targetInt$AllVar))
      targetInt = targetInt[ind_kept,]
      
      levelsMod = levels(targetInt$AllVar)
      
      ## Create the counts matrix only for the selected subset
      counts_tmp = counts[Taxonomy%in%ind_taxo,]
      counts_tmp = counts_tmp[,colnames(counts_tmp)%in%rownames(targetInt)]
      
      ## Proportions over all the taxonomies
672
      ## Proportion verified
svolant's avatar
svolant committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
      prop_all = t(counts)/rowSums(t(counts))
      prop_all = as.data.frame(prop_all[,Taxonomy%in%ind_taxo])
      prop_all = as.matrix(prop_all[rownames(prop_all)%in%rownames(targetInt),])
      rownames(prop_all) = targetInt$AllVar
      
      ## Be careful transposition !
      if(aggregate && nrow(counts_tmp)>0 && nrow(targetInt)>0)
      { 
        counts_tmp_combined = aggregate(t(counts_tmp),by=list(targetInt$AllVar),mean)
        rownames(counts_tmp_combined) = counts_tmp_combined$Group.1
        namesCounts = counts_tmp_combined$Group.1
        counts_tmp_combined = as.matrix(counts_tmp_combined[,-1])
      }
      if(!aggregate && nrow(counts_tmp)>0 && nrow(targetInt)>0)
      {  
688
        ## Proportion verified
svolant's avatar
svolant committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
        counts_tmp_combined = t(counts_tmp)
        prop_tmp_combined = counts_tmp_combined/colSums(counts_tmp)
        rownames(counts_tmp_combined) = targetInt$AllVar
        namesCounts = targetInt$AllVar
        rownames(prop_tmp_combined) = targetInt$AllVar
      }
      
      ## Ordering the counts
      if(!is.null(counts_tmp_combined))
      {
        MeanCounts = apply(counts_tmp_combined,2,mean)
        ord = order(MeanCounts,decreasing=TRUE)
        counts_tmp_combined = as.matrix(counts_tmp_combined[,ord])
        if(!aggregate) prop_tmp_combined = as.matrix(prop_tmp_combined[,ord])
        prop_all = as.matrix(prop_all[,ord])
      }
    }
  }
707
  return(list(counts=counts_tmp_combined,targetInt=targetInt,prop=prop_tmp_combined,namesCounts=namesCounts,levelsMod=levelsMod,prop_all=prop_all))
svolant's avatar
svolant committed
708
709
710
711
  
}


svolant's avatar
svolant committed
712
713
714
715
716
717
718
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
## Create the data table for the tree representation
CreateTableTree <- function(input,resDiff,CT_Norm_OTU,taxo_table,VarInt,ind_taxo=rownames(CT_Norm_OTU))
{
  dds = resDiff$dds
  val = c()
  list.val = list()
  counts = CT_Norm_OTU
  target = resDiff$target
  counts_tmp_combined = NULL
  prop_tmp_combined = NULL
  targetInt = NULL
  namesCounts = NULL
  levelsMod = NULL
  ## Select a subset within the taxonomy level (default is the 12 most abundant)
  nbKept = length(ind_taxo)
  Taxonomy = rownames(counts)
  
  if (length(VarInt)>0 && nbKept>0)
  { 
    
    ## Get the modalities to keep
    for(i in 1:length(VarInt))
    { 
      ## Replace "-" by "." 
      target[,VarInt[i]] =  gsub("-",".",target[,VarInt[i]])
      
      Tinput = paste("input$","ModVisu",VarInt[i],sep="")
      expr=parse(text=Tinput)
      ## All the modalities for all the var of interest
      val = c(val,eval(expr))
      list.val[[i]] = eval(expr)
    }
    if (!is.null(val) && !is.null(list.val))
    {
svolant's avatar
svolant committed
746
      # save(val,list.val,VarInt,target,Taxonomy,counts,ind_taxo,file="testTree.RData")
svolant's avatar
svolant committed
747
748
749
750
751
752
      
      ## Create the variable to plot
      targetInt = as.data.frame(target[,VarInt])
      rownames(targetInt)=rownames(target)  
      ## Combining the Varint
      if(length(VarInt)>1){targetInt$AllVar = apply(targetInt,1,paste, collapse = "-"); targetInt$AllVar = factor(targetInt$AllVar,levels =  expand.grid2.list(list.val))}
753
      else{targetInt$AllVar = target[,VarInt]; targetInt$AllVar = factor(targetInt$AllVar,levels = val)}
svolant's avatar
svolant committed
754
755
756
757
758
759
760
761
762
763
764
765
766
      colnames(targetInt) = c(VarInt,"AllVar")
      
      ## Keep only the selected modalities
      ind_kept = which(!is.na(targetInt$AllVar))
      targetInt = targetInt[ind_kept,]
      
      levelsMod = levels(targetInt$AllVar)
      
      ## Create the counts matrix only for the selected subset
      counts_tmp = counts[Taxonomy%in%ind_taxo,]
      counts_tmp = counts_tmp[,colnames(counts_tmp)%in%rownames(targetInt)]
      
      ## Be careful transposition !
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
767
      # Group per condition
svolant's avatar
svolant committed
768
769
770
771
      if(nrow(counts_tmp)>0 && nrow(targetInt)>0)
      { 
        counts_tmp_combined = aggregate(t(counts_tmp),by=list(targetInt$AllVar),mean)
        namesCounts = counts_tmp_combined$Group.1
svolant's avatar
svolant committed
772
773
        rownames(counts_tmp_combined) = namesCounts
        
svolant's avatar
svolant committed
774
775
776
777
778
        counts_tmp_combined = as.matrix(counts_tmp_combined[,-1])
      }

      
      ## Ordering the counts
svolant's avatar
svolant committed
779
780
781
782
783
784
785
#       if(!is.null(counts_tmp_combined))
#       {
#         MeanCounts = apply(counts_tmp_combined,2,mean)
#         ord = order(MeanCounts,decreasing=TRUE)
#         counts_tmp_combined = as.matrix(counts_tmp_combined[,ord])
#         
#       }
svolant's avatar
svolant committed
786
787
788
789
790
791
792
793
794
795
796
797
      
    }
  }
  
  return(list(counts = counts_tmp_combined,targetInt=targetInt,namesCounts=namesCounts,levelsMod=levelsMod))
  
  
}




798
##                       ##
svolant's avatar
svolant committed
799
##        Tree
800
##                       ##
svolant's avatar
svolant committed
801
802
803
804
805
806
807
808
809

## The count matrix must be given at the leaf level.

Plot_Visu_Tree <- function(input,resDiff,CT_Norm_OTU,taxo_table)
{
  res = NULL
  ## Get Input for BarPlot
  VarInt = input$VisuVarInt
  ind_taxo = input$selectTaxoPlot
svolant's avatar
svolant committed
810
  nodeFind = input$TaxoTree
svolant's avatar
svolant committed
811
812
813
814
815
816
817
818
  ## Removed column with only 1 modality
  ind = which(apply(taxo_table,2,FUN = function(x) length(unique(x[!is.na(x)])))==1)
  if(length(ind)>0) taxo_table = taxo_table[,-ind]
  # tmp_combined = GetDataToPlot(input,resDiff,VarInt,ind_taxo,CT_Norm_OTU=CT_Norm_OTU)
  
  if(nrow(CT_Norm_OTU)>0 && !is.null(CT_Norm_OTU) && nrow(taxo_table)>0 && !is.null(taxo_table))
  { 
    tmp = CreateTableTree(input,resDiff,CT_Norm_OTU,taxo_table,VarInt)
svolant's avatar
svolant committed
819
  
820
    if(nrow(tmp$counts)>0 && !is.null(tmp$counts))
svolant's avatar
svolant committed
821
    {
Amine  GHOZLANE's avatar
Amine GHOZLANE committed
822
      #save(tmp,taxo_table,nodeFind,file="testTree.RData")
svolant's avatar
svolant committed
823
824
825
826
      merge_dat = merge(taxo_table,round(t(tmp$counts)),by="row.names")
      colnames(merge_dat)[1] = "OTU"
      levels <- c("OTU", colnames(taxo_table))
      conditions <- rownames(tmp$counts)
827
828
829
      #nodeFind = input$TaxoTree
      nodeFind =input$selectTaxoPlot
      if(length(input$selectTaxoPlot) == 0) nodeFind = NULL
830
      res = treeWeightD3(merge_dat,conditions,levels,nodeFind=nodeFind, height =input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
svolant's avatar
svolant committed
831
832
833
834
835
    }
  }
  return(res)
}

836
##                       ##
837
##      NETWORK
838
##                       ##
839
840
841
842
843
844
845
846
847
848
849

Plot_network <- function(input,resDiff,availableTaxo, ind_taxo, qualiVariable, export = FALSE){
  plot = NULL
  dataVN = NULL
  
  VarInt = input$VisuVarInt
  
  if(isolate(input$colorCorr)){sec_variable = isolate(input$sec_variable)}
  else{sec_variable = NULL}
  
  data <- GetDataToPlot(input,resDiff,VarInt,availableTaxo, sec_variable = sec_variable, aggregate = FALSE)
850
  if(!is.null(data) && !is.null(data$targetInt)){
851
852
  counts_tmp_combined <- data$counts
  dataVariables <- as.matrix(data$targetInt)
853
  if(isolate(input$colorCorr && qualiVariable()) && !is.null(dataVariables)){dataVariables[,sec_variable] <- sapply(dataVariables[,sec_variable], function(x) if(is.element(x,isolate(input$values1))){1}else{0})}
854
855
856
857
858
859
860
861
862
863
864
865
866
  
  if(!is.null(counts_tmp_combined)){
    countsMatrix <- as.matrix(counts_tmp_combined)
    
    n <- ncol(countsMatrix)
    resCorrTest <- corr.test(countsMatrix, ci = FALSE)
    cor <- resCorrTest$r
    pval <- resCorrTest$p
    pval_bool <- t(apply(pval, 1, function(v) {sapply(v, function(x){x < 0.05})}))
    cor_sgn <- t(apply(cor, 1, function(v) {sapply(v, sign)}))
    adjacency <- matrix(mapply(function(a,b) {mapply(function(x,y){x*y}, x=a, y=b)}, a=pval_bool, b=cor_sgn), nrow = n)
    rownames(adjacency) <- colnames(countsMatrix)
    colnames(adjacency) <- colnames(countsMatrix)
867
    # ### Remove rows and columns with only NA        # this way, elements with the same count in all sample (often 0 in this case) will not appear
868
869
870
871
872
    # adjacency <- adjacency[apply(adjacency, 1, function(y) !all(is.na(y))),]
    # adjacency <- t(adjacency)
    # adjacency <- adjacency[apply(adjacency, 1, function(y) !all(is.na(y))),]
    # adjacency <- t(adjacency)
    
873
    ### Replace NA by zeros (ie "no correlation")     # this way, those elements will appear as single nodes
874
875
876
877
878
    adjacency[is.na(adjacency)] <- 0
    
    adjacency <- adjacency[,ind_taxo]
    adjacency <- adjacency[ind_taxo,]
    
879
    igraphGraph <- graph_from_adjacency_matrix(adjacency, diag = FALSE, mode = "upper" , weighted = TRUE) # mode = "upper" for adjusted p-value, mode = "lower" for p-value not adjusted
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
    
    list_to_label <- isolate(input$ToLabelNetwork)
    dataVN <- toVisNetworkData(igraphGraph)
    dataVN$nodes$title <- paste0("<b>", dataVN$nodes$id,"</b>")
    dataVN$nodes$label <- sapply(dataVN$nodes$id, function(x)if(is.element(x, list_to_label)){x}else{""})
    dataVN$edges$color <- sapply(dataVN$edges$weight, function(x)if(x==1){isolate(input$edgeColorPositive)}else{isolate(input$edgeColorNegative)})
    
    if(!is.null(sec_variable)){
        cor <- sapply(dataVN$nodes$id, function(x)cor(as.numeric(dataVariables[,sec_variable]), countsMatrix[,x]))
        dataVN$nodes$cor <- round(cor, digits = 5)
        scale <- if(isolate(input$scaleFree)){max(c(max(dataVN$nodes$cor),-min(dataVN$nodes$cor)))}else{1}
        dataVN$nodes$color.background <- sapply(dataVN$nodes$cor, function(x) colorRampPalette(rev(brewer.pal(9, isolate(input$colorPalette))))(200)[round(x, digits = 2)*100/scale+100])
        dataVN$nodes$color.highlight.background <- dataVN$nodes$color.background
    }
    else{dataVN$nodes$color.background <- isolate(input$colorBackground)
         dataVN$nodes$color.highlight.background <- isolate(input$colorHighlightBackground)}
    dataVN$nodes$color.border <- isolate(input$colorBorder)
    dataVN$nodes$color.highlight.border <- isolate(input$colorHighlightBorder)
    
    plot <- visNetwork(nodes = dataVN$nodes, edges = dataVN$edges)
    plot <- visIgraphLayout(plot, layout = "layout_nicely", physics = FALSE, smooth = FALSE)
901
    plot <- visNodes(plot, size = 20)
902
903
904
905
906
907
908
909
910
    plot <- visEdges(plot, width = 1)
    plot <- visOptions(plot, width = if(isolate(input$modifwidthVisu)){isolate(input$widthVisu)}, height = isolate(input$heightVisu), autoResize = FALSE)
    #plot <- visLegend(plot, addEdges = data.frame(color = c("red", "blue"), label = c("Positive correlation","Negative correlation")))
    
    #plot <- visExport(plot, type = "pdf", name = "network_SHAMAN.pdf", float="bottom")
  }
  }
  return(list(plot = plot, data = dataVN))
}