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, if_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, if_statement; 16, for_statement; 17, expression_statement; 18, expression_statement; 19, return_statement; 20, comment:"""
      %prog sort fastafile
  
      Sort a list of sequences and output with sorted IDs, etc.
      """; 21, assignment; 22, call; 23, assignment; 24, comparison_operator:len(args) != 1; 25, block; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, attribute; 31, comment:# Sort by decreasing size; 32, block; 33, else_clause; 34, identifier:key; 35, identifier:sortlist; 36, block; 37, call; 38, call; 39, identifier:sortedfastafile; 40, identifier:p; 41, call; 42, attribute; 43, argument_list; 44, pattern_list; 45, call; 46, call; 47, integer:1; 48, expression_statement; 49, pattern_list; 50, identifier:args; 51, identifier:sortedfastafile; 52, binary_operator:fastafile.rsplit(".", 1)[0] + ".sorted.fasta"; 53, identifier:f; 54, call; 55, identifier:fw; 56, call; 57, identifier:opts; 58, identifier:sizes; 59, expression_statement; 60, expression_statement; 61, expression_statement; 62, block; 63, expression_statement; 64, expression_statement; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, identifier:OptionParser; 70, argument_list; 71, identifier:p; 72, identifier:add_option; 73, string:"--sizes"; 74, keyword_argument; 75, keyword_argument; 76, keyword_argument; 77, identifier:opts; 78, identifier:args; 79, attribute; 80, argument_list; 81, identifier:len; 82, argument_list; 83, call; 84, identifier:fastafile; 85, subscript; 86, string:".sorted.fasta"; 87, identifier:Fasta; 88, argument_list; 89, identifier:must_open; 90, argument_list; 91, assignment; 92, call; 93, assignment; 94, expression_statement; 95, assignment; 96, call; 97, identifier:logging; 98, identifier:debug; 99, call; 100, identifier:fw; 101, identifier:close; 102, attribute; 103, identifier:default; 104, False; 105, identifier:action; 106, string:"store_true"; 107, identifier:help; 108, string:"Sort by decreasing size [default: %default]"; 109, identifier:p; 110, identifier:parse_args; 111, identifier:args; 112, identifier:args; 113, attribute; 114, argument_list; 115, call; 116, integer:0; 117, identifier:fastafile; 118, keyword_argument; 119, identifier:sortedfastafile; 120, string:"w"; 121, identifier:sortlist; 122, call; 123, attribute; 124, argument_list; 125, identifier:sortlist; 126, list_comprehension; 127, assignment; 128, identifier:rec; 129, subscript; 130, attribute; 131, argument_list; 132, attribute; 133, argument_list; 134, identifier:sort; 135, identifier:__doc__; 136, identifier:sys; 137, identifier:exit; 138, call; 139, attribute; 140, argument_list; 141, identifier:index; 142, False; 143, identifier:sorted; 144, argument_list; 145, identifier:logging; 146, identifier:debug; 147, call; 148, identifier:x; 149, for_in_clause; 150, identifier:sortlist; 151, call; 152, identifier:f; 153, identifier:key; 154, identifier:SeqIO; 155, identifier:write; 156, list; 157, identifier:fw; 158, string:"fasta"; 159, string:"Sorted file written to `{0}`."; 160, identifier:format; 161, identifier:sortedfastafile; 162, attribute; 163, argument_list; 164, identifier:fastafile; 165, identifier:rsplit; 166, string:"."; 167, integer:1; 168, call; 169, keyword_argument; 170, attribute; 171, argument_list; 172, pattern_list; 173, identifier:sortlist; 174, identifier:sorted; 175, argument_list; 176, identifier:rec; 177, identifier:p; 178, identifier:print_help; 179, attribute; 180, argument_list; 181, identifier:key; 182, lambda; 183, string:"Sort by size: max: {0}, min: {1}"; 184, line_continuation:\; 185, identifier:format; 186, subscript; 187, subscript; 188, identifier:x; 189, identifier:s; 190, call; 191, identifier:f; 192, identifier:itersizes; 193, lambda_parameters; 194, tuple; 195, identifier:sortlist; 196, integer:0; 197, identifier:sortlist; 198, unary_operator; 199, attribute; 200, argument_list; 201, identifier:x; 202, unary_operator; 203, subscript; 204, integer:1; 205, identifier:f; 206, identifier:iterkeys; 207, subscript; 208, identifier:x; 209, integer:0; 210, identifier:x; 211, integer:1
code: def sort(args):
      """
      %prog sort fastafile
  
      Sort a list of sequences and output with sorted IDs, etc.
      """
      p = OptionParser(sort.__doc__)
      p.add_option("--sizes", default=False, action="store_true",
                   help="Sort by decreasing size [default: %default]")
  
      opts, args = p.parse_args(args)
  
      if len(args) != 1:
          sys.exit(p.print_help())
  
      fastafile, = args
      sortedfastafile = fastafile.rsplit(".", 1)[0] + ".sorted.fasta"
  
      f = Fasta(fastafile, index=False)
      fw = must_open(sortedfastafile, "w")
      if opts.sizes:
          # Sort by decreasing size
          sortlist = sorted(f.itersizes(), key=lambda x: (-x[1], x[0]))
          logging.debug("Sort by size: max: {0}, min: {1}".\
                          format(sortlist[0], sortlist[-1]))
          sortlist = [x for x, s in sortlist]
      else:
          sortlist = sorted(f.iterkeys())
  
      for key in sortlist:
          rec = f[key]
          SeqIO.write([rec], fw, "fasta")
  
      logging.debug("Sorted file written to `{0}`.".format(sortedfastafile))
      fw.close()
  
      return sortedfastafile