Debian Codegraph @

hmmer/src/hmmscan.c

raw symbols SCIP
1 /* hmmscan: search sequence(s) against a profile HMM database
2 *
3 * To do:
4 * - in MPI mode, add a check to make sure ncpus >= 2. If 1, then we
5 * only have a master, no workers. See Infernal commit r3972 on the
6 * same point; and same note in hmmsearch.c's to do list.
7 */
8 #include <p7_config.h>
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "easel.h"
15 #include "esl_alphabet.h"
16 #include "esl_getopts.h"
17 #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 ESL_SQ *qsq;
39 P7_BG *bg; /* null model */
40 P7_PIPELINE *pli; /* work pipeline */
41 P7_TOPHITS *th; /* top hit results */
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
50 #if defined (HMMER_THREADS) && defined (HMMER_MPI)
51 #define CPUOPTS "--mpi"
52 #define MPIOPTS "--cpu"
53 #else
54 #define CPUOPTS NULL
55 #define MPIOPTS NULL
56 #endif
57
58 static ESL_OPTIONS options[] = {
59 /* name type default env range toggles reqs incomp help docgroup*/
60 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
61 /* Control of output */
62 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "direct output to file <f>, not stdout", 2 },
63 { "--tblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save parseable table of per-sequence hits to file <f>", 2 },
64 { "--domtblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save parseable table of per-domain hits to file <f>", 2 },
65 { "--pfamtblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save table of hits and domains to file, in Pfam format <f>", 2 },
66 { "--acc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "prefer accessions over names in output", 2 },
67 { "--noali", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "don't output alignments, so output is smaller", 2 },
68 { "--notextw", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--textw", "unlimit ASCII text output line width", 2 },
69 { "--textw", eslARG_INT, "120", NULL, "n>=120",NULL, NULL, "--notextw", "set max width of ASCII text output lines", 2 },
70 /* Control of reporting thresholds */
71 { "-E", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, REPOPTS, "report models <= this E-value threshold in output", 4 },
72 { "-T", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, REPOPTS, "report models >= this score threshold in output", 4 },
73 { "--domE", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, DOMREPOPTS, "report domains <= this E-value threshold in output", 4 },
74 { "--domT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, DOMREPOPTS, "report domains >= this score cutoff in output", 4 },
75 /* Control of inclusion (significance) thresholds: */
76 { "--incE", eslARG_REAL, "0.01", NULL, "x>0", NULL, NULL, INCOPTS, "consider models <= this E-value threshold as significant", 5 },
77 { "--incT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCOPTS, "consider models >= this score threshold as significant", 5 },
78 { "--incdomE", eslARG_REAL, "0.01", NULL, "x>0", NULL, NULL, INCDOMOPTS, "consider domains <= this E-value threshold as significant", 5 },
79 { "--incdomT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCDOMOPTS, "consider domains >= this score threshold as significant", 5 },
80 /* Model-specific thresholding for both reporting and inclusion */
81 { "--cut_ga", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's GA gathering cutoffs to set all thresholding", 6 },
82 { "--cut_nc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's NC noise cutoffs to set all thresholding", 6 },
83 { "--cut_tc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's TC trusted cutoffs to set all thresholding", 6 },
84 /* Control of acceleration pipeline */
85 { "--max", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--F1,--F2,--F3", "Turn all heuristic filters off (less speed, more power)", 7 },
86 { "--F1", eslARG_REAL, "0.02", NULL, NULL, NULL, NULL, "--max", "MSV threshold: promote hits w/ P <= F1", 7 },
87 { "--F2", eslARG_REAL, "1e-3", NULL, NULL, NULL, NULL, "--max", "Vit threshold: promote hits w/ P <= F2", 7 },
88 { "--F3", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, "--max", "Fwd threshold: promote hits w/ P <= F3", 7 },
89 { "--nobias", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--max", "turn off composition bias filter", 7 },
90 /* Other options */
91 { "--nonull2", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "turn off biased composition score corrections", 12 },
92 { "-Z", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of comparisons done, for E-value calculation", 12 },
93 { "--domZ", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of significant seqs, for domain E-value calculation", 12 },
94 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 12 },
95 { "--qformat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert input <seqfile> is in format <s>: no autodetection", 12 },
96 #ifdef HMMER_THREADS
97 { "--cpu", eslARG_INT,"0","HMMER_NCPU","n>=0",NULL, NULL, CPUOPTS, "number of parallel CPU workers to use for multithreads", 12 }, // multithread parallelization off by default. hmmscan is i/o bound on almost all systems.
98 #endif
99 #ifdef HMMER_MPI
100 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL,"--mpi", NULL, "arrest after start: for debugging MPI under gdb", 12 },
101 { "--mpi", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, MPIOPTS, "run as an MPI parallel program", 12 },
102 #endif
103 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
104 };
105
106
107 /* struct cfg_s : "Global" application configuration shared by all threads/processes
108 *
109 * This structure is passed to routines within main.c, as a means of semi-encapsulation
110 * of shared data amongst different parallel processes (threads or MPI processes).
111 */
112 struct cfg_s {
113 char *seqfile; /* query sequence file */
114 char *hmmfile; /* database HMM file */
115
116 int do_mpi; /* TRUE if we're doing MPI parallelization */
117 int nproc; /* how many MPI processes, total */
118 int my_rank; /* who am I, in 0..nproc-1 */
119 };
120
121 static char usage[] = "[-options] <hmmdb> <seqfile>";
122 static char banner[] = "search sequence(s) against a profile database";
123
124 static int serial_master(ESL_GETOPTS *go, struct cfg_s *cfg);
125 static int serial_loop (WORKER_INFO *info, P7_HMMFILE *hfp);
126
127 #ifdef HMMER_THREADS
128 #define BLOCK_SIZE 1000
129
130 static int thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, P7_HMMFILE *hfp);
131 static void pipeline_thread(void *arg);
132 #endif
133
134 #ifdef HMMER_MPI
135 static int mpi_master (ESL_GETOPTS *go, struct cfg_s *cfg);
136 static int mpi_worker (ESL_GETOPTS *go, struct cfg_s *cfg);
137 #endif
138
139 /* process_commandline()
140 *
141 * Processes the commandline, filling in fields in <cfg> and creating and returning
142 * an <ESL_GETOPTS> options structure. The help page (hmmsearch -h) is formatted
143 * here.
144 */
145 static int
146 process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go, char **ret_hmmfile, char **ret_seqfile)
147 {
148 ESL_GETOPTS *go = esl_getopts_Create(options);
149 int status;
150
151 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; }
152 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; }
153 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; }
154
155 /* help format: */
156 if (esl_opt_GetBoolean(go, "-h") == TRUE)
157 {
158 p7_banner(stdout, argv[0], banner);
159 esl_usage(stdout, argv[0], usage);
160 if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
161 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
162
163 if (puts("\nOptions controlling output:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
164 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
165
166 if (puts("\nOptions controlling reporting thresholds:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
167 esl_opt_DisplayHelp(stdout, go, 4, 2, 80);
168
169 if (puts("\nOptions controlling inclusion (significance) thresholds:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
170 esl_opt_DisplayHelp(stdout, go, 5, 2, 80);
171
172 if (puts("\nOptions for model-specific thresholding:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
173 esl_opt_DisplayHelp(stdout, go, 6, 2, 80);
174
175 if (puts("\nOptions controlling acceleration heuristics:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
176 esl_opt_DisplayHelp(stdout, go, 7, 2, 80);
177
178 if (puts("\nOther expert options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
179 esl_opt_DisplayHelp(stdout, go, 12, 2, 80);
180 exit(0);
181 }
182
183 if (esl_opt_ArgNumber(go) != 2) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
184 if ((*ret_hmmfile = esl_opt_GetArg(go, 1)) == NULL) { if (puts("Failed to get <hmmdb> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
185 if ((*ret_seqfile = esl_opt_GetArg(go, 2)) == NULL) { if (puts("Failed to get <seqfile> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
186
187 /* Validate any attempted use of stdin streams */
188 if (strcmp(*ret_hmmfile, "-") == 0)
189 { if (puts("hmmscan cannot read <hmm database> from stdin stream, because it must have hmmpress'ed auxfiles") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
190
191 *ret_go = go;
192 return eslOK;
193
194 FAILURE: /* all errors handled here are user errors, so be polite. */
195 esl_usage(stdout, argv[0], usage);
196 if (puts("\nwhere most common options are:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
197 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
198 if (printf("\nTo see more help on available options, do %s -h\n\n", argv[0]) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
199 esl_getopts_Destroy(go);
200 exit(1);
201
202 ERROR:
203 if (go) esl_getopts_Destroy(go);
204 exit(status);
205 }
206
207
208 static int
209 output_header(FILE *ofp, ESL_GETOPTS *go, char *hmmfile, char *seqfile)
210 {
211 p7_banner(ofp, go->argv[0], banner);
212
213 if (fprintf(ofp, "# query sequence file: %s\n", seqfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
214 if (fprintf(ofp, "# target HMM database: %s\n", hmmfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
215 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");
216 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");
217 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");
218 if (esl_opt_IsUsed(go, "--pfamtblout")&& fprintf(ofp, "# pfam-style tabular hit output: %s\n", esl_opt_GetString(go, "--pfamtblout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
219 if (esl_opt_IsUsed(go, "--acc") && fprintf(ofp, "# prefer accessions over names: yes\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
220 if (esl_opt_IsUsed(go, "--noali") && fprintf(ofp, "# show alignments in output: no\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
221 if (esl_opt_IsUsed(go, "--notextw") && fprintf(ofp, "# max ASCII text line length: unlimited\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
222 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");
223 if (esl_opt_IsUsed(go, "-E") && fprintf(ofp, "# profile reporting threshold: E-value <= %g\n", esl_opt_GetReal(go, "-E")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
224 if (esl_opt_IsUsed(go, "-T") && fprintf(ofp, "# profile reporting threshold: score >= %g\n", esl_opt_GetReal(go, "-T")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
225 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");
226 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");
227 if (esl_opt_IsUsed(go, "--incE") && fprintf(ofp, "# profile inclusion threshold: E-value <= %g\n", esl_opt_GetReal(go, "--incE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
228 if (esl_opt_IsUsed(go, "--incT") && fprintf(ofp, "# profile inclusion threshold: score >= %g\n", esl_opt_GetReal(go, "--incT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
229 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");
230 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");
231 if (esl_opt_IsUsed(go, "--cut_ga") && fprintf(ofp, "# model-specific thresholding: GA cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
232 if (esl_opt_IsUsed(go, "--cut_nc") && fprintf(ofp, "# model-specific thresholding: NC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
233 if (esl_opt_IsUsed(go, "--cut_tc") && fprintf(ofp, "# model-specific thresholding: TC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
234 if (esl_opt_IsUsed(go, "--max") && fprintf(ofp, "# Max sensitivity mode: on [all heuristic filters off]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
235 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");
236 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");
237 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");
238 if (esl_opt_IsUsed(go, "--nobias") && fprintf(ofp, "# biased composition HMM filter: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
239 if (esl_opt_IsUsed(go, "--nonull2") && fprintf(ofp, "# null2 bias corrections: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
240 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");
241 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");
242 if (esl_opt_IsUsed(go, "--seed")) {
243 if (esl_opt_GetInteger(go, "--seed")==0 && fprintf(ofp, "# random number seed: one-time arbitrary\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
244 else if ( fprintf(ofp, "# random number seed set to: %d\n", esl_opt_GetInteger(go, "--seed")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
245 }
246 if (esl_opt_IsUsed(go, "--qformat") && fprintf(ofp, "# input seqfile format asserted: %s\n", esl_opt_GetString(go, "--qformat")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
247
248 #ifdef HMMER_THREADS
249 if (esl_opt_IsUsed(go, "--cpu")) {
250 if (esl_opt_GetInteger(go, "--cpu") == 0) { if (fprintf(ofp, "# multithread parallelization: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); }
251 else { if (fprintf(ofp, "# multithread parallelization: %d workers\n", esl_opt_GetInteger(go, "--cpu")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); }
252 }
253 #endif
254 #ifdef HMMER_MPI
255 if (esl_opt_IsUsed(go, "--mpi") && fprintf(ofp, "# MPI: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
256 #endif
257 if (fprintf(ofp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
258 return eslOK;
259 }
260
261
262 int
263 main(int argc, char **argv)
264 {
265 ESL_GETOPTS *go = NULL;
266 struct cfg_s cfg;
267 int status = eslOK;
268
269 impl_Init(); /* processor-specific initialization */
270 p7_FLogsumInit(); /* we're going to use table-driven Logsum() approximations at times */
271
272 /* Initialize what we can in the config structure (without knowing the alphabet yet) */
273 cfg.hmmfile = NULL;
274 cfg.seqfile = NULL;
275 cfg.do_mpi = FALSE; /* this gets reset below, if we init MPI */
276 cfg.nproc = 0; /* this gets reset below, if we init MPI */
277 cfg.my_rank = 0; /* this gets reset below, if we init MPI */
278
279 process_commandline(argc, argv, &go, &cfg.hmmfile, &cfg.seqfile);
280
281 /* Figure out who we are, and send control there:
282 * we might be an MPI master, an MPI worker, or a serial program.
283 */
284 #ifdef HMMER_MPI
285 /* pause the execution of the programs execution until the user has a
286 * chance to attach with a debugger and send a signal to resume execution
287 * i.e. (gdb) signal SIGCONT
288 */
289 if (esl_opt_GetBoolean(go, "--stall")) pause();
290
291 if (esl_opt_GetBoolean(go, "--mpi"))
292 {
293 cfg.do_mpi = TRUE;
294 MPI_Init(&argc, &argv);
295 MPI_Comm_rank(MPI_COMM_WORLD, &(cfg.my_rank));
296 MPI_Comm_size(MPI_COMM_WORLD, &(cfg.nproc));
297
298 if (cfg.my_rank > 0) status = mpi_worker(go, &cfg);
299 else status = mpi_master(go, &cfg);
300
301 MPI_Finalize();
302 }
303 else
304 #endif /*HMMER_MPI*/
305 {
306 status = serial_master(go, &cfg);
307 }
308
309 esl_getopts_Destroy(go);
310 return status;
311 }
312
313
314 /* serial_master()
315 * The serial version of hmmsearch.
316 * For each query HMM in <hmmdb> search the database for hits.
317 *
318 * A master can only return if it's successful. All errors are handled
319 * immediately and fatally with p7_Fail().
320 */
321 static int
322 serial_master(ESL_GETOPTS *go, struct cfg_s *cfg)
323 {
324 FILE *ofp = stdout; /* output file for results (default stdout) */
325 FILE *tblfp = NULL; /* output stream for tabular per-seq (--tblout) */
326 FILE *domtblfp = NULL; /* output stream for tabular per-seq (--domtblout) */
327 FILE *pfamtblfp= NULL; /* output stream for pfam tabular output (--pfamtblout) */
328 int seqfmt = eslSQFILE_UNKNOWN; /* format of seqfile */
329 ESL_SQFILE *sqfp = NULL; /* open seqfile */
330 P7_HMMFILE *hfp = NULL; /* open HMM database file */
331 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
332 P7_OPROFILE *om = NULL; /* target profile */
333 ESL_STOPWATCH *w = NULL; /* timing */
334 ESL_SQ *qsq = NULL; /* query sequence */
335 int nquery = 0;
336 int textw;
337 int status = eslOK;
338 int hstatus = eslOK;
339 int sstatus = eslOK;
340 int i;
341
342 int ncpus = 0;
343
344 int infocnt = 0;
345 WORKER_INFO *info = NULL;
346 #ifdef HMMER_THREADS
347 P7_OM_BLOCK *block = NULL;
348 ESL_THREADS *threadObj= NULL;
349 ESL_WORK_QUEUE *queue = NULL;
350 #endif
351 char errbuf[eslERRBUFSIZE];
352
353 w = esl_stopwatch_Create();
354
355 if (esl_opt_GetBoolean(go, "--notextw")) textw = 0;
356 else textw = esl_opt_GetInteger(go, "--textw");
357
358 /* If caller declared an input format, decode it */
359 if (esl_opt_IsOn(go, "--qformat")) {
360 seqfmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--qformat"));
361 if (seqfmt == eslSQFILE_UNKNOWN) p7_Fail("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--qformat"));
362 }
363
364 /* Open the target profile database to get the sequence alphabet */
365 status = p7_hmmfile_Open(cfg->hmmfile, p7_HMMDBENV, &hfp, errbuf);
366 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", cfg->hmmfile, errbuf);
367 else if (status == eslEFORMAT) p7_Fail("File format problem, trying to open HMM file %s.\n%s\n", cfg->hmmfile, errbuf);
368 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, cfg->hmmfile, errbuf);
369 if (! hfp->is_pressed) p7_Fail("Failed to open binary auxfiles for %s: use hmmpress first\n", hfp->fname);
370
371 hstatus = p7_oprofile_ReadMSV(hfp, &abc, &om);
372 if (hstatus == eslEFORMAT) p7_Fail("bad format, binary auxfiles, %s:\n%s", cfg->hmmfile, hfp->errbuf);
373 else if (hstatus == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", cfg->hmmfile);
374 else if (hstatus != eslOK) p7_Fail("Unexpected error in reading HMMs from %s", cfg->hmmfile);
375
376 p7_oprofile_Destroy(om);
377 p7_hmmfile_Close(hfp);
378
379 /* Open the query sequence database */
380 status = esl_sqfile_OpenDigital(abc, cfg->seqfile, seqfmt, NULL, &sqfp);
381 if (status == eslENOTFOUND) p7_Fail("Failed to open sequence file %s for reading\n", cfg->seqfile);
382 else if (status == eslEFORMAT) p7_Fail("Sequence file %s is empty or misformatted\n", cfg->seqfile);
383 else if (status == eslEINVAL) p7_Fail("Can't autodetect format of a stdin or .gz seqfile");
384 else if (status != eslOK) p7_Fail("Unexpected error %d opening sequence file %s\n", status, cfg->seqfile);
385 qsq = esl_sq_CreateDigital(abc);
386
387 /* Open the results output files */
388 if (esl_opt_IsOn(go, "-o")) { if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) esl_fatal("Failed to open output file %s for writing\n", esl_opt_GetString(go, "-o")); }
389 if (esl_opt_IsOn(go, "--tblout")) { if ((tblfp = fopen(esl_opt_GetString(go, "--tblout"), "w")) == NULL) esl_fatal("Failed to open tabular per-seq output file %s for writing\n", esl_opt_GetString(go, "--tblout")); }
390 if (esl_opt_IsOn(go, "--domtblout")) { if ((domtblfp = fopen(esl_opt_GetString(go, "--domtblout"), "w")) == NULL) esl_fatal("Failed to open tabular per-dom output file %s for writing\n", esl_opt_GetString(go, "--domtblout")); }
391 if (esl_opt_IsOn(go, "--pfamtblout")){ if ((pfamtblfp = fopen(esl_opt_GetString(go, "--pfamtblout"), "w")) == NULL) esl_fatal("Failed to open pfam-style tabular output file %s for writing\n", esl_opt_GetString(go, "--pfamtblout")); }
392
393 output_header(ofp, go, cfg->hmmfile, cfg->seqfile);
394
395 #ifdef HMMER_THREADS
396 /* initialize thread data */
397 ncpus = ESL_MIN( esl_opt_GetInteger(go, "--cpu"), esl_threads_GetCPUCount());
398 if (ncpus > 0)
399 {
400 threadObj = esl_threads_Create(&pipeline_thread);
401 queue = esl_workqueue_Create(ncpus * 2);
402 }
403 #endif
404
405 infocnt = (ncpus == 0) ? 1 : ncpus;
406 ESL_ALLOC(info, (ptrdiff_t) sizeof(*info) * infocnt);
407
408 for (i = 0; i < infocnt; ++i)
409 {
410 info[i].bg = p7_bg_Create(abc);
411 #ifdef HMMER_THREADS
412 info[i].queue = queue;
413 #endif
414 }
415
416 #ifdef HMMER_THREADS
417 for (i = 0; i < ncpus * 2; ++i)
418 {
419 block = p7_oprofile_CreateBlock(BLOCK_SIZE);
420 if (block == NULL) esl_fatal("Failed to allocate sequence block");
421
422 status = esl_workqueue_Init(queue, block);
423 if (status != eslOK) esl_fatal("Failed to add block to work queue");
424 }
425 #endif
426
427 /* Outside loop: over each query sequence in <seqfile>. */
428 while ((sstatus = esl_sqio_Read(sqfp, qsq)) == eslOK)
429 {
430 nquery++;
431 esl_stopwatch_Start(w);
432
433 /* Open the target profile database */
434 status = p7_hmmfile_Open(cfg->hmmfile, p7_HMMDBENV, &hfp, NULL);
435 if (status != eslOK) p7_Fail("Unexpected error %d in opening hmm file %s.\n", status, cfg->hmmfile);
436
437 #ifdef HMMER_THREADS
438 /* if we are threaded, create a lock to prevent multiple readers */
439 if (ncpus > 0)
440 {
441 status = p7_hmmfile_CreateLock(hfp);
442 if (status != eslOK) p7_Fail("Unexpected error %d creating lock\n", status);
443 }
444 #endif
445
446 if (fprintf(ofp, "Query: %s [L=%ld]\n", qsq->name, (long) qsq->n) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
447 if (qsq->acc[0] != 0 && fprintf(ofp, "Accession: %s\n", qsq->acc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
448 if (qsq->desc[0] != 0 && fprintf(ofp, "Description: %s\n", qsq->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
449
450 for (i = 0; i < infocnt; ++i)
451 {
452 /* Create processing pipeline and hit list */
453 info[i].th = p7_tophits_Create();
454 info[i].pli = p7_pipeline_Create(go, 100, 100, FALSE, p7_SCAN_MODELS); /* M_hint = 100, L_hint = 100 are just dummies for now */
455 info[i].pli->hfp = hfp; /* for two-stage input, pipeline needs <hfp> */
456
457 p7_pli_NewSeq(info[i].pli, qsq);
458 info[i].qsq = qsq;
459
460 #ifdef HMMER_THREADS
461 if (ncpus > 0) esl_threads_AddThread(threadObj, &info[i]);
462 #endif
463 }
464
465 #ifdef HMMER_THREADS
466 if (ncpus > 0) hstatus = thread_loop(threadObj, queue, hfp);
467 else hstatus = serial_loop(info, hfp);
468 #else
469 hstatus = serial_loop(info, hfp);
470 #endif
471 switch(hstatus)
472 {
473 case eslEFORMAT: p7_Fail("bad file format in HMM file %s", cfg->hmmfile); break;
474 case eslEINCOMPAT: p7_Fail("HMM file %s contains different alphabets", cfg->hmmfile); break;
475 case eslEOF: /* do nothing */ break;
476 default: p7_Fail("Unexpected error in reading HMMs from %s", cfg->hmmfile);
477 }
478
479 /* merge the results of the search results */
480 for (i = 1; i < infocnt; ++i)
481 {
482 p7_tophits_Merge(info[0].th, info[i].th);
483 p7_pipeline_Merge(info[0].pli, info[i].pli);
484
485 p7_pipeline_Destroy(info[i].pli);
486 p7_tophits_Destroy(info[i].th);
487 }
488
489 /* Print results */
490 p7_tophits_SortBySortkey(info->th);
491 p7_tophits_Threshold(info->th, info->pli);
492
493 p7_tophits_Targets(ofp, info->th, info->pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
494 p7_tophits_Domains(ofp, info->th, info->pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
495
496 if (tblfp) p7_tophits_TabularTargets(tblfp, qsq->name, qsq->acc, info->th, info->pli, (nquery == 1));
497 if (domtblfp) p7_tophits_TabularDomains(domtblfp, qsq->name, qsq->acc, info->th, info->pli, (nquery == 1));
498 if (pfamtblfp) p7_tophits_TabularXfam(pfamtblfp, qsq->name, qsq->acc, info->th, info->pli);
499
500 esl_stopwatch_Stop(w);
501 p7_pli_Statistics(ofp, info->pli, w);
502 if (fprintf(ofp, "//\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
503 fflush(ofp);
504
505 p7_hmmfile_Close(hfp);
506 p7_pipeline_Destroy(info->pli);
507 p7_tophits_Destroy(info->th);
508 esl_sq_Reuse(qsq);
509 }
510 if (sstatus == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
511 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
512 else if (sstatus != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
513 sstatus, sqfp->filename);
514
515 /* Terminate outputs - any last words?
516 */
517 if (tblfp) p7_tophits_TabularTail(tblfp, "hmmscan", p7_SCAN_MODELS, cfg->seqfile, cfg->hmmfile, go);
518 if (domtblfp) p7_tophits_TabularTail(domtblfp, "hmmscan", p7_SCAN_MODELS, cfg->seqfile, cfg->hmmfile, go);
519 if (pfamtblfp)p7_tophits_TabularTail(pfamtblfp,"hmmscan", p7_SEARCH_SEQS, cfg->seqfile, cfg->hmmfile, go);
520 if (ofp) { if (fprintf(ofp, "[ok]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); }
521
522 /* Cleanup - prepare for successful exit
523 */
524 for (i = 0; i < infocnt; ++i)
525 p7_bg_Destroy(info[i].bg);
526
527 #ifdef HMMER_THREADS
528 if (ncpus > 0)
529 {
530 esl_workqueue_Reset(queue);
531 while (esl_workqueue_Remove(queue, (void **) &block) == eslOK)
532 p7_oprofile_DestroyBlock(block);
533 esl_workqueue_Destroy(queue);
534 esl_threads_Destroy(threadObj);
535 }
536 #endif
537
538 free(info);
539
540 esl_sq_Destroy(qsq);
541 esl_stopwatch_Destroy(w);
542 esl_alphabet_Destroy(abc);
543 esl_sqfile_Close(sqfp);
544
545 if (ofp != stdout) fclose(ofp);
546 if (tblfp) fclose(tblfp);
547 if (domtblfp) fclose(domtblfp);
548 if (pfamtblfp) fclose(pfamtblfp);
549 return eslOK;
550
551 ERROR:
552 return status;
553 }
554
555 #ifdef HMMER_MPI
556
557 /* Define common tags used by the MPI master/slave processes */
558 #define HMMER_ERROR_TAG 1
559 #define HMMER_HMM_TAG 2
560 #define HMMER_SEQUENCE_TAG 3
561 #define HMMER_BLOCK_TAG 4
562 #define HMMER_PIPELINE_TAG 5
563 #define HMMER_TOPHITS_TAG 6
564 #define HMMER_HIT_TAG 7
565 #define HMMER_TERMINATING_TAG 8
566 #define HMMER_READY_TAG 9
567
568 /* mpi_failure()
569 * Generate an error message. If the clients rank is not 0, a
570 * message is created with the error message and sent to the
571 * master process for handling.
572 */
573 static void
574 mpi_failure(char *format, ...)
575 {
576 va_list argp;
577 int status = eslFAIL;
578 int len;
579 int rank;
580 char str[512];
581
582 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
583
584 /* format the error mesg */
585 va_start(argp, format);
586 len = vsnprintf(str, sizeof(str), format, argp);
587 va_end(argp);
588
589 /* make sure the error string is terminated */
590 str[sizeof(str)-1] = '\0';
591
592 /* if the caller is the master, print the results and abort */
593 if (rank == 0)
594 {
595 if (fprintf(stderr, "\nError: ") < 0) exit(eslEWRITE);
596 if (fprintf(stderr, "%s", str) < 0) exit(eslEWRITE);
597 if (fprintf(stderr, "\n") < 0) exit(eslEWRITE);
598 fflush(stderr);
599
600 MPI_Abort(MPI_COMM_WORLD, status);
601 exit(1);
602 }
603 else
604 {
605 MPI_Send(str, len, MPI_CHAR, 0, HMMER_ERROR_TAG, MPI_COMM_WORLD);
606 pause();
607 }
608 }
609
610 #define MAX_BLOCK_SIZE (512*1024)
611
612 typedef struct {
613 uint64_t offset;
614 uint64_t length;
615 uint64_t count;
616 } MSV_BLOCK;
617
618 typedef struct {
619 int complete;
620 int size;
621 int current;
622 int last;
623 MSV_BLOCK *blocks;
624 } BLOCK_LIST;
625
626 /* this routine parses the database keeping track of the blocks
627 * offset within the file, number of sequences and the length
628 * of the block. These blocks are passed as work units to the
629 * MPI workers. If multiple hmm's are in the query file, the
630 * blocks are reused without parsing the database a second time.
631 */
632 int next_block(P7_HMMFILE *hfp, BLOCK_LIST *list, MSV_BLOCK *block)
633 {
634 P7_OPROFILE *om = NULL;
635 ESL_ALPHABET *abc = NULL;
636 int status = eslOK;
637
638 /* if the list has been calculated, use it instead of parsing the database */
639 if (list->complete)
640 {
641 if (list->current == list->last)
642 {
643 block->offset = 0;
644 block->length = 0;
645 block->count = 0;
646
647 status = eslEOF;
648 }
649 else
650 {
651 int inx = list->current++;
652
653 block->offset = list->blocks[inx].offset;
654 block->length = list->blocks[inx].length;
655 block->count = list->blocks[inx].count;
656
657 status = eslOK;
658 }
659
660 return status;
661 }
662
663 block->offset = 0;
664 block->length = 0;
665 block->count = 0;
666
667 while (block->length < MAX_BLOCK_SIZE && (status = p7_oprofile_ReadInfoMSV(hfp, &abc, &om)) == eslOK)
668 {
669 if (block->count == 0) block->offset = om->roff;
670 block->length = om->eoff - block->offset + 1;
671 block->count++;
672 p7_oprofile_Destroy(om);
673 }
674
675 if (status == eslEOF && block->count > 0) status = eslOK;
676 if (status == eslEOF) list->complete = 1;
677
678 /* add the block to the list of known blocks */
679 if (status == eslOK)
680 {
681 int inx;
682
683 if (list->last >= list->size)
684 {
685 void *tmp;
686 list->size += 500;
687 ESL_RALLOC(list->blocks, tmp, sizeof(MSV_BLOCK) * list->size);
688 }
689
690 inx = list->last++;
691 list->blocks[inx].offset = block->offset;
692 list->blocks[inx].length = block->length;
693 list->blocks[inx].count = block->count;
694 }
695
696 return status;
697
698 ERROR:
699 return eslEMEM;
700 }
701
702 /* mpi_master()
703 * The MPI version of hmmscan
704 * Follows standard pattern for a master/worker load-balanced MPI program (J1/78-79).
705 *
706 * A master can only return if it's successful.
707 * Errors in an MPI master come in two classes: recoverable and nonrecoverable.
708 *
709 * Recoverable errors include all worker-side errors, and any
710 * master-side error that do not affect MPI communication. Error
711 * messages from recoverable messages are delayed until we've cleanly
712 * shut down the workers.
713 *
714 * Unrecoverable errors are master-side errors that may affect MPI
715 * communication, meaning we cannot count on being able to reach the
716 * workers and shut them down. Unrecoverable errors result in immediate
717 * p7_Fail()'s, which will cause MPI to shut down the worker processes
718 * uncleanly.
719 */
720 static int
721 mpi_master(ESL_GETOPTS *go, struct cfg_s *cfg)
722 {
723 FILE *ofp = stdout; /* output file for results (default stdout) */
724 FILE *tblfp = NULL; /* output stream for tabular per-seq (--tblout) */
725 FILE *domtblfp = NULL; /* output stream for tabular per-seq (--domtblout) */
726 FILE *pfamtblfp= NULL; /* output stream for pfam-style tabular output (--pfamtblout) */
727 int seqfmt = eslSQFILE_UNKNOWN; /* format of seqfile */
728 P7_BG *bg = NULL; /* null model */
729 ESL_SQFILE *sqfp = NULL; /* open seqfile */
730 P7_HMMFILE *hfp = NULL; /* open HMM database file */
731 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
732 P7_OPROFILE *om = NULL; /* target profile */
733 ESL_STOPWATCH *w = NULL; /* timing */
734 ESL_SQ *qsq = NULL; /* query sequence */
735 int nquery = 0;
736 int textw;
737 int status = eslOK;
738 int hstatus = eslOK;
739 int sstatus = eslOK;
740 int dest;
741
742 char *mpi_buf = NULL; /* buffer used to pack/unpack structures */
743 int mpi_size = 0; /* size of the allocated buffer */
744 BLOCK_LIST *list = NULL;
745 MSV_BLOCK block;
746
747 int i;
748 int size;
749 MPI_Status mpistatus;
750 char errbuf[eslERRBUFSIZE];
751
752 w = esl_stopwatch_Create();
753
754 if (esl_opt_GetBoolean(go, "--notextw")) textw = 0;
755 else textw = esl_opt_GetInteger(go, "--textw");
756
757 /* If caller declared an input format, decode it */
758 if (esl_opt_IsOn(go, "--qformat")) {
759 seqfmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--qformat"));
760 if (seqfmt == eslSQFILE_UNKNOWN) mpi_failure("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--qformat"));
761 }
762
763 /* Open the target profile database to get the sequence alphabet */
764 status = p7_hmmfile_Open(cfg->hmmfile, p7_HMMDBENV, &hfp, errbuf);
765 if (status == eslENOTFOUND) mpi_failure("File existence/permissions problem in trying to open HMM file %s.\n%s\n", cfg->hmmfile, errbuf);
766 else if (status == eslEFORMAT) mpi_failure("File format problem, trying to open HMM file %s.\n%s\n", cfg->hmmfile, errbuf);
767 else if (status != eslOK) mpi_failure("Unexpected error %d in opening HMM file %s.\n%s\n", status, cfg->hmmfile, errbuf);
768 if (! hfp->is_pressed) mpi_failure("Failed to open binary auxfiles for %s: use hmmpress first\n", hfp->fname);
769
770 hstatus = p7_oprofile_ReadMSV(hfp, &abc, &om);
771 if (hstatus == eslEFORMAT) mpi_failure("bad format, binary auxfiles, %s", cfg->hmmfile);
772 else if (hstatus == eslEINCOMPAT) mpi_failure("HMM file %s contains different alphabets", cfg->hmmfile);
773 else if (hstatus != eslOK) mpi_failure("Unexpected error in reading HMMs from %s", cfg->hmmfile);
774
775 p7_oprofile_Destroy(om);
776 p7_hmmfile_Close(hfp);
777
778 /* Open the query sequence database */
779 status = esl_sqfile_OpenDigital(abc, cfg->seqfile, seqfmt, NULL, &sqfp);
780 if (status == eslENOTFOUND) mpi_failure("Failed to open sequence file %s for reading\n", cfg->seqfile);
781 else if (status == eslEFORMAT) mpi_failure("Sequence file %s is empty or misformatted\n", cfg->seqfile);
782 else if (status == eslEINVAL) mpi_failure("Can't autodetect format of a stdin or .gz seqfile");
783 else if (status != eslOK) mpi_failure("Unexpected error %d opening sequence file %s\n", status, cfg->seqfile);
784
785 /* Open the results output files */
786 if (esl_opt_IsOn(go, "-o") && (ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
787 mpi_failure("Failed to open output file %s for writing\n", esl_opt_GetString(go, "-o"));
788 if (esl_opt_IsOn(go, "--tblout") && (tblfp = fopen(esl_opt_GetString(go, "--tblout"), "w")) == NULL)
789 mpi_failure("Failed to open tabular per-seq output file %s for writing\n", esl_opt_GetString(go, "--tblfp"));
790 if (esl_opt_IsOn(go, "--domtblout") && (domtblfp = fopen(esl_opt_GetString(go, "--domtblout"), "w")) == NULL)
791 mpi_failure("Failed to open tabular per-dom output file %s for writing\n", esl_opt_GetString(go, "--domtblfp"));
792 if (esl_opt_IsOn(go, "--pfamtblout") && (pfamtblfp = fopen(esl_opt_GetString(go, "--pfamtblout"), "w")) == NULL)
793 mpi_failure("Failed to open pfam-style tabular output file %s for writing\n", esl_opt_GetString(go, "--pfamtblout"));
794
795 ESL_ALLOC(list, sizeof(MSV_BLOCK));
796 list->complete = 0;
797 list->size = 0;
798 list->current = 0;
799 list->last = 0;
800 list->blocks = NULL;
801
802 output_header(ofp, go, cfg->hmmfile, cfg->seqfile);
803 qsq = esl_sq_CreateDigital(abc);
804 bg = p7_bg_Create(abc);
805
806 /* Outside loop: over each query sequence in <seqfile>. */
807 while ((sstatus = esl_sqio_Read(sqfp, qsq)) == eslOK)
808 {
809 P7_PIPELINE *pli = NULL; /* processing pipeline */
810 P7_TOPHITS *th = NULL; /* top-scoring sequence hits */
811
812 nquery++;
813
814 esl_stopwatch_Start(w);
815
816 /* seqfile may need to be rewound (multiquery mode) */
817 if (nquery > 1) list->current = 0;
818
819 /* Open the target profile database */
820 status = p7_hmmfile_Open(cfg->hmmfile, p7_HMMDBENV, &hfp, NULL);
821 if (status != eslOK) mpi_failure("Unexpected error %d in opening hmm file %s.\n", status, cfg->hmmfile);
822
823 if (fprintf(ofp, "Query: %s [L=%ld]\n", qsq->name, (long) qsq->n) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
824 if (qsq->acc[0] != 0 && fprintf(ofp, "Accession: %s\n", qsq->acc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
825 if (qsq->desc[0] != 0 && fprintf(ofp, "Description: %s\n", qsq->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
826
827 /* Create processing pipeline and hit list */
828 th = p7_tophits_Create();
829 pli = p7_pipeline_Create(go, 100, 100, FALSE, p7_SCAN_MODELS); /* M_hint = 100, L_hint = 100 are just dummies for now */
830 pli->hfp = hfp; /* for two-stage input, pipeline needs <hfp> */
831
832 p7_pli_NewSeq(pli, qsq);
833
834 /* Main loop: */
835 while ((hstatus = next_block(hfp, list, &block)) == eslOK)
836 {
837 if (MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpistatus) != 0)
838 mpi_failure("MPI error %d receiving message from %d\n", mpistatus.MPI_SOURCE);
839
840 MPI_Get_count(&mpistatus, MPI_PACKED, &size);
841 if (mpi_buf == NULL || size > mpi_size) {
842 void *tmp;
843 ESL_RALLOC(mpi_buf, tmp, sizeof(char) * size);
844 mpi_size = size;
845 }
846
847 dest = mpistatus.MPI_SOURCE;
848 MPI_Recv(mpi_buf, size, MPI_PACKED, dest, mpistatus.MPI_TAG, MPI_COMM_WORLD, &mpistatus);
849
850 if (mpistatus.MPI_TAG == HMMER_ERROR_TAG)
851 mpi_failure("MPI client %d raised error:\n%s\n", dest, mpi_buf);
852 if (mpistatus.MPI_TAG != HMMER_READY_TAG)
853 mpi_failure("Unexpected tag %d from %d\n", mpistatus.MPI_TAG, dest);
854
855 MPI_Send(&block, 3, MPI_LONG_LONG_INT, dest, HMMER_BLOCK_TAG, MPI_COMM_WORLD);
856 }
857 switch(hstatus)
858 {
859 case eslEFORMAT:
860 mpi_failure("bad file format in HMM file %s", cfg->hmmfile);
861 break;
862 case eslEINCOMPAT:
863 mpi_failure("HMM file %s contains different alphabets", cfg->hmmfile);
864 break;
865 case eslEOF:
866 /* do nothing */
867 break;
868 default:
869 mpi_failure("Unexpected error %d in reading HMMs from %s", hstatus, cfg->hmmfile);
870 }
871
872 block.offset = 0;
873 block.length = 0;
874 block.count = 0;
875
876 /* wait for all workers to finish up their work blocks */
877 for (i = 1; i < cfg->nproc; ++i)
878 {
879 if (MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpistatus) != 0)
880 mpi_failure("MPI error %d receiving message from %d\n", mpistatus.MPI_SOURCE);
881
882 MPI_Get_count(&mpistatus, MPI_PACKED, &size);
883 if (mpi_buf == NULL || size > mpi_size) {
884 void *tmp;
885 ESL_RALLOC(mpi_buf, tmp, sizeof(char) * size);
886 mpi_size = size;
887 }
888
889 dest = mpistatus.MPI_SOURCE;
890 MPI_Recv(mpi_buf, size, MPI_PACKED, dest, mpistatus.MPI_TAG, MPI_COMM_WORLD, &mpistatus);
891
892 if (mpistatus.MPI_TAG == HMMER_ERROR_TAG)
893 mpi_failure("MPI client %d raised error:\n%s\n", dest, mpi_buf);
894 if (mpistatus.MPI_TAG != HMMER_READY_TAG)
895 mpi_failure("Unexpected tag %d from %d\n", mpistatus.MPI_TAG, dest);
896 }
897
898 /* merge the results of the search results */
899 for (dest = 1; dest < cfg->nproc; ++dest)
900 {
901 P7_PIPELINE *mpi_pli = NULL;
902 P7_TOPHITS *mpi_th = NULL;
903
904 /* send an empty block to signal the worker they are done */
905 MPI_Send(&block, 3, MPI_LONG_LONG_INT, dest, HMMER_BLOCK_TAG, MPI_COMM_WORLD);
906
907 /* wait for the results */
908 if ((status = p7_tophits_MPIRecv(dest, HMMER_TOPHITS_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size, &mpi_th)) != eslOK)
909 mpi_failure("Unexpected error %d receiving tophits from %d", status, dest);
910
911 if ((status = p7_pipeline_MPIRecv(dest, HMMER_PIPELINE_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size, go, &mpi_pli)) != eslOK)
912 mpi_failure("Unexpected error %d receiving pipeline from %d", status, dest);
913
914 p7_tophits_Merge(th, mpi_th);
915 p7_pipeline_Merge(pli, mpi_pli);
916
917 p7_pipeline_Destroy(mpi_pli);
918 p7_tophits_Destroy(mpi_th);
919 }
920
921 /* Print the results. */
922 p7_tophits_SortBySortkey(th);
923 p7_tophits_Threshold(th, pli);
924 p7_tophits_Targets(ofp, th, pli, textw); fprintf(ofp, "\n\n");
925 p7_tophits_Domains(ofp, th, pli, textw); fprintf(ofp, "\n\n");
926
927 if (tblfp) p7_tophits_TabularTargets(tblfp, qsq->name, qsq->acc, th, pli, (nquery == 1));
928 if (domtblfp) p7_tophits_TabularDomains(domtblfp, qsq->name, qsq->acc, th, pli, (nquery == 1));
929 if (pfamtblfp) p7_tophits_TabularXfam(pfamtblfp, qsq->name, qsq->acc, th, pli);
930
931 esl_stopwatch_Stop(w);
932 p7_pli_Statistics(ofp, pli, w);
933 if (fprintf(ofp, "//\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
934
935 p7_hmmfile_Close(hfp);
936 p7_pipeline_Destroy(pli);
937 p7_tophits_Destroy(th);
938 esl_sq_Reuse(qsq);
939 }
940 if (sstatus == eslEFORMAT)
941 mpi_failure("Parse failed (sequence file %s):\n%s\n", sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
942 else if (sstatus != eslEOF)
943 mpi_failure("Unexpected error %d reading sequence file %s", sstatus, sqfp->filename);
944
945 /* monitor all the workers to make sure they have ended */
946 for (i = 1; i < cfg->nproc; ++i)
947 {
948 if (MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpistatus) != 0)
949 mpi_failure("MPI error %d receiving message from %d\n", mpistatus.MPI_SOURCE);
950
951 MPI_Get_count(&mpistatus, MPI_PACKED, &size);
952 if (mpi_buf == NULL || size > mpi_size) {
953 void *tmp;
954 ESL_RALLOC(mpi_buf, tmp, sizeof(char) * size);
955 mpi_size = size;
956 }
957
958 dest = mpistatus.MPI_SOURCE;
959 MPI_Recv(mpi_buf, size, MPI_PACKED, dest, mpistatus.MPI_TAG, MPI_COMM_WORLD, &mpistatus);
960
961 if (mpistatus.MPI_TAG == HMMER_ERROR_TAG)
962 mpi_failure("MPI client %d raised error:\n%s\n", dest, mpi_buf);
963 if (mpistatus.MPI_TAG != HMMER_TERMINATING_TAG)
964 mpi_failure("Unexpected tag %d from %d\n", mpistatus.MPI_TAG, dest);
965 }
966
967 /* Terminate outputs - any last words?
968 */
969 if (tblfp) p7_tophits_TabularTail(tblfp, "hmmscan", p7_SCAN_MODELS, cfg->seqfile, cfg->hmmfile, go);
970 if (domtblfp) p7_tophits_TabularTail(domtblfp, "hmmscan", p7_SCAN_MODELS, cfg->seqfile, cfg->hmmfile, go);
971 if (pfamtblfp)p7_tophits_TabularTail(pfamtblfp, "hmmscan", p7_SEARCH_SEQS, cfg->seqfile, cfg->hmmfile, go);
972 if (ofp) { if (fprintf(ofp, "[ok]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); }
973
974 /* Cleanup - prepare for successful exit
975 */
976 free(list);
977 if (mpi_buf != NULL) free(mpi_buf);
978
979 p7_bg_Destroy(bg);
980 esl_sq_Destroy(qsq);
981 esl_stopwatch_Destroy(w);
982 esl_alphabet_Destroy(abc);
983 esl_sqfile_Close(sqfp);
984
985 if (ofp != stdout) fclose(ofp);
986 if (tblfp) fclose(tblfp);
987 if (domtblfp) fclose(domtblfp);
988 if (pfamtblfp) fclose(pfamtblfp);
989
990 return eslOK;
991
992 ERROR:
993 return status;
994 }
995
996
997 static int
998 mpi_worker(ESL_GETOPTS *go, struct cfg_s *cfg)
999 {
1000 int seqfmt = eslSQFILE_UNKNOWN; /* format of seqfile */
1001 P7_BG *bg = NULL; /* null model */
1002 ESL_SQFILE *sqfp = NULL; /* open seqfile */
1003 P7_HMMFILE *hfp = NULL; /* open HMM database file */
1004 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
1005 P7_OPROFILE *om = NULL; /* target profile */
1006 ESL_STOPWATCH *w = NULL; /* timing */
1007 ESL_SQ *qsq = NULL; /* query sequence */
1008 int status = eslOK;
1009 int hstatus = eslOK;
1010 int sstatus = eslOK;
1011
1012 char *mpi_buf = NULL; /* buffer used to pack/unpack structures */
1013 int mpi_size = 0; /* size of the allocated buffer */
1014
1015 MPI_Status mpistatus;
1016 char errbuf[eslERRBUFSIZE];
1017
1018 w = esl_stopwatch_Create();
1019
1020 /* Open the target profile database to get the sequence alphabet */
1021 status = p7_hmmfile_Open(cfg->hmmfile, p7_HMMDBENV, &hfp, errbuf);
1022 if (status == eslENOTFOUND) mpi_failure("File existence/permissions problem in trying to open HMM file %s.\n%s\n", cfg->hmmfile, errbuf);
1023 else if (status == eslEFORMAT) mpi_failure("File format problem in trying to open HMM file %s.\n%s\n", cfg->hmmfile, errbuf);
1024 else if (status != eslOK) mpi_failure("Unexpected error %d in opening HMM file %s.\n%s\n", status, cfg->hmmfile, errbuf);
1025 if (! hfp->is_pressed) mpi_failure("Failed to open binary dbs for HMM file %s: use hmmpress first\n", hfp->fname);
1026
1027 hstatus = p7_oprofile_ReadMSV(hfp, &abc, &om);
1028 if (hstatus == eslEFORMAT) mpi_failure("bad file format in HMM file %s", cfg->hmmfile);
1029 else if (hstatus == eslEINCOMPAT) mpi_failure("HMM file %s contains different alphabets", cfg->hmmfile);
1030 else if (hstatus != eslOK) mpi_failure("Unexpected error in reading HMMs from %s", cfg->hmmfile);
1031
1032 p7_oprofile_Destroy(om);
1033 p7_hmmfile_Close(hfp);
1034
1035 /* Open the query sequence database */
1036 status = esl_sqfile_OpenDigital(abc, cfg->seqfile, seqfmt, NULL, &sqfp);
1037 if (status == eslENOTFOUND) mpi_failure("Failed to open sequence file %s for reading\n", cfg->seqfile);
1038 else if (status == eslEFORMAT) mpi_failure("Sequence file %s is empty or misformatted\n", cfg->seqfile);
1039 else if (status == eslEINVAL) mpi_failure("Can't autodetect format of a stdin or .gz seqfile");
1040 else if (status != eslOK) mpi_failure("Unexpected error %d opening sequence file %s\n", status, cfg->seqfile);
1041
1042 qsq = esl_sq_CreateDigital(abc);
1043 bg = p7_bg_Create(abc);
1044
1045 /* Outside loop: over each query sequence in <seqfile>. */
1046 while ((sstatus = esl_sqio_Read(sqfp, qsq)) == eslOK)
1047 {
1048 P7_PIPELINE *pli = NULL; /* processing pipeline */
1049 P7_TOPHITS *th = NULL; /* top-scoring sequence hits */
1050
1051 MSV_BLOCK block;
1052
1053 esl_stopwatch_Start(w);
1054
1055 status = 0;
1056 MPI_Send(&status, 1, MPI_INT, 0, HMMER_READY_TAG, MPI_COMM_WORLD);
1057
1058 /* Open the target profile database */
1059 status = p7_hmmfile_Open(cfg->hmmfile, p7_HMMDBENV, &hfp, NULL);
1060 if (status != eslOK) mpi_failure("Unexpected error %d in opening hmm file %s.\n", status, cfg->hmmfile);
1061
1062 /* Create processing pipeline and hit list */
1063 th = p7_tophits_Create();
1064 pli = p7_pipeline_Create(go, 100, 100, FALSE, p7_SCAN_MODELS); /* M_hint = 100, L_hint = 100 are just dummies for now */
1065 pli->hfp = hfp; /* for two-stage input, pipeline needs <hfp> */
1066
1067 p7_pli_NewSeq(pli, qsq);
1068
1069 /* receive a sequence block from the master */
1070 MPI_Recv(&block, 3, MPI_LONG_LONG_INT, 0, HMMER_BLOCK_TAG, MPI_COMM_WORLD, &mpistatus);
1071 while (block.count > 0)
1072 {
1073 uint64_t length = 0;
1074 uint64_t count = block.count;
1075
1076 hstatus = p7_oprofile_Position(hfp, block.offset);
1077 if (hstatus != eslOK) mpi_failure("Cannot position optimized model to %ld\n", block.offset);
1078
1079 while (count > 0 && (hstatus = p7_oprofile_ReadMSV(hfp, &abc, &om)) == eslOK)
1080 {
1081 length = om->eoff - block.offset + 1;
1082
1083 p7_pli_NewModel(pli, om, bg);
1084 p7_bg_SetLength(bg, qsq->n);
1085 p7_oprofile_ReconfigLength(om, qsq->n);
1086
1087 p7_Pipeline(pli, om, bg, qsq, NULL, th);
1088
1089 p7_oprofile_Destroy(om);
1090 p7_pipeline_Reuse(pli);
1091
1092 --count;
1093 }
1094
1095 /* check the status of reading the hmm */
1096
1097 /* lets do a little bit of sanity checking here to make sure the blocks are the same */
1098 if (count > 0)
1099 {
1100 switch(hstatus)
1101 {
1102 case eslEFORMAT:
1103 mpi_failure("bad file format in HMM file %s", cfg->hmmfile);
1104 break;
1105 case eslEINCOMPAT:
1106 mpi_failure("HMM file %s contains different alphabets", cfg->hmmfile);
1107 break;
1108 case eslOK:
1109 case eslEOF:
1110 mpi_failure("Block count mismatch - expected %ld found %ld at offset %ld\n", block.count, block.count-count, block.offset);
1111 break;
1112 default:
1113 mpi_failure("Unexpected error %d in reading HMMs from %s", hstatus, cfg->hmmfile);
1114 }
1115 }
1116 if (block.length != length)
1117 mpi_failure("Block length mismatch - expected %ld found %ld at offset %ld\n", block.length, length, block.offset);
1118
1119 /* inform the master we need another block of sequences */
1120 status = 0;
1121 MPI_Send(&status, 1, MPI_INT, 0, HMMER_READY_TAG, MPI_COMM_WORLD);
1122
1123 /* wait for the next block of sequences */
1124 MPI_Recv(&block, 3, MPI_LONG_LONG_INT, 0, HMMER_BLOCK_TAG, MPI_COMM_WORLD, &mpistatus);
1125 }
1126
1127 esl_stopwatch_Stop(w);
1128
1129 /* Send the top hits back to the master. */
1130 p7_tophits_MPISend(th, 0, HMMER_TOPHITS_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size);
1131 p7_pipeline_MPISend(pli, 0, HMMER_PIPELINE_TAG, MPI_COMM_WORLD, &mpi_buf, &mpi_size);
1132
1133 p7_hmmfile_Close(hfp);
1134 p7_pipeline_Destroy(pli);
1135 p7_tophits_Destroy(th);
1136 esl_sq_Reuse(qsq);
1137 } /* end outer loop over query HMMs */
1138 if (sstatus == eslEFORMAT)
1139 mpi_failure("Parse failed (sequence file %s):\n%s\n", sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
1140 else if (sstatus != eslEOF)
1141 mpi_failure("Unexpected error %d reading sequence file %s", sstatus, sqfp->filename);
1142
1143 status = 0;
1144 MPI_Send(&status, 1, MPI_INT, 0, HMMER_TERMINATING_TAG, MPI_COMM_WORLD);
1145
1146 if (mpi_buf != NULL) free(mpi_buf);
1147
1148 p7_bg_Destroy(bg);
1149
1150 esl_sq_Destroy(qsq);
1151 esl_stopwatch_Destroy(w);
1152 esl_alphabet_Destroy(abc);
1153 esl_sqfile_Close(sqfp);
1154
1155 return eslOK;
1156 }
1157 #endif /*HMMER_MPI*/
1158
1159 static int
1160 serial_loop(WORKER_INFO *info, P7_HMMFILE *hfp)
1161 {
1162 int status;
1163
1164 P7_OPROFILE *om;
1165 ESL_ALPHABET *abc = NULL;
1166 /* Main loop: */
1167 while ((status = p7_oprofile_ReadMSV(hfp, &abc, &om)) == eslOK)
1168 {
1169 p7_pli_NewModel(info->pli, om, info->bg);
1170 p7_bg_SetLength(info->bg, info->qsq->n);
1171 p7_oprofile_ReconfigLength(om, info->qsq->n);
1172
1173 status = p7_Pipeline(info->pli, om, info->bg, info->qsq, NULL, info->th);
1174 if (status == eslEINVAL) p7_Fail(info->pli->errbuf);
1175
1176 p7_oprofile_Destroy(om);
1177 p7_pipeline_Reuse(info->pli);
1178 }
1179
1180 esl_alphabet_Destroy(abc);
1181
1182 return status;
1183 }
1184
1185 #ifdef HMMER_THREADS
1186 static int
1187 thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, P7_HMMFILE *hfp)
1188 {
1189 int status = eslOK;
1190 int sstatus = eslOK;
1191 int eofCount = 0;
1192 P7_OM_BLOCK *block;
1193 ESL_ALPHABET *abc = NULL;
1194 void *newBlock;
1195
1196 esl_workqueue_Reset(queue);
1197 esl_threads_WaitForStart(obj);
1198
1199 status = esl_workqueue_ReaderUpdate(queue, NULL, &newBlock);
1200 if (status != eslOK) esl_fatal("Work queue reader failed");
1201
1202 /* Main loop: */
1203 while (sstatus == eslOK)
1204 {
1205 block = (P7_OM_BLOCK *) newBlock;
1206 sstatus = p7_oprofile_ReadBlockMSV(hfp, &abc, block);
1207 if (sstatus == eslEOF)
1208 {
1209 if (eofCount < esl_threads_GetWorkerCount(obj)) sstatus = eslOK;
1210 ++eofCount;
1211 }
1212
1213 if (sstatus == eslOK)
1214 {
1215 status = esl_workqueue_ReaderUpdate(queue, block, &newBlock);
1216 if (status != eslOK) esl_fatal("Work queue reader failed");
1217 }
1218 }
1219
1220 status = esl_workqueue_ReaderUpdate(queue, block, NULL);
1221 if (status != eslOK) esl_fatal("Work queue reader failed");
1222
1223 if (sstatus == eslEOF)
1224 {
1225 /* wait for all the threads to complete */
1226 esl_threads_WaitForFinish(obj);
1227 esl_workqueue_Complete(queue);
1228 }
1229
1230 esl_alphabet_Destroy(abc);
1231 return sstatus;
1232 }
1233
1234 static void
1235 pipeline_thread(void *arg)
1236 {
1237 int i;
1238 int status;
1239 int workeridx;
1240 WORKER_INFO *info;
1241 ESL_THREADS *obj;
1242 P7_OM_BLOCK *block;
1243 void *newBlock;
1244
1245 impl_Init();
1246
1247 obj = (ESL_THREADS *) arg;
1248 esl_threads_Started(obj, &workeridx);
1249
1250 info = (WORKER_INFO *) esl_threads_GetData(obj, workeridx);
1251
1252 status = esl_workqueue_WorkerUpdate(info->queue, NULL, &newBlock);
1253 if (status != eslOK) esl_fatal("Work queue worker failed");
1254
1255 /* loop until all blocks have been processed */
1256 block = (P7_OM_BLOCK *) newBlock;
1257 while (block->count > 0)
1258 {
1259 /* Main loop: */
1260 for (i = 0; i < block->count; ++i)
1261 {
1262 P7_OPROFILE *om = block->list[i];
1263
1264 p7_pli_NewModel(info->pli, om, info->bg);
1265 p7_bg_SetLength(info->bg, info->qsq->n);
1266 p7_oprofile_ReconfigLength(om, info->qsq->n);
1267
1268 status = p7_Pipeline(info->pli, om, info->bg, info->qsq, NULL, info->th);
1269 if (status == eslEINVAL) p7_Fail(info->pli->errbuf);
1270
1271 p7_oprofile_Destroy(om);
1272 p7_pipeline_Reuse(info->pli);
1273
1274 block->list[i] = NULL;
1275 }
1276
1277 status = esl_workqueue_WorkerUpdate(info->queue, block, &newBlock);
1278 if (status != eslOK) esl_fatal("Work queue worker failed");
1279
1280 block = (P7_OM_BLOCK *) newBlock;
1281 }
1282
1283 status = esl_workqueue_WorkerUpdate(info->queue, block, NULL);
1284 if (status != eslOK) esl_fatal("Work queue worker failed");
1285
1286 esl_threads_Finished(obj, workeridx);
1287 return;
1288 }
1289 #endif /* HMMER_THREADS */
1290
1291
1292