Introduction

renderAmpCurves and renderMeltCurves function represents amplification and melting data from real-time PCR experiments as curves based on plotly package. Main advantage of using this functions instead of regular plot renders is that it glued with RDML package. Minimal usage recures only GetFData(long.table = TRUE) function output. Also it has interactive feature - fast curves hiding without total plot redraw.

library(shinyMolBio)
library(tidyverse)
library(RDML)
library(chipPCR)
# load RDML file
rdml <- RDML$new(system.file("/extdata/stepone_std.rdml", package = "RDML"))
renderAmpCurves(inputId = "firstLook", # Shiny input ID
              label = "First Look", # optional plot label 
              ampCurves = rdml$GetFData(long.table = TRUE), # Amplification curves
              interactive = FALSE
) 

Curves customization

Color

Curve color can be directly provided by adding column color to ampCurves table or by choosing column that defines color with colorBy param.

renderAmpCurves(inputId = "color1",
                "Color by Sample Name",
              ampCurves = rdml$GetFData(long.table = TRUE),
              colorBy = "sample", # sample name will define color
              interactive = FALSE
) 
renderAmpCurves(inputId = "color2",
                "All 'red'",
              ampCurves = rdml$GetFData(long.table = TRUE) %>% 
                mutate(color = "red"), # All curves will be red
              interactive = FALSE
)
## Warning in `[.data.table`(curves, , `:=`(curveName, sprintf("%s %s %s %s", :
## Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the
## data.table so that := can add this new column by reference. At an earlier
## point, this data.table has been copied by R (or was created manually using
## structure() or similar). Avoid names<- and attr<- which in R currently (and
## oddly) may copy the whole data.table. Use set* syntax instead to avoid copying:
## ?set, ?setnames and ?setattr. If this message doesn't help, please report your
## use case to the data.table issue tracker so the root cause can be fixed or this
## message improved.

Linetype

Curve linetype can be setted by choosing column that defines linetype with linetypeBy param.

renderAmpCurves(inputId = "linetype",
                "Different Linetypes",
              ampCurves = rdml$GetFData(long.table = TRUE),
              linetypeBy = "sample.type", # sample.type will define color
              interactive = FALSE
) 

Show Cq Threshold Line

Threshold lines can be shown by choosing column that splits different threshold values with thBy param. Then input table have to contain quantFluor column.

# Create function for curves preprocessing
dataType$set("public", "Process",
             function(thValue) {
               # Subtract background
               private$.adp$fpoints$fluor <- 
                 CPP(self$adp$fpoints$cyc,
                     self$adp$fpoints$fluor,
                     bg.range = c(10,20))$y.norm
               # Calc Cq by threshold method
               cq <- th.cyc(self$adp$fpoints$cyc, self$adp$fpoints$fluor, r = thValue)[1, 1]
               self$cq <- if(!is.na(cq)) cq else NULL
               # Write threshold value
               self$quantFluor <- thValue
             },
             overwrite = TRUE)

rdml <- RDML$new(system.file("/extdata/lc96_bACTXY.rdml", package = "RDML"))

Loading experiment: ca1eb225-ecea-4793-9804-87bfbb45f81d run: 65aeb1ec-b377-4ef6-b03f-92898d47488b

# Manual threshold values for different targets
thValues <- c("bACT" = 0.03, "X" = 0.05, "Y" = 0.04, "IPC" = 0.01)

# Preprocess every curve
for (react in rdml$experiment[[1]]$run[[1]]$react) {
  for (fdata in react$data) {
    fdata$Process(thValues[fdata$tar$id])
  }
}


tbl <- rdml$AsTable(quantFluor = data$quantFluor, # Add threshold values to table
                    cq = data$cq)
renderAmpCurves("th", 
                "Show Thershold Lines", 
                rdml$GetFData(tbl, long.table = TRUE), 
                colorBy = "target",
                showCq = TRUE,
                thBy = "target", # Add threshold lines (separated by targets)
                interactive = FALSE) 
## Warning: Ignoring 10 observations

Show Markers

You can show Cq or Tm values on curves as markers setting showCq = TRUE or showTm = TRUE. Then input table have to contain cq or tm column, and quantFluor for y values.

renderAmpCurves(inputId = "cq",
                "Show Cq Values",
              ampCurves = rdml$GetFData(tbl, 
              # Cq and quantFluor values are obtained
              # from file on previous step
                long.table = TRUE), 
              showCq = TRUE, # Add Cq markers to curves
              colorBy = "sample",
              interactive = FALSE
) 
## Warning: Ignoring 10 observations

Custom Plotly Code

You can add custom qouted plotly code by plotlyCode parameter. Note that you have to add value p inside your quoted code to link it with render output.

markTbl <- tbl %>% 
  filter(position %in% c("D03", "D07"),
         target == "bACT")

renderAmpCurves("th", 
                "Show Thershold Lines", 
                rdml$GetFData(tbl, long.table = TRUE), 
                colorBy = "target",
                plotlyCode = quote(
                  # Add Cq values for tubes D03 and D07 for target bACT as blue points
                  add_markers(p,
                              data = markTbl,
                              name = ~sample,
                              x = ~cq,
                              y = ~quantFluor,
                              marker = list(color = "blue",
                                            size = 15)) %>%
                    # Set background color to light yellow
                    layout(paper_bgcolor = '#ffffe0',
                           plot_bgcolor = '#ffffe0')
                ),
                interactive = FALSE)

Melting curves

renderMeltCurves function provides all functionality described previosly in renderAmpCurves examples. Differences are showTm param instead of showCq and there is no thBy param.

# load RDML file
rdml <- RDML$new(system.file("/extdata/BioRad_qPCR_melt.rdml",
                             package = "RDML"))

Loading experiment: All Wells run: Amp Step 3_FAM

run: Amp Step 3_Cy5

Combining Bio-Rad runs

mdps <- rdml$GetFData(dp.type = "mdp", long.table = TRUE)
mdps[, diffFluor := c(0, diff(fluor)) * -1, by = fdata.name]

renderMeltCurves("melt", 
                "Show melting curves",
                mdps,
                fluorColumn = "diffFluor",
                colorBy = "target",
                interactive = FALSE)

Hiding and highlighting curves

Individual curves can be hidden without plot redraw. Use updateCurves function with fdata.name as hideCurves param. Or highlighted with fdata.name as highlightCurves param. Run shinyMolBio::runExample("pcrPlateInput") to see this in action. Curves hiding occures after wells selection at PCR plate and higlighting after mouse hovering above PCR plate or details table.

Also curves can be hided or highlighted at the plot creation passing table with columns hideCurve and highlightCurve respectively.