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

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

## ----setup--------------------------------------------------------------------
library(inferRecom)
library(rtracklayer)
library(trackViewer)

## ----detect-crossovers--------------------------------------------------------
# Detect recombinations in families with two parents and three or more children
#    and families with two parents and exactly two children
# Filter for recombination with SNP-based and cM-based filters at default values
# Option to select for only SNPs that have an rs number is disabled by default

exampleDir <- system.file("extdata", package = "inferRecom")
simCEU <- file.path(exampleDir, "simCEU")
mapFemale <- file.path(exampleDir, "female_chr4.txt")
mapMale <- file.path(exampleDir, "male_chr4.txt")

pat3 <- xoDetect(simCEU, mapMale,
    familySize = 3, parent = "father", caseControl = FALSE
)
mat3 <- xoDetect(simCEU, mapFemale,
    familySize = 3, parent = "mother", caseControl = FALSE
)
pat2 <- xoDetect(simCEU, mapMale, familySize = 2, parent = "father")
mat2 <- xoDetect(simCEU, mapFemale, familySize = 2, parent = "mother")

pat3

## ----no-cm-filter-------------------------------------------------------------
pat3_0cM <- xoDetect(simCEU,
    mapMale,
    familySize = 3,
    parent = "father",
    caseControl = FALSE,
    cmFilter = 0
)

pat3_0cM

## ----plot-case-control--------------------------------------------------------
mat3CC <- xoDetect(simCEU,
    mapFemale,
    familySize = 3,
    parent = "mother",
    caseControl = TRUE
)

# Add feature column for compatibility with "gene" type in trackViewer
mat3CC$case$feature <- "interval"
mat3CC$control$feature <- "interval"

caseTrack <- new(
    "track",
    dat = mat3CC$case,
    type = "gene",
    format = "BED"
)

setTrackStyleParam(caseTrack, "color", "black")

controlTrack <- new(
    "track",
    dat = mat3CC$control,
    type = "gene",
    format = "BED"
)

setTrackStyleParam(controlTrack, "color", "blue")

tl <- trackList(caseTrack, controlTrack)
viewRegion <- GRanges("chr4", IRanges(78000000, 79500000))
viewTracks(tl, gr = viewRegion, autoOptimizeStyle = TRUE)

## ----export-bed---------------------------------------------------------------
# export(mat3CC$case, "~/path/to/caseOutput.bed", format = "BED")
# 
# export(mat3CC$control, "~/path/to/controlOutput.bed", format = "BED")

## ----browser-figure-----------------------------------------------------------
knitr::include_graphics("figures/example-browser.png")

## ----runs-of-homozygosity-----------------------------------------------------
sexavg_chr4 <- file.path(exampleDir, "sexavg_chr4.txt")
hzRun(simCEU, sexavg_chr4)

## ----phase-haplotypes---------------------------------------------------------
phased <- xoPhase(simCEU, pat3, mat3)
phased

f6 <- colData(phased)$familyId == "F6"
geno(phased)$GT[1158:1162, f6]

## ----write-vcf, eval=FALSE----------------------------------------------------
# writeVcf(phased, "~/path/to/phased.vcf")

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

