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

## ----workflow, echo=FALSE, fig.align='center', fig.cap='FLAMES pipeline workflow summary'----
knitr::include_graphics(system.file("images/FLAMESpipeline-01.png", package = "FLAMES"))

## ----eval=TRUE, echo=TRUE-----------------------------------------------------
temp_path <- tempfile()
bfc <- BiocFileCache::BiocFileCache(temp_path, ask = FALSE)

file_url <-
  "https://raw.githubusercontent.com/OliverVoogd/FLAMESData/master/data"

annot <- bfc[[names(BiocFileCache::bfcadd(
  bfc, "Annotation",
  file.path(file_url, "gencodeshortened.gtf")
))]]

genome_fa <- bfc[[names(BiocFileCache::bfcadd(
  bfc,
  "Genomefa",
  file.path(file_url, "GRCh38shortened.fa")
))]]

fastq <- bfc[[names(BiocFileCache::bfcadd(
  bfc, "Fastq", file.path(file_url, "sc_align2genome.sample.fastq.gz")))]]

# setup other environment variables
outdir <- tempfile()
dir.create(outdir)
config_file <- FLAMES::create_config(outdir, type = "SIRV")

## ---- eval=TRUE, echo=TRUE----------------------------------------------------
library(FLAMES)
# do not run if minimap2 cannot be found
if (is.character(locate_minimap2_dir())) { 
  sce <- sc_long_pipeline(
    annotation = annot, fastq = fastq, genome_fa = genome_fa,
    outdir = outdir, config_file = config_file, match_barcode = FALSE
  )
}

## ---- eval=TRUE, echo=TRUE----------------------------------------------------
library(FLAMES)
minimap2_dir <- locate_minimap2_dir()
if (is.character(minimap2_dir)) { # do not run if minimap2 cannot be found
  config <- jsonlite::fromJSON(config_file)
  # find_barcode(...)
  genome_bam <- rownames(minimap2_align(
    config = config, fa_file = genome_fa, fq_in = fastq, annot = annot,
    outdir = outdir, minimap2_dir = minimap2_dir
  ))
  find_isoform(
    annotation = annot, genome_fa = genome_fa,
    genome_bam = genome_bam, outdir = outdir, config = config
  )
  minimap2_realign(
    config = config, fq_in = fastq,
    outdir = outdir, minimap2_dir = minimap2_dir
  )
  quantify(annotation = annot, outdir = outdir, config = config)
  sce <- create_sce_from_dir(outdir = outdir, annotation = annot)
}

## ---- eval=TRUE, message=FALSE, warning=FALSE---------------------------------
library(FLAMES)
library(MultiAssayExperiment)
 # load scm_lib80, scm_lib80 and scm_lib20
source(system.file("examples/scmixology1.R", package = "FLAMES"))
scm_lib80 <- scuttle::addPerCellQC(scm_lib80)
scm_lib20 <- scuttle::addPerCellQC(scm_lib20)
qc_80 <- scuttle::quickPerCellQC(colData(scm_lib80))
qc_20 <- scuttle::quickPerCellQC(colData(scm_lib20))
qc_80$discard[scm_lib80$sum < 10000] <- TRUE
qc_20$discard[scm_lib20$sum < 20000] <- TRUE

combined_sce <- combine_sce(
    short_read_large = scm_lib80[,!qc_80$discard],
    short_read_small = scm_lib20[,!qc_20$discard],
    long_read_sce = scm_lib20_transcripts,
    remove_duplicates = FALSE)

plots <- sc_annotate_plots(gene = "ENSG00000108107",
                           multiAssay = combined_sce)

scater::plotReducedDim(
    experiments(plots[["multiAssay"]])$gene_counts,
    dimred = "PCA",
    colour_by = colData(plots[["multiAssay"]]
        [,,"gene_counts"])[,"Lib_small",drop=FALSE])

plots[["plot_umap"]]
plots[["combined_isoform_plot"]]

## ----eval=TRUE, echo=TRUE-----------------------------------------------------
temp_path <- tempfile()
bfc <- BiocFileCache::BiocFileCache(temp_path, ask = FALSE)

file_url <-
  "https://raw.githubusercontent.com/OliverVoogd/FLAMESData/master/data"

annot <- bfc[[names(BiocFileCache::bfcadd(
  bfc, "Annotation",
  file.path(file_url, "SIRV_isoforms_multi-fasta-annotation_C_170612a.gtf")
))]]

genome_fa <- bfc[[names(BiocFileCache::bfcadd(
  bfc, "Genomefa",
  file.path(file_url, "SIRV_isoforms_multi-fasta_170612a.fasta")
))]]

# download the two fastq files, move them to a folder to be merged together
fastq1 <- bfc[[names(BiocFileCache::bfcadd(bfc, "Fastq1", 
                file.path(file_url, "fastq", "sample1.fastq.gz")))]]
fastq2 <- bfc[[names(BiocFileCache::bfcadd(bfc, "Fastq2",
                file.path(file_url, "fastq", "sample2.fastq.gz")))]]

# the downloaded fastq files need to be in a directory to be merged together
fastq_dir <- file.path(temp_path, "fastq_dir") 
dir.create(fastq_dir)
file.copy(c(fastq1, fastq2), fastq_dir)
unlink(c(fastq1, fastq2)) # the original files can be deleted

# setup other environment variables
outdir <- tempfile()
dir.create(outdir)
config_file <- FLAMES::create_config(outdir)

## ----eval=TRUE, echo=TRUE-----------------------------------------------------
library(FLAMES)
if (is.character(locate_minimap2_dir())) {
  summarizedExperiment <- bulk_long_pipeline(
    annot = annot, fastq = fastq_dir, outdir = outdir,
    genome_fa = genome_fa, minimap2_dir = minimap2_dir,
    config_file = config_file
  )
}

## ----echo=FALSE---------------------------------------------------------------
utils::sessionInfo()