Finds statistics that correspond to PSM/peptide/protein feature's
    score. Loops through list of qvality generated scores until it finds
    values closest to the feature's svm_score.

def lookup_statistic(score, stats):
    """ Finds statistics that correspond to PSM/peptide/protein feature's
    score. Loops through list of qvality generated scores until it finds
    values closest to the feature's svm_score."""
    if score in stats:
        return stats[score]['q'], stats[score]['PEP'], None
    else:
        lower, warning = None, None
        for stat_score in sorted(stats.keys()):
            if score < stat_score:
                break
            lower = stat_score
        if score > stat_score:
            warning = ('WARNING! Values found with higher svm_score than in '
                       'qvality recalculation!')
        if not lower:
            warning = ('WARNING! Values found with lower svm_score than in '
                       'qvality recalculation were set to PEP=1, qval=1.')
            return '1', '1', warning
        qval = (float(stats[stat_score]['q']) + float(stats[lower]['q'])) / 2
        pep = (float(stats[stat_score]['PEP']) +
               float(stats[lower]['PEP'])) / 2
    return str(qval), str(pep), warning