nodes: 0, module; 1, function_definition; 2, function_name:_split_by_ready_regions; 3, parameters; 4, block; 5, identifier:ext; 6, identifier:file_key; 7, identifier:dir_ext_fn; 8, expression_statement; 9, function_definition; 10, function_definition; 11, function_definition; 12, return_statement; 13, comment:"""Organize splits based on regions generated by parallel_prep_region.
  
      Sort splits so largest regions analyzed first, avoiding potentially lagging runs
      at end.
      """; 14, function_name:_sort_by_size; 15, parameters; 16, block; 17, function_name:_assign_bams_to_regions; 18, parameters; 19, block; 20, function_name:_do_work; 21, parameters; 22, block; 23, identifier:_do_work; 24, identifier:region_w_bams; 25, expression_statement; 26, expression_statement; 27, return_statement; 28, identifier:data; 29, expression_statement; 30, for_statement; 31, identifier:data; 32, if_statement; 33, assignment; 34, assignment; 35, binary_operator:end - start; 36, comment:"""Ensure BAMs aligned with input regions, either global or individual.
          """; 37, pattern_list; 38, call; 39, block; 40, comparison_operator:"region" in data; 41, block; 42, else_clause; 43, pattern_list; 44, identifier:region_w_bams; 45, pattern_list; 46, identifier:region; 47, identifier:end; 48, identifier:start; 49, identifier:i; 50, identifier:region; 51, identifier:enumerate; 52, argument_list; 53, expression_statement; 54, for_statement; 55, for_statement; 56, expression_statement; 57, string:"region"; 58, identifier:data; 59, expression_statement; 60, expression_statement; 61, expression_statement; 62, assert_statement; 63, expression_statement; 64, for_statement; 65, return_statement; 66, block; 67, identifier:region; 68, identifier:_; 69, identifier:_; 70, identifier:start; 71, identifier:end; 72, subscript; 73, assignment; 74, identifier:xs; 75, subscript; 76, block; 77, identifier:work_bam; 78, identifier:work_bams; 79, block; 80, yield; 81, assignment; 82, assignment; 83, assignment; 84, call; 85, assignment; 86, pattern_list; 87, call; 88, block; 89, expression_list; 90, return_statement; 91, identifier:data; 92, string:"region"; 93, identifier:work_bams; 94, list; 95, identifier:data; 96, string:"region_bams"; 97, if_statement; 98, assert_statement; 99, expression_list; 100, identifier:name; 101, conditional_expression:data["group"][0] if "group" in data else data["description"]; 102, identifier:out_dir; 103, call; 104, identifier:out_file; 105, call; 106, identifier:isinstance; 107, argument_list; 108, identifier:out_parts; 109, list; 110, identifier:r; 111, identifier:work_bams; 112, identifier:sorted; 113, argument_list; 114, expression_statement; 115, expression_statement; 116, expression_statement; 117, identifier:out_file; 118, identifier:out_parts; 119, expression_list; 120, comparison_operator:len(xs) == 1; 121, block; 122, else_clause; 123, call; 124, identifier:work_bam; 125, identifier:region; 126, identifier:work_bams; 127, subscript; 128, comparison_operator:"group" in data; 129, subscript; 130, attribute; 131, argument_list; 132, attribute; 133, argument_list; 134, subscript; 135, tuple; 136, call; 137, keyword_argument; 138, keyword_argument; 139, assignment; 140, assignment; 141, call; 142, None; 143, list; 144, call; 145, integer:1; 146, expression_statement; 147, block; 148, attribute; 149, argument_list; 150, subscript; 151, integer:0; 152, string:"group"; 153, identifier:data; 154, identifier:data; 155, string:"description"; 156, attribute; 157, identifier:join; 158, subscript; 159, call; 160, attribute; 161, identifier:join; 162, identifier:out_dir; 163, binary_operator:"%s%s" % (name, ext); 164, identifier:data; 165, string:"region"; 166, identifier:list; 167, identifier:tuple; 168, identifier:_assign_bams_to_regions; 169, argument_list; 170, identifier:key; 171, identifier:_sort_by_size; 172, identifier:reverse; 173, True; 174, identifier:out_region_dir; 175, call; 176, identifier:out_region_file; 177, call; 178, attribute; 179, argument_list; 180, identifier:len; 181, argument_list; 182, call; 183, expression_statement; 184, attribute; 185, identifier:exists; 186, identifier:work_bam; 187, identifier:data; 188, string:"group"; 189, identifier:os; 190, identifier:path; 191, subscript; 192, string:"work"; 193, identifier:dir_ext_fn; 194, argument_list; 195, identifier:os; 196, identifier:path; 197, string:"%s%s"; 198, tuple; 199, identifier:data; 200, attribute; 201, argument_list; 202, attribute; 203, argument_list; 204, identifier:out_parts; 205, identifier:append; 206, tuple; 207, identifier:xs; 208, attribute; 209, argument_list; 210, call; 211, identifier:os; 212, identifier:path; 213, identifier:data; 214, string:"dirs"; 215, identifier:data; 216, identifier:name; 217, identifier:ext; 218, attribute; 219, identifier:join; 220, identifier:out_dir; 221, subscript; 222, attribute; 223, identifier:join; 224, identifier:out_region_dir; 225, binary_operator:"%s-%s%s" % (name, pregion.to_safestr(r), ext); 226, identifier:r; 227, identifier:work_bams; 228, identifier:out_region_file; 229, identifier:work_bams; 230, identifier:append; 231, subscript; 232, attribute; 233, argument_list; 234, identifier:os; 235, identifier:path; 236, identifier:r; 237, integer:0; 238, identifier:os; 239, identifier:path; 240, string:"%s-%s%s"; 241, tuple; 242, identifier:xs; 243, integer:0; 244, identifier:work_bams; 245, identifier:append; 246, subscript; 247, identifier:name; 248, call; 249, identifier:ext; 250, identifier:xs; 251, identifier:i; 252, attribute; 253, argument_list; 254, identifier:pregion; 255, identifier:to_safestr; 256, identifier:r
code: def _split_by_ready_regions(ext, file_key, dir_ext_fn):
      """Organize splits based on regions generated by parallel_prep_region.
  
      Sort splits so largest regions analyzed first, avoiding potentially lagging runs
      at end.
      """
      def _sort_by_size(region_w_bams):
          region, _ = region_w_bams
          _, start, end = region
          return end - start
      def _assign_bams_to_regions(data):
          """Ensure BAMs aligned with input regions, either global or individual.
          """
          for i, region in enumerate(data["region"]):
              work_bams = []
              for xs in data["region_bams"]:
                  if len(xs) == 1:
                      work_bams.append(xs[0])
                  else:
                      work_bams.append(xs[i])
              for work_bam in work_bams:
                  assert os.path.exists(work_bam), work_bam
              yield region, work_bams
      def _do_work(data):
          if "region" in data:
              name = data["group"][0] if "group" in data else data["description"]
              out_dir = os.path.join(data["dirs"]["work"], dir_ext_fn(data))
              out_file = os.path.join(out_dir, "%s%s" % (name, ext))
              assert isinstance(data["region"], (list, tuple))
              out_parts = []
              for r, work_bams in sorted(_assign_bams_to_regions(data), key=_sort_by_size, reverse=True):
                  out_region_dir = os.path.join(out_dir, r[0])
                  out_region_file = os.path.join(out_region_dir,
                                                 "%s-%s%s" % (name, pregion.to_safestr(r), ext))
                  out_parts.append((r, work_bams, out_region_file))
              return out_file, out_parts
          else:
              return None, []
      return _do_work