@contextmanager<EOL>def open_pysam_file(fname, ftype):
try:<EOL><INDENT>if ftype == '<STR_LIT>':<EOL><INDENT>fpysam = pysam.AlignmentFile(fname, '<STR_LIT:rb>')<EOL><DEDENT>elif ftype == '<STR_LIT>':<EOL><INDENT>fpysam = pysam.FastaFile(fname)<EOL><DEDENT>yield fpysam<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>fpysam.close()<EOL><DEDENT>
Open a BAM or FASTA file with pysam (for use with "with" statement)