biocpp / biocpp-io

BioC++ Input/Output library
https://biocpp.github.io
BSD 3-Clause "New" or "Revised" License
8 stars 5 forks source link

[BUG] Header not printed if body is empty for ostringstream output. #27

Closed Irallia closed 2 years ago

Irallia commented 2 years ago

For the following snippet I would expect a VCF output with an empty body, but the output is empty.

#include <bio/var_io/writer.hpp>

int main()
{
    bio::var_io::header hdr{};
    hdr.column_labels = {"CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO", "FORMAT", "TEST"};

    std::ostringstream stream{};
    bio::var_io::writer writer{stream, bio::vcf{}};
    writer.set_header(hdr);

    std::cout << stream.str();
}

but

#include <bio/var_io/writer.hpp>

int main()
{
    bio::var_io::header hdr{};
    hdr.column_labels = {"CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO", "FORMAT", "TEST"};

    std::ostringstream stream{};
    bio::var_io::writer writer{stream, bio::vcf{}};
    writer.set_header(hdr);
    bio::var_io::default_record<> record{};
    writer.push_back(record);

    std::cout << stream.str();
}

prints

##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  TEST
.   0   .       .   0   .   .   .   
h-2 commented 2 years ago

last-resort-writing (when no records are provided), happens in the destructor. Can you check whether the following works:

#include <bio/var_io/writer.hpp>

int main()
{
    {
        bio::var_io::header hdr{};
        hdr.column_labels = {"CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO", "FORMAT", "TEST"};

        std::ostringstream stream{};
        bio::var_io::writer writer{stream, bio::vcf{}};
        writer.set_header(hdr);
    }

    std::cout << stream.str();
}

?

Irallia commented 2 years ago

That works.

h-2 commented 2 years ago

That works.

That's good to hear. That means it also works when exiting the program. In general, you cannot assum that the data appears immediately in the buffer of the string-stream, as the format may do additional buffering, too.

I will definitely need to improve the documentation on this 😇

You are of course always invited to do PRs with improvements or suggestions!