clustal-omega-1.2.4/src/clustal/hhalign_wrapper.c
/**
 * @brief PosteriorProbabilities() calculates posterior probabilities 
 *  of aligning a single sequences on-to an alignment containing this sequence
 *
 * @param[in] prMSeq
 * holds the aligned sequences [in] 
 * @param[in] rHMMalignment
 * HMM of the alignment in prMSeq
 * @param[in] rHhalignPara
 * various parameters read from commandline, needed by hhalign()
 * @param[in] pcPosteriorfile
 * name of file into which posterior probability information is written
 *
 * @return score of the alignment FIXME what is this?
 *
 * @note the PP-loop can be parallelised easily FIXME
 */
@brief PosteriorProbabilities() calculates posterior probabilities
of aligning a single sequences on-to an alignment containing this sequence

@param[in] prMSeq
holds the aligned sequences [in]
@param[in] rHMMalignment
HMM of the alignment in prMSeq
@param[in] rHhalignPara
various parameters read from commandline, needed by hhalign()
@param[in] pcPosteriorfile
name of file into which posterior probability information is written



@note the PP-loop can be parallelised easily FIXME
@brief PosteriorProbabilities () calculates posterior probabilities of aligning a single sequences on - to an alignment containing this sequence @param [ in ] prMSeq holds the aligned sequences [ in ] @param [ in ] rHMMalignment HMM of the alignment in prMSeq @param [ in ] rHhalignPara various parameters read from commandline needed by hhalign () @param [ in ] pcPosteriorfile name of file into which posterior probability information is written @note the PP - loop can be parallelised easily FIXME
int
PosteriorProbabilities(mseq_t *prMSeq, hmm_light rHMMalignment, hhalign_para rHhalignPara, char *pcPosteriorfile)
{
    double dScore = 0.0;
    int i;
    int iS; 
    int iNseq = prMSeq->nseqs; 
    int iLenHMM = rHMMalignment.L; 
    int iViterbiCount = 0; 
    char **ppcCopy = NULL;
    char **ppcRepresent = NULL;
    char zcAux[10000] = {0};
    char zcError[10000] = {0};
    hhalign_scores *prHHscores = NULL;
    FILE *pfPP = NULL;
    pfPP = fopen(pcPosteriorfile, "w");
    fprintf(pfPP, "#1.i\t2.name\t\t3.L1\t4.L2\t5.sum\t\t6.sum/L1\t7.HH\n");
    prHHscores = CKCALLOC(iNseq, sizeof(hhalign_scores));
    for (iS = 0; iS < iNseq; iS++){
        memset(&(prHHscores[iS]), 0, sizeof(hhalign_scores));
    }
    ppcCopy         = CKCALLOC(1, sizeof(char *));
    ppcCopy[0]      = CKCALLOC(2*iLenHMM, sizeof(char));
    ppcRepresent    = CKCALLOC(1, sizeof(char *));
    ppcRepresent[0] = CKCALLOC(2*iLenHMM, sizeof(char));
    for (i = 0; i < iLenHMM; i++){
        ppcRepresent[0][i] = rHMMalignment.seq[rHMMalignment.ncons][i+1];
    }
    iViterbiCount = 0;
    for (iS = 0; iS < iNseq; iS++){
        char *pcIter = NULL;
        for (pcIter = prMSeq->orig_seq[iS]; ('-' == *pcIter) || ('.' == *pcIter); pcIter++);
        strcpy(ppcCopy[0], pcIter);
        for (pcIter = &ppcCopy[0][strlen(ppcCopy[0])-1]; ('-' == *pcIter) || ('.' == *pcIter); pcIter--);
        pcIter++;
        *pcIter = '\0';
        zcError[0] = '\0';
        hhalign(ppcCopy, 1, NULL, 
                ppcRepresent, 0, NULL, 
                &dScore, &rHMMalignment, &rHMMalignment, 
                NULL, NULL, NULL, NULL,
                rHhalignPara, &prHHscores[iS], 0, FALSE , 
                zcAux, zcError);
        if (NULL != strstr(zcError, "Viterbi")){
            iViterbiCount++;
        }
    } 
    Log(&rLog, LOG_INFO, "Viterbi algorithm triggered %d times (out of %d)", iViterbiCount, iNseq-1);
    for (iS = 0; iS < iNseq; iS++){
        fprintf(pfPP, "%d\t%10s\t%3d"
               , iS, prMSeq->sqinfo[iS].name, (int)(strlen(prMSeq->orig_seq[iS])));
        fprintf(pfPP, "\t%3d\t%f\t%f\t%f", 
                prHHscores[iS].L, prHHscores[iS].sumPP, prHHscores[iS].sumPP/strlen(prMSeq->orig_seq[iS]), prHHscores[iS].hhScore);
        fprintf(pfPP, "\n");
    } 
    fclose(pfPP); pfPP = NULL;
    free(ppcRepresent[0]); ppcRepresent[0] = NULL;
    free(ppcCopy[0]); ppcCopy[0] = NULL;
    free(ppcRepresent); ppcRepresent = NULL;
    free(ppcCopy); ppcCopy = NULL;
    free(prHHscores); prHHscores = NULL;
    return 1;
}
int PosteriorProbabilities ( mseq_t * prMSeq , hmm_light rHMMalignment , hhalign_para rHhalignPara , char * pcPosteriorfile ) { double dScore = 0.0 ; int i ; int iS ; int iNseq = prMSeq -> nseqs ; int iLenHMM = rHMMalignment . L ; int iViterbiCount = 0 ; char * * ppcCopy = NULL ; char * * ppcRepresent = NULL ; char zcAux [ 10000 ] = { 0 } ; char zcError [ 10000 ] = { 0 } ; hhalign_scores * prHHscores = NULL ; FILE * pfPP = NULL ; pfPP = fopen ( pcPosteriorfile , " " ) ; fprintf ( pfPP , " \t \t \t \t \t \t \t \t \n " ) ; prHHscores = CKCALLOC ( iNseq , sizeof ( hhalign_scores ) ) ; for ( iS = 0 ; iS < iNseq ; iS ++ ) { memset ( & ( prHHscores [ iS ] ) , 0 , sizeof ( hhalign_scores ) ) ; } ppcCopy = CKCALLOC ( 1 , sizeof ( char * ) ) ; ppcCopy [ 0 ] = CKCALLOC ( 2 * iLenHMM , sizeof ( char ) ) ; ppcRepresent = CKCALLOC ( 1 , sizeof ( char * ) ) ; ppcRepresent [ 0 ] = CKCALLOC ( 2 * iLenHMM , sizeof ( char ) ) ; for ( i = 0 ; i < iLenHMM ; i ++ ) { ppcRepresent [ 0 ] [ i ] = rHMMalignment . seq [ rHMMalignment . ncons ] [ i + 1 ] ; } iViterbiCount = 0 ; for ( iS = 0 ; iS < iNseq ; iS ++ ) { char * pcIter = NULL ; for ( pcIter = prMSeq -> orig_seq [ iS ] ; ( ' ' == * pcIter ) || ( ' ' == * pcIter ) ; pcIter ++ ) ; strcpy ( ppcCopy [ 0 ] , pcIter ) ; for ( pcIter = & ppcCopy [ 0 ] [ strlen ( ppcCopy [ 0 ] ) - 1 ] ; ( ' ' == * pcIter ) || ( ' ' == * pcIter ) ; pcIter -- ) ; pcIter ++ ; * pcIter = ' \0 ' ; zcError [ 0 ] = ' \0 ' ; hhalign ( ppcCopy , 1 , NULL , ppcRepresent , 0 , NULL , & dScore , & rHMMalignment , & rHMMalignment , NULL , NULL , NULL , NULL , rHhalignPara , & prHHscores [ iS ] , 0 , FALSE , zcAux , zcError ) ; if ( NULL != strstr ( zcError , " " ) ) { iViterbiCount ++ ; } } Log ( & rLog , LOG_INFO , " " , iViterbiCount , iNseq - 1 ) ; for ( iS = 0 ; iS < iNseq ; iS ++ ) { fprintf ( pfPP , " \t \t " , iS , prMSeq -> sqinfo [ iS ] . name , ( int ) ( strlen ( prMSeq -> orig_seq [ iS ] ) ) ) ; fprintf ( pfPP , " \t \t \t \t " , prHHscores [ iS ] . L , prHHscores [ iS ] . sumPP , prHHscores [ iS ] . sumPP / strlen ( prMSeq -> orig_seq [ iS ] ) , prHHscores [ iS ] . hhScore ) ; fprintf ( pfPP , " \n " ) ; } fclose ( pfPP ) ; pfPP = NULL ; free ( ppcRepresent [ 0 ] ) ; ppcRepresent [ 0 ] = NULL ; free ( ppcCopy [ 0 ] ) ; ppcCopy [ 0 ] = NULL ; free ( ppcRepresent ) ; ppcRepresent = NULL ; free ( ppcCopy ) ; ppcCopy = NULL ; free ( prHHscores ) ; prHHscores = NULL ; return 1 ; }
@brief PosteriorProbabilities() calculates posterior probabilities
  of aligning a single sequences on-to an alignment containing this sequence
@brief PosteriorProbabilities () calculates posterior probabilities of aligning a single sequences on - to an alignment containing this sequence
/* iterator for sequences */ /* number of sequences */ /* length of alignment */ /* counts how often Viterbi is triggered */ /* FIXME: this loop can be parallelised, FS r288 */ /* single sequences may very well contain leading/trailing gaps,
         * this will trigger warning in Transfer:hhalignment-C.h */ /*prMSeq->orig_seq[iS]*/ /* debug */ /* Log-Level*/ /* 0 <= iS < iNseq */ /* 0 <= iS < iNseq */