def check_bam(bam, samtype="<STR_LIT>"):
ut.check_existance(bam)<EOL>samfile = pysam.AlignmentFile(bam, "<STR_LIT:rb>")<EOL>if not samfile.has_index():<EOL><INDENT>pysam.index(bam)<EOL>samfile = pysam.AlignmentFile(bam, "<STR_LIT:rb>")  <EOL>logging.info("<STR_LIT>")<EOL><DEDENT>if not samfile.header['<STR_LIT>']['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>logging.error("<STR_LIT>".format(bam))<EOL>sys.exit("<STR_LIT>")<EOL><DEDENT>if samtype == "<STR_LIT>":<EOL><INDENT>logging.info("<STR_LIT>".format(<EOL>bam, samfile.mapped, samfile.unmapped))<EOL>if samfile.mapped == <NUM_LIT:0>:<EOL><INDENT>logging.error("<STR_LIT>".format(bam))<EOL>sys.exit("<STR_LIT>".format(bam))<EOL><DEDENT><DEDENT>return samfile<EOL>
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