-
Notifications
You must be signed in to change notification settings - Fork 3
/
py_example.cpp
33 lines (28 loc) · 1.06 KB
/
py_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "../vcfpp.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
using namespace vcfpp;
using namespace std;
vector<int> heterozygosity(std::string vcffile, std::string region = "", std::string samples = "")
{
BcfReader vcf(vcffile, region, samples);
BcfRecord var(vcf.header); // construct a variant record
vector<int> gt; // genotype can be bool, char or int type
vector<int> hetsum(vcf.nsamples, 0);
while(vcf.getNextVariant(var))
{
var.getGenotypes(gt);
// analyze SNP variant with no genotype missingness
if(!var.isSNP()) continue; // analyze SNPs only
assert(var.ploidy() == 2); // make sure it is diploidy
for(size_t i = 0; i < gt.size() / 2; i++) hetsum[i] += abs(gt[2 * i + 0] - gt[2 * i + 1]) == 1;
}
return hetsum;
}
PYBIND11_MODULE(py_example, m)
{
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("heterozygosity", &heterozygosity,
"A function that calculates the heterozygosity for each sample in the VCF");
}