/* Written by David Wilson */ /****************************************************************************** Operations for evaluating probabilities pertaining to a protein sequence. The sequence is specified as a character string char *seq and a length. ******************************************************************************/ #include "scconst.h" #include #include "sc2seq.h" #include "hmm.h" #define MALLOC(NUMBER,TYPE) ((TYPE*)Malloc((NUMBER)*sizeof(TYPE))) #define CALLOC(NUMBER,TYPE) ((TYPE*)Calloc((NUMBER),sizeof(TYPE))) #define REALLOC(PTR,NUMBER,TYPE) ((TYPE*)Realloc((PTR),(NUMBER)*sizeof(TYPE))) #define FREE(PTR) free(PTR) /*********The following are defined in sc2seq_interface.c **************/ extern double many_pprobs[MAX_TABLE_NUMBER][NUM_RES_TYPE][POSNUM], many_pprobp[MAX_TABLE_NUMBER][NUM_RES_TYPE][NUM_RES_TYPE][POSNUM][POSNUM]; extern double gprobs[NUM_RES_TYPE], gprobp[NUM_RES_TYPE][NUM_RES_TYPE][POSNUM]; extern double (*pprobs)[POSNUM]; extern double (*pprobp)[NUM_RES_TYPE][POSNUM][POSNUM]; /************************************************************************/ void *Malloc(), *Calloc(), *Realloc(), Free(); /****************************************************************************** Given a sequence of normalized probabilities, alpha (beta) gives the normalized probability that one sees the prefix upto (suffix from) the Tth state, given this state is q. ;; Works even if alpha (beta) == nprobs. The alpha and beta arrays are indexed as the nprobs array is. ******************************************************************************/ Alphas (double *alpha, double *nprob, int length, HMM *hmm) { int i,t,n; double *alphat, *nprobt; n = hmm->nstates; bzero(alpha,length*n*sizeof(double)); for (i=n-1; i>=0; --i) alpha[i] = hmm->initprob[i] * nprob[i]; for (t=1; tntrans-1; i>=0; --i) alphat[hmm->transt[i]] += alpha[hmm->transf[i]] * hmm->transprob[i]; for (i=n-1; i>=0; --i) alphat[i] *= nprobt[i]; alpha = alphat; nprob = nprobt; } } Betas (double *beta, double *nprob, int length, HMM *hmm) { int i,t,n; double *betat, *nprobt; n = hmm->nstates; bzero(beta,length*n*sizeof(double)); betat = &(beta [n*(length-1)]); nprobt = &(nprob[n*(length-1)]); for (i=n-1; i>=0; --i) betat[i] = hmm->finprob[i] * nprobt[i]; for (t=length-2; t>=0; --t) { beta = &(betat [-n]); nprob = &(nprobt[-n]); for (i=hmm->ntrans-1; i>=0; --i) beta[hmm->transf[i]] += betat[hmm->transt[i]] * hmm->transprob[i]; for (i=n-1; i>=0; --i) beta[i] *= nprob[i]; betat = beta; nprobt = nprob; } } /****************************************************************************** Given a sequence, for each residue for each state output the probability that this residue was in that state. ******************************************************************************/ /* Contains a static variable pointing to a dynamically allocated array of doubles, and a static variable giving the length of the previous array size (initially 0). Memory is needed for 4*nstates*length. This value is rounded up to the nearest power of 2 when determining how much memory to allocate. */ double *Memory (amount) { static double *memory=(void*)0; static int memsize=0; if (!memsize) { memsize = amount; memory = MALLOC(memsize,double); } if (memsize