---
title: "ISAnalytics - Matrix import report"
date: "`r Sys.Date()`"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: scroll
    theme:
      bootswatch: cosmo
      primary: "#23687A"
    navbar:
      - { title: "Website", href: "https://calabrialab.github.io/ISAnalytics/", align: right, icon: "fa-globe" }
      - { title: "GitHub", href: "https://github.com/calabrialab/ISAnalytics", align: right, icon: "fa-github" }
params:
  files_found: null
  files_imp: null
  annot_prob: null
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
options(DT.warn.size = FALSE)
library(flexdashboard)
library(DT)
found_color <- "#77d983"
not_found_color <- "#e36868"
```

Files found {data-orientation=rows}
===============================================================================

Row1 {data-height=600 .tabset .tabset-fade}
-------------------------------------------------------------------------------
```{r}
files_found <- params$files_found %>%
  dplyr::mutate(ProjectID = as.factor(.data$ProjectID),
                concatenatePoolIDSeqRun = as.factor(.data$concatenatePoolIDSeqRun))

files_details <- files_found %>%
  dplyr::select(-.data$Files_count, -.data$Anomalies) %>%
  tidyr::unnest(.data$Files) %>%
  dplyr::mutate(Quantification_type = as.factor(.data$Quantification_type)) %>%
  dplyr::distinct()

count_details <- files_found %>%
  dplyr::select(-.data$Files, -.data$Anomalies) %>%
  tidyr::unnest(.data$Files_count) %>%
  dplyr::mutate(Quantification_type = as.factor(.data$Quantification_type)) %>%
  dplyr::distinct()
```

### Files counts by quantification 
```{r}
found_render <- JS("function(data, type, row) {",
                   "var icn = ",'"\u2713"', ";",
                   "var clss = ", '"text-success"', ";",
                   "if (data == 0) {",
                   "icn = ",'"\u2715"',";",
                   "clss = ", '"text-danger"', ";",
                   "} else if (data > 1) {",
                   "icn = ",'"\u26A0"', ";",
                   "clss = ", '"text-warning"', ";",
                   "}",
                   "return '<span class='+clss+'>'+data.toString()+' '+icn+",
                   "'</span>'",
                   "}")
datatable(
  count_details,rownames = FALSE, 
  colnames = stringr::str_replace_all(colnames(count_details), "_", " "), 
  filter = 'top',
  options = list(
    columnDefs = list(
      list(targets = 3,
           render = found_render)
      ),
      scrollY = 350
    ),
) %>% formatStyle(columns = 4, fontWeight = 'bold', textAlign = 'center')
```

### Files found per project/pool 
```{r}
datatable(
  files_details,
  rownames = FALSE, 
  colnames = stringr::str_replace_all(colnames(files_details), "_", " "), 
  filter = 'top', options = list(scrollY = 350)
)
```

### About

Summary of files found for each project/pool and quantification requested.  
If duplicated files are found you will find a 'warning' symbol next to the 
corresponding number. Please remember that in automatic mode the function
tries to discriminate between duplicates according to the provided patterns,
if no choice can be made, all files are discarded.

If no files were found for the corresponding quantification you will see a
'cross' symbol.

In the second tab are reported all files detected during file system scan.

Import summary {data-orientation=rows}
===============================================================================

Row1 {data-height=600 .tabset .tabset-fade}
-------------------------------------------------------------------------------
### Summary of files imported
```{r eval=!is.null(params$files_imp)}
files_imp <- params$files_imp %>%
  dplyr::mutate(dplyr::across(c(.data$ProjectID, 
                              .data$concatenatePoolIDSeqRun, 
                              .data$Quantification_type), .fns = as.factor))
datatable(
  files_imp,
  class = "stripe",
  filter = "top", 
  rownames = FALSE,
  colnames = stringr::str_replace_all(colnames(files_imp), "_", " "),
  options = list(
            order = list(list(4, 'asc')),
            scrollY = 350
          )
) %>% formatStyle(columns = "Imported", 
              color = styleEqual(
                levels = c(TRUE, FALSE),
                values = c(found_color, not_found_color)
              ), fontWeight = "bold", textTransform = "uppercase")
```

```{r eval=is.null(params$files_imp), results='asis'}
cat("*Nothing to report*")
```

### About

Summary of files chosen for import. If `Imported` is false, something may
have gone wrong during the import phase (most likely I/O errors).

Annotation issues {data-orientation=rows}
===============================================================================

Row1 {data-height=600 .tabset .tabset-fade}
-------------------------------------------------------------------------------
### Possible annotation problems
```{r eval=!is.null(params$annot_prob)}
datatable(
  annot_prob,
  rownames = FALSE, 
  colnames = stringr::str_replace_all(colnames(files_details), "_", " "), 
  filter = 'top', options = list(scrollY = 350)
)
```

```{r eval=is.null(params$annot_prob), results='asis'}
cat("*Nothing to report*")
```

### About

Report on annotation issues. Here are reported, if found, integration sites that
have been annotated with 2 or more different genes. If the table isn't empty,
you might want to check your matrices or re-perform the annotation step.

Reproducibility {data-orientation=rows}
===============================================================================

Row1 {data-height=600}
-------------------------------------------------------------------------------

### Session info {style="width: 95% !important"}
```{r attr.output='style="max-height: 580px;"'}
sessionInfo()
```

