def BenjaminiHochbergCorrection(pvals, fdr):
num_tests = len(pvals)<EOL>sorted_tests = sorted(pvals, key=lambda tup: tup[<NUM_LIT:1>])<EOL>max_rank = <NUM_LIT:0><EOL>pcutoff = None<EOL>for (rank, (label, p)) in enumerate(sorted_tests):<EOL><INDENT>rank = rank + <NUM_LIT:1> <EOL>bh_threshold = fdr * float(rank) / num_tests<EOL>if p <= bh_threshold: <EOL><INDENT>assert rank > max_rank<EOL>max_rank = rank<EOL>pcutoff = bh_threshold<EOL><DEDENT><DEDENT>if pcutoff == None:<EOL><INDENT>pcutoff = <NUM_LIT:1.0> / num_tests * fdr<EOL><DEDENT>significantlabels = []<EOL>for (rank, (label, p)) in enumerate(sorted_tests):<EOL><INDENT>rank = rank + <NUM_LIT:1> <EOL>if rank <= max_rank:<EOL><INDENT>assert p <= pcutoff<EOL>significantlabels.append(label)<EOL><DEDENT><DEDENT>return (pcutoff, significantlabels)<EOL>
Benjamini-Hochberg procedure to control false discovery rate.

    Calling arguments:

    *pvals* : a list of tuples of *(label, p)*  where *label* is some label assigned
    to each data point, and *p* is the corresponding *P-value*.

    *fdr* : the desired false discovery rate

    The return value is the 2-tuple *(pcutoff, significantlabels)*. After applying
    the algorithm, all data points with *p <= pcutoff* are declared significant.
    The labels for these data points are in *significantlabels*. If there are no
    significant sites, *pcutoff* is returned as the maximum P-value that would
    have made a single point significant.