code: def mergeclean(args):
      
      from itertools import groupby
      from jcvi.formats.base import FileShredder
  
      p = OptionParser(mergeclean.__doc__)
      p.set_sep(sep="_", help="Separator to group per prefix")
      opts, args = p.parse_args(args)
  
      if len(args) < 1:
          sys.exit(not p.print_help())
  
      files = sorted(args)
      sep = opts.sep
      key = lambda x: x.split(sep)[0]
      mtime = lambda x: os.stat(x).st_mtime
      for pf, fs in groupby(files, key=key):
          fs = list(fs)
          if len(fs) == 1:
              continue
          newest_f = max(fs, key=mtime)
          print("|".join(fs), "=>", newest_f, file=sys.stderr)
          fs.remove(newest_f)
          FileShredder(fs)
docs: %prog mergeclean [*.bam|*.count]
  
      Clean redundant merged bam/count files. This usually happens after running
      formats.sam.merge() several times.