code: def flanking(args):
      
      p = OptionParser(flanking.__doc__)
      p.add_option("-N", default=50, type="int",
                    help="How many genes on both directions")
      opts, args = p.parse_args(args)
  
      if len(args) != 4:
          sys.exit(not p.print_help())
  
      SI, liftover, master, te = args
      N = opts.N
      SI = SetFile(SI, column=0, delimiter='.')
      liftover = Bed(liftover)
      order = liftover.order
      neighbors = set()
      for s in SI:
          si, s = order[s]
          LB = max(si - N, 0)
          RB = min(si + N, len(liftover))
          for j in xrange(LB, RB + 1):
              a = liftover[j]
              if a.seqid != s.seqid:
                  continue
              neighbors.add(a.accn)
  
      dmain = DictFile(master, keypos=0, valuepos=None, delimiter='\t')
      dte = DictFile(te, keypos=0, valuepos=None, delimiter='\t')
      header = next(open(master))
      print("\t".join(("SI/Neighbor", "Gene/TE", header.strip())))
      for a in liftover:
          s = a.accn
          if s not in neighbors:
              continue
  
          tag = "SI" if s in SI else "neighbor"
          if s in dmain:
              d = dmain[s]
              print("\t".join([tag, "gene"] + d))
          elif s in dte:
              d = dte[s]
              print("\t".join([tag, "TE"] + d))
docs: %prog flanking SI.ids liftover.bed master.txt master-removed.txt
  
      Extract flanking genes for given SI loci.