R/make_correlation_report.R

Defines functions make_correlation_report

# Prepares the correlation report

make_correlation_report <- function(defs){

  assertthat::assert_that("linear_model_cutoff" %in% names(defs),
                          is.numeric(defs$linear_model_cutoff),
                          msg = "defs$linear_model_cutoff must be numeric")

  cutoff <- defs$linear_model_cutoff

  cat("\nGenerating HTML5 report for results with phylogeny-aware q-values < ",
      cutoff, "\n(this may take a while.",
      "Perfect time to grab a coffee, and maybe a quick snack.)")

  # filter out those with no observations
  sumY    <- sapply(defs$y, sum)  # faster than apply() or colSums()!
  idx     <- (sumY != 0)
  Y       <- defs$y[, idx]
  sumY    <- sumY[idx]
  defs$sd <- defs$sd[idx]
  defs$cv <- defs$cv[idx]

  # Prepare plotframe
  plotframe <- rbind(defs$contrasts.corrected[order(names(defs$contrasts.corrected))],
                     defs$results.correlations.pvalue.pearson[order(names(defs$results.correlations.pvalue.pearson))],
                     defs$results.correlations.pvalue.spearman[order(names(defs$results.correlations.pvalue.spearman))],
                     defs$results.correlations.pvalue.kendall[order(names(defs$results.correlations.pvalue.kendall))],
                     defs$correlations.pearson[order(names(defs$correlations.pearson))],
                     defs$correlations.spearman[order(names(defs$correlations.spearman))],
                     defs$correlations.kendall[order(names(defs$correlations.kendall))],
                     sumY[order(names(sumY))],
                     defs$sd[order(names(defs$sd))],
                     defs$cv[order(names(defs$cv))])
  plotframe <- rbind(plotframe,
                     Y[, order(colnames(Y))])

  rownames(plotframe)[1:10] <- c("corrected_contrasts",
                                 "Pearson_qvalue",
                                 "Spearman_qvalue",
                                 "Kendall_qvalue",
                                 "Pearson_cor",
                                 "Spearman_cor",
                                 "Kendall_cor",
                                 "size",
                                 "sd",
                                 "cv")

  plotframe <- as.data.frame(t(plotframe))

  description           <- unlist(defs$annotation.cor)
  plotframe$description <- description[order(names(description))]
  plotframe$name        <- rownames(plotframe)

  df_cutoff <- plotframe[plotframe$corrected_contrasts < cutoff, ]
  df_cutoff <- df_cutoff[df_cutoff$sd != 0, ] # remove trivial cases, constant values.


  # Prepare output folder
  od  <- normalizePath(defs$output_dir)
  cpd <- gsub("//", "/", paste0(od, "/correlation_Plots/"),
              fixed = TRUE)
  if(!dir.exists(cpd)) dir.create(cpd, recursive = TRUE)

  # Copy report template into output dir
  fp <- gsub("//", "/", paste0(normalizePath(defs$output_dir),
                               "/K2rep.Rmd"),
             fixed = TRUE)
  file.copy(system.file("extdata", "KOMODO2_correlation_report.Rmd",
                        package = "KOMODO2"), to = fp, overwrite = TRUE)

  suppressWarnings(rmarkdown::render(fp,
                                     output_file = "KOMODO2_report.html",
                                     quiet = TRUE))
  file.remove(fp)
  cat("done")

  return(NULL)
}
fcampelo/KOMODO2-CRAN documentation built on March 7, 2020, 6:35 a.m.