module external CreateCodonModelMatrices, CreateParametricQMatrix;
# function for creating the set of codon matrices according to the Yang-Models M0, M1/2, M3, M7/8
# parameters: model - specifies the model; freq - array of codon frequencies; w - array of
# syn/non-syn ratios; p and q - parameters of beta-distribution (for M7/8)
# creates the following global objects:
# - ...
CreateCodonModelMatrices := proc(model:{'M0','M2','M3','M8'}, freq:list(nonnegative), kappa:nonnegative, w:{nonnegative, list(nonnegative), set(nonnegative)}; (props_=[]):{nonnegative, list(nonnegative)}, p:positive, q:positive)
    if type(props_, list) then
        props := copy(props_):
        pn := 1-sum(props):
        if pn < 0 then error('proportions add up to > 1!'): fi:
        if pn > 0 then props := append(props, pn): fi:
    fi:
    if model = 'M0' then
        if not type(w, nonnegative) then
            error('M0 requres a single w-value!'):
        fi:
        Q := CreateParametricQMatrix(freq, kappa, w):
        k := -sum(Q[i,i]*freq[i],i=1..64);
        Q := Q/k;
        Qarr := [CreatePAM1Matrix(Q, freq)]:
    elif model = 'M2' then
        if not type(w, nonnegative) then
            error('M2 requires exactly one w-value! If you want to simulate M1 using M2, set w to 1.'):
        fi:
        warr := copy([0,1]):
        warr := append(warr, w):
        nw := length({op(warr)}):
        if length(props) <> nw then error('number of proportions does not match number of w classes.'): fi:
        Qarr := CreateArray(1..nw,0):
        for i to nw do
            Qarr[i] := CreateParametricQMatrix(freq, kappa, warr[i]):
        od:
        
    elif model = 'M3' then
        if not type(w, list) and not type(w,set) or length(w) < 2 then
            error('M3 should have at least 2 classes! Use M0 instead.'):
        fi:
        if not type(props_, list) or length(w) <> length(props) then error('number of proportions does not match number of w classes.') fi:
        Qarr := CreateArray(1..length(w), 0):
        for i to length(w) do
            Qarr[i] := CreateParametricQMatrix(freq, kappa, w[i]):
        od:            
    elif model = 'M8' then
        if not (type(w, nonnegative) and assigned(p) and assigned(q)) then
            error('M8 requires exactly one w-value >= 1 as well as parameters p and q!'):
        fi:
        if type(props_, list) then error('M0 requires exactly one proportion.'): fi:
        props := [seq(props_/10,10)]:
        props := append(props, 1-props_):
        Qarr := CreateArray(1..11, 0):
        for k from 0 to 9 do
            w1 := InverseCDFBeta(k/10, p, q):
            w2 := InverseCDFBeta((k+1)/10, p, q):
            Qarr[k+1] := CreateParametricQMatrix(freq, kappa, (w1+w2)/2):
        od:
        Qarr[11] := CreateParametricQMatrix(freq, kappa, w):
    fi:
    
    if model <> 'M0' then
        Qarr := CreatePAM1Matrices(Qarr, freq, props):
    fi:
    Qarr
end:


# creates a Q-Matrix from the frequencies and given kappa and w (omega) parameters. This Q-Matrix
# is then used for creating corresponding CodonMatrices
CreateParametricQMatrix := proc(freq, kappa, win)
    if win = 0 then 
        w := 1e-15: # workaround to prevent crash when using w=0
    else
        w := win:
    fi:
    Q := CreateArray(1..64, 1..64, 0):
    for i to 64 do
        ci := CIntToCodon(i):
        for j to 64 do
            if i = j then next: fi:
            cj := CIntToCodon(j):
            if HammingSearchAllString(ci, cj) <> [] then
                Q[i,j] := freq[j]:
                for pos to 3 do if ci[pos] <> cj[pos] then break fi: od:
                if (ci[pos] = 'A' or ci[pos] = 'G') and (cj[pos] = 'A' or cj[pos] = 'G') or
                   (ci[pos] = 'C' or ci[pos] = 'T') and (cj[pos] = 'T' or cj[pos] = 'C') then
                    Q[i,j] := Q[i,j] * kappa:
                fi:
                if CIntToA(i) <> CIntToA(j) then
                    Q[i,j] := Q[i,j] * w:
                fi:
            fi:
        od;
    od:
        
    for i to 64 do
        Q[i,i] := -sum(Q[i]):
    od:
   
    transpose(Q)
end:


CreatePAM1Matrix := proc(Q_, freq) option internal;
    Q := copy(Q_):
    M := exp(Q):
    t := M*freq-freq:
    if max(t)>1e-6 or min(t)<-1e-6 then
        error('M*f should be f, but is',t) fi;
    
    do  d := sum( freq[i]*(1-M[i,i]), i=1..64 );
        if |d-0.01| < DBL_EPSILON then break fi;
        Q := Q * 0.01/d;
        M := exp(Q)
    od:
    Q
end:

CreatePAM1Matrices := proc(Qarr_, freq, props) option internal;
    Qarr := copy(Qarr_):
    M := [seq(exp(Qarr[i]),i=1..length(Qarr))]:
    
    do d := sum(props[i]*sum(freq[j]*(1-M[i,j,j]), j=1..64), i=1..length(Qarr)):
        if |d-0.01| < DBL_EPSILON then break fi:
        Qarr := Qarr * 0.01/d:
        M := [seq(exp(Qarr[i]),i=1..length(Qarr))]:
    od:
    Qarr
end:
    

# Returns distribution function of the standard form of the beta distribution, 
# that is, the incomplete beta ratio I_x(p,q).
#
# lnbeta is log of the complete beta function; provide it if known,
# and otherwise use 0.
#
# This is called from InverseCDFBeta() in a root-finding loop.

# This routine is a translation into Darwin of a C function from PAML,
# which in turn is based on a Fortran subroutine by W. Fullerton of
# Los Alamos Scientific Laboratory.
# Bosten and Battiste (1974).
# Remark on Algorithm 179, CACM 17, p153, (1974).
# 

CDFBeta := proc(x:nonnegative, pin:positive, qin:positive; (lnbeta=0):numeric) option internal;
    alneps := log(DBL_EPSILON): sml := 2.2250738585072014e-308: alnsml := log(sml):
    if x < 1e-15 then return(0)
    elif x > 1-1e-15 then return(1) fi:
    
    y := x: p := pin: q := qin:
    
    # swap tails if x is greater than the mean
    if p / (p + q) < x then
        y := 1 - y: p := qin: q := pin:
    fi:
    
    if lnbeta = 0 then lnbeta := LnGamma(p) + LnGamma(q) - LnGamma(p+q): fi:
    
    if (p + q) * y / (p + 1) < DBL_EPSILON then # tail approximation
        ans := 0:
        xb := p * log(max(y, sml)) - log(p) - lnbeta:
        if xb > alnsml and y != 0 then
            ans := exp(xb):
        fi:
        if y != x or p != pin then
            ans := 1- ans:
        fi:
    else
        # evaluate the infinite sum first.  term will equal
        # y^p / beta(ps, p) * (1 - ps)-sub-i * y^i / fac(i)
        ps := q - floor(q):
        if ps = 0 then
            ps := 1:
        fi:
        
        xb := LnGamma(ps) + LnGamma(p) - LnGamma(ps+p):
        xb := p * log(y) - xb - log(p):
        
        ans := 0:
        if xb >= alnsml then
            ans := exp(xb):
            term := ans * p:
            if ps <> 1 then
                n := max(alneps/log(y), 4.0):
                for i to n do
                    xi := i:
                    term := term * (xi - ps) * y / xi:
                    ans := ans + term / (p + xi):
                od:
            fi:
        fi:
    
        # evaluate the finite sum
        if q > 1 then
            xb := p * log(y) + q * log(1-y) - lnbeta - log(q):
            ib := xb / alnsml:
            if ib < 0 then ib := 0: fi:
            term := exp(xb - ib * alnsml):
            c := 1 / (1 - y):
            p1 := q * c / (p + q - 1):
            
            finsum := 0:
            n := floor(q):
            if q = n then
                n := n - 1:
            fi:
            for i to n do
                if p1 <= 1 and term / DBL_EPSILON <= finsum then break: fi:
                xi := i:
                term := (q - xi + 1) * c * term / (p + q - xi):
                if term > 1 then
                    ib := ib - 1:
                    term := term * sml:
                fi:
                if ib = 0 then finsum := finsum + term: fi:
            od:
            ans := ans + finsum:
        fi:
        if y <> x or p <> pin then ans := 1 - ans: fi:
        if ans > 1 then ans := 1: fi:
        if ans < 0 then ans := 0: fi:
    fi:
    return(ans):
end:


# Calculates the inverse CDF of the beta distribution. This procedure is
# a translation of Ziheng Yangs implementation in PAML. Comments are preserved.
InverseCDFBeta := proc(prob:numeric, p:numeric, q:numeric) option internal;
# This calculates the inverseCDF of the beta distribution
#
# Cran, G. W., K. J. Martin and G. E. Thomas (1977).
# Remark AS R19 and Algorithm AS 109, Applied Statistics, 26(1), 111-114.
# Remark AS R83 (v.39, 309-310) and correction (v.40(1) p.236).
#
# My own implementation of the algorithm did not bracket the variable well.  
# This version is Adpated from the pbeta and qbeta routines from 
# "R : A Computer Language for Statistical Data Analysis".  It fails for 
# extreme values of p and q as well, although it seems better than my 
# previous version.
# Ziheng Yang, May 2001

    fpu := 3e-308: acu_min := 1e-300: lower := fpu: upper := 1-2.22e-16;
    # acu_min>= fpu: Minimal value for accuracy 'acu' which will depend on (a,p);
    niterations := 2000:
    prev := 0: tx := 0:

    # test for admissibility of parameters
    if prob < 0 or prob > 1 or p < 0 or q<0 then error('out of range in InverseCDFBeta') fi:

    # define accuracy and initialize
    xinbta := prob;

    if prob = 0 or prob = 1 then return(prob) fi:

    lnbeta := LnGamma(p) + LnGamma(q) - LnGamma(p+q);

    # change tail if necessary;  afterwards   0 < a <= 1/2
    if prob <= 0.5 then
        a := prob;   pp := p; qq := q; swap_tail := false;
    else
        a := 1 - prob; pp := q; qq := p; swap_tail := true;
    fi:

    # calculate the initial approximation
    r := sqrt(-log(a * a));
    y := r - (2.30753+0.27061*r)/(1 + (0.99229+0.04481*r) * r);

    if pp > 1 and qq > 1 then
        r := (y * y - 3.) / 6.;
        s := 1. / (pp*2. - 1.);
        t := 1. / (qq*2. - 1.);
        h := 2. / (s + t);
        w := y * sqrt(h + r) / h - (t - s) * (r + 5 / 6 - 2 /(3 * h));
        xinbta := pp / (pp + qq * exp(w + w));
    else
        r := qq*2.;
        t := 1. / (9. * qq);
        t := r * (1 - t + y * sqrt(t))^3:
        if t <= 0 then
            xinbta := 1 - exp((log((1 - a) * qq) + lnbeta) / qq):
        else
            t := (4 * pp + r - 2) / t:
            if t <= 1 then
                xinbta := exp((log(a * pp) + lnbeta) / pp);
            else
                xinbta := 1 - 2 / (t+1):
            fi:
        fi:
    fi:

    # solve for x by a modified newton-raphson method, using CDFBeta
    r := 1 - pp;
    t := 1 - qq;
    yprev := 0:
    adj := 1:
   
    # Changes made by Ziheng to fix a bug in qbeta()
    # qbeta(0.25, 0.143891, 0.05) = 3e-308   wrong (correct value is 0.457227)

    if xinbta <= lower or xinbta >= upper then xinbta := (a+.5)/2 fi:

    # Desired accuracy should depend on (a,p)
    # This is from Remark .. on AS 109, adapted.
    # However, it's not clear if this is "optimal" for IEEE double prec.
    # acu = fmax2(acu_min, pow(10., -25. - 5./(pp * pp) - 1./(a * a)));
    # NEW: 'acu' accuracy NOT for squared adjustment, but simple;
    # ---- i.e.,  "new acu" = sqrt(old acu)

    acu := 10^(-13 - 2.5/(pp * pp) - 0.5/(a * a));
    acu := max(acu, acu_min);

    for i_pb to niterations do
        y := CDFBeta(xinbta, pp, qq, lnbeta);
        y := (y - a) * exp(lnbeta + r * log(xinbta) + t * log(1 - xinbta));
        if y * yprev <= 0 then prev := max(abs(adj),fpu) fi:
        g := 1:
        for i_inn to niterations do
            adj := g * y;
            if abs(adj) < prev then
                tx := xinbta - adj; # trial new x
                if tx >= 0 and tx <= 1 then
                   if prev <= acu or abs(y) <= acu then return(If(swap_tail, 1 - xinbta, xinbta)): fi:
                   if tx != 0 and tx != 1 then  break fi:
                fi:
            fi:
            g := g / 3:
        od:
        if abs(tx-xinbta)<fpu then return(If(swap_tail, 1 - xinbta, xinbta)): fi:
        xinbta := tx;
        yprev := y;
    od:

    return(If(swap_tail, 1 - xinbta, xinbta)):
end:

end: #module