Last updated: 2023-08-21

Checks: 7 0

Knit directory: G000204_duplex/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210916) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version cb969b1. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rapp.history
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    analysis/cache/
    Ignored:    data/.DS_Store
    Ignored:    scripts/

Untracked files:
    Untracked:  ._.DS_Store
    Untracked:  ._rare-mutation-detection.Rproj
    Untracked:  DOCNAME
    Untracked:  analysis/._.DS_Store
    Untracked:  analysis/._ecoli_spikeins.Rmd
    Untracked:  analysis/calc_nanoseq_metrics.Rmd
    Untracked:  data/._.DS_Store
    Untracked:  data/._metrics.rds
    Untracked:  data/ecoli/
    Untracked:  data/ecoli_k12_metrics.rds
    Untracked:  data/human_mixture_capture_region.bed
    Untracked:  data/human_mixture_refs
    Untracked:  data/human_mixture_vars
    Untracked:  data/metadata/
    Untracked:  data/metrics_efficiency_nossc.rds
    Untracked:  data/metrics_spikeins.rds
    Untracked:  data/mixtures
    Untracked:  data/natasha_anstee_metrics.rds
    Untracked:  data/ref/
    Untracked:  drop_out_rate.pdf
    Untracked:  efficiency.pdf
    Untracked:  prototype_code/
    Untracked:  stats.csv

Unstaged changes:
    Modified:   analysis/model.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/human_mixtures.Rmd) and HTML (docs/human_mixtures.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd cb969b1 mcmero 2023-08-21 Fixed INDEL/N filtering from NVC output
html 603da1a mcmero 2023-08-08 Build site.
Rmd 0c24851 mcmero 2023-08-08 Added human mixture variant analysis

Human samples are duplicate of 1% spike-in of 8393 (son of Chinese ancestry HG-005) in 8391 (son of Eastern European Ashkenazi Jewish ancestry HG-0020). Reference.

library(ggplot2)
library(data.table)
library(dplyr)
library(here)
library(tibble)
library(stringr)
library(Rsamtools)
library(GenomicRanges)
library(seqinr)
library(parallel)
library(readxl)
library(patchwork)
library(RColorBrewer)
library(UpSetR)
library(vcfR)
library(R.utils)
library(knitr)
source(here('code/load_data.R'))
source(here('code/plot.R'))
source(here('code/efficiency_nanoseq_functions.R'))
variant_dir <- here('data/human_mixture_vars')
region_bed <- here('data/human_mixture_capture_region.bed')
hg002_var_file <- here('data/human_mixture_refs/HG002_GRCh38_1_22_v4.2.1_benchmark.vcf.gz')
hg005_var_file <- here('data/human_mixture_refs/HG005_GRCh38_1_22_v4.2.1_benchmark.vcf.gz')
sample_names <- c('Human1pR1', 'Human1pR2')

# load variant data
var_df <- load_variants(variant_dir, sample_names)
hg002_vars <- read.vcfR(hg002_var_file, verbose = FALSE)
hg005_vars <- read.vcfR(hg005_var_file, verbose = FALSE)

hg002v <- data.frame(hg002_vars@fix)
hg005v <- data.frame(hg005_vars@fix)

# get capture regions
regions <- read.delim(region_bed, sep = '\t', header = FALSE)
grx <- GRanges(seqnames = regions$V1,
               ranges = IRanges(start = regions$V2, end = regions$V3))

Variant Upset plot

Here we remove any “N” variant calls and INDELs and compare the overlaps for on- and off-target variant calls.

# remove any N calls and INDELs
var_df <- filter(var_df, ALT != "N") %>%
            filter(., (REF %>% str_split("") %>% lapply(., length) %>% unlist) == 1) %>%
            mutate(ALT = lapply(ALT, filter_out_indels) %>% as.character()) %>%
            filter(., (ALT %>% str_split("") %>% lapply(., length) %>% unlist) == 1)

# filter out off-target reads
vrx <- GRanges(seqnames = var_df$CHROM,
               ranges = IRanges(start = as.numeric(var_df$POS),
                                end = as.numeric(var_df$POS) + 1))
var_df$on_target <- overlapsAny(vrx, grx)

# calculate vafs
alt_dep <- apply(var_df, 1, get_alt_dep_nvc) %>% t() %>% data.frame()
var_df$AC <- alt_dep$X1
var_df$DP <- alt_dep$X2
var_df$VAF <- var_df$AC / var_df$DP

# make upsetplot
ulist <- NULL
for(sample in sample_names) {
    ont_ids <- var_df[var_df$sample %in% sample & var_df$on_target,]$id
    oft_ids <- var_df[var_df$sample %in% sample & !var_df$on_target,]$id
    ulist[[paste(sample, "on_target")]] <- ont_ids
    ulist[[paste(sample, "off_target")]] <- oft_ids
    
}

upset(fromList(ulist), order.by='freq', nsets=4)

Version Author Date
603da1a mcmero 2023-08-08

Checking on-target rate from the bam files (based on reads that fall within the region using samtools view -c -L <region_bed> <consensus_bam>) yields a higher on-target rate than the variant analysis would suggest:

                Human1pR1   Human1pR2
capture_region  188409      122466
total_reads     330368      233768
on_target       0.5703      0.5239

Variant allele frequencies

Here we plot the allelic frequencies per-replicate in three plots:

  • all filtered variant calls: no frequency or target filtering
  • on-target variant calls: only on-target variant calls (in capture region)
  • VAF-filtered on-target variant calls: all on-target variants under <0.3% VAF
ggplot(var_df, aes(VAF)) +
    geom_histogram(binwidth = 0.05) +
    facet_grid(~sample) +
    theme_minimal() +
    ggtitle("All filtered variant calls")

Version Author Date
603da1a mcmero 2023-08-08
ggplot(var_df[var_df$on_target,], aes(VAF)) +
    geom_histogram(binwidth = 0.05) +
    facet_grid(~sample) +
    theme_minimal() +
    ggtitle("On-target variant calls")

Version Author Date
603da1a mcmero 2023-08-08
ggplot(var_df[var_df$on_target & var_df$VAF < 0.3,], aes(VAF)) +
    geom_histogram(binwidth = 0.01) +
    facet_grid(~sample) +
    theme_minimal() +
    ggtitle("On-target variant calls < 0.3 VAF")

Version Author Date
603da1a mcmero 2023-08-08

Variant comparison

Given the referene information, we check how many SNPs are present in the capture area. We also filter out any variants that are common between the two samples (if the same variant appears in both samples, we can’t differentiate the calls without some kind of phasing).

# filter out any INDELs
hg005v <- filter(hg005v, (ALT %>% str_split("") %>% lapply(., length) %>% unlist) == 1) %>%
          filter(., (REF %>% str_split("") %>% lapply(., length) %>% unlist) == 1)

# construct Granges for hg005 SNPs and keep only SNPs in capture area
hg5x <- GRanges(seqnames = hg005v$CHROM,
                ranges = IRanges(start = as.numeric(hg005v$POS),
                                 end = as.numeric(hg005v$POS) + 1),
                variant = hg005v$ALT)
hg5x <- hg5x[overlapsAny(hg5x, grx) %>% suppressWarnings()]

# construct Granges for hg002
hg2x <- GRanges(seqnames = hg002v$CHROM,
                ranges = IRanges(start = as.numeric(hg002v$POS),
                                 end = as.numeric(hg002v$POS) + 1),
                variant = hg002v$ALT)

# check variants that overlap, we will keep these if they call a different base
unique_vars <- hg5x[overlapsAny(hg5x, hg2x)]$variant != hg2x[overlapsAny(hg2x, hg5x)]$variant
hg5x <- c(hg5x[!overlapsAny(hg5x, hg2x)], hg5x[overlapsAny(hg5x, hg2x)][unique_vars])

kable(hg5x)
seqnames start end width strand variant
chr1 114714012 114714013 2 * G
chr3 128486108 128486109 2 * T
chr5 177516672 177516673 2 * T
chr9 5069837 5069838 2 * A
chr10 87970403 87970404 2 * T
chr11 32396399 32396400 2 * C
chr11 64805130 64805131 2 * A
chr11 64810148 64810149 2 * C
chr17 7676301 7676302 2 * T
kable(var_df[var_df$POS %in% start(hg5x) & var_df$CHROM %in% seqnames(hg5x),])
CHROM POS ID REF ALT QUAL FILTER INFO FORMAT Sample1 sample id on_target AC DP VAF
146 chr5 177516672 NA C T NA NA AC=2;AF=0.0104712041885 GT:AC:AF:NC 0:2:0.0104712041885:C=189,T=2, Human1pR1 chr5_177516672 TRUE 2 191 0.0104712
261 chr10 87970403 NA C T NA NA AC=2;AF=0.00393700787402 GT:AC:AF:NC 0:2:0.00393700787402:C=506,T=2, Human1pR1 chr10_87970403 TRUE 2 508 0.0039370
295 chr11 32396399 NA T C NA NA AC=4;AF=0.0121951219512 GT:AC:AF:NC 0:4:0.0121951219512:C=4,T=324, Human1pR1 chr11_32396399 TRUE 4 328 0.0121951
527 chr1 114714012 NA A T NA NA AC=5,2;AF=0.0505050505051,0.020202020202 GT:AC:AF:NC 0:5,2:0.0505050505051,0.020202020202:A=92,T=2,N=5, Human1pR2 chr1_114714012 TRUE 2 99 0.0202020
590 chr3 128486108 NA C T NA NA AC=3;AF=0.0260869565217 GT:AC:AF:NC 0:3:0.0260869565217:C=112,T=3, Human1pR2 chr3_128486108 TRUE 3 115 0.0260870
832 chr11 32396399 NA T C NA NA AC=3;AF=0.0125 GT:AC:AF:NC 0:3:0.0125:C=3,T=237, Human1pR2 chr11_32396399 TRUE 3 240 0.0125000

sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /stornext/System/data/apps/R/R-4.3.0/lib64/R/lib/libRblas.so 
LAPACK: /stornext/System/data/apps/R/R-4.3.0/lib64/R/lib/libRlapack.so;  LAPACK version 3.11.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Australia/Melbourne
tzcode source: system (glibc)

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] knitr_1.43           R.utils_2.12.2       R.oo_1.25.0         
 [4] R.methodsS3_1.8.2    vcfR_1.14.0          UpSetR_1.4.0        
 [7] RColorBrewer_1.1-3   patchwork_1.1.2      readxl_1.4.3        
[10] seqinr_4.2-30        Rsamtools_2.16.0     Biostrings_2.68.1   
[13] XVector_0.40.0       GenomicRanges_1.52.0 GenomeInfoDb_1.36.1 
[16] IRanges_2.34.1       S4Vectors_0.38.1     BiocGenerics_0.46.0 
[19] stringr_1.5.0        tibble_3.2.1         here_1.0.1          
[22] dplyr_1.1.2          data.table_1.14.8    ggplot2_3.4.2       
[25] workflowr_1.7.0     

loaded via a namespace (and not attached):
 [1] ade4_1.7-22             tidyselect_1.2.0        viridisLite_0.4.2      
 [4] farver_2.1.1            bitops_1.0-7            fastmap_1.1.1          
 [7] RCurl_1.98-1.12         promises_1.2.0.1        digest_0.6.33          
[10] lifecycle_1.0.3         cluster_2.1.4           processx_3.8.2         
[13] magrittr_2.0.3          compiler_4.3.0          rlang_1.1.1            
[16] sass_0.4.7              tools_4.3.0             utf8_1.2.3             
[19] yaml_2.3.7              labeling_0.4.2          plyr_1.8.8             
[22] BiocParallel_1.34.2     memuse_4.2-3            withr_2.5.0            
[25] grid_4.3.0              fansi_1.0.4             git2r_0.32.0           
[28] colorspace_2.1-0        scales_1.2.1            MASS_7.3-58.4          
[31] cli_3.6.1               rmarkdown_2.23          vegan_2.6-4            
[34] crayon_1.5.2            generics_0.1.3          rstudioapi_0.15.0      
[37] httr_1.4.6              ape_5.7-1               cachem_1.0.8           
[40] zlibbioc_1.46.0         splines_4.3.0           cellranger_1.1.0       
[43] vctrs_0.6.3             Matrix_1.5-4            jsonlite_1.8.7         
[46] callr_3.7.3             jquerylib_0.1.4         glue_1.6.2             
[49] codetools_0.2-19        ps_1.7.5                stringi_1.7.12         
[52] gtable_0.3.3            later_1.3.1             munsell_0.5.0          
[55] pillar_1.9.0            htmltools_0.5.5         GenomeInfoDbData_1.2.10
[58] R6_2.5.1                pinfsc50_1.2.0          rprojroot_2.0.3        
[61] evaluate_0.21           lattice_0.21-8          highr_0.10             
[64] httpuv_1.6.11           bslib_0.5.0             Rcpp_1.0.11            
[67] gridExtra_2.3           nlme_3.1-162            permute_0.9-7          
[70] mgcv_1.8-42             whisker_0.4.1           xfun_0.39              
[73] fs_1.6.3                getPass_0.2-2           pkgconfig_2.0.3