### R code from vignette source 'SNPAssayR-workflows.Rnw'

###################################################
### code chunk number 1: setup
###################################################
library(SNPAssayR)
old_options <- options(width = 68)


###################################################
### code chunk number 2: input
###################################################
l <- read_locus(
  ">target\nACGTTGAATTCAAAAAAGTC\n>copy\nACGTTGAATTCAAAAAAGTC",
  "fixture", record = "target")
s <- validate_snp(l, 6, "G", "A")
stopifnot(s$alternative_sequence == "ACGTTAAATTCAAAAAAGTC")


###################################################
### code chunk number 3: tutorial-run
###################################################
source(system.file("examples", "tutorial_550bp.R",
                   package = "SNPAssayR"))


###################################################
### code chunk number 4: tutorial-output
###################################################
tutorial_primers[, c("method", "role", "sequence_5prime_to_3prime")]
range(tutorial_primers$tm_celsius)
tutorial_assays$tetra$candidates[[1]]$intended_products


###################################################
### code chunk number 5: caps
###################################################
caps <- design_caps(s, restriction_enzymes()[1, , drop = FALSE],
  primer_lengths = 3, product_min = 20, product_max = 20,
  gc_range = c(0, 1), tm_range = c(-250, 100),
  max_tm_difference = 100, screening_mismatches = 0)
stopifnot(length(caps$candidates) == 1)
caps$summary
caps$candidates[[1]]$digests$reference$fragments


###################################################
### code chunk number 6: dcaps
###################################################
d <- validate_snp(read_locus("ACGGACTTCAAAGTC", "fixture"), 9, "C", "G")
dcaps <- design_dcaps(d, restriction_enzymes()[1, , drop = FALSE],
  primer_lengths = c(3, 8), primer_gaps = 0,
  product_min = 15, product_max = 15, gc_range = c(0, 1),
  tm_range = c(-250, 100), max_tm_difference = 100)
stopifnot(length(dcaps$candidates) == 1)
dcaps$candidates[[1]]$intended_product$edits


###################################################
### code chunk number 7: arms
###################################################
a <- validate_snp(read_locus("ACGAAAGTC", "fixture"), 3, "G", "T")
arms <- design_arms(a, strands = "+", primer_lengths = 3,
  product_min = 9, product_max = 9, gc_range = c(0, 1),
  tm_range = c(-250, 100), max_tm_difference = 100)
stopifnot(length(arms$candidates) == 1)
arms$candidates[[1]]$ideal_reaction_profiles[,
  c("genotype", "reaction", "ideal_product_present", "ideal_sizes_bp")]


###################################################
### code chunk number 8: tetra
###################################################
t <- validate_snp(read_locus("ACGTCAGTACGATCGTACGTTAGCGATCGA",
                            "fixture"), 14, "C", "A")
tetra <- design_tetra_arms(t, primer_lengths = 3,
  product_min = 12, product_max = 25, gc_range = c(0, 1),
  tm_range = c(-250, 150), max_tm_difference = 200,
  min_size_difference = 1, pool_size = 2, max_candidates = 1)
stopifnot(length(tetra$candidates) == 1)
tetra$candidates[[1]]$intended_products


###################################################
### code chunk number 9: export
###################################################
rank_assays(caps)$components[,
  c("candidate_id", "quality", "evidence", "score", "rank")]
bundle <- assay_bundle(caps, selected = 1)
destination <- tempfile("snpassay-export-")
export_assay(bundle, destination, figure_formats = "pdf")
rerun <- replay_assay(readRDS(
  file.path(destination, "analysis-bundle.rds")))
stopifnot(identical(rerun, caps))
unlink(destination, recursive = TRUE)
options(old_options)


###################################################
### code chunk number 10: software-citation
###################################################
citation("SNPAssayR")
