nodes: 0, module; 1, function_definition; 2, function_name:check_bam; 3, parameters; 4, block; 5, identifier:bam; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, if_statement; 12, if_statement; 13, return_statement; 14, identifier:samtype; 15, string:"bam"; 16, comment:"""Check if bam file is valid.
  
      Bam file should:
      - exists
      - has an index (create if necessary)
      - is sorted by coordinate
      - has at least one mapped read
      """; 17, call; 18, assignment; 19, not_operator; 20, block; 21, not_operator; 22, block; 23, comparison_operator:samtype == "bam"; 24, block; 25, identifier:samfile; 26, attribute; 27, argument_list; 28, identifier:samfile; 29, call; 30, call; 31, expression_statement; 32, expression_statement; 33, comment:# Need to reload the samfile after creating index; 34, expression_statement; 35, comparison_operator:samfile.header['HD']['SO'] == 'coordinate'; 36, expression_statement; 37, expression_statement; 38, identifier:samtype; 39, string:"bam"; 40, expression_statement; 41, if_statement; 42, identifier:ut; 43, identifier:check_existance; 44, identifier:bam; 45, attribute; 46, argument_list; 47, attribute; 48, argument_list; 49, call; 50, assignment; 51, call; 52, subscript; 53, string; 54, call; 55, call; 56, call; 57, comparison_operator:samfile.mapped == 0; 58, block; 59, identifier:pysam; 60, identifier:AlignmentFile; 61, identifier:bam; 62, string:"rb"; 63, identifier:samfile; 64, identifier:has_index; 65, attribute; 66, argument_list; 67, identifier:samfile; 68, call; 69, attribute; 70, argument_list; 71, subscript; 72, string; 73, string_content:coordinate; 74, attribute; 75, argument_list; 76, attribute; 77, argument_list; 78, attribute; 79, argument_list; 80, attribute; 81, integer:0; 82, expression_statement; 83, expression_statement; 84, identifier:pysam; 85, identifier:index; 86, identifier:bam; 87, attribute; 88, argument_list; 89, identifier:logging; 90, identifier:info; 91, string:"Nanoget: No index for bam file could be found, created index."; 92, attribute; 93, string; 94, string_content:SO; 95, identifier:logging; 96, identifier:error; 97, call; 98, identifier:sys; 99, identifier:exit; 100, string:"Please use a bam file sorted by coordinate."; 101, identifier:logging; 102, identifier:info; 103, call; 104, identifier:samfile; 105, identifier:mapped; 106, call; 107, call; 108, identifier:pysam; 109, identifier:AlignmentFile; 110, identifier:bam; 111, string:"rb"; 112, identifier:samfile; 113, identifier:header; 114, string_content:HD; 115, attribute; 116, argument_list; 117, attribute; 118, argument_list; 119, attribute; 120, argument_list; 121, attribute; 122, argument_list; 123, string:"Nanoget: Bam file {} not sorted by coordinate!."; 124, identifier:format; 125, identifier:bam; 126, string:"Nanoget: Bam file {} contains {} mapped and {} unmapped reads."; 127, identifier:format; 128, identifier:bam; 129, attribute; 130, attribute; 131, identifier:logging; 132, identifier:error; 133, call; 134, identifier:sys; 135, identifier:exit; 136, call; 137, identifier:samfile; 138, identifier:mapped; 139, identifier:samfile; 140, identifier:unmapped; 141, attribute; 142, argument_list; 143, attribute; 144, argument_list; 145, string:"Nanoget: Bam file {} does not contain aligned reads."; 146, identifier:format; 147, identifier:bam; 148, string:"FATAL: not a single read was mapped in bam file {}"; 149, identifier:format; 150, identifier:bam
code: def check_bam(bam, samtype="bam"):
      """Check if bam file is valid.
  
      Bam file should:
      - exists
      - has an index (create if necessary)
      - is sorted by coordinate
      - has at least one mapped read
      """
      ut.check_existance(bam)
      samfile = pysam.AlignmentFile(bam, "rb")
      if not samfile.has_index():
          pysam.index(bam)
          samfile = pysam.AlignmentFile(bam, "rb")  # Need to reload the samfile after creating index
          logging.info("Nanoget: No index for bam file could be found, created index.")
      if not samfile.header['HD']['SO'] == 'coordinate':
          logging.error("Nanoget: Bam file {} not sorted by coordinate!.".format(bam))
          sys.exit("Please use a bam file sorted by coordinate.")
      if samtype == "bam":
          logging.info("Nanoget: Bam file {} contains {} mapped and {} unmapped reads.".format(
              bam, samfile.mapped, samfile.unmapped))
          if samfile.mapped == 0:
              logging.error("Nanoget: Bam file {} does not contain aligned reads.".format(bam))
              sys.exit("FATAL: not a single read was mapped in bam file {}".format(bam))
      return samfile