code: def closest(args):
      
      p = OptionParser(closest.__doc__)
      p.add_option("--om", default=False, action="store_true",
                   help="The bedfile is OM blocks [default: %default]")
      opts, args = p.parse_args(args)
  
      if len(args) != 3:
          sys.exit(not p.print_help())
  
      candidates, gapsbed, fastafile = args
      sizes = Sizes(fastafile).mapping
      bed = Bed(candidates)
      ranges = []
      for b in bed:
          r = range_parse(b.accn) if opts.om else b
          ranges.append([r.seqid, r.start, r.end])
  
      gapsbed = Bed(gapsbed)
      granges = [(x.seqid, x.start, x.end) for x in gapsbed]
  
      ranges = range_merge(ranges)
      for r in ranges:
          a = range_closest(granges, r)
          b = range_closest(granges, r, left=False)
          seqid = r[0]
  
          if a is not None and a[0] != seqid:
              a = None
          if b is not None and b[0] != seqid:
              b = None
  
          mmin = 1 if a is None else a[1]
          mmax = sizes[seqid] if b is None else b[2]
  
          print("\t".join(str(x) for x in (seqid, mmin - 1, mmax)))
docs: %prog closest candidates.bed gaps.bed fastafile
  
      Identify the nearest gaps flanking suggested regions.