diff --git a/src/vcf-reader.cpp b/src/vcf-reader.cpp index 70b0fc7..2379528 100644 --- a/src/vcf-reader.cpp +++ b/src/vcf-reader.cpp @@ -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 infoIntVec(std::string tag) { - var.getINFO(tag, v_int); - return v_int; + if (var.getINFO(tag, v_int)) { + return v_int; + } else { + return vector(); + } } vector infoFloatVec(std::string tag) { - var.getINFO(tag, v_float); - return vector(v_float.begin(), v_float.end()); + if (var.getINFO(tag, v_float)) { + return vector(v_float.begin(), v_float.end()); + } else { + return vector(); + } } vector genotypes(bool collapse) { @@ -197,10 +209,7 @@ class vcfreader { } vector formatInt(std::string tag) { - if (!var.getFORMAT(tag, v_int)) { - vector vna; - return vna; - } + if (!var.getFORMAT(tag, v_int)) { return vector(); } 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++) @@ -229,11 +238,11 @@ class vcfreader { } vector formatStr(std::string tag) { - if (!var.getFORMAT(tag, v_str)) { - vector vstr; - return vstr; + if (var.getFORMAT(tag, v_str)) { + return v_str; + } else { + return vector(); } - return v_str; } inline bool isSNP() const { return var.isSNP(); } diff --git a/src/vcfpp.h b/src/vcfpp.h index 89f86ed..00970cd 100644 --- a/src/vcfpp.h +++ b/src/vcfpp.h @@ -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 diff --git a/tests/testthat/test-vcf-reader.R b/tests/testthat/test-vcf-reader.R index 86773fc..5f0fbd2 100644 --- a/tests/testthat/test-vcf-reader.R +++ b/tests/testthat/test-vcf-reader.R @@ -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") @@ -206,5 +206,12 @@ 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")) }) + + +br$isSV() + +