def main(args):
    assert os.path.exists(args.fasta + '.fai'), 'please run samtools faidx %s' % args.fasta

    fa  = pysam.FastaFile(args.fasta)
    bam = pysam.AlignmentFile(args.bam, 'rb')

    bam_reflen = od([(ref, length) for ref, length in zip(bam.references, bam.lengths)])

    with open(args.outfa, 'w') as outfa:
        for ref in bam.references:
            assert ref in fa.references, 'reference not shared: %s' % ref
            assert fa.get_reference_length(ref) == bam_reflen[ref], ref % 'length mismatch on contig/chr %s'

            logger.info('writing %s to %s (len = %d)' % (ref, args.outfa, bam_reflen[ref]))

            outfa.write('>%s\n%s\n' % (ref, fa.fetch(ref)))
dataset/ETHPy150Open adamewing/bamsurgeon/scripts/match_fasta_to_bam.py/main/swapped operands of `%`