library(reshape2)
library(xlsx)
library(plotly)

pl9reader = read.xlsx("data/growthCurves.xlsx", 8)

pl9reader$Time = as.numeric(row.names(pl9reader)) / 4

molten = melt( pl9reader[,1:15]
              ,id = c("Time..h.")
              # ,variable.name = "condition"
              # ,value.name = "OD"
              )
molten = cbind(molten,colsplit(molten$variable, "QQ",c("gene", "Ca")))
moltenSD = melt(pl9reader[,c(1,17:length(pl9reader))]
                ,id = "Time..h."
                # ,variable.name = "condition"
                ,value.name = "OD")


molten$gene[molten$gene!="WT"] = paste0("<i>",molten$gene[molten$gene!="WT"],"</i>") #italic gene names
p = plot_ly(molten
        ,x = ~Time..h.
        ,y = ~value
        ,error_y = list(value = ~value + moltenSD$OD,color = 'rgba(1,1,1,0.06)', width = 0, thickness = 6)
        ,color = ~gene
        # ,colors = "Dark2" #Reds BuRd Blues Spectral Accent YloRd Dark2
        ,colors = c("red", "green", "black", "yellow", "purple", "brown", "orange")
        ,linetype = ~Ca
        #,marker =  ~Ca
        ,type = "scatter"
        ,mode = "lines"
        ,width = 750
        ,legendgroup = ~Ca
         ,height = 650
        
        ) %>% layout(
           title = "Growth curve"
          ,yaxis = list(title = "OD<sub>600</sub> nm", showline = F, ticklen = 6, showgrid = F, zeroline = F)
          ,xaxis = list(title = "Time (H)", showgrid = FALSE, showline = T)
          ,autosize = F
          # ,legend = list(orientation = 'h', x = 1, y = 1)
          ,legend = list(orientation = 'v')
          ,font = list(size = 12)
        )
p
p = plot_ly(molten
        ,x = ~Time..h.
        ,y = ~value
        ,error_y = list(value = ~value + moltenSD$OD,color = 'rgba(1,1,1,0.03)', width = 0, thickness = 6.2)
        ,color = ~gene
        ,colors = c("#000000", "#333333", "#d9d9d9", "#8c8c8c", "#808080", "#262626", "#404040")
        ,linetype = ~Ca
        ,linetypes = c("dot", "solid")
        ,type = "scatter"
        ,mode = "lines+markers"
        ,width = 650
        ,legendgroup = ~gene
        ,symbol = ~gene
        ,symbols = c( "square", "cross", "diamond", "circle","bowtie","circle-open","hexagram")
        ,line = list(width = 1)
        # ,lines = c("-"," ")
        ,marker = list(size = 5)

        
        ) %>% layout(
           title = "Growth curve"
          ,yaxis = list(title = "OD<sub>600</sub> nm", showgrid = FALSE)
          ,xaxis = list(title = "Time (H)", showgrid = FALSE, zeroline = F)
          ,autosize = F
          # ,legend = list(orientation = 'h', x = 1, y = 1)
          ,legend = list(orientation = 'h', y = -0.2)
          ,font = list(size = 12)
        )
p
## Warning: The shape palette can deal with a maximum of 6 discrete values
## because more than 6 becomes difficult to discriminate; you have 7.
## Consider specifying shapes manually if you must have them.