clustal-omega-1.2.4/src/clustal-omega.c
/**
 * @brief Create a HMM file from aligned sequences
 *
 * @warning Should be replaced in the future by some internal HMM
 * building routine that does not call external programs
 *
 * @param[in] prMSeq
 * Aligned mseq_t
 * @param[in] pcHMMOut
 * HMM output file name
 *
 * @return Non-zero on error
 *

 */
@brief Create a HMM file from aligned sequences

@warning Should be replaced in the future by some internal HMM
building routine that does not call external programs

@param[in] prMSeq
Aligned mseq_t
@param[in] pcHMMOut
HMM output file name

@return Non-zero on error
@brief Create a HMM file from aligned sequences @warning Should be replaced in the future by some internal HMM building routine that does not call external programs @param [ in ] prMSeq Aligned mseq_t @param [ in ] pcHMMOut HMM output file name @return Non - zero on error
int
AlnToHMMFile(mseq_t *prMSeq, const char *pcHMMOut)
{
    char *tmp_aln = NULL;
    char *tmp_hmm = NULL; 
    char cmdbuf[16384];
    int iHmmerVersion = 0;
    int retcode = OK;
    assert(NULL!=prMSeq);
    assert(NULL!=pcHMMOut);
    if (FALSE == prMSeq->aligned) {
        Log(&rLog, LOG_ERROR, "Sequences need to be aligned to create an HMM");
        return FAILURE;
    }
    iHmmerVersion = HmmerVersion();
    if (2 != iHmmerVersion && 3 != iHmmerVersion) {
        Log(&rLog, LOG_ERROR, "Could not find suitable HMMER binaries");
        return FAILURE;
    }
    tmp_aln = CkStrdup("/tmp/clustalo_tmpaln_XXXXXX");
    if (NULL == mktemp(tmp_aln)) {
        Log(&rLog, LOG_ERROR, "Could not create temporary alignment filename");
        retcode = FAILURE;
        goto cleanup_and_return;
    }
#define LINE_WRAP 60
    if (WriteAlignment(prMSeq, tmp_aln, MSAFILE_STOCKHOLM, LINE_WRAP, FALSE)) {
        Log(&rLog, LOG_ERROR, "Could not save alignment to %s", tmp_aln);
        retcode = FAILURE;
        goto cleanup_and_return;
    }
    if (2 == iHmmerVersion) {
        sprintf(cmdbuf, "hmmbuild %s %s >/dev/null && hmmcalibrate %s >/dev/null",
                pcHMMOut, tmp_aln, pcHMMOut);
        if (system(cmdbuf)) {
            Log(&rLog, LOG_ERROR, "Command '%s' failed", cmdbuf);
            retcode = FAILURE;
            goto cleanup_and_return;
        }
    } else if (3 == iHmmerVersion) {
        tmp_hmm = CkStrdup("/tmp/clustalo_tmphmm2_XXXXXX");
        if (NULL == mktemp(tmp_hmm)) {
            Log(&rLog, LOG_ERROR, "Could not create temporary hmm filename");
            retcode = FAILURE;
            goto cleanup_and_return;
        }
        sprintf(cmdbuf, "hmmbuild %s %s >/dev/null && hmmconvert -2 %s > %s",
                tmp_hmm, tmp_aln, tmp_hmm, pcHMMOut);
        if (system(cmdbuf)) {
            Log(&rLog, LOG_ERROR, "Command '%s' failed", cmdbuf);
            retcode = FAILURE;
            goto cleanup_and_return;
        }
    } else {
        CKFREE(tmp_aln);
        Log(&rLog, LOG_FATAL, "Internal error: Unknown Hmmer version %d", iHmmerVersion);
    }
 cleanup_and_return:
    if (NULL != tmp_aln) {
        if (FileExists(tmp_aln)) {
            if (remove(tmp_aln)) {
                Log(&rLog, LOG_WARN, "Removing %s failed. Continuing anyway", tmp_aln);
            }
        }
        CKFREE(tmp_aln);
    }
    if (NULL != tmp_hmm) {
        if (FileExists(tmp_hmm)) {
            if (remove(tmp_hmm)) {
                Log(&rLog, LOG_WARN, "Removing %s failed. Continuing anyway", tmp_hmm);
            }
        }
        CKFREE(tmp_hmm);
    }
    return retcode;
}
int AlnToHMMFile ( mseq_t * prMSeq , const char * pcHMMOut ) { char * tmp_aln = NULL ; char * tmp_hmm = NULL ; char cmdbuf [ 16384 ] ; int iHmmerVersion = 0 ; int retcode = OK ; assert ( NULL != prMSeq ) ; assert ( NULL != pcHMMOut ) ; if ( FALSE == prMSeq -> aligned ) { Log ( & rLog , LOG_ERROR , " " ) ; return FAILURE ; } iHmmerVersion = HmmerVersion ( ) ; if ( 2 != iHmmerVersion && 3 != iHmmerVersion ) { Log ( & rLog , LOG_ERROR , " " ) ; return FAILURE ; } tmp_aln = CkStrdup ( " " ) ; if ( NULL == mktemp ( tmp_aln ) ) { Log ( & rLog , LOG_ERROR , " " ) ; retcode = FAILURE ; goto cleanup_and_return ; } #define LINE_WRAP  60
 if ( WriteAlignment ( prMSeq , tmp_aln , MSAFILE_STOCKHOLM , LINE_WRAP , FALSE ) ) { Log ( & rLog , LOG_ERROR , " " , tmp_aln ) ; retcode = FAILURE ; goto cleanup_and_return ; } if ( 2 == iHmmerVersion ) { sprintf ( cmdbuf , " " , pcHMMOut , tmp_aln , pcHMMOut ) ; if ( system ( cmdbuf ) ) { Log ( & rLog , LOG_ERROR , " " , cmdbuf ) ; retcode = FAILURE ; goto cleanup_and_return ; } } else if ( 3 == iHmmerVersion ) { tmp_hmm = CkStrdup ( " " ) ; if ( NULL == mktemp ( tmp_hmm ) ) { Log ( & rLog , LOG_ERROR , " " ) ; retcode = FAILURE ; goto cleanup_and_return ; } sprintf ( cmdbuf , " " , tmp_hmm , tmp_aln , tmp_hmm , pcHMMOut ) ; if ( system ( cmdbuf ) ) { Log ( & rLog , LOG_ERROR , " " , cmdbuf ) ; retcode = FAILURE ; goto cleanup_and_return ; } } else { CKFREE ( tmp_aln ) ; Log ( & rLog , LOG_FATAL , " " , iHmmerVersion ) ; } cleanup_and_return : if ( NULL != tmp_aln ) { if ( FileExists ( tmp_aln ) ) { if ( remove ( tmp_aln ) ) { Log ( & rLog , LOG_WARN , " " , tmp_aln ) ; } } CKFREE ( tmp_aln ) ; } if ( NULL != tmp_hmm ) { if ( FileExists ( tmp_hmm ) ) { if ( remove ( tmp_hmm ) ) { Log ( & rLog , LOG_WARN , " " , tmp_hmm ) ; } } CKFREE ( tmp_hmm ) ; } return retcode ; }
@brief Create a HMM file from aligned sequences
@brief Create a HMM file from aligned sequences
/* only needed for hmmer3 to hmmer2 conversion */ /* Convert alignment to stockholm, call hmmbuild and then
     * either hmmconvert (hmmer3) or hmmcalibrate (hmmer2)
     *
     * can't be static templates, or mktemp fails (at least on os x
     * (with a bus error))
     *
     * gcc says we should use mkstemp to avoid race conditions,
     * but that returns a file descriptor, which is of no use to
     * us
     */ /* NOTE: the following won't work on windows: missing /tmp/ */ /* NOTE: the following won't work on windows: missing /tmp/ */