Commit 03e53da2 authored by GAREL Marc's avatar GAREL Marc
Browse files

Major Update on shiny part from Lloyd Izard M2

parent 47834a7b
# Shiny app allowing: plot and parameters for microbial growth dataset ##################################################
# Marc Garel, Severine Martini, Christian Tamburini ## Project: Shiny app allowing plot and parameters for microbial growth dataset
### October 2017 ## Script purpose: Perform logistic regression to estimate growth rate and maximum cells density.
## Date: October 2017
## Author: Marc Garel, Severine Martini, Christian Tamburini
##################################################
### libraries
library(shiny) library(shiny)
library(DT)
library(rgl)
library(quantreg)
library(SparseM)
library(rmarkdown)
# server script
shinyServer(
shinyServer(function(input, output) { function(input, output, session){
output$contents <- renderTable({
####### Upload data #######
getData <- reactive({
inFile <- input$file1 inFile <- input$file1
if (is.null(inFile)) return(NULL)
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, dec = input$dec)
return(df)
})
####### Calcul des estimateurs #######
calculate <- reactive({
df <- getData()
req(df)
# calcul coef pour affichage dans en dessous du graph
x <- rep(df[,1], ncol(df)-1)
y <- unlist(df[, 2:ncol(df)])
if (is.null(inFile)) crssce <- data.frame(x, y)
return(NULL)
dd<-read.csv(inFile$datapath, header=input$header, sep=input$sep, fit <- nls(y ~ SSlogis(x, Asym, xmid, scal), crssce)
quote=input$quote) param <- summary(fit)$parameters
do<-reactiveValues(data=dd)
#calcul coef pour affichage dans en dessous du graph # cat(param,'\n\n')
x=rep(dd[,1],ncol(dd)-1)
y=unlist(dd[,2:ncol(dd)])
crssce=data.frame(y,x)
fit=nls(y~SSlogis(x,Asym,xmid,scal),crssce)
param=summary(fit)$parameters
mu=round(1/param[3,1],digits=3)
sdmu=round((1/summary(fit)$parameters[3,1])-(1/(summary(fit)$parameters[3,1]+summary(fit)$parameters[3,2])), digits=3)
asym=round(param[1,1],digits=3)
sdasym=round(summary(fit)$parameters[1,2], digits=3)
#####Le graph
scal <- param[3,1]
sdscal <- param[3,2]
mu <- round(1 / scal, digits = 3)
sdmu <- round((1 / scal) - (1 / (scal + sdscal)), digits = 3)
output$plot <- renderPlot({ asym <- param[1,1]
sdasym <- param[1,2]
xmid <- param[2,1]
sdxmid <- param[2,2]
params <- as.data.frame(cbind(scal, sdscal, mu, sdmu, asym, sdasym, xmid, sdxmid))
colnames(params) <- c("scal", "sdscal", "Growth rate (Gr)", "Gr Standard deviation", "Maximum population (Mp)", "Mp Standard deviation",
"Start expo. phase (Ste)", "Ste Standard deviation")
return(params)
})
####### Raw_plot #######
output$raw_plot <- renderPlot({
df <- getData()
req(df)
x <- rep(df[, 1], ncol(df) - 1)
y <- unlist(df[, 2:ncol(df)])
crssce <- data.frame(y, x)
##############
x=rep(dd[,1],ncol(dd)-1)
y=unlist(dd[,2:ncol(dd)])
crssce=data.frame(y,x)
require("quantreg")
# limit axes extended # limit axes extended
maxx=max(x,na.rm=T)+(max(x,na.rm=T)/4) maxx <- max(x, na.rm = T) + (max(x, na.rm = T) / 4)
maxy=max(y,na.rm=T)+(max(y,na.rm=T)/4) maxy <- max(y, na.rm = T) + (max(y, na.rm = T) / 4)
w0=seq(0,maxx) w0 <- seq(0, maxx)
# modlog2=nls(x ~ SSlogis(y,Asym,xmid,scal),crssce)
# modlog=nls(y ~ SSlogis(x,Asym,xmid,scal),crssce) coef <- getInitial(y ~ SSlogis(x, asym, xmid, scal), data = cbind.data.frame(x, y))
coef=getInitial(y~SSlogis(x,asym,xmid,scal),data=cbind.data.frame(x,y)) mu <- 1 / coef[3]
mu=1/coef[3]
# output plot # output plot
plot(x,y) plot(x, y, xlab = input$xlabel, ylab = input$ylabel, xlim = c (0, maxx),
points(x,y,xlim=c(0,maxx),ylim=c(0,maxy),xlab="temps(h)",ylab="DO600nm",las=1,pch=22,bg=1) ylim = c(0, maxy), las = 1, pch = 22, bg = 1)
lines(w0,SSlogis(w0,coef[1],coef[2],coef[3]),lwd=1.5) lines(w0, SSlogis(w0, coef[1], coef[2], coef[3]), lwd = 1.5)
q1b=nlrq(y~SSlogis(x,K, xmid, r),tau=0.25,data=crssce) grid()
q3b=nlrq(y~SSlogis(x,K, xmid, r),tau=0.75,data=crssce)
q05b=nlrq(y~SSlogis(x,K, xmid, r),tau=0.025,data=crssce) q1b <- nlrq(y ~ SSlogis(x, K, xmid, r), tau = 0.25, data = crssce)
q95b=nlrq(y~SSlogis(x,K, xmid, r),tau=0.975,data=crssce) q3b <- nlrq(y ~ SSlogis(x, K, xmid, r), tau = 0.75, data = crssce)
lines(w0, predict(q1b, newdata=list(x=w0)), col="blue",lty=2)
lines(w0, predict(q3b, newdata=list(x=w0)), col="blue",lty=2) q05b <- nlrq(y ~ SSlogis(x, K, xmid, r), tau = 0.025, data = crssce)
lines(w0, predict(q05b, newdata=list(x=w0)), col="blue",lty=3) q95b <- nlrq(y ~ SSlogis(x, K, xmid, r), tau = 0.975, data = crssce)
lines(w0, predict(q95b, newdata=list(x=w0)), col="blue",lty=3)
lines(w0, predict(q1b, newdata = list(x = w0)), col = "blue", lty=2)
observeEvent(input$clicks, {print(as.numeric(do$data))}) lines(w0, predict(q3b, newdata = list(x = w0)), col = "blue", lty=2)
lines(w0, predict(q05b, newdata = list(x = w0)), col = "blue", lty=3)
lines(w0, predict(q95b, newdata = list(x = w0)), col = "blue", lty=3)
#
}) })
output$ex_out <- renderPrint({return(c(mu,sdmu,asym,sdasym))})
output$ex_out2 <- renderPrint({return(dd)})
####### Affichage du tableau des parametres #######
output$parameters <- DT::renderDataTable({
params <- calculate()
paste("The table below gives the parameters estimation.")
DT::datatable(params[,3:8])
#
}) })
output$txtout <- renderText({
paste("Martini, S., B. Al Ali, M. Garel, and others. 2013. Effects of Hydrostatic Pressure on Growth and Luminescence of a Moderately-Piezophilic Luminous Bacteria Photobacterium phosphoreum ANT-2200 A. Driks [ed.]. PLoS One 8: e66580. doi:10.1371/journal.pone.0066580") ####### Affichage du tableau initial #######
output$raw_data <- DT::renderDataTable({
df <- getData()
DT::datatable(df)
#
}) })
}) ####### Affichage de la citation #######
output$citation <- renderText({
paste("Martini, S., B. Al Ali, M. Garel, and others. 2013. Effects of Hydrostatic Pressure on Growth and Luminescence of a Moderately-Piezophilic Luminous Bacteria\nPhotobacterium phosphoreum ANT-2200 A. Driks [ed.]. PLoS One 8: e66580. doi:10.1371/journal.pone.0066580")
#
})
####### Fonction de cout #######
cost <- function(Asym, scal, xmid, xdata, ydata){
ymod <- SSlogis(xdata, Asym, xmid, scal)
SSE <- sum((ymod - ydata)^2, na.rm = T)
return(SSE)
}
####### Verify_plot #######
output$verify_plot_2D <- renderPlot({
# on recupereles donnes de l'utilisateur
df <- getData()
req(df)
# on calcule les parametres
params <- calculate()
req(params)
# on assigne les valeurs de params a nos differentes variables
scal <- params[1,1]
mu <- params[1,3]
Asym <- params[1,5]
xmid <- params[1,7]
# cat(Asym, '\n\n')
# cat(mu, '\n\n')
# cat(scal, '\n\n')
# cat(xmid, '\n\n')
x <- rep(df[, 1], ncol(df) - 1)
y <- unlist(df[, 2:ncol(df)])
# shinyServer(function(input, output, session) { Asymmin <- Asym - (5*Asym) / 100
#output$main_plot <- renderPlot({ Asymmax <- Asym + (5*Asym) / 100
# plot(dd$H,dd$A, type=input$plotType)
# scal_min <- scal - (5*scal) / 100
# scal_max <- scal + (5*scal) / 100
# output$table <- DT::renderDataTable({
# DT::datatable(cars) mu_min <- 1/scal_max
# }) mu_max <- 1/scal_min
# })
\ No newline at end of file # cat(Asymmin, '\n\n')
# cat(Asymmax, '\n\n')
#
# cat(scal_min, '\n\n')
# cat(scal_max, '\n\n')
#
# cat(mu_min, '\n\n')
# cat(mu_max, '\n\n')
SSE <- cost(Asym, scal, xmid, x, y)
# cat(SSE, '\n\n')
scal_Vector <- seq(scal_min, scal_max, length=100)
mu_Vector <- seq(mu_min, mu_max, length=100)
Asym_vector <- seq(Asymmin, Asymmax, length=100)
costV <- Vectorize(cost, c("scal", "Asym"))
z <- outer(Asym_vector, scal_Vector, FUN = "costV", xmid = xmid, xdata = x, ydata = y)
# print(costV, '\n\n')
# cat(Asym, '\n\n')
# cat(mu, '\n\n')
# cat(head(z), '\n\n')
contour(Asym_vector, mu_Vector, z, nlevels = 30, xlab = "K (en cellules/ml)", ylab = "Growth rate (h-1)",
xlim = range(Asym_vector, finite = TRUE),
ylim = range(mu_Vector, finite = TRUE),
zlim = range(z, finite = TRUE))
points(Asym, mu, pch = 19, col = "lightseagreen")
})
####### Verify plot 3D #######
output$verify_plot_3D <- renderRglwidget({
df <- getData()
req(df)
params <- calculate()
req(params)
scal <- params[1,1]
mu <- params[1,3]
Asym <- params[1,5]
xmid <- params[1,7]
x <- rep(df[, 1], ncol(df) - 1)
y <- unlist(df[, 2:ncol(df)])
Asymmin <- Asym - (5*Asym) / 100
Asymmax <- Asym + (5*Asym) / 100
scal_min <- scal - (5*scal) / 100
scal_max <- scal + (5*scal) / 100
mu_min <- 1/scal_max
mu_max <- 1/scal_min
SSE <- cost(Asym, scal, xmid, x, y)
scal_Vector <- seq(scal_min, scal_max, length=100)
mu_Vector <- seq(mu_min, mu_max, length=100)
Asym_vector <- seq(Asymmin, Asymmax, length=100)
costV <- Vectorize(cost, c("scal", "Asym"))
z <- outer(Asym_vector, scal_Vector, FUN = "costV", xmid = xmid, xdata = x, ydata = y)
rgl.open(useNULL=T)
bg3d("white")
view3d( theta = 0, phi = -45)
plot3d(rep(Asym, length(Asym)),
rep(mu, each = length(mu)), z,
xlim = range(Asym_vector, finite = TRUE),
ylim = range(mu_Vector, finite = TRUE),
zlim = range(z, finite = TRUE),
type = "n", xlab = "Maximum population", ylab = "Growth rate", zlab = "SSE")
surface3d(Asym_vector, mu_Vector, z, col = "lightseagreen")
points3d(Asym, mu, SSE, size = 5, col = "black")
rglwidget()
})
output$downloadData <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.txt', sep='')
},
content = function(con) {
param <- calculate()
write.table((param[,3:8]), con, row.names = F, col.names = T, sep = '\t')
}
)
})
\ No newline at end of file
# Shiny app allowing: plot and parameters for microbial growth dataset ##################################################
# Marc Garel, Severine Martini, Christian Tamburini ## Project: Shiny app allowing plot and parameters for microbial growth dataset
### October 2017 ## Script purpose: Perform logistic regression to estimate growth rate and maximum cells density
## Date: October 2017
## Author: Marc Garel, Severine Martini, Christian Tamburini
##################################################
### libraries
library(shiny) library(shiny)
library(DT)
library(rgl)
library(quantreg)
library(SparseM)
library(rmarkdown)
### ui script
shinyUI(
fluidPage(
shinyUI(fluidPage( navbarPage("Application of a logistic model on microbial datasets",
headerPanel("Application of a logistic model on microbial datasets"), tabPanel("Upload data",
# load dataset
titlePanel("Upload file"),
sidebarLayout( sidebarLayout(
sidebarPanel("This is to perform logistic regression to estimate growth rate and maxium cells density.", sidebarPanel("",
fileInput('file1', 'Choose CSV File', fileInput('file1', 'Upload your dataset (.csv or .txt)',
accept=c('text/csv', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv', '.txt')),
'text/comma-separated-values,text/plain', # header or not header
'.csv', '.txt')),
tags$hr(),
checkboxInput('header', 'Header', TRUE), checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
#
radioButtons('sep', 'Dataset separator',
c(Comma=',', Semicolon=';', Tab='\t'), selected="\t"),
#
radioButtons('dec', 'Decimal separator',
c(Point='.', Comma=','), selected="."),
#
textInput("xlabel", "xlabel", value = "Time (h)"),
#
textInput("ylabel", "ylabel", value = "Biomass")
),
mainPanel( mainPanel(
h4("Ref"), helpText('This is to perform logistic regression to estimate growth rate and maximum cells density. In order to run this application, you have to format your dataset
verbatimTextOutput("txtout"), with tabulation separators. Also, remove all spaces in the dataset header (prefer to use "_" when needed).
withMathJax(), Organise your dataset so that there is only two arrays. The first one being the time and the second the cells density
helpText('Logistic equation $$x(t) =r.x_0.(1-\\frac{x_0}K)$$'), (e. g. optic density, cell number, biomass). This application proposes a method to perform logistic regression to estimate growth rate as well as maximum cells density.'),
tabsetPanel( withMathJax(helpText('The logistic equation is define as $$x(t) = r.x_0.(1-\\frac{x_0}K)$$'))
tabPanel("Details",tableOutput('contents'), helpText('You have to format data with tab separartor and dot for decimal. Remove all space in the header from your dataset. Replace it by "_"'),helpText('In your data set there are two arrays, time and cells density (Optic density, Cells number, Biomass ....) ')),
tabPanel("Data",verbatimTextOutput('ex_out2')),
tabPanel("Plot",plotOutput('plot'), verbatimTextOutput('ex_out'))
)
) )
) )
),
)) tabPanel("Uploaded data", DT::dataTableOutput("raw_data")),
tabPanel("Plot", plotOutput("raw_plot"), DT::dataTableOutput("parameters"),
downloadButton("downloadData", "Download")),
navbarMenu("Verify parameters estimation",
tabPanel("2D", plotOutput("verify_plot_2D")),
tabPanel("3D", rglwidgetOutput("verify_plot_3D", width = 800, height = 800))),
tabPanel("Citation", verbatimTextOutput("citation"))
)
)
)
Supports Markdown
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