code: def tips(args):
      
      p = OptionParser(tips.__doc__)
      opts, args = p.parse_args(args)
  
      if len(args) != 4:
          sys.exit(not p.print_help())
  
      pbedfile, cbedfile, sizesfile, bbfasta = args
  
      pbed = Bed(pbedfile, sorted=False)
      cbed = Bed(cbedfile, sorted=False)
  
      complements = dict()
      for object, beds in groupby(cbed, key=lambda x: x.seqid):
          beds = list(beds)
          complements[object] = beds
  
      sizes = Sizes(sizesfile).mapping
      bbsizes = Sizes(bbfasta).mapping
      tbeds = []
  
      for object, beds in groupby(pbed, key=lambda x: x.accn):
          beds = list(beds)
          startbed, endbed = beds[0], beds[-1]
          start_id, end_id = startbed.seqid, endbed.seqid
          if startbed.start == 1:
              start_id = None
          if endbed.end == sizes[end_id]:
              end_id = None
          print(object, start_id, end_id, file=sys.stderr)
          if start_id:
              b = complements[start_id][0]
              b.accn = object
              tbeds.append(b)
          tbeds.append(BedLine("\t".join(str(x) for x in \
                          (object, 0, bbsizes[object], object, 1000, "+"))))
          if end_id:
              b = complements[end_id][-1]
              b.accn = object
              tbeds.append(b)
  
      tbed = Bed()
      tbed.extend(tbeds)
  
      tbedfile = "tips.bed"
      tbed.print_to_file(tbedfile)
docs: %prog tips patchers.bed complements.bed original.fasta backbone.fasta
  
      Append telomeric sequences based on patchers and complements.