nodes: 0, module; 1, function_definition; 2, function_name:_combine_regions; 3, parameters; 4, block; 5, identifier:all_regions; 6, identifier:ref_regions; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, function_definition; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, expression_statement; 15, return_statement; 16, comment:"""Combine multiple BEDtools regions of regions into sorted final BEDtool.
      """; 17, assignment; 18, pattern_list; 19, call; 20, block; 21, function_name:wchrom_key; 22, parameters; 23, block; 24, assignment; 25, identifier:region_group; 26, identifier:all_regions; 27, block; 28, call; 29, assignment; 30, call; 31, identifier:chrom_order; 32, dictionary; 33, identifier:i; 34, identifier:x; 35, identifier:enumerate; 36, argument_list; 37, expression_statement; 38, identifier:x; 39, expression_statement; 40, return_statement; 41, identifier:all_intervals; 42, list; 43, for_statement; 44, attribute; 45, argument_list; 46, identifier:bed_lines; 47, list_comprehension; 48, attribute; 49, argument_list; 50, identifier:ref_regions; 51, assignment; 52, assignment; 53, tuple; 54, identifier:region; 55, identifier:region_group; 56, block; 57, identifier:all_intervals; 58, identifier:sort; 59, keyword_argument; 60, binary_operator:"%s\t%s\t%s" % (c, s, e); 61, for_in_clause; 62, identifier:pybedtools; 63, identifier:BedTool; 64, call; 65, keyword_argument; 66, subscript; 67, identifier:i; 68, pattern_list; 69, identifier:x; 70, subscript; 71, identifier:start; 72, identifier:end; 73, expression_statement; 74, identifier:key; 75, identifier:wchrom_key; 76, string:"%s\t%s\t%s"; 77, tuple; 78, tuple_pattern; 79, identifier:all_intervals; 80, attribute; 81, argument_list; 82, identifier:from_string; 83, True; 84, identifier:chrom_order; 85, attribute; 86, identifier:chrom; 87, identifier:start; 88, identifier:end; 89, identifier:chrom_order; 90, identifier:chrom; 91, call; 92, identifier:c; 93, identifier:s; 94, identifier:e; 95, identifier:c; 96, identifier:s; 97, identifier:e; 98, string:"\n"; 99, identifier:join; 100, identifier:bed_lines; 101, identifier:x; 102, identifier:chrom; 103, attribute; 104, argument_list; 105, identifier:all_intervals; 106, identifier:append; 107, tuple; 108, attribute; 109, call; 110, call; 111, identifier:region; 112, identifier:chrom; 113, identifier:int; 114, argument_list; 115, identifier:int; 116, argument_list; 117, attribute; 118, attribute; 119, identifier:region; 120, identifier:start; 121, identifier:region; 122, identifier:stop
code: def _combine_regions(all_regions, ref_regions):
      """Combine multiple BEDtools regions of regions into sorted final BEDtool.
      """
      chrom_order = {}
      for i, x in enumerate(ref_regions):
          chrom_order[x.chrom] = i
      def wchrom_key(x):
          chrom, start, end = x
          return (chrom_order[chrom], start, end)
      all_intervals = []
      for region_group in all_regions:
          for region in region_group:
              all_intervals.append((region.chrom, int(region.start), int(region.stop)))
      all_intervals.sort(key=wchrom_key)
      bed_lines = ["%s\t%s\t%s" % (c, s, e) for (c, s, e) in all_intervals]
      return pybedtools.BedTool("\n".join(bed_lines), from_string=True)