## ----style, echo = FALSE, results = 'asis'----------------------------------------------------------------------------
  BiocStyle::markdown()

## ----setup, include = FALSE-------------------------------------------------------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    crop = NULL 
    ## Related to 
    ## https://stat.ethz.ch/pipermail/bioc-devel/2020-April/016656.html
)

## ----vignetteSetup, echo=FALSE, message=FALSE, warning = FALSE--------------------------------------------------------
## Track time spent on making the vignette
startTime <- Sys.time()

## Bib setup
library("knitcitations")

## Load knitcitations with a clean bibliography
cleanbib()
cite_options(hyperlink = "to.doc", citation_format = "text", style = "html")

## Write bibliography information
bib <- c(
    R = citation(),
    BiocStyle = citation("BiocStyle")[1],
    knitcitations = citation("knitcitations")[1],
    knitr = citation("knitr")[1],
    rmarkdown = citation("rmarkdown")[1],
    sessioninfo = citation("sessioninfo")[1],
    testthat = citation("testthat")[1],
    ISAnalytics = citation("ISAnalytics")[1]
)

write.bibtex(bib, file = "how_to_import_functions.bib")

## ----installBioc, eval=FALSE------------------------------------------------------------------------------------------
#  if(!requireNamespace("BiocManager", quietly = TRUE))
#      install.packages("BiocManager")
#  BiocManager::install("ISAnalytics")

## ----installGitHub, eval=FALSE----------------------------------------------------------------------------------------
#  if (!require(devtools)) {
#    install.packages("devtools")
#  }
#  devtools::install_github("calabrialab/ISAnalytics", build_vignettes = TRUE)

## ---------------------------------------------------------------------------------------------------------------------
library(ISAnalytics)

## ----OptVerbose, eval=FALSE-------------------------------------------------------------------------------------------
#  # DISABLE
#  options("ISAnalytics.verbose" = FALSE)
#  
#  # ENABLE
#  options("ISAnalytics.verbose" = TRUE)
#  

## ----OptWidg, eval=FALSE----------------------------------------------------------------------------------------------
#  # DISABLE HTML REPORTS
#  options("ISAnalytics.widgets" = FALSE)
#  
#  # ENABLE HTML REPORTS
#  options("ISAnalytics.widgets" = TRUE)

## ---------------------------------------------------------------------------------------------------------------------
root_correct <- system.file("extdata", "fs.zip", package = "ISAnalytics")
root_correct <- unzip_file_system(root_correct, "fs")
fs::dir_tree(root_correct)

## ---- echo = FALSE----------------------------------------------------------------------------------------------------
example_matrix_path <- system.file("extdata", "ex_annotated_ISMatrix.tsv.xz", 
                                   package = "ISAnalytics")
example_matrix <- read.csv(example_matrix_path, sep = "\t", header = TRUE, 
                           stringsAsFactors = FALSE, check.names = FALSE)
knitr::kable(head(example_matrix), 
             caption = "A simple example of messy matrix.", align = "l")

## ---------------------------------------------------------------------------------------------------------------------
example_matrix_path <- system.file("extdata", "ex_annotated_ISMatrix.tsv.xz", 
                                   package = "ISAnalytics")
imported_im <- import_single_Vispa2Matrix(example_matrix_path)

## ---- echo = FALSE----------------------------------------------------------------------------------------------------
knitr::kable(head(imported_im), caption = "Example of tidy integration matrix")

## ---------------------------------------------------------------------------------------------------------------------
# Displays the mandatory vars, can be called also for manipulation purposes 
# on tibble instead of calling individual variables
mandatory_IS_vars()

# Displays the annotation variables
annotation_IS_vars()

## ----R.options=options(ISAnalytics.widgets = FALSE)-------------------------------------------------------------------
options(ISAnalytics.widgets = FALSE)
path_as_file <- system.file("extdata", "ex_association_file.tsv",
                            package = "ISAnalytics")
association_file <- import_association_file(path_as_file, root_correct)

## ---- eval = FALSE----------------------------------------------------------------------------------------------------
#  matrices <- import_parallel_Vispa2Matrices_interactive(
#    association_file = path_as_file,
#    root = root_correct,
#    quantification_type = c("fragmentEstimate", "seqCount"),
#    matrix_type = "annotated",
#    workers = 2)

## ---- eval = FALSE----------------------------------------------------------------------------------------------------
#  matrices <- import_parallel_Vispa2Matrices_interactive(
#    association_file = association_file,
#    root = NULL,
#    quantification_type = c("fragmentEstimate", "seqCount"),
#    matrix_type = "annotated",
#    workers = 2)

## ---- eval=FALSE------------------------------------------------------------------------------------------------------
#  matrices$fragmentEstimate
#  matrices$seqCount

## ---------------------------------------------------------------------------------------------------------------------
library(magrittr)
refined_af <- association_file %>% dplyr::filter(.data$ProjectID == "CLOEXP")

## ----R.options=options(ISAnalytics.widgets = FALSE)-------------------------------------------------------------------
matrices_auto <- import_parallel_Vispa2Matrices_auto(
  association_file = refined_af,
  root = NULL,
  quantification_type = c("fragmentEstimate","seqCount"),
  matrix_type = "annotated",
  workers = 2,
  patterns = NULL,
  matching_opt = "ANY" # Same if you choose "ALL" or "OPTIONAL"
)
matrices_auto

## ----R.options=options(ISAnalytics.widgets = FALSE)-------------------------------------------------------------------
root_err <- system.file("extdata", "fserr.zip", package = "ISAnalytics")
root_err <- unzip_file_system(root_err, "fserr")
fs::dir_tree(root_err)
association_file_fserr <- import_association_file(path_as_file, root_err)
refined_af_err <- association_file_fserr %>% 
  dplyr::filter(.data$ProjectID == "CLOEXP")
matrices_auto2 <- import_parallel_Vispa2Matrices_auto(
  association_file = refined_af_err,
  root = NULL,
  quantification_type = c("fragmentEstimate","seqCount"),
  matrix_type = "annotated",
  workers = 2,
  patterns = "NoMate",
  matching_opt = "ANY" # Same if you choose "ALL" or "OPTIONAL"
)
matrices_auto

## ----reproduce3, echo=FALSE-------------------------------------------------------------------------------------------
## Session info
library("sessioninfo")
options(width = 120)
session_info()

## ----results = "asis", echo = FALSE, warning = FALSE, message = FALSE-------------------------------------------------
## Print bibliography
bibliography()

