## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----Inspect data, echo = TRUE, eval = FALSE----------------------------------
# 
# # Assuming a bed file with columns named chromosome, start, end, and tree
# data <- read.delim("my_argdata.bed", header = TRUE)
# 
# # Split each row into a list element
# data_list <- split(data, seq_len(nrow(data)))

## ----Inspect data2, echo = TRUE, eval = TRUE----------------------------------
### Load ARGHelpR
library(ARGHelpR)

data("rattlesnake_args")

str(rattlesnake_args, list.len = 3)

data("rattlesnake_pops")

str(rattlesnake_pops)


## ----argstats-----------------------------------------------------------------
# Separate by population, this makes the function command easier. 
pop2 <- rattlesnake_pops[which(rattlesnake_pops$species == "pop2"),]
pop1 <- rattlesnake_pops[which(rattlesnake_pops$species == "pop1"),]

# Run the analysis, single core takes about 3 minutes on a machine with 16 Gb of RAM
snake_argstats <- argstats(arg.dat = rattlesnake_args, pop1 = pop1$sample, pop2 = pop2$sample, pop1.name = "pop1", pop2.name = "pop2")

# Bind into a single data frame
snake_argstats_df <- do.call("rbind", snake_argstats)

# Inspect the statistics
head(snake_argstats_df)


## ----divergence---------------------------------------------------------------

divergence_cands <- identify_argcandidates_divergence(dat = snake_argstats_df, analysis = "all")

# Bind together into a data frame 

divergence_cands_df <- do.call("rbind", divergence_cands)

# Make a column telling us which scenario each ARG was identified as
divergence_cands_df$scenario <- rownames(divergence_cands_df)

divergence_cands_df$scenario <- sub("\\..*", "", divergence_cands_df$scenario)

# How many of each scenario do we have?
table(divergence_cands_df$scenario)

## ----introgression------------------------------------------------------------

intro_cands <- identify_argcandidates_shared(dat = snake_argstats_df, analysis = "all")

# Bind together into a data frame 

intro_cands_df <- do.call("rbind", intro_cands)

# Make a column telling us which scenario each ARG was identified as
intro_cands_df$scenario <- rownames(intro_cands_df)

intro_cands_df$scenario <- sub("\\..*", "", intro_cands_df$scenario)

# How many of each scenario do we have?
table(intro_cands_df$scenario)


## ----plot args, eval=FALSE----------------------------------------------------
# 
# # Select an ARG to plot
# arg_toplot <- snake_argstats_df[8,19]
# 
# single_plot <- argplot(arg_toplot, pop1 = pop1$sample, pop2 = pop2$sample, col = c('#7D83FF', '#007FFF'), font.size = 0.75)
# 
# # We can change the scale to
# single_plot_scale <- argplot(arg_toplot, pop1 = pop1$sample, pop2 = pop2$sample, col = c('#7D83FF', '#007FFF'), scale = c(0,275000), font.size = 0.75)

## ----arg plot, out.width= "400px", out.height= "400px", echo=FALSE, eval=TRUE, fig.align='center'----
knitr::include_graphics("single_argplot.png")

## ----multipanel plot, eval=FALSE----------------------------------------------
# # Let's plot a couple of ARGs together
# par(mfrow = c(1, 2), mar = c(0.5, 0.5, 0.5, 0.5))
# 
# arg_toplot1 <- snake_argstats_df[8,19]
# 
# arg_toplot2 <- divergence_cands_df[91,19]
# 
# plot1 <- argplot(arg_toplot1, pop1 = pop1$sample, pop2 = pop2$sample, col = c('#7D83FF', '#007FFF'), scale = c(0,275000), font.size = 0.3)
# 
# rs_plot <- argplot(arg_toplot2, pop1 = pop1$sample, pop2 = pop2$sample, col = c('#7D83FF', '#007FFF'), scale = c(0,275000), font.size = 0.3)
# 

## ----multipanel arg plot, out.width= "400px", out.height= "400px", echo=FALSE, eval=TRUE, fig.align='center'----
knitr::include_graphics("multipanel_wholeargs.png")

## ----multipanel tip plot, eval=FALSE------------------------------------------
# # Let's plot a couple of ARGs together
# par(mfrow = c(1, 2), mar = c(0.5, 0.5, 0.5, 0.5))
# 
# arg_toplot1 <- snake_argstats_df[8,19]
# 
# arg_toplot2 <- divergence_cands_df[91,19]
# 
# plot1_tips <- argplot(arg_toplot1, pop1 = pop1$sample, pop2 = pop2$sample, col = c('#7D83FF', '#007FFF'), scale = c(0,1000), font.size = 0.3)
# 
# rs_plot_tips <- argplot(arg_toplot2, pop1 = pop1$sample, pop2 = pop2$sample, col = c('#7D83FF', '#007FFF'), scale = c(0,1000), font.size = 0.3)
# 

## ----multipanel arg tips, out.width= "400px", out.height= "400px", echo=FALSE, eval=TRUE, fig.align='center'----
knitr::include_graphics("multipanel_argtips.png")

