nodes: 0, module; 1, function_definition; 2, function_name:prepare_bam; 3, parameters; 4, block; 5, identifier:job; 6, identifier:uuid; 7, identifier:url; 8, identifier:config; 9, default_parameter; 10, default_parameter; 11, expression_statement; 12, comment:# 0: Align FASTQ or realign BAM; 13, if_statement; 14, comment:# 1: Sort BAM file if necessary; 15, comment:# Realigning BAM file shuffles read order; 16, if_statement; 17, comment:# 2: Index BAM; 18, comment:# The samtools index disk requirement depends on the input bam and the output bam index; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, expression_statement; 23, if_statement; 24, return_statement; 25, identifier:paired_url; 26, None; 27, identifier:rg_line; 28, None; 29, comment:"""
      Prepares BAM file for Toil germline pipeline.
  
      Steps in pipeline
      0: Download and align BAM or FASTQ sample
      1: Sort BAM
      2: Index BAM
      3: Run GATK preprocessing pipeline (Optional)
          - Uploads preprocessed BAM to output directory
  
      :param JobFunctionWrappingJob job: passed automatically by Toil
      :param str uuid: Unique identifier for the sample
      :param str url: URL or local path to BAM file or FASTQs
      :param Namespace config: Configuration options for pipeline
          Requires the following config attributes:
          config.genome_fasta         FilesStoreID for reference genome fasta file
          config.genome_fai           FilesStoreID for reference genome fasta index file
          config.genome_dict          FilesStoreID for reference genome sequence dictionary file
          config.g1k_indel            FileStoreID for 1000G INDEL resource file
          config.mills                FileStoreID for Mills resource file
          config.dbsnp                FileStoreID for dbSNP resource file
          config.suffix               Suffix added to output filename
          config.output_dir           URL or local path to output directory
          config.ssec                 Path to key file for SSE-C encryption
          config.cores                Number of cores for each job
          config.xmx                  Java heap size in bytes
      :param str|None paired_url: URL or local path to paired FASTQ file, default is None
      :param str|None rg_line: RG line for BWA alignment (i.e. @RG\tID:foo\tSM:bar), default is None
      :return: BAM and BAI FileStoreIDs
      :rtype: tuple
      """; 30, attribute; 31, block; 32, comment:# 0: Download BAM; 33, elif_clause; 34, else_clause; 35, boolean_operator; 36, block; 37, else_clause; 38, assignment; 39, assignment; 40, call; 41, call; 42, attribute; 43, block; 44, else_clause; 45, expression_list; 46, identifier:config; 47, identifier:run_bwa; 48, expression_statement; 49, comparison_operator:'.bam' in url.lower(); 50, block; 51, block; 52, attribute; 53, not_operator; 54, expression_statement; 55, comment:# The samtools sort disk requirement depends on the input bam, the tmp files, and the; 56, comment:# sorted output bam.; 57, block; 58, identifier:index_bam_disk; 59, call; 60, identifier:index_bam; 61, call; 62, attribute; 63, argument_list; 64, attribute; 65, argument_list; 66, identifier:config; 67, identifier:preprocess; 68, expression_statement; 69, expression_statement; 70, expression_statement; 71, comment:# Update output BAM promises; 72, expression_statement; 73, expression_statement; 74, comment:# Save processed BAM; 75, expression_statement; 76, expression_statement; 77, expression_statement; 78, expression_statement; 79, block; 80, identifier:output_bam_promise; 81, identifier:output_bai_promise; 82, assignment; 83, string; 84, call; 85, expression_statement; 86, expression_statement; 87, raise_statement; 88, identifier:config; 89, identifier:sorted; 90, attribute; 91, assignment; 92, expression_statement; 93, expression_statement; 94, identifier:PromisedRequirement; 95, argument_list; 96, attribute; 97, argument_list; 98, identifier:job; 99, identifier:addChild; 100, identifier:get_bam; 101, identifier:sorted_bam; 102, identifier:addChild; 103, identifier:index_bam; 104, assignment; 105, call; 106, call; 107, assignment; 108, assignment; 109, assignment; 110, assignment; 111, assignment; 112, call; 113, expression_statement; 114, expression_statement; 115, identifier:get_bam; 116, call; 117, string_content:.bam; 118, attribute; 119, argument_list; 120, call; 121, assignment; 122, call; 123, identifier:config; 124, identifier:run_bwa; 125, identifier:sorted_bam; 126, identifier:get_bam; 127, assignment; 128, assignment; 129, lambda; 130, call; 131, identifier:job; 132, identifier:wrapJobFn; 133, identifier:run_samtools_index; 134, call; 135, keyword_argument; 136, identifier:preprocess; 137, call; 138, attribute; 139, argument_list; 140, attribute; 141, argument_list; 142, identifier:output_bam_promise; 143, call; 144, identifier:output_bai_promise; 145, call; 146, identifier:output_dir; 147, call; 148, identifier:filename; 149, call; 150, identifier:output_bam; 151, call; 152, attribute; 153, argument_list; 154, assignment; 155, assignment; 156, attribute; 157, argument_list; 158, identifier:url; 159, identifier:lower; 160, attribute; 161, argument_list; 162, identifier:get_bam; 163, call; 164, identifier:ValueError; 165, argument_list; 166, identifier:sorted_bam_disk; 167, call; 168, identifier:sorted_bam; 169, call; 170, lambda_parameters; 171, attribute; 172, attribute; 173, argument_list; 174, attribute; 175, argument_list; 176, identifier:disk; 177, identifier:index_bam_disk; 178, attribute; 179, argument_list; 180, identifier:sorted_bam; 181, identifier:addChild; 182, identifier:preprocess; 183, identifier:index_bam; 184, identifier:addChild; 185, identifier:preprocess; 186, attribute; 187, argument_list; 188, attribute; 189, argument_list; 190, attribute; 191, argument_list; 192, attribute; 193, argument_list; 194, attribute; 195, argument_list; 196, identifier:preprocess; 197, identifier:addChild; 198, identifier:output_bam; 199, identifier:output_bam_promise; 200, call; 201, identifier:output_bai_promise; 202, call; 203, call; 204, identifier:encapsulate; 205, attribute; 206, identifier:logToMaster; 207, binary_operator:"Downloading BAM: %s" % uuid; 208, attribute; 209, argument_list; 210, binary_operator:'Could not generate BAM file for %s\n'
                           'Provide a FASTQ URL and set run-bwa or '
                           'provide a BAM URL that includes .bam extension.' % uuid; 211, identifier:PromisedRequirement; 212, argument_list; 213, attribute; 214, argument_list; 215, identifier:bam; 216, identifier:bam; 217, identifier:size; 218, identifier:sorted_bam; 219, identifier:rv; 220, identifier:sorted_bam; 221, identifier:rv; 222, call; 223, identifier:encapsulate; 224, identifier:preprocess; 225, identifier:rv; 226, integer:0; 227, identifier:preprocess; 228, identifier:rv; 229, integer:1; 230, attribute; 231, identifier:join; 232, attribute; 233, identifier:uuid; 234, string; 235, identifier:format; 236, identifier:uuid; 237, attribute; 238, identifier:job; 239, identifier:wrapJobFn; 240, identifier:output_file_job; 241, identifier:filename; 242, call; 243, identifier:output_dir; 244, keyword_argument; 245, attribute; 246, argument_list; 247, attribute; 248, argument_list; 249, attribute; 250, argument_list; 251, identifier:job; 252, identifier:fileStore; 253, string:"Downloading BAM: %s"; 254, identifier:uuid; 255, call; 256, identifier:encapsulate; 257, concatenated_string; 258, identifier:uuid; 259, lambda; 260, call; 261, identifier:get_bam; 262, identifier:addChildJobFn; 263, identifier:run_samtools_sort; 264, call; 265, keyword_argument; 266, keyword_argument; 267, attribute; 268, argument_list; 269, identifier:os; 270, identifier:path; 271, identifier:config; 272, identifier:output_dir; 273, string_content:{}.preprocessed{}.bam; 274, identifier:config; 275, identifier:suffix; 276, attribute; 277, argument_list; 278, identifier:s3_key_path; 279, attribute; 280, identifier:sorted_bam; 281, identifier:rv; 282, identifier:index_bam; 283, identifier:rv; 284, identifier:job; 285, identifier:wrapJobFn; 286, identifier:setup_and_run_bwakit; 287, identifier:uuid; 288, identifier:url; 289, identifier:rg_line; 290, identifier:config; 291, keyword_argument; 292, attribute; 293, argument_list; 294, string; 295, string; 296, string; 297, lambda_parameters; 298, binary_operator:3 * bam.size; 299, attribute; 300, argument_list; 301, attribute; 302, argument_list; 303, identifier:cores; 304, attribute; 305, identifier:disk; 306, identifier:sorted_bam_disk; 307, identifier:job; 308, identifier:wrapJobFn; 309, identifier:run_gatk_preprocessing; 310, call; 311, call; 312, attribute; 313, attribute; 314, attribute; 315, attribute; 316, attribute; 317, attribute; 318, keyword_argument; 319, keyword_argument; 320, identifier:preprocess; 321, identifier:rv; 322, integer:0; 323, identifier:config; 324, identifier:ssec; 325, identifier:paired_url; 326, identifier:paired_url; 327, identifier:job; 328, identifier:wrapJobFn; 329, identifier:download_url_job; 330, identifier:url; 331, keyword_argument; 332, keyword_argument; 333, keyword_argument; 334, string_content; 335, string_content:Provide a FASTQ URL and set run-bwa or; 336, string_content:provide a BAM URL that includes .bam extension.; 337, identifier:bam; 338, integer:3; 339, attribute; 340, identifier:get_bam; 341, identifier:rv; 342, identifier:get_bam; 343, identifier:rv; 344, identifier:config; 345, identifier:cores; 346, attribute; 347, argument_list; 348, attribute; 349, argument_list; 350, identifier:config; 351, identifier:genome_fasta; 352, identifier:config; 353, identifier:genome_dict; 354, identifier:config; 355, identifier:genome_fai; 356, identifier:config; 357, identifier:g1k_indel; 358, identifier:config; 359, identifier:mills; 360, identifier:config; 361, identifier:dbsnp; 362, identifier:memory; 363, attribute; 364, identifier:cores; 365, attribute; 366, identifier:name; 367, string; 368, identifier:s3_key_path; 369, attribute; 370, identifier:disk; 371, attribute; 372, escape_sequence:\n; 373, identifier:bam; 374, identifier:size; 375, identifier:sorted_bam; 376, identifier:rv; 377, identifier:index_bam; 378, identifier:rv; 379, identifier:config; 380, identifier:xmx; 381, identifier:config; 382, identifier:cores; 383, string_content:toil.bam; 384, identifier:config; 385, identifier:ssec; 386, identifier:config; 387, identifier:file_size
code: def prepare_bam(job, uuid, url, config, paired_url=None, rg_line=None):
      """
      Prepares BAM file for Toil germline pipeline.
  
      Steps in pipeline
      0: Download and align BAM or FASTQ sample
      1: Sort BAM
      2: Index BAM
      3: Run GATK preprocessing pipeline (Optional)
          - Uploads preprocessed BAM to output directory
  
      :param JobFunctionWrappingJob job: passed automatically by Toil
      :param str uuid: Unique identifier for the sample
      :param str url: URL or local path to BAM file or FASTQs
      :param Namespace config: Configuration options for pipeline
          Requires the following config attributes:
          config.genome_fasta         FilesStoreID for reference genome fasta file
          config.genome_fai           FilesStoreID for reference genome fasta index file
          config.genome_dict          FilesStoreID for reference genome sequence dictionary file
          config.g1k_indel            FileStoreID for 1000G INDEL resource file
          config.mills                FileStoreID for Mills resource file
          config.dbsnp                FileStoreID for dbSNP resource file
          config.suffix               Suffix added to output filename
          config.output_dir           URL or local path to output directory
          config.ssec                 Path to key file for SSE-C encryption
          config.cores                Number of cores for each job
          config.xmx                  Java heap size in bytes
      :param str|None paired_url: URL or local path to paired FASTQ file, default is None
      :param str|None rg_line: RG line for BWA alignment (i.e. @RG\tID:foo\tSM:bar), default is None
      :return: BAM and BAI FileStoreIDs
      :rtype: tuple
      """
      # 0: Align FASTQ or realign BAM
      if config.run_bwa:
          get_bam = job.wrapJobFn(setup_and_run_bwakit,
                                  uuid,
                                  url,
                                  rg_line,
                                  config,
                                  paired_url=paired_url).encapsulate()
  
      # 0: Download BAM
      elif '.bam' in url.lower():
          job.fileStore.logToMaster("Downloading BAM: %s" % uuid)
          get_bam = job.wrapJobFn(download_url_job,
                                  url,
                                  name='toil.bam',
                                  s3_key_path=config.ssec,
                                  disk=config.file_size).encapsulate()
      else:
          raise ValueError('Could not generate BAM file for %s\n'
                           'Provide a FASTQ URL and set run-bwa or '
                           'provide a BAM URL that includes .bam extension.' % uuid)
  
      # 1: Sort BAM file if necessary
      # Realigning BAM file shuffles read order
      if config.sorted and not config.run_bwa:
          sorted_bam = get_bam
  
      else:
          # The samtools sort disk requirement depends on the input bam, the tmp files, and the
          # sorted output bam.
          sorted_bam_disk = PromisedRequirement(lambda bam: 3 * bam.size, get_bam.rv())
          sorted_bam = get_bam.addChildJobFn(run_samtools_sort,
                                             get_bam.rv(),
                                             cores=config.cores,
                                             disk=sorted_bam_disk)
  
      # 2: Index BAM
      # The samtools index disk requirement depends on the input bam and the output bam index
      index_bam_disk = PromisedRequirement(lambda bam: bam.size, sorted_bam.rv())
      index_bam = job.wrapJobFn(run_samtools_index, sorted_bam.rv(), disk=index_bam_disk)
  
      job.addChild(get_bam)
      sorted_bam.addChild(index_bam)
  
      if config.preprocess:
          preprocess = job.wrapJobFn(run_gatk_preprocessing,
                                     sorted_bam.rv(),
                                     index_bam.rv(),
                                     config.genome_fasta,
                                     config.genome_dict,
                                     config.genome_fai,
                                     config.g1k_indel,
                                     config.mills,
                                     config.dbsnp,
                                     memory=config.xmx,
                                     cores=config.cores).encapsulate()
          sorted_bam.addChild(preprocess)
          index_bam.addChild(preprocess)
  
          # Update output BAM promises
          output_bam_promise = preprocess.rv(0)
          output_bai_promise = preprocess.rv(1)
  
          # Save processed BAM
          output_dir = os.path.join(config.output_dir, uuid)
          filename = '{}.preprocessed{}.bam'.format(uuid, config.suffix)
          output_bam = job.wrapJobFn(output_file_job,
                                     filename,
                                     preprocess.rv(0),
                                     output_dir,
                                     s3_key_path=config.ssec)
          preprocess.addChild(output_bam)
  
      else:
          output_bam_promise = sorted_bam.rv()
          output_bai_promise = index_bam.rv()
  
      return output_bam_promise, output_bai_promise