def bam_to_fastq(bam_file, is_paired):
    """Convert a BAM file to fastq files.
    """
    out_files, out_handles = _get_fastq_handles(bam_file,
            is_paired)
    if len(out_handles) > 0:
        in_bam = pysam.Samfile(bam_file, mode='rb')
        for read in in_bam:
            num = 1 if (not read.is_paired or read.is_read1) else 2
            # reverse the sequence and quality if mapped to opposite strand
            if read.is_reverse:
                seq = str(Seq.reverse_complement(Seq.Seq(read.seq)))
                qual = "".join(reversed(read.qual))
            else:
                seq = read.seq
                qual = read.qual
            out_handles[num].write("@%s\n%s\n+\n%s\n" % (read.qname,
                seq, qual))
    [h.close() for h in out_handles.values()]
    return out_files
def bam_to_fastq ( bam_file , is_paired ) : out_files , out_handles = _get_fastq_handles ( bam_file , is_paired ) if len ( out_handles ) > 0 : in_bam = pysam . Samfile ( bam_file , mode = 'rb' ) for read in in_bam : num = 1 if ( not read . is_paired or read . is_read1 ) else 2 if read . is_reverse : seq = str ( Seq . reverse_complement ( Seq . Seq ( read . seq ) ) ) qual = "" . join ( reversed ( read . qual ) ) else : seq = read . seq qual = read . qual out_handles [ num ] . write ( "@%s\n%s\n+\n%s\n" % ( read . qname , seq , qual ) ) [ h . close ( ) for h in out_handles . values ( ) ] return out_files
Convert a BAM file to fastq files.