import math


def ZScore_GeneGo(n, N, n1, n2):
    """Each subnetwork is associated with a Z-score which ranks the subnetworks according to saturation with the objects from the initial gene list. The Z-score ranks the subnetworks of the analyze network algorithm with regard to their saturation with genes from the experiment. A high Z-score means the network is highly saturated with genes from the experiment. The formula for the Z-score is:
    n (white in picked), N (total), n1 (white), n2 (picked, my gene list)
    Z = (n-n1*n2/N)/Sqrt(n1*n2/N*(1-n2/N)(1-(n1-1)/(N-1)))
    Z = (n*N-n1*n2)/Sqrt(n1*n2*(N-n1)*(N-n2)/(N-1))
    notice this formula is symmetrical for n1 and n2"""
    r=math.sqrt(n1*n2*(N-n1)*(N-n2)/(N-1))
    if r<1e-100: return 0.0
    return (n*N*1.0-n1*n2)/r