Debian Codegraph @

hmmer/src/jackhmmer.c

raw symbols SCIP
1 /* jackhmmer: iterative search of a protein sequence against a protein database
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_dmatrix.h"
12 #include "esl_getopts.h"
13 #include "esl_keyhash.h"
14 #include "esl_msa.h"
15 #include "esl_msafile.h"
16 #include "esl_scorematrix.h"
17 #include "esl_sq.h"
18 #include "esl_sqio.h"
19 #include "esl_stopwatch.h"
20
21 #ifdef HMMER_MPI
22 #include "mpi.h"
23 #include "esl_mpi.h"
24 #endif
25
26 #ifdef HMMER_THREADS
27 #include <unistd.h>
28 #include "esl_threads.h"
29 #include "esl_workqueue.h"
30 #endif
31
32 #include "hmmer.h"
33
34 typedef struct {
35 #ifdef HMMER_THREADS
36 ESL_WORK_QUEUE *queue;
37 #endif
38 P7_BG *bg;
39 P7_PIPELINE *pli;
40 P7_TOPHITS *th;
41 P7_OPROFILE *om;
42 } WORKER_INFO;
43
44 #define REPOPTS "-E,-T,--cut_ga,--cut_nc,--cut_tc"
45 #define DOMREPOPTS "--domE,--domT,--cut_ga,--cut_nc,--cut_tc"
46 #define INCOPTS "--incE,--incT,--cut_ga,--cut_nc,--cut_tc"
47 #define INCDOMOPTS "--incdomE,--incdomT,--cut_ga,--cut_nc,--cut_tc"
48 #define THRESHOPTS "-E,-T,--domE,--domT,--incE,--incT,--incdomE,--incdomT,--cut_ga,--cut_nc,--cut_tc"
49 #define CONOPTS "--fast,--hand" // jackhmmer doesn't use these - but leave them for consistency
50 #define EFFOPTS "--eent,--eentexp,--eclust,--eset,--enone" // Exclusive options for effective sequence number calculation
51 #define WGTOPTS "--wgsc,--wblosum,--wpb,--wnone" // Exclusive options for relative weighting
52
53 #if defined (HMMER_THREADS) && defined (HMMER_MPI)
54 #define CPUOPTS "--mpi"
55 #define MPIOPTS "--cpu"
56 #else
57 #define CPUOPTS NULL
58 #define MPIOPTS NULL
59 #endif
60
61 static ESL_OPTIONS options[] = {
62 /* name type default env range toggles reqs incomp help docgroup*/
63 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
64 { "-N", eslARG_INT, "5", NULL, "n>0", NULL, NULL, NULL, "set maximum number of iterations to <n>", 1 },
65 /* Control of output */
66 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "direct output to file <f>, not stdout", 2 },
67 { "-A", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save multiple alignment of hits to file <f>", 2 },
68 { "--tblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save parseable table of per-sequence hits to file <f>", 2 },
69 { "--domtblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save parseable table of per-domain hits to file <f>", 2 },
70 { "--chkhmm", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save HMM checkpoints to files <f>-<iteration>.hmm", 2 },
71 { "--chkali", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save alignment checkpoints to files <f>-<iteration>.sto", 2 },
72 { "--acc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "prefer accessions over names in output", 2 },
73 { "--noali", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "don't output alignments, so output is smaller", 2 },
74 { "--notextw", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--textw", "unlimit ASCII text output line width", 2 },
75 { "--textw", eslARG_INT, "120", NULL, "n>=120", NULL, NULL, "--notextw", "set max width of ASCII text output lines", 2 },
76 /* Control of scoring system */
77 { "--popen", eslARG_REAL, "0.02", NULL, "0<=x<0.5",NULL, NULL, NULL, "gap open probability", 3 },
78 { "--pextend", eslARG_REAL, "0.4", NULL, "0<=x<1", NULL, NULL, NULL, "gap extend probability", 3 },
79 { "--mx", eslARG_STRING, "BLOSUM62", NULL, NULL, NULL, NULL, "--mxfile", "substitution score matrix choice (of some built-in matrices)", 3 },
80 { "--mxfile", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--mx", "read substitution score matrix from file <f>", 3 },
81 /* Control of reporting thresholds */
82 { "-E", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, REPOPTS, "report sequences <= this E-value threshold in output", 4 },
83 { "-T", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, REPOPTS, "report sequences >= this score threshold in output", 4 },
84 { "--domE", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, DOMREPOPTS, "report domains <= this E-value threshold in output", 4 },
85 { "--domT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, DOMREPOPTS, "report domains >= this score cutoff in output", 4 },
86 /* Control of inclusion (significance) thresholds */
87 { "--incE", eslARG_REAL, "0.001", NULL, "x>0", NULL, NULL, INCOPTS, "consider sequences <= this E-value threshold as significant", 5 },
88 { "--incT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCOPTS, "consider sequences >= this score threshold as significant", 5 },
89 { "--incdomE", eslARG_REAL, "0.001", NULL, "x>0", NULL, NULL, INCDOMOPTS, "consider domains <= this E-value threshold as significant", 5 },
90 { "--incdomT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCDOMOPTS, "consider domains >= this score threshold as significant", 5 },
91 /* Model-specific thresholding for both reporting and inclusion (unused in jackhmmer, but p7_Builder() needs them set to defaults) */
92 { "--cut_ga", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's GA gathering cutoffs to set all thresholding", 99 }, // group=99 undocuments them; process_commandline() prohibits them.
93 { "--cut_nc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's NC noise cutoffs to set all thresholding", 99 },
94 { "--cut_tc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's TC trusted cutoffs to set all thresholding", 99 },
95 /* Control of acceleration pipeline */
96 { "--max", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--F1,--F2,--F3", "Turn all heuristic filters off (less speed, more power)", 7 },
97 { "--F1", eslARG_REAL, "0.02", NULL, NULL, NULL, NULL, "--max", "Stage 1 (MSV) threshold: promote hits w/ P <= F1", 7 },
98 { "--F2", eslARG_REAL, "1e-3", NULL, NULL, NULL, NULL, "--max", "Stage 2 (Vit) threshold: promote hits w/ P <= F2", 7 },
99 { "--F3", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, "--max", "Stage 3 (Fwd) threshold: promote hits w/ P <= F3", 7 },
100 { "--nobias", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--max", "turn off composition bias filter", 7 },
101 /* Alternative model construction strategies */
102 { "--fast", eslARG_NONE, FALSE, NULL, NULL, CONOPTS, NULL, NULL, "assign cols w/ >= symfrac residues as consensus", 99 }, // unused/prohibited in jackhmmer. Models must be --hand.
103 { "--hand", eslARG_NONE, "default", NULL, NULL, CONOPTS, NULL, NULL, "manual construction (requires reference annotation)", 99 },
104 { "--symfrac", eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL,"--fast", NULL, "sets sym fraction controlling --fast construction", 99 },
105 { "--fragthresh", eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL, NULL, NULL, "if L <= x*alen, tag sequence as a fragment", 8 },
106 /* Alternative relative sequence weighting strategies */
107 { "--wpb", eslARG_NONE, "default", NULL, NULL, WGTOPTS, NULL, NULL, "Henikoff position-based weights", 9 },
108 { "--wgsc", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "Gerstein/Sonnhammer/Chothia tree weights", 9 },
109 { "--wblosum", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "Henikoff simple filter weights", 9 },
110 { "--wnone", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "don't do any relative weighting; set all to 1", 9 },
111 { "--wgiven", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "use weights as given in MSA file", 99 }, /* unused/prohibited in jackhmmer */
112 { "--wid", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"--wblosum",NULL, "for --wblosum: set identity cutoff", 9 },
113 /* Alternative effective sequence weighting strategies */
114 { "--eent", eslARG_NONE, "default", NULL, NULL, EFFOPTS, NULL, NULL, "adjust eff seq # to achieve relative entropy target", 10 },
115 { "--eentexp", eslARG_NONE, "default",NULL, NULL, EFFOPTS, NULL, NULL, "adjust eff seq # to reach rel. ent. target using exp scaling", 10 },
116 { "--eclust", eslARG_NONE, FALSE, NULL, NULL, EFFOPTS, NULL, NULL, "eff seq # is # of single linkage clusters", 10 },
117 { "--enone", eslARG_NONE, FALSE, NULL, NULL, EFFOPTS, NULL, NULL, "no effective seq # weighting: just use nseq", 10 },
118 { "--eset", eslARG_REAL, NULL, NULL, NULL, EFFOPTS, NULL, NULL, "set eff seq # for all models to <x>", 10 },
119 { "--ere", eslARG_REAL, NULL, NULL,"x>0", NULL, NULL, NULL, "for --eent[exp]: set minimum rel entropy/position to <x>", 10 },
120 { "--esigma", eslARG_REAL, "45.0", NULL,"x>0", NULL, NULL, NULL, "for --eent[exp]: set sigma param to <x>", 10 },
121 { "--eid", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"--eclust",NULL, "for --eclust: set fractional identity cutoff to <x>", 10 },
122 /* Alternative prior strategies */
123 { "--pnone", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"--plaplace", "don't use any prior; parameters are frequencies", 13 },
124 { "--plaplace", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--pnone", "use a Laplace +1 prior", 13 },
125 /* Control of E-value calibration */
126 { "--EmL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for MSV Gumbel mu fit", 11 },
127 { "--EmN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for MSV Gumbel mu fit", 11 },
128 { "--EvL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Viterbi Gumbel mu fit", 11 },
129 { "--EvN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Viterbi Gumbel mu fit", 11 },
130 { "--EfL", eslARG_INT, "100", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Forward exp tail tau fit", 11 },
131 { "--EfN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Forward exp tail tau fit", 11 },
132 { "--Eft", eslARG_REAL, "0.04", NULL,"0<x<1", NULL, NULL, NULL, "tail mass for Forward exponential tail tau fit", 11 },
133 /* Other options */
134 { "--nonull2", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "turn off biased composition score corrections", 12 },
135 { "-Z", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of comparisons done, for E-value calculation", 12 },
136 { "--domZ", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of significant seqs, for domain E-value calculation", 12 },
137 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 12 },
138 { "--qformat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert query <seqfile> is in format <s>: no autodetection", 12 },
139 { "--tformat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert target <seqdb> is in format <s>>: no autodetection", 12 },
140
141 #ifdef HMMER_THREADS
142 { "--cpu", eslARG_INT, p7_NCPU,"HMMER_NCPU","n>=0", NULL, NULL, CPUOPTS, "number of parallel CPU workers to use for multithreads", 12 },
143 #endif
144 #ifdef HMMER_MPI
145 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL, "--mpi", NULL, "arrest after start: for debugging MPI under gdb", 12 },
146 { "--mpi", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, MPIOPTS, "run as an MPI parallel program", 12 },
147 #endif
148 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
149 };
150
151 static char usage[] = "[-options] <seqfile> <seqdb>";
152 static char banner[] = "iteratively search a protein sequence against a protein database";
153
154 /* struct cfg_s : "Global" application configuration shared by all threads/processes
155 *
156 * This structure is passed to routines within main.c, as a means of semi-encapsulation
157 * of shared data amongst different parallel processes (threads or MPI processes).
158 */
159 struct cfg_s {
160 char *qfile; /* file to read query sequence from */
161 char *dbfile; /* file to read sequence(s) from */
162
163 int do_mpi; /* TRUE if we're doing MPI parallelization */
164 int nproc; /* how many MPI processes, total */
165 int my_rank; /* who am I, in 0..nproc-1 */
166 };
167
168
169
170 static int serial_master(ESL_GETOPTS *go, struct cfg_s *cfg);
171 static int serial_loop(WORKER_INFO *info, ESL_SQFILE *dbfp);
172 #ifdef HMMER_THREADS
173 #define BLOCK_SIZE 1000
174
175 static int thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, ESL_SQFILE *dbfp);
176 static void pipeline_thread(void *arg);
177 #endif
178
179 #ifdef HMMER_MPI
180 static int mpi_master (ESL_GETOPTS *go, struct cfg_s *cfg);
181 static int mpi_worker (ESL_GETOPTS *go, struct cfg_s *cfg);
182 #endif
183
184 static void checkpoint_hmm(int nquery, P7_HMM *hmm, char *basename, int iteration);
185 static void checkpoint_msa(int nquery, ESL_MSA *msa, char *basename, int iteration);
186
187 /* process_commandline()
188 * Take argc, argv, and options; parse the command line;
189 * display help/usage info.
190 */
191 static int
192 process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go, char **ret_qfile, char **ret_dbfile)
193 {
194 ESL_GETOPTS *go = esl_getopts_Create(options);
195 int status;
196
197 if (esl_opt_ProcessEnvironment(go) != eslOK) { if (printf("Failed to process environment: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
198 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) { if (printf("Failed to parse command line: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
199 if (esl_opt_VerifyConfig(go) != eslOK) { if (printf("Failed to parse command line: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
200
201 /* jackhmmer prohibits setting some standard p7_Builder() options.
202 * they don't appear in documentation (they're set to help group 99)
203 * but we want to make positively sure they don't get set. Not
204 * sufficient to do ! esl_opt_IsDefault(), which only checks that
205 * the setting is at default regardless of how it was set that way;
206 * instead, verify that the option wasn't set at all.
207 */
208 if (esl_opt_GetSetter(go, "--cut_ga") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --cut-ga option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
209 if (esl_opt_GetSetter(go, "--cut_nc") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --cut-nc option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
210 if (esl_opt_GetSetter(go, "--cut_tc") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --cut-tc option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
211 if (esl_opt_GetSetter(go, "--fast") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --fast option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
212 if (esl_opt_GetSetter(go, "--hand") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --hand option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
213 if (esl_opt_GetSetter(go, "--symfrac") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --symfrac option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
214 if (esl_opt_GetSetter(go, "--wgiven") != eslARG_SETBY_DEFAULT) { if (printf("Failed to parse command line: jackhmmer does not accept a --wgiven option\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
215
216 /* help format: */
217 if (esl_opt_GetBoolean(go, "-h") == TRUE)
218 {
219 p7_banner(stdout, argv[0], banner);
220 esl_usage(stdout, argv[0], usage);
221 if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
222 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 120=textwidth*/
223
224 if (puts("\nOptions directing output:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
225 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
226
227 if (puts("\nOptions controlling scoring system in first iteration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
228 esl_opt_DisplayHelp(stdout, go, 3, 2, 80);
229
230 if (puts("\nOptions controlling reporting thresholds:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
231 esl_opt_DisplayHelp(stdout, go, 4, 2, 80);
232
233 if (puts("\nOptions controlling significance thresholds for inclusion in next round:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
234 esl_opt_DisplayHelp(stdout, go, 5, 2, 80);
235
236 if (puts("\nOptions controlling acceleration heuristics:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
237 esl_opt_DisplayHelp(stdout, go, 7, 2, 80);
238
239 if (puts("\nOptions controlling model construction after first iteration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
240 esl_opt_DisplayHelp(stdout, go, 8, 2, 80);
241
242 if (puts("\nOptions controlling relative weights in models after first iteration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
243 esl_opt_DisplayHelp(stdout, go, 9, 2, 80);
244
245 if (puts("\nOptions controlling effective seq number in models after first iteration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
246 esl_opt_DisplayHelp(stdout, go, 10, 2, 80);
247
248 if (puts("\nOptions controlling prior strategy in models after first iteration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
249 esl_opt_DisplayHelp(stdout, go, 13, 2, 80);
250
251 if (puts("\nOptions controlling E value calibration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
252 esl_opt_DisplayHelp(stdout, go, 11, 2, 80);
253
254 if (puts("\nOther expert options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
255 esl_opt_DisplayHelp(stdout, go, 12, 2, 80);
256 exit(0);
257 }
258
259 if (esl_opt_ArgNumber(go) != 2) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
260 if ((*ret_qfile = esl_opt_GetArg(go, 1)) == NULL) { if (puts("Failed to get <seqfile> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
261 if ((*ret_dbfile = esl_opt_GetArg(go, 2)) == NULL) { if (puts("Failed to get <seqdb> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
262
263 /* Validate any attempted use of stdin streams */
264 if (strcmp(*ret_dbfile, "-") == 0)
265 { if (puts("jackhmmer cannot read <seqdb> from stdin stream") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
266
267 *ret_go = go;
268 return eslOK;
269
270 FAILURE: /* all errors handled here are user errors, so be polite. */
271 esl_usage(stdout, argv[0], usage);
272 if (puts("\nwhere basic options are:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
273 esl_opt_DisplayHelp(stdout, go, 1, 2, 120); /* 1= group; 2 = indentation; 120=textwidth*/
274 if (printf("\nTo see more help on available options, do %s -h\n\n", argv[0]) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
275 esl_getopts_Destroy(go);
276 exit(1);
277
278 ERROR:
279 if (go) esl_getopts_Destroy(go);
280 exit(status);
281 }
282
283 static int
284 output_header(FILE *ofp, ESL_GETOPTS *go, char *qfile, char *dbfile)
285 {
286 p7_banner(ofp, go->argv[0], banner);
287
288 if (fprintf(ofp, "# query sequence file: %s\n", qfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
289 if (fprintf(ofp, "# target sequence database: %s\n", dbfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
290 if (esl_opt_IsUsed(go, "-N") && fprintf(ofp, "# maximum iterations set to: %d\n", esl_opt_GetInteger(go, "-N")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
291 if (esl_opt_IsUsed(go, "-o") && fprintf(ofp, "# output directed to file: %s\n", esl_opt_GetString(go, "-o")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
292 if (esl_opt_IsUsed(go, "-A") && fprintf(ofp, "# MSA of hits saved to file: %s\n", esl_opt_GetString(go, "-A")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
293 if (esl_opt_IsUsed(go, "--tblout") && fprintf(ofp, "# per-seq hits tabular output: %s\n", esl_opt_GetString(go, "--tblout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
294 if (esl_opt_IsUsed(go, "--domtblout") && fprintf(ofp, "# per-dom hits tabular output: %s\n", esl_opt_GetString(go, "--domtblout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
295 if (esl_opt_IsUsed(go, "--chkhmm") && fprintf(ofp, "# HMM checkpoint files output: %s-<i>.hmm\n", esl_opt_GetString(go, "--chkhmm")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
296 if (esl_opt_IsUsed(go, "--chkali") && fprintf(ofp, "# MSA checkpoint files output: %s-<i>.sto\n", esl_opt_GetString(go, "--chkali")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
297 if (esl_opt_IsUsed(go, "--acc") && fprintf(ofp, "# prefer accessions over names: yes\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
298 if (esl_opt_IsUsed(go, "--noali") && fprintf(ofp, "# show alignments in output: no\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
299 if (esl_opt_IsUsed(go, "--notextw") && fprintf(ofp, "# max ASCII text line length: unlimited\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
300 if (esl_opt_IsUsed(go, "--textw") && fprintf(ofp, "# max ASCII text line length: %d\n", esl_opt_GetInteger(go, "--textw")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
301 if (esl_opt_IsUsed(go, "--popen") && fprintf(ofp, "# gap open probability: %f\n", esl_opt_GetReal (go, "--popen")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
302 if (esl_opt_IsUsed(go, "--pextend") && fprintf(ofp, "# gap extend probability: %f\n", esl_opt_GetReal (go, "--pextend")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
303 if (esl_opt_IsUsed(go, "--mx") && fprintf(ofp, "# subst score matrix (built-in): %s\n", esl_opt_GetString (go, "--mx")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
304 if (esl_opt_IsUsed(go, "--mxfile") && fprintf(ofp, "# subst score matrix (file): %s\n", esl_opt_GetString (go, "--mxfile")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
305 if (esl_opt_IsUsed(go, "-E") && fprintf(ofp, "# sequence reporting threshold: E-value <= %g\n", esl_opt_GetReal (go, "-E")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
306 if (esl_opt_IsUsed(go, "-T") && fprintf(ofp, "# sequence reporting threshold: score <= %g\n", esl_opt_GetReal (go, "-T")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
307 if (esl_opt_IsUsed(go, "--domE") && fprintf(ofp, "# domain reporting threshold: E-value <= %g\n", esl_opt_GetReal (go, "--domE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
308 if (esl_opt_IsUsed(go, "--domT") && fprintf(ofp, "# domain reporting threshold: score <= %g\n", esl_opt_GetReal (go, "--domT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
309 if (esl_opt_IsUsed(go, "--incE") && fprintf(ofp, "# sequence inclusion threshold: E-value <= %g\n", esl_opt_GetReal (go, "--incE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
310 if (esl_opt_IsUsed(go, "--incT") && fprintf(ofp, "# sequence inclusion threshold: score >= %g\n", esl_opt_GetReal (go, "--incT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
311 if (esl_opt_IsUsed(go, "--incdomE") && fprintf(ofp, "# domain inclusion threshold: E-value <= %g\n", esl_opt_GetReal (go, "--incdomE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
312 if (esl_opt_IsUsed(go, "--incdomT") && fprintf(ofp, "# domain inclusion threshold: score >= %g\n", esl_opt_GetReal (go, "--incdomT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
313 //if (esl_opt_IsUsed(go, "--cut_ga") && fprintf(ofp, "# model-specific thresholding: GA cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
314 //if (esl_opt_IsUsed(go, "--cut_nc") && fprintf(ofp, "# model-specific thresholding: NC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
315 //if (esl_opt_IsUsed(go, "--cut_tc") && fprintf(ofp, "# model-specific thresholding: TC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
316 if (esl_opt_IsUsed(go, "--max") && fprintf(ofp, "# Max sensitivity mode: on [all heuristic filters off]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
317 if (esl_opt_IsUsed(go, "--F1") && fprintf(ofp, "# MSV filter P threshold: <= %g\n", esl_opt_GetReal(go, "--F1")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
318 if (esl_opt_IsUsed(go, "--F2") && fprintf(ofp, "# Vit filter P threshold: <= %g\n", esl_opt_GetReal(go, "--F2")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
319 if (esl_opt_IsUsed(go, "--F3") && fprintf(ofp, "# Fwd filter P threshold: <= %g\n", esl_opt_GetReal(go, "--F3")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
320 if (esl_opt_IsUsed(go, "--nobias") && fprintf(ofp, "# biased composition HMM filter: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
321 if (esl_opt_IsUsed(go, "--fast") && fprintf(ofp, "# model architecture construction: fast/heuristic\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
322 if (esl_opt_IsUsed(go, "--hand") && fprintf(ofp, "# model architecture construction: hand-specified by RF annotation\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
323 if (esl_opt_IsUsed(go, "--symfrac") && fprintf(ofp, "# sym frac for model structure: %.3f\n", esl_opt_GetReal(go, "--symfrac")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
324 if (esl_opt_IsUsed(go, "--fragthresh") && fprintf(ofp, "# define fragments if <= x*alen: %.3f\n", esl_opt_GetReal(go, "--fragthresh")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
325 if (esl_opt_IsUsed(go, "--wpb") && fprintf(ofp, "# relative weighting scheme: Henikoff PB\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
326 if (esl_opt_IsUsed(go, "--wgsc") && fprintf(ofp, "# relative weighting scheme: G/S/C\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
327 if (esl_opt_IsUsed(go, "--wblosum") && fprintf(ofp, "# relative weighting scheme: BLOSUM filter\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
328 if (esl_opt_IsUsed(go, "--wnone") && fprintf(ofp, "# relative weighting scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
329 if (esl_opt_IsUsed(go, "--wid") && fprintf(ofp, "# frac id cutoff for BLOSUM wgts: %f\n", esl_opt_GetReal(go, "--wid")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
330 if (esl_opt_IsUsed(go, "--eent") && fprintf(ofp, "# effective seq number scheme: entropy weighting\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
331 if (esl_opt_IsUsed(go, "--eclust") && fprintf(ofp, "# effective seq number scheme: single linkage clusters\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
332 if (esl_opt_IsUsed(go, "--enone") && fprintf(ofp, "# effective seq number scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
333 if (esl_opt_IsUsed(go, "--eset") && fprintf(ofp, "# effective seq number: set to %f\n", esl_opt_GetReal(go, "--eset")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
334 if (esl_opt_IsUsed(go, "--ere") && fprintf(ofp, "# minimum rel entropy target: %f bits\n", esl_opt_GetReal(go, "--ere")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
335 if (esl_opt_IsUsed(go, "--esigma") && fprintf(ofp, "# entropy target sigma parameter: %f bits\n", esl_opt_GetReal(go, "--esigma")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
336 if (esl_opt_IsUsed(go, "--eid") && fprintf(ofp, "# frac id cutoff for --eclust: %f\n", esl_opt_GetReal(go, "--eid")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
337 if (esl_opt_IsUsed(go, "--pnone") && fprintf(ofp, "# prior scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
338 if (esl_opt_IsUsed(go, "--plaplace") && fprintf(ofp, "# prior scheme: Laplace +1\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
339 if (esl_opt_IsUsed(go, "--EmL") && fprintf(ofp, "# seq length, MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EmL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
340 if (esl_opt_IsUsed(go, "--EmN") && fprintf(ofp, "# seq number, MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EmN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
341 if (esl_opt_IsUsed(go, "--EvL") && fprintf(ofp, "# seq length, Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EvL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
342 if (esl_opt_IsUsed(go, "--EvN") && fprintf(ofp, "# seq number, Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EvN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
343 if (esl_opt_IsUsed(go, "--EfL") && fprintf(ofp, "# seq length, Fwd exp tau fit: %d\n", esl_opt_GetInteger(go, "--EfL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
344 if (esl_opt_IsUsed(go, "--EfN") && fprintf(ofp, "# seq number, Fwd exp tau fit: %d\n", esl_opt_GetInteger(go, "--EfN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
345 if (esl_opt_IsUsed(go, "--Eft") && fprintf(ofp, "# tail mass for Fwd exp tau fit: %f\n", esl_opt_GetReal (go, "--Eft")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
346 if (esl_opt_IsUsed(go, "--nonull2") && fprintf(ofp, "# null2 bias corrections: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
347 if (esl_opt_IsUsed(go, "-Z") && fprintf(ofp, "# sequence search space set to: %.0f\n", esl_opt_GetReal(go, "-Z")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
348 if (esl_opt_IsUsed(go, "--domZ") && fprintf(ofp, "# domain search space set to: %.0f\n", esl_opt_GetReal(go, "--domZ")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
349 if (esl_opt_IsUsed(go, "--seed"))
350 {
351 if (esl_opt_GetInteger(go, "--seed") == 0 && fprintf(ofp, "# random number seed: one-time arbitrary\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
352 else if (fprintf(ofp, "# random number seed set to: %d\n", esl_opt_GetInteger(go, "--seed")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
353 }
354 if (esl_opt_IsUsed(go, "--qformat") && fprintf(ofp, "# query <seqfile> format asserted: %s\n", esl_opt_GetString(go, "--qformat")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
355 if (esl_opt_IsUsed(go, "--tformat") && fprintf(ofp, "# target <seqdb> format asserted: %s\n", esl_opt_GetString(go, "--tformat")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
356 #ifdef HMMER_THREADS
357 if (esl_opt_IsUsed(go, "--cpu") && fprintf(ofp, "# number of worker threads: %d\n", esl_opt_GetInteger(go, "--cpu")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
358 #endif
359 #ifdef HMMER_MPI
360 if (esl_opt_IsUsed(go, "--mpi") && fprintf(ofp, "# MPI: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
361 #endif
362 if (fprintf(ofp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
363 return eslOK;
364 }
365
366 int
367 main(int argc, char **argv)
368 {
369 ESL_GETOPTS *go = NULL; /* command line processing */
370 struct cfg_s cfg; /* configuration data */
371 int status = eslOK;
372
373 /* Set processor specific flags */
374 impl_Init();
375
376 /* Initialize what we can in the config structure (without knowing the alphabet yet)
377 */
378 cfg.qfile = NULL;
379 cfg.dbfile = NULL;
380 cfg.do_mpi = FALSE; /* this gets reset below, if we init MPI */
381 cfg.nproc = 0; /* this gets reset below, if we init MPI */
382 cfg.my_rank = 0; /* this gets reset below, if we init MPI */
383
384 /* Initializations */
385 p7_FLogsumInit(); /* we're going to use table-driven Logsum() approximations at times */
386 process_commandline(argc, argv, &go, &cfg.qfile, &cfg.dbfile);
387
388 /* Figure out who we are, and send control there:
389 * we might be an MPI master, an MPI worker, or a serial program.
390 */
391 #ifdef HMMER_MPI
392 /* pause the execution of the programs execution until the user has a
393 * chance to attach with a debugger and send a signal to resume execution
394 * i.e. (gdb) signal SIGCONT
395 */
396 if (esl_opt_GetBoolean(go, "--stall")) pause();
397
398 if (esl_opt_GetBoolean(go, "--mpi"))
399 {
400 cfg.do_mpi = TRUE;
401 MPI_Init(&argc, &argv);
402 MPI_Comm_rank(MPI_COMM_WORLD, &(cfg.my_rank));
403 MPI_Comm_size(MPI_COMM_WORLD, &(cfg.nproc));
404
405 if (cfg.my_rank > 0) status = mpi_worker(go, &cfg);
406 else status = mpi_master(go, &cfg);
407
408 MPI_Finalize();
409 }
410 else
411 #endif /*HMMER_MPI*/
412 {
413 status = serial_master(go, &cfg);
414 }
415
416 esl_getopts_Destroy(go);
417
418 return status;
419 }
420
421 /* serial_master()
422 * The serial version of hmmsearch.
423 * For each query HMM in <hmmfile> search the database for hits.
424 *
425 * A master can only return if it's successful. All errors are handled immediately and fatally with p7_Fail().
426 */
427 static int
428 serial_master(ESL_GETOPTS *go, struct cfg_s *cfg)
429 {
430 FILE *ofp = stdout; /* output file for results (default stdout) */
431 FILE *afp = NULL; /* alignment output file (-A option) */
432 FILE *tblfp = NULL; /* output stream for tabular per-seq (--tblout) */
433 FILE *domtblfp = NULL; /* output stream for tabular per-seq (--domtblout) */
434 int qformat = eslSQFILE_UNKNOWN; /* format of qfile */
435 int dbformat = eslSQFILE_UNKNOWN; /* format of dbfile */
436 ESL_SQFILE *qfp = NULL; /* open qfile */
437 ESL_SQFILE *dbfp = NULL; /* open dbfile */
438 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
439 P7_BG *bg = NULL; /* null model */
440 P7_BUILDER *bld = NULL; /* HMM construction configuration */
441 ESL_SQ *qsq = NULL; /* query sequence */
442 ESL_KEYHASH *kh = NULL; /* hash of previous top hits' ranks */
443 ESL_STOPWATCH *w = NULL; /* for timing */
444 int nquery = 0;
445 int textw;
446 int iteration;
447 int maxiterations;
448 int nnew_targets;
449 int prv_msa_nseq;
450 int status = eslOK;
451 int qstatus = eslOK;
452 int sstatus = eslOK;
453
454 int i;
455 int ncpus = 0;
456
457 int infocnt = 0;
458 WORKER_INFO *info = NULL;
459 #ifdef HMMER_THREADS
460 ESL_SQ_BLOCK *block = NULL;
461 ESL_THREADS *threadObj= NULL;
462 ESL_WORK_QUEUE *queue = NULL;
463 #endif
464
465 /* Initializations */
466 abc = esl_alphabet_Create(eslAMINO);
467 w = esl_stopwatch_Create();
468 kh = esl_keyhash_Create();
469 maxiterations = esl_opt_GetInteger(go, "-N");
470 textw = (esl_opt_GetBoolean(go, "--notextw") ? 0 : esl_opt_GetInteger(go, "--textw"));
471
472 esl_stopwatch_Start(w);
473
474 /* If caller declared input formats, decode them */
475 if (esl_opt_IsOn(go, "--qformat")) {
476 qformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--qformat"));
477 if (qformat == eslSQFILE_UNKNOWN) p7_Fail("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--qformat"));
478 }
479 if (esl_opt_IsOn(go, "--tformat")) {
480 dbformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--tformat"));
481 if (dbformat == eslSQFILE_UNKNOWN) p7_Fail("%s is not a recognized sequence database file format\n", esl_opt_GetString(go, "--tformat"));
482 }
483
484 /* Initialize a null model.
485 * The single-sequence P7_BUILDER needs to see this, to construct its probabilities.
486 */
487 bg = p7_bg_Create(abc);
488
489 /* Initialize builder configuration
490 * Default matrix is stored in the --mx option, so it's always IsOn().
491 * Check --mxfile first; then go to the --mx option and the default.
492 */
493 bld = p7_builder_Create(go, abc);
494 if (esl_opt_IsOn(go, "--mxfile")) status = p7_builder_SetScoreSystem (bld, esl_opt_GetString(go, "--mxfile"), NULL, esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), bg);
495 else status = p7_builder_LoadScoreSystem(bld, esl_opt_GetString(go, "--mx"), esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), bg);
496 if (status != eslOK) p7_Fail("Failed to set single query seq score system:\n%s\n", bld->errbuf);
497
498 /* Open results output files */
499 if (esl_opt_IsOn(go, "-o") && (ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
500 p7_Fail("Failed to open output file %s for writing\n", esl_opt_GetString(go, "-o"));
501 if (esl_opt_IsOn(go, "-A") && (afp = fopen(esl_opt_GetString(go, "-A"), "w")) == NULL)
502 p7_Fail("Failed to open alignment output file %s for writing\n", esl_opt_GetString(go, "-A"));
503 if (esl_opt_IsOn(go, "--tblout") && (tblfp = fopen(esl_opt_GetString(go, "--tblout"), "w")) == NULL)
504 p7_Fail("Failed to open tabular per-seq output file %s for writing\n", esl_opt_GetString(go, "--tblout"));
505 if (esl_opt_IsOn(go, "--domtblout") && (domtblfp = fopen(esl_opt_GetString(go, "--domtblout"), "w")) == NULL)
506 p7_Fail("Failed to open tabular per-dom output file %s for writing\n", esl_opt_GetString(go, "--domtblout"));
507
508 /* Open the target sequence database for sequential access. */
509 status = esl_sqfile_OpenDigital(abc, cfg->dbfile, dbformat, p7_SEQDBENV, &dbfp);
510 if (status == eslENOTFOUND) p7_Fail("Failed to open target sequence database %s for reading\n", cfg->dbfile);
511 else if (status == eslEFORMAT) p7_Fail("Target sequence database file %s is empty or misformatted\n", cfg->dbfile);
512 else if (status == eslEINVAL) p7_Fail("Can't autodetect format of a stdin or .gz seqfile");
513 else if (status != eslOK) p7_Fail("Unexpected error %d opening target sequence database file %s\n", status, cfg->dbfile);
514
515 if (! esl_sqfile_IsRewindable(dbfp))
516 p7_Fail("Target sequence file %s isn't rewindable; jackhmmer requires that it is", cfg->dbfile);
517
518 /* Open the query sequence file */
519 status = esl_sqfile_OpenDigital(abc, cfg->qfile, qformat, NULL, &qfp);
520 if (status == eslENOTFOUND) p7_Fail("Failed to open sequence file %s for reading\n", cfg->qfile);
521 else if (status == eslEFORMAT) p7_Fail("Sequence file %s is empty or misformatted\n", cfg->qfile);
522 else if (status == eslEINVAL) p7_Fail("Can't autodetect format of a stdin or .gz seqfile");
523 else if (status != eslOK) p7_Fail ("Unexpected error %d opening sequence file %s\n", status, cfg->qfile);
524 qsq = esl_sq_CreateDigital(abc);
525
526 #ifdef HMMER_THREADS
527 /* initialize thread data */
528 ncpus = ESL_MIN(esl_opt_GetInteger(go, "--cpu"), esl_threads_GetCPUCount());
529 if (ncpus > 0)
530 {
531 threadObj = esl_threads_Create(&pipeline_thread);
532 queue = esl_workqueue_Create(ncpus * 2);
533 }
534 #endif
535
536 infocnt = (ncpus == 0) ? 1 : ncpus;
537 ESL_ALLOC(info, (ptrdiff_t) sizeof(*info) * infocnt);
538
539 /* Ready to begin */
540 output_header(ofp, go, cfg->qfile, cfg->dbfile);
541
542 for (i = 0; i < infocnt; ++i)
543 {
544 info[i].pli = NULL;
545 info[i].th = NULL;
546 info[i].om = NULL;
547 info[i].bg = p7_bg_Clone(bg);
548 #ifdef HMMER_THREADS
549 info[i].queue = queue;
550 #endif
551 }
552
553 #ifdef HMMER_THREADS
554 for (i = 0; i < ncpus * 2; ++i)
555 {
556 block = esl_sq_CreateDigitalBlock(BLOCK_SIZE, abc);
557 if (block == NULL)
558 {
559 p7_Fail("Failed to allocate sequence block");
560 }
561
562 status = esl_workqueue_Init(queue, block);
563 if (status != eslOK)
564 {
565 p7_Fail("Failed to add block to work queue");
566 }
567 }
568 #endif
569
570 /* Outer loop over sequence queries, if more than one */
571 while ((qstatus = esl_sqio_Read(qfp, qsq)) == eslOK)
572 {
573 P7_HMM *hmm = NULL; /* HMM - only needed if checkpointed */
574 P7_HMM **ret_hmm = NULL; /* HMM - only needed if checkpointed */
575 P7_OPROFILE *om = NULL; /* optimized query profile */
576 P7_TRACE *qtr = NULL; /* faux trace for query sequence */
577 ESL_MSA *msa = NULL; /* multiple alignment of included hits */
578
579 if (esl_opt_IsOn(go, "--chkhmm")) ret_hmm = &hmm;
580
581 nquery++;
582 if (qsq->n == 0) continue; /* skip zero length queries as if they aren't even present. */
583
584 if (fprintf(ofp, "Query: %s [L=%ld]\n", qsq->name, (long) qsq->n) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
585 if (qsq->acc[0] != '\0' && fprintf(ofp, "Accession: %s\n", qsq->acc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
586 if (qsq->desc[0] != '\0' && fprintf(ofp, "Description: %s\n", qsq->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
587 if (fprintf(ofp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
588
589 for (iteration = 1; iteration <= maxiterations; iteration++)
590 { /* We enter each iteration with an optimized profile. */
591 esl_stopwatch_Start(w);
592
593 if (om != NULL) p7_oprofile_Destroy(om);
594 if (info->pli != NULL) p7_pipeline_Destroy(info->pli);
595 if (info->th != NULL) p7_tophits_Destroy(info->th);
596 if (info->om != NULL) p7_oprofile_Destroy(info->om);
597
598 /* Create the search model: from query alone (round 1) or from MSA (round 2+) */
599 if (msa == NULL) /* round 1 */
600 {
601 p7_SingleBuilder(bld, qsq, info[0].bg, ret_hmm, &qtr, NULL, &om); /* bypass HMM - only need model */
602 prv_msa_nseq = 1;
603 }
604 else
605 {
606 /* Throw away old model. Build new one. */
607 status = p7_Builder(bld, msa, info[0].bg, ret_hmm, NULL, NULL, &om, NULL);
608 if (status == eslENORESULT) p7_Fail("Failed to construct new model from iteration %d results:\n%s", iteration, bld->errbuf);
609 else if (status == eslEFORMAT) p7_Fail("Failed to construct new model from iteration %d results:\n%s", iteration, bld->errbuf);
610 else if (status != eslOK) p7_Fail("Unexpected error constructing new model at iteration %d:", iteration);
611
612 if (fprintf(ofp, "@@\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
613 if (fprintf(ofp, "@@ Round: %d\n", iteration) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
614 if (fprintf(ofp, "@@ Included in MSA: %d subsequences (query + %d subseqs from %d targets)\n",
615 msa->nseq, msa->nseq-1, kh->nkeys) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
616 if (fprintf(ofp, "@@ Model size: %d positions\n", om->M) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
617 if (fprintf(ofp, "@@\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
618
619 prv_msa_nseq = msa->nseq;
620 esl_msa_Destroy(msa);
621 }
622
623 /* HMM checkpoint output */
624 if (esl_opt_IsOn(go, "--chkhmm")) {
625 checkpoint_hmm(nquery, hmm, esl_opt_GetString(go, "--chkhmm"), iteration);
626 p7_hmm_Destroy(hmm);
627 hmm = NULL;
628 }
629
630 /* Create new processing pipeline and top hits list; destroy old. (TODO: reuse rather than recreate) */
631 for (i = 0; i < infocnt; ++i)
632 {
633 info[i].th = p7_tophits_Create();
634 info[i].om = p7_oprofile_Clone(om);
635 info[i].pli = p7_pipeline_Create(go, om->M, 400, FALSE, p7_SEARCH_SEQS); /* 400 is a dummy length for now */
636 p7_pli_NewModel(info[i].pli, info[i].om, info[i].bg);
637
638 #ifdef HMMER_THREADS
639 if (ncpus > 0) esl_threads_AddThread(threadObj, &info[i]);
640 #endif
641 }
642
643 #ifdef HMMER_THREADS
644 if (ncpus > 0) sstatus = thread_loop(threadObj, queue, dbfp);
645 else sstatus = serial_loop(info, dbfp);
646 #else
647 sstatus = serial_loop(info, dbfp);
648 #endif
649 switch(sstatus)
650 {
651 case eslEFORMAT:
652 p7_Fail("Parse failed (sequence file %s):\n%s\n",
653 dbfp->filename, esl_sqfile_GetErrorBuf(dbfp));
654 break;
655 case eslEOF:
656 /* do nothing */
657 break;
658 default:
659 p7_Fail("Unexpected error %d reading sequence file %s",
660 sstatus, dbfp->filename);
661 }
662
663 /* merge the results of the search results */
664 for (i = 1; i < infocnt; ++i)
665 {
666 p7_tophits_Merge(info[0].th, info[i].th);
667 p7_pipeline_Merge(info[0].pli, info[i].pli);
668
669 p7_pipeline_Destroy(info[i].pli);
670 p7_tophits_Destroy(info[i].th);
671 p7_oprofile_Destroy(info[i].om);
672 }
673
674 /* Print the results. */
675 p7_tophits_SortBySortkey(info->th);
676 p7_tophits_Threshold(info->th, info->pli);
677 p7_tophits_CompareRanking(info->th, kh, &nnew_targets);
678 p7_tophits_Targets(ofp, info->th, info->pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
679 p7_tophits_Domains(ofp, info->th, info->pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
680
681 /* Create alignment of the top hits */
682 /* <&qsq, &qtr, 1> included in p7_tophits_Alignment args here => initial query is added to the msa at each round. */
683 p7_tophits_Alignment(info->th, abc, &qsq, &qtr, 1, p7_ALL_CONSENSUS_COLS, &msa);
684 esl_msa_Digitize(abc,msa,NULL);
685 esl_msa_FormatName(msa, "%s-i%d", qsq->name, iteration);
686 if (qsq->acc[0] != '\0') esl_msa_SetAccession(msa, qsq->acc, -1);
687 if (qsq->desc[0] != '\0') esl_msa_SetDesc (msa, qsq->desc, -1);
688 esl_msa_FormatAuthor(msa, "jackhmmer (HMMER %s)", HMMER_VERSION);
689
690 /* Optional checkpointing */
691 if (esl_opt_IsOn(go, "--chkali")) checkpoint_msa(nquery, msa, esl_opt_GetString(go, "--chkali"), iteration);
692
693 esl_stopwatch_Stop(w);
694 p7_pli_Statistics(ofp, info->pli, w);
695
696
697 /* Convergence test */
698 if (fprintf(ofp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
699 if (fprintf(ofp, "@@ New targets included: %d\n", nnew_targets) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
700 if (fprintf(ofp, "@@ New alignment includes: %d subseqs (was %d), including original query\n",
701 msa->nseq, prv_msa_nseq) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
702 if (nnew_targets == 0 && msa->nseq <= prv_msa_nseq)
703 {
704 if (fprintf(ofp, "@@\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
705 if (fprintf(ofp, "@@ CONVERGED (in %d rounds). \n", iteration) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
706 if (fprintf(ofp, "@@\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
707 break;
708 }
709 else if (iteration < maxiterations)
710 { if (fprintf(ofp, "@@ Continuing to next round.\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); }
711
712 esl_sqfile_Position(dbfp, 0);
713 } /* end iteration loop */
714
715 /* Because we destroy/create the hitlist, om, pipeline, and msa above, rather than create/destroy,
716 * the results of the last iteration have carried through to us now, and we can output
717 * whatever final results we care to.
718 */
719 if (tblfp) p7_tophits_TabularTargets(tblfp, qsq->name, qsq->acc, info->th, info->pli, (nquery == 1));
720 if (domtblfp) p7_tophits_TabularDomains(domtblfp, qsq->name, qsq->acc, info->th, info->pli, (nquery == 1));
721 if (afp)
722 {
723 if (textw > 0) esl_msafile_Write(afp, msa, eslMSAFILE_STOCKHOLM);
724 else esl_msafile_Write(afp, msa, eslMSAFILE_PFAM);
725
726 if (fprintf(ofp, "# Alignment of %d hits satisfying inclusion thresholds saved to: %s\n", msa->nseq, esl_opt_GetString(go, "-A")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
727 }
728 if (fprintf(ofp, "//\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
729
730 p7_pipeline_Destroy(info->pli);
731 p7_tophits_Destroy(info->th);
732 p7_oprofile_Destroy(info->om);
733
734 info->pli = NULL;
735 info->th = NULL;
736 info->om = NULL;
737
738 esl_msa_Destroy(msa);
739 p7_oprofile_Destroy(om);
740 p7_trace_Destroy(qtr);
741 esl_sq_Reuse(qsq);
742 esl_keyhash_Reuse(kh);
743 esl_sqfile_Position(dbfp, 0);
744 }
745 if (qstatus == eslEFORMAT) p7_Fail("Parse failed (sequence file %s):\n%s\n",
746 qfp->filename, esl_sqfile_GetErrorBuf(qfp));
747 else if (qstatus != eslEOF) p7_Fail("Unexpected error %d reading sequence file %s",
748 qstatus, qfp->filename);
749
750 /* Terminate outputs - any last words?
751 */
752 if (tblfp) p7_tophits_TabularTail(tblfp, "jackhmmer", p7_SEARCH_SEQS, cfg->qfile, cfg->dbfile, go);
753 if (domtblfp) p7_tophits_TabularTail(domtblfp, "jackhmmer", p7_SEARCH_SEQS, cfg->qfile, cfg->dbfile, go);
754 if (ofp && fprintf(ofp, "[ok]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
755
756 /* Cleanup - prepare for successful exit
757 */
758 for (i = 0; i < infocnt; ++i)
759 p7_bg_Destroy(info[i].bg);
760
761 #ifdef HMMER_THREADS
762 if (ncpus > 0)
763 {
764 esl_workqueue_Reset(queue);
765 while (esl_workqueue_Remove(queue, (void **) &block) == eslOK)
766 esl_sq_DestroyBlock(block);
767 esl_workqueue_Destroy(queue);
768 esl_threads_Destroy(threadObj);
769 }
770 #endif
771
772 free(info);
773
774 esl_keyhash_Destroy(kh);
775 esl_sqfile_Close(qfp);
776 esl_sqfile_Close(dbfp);
777 esl_sq_Destroy(qsq);
778 esl_stopwatch_Destroy(w);
779 p7_builder_Destroy(bld);
780 esl_alphabet_Destroy(abc);
781 p7_bg_Destroy(bg);
782
783 if (ofp != stdout) fclose(ofp);
784 if (afp != NULL) fclose(afp);
785 if (tblfp != NULL) fclose(tblfp);
786 if (domtblfp != NULL) fclose(domtblfp);
787
788 return eslOK;
789
790 ERROR:
791 return eslFAIL;
792 }
793
794 #ifdef HMMER_MPI
795
796 /* Define common tags used by the MPI master/slave processes */
797 #define HMMER_ERROR_TAG 1
798 #define HMMER_HMM_TAG 2
799 #define HMMER_SEQUENCE_TAG 3
800 #define HMMER_BLOCK_TAG 4
801 #define HMMER_PIPELINE_TAG 5
802 #define HMMER_TOPHITS_TAG 6
803 #define HMMER_HIT_TAG 7
804 #define HMMER_TERMINATING_TAG 8
805 #define HMMER_READY_TAG 9
806 #define HMMER_SETUP_READY_TAG 10
807 #define HMMER_OPROFILE_TAG 11
808 #define HMMER_CONTINUE_TAG 12
809
810 char *HMM_TAG_STR[] = {
811 "",
812 "HMMER_ERROR_TAG",
813 "HMMER_HMM_TAG",
814 "HMMER_SEQUENCE_TAG",
815 "HMMER_BLOCK_TAG",
816 "HMMER_PIPELINE_TAG",
817 "HMMER_TOPHITS_TAG",
818 "HMMER_HIT_TAG",
819 "HMMER_TERMINATING_TAG",
820 "HMMER_READY_TAG",
821 "HMMER_SETUP_READY_TAG",
822 "HMMER_OPROFILE_TAG",
823 "HMMER_CONTINUE_TAG",
824 };
825
826 /* mpi_failure()
827 * Generate an error message. If the clients rank is not 0, a
828 * message is created with the error message and sent to the
829 * master process for handling.
830 */
831 static void
832 mpi_failure(char *format, ...)
833 {
834 va_list argp;
835 int status = eslFAIL;
836 int len;
837 int rank;
838 char str[512];
839
840 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
841
842 /* format the error mesg */
843 va_start(argp, format);
844 len = vsnprintf(str, sizeof(str), format, argp);
845 va_end(argp);
846
847 /* make sure the error string is terminated */
848 str[sizeof(str)-1] = '\0';
849
850 /* if the caller is the master, print the results and abort */
851 if (rank == 0)
852 {
853 if (fprintf(stderr, "\nError: ") < 0) exit(eslEWRITE);
854 if (fprintf(stderr, "%s", str) < 0) exit(eslEWRITE);
855 if (fprintf(stderr, "\n") < 0) exit(eslEWRITE);
856 fflush(stderr);
857
858 MPI_Abort(MPI_COMM_WORLD, status);
859 exit(1);
860 }
861 else
862 {
863 MPI_Send(str, len, MPI_CHAR, 0, HMMER_ERROR_TAG, MPI_COMM_WORLD);
864 pause();
865 }
866 }
867
868 #define MAX_BLOCK_SIZE (512*1024)
869
870 typedef struct {
871 uint64_t offset;
872 uint64_t length;
873 uint64_t count;
874 } SEQ_BLOCK;
875
876 typedef struct {
877 int complete;
878 int size;
879 int current;
880 int last;
881 SEQ_BLOCK *blocks;
882 } BLOCK_LIST;
883
884 /* this routine parses the database keeping track of the blocks
885 * offset within the file, number of sequences and the length
886 * of the block. These blocks are passed as work units to the
887 * MPI workers. If multiple hmm's are in the query file, the
888 * blocks are reused without parsing the database a second time.
889 */
890 int next_block(ESL_SQFILE *sqfp, ESL_SQ *sq, BLOCK_LIST *list, SEQ_BLOCK *block)
891 {
892 int status = eslOK;
893
894 /* if the list has been calculated, use it instead of parsing the database */
895 if (list->complete)
896 {
897 if (list->current == list->last)
898 {
899 block->offset = 0;
900 block->length = 0;
901 block->count = 0;
902
903 status = eslEOF;
904 }
905 else
906 {
907 int inx = list->current++;
908
909 block->offset = list->blocks[inx].offset;
910 block->length = list->blocks[inx].length;
911 block->count = list->blocks[inx].count;
912
913 status = eslOK;
914 }
915
916 return status;
917 }
918
919 block->offset = 0;
920 block->length = 0;
921 block->count = 0;
922
923 esl_sq_Reuse(sq);
924 while (block->length < MAX_BLOCK_SIZE && (status = esl_sqio_ReadInfo(sqfp, sq)) == eslOK)
925 {
926 if (block->count == 0) block->offset = sq->roff;
927 block->length = sq->eoff - block->offset + 1;
928 block->count++;
929 esl_sq_Reuse(sq);
930 }
931
932 if (status == eslEOF && block->count > 0) status = eslOK;
933 if (status == eslEOF)
934 {
935 list->complete = 1;
936 list->current = list->last;
937 }
938
939 /* add the block to the list of known blocks */
940 if (status == eslOK)
941 {
942 int inx;
943
944 if (list->last >= list->size)
945 {
946 void *tmp;
947 list->size += 500;
948 ESL_RALLOC(list->blocks, tmp, sizeof(SEQ_BLOCK) * list->size);
949 }
950
951 inx = list->last++;
952 list->blocks[inx].offset = block->offset;
953 list->blocks[inx].length = block->length;
954 list->blocks[inx].count = block->count;
955 }
956
957 return status;
958
959 ERROR:
960 return eslEMEM;
961 }
962
963 /* mpi_master()
964 * The MPI version of hmmbuild.
965 * Follows standard pattern for a master/worker load-balanced MPI program (J1/78-79).
966 *
967 * A master can only return if it's successful.
968 * Errors in an MPI master come in two classes: recoverable and nonrecoverable.
969 *
970 * Recoverable errors include all worker-side errors, and any
971 * master-side error that do not affect MPI communication. Error
972 * messages from recoverable messages are delayed until we've cleanly
973 * shut down the workers.
974 *
975 * Unrecoverable errors are master-side errors that may affect MPI
976 * communication, meaning we cannot count on being able to reach the
977 * workers and shut them down. Unrecoverable errors result in immediate
978 * p7_Fail()'s, which will cause MPI to shut down the worker processes
979 * uncleanly.
980 */
981 static int
982 mpi_master(ESL_GETOPTS *go, struct cfg_s *cfg)
983 {
984 FILE *ofp = stdout; /* output file for results (default stdout) */
985 FILE *afp = NULL; /* alignment output file (-A option) */
986 FILE *tblfp = NULL; /* output stream for tabular per-seq (--tblout) */
987 FILE *domtblfp = NULL; /* output stream for tabular per-seq (--domtblout) */
988 int qformat = eslSQFILE_UNKNOWN; /* format of qfile */
989 int dbformat = eslSQFILE_UNKNOWN; /* format of dbfile */
990 ESL_SQFILE *qfp = NULL; /* open qfile */
991 ESL_SQFILE *dbfp = NULL; /* open dbfile */
992 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
993 P7_BG *bg = NULL; /* null model */
994 P7_BUILDER *bld = NULL; /* HMM construction configuration */
995 ESL_SQ *qsq = NULL; /* query sequence */
996 ESL_SQ *dbsq = NULL; /* target sequence */
997 ESL_KEYHASH *kh = NULL; /* hash of previous top hits' ranks */
998 ESL_STOPWATCH *w = NULL; /* for timing */
999 int nquery = 0;
1000 int textw;
1001 int iteration;
1002 int maxiterations;
1003 int nnew_targets;
1004 int prv_msa_nseq;
1005 int status = eslOK;
1006 int qstatus = eslOK;
1007 int sstatus = eslOK;
1008 int dest;
1009 int tag;
1010
1011 char *mpi_buf = NULL; /* buffer used to pack/unpack structures */
1012 int mpi_size = 0; /* size of the allocated buffer */
1013 BLOCK_LIST *list = NULL;
1014 SEQ_BLOCK block;
1015
1016 int i;
1017 int size;
1018 int done;
1019 MPI_Status mpistatus;
1020
1021 /* Initializations */
1022 abc = esl_alphabet_Create(eslAMINO);
1023 w = esl_stopwatch_Create();
1024 kh = esl_keyhash_Create();
1025 maxiterations = esl_opt_GetInteger(go, "-N");
1026 if (esl_opt_GetBoolean(go, "--notextw")) textw = 0;
1027 else textw = esl_opt_GetInteger(go, "--textw");
1028 esl_stopwatch_Start(w);
1029
1030 /* If caller declared input formats, decode them */
1031 if (esl_opt_IsOn(go, "--qformat")) {
1032 qformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--qformat"));
1033 if (qformat == eslSQFILE_UNKNOWN) mpi_failure("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--qformat"));
1034 }
1035 if (esl_opt_IsOn(go, "--tformat")) {
1036 dbformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--tformat"));
1037 if (dbformat == eslSQFILE_UNKNOWN) mpi_failure("%s is not a recognized sequence database file format\n", esl_opt_GetString(go, "--tformat"));
1038 }
1039
1040 bg = p7_bg_Create(abc);
1041
1042 /* Initialize builder configuration */
1043 bld = p7_builder_Create(go, abc);
1044 /* Default is stored in the --mx option, so it's always IsOn(). Check --mxfile first; then go to the --mx option and the default. */
1045 if (esl_opt_IsOn(go, "--mxfile")) status = p7_builder_SetScoreSystem (bld, esl_opt_GetString(go, "--mxfile"), NULL, esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), bg);
1046 else status = p7_builder_LoadScoreSystem(bld, esl_opt_GetString(go, "--mx"), esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), bg);
1047 if (status != eslOK) mpi_failure("Failed to set single query seq score system:\n%s\n", bld->errbuf);
1048
1049 /* Open results output files */
1050 if (esl_opt_IsOn(go, "-o") && (ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
1051 mpi_failure("Failed to open output file %s for writing\n", esl_opt_GetString(go, "-o"));
1052 if (esl_opt_IsOn(go, "-A") && (afp = fopen(esl_opt_GetString(go, "-A"), "w")) == NULL)
1053 mpi_failure("Failed to open alignment output file %s for writing\n", esl_opt_GetString(go, "-A"));
1054 if (esl_opt_IsOn(go, "--tblout") && (tblfp = fopen(esl_opt_GetString(go, "--tblout"), "w")) == NULL)
1055 mpi_failure("Failed to open tabular per-seq output file %s for writing\n", esl_opt_GetString(go, "--tblfp"));
1056 if (esl_opt_IsOn(go, "--domtblout") && (domtblfp = fopen(esl_opt_GetString(go, "--domtblout"), "w")) == NULL)
1057 mpi_failure("Failed to open tabular per-dom output file %s for writing\n", esl_opt_GetString(go, "--domtblfp"));
1058
1059 /* Open the target sequence database for sequential access. */
1060 status = esl_sqfile_OpenDigital(abc, cfg->dbfile, dbformat, p7_SEQDBENV, &dbfp);
1061 if (status == eslENOTFOUND) mpi_failure("Failed to open target sequence database %s for reading\n", cfg->dbfile);
1062 else if (status == eslEFORMAT) mpi_failure("Target sequence database file %s is empty or misformatted\n", cfg->dbfile);
1063 else if (status == eslEINVAL) mpi_failure("Can't autodetect format of a stdin or .gz seqfile");
1064 else if (status != eslOK) mpi_failure("Unexpected error %d opening target sequence database file %s\n", status, cfg->dbfile);
1065 dbsq = esl_sq_CreateDigital(abc);
1066
1067 if (! esl_sqfile_IsRewindable(dbfp))
1068 mpi_failure("Target sequence file %s isn't rewindable; jackhmmer requires that it is", cfg->dbfile);
1069
1070 /* Open the query sequence file */
1071 status = esl_sqfile_OpenDigital(abc, cfg->qfile, qformat, NULL, &qfp);
1072 if (status == eslENOTFOUND) mpi_failure("Failed to open sequence file %s for reading\n", cfg->qfile);
1073 else if (status == eslEFORMAT) mpi_failure("Sequence file %s is empty or misformatted\n", cfg->qfile);
1074 else if (status == eslEINVAL) mpi_failure("Can't autodetect format of a stdin or .gz seqfile");
1075 else if (status != eslOK) mpi_failure ("Unexpected error %d opening sequence file %s\n", status, cfg->qfile);
1076 qsq = esl_sq_CreateDigital(abc);
1077
1078 ESL_ALLOC(list, sizeof(SEQ_BLOCK));
1079 list->complete = 0;
1080 list->size = 0;
1081 list->current = 0;
1082 list->last = 0;
1083 list->blocks = NULL;
1084
1085 /* Ready to begin */
1086 output_header(ofp, go, cfg->qfile, cfg->dbfile);
1087
1088 /* Outer loop over sequence queries, if more than one */
1089 while ((qstatus = esl_sqio_Read(qfp, qsq)) == eslOK)
1090 {
1091 P7_PIPELINE *pli = NULL; /* accelerated HMM/seq comparison pipeline */
1092 P7_TOPHITS *th = NULL; /* top-scoring sequence hits */
1093 P7_HMM *hmm = NULL; /* HMM - only needed if checkpointed */
1094 P7_HMM **ret_hmm = NULL; /* HMM - only needed if checkpointed */
1095 P7_OPROFILE *om = NULL; /* optimized query profile */
1096 P7_TRACE *qtr = NULL; /* faux trace for query sequence */
1097 ESL_MSA *msa = NULL; /* multiple alignment of included hits */
1098
1099 if (esl_opt_IsOn(go, "--chkhmm")) ret_hmm = &hmm;
1100
1101 nquery++;
1102 if (qsq->n == 0) continue; /* skip zero length queries as if they aren't even present. */
1103
1104 if (fprintf(ofp, "Query: %s [L=%ld]\n", qsq->name, (long) qsq->n) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1105 if (qsq->acc[0] != '\0' && fprintf(ofp, "Accession: %s\n", qsq->acc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1106 if (qsq->desc[0] != '\0' && fprintf(ofp, "Description: %s\n", qsq->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1107 if (fprintf(ofp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1108
1109 for (iteration = 1; iteration <= maxiterations; iteration++)
1110 { /* We enter each iteration with an optimized profile. */
1111 esl_stopwatch_Start(w);
1112
1113 list->current = 0;
1114
1115 if (pli != NULL) p7_pipeline_Destroy(pli);
1116 if (th != NULL) p7_tophits_Destroy(th);
1117 if (om != NULL) p7_oprofile_Destroy(om);
1118
1119 /* Create the search model: from query alone (round 1) or from MSA (round 2+) */
1120 if (msa == NULL) /* round 1 */
1121 {
1122 p7_SingleBuilder(bld, qsq, bg, ret_hmm, &qtr, NULL, &om); /* bypass HMM - only need model */
1123
1124 prv_msa_nseq = 1;
1125 }
1126 else
1127 {
1128 /* Throw away old model. Build new one. */
1129 status = p7_Builder(bld, msa, bg, ret_hmm, NULL, NULL, &om, NULL);
1130 if (status == eslENORESULT) mpi_failure("Failed to construct new model from iteration %d results:\n%s", iteration, bld->errbuf);
1131 else if (status == eslEFORMAT) mpi_failure("Failed to construct new model from iteration %d results:\n%s", iteration, bld->errbuf);
1132 else if (status != eslOK) mpi_failure("Unexpected error constructing new model at iteration %d:", iteration);
1133
1134 if (fprintf(ofp, "@@\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1135 if (fprintf(ofp, "@@ Round: %d\n", iteration) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1136 if (fprintf(ofp, "@@ Included in MSA: %d subsequences (query + %d subseqs from %d targets)\n",
1137 msa->nseq, msa->nseq-1, kh->nkeys) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1138 if (fprintf(ofp, "@@ Model size: %d positions\n", om->M) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1139 if (fprintf(ofp, "@@\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1140
1141 prv_msa_nseq = msa->nseq;
1142 esl_msa_Destroy(msa);
1143 }
1144
1145 /* HMM checkpoint output */
1146 if (esl_opt_IsOn(go, "--chkhmm")) {
1147 checkpoint_hmm(nquery, hmm, esl_opt_GetString(go, "--chkhmm"), iteration);
1148 p7_hmm_Destroy(hmm);
1149 hmm = NULL;
1150 }
1151
1152 /* Create new processing pipeline and top hits list; destroy old. (TODO: reuse rather than recreate) */
1153 th = p7_tophits_Create();
1154 pli = p7_pipeline_Create(go, om->M, 400, FALSE, p7_SEARCH_SEQS); /* 400 is a dummy length for now */
1155 p7_pli_NewModel(pli, om, bg);
1156
1157 /* Send to all the workers the optimized model to search with */
1158 done = 1;
1159 while (done < cfg->nproc)
1160 {
1161 P7_PIPELINE *mpi_pli = NULL;
1162 P7_TOPHITS *mpi_th = NULL;
1163
1164 if (MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpistatus) != 0)
1165 mpi_failure("MPI error %d receiving message from %d\n", mpistatus.MPI_SOURCE);
1166
1167 tag = mpistatus.MPI_TAG;
1168 dest = mpistatus.MPI_SOURCE;
1169
1170 if (tag == HMMER_TOPHITS_TAG)
1171 {
1172 status = p7_tophits_MPIRecv(dest, HMMER_TOPHITS_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size, &mpi_th);
1173 if (status != eslOK) mpi_failure("Unexpected error %d receiving tophits from %d", status, dest);
1174 p7_tophits_Merge(th, mpi_th);
1175 p7_tophits_Destroy(mpi_th);
1176 }
1177 else if (tag == HMMER_PIPELINE_TAG)
1178 {
1179 status = p7_pipeline_MPIRecv(dest, HMMER_PIPELINE_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size, go, &mpi_pli);
1180 if (status != eslOK) mpi_failure("Unexpected error %d receiving pipeline from %d", status, dest);
1181 p7_pipeline_Merge(pli, mpi_pli);
1182 p7_pipeline_Destroy(mpi_pli);
1183
1184 /* after the pipeline message, the worker is done and waiting */
1185 ++done;
1186 }
1187 else
1188 {
1189 MPI_Get_count(&mpistatus, MPI_PACKED, &size);
1190 if (mpi_buf == NULL || size > mpi_size) {
1191 void *tmp;
1192 ESL_RALLOC(mpi_buf, tmp, sizeof(char) * size);
1193 mpi_size = size;
1194 }
1195
1196 MPI_Recv(mpi_buf, size, MPI_PACKED, dest, tag, MPI_COMM_WORLD, &mpistatus);
1197
1198 switch(tag) {
1199 case HMMER_ERROR_TAG:
1200 mpi_failure("MPI client %d raised error:\n%s\n", dest, mpi_buf);
1201 break;
1202 case HMMER_SETUP_READY_TAG:
1203 status = p7_oprofile_MPISend(om, dest, HMMER_OPROFILE_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size);
1204 if (status != eslOK) mpi_failure("Failed to send optimized model to %d\n", dest);
1205 break;
1206 case HMMER_READY_TAG:
1207 sstatus = next_block(dbfp, dbsq, list, &block);
1208 if (sstatus == eslOK || sstatus == eslEOF)
1209 {
1210 MPI_Send(&block, 3, MPI_LONG_LONG_INT, dest, HMMER_BLOCK_TAG, MPI_COMM_WORLD);
1211 }
1212 else if (sstatus == eslEFORMAT)
1213 {
1214 mpi_failure("Parse failed (sequence file %s):\n%s\n",
1215 dbfp->filename, esl_sqfile_GetErrorBuf(dbfp));
1216 }
1217 else
1218 {
1219 mpi_failure("Unexpected error %d reading sequence file %s", sstatus, dbfp->filename);
1220 }
1221 break;
1222 default:
1223 mpi_failure("Unexpected tag %d from %d\n", tag, dest);
1224 break;
1225 }
1226 }
1227 }
1228
1229 /* Print the results. */
1230 p7_tophits_SortBySortkey(th);
1231 p7_tophits_Threshold(th, pli);
1232 p7_tophits_CompareRanking(th, kh, &nnew_targets);
1233 p7_tophits_Targets(ofp, th, pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1234 p7_tophits_Domains(ofp, th, pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1235
1236 /* Create alignment of the top hits */
1237 p7_tophits_Alignment(th, abc, &qsq, &qtr, 1, p7_ALL_CONSENSUS_COLS, &msa);
1238 esl_msa_Digitize(abc,msa,NULL);
1239 esl_msa_FormatName(msa, "%s-i%d", qsq->name, iteration);
1240 if (qsq->acc[0] != '\0') esl_msa_SetAccession(msa, qsq->acc, -1);
1241 if (qsq->desc[0] != '\0') esl_msa_SetDesc (msa, qsq->desc, -1);
1242 esl_msa_FormatAuthor(msa, "jackhmmer (HMMER %s)", HMMER_VERSION);
1243
1244 /* Optional checkpointing */
1245 if (esl_opt_IsOn(go, "--chkali")) checkpoint_msa(nquery, msa, esl_opt_GetString(go, "--chkali"), iteration);
1246
1247 esl_stopwatch_Stop(w);
1248 p7_pli_Statistics(ofp, pli, w);
1249
1250 /* Convergence test */
1251 if (fprintf(ofp, "\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1252 if (fprintf(ofp, "@@ New targets included: %d\n", nnew_targets) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1253 if (fprintf(ofp, "@@ New alignment includes: %d subseqs (was %d), including original query\n",
1254 msa->nseq, prv_msa_nseq) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1255 if (nnew_targets == 0 && msa->nseq <= prv_msa_nseq)
1256 {
1257 if (fprintf(ofp, "@@\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1258 if (fprintf(ofp, "@@ CONVERGED (in %d rounds). \n", iteration) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1259 if (fprintf(ofp, "@@\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1260 break;
1261 }
1262 else if (iteration < maxiterations)
1263 {
1264 if (fprintf(ofp, "@@ Continuing to next round.\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1265
1266 /* send all the workers a CONTINUE signal */
1267 for (dest = 1; dest < cfg->nproc; ++dest)
1268 {
1269 status = eslOK;
1270 MPI_Send(&status, 1, MPI_INT, dest, HMMER_CONTINUE_TAG, MPI_COMM_WORLD);
1271 }
1272 }
1273 } /* end iteration loop */
1274
1275 /* send all the workers a CONTINUE signal */
1276 for (dest = 1; dest < cfg->nproc; ++dest)
1277 {
1278 status = eslEOD;
1279 MPI_Send(&status, 1, MPI_INT, dest, HMMER_CONTINUE_TAG, MPI_COMM_WORLD);
1280 }
1281
1282 /* Because we destroy/create the hitlist, om, pipeline, and msa above, rather than create/destroy,
1283 * the results of the last iteration have carried through to us now, and we can output
1284 * whatever final results we care to.
1285 */
1286 if (tblfp) p7_tophits_TabularTargets(tblfp, qsq->name, qsq->acc, th, pli, (nquery == 1));
1287 if (domtblfp) p7_tophits_TabularDomains(domtblfp, qsq->name, qsq->acc, th, pli, (nquery == 1));
1288 if (afp)
1289 {
1290 if (textw > 0) esl_msafile_Write(afp, msa, eslMSAFILE_STOCKHOLM);
1291 else esl_msafile_Write(afp, msa, eslMSAFILE_PFAM);
1292
1293 if (fprintf(ofp, "# Alignment of %d hits satisfying inclusion thresholds saved to: %s\n", msa->nseq, esl_opt_GetString(go, "-A")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1294 }
1295 if (fprintf(ofp, "//\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1296
1297 p7_pipeline_Destroy(pli);
1298 p7_tophits_Destroy(th);
1299 p7_oprofile_Destroy(om);
1300
1301 esl_msa_Destroy(msa);
1302 p7_trace_Destroy(qtr);
1303 esl_sq_Reuse(qsq);
1304 esl_keyhash_Reuse(kh);
1305 esl_sqfile_Position(dbfp, 0);
1306 }
1307 if (qstatus == eslEFORMAT) mpi_failure("Parse failed (sequence file %s):\n%s\n",
1308 qfp->filename, esl_sqfile_GetErrorBuf(qfp));
1309 else if (qstatus != eslEOF) mpi_failure("Unexpected error %d reading sequence file %s",
1310 qstatus, qfp->filename);
1311
1312 /* monitor all the workers to make sure they have ended */
1313 for (i = 1; i < cfg->nproc; ++i)
1314 {
1315 if (MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpistatus) != 0)
1316 mpi_failure("MPI error %d receiving message from %d\n", mpistatus.MPI_SOURCE);
1317
1318 MPI_Get_count(&mpistatus, MPI_PACKED, &size);
1319 if (mpi_buf == NULL || size > mpi_size) {
1320 void *tmp;
1321 ESL_RALLOC(mpi_buf, tmp, sizeof(char) * size);
1322 mpi_size = size;
1323 }
1324
1325 dest = mpistatus.MPI_SOURCE;
1326 MPI_Recv(mpi_buf, size, MPI_PACKED, dest, mpistatus.MPI_TAG, MPI_COMM_WORLD, &mpistatus);
1327
1328 if (mpistatus.MPI_TAG == HMMER_ERROR_TAG)
1329 mpi_failure("MPI client %d raised error:\n%s\n", dest, mpi_buf);
1330 if (mpistatus.MPI_TAG != HMMER_TERMINATING_TAG)
1331 mpi_failure("Unexpected tag %d from %d\n", mpistatus.MPI_TAG, dest);
1332 }
1333
1334
1335 /* Terminate outputs - any last words?
1336 */
1337 if (tblfp) p7_tophits_TabularTail(tblfp, "jackhmmer", p7_SEARCH_SEQS, cfg->qfile, cfg->dbfile, go);
1338 if (domtblfp) p7_tophits_TabularTail(domtblfp, "jackhmmer", p7_SEARCH_SEQS, cfg->qfile, cfg->dbfile, go);
1339 if (ofp && fprintf(ofp, "[ok]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1340
1341 /* Cleanup - prepare for successful exit */
1342 free(list);
1343 if (mpi_buf != NULL) free(mpi_buf);
1344
1345 p7_bg_Destroy(bg);
1346 esl_keyhash_Destroy(kh);
1347 esl_sqfile_Close(qfp);
1348 esl_sqfile_Close(dbfp);
1349 esl_sq_Destroy(dbsq);
1350 esl_sq_Destroy(qsq);
1351 esl_stopwatch_Destroy(w);
1352 p7_builder_Destroy(bld);
1353 esl_alphabet_Destroy(abc);
1354
1355 if (ofp != stdout) fclose(ofp);
1356 if (afp != NULL) fclose(afp);
1357 if (tblfp != NULL) fclose(tblfp);
1358 if (domtblfp != NULL) fclose(domtblfp);
1359
1360 return eslOK;
1361
1362 ERROR:
1363 return eslFAIL;
1364 }
1365
1366
1367 static int
1368 mpi_worker(ESL_GETOPTS *go, struct cfg_s *cfg)
1369 {
1370 int qformat = eslSQFILE_UNKNOWN; /* format of qfile */
1371 int dbformat = eslSQFILE_UNKNOWN; /* format of dbfile */
1372 ESL_SQFILE *qfp = NULL; /* open qfile */
1373 ESL_SQFILE *dbfp = NULL; /* open dbfile */
1374 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
1375 P7_BG *bg = NULL; /* null model */
1376 P7_BUILDER *bld = NULL; /* HMM construction configuration */
1377 ESL_SQ *qsq = NULL; /* query sequence */
1378 ESL_SQ *dbsq = NULL; /* target sequence */
1379 ESL_KEYHASH *kh = NULL; /* hash of previous top hits' ranks */
1380 ESL_STOPWATCH *w = NULL; /* for timing */
1381 int iteration;
1382 int maxiterations;
1383 int status = eslOK;
1384 int qstatus = eslOK;
1385 int sstatus = eslOK;
1386
1387 char *mpi_buf = NULL; /* buffer used to pack/unpack structures */
1388 int mpi_size = 0; /* size of the allocated buffer */
1389
1390 MPI_Status mpistatus;
1391
1392 /* Initializations */
1393 abc = esl_alphabet_Create(eslAMINO);
1394 w = esl_stopwatch_Create();
1395 kh = esl_keyhash_Create();
1396 maxiterations = esl_opt_GetInteger(go, "-N");
1397
1398 esl_stopwatch_Start(w);
1399
1400 /* If caller declared input formats, decode them */
1401 if (esl_opt_IsOn(go, "--qformat")) {
1402 qformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--qformat"));
1403 if (qformat == eslSQFILE_UNKNOWN) mpi_failure("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--qformat"));
1404 }
1405 if (esl_opt_IsOn(go, "--tformat")) {
1406 dbformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--tformat"));
1407 if (dbformat == eslSQFILE_UNKNOWN) mpi_failure("%s is not a recognized sequence database file format\n", esl_opt_GetString(go, "--tformat"));
1408 }
1409
1410 bg = p7_bg_Create(abc);
1411
1412 /* Initialize builder configuration */
1413 bld = p7_builder_Create(go, abc);
1414 if (esl_opt_IsOn(go, "--mxfile")) status = p7_builder_SetScoreSystem (bld, esl_opt_GetString(go, "--mxfile"), NULL, esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), bg);
1415 else status = p7_builder_LoadScoreSystem(bld, esl_opt_GetString(go, "--mx"), esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), bg);
1416 if (status != eslOK) mpi_failure("Failed to set single query seq score system:\n%s\n", bld->errbuf);
1417
1418 /* Open the target sequence database for sequential access. */
1419 status = esl_sqfile_OpenDigital(abc, cfg->dbfile, dbformat, p7_SEQDBENV, &dbfp);
1420 if (status == eslENOTFOUND) mpi_failure("Failed to open target sequence database %s for reading\n", cfg->dbfile);
1421 else if (status == eslEFORMAT) mpi_failure("Target sequence database file %s is empty or misformatted\n", cfg->dbfile);
1422 else if (status == eslEINVAL) mpi_failure("Can't autodetect format of a stdin or .gz seqfile");
1423 else if (status != eslOK) mpi_failure("Unexpected error %d opening target sequence database file %s\n", status, cfg->dbfile);
1424 dbsq = esl_sq_CreateDigital(abc);
1425
1426 if (! esl_sqfile_IsRewindable(dbfp))
1427 mpi_failure("Target sequence file %s isn't rewindable; jackhmmer requires that it is", cfg->dbfile);
1428
1429 /* Open the query sequence file */
1430 status = esl_sqfile_OpenDigital(abc, cfg->qfile, qformat, NULL, &qfp);
1431 if (status == eslENOTFOUND) mpi_failure("Failed to open sequence file %s for reading\n", cfg->qfile);
1432 else if (status == eslEFORMAT) mpi_failure("Sequence file %s is empty or misformatted\n", cfg->qfile);
1433 else if (status == eslEINVAL) mpi_failure("Can't autodetect format of a stdin or .gz seqfile");
1434 else if (status != eslOK) mpi_failure ("Unexpected error %d opening sequence file %s\n", status, cfg->qfile);
1435 qsq = esl_sq_CreateDigital(abc);
1436
1437 /* Outer loop over sequence queries, if more than one */
1438 while ((qstatus = esl_sqio_Read(qfp, qsq)) == eslOK)
1439 {
1440 P7_PIPELINE *pli = NULL; /* accelerated HMM/seq comparison pipeline */
1441 P7_TOPHITS *th = NULL; /* top-scoring sequence hits */
1442 P7_OPROFILE *om = NULL; /* optimized query profile */
1443 P7_TRACE *qtr = NULL; /* faux trace for query sequence */
1444
1445 SEQ_BLOCK block;
1446
1447 if (qsq->n == 0) continue; /* skip zero length queries as if they aren't even present. */
1448
1449 iteration = 1;
1450 while (iteration > 0)
1451 { /* We enter each iteration with an optimized profile. */
1452 esl_stopwatch_Start(w);
1453
1454 status = 0;
1455 MPI_Send(&status, 1, MPI_INT, 0, HMMER_SETUP_READY_TAG, MPI_COMM_WORLD);
1456
1457 /* Receive the search model from the master */
1458 status = p7_oprofile_MPIRecv(0, HMMER_OPROFILE_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size, &abc, &om);
1459
1460 /* check the status of the oprofile */
1461 if (status != eslOK) mpi_failure("Error %d receiving optimized model on iteration %d\n", status, iteration);
1462 if (iteration > maxiterations) mpi_failure("Iteration %d exceeds max iterations of %d\n", iteration, maxiterations);
1463
1464 status = 0;
1465 MPI_Send(&status, 1, MPI_INT, 0, HMMER_READY_TAG, MPI_COMM_WORLD);
1466
1467 /* Create new processing pipeline and top hits list; destroy old. (TODO: reuse rather than recreate) */
1468 th = p7_tophits_Create();
1469 pli = p7_pipeline_Create(go, om->M, 400, FALSE, p7_SEARCH_SEQS); /* 400 is a dummy length for now */
1470 p7_pli_NewModel(pli, om, bg);
1471
1472 /* receive a sequence block from the master */
1473 MPI_Recv(&block, 3, MPI_LONG_LONG_INT, 0, HMMER_BLOCK_TAG, MPI_COMM_WORLD, &mpistatus);
1474 while (block.count > 0)
1475 {
1476 uint64_t length = 0;
1477 uint64_t count = block.count;
1478
1479 status = esl_sqfile_Position(dbfp, block.offset);
1480 if (status != eslOK) mpi_failure("Cannot position sequence database to %ld\n", block.offset);
1481
1482 while (count > 0 && (sstatus = esl_sqio_Read(dbfp, dbsq)) == eslOK)
1483 {
1484 length = dbsq->eoff - block.offset + 1;
1485
1486 p7_pli_NewSeq(pli, dbsq);
1487 p7_bg_SetLength(bg, dbsq->n);
1488 p7_oprofile_ReconfigLength(om, dbsq->n);
1489
1490 p7_Pipeline(pli, om, bg, dbsq, NULL, th);
1491
1492 esl_sq_Reuse(dbsq);
1493 p7_pipeline_Reuse(pli);
1494
1495 --count;
1496 }
1497
1498 /* lets do a little bit of sanity checking here to make sure the blocks are the same */
1499 if (count > 0) mpi_failure("Block count mismatch - expected %ld found %ld at offset %ld\n", block.count, block.count - count, block.offset);
1500 if (block.length != length) mpi_failure("Block length mismatch - expected %ld found %ld at offset %ld\n", block.length, length, block.offset);
1501
1502 /* inform the master we need another block of sequences */
1503 status = 0;
1504 MPI_Send(&status, 1, MPI_INT, 0, HMMER_READY_TAG, MPI_COMM_WORLD);
1505
1506 /* wait for the next block of sequences */
1507 MPI_Recv(&block, 3, MPI_LONG_LONG_INT, 0, HMMER_BLOCK_TAG, MPI_COMM_WORLD, &mpistatus);
1508 }
1509
1510 esl_stopwatch_Stop(w);
1511
1512 /* Send the top hits back to the master. */
1513 p7_tophits_MPISend(th, 0, HMMER_TOPHITS_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size);
1514 p7_pipeline_MPISend(pli, 0, HMMER_PIPELINE_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size);
1515
1516 if (om != NULL) p7_oprofile_Destroy(om);
1517 if (pli != NULL) p7_pipeline_Destroy(pli);
1518 if (th != NULL) p7_tophits_Destroy(th);
1519
1520 /* wait until the master lets us continue */
1521 MPI_Recv(&status, 1, MPI_INT, 0, HMMER_CONTINUE_TAG, MPI_COMM_WORLD, &mpistatus);
1522 iteration = (status == eslOK) ? iteration+1 : 0;
1523 } /* end iteration loop */
1524
1525 p7_trace_Destroy(qtr);
1526 esl_sq_Reuse(qsq);
1527 esl_keyhash_Reuse(kh);
1528 esl_sqfile_Position(dbfp, 0);
1529 }
1530 if (qstatus == eslEFORMAT) mpi_failure("Parse failed (sequence file %s):\n%s\n",
1531 qfp->filename, esl_sqfile_GetErrorBuf(qfp));
1532 else if (qstatus != eslEOF) mpi_failure("Unexpected error %d reading sequence file %s",
1533 qstatus, qfp->filename);
1534
1535 status = 0;
1536 MPI_Send(&status, 1, MPI_INT, 0, HMMER_TERMINATING_TAG, MPI_COMM_WORLD);
1537
1538 if (mpi_buf != NULL) free(mpi_buf);
1539
1540 p7_bg_Destroy(bg);
1541 esl_keyhash_Destroy(kh);
1542 esl_sqfile_Close(qfp);
1543 esl_sqfile_Close(dbfp);
1544 esl_sq_Destroy(dbsq);
1545 esl_sq_Destroy(qsq);
1546 esl_stopwatch_Destroy(w);
1547 p7_builder_Destroy(bld);
1548 esl_alphabet_Destroy(abc);
1549 return eslOK;
1550 }
1551 #endif /*HMMER_MPI*/
1552
1553
1554 /* checkpoint_hmm()
1555 *
1556 * Purpose: Save <hmm> to a file <basename>-<iteration>.hmm.
1557 * If <nquery == 1>, start a new checkpoint file;
1558 * for <nquery > 1>, append to existing one.
1559 */
1560 static void
1561 checkpoint_hmm(int nquery, P7_HMM *hmm, char *basename, int iteration)
1562 {
1563 FILE *fp = NULL;
1564 char *filename = NULL;
1565
1566 esl_sprintf(&filename, "%s-%d.hmm", basename, iteration);
1567 if (nquery == 1) { if ((fp = fopen(filename, "w")) == NULL) p7_Fail("Failed to open HMM checkpoint file %s for writing\n", filename); }
1568 else { if ((fp = fopen(filename, "a")) == NULL) p7_Fail("Failed to open HMM checkpoint file %s for append\n", filename); }
1569 p7_hmmfile_WriteASCII(fp, -1, hmm);
1570
1571 fclose(fp);
1572 free(filename);
1573 return;
1574 }
1575
1576
1577 /* checkpoint_msa()
1578 *
1579 * Purpose: Save <msa> to a file <basename>-<iteration>.sto.
1580 * If <nquery == 1>, start a new checkpoint file;
1581 * for <nquery > 1>, append to existing one.
1582 */
1583 static void
1584 checkpoint_msa(int nquery, ESL_MSA *msa, char *basename, int iteration)
1585 {
1586 FILE *fp = NULL;
1587 char *filename = NULL;
1588
1589 esl_sprintf(&filename, "%s-%d.sto", basename, iteration);
1590 if (nquery == 1) { if ((fp = fopen(filename, "w")) == NULL) p7_Fail("Failed to open MSA checkpoint file %s for writing\n", filename); }
1591 else { if ((fp = fopen(filename, "a")) == NULL) p7_Fail("Failed to open MSA checkpoint file %s for append\n", filename); }
1592 esl_msafile_Write(fp, msa, eslMSAFILE_PFAM);
1593
1594 fclose(fp);
1595 free(filename);
1596 return;
1597
1598 }
1599
1600 static int
1601 serial_loop(WORKER_INFO *info, ESL_SQFILE *dbfp)
1602 {
1603 int sstatus;
1604 ESL_SQ *dbsq = NULL; /* one target sequence (digital) */
1605
1606 dbsq = esl_sq_CreateDigital(info->om->abc);
1607
1608 /* Main loop: */
1609 while ((sstatus = esl_sqio_Read(dbfp, dbsq)) == eslOK)
1610 {
1611 p7_pli_NewSeq(info->pli, dbsq);
1612 p7_bg_SetLength(info->bg, dbsq->n);
1613 p7_oprofile_ReconfigLength(info->om, dbsq->n);
1614
1615 p7_Pipeline(info->pli, info->om, info->bg, dbsq, NULL, info->th);
1616
1617 esl_sq_Reuse(dbsq);
1618 p7_pipeline_Reuse(info->pli);
1619 }
1620
1621 esl_sq_Destroy(dbsq);
1622
1623 return sstatus;
1624 }
1625
1626 #ifdef HMMER_THREADS
1627 static int
1628 thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, ESL_SQFILE *dbfp)
1629 {
1630 int status = eslOK;
1631 int sstatus = eslOK;
1632 int eofCount = 0;
1633 ESL_SQ_BLOCK *block;
1634 void *newBlock;
1635
1636 esl_workqueue_Reset(queue);
1637 esl_threads_WaitForStart(obj);
1638
1639 status = esl_workqueue_ReaderUpdate(queue, NULL, &newBlock);
1640 if (status != eslOK) p7_Fail("Work queue reader failed");
1641
1642 /* Main loop: */
1643 while (sstatus == eslOK)
1644 {
1645 block = (ESL_SQ_BLOCK *) newBlock;
1646 sstatus = esl_sqio_ReadBlock(dbfp, block, -1, -1, /*max_init_window=*/FALSE, FALSE);
1647 if (sstatus == eslEOF)
1648 {
1649 if (eofCount < esl_threads_GetWorkerCount(obj)) sstatus = eslOK;
1650 ++eofCount;
1651 }
1652
1653 if (sstatus == eslOK)
1654 {
1655 status = esl_workqueue_ReaderUpdate(queue, block, &newBlock);
1656 if (status != eslOK) p7_Fail("Work queue reader failed");
1657 }
1658 }
1659
1660 status = esl_workqueue_ReaderUpdate(queue, block, NULL);
1661 if (status != eslOK) p7_Fail("Work queue reader failed");
1662
1663 if (sstatus == eslEOF)
1664 {
1665 /* wait for all the threads to complete */
1666 esl_threads_WaitForFinish(obj);
1667 esl_workqueue_Complete(queue);
1668 }
1669
1670 return sstatus;
1671 }
1672
1673 static void
1674 pipeline_thread(void *arg)
1675 {
1676 int i;
1677 int status;
1678 int workeridx;
1679 WORKER_INFO *info;
1680 ESL_THREADS *obj;
1681
1682 ESL_SQ_BLOCK *block = NULL;
1683 void *newBlock;
1684
1685 impl_Init();
1686
1687
1688 obj = (ESL_THREADS *) arg;
1689 esl_threads_Started(obj, &workeridx);
1690
1691 info = (WORKER_INFO *) esl_threads_GetData(obj, workeridx);
1692
1693 status = esl_workqueue_WorkerUpdate(info->queue, NULL, &newBlock);
1694 if (status != eslOK) p7_Fail("Work queue worker failed");
1695
1696 /* loop until all blocks have been processed */
1697 block = (ESL_SQ_BLOCK *) newBlock;
1698 while (block->count > 0)
1699 {
1700 /* Main loop: */
1701 for (i = 0; i < block->count; ++i)
1702 {
1703 ESL_SQ *dbsq = block->list + i;
1704
1705 p7_pli_NewSeq(info->pli, dbsq);
1706 p7_bg_SetLength(info->bg, dbsq->n);
1707 p7_oprofile_ReconfigLength(info->om, dbsq->n);
1708
1709 p7_Pipeline(info->pli, info->om, info->bg, dbsq, NULL, info->th);
1710
1711 esl_sq_Reuse(dbsq);
1712 p7_pipeline_Reuse(info->pli);
1713 }
1714
1715 status = esl_workqueue_WorkerUpdate(info->queue, block, &newBlock);
1716 if (status != eslOK) p7_Fail("Work queue worker failed");
1717
1718 block = (ESL_SQ_BLOCK *) newBlock;
1719 }
1720
1721 status = esl_workqueue_WorkerUpdate(info->queue, block, NULL);
1722 if (status != eslOK) p7_Fail("Work queue worker failed");
1723
1724 esl_threads_Finished(obj, workeridx);
1725 return;
1726 }
1727 #endif /* HMMER_THREADS */
1728
1729