Debian Codegraph @

hmmer/src/p7_pipeline.c

raw symbols SCIP
1 /* H3's accelerated seq/profile comparison pipeline
2 *
3 * Contents:
4 * 1. P7_PIPELINE: allocation, initialization, destruction
5 * 2. Pipeline API
6 * 3. Example 1: search mode (in a sequence db)
7 * 4. Example 2: scan mode (in an HMM db)
8 */
9 #include <p7_config.h>
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "easel.h"
16 #include "esl_exponential.h"
17 #include "esl_getopts.h"
18 #include "esl_gumbel.h"
19 #include "esl_vectorops.h"
20
21 #include "hmmer.h"
22
23 #include "esl_sqio.h" //!!!!DEBUG
24
25 /* Struct used to pass a collection of useful temporary objects around
26 * within the LongTarget functions
27 * */
28 typedef struct {
29 ESL_SQ *tmpseq; // - a new or reused digital sequence object used for p7_alidisplay_Create() call
30 P7_BG *bg;
31 P7_OPROFILE *om;
32 float *scores;
33 float *fwd_emissions_arr;
34 } P7_PIPELINE_LONGTARGET_OBJS;
35
36
37 /*****************************************************************
38 * 1. The P7_PIPELINE object: allocation, initialization, destruction.
39 *****************************************************************/
40
41 /* Function: p7_pipeline_Create()
42 * Synopsis: Create a new accelerated comparison pipeline.
43 *
44 * Purpose: Given an application configuration structure <go>
45 * containing certain standardized options (described
46 * below), some initial guesses at the model size <M_hint>
47 * and sequence length <L_hint> that will be processed,
48 * and a <mode> that can be either <p7_SCAN_MODELS> or
49 * <p7_SEARCH_SEQS> depending on whether we're searching one sequence
50 * against a model database (hmmscan mode) or one model
51 * against a sequence database (hmmsearch mode); create new
52 * pipeline object.
53 *
54 * In search mode, we would generally know the length of
55 * our query profile exactly, and would pass <om->M> as <M_hint>;
56 * in scan mode, we generally know the length of our query
57 * sequence exactly, and would pass <sq->n> as <L_hint>.
58 * Targets will come in various sizes as we read them,
59 * and the pipeline will resize any necessary objects as
60 * needed, so the other (unknown) length is only an
61 * initial allocation.
62 *
63 * The configuration <go> must include settings for the
64 * following options:
65 *
66 * || option || description || usually ||
67 * | --noali | don't output alignments (smaller output) | FALSE |
68 * | -E | report hits <= this E-value threshold | 10.0 |
69 * | -T | report hits >= this bit score threshold | NULL |
70 * | -Z | set initial hit search space size | NULL |
71 * | --domZ | set domain search space size | NULL |
72 * | --domE | report domains <= this E-value threshold | 10.0 |
73 * | --domT | report domains <= this bit score threshold | NULL |
74 * | --incE | include hits <= this E-value threshold | 0.01 |
75 * | --incT | include hits >= this bit score threshold | NULL |
76 * | --incdomE | include domains <= this E-value threshold | 0.01 |
77 * | --incdomT | include domains <= this score threshold | NULL |
78 * | --cut_ga | model-specific thresholding using GA | FALSE |
79 * | --cut_nc | model-specific thresholding using NC | FALSE |
80 * | --cut_tc | model-specific thresholding using TC | FALSE |
81 * | --max | turn all heuristic filters off | FALSE |
82 * | --F1 | Stage 1 (MSV) thresh: promote hits P <= F1 | 0.02 |
83 * | --F2 | Stage 2 (Vit) thresh: promote hits P <= F2 | 1e-3 |
84 * | --F3 | Stage 2 (Fwd) thresh: promote hits P <= F3 | 1e-5 |
85 * | --nobias | turn OFF composition bias filter HMM | FALSE |
86 * | --nonull2 | turn OFF biased comp score correction | FALSE |
87 * | --seed | RNG seed (0=use arbitrary seed) | 42 |
88 * | --acc | prefer accessions over names in output | FALSE |
89 *
90 * As a special case, if <go> is <NULL>, defaults are set as above.
91 * This shortcut is used in simplifying test programs and the like.
92 *
93 * Returns: ptr to new <P7_PIPELINE> object on success. Caller frees this
94 * with <p7_pipeline_Destroy()>.
95 *
96 * Throws: <NULL> on allocation failure.
97 */
98 P7_PIPELINE *
99 p7_pipeline_Create(const ESL_GETOPTS *go, int M_hint, int L_hint, int long_targets, enum p7_pipemodes_e mode)
100 {
101 P7_PIPELINE *pli = NULL;
102 int seed = (go ? esl_opt_GetInteger(go, "--seed") : 42);
103 int status;
104
105 ESL_ALLOC(pli, sizeof(P7_PIPELINE));
106
107 pli->do_alignment_score_calc = 0;
108 pli->long_targets = long_targets;
109
110 if ((pli->fwd = p7_omx_Create(M_hint, L_hint, L_hint)) == NULL) goto ERROR;
111 if ((pli->bck = p7_omx_Create(M_hint, L_hint, L_hint)) == NULL) goto ERROR;
112 if ((pli->oxf = p7_omx_Create(M_hint, 0, L_hint)) == NULL) goto ERROR;
113 if ((pli->oxb = p7_omx_Create(M_hint, 0, L_hint)) == NULL) goto ERROR;
114
115 /* Normally, we reinitialize the RNG to the original seed every time we're
116 * about to collect a stochastic trace ensemble. This eliminates run-to-run
117 * variability. As a special case, if seed==0, we choose an arbitrary one-time
118 * seed: time() sets the seed, and we turn off the reinitialization.
119 */
120 pli->r = esl_randomness_CreateFast(seed);
121 pli->do_reseeding = (seed == 0) ? FALSE : TRUE;
122 pli->ddef = p7_domaindef_Create(pli->r);
123 pli->ddef->do_reseeding = pli->do_reseeding;
124
125 /* Configure reporting thresholds */
126 pli->by_E = TRUE;
127 pli->E = (go ? esl_opt_GetReal(go, "-E") : 10.0);
128 pli->T = 0.0;
129 pli->dom_by_E = TRUE;
130 pli->domE = (go ? esl_opt_GetReal(go, "--domE") : 10.0);
131 pli->domT = 0.0;
132 pli->use_bit_cutoffs = FALSE;
133 if (go && esl_opt_IsOn(go, "-T"))
134 {
135 pli->T = esl_opt_GetReal(go, "-T");
136 pli->by_E = FALSE;
137 }
138 if (go && esl_opt_IsOn(go, "--domT"))
139 {
140 pli->domT = esl_opt_GetReal(go, "--domT");
141 pli->dom_by_E = FALSE;
142 }
143
144
145 /* Configure inclusion thresholds */
146 pli->inc_by_E = TRUE;
147 pli->incE = (go ? esl_opt_GetReal(go, "--incE") : 0.01);
148 pli->incT = 0.0;
149 pli->incdom_by_E = TRUE;
150 pli->incdomE = (go ? esl_opt_GetReal(go, "--incdomE") : 0.01);
151 pli->incdomT = 0.0;
152 if (go && esl_opt_IsOn(go, "--incT"))
153 {
154 pli->incT = esl_opt_GetReal(go, "--incT");
155 pli->inc_by_E = FALSE;
156 }
157 if (go && esl_opt_IsOn(go, "--incdomT"))
158 {
159 pli->incdomT = esl_opt_GetReal(go, "--incdomT");
160 pli->incdom_by_E = FALSE;
161 }
162
163
164 /* Configure for one of the model-specific thresholding options */
165 if (go && esl_opt_GetBoolean(go, "--cut_ga"))
166 {
167 pli->T = pli->domT = 0.0;
168 pli->by_E = pli->dom_by_E = FALSE;
169 pli->incT = pli->incdomT = 0.0;
170 pli->inc_by_E = pli->incdom_by_E = FALSE;
171 pli->use_bit_cutoffs = p7H_GA;
172 }
173 if (go && esl_opt_GetBoolean(go, "--cut_nc"))
174 {
175 pli->T = pli->domT = 0.0;
176 pli->by_E = pli->dom_by_E = FALSE;
177 pli->incT = pli->incdomT = 0.0;
178 pli->inc_by_E = pli->incdom_by_E = FALSE;
179 pli->use_bit_cutoffs = p7H_NC;
180 }
181 if (go && esl_opt_GetBoolean(go, "--cut_tc"))
182 {
183 pli->T = pli->domT = 0.0;
184 pli->by_E = pli->dom_by_E = FALSE;
185 pli->incT = pli->incdomT = 0.0;
186 pli->inc_by_E = pli->incdom_by_E = FALSE;
187 pli->use_bit_cutoffs = p7H_TC;
188 }
189
190
191 /* Configure search space sizes for E value calculations */
192 pli->Z = pli->domZ = 0.0;
193 pli->Z_setby = pli->domZ_setby = p7_ZSETBY_NTARGETS;
194 if (go && esl_opt_IsOn(go, "-Z"))
195 {
196 pli->Z_setby = p7_ZSETBY_OPTION;
197 pli->Z = esl_opt_GetReal(go, "-Z");
198 }
199 if (go && esl_opt_IsOn(go, "--domZ"))
200 {
201 pli->domZ_setby = p7_ZSETBY_OPTION;
202 pli->domZ = esl_opt_GetReal(go, "--domZ");
203 }
204
205
206 /* Configure acceleration pipeline thresholds */
207 pli->do_max = FALSE;
208 pli->do_biasfilter = TRUE;
209 pli->do_null2 = TRUE;
210 pli->F1 = ((go && esl_opt_IsOn(go, "--F1")) ? ESL_MIN(1.0, esl_opt_GetReal(go, "--F1")) : 0.02);
211 pli->F2 = (go ? ESL_MIN(1.0, esl_opt_GetReal(go, "--F2")) : 1e-3);
212 pli->F3 = (go ? ESL_MIN(1.0, esl_opt_GetReal(go, "--F3")) : 1e-5);
213 if (long_targets) {
214 pli->B1 = (go ? esl_opt_GetInteger(go, "--B1") : 100);
215 pli->B2 = (go ? esl_opt_GetInteger(go, "--B2") : 240);
216 pli->B3 = (go ? esl_opt_GetInteger(go, "--B3") : 1000);
217 } else {
218 pli->B1 = pli->B2 = pli->B3 = -1;
219 }
220
221
222 if (go && esl_opt_GetBoolean(go, "--max"))
223 {
224 pli->do_max = TRUE;
225 pli->do_biasfilter = FALSE;
226
227 pli->F2 = pli->F3 = 1.0;
228 pli->F1 = (pli->long_targets ? 0.3 : 1.0); // need to set some threshold for F1 even on long targets. Should this be tighter?
229 }
230 if (go && esl_opt_GetBoolean(go, "--nonull2")) pli->do_null2 = FALSE;
231 if (go && esl_opt_GetBoolean(go, "--nobias")) pli->do_biasfilter = FALSE;
232
233
234 /* Accounting as we collect results */
235 pli->nmodels = 0;
236 pli->nseqs = 0;
237 pli->nres = 0;
238 pli->nnodes = 0;
239 pli->n_past_msv = 0;
240 pli->n_past_bias = 0;
241 pli->n_past_vit = 0;
242 pli->n_past_fwd = 0;
243 pli->pos_past_msv = 0;
244 pli->pos_past_bias = 0;
245 pli->pos_past_vit = 0;
246 pli->pos_past_fwd = 0;
247 pli->mode = mode;
248 pli->show_accessions = (go && esl_opt_GetBoolean(go, "--acc") ? TRUE : FALSE);
249 pli->show_alignments = (go && esl_opt_GetBoolean(go, "--noali") ? FALSE : TRUE);
250 pli->hfp = NULL;
251 pli->errbuf[0] = '\0';
252
253 return pli;
254
255 ERROR:
256 p7_pipeline_Destroy(pli);
257 return NULL;
258 }
259
260
261 /* Function: p7_pipeline_Reuse()
262 * Synopsis: Reuse a pipeline for next target.
263 *
264 * Purpose: Reuse <pli> for next target sequence (search mode)
265 * or model (scan mode).
266 *
267 * May eventually need to distinguish from reusing pipeline
268 * for next query, but we're not really focused on multiquery
269 * use of hmmscan/hmmsearch/phmmer for the moment.
270 */
271 int
272 p7_pipeline_Reuse(P7_PIPELINE *pli)
273 {
274 p7_omx_Reuse(pli->oxf);
275 p7_omx_Reuse(pli->oxb);
276 p7_omx_Reuse(pli->fwd);
277 p7_omx_Reuse(pli->bck);
278 p7_domaindef_Reuse(pli->ddef);
279 return eslOK;
280 }
281
282
283
284 /* Function: p7_pipeline_Destroy()
285 * Synopsis: Free a <P7_PIPELINE> object.
286 *
287 * Purpose: Free a <P7_PIPELINE> object.
288 */
289 void
290 p7_pipeline_Destroy(P7_PIPELINE *pli)
291 {
292 if (pli == NULL) return;
293
294 p7_omx_Destroy(pli->oxf);
295 p7_omx_Destroy(pli->oxb);
296 p7_omx_Destroy(pli->fwd);
297 p7_omx_Destroy(pli->bck);
298 esl_randomness_Destroy(pli->r);
299 p7_domaindef_Destroy(pli->ddef);
300 free(pli);
301 }
302 /*---------------- end, P7_PIPELINE object ----------------------*/
303
304
305 /*****************************************************************
306 * 2. The pipeline API.
307 *****************************************************************/
308
309 /* Function: p7_pli_ExtendAndMergeWindows
310 * Synopsis: Turns a list of ssv diagonals into windows, and merges
311 * overlapping windows.
312 *
313 * Purpose: Accepts a <windowlist> of SSV diagonals, extends those
314 * to windows based on a combination of the max_length
315 * value from <om> and the prefix and suffix lengths stored
316 * in <data>, then merges (in place) windows that overlap
317 * by more than <pct_overlap> percent, ensuring that windows
318 * stay within the bounds of 1..<L>.
319 *
320 * Returns: <eslOK>
321 */
322 int
323 p7_pli_ExtendAndMergeWindows (P7_OPROFILE *om, const P7_SCOREDATA *data, P7_HMM_WINDOWLIST *windowlist, float pct_overlap) {
324
325 int i;
326 P7_HMM_WINDOW *prev_window = NULL;
327 P7_HMM_WINDOW *curr_window = NULL;
328 int64_t window_start;
329 int64_t window_end;
330 int32_t window_len;
331 int64_t tmp;
332 int new_hit_cnt = 0;
333
334 if (windowlist->count == 0)
335 return eslOK;
336
337 /* extend windows */
338 for (i=0; i<windowlist->count; i++) {
339
340 curr_window = windowlist->windows+i;
341
342 if ( curr_window->complementarity == p7_COMPLEMENT) {
343 //flip for complement (then flip back), so the min and max bounds allow for appropriate overlap into neighboring segments in a multi-segment FM sequence
344 curr_window->n = curr_window->target_len - curr_window->n + 1;
345
346 window_start = ESL_MAX( 1 , curr_window->n - curr_window->length - (om->max_length * (0.1 + data->suffix_lengths[curr_window->k] ) ) ) ;
347 window_end = ESL_MIN( curr_window->target_len, curr_window->n + (om->max_length * (0.1 + data->prefix_lengths[curr_window->k - curr_window->length + 1] )) ) ;
348 tmp = window_end;
349 window_end = curr_window->target_len - window_start; // + 1;
350 window_start = curr_window->target_len - tmp ; //+ 1;
351
352 curr_window->n = curr_window->target_len - curr_window->n + 1;
353
354 } else {
355
356 // the 0.1 multiplier provides for a small buffer in excess of the predefined prefix/suffix lengths - one proportional to max_length
357 window_start = ESL_MAX( 1 , curr_window->n - (om->max_length * (0.1 + data->prefix_lengths[curr_window->k - curr_window->length + 1] )) ) ;
358 window_end = ESL_MIN( curr_window->target_len, curr_window->n + curr_window->length + (om->max_length * (0.1 + data->suffix_lengths[curr_window->k] ) ) ) ;
359 }
360
361 curr_window->length = window_end - window_start + 1;
362
363 curr_window->fm_n -= (curr_window->n - window_start);
364 curr_window->n = window_start;
365 }
366
367
368 /* merge overlapping windows, compressing list in place. */
369 for (i=1; i<windowlist->count; i++) {
370 prev_window = windowlist->windows+new_hit_cnt;
371 curr_window = windowlist->windows+i;
372
373 window_start = ESL_MAX(prev_window->n, curr_window->n);
374 window_end = ESL_MIN(prev_window->n+prev_window->length-1, curr_window->n+curr_window->length-1);
375 window_len = window_end - window_start + 1;
376
377 if ( prev_window->complementarity == curr_window->complementarity &&
378 prev_window->id == curr_window->id &&
379 (float)(window_len)/ESL_MIN(prev_window->length, curr_window->length) > pct_overlap // &&
380 //curr_window->n + curr_window->length >= prev_window->n + prev_window->length
381 )
382 {
383 //merge windows
384 window_start = ESL_MIN(prev_window->n, curr_window->n);
385 window_end = ESL_MAX(prev_window->n+prev_window->length-1, curr_window->n+curr_window->length-1);
386 prev_window->fm_n -= (prev_window->n - window_start);
387 prev_window->n = window_start;
388 prev_window->length = window_end - window_start + 1;
389 } else {
390 new_hit_cnt++;
391 windowlist->windows[new_hit_cnt] = windowlist->windows[i];
392 }
393 }
394 windowlist->count = new_hit_cnt+1;
395
396 return eslOK;
397 }
398
399 /* Function: p7_pli_TargetReportable
400 * Synopsis: Returns TRUE if target score meets reporting threshold.
401 *
402 * Purpose: Returns <TRUE> if the bit score <score> and/or
403 * log P-value <lnP> meet per-target reporting thresholds
404 * for the processing pipeline.
405 */
406 int
407 p7_pli_TargetReportable(P7_PIPELINE *pli, float score, double lnP)
408 {
409 if ( pli->by_E )
410 {
411 if ( !pli->long_targets && exp(lnP) * pli->Z <= pli->E) return TRUE;
412 if ( pli->long_targets && exp(lnP) <= pli->E) return TRUE; // database size is already built into the Pval if pli->targetlength == p7_TARGET_LONG
413 }
414 else if (! pli->by_E && score >= pli->T) return TRUE;
415
416 return FALSE;
417 }
418
419 /* Function: p7_pli_DomainReportable
420 * Synopsis: Returns TRUE if domain score meets reporting threshold.
421 *
422 * Purpose: Returns <TRUE> if the bit score <score> and/or
423 * log P-value <lnP> meet per-domain reporting thresholds
424 * for the processing pipeline.
425 */
426 int
427 p7_pli_DomainReportable(P7_PIPELINE *pli, float dom_score, double lnP)
428 {
429 if ( pli->dom_by_E )
430 {
431 if ( !pli->long_targets && exp(lnP) * pli->domZ <= pli->domE) return TRUE;
432 if ( pli->long_targets && exp(lnP) <= pli->domE) return TRUE;
433 }
434 else if (! pli->dom_by_E && dom_score >= pli->domT) return TRUE;
435 return FALSE;
436 }
437
438 /* Function: p7_pli_TargetIncludable()
439 * Synopsis: Returns TRUE if target score meets inclusion threshold.
440 */
441 int
442 p7_pli_TargetIncludable(P7_PIPELINE *pli, float score, double lnP)
443 {
444 if ( pli->inc_by_E )
445 {
446 if ( !pli->long_targets && exp(lnP) * pli->Z <= pli->incE) return TRUE;
447 if ( pli->long_targets && exp(lnP) <= pli->incE) return TRUE;
448 }
449
450 else if (! pli->inc_by_E && score >= pli->incT) return TRUE;
451
452 return FALSE;
453 }
454
455 /* Function: p7_pli_DomainIncludable()
456 * Synopsis: Returns TRUE if domain score meets inclusion threshold.
457 */
458 int
459 p7_pli_DomainIncludable(P7_PIPELINE *pli, float dom_score, double lnP)
460 {
461 if ( pli->incdom_by_E && exp(lnP) * pli->domZ <= pli->incdomE) return TRUE;
462 else if (! pli->incdom_by_E && dom_score >= pli->incdomT) return TRUE;
463 else return FALSE;
464 }
465
466
467
468
469 /* Function: p7_pli_NewModel()
470 * Synopsis: Prepare pipeline for a new model (target or query)
471 *
472 * Purpose: Caller has a new model <om>. Prepare the pipeline <pli>
473 * to receive this model as either a query or a target.
474 *
475 * If the "experimental" bias filter HMM is in use, this
476 * call resets it to use the new model's composition. This
477 * overwrites the bias filter HMM's expected length! You
478 * need to call <p7_bg_SetLength()> after a <NewModel()> call.
479 * (Failure to do this is bug #h85, 14 Dec 10.)
480 *
481 * The pipeline may alter the null model <bg> in a model-specific
482 * way (if we're using a composition bias filter HMM in the
483 * pipeline).
484 *
485 * Returns: <eslOK> on success.
486 *
487 * <eslEINVAL> if pipeline expects to be able to use a
488 * model's bit score thresholds, but this model does not
489 * have the appropriate ones set.
490 */
491 int
492 p7_pli_NewModel(P7_PIPELINE *pli, const P7_OPROFILE *om, P7_BG *bg)
493 {
494 int status = eslOK;
495
496 pli->nmodels++;
497 pli->nnodes += om->M;
498 if (pli->Z_setby == p7_ZSETBY_NTARGETS && pli->mode == p7_SCAN_MODELS) pli->Z = pli->nmodels;
499
500 if (pli->do_biasfilter) p7_bg_SetFilter(bg, om->M, om->compo);
501
502 if (pli->mode == p7_SEARCH_SEQS)
503 status = p7_pli_NewModelThresholds(pli, om);
504
505 pli->W = om->max_length;
506
507 return status;
508 }
509
510 /* Function: p7_pli_NewModelThresholds()
511 * Synopsis: Set reporting and inclusion bit score thresholds on a new model.
512 *
513 * Purpose: Set the bit score thresholds on a new model, if we're
514 * using Pfam GA, TC, or NC cutoffs for reporting or
515 * inclusion.
516 *
517 * In a "search" pipeline, this only needs to be done once
518 * per query model, so <p7_pli_NewModelThresholds()> gets
519 * called by <p7_pli_NewModel()>.
520 *
521 * In a "scan" pipeline, this needs to be called for each
522 * model, and it needs to be called after
523 * <p7_oprofile_ReadRest()>, because that's when the bit
524 * score thresholds get read.
525 *
526 * Returns: <eslOK> on success.
527 *
528 * <eslEINVAL> if pipeline expects to be able to use a
529 * model's bit score thresholds, but this model does not
530 * have the appropriate ones set.
531 *
532 * Xref: Written to fix bug #h60.
533 */
534 int
535 p7_pli_NewModelThresholds(P7_PIPELINE *pli, const P7_OPROFILE *om)
536 {
537
538 if (pli->use_bit_cutoffs)
539 {
540 if (pli->use_bit_cutoffs == p7H_GA)
541 {
542 if (om->cutoff[p7_GA1] == p7_CUTOFF_UNSET)
543 ESL_FAIL(eslEINVAL, pli->errbuf, "GA bit thresholds unavailable on model %s\n", om->name);
544 pli->T = pli->incT = om->cutoff[p7_GA1];
545 pli->domT = pli->incdomT = om->cutoff[p7_GA2];
546 }
547 else if (pli->use_bit_cutoffs == p7H_TC)
548 {
549 if (om->cutoff[p7_TC1] == p7_CUTOFF_UNSET)
550 ESL_FAIL(eslEINVAL, pli->errbuf, "TC bit thresholds unavailable on model %s\n", om->name);
551 pli->T = pli->incT = om->cutoff[p7_TC1];
552 pli->domT = pli->incdomT = om->cutoff[p7_TC2];
553 }
554 else if (pli->use_bit_cutoffs == p7H_NC)
555 {
556 if (om->cutoff[p7_NC1] == p7_CUTOFF_UNSET)
557 ESL_FAIL(eslEINVAL, pli->errbuf, "NC bit thresholds unavailable on model %s\n", om->name);
558 pli->T = pli->incT = om->cutoff[p7_NC1];
559 pli->domT = pli->incdomT = om->cutoff[p7_NC2];
560 }
561 }
562
563 return eslOK;
564 }
565
566
567 /* Function: p7_pli_NewSeq()
568 * Synopsis: Prepare pipeline for a new sequence (target or query)
569 *
570 * Purpose: Caller has a new sequence <sq>. Prepare the pipeline <pli>
571 * to receive this model as either a query or a target.
572 *
573 * Returns: <eslOK> on success.
574 */
575 int
576 p7_pli_NewSeq(P7_PIPELINE *pli, const ESL_SQ *sq)
577 {
578 if (! pli->long_targets) { // if long_targets, sequence counting happens in main loop of the master thread, which can track multiple windows for a single long sequence
579 pli->nseqs++;
580 if (pli->Z_setby == p7_ZSETBY_NTARGETS && pli->mode == p7_SEARCH_SEQS) pli->Z = pli->nseqs; // ... whereas worker threads call p7_pli_NewSeq(), so pli->nseqs can't even be read, when in longtargets mode
581 }
582 pli->nres += sq->n;
583 return eslOK;
584
585 // Note on what nhmmer (long_targets mode) is doing w.r.t. threads and nseqs/nres.
586 //
587 // nhmmer counts pli->nseqs in the master thread (albeit with a possible
588 // adjustment at the end in FM-indexing mode). nres, though, is
589 // counted by *worker* threads, by calling p7_pli_NewSeq(), followed
590 // by adjustments in pipeline_thread() for window overlap and the
591 // opposite strand.
592 //
593 // To avoid thread races, in longtarget mode you must make sure
594 // you only touch nres here, not nseqs. Since nhmmer does not use
595 // nseqs for E-value calculations (it uses nres/W) and furthermore
596 // it does not use lower bound E-values during a search the way
597 // hmmsearch does, we can just have p7_pli_NewSeq() update nres
598 // and ignore anything having to do with nseqs.
599 }
600
601 /* Function: p7_pipeline_Merge()
602 * Synopsis: Merge the pipeline statistics
603 *
604 * Purpose: Caller has a new model <om>. Prepare the pipeline <pli>
605 * to receive this model as either a query or a target.
606 *
607 * The pipeline may alter the null model <bg> in a model-specific
608 * way (if we're using a composition bias filter HMM in the
609 * pipeline).
610 *
611 * Returns: <eslOK> on success.
612 *
613 * <eslEINVAL> if pipeline expects to be able to use a
614 * model's bit score thresholds, but this model does not
615 * have the appropriate ones set.
616 */
617 int
618 p7_pipeline_Merge(P7_PIPELINE *p1, P7_PIPELINE *p2)
619 {
620 /* if we are searching a sequence database, we need to keep track of the
621 * number of sequences and residues processed.
622 */
623 if (p1->mode == p7_SEARCH_SEQS)
624 {
625 p1->nseqs += p2->nseqs;
626 p1->nres += p2->nres;
627 }
628 else
629 {
630 p1->nmodels += p2->nmodels;
631 p1->nnodes += p2->nnodes;
632 }
633
634 p1->n_past_msv += p2->n_past_msv;
635 p1->n_past_bias += p2->n_past_bias;
636 p1->n_past_vit += p2->n_past_vit;
637 p1->n_past_fwd += p2->n_past_fwd;
638 p1->n_output += p2->n_output;
639
640 p1->pos_past_msv += p2->pos_past_msv;
641 p1->pos_past_bias += p2->pos_past_bias;
642 p1->pos_past_vit += p2->pos_past_vit;
643 p1->pos_past_fwd += p2->pos_past_fwd;
644 p1->pos_output += p2->pos_output;
645
646 if (p1->Z_setby == p7_ZSETBY_NTARGETS)
647 {
648 p1->Z += (p1->mode == p7_SCAN_MODELS) ? p2->nmodels : p2->nseqs;
649 }
650 else
651 {
652 p1->Z = p2->Z;
653 }
654
655 return eslOK;
656 }
657
658 /* Function: p7_Pipeline()
659 * Synopsis: HMMER3's accelerated seq/profile comparison pipeline.
660 *
661 * Purpose: Run H3's accelerated pipeline to compare profile <om>
662 * against sequence <sq>. If a significant hit is found,
663 * information about it is added to the <hitlist>. The pipeline
664 * accumulates beancounting information about how many comparisons
665 * flow through the pipeline while it's active.
666 *
667 * Returns: <eslOK> on success. If a significant hit is obtained,
668 * its information is added to the growing <hitlist>.
669 *
670 * <eslEINVAL> if (in a scan pipeline) we're supposed to
671 * set GA/TC/NC bit score thresholds but the model doesn't
672 * have any.
673 *
674 * <eslERANGE> on numerical overflow errors in the
675 * optimized vector implementations; particularly in
676 * posterior decoding. I don't believe this is possible for
677 * multihit local models, but I'm set up to catch it
678 * anyway. We may emit a warning to the user, but cleanly
679 * skip the problematic sequence and continue.
680 *
681 * Throws: <eslEMEM> on allocation failure.
682 *
683 * <eslETYPE> if <sq> is more than 100K long, which can
684 * happen when someone uses hmmsearch/hmmscan instead of
685 * nhmmer/nhmmscan on a genome DNA seq db.
686 *
687 * Xref: J4/25.
688 *
689 * Note: Error handling needs improvement. The <eslETYPE> exception
690 * was added as a late bugfix. It really should be an <eslEINVAL>
691 * normal error (because it's a user error). But then we need
692 * all our p7_Pipeline() calls to check their return status
693 * and handle normal errors appropriately, which we haven't
694 * been careful enough about. [SRE H9/4]
695 */
696 int
697 p7_Pipeline(P7_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, const ESL_SQ *sq, const ESL_SQ *ntsq, P7_TOPHITS *hitlist)
698 {
699 P7_HIT *hit = NULL; /* ptr to the current hit output data */
700 float usc, vfsc, fwdsc; /* filter scores */
701 float filtersc; /* HMM null filter score */
702 float nullsc; /* null model score */
703 float seqbias;
704 float seq_score; /* the corrected per-seq bit score */
705 float sum_score; /* the corrected reconstruction score for the seq */
706 float pre_score, pre2_score; /* uncorrected bit scores for seq */
707 double P; /* P-value of a hit */
708 double lnP; /* log P-value of a hit */
709 int Ld; /* # of residues in envelopes */
710 int d;
711 int status;
712
713 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
714 if (sq->n > 100000) ESL_EXCEPTION(eslETYPE, "Target sequence length > 100K, over comparison pipeline limit.\n(Did you mean to use nhmmer/nhmmscan?)");
715
716 p7_omx_GrowTo(pli->oxf, om->M, 0, sq->n); /* expand the one-row omx if needed */
717
718 /* Base null model score (we could calculate this in NewSeq(), for a scan pipeline) */
719 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
720
721 /* First level filter: the MSV filter, multihit with <om> */
722 p7_MSVFilter(sq->dsq, sq->n, om, pli->oxf, &usc);
723 seq_score = (usc - nullsc) / eslCONST_LOG2;
724 P = esl_gumbel_surv(seq_score, om->evparam[p7_MMU], om->evparam[p7_MLAMBDA]);
725 if (P > pli->F1) return eslOK;
726 pli->n_past_msv++;
727
728 /* biased composition HMM filtering */
729 if (pli->do_biasfilter)
730 {
731 p7_bg_FilterScore(bg, sq->dsq, sq->n, &filtersc);
732 seq_score = (usc - filtersc) / eslCONST_LOG2;
733 P = esl_gumbel_surv(seq_score, om->evparam[p7_MMU], om->evparam[p7_MLAMBDA]);
734 if (P > pli->F1) return eslOK;
735 }
736 else filtersc = nullsc;
737 pli->n_past_bias++;
738
739 /* In scan mode, if it passes the MSV filter, read the rest of the profile */
740 if (pli->mode == p7_SCAN_MODELS)
741 {
742 if (pli->hfp) p7_oprofile_ReadRest(pli->hfp, om);
743 p7_oprofile_ReconfigRestLength(om, sq->n);
744 if ((status = p7_pli_NewModelThresholds(pli, om)) != eslOK) return status; /* pli->errbuf has err msg set */
745 }
746
747 /* Second level filter: ViterbiFilter(), multihit with <om> */
748 if (P > pli->F2)
749 {
750 p7_ViterbiFilter(sq->dsq, sq->n, om, pli->oxf, &vfsc);
751 seq_score = (vfsc-filtersc) / eslCONST_LOG2;
752 P = esl_gumbel_surv(seq_score, om->evparam[p7_VMU], om->evparam[p7_VLAMBDA]);
753 if (P > pli->F2) return eslOK;
754 }
755 pli->n_past_vit++;
756
757
758 /* Parse it with Forward and obtain its real Forward score. */
759 p7_ForwardParser(sq->dsq, sq->n, om, pli->oxf, &fwdsc);
760 seq_score = (fwdsc-filtersc) / eslCONST_LOG2;
761 P = esl_exp_surv(seq_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
762 if (P > pli->F3) return eslOK;
763 pli->n_past_fwd++;
764
765 /* ok, it's for real. Now a Backwards parser pass, and hand it to domain definition workflow */
766 p7_omx_GrowTo(pli->oxb, om->M, 0, sq->n);
767 p7_BackwardParser(sq->dsq, sq->n, om, pli->oxf, pli->oxb, NULL);
768
769 status = p7_domaindef_ByPosteriorHeuristics(sq, ntsq, om, pli->oxf, pli->oxb, pli->fwd, pli->bck, pli->ddef, bg, FALSE, NULL, NULL, NULL);
770 if (status != eslOK) ESL_FAIL(status, pli->errbuf, "domain definition workflow failure"); /* eslERANGE can happen */
771 if (pli->ddef->nregions == 0) return eslOK; /* score passed threshold but there's no discrete domains here */
772 if (pli->ddef->nenvelopes == 0) return eslOK; /* rarer: region was found, stochastic clustered, no envelopes found */
773 if (pli->ddef->ndom == 0) return eslOK; /* even rarer: envelope found, no domain identified {iss131} */
774
775
776 /* Calculate the null2-corrected per-seq score */
777 if (pli->do_null2)
778 {
779 seqbias = esl_vec_FSum(pli->ddef->n2sc, sq->n+1);
780 seqbias = p7_FLogsum(0.0, log(bg->omega) + seqbias);
781 }
782 else seqbias = 0.0;
783 pre_score = (fwdsc - nullsc) / eslCONST_LOG2;
784 seq_score = (fwdsc - (nullsc + seqbias)) / eslCONST_LOG2;
785
786
787 /* Calculate the "reconstruction score": estimated
788 * per-sequence score as sum of individual domains,
789 * discounting domains that aren't significant after they're
790 * null-corrected.
791 */
792 sum_score = 0.0f;
793 seqbias = 0.0f;
794
795 Ld = 0;
796 if (pli->do_null2)
797 {
798 for (d = 0; d < pli->ddef->ndom; d++)
799 {
800 if (pli->ddef->dcl[d].envsc - pli->ddef->dcl[d].domcorrection > 0.0)
801 {
802 sum_score += pli->ddef->dcl[d].envsc; /* NATS */
803 Ld += pli->ddef->dcl[d].jenv - pli->ddef->dcl[d].ienv + 1;
804 seqbias += pli->ddef->dcl[d].domcorrection; /* NATS */
805 }
806 }
807 seqbias = p7_FLogsum(0.0, log(bg->omega) + seqbias); /* NATS */
808 }
809 else
810 {
811 for (d = 0; d < pli->ddef->ndom; d++)
812 {
813 if (pli->ddef->dcl[d].envsc > 0.0)
814 {
815 sum_score += pli->ddef->dcl[d].envsc; /* NATS */
816 Ld += pli->ddef->dcl[d].jenv - pli->ddef->dcl[d].ienv + 1;
817 }
818 }
819 seqbias = 0.0;
820 }
821 sum_score += (sq->n-Ld) * log((float) sq->n / (float) (sq->n+3)); /* NATS */
822 pre2_score = (sum_score - nullsc) / eslCONST_LOG2; /* BITS */
823 sum_score = (sum_score - (nullsc + seqbias)) / eslCONST_LOG2; /* BITS */
824
825 /* A special case: let sum_score override the seq_score when it's better, and it includes at least 1 domain */
826 if (Ld > 0 && sum_score > seq_score)
827 {
828 seq_score = sum_score;
829 pre_score = pre2_score;
830 }
831
832 /* Apply thresholding and determine whether to put this
833 * target into the hit list. E-value thresholding may
834 * only be a lower bound for now, so this list may be longer
835 * than eventually reported.
836 */
837 lnP = esl_exp_logsurv (seq_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
838 if (p7_pli_TargetReportable(pli, seq_score, lnP))
839 {
840 p7_tophits_CreateNextHit(hitlist, &hit);
841 if (pli->mode == p7_SEARCH_SEQS) {
842 if ( (status = esl_strdup(sq->name, -1, &(hit->name))) != eslOK) ESL_EXCEPTION(eslEMEM, "allocation failure");
843 if (sq->acc[0] != '\0' && (status = esl_strdup(sq->acc, -1, &(hit->acc))) != eslOK) ESL_EXCEPTION(eslEMEM, "allocation failure");
844 if (sq->desc[0] != '\0' && (status = esl_strdup(sq->desc, -1, &(hit->desc))) != eslOK) ESL_EXCEPTION(eslEMEM, "allocation failure");
845 } else {
846 if ((status = esl_strdup(om->name, -1, &(hit->name))) != eslOK) esl_fatal("allocation failure");
847 if ((status = esl_strdup(om->acc, -1, &(hit->acc))) != eslOK) esl_fatal("allocation failure");
848 if ((status = esl_strdup(om->desc, -1, &(hit->desc))) != eslOK) esl_fatal("allocation failure");
849 }
850 hit->ndom = pli->ddef->ndom;
851 hit->nexpected = pli->ddef->nexpected;
852 hit->nregions = pli->ddef->nregions;
853 hit->nclustered = pli->ddef->nclustered;
854 hit->noverlaps = pli->ddef->noverlaps;
855 hit->nenvelopes = pli->ddef->nenvelopes;
856
857 hit->pre_score = pre_score; /* BITS */
858 hit->pre_lnP = esl_exp_logsurv (hit->pre_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
859
860 hit->score = seq_score; /* BITS */
861 hit->lnP = lnP;
862 hit->sortkey = pli->inc_by_E ? -lnP : seq_score; /* per-seq output sorts on bit score if inclusion is by score */
863
864 hit->sum_score = sum_score; /* BITS */
865 hit->sum_lnP = esl_exp_logsurv (hit->sum_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
866
867 /* Transfer all domain coordinates (unthresholded for
868 * now) with their alignment displays to the hit list,
869 * associated with the sequence. Domain reporting will
870 * be thresholded after complete hit list is collected,
871 * because we probably need to know # of significant
872 * hits found to set domZ, and thence threshold and
873 * count reported domains.
874 */
875 hit->dcl = pli->ddef->dcl;
876 pli->ddef->dcl = NULL;
877 hit->best_domain = 0;
878 for (d = 0; d < hit->ndom; d++)
879 {
880 Ld = hit->dcl[d].jenv - hit->dcl[d].ienv + 1;
881 hit->dcl[d].bitscore = hit->dcl[d].envsc + (sq->n-Ld) * log((float) sq->n / (float) (sq->n+3)); /* NATS, for the moment... */
882 hit->dcl[d].dombias = (pli->do_null2 ? p7_FLogsum(0.0, log(bg->omega) + hit->dcl[d].domcorrection) : 0.0); /* NATS, and will stay so */
883 hit->dcl[d].bitscore = (hit->dcl[d].bitscore - (nullsc + hit->dcl[d].dombias)) / eslCONST_LOG2; /* now BITS, as it should be */
884 hit->dcl[d].lnP = esl_exp_logsurv (hit->dcl[d].bitscore, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
885
886 if (hit->dcl[d].bitscore > hit->dcl[hit->best_domain].bitscore) hit->best_domain = d;
887 }
888
889 /* If we're using model-specific bit score thresholds (GA | TC |
890 * NC) and we're in an hmmscan pipeline (mode = p7_SCAN_MODELS),
891 * then we *must* apply those reporting or inclusion thresholds
892 * now, because this model is about to go away; we won't have
893 * its thresholds after all targets have been processed.
894 *
895 * If we're using E-value thresholds and we don't know the
896 * search space size (Z_setby or domZ_setby =
897 * p7_ZSETBY_NTARGETS), we *cannot* apply those thresholds now,
898 * and we *must* wait until all targets have been processed
899 * (see p7_tophits_Threshold()).
900 *
901 * For any other thresholding, it doesn't matter whether we do
902 * it here (model-specifically) or at the end (in
903 * p7_tophits_Threshold()).
904 *
905 * What we actually do, then, is to set the flags if we're using
906 * model-specific score thresholds (regardless of whether we're
907 * in a scan or a search pipeline); otherwise we leave it to
908 * p7_tophits_Threshold(). p7_tophits_Threshold() is always
909 * responsible for *counting* the reported, included sequences.
910 *
911 * [xref J5/92]
912 */
913 if (pli->use_bit_cutoffs)
914 {
915 if (p7_pli_TargetReportable(pli, hit->score, hit->lnP))
916 {
917 hit->flags |= p7_IS_REPORTED;
918 if (p7_pli_TargetIncludable(pli, hit->score, hit->lnP))
919 hit->flags |= p7_IS_INCLUDED;
920 }
921
922 for (d = 0; d < hit->ndom; d++)
923 {
924 if (p7_pli_DomainReportable(pli, hit->dcl[d].bitscore, hit->dcl[d].lnP))
925 {
926 hit->dcl[d].is_reported = TRUE;
927 if (p7_pli_DomainIncludable(pli, hit->dcl[d].bitscore, hit->dcl[d].lnP))
928 hit->dcl[d].is_included = TRUE;
929 }
930 }
931 }
932
933 }
934
935 return eslOK;
936 }
937
938
939
940 /* Function: p7_pli_computeAliScores()
941 * Synopsis: Compute per-position scores for the alignment for a domain
942 *
943 * Purpose: Compute per-position (Viterbi) scores for the alignment for a domain,
944 * for the purpose of optionally printing these scores out in association
945 * with each alignment. Such scores can, for example, be used to detangle
946 * overlapping alignments (from different models)
947 *
948 * Args: dom - domain with the alignment for which we wish to compute scores
949 * seq - sequence in which domain resides
950 * data - contains model's emission and transition values in unstriped form
951 * K - alphabet size
952 *
953 * Returns: <eslOK> on success.
954 *
955 * Throws: <eslEMEM> on allocation failure.
956 */
957 static int
958 p7_pli_computeAliScores(P7_DOMAIN *dom, ESL_DSQ *seq, const P7_SCOREDATA *data, int K)
959 {
960 int status;
961 int i, j, k;
962 float sc;
963
964 //Compute score contribution of each position in the alignment to the overall Viterbi score
965 ESL_ALLOC( dom->scores_per_pos, sizeof(float) * dom->ad->N );
966 for (i=0; i<dom->ad->N; i++) dom->scores_per_pos[i] = 0.0;
967
968 i = dom->iali - 1; //sequence position
969 j = dom->ad->hmmfrom - 1; //model position
970 k = 0;
971 while ( k<dom->ad->N) {
972 if (dom->ad->model[k] != '.' && dom->ad->aseq[k] != '-') { //match
973 i++; j++;
974 // Including the MM cost is a hack. The cost of getting to/from this match
975 // state does matter, but an IM or DM transition would improperly deflate
976 // the score of this column, so just give MM. That amount is offset out of
977 // the score shown for preceding indels
978 dom->scores_per_pos[k] = data->fwd_scores[K * j + seq[i]]
979 + (j==1 ? 0 : log(data->fwd_transitions[p7O_MM][j]) );
980 k++;
981 } else if (dom->ad->model[k] == '.' ) { // insert
982 //spin through the insert, accumulating cost; only assign to final column in gap
983 dom->scores_per_pos[k] = -eslINFINITY;
984
985 sc = log(data->fwd_transitions[p7O_MI][j]);
986 i++; k++;
987 while (k<dom->ad->N && dom->ad->model[k] == '.') { //extend insert
988 dom->scores_per_pos[k] = -eslINFINITY;
989 sc += log(data->fwd_transitions[p7O_II][j]);
990 i++; k++;
991 }
992 sc += log(data->fwd_transitions[p7O_IM][j+1]) - log(data->fwd_transitions[p7O_MM][j+1]);
993 dom->scores_per_pos[k-1] = sc;
994
995 } else if (dom->ad->aseq[k] == '-' ) { // delete
996 dom->scores_per_pos[k] = -eslINFINITY;
997 sc = log(data->fwd_transitions[p7O_MD][j]);
998 j++; k++;
999 while (k<dom->ad->N && dom->ad->aseq[k] == '-') { //extend delete
1000 dom->scores_per_pos[k] = -eslINFINITY;
1001 sc += log(data->fwd_transitions[p7O_DD][j]);
1002 j++; k++;
1003 }
1004 sc += log(data->fwd_transitions[p7O_DM][j+1]) - log(data->fwd_transitions[p7O_MM][j+1]);
1005 dom->scores_per_pos[k-1] = sc;
1006 }
1007 }
1008
1009 return eslOK;
1010
1011 ERROR:
1012 return eslEMEM;
1013
1014 }
1015
1016
1017 /* Function: p7_pli_postViterbi_LongTarget()
1018 * Synopsis: the part of the LongTarget P7 search Pipeline downstream
1019 * of the Viterbi filter
1020 *
1021 * Purpose: This is called by postMSV_LongTarget(), and runs the
1022 * post-Viterbi part of HMMER's accelerated pipeline to
1023 * compare profile <om> against sequence <sq>. If a
1024 * significant hit is found, information about it is
1025 * added to the <hitlist>.
1026 * The pipeline accumulates beancounting information
1027 * about how many comparisons (and residues) flow through
1028 * the pipeline while it's active.
1029 *
1030 * Args: pli - the main pipeline object
1031 * om - optimized profile (query)
1032 * bg - background model
1033 * hitlist - pointer to hit storage bin
1034 * data - for computing windows based on maximum prefix/suffix extensions
1035 * seqidx - the id # of the sequence from which the current window was extracted
1036 * window_start - the starting position of the extracted window (offset from the first
1037 * position of the block of a possibly longer sequence)
1038 * window_len - the length of the extracted window
1039 * subseq - digital sequence of the extracted window
1040 * seq_start - first position of the sequence block passed in to the calling pipeline function
1041 * seq_name - name of the sequence the window comes from
1042 * seq_source - source of the sequence the window comes from
1043 * seq_acc - acc of the sequence the window comes from
1044 * seq_desc - desc of the sequence the window comes from
1045 * seq_len - length of the sequence the window comes from (Available from FM; otherwise 0 and to be ignored)
1046 * complementarity - boolean; is the passed window sourced from a complementary sequence block
1047 * overlap - number of residues in this sequence window that overlap a preceding window.
1048 * pli_tmp - a collection of objects used in the long target pipeline that should be
1049 * (and are) only allocated once per pipeline to minimize alloc overhead.
1050
1051 * Returns: <eslOK> on success. If a significant hit is obtained,
1052 * its information is added to the growing <hitlist>.
1053 *
1054 * <eslERANGE> on numerical overflow errors in the
1055 * optimized vector implementations; particularly in
1056 * posterior decoding. I don't believe this is possible for
1057 * multihit local models, but I'm set up to catch it
1058 * anyway. We may emit a warning to the user, but cleanly
1059 * skip the problematic sequence and continue.
1060 *
1061 * Throws: <eslEMEM> on allocation failure.
1062 *
1063 */
1064 static int
1065 p7_pli_postViterbi_LongTarget(P7_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, P7_TOPHITS *hitlist, const P7_SCOREDATA *data,
1066 int64_t seqidx, int window_start, int window_len, ESL_DSQ *subseq,
1067 int64_t seq_start, char *seq_name, char *seq_source, char* seq_acc, char* seq_desc, int seq_len,
1068 int complementarity, int *overlap, P7_PIPELINE_LONGTARGET_OBJS *pli_tmp)
1069 {
1070 P7_DOMAIN *dom = NULL; /* convenience variable, ptr to current domain */
1071 P7_HIT *hit = NULL; /* ptr to the current hit output data */
1072 float fwdsc; /* filter scores */
1073 float nullsc;
1074 float filtersc; /* HMM null filter score */
1075 float bias_filtersc; /* HMM null filter score */
1076 float seq_score; /* the corrected per-seq bit score */
1077 double P; /* P-value of a hit */
1078 int d;
1079 int status;
1080 // int nres;
1081 ESL_DSQ *dsq_holder;
1082
1083 int env_len;
1084 int ali_len;
1085 float bitscore;
1086 float dom_bias;
1087 float dom_score;
1088 double dom_lnP;
1089
1090 int F3_L = ESL_MIN( window_len, pli->B3);
1091
1092 p7_bg_SetLength(bg, window_len);
1093 p7_bg_NullOne (bg, subseq, window_len, &nullsc);
1094 if (pli->do_biasfilter)
1095 {
1096 p7_bg_FilterScore(bg, subseq, window_len, &bias_filtersc);
1097 bias_filtersc -= nullsc; //remove nullsc, so bias scaling can be done, then add it back on later
1098 } else {
1099 bias_filtersc = 0;
1100 }
1101
1102 p7_oprofile_ReconfigRestLength(om, window_len);
1103
1104 /* Parse with Forward and obtain its real Forward score. */
1105 p7_ForwardParser(subseq, window_len, om, pli->oxf, &fwdsc);
1106 filtersc = nullsc + (bias_filtersc * ( F3_L>window_len ? 1.0 : (float)F3_L/window_len) );
1107 seq_score = (fwdsc - filtersc) / eslCONST_LOG2;
1108 P = esl_exp_surv(seq_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
1109 if (P > pli->F3 ) return eslOK;
1110
1111 pli->pos_past_fwd += window_len - *overlap;
1112
1113 *overlap = -1; // overload variable to tell calling function that this window passed fwd
1114
1115 /*now that almost everything has been filtered away, set up seq object for domaindef function*/
1116 if ((status = esl_sq_SetName (pli_tmp->tmpseq, seq_name)) != eslOK) goto ERROR;
1117 if ((status = esl_sq_SetSource (pli_tmp->tmpseq, seq_source)) != eslOK) goto ERROR;
1118 if ((status = esl_sq_SetAccession(pli_tmp->tmpseq, seq_acc)) != eslOK) goto ERROR;
1119 if ((status = esl_sq_SetDesc (pli_tmp->tmpseq, seq_desc)) != eslOK) goto ERROR;
1120 pli_tmp->tmpseq->L = seq_len;
1121 pli_tmp->tmpseq->n = window_len;
1122 dsq_holder = pli_tmp->tmpseq->dsq; // will point back to the original at the end
1123 pli_tmp->tmpseq->dsq = subseq;
1124
1125 /* Now a Backwards parser pass, and hand it to domain definition workflow
1126 * In this case "domains" will end up being translated as independent "hits" */
1127 p7_omx_GrowTo(pli->oxb, om->M, 0, window_len);
1128 p7_BackwardParser(subseq, window_len, om, pli->oxf, pli->oxb, NULL);
1129
1130 //if we're asked to not do null correction, pass a NULL instead of a temp scores variable - domaindef knows what to do
1131 status = p7_domaindef_ByPosteriorHeuristics(pli_tmp->tmpseq, NULL, om, pli->oxf, pli->oxb, pli->fwd, pli->bck, pli->ddef, bg, TRUE,
1132 pli_tmp->bg, (pli->do_null2?pli_tmp->scores:NULL), pli_tmp->fwd_emissions_arr);
1133
1134 pli_tmp->tmpseq->dsq = dsq_holder;
1135 if (status != eslOK) ESL_FAIL(status, pli->errbuf, "domain definition workflow failure"); /* eslERANGE can happen */
1136 if (pli->ddef->nregions == 0) return eslOK; /* score passed threshold but there's no discrete domains here */
1137 if (pli->ddef->nenvelopes == 0) return eslOK; /* rarer: region was found, stochastic clustered, no envelopes found */
1138
1139
1140 /* Put these hits ("domains") into the hit list.
1141 *
1142 * Modified original pipeline to create a single hit for each
1143 * domain, so the remainder of the typical-case hit-merging
1144 * process can remain mostly intact.
1145 *
1146 * Some of them may not pass eventual E-value thresholds. In
1147 * protein context, these would be reported as supplementary
1148 * data (domains contributing to a full-sequence score), but
1149 * in nhmmer context, they'll just get thrown away later, so
1150 * drop them now, if possible.
1151 */
1152 for (d = 0; d < pli->ddef->ndom; d++)
1153 {
1154
1155 dom = pli->ddef->dcl + d;
1156
1157 //adjust the score of a hit to account for the full length model - the characters outside the envelope but in the window
1158 env_len = dom->jenv - dom->ienv + 1;
1159 ali_len = dom->jali - dom->iali + 1;
1160 bitscore = dom->envsc ;
1161
1162
1163 if (ali_len < 8) {
1164 p7_alidisplay_Destroy(dom->ad);
1165 continue; // anything less than this is a funny byproduct of the Forward score passing a very low threshold, but no reliable alignment existing that supports it
1166 }
1167
1168 /* Note: this bitscore was computed under a model with length of
1169 * env_len (jenv-ienv+1). Here, the score is modified (reduced) by
1170 * treating the hit as though it came from a window of length
1171 * om->max_length. To do this:
1172 */
1173
1174 // (1) the entrance/exit costs are shifted from env_len to max_length:
1175 bitscore -= 2 * log(2. / (env_len+2)) ;
1176 bitscore += 2 * log(2. / (om->max_length+2)) ;
1177
1178 // (2) the extension cost for going from ali bounds to env bounds is removed,
1179 // and replaced with the cost of going from ali bounds to max length (or env
1180 // bounds in the extremely rare case that the env_len is actually larger than om->max_length).
1181 bitscore -= (env_len-ali_len) * log((float)env_len / (env_len+2));
1182 bitscore += (ESL_MAX(om->max_length, env_len) - ali_len) * log((float)om->max_length / (float) (om->max_length+2));
1183
1184 /* Compute scores used to decide if we should keep this "domain" as a hit.
1185 * Note that the bias correction was captured in dom->domcorrection during
1186 * the p7_domaindef_ByPosteriorHeuristics() call.
1187 */
1188 dom_bias = dom->domcorrection;
1189 p7_bg_SetLength(bg, ESL_MAX(om->max_length, env_len));
1190 p7_bg_NullOne (bg, subseq, ESL_MAX(om->max_length, env_len), &nullsc);
1191 dom_score = (bitscore - (nullsc)) / eslCONST_LOG2;
1192 dom_lnP = esl_exp_logsurv(dom_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
1193
1194 if (pli->do_alignment_score_calc)
1195 p7_pli_computeAliScores(dom, subseq, data, om->abc->Kp);
1196
1197 p7_tophits_CreateNextHit(hitlist, &hit);
1198
1199 hit->ndom = 1;
1200 hit->best_domain = 0;
1201
1202 hit->window_length = om->max_length;
1203 hit->seqidx = seqidx;
1204 hit->subseq_start = seq_start;
1205
1206 ESL_ALLOC(hit->dcl, sizeof(P7_DOMAIN) );
1207 hit->dcl[0] = pli->ddef->dcl[d];
1208
1209 hit->dcl[0].ad->L = seq_len;
1210
1211
1212 // compute the real positions within the sequence handed to the pipeline
1213 if (complementarity == p7_NOCOMPLEMENT) {
1214 hit->dcl[0].ienv += seq_start + window_start - 2;
1215 hit->dcl[0].jenv += seq_start + window_start - 2;
1216 hit->dcl[0].iali += seq_start + window_start - 2;
1217 hit->dcl[0].jali += seq_start + window_start - 2;
1218 hit->dcl[0].ad->sqfrom += seq_start + window_start - 2;
1219 hit->dcl[0].ad->sqto += seq_start + window_start - 2;
1220 } else {
1221 hit->dcl[0].ienv = seq_start - (window_start + hit->dcl[0].ienv) + 2;
1222 hit->dcl[0].jenv = seq_start - (window_start + hit->dcl[0].jenv) + 2;
1223 hit->dcl[0].iali = seq_start - (window_start + hit->dcl[0].iali) + 2;
1224 hit->dcl[0].jali = seq_start - (window_start + hit->dcl[0].jali) + 2;
1225 hit->dcl[0].ad->sqfrom = seq_start - (window_start + hit->dcl[0].ad->sqfrom) + 2;
1226 hit->dcl[0].ad->sqto = seq_start - (window_start + hit->dcl[0].ad->sqto) + 2;
1227 }
1228 hit->pre_score = bitscore / eslCONST_LOG2;
1229 hit->pre_lnP = esl_exp_logsurv (hit->pre_score, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
1230
1231 hit->dcl[0].dombias = dom_bias;
1232 hit->sum_score = hit->score = hit->dcl[0].bitscore = dom_score;
1233 hit->sum_lnP = hit->lnP = hit->dcl[0].lnP = dom_lnP;
1234
1235
1236 if (pli->mode == p7_SEARCH_SEQS)
1237 {
1238 if ( (status = esl_strdup(seq_name, -1, &(hit->name))) != eslOK) ESL_EXCEPTION(eslEMEM, "allocation failure");
1239 if (seq_acc[0] != '\0' && (status = esl_strdup(seq_acc, -1, &(hit->acc))) != eslOK) ESL_EXCEPTION(eslEMEM, "allocation failure");
1240 if (seq_desc[0] != '\0' && (status = esl_strdup(seq_desc, -1, &(hit->desc))) != eslOK) ESL_EXCEPTION(eslEMEM, "allocation failure");
1241 } else {
1242 if ((status = esl_strdup(om->name, -1, &(hit->name))) != eslOK) esl_fatal("allocation failure");
1243 if ((status = esl_strdup(om->acc, -1, &(hit->acc))) != eslOK) esl_fatal("allocation failure");
1244 if ((status = esl_strdup(om->desc, -1, &(hit->desc))) != eslOK) esl_fatal("allocation failure");
1245 }
1246
1247
1248 /* If using model-specific thresholds, filter now. See notes in front
1249 * of the analogous piece of code in p7_Pipeline() for further explanation
1250 * of timing.
1251 */
1252 if (pli->use_bit_cutoffs)
1253 {
1254 if (p7_pli_TargetReportable(pli, hit->score, hit->lnP))
1255 {
1256 hit->flags |= p7_IS_REPORTED;
1257 if (p7_pli_TargetIncludable(pli, hit->score, hit->lnP))
1258 hit->flags |= p7_IS_INCLUDED;
1259 }
1260
1261 if (p7_pli_DomainReportable(pli, hit->dcl[0].bitscore, hit->dcl[0].lnP))
1262 {
1263 hit->dcl[0].is_reported = TRUE;
1264 if (p7_pli_DomainIncludable(pli, hit->dcl[0].bitscore, hit->dcl[0].lnP))
1265 hit->dcl[0].is_included = TRUE;
1266 }
1267
1268 }
1269
1270 }
1271
1272 return eslOK;
1273
1274 ERROR:
1275 ESL_EXCEPTION(eslEMEM, "Error in LongTarget pipeline\n");
1276
1277 }
1278
1279
1280 /* Function: p7_pli_postSSV_LongTarget()
1281 * Synopsis: the part of the LongTarget P7 search Pipeline downstream
1282 * of the SSV filter
1283 *
1284 * Purpose: This is called by either the standard (SIMD-SSV) long-target
1285 * pipeline (p7_Pipeline_LongTarget) or the FM-index long-target
1286 * pipeline (p7_Pipeline_FM), and runs the post-MSV part of H3's
1287 * accelerated pipeline to compare profile <om> against sequence
1288 * <sq>. If a significant hit is found (within the function
1289 * p7_pipeline_postViterbi_LongTarget(), called in this function),
1290 * information about it is added to the <hitlist>. The pipeline
1291 * accumulates beancounting information about how many comparisons
1292 * and residues flow through the pipeline while it's active.
1293 *
1294 * Args: pli - the main pipeline object
1295 * om - optimized profile (query)
1296 * bg - background model
1297 * hitlist - pointer to hit storage bin
1298 * data - for computing windows based on maximum prefix/suffix extensions
1299 * seqidx - the id # of the sequence from which the current window was extracted
1300 * window_start - the starting position of the extracted window (offset from the first
1301 * position of the block of a possibly longer sequence)
1302 * window_len - the length of the extracted window
1303 * subseq - digital sequence of the extracted window
1304 * seq_start - first position of the sequence block passed in to the calling pipeline function
1305 * seq_name - name of the sequence the window comes from
1306 * seq_source - source of the sequence the window comes from
1307 * seq_acc - acc of the sequence the window comes from
1308 * seq_desc - desc of the sequence the window comes from
1309 * seq_len - length of the sequence the window comes from (only FM will have it; otherwise, 0 and ignored)
1310 * nullsc - score of the passed window vs the bg model
1311 * usc - msv score of the passed window
1312 * complementarity - boolean; is the passed window sourced from a complementary sequence block
1313 * vit_windowlist - initialized window list, in which viterbi-passing hits are captured
1314 * pli_tmp - a collection of objects used in the long target pipeline that should be
1315 * (and are) only allocated once per pipeline to minimize alloc overhead.
1316 *
1317 * Returns: <eslOK> on success. If a significant hit is obtained,
1318 * its information is added to the growing <hitlist>.
1319 *
1320 * <eslERANGE> on numerical overflow errors in the
1321 * optimized vector implementations; particularly in
1322 * posterior decoding. I don't believe this is possible for
1323 * multihit local models, but I'm set up to catch it
1324 * anyway. We may emit a warning to the user, but cleanly
1325 * skip the problematic sequence and continue.
1326 *
1327 * Throws: <eslEMEM> on allocation failure.
1328 *
1329 */
1330 static int
1331 p7_pli_postSSV_LongTarget(P7_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, P7_TOPHITS *hitlist, const P7_SCOREDATA *data,
1332 int64_t seqidx, uint64_t window_start, int window_len, ESL_DSQ *subseq,
1333 uint64_t seq_start, char *seq_name, char *seq_source, char* seq_acc, char* seq_desc, int seq_len,
1334 float nullsc, float usc, int complementarity, P7_HMM_WINDOWLIST *vit_windowlist,
1335 P7_PIPELINE_LONGTARGET_OBJS *pli_tmp
1336 )
1337 {
1338 float filtersc; /* HMM null filter score */
1339 float bias_filtersc; /* HMM null filter score */
1340 float seq_score; /* the corrected per-seq bit score */
1341 double P; /* P-value of a hit */
1342 int i;
1343 int overlap;
1344 uint64_t new_n;
1345 uint32_t new_len;
1346
1347 int loc_window_len; //used to re-parameterize to shorter target windows
1348
1349 int max_window_len = 80000;
1350 int overlap_len = ESL_MIN(40000, om->max_length); // Won't allow more than 40K overlap - that's an absurdly long MAXL.
1351
1352 int F1_L = ESL_MIN( window_len, pli->B1);
1353 int F2_L = ESL_MIN( window_len, pli->B2);
1354
1355 //initial bias filter, based on the input window_len
1356 if (pli->do_biasfilter) {
1357 p7_bg_SetLength(bg, window_len);
1358 p7_bg_FilterScore(bg, subseq, window_len, &bias_filtersc);
1359 bias_filtersc -= nullsc; // doing this because I'll be modifying the bias part of filtersc based on length, then adding nullsc back in.
1360 filtersc = nullsc + (bias_filtersc * (float)(( F1_L>window_len ? 1.0 : (float)F1_L/window_len)));
1361 seq_score = (usc - filtersc) / eslCONST_LOG2;
1362 P = esl_gumbel_surv(seq_score, om->evparam[p7_MMU], om->evparam[p7_MLAMBDA]);
1363 if (P > pli->F1) return eslOK;
1364 } else {
1365 bias_filtersc = 0; // mullsc will be added in later
1366 }
1367 pli->pos_past_bias += window_len;
1368
1369 //establish a possibly shorter target window parameterization
1370 loc_window_len = ESL_MIN(window_len,om->max_length);
1371
1372 //compute the new nullsc based on possibly shorter window
1373 p7_bg_SetLength(bg, loc_window_len);
1374 p7_bg_NullOne (bg, subseq, loc_window_len, &nullsc);
1375
1376 // bias_filtersc has already been reduced by nullsc based on window_len
1377 // We compute a --B2-scaled bias, then tack on the nullsc based on the new,
1378 // possibly shorter length model
1379 filtersc = nullsc + (bias_filtersc * ( F2_L>window_len ? 1.0 : (float)F2_L/window_len) );
1380
1381 //Then configure the model length based on the possibly shorter window length
1382 p7_oprofile_ReconfigRestLength(om, loc_window_len);
1383
1384 /* Second level filter: ViterbiFilter(), multihit with <om> */
1385 p7_omx_GrowTo(pli->oxf, om->M, 0, window_len);
1386
1387 //use window_len instead of loc_window_len, because length parameterization is done, just need to loop over subseq
1388 p7_ViterbiFilter_longtarget(subseq, window_len, om, pli->oxf, filtersc, pli->F2, vit_windowlist);
1389
1390 p7_pli_ExtendAndMergeWindows (om, data, vit_windowlist, 0.5);
1391
1392 // if a window is still too long (>80Kb), need to split it up to
1393 // ensure numeric stability in Fwd.
1394 for (i=0; i<vit_windowlist->count; i++) {
1395
1396 if (vit_windowlist->windows[i].length > max_window_len) {
1397 //modify the current window to restrict length to 40K, then add
1398 //new windows with max length 40K, and MAXL overlap w/ preceding window
1399 new_n = vit_windowlist->windows[i].n ;
1400 new_len = vit_windowlist->windows[i].length ;
1401 vit_windowlist->windows[i].length = max_window_len;
1402
1403 do {
1404 int shift = max_window_len - overlap_len;
1405 new_n += shift;
1406 new_len -= shift;
1407 p7_hmmwindow_new(vit_windowlist, 0, new_n, 0, 0, ESL_MIN(max_window_len,new_len), 0.0, p7_NOCOMPLEMENT, new_len );
1408 } while (new_len > max_window_len);
1409 }
1410 }
1411
1412 overlap = 0;
1413 for (i=0; i<vit_windowlist->count; i++) {
1414 pli->pos_past_vit += vit_windowlist->windows[i].length;
1415 //remove overlap with preceding window
1416 if (i>0)
1417 pli->pos_past_vit -= ESL_MAX(0, vit_windowlist->windows[i-1].n + vit_windowlist->windows[i-1].length - vit_windowlist->windows[i].n );
1418
1419 p7_pli_postViterbi_LongTarget(pli, om, bg, hitlist, data, seqidx,
1420 window_start+vit_windowlist->windows[i].n-1, vit_windowlist->windows[i].length,
1421 subseq + vit_windowlist->windows[i].n - 1,
1422 seq_start, seq_name, seq_source, seq_acc, seq_desc, seq_len, complementarity, &overlap,
1423 pli_tmp
1424 );
1425 if (overlap == -1 && i<vit_windowlist->count-1) {
1426 overlap = ESL_MAX(0, vit_windowlist->windows[i].n + vit_windowlist->windows[i].length - vit_windowlist->windows[i+1].n );
1427 } else {
1428 //that window didn't pass Fwd
1429 overlap = 0;
1430 }
1431
1432 pli->ddef->ndom = 0;
1433
1434 }
1435
1436 return eslOK;
1437
1438 }
1439
1440
1441
1442
1443 /* Function: p7_Pipeline_LongTarget()
1444 * Synopsis: Accelerated seq/profile comparison pipeline for long target sequences.
1445 *
1446 * Purpose: Run HMMER's accelerated pipeline to compare profile <om>
1447 * against sequence <sq>. If a significant hit is found,
1448 * information about it is added to the <hitlist>. This is
1449 * a variant of p7_Pipeline that runs one of two
1450 * alternative SSV filters
1451 * (1) the scanning SSV filter (p7_SSVFilter_longtarget) that scans
1452 * a long sequence and finds high-scoring regions (windows), or
1453 * (2) the FM-index-based SSV filter that finds modest-scoring
1454 * diagonals using the FM-index, and extends them to maximum-
1455 * scoring diagonals subjected to the SSV filter thresholds
1456 *
1457 * Windows passing the appropriate SSV filter are then passed
1458 * to the remainder of the pipeline. The pipeline accumulates
1459 * bean counting information about how many comparisons and
1460 * residues flow through the pipeline while it's active.
1461 *
1462 * Returns: <eslOK> on success. If a significant hit is obtained,
1463 * its information is added to the growing <hitlist>.
1464 *
1465 * <eslEINVAL> if (in a scan pipeline) we're supposed to
1466 * set GA/TC/NC bit score thresholds but the model doesn't
1467 * have any.
1468 *
1469 * <eslERANGE> on numerical overflow errors in the
1470 * optimized vector implementations; particularly in
1471 * posterior decoding. We don't believe this is possible for
1472 * multihit local models, but we're set up to catch it
1473 * anyway. We may emit a warning to the user, but cleanly
1474 * skip the problematic sequence and continue.
1475 *
1476 * Args: pli - the main pipeline object
1477 * om - optimized profile (query)
1478 * data - for computing diagonals, and picking window edges based
1479 * on maximum prefix/suffix extensions
1480 * bg - background model
1481 * hitlist - pointer to hit storage bin (already allocated)
1482 *
1483 * :: the next three values are assigned if a standard sequence database is being used. If FM database is used, they are ignored
1484 * seqidx - the id # of the sequence from which the current window was extracted
1485 * sq - digital sequence of the window
1486 * complementarity - is <sq> from the top strand (p7_NOCOMPLEMENT), or bottom strand (P7_COMPLEMENT)
1487 *
1488 * :: the next three are assigned if an FM database is being used. If standard sequence is used, they are set to NULL.
1489 * fmf - the FM_DATA for forward-strand search
1490 * fmb - the FM_DATA for reverse-strand (complement) search
1491 * fm_cfg - general FM configuration
1492 *
1493 * Throws: <eslEMEM> on allocation failure.
1494 */
1495 int
1496 p7_Pipeline_LongTarget(P7_PIPELINE *pli, P7_OPROFILE *om, P7_SCOREDATA *data,
1497 P7_BG *bg, P7_TOPHITS *hitlist,
1498 int64_t seqidx, const ESL_SQ *sq, int complementarity,
1499 const FM_DATA *fmf, const FM_DATA *fmb, FM_CFG *fm_cfg
1500 )
1501 {
1502 int i;
1503 int status;
1504 float nullsc; /* null model score */
1505 float usc; /* msv score */
1506 float P;
1507 float bias_filtersc;
1508
1509 ESL_DSQ *subseq;
1510 uint64_t seq_start;
1511
1512
1513 P7_HMM_WINDOWLIST msv_windowlist;
1514 P7_HMM_WINDOWLIST vit_windowlist;
1515 P7_HMM_WINDOW *window;
1516 FM_SEQDATA seq_data;
1517
1518 P7_PIPELINE_LONGTARGET_OBJS *pli_tmp;
1519
1520 if ((sq && (sq->n == 0)) || (fmf && (fmf->N == 0))) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
1521
1522
1523 ESL_ALLOC(pli_tmp, sizeof(P7_PIPELINE_LONGTARGET_OBJS));
1524 pli_tmp->tmpseq = NULL;
1525 pli_tmp->bg = p7_bg_Clone(bg);
1526 pli_tmp->om = p7_oprofile_Create(om->M, om->abc);
1527 ESL_ALLOC(pli_tmp->scores, sizeof(float) * om->abc->Kp * 4); //allocation of space to store scores that will be used in p7_oprofile_Update(Fwd|Vit|MSV)EmissionScores
1528 ESL_ALLOC(pli_tmp->fwd_emissions_arr, sizeof(float) * om->abc->Kp * (om->M+1));
1529
1530 msv_windowlist.windows = NULL;
1531 vit_windowlist.windows = NULL;
1532 p7_hmmwindow_init(&msv_windowlist);
1533
1534 p7_omx_GrowTo(pli->oxf, om->M, 0, om->max_length); /* expand the one-row omx if needed */
1535
1536 /* Set false target length. This is a conservative estimate of the length of window that'll
1537 * soon be passed on to later phases of the pipeline; used to recover some bits of the score
1538 * that we would miss if we left length parameters set to the full target length */
1539 p7_oprofile_ReconfigMSVLength(om, om->max_length);
1540
1541
1542 /* First level filter: the SSV filter, with <om>.
1543 * This variant of SSV will scan a long sequence and find
1544 * short high-scoring regions.
1545 */
1546 if (fmf) // using an FM-index
1547 p7_SSVFM_longlarget(om, 2.0, bg, pli->F1, fmf, fmb, fm_cfg, data, pli->strands, pli->r, &msv_windowlist );
1548 else // compare directly to sequence
1549 p7_SSVFilter_longtarget(sq->dsq, sq->n, om, pli->oxf, data, bg, pli->F1, &msv_windowlist);
1550
1551
1552 /* convert hits to windows, merging neighboring windows
1553 */
1554 if ( msv_windowlist.count > 0 ) {
1555
1556 /* In scan mode, if it passes the MSV filter, read the rest of the profile */
1557 if (!fmf && pli->hfp)
1558 {
1559 if (om->base_w == 0 && om->scale_w == 0) { // we haven't already read this hmm (if we're on the second strand, we would've)
1560 p7_oprofile_ReadRest(pli->hfp, om);
1561 if ((status = p7_pli_NewModelThresholds(pli, om)) != eslOK) goto ERROR;
1562 }
1563 }
1564
1565 p7_oprofile_GetFwdEmissionArray(om, bg, pli_tmp->fwd_emissions_arr);
1566
1567 if (data->prefix_lengths == NULL) // otherwise, already filled in
1568 p7_hmm_ScoreDataComputeRest(om, data);
1569
1570 p7_pli_ExtendAndMergeWindows (om, data, &msv_windowlist, 0);
1571
1572 /* If using FM, it's possible for a seed we just created to span more than one segment
1573 * in the target. Check for this, and resolve it, by trimming an over-extended
1574 * segment, and tacking it on as a new window (to be dealt with in a later pass)
1575 */
1576 if (fmf) {
1577 for (i=0; i<msv_windowlist.count; i++) {
1578 int again = TRUE;
1579 window = msv_windowlist.windows + i;
1580
1581 while (again) {
1582 uint32_t seg_id;
1583 uint64_t seg_pos;
1584 again = FALSE;
1585
1586 status = fm_getOriginalPosition (fmf, fm_cfg->meta, 0, window->length, window->complementarity, window->fm_n, &seg_id, &seg_pos);
1587
1588 if (status == eslERANGE) {
1589 int overext;
1590 int use_length;
1591 int is_compl = (window->complementarity == p7_COMPLEMENT);
1592
1593 overext = (seg_pos + window->length) - (fm_cfg->meta->seq_data[ seg_id ].target_start + fm_cfg->meta->seq_data[ seg_id ].length - 1) ;
1594
1595 use_length = window->length - overext + 1;
1596
1597 if (use_length >= 8 && window->length >= 8) { // if both halves are kinda long, split the first half off as a new window
1598 p7_hmmwindow_new(&msv_windowlist, seg_id + (is_compl?-1:1), window->n, window->fm_n, window->k+use_length-1, use_length, window->score, window->complementarity, fm_cfg->meta->seq_data[seg_id].length);
1599 window = msv_windowlist.windows + i; // it may have moved due a a realloc
1600 window->k += use_length;
1601 window->length = overext;
1602 again = TRUE;
1603 } else if (window->length >= 8) { //if just the right half is long enough, shift numbers over
1604 window->k += use_length;
1605 window->length = overext;
1606 } else { //just limit the length of the left half
1607 window->length = use_length;
1608 }
1609
1610 }
1611 }
1612 }
1613 }
1614
1615 /* Pass each remaining window on to the remaining pipeline */
1616 p7_hmmwindow_init(&vit_windowlist);
1617 pli_tmp->tmpseq = esl_sq_CreateDigital(om->abc);
1618 if (!fmf )
1619 free (pli_tmp->tmpseq->dsq); //this ESL_SQ object is just a container that'll point to a series of other DSQs, so free the one we just created inside the larger SQ object
1620
1621
1622 for (i=0; i<msv_windowlist.count; i++){
1623 window = msv_windowlist.windows + i ;
1624
1625 if (fmf) {
1626 fm_convertRange2DSQ( fmf, fm_cfg->meta, window->fm_n, window->length, window->complementarity, pli_tmp->tmpseq, TRUE );
1627 subseq = pli_tmp->tmpseq->dsq;
1628 } else {
1629 subseq = sq->dsq + window->n - 1;
1630 }
1631
1632 p7_bg_SetLength(bg, window->length);
1633 p7_bg_NullOne (bg, subseq, window->length, &nullsc);
1634
1635 p7_bg_FilterScore(bg, subseq, window->length, &bias_filtersc);
1636 // Compute standard MSV to ensure that bias doesn't overcome SSV score when MSV
1637 // would have survived it
1638 p7_oprofile_ReconfigMSVLength(om, window->length);
1639 p7_MSVFilter(subseq, window->length, om, pli->oxf, &usc);
1640 P = esl_gumbel_surv( (usc-nullsc)/eslCONST_LOG2, om->evparam[p7_MMU], om->evparam[p7_MLAMBDA]);
1641
1642 if (P > pli->F1 ) continue;
1643 pli->pos_past_msv += window->length;
1644
1645 if (fmf) {
1646 seq_data = fm_cfg->meta->seq_data[window->id];
1647 seq_start = seq_data.target_start;
1648 if (window->complementarity == p7_COMPLEMENT)
1649 seq_start += seq_data.length - 2;
1650 }
1651
1652 status = p7_pli_postSSV_LongTarget(pli, om, bg, hitlist, data,
1653 (fmf != NULL ? seq_data.target_id : seqidx),
1654 window->n, window->length, subseq,
1655 (fmf != NULL ? seq_start : sq->start),
1656 (fmf != NULL ? seq_data.name : sq->name),
1657 (fmf != NULL ? seq_data.source : sq->source),
1658 (fmf != NULL ? seq_data.acc : sq->acc),
1659 (fmf != NULL ? seq_data.desc : sq->desc),
1660 (fmf != NULL ? seq_data.length : -1),
1661 nullsc,
1662 usc,
1663 (fmf != NULL ? window->complementarity : complementarity),
1664 &vit_windowlist,
1665 pli_tmp
1666 );
1667 if (status != eslOK) goto ERROR;
1668
1669 }
1670
1671 if (fmf) free (pli_tmp->tmpseq->dsq);
1672
1673 pli_tmp->tmpseq->dsq = NULL; //it's a pointer to a dsq object belonging to another sequence
1674
1675 esl_sq_Destroy(pli_tmp->tmpseq);
1676 free (vit_windowlist.windows);
1677 }
1678
1679 if (msv_windowlist.windows != NULL) free (msv_windowlist.windows);
1680
1681 if (pli_tmp != NULL) {
1682 if (pli_tmp->bg != NULL) p7_bg_Destroy(pli_tmp->bg);
1683 if (pli_tmp->om != NULL) p7_oprofile_Destroy(pli_tmp->om);
1684 if (pli_tmp->scores != NULL) free (pli_tmp->scores);
1685 if (pli_tmp->fwd_emissions_arr != NULL) free (pli_tmp->fwd_emissions_arr);
1686 free(pli_tmp);
1687 }
1688
1689 return eslOK;
1690
1691 ERROR:
1692 if (msv_windowlist.windows != NULL) free (msv_windowlist.windows);
1693 if (vit_windowlist.windows != NULL) free (vit_windowlist.windows);
1694
1695 if (pli_tmp != NULL) {
1696 if (pli_tmp->tmpseq != NULL) esl_sq_Destroy(pli_tmp->tmpseq);
1697 if (pli_tmp->bg != NULL) p7_bg_Destroy(pli_tmp->bg);
1698 if (pli_tmp->om != NULL) p7_oprofile_Destroy(pli_tmp->om);
1699 if (pli_tmp->scores != NULL) free (pli_tmp->scores);
1700 if (pli_tmp->fwd_emissions_arr != NULL) free (pli_tmp->fwd_emissions_arr);
1701 free(pli_tmp);
1702 }
1703
1704 return status;
1705
1706 }
1707
1708
1709 /* Function: p7_pli_Statistics()
1710 * Synopsis: Final statistics output from a processing pipeline.
1711 *
1712 * Purpose: Print a standardized report of the internal statistics of
1713 * a finished processing pipeline <pli> to stream <ofp>.
1714 *
1715 * If stopped, non-<NULL> stopwatch <w> is provided for a
1716 * stopwatch that was timing the pipeline, then the report
1717 * includes timing information.
1718 *
1719 * Returns: <eslOK> on success.
1720 */
1721 int
1722 p7_pli_Statistics(FILE *ofp, P7_PIPELINE *pli, ESL_STOPWATCH *w)
1723 {
1724 double ntargets;
1725
1726 fprintf(ofp, "Internal pipeline statistics summary:\n");
1727 fprintf(ofp, "-------------------------------------\n");
1728 if (pli->mode == p7_SEARCH_SEQS) {
1729 fprintf(ofp, "Query model(s): %15" PRId64 " (%" PRId64 " nodes)\n", pli->nmodels, pli->nnodes);
1730 fprintf(ofp, "Target sequences: %15" PRId64 " (%" PRId64 " residues searched)\n", pli->nseqs, pli->nres);
1731 ntargets = pli->nseqs;
1732 } else {
1733 fprintf(ofp, "Query sequence(s): %15" PRId64 " (%" PRId64 " residues searched)\n", pli->nseqs, pli->nres);
1734 fprintf(ofp, "Target model(s): %15" PRId64 " (%" PRId64 " nodes)\n", pli->nmodels, pli->nnodes);
1735 ntargets = pli->nmodels;
1736 }
1737
1738 if (pli->long_targets) { // nhmmer style
1739 fprintf(ofp, "Residues passing SSV filter: %15" PRId64 " (%.3g); expected (%.3g)\n",
1740 pli->pos_past_msv,
1741 (double)pli->pos_past_msv / (pli->nres*pli->nmodels) ,
1742 pli->F1);
1743
1744 fprintf(ofp, "Residues passing bias filter:%15" PRId64 " (%.3g); expected (%.3g)\n",
1745 pli->pos_past_bias,
1746 (double)pli->pos_past_bias / (pli->nres*pli->nmodels) ,
1747 pli->F1);
1748
1749 fprintf(ofp, "Residues passing Vit filter: %15" PRId64 " (%.3g); expected (%.3g)\n",
1750 pli->pos_past_vit,
1751 (double)pli->pos_past_vit / (pli->nres*pli->nmodels) ,
1752 pli->F2);
1753
1754 fprintf(ofp, "Residues passing Fwd filter: %15" PRId64 " (%.3g); expected (%.3g)\n",
1755 pli->pos_past_fwd,
1756 (double)pli->pos_past_fwd / (pli->nres*pli->nmodels) ,
1757 pli->F3);
1758
1759 fprintf(ofp, "Total number of hits: %15d (%.3g)\n",
1760 (int)pli->n_output,
1761 (double)pli->pos_output / (pli->nres*pli->nmodels) );
1762
1763 } else { // typical case output
1764
1765 fprintf(ofp, "Passed MSV filter: %15" PRId64 " (%.6g); expected %.1f (%.6g)\n",
1766 pli->n_past_msv,
1767 (double) pli->n_past_msv / ntargets,
1768 pli->F1 * ntargets,
1769 pli->F1);
1770
1771 fprintf(ofp, "Passed bias filter: %15" PRId64 " (%.6g); expected %.1f (%.6g)\n",
1772 pli->n_past_bias,
1773 (double) pli->n_past_bias / ntargets,
1774 pli->F1 * ntargets,
1775 pli->F1);
1776
1777 fprintf(ofp, "Passed Vit filter: %15" PRId64 " (%.6g); expected %.1f (%.6g)\n",
1778 pli->n_past_vit,
1779 (double) pli->n_past_vit / ntargets,
1780 pli->F2 * ntargets,
1781 pli->F2);
1782
1783 fprintf(ofp, "Passed Fwd filter: %15" PRId64 " (%.6g); expected %.1f (%.6g)\n",
1784 pli->n_past_fwd,
1785 (double) pli->n_past_fwd / ntargets,
1786 pli->F3 * ntargets,
1787 pli->F3);
1788
1789 fprintf(ofp, "Initial search space (Z): %15.0f %s\n", pli->Z, pli->Z_setby == p7_ZSETBY_OPTION ? "[as set by --Z on cmdline]" : "[actual number of targets]");
1790 fprintf(ofp, "Domain search space (domZ): %15.0f %s\n", pli->domZ, pli->domZ_setby == p7_ZSETBY_OPTION ? "[as set by --domZ on cmdline]" : "[number of targets reported over threshold]");
1791 }
1792
1793 if (w != NULL) {
1794 esl_stopwatch_Display(ofp, w, "# CPU time: ");
1795 fprintf(ofp, "# Mc/sec: %.2f\n",
1796 (double) pli->nres * (double) pli->nnodes / (w->elapsed * 1.0e6));
1797 }
1798
1799 return eslOK;
1800 }
1801 /*------------------- end, pipeline API -------------------------*/
1802
1803
1804 /*****************************************************************
1805 * 3. Example 1: "search mode" in a sequence db
1806 *****************************************************************/
1807
1808 #ifdef p7PIPELINE_EXAMPLE
1809 /* gcc -o pipeline_example -g -Wall -I../easel -L../easel -I. -L. -Dp7PIPELINE_EXAMPLE p7_pipeline.c -lhmmer -leasel -lm
1810 * ./pipeline_example <hmmfile> <sqfile>
1811 */
1812
1813 #include <p7_config.h>
1814
1815 #include "easel.h"
1816 #include "esl_getopts.h"
1817 #include "esl_sqio.h"
1818
1819 #include "hmmer.h"
1820
1821 static ESL_OPTIONS options[] = {
1822 /* name type default env range toggles reqs incomp help docgroup*/
1823 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1824 { "-E", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "E-value cutoff for reporting significant sequence hits", 0 },
1825 { "-T", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "bit score cutoff for reporting significant sequence hits", 0 },
1826 { "-Z", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of comparisons done, for E-value calculation", 0 },
1827 { "--domE", eslARG_REAL,"1000.0", NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "E-value cutoff for reporting individual domains", 0 },
1828 { "--domT", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "bit score cutoff for reporting individual domains", 0 },
1829 { "--domZ", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of significant seqs, for domain E-value calculation", 0 },
1830 { "--cut_ga", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqE,--seqT,--domE,--domT", "use GA gathering threshold bit score cutoffs in <hmmfile>", 0 },
1831 { "--cut_nc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqE,--seqT,--domE,--domT", "use NC noise threshold bit score cutoffs in <hmmfile>", 0 },
1832 { "--cut_tc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqE,--seqT,--domE,--domT", "use TC trusted threshold bit score cutoffs in <hmmfile>", 0 },
1833 { "--max", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--F1,--F2,--F3", "Turn all heuristic filters off (less speed, more power)", 0 },
1834 { "--F1", eslARG_REAL, "0.02", NULL, NULL, NULL, NULL, "--max", "Stage 1 (MSV) threshold: promote hits w/ P <= F1", 0 },
1835 { "--F2", eslARG_REAL, "1e-3", NULL, NULL, NULL, NULL, "--max", "Stage 2 (Vit) threshold: promote hits w/ P <= F2", 0 },
1836 { "--F3", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, "--max", "Stage 3 (Fwd) threshold: promote hits w/ P <= F3", 0 },
1837 { "--nobias", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--max", "turn off composition bias filter", 0 },
1838 { "--nonull2", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "turn off biased composition score corrections", 0 },
1839 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 0 },
1840 { "--acc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output target accessions instead of names if possible", 0 },
1841 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1842 };
1843 static char usage[] = "[-options] <hmmfile> <seqdb>";
1844 static char banner[] = "example of using acceleration pipeline in search mode (seq targets)";
1845
1846 int
1847 main(int argc, char **argv)
1848 {
1849 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
1850 char *hmmfile = esl_opt_GetArg(go, 1);
1851 char *seqfile = esl_opt_GetArg(go, 2);
1852 int format = eslSQFILE_FASTA;
1853 P7_HMMFILE *hfp = NULL;
1854 ESL_ALPHABET *abc = NULL;
1855 P7_BG *bg = NULL;
1856 P7_HMM *hmm = NULL;
1857 P7_PROFILE *gm = NULL;
1858 P7_OPROFILE *om = NULL;
1859 ESL_SQFILE *sqfp = NULL;
1860 ESL_SQ *sq = NULL;
1861 P7_PIPELINE *pli = NULL;
1862 P7_TOPHITS *hitlist = NULL;
1863 int h,d,namew;
1864
1865 /* Don't forget this. Null2 corrections need FLogsum() */
1866 p7_FLogsumInit();
1867
1868 /* Read in one HMM */
1869 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
1870 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
1871 p7_hmmfile_Close(hfp);
1872
1873 /* Open a sequence file */
1874 if (esl_sqfile_OpenDigital(abc, seqfile, format, NULL, &sqfp) != eslOK) p7_Fail("Failed to open sequence file %s\n", seqfile);
1875 sq = esl_sq_CreateDigital(abc);
1876
1877 /* Create a pipeline and a top hits list */
1878 pli = p7_pipeline_Create(go, hmm->M, 400, FALSE, p7_SEARCH_SEQS);
1879 hitlist = p7_tophits_Create();
1880
1881 /* Configure a profile from the HMM */
1882 bg = p7_bg_Create(abc);
1883 gm = p7_profile_Create(hmm->M, abc);
1884 om = p7_oprofile_Create(hmm->M, abc);
1885 p7_ProfileConfig(hmm, bg, gm, 400, p7_LOCAL);
1886 p7_oprofile_Convert(gm, om); /* <om> is now p7_LOCAL, multihit */
1887 p7_pli_NewModel(pli, om, bg);
1888
1889 /* Run each target sequence through the pipeline */
1890 while (esl_sqio_Read(sqfp, sq) == eslOK)
1891 {
1892 p7_pli_NewSeq(pli, sq);
1893 p7_bg_SetLength(bg, sq->n);
1894 p7_oprofile_ReconfigLength(om, sq->n);
1895
1896 p7_Pipeline(pli, om, bg, sq, NULL, hitlist);
1897
1898 esl_sq_Reuse(sq);
1899 p7_pipeline_Reuse(pli);
1900 }
1901
1902 /* Print the results.
1903 * This example is a stripped version of hmmsearch's tabular output.
1904 */
1905 p7_tophits_SortBySortkey(hitlist);
1906 namew = ESL_MAX(8, p7_tophits_GetMaxNameLength(hitlist));
1907 for (h = 0; h < hitlist->N; h++)
1908 {
1909 d = hitlist->hit[h]->best_domain;
1910
1911 printf("%10.2g %7.1f %6.1f %7.1f %6.1f %10.2g %6.1f %5d %-*s %s\n",
1912 exp(hitlist->hit[h]->lnP) * (double) pli->Z,
1913 hitlist->hit[h]->score,
1914 hitlist->hit[h]->pre_score - hitlist->hit[h]->score, /* bias correction */
1915 hitlist->hit[h]->dcl[d].bitscore,
1916 eslCONST_LOG2R * p7_FLogsum(0.0, log(bg->omega) + hitlist->hit[h]->dcl[d].domcorrection), /* print in units of bits */
1917 exp(hitlist->hit[h]->dcl[d].lnP) * (double) pli->Z,
1918 hitlist->hit[h]->nexpected,
1919 hitlist->hit[h]->nreported,
1920 namew,
1921 hitlist->hit[h]->name,
1922 hitlist->hit[h]->desc);
1923 }
1924
1925 /* Done. */
1926 p7_tophits_Destroy(hitlist);
1927 p7_pipeline_Destroy(pli);
1928 esl_sq_Destroy(sq);
1929 esl_sqfile_Close(sqfp);
1930 p7_oprofile_Destroy(om);
1931 p7_profile_Destroy(gm);
1932 p7_hmm_Destroy(hmm);
1933 p7_bg_Destroy(bg);
1934 esl_alphabet_Destroy(abc);
1935 esl_getopts_Destroy(go);
1936 return 0;
1937 }
1938 #endif /*p7PIPELINE_EXAMPLE*/
1939 /*----------- end, search mode (seq db) example -----------------*/
1940
1941
1942
1943
1944 /*****************************************************************
1945 * 4. Example 2: "scan mode" in an HMM db
1946 *****************************************************************/
1947 #ifdef p7PIPELINE_EXAMPLE2
1948 /* gcc -o pipeline_example2 -g -Wall -I../easel -L../easel -I. -L. -Dp7PIPELINE_EXAMPLE2 p7_pipeline.c -lhmmer -leasel -lm
1949 * ./pipeline_example2 <hmmdb> <sqfile>
1950 */
1951
1952 #include <p7_config.h>
1953
1954 #include "easel.h"
1955 #include "esl_getopts.h"
1956 #include "esl_sqio.h"
1957
1958 #include "hmmer.h"
1959
1960 static ESL_OPTIONS options[] = {
1961 /* name type default env range toggles reqs incomp help docgroup*/
1962 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1963 { "-E", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "E-value cutoff for reporting significant sequence hits", 0 },
1964 { "-T", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "bit score cutoff for reporting significant sequence hits", 0 },
1965 { "-Z", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of comparisons done, for E-value calculation", 0 },
1966 { "--domE", eslARG_REAL,"1000.0", NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "E-value cutoff for reporting individual domains", 0 },
1967 { "--domT", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, "--cut_ga,--cut_nc,--cut_tc", "bit score cutoff for reporting individual domains", 0 },
1968 { "--domZ", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of significant seqs, for domain E-value calculation", 0 },
1969 { "--cut_ga", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqE,--seqT,--domE,--domT", "use GA gathering threshold bit score cutoffs in <hmmfile>", 0 },
1970 { "--cut_nc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqE,--seqT,--domE,--domT", "use NC noise threshold bit score cutoffs in <hmmfile>", 0 },
1971 { "--cut_tc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqE,--seqT,--domE,--domT", "use TC trusted threshold bit score cutoffs in <hmmfile>", 0 },
1972 { "--max", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--F1,--F2,--F3", "Turn all heuristic filters off (less speed, more power)", 0 },
1973 { "--F1", eslARG_REAL, "0.02", NULL, NULL, NULL, NULL, "--max", "Stage 1 (MSV) threshold: promote hits w/ P <= F1", 0 },
1974 { "--F2", eslARG_REAL, "1e-3", NULL, NULL, NULL, NULL, "--max", "Stage 2 (Vit) threshold: promote hits w/ P <= F2", 0 },
1975 { "--F3", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, "--max", "Stage 3 (Fwd) threshold: promote hits w/ P <= F3", 0 },
1976 { "--nobias", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--max", "turn off composition bias filter", 0 },
1977 { "--nonull2", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "turn off biased composition score corrections", 0 },
1978 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 0 },
1979 { "--acc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output target accessions instead of names if possible", 0 },
1980 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1981 };
1982 static char usage[] = "[-options] <hmmfile> <seqfile>";
1983 static char banner[] = "example of using acceleration pipeline in scan mode (HMM targets)";
1984
1985 int
1986 main(int argc, char **argv)
1987 {
1988 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
1989 char *hmmfile = esl_opt_GetArg(go, 1);
1990 char *seqfile = esl_opt_GetArg(go, 2);
1991 int format = eslSQFILE_FASTA;
1992 P7_HMMFILE *hfp = NULL;
1993 ESL_ALPHABET *abc = NULL;
1994 P7_BG *bg = NULL;
1995 P7_OPROFILE *om = NULL;
1996 ESL_SQFILE *sqfp = NULL;
1997 ESL_SQ *sq = NULL;
1998 P7_PIPELINE *pli = NULL;
1999 P7_TOPHITS *hitlist = p7_tophits_Create();
2000 int h,d,namew;
2001
2002 /* Don't forget this. Null2 corrections need FLogsum() */
2003 p7_FLogsumInit();
2004
2005 /* Open a sequence file, read one seq from it.
2006 * Convert to digital later, after 1st HMM is input and abc becomes known
2007 */
2008 sq = esl_sq_Create();
2009 if (esl_sqfile_Open(seqfile, format, NULL, &sqfp) != eslOK) p7_Fail("Failed to open sequence file %s\n", seqfile);
2010 if (esl_sqio_Read(sqfp, sq) != eslOK) p7_Fail("Failed to read sequence from %s\n", seqfile);
2011 esl_sqfile_Close(sqfp);
2012
2013 /* Open the HMM db */
2014 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
2015
2016 /* Create a pipeline for the query sequence in scan mode */
2017 pli = p7_pipeline_Create(go, 100, sq->n, FALSE, p7_SCAN_MODELS);
2018 p7_pli_NewSeq(pli, sq);
2019
2020 /* Some additional config of the pipeline specific to scan mode */
2021 pli->hfp = hfp;
2022 if (! pli->Z_is_fixed && hfp->is_pressed) { pli->Z_is_fixed = TRUE; pli->Z = hfp->ssi->nprimary; }
2023
2024 /* Read (partial) of each HMM in file */
2025 while (p7_oprofile_ReadMSV(hfp, &abc, &om) == eslOK)
2026 {
2027 /* One time only initialization after abc becomes known */
2028 if (bg == NULL)
2029 {
2030 bg = p7_bg_Create(abc);
2031 if (esl_sq_Digitize(abc, sq) != eslOK) p7_Die("alphabet mismatch");
2032 }
2033 p7_pli_NewModel(pli, om, bg);
2034 p7_bg_SetLength(bg, sq->n); /* SetLength() call MUST follow NewModel() call, because NewModel() resets the filter HMM, including its default expected length; see bug #h85 */
2035 p7_oprofile_ReconfigLength(om, sq->n);
2036
2037 p7_Pipeline(pli, om, bg, sq, hitlist);
2038
2039 p7_oprofile_Destroy(om);
2040 p7_pipeline_Reuse(pli);
2041 }
2042
2043 /* Print the results.
2044 * This example is a stripped version of hmmsearch's tabular output.
2045 */
2046 p7_tophits_SortBySortkey(hitlist);
2047 namew = ESL_MAX(8, p7_tophits_GetMaxNameLength(hitlist));
2048 for (h = 0; h < hitlist->N; h++)
2049 {
2050 d = hitlist->hit[h]->best_domain;
2051
2052 printf("%10.2g %7.1f %6.1f %7.1f %6.1f %10.2g %6.1f %5d %-*s %s\n",
2053 exp(hitlist->hit[h]->lnP) * (double) pli->Z,
2054 hitlist->hit[h]->score,
2055 hitlist->hit[h]->pre_score - hitlist->hit[h]->score, /* bias correction */
2056 hitlist->hit[h]->dcl[d].bitscore,
2057 eslCONST_LOG2R * p7_FLogsum(0.0, log(bg->omega) + hitlist->hit[h]->dcl[d].domcorrection), /* print in units of BITS */
2058 exp(hitlist->hit[h]->dcl[d].lnP) * (double) pli->Z,
2059 hitlist->hit[h]->nexpected,
2060 hitlist->hit[h]->nreported,
2061 namew,
2062 hitlist->hit[h]->name,
2063 hitlist->hit[h]->desc);
2064 }
2065
2066 /* Done. */
2067 p7_tophits_Destroy(hitlist);
2068 p7_pipeline_Destroy(pli);
2069 esl_sq_Destroy(sq);
2070 p7_hmmfile_Close(hfp);
2071 p7_bg_Destroy(bg);
2072 esl_alphabet_Destroy(abc);
2073 esl_getopts_Destroy(go);
2074 return 0;
2075 }
2076 #endif /*p7PIPELINE_EXAMPLE2*/
2077 /*--------------- end, scan mode (HMM db) example ---------------*/
2078
2079