Debian Codegraph @

hmmer/src/hmmbuild.c

raw symbols SCIP
1 /* Profile HMM construction from a multiple sequence alignment
2 */
3 #include <p7_config.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #ifdef HMMER_MPI
10 #include "mpi.h"
11 #endif
12
13 #include "easel.h"
14 #include "esl_alphabet.h"
15 #include "esl_getopts.h"
16 #include "esl_mpi.h"
17 #include "esl_msa.h"
18 #include "esl_msafile.h"
19 #include "esl_msaweight.h"
20 #include "esl_msacluster.h"
21 #include "esl_stopwatch.h"
22 #include "esl_vectorops.h"
23
24 #ifdef HMMER_THREADS
25 #include <unistd.h>
26 #include "esl_threads.h"
27 #include "esl_workqueue.h"
28 #endif /*HMMER_THREADS*/
29
30 #include "hmmer.h"
31
32 typedef struct {
33 #ifdef HMMER_THREADS
34 ESL_WORK_QUEUE *queue;
35 #endif /*HMMER_THREADS*/
36 P7_BG *bg;
37 P7_BUILDER *bld;
38 } WORKER_INFO;
39
40 #ifdef HMMER_THREADS
41 typedef struct {
42 int nali;
43 int processed;
44 ESL_MSA *postmsa;
45 ESL_MSA *msa;
46 P7_HMM *hmm;
47 double entropy;
48 int force_single; /* FALSE by default, TRUE if esl_opt_IsUsed(go, "--singlemx") ; only matters for single sequences */
49 } WORK_ITEM;
50
51 typedef struct _pending_s {
52 int nali;
53 ESL_MSA *postmsa;
54 ESL_MSA *msa;
55 P7_HMM *hmm;
56 double entropy;
57 struct _pending_s *next;
58 } PENDING_ITEM;
59 #endif /*HMMER_THREADS*/
60
61 #define ALPHOPTS "--amino,--dna,--rna" /* Exclusive options for alphabet choice */
62 #define CONOPTS "--fast,--hand" /* Exclusive options for model construction */
63 #define EFFOPTS "--eent,--eentexp,--eclust,--eset,--enone" /* Exclusive options for effective sequence number calculation */
64 #define WGTOPTS "--wgsc,--wblosum,--wpb,--wnone,--wgiven" /* Exclusive options for relative weighting */
65
66 static ESL_OPTIONS options[] = {
67 /* name type default env range toggles reqs incomp help docgroup*/
68 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
69 { "-n", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "name the HMM <s>", 1 },
70 { "-o", eslARG_OUTFILE,FALSE, NULL, NULL, NULL, NULL, NULL, "direct summary output to file <f>, not stdout", 1 },
71 { "-O", eslARG_OUTFILE,FALSE, NULL, NULL, NULL, NULL, NULL, "resave annotated, possibly modified MSA to file <f>", 1 },
72
73 /* Selecting the alphabet rather than autoguessing it */
74 { "--amino", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "input alignment is protein sequence data", 2 },
75 { "--dna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "input alignment is DNA sequence data", 2 },
76 { "--rna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "input alignment is RNA sequence data", 2 },
77
78 /* Alternate model construction strategies */
79 { "--fast", eslARG_NONE,"default",NULL, NULL, CONOPTS, NULL, NULL, "assign cols w/ >= symfrac residues as consensus", 3 },
80 { "--hand", eslARG_NONE, FALSE, NULL, NULL, CONOPTS, NULL, NULL, "manual construction (requires reference annotation)", 3 },
81 { "--symfrac", eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL, "--fast", NULL, "sets sym fraction controlling --fast construction", 3 },
82 { "--fragthresh",eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL, NULL, NULL, "if L <= x*alen, tag sequence as a fragment", 3 },
83
84 /* Alternate relative sequence weighting strategies */
85 /* --wme not implemented in HMMER3 yet */
86 { "--wpb", eslARG_NONE,"default",NULL, NULL, WGTOPTS, NULL, NULL, "Henikoff position-based weights", 4 },
87 { "--wgsc", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "Gerstein/Sonnhammer/Chothia tree weights", 4 },
88 { "--wblosum", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "Henikoff simple filter weights", 4 },
89 { "--wnone", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "don't do any relative weighting; set all to 1", 4 },
90 { "--wgiven", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "use weights as given in MSA file", 4 },
91 { "--wid", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"--wblosum", NULL, "for --wblosum: set identity cutoff", 4 },
92
93 /* Alternative effective sequence weighting strategies */
94 { "--eent", eslARG_NONE,"default",NULL, NULL, EFFOPTS, NULL, NULL, "adjust eff seq # to achieve relative entropy target", 5 },
95 { "--eclust", eslARG_NONE, FALSE, NULL, NULL, EFFOPTS, NULL, NULL, "eff seq # is # of single linkage clusters", 5 },
96 { "--enone", eslARG_NONE, FALSE, NULL, NULL, EFFOPTS, NULL, NULL, "no effective seq # weighting: just use nseq", 5 },
97 { "--eset", eslARG_REAL, NULL, NULL, NULL, EFFOPTS, NULL, NULL, "set eff seq # for all models to <x>", 5 },
98 { "--ere", eslARG_REAL, NULL, NULL,"x>0", NULL, NULL, NULL, "for --eent: set minimum rel entropy/position to <x>", 5 },
99 { "--esigma", eslARG_REAL, "45.0", NULL,"x>0", NULL, NULL, NULL, "for --eent: set sigma param to <x>", 5 },
100 { "--eid", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"--eclust", NULL, "for --eclust: set fractional identity cutoff to <x>", 5 },
101
102 /* Alternative prior strategies */
103 { "--pnone", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"--plaplace", "don't use any prior; parameters are frequencies", 9 },
104 { "--plaplace",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--pnone", "use a Laplace +1 prior", 9 },
105
106 /* Single sequence methods */
107 { "--singlemx", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "", "use substitution score matrix for single-sequence inputs", 10 },
108 { "--mx", eslARG_STRING, "BLOSUM62", NULL, NULL, NULL, NULL, "--mxfile", "substitution score matrix (built-in matrices, with --singlemx)", 10 },
109 { "--mxfile", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--mx", "read substitution score matrix from file <f> (with --singlemx)", 10 },
110 { "--popen", eslARG_REAL, NULL, NULL,"0<=x<0.5",NULL, NULL, "", "force gap open prob. (w/ --singlemx, aa default 0.02, nt 0.031)", 10 },
111 { "--pextend", eslARG_REAL, NULL, NULL, "0<=x<1", NULL, NULL, "", "force gap extend prob. (w/ --singlemx, aa default 0.4, nt 0.75)", 10 },
112
113 /* Control of E-value calibration */
114 { "--EmL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for MSV Gumbel mu fit", 6 },
115 { "--EmN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for MSV Gumbel mu fit", 6 },
116 { "--EvL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Viterbi Gumbel mu fit", 6 },
117 { "--EvN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Viterbi Gumbel mu fit", 6 },
118 { "--EfL", eslARG_INT, "100", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Forward exp tail tau fit", 6 },
119 { "--EfN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Forward exp tail tau fit", 6 },
120 { "--Eft", eslARG_REAL, "0.04", NULL,"0<x<1", NULL, NULL, NULL, "tail mass for Forward exponential tail tau fit", 6 },
121
122 /* Other options */
123 #ifdef HMMER_THREADS
124 { "--cpu", eslARG_INT, p7_NCPU,"HMMER_NCPU","n>=0",NULL, NULL, NULL, "number of parallel CPU workers for multithreads", 8 },
125 #endif
126 #ifdef HMMER_MPI
127 { "--mpi", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "run as an MPI parallel program", 8 },
128 #endif
129 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "arrest after start: for attaching debugger to process", 8 },
130 { "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert input alifile is in format <s> (no autodetect)", 8 },
131 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 8 },
132 { "--w_beta", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "tail mass at which window length is determined", 8 },
133 { "--w_length", eslARG_INT, NULL, NULL, NULL, NULL, NULL, NULL, "window length ", 8 },
134 { "--maxinsertlen", eslARG_INT, NULL, NULL, "n>=5", NULL, NULL, NULL, "pretend all inserts are length <= <n>", 8 },
135
136 /*Expert-only option, hidden from view. Likely to be removed in the future.
137 This is an experimental alternative method for weighting sequence counts.
138 It produces non-uniform scaling across columns -- columns with higher
139 counts are reduced to a greater extent than columns with low counts. It
140 has proven useful in reducing alignment overextension with Dfam models -
141 these are often based on MSAs reconstructed from heavily-fragmented
142 sequences, and thus may have wildly different per-column coverage levels.
143 Uniform scaling causes low-coverage regions to be scaled down to
144 essentially no (weighted) observations, leading to low local relative
145 entropy, which produces high overextension.
146
147 However, we don't recommend using this method in general, as it
148 may show odd behavior (or fail to even work) in the case of low
149 overall observed counts (e.g. an alignemnt of 2 or 4 sequences)
150 */
151 { "--eentexp", eslARG_NONE,"default",NULL, NULL, EFFOPTS, NULL, NULL, "adjust eff seq # to reach rel. ent. target using exp scaling", 99 },
152 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
153 };
154
155
156 /* struct cfg_s : "Global" application configuration shared by all threads/processes
157 *
158 * This structure is passed to routines within main.c, as a means of semi-encapsulation
159 * of shared data amongst different parallel processes (threads or MPI processes).
160 */
161 struct cfg_s {
162 FILE *ofp; /* output file (default is stdout) */
163
164 char *alifile; /* name of the alignment file we're building HMMs from */
165 int fmt; /* format code for alifile */
166 ESL_MSAFILE *afp; /* open alifile */
167 ESL_ALPHABET *abc; /* digital alphabet */
168
169 char *hmmName; /* hmm file name supplied from -n */
170 char *hmmfile; /* file to write HMM to */
171 FILE *hmmfp; /* HMM output file handle */
172
173 char *postmsafile; /* optional file to resave annotated, modified MSAs to */
174 FILE *postmsafp; /* open <postmsafile>, or NULL */
175
176 int nali; /* which # alignment this is in file (only valid in serial mode) */
177 int nnamed; /* number of alignments that had their own names */
178
179 int do_mpi; /* TRUE if we're doing MPI parallelization */
180 int nproc; /* how many MPI processes, total */
181 int my_rank; /* who am I, in 0..nproc-1 */
182 int do_stall; /* TRUE to stall the program until gdb attaches */
183 };
184
185
186 static char usage[] = "[-options] <hmmfile_out> <msafile>";
187 static char banner[] = "profile HMM construction from multiple sequence alignments";
188
189 static int usual_master(const ESL_GETOPTS *go, struct cfg_s *cfg);
190 static void serial_loop (WORKER_INFO *info, struct cfg_s *cfg, const ESL_GETOPTS *go);
191 #ifdef HMMER_THREADS
192 static void thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, struct cfg_s *cfg, const ESL_GETOPTS *go);
193 static void pipeline_thread(void *arg);
194 #endif /*HMMER_THREADS*/
195
196 #ifdef HMMER_MPI
197 static void mpi_master (const ESL_GETOPTS *go, struct cfg_s *cfg);
198 static void mpi_worker (const ESL_GETOPTS *go, struct cfg_s *cfg);
199 static void mpi_init_open_failure(ESL_MSAFILE *afp, int status);
200 static void mpi_init_other_failure(char *format, ...);
201 #endif
202
203 static int output_header(const ESL_GETOPTS *go, const struct cfg_s *cfg);
204 static int output_result(const struct cfg_s *cfg, char *errbuf, int msaidx, ESL_MSA *msa, P7_HMM *hmm, ESL_MSA *postmsa, double entropy);
205 static int set_msa_name ( struct cfg_s *cfg, char *errbuf, ESL_MSA *msa);
206
207
208 static int
209 process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go, char **ret_hmmfile, char **ret_alifile)
210 {
211 ESL_GETOPTS *go = esl_getopts_Create(options);
212 int status;
213
214 if (esl_opt_ProcessEnvironment(go) != eslOK) { if (printf("Failed to process environment:\n%s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
215 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) { if (printf("Failed to parse command line:\n%s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
216 if (esl_opt_VerifyConfig(go) != eslOK) { if (printf("Failed to parse command line:\n%s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
217
218 /* help format: */
219 if (esl_opt_GetBoolean(go, "-h") == TRUE)
220 {
221 p7_banner(stdout, argv[0], banner);
222 esl_usage(stdout, argv[0], usage);
223
224 if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
225 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
226
227 if (puts("\nOptions for selecting alphabet rather than guessing it:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
228 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
229
230 if (puts("\nAlternative model construction strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
231 esl_opt_DisplayHelp(stdout, go, 3, 2, 80);
232
233 if (puts("\nAlternative relative sequence weighting strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
234 esl_opt_DisplayHelp(stdout, go, 4, 2, 80);
235
236 if (puts("\nAlternative effective sequence weighting strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
237 esl_opt_DisplayHelp(stdout, go, 5, 2, 80);
238
239 if (puts("\nAlternative prior strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
240 esl_opt_DisplayHelp(stdout, go, 9, 2, 80);
241
242 if (puts("\nHandling single sequence inputs:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
243 esl_opt_DisplayHelp(stdout, go, 10, 2, 80);
244
245
246 if (puts("\nControl of E-value calibration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
247 esl_opt_DisplayHelp(stdout, go, 6, 2, 80);
248
249 if (puts("\nOther options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
250 esl_opt_DisplayHelp(stdout, go, 8, 2, 80);
251 exit(0);
252 }
253
254 if (esl_opt_ArgNumber(go) != 2) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
255 if ((*ret_hmmfile = esl_opt_GetArg(go, 1)) == NULL) { if (puts("Failed to get <hmmfile_out> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
256 if ((*ret_alifile = esl_opt_GetArg(go, 2)) == NULL) { if (puts("Failed to get <msafile> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
257
258 if (strcmp(*ret_hmmfile, "-") == 0)
259 { if (puts("Can't write <hmmfile_out> to stdout: don't use '-'") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
260 if (strcmp(*ret_alifile, "-") == 0 && ! esl_opt_IsOn(go, "--informat"))
261 { if (puts("Must specify --informat to read <alifile> from stdin ('-')") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
262
263 #ifdef HMMER_MPI
264 if (esl_opt_IsOn(go, "--mpi") && esl_opt_IsOn(go, "--cpu"))
265 {
266 int mpisetby = esl_opt_GetSetter(go, "--mpi");
267 int cpusetby = esl_opt_GetSetter(go, "--cpu");
268
269 if (mpisetby == cpusetby) {
270 if (puts("Options --cpu and --mpi are incompatible. The MPI implementation is not multithreaded.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
271 goto FAILURE;
272 }
273 }
274 #endif
275
276 *ret_go = go;
277 return eslOK;
278
279 FAILURE: /* all errors handled here are user errors, so be polite. */
280 esl_usage(stdout, argv[0], usage);
281 puts("\nwhere basic options are:");
282 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
283 printf("\nTo see more help on other available options, do:\n %s -h\n\n", argv[0]);
284 esl_getopts_Destroy(go);
285 exit(1);
286
287 ERROR:
288 if (go) esl_getopts_Destroy(go);
289 exit(status);
290 }
291
292 static int
293 output_header(const ESL_GETOPTS *go, const struct cfg_s *cfg)
294 {
295 if (cfg->my_rank > 0) return eslOK;
296
297 p7_banner(cfg->ofp, go->argv[0], banner);
298
299 if (fprintf(cfg->ofp, "# input alignment file: %s\n", cfg->alifile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
300 if (fprintf(cfg->ofp, "# output HMM file: %s\n", cfg->hmmfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
301
302 if (esl_opt_IsUsed(go, "-n") && fprintf(cfg->ofp, "# name (the single) HMM: %s\n", esl_opt_GetString(go, "-n")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
303 if (esl_opt_IsUsed(go, "-o") && fprintf(cfg->ofp, "# output directed to file: %s\n", esl_opt_GetString(go, "-o")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
304 if (esl_opt_IsUsed(go, "-O") && fprintf(cfg->ofp, "# processed alignment resaved to: %s\n", esl_opt_GetString(go, "-O")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
305 if (esl_opt_IsUsed(go, "--amino") && fprintf(cfg->ofp, "# input alignment is asserted as: protein\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
306 if (esl_opt_IsUsed(go, "--dna") && fprintf(cfg->ofp, "# input alignment is asserted as: DNA\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
307 if (esl_opt_IsUsed(go, "--rna") && fprintf(cfg->ofp, "# input alignment is asserted as: RNA\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
308 if (esl_opt_IsUsed(go, "--fast") && fprintf(cfg->ofp, "# model architecture construction: fast/heuristic\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
309 if (esl_opt_IsUsed(go, "--hand") && fprintf(cfg->ofp, "# model architecture construction: hand-specified by RF annotation\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
310 if (esl_opt_IsUsed(go, "--symfrac") && fprintf(cfg->ofp, "# sym fraction for model structure: %.3f\n", esl_opt_GetReal(go, "--symfrac")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
311 if (esl_opt_IsUsed(go, "--fragthresh") && fprintf(cfg->ofp, "# seq called frag if L <= x*alen: %.3f\n", esl_opt_GetReal(go, "--fragthresh")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
312 if (esl_opt_IsUsed(go, "--wpb") && fprintf(cfg->ofp, "# relative weighting scheme: Henikoff PB\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
313 if (esl_opt_IsUsed(go, "--wgsc") && fprintf(cfg->ofp, "# relative weighting scheme: G/S/C\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
314 if (esl_opt_IsUsed(go, "--wblosum") && fprintf(cfg->ofp, "# relative weighting scheme: BLOSUM filter\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
315 if (esl_opt_IsUsed(go, "--wnone") && fprintf(cfg->ofp, "# relative weighting scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
316 if (esl_opt_IsUsed(go, "--wid") && fprintf(cfg->ofp, "# frac id cutoff for BLOSUM wgts: %f\n", esl_opt_GetReal(go, "--wid")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
317 if (esl_opt_IsUsed(go, "--eent") && fprintf(cfg->ofp, "# effective seq number scheme: entropy weighting\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
318 if (esl_opt_IsUsed(go, "--eentexp") && fprintf(cfg->ofp, "# effective seq number scheme: entropy weighting using exponent-based scaling\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
319 if (esl_opt_IsUsed(go, "--eclust") && fprintf(cfg->ofp, "# effective seq number scheme: single linkage clusters\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
320 if (esl_opt_IsUsed(go, "--enone") && fprintf(cfg->ofp, "# effective seq number scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
321 if (esl_opt_IsUsed(go, "--eset") && fprintf(cfg->ofp, "# effective seq number: set to %f\n", esl_opt_GetReal(go, "--eset")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
322 if (esl_opt_IsUsed(go, "--ere") && fprintf(cfg->ofp, "# minimum rel entropy target: %f bits\n", esl_opt_GetReal(go, "--ere")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
323 if (esl_opt_IsUsed(go, "--esigma") && fprintf(cfg->ofp, "# entropy target sigma parameter: %f bits\n", esl_opt_GetReal(go, "--esigma")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
324 if (esl_opt_IsUsed(go, "--eid") && fprintf(cfg->ofp, "# frac id cutoff for --eclust: %f\n", esl_opt_GetReal(go, "--eid")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
325 if (esl_opt_IsUsed(go, "--pnone") && fprintf(cfg->ofp, "# prior scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
326 if (esl_opt_IsUsed(go, "--plaplace") && fprintf(cfg->ofp, "# prior scheme: Laplace +1\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
327 if (esl_opt_IsUsed(go, "--EmL") && fprintf(cfg->ofp, "# seq length for MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EmL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
328 if (esl_opt_IsUsed(go, "--EmN") && fprintf(cfg->ofp, "# seq number for MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EmN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
329 if (esl_opt_IsUsed(go, "--EvL") && fprintf(cfg->ofp, "# seq length for Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EvL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
330 if (esl_opt_IsUsed(go, "--EvN") && fprintf(cfg->ofp, "# seq number for Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EvN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
331 if (esl_opt_IsUsed(go, "--EfL") && fprintf(cfg->ofp, "# seq length for Fwd exp tau fit: %d\n", esl_opt_GetInteger(go, "--EfL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
332 if (esl_opt_IsUsed(go, "--EfN") && fprintf(cfg->ofp, "# seq number for Fwd exp tau fit: %d\n", esl_opt_GetInteger(go, "--EfN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
333 if (esl_opt_IsUsed(go, "--Eft") && fprintf(cfg->ofp, "# tail mass for Fwd exp tau fit: %f\n", esl_opt_GetReal(go, "--Eft")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
334 if (esl_opt_IsUsed(go, "--singlemx") && fprintf(cfg->ofp, "# use score matrix for 1-seq MSAs: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
335 if (esl_opt_IsUsed(go, "--popen") && fprintf(cfg->ofp, "# gap open probability: %f\n", esl_opt_GetReal (go, "--popen")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
336 if (esl_opt_IsUsed(go, "--pextend") && fprintf(cfg->ofp, "# gap extend probability: %f\n", esl_opt_GetReal (go, "--pextend")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
337 if (esl_opt_IsUsed(go, "--mx") && fprintf(cfg->ofp, "# subst score matrix (built-in): %s\n", esl_opt_GetString (go, "--mx")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
338 if (esl_opt_IsUsed(go, "--mxfile") && fprintf(cfg->ofp, "# subst score matrix (file): %s\n", esl_opt_GetString (go, "--mxfile")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
339 if (esl_opt_IsUsed(go, "--maxinsertlen") && fprintf(cfg->ofp, "# max insert length: %d\n", esl_opt_GetInteger (go, "--maxinsertlen")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
340
341
342 #ifdef HMMER_THREADS
343 if (esl_opt_IsUsed(go, "--cpu") && fprintf(cfg->ofp, "# number of worker threads: %d\n", esl_opt_GetInteger(go, "--cpu")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
344 #endif
345 #ifdef HMMER_MPI
346 if (esl_opt_IsUsed(go, "--mpi") && fprintf(cfg->ofp, "# parallelization mode: MPI\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
347 #endif
348 if (esl_opt_IsUsed(go, "--seed")) {
349 if (esl_opt_GetInteger(go, "--seed") == 0 && fprintf(cfg->ofp,"# random number seed: one-time arbitrary\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
350 else if ( fprintf(cfg->ofp,"# random number seed set to: %d\n", esl_opt_GetInteger(go, "--seed")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
351 }
352 if (esl_opt_IsUsed(go, "--w_beta") && fprintf(cfg->ofp, "# window length tail mass: %g bits\n", esl_opt_GetReal(go, "--w_beta")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
353 if (esl_opt_IsUsed(go, "--w_length") && fprintf(cfg->ofp, "# window length : %d\n", esl_opt_GetInteger(go, "--w_length"))< 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
354
355
356 if (fprintf(cfg->ofp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
357 return eslOK;
358 }
359
360 void
361 apply_fixed_gap_params(P7_HMM *hmm, double popen, double pextend){
362 int k;
363 for (k = 0; k <= hmm->M; k++)
364 {
365 if (popen != -1) {
366 hmm->t[k][p7H_MM] = 1.0 - 2 * popen;
367 hmm->t[k][p7H_MI] = popen;
368 hmm->t[k][p7H_MD] = popen;
369 }
370 if (pextend != -1) {
371 hmm->t[k][p7H_IM] = 1.0 - pextend;
372 hmm->t[k][p7H_II] = pextend;
373 hmm->t[k][p7H_DM] = 1.0 - pextend;
374 hmm->t[k][p7H_DD] = pextend;
375 }
376 }
377
378 /* Deal w/ special stuff at node M, overwriting a little of what we
379 * just did.
380 */
381 if (popen != -1) {
382 hmm->t[hmm->M][p7H_MM] = 1.0 - popen;
383 }
384 hmm->t[hmm->M][p7H_MD] = 0.;
385 hmm->t[hmm->M][p7H_DM] = 1.0;
386 hmm->t[hmm->M][p7H_DD] = 0.;
387
388 }
389
390 int
391 main(int argc, char **argv)
392 {
393 ESL_GETOPTS *go = NULL; /* command line processing */
394 ESL_STOPWATCH *w = esl_stopwatch_Create();
395 struct cfg_s cfg;
396
397 /* Set processor specific flags */
398 impl_Init();
399
400 cfg.alifile = NULL;
401 cfg.hmmfile = NULL;
402
403 /* Parse the command line
404 */
405 process_commandline(argc, argv, &go, &cfg.hmmfile, &cfg.alifile);
406
407 /* Initialize what we can in the config structure (without knowing the alphabet yet).
408 * Fields controlled by masters are set up in usual_master() or mpi_master()
409 * Fields used by workers are set up in mpi_worker()
410 */
411 cfg.ofp = NULL;
412 cfg.fmt = eslMSAFILE_UNKNOWN; /* autodetect alignment format by default. */
413 cfg.afp = NULL;
414 cfg.abc = NULL;
415 cfg.hmmfp = NULL;
416 cfg.postmsafile = esl_opt_GetString(go, "-O"); /* NULL by default */
417 cfg.postmsafp = NULL;
418
419 cfg.nali = 0; /* this counter is incremented in masters */
420 cfg.nnamed = 0; /* 0 or 1 if a single MSA; == nali if multiple MSAs */
421 cfg.do_mpi = FALSE; /* this gets reset below, if we init MPI */
422 cfg.nproc = 0; /* this gets reset below, if we init MPI */
423 cfg.my_rank = 0; /* this gets reset below, if we init MPI */
424 cfg.do_stall = esl_opt_GetBoolean(go, "--stall");
425 cfg.hmmName = esl_opt_GetString(go, "-n"); /* NULL by default */
426
427 if (esl_opt_IsOn(go, "--informat")) {
428 cfg.fmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"));
429 if (cfg.fmt == eslMSAFILE_UNKNOWN) p7_Fail("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--informat"));
430 }
431
432
433 /* This is our stall point, if we need to wait until we get a
434 * debugger attached to this process for debugging (especially
435 * useful for MPI):
436 */
437 while (cfg.do_stall);
438
439 /* Start timing. */
440 esl_stopwatch_Start(w);
441
442 /* Figure out who we are, and send control there:
443 * we might be an MPI master, an MPI worker, or a serial program.
444 */
445 #ifdef HMMER_MPI
446 if (esl_opt_GetBoolean(go, "--mpi"))
447 {
448 cfg.do_mpi = TRUE;
449 MPI_Init(&argc, &argv);
450 MPI_Comm_rank(MPI_COMM_WORLD, &(cfg.my_rank));
451 MPI_Comm_size(MPI_COMM_WORLD, &(cfg.nproc));
452
453 if (cfg.my_rank > 0) mpi_worker(go, &cfg);
454 else mpi_master(go, &cfg);
455
456 esl_stopwatch_Stop(w);
457 esl_stopwatch_MPIReduce(w, 0, MPI_COMM_WORLD);
458 MPI_Finalize();
459 }
460 else
461 #endif /*HMMER_MPI*/
462 {
463 usual_master(go, &cfg);
464 esl_stopwatch_Stop(w);
465 }
466
467 if (cfg.my_rank == 0) {
468 fputc('\n', cfg.ofp);
469 esl_stopwatch_Display(cfg.ofp, w, "# CPU time: ");
470 }
471
472 /* Clean up the shared cfg.
473 */
474 if (cfg.my_rank == 0) {
475 if (esl_opt_IsOn(go, "-o")) { fclose(cfg.ofp); }
476 if (cfg.afp) esl_msafile_Close(cfg.afp);
477 if (cfg.abc) esl_alphabet_Destroy(cfg.abc);
478 if (cfg.hmmfp) fclose(cfg.hmmfp);
479 }
480 esl_getopts_Destroy(go);
481 esl_stopwatch_Destroy(w);
482 return 0;
483 }
484
485
486 /* usual_master()
487 * The usual version of hmmbuild, serial or threaded
488 * For each MSA, build an HMM and save it.
489 *
490 * A master can only return if it's successful.
491 * All errors are handled immediately and fatally with p7_Fail() or equiv.
492 */
493 static int
494 usual_master(const ESL_GETOPTS *go, struct cfg_s *cfg)
495 {
496 int ncpus = 0;
497 int infocnt = 0;
498 WORKER_INFO *info = NULL;
499 #ifdef HMMER_THREADS
500 WORK_ITEM *item = NULL;
501 ESL_THREADS *threadObj= NULL;
502 ESL_WORK_QUEUE *queue = NULL;
503 #endif
504 double popen;
505 double pextend;
506 int i;
507 int status;
508
509 /* Open files, set alphabet.
510 * cfg->afp - open alignment file for input
511 * cfg->abc - alphabet expected or guessed in ali file
512 * cfg->hmmfp - open HMM file for output
513 * cfg->postmsafp - optional open MSA resave file, or NULL
514 * cfp->ofp - optional open output file, or stdout
515 * The mpi_master's version of this is in init_master_cfg(), with
516 * different error handling (necessitated by our MPI design).
517 */
518 if (esl_opt_GetBoolean(go, "--amino")) cfg->abc = esl_alphabet_Create(eslAMINO);
519 else if (esl_opt_GetBoolean(go, "--dna")) cfg->abc = esl_alphabet_Create(eslDNA);
520 else if (esl_opt_GetBoolean(go, "--rna")) cfg->abc = esl_alphabet_Create(eslRNA);
521 else cfg->abc = NULL;
522
523 status = esl_msafile_Open(&(cfg->abc), cfg->alifile, NULL, cfg->fmt, NULL, &(cfg->afp));
524 if (status != eslOK) esl_msafile_OpenFailure(cfg->afp, status);
525
526 cfg->hmmfp = fopen(cfg->hmmfile, "w");
527 if (cfg->hmmfp == NULL) p7_Fail("Failed to open HMM file %s for writing", cfg->hmmfile);
528
529 if (esl_opt_IsUsed(go, "-o"))
530 {
531 cfg->ofp = fopen(esl_opt_GetString(go, "-o"), "w");
532 if (cfg->ofp == NULL) p7_Fail("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o"));
533 }
534 else cfg->ofp = stdout;
535
536 if (cfg->postmsafile)
537 {
538 cfg->postmsafp = fopen(cfg->postmsafile, "w");
539 if (cfg->postmsafp == NULL) p7_Fail("Failed to resave MSA file %s", cfg->postmsafile);
540 }
541 else cfg->postmsafp = NULL;
542
543
544 /* Looks like the i/o is set up successfully...
545 * Initial output to the user
546 */
547 output_header(go, cfg); /* cheery output header */
548 output_result(cfg, NULL, 0, NULL, NULL, NULL, 0.0); /* tabular results header (with no args, special-case) */
549
550 #ifdef HMMER_THREADS
551 /* initialize thread data */
552 ncpus = ESL_MIN(esl_opt_GetInteger(go, "--cpu"), esl_threads_GetCPUCount());
553 if (ncpus > 0)
554 {
555 threadObj = esl_threads_Create(&pipeline_thread);
556 queue = esl_workqueue_Create(ncpus * 2);
557 }
558 #endif
559
560 infocnt = (ncpus == 0) ? 1 : ncpus;
561 ESL_ALLOC(info, (ptrdiff_t) sizeof(*info) * infocnt);
562
563 for (i = 0; i < infocnt; ++i)
564 {
565 info[i].bg = p7_bg_Create(cfg->abc);
566 info[i].bld = p7_builder_Create(go, cfg->abc);
567
568 if (info[i].bld == NULL) p7_Fail("p7_builder_Create failed");
569
570 //do this here instead of in p7_builder_Create(), because it's an hmmbuild-specific option
571
572 if ( esl_opt_IsOn(go, "--maxinsertlen") )
573 info[i].bld->max_insert_len = esl_opt_GetInteger(go, "--maxinsertlen");
574
575 if ( cfg->abc->type == eslDNA || cfg->abc->type == eslRNA ) {
576 //If user hasn't overridden defaults, assign the nucleotide defaults
577 popen = esl_opt_IsUsed(go, "--popen") ? esl_opt_GetReal(go, "--popen") : 0.03125;
578 pextend = esl_opt_IsUsed(go, "--pextend") ? esl_opt_GetReal(go, "--pextend") : 0.75;
579 } else {
580 //protein defaults
581 popen = esl_opt_IsUsed(go, "--popen") ? esl_opt_GetReal(go, "--popen") : 0.02;
582 pextend = esl_opt_IsUsed(go, "--pextend") ? esl_opt_GetReal(go, "--pextend") : 0.4;
583 }
584
585 /* Default matrix is stored in the --mx option, so it's always IsOn().
586 * Check --mxfile first; then go to the --mx option and the default.
587 */
588 if ( esl_opt_IsUsed(go, "--singlemx") ) {
589 char *mx = esl_opt_GetString(go, "--mx");
590
591 if ( cfg->abc->type == eslDNA || cfg->abc->type == eslRNA ) {
592 //If user hasn't overridden defaults, assign the nucleotide defaults
593 if ( !esl_opt_IsUsed(go, "--mx") ) mx = "DNA1";
594 }
595 if (esl_opt_IsOn(go, "--mxfile")) status = p7_builder_SetScoreSystem (info[i].bld, esl_opt_GetString(go, "--mxfile"), NULL, popen, pextend, info[i].bg);
596 else status = p7_builder_LoadScoreSystem(info[i].bld, mx, popen, pextend, info[i].bg);
597 if (status != eslOK) p7_Fail("Failed to set single query seq score system:\n%s\n", info[i].bld->errbuf);
598 } else {
599 if (esl_opt_IsUsed(go, "--popen") ) info[i].bld->popen = popen;
600 if (esl_opt_IsUsed(go, "--pextend")) info[i].bld->pextend = pextend;
601 }
602
603 /* special arguments for hmmbuild */
604 info[i].bld->w_len = (go != NULL && esl_opt_IsOn (go, "--w_length")) ? esl_opt_GetInteger(go, "--w_length"): -1;
605 info[i].bld->w_beta = (go != NULL && esl_opt_IsOn (go, "--w_beta")) ? esl_opt_GetReal (go, "--w_beta") : p7_DEFAULT_WINDOW_BETA;
606 if ( info[i].bld->w_beta < 0 || info[i].bld->w_beta > 1 ) esl_fatal("Invalid window-length beta value\n");
607
608 #ifdef HMMER_THREADS
609 info[i].queue = queue;
610 if (ncpus > 0) esl_threads_AddThread(threadObj, &info[i]);
611 #endif
612 }
613
614 #ifdef HMMER_THREADS
615 for (i = 0; i < ncpus * 2; ++i)
616 {
617 ESL_ALLOC(item, sizeof(*item));
618
619 item->nali = 0;
620 item->processed = FALSE;
621 item->postmsa = NULL;
622 item->msa = NULL;
623 item->hmm = NULL;
624 item->entropy = 0.0;
625
626 status = esl_workqueue_Init(queue, item);
627 if (status != eslOK) esl_fatal("Failed to add block to work queue");
628 }
629 #endif
630
631 #ifdef HMMER_THREADS
632 if (ncpus > 0) thread_loop(threadObj, queue, cfg, go);
633 else serial_loop(info, cfg, go);
634 #else
635 serial_loop(info, cfg, go);
636 #endif
637
638 for (i = 0; i < infocnt; ++i)
639 {
640 p7_bg_Destroy(info[i].bg);
641 p7_builder_Destroy(info[i].bld);
642 }
643
644 #ifdef HMMER_THREADS
645 if (ncpus > 0)
646 {
647 esl_workqueue_Reset(queue);
648 while (esl_workqueue_Remove(queue, (void **) &item) == eslOK)
649 {
650 free(item);
651 }
652 esl_workqueue_Destroy(queue);
653 esl_threads_Destroy(threadObj);
654 }
655 #endif
656
657 free(info);
658 return eslOK;
659
660 ERROR:
661 return eslFAIL;
662 }
663
664 #ifdef HMMER_MPI
665 /* mpi_master()
666 * The MPI version of hmmbuild.
667 * Follows standard pattern for a master/worker load-balanced MPI program (J1/78-79).
668 *
669 * A master can only return if it's successful.
670 * Errors in an MPI master come in two classes: recoverable and nonrecoverable.
671 *
672 * Recoverable errors include all worker-side errors, and any
673 * master-side error that do not affect MPI communication. Error
674 * messages from recoverable messages are delayed until we've cleanly
675 * shut down the workers.
676 *
677 * Unrecoverable errors are master-side errors that may affect MPI
678 * communication, meaning we cannot count on being able to reach the
679 * workers and shut them down. Unrecoverable errors result in immediate
680 * p7_Fail()'s, which will cause MPI to shut down the worker processes
681 * uncleanly.
682 */
683 static void
684 mpi_master(const ESL_GETOPTS *go, struct cfg_s *cfg)
685 {
686 int have_work = TRUE; /* TRUE while alignments remain */
687 int nproc_working = 0; /* number of worker processes working, up to nproc-1 */
688 int wi; /* rank of next worker to get an alignment to work on */
689 char *buf = NULL; /* input/output buffer, for packed MPI messages */
690 int bn = 0;
691 ESL_MSA *msa = NULL;
692 P7_HMM *hmm = NULL;
693 P7_BG *bg = NULL;
694 ESL_MSA **msalist = NULL;
695 ESL_MSA *postmsa = NULL;
696 int *msaidx = NULL;
697 char errmsg[eslERRBUFSIZE];
698 int n;
699 int pos;
700 double entropy;
701 int status;
702 int xstatus = eslOK; /* changes from OK on recoverable error */
703 int rstatus; /* status specifically from msa read */
704 MPI_Status mpistatus;
705
706 /* Open files, set alphabet.
707 * cfg->abc - alphabet expected or guessed in ali file
708 * cfg->afp - open alignment file for input
709 * cfg->hmmfp - open HMM file for output
710 * cfp->ofp - optional open output file, or stdout
711 * cfg->postmsafp - optional open MSA resave file, or NULL
712 * Error handling requires first broadcasting a non-OK status to workers
713 * to get them to shut down cleanly.
714 */
715 if (esl_opt_GetBoolean(go, "--amino")) cfg->abc = esl_alphabet_Create(eslAMINO);
716 else if (esl_opt_GetBoolean(go, "--dna")) cfg->abc = esl_alphabet_Create(eslDNA);
717 else if (esl_opt_GetBoolean(go, "--rna")) cfg->abc = esl_alphabet_Create(eslRNA);
718 else cfg->abc = NULL;
719
720 status = esl_msafile_Open(&(cfg->abc), cfg->alifile, NULL, cfg->fmt, NULL, &(cfg->afp));
721 if (status != eslOK) mpi_init_open_failure(cfg->afp, status);
722
723 cfg->hmmfp = fopen(cfg->hmmfile, "w");
724 if (cfg->hmmfp == NULL) mpi_init_other_failure("Failed to open HMM file %s for writing", cfg->hmmfile);
725
726 if (esl_opt_IsUsed(go, "-o"))
727 {
728 cfg->ofp = fopen(esl_opt_GetString(go, "-o"), "w");
729 if (cfg->ofp == NULL) mpi_init_other_failure("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o"));
730 }
731 else cfg->ofp = stdout;
732
733 if (cfg->postmsafile)
734 {
735 cfg->postmsafp = fopen(cfg->postmsafile, "w");
736 if (cfg->postmsafp == NULL) mpi_init_other_failure("Failed to MSA resave file %s for writing", cfg->postmsafile);
737 }
738 else cfg->postmsafp = NULL;
739
740 /* Other initialization in the master
741 */
742 bn = 4096;
743 if ((buf = malloc(sizeof(char) * bn)) == NULL) mpi_init_other_failure("allocation failed");
744 if ((msalist = malloc(sizeof(ESL_MSA *) * cfg->nproc)) == NULL) mpi_init_other_failure("allocation failed");
745 if ((msaidx = malloc(sizeof(int) * cfg->nproc)) == NULL) mpi_init_other_failure("allocation failed");
746 if ((bg = p7_bg_Create(cfg->abc)) == NULL) mpi_init_other_failure("allocation failed");
747
748 for (wi = 0; wi < cfg->nproc; wi++) { msalist[wi] = NULL; msaidx[wi] = 0; }
749
750 /* Looks like the master is initialized successfully...
751 * Tell the workers we're fine; send initial output to the user
752 */
753 xstatus = eslOK;
754 MPI_Bcast(&xstatus, 1, MPI_INT, 0, MPI_COMM_WORLD);
755 output_header(go, cfg); /* cheery output header */
756 output_result(cfg, NULL, 0, NULL, NULL, NULL, 0.0); /* tabular results header (with no args, special-case) */
757 ESL_DPRINTF1(("MPI master is initialized\n"));
758
759 /* Worker initialization:
760 * Because we've already successfully initialized the master before we start
761 * initializing the workers, we don't expect worker initialization to fail;
762 * so we just receive a quick OK/error code reply from each worker to be sure,
763 * and don't worry about an informative message.
764 */
765 MPI_Bcast(&(cfg->abc->type), 1, MPI_INT, 0, MPI_COMM_WORLD);
766 MPI_Reduce(&xstatus, &status, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
767 if (status != eslOK) { MPI_Finalize(); p7_Fail("One or more MPI worker processes failed to initialize."); }
768 ESL_DPRINTF1(("%d workers are initialized\n", cfg->nproc-1));
769
770
771 /* Main loop: combining load workers, send/receive, clear workers loops;
772 * also, catch error states and die later, after clean shutdown of workers.
773 *
774 * When a recoverable error occurs, have_work = FALSE, xstatus !=
775 * eslOK, and errmsg is set to an informative message. No more
776 * errmsg's can be received after the first one. We wait for all the
777 * workers to clear their work units, then send them shutdown signals,
778 * then finally print our errmsg and exit.
779 *
780 * Unrecoverable errors just crash us out with p7_Fail().
781 */
782 wi = 1;
783 while (have_work || nproc_working)
784 {
785 if (have_work)
786 {
787 rstatus = esl_msafile_Read(cfg->afp, &msa);
788 if (rstatus == eslOK) { cfg->nali++; ESL_DPRINTF1(("MPI master read MSA %s\n", msa->name == NULL? "" : msa->name)); }
789 else if (rstatus == eslEOF) { have_work = FALSE; rstatus = eslOK; ESL_DPRINTF1(("MPI master has run out of MSAs (having read %d)\n", cfg->nali)); }
790 else { have_work = FALSE; xstatus = rstatus; ESL_DPRINTF1(("MPI master msa read has failed... start to shut down\n")); }
791 }
792
793 if ((have_work && nproc_working == cfg->nproc-1) || (!have_work && nproc_working > 0))
794 {
795 if (MPI_Probe(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &mpistatus) != 0) { MPI_Finalize(); p7_Fail("mpi probe failed"); }
796 if (MPI_Get_count(&mpistatus, MPI_PACKED, &n) != 0) { MPI_Finalize(); p7_Fail("mpi get count failed"); }
797 wi = mpistatus.MPI_SOURCE;
798 ESL_DPRINTF1(("MPI master sees a result of %d bytes from worker %d\n", n, wi));
799
800 if (n > bn) {
801 if ((buf = realloc(buf, sizeof(char) * n)) == NULL) p7_Fail("reallocation failed");
802 bn = n;
803 }
804 if (MPI_Recv(buf, bn, MPI_PACKED, wi, 0, MPI_COMM_WORLD, &mpistatus) != 0) { MPI_Finalize(); p7_Fail("mpi recv failed"); }
805 ESL_DPRINTF1(("MPI master has received the buffer\n"));
806
807 /* If we're in a recoverable error state, we're only clearing worker results;
808 * just receive them, don't unpack them or print them.
809 * But if our xstatus is OK, go ahead and process the result buffer.
810 */
811 if (xstatus == eslOK)
812 {
813 pos = 0;
814 if (MPI_Unpack(buf, bn, &pos, &xstatus, 1, MPI_INT, MPI_COMM_WORLD) != 0) { MPI_Finalize(); p7_Fail("mpi unpack failed");}
815 if (xstatus == eslOK) /* worker reported success. Get the HMM. */
816 {
817 ESL_DPRINTF1(("MPI master sees that the result buffer contains an HMM\n"));
818 if (p7_hmm_MPIUnpack(buf, bn, &pos, MPI_COMM_WORLD, &(cfg->abc), &hmm) != eslOK) { MPI_Finalize(); p7_Fail("HMM unpack failed"); }
819 ESL_DPRINTF1(("MPI master has unpacked the HMM\n"));
820
821 if (cfg->postmsafile != NULL) {
822 if (esl_msa_MPIUnpack(cfg->abc, buf, bn, &pos, MPI_COMM_WORLD, &postmsa) != eslOK) { MPI_Finalize(); p7_Fail("postmsa unpack failed");}
823 }
824
825 entropy = p7_MeanMatchRelativeEntropy(hmm, bg);
826 if ((status = output_result(cfg, errmsg, msaidx[wi], msalist[wi], hmm, postmsa, entropy)) != eslOK) xstatus = status;
827
828 esl_msa_Destroy(postmsa); postmsa = NULL;
829 p7_hmm_Destroy(hmm); hmm = NULL;
830 }
831 else /* worker reported an error. Get the errmsg. */
832 {
833 if (MPI_Unpack(buf, bn, &pos, errmsg, eslERRBUFSIZE, MPI_CHAR, MPI_COMM_WORLD) != 0) { MPI_Finalize(); p7_Fail("mpi unpack of errmsg failed"); }
834 ESL_DPRINTF1(("MPI master sees that the result buffer contains an error message\n"));
835 }
836 }
837 esl_msa_Destroy(msalist[wi]);
838 msalist[wi] = NULL;
839 msaidx[wi] = 0;
840 nproc_working--;
841 }
842
843 if (have_work)
844 {
845 ESL_DPRINTF1(("MPI master is sending MSA %s to worker %d\n", msa->name == NULL ? "":msa->name, wi));
846 if (esl_msa_MPISend(msa, wi, 0, MPI_COMM_WORLD, &buf, &bn) != eslOK) p7_Fail("MPI msa send failed");
847 msalist[wi] = msa;
848 msaidx[wi] = cfg->nali; /* 1..N for N alignments in the MSA database */
849 msa = NULL;
850 wi++;
851 nproc_working++;
852 }
853 }
854
855 /* On success or recoverable errors:
856 * Shut down workers cleanly.
857 */
858 ESL_DPRINTF1(("MPI master is done. Shutting down all the workers cleanly\n"));
859 for (wi = 1; wi < cfg->nproc; wi++)
860 if (esl_msa_MPISend(NULL, wi, 0, MPI_COMM_WORLD, &buf, &bn) != eslOK) p7_Fail("MPI msa send failed");
861
862 free(buf);
863 free(msaidx);
864 free(msalist);
865 p7_bg_Destroy(bg);
866
867 if (rstatus != eslOK) { MPI_Finalize(); esl_msafile_ReadFailure(cfg->afp, rstatus); }
868 else if (xstatus != eslOK) { MPI_Finalize(); p7_Fail(errmsg); }
869 else return;
870 }
871
872 static void
873 mpi_init_open_failure(ESL_MSAFILE *afp, int status)
874 {
875 MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
876 MPI_Finalize();
877 esl_msafile_OpenFailure(afp, status);
878 }
879
880 static void
881 mpi_init_other_failure(char *format, ...)
882 {
883 va_list argp;
884 int status = eslFAIL;
885
886 MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
887 MPI_Finalize();
888 if (fprintf(stderr, "\nError: ") < 0) exit(eslEWRITE);
889
890 va_start(argp, format);
891 if (vfprintf(stderr, format, argp) < 0) exit(eslEWRITE);
892 va_end(argp);
893
894 if (fprintf(stderr, "\n") < 0) exit(eslEWRITE);
895 fflush(stderr);
896 exit(1);
897 }
898
899 static void
900 mpi_worker(const ESL_GETOPTS *go, struct cfg_s *cfg)
901 {
902 int xstatus = eslOK;
903 int status;
904 int type;
905 P7_BUILDER *bld = NULL;
906 ESL_MSA *msa = NULL;
907 ESL_MSA *postmsa = NULL;
908 ESL_MSA **postmsa_ptr = (cfg->postmsafile != NULL) ? &postmsa : NULL;
909 P7_HMM *hmm = NULL;
910 P7_BG *bg = NULL;
911 char *wbuf = NULL; /* packed send/recv buffer */
912 void *tmp; /* for reallocation of wbuf */
913 int wn = 0; /* allocation size for wbuf */
914 int sz, n; /* size of a packed message */
915 int pos;
916 char errmsg[eslERRBUFSIZE];
917 ESL_SQ *sq = NULL;
918
919 /* After master initialization: master broadcasts its status.
920 */
921 MPI_Bcast(&xstatus, 1, MPI_INT, 0, MPI_COMM_WORLD);
922 if (xstatus != eslOK) return; /* master saw an error code; workers do an immediate normal shutdown. */
923 ESL_DPRINTF2(("worker %d: sees that master has initialized\n", cfg->my_rank));
924
925 /* Master now broadcasts worker initialization information (alphabet type)
926 * Workers returns their status post-initialization.
927 * Initial allocation of wbuf must be large enough to guarantee that
928 * we can pack an error result into it, because after initialization,
929 * errors will be returned as packed (code, errmsg) messages.
930 */
931 MPI_Bcast(&type, 1, MPI_INT, 0, MPI_COMM_WORLD);
932 if (xstatus == eslOK) { if ((cfg->abc = esl_alphabet_Create(type)) == NULL) xstatus = eslEMEM; }
933 if (xstatus == eslOK) { wn = 4096; if ((wbuf = malloc(wn * sizeof(char))) == NULL) xstatus = eslEMEM; }
934 if (xstatus == eslOK) { if ((bld = p7_builder_Create(go, cfg->abc)) == NULL) xstatus = eslEMEM; }
935
936 //special arguments for hmmbuild
937 bld->w_len = (go != NULL && esl_opt_IsOn (go, "--w_length")) ? esl_opt_GetInteger(go, "--w_length"): -1;
938 bld->w_beta = (go != NULL && esl_opt_IsOn (go, "--w_beta")) ? esl_opt_GetReal (go, "--w_beta") : p7_DEFAULT_WINDOW_BETA;
939 if ( bld->w_beta < 0 || bld->w_beta > 1 ) goto ERROR;
940
941
942 MPI_Reduce(&xstatus, &status, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD); /* everyone sends xstatus back to master */
943 if (xstatus != eslOK) {
944 if (wbuf != NULL) free(wbuf);
945 if (bld != NULL) p7_builder_Destroy(bld);
946 return; /* shutdown; we passed the error back for the master to deal with. */
947 }
948
949 bg = p7_bg_Create(cfg->abc);
950
951 ESL_DPRINTF2(("worker %d: initialized\n", cfg->my_rank));
952
953 /* source = 0 (master); tag = 0 */
954 while (esl_msa_MPIRecv(0, 0, MPI_COMM_WORLD, cfg->abc, &wbuf, &wn, &msa) == eslOK)
955 {
956 /* Build the HMM */
957 ESL_DPRINTF2(("worker %d: has received MSA %s (%d columns, %d seqs)\n", cfg->my_rank, msa->name, msa->alen, msa->nseq));
958
959 if ( msa->nseq == 1 && esl_opt_IsUsed(go, "--singlemx")) {
960 //for, single sequence, use blosum matrix:
961 sq = esl_sq_CreateDigital(cfg->abc);
962 if ((status = esl_sq_FetchFromMSA(msa, 0, &sq)) != eslOK) { strcpy(errmsg, bld->errbuf); goto ERROR; }
963 if ((status = p7_SingleBuilder(bld, sq, bg, &hmm, NULL, NULL, NULL)) != eslOK) { strcpy(errmsg, bld->errbuf); goto ERROR; }
964 esl_sq_Destroy(sq);
965 sq = NULL;
966 hmm->eff_nseq = 1;
967 } else {
968 if ((status = p7_Builder(bld, msa, bg, &hmm, NULL, NULL, NULL, postmsa_ptr)) != eslOK) { strcpy(errmsg, bld->errbuf); goto ERROR; }
969 }
970
971
972 ESL_DPRINTF2(("worker %d: has produced an HMM %s\n", cfg->my_rank, hmm->name));
973
974 /* Calculate upper bound on size of sending status, HMM, and optional postmsa; make sure wbuf can hold it. */
975 n = 0;
976 if (MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &sz) != 0) goto ERROR; else n += sz;
977 if (p7_hmm_MPIPackSize( hmm, MPI_COMM_WORLD, &sz) != eslOK) goto ERROR; else n += sz;
978 if (esl_msa_MPIPackSize(postmsa, MPI_COMM_WORLD, &sz) != eslOK) goto ERROR; else n += sz;
979 if (n > wn) { ESL_RALLOC(wbuf, tmp, sizeof(char) * n); wn = n; }
980 ESL_DPRINTF2(("worker %d: has calculated that HMM will pack into %d bytes\n", cfg->my_rank, n));
981
982 /* Send status, HMM, and optional postmsa back to the master */
983 pos = 0;
984 if (MPI_Pack (&status, 1, MPI_INT, wbuf, wn, &pos, MPI_COMM_WORLD) != 0) goto ERROR;
985 if (p7_hmm_MPIPack (hmm, wbuf, wn, &pos, MPI_COMM_WORLD) != eslOK) goto ERROR;
986 if (esl_msa_MPIPack(postmsa, wbuf, wn, &pos, MPI_COMM_WORLD) != eslOK) goto ERROR;
987 MPI_Send(wbuf, pos, MPI_PACKED, 0, 0, MPI_COMM_WORLD);
988 ESL_DPRINTF2(("worker %d: has sent HMM to master in message of %d bytes\n", cfg->my_rank, pos));
989
990 esl_msa_Destroy(msa); msa = NULL;
991 esl_msa_Destroy(postmsa); postmsa = NULL;
992 p7_hmm_Destroy(hmm); hmm = NULL;
993 }
994
995 if (wbuf != NULL) free(wbuf);
996 p7_builder_Destroy(bld);
997 return;
998
999 ERROR:
1000 ESL_DPRINTF2(("worker %d: fails, is sending an error message, as follows:\n%s\n", cfg->my_rank, errmsg));
1001 pos = 0;
1002 MPI_Pack(&status, 1, MPI_INT, wbuf, wn, &pos, MPI_COMM_WORLD);
1003 MPI_Pack(errmsg, eslERRBUFSIZE, MPI_CHAR, wbuf, wn, &pos, MPI_COMM_WORLD);
1004 MPI_Send(wbuf, pos, MPI_PACKED, 0, 0, MPI_COMM_WORLD);
1005 if (wbuf != NULL) free(wbuf);
1006 if (msa != NULL) esl_msa_Destroy(msa);
1007 if (hmm != NULL) p7_hmm_Destroy(hmm);
1008 if (bld != NULL) p7_builder_Destroy(bld);
1009 return;
1010 }
1011 #endif /*HMMER_MPI*/
1012
1013
1014 static void
1015 serial_loop(WORKER_INFO *info, struct cfg_s *cfg, const ESL_GETOPTS *go)
1016 {
1017 P7_BUILDER *bld = NULL;
1018 ESL_MSA *msa = NULL;
1019 ESL_SQ *sq = NULL;
1020 ESL_MSA *postmsa = NULL;
1021 ESL_MSA **postmsa_ptr = (cfg->postmsafile != NULL) ? &postmsa : NULL;
1022 P7_HMM *hmm = NULL;
1023 char errmsg[eslERRBUFSIZE];
1024 int status;
1025
1026 double entropy;
1027
1028 cfg->nali = 0;
1029 while ((status = esl_msafile_Read(cfg->afp, &msa)) != eslEOF)
1030 {
1031 if (status != eslOK) esl_msafile_ReadFailure(cfg->afp, status);
1032 cfg->nali++;
1033
1034 if ((status = set_msa_name(cfg, errmsg, msa)) != eslOK) p7_Fail("%s\n", errmsg); /* cfg->nnamed gets incremented in this call */
1035
1036
1037 /* bg new-HMM trarr gm om */
1038 if ( msa->nseq == 1 && esl_opt_IsUsed(go, "--singlemx")) {
1039 if ((status = esl_sq_FetchFromMSA(msa, 0, &sq)) != eslOK) p7_Fail("build failed: %s", bld->errbuf);
1040 if ((status = p7_SingleBuilder(info->bld, sq, info->bg, &hmm, NULL, NULL, NULL)) != eslOK) p7_Fail("build failed: %s", bld->errbuf);
1041 esl_sq_Destroy(sq);
1042 sq = NULL;
1043 hmm->eff_nseq = 1;
1044 } else {
1045 if ((status = p7_Builder(info->bld, msa, info->bg, &hmm, NULL, NULL, NULL, postmsa_ptr )) != eslOK) p7_Fail("build failed: %s", bld->errbuf);
1046
1047 //if not --singlemx, but the user set the popen/pextend flags, override the computed gap params now:
1048 if (info->bld->popen != -1 || info->bld->pextend != -1) {
1049 apply_fixed_gap_params(hmm, info->bld->popen, info->bld->pextend);
1050 }
1051 }
1052 entropy = p7_MeanMatchRelativeEntropy(hmm, info->bg);
1053 if ((status = output_result(cfg, errmsg, cfg->nali, msa, hmm, postmsa, entropy)) != eslOK) p7_Fail(errmsg);
1054
1055
1056
1057 p7_hmm_Destroy(hmm);
1058 esl_msa_Destroy(msa);
1059 esl_msa_Destroy(postmsa);
1060 }
1061 }
1062
1063 #ifdef HMMER_THREADS
1064 static void
1065 thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, struct cfg_s *cfg, const ESL_GETOPTS *go)
1066 {
1067 int status = eslOK;
1068 int sstatus = eslOK;
1069 int processed = 0;
1070 WORK_ITEM *item;
1071 void *newItem;
1072
1073 int next = 1;
1074 PENDING_ITEM *top = NULL;
1075 PENDING_ITEM *empty = NULL;
1076 PENDING_ITEM *tmp = NULL;
1077
1078 char errmsg[eslERRBUFSIZE];
1079
1080 esl_workqueue_Reset(queue);
1081 esl_threads_WaitForStart(obj);
1082
1083 status = esl_workqueue_ReaderUpdate(queue, NULL, &newItem);
1084 if (status != eslOK) esl_fatal("Work queue reader failed");
1085
1086 /* Main loop: */
1087 item = (WORK_ITEM *) newItem;
1088 while (sstatus == eslOK) {
1089 sstatus = esl_msafile_Read(cfg->afp, &item->msa);
1090 if (sstatus == eslOK) {
1091 item->nali = ++cfg->nali;
1092 if (set_msa_name(cfg, errmsg, item->msa) != eslOK) p7_Fail("%s\n", errmsg);
1093 }
1094 else if (sstatus == eslEOF && processed < cfg->nali) sstatus = eslOK;
1095 else if (sstatus != eslEOF)
1096 esl_msafile_ReadFailure(cfg->afp, sstatus);
1097
1098 if (sstatus == eslOK) {
1099 item->force_single = esl_opt_IsUsed(go, "--singlemx");
1100 status = esl_workqueue_ReaderUpdate(queue, item, &newItem);
1101 if (status != eslOK) esl_fatal("Work queue reader failed");
1102
1103 /* process any results */
1104 item = (WORK_ITEM *) newItem;
1105 if (item->processed == TRUE) {
1106 ++processed;
1107
1108 /* try to keep the input output order the same */
1109 if (item->nali == next) {
1110 sstatus = output_result(cfg, errmsg, item->nali, item->msa, item->hmm, item->postmsa, item->entropy);
1111 if (sstatus != eslOK) p7_Fail(errmsg);
1112
1113 p7_hmm_Destroy(item->hmm);
1114 esl_msa_Destroy(item->msa);
1115 esl_msa_Destroy(item->postmsa);
1116
1117 ++next;
1118
1119 /* output any pending msa as long as the order
1120 * remains the same as read in.
1121 */
1122 while (top != NULL && top->nali == next) {
1123 sstatus = output_result(cfg, errmsg, top->nali, top->msa, top->hmm, top->postmsa, top->entropy);
1124 if (sstatus != eslOK) p7_Fail(errmsg);
1125
1126 p7_hmm_Destroy(top->hmm);
1127 esl_msa_Destroy(top->msa);
1128 esl_msa_Destroy(top->postmsa);
1129
1130 tmp = top;
1131 top = tmp->next;
1132
1133 tmp->next = empty;
1134 empty = tmp;
1135
1136 ++next;
1137 }
1138 } else {
1139 /* queue up the msa so the sequence order is the same in
1140 * the .sto and .hmm
1141 */
1142 if (empty != NULL) {
1143 tmp = empty;
1144 empty = tmp->next;
1145 } else {
1146 ESL_ALLOC(tmp, sizeof(PENDING_ITEM));
1147 }
1148
1149 tmp->nali = item->nali;
1150 tmp->hmm = item->hmm;
1151 tmp->msa = item->msa;
1152 tmp->postmsa = item->postmsa;
1153 tmp->entropy = item->entropy;
1154
1155 /* add the msa to the pending list */
1156 if (top == NULL || tmp->nali < top->nali) {
1157 tmp->next = top;
1158 top = tmp;
1159 } else {
1160 PENDING_ITEM *ptr = top;
1161 while (ptr->next != NULL && tmp->nali > ptr->next->nali) {
1162 ptr = ptr->next;
1163 }
1164 tmp->next = ptr->next;
1165 ptr->next = tmp;
1166 }
1167 }
1168
1169 item->nali = 0;
1170 item->processed = FALSE;
1171 item->hmm = NULL;
1172 item->msa = NULL;
1173 item->postmsa = NULL;
1174 item->entropy = 0.0;
1175 }
1176 }
1177 }
1178
1179 if (top != NULL) esl_fatal("Top is not empty\n");
1180
1181 while (empty != NULL) {
1182 tmp = empty;
1183 empty = tmp->next;
1184 free(tmp);
1185 }
1186
1187 status = esl_workqueue_ReaderUpdate(queue, item, NULL);
1188 if (status != eslOK) esl_fatal("Work queue reader failed");
1189
1190 if (sstatus == eslEOF)
1191 {
1192 /* wait for all the threads to complete */
1193 esl_threads_WaitForFinish(obj);
1194 esl_workqueue_Complete(queue);
1195 }
1196 return;
1197
1198 ERROR:
1199 p7_Fail("thread_loop failed: memory allocation problem");
1200 }
1201
1202 static void
1203 pipeline_thread(void *arg)
1204 {
1205 int workeridx;
1206 int status;
1207
1208 WORK_ITEM *item;
1209 void *newItem;
1210
1211 WORKER_INFO *info;
1212 ESL_THREADS *obj;
1213 ESL_SQ *sq = NULL;
1214
1215 obj = (ESL_THREADS *) arg;
1216 esl_threads_Started(obj, &workeridx);
1217
1218 info = (WORKER_INFO *) esl_threads_GetData(obj, workeridx);
1219
1220 status = esl_workqueue_WorkerUpdate(info->queue, NULL, &newItem);
1221 if (status != eslOK) esl_fatal("Work queue worker failed");
1222
1223 /* loop until all blocks have been processed */
1224 item = (WORK_ITEM *) newItem;
1225 while (item->msa != NULL)
1226 {
1227
1228 if ( item->msa->nseq == 1 && item->force_single) {
1229 status = esl_sq_FetchFromMSA(item->msa, 0, &sq);
1230 if (status != eslOK) p7_Fail("build failed: %s", info->bld->errbuf);
1231
1232 status = p7_SingleBuilder(info->bld, sq, info->bg, &item->hmm, NULL, NULL, NULL);
1233 if (status != eslOK) p7_Fail("build failed: %s", info->bld->errbuf);
1234
1235 esl_sq_Destroy(sq);
1236 sq = NULL;
1237 item->hmm->eff_nseq = 1;
1238 } else {
1239 status = p7_Builder(info->bld, item->msa, info->bg, &item->hmm, NULL, NULL, NULL, &item->postmsa);
1240 if (status != eslOK) p7_Fail("build failed: %s", info->bld->errbuf);
1241
1242 //if not --singlemx, but the user set the popen/pextend flags, override the computed gap params now:
1243 if (info->bld->popen != -1 || info->bld->pextend != -1) {
1244 apply_fixed_gap_params(item->hmm, info->bld->popen, info->bld->pextend);
1245 }
1246 }
1247
1248 item->entropy = p7_MeanMatchRelativeEntropy(item->hmm, info->bg);
1249 item->processed = TRUE;
1250
1251 status = esl_workqueue_WorkerUpdate(info->queue, item, &newItem);
1252 if (status != eslOK) esl_fatal("Work queue worker failed");
1253
1254 item = (WORK_ITEM *) newItem;
1255 }
1256
1257 status = esl_workqueue_WorkerUpdate(info->queue, item, NULL);
1258 if (status != eslOK) esl_fatal("Work queue worker failed");
1259
1260 esl_threads_Finished(obj, workeridx);
1261 return;
1262 }
1263 #endif /* HMMER_THREADS */
1264
1265
1266
1267
1268 static int
1269 output_result(const struct cfg_s *cfg, char *errbuf, int msaidx, ESL_MSA *msa, P7_HMM *hmm, ESL_MSA *postmsa, double entropy)
1270 {
1271 int status;
1272
1273 /* Special case: output the tabular results header.
1274 * Arranged this way to keep the two fprintf()'s close together in the code,
1275 * so we can keep the data and labels properly sync'ed.
1276 */
1277 if (msa == NULL)
1278 {
1279 if (cfg->abc->type == eslAMINO) {
1280 if (fprintf(cfg->ofp, "#%4s %-20s %5s %5s %5s %8s %6s %s\n", " idx", "name", "nseq", "alen", "mlen", "eff_nseq", "re/pos", "description") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "output_result: write failed");
1281 if (fprintf(cfg->ofp, "#%4s %-20s %5s %5s %5s %8s %6s %s\n", "----", "--------------------", "-----", "-----", "-----", "--------", "------", "-----------") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "output_result: write failed");
1282 } else {
1283 if (fprintf(cfg->ofp, "#%4s %-20s %5s %5s %5s %5s %8s %6s %s\n", " idx", "name", "nseq", "alen", "mlen", "W", "eff_nseq", "re/pos", "description") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "output_result: write failed");
1284 if (fprintf(cfg->ofp, "#%4s %-20s %5s %5s %5s %5s %8s %6s %s\n", "----", "--------------------", "-----", "-----", "-----", "-----", "--------", "------", "-----------") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "output_result: write failed");
1285 }
1286 return eslOK;
1287 }
1288 // if ((status = p7_hmm_Validate(hmm, errbuf, 0.0001)) != eslOK) return status;
1289 if ((status = p7_hmmfile_WriteASCII(cfg->hmmfp, -1, hmm)) != eslOK) ESL_FAIL(status, errbuf, "HMM save failed");
1290
1291 /* # name nseq alen M max_length eff_nseq re/pos description */
1292 if (cfg->abc->type == eslAMINO) {
1293 if (fprintf(cfg->ofp, "%-5d %-20s %5d %5" PRId64 " %5d %8.2f %6.3f %s\n",
1294 msaidx,
1295 (msa->name != NULL) ? msa->name : "",
1296 msa->nseq,
1297 msa->alen,
1298 hmm->M,
1299 hmm->eff_nseq,
1300 entropy,
1301 (msa->desc != NULL) ? msa->desc : "") < 0)
1302 ESL_EXCEPTION_SYS(eslEWRITE, "output_result: write failed");
1303 } else {
1304 if (fprintf(cfg->ofp, "%-5d %-20s %5d %5" PRId64 " %5d %5d %8.2f %6.3f %s\n",
1305 msaidx,
1306 (msa->name != NULL) ? msa->name : "",
1307 msa->nseq,
1308 msa->alen,
1309 hmm->M,
1310 hmm->max_length,
1311 hmm->eff_nseq,
1312 entropy,
1313 (msa->desc != NULL) ? msa->desc : "") < 0)
1314 ESL_EXCEPTION_SYS(eslEWRITE, "output_result: write failed");
1315 }
1316
1317 if (cfg->postmsafp != NULL && postmsa != NULL) {
1318 esl_msafile_Write(cfg->postmsafp, postmsa, eslMSAFILE_STOCKHOLM);
1319 }
1320
1321 return eslOK;
1322 }
1323
1324
1325
1326 /* set_msa_name()
1327 * Make sure the alignment has a name; this name will
1328 * then be transferred to the model.
1329 *
1330 * We can only do this for a single alignment in a file. For multi-MSA
1331 * files, each MSA is required to have a name already.
1332 *
1333 * Priority is:
1334 * 1. Use -n <name> if set, overriding any name the alignment might already have.
1335 * 2. Use alignment's existing name, if non-NULL.
1336 * 3. Make a name, from alignment file name without path and without filename extension
1337 * (e.g. "/usr/foo/globins.slx" gets named "globins")
1338 * If none of these succeeds, return <eslEINVAL>.
1339 *
1340 * If a multiple MSA database (e.g. Stockholm/Pfam), and we encounter
1341 * an MSA that doesn't already have a name, return <eslEINVAL> if nali > 1.
1342 * (We don't know we're in a multiple MSA database until we're on the second
1343 * alignment.)
1344 *
1345 * If we're in MPI mode, we assume we're in a multiple MSA database,
1346 * even on the first alignment.
1347 *
1348 * Because we can't tell whether we've got more than one
1349 * alignment 'til we're on the second one, these fatal errors
1350 * only happen after the first HMM has already been built.
1351 * Oh well.
1352 */
1353 static int
1354 set_msa_name(struct cfg_s *cfg, char *errbuf, ESL_MSA *msa)
1355 {
1356 char *name = NULL;
1357 int status;
1358
1359 if (cfg->do_mpi == FALSE && cfg->nali == 1) /* first (only?) HMM in file: */
1360 {
1361 if (cfg->hmmName != NULL)
1362 {
1363 if ((status = esl_msa_SetName(msa, cfg->hmmName, -1)) != eslOK) return status;
1364 }
1365 else if (msa->name != NULL)
1366 {
1367 cfg->nnamed++;
1368 }
1369 else if (cfg->afp->bf->filename)
1370 {
1371 if ((status = esl_FileTail(cfg->afp->bf->filename, TRUE, &name)) != eslOK) return status; /* TRUE=nosuffix */
1372 if ((status = esl_msa_SetName(msa, name, -1)) != eslOK) return status;
1373 free(name);
1374 }
1375 else ESL_FAIL(eslEINVAL, errbuf, "Failed to set model name: msa has no name, no msa filename, and no -n");
1376 }
1377 else
1378 {
1379 if (cfg->hmmName != NULL) ESL_FAIL(eslEINVAL, errbuf, "Oops. Wait. You can't use -n with an alignment database.");
1380 else if (msa->name != NULL) cfg->nnamed++;
1381 else ESL_FAIL(eslEINVAL, errbuf, "Oops. Wait. I need name annotation on each alignment in a multi MSA file; failed on #%d", cfg->nali+1);
1382
1383 /* special kind of failure: the *first* alignment didn't have a name, and we used the filename to
1384 * construct one; now that we see a second alignment, we realize this was a boo-boo*/
1385 if (cfg->nnamed != cfg->nali) ESL_FAIL(eslEINVAL, errbuf, "Oops. Wait. I need name annotation on each alignment in a multi MSA file; first MSA didn't have one");
1386 }
1387 return eslOK;
1388 }
1389
1390