<s> import os <EOL> import pysam <EOL> import argparse <EOL> import logging <EOL> import subprocess <EOL> from collections import OrderedDict as od <EOL> logger = logging . getLogger ( __name__ ) <EOL> FORMAT = '<STR_LIT>' <EOL> logging . basicConfig ( format = FORMAT ) <EOL> logger . setLevel ( logging . INFO ) <EOL> def main ( args ) : <EOL> assert os . path . exists ( args . fasta + '<STR_LIT>' ) , '<STR_LIT>' % args . fasta <EOL> fa = pysam . FastaFile ( args . fasta ) <EOL> bam = pysam . AlignmentFile ( args . bam , '<STR_LIT:rb>' ) <EOL> bam_reflen = od ( [ ( ref , length ) for ref , length in zip ( bam . references , bam . lengths ) ] ) <EOL> with open ( args . outfa , '<STR_LIT:w>' ) as outfa : <EOL> for ref in bam . references : <EOL> assert ref in fa . references , '<STR_LIT>' % ref <EOL> assert fa . get_reference_length ( ref ) == bam_reflen [ ref ] , '<STR_LIT>' % ref <EOL> logger . info ( '<STR_LIT>' % ( ref , args . outfa , bam_reflen [ ref ] ) ) <EOL> outfa . write ( '<STR_LIT>' % ( ref , fa . fetch ( ref ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , required = True , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , required = True , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> main ( args ) </s>
