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
library(shiny) ##################################################
shinyServer(function(input, output) { ### libraries
output$contents <- renderTable({ library(shiny)
library(DT)
inFile <- input$file1 library(rgl)
library(quantreg)
if (is.null(inFile)) library(SparseM)
return(NULL) library(rmarkdown)
dd<-read.csv(inFile$datapath, header=input$header, sep=input$sep, # server script
quote=input$quote) shinyServer(
do<-reactiveValues(data=dd)
function(input, output, session){
#calcul coef pour affichage dans en dessous du graph
x=rep(dd[,1],ncol(dd)-1) ####### Upload data #######
y=unlist(dd[,2:ncol(dd)]) getData <- reactive({
crssce=data.frame(y,x) inFile <- input$file1
fit=nls(y~SSlogis(x,Asym,xmid,scal),crssce) if (is.null(inFile)) return(NULL)
param=summary(fit)$parameters df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, dec = input$dec)
mu=round(1/param[3,1],digits=3) return(df)
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) ####### Calcul des estimateurs #######
#####Le graph calculate <- reactive({
df <- getData()
req(df)
output$plot <- renderPlot({
# calcul coef pour affichage dans en dessous du graph
############## x <- rep(df[,1], ncol(df)-1)
x=rep(dd[,1],ncol(dd)-1) y <- unlist(df[, 2:ncol(df)])
y=unlist(dd[,2:ncol(dd)])
crssce=data.frame(y,x) crssce <- data.frame(x, y)
require("quantreg")
# limit axes extended fit <- nls(y ~ SSlogis(x, Asym, xmid, scal), crssce)
maxx=max(x,na.rm=T)+(max(x,na.rm=T)/4) param <- summary(fit)$parameters
maxy=max(y,na.rm=T)+(max(y,na.rm=T)/4)
w0=seq(0,maxx) # cat(param,'\n\n')
# modlog2=nls(x ~ SSlogis(y,Asym,xmid,scal),crssce)
# modlog=nls(y ~ SSlogis(x,Asym,xmid,scal),crssce) scal <- param[3,1]
coef=getInitial(y~SSlogis(x,asym,xmid,scal),data=cbind.data.frame(x,y)) sdscal <- param[3,2]
mu=1/coef[3]
# output plot mu <- round(1 / scal, digits = 3)
plot(x,y) sdmu <- round((1 / scal) - (1 / (scal + sdscal)), digits = 3)
points(x,y,xlim=c(0,maxx),ylim=c(0,maxy),xlab="temps(h)",ylab="DO600nm",las=1,pch=22,bg=1)
lines(w0,SSlogis(w0,coef[1],coef[2],coef[3]),lwd=1.5) asym <- param[1,1]
q1b=nlrq(y~SSlogis(x,K, xmid, r),tau=0.25,data=crssce) sdasym <- param[1,2]
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) xmid <- param[2,1]
q95b=nlrq(y~SSlogis(x,K, xmid, r),tau=0.975,data=crssce) sdxmid <- param[2,2]
lines(w0, predict(q1b, newdata=list(x=w0)), col="blue",lty=2)
lines(w0, predict(q3b, newdata=list(x=w0)), col="blue",lty=2) params <- as.data.frame(cbind(scal, sdscal, mu, sdmu, asym, sdasym, xmid, sdxmid))
lines(w0, predict(q05b, newdata=list(x=w0)), col="blue",lty=3) colnames(params) <- c("scal", "sdscal", "Growth rate (Gr)", "Gr Standard deviation", "Maximum population (Mp)", "Mp Standard deviation",
lines(w0, predict(q95b, newdata=list(x=w0)), col="blue",lty=3) "Start expo. phase (Ste)", "Ste Standard deviation")
observeEvent(input$clicks, {print(as.numeric(do$data))}) return(params)
})
})
output$ex_out <- renderPrint({return(c(mu,sdmu,asym,sdasym))}) ####### Raw_plot #######
output$ex_out2 <- renderPrint({return(dd)}) output$raw_plot <- renderPlot({
}) df <- getData()
output$txtout <- renderText({ req(df)
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") x <- rep(df[, 1], ncol(df) - 1)
}) y <- unlist(df[, 2:ncol(df)])
crssce <- data.frame(y, x)
})
# limit axes extended
# shinyServer(function(input, output, session) { maxx <- max(x, na.rm = T) + (max(x, na.rm = T) / 4)
#output$main_plot <- renderPlot({ maxy <- max(y, na.rm = T) + (max(y, na.rm = T) / 4)
# plot(dd$H,dd$A, type=input$plotType) w0 <- seq(0, maxx)
# coef <- getInitial(y ~ SSlogis(x, asym, xmid, scal), data = cbind.data.frame(x, y))
# mu <- 1 / coef[3]
# output$table <- DT::renderDataTable({
# DT::datatable(cars) # output plot
# }) plot(x, y, xlab = input$xlabel, ylab = input$ylabel, xlim = c (0, maxx),
# }) ylim = c(0, maxy), las = 1, pch = 22, bg = 1)
\ No newline at end of file lines(w0, SSlogis(w0, coef[1], coef[2], coef[3]), lwd = 1.5)
grid()
q1b <- nlrq(y ~ SSlogis(x, K, xmid, r), tau = 0.25, data = crssce)
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)
q95b <- nlrq(y ~ SSlogis(x, K, xmid, r), tau = 0.975, 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)
lines(w0, predict(q05b, newdata = list(x = w0)), col = "blue", lty=3)
lines(w0, predict(q95b, newdata = list(x = w0)), col = "blue", lty=3)
#
})
####### Affichage du tableau des parametres #######
output$parameters <- DT::renderDataTable({
params <- calculate()
paste("The table below gives the parameters estimation.")
DT::datatable(params[,3:8])
#
})
####### 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)])
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
# 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
library(shiny) ## Date: October 2017
## Author: Marc Garel, Severine Martini, Christian Tamburini
##################################################
shinyUI(fluidPage(
headerPanel("Application of a logistic model on microbial datasets"), ### libraries
# load dataset library(shiny)
titlePanel("Upload file"), library(DT)
sidebarLayout( library(rgl)
sidebarPanel("This is to perform logistic regression to estimate growth rate and maxium cells density.", library(quantreg)
fileInput('file1', 'Choose CSV File', library(SparseM)
accept=c('text/csv', library(rmarkdown)
'text/comma-separated-values,text/plain',
'.csv', '.txt')), ### ui script
tags$hr(), shinyUI(
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', fluidPage(
c(Comma=',',
Semicolon=';', navbarPage("Application of a logistic model on microbial datasets",
Tab='\t'), tabPanel("Upload data",
','), sidebarLayout(
radioButtons('quote', 'Quote', sidebarPanel("",
c(None='', fileInput('file1', 'Upload your dataset (.csv or .txt)',
'Double Quote'='"', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv', '.txt')),
'Single Quote'="'"), # header or not header
'"') checkboxInput('header', 'Header', TRUE),
),
#
mainPanel( radioButtons('sep', 'Dataset separator',
h4("Ref"), c(Comma=',', Semicolon=';', Tab='\t'), selected="\t"),
verbatimTextOutput("txtout"),
withMathJax(), #
helpText('Logistic equation $$x(t) =r.x_0.(1-\\frac{x_0}K)$$'), radioButtons('dec', 'Decimal separator',
tabsetPanel( c(Point='.', Comma=','), selected="."),
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')) textInput("xlabel", "xlabel", value = "Time (h)"),
)
) #
) textInput("ylabel", "ylabel", value = "Biomass")
),
)) mainPanel(
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
with tabulation separators. Also, remove all spaces in the dataset header (prefer to use "_" when needed).
Organise your dataset so that there is only two arrays. The first one being the time and the second the cells density
(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.'),
withMathJax(helpText('The logistic equation is define as $$x(t) = r.x_0.(1-\\frac{x_0}K)$$'))
)
)
),
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"))
)
)
)
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