## ----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", do_barcode_demultiplex = TRUE)

## ----eval=FALSE, echo=TRUE----------------------------------------------------
#  library(FLAMES)
#  # do not run if minimap2 cannot be found
#  if (!any(is.na(sys_which(c("minimap2", "k8"))))) {
#    sce <- sc_long_pipeline(
#      annotation = annot, fastq = fastq, genome_fa = genome_fa,
#      outdir = outdir, config_file = config_file, expect_cell_number = 10)
#  }

## ----eval=FALSE, echo=TRUE----------------------------------------------------
#  library(FLAMES)
#  # do not run if minimap2 cannot be found
#  if (!any(is.na(sys_which(c("minimap2", "k8"))))) {
#    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
#    ))
#    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
#    )
#    quantify_transcript(annotation = annot, outdir = outdir, config = config)
#    sce <- create_sce_from_dir(outdir = outdir, annotation = annot)
#  }

## ----eval=FALSE, message=FALSE, warning=FALSE---------------------------------
#  library(FLAMES)
#  library(MultiAssayExperiment)
#  
#  combined_sce <- combine_sce(
#      short_read_large = scmixology_lib90,
#      short_read_small = scmixology_lib10,
#      long_read_sce = scmixology_lib10_transcripts,
#      remove_duplicates = FALSE) |>
#    sc_reduce_dims(n_pcs = 20, n_hvgs = 1000)
#  
#  sc_umap_expression(gene = "ENSG00000108107", multiAssay = combined_sce)
#  sc_heatmap_expression(gene = "ENSG00000108107", multiAssay = combined_sce)
#  
#  scater::plotReducedDim(
#      experiments(combined_sce)$gene_counts,
#      dimred = "PCA",
#      colour_by = colData(combined_sce
#          [,,"gene_counts"])[,"Lib_small",drop=FALSE])

## ----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=FALSE, echo=TRUE----------------------------------------------------
#  library(FLAMES)
#  if (!any(is.na(sys_which(c("minimap2", "k8"))))) {
#    summarizedExperiment <- bulk_long_pipeline(
#      annot = annot, fastq = fastq_dir, outdir = outdir,
#      genome_fa = genome_fa, config_file = config_file
#    )
#  }

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

