---
title: "AbSeq analysis report"
output:
    html_document:
        # using the same theme as template.Rmd
        theme: cosmo
        toc: true
        toc_float:
            collapsed: false
            smooth_scroll: false
params:
    singleSamples: "PCR1,PCR2,PCR3"
    multiSamples: "PCR1_vs_PCR2,PCR1_vs_PCR3,PCR1_vs_PCR5,PCR2_vs_PCR3"
    singleSampleLinks: "link1,link2,link3"
    multiSampleLinks: "link12,link13,link14,link15"
    chains: "hv,lv,hv"
    rawReads: "1000,1000,1000"
    annotReads: "900, 291, 987"
    filtReads: "900, 291, 987"
    prodReads: "500, 200, 123"
    filters: "bitscore: 1 - inf, alignlen: 1 - inf, sstart: 1 - inf, qstart: 1 - inf ? bitscore: 1 - inf, alignlen: 1 - inf, sstart: 1 - inf, qstart: 1 - inf ? bitscore: 1 - inf, alignlen: 1 - inf, sstart: 1 - inf, qstart: 1 - inf "
    filterSplitter: "?"
    analysisParams: "/newhome/jiahong/sandbox/immulator_out/out/report/PCR1/analysis.params"
---

```{r abseq_index_landing_setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)

.formatNumber <- function(numStr, denom) {
    n <- suppressWarnings(as.numeric(numStr))
    d <- suppressWarnings(as.numeric(denom))
    nStr <- prettyNum(n, big.mark = ",")
    dStr <- prettyNum(d, big.mark = ",")
    perc <- n / d * 100
    return(sprintf("%s%% (%s/%s)", round(perc, 2), nStr, dStr))
}
```

generated on `r format(Sys.time(), "%a %b %d %Y %H:%M:%S")`

<!-- number of samples:, indv, comp -->
# Summary

Individual samples are listed in the following table.

Click on the sample names for more details.

The comparative analysis (if any) will appear [after the table](#comparative-reports).

## Individual sample reports

Number of samples: `r length(unlist(strsplit(params$singleSampleLinks, ",")))`


```{r abseq_index_table_summary, echo=FALSE}
samples <- unlist(strsplit(params$singleSamples, ","))
links <- unlist(strsplit(params$singleSampleLinks, ","))
chains <- unlist(strsplit(params$chains, ","))
rawReads <- unlist(strsplit(params$rawReads, ","))
filtReads <- unlist(strsplit(params$filtReads, ","))
annotReads <- unlist(strsplit(params$annotReads, ","))
prodReads <- unlist(strsplit(params$prodReads, ","))
filters <- unlist(strsplit(params$filters, params$filterSplitter, fixed = TRUE))
if (length(samples) * 8 != (length(samples) + length(links) + length(chains) + length(rawReads) + length(filtReads) + length(annotReads) + length(prodReads)) + length(filters)) {
    stop(sprintf("Expected same argument length for singleSamples (%d), links (%d), chains (%d), rawReads (%d), filteredReads (%d), annotReads (%d), prodReads (%d) and filters (%d)",
                 length(samples),
                 length(links),
                 length(chains),
                 length(rawReads),
                 length(filtReads),
                 length(annotReads),
                 length(prodReads),
                 length(filters)
                 ))
}
singleTableInfo <- lapply(seq_along(samples), function(i) {
    reads <- prettyNum(rawReads[i], big.mark = ",")
    annots <- .formatNumber(annotReads[i], rawReads[i])
    filtered <- .formatNumber(filtReads[i], annotReads[i])
    prods <- .formatNumber(prodReads[i], filtReads[i])
    filts <- gsub(",", "<br>", filters[i], fixed = TRUE)
    knitr::knit_expand(text = sprintf("[%s](%s) | %s | %s | %s | %s | %s | %s", samples[i], links[i], chains[i], reads, annots, filtered, prods, filts))
})

singleTableInfo <- c("sample | chain | # reads | annotated reads | filtered(kept) reads | productive reads | filters applied",
                     "-------|-------|---------|-----------------|----------------------|------------------|---------------",
                     singleTableInfo)
```

`r paste(knitr::knit(text = paste(singleTableInfo, collapse = '\n')))`

## Comparative reports

Number of comparative reports: `r length(unlist(strsplit(params$multiSampleLinks, ",")))`

```{r abseq_comparative_show, echo=FALSE}
samples <- unlist(strsplit(params$multiSamples, ","))
links <- unlist(strsplit(params$multiSampleLinks, ","))
if (length(samples) != length(links)) {
    stop("Expected equal number of samples and links")
}

comparativePoints <- lapply(seq_along(samples), function(i) {
    knitr::knit_expand(text = sprintf("* [%s](%s)", samples[i], links[i]))
})
```

`r paste(knitr::knit(text = paste(comparativePoints, collapse = '\n')))`

<!-- R, py version - R, py packages -->
# Session information

## AbSeqR

```{r abseq_display_session_info, echo=TRUE}
# This report is generated with the following packages(and versions)
sessionInfo()
```

## AbSeqPy

```{r abseq_display_abseqPy_info, echo=FALSE}
if (file.exists(params$analysisParams)) {
    con <- file(params$analysisParams, "r")
    lines <- readLines(con)
    close(con)
    versions <- lapply(lines, function(line) {
        if (grepl(".*version.*", line)) {
            line
        } else {
            NA
        }
    })
    
    cat(paste(versions[!is.na(versions)], collapse = "\n"))
} else {
    cat(paste(params$analysisParams), "could not be found, no information",
        "available.")
}
```
