code: def chimera(args):
      
      p = OptionParser(chimera.__doc__)
      opts, args = p.parse_args(args)
  
      if len(args) != 1:
          sys.exit(not p.print_help())
  
      bedfile, = args
      bed = Bed(bedfile)
      selected = select_bed(bed)
      mapped = defaultdict(set)  # scaffold => chr
      chimerabed = "chimera.bed"
      fw = open(chimerabed, "w")
      for b in selected:
          scf = range_parse(b.accn).seqid
          chr = b.seqid
          mapped[scf].add(chr)
  
      nchimera = 0
      for s, chrs in sorted(mapped.items()):
          if len(chrs) == 1:
              continue
  
          print("=" * 80, file=sys.stderr)
          print("{0} mapped to multiple locations: {1}".\
                  format(s, ",".join(sorted(chrs))), file=sys.stderr)
          ranges = []
          for b in selected:
              rr = range_parse(b.accn)
              scf = rr.seqid
              if scf == s:
                  print(b, file=sys.stderr)
                  ranges.append(rr)
  
          # Identify breakpoints
          ranges.sort(key=lambda x: (x.seqid, x.start, x.end))
          for a, b in pairwise(ranges):
              seqid = a.seqid
              if seqid != b.seqid:
                  continue
  
              start, end = a.end, b.start
              if start > end:
                  start, end = end, start
  
              chimeraline = "\t".join(str(x) for x in (seqid, start, end))
              print(chimeraline, file=fw)
              print(chimeraline, file=sys.stderr)
              nchimera += 1
  
      fw.close()
      logging.debug("A total of {0} junctions written to `{1}`.".\
                    format(nchimera, chimerabed))
docs: %prog chimera bedfile
  
      Scan the bed file to break scaffolds that multi-maps.