Debian Codegraph @

src/cmpress.c

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