code: def best(args):
      
      p = OptionParser(best.__doc__)
  
      p.add_option("-n", default=1, type="int",
              help="get best N hits [default: %default]")
      p.add_option("--nosort", default=False, action="store_true",
              help="assume BLAST is already sorted [default: %default]")
      p.add_option("--hsps", default=False, action="store_true",
              help="get all HSPs for the best pair [default: %default]")
      p.add_option("--subject", default=False, action="store_true",
              help="get best hit(s) for subject genome instead [default: %default]")
      p.set_tmpdir()
      opts, args = p.parse_args(args)
  
      if len(args) != 1:
          sys.exit(not p.print_help())
  
      blastfile, = args
      n = opts.n
      hsps = opts.hsps
      tmpdir = opts.tmpdir
      ref = "query" if not opts.subject else "subject"
  
      if not opts.nosort:
          sargs = [blastfile]
          if tmpdir:
              sargs += ["-T {0}".format(tmpdir)]
          if ref != "query":
              sargs += ["--refscore"]
          sort(sargs)
      else:
          logging.debug("Assuming sorted BLAST")
  
      if not opts.subject:
          bestblastfile = blastfile + ".best"
      else:
          bestblastfile = blastfile + ".subject.best"
      fw = open(bestblastfile, "w")
  
      b = Blast(blastfile)
      for q, bline in b.iter_best_hit(N=n, hsps=hsps, ref=ref):
          print(bline, file=fw)
  
      return bestblastfile
docs: %prog best blastfile
  
      print the best hit for each query in the blastfile