## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      fig.width = 7, fig.height = 4, dpi = 120)
has_ggplot <- requireNamespace("ggplot2", quietly = TRUE)

## ----results-data-------------------------------------------------------------
## Summary results (from the benchmark runs; see inst/benchmarks/ to reproduce).
read_tbl <- data.frame(
  Workload    = c("Core fields", "GAlignments", "Sequence + quality"),
  Reads       = c("qname/flag/rname/pos/mapq/cigar", "-> GAlignments object", "seq + base quality"),
  `Standard (s)` = c(15.6, 10.9, 22.3),
  `BamScale (s)` = c(6.8, 3.4, 8.1),
  Threads     = c(48L, 48L, 24L),
  Speedup     = c(2.29, 3.22, 2.76),
  check.names = FALSE
)

spectrum_tbl <- data.frame(
  Endpoint      = c("ATAC fragment-size QC", "GAlignments (read)", "Sequence + quality (read)",
                    "Coverage -> RleList", "Core fields (read)", "Coverage -> bigWig"),
  Layer         = c("workflow", "read", "read", "workflow", "read", "workflow"),
  `Read fraction` = c(0.93, 1.00, 1.00, 0.76, 1.00, 0.21),
  Speedup       = c(3.66, 3.22, 2.76, 2.50, 2.29, 1.23),
  check.names = FALSE
)

## ----read-table---------------------------------------------------------------
knitr::kable(
  transform(read_tbl, Speedup = sprintf("%.2fx", Speedup)),
  caption = "Single-file read throughput: best BamScale configuration vs the single-threaded standard reader (median of 5 iterations)."
)

## ----read-plot, eval=has_ggplot-----------------------------------------------
library(ggplot2)
ggplot(read_tbl, aes(stats::reorder(Workload, Speedup), Speedup)) +
  geom_col(fill = "#2a78d6", width = 0.65) +
  geom_hline(yintercept = 1, linetype = "dashed", colour = "grey55") +
  geom_text(aes(label = sprintf("%.2fx", Speedup)), hjust = -0.15, size = 3.5) +
  coord_flip() +
  scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
  labs(x = NULL, y = "speedup over standard reader (dashed = parity)",
       title = "Single-file read speedup") +
  theme_minimal(base_size = 12)

## ----spectrum-table-----------------------------------------------------------
knitr::kable(
  transform(spectrum_tbl,
            `Read fraction` = paste0(round(100 * spectrum_tbl$`Read fraction`), "%"),
            Speedup = sprintf("%.2fx", Speedup)),
  caption = "End-to-end speedup by workload endpoint. Read-pattern rows are single-file read throughput; workflow rows are end-to-end. Read fraction is the share of the standard pipeline spent reading."
)

## ----spectrum-plot, eval=has_ggplot, fig.height=4.4---------------------------
ggplot(spectrum_tbl, aes(stats::reorder(Endpoint, Speedup), Speedup, fill = `Read fraction`)) +
  geom_col(width = 0.65) +
  geom_hline(yintercept = 1, linetype = "dashed", colour = "grey55") +
  geom_text(aes(label = sprintf("%.2fx", Speedup)), hjust = -0.15, size = 3.4) +
  coord_flip() +
  scale_fill_gradient(low = "#cfe0f3", high = "#12355b", labels = scales::percent) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
  labs(x = NULL, y = "end-to-end speedup (dashed = parity)", fill = "read\nfraction",
       title = "The gain tracks the read fraction") +
  theme_minimal(base_size = 12)

## ----demo, echo=TRUE, eval=TRUE-----------------------------------------------
library(BamScale)
bam <- ompBAM::example_BAM("Unsorted")

## Alignment fields as a GAlignments object (drop-in for readGAlignments)
ga <- bam_read(bam, what = c("rname", "pos", "cigar", "strand"),
               as = "GAlignments", threads = 2)
ga

## ...or core fields as a data.frame; `threads` controls within-file parallelism
df <- bam_read(bam, what = c("qname", "flag", "mapq"),
               as = "data.frame", threads = 2)
head(df)

## ----repro, echo=TRUE, eval=FALSE---------------------------------------------
# dir(system.file("benchmarks", package = "BamScale"))
# # run_server_benchmark.R      : read-pattern micro-benchmarks (step1 / GAlignments / seq+qual)
# # run_workflow_benchmark.R    : end-to-end coverage and ATAC fragment-size QC workflows
# # download_atac_data.R        : fetch + index the ENCODE GM12878 ATAC BAMs used here

## ----session-info-------------------------------------------------------------
sessionInfo()

