static void utest_ReadWrite(P7_HMM *hmm, P7_OPROFILE *om) { char *msg = "oprofile read/write unit test failure"; ESL_ALPHABET *abc = NULL; P7_OPROFILE *om2 = NULL; char tmpfile[16] = "esltmpXXXXXX"; char *mfile = NULL; char *ffile = NULL; char *pfile = NULL; char *ssifile = NULL; FILE *fp = NULL; FILE *mfp = NULL; FILE *ffp = NULL; FILE *pfp = NULL; ESL_NEWSSI *nssi = NULL; P7_HMMFILE *hfp = NULL; uint16_t fh = 0; float tolerance = 0.001; char errbuf[eslERRBUFSIZE]; /* 1. A mini version of hmmpress: save the test HMM to a file along with its associated .h3{mfpi} files */ if ( esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal(msg); if ( esl_sprintf(&mfile, "%s.h3m", tmpfile) != eslOK) esl_fatal(msg); if ( esl_sprintf(&ffile, "%s.h3f", tmpfile) != eslOK) esl_fatal(msg); if ( esl_sprintf(&pfile, "%s.h3p", tmpfile) != eslOK) esl_fatal(msg); if ( esl_sprintf(&ssifile, "%s.h3i", tmpfile) != eslOK) esl_fatal(msg); if ( esl_newssi_Open(ssifile, TRUE, &nssi) != eslOK) esl_fatal(msg); if (( mfp = fopen(mfile, "wb")) == NULL) esl_fatal(msg); if (( ffp = fopen(ffile, "wb")) == NULL) esl_fatal(msg); if (( pfp = fopen(pfile, "wb")) == NULL) esl_fatal(msg); /* the disk offsets are all 0 by construction, if there's only one * HMM in the file - but don't want to forget them, if we change the * unit test in the future to be multi HMM */ if ((om->offs[p7_MOFFSET] = ftello(mfp)) == -1) esl_fatal(msg); if ((om->offs[p7_FOFFSET] = ftello(ffp)) == -1) esl_fatal(msg); if ((om->offs[p7_POFFSET] = ftello(pfp)) == -1) esl_fatal(msg); if ( p7_hmmfile_WriteASCII(fp, -1, hmm) != eslOK) esl_fatal(msg); if ( p7_hmmfile_WriteBinary(mfp, -1, hmm) != eslOK) esl_fatal(msg); if ( p7_oprofile_Write(ffp, pfp, om) != eslOK) esl_fatal(msg); if ( esl_newssi_AddFile(nssi, tmpfile, 0, &fh) != eslOK) esl_fatal(msg); if ( esl_newssi_AddKey (nssi, hmm->name, fh, om->offs[p7_MOFFSET], 0, 0) != eslOK) esl_fatal(msg); if ( esl_newssi_Write(nssi) != eslOK) esl_fatal(msg); fclose(fp); fclose(mfp); fclose(ffp); fclose(pfp); esl_newssi_Close(nssi); /* 2. read the optimized profile back in */ if ( p7_hmmfile_Open(tmpfile, NULL, &hfp) != eslOK) esl_fatal(msg); if ( p7_oprofile_ReadMSV(hfp, &abc, &om2) != eslOK) esl_fatal(msg); if ( p7_oprofile_ReadRest(hfp, om2) != eslOK) esl_fatal(msg); /* 3. it should be identical to the original */ if ( p7_oprofile_Compare(om, om2, tolerance, errbuf) != eslOK) esl_fatal("%s\n%s", msg, errbuf); p7_oprofile_Destroy(om2); p7_hmmfile_Close(hfp); esl_alphabet_Destroy(abc); remove(ssifile); remove(ffile); remove(pfile); remove(mfile); remove(tmpfile); free(ssifile); free(mfile); free(ffile); free(pfile); }
int main(int argc, char **argv) { ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage); char *hmmfile = esl_opt_GetArg(go, 1); ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO); P7_BG *bg = p7_bg_Create(abc); int my_rank; int nproc; char *buf = NULL; int nbuf = 0; int subtotalM = 0; int allM = 0; int stalling = esl_opt_GetBoolean(go, "--stall"); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &nproc); while (stalling); /* Master MPI process: */ if (my_rank == 0) { ESL_STOPWATCH *w = esl_stopwatch_Create(); P7_HMMFILE *hfp = NULL; P7_OPROFILE *om = NULL; P7_HMM *hmm = NULL; /* Read HMMs from a file. */ if (p7_hmmfile_OpenE(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); esl_stopwatch_Start(w); while (p7_oprofile_ReadMSV(hfp, &abc, &om) == eslOK && p7_oprofile_ReadRest(hfp, om) == eslOK) { if (!esl_opt_GetBoolean(go, "-b")) p7_oprofile_MPISend(om, 1, 0, MPI_COMM_WORLD, &buf, &nbuf); /* 1 = dest; 0 = tag */ p7_hmm_Destroy(hmm); p7_oprofile_Destroy(om); } p7_oprofile_MPISend(NULL, 1, 0, MPI_COMM_WORLD, &buf, &nbuf); /* send the "no more HMMs" sign */ MPI_Reduce(&subtotalM, &allM, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); printf("total: %d\n", allM); esl_stopwatch_Stop(w); esl_stopwatch_Display(stdout, w, "CPU Time: "); esl_stopwatch_Destroy(w); } /* Worker MPI process: */ else { P7_OPROFILE *om_recd = NULL; while (p7_oprofile_MPIRecv(0, 0, MPI_COMM_WORLD, &buf, &nbuf, &abc, &om_recd) == eslOK) { subtotalM += om_recd->M; p7_oprofile_Destroy(om_recd); } MPI_Reduce(&subtotalM, &allM, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); } free(buf); p7_bg_Destroy(bg); esl_alphabet_Destroy(abc); esl_getopts_Destroy(go); MPI_Finalize(); exit(0); }
/* Function: p7_hmmcache_Open() * Synopsis: Cache a profile database. * * Purpose: Open <hmmfile> and read all of its contents, creating * a cached profile database in memory. Return a ptr to the * cached profile database in <*ret_cache>. * * Caller may optionally provide an <errbuf> ptr to * at least <eslERRBUFSIZE> bytes, to capture an * informative error message on failure. * * Args: hmmfile - (base) name of profile file to open * ret_cache - RETURN: cached profile database * errbuf - optRETURN: error message for a failure * * Returns: <eslOK> on success. <*ret_cache> points to the * cached db. <errbuf> is unchanged. * * Failure codes: * <eslENOTFOUND> : <hmmfile> couldn't be opened for reading * <eslEFORMAT> : <hmmfile> isn't in recognized HMMER file format * <eslEINCOMPAT> : profiles in <hmmfile> have different alphabets * * On any failure, <*ret_cache> is <NULL> and <errbuf> contains * an informative error message for the user. * * Throws: <eslEMEM> : memory allocation error. */ int p7_hmmcache_Open(char *hmmfile, P7_HMMCACHE **ret_cache, char *errbuf) { P7_HMMCACHE *cache = NULL; P7_HMMFILE *hfp = NULL; P7_HMM *hmm = NULL; P7_BG *bg = NULL; P7_PROFILE *gm = NULL; P7_OPROFILE *om = NULL; int status; if (errbuf) errbuf[0] = '\0'; ESL_ALLOC(cache, sizeof(P7_HMMCACHE)); cache->name = NULL; cache->abc = NULL; cache->omlist = NULL; cache->gmlist = NULL; cache->lalloc = 4096; /* allocation chunk size for <list> of ptrs */ cache->n = 0; if ( ( status = esl_strdup(hmmfile, -1, &cache->name) != eslOK)) goto ERROR; ESL_ALLOC(cache->omlist, sizeof(P7_OPROFILE *) * cache->lalloc); ESL_ALLOC(cache->gmlist, sizeof(P7_PROFILE *) * cache->lalloc); if ( (status = p7_hmmfile_OpenE(hmmfile, NULL, &hfp, errbuf)) != eslOK) goto ERROR; // eslENOTFOUND | eslEFORMAT; <errbuf> while ((status = p7_hmmfile_Read(hfp, &(cache->abc), &hmm)) != eslEOF) // eslEFORMAT | eslEINCOMPAT; <errbuf> { if (status != eslOK) ESL_XFAIL(status, errbuf, "%s", hfp->errbuf); if (!bg && (bg = p7_bg_Create(cache->abc)) == NULL) { status = eslEMEM; goto ERROR; } if ( ( gm = p7_profile_Create(hmm->M, cache->abc)) == NULL) { status = eslEMEM; goto ERROR; } if ( (status = p7_profile_Config(gm, hmm, bg)) != eslOK) goto ERROR; if ( (status = p7_oprofile_ReadMSV (hfp, &(cache->abc), &om)) != eslOK || /* eslEFORMAT: hfp->errbuf | eslEINCOMPAT | eslEOF */ (status = p7_oprofile_ReadRest(hfp, om)) != eslOK) /* eslEFORMAT: hfp->errbuf */ { if (status == eslEOF) ESL_XFAIL(eslEFORMAT, errbuf, "Premature EOF in vectorized profile files"); else goto ERROR; } ESL_DASSERT1(( strcmp(gm->name, om->name) == 0 )); if (cache->n >= cache->lalloc) { ESL_REALLOC(cache->gmlist, sizeof(P7_PROFILE *) * cache->lalloc * 2); ESL_REALLOC(cache->omlist, sizeof(P7_OPROFILE *) * cache->lalloc * 2); cache->lalloc *= 2; } cache->omlist[cache->n] = om; cache->gmlist[cache->n] = gm; cache->n++; om = NULL; gm = NULL; p7_hmm_Destroy(hmm); } //printf("\nfinal:: %d memory %" PRId64 "\n", inx, total_mem); p7_hmmfile_Close(hfp); p7_bg_Destroy(bg); *ret_cache = cache; return eslOK; ERROR: if (cache) p7_hmmcache_Close(cache); if (om) p7_oprofile_Destroy(om); if (gm) p7_profile_Destroy(gm); if (hmm) p7_hmm_Destroy(hmm); if (bg) p7_bg_Destroy(bg); if (hfp) p7_hmmfile_Close(hfp); return status; }