nodes: 0, module; 1, function_definition; 2, function_name:sort_sam; 3, parameters; 4, block; 5, identifier:sam; 6, identifier:sort; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, return_statement; 11, comment:"""
      sort sam file
      """; 12, assignment; 13, comparison_operator:sort is True; 14, block; 15, else_clause; 16, identifier:mapping; 17, identifier:tempdir; 18, binary_operator:'%s/' % (os.path.abspath(sam).rsplit('/', 1)[0]); 19, identifier:sort; 20, True; 21, expression_statement; 22, if_statement; 23, expression_statement; 24, block; 25, string; 26, parenthesized_expression; 27, assignment; 28, comparison_operator:sam != '-'; 29, block; 30, else_clause; 31, assignment; 32, if_statement; 33, string_content:%s/; 34, subscript; 35, identifier:mapping; 36, binary_operator:'%s.sorted.sam' % (sam.rsplit('.', 1)[0]); 37, identifier:sam; 38, string; 39, if_statement; 40, block; 41, identifier:mapping; 42, call; 43, comparison_operator:sam == '-'; 44, block; 45, else_clause; 46, call; 47, integer:0; 48, string; 49, parenthesized_expression; 50, string_content:-; 51, comparison_operator:os.path.exists(mapping) is False; 52, block; 53, expression_statement; 54, expression_statement; 55, expression_statement; 56, identifier:open; 57, argument_list; 58, identifier:sam; 59, string; 60, expression_statement; 61, block; 62, attribute; 63, argument_list; 64, string_content:%s.sorted.sam; 65, subscript; 66, call; 67, False; 68, expression_statement; 69, assignment; 70, assignment; 71, call; 72, identifier:mapping; 73, string_content:-; 74, assignment; 75, expression_statement; 76, call; 77, identifier:rsplit; 78, string; 79, integer:1; 80, call; 81, integer:0; 82, attribute; 83, argument_list; 84, call; 85, identifier:mapping; 86, string; 87, identifier:p; 88, call; 89, attribute; 90, argument_list; 91, identifier:mapping; 92, attribute; 93, assignment; 94, attribute; 95, argument_list; 96, string_content:/; 97, attribute; 98, argument_list; 99, attribute; 100, identifier:exists; 101, identifier:mapping; 102, attribute; 103, argument_list; 104, string_content:stdin-sam.sorted.sam; 105, identifier:Popen; 106, argument_list; 107, identifier:p; 108, identifier:communicate; 109, identifier:sys; 110, identifier:stdin; 111, identifier:mapping; 112, call; 113, attribute; 114, identifier:abspath; 115, identifier:sam; 116, identifier:sam; 117, identifier:rsplit; 118, string; 119, integer:1; 120, identifier:os; 121, identifier:path; 122, identifier:os; 123, identifier:system; 124, binary_operator:"\
                      sort -k1 --buffer-size=%sG -T %s -o %s %s\
                      " % (sbuffer, tempdir, mapping, sam); 125, binary_operator:"sort -k1 --buffer-size=%sG -T %s -o %s" \
                      % (sbuffer, tempdir, mapping); 126, keyword_argument; 127, keyword_argument; 128, identifier:open; 129, argument_list; 130, identifier:os; 131, identifier:path; 132, string_content:.; 133, string:"\
                      sort -k1 --buffer-size=%sG -T %s -o %s %s\
                      "; 134, tuple; 135, string:"sort -k1 --buffer-size=%sG -T %s -o %s"; 136, line_continuation:\; 137, tuple; 138, identifier:stdin; 139, attribute; 140, identifier:shell; 141, True; 142, identifier:sam; 143, identifier:sbuffer; 144, identifier:tempdir; 145, identifier:mapping; 146, identifier:sam; 147, identifier:sbuffer; 148, identifier:tempdir; 149, identifier:mapping; 150, identifier:sys; 151, identifier:stdin
code: def sort_sam(sam, sort):
      """
      sort sam file
      """
      tempdir = '%s/' % (os.path.abspath(sam).rsplit('/', 1)[0])
      if sort is True:
          mapping = '%s.sorted.sam' % (sam.rsplit('.', 1)[0])
          if sam != '-':
              if os.path.exists(mapping) is False:
                  os.system("\
                      sort -k1 --buffer-size=%sG -T %s -o %s %s\
                      " % (sbuffer, tempdir, mapping, sam)) 
          else:
              mapping = 'stdin-sam.sorted.sam'
              p = Popen("sort -k1 --buffer-size=%sG -T %s -o %s" \
                      % (sbuffer, tempdir, mapping), stdin = sys.stdin, shell = True) 
              p.communicate()
          mapping = open(mapping)
      else:
          if sam == '-':
              mapping = sys.stdin
          else:
              mapping = open(sam)
      return mapping