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, expression_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, if_statement; 20, comment:"""
      %prog sort gffile
  
      Sort gff file using plain old unix sort based on [chromosome, start coordinate].
      or topologically based on hierarchy of features using the gt (genometools) toolkit
      """; 21, assignment; 22, assignment; 23, call; 24, call; 25, call; 26, call; 27, call; 28, assignment; 29, comparison_operator:len(args) != 1; 30, block; 31, assignment; 32, assignment; 33, attribute; 34, block; 35, comparison_operator:opts.method == "unix"; 36, block; 37, elif_clause; 38, identifier:valid_sort_methods; 39, tuple; 40, identifier:p; 41, call; 42, attribute; 43, argument_list; 44, attribute; 45, argument_list; 46, attribute; 47, argument_list; 48, attribute; 49, argument_list; 50, attribute; 51, argument_list; 52, pattern_list; 53, call; 54, call; 55, integer:1; 56, expression_statement; 57, pattern_list; 58, identifier:args; 59, identifier:sortedgff; 60, attribute; 61, identifier:opts; 62, identifier:inplace; 63, if_statement; 64, attribute; 65, string:"unix"; 66, expression_statement; 67, expression_statement; 68, if_statement; 69, if_statement; 70, expression_statement; 71, comparison_operator:opts.method == "topo"; 72, block; 73, string:"unix"; 74, string:"topo"; 75, identifier:OptionParser; 76, argument_list; 77, identifier:p; 78, identifier:add_option; 79, string:"--method"; 80, keyword_argument; 81, keyword_argument; 82, keyword_argument; 83, identifier:p; 84, identifier:add_option; 85, string:"-i"; 86, keyword_argument; 87, keyword_argument; 88, keyword_argument; 89, keyword_argument; 90, identifier:p; 91, identifier:set_tmpdir; 92, identifier:p; 93, identifier:set_outfile; 94, identifier:p; 95, identifier:set_home; 96, string:"gt"; 97, identifier:opts; 98, identifier:args; 99, attribute; 100, argument_list; 101, identifier:len; 102, argument_list; 103, call; 104, identifier:gffile; 105, identifier:opts; 106, identifier:outfile; 107, boolean_operator; 108, block; 109, identifier:opts; 110, identifier:method; 111, assignment; 112, augmented_assignment; 113, attribute; 114, block; 115, attribute; 116, block; 117, call; 118, attribute; 119, string:"topo"; 120, expression_statement; 121, if_statement; 122, expression_statement; 123, expression_statement; 124, expression_statement; 125, attribute; 126, identifier:default; 127, string:"unix"; 128, identifier:choices; 129, identifier:valid_sort_methods; 130, identifier:help; 131, string:"Specify sort method [default: %default]"; 132, identifier:dest; 133, string:"inplace"; 134, identifier:default; 135, False; 136, identifier:action; 137, string:"store_true"; 138, identifier:help; 139, string:"If doing a unix sort, perform sort inplace [default: %default]"; 140, identifier:p; 141, identifier:parse_args; 142, identifier:args; 143, identifier:args; 144, attribute; 145, argument_list; 146, comparison_operator:opts.method == "topo"; 147, parenthesized_expression; 148, expression_statement; 149, expression_statement; 150, identifier:cmd; 151, string:"sort"; 152, identifier:cmd; 153, call; 154, identifier:opts; 155, identifier:tmpdir; 156, expression_statement; 157, identifier:opts; 158, identifier:inplace; 159, expression_statement; 160, expression_statement; 161, identifier:sh; 162, argument_list; 163, identifier:opts; 164, identifier:method; 165, assignment; 166, not_operator; 167, block; 168, assignment; 169, augmented_assignment; 170, call; 171, identifier:sort; 172, identifier:__doc__; 173, identifier:sys; 174, identifier:exit; 175, not_operator; 176, attribute; 177, string:"topo"; 178, boolean_operator; 179, call; 180, call; 181, attribute; 182, argument_list; 183, augmented_assignment; 184, augmented_assignment; 185, assignment; 186, identifier:cmd; 187, keyword_argument; 188, identifier:GT_HOME; 189, attribute; 190, call; 191, expression_statement; 192, expression_statement; 193, identifier:cmd; 194, call; 195, identifier:cmd; 196, call; 197, identifier:sh; 198, argument_list; 199, call; 200, identifier:opts; 201, identifier:method; 202, comparison_operator:opts.method == "unix"; 203, comparison_operator:gffile in ("-", "stdin"); 204, attribute; 205, argument_list; 206, attribute; 207, argument_list; 208, string:" -k1,1 -k4,4n {0}"; 209, identifier:format; 210, identifier:gffile; 211, identifier:cmd; 212, call; 213, identifier:cmd; 214, attribute; 215, identifier:sortedgff; 216, None; 217, identifier:outfile; 218, identifier:sortedgff; 219, identifier:opts; 220, identifier:gt_home; 221, attribute; 222, argument_list; 223, call; 224, call; 225, attribute; 226, argument_list; 227, attribute; 228, argument_list; 229, identifier:cmd; 230, keyword_argument; 231, attribute; 232, argument_list; 233, attribute; 234, string:"unix"; 235, identifier:gffile; 236, tuple; 237, identifier:logging; 238, identifier:error; 239, binary_operator:"Cannot perform inplace sort when method is `topo`" + \
                  " or method is `unix` and input is `stdin` stream"; 240, identifier:sys; 241, identifier:exit; 242, attribute; 243, argument_list; 244, string:" -o {0}"; 245, identifier:gffile; 246, identifier:op; 247, identifier:isdir; 248, identifier:GT_HOME; 249, attribute; 250, argument_list; 251, attribute; 252, argument_list; 253, string:"{0}"; 254, identifier:format; 255, call; 256, string:" gff3 -sort -tidy -retainids -addids no {0}"; 257, identifier:format; 258, identifier:gffile; 259, identifier:outfile; 260, identifier:sortedgff; 261, identifier:p; 262, identifier:print_help; 263, identifier:opts; 264, identifier:method; 265, string:"-"; 266, string:"stdin"; 267, string:"Cannot perform inplace sort when method is `topo`"; 268, string:" or method is `unix` and input is `stdin` stream"; 269, string:" -T {0}"; 270, identifier:format; 271, attribute; 272, identifier:logging; 273, identifier:error; 274, call; 275, identifier:sys; 276, identifier:exit; 277, attribute; 278, argument_list; 279, identifier:opts; 280, identifier:tmpdir; 281, attribute; 282, argument_list; 283, identifier:op; 284, identifier:join; 285, identifier:GT_HOME; 286, string:"bin"; 287, string:"gt"; 288, string:"GT_HOME={0} directory does not exist"; 289, identifier:format; 290, identifier:GT_HOME
code: def sort(args):
      """
      %prog sort gffile
  
      Sort gff file using plain old unix sort based on [chromosome, start coordinate].
      or topologically based on hierarchy of features using the gt (genometools) toolkit
      """
      valid_sort_methods = ("unix", "topo")
  
      p = OptionParser(sort.__doc__)
      p.add_option("--method", default="unix", choices=valid_sort_methods,
                   help="Specify sort method [default: %default]")
      p.add_option("-i", dest="inplace", default=False, action="store_true",
                   help="If doing a unix sort, perform sort inplace [default: %default]")
      p.set_tmpdir()
      p.set_outfile()
      p.set_home("gt")
      opts, args = p.parse_args(args)
  
      if len(args) != 1:
          sys.exit(not p.print_help())
  
      gffile, = args
      sortedgff = opts.outfile
      if opts.inplace:
          if opts.method == "topo" or (opts.method == "unix" and gffile in ("-", "stdin")):
              logging.error("Cannot perform inplace sort when method is `topo`" + \
                  " or method is `unix` and input is `stdin` stream")
              sys.exit()
  
      if opts.method == "unix":
          cmd = "sort"
          cmd += " -k1,1 -k4,4n {0}".format(gffile)
          if opts.tmpdir:
              cmd += " -T {0}".format(opts.tmpdir)
          if opts.inplace:
              cmd += " -o {0}".gffile
              sortedgff = None
          sh(cmd, outfile=sortedgff)
      elif opts.method == "topo":
          GT_HOME = opts.gt_home
          if not op.isdir(GT_HOME):
              logging.error("GT_HOME={0} directory does not exist".format(GT_HOME))
              sys.exit()
          cmd = "{0}".format(op.join(GT_HOME, "bin", "gt"))
          cmd += " gff3 -sort -tidy -retainids -addids no {0}".format(gffile)
          sh(cmd, outfile=sortedgff)