nodes: 0, module; 1, function_definition; 2, function_name:coverage; 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, if_statement; 13, expression_statement; 14, expression_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, if_statement; 20, expression_statement; 21, if_statement; 22, expression_statement; 23, expression_statement; 24, for_statement; 25, expression_statement; 26, comment:"""
      %prog coverage fastafile bamfile
  
      Calculate coverage for BAM file. BAM file will be sorted unless with
      --nosort.
      """; 27, assignment; 28, call; 29, call; 30, call; 31, assignment; 32, comparison_operator:len(args) != 2; 33, block; 34, assignment; 35, assignment; 36, attribute; 37, block; 38, else_clause; 39, assignment; 40, assignment; 41, assignment; 42, comparison_operator:format in ("bedgraph", "bigwig"); 43, block; 44, assignment; 45, call; 46, block; 47, assignment; 48, assignment; 49, pattern_list; 50, call; 51, block; 52, call; 53, identifier:p; 54, call; 55, attribute; 56, argument_list; 57, attribute; 58, argument_list; 59, attribute; 60, argument_list; 61, pattern_list; 62, call; 63, call; 64, integer:2; 65, expression_statement; 66, pattern_list; 67, identifier:args; 68, identifier:format; 69, attribute; 70, identifier:opts; 71, identifier:nosort; 72, expression_statement; 73, block; 74, identifier:pf; 75, subscript; 76, identifier:sizesfile; 77, attribute; 78, identifier:cmd; 79, call; 80, identifier:format; 81, tuple; 82, expression_statement; 83, expression_statement; 84, expression_statement; 85, if_statement; 86, expression_statement; 87, expression_statement; 88, expression_statement; 89, return_statement; 90, identifier:coveragefile; 91, binary_operator:pf + ".coverage"; 92, identifier:need_update; 93, argument_list; 94, expression_statement; 95, identifier:gcf; 96, call; 97, identifier:fw; 98, call; 99, identifier:seqid; 100, identifier:cov; 101, attribute; 102, argument_list; 103, expression_statement; 104, attribute; 105, argument_list; 106, identifier:OptionParser; 107, argument_list; 108, identifier:p; 109, identifier:add_option; 110, string:"--format"; 111, keyword_argument; 112, keyword_argument; 113, keyword_argument; 114, identifier:p; 115, identifier:add_option; 116, string:"--nosort"; 117, keyword_argument; 118, keyword_argument; 119, keyword_argument; 120, identifier:p; 121, identifier:set_outfile; 122, identifier:opts; 123, identifier:args; 124, attribute; 125, argument_list; 126, identifier:len; 127, argument_list; 128, call; 129, identifier:fastafile; 130, identifier:bamfile; 131, identifier:opts; 132, identifier:format; 133, call; 134, expression_statement; 135, call; 136, integer:0; 137, call; 138, identifier:filename; 139, attribute; 140, argument_list; 141, string:"bedgraph"; 142, string:"bigwig"; 143, augmented_assignment; 144, assignment; 145, call; 146, comparison_operator:format == "bedgraph"; 147, block; 148, assignment; 149, assignment; 150, call; 151, identifier:bigwigfile; 152, identifier:pf; 153, string:".coverage"; 154, identifier:fastafile; 155, identifier:coveragefile; 156, call; 157, identifier:GenomeCoverageFile; 158, argument_list; 159, identifier:must_open; 160, argument_list; 161, identifier:gcf; 162, identifier:iter_coverage_seqid; 163, call; 164, identifier:fw; 165, identifier:close; 166, attribute; 167, identifier:default; 168, string:"bigwig"; 169, identifier:choices; 170, tuple; 171, identifier:help; 172, string:"Output format"; 173, identifier:default; 174, False; 175, identifier:action; 176, string:"store_true"; 177, identifier:help; 178, string:"Do not sort BAM"; 179, identifier:p; 180, identifier:parse_args; 181, identifier:args; 182, identifier:args; 183, attribute; 184, argument_list; 185, attribute; 186, argument_list; 187, assignment; 188, attribute; 189, argument_list; 190, identifier:Sizes; 191, argument_list; 192, string:"genomeCoverageBed -ibam {0} -g {1}"; 193, identifier:format; 194, identifier:bamfile; 195, identifier:sizesfile; 196, identifier:cmd; 197, string:" -bg"; 198, identifier:bedgraphfile; 199, binary_operator:pf + ".bedgraph"; 200, identifier:sh; 201, argument_list; 202, identifier:format; 203, string:"bedgraph"; 204, return_statement; 205, identifier:bigwigfile; 206, binary_operator:pf + ".bigwig"; 207, identifier:cmd; 208, call; 209, identifier:sh; 210, argument_list; 211, identifier:sh; 212, argument_list; 213, identifier:coveragefile; 214, attribute; 215, string:"w"; 216, identifier:print; 217, argument_list; 218, identifier:coverage; 219, identifier:__doc__; 220, string:"bedgraph"; 221, string:"bigwig"; 222, string:"coverage"; 223, identifier:sys; 224, identifier:exit; 225, not_operator; 226, identifier:logging; 227, identifier:debug; 228, string:"BAM sorting skipped"; 229, identifier:bamfile; 230, call; 231, identifier:bamfile; 232, identifier:rsplit; 233, string:"."; 234, integer:2; 235, identifier:fastafile; 236, identifier:pf; 237, string:".bedgraph"; 238, identifier:cmd; 239, keyword_argument; 240, identifier:bedgraphfile; 241, identifier:pf; 242, string:".bigwig"; 243, attribute; 244, argument_list; 245, identifier:cmd; 246, identifier:cmd; 247, keyword_argument; 248, identifier:opts; 249, identifier:outfile; 250, call; 251, keyword_argument; 252, call; 253, identifier:index; 254, argument_list; 255, identifier:outfile; 256, identifier:bedgraphfile; 257, string:"bedGraphToBigWig {0} {1} {2}"; 258, line_continuation:\; 259, identifier:format; 260, identifier:bedgraphfile; 261, identifier:sizesfile; 262, identifier:bigwigfile; 263, identifier:outfile; 264, identifier:coveragefile; 265, attribute; 266, argument_list; 267, identifier:file; 268, identifier:fw; 269, attribute; 270, argument_list; 271, list; 272, string:"\t"; 273, identifier:join; 274, tuple; 275, identifier:p; 276, identifier:print_help; 277, identifier:bamfile; 278, call; 279, identifier:seqid; 280, call; 281, attribute; 282, argument_list; 283, attribute; 284, argument_list; 285, string:"--fasta={0}"; 286, identifier:format; 287, identifier:fastafile; 288, string:"{0:.1f}"; 289, identifier:format; 290, identifier:cov
code: def coverage(args):
      """
      %prog coverage fastafile bamfile
  
      Calculate coverage for BAM file. BAM file will be sorted unless with
      --nosort.
      """
      p = OptionParser(coverage.__doc__)
      p.add_option("--format", default="bigwig",
                   choices=("bedgraph", "bigwig", "coverage"),
                   help="Output format")
      p.add_option("--nosort", default=False, action="store_true",
                   help="Do not sort BAM")
      p.set_outfile()
      opts, args = p.parse_args(args)
  
      if len(args) != 2:
          sys.exit(not p.print_help())
  
      fastafile, bamfile = args
      format = opts.format
      if opts.nosort:
          logging.debug("BAM sorting skipped")
      else:
          bamfile = index([bamfile, "--fasta={0}".format(fastafile)])
  
      pf = bamfile.rsplit(".", 2)[0]
      sizesfile = Sizes(fastafile).filename
      cmd = "genomeCoverageBed -ibam {0} -g {1}".format(bamfile, sizesfile)
      if format in ("bedgraph", "bigwig"):
          cmd += " -bg"
          bedgraphfile = pf + ".bedgraph"
          sh(cmd, outfile=bedgraphfile)
  
          if format == "bedgraph":
              return bedgraphfile
  
          bigwigfile = pf + ".bigwig"
          cmd = "bedGraphToBigWig {0} {1} {2}".\
                      format(bedgraphfile, sizesfile, bigwigfile)
          sh(cmd)
          return bigwigfile
  
      coveragefile = pf + ".coverage"
      if need_update(fastafile, coveragefile):
          sh(cmd, outfile=coveragefile)
  
      gcf = GenomeCoverageFile(coveragefile)
      fw = must_open(opts.outfile, "w")
      for seqid, cov in gcf.iter_coverage_seqid():
          print("\t".join((seqid, "{0:.1f}".format(cov))), file=fw)
      fw.close()