## ----setup, include = FALSE------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

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

## ----getDevel, eval=FALSE--------------------------------------------------
#  BiocManager::install("hubentu/RcwlPipelines")

## ----Load, message=FALSE---------------------------------------------------
library(RcwlPipelines)

## --------------------------------------------------------------------------
tools <- cwlTools(cachePath = tempdir())
tools

## ---- message=FALSE--------------------------------------------------------
library(dplyr)
bfcinfo(tools) %>% select(rname, fpath)

## ---- results = 'asis'-----------------------------------------------------
tls <- bfcinfo(tools) %>% filter(Type == "tool") %>%
    select(rname, Command, Container)
knitr::kable(tls)

## ---- results = 'asis'-----------------------------------------------------
pls <- bfcinfo(tools) %>% filter(Type == "pipeline") %>% pull(rname)
pls <- setdiff(pls, "mc3")
Steps <- sapply(pls, function(x) {
    cwl <- get(x, envir = environment())
    ss <- names(steps(cwl))
    paste(ss, collapse = "+")
})

knitr::kable(data.frame(Pipelines = pls, Steps), row.names = FALSE)

## --------------------------------------------------------------------------
bfcquery(tools, "bwa|sam2bam|sortBam|samtools_index|markdup") %>%
    filter(Type == "tool") %>%
    select(rname, Command, Container)

## --------------------------------------------------------------------------
p1 <- InputParam(id = "threads", type = "int")
p2 <- InputParam(id = "RG", type = "string")
p3 <- InputParam(id = "Ref", type = "string")
p4 <- InputParam(id = "FQ1", type = "File")
p5 <- InputParam(id = "FQ2", type = "File?")

## --------------------------------------------------------------------------
## bwa
s1 <- Step(id = "bwa", run = bwa,
           In = list(threads = "threads",
                     RG = "RG",
                     Ref = "Ref",
                     FQ1 = "FQ1",
                     FQ2 = "FQ2"))
## sam to bam
s2 <- Step(id = "sam2bam", run = sam2bam,
           In = list(sam = "bwa/sam"))
## sort bam
s3 <- Step(id = "sortBam", run = sortBam,
           In = list(bam = "sam2bam/bam"))
## mark duplicates
s4 <- Step(id = "markdup", run = markdup,
           In = list(ibam = "sortBam/sbam",
                     obam = list(
                         valueFrom="$(inputs.ibam.nameroot).mdup.bam"),
                     matrix = list(
                         valueFrom="$(inputs.ibam.nameroot).markdup.txt")))
## index bam
s5 <- Step(id = "idxBam", run = samtools_index,
           In = list(bam = "markdup/mBam"))

## --------------------------------------------------------------------------
req1 <- list(class = "StepInputExpressionRequirement")
req2 <- list(class = "InlineJavascriptRequirement")
## outputs
o1 <- OutputParam(id = "Bam", type = "File", outputSource = "markdup/mBam")
o2 <- OutputParam(id = "Idx", type = "File", outputSource = "idxBam/idx")
## stepParam
Align <- cwlStepParam(requirements = list(req1, req2),
                      inputs = InputParamList(p1, p2, p3, p4, p5),
                      outputs = OutputParamList(o1, o2))
## build pipeline
Align <- Align + s1 + s2 + s3 + s4 + s5

## --------------------------------------------------------------------------
plotCWL(Align)

## --------------------------------------------------------------------------
inputs(alignMerge)

## --------------------------------------------------------------------------
runs(runs(alignMerge)[[1]])

## --------------------------------------------------------------------------
runs(runs(alignMerge)[[2]])

## --------------------------------------------------------------------------
outputs(alignMerge)

## --------------------------------------------------------------------------
inputs(rnaseq_Sf)

## --------------------------------------------------------------------------
bfcinfo(tools) %>% filter(rname == "mc3") %>% pull(rpath) %>% source
inputs(mc3)

## --------------------------------------------------------------------------
outputs(mc3)

## --------------------------------------------------------------------------
arguments(Mutect2) <- list("--max-mnp-distance", "0")
Mutect2

## --------------------------------------------------------------------------
runs(GPoN)

## --------------------------------------------------------------------------
plotCWL(Mutect2PL)

## --------------------------------------------------------------------------
sessionInfo()