Debian Codegraph @

hmmer/src/hmmpress.c

raw symbols SCIP
1 /* hmmpress: prepare an HMM database for faster hmmscan searches.
2 */
3 #include <p7_config.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "easel.h"
10 #include "esl_alphabet.h"
11 #include "esl_getopts.h"
12
13 #include "hmmer.h"
14
15 static ESL_OPTIONS options[] = {
16 /* name type default env range toggles reqs incomp help docgroup*/
17 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
18 { "-f", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "force: overwrite any previous pressed files", 0 },
19 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
20 };
21 static char usage[] = "[-options] <hmmfile>";
22 static char banner[] = "prepare an HMM database for faster hmmscan searches";
23
24 /* hmmpress creates four output files.
25 * Bundling their info into a structure streamlines creation and cleanup.
26 */
27 struct dbfiles {
28 char *mfile; // .h3m file: binary core HMMs
29 char *ffile; // .h3f file: binary vectorized profiles, MSV filter part only
30 char *pfile; // .h3p file: binary vectorized profiles, remainder (excluding MSV filter part)
31 char *ssifile; // .h3i file: SSI index for retrieval from .h3m
32
33 FILE *mfp;
34 FILE *ffp;
35 FILE *pfp;
36 ESL_NEWSSI *nssi;
37 };
38
39 static struct dbfiles *open_dbfiles (ESL_GETOPTS *go, char *basename);
40 static void close_dbfiles(struct dbfiles *dbf, int status);
41
42 int
43 main(int argc, char **argv)
44 {
45 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
46 ESL_ALPHABET *abc = NULL;
47 char *hmmfile = esl_opt_GetArg(go, 1);
48 P7_HMMFILE *hfp = NULL;
49 P7_HMM *hmm = NULL;
50 P7_BG *bg = NULL;
51 P7_PROFILE *gm = NULL;
52 P7_OPROFILE *om = NULL;
53 struct dbfiles *dbf = NULL;
54 uint16_t fh = 0;
55 int nmodel = 0;
56 int status;
57 char errbuf[eslERRBUFSIZE];
58
59 if (strcmp(hmmfile, "-") == 0) p7_Fail("Can't use - for <hmmfile> argument: can't index standard input\n");
60
61 status = p7_hmmfile_OpenNoDB(hmmfile, NULL, &hfp, errbuf);
62 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
63 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
64 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
65
66 if (hfp->do_stdin || hfp->do_gzip) p7_Fail("HMM file %s must be a normal file, not gzipped or a stdin pipe", hmmfile);
67
68 dbf = open_dbfiles(go, hmmfile); // After this, we have to close_dbfiles() before exiting with any error. Don't leave partial/corrupt files.
69
70 if (( status = esl_newssi_AddFile(dbf->nssi, hfp->fname, 0, &fh)) != eslOK) /* 0 = format code (HMMs don't have any yet) */
71 ESL_XFAIL(status, errbuf, "Failed to add HMM file %s to new SSI index\n", hfp->fname);
72
73 printf("Working... ");
74 fflush(stdout);
75
76 while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) == eslOK)
77 {
78 if (hmm->name == NULL) ESL_XFAIL(eslEINVAL, errbuf, "Every HMM must have a name to be indexed. Failed to find name of HMM #%d\n", nmodel+1);
79
80 if (nmodel == 0) { /* first time initialization, now that alphabet known */
81 bg = p7_bg_Create(abc);
82 p7_bg_SetLength(bg, 400);
83 }
84
85 nmodel++;
86
87 gm = p7_profile_Create(hmm->M, abc);
88 p7_ProfileConfig(hmm, bg, gm, 400, p7_LOCAL);
89 om = p7_oprofile_Create(gm->M, abc);
90 p7_oprofile_Convert(gm, om);
91
92 if ((om->offs[p7_MOFFSET] = ftello(dbf->mfp)) == -1) ESL_XFAIL(eslESYS, errbuf, "Failed to ftello() current disk position of HMM db file");
93 if ((om->offs[p7_FOFFSET] = ftello(dbf->ffp)) == -1) ESL_XFAIL(eslESYS, errbuf, "Failed to ftello() current disk position of MSV db file");
94 if ((om->offs[p7_POFFSET] = ftello(dbf->pfp)) == -1) ESL_XFAIL(eslESYS, errbuf, "Failed to ftello() current disk position of profile db file");
95
96 if ((status = esl_newssi_AddKey(dbf->nssi, hmm->name, fh, om->offs[p7_MOFFSET], 0, 0)) != eslOK) ESL_XFAIL(status, errbuf, "Failed to add key %s to SSI index", hmm->name);
97 if (hmm->acc) {
98 if ((status = esl_newssi_AddAlias(dbf->nssi, hmm->acc, hmm->name)) != eslOK) ESL_XFAIL(status, errbuf, "Failed to add secondary key %s to SSI index", hmm->acc);
99 }
100
101 p7_hmmfile_WriteBinary(dbf->mfp, -1, hmm);
102 p7_oprofile_Write(dbf->ffp, dbf->pfp, om);
103
104 p7_profile_Destroy(gm);
105 p7_oprofile_Destroy(om);
106 p7_hmm_Destroy(hmm);
107 }
108 if (status == eslEFORMAT) ESL_XFAIL(status, errbuf, "bad file format in HMM file %s", hmmfile);
109 else if (status == eslEINCOMPAT) ESL_XFAIL(status, errbuf, "HMM file %s contains different alphabets", hmmfile);
110 else if (status != eslEOF) ESL_XFAIL(status, errbuf, "Unexpected error in reading HMMs from %s", hmmfile);
111
112 status = esl_newssi_Write(dbf->nssi);
113 if (status == eslEDUP) ESL_XFAIL(status, errbuf, "SSI index construction failed:\n %s", dbf->nssi->errbuf);
114 else if (status == eslERANGE) ESL_XFAIL(status, errbuf, "SSI index file size exceeds maximum allowed by your filesystem");
115 else if (status == eslESYS) ESL_XFAIL(status, errbuf, "SSI index sort failed:\n %s", dbf->nssi->errbuf);
116 else if (status != eslOK) ESL_XFAIL(status, errbuf, "SSI indexing failed:\n %s", dbf->nssi->errbuf);
117
118 printf("done.\n");
119 if (dbf->nssi->nsecondary > 0)
120 printf("Pressed and indexed %d HMMs (%ld names and %ld accessions).\n", nmodel, (long) dbf->nssi->nprimary, (long) dbf->nssi->nsecondary);
121 else
122 printf("Pressed and indexed %d HMMs (%ld names).\n", nmodel, (long) dbf->nssi->nprimary);
123 printf("Models pressed into binary file: %s\n", dbf->mfile);
124 printf("SSI index for binary model file: %s\n", dbf->ssifile);
125 printf("Profiles (MSV part) pressed into: %s\n", dbf->ffile);
126 printf("Profiles (remainder) pressed into: %s\n", dbf->pfile);
127
128 close_dbfiles(dbf, eslOK);
129 p7_bg_Destroy(bg);
130 p7_hmmfile_Close(hfp);
131 esl_alphabet_Destroy(abc);
132 esl_getopts_Destroy(go);
133 exit(0);
134
135 ERROR:
136 fprintf(stderr, "%s\n", errbuf);
137 close_dbfiles(dbf, status);
138 p7_bg_Destroy(bg);
139 p7_hmmfile_Close(hfp);
140 esl_alphabet_Destroy(abc);
141 esl_getopts_Destroy(go);
142 exit(1);
143 }
144
145
146 static struct dbfiles *
147 open_dbfiles(ESL_GETOPTS *go, char *basename)
148 {
149 struct dbfiles *dbf = NULL;
150 int allow_overwrite = esl_opt_GetBoolean(go, "-f");
151 char errbuf[eslERRBUFSIZE];
152 int status;
153
154 if ( ( dbf = malloc(sizeof(struct dbfiles))) == NULL) p7_Die("malloc() failed");
155 dbf->mfile = NULL;
156 dbf->ffile = NULL;
157 dbf->pfile = NULL;
158 dbf->ssifile = NULL;
159 dbf->mfp = NULL;
160 dbf->ffp = NULL;
161 dbf->pfp = NULL;
162 dbf->nssi = NULL;
163
164 if ( (status = esl_sprintf(&(dbf->ssifile), "%s.h3i", basename)) != eslOK) ESL_XFAIL(status, errbuf, "esl_sprintf() failed");
165 if ( (status = esl_sprintf(&(dbf->mfile), "%s.h3m", basename)) != eslOK) ESL_XFAIL(status, errbuf, "esl_sprintf() failed");
166 if ( (status = esl_sprintf(&(dbf->ffile), "%s.h3f", basename)) != eslOK) ESL_XFAIL(status, errbuf, "esl_sprintf() failed");
167 if ( (status = esl_sprintf(&(dbf->pfile), "%s.h3p", basename)) != eslOK) ESL_XFAIL(status, errbuf, "esl_sprintf() failed");
168
169 if (! allow_overwrite && esl_FileExists(dbf->ssifile)) ESL_XFAIL(eslEOVERWRITE, errbuf, "SSI index file %s already exists;\nDelete old hmmpress indices first", dbf->ssifile);
170 if (! allow_overwrite && esl_FileExists(dbf->mfile)) ESL_XFAIL(eslEOVERWRITE, errbuf, "Binary HMM file %s already exists;\nDelete old hmmpress indices first", dbf->mfile);
171 if (! allow_overwrite && esl_FileExists(dbf->ffile)) ESL_XFAIL(eslEOVERWRITE, errbuf, "Binary MSV filter file %s already exists\nDelete old hmmpress indices first", dbf->ffile);
172 if (! allow_overwrite && esl_FileExists(dbf->pfile)) ESL_XFAIL(eslEOVERWRITE, errbuf, "Binary profile file %s already exists\nDelete old hmmpress indices first", dbf->pfile);
173
174 status = esl_newssi_Open(dbf->ssifile, allow_overwrite, &(dbf->nssi));
175 if (status == eslENOTFOUND) ESL_XFAIL(status, errbuf, "failed to open SSI index %s", dbf->ssifile);
176 else if (status == eslEOVERWRITE) ESL_XFAIL(status, errbuf, "SSI index file %s already exists;\nDelete old hmmpress indices first", basename);
177 else if (status != eslOK) ESL_XFAIL(status, errbuf, "failed to create a new SSI index");
178
179 if ((dbf->mfp = fopen(dbf->mfile, "wb")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "Failed to open binary HMM file %s for writing", dbf->mfile);
180 if ((dbf->ffp = fopen(dbf->ffile, "wb")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "Failed to open binary MSV filter file %s for writing", dbf->ffile);
181 if ((dbf->pfp = fopen(dbf->pfile, "wb")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "Failed to open binary profile file %s for writing", dbf->pfile);
182
183 return dbf;
184
185 ERROR:
186 fprintf(stderr, "%s\n", errbuf);
187 close_dbfiles(dbf, status);
188 exit(1);
189 }
190
191 /* If status != eslOK, then in addition to free'ing memory, also
192 * remove the four output files.
193 */
194 static void
195 close_dbfiles(struct dbfiles *dbf, int status)
196 {
197 if (dbf)
198 {
199 /* Close the output files first */
200 if (dbf->mfp) fclose(dbf->mfp);
201 if (dbf->ffp) fclose(dbf->ffp);
202 if (dbf->pfp) fclose(dbf->pfp);
203 if (dbf->nssi) esl_newssi_Close(dbf->nssi);
204
205 /* Then remove them, if status isn't OK. esl_newssi_Write() takes care of the ssifile. */
206 if (status != eslOK)
207 {
208 if (esl_FileExists(dbf->mfile)) remove(dbf->mfile);
209 if (esl_FileExists(dbf->ffile)) remove(dbf->ffile);
210 if (esl_FileExists(dbf->pfile)) remove(dbf->pfile);
211 }
212
213 /* Finally free their names, and the structure. */
214 if (dbf->mfile) free(dbf->mfile);
215 if (dbf->ffile) free(dbf->ffile);
216 if (dbf->pfile) free(dbf->pfile);
217 if (dbf->ssifile) free(dbf->ssifile);
218 free(dbf);
219 }
220
221 }
222