code: def open_pysam_file(fname, ftype):
      
      try:
          if ftype == 'bam':
              fpysam = pysam.AlignmentFile(fname, 'rb')
          elif ftype == 'fasta':
              fpysam = pysam.FastaFile(fname)
          yield fpysam
      except:
          raise
      else:
          fpysam.close()
docs: Open a BAM or FASTA file with pysam (for use with "with" statement)