nodes: 0, module; 1, function_definition; 2, function_name:sort_by_ref; 3, parameters; 4, block; 5, identifier:vcf_file; 6, identifier:data; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, return_statement; 11, comment:"""Sort a VCF file by genome reference and position, adding contig information.
      """; 12, assignment; 13, not_operator; 14, block; 15, call; 16, identifier:out_file; 17, binary_operator:"%s-prep.vcf.gz" % utils.splitext_plus(vcf_file)[0]; 18, call; 19, with_statement; 20, identifier:bgzip_and_index; 21, argument_list; 22, string:"%s-prep.vcf.gz"; 23, subscript; 24, attribute; 25, argument_list; 26, with_clause; 27, block; 28, identifier:out_file; 29, subscript; 30, call; 31, integer:0; 32, identifier:utils; 33, identifier:file_uptodate; 34, identifier:out_file; 35, identifier:vcf_file; 36, with_item; 37, expression_statement; 38, with_statement; 39, expression_statement; 40, expression_statement; 41, with_statement; 42, identifier:data; 43, string:"config"; 44, attribute; 45, argument_list; 46, as_pattern; 47, assignment; 48, with_clause; 49, block; 50, assignment; 51, assignment; 52, with_clause; 53, block; 54, identifier:utils; 55, identifier:splitext_plus; 56, identifier:vcf_file; 57, call; 58, as_pattern_target; 59, identifier:header_file; 60, binary_operator:"%s-header.txt" % utils.splitext_plus(tx_out_file)[0]; 61, with_item; 62, for_statement; 63, identifier:cat_cmd; 64, conditional_expression:"zcat" if vcf_file.endswith("vcf.gz") else "cat"; 65, identifier:cmd; 66, parenthesized_expression; 67, with_item; 68, expression_statement; 69, identifier:file_transaction; 70, argument_list; 71, identifier:tx_out_file; 72, string:"%s-header.txt"; 73, subscript; 74, as_pattern; 75, identifier:region; 76, call; 77, block; 78, string:"zcat"; 79, call; 80, string:"cat"; 81, concatenated_string; 82, call; 83, call; 84, identifier:data; 85, identifier:out_file; 86, call; 87, integer:0; 88, call; 89, as_pattern_target; 90, attribute; 91, argument_list; 92, expression_statement; 93, attribute; 94, argument_list; 95, string:"{cat_cmd} {vcf_file} | grep -v ^##contig | bcftools annotate -h {header_file} | "; 96, string:"vt sort -m full -o {tx_out_file} -"; 97, attribute; 98, argument_list; 99, attribute; 100, argument_list; 101, attribute; 102, argument_list; 103, identifier:open; 104, argument_list; 105, identifier:out_handle; 106, identifier:ref; 107, identifier:file_contigs; 108, call; 109, subscript; 110, call; 111, identifier:vcf_file; 112, identifier:endswith; 113, string:"vcf.gz"; 114, identifier:utils; 115, identifier:chdir; 116, call; 117, identifier:do; 118, identifier:run; 119, call; 120, string:"Sort VCF by reference"; 121, identifier:utils; 122, identifier:splitext_plus; 123, identifier:tx_out_file; 124, identifier:header_file; 125, string:"w"; 126, attribute; 127, argument_list; 128, identifier:data; 129, string:"config"; 130, attribute; 131, argument_list; 132, attribute; 133, argument_list; 134, attribute; 135, argument_list; 136, identifier:dd; 137, identifier:get_ref_file; 138, identifier:data; 139, identifier:out_handle; 140, identifier:write; 141, binary_operator:"##contig=<ID=%s,length=%s>\n" % (region.name, region.size); 142, attribute; 143, identifier:dirname; 144, identifier:tx_out_file; 145, identifier:cmd; 146, identifier:format; 147, dictionary_splat; 148, string:"##contig=<ID=%s,length=%s>\n"; 149, tuple; 150, identifier:os; 151, identifier:path; 152, call; 153, attribute; 154, attribute; 155, identifier:locals; 156, argument_list; 157, identifier:region; 158, identifier:name; 159, identifier:region; 160, identifier:size
code: def sort_by_ref(vcf_file, data):
      """Sort a VCF file by genome reference and position, adding contig information.
      """
      out_file = "%s-prep.vcf.gz" % utils.splitext_plus(vcf_file)[0]
      if not utils.file_uptodate(out_file, vcf_file):
          with file_transaction(data, out_file) as tx_out_file:
              header_file = "%s-header.txt" % utils.splitext_plus(tx_out_file)[0]
              with open(header_file, "w") as out_handle:
                  for region in ref.file_contigs(dd.get_ref_file(data), data["config"]):
                      out_handle.write("##contig=<ID=%s,length=%s>\n" % (region.name, region.size))
              cat_cmd = "zcat" if vcf_file.endswith("vcf.gz") else "cat"
              cmd = ("{cat_cmd} {vcf_file} | grep -v ^##contig | bcftools annotate -h {header_file} | "
                     "vt sort -m full -o {tx_out_file} -")
              with utils.chdir(os.path.dirname(tx_out_file)):
                  do.run(cmd.format(**locals()), "Sort VCF by reference")
      return bgzip_and_index(out_file, data["config"])