def convert_cufflinks_mapq(in_bam, out_bam=None):
    """Cufflinks expects the not-valid 255 MAPQ for uniquely mapped reads.
    This detects the maximum mapping quality in a BAM file and sets
    reads with that quality to be 255
    """
    CUFFLINKSMAPQ = 255
    if not out_bam:
        out_bam = os.path.splitext(in_bam)[0] + "-cufflinks.bam"
    if utils.file_exists(out_bam):
        return out_bam
    maxmapq = estimate_max_mapq(in_bam)
    if maxmapq == CUFFLINKSMAPQ:
        return in_bam
    logger.info("Converting MAPQ scores in %s to be Cufflinks compatible." % in_bam)
    with pysam.Samfile(in_bam, "rb") as in_bam_fh:
        with pysam.Samfile(out_bam, "wb", template=in_bam_fh) as out_bam_fh:
            for read in in_bam_fh:
                if read.mapq == maxmapq and not read.is_unmapped:
                    read.mapq = CUFFLINKSMAPQ
                out_bam_fh.write(read)
    return out_bam
Cufflinks expects the not-valid 255 MAPQ for uniquely mapped reads.
    This detects the maximum mapping quality in a BAM file and sets
    reads with that quality to be 255