<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _combine_variants(in_vcfs, out_file, ref_file, config):
 """Combine variant files, writing the header from the first non-empty input. in_vcfs is a list with each item starting with the chromosome regions, and ending with the input file. We sort by these regions to ensure the output file is in the expected order. """

    in_vcfs.sort()
    wrote_header = False
    with open(out_file, "w") as out_handle:
        for in_vcf in (x[-1] for x in in_vcfs):
            with open(in_vcf) as in_handle:
                header = list(itertools.takewhile(lambda x: x.startswith("#"),
                                                  in_handle))
                if not header[0].startswith("##fileformat=VCFv4"):
                    raise ValueError("Unexpected VCF file: %s" % in_vcf)
                for line in in_handle:
                    if not wrote_header:
                        wrote_header = True
                        out_handle.write("".join(header))
                    out_handle.write(line)
        if not wrote_header:
            out_handle.write("".join(header))
    return out_file