Debian Codegraph @

hmmer/src/nhmmer.c

raw symbols SCIP
1 /* nhmmer: search profile HMM(s) against a nucleotide sequence 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_msa.h"
14 #include "esl_msafile.h"
15 #include "esl_scorematrix.h"
16 #include "esl_sq.h"
17 #include "esl_sqio.h"
18 #include "esl_stopwatch.h"
19
20
21
22 #ifdef HMMER_THREADS
23 #include <unistd.h>
24 #include "esl_threads.h"
25 #include "esl_workqueue.h"
26 #endif /*HMMER_THREADS*/
27
28 #include "hmmer.h"
29
30 /* set the max residue count to 1/4 meg when reading a block */
31 #define NHMMER_MAX_RESIDUE_COUNT (1024 * 256) /* 1/4 Mb */
32
33 typedef struct {
34 #ifdef HMMER_THREADS
35 ESL_WORK_QUEUE *queue;
36 #endif /*HMMER_THREADS*/
37 P7_BG *bg; /* null model */
38 P7_PIPELINE *pli; /* work pipeline */
39 P7_TOPHITS *th; /* top hit results */
40 P7_OPROFILE *om; /* optimized query profile */
41 FM_CFG *fm_cfg; /* global data for FM-index for fast SSV */
42 P7_SCOREDATA *scoredata; /* hmm-specific data used by nhmmer */
43 } WORKER_INFO;
44
45 typedef struct {
46 FM_DATA *fmf;
47 FM_DATA *fmb;
48 int active; //TRUE is worker is supposed to work on the contents, FALSE otherwise
49 } FM_THREAD_INFO;
50
51
52 typedef struct {
53 int id; /* internal sequence ID */
54 int length; /* length of sequence */
55 } ID_LENGTH;
56
57 typedef struct {
58 ID_LENGTH *id_lengths;
59 int count;
60 int size;
61 } ID_LENGTH_LIST;
62
63
64 static ID_LENGTH_LIST* init_id_length( int size );
65 static void destroy_id_length( ID_LENGTH_LIST *list );
66 static int add_id_length(ID_LENGTH_LIST *list, int id, int L);
67 static int assign_Lengths(P7_TOPHITS *th, ID_LENGTH_LIST *id_length_list);
68
69 #define REPOPTS "-E,-T,--cut_ga,--cut_nc,--cut_tc"
70 #define DOMREPOPTS "--domE,--domT,--cut_ga,--cut_nc,--cut_tc"
71 #define INCOPTS "--incE,--incT,--cut_ga,--cut_nc,--cut_tc"
72 #define INCDOMOPTS "--incdomE,--incdomT,--cut_ga,--cut_nc,--cut_tc"
73 #define THRESHOPTS "-E,-T,--domE,--domT,--incE,--incT,--incdomE,--incdomT,--cut_ga,--cut_nc,--cut_tc"
74
75
76 #define CPUOPTS NULL
77 #define MPIOPTS NULL
78
79
80 static ESL_OPTIONS options[] = {
81 /* name type default env range toggles reqs incomp help docgroup*/
82 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
83 /* Control of output */
84 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "direct output to file <f>, not stdout", 2 },
85 { "-A", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save multiple alignment of all hits to file <f>", 2 },
86 { "--tblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save parseable table of hits to file <f>", 2 },
87 { "--dfamtblout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save table of hits to file, in Dfam format <f>", 2 },
88 { "--aliscoresout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save scores for each position in each alignment to <f>", 2 },
89 { "--hmmout", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "if input is alignment(s), write produced hmms to file <f>", 2 },
90 { "--acc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "prefer accessions over names in output", 2 },
91 { "--noali", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "don't output alignments, so output is smaller", 2 },
92 { "--notextw", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--textw", "unlimit ASCII text output line width", 2 },
93 { "--textw", eslARG_INT, "120", NULL, "n>=120",NULL, NULL, "--notextw", "set max width of ASCII text output lines", 2 },
94 /* Control of scoring system */
95 { "--singlemx", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "", "use substitution score matrix w/ single-sequence MSA-format inputs", 3 },
96 { "--popen", eslARG_REAL, "0.03125",NULL,"0<=x<0.5",NULL, NULL, NULL, "gap open probability", 3 },
97 { "--pextend", eslARG_REAL, "0.75", NULL, "0<=x<1", NULL, NULL, NULL, "gap extend probability", 3 },
98 { "--mx", eslARG_STRING, "DNA1", NULL, NULL, NULL, NULL, "--mxfile", "substitution score matrix choice (of some built-in matrices)", 99 }, /* hidden; only one choice available = DNA1*/
99 { "--mxfile", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--mx", "read substitution score matrix from file <f>", 3 },
100 /* Control of reporting thresholds */
101 { "-E", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, REPOPTS, "report sequences <= this E-value threshold in output", 4 },
102 { "-T", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, REPOPTS, "report sequences >= this score threshold in output", 4 },
103 /* Control of inclusion (significance) thresholds */
104 { "--incE", eslARG_REAL, "0.01", NULL, "x>0", NULL, NULL, INCOPTS, "consider sequences <= this E-value threshold as significant", 5 },
105 { "--incT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCOPTS, "consider sequences >= this score threshold as significant", 5 },
106 /* Model-specific thresholding for both reporting and inclusion */
107 { "--cut_ga", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's GA gathering cutoffs to set all thresholding", 6 },
108 { "--cut_nc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's NC noise cutoffs to set all thresholding", 6 },
109 { "--cut_tc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's TC trusted cutoffs to set all thresholding", 6 },
110 /* Control of acceleration pipeline */
111 { "--max", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--F1,--F2,--F3", "Turn all heuristic filters off (less speed, more power)", 7 },
112 { "--F1", eslARG_REAL, /*set below*/NULL, NULL, NULL, NULL, NULL, "--max", "Stage 1 (SSV) threshold: promote hits w/ P <= F1", 7 },
113 { "--F2", eslARG_REAL, "3e-3", NULL, NULL, NULL, NULL, "--max", "Stage 2 (Vit) threshold: promote hits w/ P <= F2", 7 },
114 { "--F3", eslARG_REAL, "3e-5", NULL, NULL, NULL, NULL, "--max", "Stage 3 (Fwd) threshold: promote hits w/ P <= F3", 7 },
115 { "--nobias", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--max", "turn off composition bias filter", 7 },
116
117 /* Selecting the alphabet rather than autoguessing it */
118 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--rna", "input alignment is DNA sequence data", 8 },
119 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--dna", "input alignment is RNA sequence data", 8 },
120
121 #if defined (eslENABLE_SSE)
122 /* Control of FM pruning/extension */
123 { "--seed_max_depth", eslARG_INT, "15", NULL, NULL, NULL, NULL, NULL, "seed length at which bit threshold must be met", 9 },
124 { "--seed_sc_thresh", eslARG_REAL, "14", NULL, NULL, NULL, NULL, NULL, "Default req. score for FM seed (bits)", 9 },
125 { "--seed_sc_density", eslARG_REAL, "0.75", NULL, NULL, NULL, NULL, NULL, "seed must maintain this bit density from one of two ends", 9 },
126 { "--seed_drop_max_len", eslARG_INT, "4", NULL, NULL, NULL, NULL, NULL, "maximum run length with score under (max - [fm_drop_lim])", 9 },
127 { "--seed_drop_lim", eslARG_REAL, "0.3", NULL, NULL, NULL, NULL, NULL, "in seed, max drop in a run of length [fm_drop_max_len]", 9 },
128 { "--seed_req_pos", eslARG_INT, "5", NULL, NULL, NULL, NULL, NULL, "minimum number consecutive positive scores in seed" , 9 },
129 { "--seed_consens_match", eslARG_INT, "11", NULL, NULL, NULL, NULL, NULL, "<n> consecutive matches to consensus will override score threshold" , 9 },
130 { "--seed_ssv_length", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "length of window around FM seed to get full SSV diagonal", 9 },
131 #endif
132
133 /* Other options */
134 { "--qformat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL , NULL, "assert query is in format <s> (can be seq or msa format)", 12 },
135 { "--qsingle_seqs", eslARG_NONE, NULL, NULL, NULL, NULL, NULL , NULL, "force query to be read as individual sequences, even if in an msa format", 12 },
136 { "--tformat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert target <seqdb> is in format <s>", 12 },
137 { "--nonull2", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "turn off biased composition score corrections", 12 },
138 { "-Z", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set database size (Megabases) to <x> for E-value calculations", 12 },
139 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 12 },
140 { "--w_beta", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "tail mass at which window length is determined", 12 },
141 { "--w_length", eslARG_INT, NULL, NULL, NULL, NULL, NULL, NULL, "window length - essentially max expected hit length" , 12 },
142 { "--block_length", eslARG_INT, NULL, NULL, "n>=50000", NULL, NULL, NULL, "length of blocks read from target database (threaded) ", 12 },
143 { "--watson", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--crick", "only search the top strand", 12 },
144 { "--crick", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--watson", "only search the bottom strand", 12 },
145
146
147 /* Restrict search to subset of database - hidden because these flags are
148 * (a) currently for internal use
149 * (b) probably going to change
150 */
151 { "--restrictdb_stkey", eslARG_STRING, "0", NULL, NULL, NULL,"--restrictdb_n,--ssifile", NULL, "Search starts at the sequence with name <s>", 99 },
152 { "--restrictdb_n",eslARG_INT, "-1", NULL, NULL, NULL,"--restrictdb_stkey,--ssifile", NULL, "Search <j> target sequences (starting at --restrictdb_stkey)", 99 },
153 { "--ssifile", eslARG_STRING, NULL, NULL, NULL, NULL,"--restrictdb_stkey,--restrictdb_n", NULL, "restrictdb_x values require ssi file. Override default to <s>", 99 },
154
155
156 /* stage-specific window length used for bias composition estimate,
157 * hidden because they are confusing/expert options. May drag them out
158 * into the daylight eventually
159 */
160 { "--B1", eslARG_INT, "110", NULL, NULL, NULL, NULL, "--max,--nobias", "window length for biased-composition modifier (SSV)", 99 },
161 { "--B2", eslARG_INT, "240", NULL, NULL, NULL, NULL, "--max,--nobias", "window length for biased-composition modifier (Vit)", 99 },
162 { "--B3", eslARG_INT, "1000", NULL, NULL, NULL, NULL, "--max,--nobias", "window length for biased-composition modifier (Fwd)", 99 },
163
164 /* expert-only option (for now), hidden from view, for altering bg probs. May not keep. */
165 { "--bgfile", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, NULL, "override default background probs with values in file <f>", 99 },
166
167
168 /* Not used, but retained because esl option-handling code errors if it isn't kept here. Placed in group 99 so it doesn't print to help*/
169 { "--domZ", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "Not used", 99 },
170 { "--domE", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, DOMREPOPTS, "Not used", 99 },
171 { "--domT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, DOMREPOPTS, "Not used", 99 },
172 { "--incdomE", eslARG_REAL, "0.01", NULL, "x>0", NULL, NULL, INCDOMOPTS, "Not used", 99 },
173 { "--incdomT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCDOMOPTS, "Not used", 99 },
174
175
176
177 #ifdef HMMER_THREADS
178 { "--cpu", eslARG_INT, p7_NCPU,"HMMER_NCPU","n>=0",NULL, NULL, CPUOPTS, "number of parallel CPU workers to use for multithreads", 12 },
179 #endif
180 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
181 };
182
183
184 /* struct cfg_s : "Global" application configuration shared by all threads/processes
185 *
186 * This structure is passed to routines within main.c, as a means of semi-encapsulation
187 * of shared data amongst different parallel processes (threads or MPI processes).
188 */
189 struct cfg_s {
190 char *dbfile; /* target sequence database file */
191 char *queryfile; /* query file (hmm, fasta, or some MSA) */
192 int qfmt;
193
194
195 int do_mpi; /* TRUE if we're doing MPI parallelization */
196 int nproc; /* how many MPI processes, total */
197 int my_rank; /* who am I, in 0..nproc-1 */
198
199 char *firstseq_key; /* name of the first sequence in the restricted db range */
200 int n_targetseq; /* number of sequences in the restricted range */
201 };
202
203 static char usage[] = "[options] <query hmmfile|alignfile|seqfile> <target seqfile>";
204 static char banner[] = "search a DNA model, alignment, or sequence against a DNA database";
205
206
207 static int serial_master (ESL_GETOPTS *go, struct cfg_s *cfg);
208 static int serial_loop (WORKER_INFO *info, ID_LENGTH_LIST *id_length_list, ESL_SQFILE *dbfp, char *firstseq_key, int n_targetseqs );
209 #if defined (eslENABLE_SSE)
210 static int serial_loop_FM (WORKER_INFO *info, ESL_SQFILE *dbfp);
211 #endif
212 #ifdef HMMER_THREADS
213 #define BLOCK_SIZE 1000
214
215 static int thread_loop(WORKER_INFO *info, ID_LENGTH_LIST *id_length_list, ESL_THREADS *obj, ESL_WORK_QUEUE *queue, ESL_SQFILE *dbfp, char *firstseq_key, int n_targetseqs);
216 static void pipeline_thread(void *arg);
217 #if defined (eslENABLE_SSE)
218 static int thread_loop_FM(WORKER_INFO *info, ESL_THREADS *obj, ESL_WORK_QUEUE *queue, ESL_SQFILE *dbfp);
219 static void pipeline_thread_FM(void *arg);
220 #endif
221
222 #endif /*HMMER_THREADS*/
223
224
225 static int
226 process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go, char **ret_queryfile, char **ret_seqfile)
227 {
228 ESL_GETOPTS *go = esl_getopts_Create(options);
229 int status;
230
231 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; }
232 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; }
233 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; }
234
235 /* help format: */
236 if (esl_opt_GetBoolean(go, "-h") == TRUE)
237 {
238 p7_banner(stdout, argv[0], banner);
239 esl_usage(stdout, argv[0], usage);
240 if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
241 esl_opt_DisplayHelp(stdout, go, 1, 2, 100); /* 1= group; 2 = indentation; 120=textwidth*/
242
243 if (puts("\nOptions directing output:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
244 esl_opt_DisplayHelp(stdout, go, 2, 2, 100);
245
246 if (puts("\nOptions controlling scoring system:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
247 esl_opt_DisplayHelp(stdout, go, 3, 2, 100);
248
249 if (puts("\nOptions controlling reporting thresholds:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
250 esl_opt_DisplayHelp(stdout, go, 4, 2, 100);
251
252 if (puts("\nOptions controlling inclusion (significance) thresholds:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
253 esl_opt_DisplayHelp(stdout, go, 5, 2, 100);
254
255 if (puts("\nOptions controlling model-specific thresholding:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
256 esl_opt_DisplayHelp(stdout, go, 6, 2, 80);
257
258 if (puts("\nOptions controlling acceleration heuristics:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
259 esl_opt_DisplayHelp(stdout, go, 7, 2, 100);
260
261 if (puts("\nOptions for selecting query alphabet rather than guessing it:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
262 esl_opt_DisplayHelp(stdout, go, 8, 2, 80);
263
264 // if (puts("\nOptions for restricting search to a range of target database sequences:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
265 // esl_opt_DisplayHelp(stdout, go, 8, 2, 100);
266
267 if (puts("\nOptions controlling seed search heuristic:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
268 esl_opt_DisplayHelp(stdout, go, 9, 2, 100);
269
270 if (puts("\nOther expert options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
271 esl_opt_DisplayHelp(stdout, go, 12, 2, 100);
272 exit(0);
273
274 }
275
276 if (esl_opt_ArgNumber(go) != 2) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
277 if ((*ret_queryfile = esl_opt_GetArg(go, 1)) == NULL) { if (puts("Failed to get <queryfile> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
278 if ((*ret_seqfile = 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; }
279
280 /* Validate any attempted use of stdin streams */
281 if (strcmp(*ret_queryfile, "-") == 0 && strcmp(*ret_seqfile, "-") == 0)
282 { if (puts("Either <query hmmfile|alignfile> or <seqdb> may be '-' (to read from stdin), but not both.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
283
284 *ret_go = go;
285 return eslOK;
286
287 FAILURE: /* all errors handled here are user errors, so be polite. */
288 esl_usage(stdout, argv[0], usage);
289 if (puts("\nwhere basic options are:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
290 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
291 if (printf("\nTo see more help on available options, do %s -h\n\n", argv[0]) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
292 esl_getopts_Destroy(go);
293 exit(1);
294
295 ERROR:
296 if (go) esl_getopts_Destroy(go);
297 exit(status);
298 }
299
300 static int
301 output_header(FILE *ofp, const ESL_GETOPTS *go, char *queryfile, char *seqfile, int ncpus)
302 {
303 p7_banner(ofp, go->argv[0], banner);
304
305 if (fprintf(ofp, "# query file: %s\n", queryfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
306 if (fprintf(ofp, "# target sequence database: %s\n", seqfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
307 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");
308 if (esl_opt_IsUsed(go, "-A") && fprintf(ofp, "# MSA of all hits saved to file: %s\n", esl_opt_GetString(go, "-A")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
309 if (esl_opt_IsUsed(go, "--tblout") && fprintf(ofp, "# hits tabular output: %s\n", esl_opt_GetString(go, "--tblout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
310 if (esl_opt_IsUsed(go, "--dfamtblout") && fprintf(ofp, "# hits output in Dfam format: %s\n", esl_opt_GetString(go, "--dfamtblout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
311 if (esl_opt_IsUsed(go, "--aliscoresout") && fprintf(ofp, "# alignment scores output: %s\n", esl_opt_GetString(go, "--aliscoresout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
312 if (esl_opt_IsUsed(go, "--hmmout") && fprintf(ofp, "# hmm output: %s\n", esl_opt_GetString(go, "--hmmout")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
313
314 if (esl_opt_IsUsed(go, "--acc") && fprintf(ofp, "# prefer accessions over names: yes\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
315 if (esl_opt_IsUsed(go, "--noali") && fprintf(ofp, "# show alignments in output: no\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
316 if (esl_opt_IsUsed(go, "--notextw") && fprintf(ofp, "# max ASCII text line length: unlimited\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
317 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");
318 if (esl_opt_IsUsed(go, "--singlemx") && fprintf(ofp, "# Use score matrix for 1-seq MSAs: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
319 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");
320 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");
321 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");
322 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");
323 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");
324 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");
325 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");
326 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");
327 if (esl_opt_IsUsed(go, "--cut_ga") && fprintf(ofp, "# model-specific thresholding: GA cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
328 if (esl_opt_IsUsed(go, "--cut_nc") && fprintf(ofp, "# model-specific thresholding: NC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
329 if (esl_opt_IsUsed(go, "--cut_tc") && fprintf(ofp, "# model-specific thresholding: TC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
330 if (esl_opt_IsUsed(go, "--max") && fprintf(ofp, "# Max sensitivity mode: on [all heuristic filters off]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
331 if (esl_opt_IsUsed(go, "--F1") && fprintf(ofp, "# SSV filter P threshold: <= %g\n", esl_opt_GetReal(go, "--F1")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
332 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");
333 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");
334 if (esl_opt_IsUsed(go, "--nobias") && fprintf(ofp, "# biased composition HMM filter: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
335
336 if (esl_opt_IsUsed(go, "--B1") && fprintf(ofp, "# biased comp SSV window len: %d\n", esl_opt_GetInteger(go, "--B1")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
337 if (esl_opt_IsUsed(go, "--B2") && fprintf(ofp, "# biased comp Viterbi window len: %d\n", esl_opt_GetInteger(go, "--B2")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
338 if (esl_opt_IsUsed(go, "--B3") && fprintf(ofp, "# biased comp Forward window len: %d\n", esl_opt_GetInteger(go, "--B3")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
339 if (esl_opt_IsUsed(go, "--bgfile") && fprintf(ofp, "# file with custom bg probs: %s\n", esl_opt_GetString(go, "--bgfile")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
340
341 if (esl_opt_IsUsed(go, "--dna") && fprintf(ofp, "# input query is asserted as: DNA\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
342 if (esl_opt_IsUsed(go, "--rna") && fprintf(ofp, "# input query is asserted as: RNA\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
343
344 #if defined (eslENABLE_SSE)
345 if (esl_opt_IsUsed(go, "--seed_max_depth") && fprintf(ofp, "# FM Seed length: %d\n", esl_opt_GetInteger(go, "--seed_max_depth")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
346 if (esl_opt_IsUsed(go, "--seed_sc_thresh") && fprintf(ofp, "# FM score threshold (bits): %g\n", esl_opt_GetReal(go, "--seed_sc_thresh")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
347 if (esl_opt_IsUsed(go, "--seed_sc_density") && fprintf(ofp, "# FM score density (bits/pos): %g\n", esl_opt_GetReal(go, "--seed_sc_density")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
348 if (esl_opt_IsUsed(go, "--seed_drop_max_len") && fprintf(ofp, "# FM max neg-growth length: %d\n", esl_opt_GetInteger(go, "--seed_drop_max_len")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
349 if (esl_opt_IsUsed(go, "--seed_drop_lim") && fprintf(ofp, "# FM max run drop: %g\n", esl_opt_GetReal(go, "--seed_drop_lim")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
350 if (esl_opt_IsUsed(go, "--seed_req_pos") && fprintf(ofp, "# FM req positive run length: %d\n", esl_opt_GetInteger(go, "--seed_req_pos")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
351 if (esl_opt_IsUsed(go, "--seed_consens_match") && fprintf(ofp, "# FM consec consensus match req: %d\n", esl_opt_GetInteger(go, "--seed_consens_match")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
352 if (esl_opt_IsUsed(go, "--seed_ssv_length") && fprintf(ofp, "# FM len used for Vit window: %d\n", esl_opt_GetInteger(go, "--seed_ssv_length")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
353 #endif
354 if (esl_opt_IsUsed(go, "--restrictdb_stkey") && fprintf(ofp, "# Restrict db to start at seq key: %s\n", esl_opt_GetString(go, "--restrictdb_stkey")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
355 if (esl_opt_IsUsed(go, "--restrictdb_n") && fprintf(ofp, "# Restrict db to # target seqs: %d\n", esl_opt_GetInteger(go, "--restrictdb_n")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
356 if (esl_opt_IsUsed(go, "--ssifile") && fprintf(ofp, "# Override ssi file to: %s\n", esl_opt_GetString(go, "--ssifile")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
357
358
359 if (esl_opt_IsUsed(go, "--nonull2") && fprintf(ofp, "# null2 bias corrections: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
360
361 if (esl_opt_IsUsed(go, "--watson") && fprintf(ofp, "# search only top strand: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
362 if (esl_opt_IsUsed(go, "--crick") && fprintf(ofp, "# search only bottom strand: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
363 if (esl_opt_IsUsed(go, "-Z") && fprintf(ofp, "# database size is set to: %.1f Mb\n", esl_opt_GetReal(go, "-Z")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
364 if (esl_opt_IsUsed(go, "--seed")) {
365 if (esl_opt_GetInteger(go, "--seed") == 0 && fprintf(ofp, "# random number seed: one-time arbitrary\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
366 else if ( fprintf(ofp, "# random number seed set to: %d\n", esl_opt_GetInteger(go, "--seed")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
367 }
368 if (esl_opt_IsUsed(go, "--qformat") && fprintf(ofp, "# query format asserted: %s\n", esl_opt_GetString(go, "--qformat")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
369 if (esl_opt_IsUsed(go, "--qsingle_seqs")&& fprintf(ofp,"# query contains individual seqs: on\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
370 if (esl_opt_IsUsed(go, "--tformat") && fprintf(ofp, "# target format asserted: %s\n", esl_opt_GetString(go, "--tformat")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
371 if (esl_opt_IsUsed(go, "--w_beta") && fprintf(ofp, "# window length beta value: %g\n", esl_opt_GetReal(go, "--w_beta")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
372 if (esl_opt_IsUsed(go, "--w_length") && fprintf(ofp, "# window length : %d\n", esl_opt_GetInteger(go, "--w_length")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
373 if (esl_opt_IsUsed(go, "--block_length")&&fprintf(ofp, "# block length : %d\n", esl_opt_GetInteger(go, "--block_length")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
374 #ifdef HMMER_THREADS
375 //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");
376 if (fprintf(ofp, "# number of worker threads: %d\n", ncpus) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
377 #endif
378 if (fprintf(ofp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
379 return eslOK;
380 }
381
382
383
384 int
385 main(int argc, char **argv)
386 {
387 ESL_GETOPTS *go = NULL;
388 struct cfg_s cfg;
389 int status = eslOK;
390
391 impl_Init(); /* processor specific initialization */
392 p7_FLogsumInit(); /* we're going to use table-driven Logsum() approximations at times */
393
394 /* Initialize what we can in the config structure (without knowing the alphabet yet)
395 */
396 cfg.queryfile = NULL;
397 cfg.dbfile = NULL;
398 cfg.qfmt = eslSQFILE_UNKNOWN;
399 cfg.do_mpi = FALSE; /* this gets reset below, if we init MPI */
400 cfg.nproc = 0; /* this gets reset below, if we init MPI */
401 cfg.my_rank = 0; /* this gets reset below, if we init MPI */
402
403 cfg.firstseq_key = NULL;
404 cfg.n_targetseq = -1;
405
406 process_commandline(argc, argv, &go, &cfg.queryfile, &cfg.dbfile);
407
408 if (esl_opt_IsOn(go, "--qformat")) { /* is this an msa or a single sequence file? */
409 cfg.qfmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--qformat")); // try single sequence format
410 if (cfg.qfmt == eslSQFILE_UNKNOWN) {
411 p7_Fail("%s is not a recognized input file format\n", esl_opt_GetString(go, "--qformat"));
412 } else { /* disallow target-only formats */
413 if (cfg.qfmt == eslSQFILE_NCBI || cfg.qfmt == eslSQFILE_DAEMON ||
414 cfg.qfmt == eslSQFILE_HMMPGMD || cfg.qfmt == eslSQFILE_FMINDEX )
415 p7_Fail("%s is not a valid query format\n", esl_opt_GetString(go, "--qformat"));
416 }
417 }
418
419
420 if (esl_opt_IsUsed(go, "--restrictdb_stkey") )
421 if ((cfg.firstseq_key = esl_opt_GetString(go, "--restrictdb_stkey")) == NULL) p7_Fail("Failure capturing --restrictdb_stkey\n");
422
423 if (esl_opt_IsUsed(go, "--restrictdb_n") )
424 cfg.n_targetseq = esl_opt_GetInteger(go, "--restrictdb_n");
425
426 if ( cfg.n_targetseq != -1 && cfg.n_targetseq < 1 )
427 p7_Fail("--restrictdb_n must be >= 1\n");
428
429 status = serial_master(go, &cfg);
430
431 esl_getopts_Destroy(go);
432 return status;
433 }
434
435 static int
436 nhmmer_open_hmm_file(struct cfg_s *cfg, P7_HMMFILE **hfp, char *errbuf, ESL_ALPHABET **abc, P7_HMM **hmm ) {
437 int status = p7_hmmfile_Open(cfg->queryfile, NULL, hfp, errbuf);
438
439 if (status == eslENOTFOUND) {
440 p7_Fail("File existence/permissions problem in trying to open query file %s.\n%s\n", cfg->queryfile, errbuf);
441 } else if (status == eslOK) {
442 //Successfully opened HMM file
443 status = p7_hmmfile_Read(*hfp, abc, hmm);
444 if (status != eslOK) p7_Fail("Error reading hmm from file %s (%d)\n", cfg->queryfile, status);
445 }
446 return status;
447 }
448
449 static int
450 nhmmer_open_msa_file(struct cfg_s *cfg, ESL_MSAFILE **qfp_msa, ESL_ALPHABET **abc, ESL_MSA **msa ) {
451 int status = esl_msafile_Open(abc, cfg->queryfile, NULL, cfg->qfmt, NULL, qfp_msa);
452 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open query file %s.\n", cfg->queryfile);
453 if (status == eslOK) {
454 status = esl_msafile_Read(*qfp_msa, msa);
455 }
456 return status;
457 }
458
459
460 static int
461 nhmmer_open_seq_file (struct cfg_s *cfg, ESL_SQFILE **qfp_sq, ESL_ALPHABET **abc, ESL_SQ **qsq, int used_qsingle_seqs) {
462 int status = esl_sqfile_Open(cfg->queryfile, cfg->qfmt, NULL, qfp_sq);
463 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open query file %s.\n", cfg->queryfile);
464 if (status == eslOK) {
465 if (*abc == NULL) {
466 int q_type = eslUNKNOWN;
467 status = esl_sqfile_GuessAlphabet(*qfp_sq, &q_type);
468 if ( (*qfp_sq)->format == eslSQFILE_FASTA /* we've guessed or been told it's a single sequence fasta file */
469 && status == eslEFORMAT /* format error most likely to be due to presence of a gap character, so it's really an afa file */
470 && used_qsingle_seqs /* we were instructed to treat the input as single seqs, so override the fasta guess/instruction, and force single-sequence handling of afa file */
471 ) {
472 esl_sqfile_Close(*qfp_sq);
473 status = esl_sqfile_Open(cfg->queryfile, eslMSAFILE_AFA, NULL, qfp_sq);
474 if (status == eslOK && *abc == NULL)
475 status = esl_sqfile_GuessAlphabet(*qfp_sq, &q_type);
476 }
477 if (status == eslEFORMAT) p7_Fail("Parse failed (sequence file %s):\n%s\n", (*qfp_sq)->filename, esl_sqfile_GetErrorBuf(*qfp_sq));
478 if (q_type == eslUNKNOWN) p7_Fail("Unable to guess alphabet for the %s%s query file %s\n", (cfg->qfmt==eslUNKNOWN ? "" : esl_sqio_DecodeFormat(cfg->qfmt)), (cfg->qfmt==eslSQFILE_UNKNOWN ? "":"-formatted"), cfg->queryfile);
479 *abc = esl_alphabet_Create(q_type);
480 }
481 if (!((*abc)->type == eslRNA || (*abc)->type == eslDNA))
482 p7_Fail("Invalid alphabet type in the %s%squery file %s. Expect DNA or RNA\n", (cfg->qfmt==eslUNKNOWN ? "" : esl_sqio_DecodeFormat(cfg->qfmt)), (cfg->qfmt==eslSQFILE_UNKNOWN ? "":"-formatted "), cfg->queryfile);
483
484 esl_sqfile_SetDigital(*qfp_sq, *abc);
485 // read first sequence
486 *qsq = esl_sq_CreateDigital(*abc);
487 status = esl_sqio_Read(*qfp_sq, *qsq);
488 if (status != eslOK) p7_Fail("reading sequence from file %s (%d): \n%s\n", cfg->queryfile, status, esl_sqfile_GetErrorBuf(*qfp_sq));
489 }
490 return status;
491 }
492
493 /* serial_master()
494 * The serial version of hmmsearch.
495 * For each query HMM in <queryfile> search the database for hits.
496 *
497 * A master can only return if it's successful. All errors are handled
498 * immediately and fatally with p7_Fail().
499 */
500 static int
501 serial_master(ESL_GETOPTS *go, struct cfg_s *cfg)
502 {
503 FILE *ofp = stdout; /* results output file (-o) */
504 FILE *afp = NULL; /* alignment output file (-A) */
505 FILE *tblfp = NULL; /* output stream for tabular (--tblout) */
506 FILE *dfamtblfp = NULL; /* output stream for tabular Dfam format (--dfamtblout) */
507 FILE *aliscoresfp = NULL; /* output stream for alignment scores (--aliscoresout) */
508
509 /*Some fraction of these will be used, depending on what sort of input is used for the query*/
510 P7_HMMFILE *hfp = NULL; /* open input HMM file */
511 P7_HMM *hmm = NULL; /* one HMM query */
512 ESL_MSAFILE *qfp_msa = NULL; /* open query alifile */
513 ESL_SQFILE *qfp_sq = NULL; /* open query seqfile */
514 ESL_SQ *qsq = NULL; /* query sequence */
515 FILE *hmmoutfp = NULL; /* output stream for hmms (--hmmout), only if input is an alignment file */
516 char *hmmfile = NULL; /* file to write HMM to */
517
518 int dbformat = eslSQFILE_UNKNOWN; /* format of dbfile */
519 ESL_SQFILE *dbfp = NULL; /* open input sequence file */
520
521 P7_BG *bg_manual = NULL;
522
523 ESL_ALPHABET *abc = NULL; /* digital alphabet */
524 ESL_STOPWATCH *w;
525 P7_SCOREDATA *scoredata = NULL;
526
527 int textw = 0;
528 int nquery = 0;
529 int status = eslOK;
530 int qhstatus = eslOK;
531 int sstatus = eslOK;
532 int i;
533 int64_t resCnt = 0;
534
535 /* used to keep track of the lengths of the sequences that are processed */
536 ID_LENGTH_LIST *id_length_list = NULL;
537
538 /* these variables are only used if db type is FM-index*/
539 FM_CFG *fm_cfg = NULL;
540 FM_METADATA *fm_meta = NULL;
541 fpos_t fm_basepos;
542 /* end FM-index-specific variables */
543
544
545 int ncpus = 0;
546
547 int infocnt = 0;
548 WORKER_INFO *info = NULL;
549 #ifdef HMMER_THREADS
550 ESL_SQ_BLOCK *block = NULL;
551 #ifdef eslENABLE_SSE
552 FM_THREAD_INFO *fminfo = NULL;
553 #endif // eslENABLE_SSE
554 ESL_THREADS *threadObj= NULL;
555 ESL_WORK_QUEUE *queue = NULL;
556 #endif // HMMER_THREADS
557 char errbuf[eslERRBUFSIZE];
558 double window_beta = -1.0 ;
559 int window_length = -1;
560
561 P7_BUILDER *builder = NULL;
562 ESL_MSA *msa = NULL;
563 int msas_named = 0;
564 int force_single = ( esl_opt_IsOn(go, "--singlemx") ? TRUE : FALSE );
565
566 if (esl_opt_IsUsed(go, "--w_beta")) { if (( window_beta = esl_opt_GetReal (go, "--w_beta") ) < 0 || window_beta > 1 ) esl_fatal("Invalid window-length beta value\n"); }
567 if (esl_opt_IsUsed(go, "--w_length")) { if (( window_length = esl_opt_GetInteger(go, "--w_length")) < 4 ) esl_fatal("Invalid window length value\n"); }
568
569 w = esl_stopwatch_Create();
570
571 if (esl_opt_GetBoolean(go, "--notextw")) textw = 0;
572 else textw = esl_opt_GetInteger(go, "--textw");
573
574 if ( esl_opt_IsOn(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
575 else if ( esl_opt_IsOn(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
576
577
578 /* nhmmer accepts _query_ files that are either (i) hmm(s), (2) msa(s), or (3) sequence(s).
579 * The following code will follow the mandate of --qformat, and otherwise figure what
580 * the file type is.
581 */
582
583 /* (1)
584 * If we were told a specific query file type, just do what we're told
585 */
586 if (esl_sqio_IsAlignment(cfg->qfmt) /* msa file */ && !esl_opt_IsOn(go, "--qsingle_seqs") /* msa intent is not overridden */) {
587 status = nhmmer_open_msa_file(cfg, &qfp_msa, &abc, &msa);
588 if (status != eslOK) p7_Fail("Error reading msa from the %s-formatted file %s (%d)\n", esl_sqio_DecodeFormat(cfg->qfmt), cfg->queryfile, status);
589 } else if (cfg->qfmt != eslSQFILE_UNKNOWN /* sequence file */) {
590 status = nhmmer_open_seq_file(cfg, &qfp_sq, &abc, &qsq, esl_opt_IsOn(go, "--qsingle_seqs"));
591 if (status != eslOK) p7_Fail("Error reading sequence from the %s-formatted file %s (%d)\n", esl_sqio_DecodeFormat(cfg->qfmt), cfg->queryfile, status);
592 }
593
594
595 /* (2)
596 * Guessing query format.
597 *
598 * First check if it's an HMM. This fails easily if it's not,
599 * and lets us either (a) give up right away if the input is piped (not rewindable),
600 * or (b) continue guessing
601 *
602 * If it isn't an HMM, and it's a rewindable file, we'll check to see
603 * if it's obviously an MSA file or obviously a sequence file
604 * If not obvious, we'll force the user to tell us.
605 * That looks like this:
606 * - Try to open it as an MSA file
607 * - if ok (i.e. it opens and passes the MSA check, including that
608 * all sequences are the same length)
609 * - if it's a FASTA format, it still might be a sequence file
610 * (note: a2m is FASTA-like, but explicitly a multiple sequence alignment)
611 * - if the "MSA" is a single sequence, then rewind and call it
612 * a sequence input. Otherwise give "must specify" message
613 * - otherwise, it's an MSA; proceed accordingly
614 * - if not ok (i.e. it's not an MSA file)
615 * - if it's anything, it must be a sequence file, proceed accordingly *
616 */
617
618 if ( cfg->qfmt == eslSQFILE_UNKNOWN ) {
619 status = nhmmer_open_hmm_file(cfg, &hfp, errbuf, &abc, &hmm);
620 if (status != eslOK) { /* if it is eslOK, then it's an HMM, so we're done guessing */
621 if (hfp!=NULL) { p7_hmmfile_Close(hfp); hfp=NULL;}
622 if (strcmp(cfg->queryfile, "-") == 0 ) {
623 /* we can't rewind a piped file, so we can't perform any more autodetection on the query format*/
624 p7_Fail("Must specify query file format (--qformat) to read <query file> from stdin ('-')");
625 } else {
626 if (esl_opt_IsOn(go, "--qsingle_seqs")) { /* only try to open as a seq file*/
627 status = nhmmer_open_seq_file(cfg, &qfp_sq, &abc, &qsq, esl_opt_IsOn(go, "--qsingle_seqs"));
628 if (status != eslOK) p7_Fail("Error reading query file %s (%d)\n", cfg->queryfile, status);
629 } else { /* first try as an msa, then fall back to seq */
630 status = nhmmer_open_msa_file(cfg, &qfp_msa, &abc, &msa);
631 if (status == eslOK) {
632 if (qfp_msa->format == eslMSAFILE_AFA) {
633 /* this could just be a sequence file with o single sequence (in which case, fall through
634 * to the "sequence" case), or with several same-sized sequences (in which case ask for guidance) */
635 if (msa->nseq > 1)
636 p7_Fail("Query file type could be either aligned or unaligned; please specify (--qformat [afa|fasta])");
637 } else {
638 /* if ok, and not fasta, then it's an MSA ... proceed */
639 cfg->qfmt = qfp_msa->format;
640 }
641 }
642 if (cfg->qfmt == eslSQFILE_UNKNOWN) { /* it's not an MSA, try seq */
643 if (qfp_msa) {
644 esl_msafile_Close(qfp_msa);
645 qfp_msa = NULL;
646 esl_msa_Destroy(msa);
647 }
648 status = nhmmer_open_seq_file(cfg, &qfp_sq, &abc, &qsq, esl_opt_IsOn(go, "--qsingle_seqs"));
649 if (status != eslOK) p7_Fail("Error reading query file %s (%d)\n", cfg->queryfile, status);
650 }
651 }
652 }
653 } else {
654 if (esl_opt_IsOn(go, "--qsingle_seqs"))
655 p7_Fail("--qsingle_seqs flag is incompatible with an hmm-formatted query file\n");
656 }
657 }
658
659 if (! (abc->type == eslRNA || abc->type == eslDNA))
660 p7_Fail("Invalid alphabet type in query for nhmmer. Expect DNA or RNA.\n");
661
662
663 /* nhmmer accepts _target_ files that are either (i) some sequence file format, or
664 * (2) the eslSQFILE_FMINDEX format (called fmindex).
665 * The following code will follow the mandate of --tformat, and otherwise figure what
666 * the file type is.
667 */
668
669 /* If caller declared target format, decode it */
670 if (esl_opt_IsOn(go, "--tformat")) {
671 dbformat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--tformat"));
672 if (dbformat == eslSQFILE_UNKNOWN) p7_Fail("%s is not a recognized sequence database file format\n", esl_opt_GetString(go, "--tformat"));
673 }
674
675 /* (1)
676 * First try to read it as a non-fmindex format, either following
677 * the mandate of the --tformat flag or autodetecting. If tformat is
678 * unspecified and the file isn't readable as one of the other formats,
679 * will fall through to testing the fmindex in step (2)
680 */
681 if (dbformat != eslSQFILE_FMINDEX) { /* either we've been told what it is, or we need to autodetect. Start with sequence file */
682
683 status = esl_sqfile_Open(cfg->dbfile, dbformat, p7_SEQDBENV, &dbfp);
684 if (status == eslEFORMAT) {
685 if (dbformat == eslSQFILE_UNKNOWN) {
686 /* We're expected to autodetect; still need to try fmindex below */
687 esl_sqfile_Close(dbfp);
688 dbfp = NULL;
689 } else {
690 /* we were told the format, but it didn't work */
691 p7_Fail("Target sequence database file %s is empty or misformatted\n", cfg->dbfile);
692 }
693 }
694 else if (status == eslENOTFOUND) p7_Fail("Failed to open target sequence database %s for reading\n", cfg->dbfile);
695 else if (status == eslEINVAL) p7_Fail("Can't autodetect format of a stdin or .gz seqfile");
696 else if (status != eslOK) p7_Fail("Unexpected error %d opening target sequence database file %s\n", status, cfg->dbfile);
697 else {
698 /* We assume the query is the guide for alphabet type, allowing it to override
699 * guesser uncertainty; but if the guesser is certain that the target sequence
700 * is a protein (or something else non-nucleotide), we fail with an error. */
701 int q_type = eslUNKNOWN;
702 if ( esl_opt_IsOn(go, "--dna") )
703 q_type = eslDNA;
704 else if ( esl_opt_IsOn(go, "--rna") )
705 q_type = eslRNA;
706 else {
707 status = esl_sqfile_GuessAlphabet(dbfp, &q_type);
708 if (status != eslOK)
709 p7_Fail("Unable to guess alphabet for target sequence database file %s\n", cfg->dbfile);
710 }
711 if (! (q_type == eslDNA || q_type == eslRNA || q_type == eslUNKNOWN))
712 p7_Fail("Invalid alphabet type in target for nhmmer. Expect DNA or RNA.\n");
713
714 /*success; move forward with other necessary steps*/
715 if (esl_opt_IsUsed(go, "--restrictdb_stkey") || esl_opt_IsUsed(go, "--restrictdb_n")) {
716 if (dbfp->format != eslSQFILE_FASTA) {
717 p7_Fail("--restrictdb_stkey and --restrictdb_n flags only allowed for fasta formatted files\n");
718 }
719 if (esl_opt_IsUsed(go, "--ssifile"))
720 esl_sqfile_OpenSSI(dbfp, esl_opt_GetString(go, "--ssifile"));
721 else
722 esl_sqfile_OpenSSI(dbfp, NULL);
723 }
724 dbformat = dbfp->format;
725 }
726 }
727
728 /* (2)
729 * We've either been told it's fmindex format, or the autodetect
730 * has fallen through to ask us to check fmindex format
731 */
732 if (dbfp == NULL ) {
733
734 #if !defined (eslENABLE_SSE)
735 if (dbformat == eslSQFILE_FMINDEX) {
736 p7_Fail("fmindex is a valid sequence database file format only on systems supporting SSE vector instructions\n");
737 } else {
738 p7_Fail("Failed to autodetect format for target sequence database %s\n", cfg->dbfile);
739 }
740 #endif
741
742 if (dbformat != eslSQFILE_FMINDEX && strcmp(cfg->dbfile, "-") == 0 ) {
743 /* we can't rewind a piped file, so we can't perform any more autodetection on the target format*/
744 p7_Fail("Must specify target file type (fmindex, or a sequence file format) to read <dbfile> from stdin ('-')");
745 }
746
747 if (esl_opt_IsOn(go, "--max")) {
748 if (dbformat == eslSQFILE_FMINDEX) {
749 p7_Fail("--max flag is incompatible with the fmindex target type\n");
750 } else {
751 p7_Fail("Failed to autodetect target sequence format; --max flag is compatible only with sequence formats\n");
752 }
753 }
754
755 status = fm_configAlloc(&fm_cfg);
756 if (status != eslOK) p7_Fail("unable to allocate memory to store FM meta data\n");
757 fm_meta = fm_cfg->meta;
758
759 if((fm_meta->fp = fopen(cfg->dbfile, "rb")) == NULL)
760 p7_Fail("Failed to open target sequence database %s for reading\n", cfg->dbfile);
761
762 if ( (status = fm_readFMmeta(fm_meta)) != eslOK) {
763 if (dbformat == eslSQFILE_FMINDEX)
764 p7_Fail("Failed to read FM meta data from target sequence database %s\n", cfg->dbfile);
765 else
766 p7_Fail("Failed to autodetect format for target sequence database %s\n", cfg->dbfile);
767 }
768 /* Sanity check */
769 if ( ! ( fm_meta->alph_type == fm_DNA && (fm_meta->alph_size > 0 && fm_meta->alph_size < 30) ) ) {
770 p7_Fail("Unable to autodetect format of %s\n", cfg->dbfile);
771 }
772
773 if ( (status = fm_configInit(fm_cfg, go)) != eslOK)
774 p7_Fail("Failed to initialize FM configuration for target sequence database %s\n", cfg->dbfile);
775
776 if ( (status = fm_alphabetCreate(fm_meta, NULL)) != eslOK)
777 p7_Fail("Failed to create FM alphabet for target sequence database %s\n", cfg->dbfile);
778
779 fgetpos( fm_meta->fp, &fm_basepos);
780
781 dbformat = eslSQFILE_FMINDEX;
782 }
783
784
785
786
787 /* Open the results output files */
788 if (esl_opt_IsOn(go, "-o")) { if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) p7_Fail("Failed to open output file %s for writing\n", esl_opt_GetString(go, "-o")); }
789 if (esl_opt_IsOn(go, "-A")) { if ((afp = fopen(esl_opt_GetString(go, "-A"), "w")) == NULL) p7_Fail("Failed to open alignment file %s for writing\n", esl_opt_GetString(go, "-A")); }
790 if (esl_opt_IsOn(go, "--tblout")) { if ((tblfp = fopen(esl_opt_GetString(go, "--tblout"), "w")) == NULL) esl_fatal("Failed to open tabular output file %s for writing\n", esl_opt_GetString(go, "--tblout")); }
791 if (esl_opt_IsOn(go, "--dfamtblout")) { if ((dfamtblfp = fopen(esl_opt_GetString(go, "--dfamtblout"),"w")) == NULL) esl_fatal("Failed to open tabular dfam output file %s for writing\n", esl_opt_GetString(go, "--dfamtblout")); }
792 if (esl_opt_IsOn(go, "--aliscoresout")) { if ((aliscoresfp = fopen(esl_opt_GetString(go, "--aliscoresout"),"w")) == NULL) esl_fatal("Failed to open alignment scores output file %s for writing\n", esl_opt_GetString(go, "--aliscoresout")); }
793
794 if (qfp_msa != NULL || qfp_sq != NULL) {
795 if (esl_opt_IsOn(go, "--hmmout")) {
796 hmmfile = esl_opt_GetString(go, "--hmmout");
797 if ((hmmoutfp = fopen(hmmfile,"w")) == NULL) esl_fatal("Failed to open hmm output file %s for writing\n", hmmfile);
798 }
799 }
800
801 #ifdef HMMER_THREADS
802 /* initialize thread data */
803 ncpus = ESL_MIN(esl_opt_GetInteger(go, "--cpu"), esl_threads_GetCPUCount());
804
805 if (ncpus > 0) {
806 #if defined (eslENABLE_SSE)
807 if (dbformat == eslSQFILE_FMINDEX)
808 threadObj = esl_threads_Create(&pipeline_thread_FM);
809 else
810 #endif
811 threadObj = esl_threads_Create(&pipeline_thread);
812
813 queue = esl_workqueue_Create(ncpus * 2);
814 }
815 #endif
816
817 if (esl_opt_IsOn(go, "--bgfile")) {
818 bg_manual = p7_bg_Create(abc);
819 status = p7_bg_Read(esl_opt_GetString(go, "--bgfile"), bg_manual, errbuf);
820 if (status != eslOK) p7_Fail("Trouble reading bgfile: %s\n", errbuf);
821 }
822
823 infocnt = (ncpus == 0) ? 1 : ncpus;
824 ESL_ALLOC(info, (ptrdiff_t) sizeof(*info) * infocnt);
825
826 if (status == eslOK) {
827 /* One-time initializations after alphabet <abc> becomes known */
828 output_header(ofp, go, cfg->queryfile, cfg->dbfile, ncpus);
829
830 if (dbformat != eslSQFILE_FMINDEX)
831 dbfp->abc = abc;
832
833 for (i = 0; i < infocnt; ++i) {
834 info[i].pli = NULL;
835 info[i].th = NULL;
836 info[i].om = NULL;
837 if (bg_manual != NULL)
838 info[i].bg = p7_bg_Clone(bg_manual);
839 else
840 info[i].bg = p7_bg_Create(abc);
841
842 #ifdef HMMER_THREADS
843 info[i].queue = queue;
844 #endif
845 }
846
847 #ifdef HMMER_THREADS
848 for (i = 0; i < ncpus * 2; ++i) {
849 #if defined (eslENABLE_SSE)
850 if (dbformat == eslSQFILE_FMINDEX) {
851 ESL_ALLOC(fminfo, sizeof(FM_THREAD_INFO));
852 if (fminfo == NULL) esl_fatal("Failed to allocate FM thread info");
853
854 ESL_ALLOC(fminfo->fmf, sizeof(FM_DATA));
855 ESL_ALLOC(fminfo->fmb, sizeof(FM_DATA));
856 if (fminfo->fmf == NULL || fminfo->fmb == NULL) esl_fatal("Failed to allocate FM thread info");
857 fminfo->active = FALSE;
858
859 status = esl_workqueue_Init(queue, fminfo);
860 if (status != eslOK) esl_fatal("Failed to add FM info to work queue");
861
862 }
863 else
864 #endif
865 {
866
867 block = esl_sq_CreateDigitalBlock(BLOCK_SIZE, abc);
868 if (block == NULL) esl_fatal("Failed to allocate sequence block");
869
870 status = esl_workqueue_Init(queue, block);
871 if (status != eslOK) esl_fatal("Failed to add block to work queue");
872 }
873 }
874 #endif
875 }
876
877 if (qfp_sq != NULL || qfp_msa != NULL ) { // need to convert query sequence / msa to HMM
878 builder = p7_builder_Create(NULL, abc);
879 if (builder == NULL) p7_Fail("p7_builder_Create failed");
880
881 // special arguments for hmmbuild
882 builder->w_len = (go != NULL && esl_opt_IsOn (go, "--w_length")) ? esl_opt_GetInteger(go, "--w_length"): -1;
883 builder->w_beta = (go != NULL && esl_opt_IsOn (go, "--w_beta")) ? esl_opt_GetReal (go, "--w_beta") : p7_DEFAULT_WINDOW_BETA;
884 if ( builder->w_beta < 0 || builder->w_beta > 1 ) esl_fatal("Invalid window-length beta value\n");
885
886 }
887
888 if (qfp_sq != NULL || (qfp_msa != NULL && force_single )) {
889 /* We'll use this scoring matrix whenever we have a single sequence (even in MSA format)
890 * Default is stored in the --mx option, so it's always IsOn(). Check --mxfile first; then go to the --mx option and the default.
891 */
892 if (esl_opt_IsOn(go, "--mxfile")) status = p7_builder_SetScoreSystem (builder, esl_opt_GetString(go, "--mxfile"), NULL, esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), info->bg);
893 else status = p7_builder_LoadScoreSystem(builder, esl_opt_GetString(go, "--mx"), esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), info->bg);
894 if (status != eslOK) p7_Fail("Failed to set single query seq score system:\n%s\n", builder->errbuf);
895 }
896
897
898 /* Outer loop: over each query HMM or alignment in <query file>. */
899 while (qhstatus == eslOK) {
900 P7_PROFILE *gm = NULL;
901 P7_OPROFILE *om = NULL; /* optimized query profile */
902
903 if ( qfp_sq != NULL) {// FASTA format, each query is a single sequence, they all have names
904 //Turn sequence into an HMM
905 if ((qhstatus = p7_SingleBuilder(builder, qsq, info->bg, &hmm, NULL, NULL, NULL)) != eslOK) p7_Fail("build failed: %s", builder->errbuf);
906
907 } else if ( qfp_msa != NULL ) {
908 //deal with recently read MSA
909 //if name isn't assigned, give it one (can only do this if there's a single unnamed alignment, so pick its filename)
910 if (msa->name == NULL) {
911 char *name = NULL;
912 if (msas_named>0) p7_Fail("Name annotation is required for each alignment in a multi MSA file; failed on #%d", nquery+1);
913
914 if (cfg->queryfile != NULL) {
915 if ((status = esl_FileTail(cfg->queryfile, TRUE, &name)) != eslOK) return status; /* TRUE=nosuffix */
916 } else {
917 name = "Query";
918 }
919
920 if ((status = esl_msa_SetName(msa, name, -1)) != eslOK) p7_Fail("Error assigning name to alignment");
921 msas_named++;
922
923 free(name);
924 }
925
926 //Turn sequence alignment into an HMM
927 if (msa->nseq == 1 && force_single) {
928 if (qsq!=NULL) esl_sq_Destroy(qsq);
929 qsq = esl_sq_CreateDigitalFrom(msa->abc, (msa->sqname?msa->sqname[0]:"Query"), msa->ax[0], msa->alen, (msa->sqdesc?msa->sqdesc[0]:NULL), (msa->sqacc?msa->sqacc[0]:NULL), NULL);
930 esl_abc_XDealign(qsq->abc, qsq->dsq, qsq->dsq, &(qsq->n));
931 if ((qhstatus = p7_SingleBuilder(builder, qsq, info->bg, &hmm, NULL, NULL, NULL)) != eslOK) p7_Fail("build failed: %s", builder->errbuf);
932 } else {
933 if ((qhstatus = p7_Builder(builder, msa, info->bg, &hmm, NULL, NULL, NULL, NULL)) != eslOK) p7_Fail("build failed: %s", builder->errbuf);
934 }
935 }
936
937
938 // Assign HMM max_length
939 if (window_length > 0) hmm->max_length = window_length;
940 else if (window_beta > 0) p7_Builder_MaxLength(hmm, window_beta);
941 else if (hmm->max_length == -1 ) p7_Builder_MaxLength(hmm, p7_DEFAULT_WINDOW_BETA);
942
943
944 if (hmmoutfp != NULL) {
945 if ((status = p7_hmmfile_WriteASCII(hmmoutfp, -1, hmm)) != eslOK) ESL_FAIL(status, errbuf, "HMM save failed");
946 }
947
948 nquery++;
949 resCnt = 0;
950 esl_stopwatch_Start(w);
951
952 /* seqfile may need to be rewound (multiquery mode) */
953 if (nquery > 1) {
954 #if defined (eslENABLE_SSE)
955 if (dbformat == eslSQFILE_FMINDEX) { //rewind
956 if (fsetpos(fm_meta->fp, &fm_basepos) != 0) ESL_EXCEPTION(eslESYS, "rewind via fsetpos() failed");
957 }
958 else
959 #endif
960 {
961 if (! esl_sqfile_IsRewindable(dbfp))
962 esl_fatal("Target sequence file %s isn't rewindable; can't search it with multiple queries", cfg->dbfile);
963
964 if (! esl_opt_IsUsed(go, "--restrictdb_stkey") )
965 esl_sqfile_Position(dbfp, 0); //only re-set current position to 0 if we're not planning to set it in a moment
966 }
967 }
968
969 if (dbformat == eslSQFILE_FASTA) {
970 if ( cfg->firstseq_key != NULL ) { //it's tempting to want to do this once and capture the offset position for future passes, but ncbi files make this non-trivial, so this keeps it general
971 sstatus = esl_sqfile_PositionByKey(dbfp, cfg->firstseq_key);
972 if (sstatus != eslOK)
973 p7_Fail("Failure setting restrictdb_stkey to %d\n", cfg->firstseq_key);
974 }
975 }
976
977 if (fprintf(ofp, "Query: %s [M=%d]\n", hmm->name, hmm->M) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
978 if (hmm->acc && fprintf(ofp, "Accession: %s\n", hmm->acc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
979 if (hmm->desc && fprintf(ofp, "Description: %s\n", hmm->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
980
981
982 /* Convert to an optimized model */
983 gm = p7_profile_Create (hmm->M, abc);
984 om = p7_oprofile_Create(hmm->M, abc);
985 p7_ProfileConfig(hmm, info->bg, gm, 100, p7_LOCAL); /* 100 is a dummy length for now; and MSVFilter requires local mode */
986 p7_oprofile_Convert(gm, om); /* <om> is now p7_LOCAL, multihit */
987
988 #if defined (eslENABLE_SSE)
989 if (dbformat == eslSQFILE_FMINDEX) {
990 //capture a measure of score density multiplied by something I conjecture to be related to
991 //the expected longest common subsequence (sqrt(M)). If less than a default target
992 //(7 bits of expected LCS), then the requested score threshold will be shifted down
993 // according to this ratio.
994 // Xref: ~wheelert/notebook/2014/03-04-FM-time-v-len/00NOTES -- Thu Mar 6 14:40:48 EST 2014
995 float best_sc_avg = 0;
996 int j;
997 for (i = 1; i <= om->M; i++) {
998 float max_score = 0;
999 for (j=0; j<hmm->abc->K; j++) {
1000 if ( esl_abc_XIsResidue(om->abc,j) && gm->rsc[j][(i) * p7P_NR + p7P_MSC] > max_score) max_score = gm->rsc[j][(i) * p7P_NR + p7P_MSC];
1001 }
1002 best_sc_avg += max_score;
1003 }
1004 best_sc_avg /= sqrt((double) hmm->M); //that's dividing by M to get score density, then multiplying by sqrt(M) as a proxy for expected LCS
1005 best_sc_avg = ESL_MAX(5.0,best_sc_avg); // don't let it get too low, or run time will dramatically suffer
1006
1007 fm_cfg->sc_thresh_ratio = ESL_MIN(best_sc_avg/7.0, 1.0);
1008 scoredata = p7_hmm_ScoreDataCreate(om, gm);
1009 }
1010 else
1011 #endif
1012 scoredata = p7_hmm_ScoreDataCreate(om, NULL);
1013
1014 for (i = 0; i < infocnt; ++i) {
1015 /* Create processing pipeline and hit list */
1016 info[i].th = p7_tophits_Create();
1017 info[i].om = p7_oprofile_Copy(om);
1018 info[i].pli = p7_pipeline_Create(go, om->M, 100, TRUE, p7_SEARCH_SEQS); /* L_hint = 100 is just a dummy for now */
1019
1020 //set method specific --F1, if it wasn't set at command line
1021 if (!esl_opt_IsOn(go, "--F1") ) {
1022 #if defined (eslENABLE_SSE)
1023 if (dbformat == eslSQFILE_FMINDEX)
1024 info[i].pli->F1 = 0.03;
1025 else
1026 #endif
1027 info[i].pli->F1 = 0.02;
1028 }
1029
1030 #if defined (eslENABLE_SSE)
1031 info[i].fm_cfg = fm_cfg;
1032 #endif
1033 status = p7_pli_NewModel(info[i].pli, info[i].om, info[i].bg);
1034 if (status == eslEINVAL) p7_Fail(info->pli->errbuf);
1035
1036 info[i].pli->do_alignment_score_calc = esl_opt_IsOn(go, "--aliscoresout") ;
1037
1038 if ( esl_opt_IsUsed(go, "--watson")) info[i].pli->strands = p7_STRAND_TOPONLY;
1039 else if ( esl_opt_IsUsed(go, "--crick")) info[i].pli->strands = p7_STRAND_BOTTOMONLY;
1040 else info[i].pli->strands = p7_STRAND_BOTH;
1041
1042 if (dbformat != eslSQFILE_FMINDEX) {
1043 if ( esl_opt_IsUsed(go, "--block_length") )
1044 info[i].pli->block_length = esl_opt_GetInteger(go, "--block_length");
1045 else
1046 info[i].pli->block_length = NHMMER_MAX_RESIDUE_COUNT;
1047 }
1048
1049 info[i].scoredata = p7_hmm_ScoreDataClone(scoredata, om->abc->Kp);
1050
1051 #ifdef HMMER_THREADS
1052 if (ncpus > 0)
1053 esl_threads_AddThread(threadObj, &info[i]);
1054 #endif
1055 }
1056
1057 /* establish the id_lengths data structutre */
1058 id_length_list = init_id_length(1000);
1059
1060 #ifdef HMMER_THREADS
1061 #if defined (eslENABLE_SSE)
1062 if (dbformat == eslSQFILE_FMINDEX) {
1063 for(i=0; i<fm_cfg->meta->seq_count; i++)
1064 add_id_length(id_length_list, fm_cfg->meta->seq_data[i].target_id, fm_cfg->meta->seq_data[i].target_start + fm_cfg->meta->seq_data[i].length - 1);
1065
1066 if (ncpus > 0) sstatus = thread_loop_FM (info, threadObj, queue, dbfp);
1067 else sstatus = serial_loop_FM (info, dbfp);
1068 }
1069 else
1070 #endif //defined (eslENABLE_SSE)
1071 {
1072 if (ncpus > 0) sstatus = thread_loop (info, id_length_list, threadObj, queue, dbfp, cfg->firstseq_key, cfg->n_targetseq);
1073 else sstatus = serial_loop (info, id_length_list, dbfp, cfg->firstseq_key, cfg->n_targetseq);
1074 }
1075
1076 #else //HMMER_THREADS
1077 #if defined (eslENABLE_SSE)
1078 if (dbformat == eslSQFILE_FMINDEX)
1079 sstatus = serial_loop_FM (info, dbfp);
1080 else
1081 #endif // defined (eslENABLE_SSE)
1082 sstatus = serial_loop (info, id_length_list, dbfp, cfg->firstseq_key, cfg->n_targetseq);
1083 #endif //HMMER_THREADS
1084
1085
1086 switch(sstatus) {
1087 case eslEFORMAT:
1088 esl_fatal("Parse failed (sequence file %s):\n%s\n",
1089 dbfp->filename, esl_sqfile_GetErrorBuf(dbfp));
1090 break;
1091 case eslEOF:
1092 case eslOK:
1093 /* do nothing */
1094 break;
1095 default:
1096 esl_fatal("Unexpected error %d reading sequence file %s", sstatus, dbfp->filename);
1097 }
1098
1099 //need to re-compute e-values before merging (when list will be sorted)
1100 if (esl_opt_IsUsed(go, "-Z")) {
1101 resCnt = 1000000*esl_opt_GetReal(go, "-Z");
1102
1103 if ( info[0].pli->strands == p7_STRAND_BOTH)
1104 resCnt *= 2;
1105
1106 } else {
1107 #if defined (eslENABLE_SSE)
1108 if (dbformat == eslSQFILE_FMINDEX) {
1109 resCnt = 2 * fm_meta->char_count;
1110 }
1111 else
1112 #endif
1113 {
1114 for (i = 0; i < infocnt; ++i)
1115 resCnt += info[i].pli->nres;
1116 }
1117 }
1118
1119 for (i = 0; i < infocnt; ++i)
1120 p7_tophits_ComputeNhmmerEvalues(info[i].th, resCnt, info[i].om->max_length);
1121
1122 /* merge the results of the search results */
1123 for (i = 1; i < infocnt; ++i) {
1124 p7_tophits_Merge(info[0].th, info[i].th);
1125 p7_pipeline_Merge(info[0].pli, info[i].pli);
1126
1127 p7_pipeline_Destroy(info[i].pli);
1128 p7_tophits_Destroy(info[i].th);
1129 p7_oprofile_Destroy(info[i].om);
1130 }
1131
1132 #if defined (eslENABLE_SSE)
1133 if (dbformat == eslSQFILE_FMINDEX) {
1134 info[0].pli->nseqs = fm_meta->seq_data[fm_meta->seq_count-1].target_id + 1;
1135 info[0].pli->nres = resCnt;
1136 }
1137 #endif
1138
1139 /* Print the results. */
1140 p7_tophits_SortBySeqidxAndAlipos(info->th);
1141 assign_Lengths(info->th, id_length_list);
1142 p7_tophits_RemoveDuplicates(info->th, info->pli->use_bit_cutoffs);
1143
1144 p7_tophits_SortBySortkey(info->th);
1145 p7_tophits_Threshold(info->th, info->pli);
1146
1147
1148 //tally up total number of hits and target coverage
1149 info->pli->n_output = info->pli->pos_output = 0;
1150 for (i = 0; i < info->th->N; i++) {
1151 if ( (info->th->hit[i]->flags & p7_IS_REPORTED) || info->th->hit[i]->flags & p7_IS_INCLUDED) {
1152 info->pli->n_output++;
1153 info->pli->pos_output += 1 + (info->th->hit[i]->dcl[0].jali > info->th->hit[i]->dcl[0].iali ? info->th->hit[i]->dcl[0].jali - info->th->hit[i]->dcl[0].iali : info->th->hit[i]->dcl[0].iali - info->th->hit[i]->dcl[0].jali) ;
1154 }
1155 }
1156
1157 p7_tophits_Targets(ofp, info->th, info->pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1158 p7_tophits_Domains(ofp, info->th, info->pli, textw); if (fprintf(ofp, "\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1159
1160 if (tblfp) p7_tophits_TabularTargets(tblfp, hmm->name, hmm->acc, info->th, info->pli, (nquery == 1));
1161 if (dfamtblfp) p7_tophits_TabularXfam(dfamtblfp, hmm->name, hmm->acc, info->th, info->pli);
1162 if (aliscoresfp) p7_tophits_AliScores(aliscoresfp, hmm->name, info->th );
1163
1164 esl_stopwatch_Stop(w);
1165
1166 p7_pli_Statistics(ofp, info->pli, w);
1167
1168 if (fprintf(ofp, "//\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1169
1170 /* Output the results in an MSA (-A option) */
1171 if (afp) {
1172 ESL_MSA *msa = NULL;
1173
1174 if (p7_tophits_Alignment(info->th, abc, NULL, NULL, 0, p7_DEFAULT, &msa) == eslOK)
1175 {
1176 esl_msa_SetName (msa, hmm->name, -1);
1177 esl_msa_SetAccession(msa, hmm->acc, -1);
1178 esl_msa_SetDesc (msa, hmm->desc, -1);
1179 esl_msa_FormatAuthor(msa, "nhmmer (HMMER %s)", HMMER_VERSION);
1180
1181 if (textw > 0) esl_msafile_Write(afp, msa, eslMSAFILE_STOCKHOLM);
1182 else esl_msafile_Write(afp, msa, eslMSAFILE_PFAM);
1183
1184 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");
1185 }
1186 else
1187 {
1188 if (fprintf(ofp, "# No hits satisfy inclusion thresholds; no alignment saved\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
1189 }
1190 esl_msa_Destroy(msa);
1191 }
1192
1193 for (i = 0; i < infocnt; ++i)
1194 p7_hmm_ScoreDataDestroy(info[i].scoredata);
1195
1196 p7_hmm_ScoreDataDestroy(scoredata);
1197 p7_pipeline_Destroy(info->pli);
1198 p7_tophits_Destroy(info->th);
1199 p7_oprofile_Destroy(info->om);
1200 p7_oprofile_Destroy(om);
1201 p7_profile_Destroy(gm);
1202 p7_hmm_Destroy(hmm);
1203 destroy_id_length(id_length_list);
1204 if (qsq != NULL) esl_sq_Reuse(qsq);
1205
1206 if (hfp != NULL) {
1207 qhstatus = p7_hmmfile_Read(hfp, &abc, &hmm);
1208 } else if (qfp_msa != NULL){
1209 esl_msa_Destroy(msa);
1210 qhstatus = esl_msafile_Read(qfp_msa, &msa);
1211 } else { // qfp_sq
1212 qhstatus = esl_sqio_Read(qfp_sq, qsq);
1213 }
1214 if (qhstatus != eslOK && qhstatus != eslEOF) p7_Fail("reading from query file %s (%d)\n", cfg->queryfile, qhstatus);
1215
1216
1217
1218
1219 } /* end outer loop over queries */
1220
1221 if (hfp != NULL) {
1222 switch(qhstatus) {
1223 case eslEOD: p7_Fail("read failed, HMM file %s may be truncated?", cfg->queryfile); break;
1224 case eslEFORMAT: p7_Fail("bad file format in HMM file %s", cfg->queryfile); break;
1225 case eslEINCOMPAT: p7_Fail("HMM file %s contains different alphabets", cfg->queryfile); break;
1226 case eslEOF: /* do nothing; EOF is what we expect here */ break;
1227 default: p7_Fail("Unexpected error (%d) in reading HMMs from %s", qhstatus, cfg->queryfile);
1228 }
1229 } else if (qfp_msa != NULL){
1230 if (qhstatus != eslEOF ) esl_msafile_ReadFailure(qfp_msa, status);
1231 } else { // qfp_sq
1232 if (qhstatus == eslEFORMAT) p7_Fail("Parse failed (sequence file %s):\n%s\n",
1233 qfp_sq->filename, esl_sqfile_GetErrorBuf(qfp_sq));
1234 else if (qhstatus != eslEOF) p7_Fail("Unexpected error %d reading sequence file %s",
1235 qhstatus, qfp_sq->filename);
1236 }
1237
1238 if (hmmoutfp != NULL)
1239 fclose(hmmoutfp);
1240
1241 /* Terminate outputs - any last words?
1242 */
1243 if (tblfp) p7_tophits_TabularTail(tblfp, "nhmmer", p7_SEARCH_SEQS, cfg->queryfile, cfg->dbfile, go);
1244 if (ofp) { if (fprintf(ofp, "[ok]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed"); }
1245
1246 /* Cleanup - prepare for successful exit
1247 */
1248 for (i = 0; i < infocnt; ++i)
1249 p7_bg_Destroy(info[i].bg);
1250
1251 #ifdef HMMER_THREADS
1252 if (ncpus > 0) {
1253 esl_workqueue_Reset(queue);
1254 #if defined (eslENABLE_SSE)
1255 if (dbformat == eslSQFILE_FMINDEX) {
1256 while (esl_workqueue_Remove(queue, (void **) &fminfo) == eslOK) {
1257 if (fminfo) {
1258 if (fminfo->fmf) free(fminfo->fmf);
1259 if (fminfo->fmb) free(fminfo->fmb);
1260 free(fminfo);
1261 }
1262 }
1263 }
1264 else
1265 #endif
1266 {
1267 while (esl_workqueue_Remove(queue, (void **) &block) == eslOK) {
1268 esl_sq_DestroyBlock(block);
1269 }
1270 }
1271 esl_workqueue_Destroy(queue);
1272 esl_threads_Destroy(threadObj);
1273 }
1274 #endif
1275
1276 free(info);
1277
1278 if (hfp) p7_hmmfile_Close(hfp);
1279 if (qfp_msa) esl_msafile_Close(qfp_msa);
1280 if (qfp_sq) esl_sqfile_Close(qfp_sq);
1281
1282 if (builder) p7_builder_Destroy(builder);
1283 if (qsq) esl_sq_Destroy(qsq);
1284
1285 esl_sqfile_Close(dbfp);
1286 esl_alphabet_Destroy(abc);
1287 esl_stopwatch_Destroy(w);
1288
1289 #if defined (eslENABLE_SSE)
1290 if (dbformat == eslSQFILE_FMINDEX) {
1291 fclose(fm_meta->fp);
1292 fm_configDestroy(fm_cfg); // will cascade to destroy meta and alphabet, too
1293 }
1294 #endif
1295
1296 if (ofp != stdout) fclose(ofp);
1297 if (afp) fclose(afp);
1298 if (tblfp) fclose(tblfp);
1299 if (dfamtblfp) fclose(dfamtblfp);
1300 if (aliscoresfp) fclose(aliscoresfp);
1301
1302 return eslOK;
1303
1304 ERROR:
1305 if (hfp) p7_hmmfile_Close(hfp);
1306 if (qfp_msa) esl_msafile_Close(qfp_msa);
1307 if (qfp_sq) esl_sqfile_Close(qfp_sq);
1308 if (builder) p7_builder_Destroy(builder);
1309 if (qsq) esl_sq_Destroy(qsq);
1310
1311 if (ofp != stdout) fclose(ofp);
1312 if (afp) fclose(afp);
1313 if (tblfp) fclose(tblfp);
1314 if (dfamtblfp) fclose(dfamtblfp);
1315 if (aliscoresfp) fclose(aliscoresfp);
1316
1317 #if defined (eslENABLE_SSE)
1318 if (dbformat == eslSQFILE_FMINDEX) {
1319 fm_configDestroy(fm_cfg);
1320 }
1321 #endif
1322
1323 if (hmmfile != NULL) free (hmmfile);
1324
1325 return eslFAIL;
1326 }
1327
1328 //TODO: MPI code needs to be added here
1329 static int
1330 serial_loop(WORKER_INFO *info, ID_LENGTH_LIST *id_length_list, ESL_SQFILE *dbfp, char *firstseq_key, int n_targetseqs)
1331 {
1332 ESL_SQ *dbsq = esl_sq_CreateDigital(info->om->abc);
1333 ESL_SQ *dbsq_revcmp;
1334 int wstatus = eslOK;
1335 int seq_id = 0;
1336
1337 if (dbsq->abc->complement)
1338 dbsq_revcmp = esl_sq_CreateDigital(info->om->abc);
1339
1340 wstatus = esl_sqio_ReadWindow(dbfp, 0, info->pli->block_length, dbsq);
1341
1342 while (wstatus == eslOK && (n_targetseqs==-1 || seq_id < n_targetseqs) ) {
1343 dbsq->idx = seq_id;
1344 p7_pli_NewSeq(info->pli, dbsq);
1345
1346 if (info->pli->strands != p7_STRAND_BOTTOMONLY) {
1347
1348 info->pli->nres -= dbsq->C; // to account for overlapping region of windows
1349 p7_Pipeline_LongTarget(info->pli, info->om, info->scoredata, info->bg, info->th, info->pli->nseqs, dbsq, p7_NOCOMPLEMENT, NULL, NULL, NULL);
1350 p7_pipeline_Reuse(info->pli); // prepare for next search
1351
1352 } else {
1353 info->pli->nres -= dbsq->n;
1354 }
1355
1356 //reverse complement
1357 if (info->pli->strands != p7_STRAND_TOPONLY && dbsq->abc->complement != NULL )
1358 {
1359 esl_sq_Copy(dbsq,dbsq_revcmp);
1360 esl_sq_ReverseComplement(dbsq_revcmp);
1361 p7_Pipeline_LongTarget(info->pli, info->om, info->scoredata, info->bg, info->th, info->pli->nseqs, dbsq_revcmp, p7_COMPLEMENT, NULL, NULL, NULL);
1362 p7_pipeline_Reuse(info->pli); // prepare for next search
1363
1364 info->pli->nres += dbsq_revcmp->W;
1365
1366 }
1367
1368 wstatus = esl_sqio_ReadWindow(dbfp, info->om->max_length, info->pli->block_length, dbsq);
1369 if (wstatus == eslEOD) { // no more left of this sequence ... move along to the next sequence.
1370 add_id_length(id_length_list, dbsq->idx, dbsq->L);
1371
1372 info->pli->nseqs++;
1373 esl_sq_Reuse(dbsq);
1374 wstatus = esl_sqio_ReadWindow(dbfp, 0, info->pli->block_length, dbsq);
1375
1376 seq_id++;
1377
1378 }
1379 }
1380
1381
1382 if (dbsq) esl_sq_Destroy(dbsq);
1383 if (dbsq_revcmp) esl_sq_Destroy(dbsq_revcmp);
1384
1385 return wstatus;
1386
1387 }
1388
1389
1390 #if defined (eslENABLE_SSE)
1391 static int
1392 serial_loop_FM(WORKER_INFO *info, ESL_SQFILE *dbfp)
1393 {
1394
1395 int wstatus = eslOK;
1396 int i;
1397
1398 FM_DATA fmf;
1399 FM_DATA fmb;
1400
1401 FM_METADATA *meta = info->fm_cfg->meta;
1402
1403
1404 for ( i=0; i<info->fm_cfg->meta->block_count; i++ ) {
1405
1406 wstatus = fm_FM_read( &fmf, meta, TRUE );
1407 if (wstatus != eslOK) return wstatus;
1408 wstatus = fm_FM_read( &fmb, meta, FALSE );
1409 if (wstatus != eslOK) return wstatus;
1410
1411 fmb.SA = fmf.SA;
1412 fmb.T = fmf.T;
1413
1414 wstatus = p7_Pipeline_LongTarget(info->pli, info->om, info->scoredata, info->bg,
1415 info->th, -1, NULL, -1, &fmf, &fmb, info->fm_cfg);
1416 if (wstatus != eslOK) return wstatus;
1417
1418 fm_FM_destroy(&fmf, 1);
1419 fm_FM_destroy(&fmb, 0);
1420 }
1421
1422 return wstatus;
1423
1424 }
1425 #endif //#if defined (eslENABLE_SSE)
1426
1427 #ifdef HMMER_THREADS
1428 static int
1429 thread_loop(WORKER_INFO *info, ID_LENGTH_LIST *id_length_list, ESL_THREADS *obj, ESL_WORK_QUEUE *queue, ESL_SQFILE *dbfp, char *firstseq_key, int n_targetseqs)
1430 {
1431
1432 int i;
1433 int status = eslOK;
1434 int sstatus = eslOK;
1435 int eofCount = 0;
1436 ESL_SQ_BLOCK *block;
1437 void *newBlock;
1438 int seqid = -1;
1439
1440 ESL_SQ *tmpsq = esl_sq_CreateDigital(info->om->abc);
1441 int abort = FALSE; // in the case n_targetseqs != -1, a block may get abbreviated
1442
1443
1444 esl_workqueue_Reset(queue);
1445 esl_threads_WaitForStart(obj);
1446
1447 status = esl_workqueue_ReaderUpdate(queue, NULL, &newBlock);
1448 if (status != eslOK) esl_fatal("Work queue reader failed");
1449 ((ESL_SQ_BLOCK *)newBlock)->complete = TRUE;
1450
1451 /* Main loop: */
1452 while (sstatus == eslOK ) {
1453 block = (ESL_SQ_BLOCK *) newBlock;
1454
1455 if (abort) {
1456 block->count = 0;
1457 sstatus = eslEOF;
1458 } else {
1459 sstatus = esl_sqio_ReadBlock(dbfp, block, info->pli->block_length, n_targetseqs, /*max_init_window=*/FALSE, TRUE);
1460 }
1461
1462 block->first_seqidx = info->pli->nseqs;
1463 seqid = block->first_seqidx;
1464 for (i=0; i<block->count; i++) {
1465 block->list[i].idx = seqid;
1466 add_id_length(id_length_list, seqid, block->list[i].L);
1467 seqid++;
1468
1469 if ( seqid == n_targetseqs // hit the sequence target
1470 && ( i<block->count-1 || block->complete ) // and either it's not the last sequence (so it's complete), or its complete
1471 ) {
1472 abort = TRUE;
1473 block->count = i+1;
1474 break;
1475 }
1476 }
1477 info->pli->nseqs += block->count - ((abort || block->complete) ? 0 : 1);// if there's an incomplete sequence read into the block wait to count it until it's complete.
1478
1479
1480 if (sstatus == eslEOF) {
1481 if (eofCount < esl_threads_GetWorkerCount(obj)) sstatus = eslOK;
1482 ++eofCount;
1483 } else if (!block->complete ) {
1484 // The final sequence on the block was an incomplete window of the active sequence,
1485 // so our next read will need a copy of it to correctly deal with overlapping
1486 // regions. We capture a copy of the sequence here before sending it off to the
1487 // pipeline to avoid odd race conditions that can occur otherwise.
1488 // Copying the entire sequence isn't really necessary, and is a bit heavy-
1489 // handed. Could accelerate if this proves to have any notable impact on speed.
1490 esl_sq_Copy(block->list + (block->count - 1) , tmpsq);
1491 }
1492
1493
1494 if (sstatus == eslOK) {
1495
1496 /* Capture "complete" status prior to placing current block into the work
1497 * queue, to avoid appearance of a race condition. With only one reader
1498 * thread, there isn't really a race risk, since "complete" is only set
1499 * during the esl_sqio_ReadBlock() function call earlier in this loop
1500 * (i.e. "complete" isn't altered by the worker threads)*/
1501 int prev_complete = block->complete;
1502 status = esl_workqueue_ReaderUpdate(queue, block, &newBlock);
1503
1504
1505 if (status != eslOK) esl_fatal("Work queue reader failed");
1506
1507 // Check how much space the new structure is using and re-allocate if it has grown to more than 20*block_size bytes
1508 // this loop iterates from 0 to newBlock->listsize rather than newBlock->count because we want to count all of the
1509 // block's sub-structures, not just the ones that contained sequence data after the last call to ReadBlock()
1510 // This doesn't check some of the less-common sub-structures in a sequence, but it should be good enough for
1511 // our goal of keeping block size under control
1512
1513 // Do this check before copying any data from block into newBlock because otherwise, the reallocation clobbers
1514 // information that's needed when block ends in mid sequence.
1515
1516 uint64_t block_space = 0;
1517 for(i=0; i<((ESL_SQ_BLOCK *)newBlock)->listSize; i++){
1518 block_space += ((ESL_SQ_BLOCK *)newBlock)->list[i].nalloc;
1519 block_space += ((ESL_SQ_BLOCK *)newBlock)->list[i].aalloc;
1520 block_space += ((ESL_SQ_BLOCK *)newBlock)->list[i].dalloc;
1521 block_space += ((ESL_SQ_BLOCK *)newBlock)->list[i].srcalloc;
1522 block_space += ((ESL_SQ_BLOCK *)newBlock)->list[i].salloc;
1523 if (((ESL_SQ_BLOCK *)newBlock)->list[i].ss != NULL){
1524 block_space += ((ESL_SQ_BLOCK *)newBlock)->list[i].salloc; // ss field is not always presesnt, but takes salloc bytes if it is
1525 }
1526 }
1527
1528 if(block_space > 20* info->pli->block_length){
1529 if(esl_sq_BlockReallocSequences(((ESL_SQ_BLOCK *)newBlock)) != eslOK){
1530 esl_fatal( "Error reallocating sequence data in block.\n");
1531 }
1532 }
1533
1534 //newBlock needs all this information so the next ReadBlock call will know what to do
1535 ((ESL_SQ_BLOCK *)newBlock)->complete = prev_complete;
1536 if (!prev_complete) {
1537 // Push the captured copy of the previously-read sequence into the new block,
1538 // in preparation for ReadWindow (double copy ... slower than necessary)
1539 esl_sq_Copy(tmpsq, ((ESL_SQ_BLOCK *)newBlock)->list);
1540
1541 if ( ((ESL_SQ_BLOCK *)newBlock)->list->n < info->om->max_length ) {
1542 //no reason to search the final partial sequence on the block, as the next block will search this whole chunk
1543 ((ESL_SQ_BLOCK *)newBlock)->list->C = ((ESL_SQ_BLOCK *)newBlock)->list->n;
1544 (((ESL_SQ_BLOCK *)newBlock)->count)--;
1545 } else {
1546 ((ESL_SQ_BLOCK *)newBlock)->list->C = info->om->max_length;
1547 }
1548
1549 }
1550 }
1551 }
1552
1553
1554 status = esl_workqueue_ReaderUpdate(queue, block, NULL);
1555 if (status != eslOK) esl_fatal("Work queue reader failed");
1556
1557 if (sstatus == eslEOF) {
1558 /* wait for all the threads to complete */
1559 esl_threads_WaitForFinish(obj);
1560 esl_workqueue_Complete(queue);
1561 }
1562
1563 esl_sq_Destroy(tmpsq);
1564
1565 return sstatus;
1566 }
1567
1568 static void
1569 pipeline_thread(void *arg)
1570 {
1571 int i;
1572 int status;
1573 int workeridx;
1574 WORKER_INFO *info;
1575 ESL_THREADS *obj;
1576 ESL_SQ_BLOCK *block = NULL;
1577 void *newBlock;
1578
1579 impl_Init();
1580
1581 obj = (ESL_THREADS *) arg;
1582 esl_threads_Started(obj, &workeridx);
1583
1584 info = (WORKER_INFO *) esl_threads_GetData(obj, workeridx);
1585
1586 status = esl_workqueue_WorkerUpdate(info->queue, NULL, &newBlock);
1587 if (status != eslOK) esl_fatal("Work queue worker failed");
1588
1589
1590 /* loop until all blocks have been processed */
1591 block = (ESL_SQ_BLOCK *) newBlock;
1592
1593 while (block->count > 0)
1594 {
1595 /* Main loop: */
1596 for (i = 0; i < block->count; ++i)
1597 {
1598 ESL_SQ *dbsq = block->list + i;
1599
1600 p7_pli_NewSeq(info->pli, dbsq);
1601
1602 if (info->pli->strands != p7_STRAND_BOTTOMONLY) {
1603 info->pli->nres -= dbsq->C; // to account for overlapping region of windows
1604
1605 p7_Pipeline_LongTarget(info->pli, info->om, info->scoredata, info->bg, info->th, block->first_seqidx + i, dbsq, p7_NOCOMPLEMENT, NULL, NULL, NULL/*, NULL, NULL, NULL*/);
1606 p7_pipeline_Reuse(info->pli); // prepare for next search
1607
1608 } else {
1609 info->pli->nres -= dbsq->n;
1610 }
1611
1612 //reverse complement
1613 if (info->pli->strands != p7_STRAND_TOPONLY && dbsq->abc->complement != NULL)
1614 {
1615 esl_sq_ReverseComplement(dbsq);
1616 p7_Pipeline_LongTarget(info->pli, info->om, info->scoredata, info->bg, info->th, block->first_seqidx + i, dbsq, p7_COMPLEMENT, NULL, NULL, NULL/*, NULL, NULL, NULL*/);
1617 p7_pipeline_Reuse(info->pli); // prepare for next search
1618
1619 info->pli->nres += dbsq->W;
1620 }
1621 }
1622
1623 status = esl_workqueue_WorkerUpdate(info->queue, block, &newBlock);
1624 if (status != eslOK) esl_fatal("Work queue worker failed");
1625
1626 block = (ESL_SQ_BLOCK *) newBlock;
1627
1628 }
1629 status = esl_workqueue_WorkerUpdate(info->queue, block, NULL);
1630 if (status != eslOK) esl_fatal("Work queue worker failed");
1631 esl_threads_Finished(obj, workeridx);
1632 return;
1633 }
1634
1635
1636 #if defined (eslENABLE_SSE)
1637 static int
1638 thread_loop_FM(WORKER_INFO *info, ESL_THREADS *obj, ESL_WORK_QUEUE *queue, ESL_SQFILE *dbfp)
1639 {
1640
1641 int status = eslOK;
1642 int i;
1643
1644 FM_METADATA *meta = info->fm_cfg->meta;
1645 FM_THREAD_INFO *fminfo = NULL;
1646 void *newFMinfo = NULL;
1647
1648 esl_workqueue_Reset(queue);
1649 esl_threads_WaitForStart(obj);
1650
1651 status = esl_workqueue_ReaderUpdate(queue, NULL, &newFMinfo);
1652 if (status != eslOK) esl_fatal("Work queue reader failed");
1653 fminfo = (FM_THREAD_INFO *) newFMinfo;
1654
1655 /* Main loop: */
1656 for ( i=0; i<info->fm_cfg->meta->block_count; i++ ) {
1657
1658 status = fm_FM_read( fminfo->fmf, meta, TRUE );
1659 if (status != eslOK) return status;
1660 status = fm_FM_read( fminfo->fmb, meta, FALSE );
1661 if (status != eslOK) return status;
1662
1663 fminfo->fmb->SA = fminfo->fmf->SA;
1664 fminfo->fmb->T = fminfo->fmf->T;
1665 fminfo->active = TRUE;
1666
1667 status = esl_workqueue_ReaderUpdate(queue, fminfo, &newFMinfo);
1668 if (status != eslOK) esl_fatal("Work queue reader failed");
1669 fminfo = (FM_THREAD_INFO *) newFMinfo;
1670
1671 }
1672
1673 /* this part is here to feed the worker threads with new fminfo objects to swap from
1674 * the queue while they are confirming completion of earlier fminfo objects (by
1675 * returning them). They are labelled inactive, so the worker doesn't bother
1676 * computing on them.
1677 */
1678 for (i=0; i<esl_threads_GetWorkerCount(obj)-1; i++) {
1679 fminfo->active = FALSE;
1680 esl_workqueue_ReaderUpdate(queue, fminfo, &newFMinfo);
1681 if (status != eslOK) esl_fatal("Work queue reader failed");
1682 fminfo = (FM_THREAD_INFO *) newFMinfo;
1683 }
1684 fminfo->active = FALSE;
1685 esl_workqueue_ReaderUpdate(queue, fminfo, NULL);
1686 if (status != eslOK) esl_fatal("Work queue reader failed");
1687
1688 esl_threads_WaitForFinish(obj);
1689 esl_workqueue_Complete(queue);
1690
1691 return status;
1692 }
1693
1694
1695 static void
1696 pipeline_thread_FM(void *arg)
1697 {
1698 int status;
1699 int workeridx;
1700 WORKER_INFO *info;
1701 ESL_THREADS *obj;
1702 FM_THREAD_INFO *fminfo = NULL;
1703 void *newFMinfo = NULL;
1704
1705
1706 impl_Init();
1707
1708 obj = (ESL_THREADS *) arg;
1709 esl_threads_Started(obj, &workeridx);
1710
1711 info = (WORKER_INFO *) esl_threads_GetData(obj, workeridx);
1712
1713 status = esl_workqueue_WorkerUpdate(info->queue, NULL, &newFMinfo);
1714 if (status != eslOK) esl_fatal("Work queue worker failed");
1715
1716 /* loop until all blocks have been processed */
1717 fminfo = (FM_THREAD_INFO *) newFMinfo;
1718
1719 while (fminfo->active)
1720 {
1721 status = p7_Pipeline_LongTarget(info->pli, info->om, info->scoredata, info->bg,
1722 info->th, -1, NULL, -1, fminfo->fmf, fminfo->fmb, info->fm_cfg/*, NULL, NULL, NULL */);
1723 if (status != eslOK) esl_fatal ("Work queue worker failed");
1724
1725 fm_FM_destroy(fminfo->fmf, 1);
1726 fm_FM_destroy(fminfo->fmb, 0);
1727
1728 status = esl_workqueue_WorkerUpdate(info->queue, fminfo, &newFMinfo);
1729 if (status != eslOK) esl_fatal("Work queue worker failed");
1730 fminfo = (FM_THREAD_INFO *) newFMinfo;
1731
1732 }
1733
1734 status = esl_workqueue_WorkerUpdate(info->queue, fminfo, NULL);
1735 if (status != eslOK) esl_fatal("Work queue worker failed");
1736
1737 esl_threads_Finished(obj, workeridx);
1738 return;
1739 }
1740 #endif //#if defined (eslENABLE_SSE)
1741
1742
1743 #endif /* HMMER_THREADS */
1744
1745
1746
1747 /* helper functions for tracking id_lengths */
1748
1749 static ID_LENGTH_LIST *
1750 init_id_length( int size )
1751 {
1752 int status;
1753 ID_LENGTH_LIST *list;
1754
1755 ESL_ALLOC (list, sizeof(ID_LENGTH_LIST));
1756 list->count = 0;
1757 list->size = size;
1758 list->id_lengths = NULL;
1759
1760 ESL_ALLOC (list->id_lengths, size * sizeof(ID_LENGTH));
1761
1762 return list;
1763
1764 ERROR:
1765 return NULL;
1766 }
1767
1768 static void
1769 destroy_id_length( ID_LENGTH_LIST *list )
1770 {
1771
1772 if (list != NULL) {
1773 if (list->id_lengths != NULL) free (list->id_lengths);
1774 free (list);
1775 }
1776
1777 }
1778
1779
1780 static int
1781 add_id_length(ID_LENGTH_LIST *list, int id, int L)
1782 {
1783 int status;
1784
1785 if (list->count > 0 && list->id_lengths[list->count-1].id == id) {
1786 // the last time this gets updated, it'll have the sequence's actual length
1787 list->id_lengths[list->count-1].length = L;
1788 } else {
1789
1790 if (list->count == list->size) {
1791 list->size *= 10;
1792 ESL_REALLOC(list->id_lengths, list->size * sizeof(ID_LENGTH));
1793 }
1794
1795 list->id_lengths[list->count].id = id;
1796 list->id_lengths[list->count].length = L;
1797
1798 list->count++;
1799 }
1800 return eslOK;
1801
1802 ERROR:
1803 return status;
1804 }
1805
1806 static int
1807 assign_Lengths(P7_TOPHITS *th, ID_LENGTH_LIST *id_length_list) {
1808
1809 int i;
1810 int j = 0;
1811 for (i=0; i<th->N; i++) {
1812 while (th->hit[i]->seqidx != id_length_list->id_lengths[j].id) { j++; }
1813 th->hit[i]->dcl[0].ad->L = id_length_list->id_lengths[j].length;
1814 }
1815
1816 return eslOK;
1817 }
1818
1819
1820