Skip to content

Commit

Permalink
vcfcomp can eat objects of vcftable class
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Sep 10, 2024
1 parent 56a5433 commit a4907a0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
25 changes: 15 additions & 10 deletions R/vcf-compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#' \code{vcfcomp} implements various statisitcs to compare two VCF/BCF files,
#' e.g. report genotype concocrdance, correlation stratified by allele frequency.
#'
#' @param test path to the first VCF/BCF file referred as test, or saved RDS file.
#' @param test path to the comparision file (test), which can be a VCF/BCF file, vcftable object or saved RDS file.
#'
#' @param truth path to the second VCF/BCF file referred as truth, or saved RDS file.
#' @param truth path to the baseline file (truth),which can be a VCF/BCF file, vcftable object or saved RDS file.
#'
#' @param formats character vector. the FORMAT tags to extract for the test and truth respectively.
#' default c("DS", "GT") extracts 'DS' of the target and 'GT' of the truth.
Expand Down Expand Up @@ -48,7 +48,7 @@
#' test <- system.file("extdata", "imputed.gt.vcf.gz", package="vcfppR")
#' truth <- system.file("extdata", "imputed.gt.vcf.gz", package="vcfppR")
#' samples <- "HG00133,HG00143,HG00262"
#' res <- vcfcomp(test, truth, stats="f1", format=c('GT','GT'), samples=samples, setid=TRUE)
#' res <- vcfcomp(test, truth, stats="f1", samples=samples, setid=TRUE)
#' str(res)
#' @export
vcfcomp <- function(test, truth,
Expand Down Expand Up @@ -78,12 +78,17 @@ vcfcomp <- function(test, truth,
formats[1] <- "GT"
}
collapse <- ifelse(stats=="pse", FALSE, TRUE)
d1 <- tryCatch( { suppressWarnings(readRDS(test)) }, error = function(e) {
vcftable(test, format = formats[1], collapse = collapse, ...)
} )
d2 <- tryCatch( { suppressWarnings(readRDS(truth)) }, error = function(e) {
vcftable(truth, format = formats[2], collapse = collapse, ...)
} )
if(is(test, "vcftable") & is(truth, "vcftable")) {
d1 <- test
d2 <- truth
} else {
d1 <- tryCatch( { suppressWarnings(readRDS(test)) }, error = function(e) {
vcftable(test, format = formats[1], collapse = collapse, ...)
} )
d2 <- tryCatch( { suppressWarnings(readRDS(truth)) }, error = function(e) {
vcftable(truth, format = formats[2], collapse = collapse, ...)
} )
}
if(!is.null(names) & is.vector(names)) d1$samples <- names
## chr pos ref alt af
sites <- intersect(d1$id, d2$id)
Expand Down Expand Up @@ -126,7 +131,7 @@ vcfcomp <- function(test, truth,
if(stats == "r2")
return(list(samples = d1$samples, r2=res.r2))
## F1 and NRC
d1 <- vcftable(test, format = "GT", setid = TRUE, ...)
d1 <- vcftable(test, format = "GT", ...)
ds <- d1[[10]]
ds <- ds[match(sites, d1$id), ]
rownames(ds) <- sites
Expand Down
6 changes: 3 additions & 3 deletions man/vcfcomp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-vcf-comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ test_that("can work for PSE", {
})



test_that("can work for vcftable objects", {
skip_on_os(c("windows"), arch = NULL)
samples <- "HG00673,NA10840"
test <- vcftable(rawvcf, samples = samples, setid = T)
truth <- vcftable(imputedvcf, samples = samples, setid = T)
res <- vcfcomp(test, truth, stats = "nrc", samples = samples,
by.sample = TRUE, bins = c(0,1), setid = TRUE)
expect_identical(paste0(res$samples, collapse = ","), samples)
expect_identical(unlist(res[[2]][[3]]), rep(1,2))
})

0 comments on commit a4907a0

Please sign in to comment.