nodes: 0, module; 1, function_definition; 2, function_name:_combine_variants; 3, parameters; 4, block; 5, identifier:in_vcfs; 6, identifier:out_file; 7, identifier:ref_file; 8, identifier:config; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, with_statement; 13, return_statement; 14, comment:"""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.
      """; 15, call; 16, assignment; 17, with_clause; 18, block; 19, identifier:out_file; 20, attribute; 21, argument_list; 22, identifier:wrote_header; 23, False; 24, with_item; 25, for_statement; 26, if_statement; 27, identifier:in_vcfs; 28, identifier:sort; 29, as_pattern; 30, identifier:in_vcf; 31, generator_expression; 32, block; 33, not_operator; 34, block; 35, call; 36, as_pattern_target; 37, subscript; 38, for_in_clause; 39, with_statement; 40, identifier:wrote_header; 41, expression_statement; 42, identifier:open; 43, argument_list; 44, identifier:out_handle; 45, identifier:x; 46, unary_operator; 47, identifier:x; 48, identifier:in_vcfs; 49, with_clause; 50, block; 51, call; 52, identifier:out_file; 53, string:"w"; 54, integer:1; 55, with_item; 56, expression_statement; 57, if_statement; 58, for_statement; 59, attribute; 60, argument_list; 61, as_pattern; 62, assignment; 63, not_operator; 64, block; 65, identifier:line; 66, identifier:in_handle; 67, block; 68, identifier:out_handle; 69, identifier:write; 70, call; 71, call; 72, as_pattern_target; 73, identifier:header; 74, call; 75, call; 76, raise_statement; 77, if_statement; 78, expression_statement; 79, attribute; 80, argument_list; 81, identifier:open; 82, argument_list; 83, identifier:in_handle; 84, identifier:list; 85, argument_list; 86, attribute; 87, argument_list; 88, call; 89, not_operator; 90, block; 91, call; 92, string:""; 93, identifier:join; 94, identifier:header; 95, identifier:in_vcf; 96, call; 97, subscript; 98, identifier:startswith; 99, string:"##fileformat=VCFv4"; 100, identifier:ValueError; 101, argument_list; 102, identifier:wrote_header; 103, expression_statement; 104, expression_statement; 105, attribute; 106, argument_list; 107, attribute; 108, argument_list; 109, identifier:header; 110, integer:0; 111, binary_operator:"Unexpected VCF file: %s" % in_vcf; 112, assignment; 113, call; 114, identifier:out_handle; 115, identifier:write; 116, identifier:line; 117, identifier:itertools; 118, identifier:takewhile; 119, lambda; 120, identifier:in_handle; 121, string:"Unexpected VCF file: %s"; 122, identifier:in_vcf; 123, identifier:wrote_header; 124, True; 125, attribute; 126, argument_list; 127, lambda_parameters; 128, call; 129, identifier:out_handle; 130, identifier:write; 131, call; 132, identifier:x; 133, attribute; 134, argument_list; 135, attribute; 136, argument_list; 137, identifier:x; 138, identifier:startswith; 139, string:"#"; 140, string:""; 141, identifier:join; 142, identifier:header
code: 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