nodes: 0, module; 1, function_definition; 2, function_name:sort_bam_by_reference; 3, parameters; 4, block; 5, identifier:job; 6, identifier:job_vars; 7, expression_statement; 8, comment:# Unpack variables; 9, expression_statement; 10, expression_statement; 11, comment:# I/O; 12, expression_statement; 13, expression_statement; 14, comment:# Call: Samtools; 15, expression_statement; 16, expression_statement; 17, for_statement; 18, expression_statement; 19, comment:# Iterate through chromosomes to create mini-bams; 20, for_statement; 21, expression_statement; 22, expression_statement; 23, expression_statement; 24, comment:# Write to FileStore; 25, expression_statement; 26, expression_statement; 27, expression_statement; 28, return_statement; 29, comment:"""
      Sorts the bam by reference
  
      job_vars: tuple     Tuple of dictionaries: input_args and ids
      """; 30, assignment; 31, assignment; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, identifier:line; 37, identifier:handle; 38, block; 39, call; 40, identifier:chrom; 41, identifier:ref_seqs; 42, comment:# job.addChildJobFn(sbbr_child, chrom, os.path.join(work_dir, chrom), sorted_bam); 43, block; 44, assignment; 45, assignment; 46, call; 47, assignment; 48, assignment; 49, assignment; 50, expression_list; 51, pattern_list; 52, identifier:job_vars; 53, identifier:work_dir; 54, call; 55, pattern_list; 56, call; 57, identifier:output; 58, call; 59, identifier:ref_seqs; 60, list; 61, identifier:handle; 62, attribute; 63, if_statement; 64, attribute; 65, argument_list; 66, expression_statement; 67, expression_statement; 68, expression_statement; 69, expression_statement; 70, identifier:sorted_files; 71, list_comprehension; 72, identifier:cmd; 73, binary_operator:["samtools", "cat", "-o", output] + sorted_files; 74, attribute; 75, argument_list; 76, subscript; 77, call; 78, identifier:rsem_id; 79, call; 80, identifier:exon_id; 81, call; 82, identifier:exon_id; 83, identifier:rsem_id; 84, identifier:input_args; 85, identifier:ids; 86, attribute; 87, argument_list; 88, identifier:sorted_bam; 89, identifier:sorted_bai; 90, identifier:return_input_paths; 91, argument_list; 92, attribute; 93, argument_list; 94, call; 95, identifier:stdout; 96, call; 97, block; 98, identifier:handle; 99, identifier:close; 100, assignment; 101, assignment; 102, assignment; 103, call; 104, binary_operator:os.path.join(work_dir, chrom) + '.bam'; 105, for_in_clause; 106, list; 107, identifier:sorted_files; 108, identifier:subprocess; 109, identifier:check_call; 110, identifier:cmd; 111, identifier:ids; 112, string; 113, attribute; 114, argument_list; 115, attribute; 116, argument_list; 117, attribute; 118, argument_list; 119, attribute; 120, identifier:getLocalTempDir; 121, identifier:job; 122, identifier:work_dir; 123, identifier:ids; 124, string; 125, string; 126, attribute; 127, identifier:join; 128, identifier:work_dir; 129, string; 130, attribute; 131, argument_list; 132, attribute; 133, argument_list; 134, expression_statement; 135, expression_statement; 136, expression_statement; 137, identifier:cmd_view; 138, list; 139, identifier:cmd_sort; 140, list; 141, identifier:p1; 142, call; 143, attribute; 144, argument_list; 145, call; 146, string; 147, identifier:chrom; 148, identifier:ref_seqs; 149, string:"samtools"; 150, string:"cat"; 151, string:"-o"; 152, identifier:output; 153, string_content:sort_by_ref.bam; 154, attribute; 155, identifier:writeGlobalFile; 156, identifier:output; 157, call; 158, identifier:rv; 159, call; 160, identifier:rv; 161, identifier:job; 162, identifier:fileStore; 163, string_content:sorted.bam; 164, string_content:sorted.bam.bai; 165, identifier:os; 166, identifier:path; 167, string_content:sort_by_ref.bam; 168, identifier:subprocess; 169, identifier:Popen; 170, list; 171, keyword_argument; 172, identifier:line; 173, identifier:startswith; 174, string:"@SQ"; 175, assignment; 176, assignment; 177, call; 178, string:"samtools"; 179, string:"view"; 180, string:"-b"; 181, identifier:sorted_bam; 182, identifier:chrom; 183, string:"samtools"; 184, string:"sort"; 185, string:"-m"; 186, string:"3000000000"; 187, string:"-n"; 188, string:"-"; 189, call; 190, attribute; 191, argument_list; 192, identifier:subprocess; 193, identifier:check_call; 194, identifier:cmd_sort; 195, keyword_argument; 196, attribute; 197, argument_list; 198, string_content:.bam; 199, identifier:job; 200, identifier:fileStore; 201, attribute; 202, argument_list; 203, attribute; 204, argument_list; 205, string:"samtools"; 206, string:"view"; 207, string:"-H"; 208, identifier:sorted_bam; 209, identifier:stdout; 210, attribute; 211, identifier:tmp; 212, call; 213, identifier:chrom; 214, subscript; 215, attribute; 216, argument_list; 217, attribute; 218, argument_list; 219, identifier:subprocess; 220, identifier:Popen; 221, identifier:cmd_view; 222, keyword_argument; 223, identifier:stdin; 224, attribute; 225, attribute; 226, identifier:join; 227, identifier:work_dir; 228, identifier:chrom; 229, identifier:job; 230, identifier:addChildJobFn; 231, identifier:transcriptome; 232, identifier:job_vars; 233, keyword_argument; 234, keyword_argument; 235, identifier:job; 236, identifier:addChildJobFn; 237, identifier:exon_count; 238, identifier:job_vars; 239, keyword_argument; 240, identifier:subprocess; 241, identifier:PIPE; 242, attribute; 243, argument_list; 244, call; 245, integer:1; 246, identifier:ref_seqs; 247, identifier:append; 248, identifier:chrom; 249, attribute; 250, identifier:join; 251, identifier:work_dir; 252, identifier:chrom; 253, identifier:stdout; 254, attribute; 255, identifier:p1; 256, identifier:stdout; 257, identifier:os; 258, identifier:path; 259, identifier:disk; 260, string; 261, identifier:memory; 262, string; 263, identifier:disk; 264, string; 265, identifier:line; 266, identifier:split; 267, string:"\t"; 268, attribute; 269, argument_list; 270, identifier:os; 271, identifier:path; 272, identifier:subprocess; 273, identifier:PIPE; 274, string_content:30 G; 275, string_content:30 G; 276, string_content:30 G; 277, subscript; 278, identifier:split; 279, string:":"; 280, identifier:tmp; 281, integer:1
code: def sort_bam_by_reference(job, job_vars):
      """
      Sorts the bam by reference
  
      job_vars: tuple     Tuple of dictionaries: input_args and ids
      """
      # Unpack variables
      input_args, ids = job_vars
      work_dir = job.fileStore.getLocalTempDir()
      # I/O
      sorted_bam, sorted_bai = return_input_paths(job, work_dir, ids, 'sorted.bam', 'sorted.bam.bai')
      output = os.path.join(work_dir, 'sort_by_ref.bam')
      # Call: Samtools
      ref_seqs = []
      handle = subprocess.Popen(["samtools", "view", "-H", sorted_bam], stdout=subprocess.PIPE).stdout
      for line in handle:
          if line.startswith("@SQ"):
              tmp = line.split("\t")
              chrom = tmp[1].split(":")[1]
              ref_seqs.append(chrom)
      handle.close()
      # Iterate through chromosomes to create mini-bams
      for chrom in ref_seqs:
          # job.addChildJobFn(sbbr_child, chrom, os.path.join(work_dir, chrom), sorted_bam)
          cmd_view = ["samtools", "view", "-b", sorted_bam, chrom]
          cmd_sort = ["samtools", "sort", "-m", "3000000000", "-n", "-", os.path.join(work_dir, chrom)]
          p1 = subprocess.Popen(cmd_view, stdout=subprocess.PIPE)
          subprocess.check_call(cmd_sort, stdin=p1.stdout)
      sorted_files = [os.path.join(work_dir, chrom) + '.bam' for chrom in ref_seqs]
      cmd = ["samtools", "cat", "-o", output] + sorted_files
      subprocess.check_call(cmd)
      # Write to FileStore
      ids['sort_by_ref.bam'] = job.fileStore.writeGlobalFile(output)
      rsem_id = job.addChildJobFn(transcriptome, job_vars, disk='30 G', memory='30 G').rv()
      exon_id = job.addChildJobFn(exon_count, job_vars, disk='30 G').rv()
      return exon_id, rsem_id