def getsegs(bam, mindepth, minlength):
    seglist = []
    cmd = ['samtools', 'mpileup', bam]
    
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)

    seg = {'chrom': None, 'start': None, 'end': None}

    for line in p.stdout:
        chrom, pos, base, depth = line.strip().split()[:4]

        depth = int(depth)
        pos   = int(pos)

        if seg['chrom'] != chrom:
            if seg['chrom'] is not None:
                if seg['end'] - seg['start'] >= minlength:
                    seglist.append(seg)

            seg = {'chrom': chrom, 'start': pos, 'end': pos}

        else:
            if pos == seg['end']+1 and depth > mindepth:
                seg['end'] = pos

            else:
                if seg['start'] - seg['end'] >= minlength:
                    seglist.append(seg)
                seg = {'chrom': chrom, 'start': pos, 'end': pos}

    return seglist
dataset/ETHPy150Open adamewing/bamsurgeon/scripts/covered_segments.py/getsegs/swapped operands of `-`