def vcf_writer(parser, keep, extract, args):
    """Writes the data in VCF format."""
    # The output
    output = sys.stdout if args.output == "-" else open(args.output, "w")

    try:
        # Getting the samples
        samples = np.array(parser.get_samples(), dtype=str)
        k = _get_sample_select(samples=samples, keep=keep)

        # Writing the VCF header
        output.write(_VCF_HEADER.format(
            date=datetime.today().strftime("%Y%m%d"),
            version=__version__,
            samples="\t".join(samples[k]),
        ))

        # The data generator
        generator = _get_generator(parser=parser, extract=extract, keep=k,
                                   check_maf=args.maf)

        # The number of markers extracted
        nb_extracted = 0

        for data in generator:
            # Keeping only the required genotypes
            genotypes = data.genotypes

            # Computing the alternative allele frequency
            af = np.nanmean(genotypes) / 2

            print(data.variant.chrom, data.variant.pos, data.variant.name,
                  data.reference, data.coded, ".", "PASS", "AF={}".format(af),
                  "GT:DS", sep="\t", end="", file=output)

            for geno in genotypes:
                if np.isnan(geno):
                    output.write("\t./.:.")
                else:
                    rounded_geno = int(round(geno, 0))
                    output.write("\t{}:{}".format(
                        _VCF_GT_MAP[rounded_geno], geno,
                    ))

            output.write("\n")
            nb_extracted += 1

        if nb_extracted == 0:
            logger.warning("No markers matched the extract list")

    finally:
        output.close()