<s> '''<STR_LIT>''' <EOL> import sys <EOL> import os <EOL> from ngsutils . bam import bam_iter <EOL> import pysam <EOL> def bam_tobed ( fname , out = sys . stdout ) : <EOL> bamfile = pysam . Samfile ( fname , "<STR_LIT:rb>" ) <EOL> for read in bam_iter ( bamfile ) : <EOL> write_read ( read , bamfile . getrname ( read . rname ) , out ) <EOL> bamfile . close ( ) <EOL> def write_read ( read , chrom , out ) : <EOL> if not read . is_unmapped : <EOL> out . write ( '<STR_LIT>' % ( chrom , read . pos , read . aend , read . qname , '<STR_LIT:->' if read . is_reverse else '<STR_LIT:+>' ) ) <EOL> def usage ( ) : <EOL> print __doc__ <EOL> print """<STR_LIT>""" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if len ( sys . argv ) < <NUM_LIT:2> or not os . path . exists ( sys . argv [ - <NUM_LIT:1> ] ) : <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> bam_tobed ( sys . argv [ <NUM_LIT:1> ] ) </s>
