Skip to content

VCF

Support reading and writing vcf files 🔗

Record and Record-View

IVio provides the data structs ivio::vcf::record and ivio::vcf::record_view. A record_view has following layout:

struct record_view {
    std::string_view            chrom;
    int32_t                     pos;
    std::string_view            id;
    std::string_view            ref;
    std::string_view            alts;
    std::optional<float>        qual;
    std::string_view            filters;
    std::string_view            infos;
    std::string_view            formats;
    std::string_view            samples;
};
The type record looks like this:
struct record {
    std::string                 chrom;
    int32_t                     pos;
    std::string                 id;
    std::string                 ref;
    std::string                 alts;
    std::optional<float>        qual;
    std::string                 filters;
    std::string                 infos;
    std::string                 formats;
    std::string                 samples;
};

Reading

Example

#include <ivio/ivio.h>
#include <iostream>

int main(int /*argc*/, char** argv) {
    auto file = std::filesystem::path{argv[1]};
    for (auto view : ivio::vcf::reader{{file}}) {
        std::cout << view.ref << "\n";
    }
}
Output
G
T
A
T
GTC

Writing

Example

#include <ivio/ivio.h>

int main(int argc, char** argv) {
    if (argc != 2) {
        return 1;
    }
    auto file = std::filesystem::path{argv[1]};
    auto header = ivio::vcf::header { // fileformat is being set automatically
        .table = {
            {"fileDate", "20090805"},
            {"reference", "file:///somefile.fasta"},
        },
        .genotypes = {"NA00001", "NA00002", "NA00003" },
    };

    auto writer = ivio::vcf::writer{{file, header}};

    auto record = ivio::vcf::record {
        .chrom = "Name",
        .pos   = 1,
        .id    = "some id",
        .ref   = "A",
        .alt   = "G",
        .qual  = 29,
        .filters = "PASS",
        .infos   = "NS=3;DP=14;AF=0.5;DB;H2",
        .formats = "GT:GQ:DP:HQ",
        .sampmles = "0|0:48:1:51,51 1|0:48:8:51,51  1/1:43:5:.,.",
    }
    writer.write(record);
}
Output
##fileformat=VCFv4.3
##fileDate=20090805
##reference=file:///somefile.fasta
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  NA00001 NA00002 NA00003
Name    1   some id A   G   29  PASS    NS=3;DP=14;AF=0.5;DB;H2 GT:GQ:DP:HQ 0|0:48:1:51,51  1|0:48:8:51,51  1/1:43:5:.,.