if (1 == 0) {
    
    library("testthat"); library("STITCH"); library("rrbgen")
    dir <- "~/proj/STITCH/"
    setwd(paste0(dir, "/STITCH/R"))
    a <- dir(pattern = "*.R")
    b <- grep("~", a)
    if (length(b) > 0) {
        a <- a[-b]
    }
    o <- sapply(a, source)
    setwd(dir)
    Sys.setenv(PATH = paste0(getwd(), ":", Sys.getenv("PATH")))

}

n_snps <- 20
reads_span_n_snps <- 6
chr <- "rep_1.1"
original_chr <- chr
n_reads <- 5 / (reads_span_n_snps / n_snps) ## want about 5X / sample
extension <- c("bgvcf" = ".vcf.gz", "bgen" = ".bgen")

K <- 10
phasemaster <- matrix(sample(c(0, 1), replace = TRUE, n_snps * K), ncol = K)
L <- 10 + seq(1, 3 * n_snps, 3)
L_original <- L




data_package <- make_acceptance_test_data_package(
    n_samples = 10,
    n_snps = n_snps,
    n_reads = n_reads,
    L = L,
    seed = 3,
    chr = chr,
    K = 2,
    reads_span_n_snps = reads_span_n_snps,
    phasemaster = phasemaster
)

outputdir <- make_unique_tempdir()
set.seed(478)

write.table(
    "/var/folders/yy/5xsy75511pg691vl8jkmr209vrqq7r/T/RtmpatTGyO/temp/file2.bam",
    file = "/var/folders/yy/5xsy75511pg691vl8jkmr209vrqq7r/T/RtmpatTGyO/temp/file2.bamlist",
    row.names = FALSE,
    col.names = FALSE,
    sep = "\t",
    quote = FALSE
)
        

x <-  "/var/folders/yy/5xsy75511pg691vl8jkmr209vrqq7r/T/RtmpatTGyO/temp/file2.bamlist"
y <- "/var/folders/yy/5xsy75511pg691vl8jkmr209vrqq7r/T/RtmpatTGyO/temp/pos.txt"
STITCH(
    chr = data_package$chr,
    bamlist = data_package$bamlist,
    posfile = data_package$posfile,
    outputdir = tempdir(),
    K = K,
    S = 1,
    nGen = 100,
    nCores = 1
)


    ## recall - whole ref is A otherwise

    ## https://samtools.github.io/hts-specs/SAMv1.pdf
    ## recall that soft clipped reads give first non-soft clipped position
    ## so below, 17 is the start of the non-soft clipped part
    ## so the read including soft clipping starts at 15
    bam_file <- make_simple_bam(
        file_stem = file.path(tempdir(), "simple"),
        sam = make_simple_sam_text(
            list(
                c("r001", "0", chr, "17", "60",
                  "2S2M2S", "*", "0", "0",
                  "ccCCgg", ":;<=>?") # bqs 25-30
            ),
            chr
        )
    )

    regionName <- "XXX"
    loadBamAndConvert(
        iBam = 1,
        L = as.integer(pos[, 2]),
        pos = pos,
        nSNPs = as.integer(nrow(pos)),
        bam_files = "/var/folders/yy/5xsy75511pg691vl8jkmr209vrqq7r/T/RtmpatTGyO/temp/sample.bam",
        N = 1,
        sampleNames = "test-name-1-read-soft-clipped",
        inputdir = tempdir(),
        regionName = regionName,
        tempdir = tempdir(),
        chr = chr,
        chrStart = 1,
        chrEnd = 1000,
        useSoftClippedBases = TRUE
    )

## seems to work
## read.table(file.path(outputdir, "stitch.rep_1.vcf.gz"))


av_depth <- 0.1
K <- 10
Ns <- round(10 ** seq(2, 4, length.out = 10))
y <- sapply(Ns, function(N) {
    ## depth * N / 10 >= K
    return(av_depth * N / 10)
})
plot(x = Ns, y)
abline(h = 10)
