Skip to content

Commit

Permalink
fix vcfreader and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Jun 28, 2024
1 parent 6f7ba58 commit 4973704
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/vcf-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ class vcfreader {
}

vector<int> formatInt(std::string tag) {
var.getFORMAT(tag, v_int);
if (!var.getFORMAT(tag, v_int)) {
vector<int> vna;
return vna;
}
int nvals = v_int.size() / br.nsamples; // how many values per sample
for (int i = 0; i < br.nsamples; i++) {
for (int j = 0; j < nvals; j++)
Expand All @@ -211,7 +214,7 @@ class vcfreader {

vector<double> formatFloat(std::string tag) {
vector<double> vecd;
var.getFORMAT(tag, v_float);
if (!var.getFORMAT(tag, v_float)) return vecd;
int nvals = v_float.size() / br.nsamples; // how many values per sample
vecd.resize(v_float.size());
for (int i = 0; i < br.nsamples; i++) {
Expand All @@ -226,7 +229,10 @@ class vcfreader {
}

vector<std::string> formatStr(std::string tag) {
var.getFORMAT(tag, v_str);
if (!var.getFORMAT(tag, v_str)) {
vector<std::string> vstr;
return vstr;
}
return v_str;
}

Expand Down
11 changes: 9 additions & 2 deletions tests/testthat/test-vcf-reader.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,20 @@ test_that("vcfreader: read and output variant", {

test_that("vcfreader: remove tag from FORMAT", {
br <- vcfreader$new(vcffile)
br$variant()
br$variant() ## first variant
s <- unlist(strsplit(br$line(), "\t"))
expect_identical(s[9], "GT:AD:DP:GQ:PL")
br$variant() ## second variant
br$rmFormatTag("AD")
br$rmFormatTag("AB")
br$rmFormatTag("PGT")
br$rmFormatTag("PID")
s <- unlist(strsplit(br$line(), "\t"))
expect_identical(s[9], "GT:DP:GQ:PL")
expect_error(br$formatInt("AD"))
expect_identical(s[10], "1/1:2:6:64,6,0")
## AD was removed, so we get integer(0)
ad <- br$formatInt("AD")
expect_identical(length(ad),0L)
## output current variant to another vcf
outvcf <- file.path(tempdir(), "test.vcf.gz")
file.create(outvcf)
Expand Down

0 comments on commit 4973704

Please sign in to comment.