Commit f1d12a47 authored by Amine  GHOZLANE's avatar Amine GHOZLANE
Browse files

Add Krona and phylogeny plot

parent 168fa0ca
......@@ -159,7 +159,6 @@ if(!require(PhyloTreeMetaR)){
library(PhyloTreeMetaR)
}
# if(!require(plotly)){
# install.packages("plotly")
# library(plotly)
......
......@@ -209,7 +209,80 @@ Plot_Visu_Boxplot <- function(input,resDiff,alpha=0.7){
return(gg)
}
##############################
## KRONA
##############################
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,]
}
}
return(res)
}
##############################
## Phylo PLOT
##############################
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)
}
##############################
## SCATTER PLOT
......@@ -241,7 +314,7 @@ Plot_Visu_Scatterplot<- function(input,resDiff,export=FALSE,lmEst = FALSE,CorEst
colnames(div) = c("Alpha div","Shannon div","Inv.Simpson div","Simpson div")
}
if(input$TransDataScatter =="log2") data = cbind(target,log2(t(counts)+1),div)
if(input$TransDataScatter =="none") data = cbind(target,t(counts),div)
else if(input$TransDataScatter =="none") data = cbind(target,t(counts),div)
## Get Input for ScatterPlot
......@@ -669,13 +742,16 @@ CreateTableTree <- function(input,resDiff,CT_Norm_OTU,taxo_table,VarInt,ind_taxo
counts_tmp = counts_tmp[,colnames(counts_tmp)%in%rownames(targetInt)]
## Be careful transposition !
# Group per condition
if(nrow(counts_tmp)>0 && nrow(targetInt)>0)
{
print(t(counts_tmp)[1:2,])
counts_tmp_combined = aggregate(t(counts_tmp),by=list(targetInt$AllVar),mean)
namesCounts = counts_tmp_combined$Group.1
rownames(counts_tmp_combined) = namesCounts
counts_tmp_combined = as.matrix(counts_tmp_combined[,-1])
print(counts_tmp_combined[1:2,])
}
......
......@@ -37,6 +37,7 @@ shinyServer(function(input, output,session) {
colnames(data) = gsub("-",".",colnames(data))
## Rownames
if(!TRUE%in%duplicated(data[,1])) rownames(data)=data[,1];data=data[,-1]
try(round(data, 0)->data, silent=T)
}
return(as.data.frame(data))
})
......@@ -416,8 +417,8 @@ shinyServer(function(input, output,session) {
menuItem("Visualization",icon = icon("area-chart"),
menuSubItem("Global views",tabName="GlobVisu"),
menuSubItem("Comparison plots",tabName="CompPlot"),
tabName = "Visu"),
menuItem("Perspective plots", icon = icon("pie-chart"), tabName = "Krona")
tabName = "Visu")
#menuItem("Perspective plots", icon = icon("pie-chart"), tabName = "Krona")
)
} else{ sidebarMenu(id = "side",NULL)}
......@@ -737,7 +738,8 @@ shinyServer(function(input, output,session) {
## Box for merged counts
output$BoxCountsMerge <- renderUI({
input$RunDESeq
counts = isolate(dataMergeCounts()$counts)
#counts = isolate(dataMergeCounts()$counts)
counts = isolate(dataMergeCounts()$CT_Norm)
taxo = input$TaxoSelect
if(!is.null(counts) && taxo != "...")
......@@ -754,13 +756,14 @@ shinyServer(function(input, output,session) {
## Export in .csv
output$ExportCounts <- downloadHandler(
filename = function() { 'NormCounts.csv' },
content = function(file){write.csv(dataMergeCounts()$counts, file)}
#content = function(file){write.csv(dataMergeCounts()$counts, file)}
content = function(file){write.csv(dataMergeCounts()$CT_Norm, file)}
)
## Export in .csv
output$ExportRelative <- downloadHandler(
filename = function() { 'RelativeAb.csv' },
content = function(file){write.csv(sweep(dataMergeCounts()$counts,2,colSums(dataMergeCounts()$counts),`/`), file)}
content = function(file){write.csv(sweep(dataMergeCounts()$CT_Norm,2,colSums(dataMergeCounts()$CT_Norm),`/`), file)}
)
## Export size factors
......@@ -1887,6 +1890,21 @@ shinyServer(function(input, output,session) {
##
#####################################################
output$PhyloTreeMetaR2 <- renderPhyloTreeMetaR({
resDiff = ResDiffAnal()
taxo_table = dataInput()$data$taxo
treeseq = dataInputTree()$treeseq
if(!is.null(resDiff$dds) && length(input$VisuVarInt)>=1 )
if(input$NormOrRaw=="norm") withProgress(message="Loading...", Plot_Visu_Phylotree(input, resDiff, dataMergeCounts()$CT_Norm, taxo_table, treeseq))
else withProgress(message="Loading...", Plot_Visu_Phylotree(input, resDiff, dataMergeCounts()$CT_noNorm, taxo_table, treeseq))
})
output$PlotVisuTree <- renderTreeWeightD3({
resDiff = ResDiffAnal()
taxo_table = dataInput()$data$taxo
......@@ -1900,8 +1918,22 @@ shinyServer(function(input, output,session) {
return(res)
})
KronaR =function(){
resDiff = ResDiffAnal()
taxo_table = dataInput()$data$taxo
res = NULL
if(!is.null(resDiff$dds) && length(input$VisuVarInt)>=1){
if(input$NormOrRaw=="norm") res = Plot_Visu_Krona(input,resDiff,dataMergeCounts()$CT_Norm,taxo_table)
else res = Plot_Visu_Krona(input,resDiff,dataMergeCounts()$CT_noNorm,taxo_table)
}
temp = tempfile(pattern = "file", tmpdir = tempdir(), fileext =".tsv")
write.table(res, file=temp, quote=F, sep="\t", row.names =F, col.names=F)
return(temp)
}
output$PlotVisuBar <- renderChart({
resDiff = ResDiffAnal()
res = NULL
......@@ -2087,17 +2119,18 @@ shinyServer(function(input, output,session) {
output$plotVisu <- renderUI({
res=NULL
if(input$PlotVisuSelect=="Barplot") res = showOutput("PlotVisuBar")
if(input$PlotVisuSelect=="Heatmap") res = d3heatmapOutput("heatmap", height = input$heightVisu+10, width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"))
if(input$PlotVisuSelect=="Boxplot") res = plotOutput("Boxplot", height = input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
if(input$PlotVisuSelect=="Tree") res = treeWeightD3Output('PlotVisuTree', height = input$heightVisu+10,width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"))
if(input$PlotVisuSelect=="Scatterplot" && !input$AddRegScatter) res = scatterD3Output("ScatterplotD3", height = input$heightVisu+10, width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"))
if(input$PlotVisuSelect=="Scatterplot" && input$AddRegScatter) res = plotOutput("Scatterplotgg", height = input$heightVisu+10,width=if(input$modifwidthVisu){input$widthVisu})
if(input$PlotVisuSelect=="Diversity") res = plotOutput("DiversityPlot", height = input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
if(input$PlotVisuSelect=="Rarefaction") res = plotOutput("RarefactionPlot",dblclick = "RarefactionPlot_dblclick",brush = brushOpts(id = "RarefactionPlot_brush",resetOnNew = TRUE), height = input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
else if(input$PlotVisuSelect=="Heatmap") res = d3heatmapOutput("heatmap", height = input$heightVisu+10, width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"))
else if(input$PlotVisuSelect=="Boxplot") res = plotOutput("Boxplot", height = input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
else if(input$PlotVisuSelect=="Krona") res= tags$iframe(src=paste0("http://127.0.0.1:5438/?parameter=",KronaR()), height = input$heightVisu+10, width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"), seamless=NA)
else if(input$PlotVisuSelect=="Tree") res = treeWeightD3Output('PlotVisuTree', height = input$heightVisu+10,width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"))
else if(input$PlotVisuSelect=="Scatterplot" && !input$AddRegScatter) res = scatterD3Output("ScatterplotD3", height = input$heightVisu+10, width=ifelse(input$modifwidthVisu,input$widthVisu,"100%"))
else if(input$PlotVisuSelect=="Scatterplot" && input$AddRegScatter) res = plotOutput("Scatterplotgg", height = input$heightVisu+10,width=if(input$modifwidthVisu){input$widthVisu})
else if(input$PlotVisuSelect=="Diversity") res = plotOutput("DiversityPlot", height = input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
else if(input$PlotVisuSelect=="Rarefaction") res = plotOutput("RarefactionPlot",dblclick = "RarefactionPlot_dblclick",brush = brushOpts(id = "RarefactionPlot_brush",resetOnNew = TRUE), height = input$heightVisu+10, width=if(input$modifwidthVisu){input$widthVisu})
else if(input$PlotVisuSelect=="Phylogeny") res = PhyloTreeMetaROutput('PhyloTreeMetaR2')
#print(res)
return(res)
})
......@@ -2311,7 +2344,7 @@ shinyServer(function(input, output,session) {
resDiff = ResDiffAnal()
res = list()
namesTarget = colnames(target)[2:ncol(target)]
if(!is.null(data$counts) && !is.null(data$taxo) && nrow(data$counts)>0 && nrow(data$taxo)>0 && !is.null(taxo) && taxo!="..." && !is.null(target))
{
counts = dataMergeCounts()$counts
......@@ -2370,21 +2403,24 @@ shinyServer(function(input, output,session) {
## KRONA
##
#####################################################
output$krona <- renderTable({
data = dataInput()$data
taxo = input$TaxoSelect
if(!is.null(data$counts) && !is.null(data$taxo) && nrow(data$counts)>0 && nrow(data$taxo)>0 && !is.null(taxo) && taxo!="...")
{
#output$kronar <- renderTable({
# data = dataInput()$data
# taxo = input$TaxoSelect
# if(!is.null(data$counts) && !is.null(data$taxo) && nrow(data$counts)>0 && nrow(data$taxo)>0 && !is.null(taxo) && taxo!="...")
# {
# print(counts)
# print(data)
#KronaR(dat)
#print(data$counts)
krona_table=tempfile(pattern = "krona", tmpdir = tempdir(), fileext = "")
url=paste(krona_table, ".html", sep="")
#krona_table=tempfile(pattern = "krona", tmpdir = tempdir(), fileext = "")
#url=paste(krona_table, ".html", sep="")
#system(paste("export PERL5LIB=/home/aghozlan/workspace/SHAMAN_App/KronaTools-2.6/lib:$PERL5LIB; /home/aghozlan/workspace/META10S_App/krona_bin/bin/ktImportText", krona_table))
system(paste("ktImportText", krona_table))
refs <- paste0("<a href='", url, "' target='_blank'>krona</a>")
#system(paste("ktImportText", krona_table))
#refs <- paste0("<a href='", url, "' target='_blank'>krona</a>")
data.frame(refs)
}
}, sanitize.text.function = function(x) x)
#data.frame(refs)
# }
#}, sanitize.text.function = function(x) x)
#####################################################
......
......@@ -606,6 +606,8 @@ body <- dashboardBody(
tags$img(src = "gears.gif",id ="loading-spinner")
),
uiOutput("plotVisu"),
#conditionalPanel(condition="input.PlotVisuSelect!='Krona'",),
#conditionalPanel(condition="input.PlotVisuSelect=='Krona'",tags$script(uiOutput("plotVisu"))),
conditionalPanel(condition="input.PlotVisuSelect=='Scatterplot' && !input.AddRegScatter",
p(actionButton("scatterD3-reset-zoom", HTML ("<span class='glyphicon glyphicon-search' aria-hidden='true'></span> Reset Zoom")),Align="right")
)
......@@ -649,7 +651,7 @@ body <- dashboardBody(
column(width=3,
box(title = "Select your plot", width = NULL, status = "primary", solidHeader = TRUE,collapsible = FALSE,collapsed= FALSE,
selectizeInput("PlotVisuSelect","",c("Barplot"="Barplot","Heatmap"="Heatmap","Boxplot"="Boxplot","Tree"="Tree","Scatterplot"="Scatterplot","Diversity"="Diversity","Rarefaction"="Rarefaction"),selected = "Barplot")
selectizeInput("PlotVisuSelect","",c("Barplot"="Barplot","Heatmap"="Heatmap","Boxplot"="Boxplot","Tree"="Tree","Scatterplot"="Scatterplot","Diversity"="Diversity","Rarefaction"="Rarefaction","Krona"="Krona", "Phylogeny"="Phylogeny"),selected = "Barplot")
),
......@@ -676,14 +678,14 @@ body <- dashboardBody(
radioButtons("CorMeth","Correlation method",c("Pearson" = "pearson", "Spearman" = "spearman"),inline=TRUE),
checkboxInput("AddRegScatter","Add regression line",FALSE)
),
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Diversity' && input.PlotVisuSelect!='Scatterplot'",
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Diversity' && input.PlotVisuSelect!='Scatterplot' && input.PlotVisuSelect!='Krona'",
radioButtons("SelectSpecifTaxo","Select the features",c("Most abundant"="Most","All"="All", "Differential features" = "Diff", "Non differential features" = "NoDiff"))
),
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Diversity' && input.PlotVisuSelect!='Scatterplot' && (input.SelectSpecifTaxo=='Diff' || input.SelectSpecifTaxo=='NoDiff') ",
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Diversity' && input.PlotVisuSelect!='Scatterplot' && (input.SelectSpecifTaxo=='Diff' || input.SelectSpecifTaxo=='NoDiff') && input.PlotVisuSelect!='Krona' ",
selectizeInput("ContrastList_table_Visu","",choices = "", multiple = TRUE),
radioButtons("UnionInterContrasts","Union or intersection ?",c("Union"="Union","Intersection"="Inter"),inline = TRUE)
),
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Diversity' && input.PlotVisuSelect!='Scatterplot'",
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Diversity' && input.PlotVisuSelect!='Scatterplot' && input.PlotVisuSelect!='Krona'",
uiOutput("TaxoToPlotVisu")
),
......@@ -788,23 +790,25 @@ body <- dashboardBody(
##################
## ALL
##################
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Scatterplot'",
conditionalPanel(condition="input.PlotVisuSelect!='Rarefaction' && input.PlotVisuSelect!='Scatterplot' && input.PlotVisuSelect!='Krona' && input.PlotVisuSelect!='Phylogeny'",
radioButtons(inputId = "SensPlotVisu",label = h6(strong("Orientation")),choices = c("Vertical" = "Vertical", "Horizontal" = "Horizontal"),selected = "Vertical",inline = TRUE)
)
),
box(title = "Export", width = NULL, status = "primary", solidHeader = TRUE,collapsible = TRUE,collapsed= TRUE,
##################
## BARPLOT
##################
conditionalPanel(condition="input.PlotVisuSelect=='Barplot'",
radioButtons("positionBarPlot","Position",c("Grouped"="fill","Stacked"="dodge"),inline=TRUE)
),
selectInput("Exp_format_Visu",h5(strong("Export format")),c("png"="png","pdf"="pdf","eps"="eps","svg"="svg"), multiple = FALSE),
fluidRow(
column(width=6,numericInput("heightVisuExport", "Height (in px)",min=100,max=NA,value = 500,step =1)),
column(width=6,numericInput("widthVisuExport", "Width (in px)",min=100,max=NA,value = 500,step =1))
),
downloadButton("exportVisu", "Export")
conditionalPanel(condition="input.PlotVisuSelect!='Krona' && input.PlotVisuSelect!='Phylogeny'",
box(title = "Export", width = NULL, status = "primary", solidHeader = TRUE,collapsible = TRUE,collapsed= TRUE,
##################
## BARPLOT
##################
conditionalPanel(condition="input.PlotVisuSelect=='Barplot'",
radioButtons("positionBarPlot","Position",c("Grouped"="fill","Stacked"="dodge"),inline=TRUE)
),
selectInput("Exp_format_Visu",h5(strong("Export format")),c("png"="png","pdf"="pdf","eps"="eps","svg"="svg"), multiple = FALSE),
fluidRow(
column(width=6,numericInput("heightVisuExport", "Height (in px)",min=100,max=NA,value = 500,step =1)),
column(width=6,numericInput("widthVisuExport", "Width (in px)",min=100,max=NA,value = 500,step =1))
),
downloadButton("exportVisu", "Export")
)
)
)
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment