Skip to content

Commit

Permalink
add vcfreader@samples()
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Jan 4, 2024
1 parent d99d313 commit 948a007
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# vcfppR 0.3.7

* add `vcfreader@samples()`

# vcfppR 0.3.6

* add `vcfreader::line`
Expand Down
1 change: 1 addition & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ heterozygosity <- function(vcffile, region = "", samples = "-", pass = FALSE, qu
#' @field hasOTHER Test if current variant has a OTHER or not
#' @field hasOVERLAP Test if current variant has a OVERLAP or not
#' @field nsamples Return the number of samples
#' @field samples Return a vector of samples id
#' @field header Return the raw string of the vcf header
#' @field string Return the raw string of current variant including newline
#' @field line Return the raw string of current variant without newline
Expand Down
2 changes: 2 additions & 0 deletions man/vcfreader.Rd

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

3 changes: 3 additions & 0 deletions src/vcf-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ using namespace std;
//' @field hasOTHER Test if current variant has a OTHER or not
//' @field hasOVERLAP Test if current variant has a OVERLAP or not
//' @field nsamples Return the number of samples
//' @field samples Return a vector of samples id
//' @field header Return the raw string of the vcf header
//' @field string Return the raw string of current variant including newline
//' @field line Return the raw string of current variant without newline
Expand Down Expand Up @@ -245,6 +246,7 @@ class vcfreader {
return v.size() / nsamples();
}
inline std::string header() const { return br.header.asString(); }
inline std::vector<std::string> samples() const { return br.header.getSamples(); }
inline std::string string() const { return var.asString(); }
inline std::string line() {
std::string s = var.asString();
Expand Down Expand Up @@ -363,6 +365,7 @@ RCPP_MODULE(vcfreader) {
.method("hasOTHER", &vcfreader::hasOTHER)
.method("hasOVERLAP", &vcfreader::hasOVERLAP)
.method("nsamples", &vcfreader::nsamples)
.method("samples", &vcfreader::samples)
.method("header", &vcfreader::header)
.method("string", &vcfreader::string)
.method("line", &vcfreader::line)
Expand Down
14 changes: 9 additions & 5 deletions tests/testthat/test-vcf-reader.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ svfile <- system.file("extdata", "sv.vcf.gz", package="vcfppR")

test_that("vcfreader: constructor with vcfffile only", {
br <- vcfreader$new(vcffile)
br$variant()
expect_true(br$variant())
expect_identical(br$chr(), "chr21")
expect_identical(br$pos(), 5030082L)
expect_identical(br$id(), "chr21:5030082:G:A")
Expand All @@ -15,7 +15,7 @@ test_that("vcfreader: constructor with vcfffile only", {

test_that("vcfreader: constructor with vcfffile and region", {
br <- vcfreader$new(vcffile, "chr21:5030082-")
br$variant()
expect_true(br$variant())
expect_identical(br$chr(), "chr21")
expect_identical(br$pos(), 5030082L)
expect_identical(br$id(), "chr21:5030082:G:A")
Expand All @@ -25,7 +25,8 @@ test_that("vcfreader: constructor with vcfffile and region", {

test_that("vcfreader: constructor with vcfffile, region and samples", {
br <- vcfreader$new(vcffile, "chr21:5030082-", "HG00097,HG00099")
br$variant()
expect_identical(br$samples(), c("HG00097", "HG00099"))
expect_true(br$variant())
expect_identical(br$genotypes(F), rep(0L, 4))
expect_identical(br$chr(), "chr21")
expect_identical(br$pos(), 5030082L)
Expand All @@ -36,20 +37,23 @@ test_that("vcfreader: constructor with vcfffile, region and samples", {

test_that("vcfreader: get FORMAT item with Float type", {
br <- vcfreader$new(vcffile, "chr21:5030347-", "HG00097,HG00099")
br$variant()
expect_identical(br$samples(), c("HG00097", "HG00099"))
expect_true(br$variant())
expect_identical(br$pos(), 5030347L)
expect_identical(all(is.na(br$formatFloat("AB"))), TRUE)
})

test_that("vcfreader: get FORMAT item with Integer type", {
br <- vcfreader$new(vcffile, "chr21:5030347-", "HG00097,HG00099")
br$variant()
expect_identical(br$samples(), c("HG00097", "HG00099"))
expect_true(br$variant())
expect_identical(br$pos(), 5030347L)
expect_identical(br$formatInt("AD"), c(4L, 0L, 16L, 0L))
})

test_that("vcfreader: get FORMAT item with String type", {
br <- vcfreader$new(svfile, "chr21:5114000-", "HG00096,HG00097")
expect_identical(br$samples(), c("HG00096", "HG00097"))
expect_true(br$variant())
expect_identical(br$pos(), 5114000L)
expect_identical(br$formatStr("EV"), c("RD","RD"))
Expand Down

0 comments on commit 948a007

Please sign in to comment.