def select_ChIP_by_score_cutoff(ChIP, scorecutoff = 0, descend = True):
    """Select ChIP regions according to a score cutoff
    
    Parameters:
    1. ChIP: Bed object
    2. scorecutoff: the cut-off for scores
    3. descend: If True, select ChIP regions with higher scores first.
    """
    
    # if no chroms, just return None
    chroms = ChIP.get_chroms()
    if not chroms: return None
    
    # if any of chroms does not have 'score', just return None
    YESSCORE = True
    for chrom in chroms:
        YESSCORE = YESSCORE and ChIP[chrom].has_key('score')
    if not YESSCORE: return None
    
    newChIP = Bed()
    for chrom in chroms:
        if descend:
            b = map(lambda x: x>= scorecutoff, ChIP[chrom]['score'])
        else:
            b = map(lambda x: x<= scorecutoff, ChIP[chrom]['score'])
        newChIP.bed = {}
        newChIP.bed[chrom]={}
        for key in ChIP[chrom].keys():
            if key == 'start' or key =='end':
                newChIP.bed[chrom][key] = array('l', array_extractor(ChIP[chrom]['start'], b))
            elif key == 'score':
                newChIP.bed[chrom][key] = array('d', array_extractor(ChIP[chrom]['score'], b))
            else:
                newChIP.bed[chrom][key] = array_extractor(ChIP[chrom][key])
                
    return newChIP
dataset/ETHPy150Open taoliu/taolib/Assoc/inout.py/select_ChIP_by_score_cutoff/original