Skip to content

Commit

Permalink
update vcfreader
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Jun 28, 2024
1 parent 4973704 commit 99ca8df
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
43 changes: 26 additions & 17 deletions src/vcf-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,38 @@ class vcfreader {

int infoInt(std::string tag) {
int i;
var.getINFO(tag, i);
return i;
if (var.getINFO(tag, i)) {
return i;
} else {
return NA_INTEGER;
}
}
double infoFloat(std::string tag) {
float f;
var.getINFO(tag, f);
return (double)f;
if (var.getINFO(tag, f)) {
return (double)f;
} else {
return NA_REAL;
}
}
std::string infoStr(std::string tag) {
std::string s;
std::string s{""};
var.getINFO(tag, s);
return s;
}
vector<int> infoIntVec(std::string tag) {
var.getINFO(tag, v_int);
return v_int;
if (var.getINFO(tag, v_int)) {
return v_int;
} else {
return vector<int>();
}
}
vector<double> infoFloatVec(std::string tag) {
var.getINFO(tag, v_float);
return vector<double>(v_float.begin(), v_float.end());
if (var.getINFO(tag, v_float)) {
return vector<double>(v_float.begin(), v_float.end());
} else {
return vector<double>();
}
}

vector<int> genotypes(bool collapse) {
Expand All @@ -197,10 +209,7 @@ class vcfreader {
}

vector<int> formatInt(std::string tag) {
if (!var.getFORMAT(tag, v_int)) {
vector<int> vna;
return vna;
}
if (!var.getFORMAT(tag, v_int)) { return vector<int>(); }
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 Down Expand Up @@ -229,11 +238,11 @@ class vcfreader {
}

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

inline bool isSNP() const { return var.isSNP(); }
Expand Down
2 changes: 1 addition & 1 deletion src/vcfpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ class BcfRecord
fmt = bcf_get_fmt(header->hdr, line.get(), tag.c_str());
if(!fmt)
{
throw std::invalid_argument("no FORMAT=" + tag + " in the VCF header.\n");
throw std::invalid_argument("invalid FORMAT=" + tag + " for current variant.\n");
}
nvalues = fmt->n;
// if ndst < (fmt->n+1)*nsmpl; then realloc is involved
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-vcf-reader.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ test_that("vcfreader: remove tag from FORMAT", {
expect_identical(s[9], "GT:DP:GQ:PL")
expect_identical(s[10], "1/1:2:6:64,6,0")
## AD was removed, so we get integer(0)
ad <- br$formatInt("AD")
ad <- br$formatInt("AD")
expect_identical(length(ad),0L)
## output current variant to another vcf
outvcf <- file.path(tempdir(), "test.vcf.gz")
Expand All @@ -206,5 +206,8 @@ test_that("vcfreader: remove tag from FORMAT", {
br$variant()
s <- unlist(strsplit(br$line(), "\t"))
expect_identical(s[9], "GT:DP:GQ:PL")
expect_identical(s[10], "1/1:2:6:64,6,0")
## AD doesn't exist. so we get error, is this a bad design?
expect_error(br$formatInt("AD"))
})

0 comments on commit 99ca8df

Please sign in to comment.