code: def sort(args):
      
      p = OptionParser(sort.__doc__)
      p.add_option("--query", default=False, action="store_true",
              help="Sort by query position [default: %default]")
      p.add_option("--ref", default=False, action="store_true",
              help="Sort by reference position [default: %default]")
      p.add_option("--refscore", default=False, action="store_true",
              help="Sort by reference name, then score descending [default: %default]")
      p.add_option("--coords", default=False, action="store_true",
              help="File is .coords generated by NUCMER [default: %default]")
      p.set_tmpdir()
  
      opts, args = p.parse_args(args)
  
      if len(args) != 1:
          sys.exit(not p.print_help())
  
      blastfile, = args
  
      if opts.coords:
          if opts.query:
              key = "-k13,13 -k3,3n"
          elif opts.ref:
              key = "-k12,12 -k1,1n"
  
      else:
          if opts.query:
              key = "-k1,1 -k7,7n"
          elif opts.ref:
              key = "-k2,2 -k9,9n"
          elif opts.refscore:
              key = "-k2,2 -k12,12gr"
          else:
              key = "-k1,1 -k12,12gr"
  
      cmd = "sort"
      if opts.tmpdir:
          cmd += " -T {0}".format(opts.tmpdir)
      cmd += " {0} {1} -o {1}".format(key, blastfile)
      sh(cmd)
docs: %prog sort <blastfile|coordsfile>
  
      Sort lines so that same query grouped together with scores descending. The
      sort is 'in-place'.