nodes: 0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:args; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, expression_statement; 17, if_statement; 18, expression_statement; 19, if_statement; 20, expression_statement; 21, expression_statement; 22, if_statement; 23, if_statement; 24, expression_statement; 25, if_statement; 26, return_statement; 27, comment:"""
      %prog sort bedfile
  
      Sort bed file to have ascending order of seqid, then start. It uses the
      `sort` command.
      """; 28, assignment; 29, call; 30, call; 31, call; 32, call; 33, call; 34, assignment; 35, comparison_operator:len(args) != 1; 36, block; 37, assignment; 38, assignment; 39, boolean_operator; 40, block; 41, assignment; 42, identifier:inplace; 43, block; 44, elif_clause; 45, assignment; 46, assignment; 47, attribute; 48, block; 49, attribute; 50, block; 51, augmented_assignment; 52, boolean_operator; 53, block; 54, identifier:sortedbed; 55, identifier:p; 56, call; 57, attribute; 58, argument_list; 59, attribute; 60, argument_list; 61, attribute; 62, argument_list; 63, attribute; 64, argument_list; 65, attribute; 66, argument_list; 67, pattern_list; 68, call; 69, call; 70, integer:1; 71, expression_statement; 72, pattern_list; 73, identifier:args; 74, identifier:inplace; 75, attribute; 76, not_operator; 77, comparison_operator:".sorted." in bedfile; 78, return_statement; 79, identifier:sortedbed; 80, attribute; 81, expression_statement; 82, comparison_operator:opts.outfile is None; 83, block; 84, identifier:sortopt; 85, conditional_expression:"-k1,1 -k2,2n -k3,3n -k4,4" if not opts.accn else \
                "-k4,4 -k1,1 -k2,2n -k3,3n"; 86, identifier:cmd; 87, string:"sort"; 88, identifier:opts; 89, identifier:tmpdir; 90, expression_statement; 91, identifier:opts; 92, identifier:unique; 93, expression_statement; 94, identifier:cmd; 95, call; 96, identifier:inplace; 97, call; 98, expression_statement; 99, identifier:OptionParser; 100, argument_list; 101, identifier:p; 102, identifier:add_option; 103, string:"-i"; 104, string:"--inplace"; 105, keyword_argument; 106, keyword_argument; 107, keyword_argument; 108, keyword_argument; 109, identifier:p; 110, identifier:add_option; 111, string:"-u"; 112, keyword_argument; 113, keyword_argument; 114, keyword_argument; 115, keyword_argument; 116, identifier:p; 117, identifier:add_option; 118, string:"--accn"; 119, keyword_argument; 120, keyword_argument; 121, keyword_argument; 122, identifier:p; 123, identifier:set_outfile; 124, keyword_argument; 125, identifier:p; 126, identifier:set_tmpdir; 127, identifier:opts; 128, identifier:args; 129, attribute; 130, argument_list; 131, identifier:len; 132, argument_list; 133, call; 134, identifier:bedfile; 135, identifier:opts; 136, identifier:inplace; 137, identifier:inplace; 138, string:".sorted."; 139, identifier:bedfile; 140, identifier:bedfile; 141, identifier:opts; 142, identifier:outfile; 143, assignment; 144, attribute; 145, None; 146, expression_statement; 147, expression_statement; 148, string:"-k1,1 -k2,2n -k3,3n -k4,4"; 149, not_operator; 150, string:"-k4,4 -k1,1 -k2,2n -k3,3n"; 151, augmented_assignment; 152, augmented_assignment; 153, attribute; 154, argument_list; 155, identifier:need_update; 156, argument_list; 157, call; 158, attribute; 159, identifier:dest; 160, string:"inplace"; 161, identifier:default; 162, False; 163, identifier:action; 164, string:"store_true"; 165, identifier:help; 166, string:"Sort bed file in place [default: %default]"; 167, identifier:dest; 168, string:"unique"; 169, identifier:default; 170, False; 171, identifier:action; 172, string:"store_true"; 173, identifier:help; 174, string:"Uniqify the bed file"; 175, identifier:default; 176, False; 177, identifier:action; 178, string:"store_true"; 179, identifier:help; 180, string:"Sort based on the accessions [default: %default]"; 181, identifier:outfile; 182, None; 183, identifier:p; 184, identifier:parse_args; 185, identifier:args; 186, identifier:args; 187, attribute; 188, argument_list; 189, identifier:sortedbed; 190, identifier:bedfile; 191, identifier:opts; 192, identifier:outfile; 193, assignment; 194, assignment; 195, attribute; 196, identifier:cmd; 197, call; 198, identifier:cmd; 199, string:" -u"; 200, string:" {0} {1} -o {2}"; 201, identifier:format; 202, identifier:sortopt; 203, identifier:bedfile; 204, identifier:sortedbed; 205, identifier:bedfile; 206, identifier:sortedbed; 207, identifier:sh; 208, argument_list; 209, identifier:sort; 210, identifier:__doc__; 211, identifier:sys; 212, identifier:exit; 213, not_operator; 214, pattern_list; 215, call; 216, identifier:sortedbed; 217, binary_operator:pf + ".sorted." + sf; 218, identifier:opts; 219, identifier:accn; 220, attribute; 221, argument_list; 222, identifier:cmd; 223, call; 224, identifier:pf; 225, identifier:sf; 226, attribute; 227, argument_list; 228, binary_operator:pf + ".sorted."; 229, identifier:sf; 230, string:" -T {0}"; 231, identifier:format; 232, attribute; 233, attribute; 234, argument_list; 235, call; 236, identifier:rsplit; 237, string:"."; 238, integer:1; 239, identifier:pf; 240, string:".sorted."; 241, identifier:opts; 242, identifier:tmpdir; 243, identifier:p; 244, identifier:print_help; 245, attribute; 246, argument_list; 247, identifier:op; 248, identifier:basename; 249, identifier:bedfile
code: def sort(args):
      """
      %prog sort bedfile
  
      Sort bed file to have ascending order of seqid, then start. It uses the
      `sort` command.
      """
      p = OptionParser(sort.__doc__)
      p.add_option("-i", "--inplace", dest="inplace",
              default=False, action="store_true",
              help="Sort bed file in place [default: %default]")
      p.add_option("-u", dest="unique",
              default=False, action="store_true",
              help="Uniqify the bed file")
      p.add_option("--accn", default=False, action="store_true",
              help="Sort based on the accessions [default: %default]")
      p.set_outfile(outfile=None)
      p.set_tmpdir()
      opts, args = p.parse_args(args)
  
      if len(args) != 1:
          sys.exit(not p.print_help())
  
      bedfile, = args
      inplace = opts.inplace
  
      if not inplace and ".sorted." in bedfile:
          return bedfile
  
      sortedbed = opts.outfile
      if inplace:
          sortedbed = bedfile
      elif opts.outfile is None:
          pf, sf = op.basename(bedfile).rsplit(".", 1)
          sortedbed = pf + ".sorted." + sf
  
      sortopt = "-k1,1 -k2,2n -k3,3n -k4,4" if not opts.accn else \
                "-k4,4 -k1,1 -k2,2n -k3,3n"
      cmd = "sort"
      if opts.tmpdir:
          cmd += " -T {0}".format(opts.tmpdir)
      if opts.unique:
          cmd += " -u"
      cmd += " {0} {1} -o {2}".format(sortopt, bedfile, sortedbed)
  
      if inplace or need_update(bedfile, sortedbed):
          sh(cmd)
  
      return sortedbed