nodes: 0, module; 1, function_definition; 2, function_name:_sort_by_region; 3, parameters; 4, block; 5, identifier:fnames; 6, identifier:regions; 7, identifier:ref_file; 8, identifier:config; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, assert_statement; 14, expression_statement; 15, for_statement; 16, expression_statement; 17, return_statement; 18, comment:"""Sort a set of regionally split files by region for ordered output.
      """; 19, assignment; 20, pattern_list; 21, call; 22, block; 23, assignment; 24, comparison_operator:len(regions) == len(fnames); 25, tuple; 26, assignment; 27, pattern_list; 28, call; 29, block; 30, call; 31, list_comprehension; 32, identifier:contig_order; 33, dictionary; 34, identifier:i; 35, identifier:sq; 36, identifier:enumerate; 37, argument_list; 38, expression_statement; 39, identifier:sitems; 40, list; 41, call; 42, call; 43, identifier:regions; 44, identifier:fnames; 45, identifier:added_fnames; 46, call; 47, identifier:region; 48, identifier:fname; 49, identifier:zip; 50, argument_list; 51, if_statement; 52, attribute; 53, argument_list; 54, tuple; 55, for_in_clause; 56, call; 57, assignment; 58, identifier:len; 59, argument_list; 60, identifier:len; 61, argument_list; 62, identifier:set; 63, argument_list; 64, identifier:regions; 65, identifier:fnames; 66, comparison_operator:fname not in added_fnames; 67, block; 68, identifier:sitems; 69, identifier:sort; 70, subscript; 71, subscript; 72, identifier:x; 73, identifier:sitems; 74, attribute; 75, argument_list; 76, subscript; 77, identifier:i; 78, identifier:regions; 79, identifier:fnames; 80, list; 81, identifier:fname; 82, identifier:added_fnames; 83, if_statement; 84, expression_statement; 85, expression_statement; 86, identifier:x; 87, integer:1; 88, identifier:x; 89, integer:2; 90, identifier:ref; 91, identifier:file_contigs; 92, identifier:ref_file; 93, identifier:config; 94, identifier:contig_order; 95, attribute; 96, call; 97, block; 98, elif_clause; 99, else_clause; 100, call; 101, call; 102, identifier:sq; 103, identifier:name; 104, identifier:isinstance; 105, argument_list; 106, expression_statement; 107, boolean_operator; 108, block; 109, block; 110, attribute; 111, argument_list; 112, attribute; 113, argument_list; 114, identifier:region; 115, tuple; 116, assignment; 117, call; 118, comparison_operator:region.find(":") >= 0; 119, expression_statement; 120, expression_statement; 121, expression_statement; 122, expression_statement; 123, identifier:sitems; 124, identifier:append; 125, tuple; 126, identifier:added_fnames; 127, identifier:add; 128, identifier:fname; 129, identifier:list; 130, identifier:tuple; 131, pattern_list; 132, identifier:region; 133, identifier:isinstance; 134, argument_list; 135, call; 136, integer:0; 137, assignment; 138, assignment; 139, assignment; 140, assignment; 141, tuple; 142, identifier:c; 143, identifier:fname; 144, identifier:c; 145, identifier:s; 146, identifier:e; 147, identifier:region; 148, attribute; 149, attribute; 150, argument_list; 151, pattern_list; 152, call; 153, pattern_list; 154, list_comprehension; 155, identifier:c; 156, identifier:region; 157, pattern_list; 158, expression_list; 159, subscript; 160, identifier:s; 161, identifier:e; 162, identifier:six; 163, identifier:string_types; 164, identifier:region; 165, identifier:find; 166, string:":"; 167, identifier:c; 168, identifier:coords; 169, attribute; 170, argument_list; 171, identifier:s; 172, identifier:e; 173, call; 174, for_in_clause; 175, identifier:s; 176, identifier:e; 177, integer:0; 178, integer:0; 179, identifier:contig_order; 180, identifier:c; 181, identifier:region; 182, identifier:split; 183, string:":"; 184, identifier:int; 185, argument_list; 186, identifier:x; 187, call; 188, identifier:x; 189, attribute; 190, argument_list; 191, identifier:coords; 192, identifier:split; 193, string:"-"
code: def _sort_by_region(fnames, regions, ref_file, config):
      """Sort a set of regionally split files by region for ordered output.
      """
      contig_order = {}
      for i, sq in enumerate(ref.file_contigs(ref_file, config)):
          contig_order[sq.name] = i
      sitems = []
      assert len(regions) == len(fnames), (regions, fnames)
      added_fnames = set([])
      for region, fname in zip(regions, fnames):
          if fname not in added_fnames:
              if isinstance(region, (list, tuple)):
                  c, s, e = region
              elif isinstance(region, six.string_types) and region.find(":") >= 0:
                  c, coords = region.split(":")
                  s, e = [int(x) for x in coords.split("-")]
              else:
                  c = region
                  s, e = 0, 0
              sitems.append(((contig_order[c], s, e), c, fname))
              added_fnames.add(fname)
      sitems.sort()
      return [(x[1], x[2]) for x in sitems]