Skip to content

Commit

Permalink
use vcfpp.h v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Jul 2, 2024
1 parent 99ca8df commit 9836448
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/vcf-popgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ List heterozygosity(std::string vcffile, std::string region = "", std::string sa
while (vcf.getNextVariant(var)) {
if (pass && (var.FILTER() != "PASS")) continue;
if ((qual > 0) && (var.QUAL() < qual)) continue;
var.getGenotypes(gt);
if(!var.getGenotypes(gt)) continue;
// analyze SNP variant
if (!var.isSNP()) continue;
assert(var.ploidy() == 2); // make sure it is diploidy
Expand Down
2 changes: 1 addition & 1 deletion src/vcf-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class vcfreader {
}

vector<int> genotypes(bool collapse) {
var.getGenotypes(v_int);
if (!var.getGenotypes(v_int)) { return vector<int>(); }
if (var.ploidy() == 2 && collapse) {
for (size_t i = 0; i < v_int.size(); i += 2) {
v_int[i + 1] += v_int[i];
Expand Down
20 changes: 18 additions & 2 deletions src/vcfpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file https://github.com/Zilong-Li/vcfpp/vcfpp.h
* @author Zilong Li
* @email zilong.dk@gmail.com
* @version v0.3.9
* @version v0.4.0
* @breif a single C++ file for manipulating VCF
* Copyright (C) 2022-2023.The use of this code is governed by the LICENSE file.
******************************************************************************/
Expand Down Expand Up @@ -497,7 +497,9 @@ class BcfRecord
ret = bcf_get_genotypes(header->hdr, line.get(), &gts, &ndst);
if(ret <= 0)
{
# if defined(VERBOSE)
std::cerr << "GT not present for current site. did you initilize the variant object?\n";
# endif
return false;
}
// if nploidy is not set manually. find the max nploidy using the first variant (eg. 2) resize v as
Expand Down Expand Up @@ -568,7 +570,9 @@ class BcfRecord
ret = bcf_get_genotypes(header->hdr, line.get(), &gts, &ndst);
if(ret <= 0)
{
# if defined(VERBOSE)
std::cerr << "GT not present for current site. did you initilize the variant object?\n";
# endif
return false;
}
v.resize(ret);
Expand Down Expand Up @@ -672,7 +676,7 @@ class BcfRecord
fmt = bcf_get_fmt(header->hdr, line.get(), tag.c_str());
if(!fmt)
{
throw std::invalid_argument("invalid FORMAT=" + tag + " for current variant.\n");
throw std::invalid_argument("no FORMAT=" + tag + " in the VCF header.\n");
}
nvalues = fmt->n;
// if ndst < (fmt->n+1)*nsmpl; then realloc is involved
Expand Down Expand Up @@ -767,8 +771,10 @@ class BcfRecord
}
else
{
# if defined(VERBOSE)
std::cerr << "there are multiple values for " + tag
+ " in INFO for current site. please use vector instead\n";
# endif
return false;
}
}
Expand Down Expand Up @@ -825,7 +831,9 @@ class BcfRecord
}
if(ret < 0)
{
# if defined(VERBOSE)
std::cerr << "couldn't set " + tag + " for this variant.\nplease add the tag in headerfirst.\n";
# endif
return false;
}
return true;
Expand Down Expand Up @@ -861,7 +869,9 @@ class BcfRecord

if(ret < 0)
{
# if defined(VERBOSE)
std::cerr << "couldn't set " + tag + " for this variant.\nplease add the tag in headerfirst.\n";
# endif
return false;
}
return true;
Expand Down Expand Up @@ -927,7 +937,9 @@ class BcfRecord
if(bcf_update_genotypes(header->hdr, line.get(), gt, v.size()) < 0)
{
free(gt);
# if defined(VERBOSE)
std::cerr << "couldn't set genotypes correctly.\n";
# endif
return false;
}
free(gt);
Expand Down Expand Up @@ -994,7 +1006,9 @@ class BcfRecord

if(ret < 0)
{
# if defined(VERBOSE)
std::cerr << "couldn't set format " + tag + " corectly.\n";
# endif
return false;
}
return true;
Expand Down Expand Up @@ -1026,7 +1040,9 @@ class BcfRecord
}
if(ret < 0)
{
# if defined(VERBOSE)
std::cerr << "couldn't set format " + tag + " corectly.\n";
# endif
return false;
}
return true;
Expand Down

0 comments on commit 9836448

Please sign in to comment.