Debian Codegraph @

src/cm_pipeline.c

raw symbols SCIP
1 /* Infernal's accelerated seq/profile comparison pipeline.
2 *
3 * Contents:
4 * 1. CM_PIPELINE: allocation, initialization, destruction
5 * 2. Pipeline API
6 * 3. Non-API filter stage search functions.
7 */
8 #include <esl_config.h>
9 #include <p7_config.h>
10 #include "config.h"
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "easel.h"
17 #include "esl_exponential.h"
18 #include "esl_getopts.h"
19 #include "esl_gumbel.h"
20 #include "esl_vectorops.h"
21
22 #include "hmmer.h"
23
24 #include "infernal.h"
25
26 static int pli_p7_filter (CM_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, P7_SCOREDATA *msvdata, const ESL_SQ *sq, int64_t **ret_ws, int64_t **ret_we, float **ret_wb, int *ret_nwin);
27 static int pli_p7_env_def (CM_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, const ESL_SQ *sq, int64_t *ws, int64_t *we, int nwin, P7_HMM **opt_hmm, P7_PROFILE **opt_gm,
28 P7_PROFILE **opt_Rgm, P7_PROFILE **opt_Lgm, P7_PROFILE **opt_Tgm, int64_t **ret_es, int64_t **ret_ee, float **ret_eb, int *ret_nenv);
29 static int pli_cyk_env_filter (CM_PIPELINE *pli, off_t cm_offset, const ESL_SQ *sq, int64_t *p7es, int64_t *p7ee, int np7env, CM_t **opt_cm, int64_t **ret_es, int64_t **ret_ee, int *ret_nenv);
30 static int pli_cyk_seq_filter (CM_PIPELINE *pli, off_t cm_offset, const ESL_SQ *sq, CM_t **opt_cm, int64_t **ret_ws, int64_t **ret_we, int *ret_nwin);
31 static int pli_final_stage (CM_PIPELINE *pli, off_t cm_offset, const ESL_SQ *sq, int64_t *es, int64_t *ee, int nenv, CM_TOPHITS *hitlist, CM_t **opt_cm);
32 static int pli_final_stage_hmmonly(CM_PIPELINE *pli, off_t cm_offset, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, const ESL_SQ *sq, int64_t *ws, int64_t *we, int nwin, CM_TOPHITS *hitlist, CM_t **opt_cm);
33 static int pli_dispatch_cm_search (CM_PIPELINE *pli, CM_t *cm, ESL_DSQ *dsq, int64_t start, int64_t stop, CM_TOPHITS *hitlist, float cutoff, float env_cutoff, int qdbidx, float *ret_sc, int64_t *opt_envi, int64_t *opt_envj);
34 static int pli_align_hit (CM_PIPELINE *pli, CM_t *cm, const ESL_SQ *sq, CM_HIT *hit);
35 static int pli_scan_mode_read_cm (CM_PIPELINE *pli, off_t cm_offset, float *p7_evparam, int p7_max_length, CM_t **ret_cm);
36
37 static int pli_pass_statistics (FILE *ofp, CM_PIPELINE *pli, int pass_idx);
38 static int pli_hmmonly_pass_statistics(FILE *ofp, CM_PIPELINE *pli);
39 static int pli_sum_statistics (CM_PIPELINE *pli);
40 static void pli_copy_subseq (const ESL_SQ *src_sq, ESL_SQ *dest_sq, int64_t i, int64_t L);
41 static char *pli_describe_pass (int pass_idx);
42 static char *pli_describe_hits_for_pass (int pass_idx);
43 static float pli_mxsize_limit_from_W (int W);
44
45 static int pli_check_one_or_zero_envelopes(int *nA);
46 static int pli_get_pass_of_best_envelope(float **bAA, int *nA);
47 static int pli_check_full_length_envelopes(int64_t **esAA, int64_t **eeAA, int *nA, int64_t L);
48 static int pli_check_overlap_envelopes(int64_t **sAA, int64_t **eAA, int *nA, int best_pass_idx, int best_env_idx, int64_t start_offset, float min_fract, int *ret_val, char *errbuf);
49
50 /*****************************************************************
51 * 1. The CM_PIPELINE object: allocation, initialization, destruction.
52 *****************************************************************/
53
54 /* Function: cm_pipeline_Create()
55 * Synopsis: Create a new accelerated comparison pipeline.
56 * Incept: EPN, Fri Sep 24 16:14:39 2010
57 * SRE, Fri Dec 5 10:11:31 2008 [Janelia] (p7_pipeline_Create())
58 *
59 * Purpose: Given an application configuration structure <go>
60 * containing certain standardized options (described
61 * below), some initial guesses at the model size <M_hint>
62 * and sequence length <L_hint> that will be processed,
63 * and a <mode> that can be either <cm_SCAN_MODELS> or
64 * <cm_SEARCH_SEQS> depending on whether we're searching one sequence
65 * against a model database (cmscan mode) or one model
66 * against a sequence database (cmsearch mode); create new
67 * pipeline object.
68 *
69 * In search mode, we would generally know the length of
70 * our query profile exactly, and would pass <cm->clen> as <M_hint>;
71 * in scan mode, we generally know the length of our query
72 * sequence exactly, and would pass <sq->n> as <L_hint>.
73 * Targets will come in various sizes as we read them,
74 * and the pipeline will resize any necessary objects as
75 * needed, so the other (unknown) length is only an
76 * initial allocation.
77 *
78 * <Z> is passed as the database size, in residues, if
79 * known. If unknown, 0 should be passed as <Z>.
80 *
81 * The configuration <go> must include settings for the
82 * following options:
83 *
84 * || option || description || usually ||
85 * | -g | configure CM for glocal alignment | FALSE |
86 * | -Z | database size in Mb | NULL |
87 * | --acc | prefer accessions over names in output | FALSE |
88 * | --noali | don't output alignments (smaller output) | FALSE |
89 * | --verbose | verbose output mode | FALSE |
90 * | -E | report hits <= this E-value threshold | 10.0 |
91 * | -T | report hits >= this bit score threshold | NULL |
92 * | --incE | include hits <= this E-value threshold | 0.01 |
93 * | --incT | include hits >= this bit score threshold | NULL |
94 * | --cut_ga | model-specific thresholding using GA | FALSE |
95 * | --cut_nc | model-specific thresholding using NC | FALSE |
96 * | --cut_tc | model-specific thresholding using TC | FALSE |
97 * | --notrunc | turn off truncated hit detection | FALSE |
98 * | --anytrunc | allow full + trunc hits anywhere in the seq | FALSE |
99 * | --onlytrunc | allow only trunc hits anywhere in the seq | FALSE |
100 * | --max | turn all heuristic filters off | FALSE |
101 * | --nohmm | turn all HMM filters off | FALSE |
102 * | --mid | turn off MSV and Viterbi filters | FALSE |
103 * | --default | default dbsize-dependent filtering strategy | TRUE |
104 * | --rfam | set filters to strict Rfam settings | FALSE |
105 * | --FZ <x> | set filter thr as if dbsize were <x> Mb | NULL |
106 * | --Fmid <x> | with --mid, set fwd filter thresholds to <x>| NULL |
107 * | --nonull3 | turn off NULL3 correction | FALSE |
108 * | --mxsize <x> | set max allowed HMM banded DP mx size to <x>| 128 Mb |
109 * | --cyk | set final search stage as CYK, not Inside | FALSE |
110 * | --acyk | align hits with CYK, not optimal accuracy | FALSE |
111 * | --wcx <x> | set cm->W as <x> * cm->clen | FALSE |
112 * | --toponly | only search top strand | FALSE |
113 * | --bottomonly | only search bottom strand | FALSE |
114 * *** all opts below this line are 'developer' options, only visible in cmsearch/cmscan via --devhelp
115 * | --noF1 | turn off MSV filter stage | FALSE |
116 * | --noF2 | turn off Viterbi filter stage | FALSE |
117 * | --noF3 | turn off HMM local forward stage | FALSE |
118 * | --noF4 | turn off HMM glocal forward stage | FALSE |
119 * | --noF6 | turn off CYK filter stage | FALSE |
120 * | --doF1b | turn on MSV composition bias filter | FALSE |
121 * | --noF2b | turn off Viterbi composition bias filter | FALSE |
122 * | --noF3b | turn off local forward bias filter | FALSE |
123 * | --noF4b | turn off glocal forward bias filter | FALSE |
124 * | --doF5b | turn on per-envelope bias filter | TRUE |
125 * *** options for defining filter thresholds, usually NULL bc set in DB-size dependent manner
126 * | --F1 | Stage 1 (MSV) P value threshold | NULL |
127 * | --F1b | Stage 1b (MSV bias) P value threshold | NULL |
128 * | --F2 | Stage 2 (Vit) P value threshold | NULL |
129 * | --F2b | Stage 2b (Vit bias) P value threshold | NULL |
130 * | --F3 | Stage 3 (lFwd) P value threshold | NULL |
131 * | --F3b | Stage 3b (lFwd bias) P value threshold | NULL |
132 * | --F4 | Stage 4 (gFwd) P value threshold | NULL |
133 * | --F4b | Stage 4b (gFwd bias) P value threshold | NULL |
134 * | --F5 | Stage 5 (envdef) P value threshold | NULL |
135 * | --F5b | Stage 5b (envdef bias) P value threshold | NULL |
136 * | --F6 | Stage 6 (CYK) P value threshold | NULL |
137 * | --rt1 | P7_DOMAINDEF rt1 parameter | 0.25 |
138 * | --rt2 | P7_DOMAINDEF rt2 parameter | 0.10 |
139 * | --rt3 | P7_DOMAINDEF rt3 parameter | 0.20 |
140 * | --ns | number of domain/envelope tracebacks | 200 |
141 * | --ftau | HMM band tail loss prob for CYK filter | 1e-4 |
142 * | --fsums | use sums to get CYK filter HMM bands | FALSE |
143 * | --fqdb | use QDBs not HMM bands in CYK filter | FALSE |
144 * | --fbeta | beta for QDBs in CYK filter | 1e-7 |
145 * | --fnonbanded | run CYK filter without bands | FALSE |
146 * | --nocykenv | do not redefine envelopes using CYK | FALSE |
147 * | --cykenvx | P-value multiplier for CYK envelope redefn | NULL |
148 * | --tau | HMM band tail loss prob for final round | 5e-6 |
149 * | --qdb | use QDBs not HMM bands in final round | FALSE |
150 * | --sums | use sums to get final round HMM bands | FALSE |
151 * | --beta | beta for QDBs in final round | 1e-15 |
152 * | --nonbanded | run CYK filter without bands | FALSE |
153 * | --timeF1 | abort after F1b stage, for timing expts | FALSE |
154 * | --timeF2 | abort after F2b stage, for timing expts | FALSE |
155 * | --timeF3 | abort after F3b stage, for timing expts | FALSE |
156 * | --timeF4 | abort after F4b stage, for timing expts | FALSE |
157 * | --timeF5 | abort after F5b stage, for timing expts | FALSE |
158 * | --timeF6 | abort after F6 stage, for timing expts | FALSE |
159 * | --trmF3 | terminate after F3 stage, output windows | FALSE |
160 * | --nogreedy | use optimal CM hit resolution, not greedy | FALSE |
161 * | --cp9noel | turn off EL state in CP9 HMM | FALSE |
162 * | --cp9gloc | configure CP9 HMM in glocal mode | FALSE |
163 * | --null2 | turn on null2 biased composition model | FALSE |
164 * | --maxtau | max tau during band tightening | 0.01 |
165 * | --seed | RNG seed (0=use arbitrary seed) | 181 |
166 *
167 * Returns: ptr to new <cm_PIPELINE> object on success. Caller frees this
168 * with <cm_pipeline_Destroy()>.
169 *
170 * Throws: <NULL> on allocation failure.
171 */
172 CM_PIPELINE *
173 cm_pipeline_Create(ESL_GETOPTS *go, ESL_ALPHABET *abc, int clen_hint, int L_hint, int64_t Z, enum cm_zsetby_e Z_setby, enum cm_pipemodes_e mode)
174 {
175 CM_PIPELINE *pli = NULL;
176 int seed = esl_opt_GetInteger(go, "--seed");
177 int status;
178 double Z_Mb; /* database size in Mb */
179 int pass_idx; /* counter over passes */
180
181 ESL_ALLOC(pli, sizeof(CM_PIPELINE));
182
183 /* allocate matrices */
184 if ((pli->fwd = p7_omx_Create(clen_hint, L_hint, L_hint)) == NULL) goto ERROR;
185 if ((pli->bck = p7_omx_Create(clen_hint, L_hint, L_hint)) == NULL) goto ERROR;
186 if ((pli->oxf = p7_omx_Create(clen_hint, 0, L_hint)) == NULL) goto ERROR;
187 if ((pli->oxb = p7_omx_Create(clen_hint, 0, L_hint)) == NULL) goto ERROR;
188 if ((pli->gfwd = p7_gmx_Create(clen_hint, L_hint)) == NULL) goto ERROR;
189 if ((pli->gbck = p7_gmx_Create(clen_hint, L_hint)) == NULL) goto ERROR;
190 if ((pli->gxf = p7_gmx_Create(clen_hint, L_hint)) == NULL) goto ERROR;
191 if ((pli->gxb = p7_gmx_Create(clen_hint, L_hint)) == NULL) goto ERROR;
192
193 /* Initializations */
194 pli->mode = mode;
195 pli->abc = abc;
196 pli->errbuf[0] = '\0';
197 pli->maxW = 0; /* model-dependent, invalid until cm_pli_NewModel() is called */
198 pli->cmW = 0; /* model-dependent, invalid until cm_pli_NewModel() is called */
199 pli->clen = 0; /* model-dependent, invalid until cm_pli_NewModel() is called */
200 pli->cur_cm_idx = -1; /* model-dependent, invalid until cm_pli_NewModel() is called */
201 pli->cur_clan_idx = -1; /* model-dependent, invalid until cm_pli_NewModel() is called */
202 pli->cur_seq_idx = -1; /* sequence-dependent, invalid until cm_pli_NewSeq() is called */
203 pli->cur_pass_idx = -1; /* pipeline-pass-dependent, updated in cm_Pipeline() */
204 pli->cmfp = NULL; /* set by caller only if we're a scan pipeline (i.e. set in cmscan) */
205
206 /* Accounting, as we collect results */
207 pli->nseqs = 0;
208 pli->nmodels = 0;
209 pli->nnodes = 0;
210 pli->nmodels_hmmonly = 0;
211 pli->nnodes_hmmonly = 0;
212 for(pass_idx = 0; pass_idx < NPLI_PASSES; pass_idx++) {
213 cm_pli_ZeroAccounting(&(pli->acct[pass_idx]));
214 }
215
216 /* Normally, we reinitialize the RNG to the original seed every time we're
217 * about to collect a stochastic trace ensemble. This eliminates run-to-run
218 * variability. As a special case, if seed==0, we choose an arbitrary one-time
219 * seed: time() sets the seed, and we turn off the reinitialization.
220 */
221 pli->r = esl_randomness_CreateFast(seed);
222 pli->do_reseeding = (seed == 0) ? FALSE : TRUE;
223 pli->ddef = p7_domaindef_Create(pli->r);
224 pli->ddef->do_reseeding = pli->do_reseeding;
225
226 /* Miscellaneous parameters */
227 if(esl_opt_IsOn(go, "--mxsize")) {
228 pli->mxsize_limit = esl_opt_GetReal(go, "--mxsize");
229 pli->mxsize_set = TRUE;
230 }
231 else {
232 pli->mxsize_limit = 0.;
233 pli->mxsize_set = FALSE;
234 }
235 pli->do_top = esl_opt_GetBoolean(go, "--bottomonly") ? FALSE : TRUE;
236 pli->do_bot = esl_opt_GetBoolean(go, "--toponly") ? FALSE : TRUE;
237 pli->be_verbose = esl_opt_GetBoolean(go, "--verbose") ? TRUE : FALSE;
238 pli->show_accessions = esl_opt_GetBoolean(go, "--acc") ? TRUE : FALSE;
239 pli->show_alignments = esl_opt_GetBoolean(go, "--noali") ? FALSE : TRUE;
240 pli->maxtau = esl_opt_GetReal (go, "--maxtau");
241 pli->do_wcx = esl_opt_IsUsed (go, "--wcx") ? TRUE : FALSE;
242 pli->wcx = esl_opt_IsUsed (go, "--wcx") ? esl_opt_GetReal(go, "--wcx") : 0.;
243 pli->do_one_cmpass = esl_opt_GetBoolean(go, "--onepass") ? TRUE : FALSE;
244 pli->do_one_cmpass_olap = esl_opt_GetBoolean(go, "--olonepass") ? TRUE : FALSE;
245 pli->do_not_iterate = esl_opt_GetBoolean(go, "--noiter") ? TRUE : FALSE;
246 pli->do_time_F1 = esl_opt_GetBoolean(go, "--timeF1") ? TRUE : FALSE;
247 pli->do_time_F2 = esl_opt_GetBoolean(go, "--timeF2") ? TRUE : FALSE;
248 pli->do_time_F3 = esl_opt_GetBoolean(go, "--timeF3") ? TRUE : FALSE;
249 pli->do_time_F4 = esl_opt_GetBoolean(go, "--timeF4") ? TRUE : FALSE;
250 pli->do_time_F5 = esl_opt_GetBoolean(go, "--timeF5") ? TRUE : FALSE;
251 pli->do_time_F6 = esl_opt_GetBoolean(go, "--timeF6") ? TRUE : FALSE;
252 pli->do_trm_F3 = esl_opt_GetBoolean(go, "--trmF3") ? TRUE : FALSE;
253
254 /* hard-coded miscellaneous parameters that were command-line
255 * settable in past testing, and could be in future testing.
256 */
257 pli->smult = 2.0;
258 pli->wmult = 1.0;
259 pli->mlmult = 0.1;
260 pli->cmult = 1.25;
261 /* NOTE: pli->cmult is tied to the minimum allowable value for --wcx
262 * in cmsearch.c and cmscan.c (they're both 1.25). If you change
263 * one, change both.
264 */
265
266 /* Configure reporting thresholds */
267 pli->by_E = TRUE;
268 pli->E = esl_opt_GetReal(go, "-E");
269 pli->T = 0.0;
270 pli->use_bit_cutoffs = FALSE;
271 if (esl_opt_IsOn(go, "-T")) {
272 pli->T = esl_opt_GetReal(go, "-T");
273 pli->by_E = FALSE;
274 }
275
276 /* Configure inclusion thresholds */
277 pli->inc_by_E = TRUE;
278 pli->incE = esl_opt_GetReal(go, "--incE");
279 pli->incT = 0.0;
280 if (esl_opt_IsOn(go, "--incT")) {
281 pli->incT = esl_opt_GetReal(go, "--incT");
282 pli->inc_by_E = FALSE;
283 }
284
285 /* Configure for one of the model-specific thresholding options */
286 if (esl_opt_GetBoolean(go, "--cut_ga")) {
287 pli->T = 0.0;
288 pli->by_E = FALSE;
289 pli->incT = 0.0;
290 pli->inc_by_E = FALSE;
291 pli->use_bit_cutoffs = CMH_GA;
292 }
293 if (esl_opt_GetBoolean(go, "--cut_nc")) {
294 pli->T = 0.0;
295 pli->by_E = FALSE;
296 pli->incT = 0.0;
297 pli->inc_by_E = FALSE;
298 pli->use_bit_cutoffs = CMH_NC;
299 }
300 if (esl_opt_GetBoolean(go, "--cut_tc")) {
301 pli->T = 0.0;
302 pli->by_E = FALSE;
303 pli->incT = 0.0;
304 pli->inc_by_E = FALSE;
305 pli->use_bit_cutoffs = CMH_TC;
306 }
307
308 /* Configure envelope definition parameters */
309 pli->rt1 = esl_opt_GetReal(go, "--rt1");
310 pli->rt2 = esl_opt_GetReal(go, "--rt2");
311 pli->rt3 = esl_opt_GetReal(go, "--rt3");
312 pli->ns = esl_opt_GetInteger(go, "--ns");
313 pli->ddef->rt1 = pli->rt1;
314 pli->ddef->rt2 = pli->rt2;
315 pli->ddef->rt3 = pli->rt3;
316 pli->ddef->nsamples = pli->ns;
317
318 /* configure truncation hit allowance parameters */
319 if(esl_opt_GetBoolean(go, "--anytrunc")) {
320 pli->do_trunc_ends = FALSE;
321 pli->do_trunc_any = TRUE;
322 pli->do_trunc_int = FALSE;
323 pli->do_trunc_only = FALSE;
324 pli->do_trunc_5p_ends = FALSE;
325 pli->do_trunc_3p_ends = FALSE;
326 }
327 else if(esl_opt_GetBoolean(go, "--inttrunc")) {
328 pli->do_trunc_ends = FALSE;
329 pli->do_trunc_any = FALSE;
330 pli->do_trunc_int = TRUE;
331 pli->do_trunc_only = FALSE;
332 pli->do_trunc_5p_ends = FALSE;
333 pli->do_trunc_3p_ends = FALSE;
334 }
335 else if(esl_opt_GetBoolean(go, "--onlytrunc")) {
336 pli->do_trunc_ends = FALSE;
337 pli->do_trunc_any = FALSE;
338 pli->do_trunc_int = FALSE;
339 pli->do_trunc_only = TRUE;
340 pli->do_trunc_5p_ends = FALSE;
341 pli->do_trunc_3p_ends = FALSE;
342 }
343 else if(esl_opt_GetBoolean(go, "--notrunc")) {
344 pli->do_trunc_ends = FALSE;
345 pli->do_trunc_any = FALSE;
346 pli->do_trunc_int = FALSE;
347 pli->do_trunc_only = FALSE;
348 pli->do_trunc_5p_ends = FALSE;
349 pli->do_trunc_3p_ends = FALSE;
350 }
351 else if(esl_opt_GetBoolean(go, "--5trunc")) {
352 pli->do_trunc_ends = FALSE;
353 pli->do_trunc_any = FALSE;
354 pli->do_trunc_int = FALSE;
355 pli->do_trunc_only = FALSE;
356 pli->do_trunc_5p_ends = TRUE;
357 pli->do_trunc_3p_ends = FALSE;
358 }
359 else if(esl_opt_GetBoolean(go, "--3trunc")) {
360 pli->do_trunc_ends = FALSE;
361 pli->do_trunc_any = FALSE;
362 pli->do_trunc_int = FALSE;
363 pli->do_trunc_only = FALSE;
364 pli->do_trunc_5p_ends = FALSE;
365 pli->do_trunc_3p_ends = TRUE;
366 }
367 else { /* default */
368 pli->do_trunc_ends = TRUE;
369 pli->do_trunc_any = FALSE;
370 pli->do_trunc_int = FALSE;
371 pli->do_trunc_only = FALSE;
372 pli->do_trunc_5p_ends = FALSE;
373 pli->do_trunc_3p_ends = FALSE;
374 }
375
376 /* Set Z, the search space size. This is used for E-value
377 * calculations and for setting filter thresholds by default
378 * (i.e. if none of --max, --nohmm, --mid, --rfam are used) which is
379 * why we do this here, before setting filter thresholds. The
380 * database size was passed in, if -Z <x> enabled, we overwrite the
381 * passed in value with <x>.
382 */
383 if (esl_opt_IsOn(go, "-Z")) {
384 pli->Z_setby = CM_ZSETBY_OPTION;
385 pli->Z = (int64_t) (esl_opt_GetReal(go, "-Z") * 1000000.);
386 }
387 else {
388 pli->Z = Z; /* Z is an input variable to this function */
389 pli->Z_setby = Z_setby; /* so is Z_setby */
390 }
391
392 /********************************************************************************/
393 /* Configure acceleration pipeline by setting filter on/off parameters
394 * and filter thresholds. (Note: we set HMM only filter parameters later
395 * independently of these.)
396 *
397 * Two steps:
398 * 1. Set filter parameters based on which of the five filtering strategies
399 * we're using.
400 * 2. Overwrite any filter parameters set on the command-line.
401 *
402 * The five exclusive filtering strategies:
403 * 1. --max: turn off all filters
404 * 2. --nohmm: turn off all HMM filters
405 * 3. --mid: turn off MSV/Viterbi HMM filters
406 * 4. default: use all filters with DB-size dependent thresholds
407 * 5. --rfam: use all filters with strict thresholds (as if DB was size of RFAMSEQ)
408 *
409 * strategy F1?* F2/F2b? F3/F3b? F4/F4b? F5?** F6?
410 * -------- ------- ------- ------- ------- ------- -------
411 * --max off off off off off off
412 * --nohmm off off off off off on
413 * --mid off off on on on on
414 * default on on on on on on
415 * --rfam on on on on on on
416 *
417 * * By default, F1b is always off.
418 * ** By default, F5b is always off.
419 *
420 * First set defaults, then make nec changes if --max, --nohmm, --mid, --rfam
421 */
422 pli->do_max = FALSE;
423 pli->do_nohmm = FALSE;
424 pli->do_mid = FALSE;
425 pli->do_rfam = FALSE;
426 pli->do_hmmonly_cur = FALSE;
427 pli->do_hmmonly_always = FALSE;
428 pli->do_hmmonly_never = FALSE;
429 pli->do_msv = TRUE;
430 pli->do_msvbias = FALSE;
431 pli->do_vit = TRUE;
432 pli->do_vitbias = TRUE;
433 pli->do_fwd = TRUE;
434 pli->do_fwdbias = TRUE;
435 pli->do_gfwd = TRUE;
436 pli->do_gfwdbias = TRUE;
437 pli->do_edef = TRUE;
438 pli->do_edefbias = FALSE;
439 pli->do_fcyk = TRUE;
440
441 if(esl_opt_GetBoolean(go, "--max")) {
442 pli->do_max = TRUE;
443 pli->do_msv = pli->do_vit = pli->do_fwd = pli->do_gfwd = pli->do_edef = pli->do_fcyk = FALSE;
444 pli->do_msvbias = pli->do_vitbias = pli->do_fwdbias = pli->do_gfwdbias = pli->do_edefbias = pli->do_fcykenv = FALSE;
445 pli->F1 = pli->F2 = pli->F3 = pli->F4 = pli->F5 = pli->F6 = 1.0;
446 pli->F1b = pli->F2b = pli->F3b = pli->F4b = pli->F5b = pli->F6 = 1.0;
447 /* D&C truncated alignment is not robust, so we don't allow it */
448 pli->do_trunc_ends = FALSE;
449 pli->do_trunc_any = FALSE;
450 pli->do_trunc_int = FALSE;
451 pli->do_trunc_only = FALSE;
452 pli->do_trunc_5p_ends = FALSE;
453 pli->do_trunc_3p_ends = FALSE;
454 }
455 else if(esl_opt_GetBoolean(go, "--nohmm")) {
456 pli->do_nohmm = TRUE;
457 pli->do_msv = pli->do_vit = pli->do_fwd = pli->do_gfwd = pli->do_edef = FALSE;
458 pli->do_msvbias = pli->do_vitbias = pli->do_fwdbias = pli->do_gfwdbias = pli->do_edefbias = FALSE;
459 pli->F1 = pli->F2 = pli->F3 = pli->F4 = pli->F5 = 1.0;
460 pli->F1b = pli->F2b = pli->F3b = pli->F4b = pli->F5b = 1.0;
461 pli->F6 = 0.0001; /* default F6, we'll change this below if --F6 was used */
462 /* D&C truncated alignment is not robust, so we don't allow it */
463 pli->do_trunc_ends = FALSE;
464 pli->do_trunc_any = FALSE;
465 pli->do_trunc_int = FALSE;
466 pli->do_trunc_only = FALSE;
467 pli->do_trunc_5p_ends = FALSE;
468 pli->do_trunc_3p_ends = FALSE;
469 }
470 else if(esl_opt_GetBoolean(go, "--mid")) {
471 pli->do_mid = TRUE;
472 pli->do_msv = pli->do_vit = FALSE;
473 pli->do_msvbias = pli->do_vitbias = FALSE;
474 pli->F1 = pli->F2 = 1.0;
475 pli->F1b = pli->F2b = 1.0;
476 pli->F3 = pli->F3b = pli->F4 = pli->F4b = pli->F5 = pli->F5b = esl_opt_GetReal(go, "--Fmid");
477 pli->F6 = 0.0001; /* default F6, we'll change this below if --F6 was used */
478 }
479 else if(esl_opt_GetBoolean(go, "--rfam")) {
480 pli->do_rfam = TRUE;
481 pli->F1 = 0.06;
482 pli->F2 = pli->F2b = 0.02;
483 pli->F3 = pli->F3b = 0.0002;
484 pli->F4 = pli->F4b = 0.0002;
485 pli->F5 = pli->F5b = 0.0002;
486 pli->F6 = 0.0001;
487 /* these are the same as the defaults for a 20 Gb database or larger */
488 }
489 else {
490 /* None of --max, --nohmm, --mid, --rfam, --hmmonly enabled, use
491 * default strategy, set filter thresholds dependent on Z, which
492 * was set above. These default thresholds are hard-coded and were
493 * determined by a systematic search over possible filter
494 * threshold combinations. xref:
495 * ~nawrockie/notebook/11_0513_inf_dcmsearch_thresholds/00LOG
496 */
497 Z_Mb = esl_opt_IsOn(go, "--FZ") ? esl_opt_GetReal(go, "--FZ") : pli->Z / 1000000.;
498 /* None of --max, --nohmm, --mid, --rfam enabled, use default
499 * strategy, set filter thresholds dependent on Z, which was set
500 * above. These default thresholds are hard-coded and were determined
501 * by a systematic search over possible filter threshold combinations.
502 * xref: ~nawrockie/notebook/12_0319_inf_finalize_filter_thresholds/00LOG
503 */
504 Z_Mb = esl_opt_IsUsed(go, "--FZ") ? esl_opt_GetReal(go, "--FZ") : pli->Z / 1000000.;
505
506 /* turn off MSV bias filter and env def bias filter */
507 pli->do_msvbias = pli->do_edefbias = FALSE;
508 pli->F1b = pli->F5b = 1.0; /* these are irrelevant */
509
510 /* set all other thresholds to db size defaults */
511 if(Z_Mb >= (20000. - eslSMALLX1)) { /* Z >= 20 Gb */
512 pli->F1 = 0.06;
513 pli->F2 = pli->F2b = 0.02;
514 pli->F3 = pli->F3b = 0.0002;
515 pli->F4 = pli->F4b = 0.0002;
516 pli->F5 = pli->F5b = 0.0002;
517 pli->F6 = 0.0001;
518 }
519 else if(Z_Mb >= (2000. - eslSMALLX1)) { /* 20 Gb > Z >= 2 Gb */
520 pli->F1 = 0.15;
521 pli->F2 = pli->F2b = 0.15;
522 pli->F3 = pli->F3b = 0.0002;
523 pli->F4 = pli->F4b = 0.0002;
524 pli->F5 = pli->F5b = 0.0002;
525 pli->F6 = 0.0001;
526 }
527 else if(Z_Mb >= (200. - eslSMALLX1)) { /* 2 Gb > Z >= 200 Mb */
528 pli->F1 = 0.15;
529 pli->F2 = pli->F2b = 0.15;
530 pli->F3 = pli->F3b = 0.0008;
531 pli->F4 = pli->F4b = 0.0008;
532 pli->F5 = pli->F5b = 0.0008;
533 pli->F6 = 0.0001;
534 }
535 else if(Z_Mb >= (20. - eslSMALLX1)) { /* 200 Mb > Z >= 20 Mb */
536 pli->F1 = pli->F1b = 0.35;
537 pli->F2 = pli->F2b = 0.15;
538 pli->F3 = pli->F3b = 0.003;
539 pli->F4 = pli->F4b = 0.003;
540 pli->F5 = pli->F5b = 0.003;
541 pli->F6 = 0.0001;
542 }
543 else if(Z_Mb >= (2. - eslSMALLX1)) { /* 20 Mb > Z >= 2 Mb */
544 pli->F1 = 0.35;
545 pli->do_vit = pli->do_vitbias = FALSE;
546 pli->F2 = pli->F2b = 1.00; /* these are irrelevant */
547 pli->F3 = pli->F3b = 0.005;
548 pli->F4 = pli->F4b = 0.005;
549 pli->F5 = pli->F5b = 0.005;
550 pli->F6 = 0.0001;
551 }
552 else { /* 2 Mb > Z */
553 pli->F1 = 0.35;
554 pli->do_vit = pli->do_vitbias = FALSE;
555 pli->F2 = pli->F2b = 1.00; /* these are irrelevant */
556 pli->F3 = pli->F3b = 0.02;
557 pli->F4 = pli->F4b = 0.02;
558 pli->F5 = pli->F5b = 0.02;
559 pli->F6 = 0.0001;
560 }
561 } /* end of 'else' entered if none of --max, --nohmm, --mid, --rfam used */
562
563 /* Filter on/off parameters and thresholds are now completely set
564 * based on filtering strategy. Final step is to overwrite any that
565 * the user set on the command-line. (Only expert users should be
566 * doing this.)
567 *
568 * We have to be careful here to not turn on filters that our
569 * strategy disallows. The definition of ESL_GETOPTS <go> should
570 * enforce that incompatible options cause a failure (and thus will
571 * never exist in <go>), but we do a second check here for some
572 * combinations.
573 */
574 if((! pli->do_max) && (! pli->do_nohmm) && (! pli->do_mid)) {
575 if(esl_opt_IsOn(go, "--F1")) { pli->do_msv = TRUE; pli->F1 = ESL_MIN(1.0, esl_opt_GetReal(go, "--F1")); }
576 if(esl_opt_IsOn(go, "--F1b")) { pli->do_msvbias = TRUE; pli->F1b = ESL_MIN(1.0, esl_opt_GetReal(go, "--F1b")); }
577 if(esl_opt_IsOn(go, "--F2")) { pli->do_vit = TRUE; pli->F2 = ESL_MIN(1.0, esl_opt_GetReal(go, "--F2")); }
578 if(esl_opt_IsOn(go, "--F2b")) { pli->do_vitbias = TRUE; pli->F2b = ESL_MIN(1.0, esl_opt_GetReal(go, "--F2b")); }
579 }
580 if((! pli->do_max) && (! pli->do_nohmm)) {
581 if(esl_opt_IsOn(go, "--F3")) { pli->do_fwd = TRUE; pli->F3 = ESL_MIN(1.0, esl_opt_GetReal(go, "--F3")); }
582 if(esl_opt_IsOn(go, "--F3b")) { pli->do_fwdbias = TRUE; pli->F3b = ESL_MIN(1.0, esl_opt_GetReal(go, "--F3b")); }
583 if(esl_opt_IsOn(go, "--F4")) { pli->do_gfwd = TRUE; pli->F4 = ESL_MIN(1.0, esl_opt_GetReal(go, "--F4")); }
584 if(esl_opt_IsOn(go, "--F4b")) { pli->do_gfwdbias = TRUE; pli->F4b = ESL_MIN(1.0, esl_opt_GetReal(go, "--F4b")); }
585 if(esl_opt_IsOn(go, "--F5")) { pli->do_edef = TRUE; pli->F5 = ESL_MIN(1.0, esl_opt_GetReal(go, "--F5")); }
586 if(esl_opt_IsOn(go, "--F5b")) { pli->do_edefbias = TRUE; pli->F5b = ESL_MIN(1.0, esl_opt_GetReal(go, "--F5b")); }
587 }
588 if(! pli->do_max) {
589 if(esl_opt_IsOn(go, "--F6")) { pli->do_fcyk = TRUE; pli->F6 = ESL_MIN(1.0, esl_opt_GetReal(go, "--F6")); }
590 }
591
592 if(esl_opt_GetBoolean(go, "--noF1")) pli->do_msv = FALSE;
593 if(esl_opt_GetBoolean(go, "--noF2")) pli->do_vit = FALSE;
594 if(esl_opt_GetBoolean(go, "--noF3")) pli->do_fwd = FALSE;
595 if(esl_opt_GetBoolean(go, "--noF4")) pli->do_gfwd = FALSE;
596 if(esl_opt_GetBoolean(go, "--noF6")) pli->do_fcyk = FALSE;
597
598 if((! pli->do_max) && (! pli->do_nohmm) && (! pli->do_mid)) {
599 if(esl_opt_GetBoolean(go, "--doF1b")) pli->do_msvbias = TRUE;
600 }
601 if(esl_opt_GetBoolean(go, "--noF2b")) pli->do_vitbias = FALSE;
602 if(esl_opt_GetBoolean(go, "--noF3b")) pli->do_fwdbias = FALSE;
603 if(esl_opt_GetBoolean(go, "--noF4b")) pli->do_gfwdbias = FALSE;
604 if(esl_opt_GetBoolean(go, "--doF5b")) pli->do_edefbias = TRUE;
605
606 /* set HMM only thresholds and filter stage on/off parameters, these
607 * will only be relevant in HMM only mode (for all models if
608 * pli->do_hmmonly_always, or all models with 0 basepairs if
609 * (!pli->no_hmmonly)).
610 *
611 * First set defaults, then change them if nec.
612 */
613 pli->do_hmmonly_always = (esl_opt_GetBoolean(go, "--hmmonly")) ? TRUE : FALSE;
614 pli->do_hmmonly_never = (esl_opt_GetBoolean(go, "--nohmmonly") ||
615 esl_opt_GetBoolean(go, "--max") ||
616 esl_opt_GetBoolean(go, "--nohmm")) ? TRUE : FALSE;
617 /* pli->do_hmmonly_cur is set in cm_pli_NewModel(), stays FALSE until then */
618 pli->do_max_hmmonly = FALSE;
619 pli->do_bias_hmmonly = TRUE;
620 pli->do_null2_hmmonly = TRUE;
621 pli->F1_hmmonly = ESL_MIN(1.0, esl_opt_GetReal(go, "--hmmF1"));
622 pli->F2_hmmonly = ESL_MIN(1.0, esl_opt_GetReal(go, "--hmmF2"));
623 pli->F3_hmmonly = ESL_MIN(1.0, esl_opt_GetReal(go, "--hmmF3"));
624 if(esl_opt_GetBoolean(go, "--hmmmax")) {
625 pli->do_max_hmmonly = TRUE;
626 pli->do_bias_hmmonly = FALSE;
627 pli->F1_hmmonly = 0.3;
628 pli->F2_hmmonly = 1.0;
629 pli->F3_hmmonly = 1.0;
630 }
631 if(esl_opt_GetBoolean(go, "--hmmnonull2")) pli->do_null2_hmmonly = FALSE;
632 if(esl_opt_GetBoolean(go, "--hmmnobias")) pli->do_bias_hmmonly = FALSE;
633
634 /* Finished setting filter stage on/off parameters and thresholds */
635 /********************************************************************************/
636
637 /********************************************************************************/
638 /* Configure options for the CM stages */
639 pli->do_null2 = esl_opt_GetBoolean(go, "--null2") ? TRUE : FALSE;
640 pli->do_null3 = esl_opt_GetBoolean(go, "--nonull3") ? FALSE : TRUE;
641
642 pli->fcyk_cm_search_opts = 0;
643 pli->final_cm_search_opts = 0;
644 pli->fcyk_beta = esl_opt_GetReal(go, "--fbeta");
645 pli->fcyk_tau = esl_opt_GetReal(go, "--ftau");
646 pli->do_fcykenv = esl_opt_GetBoolean(go, "--nocykenv") ? FALSE : TRUE;
647 /* important to set F6env after F6 is set to final value */
648 pli->F6env = ESL_MIN(1.0, pli->F6 * (float) esl_opt_GetInteger(go, "--cykenvx"));
649
650 pli->final_beta = esl_opt_GetReal(go, "--beta");
651 pli->final_tau = esl_opt_GetReal(go, "--tau");
652
653 /* There are 3 options for banding in CYK filter and final round.
654 * Choice of the 3 varies depending on if pli->do_max, pli->do_nohmm
655 * or neither.
656 *
657 * if(do_max) {
658 * filter CYK is off.
659 * final round: --qdb: use QDBs, else non-banded
660 * }
661 * else if(do_nohmm) {
662 * filter CYK : --fnonbanded: no bands, else use QDBs
663 * final round: --nonbanded: no bands, else use QDBs
664 * }
665 * else { normal case
666 * filter CYK : --fnonbanded: no bands, --fqdb: use QDBs, else use HMM bands
667 * final round: --nonbanded: no bands, --qdb: use QDBs, else use HMM bands
668 * }
669 *
670 * In all cases, if QDBs used for filter CYK beta is from --fbeta
671 * <x>, final beta is from --beta <x>.
672 *
673 * In all cases, if HMM bands used for filter CYK tau is from --ftau
674 * <x>, final tau is from --tau <x>.
675 */
676
677 /* CYK filter settings, only set these if do_fcyk
678 */
679 if(pli->do_fcyk) {
680 if(pli->do_nohmm) {
681 /* special case: default behavior for fcyk is to do QDB, HMM banded is not allowed.
682 */
683 if(esl_opt_GetBoolean(go, "--fnonbanded")) pli->fcyk_cm_search_opts |= CM_SEARCH_NONBANDED;
684 else pli->fcyk_cm_search_opts |= CM_SEARCH_QDB;
685 }
686 else {
687 if (esl_opt_GetBoolean(go, "--fnonbanded")) pli->fcyk_cm_search_opts |= CM_SEARCH_NONBANDED;
688 else if(esl_opt_GetBoolean(go, "--fqdb")) pli->fcyk_cm_search_opts |= CM_SEARCH_QDB;
689 else pli->fcyk_cm_search_opts |= CM_SEARCH_HBANDED;
690 }
691 if( esl_opt_GetBoolean(go, "--fsums")) pli->fcyk_cm_search_opts |= CM_SEARCH_SUMS;
692 if(! esl_opt_GetBoolean(go, "--nonull3")) pli->fcyk_cm_search_opts |= CM_SEARCH_NULL3;
693 }
694
695 /* set up final round parameters, always set these (we always do the final CM round) */
696 if(! esl_opt_GetBoolean(go, "--cyk")) pli->final_cm_search_opts |= CM_SEARCH_INSIDE;
697 if (pli->do_max) { /* special case, default behavior in final round is to do non-banded, HMM banded is not allowed */
698 if(esl_opt_GetBoolean(go, "--qdb")) pli->final_cm_search_opts |= CM_SEARCH_QDB;
699 else pli->final_cm_search_opts |= CM_SEARCH_NONBANDED;
700 }
701 else if(pli->do_nohmm) { /* special case, default behavior in final round is to do QDB, HMM banded is not allowed */
702 if(esl_opt_GetBoolean(go, "--nonbanded")) pli->final_cm_search_opts |= CM_SEARCH_NONBANDED;
703 else pli->final_cm_search_opts |= CM_SEARCH_QDB;
704 }
705 else { /* normal case, default is HMM banded */
706 if (esl_opt_GetBoolean(go, "--nonbanded")) pli->final_cm_search_opts |= CM_SEARCH_NONBANDED;
707 else if(esl_opt_GetBoolean(go, "--qdb")) pli->final_cm_search_opts |= CM_SEARCH_QDB;
708 else pli->final_cm_search_opts |= CM_SEARCH_HBANDED;
709 }
710 if(esl_opt_GetBoolean(go, "--sums")) pli->final_cm_search_opts |= CM_SEARCH_SUMS;
711 if(esl_opt_GetBoolean(go, "--nogreedy")) pli->final_cm_search_opts |= CM_SEARCH_CMNOTGREEDY;
712
713 /* Determine cm->config_opts and cm->align_opts we'll use to
714 * configure CMs after reading within a SCAN pipeline. Search
715 * options will change for the CYK filter and final stage, so
716 * these are stored in fcyk_cm_search_opts and final_cm_search_opts
717 * determined above.
718 */
719 pli->cm_config_opts = 0;
720 pli->cm_align_opts = 0;
721 /* should we configure CM/CP9 into local mode? */
722 if(! esl_opt_GetBoolean(go, "-g")) {
723 pli->cm_config_opts |= CM_CONFIG_LOCAL;
724 if(! esl_opt_GetBoolean(go, "--cp9gloc")) {
725 pli->cm_config_opts |= CM_CONFIG_HMMLOCAL;
726 if(! esl_opt_GetBoolean(go, "--cp9noel")) {
727 pli->cm_config_opts |= CM_CONFIG_HMMEL;
728 }
729 }
730 }
731 /* should we setup for truncated alignments? */
732 if(pli->do_trunc_ends || pli->do_trunc_any || pli->do_trunc_int || pli->do_trunc_only || pli->do_trunc_5p_ends || pli->do_trunc_3p_ends) pli->cm_config_opts |= CM_CONFIG_TRUNC;
733
734 /* will we be requiring a CM_SCAN_MX? a CM_TR_SCAN_MX? */
735 if(pli->do_max || /* max mode, no filters */
736 pli->do_nohmm || /* nohmm mode, no HMM filters */
737 esl_opt_GetBoolean(go, "--fqdb") || /* user specified to use --fqdb, do it */
738 esl_opt_GetBoolean(go, "--qdb")) { /* user specified to use --qdb, do it */
739 pli->cm_config_opts |= CM_CONFIG_SCANMX;
740 if(pli->do_trunc_ends || pli->do_trunc_any || pli->do_trunc_int || pli->do_trunc_only || pli->do_trunc_5p_ends || pli->do_trunc_3p_ends) pli->cm_config_opts |= CM_CONFIG_TRSCANMX;
741 }
742 /* will we be requiring non-banded alignment matrices? */
743 if(pli->do_max || /* max mode, no filters, hit alignment will be nonbanded */
744 pli->do_nohmm || /* nohmm mode, no HMM filters, hit alignment will be nonbanded */
745 esl_opt_GetBoolean(go, "--qdb")) { /* using QDBs in final Inside stage, only safe way to go is nonbanded alignment */
746 pli->cm_align_opts |= CM_ALIGN_SMALL;
747 pli->cm_align_opts |= CM_ALIGN_CYK;
748 if(pli->do_max) pli->cm_align_opts |= CM_ALIGN_NONBANDED;
749 else pli->cm_align_opts |= CM_ALIGN_QDB; /* --nohmm, --qdb */
750 /* D&C truncated alignment is not robust, so we don't allow it */
751 pli->do_trunc_ends = FALSE;
752 pli->do_trunc_any = FALSE;
753 pli->do_trunc_int = FALSE;
754 pli->do_trunc_only = FALSE;
755 pli->do_trunc_5p_ends = FALSE;
756 pli->do_trunc_3p_ends = FALSE;
757 }
758 else {
759 pli->cm_align_opts |= CM_ALIGN_HBANDED;
760 pli->cm_align_opts |= CM_ALIGN_POST;
761 if(esl_opt_GetBoolean(go, "--acyk")) pli->cm_align_opts |= CM_ALIGN_CYK;
762 else pli->cm_align_opts |= CM_ALIGN_OPTACC;
763 }
764
765 /* Determine statistics modes for CM stages */
766 pli->do_glocal_cm_always = (esl_opt_GetBoolean(go, "-g")) ? TRUE : FALSE;
767 pli->do_glocal_cm_cur = pli->do_glocal_cm_always ? TRUE : FALSE;
768 pli->do_glocal_cm_sometimes = (esl_opt_IsOn(go, "--glist")) ? TRUE : FALSE;
769
770 pli->fcyk_cm_exp_mode = pli->do_glocal_cm_always ? EXP_CM_GC : EXP_CM_LC;
771 if(pli->final_cm_search_opts & CM_SEARCH_INSIDE) {
772 pli->final_cm_exp_mode = pli->do_glocal_cm_always ? EXP_CM_GI : EXP_CM_LI;
773 }
774 else {
775 pli->final_cm_exp_mode = pli->do_glocal_cm_always ? EXP_CM_GC : EXP_CM_LC;
776 }
777 /* finished setting up parameters for CM stages */
778 /********************************************************************************/
779
780 return pli;
781
782 ERROR:
783 cm_pipeline_Destroy(pli, NULL);
784 return NULL;
785 }
786
787 /* Function: cm_pipeline_Reuse()
788 * Synopsis: Reuse a pipeline for next target.
789 * Incept: EPN, Fri Sep 24 16:28:55 2010
790 * SRE, Fri Dec 5 10:31:36 2008 [Janelia] (p7_pipeline_Reuse())
791 *
792 * Purpose: Reuse <pli> for next target sequence (search mode)
793 * or model (scan mode).
794 *
795 * May eventually need to distinguish from reusing pipeline
796 * for next query, but we're not really focused on multiquery
797 * use of hmmscan/hmmsearch/phmmer for the moment.
798 */
799 int
800 cm_pipeline_Reuse(CM_PIPELINE *pli)
801 {
802 p7_omx_Reuse(pli->oxf);
803 p7_omx_Reuse(pli->oxb);
804 p7_omx_Reuse(pli->fwd);
805 p7_omx_Reuse(pli->bck);
806 p7_domaindef_Reuse(pli->ddef);
807 /* TO DO, write scanmatrixreuse */
808 return eslOK;
809 }
810
811
812 /* Function: cm_pipeline_Destroy()
813 * Synopsis: Free a <CM_PIPELINE> object.
814 * Incept: EPN, Fri Sep 24 16:29:34 2010
815 * SRE, Fri Dec 5 10:30:23 2008 [Janelia] (p7_pipeline_Destroy())
816 *
817 * Purpose: Free a <CM_PIPELINE> object. Requires a cm (sigh) to free the scan matrix.
818 */
819 void
820 cm_pipeline_Destroy(CM_PIPELINE *pli, CM_t *cm)
821 {
822 if (pli == NULL) return;
823
824 p7_omx_Destroy(pli->oxf);
825 p7_omx_Destroy(pli->oxb);
826 p7_omx_Destroy(pli->fwd);
827 p7_omx_Destroy(pli->bck);
828 p7_gmx_Destroy(pli->gfwd);
829 p7_gmx_Destroy(pli->gbck);
830 p7_gmx_Destroy(pli->gxf);
831 p7_gmx_Destroy(pli->gxb);
832 esl_randomness_Destroy(pli->r);
833 p7_domaindef_Destroy(pli->ddef);
834 free(pli);
835 }
836 /*---------------- end, CM_PIPELINE object ----------------------*/
837
838
839 /*****************************************************************
840 * 2. Pipeline API.
841 *****************************************************************/
842
843 /* Function: cm_pli_TargetReportable
844 * Synopsis: Returns TRUE if target score meets reporting threshold.
845 * Incept: EPN, Fri Sep 24 16:34:37 2010
846 * SRE, Tue Dec 9 08:57:26 2008 [Janelia] (p7_pli_TargetReportable())
847 *
848 * Purpose: Returns <TRUE> if the bit score <score> and/or
849 * E-value <Eval> meets per-target reporting thresholds
850 * for the processing pipeline.
851 */
852 int
853 cm_pli_TargetReportable(CM_PIPELINE *pli, float score, double Eval)
854 {
855 if ( pli->by_E && Eval <= pli->E) return TRUE;
856 else if (! pli->by_E && score >= pli->T) return TRUE;
857 return FALSE;
858 }
859
860 /* Function: cm_pli_TargetIncludable()
861 * Synopsis: Returns TRUE if target score meets inclusion threshold.
862 * Incept: EPN, Fri Sep 24 16:32:43 2010
863 * SRE, Fri Jan 16 11:18:08 2009 [Janelia] (p7_pli_TargetIncludable())
864 */
865 int
866 cm_pli_TargetIncludable(CM_PIPELINE *pli, float score, double Eval)
867 {
868 if ( pli->by_E && Eval <= pli->incE) return TRUE;
869 else if (! pli->by_E && score >= pli->incT) return TRUE;
870
871 return FALSE;
872 }
873
874 /* Function: cm_pli_NewModel()
875 * Synopsis: Prepare pipeline for a new CM/HMM
876 * Incept: EPN, Fri Sep 24 16:35:35 2010
877 * SRE, Fri Dec 5 10:35:37 2008 [Janelia] (p7_pli_NewModel())
878 *
879 * Purpose: Caller has a new model.
880 * Prepare the pipeline <pli> to receive this model as either
881 * a query or a target.
882 *
883 * The information of the model we may receive varies, as
884 * indicated by <modmode> and <pli->mode>. This is enforced
885 * by a contract check upon entrance, failure causes immediate
886 * return of eslEINCOMPAT.
887 *
888 * case <pli->mode> <modmode> <cm> <om> and <bg>
889 * ---- -------------- --------------- -------- -------------
890 * 1 CM_SEARCH_SEQS CM_NEWMODEL_CM non-null non-null
891 * 2 CM_SCAN_SEQS CM_NEWMODEL_MSV NULL non-null
892 * 3 CM_SCAN_SEQS CM_NEWMODEL_CM non-null NULL
893 *
894 * <cm_clen>, <cm_W> and <cm_nbp> are always valid, but are only
895 * necessary for case 2.
896 *
897 * Note that if we're in SEARCH mode, <modmode> will
898 * always be CM_NEWMODEL_CM. In SCAN mode, we may only
899 * call this function once with <modmode> == CM_NEWMODEL_MSV
900 * (case 2 above, this happens if no hit from the query
901 * sequence survives to the CM stage). Also, if we
902 * are in SCAN mode with modmode==CM_NEWMODEL_CM we must
903 * have entered this function previously for the same
904 * 'model' with modmode==CM_NEWMODEL_MSV.
905 *
906 * The pipeline may alter the null model in <bg> in a
907 * model-specific way (if we're using composition bias
908 * filter HMMs in the pipeline).
909 *
910 * The value of <cm_W> passed in will not be changed. Upon
911 * exit >pli->cmW> will == <cm_W>. That is, we don't
912 * potentially set it based on the wcx mechanism within
913 * this function, the caller must have already done that if
914 * it should be done. (The wcx mechanism allows the user to
915 * specify W as a multiple (<x>) of the consensus length of
916 * the model via the --wcx option, controlled with
917 * pli->use_wcx and pli->wcx.)
918 *
919 * If pli->do_hmmonly_cur and pli->by_E, we have to set
920 * pli->T as the bit score corresponding to an E-value
921 * of pli->E. This requires <p7_evparam> and <p7_max_length>.
922 * (If we're in SEARCH mode, <p7_max_length> will likely
923 * be equal to om->max_length, but in SCAN mode om may
924 * be NULL).
925 *
926 * <cur_clan_idx> indicates the index of the clan for the
927 * model and will be transferred to any hits (CM_HIT) found
928 * with this model. It can be -1 if the model either does
929 * not belong to a clan or if we are not keeping track of
930 * clans (which is the usual case: clans are only used for
931 * certain options in cmscan).
932 *
933 * <glocal_kh> is irrelevant (and can be NULL) unless
934 * we're in case 2 and pli->do_glocal_cm_sometimes is
935 * TRUE. If so, we update pli parameters relevant to
936 * the CM model configuration (local/global) depending
937 * on whether om->name is in <glocal_kh> (if it is,
938 * we set configuration to global, else we set it
939 * to local). This allows us to use glocal mode for
940 * only those models who are keys in glocal_kh (the
941 * cmscan --glist option) which was implemented between
942 * v1.1rc1 and v1.1rc2 solely to handle the fact that
943 * some Rfam bit score thresholds correspond to glocal
944 * mode and others to local (i.e. it's a brutal hack).
945 *
946 * Returns: <eslOK> on success.
947 *
948 * <eslEINCOMPAT> in contract is not met.
949 *
950 * <eslEINVAL> if pipeline expects to be able to use a
951 * model's bit score thresholds, but this model does not
952 * have the appropriate ones set.
953 */
954 int
955 cm_pli_NewModel(CM_PIPELINE *pli, int modmode, CM_t *cm, int cm_clen, int cm_W, int cm_nbp, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, int p7_max_length, int64_t cur_cm_idx, int cur_clan_idx, ESL_KEYHASH *glocal_kh)
956 {
957 int status = eslOK;
958 float T;
959
960 /* check contract */
961 if(pli->mode == CM_SEARCH_SEQS) { /* case 1 */
962 if(modmode != CM_NEWMODEL_CM)
963 if(cm == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SEARCH mode and CM is NULL");
964 if(cm->clen != cm_clen) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, cm->clen != cm_clen");
965 if(cm->W != cm_W) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, cm->W != cm_W");
966 if(CMCountNodetype(cm, MATP_nd) != cm_nbp) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, cm->clen != cm_clen");
967 if(om == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SEARCH mode and om is NULL");
968 if(bg == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SEARCH mode and bg is NULL");
969 }
970 else if(pli->mode == CM_SCAN_MODELS) {
971 if(modmode == CM_NEWMODEL_MSV) { /* case 2 */
972 if(cm != NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SCAN/MSV mode, and CM is non-NULL");
973 if(om == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SCAN/MSV mode, and om is NULL");
974 if(bg == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SCAN/MSV mode, and bg is NULL");
975 }
976 else if(modmode == CM_NEWMODEL_CM) { /* case 3 */
977 if(cm == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SCAN/CM mode, and CM is NULL");
978 if(cm->clen != cm_clen) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, cm->clen != cm_clen");
979 if(cm->W != cm_W) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, cm->W != cm_W");
980 if(CMCountNodetype(cm, MATP_nd) != cm_nbp) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, cm->clen != cm_clen");
981 if(om != NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SCAN/CM mode, and om is non-NULL");
982 if(bg != NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, SCAN/CM mode, and bg is non-NULL");
983 }
984 }
985
986 pli->cur_cm_idx = cur_cm_idx;
987 pli->cur_clan_idx = cur_clan_idx;
988
989 /* Two sets (A and B) of value updates:
990 * case 1: we do both sets
991 * case 2: we do set A only
992 * case 3: we do set B only
993 */
994 if(pli->mode == CM_SEARCH_SEQS || modmode == CM_NEWMODEL_MSV) {
995 /* set A updates: case 1 and 2 do these */
996
997 /* set model configuration local/global if we're in SCAN mode and do_glocal_cm_sometimes is TRUE*/
998 if(modmode == CM_NEWMODEL_MSV && pli->do_glocal_cm_sometimes) {
999 if(glocal_kh == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, do_glocal_cm_sometimes is TRUE but glocal_kh is NULL");
1000 if(pli->do_glocal_cm_always) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "cm_pli_NewModel(), contract violated, do_glocal_cm_sometimes and do_glocal_cm_always both TRUE");
1001 status = esl_keyhash_Lookup(glocal_kh, om->name, -1, NULL);
1002 if(status == eslOK) { /* om->name is in glocal_kh, use glocal CM stages for this model */
1003 pli->do_glocal_cm_cur = TRUE;
1004 pli->fcyk_cm_exp_mode = EXP_CM_GC;
1005 pli->final_cm_exp_mode = (pli->final_cm_search_opts & CM_SEARCH_INSIDE) ? EXP_CM_GI : EXP_CM_GC;
1006 pli->cm_config_opts &= ~CM_CONFIG_LOCAL;
1007 pli->cm_config_opts &= ~CM_CONFIG_HMMLOCAL;
1008 pli->cm_config_opts &= ~CM_CONFIG_HMMEL;
1009 }
1010 else if (status == eslENOTFOUND) { /* om->name is not in glocal_kh, use local CM stages for this model */
1011 pli->do_glocal_cm_cur = FALSE;
1012 pli->fcyk_cm_exp_mode = EXP_CM_LC;
1013 pli->final_cm_exp_mode = (pli->final_cm_search_opts & CM_SEARCH_INSIDE) ? EXP_CM_LI : EXP_CM_LC;
1014 pli->cm_config_opts |= CM_CONFIG_LOCAL;
1015 pli->cm_config_opts |= CM_CONFIG_HMMLOCAL;
1016 pli->cm_config_opts |= CM_CONFIG_HMMEL;
1017 }
1018 else { ESL_FAIL(status, pli->errbuf, "cm_pli_NewModel(), unexpected error looking up model %s\n", om->name); }
1019 }
1020
1021 /* determine if we should use the special HMM only pipeline for this model,
1022 * if pli->do_hmmonly_never == TRUE: we won't,
1023 * if pli->do_glocal_cm_cur == TRUE: we won't,
1024 * if pli->do_hmmonly_always == TRUE: we will,
1025 * else we will only if model has 0 base pairs.
1026 */
1027
1028 if (pli->do_hmmonly_never || pli->do_glocal_cm_cur) pli->do_hmmonly_cur = FALSE;
1029 else if(pli->do_hmmonly_always || cm_nbp == 0) pli->do_hmmonly_cur = TRUE;
1030 else pli->do_hmmonly_cur = FALSE;
1031
1032 if(pli->do_hmmonly_cur) {
1033 pli->nmodels_hmmonly++;
1034 pli->nnodes_hmmonly += cm_clen;
1035 }
1036 else {
1037 pli->nmodels++;
1038 pli->nnodes += cm_clen;
1039 }
1040
1041 if (pli->do_msvbias || pli->do_vitbias || pli->do_fwdbias || pli->do_gfwdbias || pli->do_edefbias) {
1042 p7_bg_SetFilter(bg, om->M, om->compo);
1043 }
1044 /* copy some values for the model */
1045 pli->cmW = cm_W;
1046 pli->clen = cm_clen;
1047
1048 /* determine pli->maxW, this will be one more than the number of
1049 * residues that must overlap between adjacent windows on a single
1050 * sequence, this is MAX of cm->W and pli->cmult * cm->clen.
1051 * wmult is hardcoded as 1.0 and cmult as 1.25 in
1052 * cm_pipeline_Create(). (cmult is purposefully equal to the
1053 * minimum value for <x> from --wcx so that <x> is always obeyed.)
1054 */
1055 pli->maxW = ESL_MAX(pli->wmult * cm_W, pli->cmult * cm_clen);
1056 }
1057 if(pli->mode == CM_SEARCH_SEQS || modmode == CM_NEWMODEL_CM) {
1058 /* set B updates: case 1 and 3 do these (they require a valid CM) */
1059
1060 /* Update the current effective database size so it pertains to
1061 * the new model. Also, If we're using an E-value threshold
1062 * determine the bit score for this model that pertains to that
1063 * E-value. We have to do this differently if we're in HMM-only
1064 * mode or not. If we're in HMM-only mode we probably don't even
1065 * have CM E-value parameters.
1066 */
1067 if(pli->do_hmmonly_cur) {
1068 if(pli->by_E) {
1069 pli->T = cm_p7_E2Score(pli->E, pli->Z, p7_max_length, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
1070 }
1071 }
1072 else { /* ! do_hmmonly_cur */
1073 if((status = UpdateExpsForDBSize(cm, pli->errbuf, pli->Z)) != eslOK) return status;
1074 if(pli->by_E) {
1075 if((status = E2ScoreGivenExpInfo(cm->expA[pli->final_cm_exp_mode], pli->errbuf, pli->E, &T)) != eslOK) ESL_FAIL(status, pli->errbuf, "problem determining min score for E-value %6g for model %s\n", pli->E, cm->name);
1076 pli->T = (double) T;
1077 }
1078 }
1079
1080 /* if we're using Rfam GA, NC, or TC cutoffs, update them for this model */
1081 if (pli->use_bit_cutoffs) {
1082 if((status = cm_pli_NewModelThresholds(pli, cm)) != eslOK) return status;
1083 }
1084 }
1085 return eslOK;
1086 }
1087
1088 /* Function: cm_pli_NewModelThresholds()
1089 * Synopsis: Set reporting and inclusion bit score thresholds on a new model.
1090 * Incept: EPN, Wed Jun 15 14:40:46 2011
1091 * SRE, Sat Oct 17 12:07:43 2009 [Janelia] (p7_pli_NewModelThresholds()
1092 *
1093 * Purpose: Set the bit score thresholds on a new model, if we're
1094 * either using Rfam GA, TC, or NC cutoffs for reporting or
1095 * inclusion, and/or if we already know the total
1096 * database size.
1097 *
1098 * In a "search" pipeline, this only needs to be done once
1099 * per query model, so <cm_pli_NewModelThresholds()> gets
1100 * called by <cm_pli_NewModel()>.
1101 *
1102 * In a "scan" pipeline, this needs to be called for each
1103 * target model.
1104 *
1105 * Returns: <eslOK> on success.
1106 *
1107 * <eslEINVAL> if pipeline expects to be able to use a
1108 * model's bit score thresholds, but this model does not
1109 * have the appropriate ones set.
1110 *
1111 * Xref: Written to fix bug #h60 (p7_pli_NewModelThreshold())
1112 */
1113 int
1114 cm_pli_NewModelThresholds(CM_PIPELINE *pli, CM_t *cm)
1115 {
1116
1117 if (pli->use_bit_cutoffs) {
1118 if(pli->use_bit_cutoffs == CMH_GA) {
1119 if (! (cm->flags & CMH_GA)) ESL_FAIL(eslEINVAL, pli->errbuf, "GA bit threshold unavailable for model %s\n", cm->name);
1120 pli->T = pli->incT = cm->ga;
1121 }
1122 else if(pli->use_bit_cutoffs == CMH_TC) {
1123 if (! (cm->flags & CMH_TC)) ESL_FAIL(eslEINVAL, pli->errbuf, "TC bit threshold unavailable for model %s\n", cm->name);
1124 pli->T = pli->incT = cm->tc;
1125 }
1126 else if(pli->use_bit_cutoffs == CMH_NC) {
1127 if (! (cm->flags & CMH_NC)) ESL_FAIL(eslEINVAL, pli->errbuf, "NC bit threshold unavailable for model %s\n", cm->name);
1128 pli->T = pli->incT = cm->nc;
1129 }
1130 }
1131 return eslOK;
1132 }
1133
1134 /* Function: cm_pli_NewSeq()
1135 * Synopsis: Prepare pipeline for a new sequence (target or query)
1136 * Incept: EPN, Fri Sep 24 16:39:52 2010
1137 * SRE, Fri Dec 5 10:57:15 2008 [Janelia] (p7_pli_NewSeq())
1138 *
1139 * Purpose: Caller has a new sequence <sq>. Prepare the pipeline <pli>
1140 * to receive this model as either a query or a target.
1141 *
1142 * This function differs from the analog in the P7 HMMER3
1143 * pipeline in that we don't update the <nres> residue
1144 * count here. In a CM pipeline, we keep pass-specific
1145 * counts of residues, and those counts are updated in
1146 * cm_Pipeline(). Also, we don't update pli->Z, which must
1147 * be set at beginning of a search, in the P7 pipeline
1148 * Z is updated as sequences are read, by default.
1149 *
1150 * Returns: <eslOK> on success.
1151 */
1152 int
1153 cm_pli_NewSeq(CM_PIPELINE *pli, const ESL_SQ *sq, int64_t cur_seq_idx)
1154 {
1155 /* Update cur_seq_idx, which is a unique identifier for the sequence, so
1156 * we can reliably remove overlaps. This index is copied to all the
1157 * hit objects for all hits found by the pipeline when searching this sequence. */
1158 pli->cur_seq_idx = cur_seq_idx;
1159
1160 return eslOK;
1161 }
1162
1163 /* Function: cm_pipeline_Merge()
1164 * Synopsis: Merge pipeline statistics from <p2> with those in <p1>.
1165 * Incept: EPN, Fri Sep 24 16:41:17 2010
1166 *
1167 * Purpose: Merge statistics from <p2> with those in <p1>. The
1168 * merged statistics will be saved in <p1>.
1169 *
1170 * Returns: <eslOK> on success.
1171 *
1172 * <eslEINVAL> if pipeline expects to be able to use a
1173 * model's bit score thresholds, but this model does not
1174 * have the appropriate ones set.
1175 */
1176 int
1177 cm_pipeline_Merge(CM_PIPELINE *p1, CM_PIPELINE *p2)
1178 {
1179 /* if we are searching a sequence database, we need to keep track of the
1180 * number of sequences and residues processed.
1181 */
1182 int p; /* counter over pipeline passes */
1183
1184 if (p1->mode == CM_SEARCH_SEQS)
1185 {
1186 p1->nseqs += p2->nseqs;
1187 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].npli_top += p2->acct[p].npli_top;
1188 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].npli_bot += p2->acct[p].npli_bot;
1189 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].nres_top += p2->acct[p].nres_top;
1190 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].nres_bot += p2->acct[p].nres_bot;
1191 }
1192 else
1193 { /* SCAN mode */
1194 p1->nmodels += p2->nmodels;
1195 p1->nnodes += p2->nnodes;
1196 p1->nmodels_hmmonly += p2->nmodels_hmmonly;
1197 p1->nnodes_hmmonly += p2->nnodes_hmmonly;
1198 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].nres_top += p2->acct[p].nres_top;
1199 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].nres_bot += p2->acct[p].nres_bot;
1200 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].npli_top += p2->acct[p].npli_top;
1201 for(p = 0; p < NPLI_PASSES; p++) p1->acct[p].npli_bot += p2->acct[p].npli_bot;
1202 /* If target CM file was small, it's possible that <p1->nseqs>
1203 * is 0 and <p2->nseqs> is not, or vice versa. This happens if
1204 * we had so few CMs in our database that not all threads got to
1205 * actually do a search. To properly handle this case, we take
1206 * the max instead of adding.
1207 */
1208 p1->nseqs = ESL_MAX(p1->nseqs, p2->nseqs);
1209 }
1210
1211 for(p = 0; p < NPLI_PASSES; p++) {
1212 p1->acct[p].n_past_msv += p2->acct[p].n_past_msv;
1213 p1->acct[p].n_past_vit += p2->acct[p].n_past_vit;
1214 p1->acct[p].n_past_fwd += p2->acct[p].n_past_fwd;
1215 p1->acct[p].n_past_gfwd += p2->acct[p].n_past_gfwd;
1216 p1->acct[p].n_past_edef += p2->acct[p].n_past_edef;
1217 p1->acct[p].n_past_cyk += p2->acct[p].n_past_cyk;
1218 p1->acct[p].n_past_ins += p2->acct[p].n_past_ins;
1219 p1->acct[p].n_output += p2->acct[p].n_output;
1220
1221 p1->acct[p].n_past_msvbias += p2->acct[p].n_past_msvbias;
1222 p1->acct[p].n_past_vitbias += p2->acct[p].n_past_vitbias;
1223 p1->acct[p].n_past_fwdbias += p2->acct[p].n_past_fwdbias;
1224 p1->acct[p].n_past_gfwdbias += p2->acct[p].n_past_gfwdbias;
1225 p1->acct[p].n_past_edefbias += p2->acct[p].n_past_edefbias;
1226
1227 p1->acct[p].pos_past_msv += p2->acct[p].pos_past_msv;
1228 p1->acct[p].pos_past_vit += p2->acct[p].pos_past_vit;
1229 p1->acct[p].pos_past_fwd += p2->acct[p].pos_past_fwd;
1230 p1->acct[p].pos_past_gfwd += p2->acct[p].pos_past_gfwd;
1231 p1->acct[p].pos_past_edef += p2->acct[p].pos_past_edef;
1232 p1->acct[p].pos_past_cyk += p2->acct[p].pos_past_cyk;
1233 p1->acct[p].pos_past_ins += p2->acct[p].pos_past_ins;
1234 p1->acct[p].pos_output += p2->acct[p].pos_output;
1235
1236 p1->acct[p].pos_past_msvbias += p2->acct[p].pos_past_msvbias;
1237 p1->acct[p].pos_past_vitbias += p2->acct[p].pos_past_vitbias;
1238 p1->acct[p].pos_past_fwdbias += p2->acct[p].pos_past_fwdbias;
1239 p1->acct[p].pos_past_gfwdbias+= p2->acct[p].pos_past_gfwdbias;
1240 p1->acct[p].pos_past_edefbias += p2->acct[p].pos_past_edefbias;
1241
1242 p1->acct[p].n_overflow_fcyk += p2->acct[p].n_overflow_fcyk;
1243 p1->acct[p].n_overflow_final += p2->acct[p].n_overflow_final;
1244 p1->acct[p].n_aln_hb += p2->acct[p].n_aln_hb;
1245 p1->acct[p].n_aln_dccyk += p2->acct[p].n_aln_dccyk;
1246 }
1247
1248 return eslOK;
1249 }
1250
1251 /* Function: cm_Pipeline()
1252 * Synopsis: The accelerated seq/CM comparison pipeline.
1253 * Incept: EPN, Fri Sep 24 16:42:21 2010
1254 * TJW, Fri Feb 26 10:17:53 2018 [Janelia] (p7_Pipeline_Longtargets())
1255 *
1256 * Purpose: Run the accelerated pipeline to compare p7 HMM <om> and
1257 * covariance model <opt_cm> against sequence <sq>. This
1258 * function calls other functions specific to each stage of
1259 * the pipeline: pli_p7_filter(), pli_p7_env_def(),
1260 * pli_cyk_env_filter(), pli_cyk_seq_filter(),
1261 * pli_final_stage().
1262 *
1263 * Returns: <eslOK> on success. If a significant hit is obtained,
1264 * its information is added to the growing <hitlist>.
1265 *
1266 * <eslEINVAL> if (in a scan pipeline) we're supposed to
1267 * set GA/TC/NC bit score thresholds but the model doesn't
1268 * have any OR if problem with start..stop order (stop > start)
1269 * when comparing overlaps if pli->do_onepass_olap.
1270 *
1271 * <eslERANGE> on numerical overflow errors in the
1272 * optimized vector implementations; particularly in
1273 * posterior decoding. I don't believe this is possible for
1274 * multihit local models, but I'm set up to catch it
1275 * anyway. We may emit a warning to the user, but cleanly
1276 * skip the problematic sequence and continue.
1277 *
1278 *
1279 * Throws: <eslEMEM> on allocation failure.
1280 *
1281 * Xref: J4/25.
1282 */
1283 int
1284 cm_Pipeline(CM_PIPELINE *pli, off_t cm_offset, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, P7_SCOREDATA *msvdata, ESL_SQ *sq, CM_TOPHITS *hitlist, int in_rc, P7_HMM **opt_hmm, P7_PROFILE **opt_gm, P7_PROFILE **opt_Rgm, P7_PROFILE **opt_Lgm, P7_PROFILE **opt_Tgm, CM_t **opt_cm)
1285 {
1286 int status;
1287 int nwin = 0; /* number of windows surviving MSV & Vit & lFwd, filled by pli_p7_filter() */
1288 int64_t *ws = NULL; /* [0..i..nwin-1] window start positions, filled by pli_p7_filter() */
1289 int64_t *we = NULL; /* [0..i..nwin-1] window end positions, filled by pli_p7_filter() */
1290 float *wb = NULL; /* [0..i..nwin-1] window bit scores, filled by pli_p7_filter, relevant only if pli->do_trm_F3 is TRUE */
1291 int *np7envA =NULL; /* [0..p..NPLI_PASSES] number of envelopes surviving MSV & Vit & lFwd & gFwd & EnvDef, filled by pli_p7_env_def() */
1292 int64_t **p7esAA = NULL; /* [0..p..NPLI_PASSES][0..i..np7env-1] window start positions, filled by pli_p7_env_def() */
1293 int64_t **p7eeAA = NULL; /* [0..p..NPLI_PASSES][0..i..np7env-1] window end positions, filled by pli_p7_env_def() */
1294 float **p7ebAA = NULL; /* [0..p..NPLI_PASSES][0..i..np7env-1] window bit score, filled by pli_p7_env_def() */
1295 int nenv = 0; /* number of envelopes surviving CYK filter, filled by pli_cyk_env_filter() or pli_cyk_seq_filter() */
1296 int64_t *es = NULL; /* [0..i..nenv-1] envelope start positions, filled by pli_cyk_env_filter() or pli_cyk_seq_filter() */
1297 int64_t *ee = NULL; /* [0..i..nenv-1] envelope end positions, filled by pli_cyk_env_filter() or pli_cyk_seq_filter() */
1298 int i; /* counter over envelopes */
1299 CM_HIT *hit = NULL; /* ptr to the current hit output data, only used if pli->do_trmF3 */
1300
1301 /* variables necessary for re-searching sequence ends */
1302 ESL_SQ *sq2search = NULL; /* a pointer to the sequence to search on current pass */
1303 ESL_SQ *term5sq = NULL; /* a copy of the 5'-most pli->maxW residues from sq */
1304 ESL_SQ *term3sq = NULL; /* a copy of the 3'-most pli->maxW residues from sq */
1305 int p; /* counter over passes */
1306 int nwin_pass_std_any = -1; /* number of windows that survived pass 1 (PLI_PASS_STD_ANY), -1 indicates we didn't run PLI_PASS_STD_ANY */
1307 int do_pass_std_any; /* should we do pass 2 (PLI_PASS_STD_ANY) search the full sequence with CM pipeline? */
1308 int do_pass_5p_only_force; /* should we do pass 2 (PLI_PASS_5P_ONLY_FORCE)? re-search the 5'-most pli->maxW residues */
1309 int do_pass_3p_only_force; /* should we do pass 3 (PLI_PASS_3P_ONLY_FORCE)? re-search the 3'-most pli->maxW residues */
1310 int do_pass_5p_and_3p_force; /* should we do pass 4 (PLI_PASS_5P_AND_3P_FORCE)? re-search the full sequence allowing 5' and 3' truncated hits? */
1311 int do_pass_5p_and_3p_any; /* should we do pass 5 (PLI_PASS_5P_AND_3P_ANY)? re-search the full sequence allowing for all truncated hits? */
1312 int do_pass_hmm_only_any; /* should we do pass 6 (PLI_PASS_HMM_ONLY_ANY) search the full sequence with HMM only pipeline? */
1313 int have5term; /* TRUE if sq contains the 5'-most pli->maxW residues of its source sequence */
1314 int have3term; /* TRUE if sq contains the 3'-most pli->maxW residues of its source sequence */
1315 int h; /* counter over hits */
1316 int prv_ntophits; /* number of hits */
1317 int64_t start_offset; /* offset to add to start/stop coordinates of hits found in pass 3, in which we re-search the 3' terminus */
1318
1319 /* variables necessary only if --onepass (pli->do_one_cmpass) or --olonepass (pli->do_one_cmpass_olap) */
1320 int best_pass = -1; /* best scoring pass in HMM stage, only used if pli->do_one_cmpass */
1321 int pass_olap = -1; /* set to '1' if pli->do_one_cmpass_olap and all envelopes pass the overlap test */
1322
1323 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
1324
1325 if ((! pli->do_edef) && (pli->do_one_cmpass || pli->do_one_cmpass_olap)) {
1326 ESL_FAIL(eslEINVAL, pli->errbuf, "cm_Pipeline() entered with do_edef as FALSE but do_one_cmpass or do_one_cmpass_olap is TRUE, coding bug.");
1327 }
1328
1329 /* Determine if we have the 5' and/or 3' termini. We can do this
1330 * because sq->L should always be valid. (Caller should enforce
1331 * this, but it takes some effort if caller is potentially reading
1332 * subsequences of large sequences. For example, cmsearch does an
1333 * initial readthrough of the entire target database file storing
1334 * the sequence lengths prior to doing any cm_Pipeline() calls (or
1335 * it uses sequence length info in an SSI index).
1336 */
1337 if(sq->start <= sq->end) { /* not in reverse complement (or 1 residue sequence, in revcomp) */
1338 have5term = (sq->start == 1) ? TRUE : FALSE;
1339 have3term = (sq->end == sq->L) ? TRUE : FALSE;
1340 }
1341 else { /* in reverse complement */
1342 have5term = (sq->start == sq->L) ? TRUE : FALSE;
1343 have3term = (sq->end == 1) ? TRUE : FALSE;
1344 }
1345
1346 #if eslDEBUGLEVEL >= 2
1347 printf("#DEBUG: \n#DEBUG: PIPELINE ENTRANCE %-15s %15s (n: %6" PRId64 " start: %6" PRId64 " end: %6" PRId64 " C: %6" PRId64 " W: %6" PRId64 " L: %6" PRId64 " have5term: %d have3term: %d)\n",
1348 sq->name, om->name, sq->n, sq->start, sq->end, sq->C, sq->W, sq->L, have5term, have3term);
1349 #endif
1350
1351 /* allocate *p7* variables, if nec */
1352 if(pli->do_edef) {
1353 ESL_ALLOC(np7envA, sizeof(int) * NPLI_PASSES);
1354 ESL_ALLOC(p7esAA, sizeof(int *) * NPLI_PASSES);
1355 ESL_ALLOC(p7eeAA, sizeof(int *) * NPLI_PASSES);
1356 ESL_ALLOC(p7ebAA, sizeof(float *) * NPLI_PASSES);
1357 for(p = 0; p < NPLI_PASSES; p++) {
1358 np7envA[p] = 0;
1359 p7esAA[p] = NULL;
1360 p7eeAA[p] = NULL;
1361 p7ebAA[p] = NULL;
1362 }
1363 }
1364
1365 /* determine which passes we'll need to do for this sequence. The
1366 * 'do_' variables indicated which type of truncations are allowed
1367 * in each pass; e.g. do_pass_5p_only_force: only 5' truncations are
1368 * allowed in that pass - we do this pass if <do_trunc_ends> is TRUE
1369 * and have5term is TRUE.
1370 */
1371 if(pli->do_hmmonly_cur) { /* HMM only mode, only PLI_PASS_HMM_ONLY_ANY is performed */
1372 do_pass_hmm_only_any = TRUE;
1373 do_pass_std_any = do_pass_5p_only_force = do_pass_3p_only_force = do_pass_5p_and_3p_force = do_pass_5p_and_3p_any = FALSE;
1374 }
1375 else if(pli->do_trunc_ends) { /* we allow std hits and truncated hits only at 5' or 3' end (default) */
1376 do_pass_std_any = TRUE;
1377 do_pass_5p_only_force = have5term ? TRUE : FALSE;
1378 do_pass_3p_only_force = have3term ? TRUE : FALSE;
1379 do_pass_5p_and_3p_force = (have5term && have3term && sq->n <= pli->maxW) ? TRUE : FALSE;
1380 do_pass_5p_and_3p_any = do_pass_hmm_only_any = FALSE;
1381 }
1382 else if(pli->do_trunc_5p_ends) { /* we allow truncated hits only at 5' end */
1383 do_pass_std_any = TRUE;
1384 do_pass_5p_only_force = have5term ? TRUE : FALSE;
1385 do_pass_3p_only_force = do_pass_5p_and_3p_force = do_pass_5p_and_3p_any = do_pass_hmm_only_any = FALSE;
1386 }
1387 else if(pli->do_trunc_3p_ends) { /* we allow truncated hits only at 3' end */
1388 do_pass_std_any = TRUE;
1389 do_pass_3p_only_force = have3term ? TRUE : FALSE;
1390 do_pass_5p_only_force = do_pass_5p_and_3p_force = do_pass_5p_and_3p_any = do_pass_hmm_only_any = FALSE;
1391 }
1392 else if(pli->do_trunc_any) { /* we allow std hits and truncated hits at 5' and/or 3' ends (like default) but also any internal truncated hit (PLI_PASS_5P_AND_3P_ANY) */
1393 do_pass_std_any = TRUE;
1394 do_pass_5p_only_force = have5term ? TRUE : FALSE;
1395 do_pass_3p_only_force = have3term ? TRUE : FALSE;
1396 do_pass_5p_and_3p_force = (have5term && have3term && sq->n <= pli->maxW) ? TRUE : FALSE;
1397 do_pass_5p_and_3p_any = TRUE;
1398 do_pass_hmm_only_any = FALSE;
1399 }
1400 else if(pli->do_trunc_int) { /* we allow std hits and any internal truncated hit (PLI_PASS_5P_AND_3P_ANY) but not truncations at end (--anytrunc behavior from v1.1 to v1.1.4) */
1401 do_pass_std_any = TRUE;
1402 do_pass_5p_and_3p_any = TRUE;
1403 do_pass_5p_only_force = do_pass_3p_only_force = do_pass_5p_and_3p_force = do_pass_hmm_only_any = FALSE;
1404 }
1405 else if(pli->do_trunc_only) { /* we do not allow std hits, and only allow internal truncated hits (PLI_PASS_5P_AND_3P_ANY) */
1406 do_pass_5p_and_3p_any = TRUE;
1407 do_pass_std_any = do_pass_5p_only_force = do_pass_3p_only_force = do_pass_5p_and_3p_force = do_pass_hmm_only_any = FALSE;
1408 }
1409 else { /* we're not allowing any truncated hits */
1410 do_pass_std_any = TRUE;
1411 do_pass_5p_only_force = do_pass_3p_only_force = do_pass_5p_and_3p_force = do_pass_5p_and_3p_any = do_pass_hmm_only_any = FALSE;
1412 }
1413
1414 #if eslDEBUGLEVEL >= 1
1415 printf("#DEBUG: in cm_Pipeline() %s\n", sq->name);
1416 printf("#DEBUG: do_pass_std_any: %d\n", do_pass_std_any);
1417 printf("#DEBUG: do_pass_5p_only_force: %d\n", do_pass_5p_only_force);
1418 printf("#DEBUG: do_pass_3p_only_force: %d\n", do_pass_3p_only_force);
1419 printf("#DEBUG: do_pass_5p_and_3p_force: %d\n", do_pass_5p_and_3p_force);
1420 printf("#DEBUG: do_pass_5p_and_3p_any: %d\n", do_pass_5p_and_3p_any);
1421 printf("#DEBUG: do_pass_hmm_only_any: %d\n", do_pass_hmm_only_any);
1422 #endif
1423
1424 /* First loop over each pipeline pass:
1425 * A. Update pipeline accounting numbers
1426 * B. Complete all HMM-based calculations (we'll do CM calculations in second loop over passes)
1427 */
1428 for(p = PLI_PASS_STD_ANY; p < NPLI_PASSES; p++) { /* p will go from 1..6 */
1429 if(p == PLI_PASS_STD_ANY && (! do_pass_std_any)) continue;
1430 if(p == PLI_PASS_5P_ONLY_FORCE && (! do_pass_5p_only_force)) continue;
1431 if(p == PLI_PASS_3P_ONLY_FORCE && (! do_pass_3p_only_force)) continue;
1432 if(p == PLI_PASS_5P_AND_3P_FORCE && (! do_pass_5p_and_3p_force)) continue;
1433 if(p == PLI_PASS_5P_AND_3P_ANY && (! do_pass_5p_and_3p_any)) continue;
1434 if(p == PLI_PASS_HMM_ONLY_ANY && (! do_pass_hmm_only_any)) continue;
1435
1436 /* A. Update pipeline accounting numbers
1437 * Update npli_{top,bot} run and nres_{top,bot} searched for this
1438 * pass. It's important to do this precisely here, in between the
1439 * 'continue's above, but prior to the one below that 'continue's
1440 * only because we know no windows pass local Fwd (F3).
1441 */
1442 if(in_rc) pli->acct[p].npli_bot++;
1443 else pli->acct[p].npli_top++;
1444 if(p == PLI_PASS_STD_ANY || p == PLI_PASS_5P_AND_3P_ANY || p == PLI_PASS_HMM_ONLY_ANY) {
1445 if(in_rc) pli->acct[p].nres_bot += sq->n;
1446 else pli->acct[p].nres_top += sq->n;
1447 }
1448 else {
1449 if(in_rc) pli->acct[p].nres_bot += ESL_MIN(pli->maxW, sq->n);
1450 else pli->acct[p].nres_top += ESL_MIN(pli->maxW, sq->n);
1451 }
1452
1453 /* if we know there's no windows that pass local Fwd (F3), our terminal (or full) seqs won't have any either, continue */
1454 if(p != PLI_PASS_STD_ANY && p != PLI_PASS_HMM_ONLY_ANY && nwin_pass_std_any == 0 && (! pli->do_max)) continue;
1455
1456 /* set sq2search and remember start_offset */
1457 start_offset = 0;
1458 if(p == PLI_PASS_STD_ANY || p == PLI_PASS_5P_AND_3P_FORCE || p == PLI_PASS_5P_AND_3P_ANY || p == PLI_PASS_HMM_ONLY_ANY || sq->n <= pli->maxW) {
1459 sq2search = sq;
1460 }
1461 else if(p == PLI_PASS_5P_ONLY_FORCE) { /* research first (5') pli->maxW residues */
1462 term5sq = esl_sq_CreateDigital(bg->abc);
1463 pli_copy_subseq(sq, term5sq, 1, pli->maxW);
1464 sq2search = term5sq;
1465 }
1466 else if(p == PLI_PASS_3P_ONLY_FORCE) { /* research first (5') pli->maxW residues */
1467 term3sq = esl_sq_CreateDigital(bg->abc);
1468 pli_copy_subseq(sq, term3sq, sq->n - pli->maxW + 1, pli->maxW);
1469 sq2search = term3sq;
1470 start_offset = sq->n - pli->maxW;
1471 }
1472 pli->cur_pass_idx = p; /* pipeline stages will use this to modify pass-specific behavior as necessary */
1473
1474 /* Execute the pipeline, either HMM only mode (relatively simple), or normal CM mode (more complex) */
1475 if(p == PLI_PASS_HMM_ONLY_ANY) {
1476 /********************************************************************************************************/
1477 /* Execute the HMM-only filter pipeline:
1478 * A. pli_p7_filter(): MSV, Viterbi, local Forward filters
1479 * B. pli_final_stage_hmmonly(): use H3 local domain definition to define HMM hits
1480 ********************************************************************************************************/
1481 #if eslDEBUGLEVEL >= 2
1482 printf("#DEBUG:\n#DEBUG: HMM ONLY PIPELINE calling p7_filter() %s %" PRId64 " residues (pass: %d)\n", sq2search->name, sq2search->n, p);
1483 #endif
1484 if((status = pli_p7_filter(pli, om, bg, p7_evparam, msvdata, sq2search, &ws, &we, &wb, &nwin)) != eslOK) return status;
1485 if(pli->do_time_F1 || pli->do_time_F2 || pli->do_time_F3) return status;
1486 prv_ntophits = hitlist->N;
1487
1488 if(pli->do_trm_F3) { /* terminate after F3, and convert surviving windows to hits */
1489 for(h = 0; h < nwin; h++) {
1490 /* create a hit from each window to be output at end of run, we do this (as opposed to
1491 * just outputting info on windows *here*) so that we can use our machinery for removing
1492 * overlaps later before we output.
1493 */
1494 cm_tophits_CreateNextHit(hitlist, &hit);
1495 hit->start = ws[h];
1496 hit->stop = we[h];
1497 hit->root = -1; /* irrelevant in HMM only hit */
1498 hit->mode = TRMODE_J; /* irrelevant */
1499 hit->score = wb[h];
1500
1501 hit->cm_idx = pli->cur_cm_idx;
1502 hit->clan_idx = pli->cur_clan_idx;
1503 hit->seq_idx = pli->cur_seq_idx;
1504 hit->pass_idx = pli->cur_pass_idx;
1505 hit->pvalue = 0.; /* irrelevant */
1506 hit->srcL = sq->L; /* this may be -1, in which case it will be updated by caller (cmsearch or cmscan) when full length is known */
1507
1508 hit->hmmonly = TRUE;
1509 hit->glocal = FALSE; /* all HMM hits are local */
1510 hit->bias = 0.; /* irrelevant */
1511 hit->evalue = 0.; /* irrelevant */
1512 hit->has_evalue = FALSE;
1513 hit->ad = NULL;
1514
1515 if (pli->mode == CM_SEARCH_SEQS) {
1516 if ( (status = esl_strdup(sq->name, -1, &(hit->name))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
1517 if (sq->acc[0] != '\0' && (status = esl_strdup(sq->acc, -1, &(hit->acc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
1518 /* special for do_trm_F3: description gets overwritten as query name */
1519 if ((status = esl_strdup(om->name, -1, &(hit->desc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
1520 }
1521 else { /* SCAN mode */
1522 if ((status = esl_strdup(om->name, -1, &(hit->name))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
1523 if ((status = esl_strdup(om->acc, -1, &(hit->acc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
1524 /* special for do_trm_F3: description gets overwritten as query name */
1525 if ((status = esl_strdup(sq->name, -1, &(hit->desc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
1526 }
1527 }
1528 } /* end of 'if(pli->do_trm_F3)' */
1529 else {
1530 if((status = pli_final_stage_hmmonly(pli, cm_offset, om, bg, p7_evparam, sq2search, ws, we, nwin, hitlist, opt_cm)) != eslOK) return status;
1531 }
1532 }
1533 else { /* normal case, p != PLI_PASS_HMM_ONLY_ANY */
1534 /* Use HMM to define envelopes, if nec.
1535 * If we skip this step (if pli->do_edef == FALSE)
1536 * we will do it with the CM in the second loop over passes below.
1537 */
1538 if(pli->do_edef) {
1539 /* Defining envelopes with p7 HMM:
1540 * A. pli_p7_filter(): SSV, Viterbi, local Forward filters
1541 * B. pli_p7_env_def(): glocal Forward, and glocal (usually) HMM envelope definition, then
1542 */
1543 #if eslDEBUGLEVEL >= 2
1544 printf("#DEBUG:\n#DEBUG: PIPELINE calling p7_filter() %s %" PRId64 " residues (pass: %d)\n", sq2search->name, sq2search->n, p);
1545 #endif
1546 if((status = pli_p7_filter(pli, om, bg, p7_evparam, msvdata, sq2search, &ws, &we, &wb, &nwin)) != eslOK) return status;
1547 if(p == PLI_PASS_STD_ANY) nwin_pass_std_any = nwin;
1548 if(pli->do_time_F1 || pli->do_time_F2 || pli->do_time_F3) return status;
1549
1550 #if eslDEBUGLEVEL >= 2
1551 printf("#DEBUG:\n#DEBUG: PIPELINE calling p7_env_def() %s %" PRId64 " residues (pass: %d)\n", sq2search->name, sq2search->n, p);
1552 #endif
1553 if((status = pli_p7_env_def(pli, om, bg, p7_evparam, sq2search, ws, we, nwin, opt_hmm, opt_gm, opt_Rgm, opt_Lgm, opt_Tgm, &(p7esAA[p]), &(p7eeAA[p]), &(p7ebAA[p]), &(np7envA[p]))) != eslOK) return status;
1554 } /* end of if(pli->do_edef) */
1555 } /* end of 'else' entered if p != PLI_PASS_HMM_ONLY_ANY */
1556 if(ws != NULL) { free(ws); ws = NULL; }
1557 if(we != NULL) { free(we); we = NULL; }
1558 if(wb != NULL) { free(wb); wb = NULL; }
1559 nwin = 0;
1560 } /* end of 'for(p = PLI_PASS_STD_ANY; p <= PLI_NPASSES; p++)', first loop over pipeline passes */
1561
1562 /* Two special cases: pli->do_one_cmpass and pli->do_one_cmpass_olap */
1563 if(pli->do_one_cmpass || pli->do_one_cmpass_olap) {
1564 /* pli->do_one_cmpass:
1565 * if following criteria are met, only perform CM stages below with best scoring pass:
1566 * C1. all passes have 0 or 1 envelopes
1567 * C2. all envelopes encompass the full sequence
1568 *
1569 * pli->do_one_cmpass_olap:
1570 * if following criteria are met, only perform CM stages below with best scoring pass:
1571 * C1. all passes have 0 or 1 envelopes
1572 * C3. all passes overlap by 50% or more with the
1573 * envelope from the pass that has the highest score
1574 */
1575 /* check for C1 first and find the best scoring envelope,
1576 * we need to do this for either do_one_cmpass || do_one_cmpass_olap */
1577 best_pass = -1;
1578 if(pli_check_one_or_zero_envelopes(np7envA)) {
1579 best_pass = pli_get_pass_of_best_envelope(p7ebAA, np7envA);
1580 }
1581 if(best_pass != -1) {
1582 /* C1 criteria met, go on */
1583 if(pli->do_one_cmpass) {
1584 /* check for C2. all envelopes encompass the full sequence */
1585 if(! pli_check_full_length_envelopes(p7esAA, p7eeAA, np7envA, sq->L)) {
1586 /* C2 violated, break; */
1587 best_pass = -1;
1588 }
1589 }
1590 else if(pli->do_one_cmpass_olap) {
1591 /* C3. all passes overlap by 50% or more with the
1592 * envelope from the pass that has the highest score
1593 */
1594 if((status = pli_check_overlap_envelopes(p7esAA, p7eeAA, np7envA, best_pass, 0, /*best_env_idx, we know it's 0 b/c only 1 hit)*/
1595 ESL_MAX(0, sq->n - pli->maxW), /* start_offset to subtract if p == PLI_PASS_3P_ONLY_FORCE*/
1596 0.5, /*min_fract for overlap*/
1597 &pass_olap, pli->errbuf)) != eslOK) return status;
1598 if(! pass_olap) {
1599 /* C3 violated, break; */
1600 best_pass = -1;
1601 }
1602 }
1603 }
1604 /* when we get here, best_pass will be -1 if all criteria for
1605 * pli->do_one_cmpass or pli->do_one_cmpass_olap were not met
1606 */
1607 }
1608
1609 /* Second loop over each pipeline pass:
1610 * Complete all CM-based calculations.
1611 */
1612 for(p = PLI_PASS_STD_ANY; p < NPLI_PASSES; p++) { /* p will go from 1..6 */
1613 if(pli->do_trm_F3) continue;
1614 if(best_pass != -1 && p != best_pass) continue;
1615 if(p == PLI_PASS_STD_ANY && (! do_pass_std_any)) continue;
1616 if(p == PLI_PASS_5P_ONLY_FORCE && (! do_pass_5p_only_force)) continue;
1617 if(p == PLI_PASS_3P_ONLY_FORCE && (! do_pass_3p_only_force)) continue;
1618 if(p == PLI_PASS_5P_AND_3P_FORCE && (! do_pass_5p_and_3p_force)) continue;
1619 if(p == PLI_PASS_5P_AND_3P_ANY && (! do_pass_5p_and_3p_any)) continue;
1620 if(p == PLI_PASS_HMM_ONLY_ANY) continue; /* we already handled this pass completely in the first loop over passes above */
1621
1622 /* if we know there's no windows that pass local Fwd (F3), our terminal (or full) seqs won't have any either, continue */
1623 if(p != PLI_PASS_STD_ANY && p != PLI_PASS_HMM_ONLY_ANY && nwin_pass_std_any == 0 && (! pli->do_max)) continue;
1624
1625 /* set sq2search and remember start_offset as we did above in first loop over passes, but we don't have to create term5sq and term3sq again */
1626 start_offset = 0;
1627 if(p == PLI_PASS_STD_ANY || p == PLI_PASS_5P_AND_3P_FORCE || p == PLI_PASS_5P_AND_3P_ANY || p == PLI_PASS_HMM_ONLY_ANY || sq->n <= pli->maxW) {
1628 sq2search = sq;
1629 }
1630 else if(p == PLI_PASS_5P_ONLY_FORCE) { /* research first (5') pli->maxW residues */
1631 sq2search = term5sq;
1632 }
1633 else if(p == PLI_PASS_3P_ONLY_FORCE) { /* research final (3') pli->maxW residues */
1634 sq2search = term3sq;
1635 start_offset = sq->n - pli->maxW;
1636 }
1637 pli->cur_pass_idx = p; /* pipeline stages will use this to modify pass-specific behavior as necessary */
1638
1639 /* 3 ways to determine the envelopes to pass to the Final CM stage:
1640 * 1. Banded CYK on the envelopes defined by the p7 HMM in the pipeline pass above (if pli->do_edef == TRUE)
1641 * 2. Using CYK to define envelopes on full sequences (if pli->do_edef == FALSE && pli->fcyk = TRUE)
1642 * 3. Each full seq is an envelope to be examined by the Final stage (no filters, if pli->do_edef == FALSE && pli->fcyk = FALSE)
1643 */
1644
1645 /* 1. Banded CYK on the envelopes defined by the p7 HMM in the pipeline pass above (if pli->do_edef == TRUE) */
1646 if(pli->do_edef) {
1647 if(pli->do_fcyk) {
1648 #if eslDEBUGLEVEL >= 2
1649 printf("#DEBUG:\n#DEBUG: PIPELINE calling pli_cyk_env_filter() %s %" PRId64 " residues (pass: %d)\n", sq2search->name, sq2search->n, p);
1650 #endif
1651 if((status = pli_cyk_env_filter(pli, cm_offset, sq2search, p7esAA[p], p7eeAA[p], np7envA[p], opt_cm, &es, &ee, &nenv)) != eslOK) return status;
1652 if(pli->do_time_F4 || pli->do_time_F5) return status;
1653 }
1654 else { /* defined envelopes with HMM, but CYK filter is off: act as if all p7-defined envelopes survived CYK */
1655 ESL_ALLOC(es, sizeof(int64_t) * ESL_MAX(1, np7envA[p])); // avoid 0 malloc
1656 ESL_ALLOC(ee, sizeof(int64_t) * ESL_MAX(1, np7envA[p]));
1657 for(i = 0; i < np7envA[p]; i++) { es[i] = p7esAA[p][i]; ee[i] = p7eeAA[p][i]; }
1658 nenv = np7envA[p];
1659 }
1660 }
1661 /* 2. Using CYK to define envelopes on full sequences (if pli->do_edef == FALSE && pli->fcyk = TRUE) */
1662 else if((! pli->do_edef) && pli->do_fcyk) {
1663 /* Defining envelopes with CYK */
1664 #if eslDEBUGLEVEL >= 2
1665 printf("#DEBUG:\n#DEBUG: PIPELINE calling pli_cyk_seq_filter() %s %" PRId64 " residues (pass: %d)\n", sq2search->name, sq2search->n, p);
1666 #endif
1667 if((status = pli_cyk_seq_filter(pli, cm_offset, sq2search, opt_cm, &es, &ee, &nenv)) != eslOK) return status;
1668 }
1669 /* 3. Each full seq is an envelope to be examined by the Final stage (no filters, if pli->do_edef == FALSE && pli->fcyk = FALSE) */
1670 else {
1671 ESL_ALLOC(es, sizeof(int64_t) * 1);
1672 ESL_ALLOC(ee, sizeof(int64_t) * 1);
1673 nenv = 1; es[0] = 1; ee[0] = sq2search->n;
1674 }
1675 if(pli->do_time_F6) return status;
1676
1677 /* Filters are finished. Final stage of pipeline (always run).
1678 */
1679 #if eslDEBUGLEVEL >= 2
1680 printf("#DEBUG:\n#DEBUG: PIPELINE calling FinalStage() %s %" PRId64 " residues model: %s (pass: %d) nhits: %" PRId64 "\n", sq2search->name, sq2search->n, om->name, p, hitlist->N);
1681 #endif
1682 prv_ntophits = hitlist->N;
1683 if((status = pli_final_stage(pli, cm_offset, sq2search, es, ee, nenv, hitlist, opt_cm)) != eslOK) return status;
1684 #if eslDEBUGLEVEL >= 2
1685 printf("#DEBUG\n#DEBUG: PIPELINE back from FinalStage() %s %" PRId64 " residues model: %s (pass: %d) nhits: %" PRId64 "\n", sq2search->name, sq2search->n, om->name, p, hitlist->N);
1686 #endif
1687
1688 /* if we're researching a 3' terminus, adjust the start/stop
1689 * positions so they are relative to the actual 5' start
1690 */
1691 if(hitlist->N > prv_ntophits) {
1692 if(start_offset != 0) { /* this will only be non-zero if we're in pass PLI_PASS_3P_ONLY_FORCE */
1693 for(h = prv_ntophits; h < hitlist->N; h++) {
1694 hitlist->unsrt[h].start += start_offset;
1695 hitlist->unsrt[h].stop += start_offset;
1696 if(hitlist->unsrt[h].ad != NULL) {
1697 hitlist->unsrt[h].ad->sqfrom += start_offset;
1698 hitlist->unsrt[h].ad->sqto += start_offset;
1699 }
1700 }
1701 }
1702 }
1703 if(es != NULL) { free(es); es = NULL; }
1704 if(ee != NULL) { free(ee); ee = NULL; }
1705 nenv = 0;
1706 } /* end of 'for(p = PLI_PASS_STD_ANY; p <= PLI_NPASSES; p++)', second loop over pipeline passes */
1707
1708 if(np7envA != NULL) free(np7envA);
1709 for(p = 0; p < NPLI_PASSES; p++) {
1710 if(p7esAA && p7esAA[p]) free(p7esAA[p]);
1711 if(p7eeAA && p7eeAA[p]) free(p7eeAA[p]);
1712 if(p7ebAA && p7ebAA[p]) free(p7ebAA[p]);
1713 }
1714 if(p7esAA) free(p7esAA);
1715 if(p7eeAA) free(p7eeAA);
1716 if(p7ebAA) free(p7ebAA);
1717
1718 if(term5sq != NULL) esl_sq_Destroy(term5sq);
1719 if(term3sq != NULL) esl_sq_Destroy(term3sq);
1720
1721 return eslOK;
1722
1723 ERROR:
1724 ESL_FAIL(eslEMEM, pli->errbuf, "out of memory");
1725 }
1726
1727 /* Function: cm_pli_Statistics()
1728 * Synopsis: Final stats output for all passes of a pipeline.
1729 * Incept: EPN, Thu Feb 16 15:19:35 2012
1730 *
1731 * Purpose: Print a standardized report of the internal statistics of
1732 * a finished processing pipeline <pli> to stream <ofp>.
1733 * If pli->be_verbose, print statistics for each pass,
1734 * else only print statistics for the standard pass.
1735 *
1736 * Actual work is done by repeated calls to
1737 * pli_pass_statistics().
1738 *
1739 * Returns: <eslOK> on success.
1740 */
1741 int
1742 cm_pli_Statistics(FILE *ofp, CM_PIPELINE *pli, ESL_STOPWATCH *w)
1743 {
1744 if(pli->nmodels > 0 && pli->be_verbose) { /* print stats out for each stage */
1745 if(! pli->do_trunc_only) {
1746 pli_pass_statistics(ofp, pli, PLI_PASS_STD_ANY); fprintf(ofp, "\n");
1747 }
1748
1749 /* now an if/else for additional modes */
1750 if(pli->do_trunc_ends) {
1751 pli_pass_statistics(ofp, pli, PLI_PASS_5P_ONLY_FORCE); fprintf(ofp, "\n");
1752 pli_pass_statistics(ofp, pli, PLI_PASS_3P_ONLY_FORCE); fprintf(ofp, "\n");
1753 pli_pass_statistics(ofp, pli, PLI_PASS_5P_AND_3P_FORCE); fprintf(ofp, "\n");
1754 }
1755 else if(pli->do_trunc_any) {
1756 pli_pass_statistics(ofp, pli, PLI_PASS_5P_ONLY_FORCE); fprintf(ofp, "\n");
1757 pli_pass_statistics(ofp, pli, PLI_PASS_3P_ONLY_FORCE); fprintf(ofp, "\n");
1758 pli_pass_statistics(ofp, pli, PLI_PASS_5P_AND_3P_FORCE); fprintf(ofp, "\n");
1759 pli_pass_statistics(ofp, pli, PLI_PASS_5P_AND_3P_ANY); fprintf(ofp, "\n");
1760 }
1761 else if(pli->do_trunc_5p_ends) {
1762 pli_pass_statistics(ofp, pli, PLI_PASS_5P_ONLY_FORCE); fprintf(ofp, "\n");
1763 }
1764 else if(pli->do_trunc_3p_ends) {
1765 pli_pass_statistics(ofp, pli, PLI_PASS_3P_ONLY_FORCE); fprintf(ofp, "\n");
1766 }
1767 else if(pli->do_trunc_int || pli->do_trunc_only) {
1768 pli_pass_statistics(ofp, pli, PLI_PASS_5P_AND_3P_ANY); fprintf(ofp, "\n");
1769 }
1770 }
1771 if(pli->nmodels_hmmonly > 0) { /* HMM only pipeline used for at least one model */
1772 pli_hmmonly_pass_statistics(ofp, pli); fprintf(ofp, "\n");
1773 }
1774 if(pli->nmodels > 0) { /* CM pipeline was used for at least one model */
1775 pli_sum_statistics(pli);
1776 pli_pass_statistics(ofp, pli, PLI_PASS_CM_SUMMED); fprintf(ofp, "\n");
1777 }
1778 if(pli->nmodels > 0 && pli->nmodels_hmmonly > 0) {
1779 fprintf(ofp, "Total CM and HMM hits reported: %15d\n\n",
1780 (int) (pli->acct[PLI_PASS_CM_SUMMED].n_output) +
1781 (int) (pli->acct[PLI_PASS_HMM_ONLY_ANY].n_output));
1782 }
1783 if(w != NULL) esl_stopwatch_Display(ofp, w, "# CPU time: ");
1784
1785 return eslOK;
1786 }
1787
1788 /* Function: pli_pass_statistics()
1789 * Synopsis: Final stats output for one pass of a pipeline.
1790 * Incept: EPN, Fri Sep 24 16:48:06 2010
1791 * SRE, Tue Dec 9 10:19:45 2008 [Janelia] (p7_pli_Statistics())
1792 *
1793 * Purpose: Print a standardized report of the internal statistics of
1794 * a finished processing pipeline <pli> to stream <ofp>.
1795 *
1796 * Returns: <eslOK> on success.
1797 */
1798 int
1799 pli_pass_statistics(FILE *ofp, CM_PIPELINE *pli, int pass_idx)
1800 {
1801 int64_t nwin_fcyk = 0; /* number of windows CYK filter evaluated */
1802 int64_t nwin_final = 0; /* number of windows final stage evaluated */
1803 int64_t n_output_trunc; /* number of truncated hits */
1804 int64_t pos_output_trunc; /* number of residues in truncated hits */
1805 int64_t nres_searched = 0; /* number of residues searched in this stage */
1806 int64_t nres_researched = 0; /* number of residues re-searched for truncated hits */
1807
1808 CM_PLI_ACCT *pli_acct = &(pli->acct[pass_idx]);
1809
1810 /* first, determine number of residues searched, and num res re-searched for truncated hits */
1811 nres_searched = pli_acct->nres_top + pli_acct->nres_bot;
1812 switch(pass_idx) {
1813 case PLI_PASS_CM_SUMMED:
1814 nres_researched = (pli_acct->nres_top + pli->acct->nres_bot) - (pli->acct[PLI_PASS_STD_ANY].nres_top + pli->acct[PLI_PASS_STD_ANY].nres_bot);
1815 break;
1816 case PLI_PASS_STD_ANY:
1817 nres_researched = 0;
1818 break;
1819 case PLI_PASS_5P_ONLY_FORCE:
1820 case PLI_PASS_3P_ONLY_FORCE:
1821 case PLI_PASS_5P_AND_3P_FORCE:
1822 case PLI_PASS_5P_AND_3P_ANY:
1823 nres_researched = pli_acct->nres_top + pli_acct->nres_bot;
1824 break;
1825 default:
1826 nres_researched = 0;
1827 break;
1828 }
1829
1830 if(pli->be_verbose) {
1831 fprintf(ofp, "Internal CM pipeline statistics summary: %s\n", pli_describe_pass(pass_idx));
1832 }
1833 else {
1834 fprintf(ofp, "Internal CM pipeline statistics summary:\n");
1835 }
1836 fprintf(ofp, "----------------------------------------\n");
1837 if (pli->mode == CM_SEARCH_SEQS) {
1838 fprintf(ofp, "Query model(s): %15" PRId64 " (%" PRId64 " consensus positions)\n",
1839 pli->nmodels, pli->nnodes);
1840 if(pass_idx == PLI_PASS_STD_ANY || pass_idx == PLI_PASS_CM_SUMMED) {
1841 fprintf(ofp, "Target sequences: %15" PRId64 " (%" PRId64 " residues searched)\n",
1842 pli->nseqs, nres_searched - nres_researched);
1843 }
1844 if(pass_idx != PLI_PASS_STD_ANY) {
1845 if(pass_idx == PLI_PASS_5P_AND_3P_FORCE) {
1846 /* special case, not all sequences get searched by this pass,
1847 * since only seqs < maxW are searched in this pass (so no
1848 * sequence is chopped up into overlapping windows) npli
1849 * is equal to number of sequences searched (npli_top or
1850 * npli_bot).
1851 */
1852 fprintf(ofp, "Target sequences re-searched for truncated hits: %15" PRId64 " (%" PRId64 " residues re-searched)\n",
1853 (pli->do_trunc_ends) ? ESL_MAX(pli_acct->npli_top, pli_acct->npli_bot) : 0,
1854 nres_researched);
1855 }
1856 else {
1857 if(pli->do_trunc_only) {
1858 fprintf(ofp, "Target sequences searched for truncated hits: %15" PRId64 " (%" PRId64 " residues searched)\n",
1859 pli->nseqs, nres_researched);
1860 }
1861 else {
1862 fprintf(ofp, "Target sequences re-searched for truncated hits: %15" PRId64 " (%" PRId64 " residues re-searched)\n",
1863 (pli->do_trunc_ends || pli->do_trunc_any || pli->do_trunc_int || pli->do_trunc_5p_ends || pli->do_trunc_3p_ends) ? pli->nseqs : 0,
1864 nres_researched);
1865 }
1866 }
1867 }
1868 } else { /* SCAN mode */
1869 if(pass_idx == PLI_PASS_STD_ANY || pass_idx == PLI_PASS_CM_SUMMED) {
1870 fprintf(ofp, "Query sequence(s): %15" PRId64 " (%d residues searched)\n",
1871 pli->nseqs, (int) ((nres_searched - nres_researched) / pli->nmodels));
1872 }
1873 if(pass_idx != PLI_PASS_STD_ANY) {
1874 if(pass_idx == PLI_PASS_5P_AND_3P_FORCE) {
1875 /* special case, not all sequences get searched by this pass,
1876 * since only seqs < maxW are searched in this pass, if
1877 * npli_top or npli_bot is greater than 0, then we searched
1878 * pli->nseqs (which is always 1) with at least 1 model
1879 * (we could also use pli->nres_researched > 0 as an
1880 * indication of whether we searched with any models or not).
1881 */
1882 fprintf(ofp, "Query sequences re-searched for truncated hits: %15" PRId64 " (%.1f residues re-searched, avg per model)\n",
1883 (pli_acct->npli_top > 0 || pli_acct->npli_bot > 0) ? pli->nseqs : 0,
1884 (pli_acct->npli_top > 0 || pli_acct->npli_bot > 0) ? (float) nres_researched / (float) (ESL_MAX(pli_acct->npli_top, pli_acct->npli_bot)) : 0.);
1885 }
1886 else {
1887 if(pli->do_trunc_only) {
1888 fprintf(ofp, "Query sequences searched for truncated hits: %15" PRId64 " (%.1f residues searched, avg per model)\n",
1889 pli->nseqs, (float) nres_researched / (float) pli->nmodels);
1890 }
1891 else {
1892 fprintf(ofp, "Query sequences re-searched for truncated hits: %15" PRId64 " (%.1f residues re-searched, avg per model)\n",
1893 (pli->do_trunc_ends || pli->do_trunc_any || pli->do_trunc_int || pli->do_trunc_5p_ends || pli->do_trunc_3p_ends) ? pli->nseqs : 0,
1894 (float) nres_researched / (float) pli->nmodels);
1895 }
1896 }
1897 }
1898 if(pass_idx == PLI_PASS_5P_AND_3P_FORCE) {
1899 /* special case, only print number of models with which
1900 * we actually searched. It won't necessarily be all models
1901 * because only models for which the sequence is < maxW will
1902 * be used. And don't print number of nodes - we don't
1903 * keep track of that for this stage.
1904 */
1905 fprintf(ofp, "Target model(s): %15" PRId64 "\n",
1906 ESL_MAX(pli_acct->npli_top, pli_acct->npli_bot));
1907 }
1908 else {
1909 fprintf(ofp, "Target model(s): %15" PRId64 " (%" PRId64 " consensus positions)\n",
1910 pli->nmodels, pli->nnodes);
1911 }
1912 }
1913
1914 if(pli->do_msv) {
1915 fprintf(ofp, "Windows passing local HMM SSV filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1916 pli_acct->n_past_msv,
1917 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_msv / nres_searched,
1918 pli->F1);
1919 nwin_fcyk = nwin_final = pli_acct->n_past_msv;
1920 }
1921 else {
1922 fprintf(ofp, "Windows passing local HMM SSV filter: %15s (off)\n", "");
1923 }
1924
1925 if(pli->do_msvbias) {
1926 fprintf(ofp, "Windows passing local HMM MSV bias filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1927 pli_acct->n_past_msvbias,
1928 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_msvbias / nres_searched,
1929 pli->F1b);
1930 nwin_fcyk = nwin_final = pli_acct->n_past_msvbias;
1931 }
1932 /* msv bias is off by default, so don't output anything if it's off */
1933
1934 if(pli->do_vit) {
1935 fprintf(ofp, "Windows passing local HMM Viterbi filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1936 pli_acct->n_past_vit,
1937 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_vit / nres_searched,
1938 pli->F2);
1939 nwin_fcyk = nwin_final = pli_acct->n_past_vit;
1940 }
1941 else {
1942 fprintf(ofp, "Windows passing local HMM Viterbi filter: %15s (off)\n", "");
1943 }
1944
1945 if(pli->do_vitbias) {
1946 fprintf(ofp, "Windows passing local HMM Viterbi bias filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1947 pli_acct->n_past_vitbias,
1948 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_vitbias / nres_searched,
1949 pli->F2b);
1950 nwin_fcyk = nwin_final = pli_acct->n_past_vitbias;
1951 }
1952 else {
1953 fprintf(ofp, "Windows passing local HMM Viterbi bias filter: %15s (off)\n", "");
1954 }
1955
1956 if(pli->do_fwd) {
1957 fprintf(ofp, "Windows passing local HMM Forward filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1958 pli_acct->n_past_fwd,
1959 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_fwd / nres_searched,
1960 pli->F3);
1961 nwin_fcyk = nwin_final = pli_acct->n_past_fwd;
1962 }
1963 else {
1964 fprintf(ofp, "Windows passing local HMM Forward filter: %15s (off)\n", "");
1965 }
1966
1967 if(pli->do_fwdbias) {
1968 fprintf(ofp, "Windows passing local HMM Forward bias filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1969 pli_acct->n_past_fwdbias,
1970 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_fwdbias / nres_searched,
1971 pli->F3b);
1972 nwin_fcyk = nwin_final = pli_acct->n_past_fwdbias;
1973 }
1974 else {
1975 fprintf(ofp, "Windows passing local HMM Forward bias filter: %15s (off)\n", "");
1976 }
1977
1978 if(pli->do_gfwd) {
1979 fprintf(ofp, "Windows passing glocal HMM Forward filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1980 pli_acct->n_past_gfwd,
1981 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_gfwd / nres_searched,
1982 pli->F4);
1983 nwin_fcyk = nwin_final = pli_acct->n_past_gfwd;
1984 }
1985 else {
1986 fprintf(ofp, "Windows passing glocal HMM Forward filter: %15s (off)\n", "");
1987 }
1988 if(pli->do_gfwdbias) {
1989 fprintf(ofp, "Windows passing glocal HMM Forward bias filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
1990 pli_acct->n_past_gfwdbias,
1991 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_gfwdbias / nres_searched,
1992 pli->F4b);
1993 nwin_fcyk = nwin_final = pli_acct->n_past_gfwdbias;
1994 }
1995 else {
1996 fprintf(ofp, "Windows passing glocal HMM Forward bias filter: %15s (off)\n", "");
1997 }
1998
1999 if(pli->do_edef) {
2000 fprintf(ofp, "Envelopes passing glocal HMM envelope defn filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2001 pli_acct->n_past_edef,
2002 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_edef / nres_searched,
2003 pli->F5);
2004 nwin_fcyk = nwin_final = pli_acct->n_past_edef;
2005 }
2006 else {
2007 fprintf(ofp, "Envelopes passing glocal HMM envelope defn filter: %15s (off)\n", "");
2008 }
2009
2010 if(pli->do_edefbias) {
2011 fprintf(ofp, "Envelopes passing glocal HMM envelope bias filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2012 pli_acct->n_past_edefbias,
2013 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_edefbias / nres_searched,
2014 pli->F5b);
2015 nwin_fcyk = nwin_final = pli_acct->n_past_edefbias;
2016 }
2017 /* edef bias is off by default, so don't output anything if it's off */
2018
2019 if(pli->do_fcyk) {
2020 fprintf(ofp, "Envelopes passing %6s CM CYK filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2021 (pli->do_glocal_cm_always || pli->do_glocal_cm_sometimes) ? ((pli->do_glocal_cm_always) ? "glocal" : "") : "local",
2022 pli_acct->n_past_cyk,
2023 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_cyk / nres_searched,
2024 pli->F6);
2025 nwin_final = pli_acct->n_past_cyk;
2026 }
2027 else {
2028 fprintf(ofp, "Envelopes passing %6s CM CYK filter: %15s (off)\n",
2029 (pli->do_glocal_cm_always || pli->do_glocal_cm_sometimes) ? ((pli->do_glocal_cm_always) ? "glocal" : "") : "local",
2030 "");
2031 }
2032
2033 if(pass_idx == PLI_PASS_CM_SUMMED) {
2034 if(pli->do_trunc_ends) {
2035 n_output_trunc = pli->acct[PLI_PASS_5P_ONLY_FORCE].n_output + pli->acct[PLI_PASS_3P_ONLY_FORCE].n_output + pli->acct[PLI_PASS_5P_AND_3P_FORCE].n_output;
2036 pos_output_trunc = pli->acct[PLI_PASS_5P_ONLY_FORCE].pos_output + pli->acct[PLI_PASS_3P_ONLY_FORCE].pos_output + pli->acct[PLI_PASS_5P_AND_3P_FORCE].pos_output;
2037 }
2038 else if(pli->do_trunc_any) {
2039 n_output_trunc = pli->acct[PLI_PASS_5P_ONLY_FORCE].n_output + pli->acct[PLI_PASS_3P_ONLY_FORCE].n_output + pli->acct[PLI_PASS_5P_AND_3P_FORCE].n_output + pli->acct[PLI_PASS_5P_AND_3P_ANY].n_output;
2040 pos_output_trunc = pli->acct[PLI_PASS_5P_ONLY_FORCE].pos_output + pli->acct[PLI_PASS_3P_ONLY_FORCE].pos_output + pli->acct[PLI_PASS_5P_AND_3P_FORCE].pos_output + pli->acct[PLI_PASS_5P_AND_3P_ANY].pos_output;
2041 }
2042 else if(pli->do_trunc_5p_ends) {
2043 n_output_trunc = pli->acct[PLI_PASS_5P_ONLY_FORCE].n_output;
2044 pos_output_trunc = pli->acct[PLI_PASS_5P_ONLY_FORCE].pos_output;
2045 }
2046 else if(pli->do_trunc_3p_ends) {
2047 n_output_trunc = pli->acct[PLI_PASS_3P_ONLY_FORCE].n_output;
2048 pos_output_trunc = pli->acct[PLI_PASS_3P_ONLY_FORCE].pos_output;
2049 }
2050 else if(pli->do_trunc_int || pli->do_trunc_only) {
2051 n_output_trunc = pli->acct[PLI_PASS_5P_AND_3P_ANY].n_output;
2052 pos_output_trunc = pli->acct[PLI_PASS_5P_AND_3P_ANY].pos_output;
2053 }
2054 else { /* no truncated hits were allowed */
2055 n_output_trunc = 0;
2056 pos_output_trunc = 0;
2057 }
2058 fprintf(ofp, "Total CM hits reported: %15d (%.4g); includes %d truncated hit(s)\n",
2059 (int) pli_acct->n_output,
2060 (nres_searched == 0) ? 0.0 : (double) (pli_acct->pos_output + pos_output_trunc) / nres_searched,
2061 (int) n_output_trunc);
2062 }
2063 else {
2064 fprintf(ofp, "Total %37s %15d (%.4g)\n",
2065 (pli->be_verbose) ? pli_describe_hits_for_pass(pass_idx) : "",
2066 (int) pli_acct->n_output,
2067 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_output / nres_searched);
2068 }
2069
2070
2071 if(pli->be_verbose) {
2072 fprintf(ofp, "\n");
2073 if(nwin_fcyk > 0) {
2074 fprintf(ofp, "%-6s filter stage scan matrix overflows: %15" PRId64 " (%.4g)\n",
2075 "CYK",
2076 pli_acct->n_overflow_fcyk,
2077 (double) pli_acct->n_overflow_fcyk / (double) nwin_fcyk);
2078 }
2079 else {
2080 fprintf(ofp, "%-6s filter stage scan matrix overflows: %15d (%.4g)\n",
2081 "CYK",
2082 0, 0.);
2083 }
2084 if(nwin_final > 0) {
2085 fprintf(ofp, "%-6s final stage scan matrix overflows: %15" PRId64 " (%.4g)\n",
2086 (pli->final_cm_search_opts & CM_SEARCH_INSIDE) ? "Inside" : "CYK",
2087 pli_acct->n_overflow_final,
2088 (double) pli_acct->n_overflow_final / (double) nwin_final);
2089 }
2090 else {
2091 fprintf(ofp, "%-6s final stage scan matrix overflows: %15d (%.4g)\n",
2092 (pli->final_cm_search_opts & CM_SEARCH_INSIDE) ? "Inside" : "CYK",
2093 0, 0.);
2094 }
2095 }
2096
2097 return eslOK;
2098 }
2099
2100 /* Function: pli_hmmonly_pass_statistics()
2101 * Synopsis: Final stats output for HMM only pass of a pipeline.
2102 * Incept: EPN, Fri Sep 24 16:48:06 2010
2103 * SRE, Tue Dec 9 10:19:45 2008 [Janelia] (p7_pli_Statistics())
2104 *
2105 * Purpose: Print a standardized report of the internal statistics of
2106 * a finished processing pipeline <pli> for HMM only passes
2107 * to stream <ofp>.
2108 *
2109 * Returns: <eslOK> on success.
2110 */
2111 int
2112 pli_hmmonly_pass_statistics(FILE *ofp, CM_PIPELINE *pli)
2113 {
2114 CM_PLI_ACCT *pli_acct = &(pli->acct[PLI_PASS_HMM_ONLY_ANY]);
2115 int match_cm_spacing = (pli->nmodels > 0) ? TRUE : FALSE;
2116 int64_t nres_searched = pli_acct->nres_top + pli_acct->nres_bot;
2117
2118 if(pli->do_hmmonly_always) {
2119 fprintf(ofp, "Internal HMM-only pipeline statistics summary: (--hmmonly used)\n");
2120 fprintf(ofp, "---------------------------------------------------------------\n");
2121 }
2122 else {
2123 fprintf(ofp, "Internal HMM-only pipeline statistics summary: (run for model(s) with zero basepairs)\n");
2124 fprintf(ofp, "--------------------------------------------------------------------------------------\n");
2125 }
2126
2127 if (pli->mode == CM_SEARCH_SEQS) {
2128 fprintf(ofp, "Query model(s): %s%15" PRId64 " (%" PRId64 " consensus positions)\n",
2129 match_cm_spacing ? " " : "",
2130 pli->nmodels_hmmonly, pli->nnodes_hmmonly);
2131 fprintf(ofp, "Target sequences: %s%15" PRId64 " (%" PRId64 " residues searched)\n",
2132 match_cm_spacing ? " " : "",
2133 pli->nseqs, nres_searched);
2134 } else { /* SCAN MODE */
2135 fprintf(ofp, "Query sequence(s): %s%15" PRId64 " (%d residues searched)\n",
2136 match_cm_spacing ? " " : "",
2137 pli->nseqs, (int) (nres_searched / pli->nmodels_hmmonly));
2138 fprintf(ofp, "Target model(s): %s%15" PRId64 " (%" PRId64 " consensus positions)\n",
2139 match_cm_spacing ? " " : "",
2140 pli->nmodels_hmmonly, pli->nnodes_hmmonly);
2141 }
2142
2143 fprintf(ofp, "Windows %spassing %slocal HMM SSV %sfilter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2144 match_cm_spacing ? " " : "",
2145 match_cm_spacing ? " " : "",
2146 match_cm_spacing ? " " : "",
2147 pli_acct->n_past_msv,
2148 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_msv / nres_searched,
2149 pli->F1_hmmonly);
2150
2151 if(pli->do_bias_hmmonly) {
2152 fprintf(ofp, "Windows %spassing %slocal HMM MSV %sbias filter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2153 match_cm_spacing ? " " : "",
2154 match_cm_spacing ? " " : "",
2155 match_cm_spacing ? " " : "",
2156 pli_acct->n_past_msvbias,
2157 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_msvbias / nres_searched,
2158 pli->F1_hmmonly);
2159 }
2160 else { /* msv bias is off by default, so don't output anything if it's off */
2161 fprintf(ofp, "Windows %spassing %slocal HMM MSV %sbias filter: %15s (off)\n",
2162 match_cm_spacing ? " " : "",
2163 match_cm_spacing ? " " : "",
2164 match_cm_spacing ? " " : "",
2165 "");
2166 }
2167
2168 if(! pli->do_max_hmmonly) {
2169 fprintf(ofp, "Windows %spassing %slocal HMM Viterbi %sfilter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2170 match_cm_spacing ? " " : "",
2171 match_cm_spacing ? " " : "",
2172 match_cm_spacing ? " " : "",
2173 pli_acct->n_past_vit,
2174 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_vit / nres_searched,
2175 pli->F2_hmmonly);
2176 }
2177 else {
2178 fprintf(ofp, "Windows %spassing %slocal HMM Viterbi %sfilter: %15s (off)\n",
2179 match_cm_spacing ? " " : "",
2180 match_cm_spacing ? " " : "",
2181 match_cm_spacing ? " " : "",
2182 "");
2183 }
2184
2185 if(! pli->do_max_hmmonly) {
2186 fprintf(ofp, "Windows %spassing %slocal HMM Forward %sfilter: %15" PRId64 " (%.4g); expected (%.4g)\n",
2187 match_cm_spacing ? " " : "",
2188 match_cm_spacing ? " " : "",
2189 match_cm_spacing ? " " : "",
2190 pli_acct->n_past_fwd,
2191 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_past_fwd / nres_searched,
2192 pli->F3_hmmonly);
2193 }
2194 else {
2195 fprintf(ofp, "Windows %spassing %slocal HMM Forward %sfilter: %15s (off)\n",
2196 match_cm_spacing ? " " : "",
2197 match_cm_spacing ? " " : "",
2198 match_cm_spacing ? " " : "",
2199 "");
2200 }
2201
2202 fprintf(ofp, "Total HMM hits reported: %s%15d (%.4g)\n",
2203 match_cm_spacing ? " " : "",
2204 (int) pli_acct->n_output,
2205 (nres_searched == 0) ? 0.0 : (double) pli_acct->pos_output / nres_searched);
2206
2207 return eslOK;
2208 }
2209
2210 /* Function: pli_sum_statistics()
2211 * Synopsis: Sum up pipeline statistics for all CM passes of a pipeline.
2212 * Incept: EPN, Thu Feb 23 11:00:39 2012
2213 *
2214 * Purpose: Sum stats for all relevant passes in a pipeline
2215 * and store them in pli->acct[PLI_PASS_CM_SUMMED].
2216 *
2217 * Returns: <eslOK> on success.
2218 */
2219 int
2220 pli_sum_statistics(CM_PIPELINE *pli)
2221 {
2222 int p; /* counter over passes */
2223
2224 /* first zero out pli->acct[PLI_PASS_CM_SUMMED] */
2225 cm_pli_ZeroAccounting(&(pli->acct[PLI_PASS_CM_SUMMED]));
2226
2227 /* now tally up counts for all passes we performed, we use
2228 * pli->acct[p].nres_top > 0 or pli->acct[p].nres_bot as indicator
2229 * pass was performed for at least one model.
2230 */
2231 for(p = 1; p < NPLI_PASSES; p++) {
2232 if(p == PLI_PASS_HMM_ONLY_ANY) continue; /* skip HMM only stage */
2233 if(pli->acct[p].nres_top > 0 || pli->acct[p].nres_bot > 0) {
2234 pli->acct[PLI_PASS_CM_SUMMED].npli_top += pli->acct[p].npli_top;
2235 pli->acct[PLI_PASS_CM_SUMMED].npli_bot += pli->acct[p].npli_bot;
2236 pli->acct[PLI_PASS_CM_SUMMED].nres_top += pli->acct[p].nres_top;
2237 pli->acct[PLI_PASS_CM_SUMMED].nres_bot += pli->acct[p].nres_bot;
2238 pli->acct[PLI_PASS_CM_SUMMED].n_past_msv += pli->acct[p].n_past_msv;
2239 pli->acct[PLI_PASS_CM_SUMMED].n_past_vit += pli->acct[p].n_past_vit;
2240 pli->acct[PLI_PASS_CM_SUMMED].n_past_fwd += pli->acct[p].n_past_fwd;
2241 pli->acct[PLI_PASS_CM_SUMMED].n_past_gfwd += pli->acct[p].n_past_gfwd;
2242 pli->acct[PLI_PASS_CM_SUMMED].n_past_edef += pli->acct[p].n_past_edef;
2243 pli->acct[PLI_PASS_CM_SUMMED].n_past_cyk += pli->acct[p].n_past_cyk;
2244 pli->acct[PLI_PASS_CM_SUMMED].n_past_ins += pli->acct[p].n_past_ins;
2245 pli->acct[PLI_PASS_CM_SUMMED].n_output += pli->acct[p].n_output;
2246 pli->acct[PLI_PASS_CM_SUMMED].n_past_msvbias += pli->acct[p].n_past_msvbias;
2247 pli->acct[PLI_PASS_CM_SUMMED].n_past_vitbias += pli->acct[p].n_past_vitbias;
2248 pli->acct[PLI_PASS_CM_SUMMED].n_past_fwdbias += pli->acct[p].n_past_fwdbias;
2249 pli->acct[PLI_PASS_CM_SUMMED].n_past_gfwdbias += pli->acct[p].n_past_gfwdbias;
2250 pli->acct[PLI_PASS_CM_SUMMED].n_past_edefbias += pli->acct[p].n_past_edefbias;
2251 pli->acct[PLI_PASS_CM_SUMMED].pos_past_msv += pli->acct[p].pos_past_msv;
2252 pli->acct[PLI_PASS_CM_SUMMED].pos_past_vit += pli->acct[p].pos_past_vit;
2253 pli->acct[PLI_PASS_CM_SUMMED].pos_past_fwd += pli->acct[p].pos_past_fwd;
2254 pli->acct[PLI_PASS_CM_SUMMED].pos_past_gfwd += pli->acct[p].pos_past_gfwd;
2255 pli->acct[PLI_PASS_CM_SUMMED].pos_past_edef += pli->acct[p].pos_past_edef;
2256 pli->acct[PLI_PASS_CM_SUMMED].pos_past_cyk += pli->acct[p].pos_past_cyk;
2257 pli->acct[PLI_PASS_CM_SUMMED].pos_past_ins += pli->acct[p].pos_past_ins;
2258 pli->acct[PLI_PASS_CM_SUMMED].pos_output += pli->acct[p].pos_output;
2259 pli->acct[PLI_PASS_CM_SUMMED].pos_past_msvbias += pli->acct[p].pos_past_msvbias;
2260 pli->acct[PLI_PASS_CM_SUMMED].pos_past_vitbias += pli->acct[p].pos_past_vitbias;
2261 pli->acct[PLI_PASS_CM_SUMMED].pos_past_fwdbias += pli->acct[p].pos_past_fwdbias;
2262 pli->acct[PLI_PASS_CM_SUMMED].pos_past_gfwdbias += pli->acct[p].pos_past_gfwdbias;
2263 pli->acct[PLI_PASS_CM_SUMMED].pos_past_edefbias += pli->acct[p].pos_past_edefbias;
2264
2265 pli->acct[PLI_PASS_CM_SUMMED].n_overflow_fcyk += pli->acct[p].n_overflow_fcyk;
2266 pli->acct[PLI_PASS_CM_SUMMED].n_overflow_final += pli->acct[p].n_overflow_final;
2267 pli->acct[PLI_PASS_CM_SUMMED].n_aln_hb += pli->acct[p].n_aln_hb;
2268 pli->acct[PLI_PASS_CM_SUMMED].n_aln_dccyk += pli->acct[p].n_aln_dccyk;
2269 }
2270 }
2271 return eslOK;
2272 }
2273
2274 /* Function: cm_pli_ZeroAccounting()
2275 * Synopsis: Zero a set of pipeline accounting statistics.
2276 * Incept: EPN, Mon Nov 28 18:40:00 2011
2277 *
2278 * Returns: <eslOK> on success.
2279 */
2280 int
2281 cm_pli_ZeroAccounting(CM_PLI_ACCT *pli_acct)
2282 {
2283 pli_acct->npli_top = 0;
2284 pli_acct->npli_bot = 0;
2285 pli_acct->nres_top = 0;
2286 pli_acct->nres_bot = 0;
2287 pli_acct->n_past_msv = 0;
2288 pli_acct->n_past_vit = 0;
2289 pli_acct->n_past_fwd = 0;
2290 pli_acct->n_past_gfwd = 0;
2291 pli_acct->n_past_edef = 0;
2292 pli_acct->n_past_cyk = 0;
2293 pli_acct->n_past_ins = 0;
2294 pli_acct->n_output = 0;
2295 pli_acct->n_past_msvbias = 0;
2296 pli_acct->n_past_vitbias = 0;
2297 pli_acct->n_past_fwdbias = 0;
2298 pli_acct->n_past_gfwdbias = 0;
2299 pli_acct->n_past_edefbias = 0;
2300 pli_acct->pos_past_msv = 0;
2301 pli_acct->pos_past_vit = 0;
2302 pli_acct->pos_past_fwd = 0;
2303 pli_acct->pos_past_gfwd = 0;
2304 pli_acct->pos_past_edef = 0;
2305 pli_acct->pos_past_cyk = 0;
2306 pli_acct->pos_past_ins = 0;
2307 pli_acct->pos_output = 0;
2308 pli_acct->pos_past_msvbias = 0;
2309 pli_acct->pos_past_vitbias = 0;
2310 pli_acct->pos_past_fwdbias = 0;
2311 pli_acct->pos_past_gfwdbias = 0;
2312 pli_acct->pos_past_edefbias = 0;
2313
2314 pli_acct->n_overflow_fcyk = 0;
2315 pli_acct->n_overflow_final = 0;
2316 pli_acct->n_aln_hb = 0;
2317 pli_acct->n_aln_dccyk = 0;
2318
2319 return eslOK;
2320 }
2321
2322 /* Function: cm_pli_PassEnforcesFirstRes()
2323 * Date: EPN, Thu Feb 16 07:42:59 2012
2324 *
2325 * Purpose: Return TRUE if the pipeline pass indicated by
2326 * <pass_idx> forces the inclusion of i0 (first
2327 * residue in the sequence) in any valid
2328 * parsetree/alignment. Else returns FALSE.
2329 *
2330 * Args: pass_idx - a pipeline pass index:
2331 * PLI_PASS_CM_SUMMED | PLI_PASS_STD_ANY | PLI_PASS_5P_ONLY_FORCE |
2332 * PLI_PASS_3P_ONLY_FORCE | PLI_PASS_5P_AND_3P_FORCE | PLI_PASS_HMM_ONLY_ANY
2333 *
2334 * Returns: TRUE if i0 inclusion is forced, FALSE if not
2335 */
2336 int
2337 cm_pli_PassEnforcesFirstRes(int pass_idx)
2338 {
2339 switch (pass_idx) {
2340 case PLI_PASS_STD_ANY: return FALSE; break;
2341 case PLI_PASS_5P_ONLY_FORCE: return TRUE; break;
2342 case PLI_PASS_3P_ONLY_FORCE: return FALSE; break;
2343 case PLI_PASS_5P_AND_3P_FORCE: return TRUE; break;
2344 case PLI_PASS_5P_AND_3P_ANY: return FALSE; break;
2345 case PLI_PASS_HMM_ONLY_ANY: return FALSE; break;
2346 default: return FALSE; break;
2347 }
2348 return FALSE;
2349 }
2350
2351 /* Function: cm_pli_PassEnforcesFinalRes()
2352 * Date: EPN, Thu Feb 16 07:46:10 2012
2353 *
2354 * Purpose: Return TRUE if the pipeline pass indicated by
2355 * <pass_idx> forces the inclusion of j0 (final
2356 * residue in the sequence) in any valid
2357 * parsetree/alignment. Else returns FALSE.
2358 *
2359 * Args: pass_idx - a pipeline pass index
2360 * PLI_PASS_CM_SUMMED | PLI_PASS_STD_ANY | PLI_PASS_5P_ONLY_FORCE |
2361 * PLI_PASS_3P_ONLY_FORCE | PLI_PASS_5P_AND_3P_FORCE | PLI_PASS_HMM_ONLY_ANY
2362 *
2363 * Returns: TRUE if j0 inclusion is forced, FALSE if not
2364 */
2365 int
2366 cm_pli_PassEnforcesFinalRes(int pass_idx)
2367 {
2368 switch (pass_idx) {
2369 case PLI_PASS_STD_ANY: return FALSE; break;
2370 case PLI_PASS_5P_ONLY_FORCE: return FALSE; break;
2371 case PLI_PASS_3P_ONLY_FORCE: return TRUE; break;
2372 case PLI_PASS_5P_AND_3P_FORCE: return TRUE; break;
2373 case PLI_PASS_5P_AND_3P_ANY: return FALSE; break;
2374 case PLI_PASS_HMM_ONLY_ANY: return FALSE; break;
2375 default: return FALSE; break;
2376 }
2377 return FALSE;
2378 }
2379
2380 /* Function: cm_pli_PassAllowsTruncation()
2381 * Date: EPN, Wed Mar 14 13:53:30 2012
2382 *
2383 * Purpose: Return TRUE if the pipeline pass indicated by <pass_idx>
2384 * allows some type of truncated alignment, else return
2385 * FALSE. In other words, if we can safely call a standard
2386 * (non-truncated) DP alignment/search function for this
2387 * pass then return FALSE, else return TRUE.
2388 *
2389 * Args: pass_idx - a pipeline pass index
2390 * PLI_PASS_CM_SUMMED | PLI_PASS_STD_ANY | PLI_PASS_5P_ONLY_FORCE |
2391 * PLI_PASS_3P_ONLY_FORCE | PLI_PASS_5P_AND_3P_FORCE | PLI_PASS_HMM_ONLY_ANY
2392 *
2393 * Returns: TRUE if j0 inclusion is forced, FALSE if not
2394 */
2395 int
2396 cm_pli_PassAllowsTruncation(int pass_idx)
2397 {
2398 switch (pass_idx) {
2399 case PLI_PASS_STD_ANY: return FALSE; break;
2400 case PLI_PASS_5P_ONLY_FORCE: return TRUE; break;
2401 case PLI_PASS_3P_ONLY_FORCE: return TRUE; break;
2402 case PLI_PASS_5P_AND_3P_FORCE: return TRUE; break;
2403 case PLI_PASS_5P_AND_3P_ANY: return TRUE; break;
2404 case PLI_PASS_HMM_ONLY_ANY: return FALSE; break;
2405 default: return FALSE; break;
2406 }
2407 return FALSE;
2408 }
2409
2410 /* Function: cm_pli_AdjustNresForOverlaps()
2411 * Incept: EPN, Wed Nov 19 09:20:17 2014
2412 *
2413 * Purpose: Update <nres_top> or <nres_bot> values in a CM_PIPELINE
2414 * <pli> to account for overlapping windows from previous pipeline
2415 * passes. Only certain passes are affected, all others can
2416 * never search overlaps. If <in_rc> is TRUE we adjust <nres_bot>,
2417 * else we adjust <nres_top>.
2418 *
2419 * Returns: void.
2420 */
2421 void
2422 cm_pli_AdjustNresForOverlaps(CM_PIPELINE *pli, int64_t noverlap, int in_rc)
2423 {
2424 if(in_rc) {
2425 if(! pli->do_hmmonly_cur) pli->acct[PLI_PASS_STD_ANY].nres_bot -= noverlap;
2426 else pli->acct[PLI_PASS_HMM_ONLY_ANY].nres_bot -= noverlap;
2427 if(pli->do_trunc_any || pli->do_trunc_int || pli->do_trunc_only) pli->acct[PLI_PASS_5P_AND_3P_ANY].nres_bot -= noverlap;
2428 }
2429 else {
2430 if(! pli->do_hmmonly_cur) pli->acct[PLI_PASS_STD_ANY].nres_top -= noverlap;
2431 else pli->acct[PLI_PASS_HMM_ONLY_ANY].nres_top -= noverlap;
2432 if(pli->do_trunc_any || pli->do_trunc_int || pli->do_trunc_only) pli->acct[PLI_PASS_5P_AND_3P_ANY].nres_top -= noverlap;
2433 }
2434 return;
2435 }
2436
2437 /*------------------- end, pipeline API -------------------------*/
2438
2439
2440 /*****************************************************************
2441 * 3. Non-API filter stage search and other functions
2442 *****************************************************************/
2443
2444
2445 /* Function: pli_p7_filter()
2446 * Synopsis: The accelerated p7 comparison pipeline: MSV through Forward filter.
2447 * Incept: EPN, Wed Nov 24 13:07:02 2010
2448 * TJW, Fri Feb 26 10:17:53 2018 [Janelia] (p7_Pipeline_Longtargets())
2449 *
2450 * Purpose: Run the accelerated pipeline to compare profile <om>
2451 * against sequence <sq>. Some combination of the MSV,
2452 * Viterbi and Forward algorithms are used, based on
2453 * option flags set in <pli>.
2454 *
2455 * In a normal pipeline run, this function call is the
2456 * first search function used and should be followed by a
2457 * call to pli_p7_env_def().
2458 *
2459 * Returns: <eslOK> on success. For the <ret_nwin> windows that
2460 * survive all filters, the start and end positions of the
2461 * windows are stored and returned in <ret_ws> and
2462 * <ret_we> respectively.
2463 *
2464 * <eslEINVAL> if (in a scan pipeline) we're supposed to
2465 * set GA/TC/NC bit score thresholds but the model doesn't
2466 * have any.
2467 *
2468 * <eslERANGE> on numerical overflow errors in the
2469 * optimized vector implementations; particularly in
2470 * posterior decoding. I don't believe this is possible for
2471 * multihit local models, but I'm set up to catch it
2472 * anyway. We may emit a warning to the user, but cleanly
2473 * skip the problematic sequence and continue.
2474 *
2475 * Throws: <eslEMEM> on allocation failure.
2476 *
2477 * Xref: J4/25.
2478 */
2479 int
2480 pli_p7_filter(CM_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, P7_SCOREDATA *msvdata, const ESL_SQ *sq, int64_t **ret_ws, int64_t **ret_we, float **ret_wb, int *ret_nwin)
2481 {
2482 int status;
2483 float mfsc, vfsc, fwdsc; /* filter scores */
2484 float filtersc; /* HMM null filter score */
2485 int have_filtersc; /* TRUE if filtersc has been calc'ed for current window */
2486 float nullsc; /* null model score */
2487 float wsc; /* the corrected bit score for a window */
2488 double P; /* P-value of a hit */
2489 int i, i2; /* counters */
2490 int wlen; /* length of current window */
2491 void *p; /* for ESL_RALLOC */
2492 int *useme = NULL; /* used when merging overlapping windows */
2493 int overlap; /* number of overlapping positions b/t 2 adjacent windows */
2494 int **survAA = NULL; /* [0..s..Np7_SURV-1][0..i..nwin-1] TRUE if window i survived stage s */
2495 int nalloc; /* currently allocated size for ws, we */
2496 int64_t *ws = NULL; /* [0..nwin-1] window start positions */
2497 int64_t *we = NULL; /* [0..nwin-1] window end positions */
2498 double *wp = NULL; /* [0..nwin-1] window P-value of furthest-reached filter alg, not valid until Vit (invalid for F1, F1b) */
2499 float *wb = NULL; /* [0..nwin-1] window bit score of furthest-reached filter alg, not valid until Vit (invalid for F1, F1b) */
2500 int nwin; /* number of windows */
2501 int64_t *new_ws = NULL; /* used when copying/modifying ws */
2502 int64_t *new_we = NULL; /* used when copying/modifying we */
2503 float *new_wb = NULL; /* used when copying/modifying wb */
2504 int nsurv_fwd; /* number of windows that survive fwd filter */
2505 ESL_DSQ *subdsq; /* a ptr to the first position of a window */
2506 int have_rest; /* do we have the full <om> read in? */
2507 P7_HMM_WINDOWLIST wlist; /* list of windows, structure taken by p7_MSVFilter_longtarget() */
2508 int save_max_length = om->max_length;
2509
2510 /* filter thresholds and on/off parameters, these will normally be set to
2511 * CM pipeline values unless pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY,
2512 * in which case they're set to HMM only pipeline values.
2513 */
2514 int cur_do_msv = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? TRUE : pli->do_msv;
2515 int cur_do_msvbias = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? pli->do_bias_hmmonly : pli->do_msvbias;
2516 int cur_do_vit = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? (! pli->do_max_hmmonly) : pli->do_vit;
2517 int cur_do_vitbias = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? FALSE : pli->do_vitbias;
2518 int cur_do_fwd = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? (! pli->do_max_hmmonly) : pli->do_fwd;
2519 int cur_do_fwdbias = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? FALSE : pli->do_fwdbias;
2520 double cur_F1 = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? pli->F1_hmmonly : pli->F1;
2521 double cur_F1b = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? pli->F1_hmmonly : pli->F1b;
2522 double cur_F2 = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? pli->F2_hmmonly : pli->F2;
2523 double cur_F2b = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? 1.0 : pli->F2b;
2524 double cur_F3 = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? pli->F3_hmmonly : pli->F3;
2525 double cur_F3b = (pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY) ? 1.0 : pli->F3b;
2526
2527 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
2528 p7_omx_GrowTo(pli->oxf, om->M, 0, sq->n); /* expand the one-row omx if needed */
2529 have_rest = (om->mode == p7_NO_MODE) ? FALSE : TRUE; /* we use om->mode as a flag to tell us whether we already have read the full <om> from disk or not */
2530
2531 /* Set false target length. This is a conservative estimate of the length of window that'll
2532 * soon be passed on to later phases of the pipeline; used to recover some bits of the score
2533 * that we would miss if we left length parameters set to the full target length */
2534
2535 /* Set MSV length as pli->maxW/
2536 * Note: this differs from nhmmer, which uses om->max_length
2537 */
2538 p7_oprofile_ReconfigMSVLength(om, pli->maxW);
2539 om->max_length = pli->maxW;
2540
2541 #if eslDEBUGLEVEL >= 2
2542 printf("#DEBUG\n#DEBUG: PIPELINE pli_p7_filter() %s %" PRId64 " residues\n", sq->name, sq->n);
2543 #endif
2544
2545 /* initializations */
2546 nsurv_fwd = 0;
2547 nwin = 0;
2548
2549 /***************************************************/
2550 /* Filter 1: SSV, long target-variant, with p7 HMM */
2551 if(cur_do_msv) {
2552 p7_hmmwindow_init(&wlist);
2553 status = p7_SSVFilter_longtarget(sq->dsq, sq->n, om, pli->oxf, msvdata, bg, cur_F1, &wlist);
2554
2555 if(wlist.count > 0) {
2556 /* In scan mode, if at least one window passes the MSV filter, read the rest of the profile */
2557 if (pli->mode == CM_SCAN_MODELS && (! have_rest)) {
2558 if (pli->cmfp) p7_oprofile_ReadRest(pli->cmfp->hfp, om);
2559 /* Note: we don't call cm_pli_NewModelThresholds() yet (as p7_pipeline()
2560 * does at this point), because we don't yet have the CM */
2561 have_rest = TRUE;
2562 }
2563 if(msvdata->prefix_lengths == NULL && msvdata->suffix_lengths == NULL) {
2564 p7_hmm_ScoreDataComputeRest(om, msvdata);
2565 /* only call *ComputeRest() if we haven't already (we may have
2566 * already in a previous pipeline pass).
2567 */
2568 }
2569 p7_pli_ExtendAndMergeWindows(om, msvdata, &wlist, 0.0);
2570 }
2571 ESL_ALLOC(ws, sizeof(int64_t) * ESL_MAX(1, wlist.count)); // avoid 0 malloc
2572 ESL_ALLOC(we, sizeof(int64_t) * ESL_MAX(1, wlist.count));
2573 nwin = wlist.count;
2574 for(i = 0; i < nwin; i++) {
2575 ws[i] = wlist.windows[i].n;
2576 we[i] = ws[i] + wlist.windows[i].length - 1;
2577 }
2578 /* split up windows > (2 * pli->cmW) into length 2W, with W-1
2579 * overlapping residues.
2580 */
2581 nalloc = nwin + 100;
2582 ESL_ALLOC(new_ws, sizeof(int64_t) * nalloc);
2583 ESL_ALLOC(new_we, sizeof(int64_t) * nalloc);
2584 for (i = 0, i2 = 0; i < nwin; i++, i2++) {
2585 wlen = we[i] - ws[i] + 1;
2586 if((i2+1) == nalloc) {
2587 nalloc += 100;
2588 ESL_RALLOC(new_ws, p, sizeof(int64_t) * nalloc);
2589 ESL_RALLOC(new_we, p, sizeof(int64_t) * nalloc);
2590 }
2591 if(wlen > (2 * pli->cmW)) {
2592 /* split this window */
2593 new_ws[i2] = ws[i];
2594 new_we[i2] = ESL_MIN((new_ws[i2] + (2 * pli->cmW) - 1), we[i]);
2595 while(new_we[i2] < we[i]) {
2596 i2++;
2597 if((i2+1) == nalloc) {
2598 nalloc += 100;
2599 ESL_RALLOC(new_ws, p, sizeof(int64_t) * nalloc);
2600 ESL_RALLOC(new_we, p, sizeof(int64_t) * nalloc);
2601 }
2602 new_ws[i2] = ESL_MIN(new_ws[i2-1] + pli->cmW, we[i]);
2603 new_we[i2] = ESL_MIN(new_we[i2-1] + pli->cmW, we[i]);
2604 }
2605 }
2606 else { /* do not split this window */
2607 new_ws[i2] = ws[i];
2608 new_we[i2] = we[i];
2609 }
2610 }
2611 free(wlist.windows);
2612 free(ws);
2613 free(we);
2614 ws = new_ws;
2615 we = new_we;
2616 nwin = i2;
2617 }
2618 else { /* do_msv is FALSE */
2619 nwin = 1; /* first window */
2620 if(sq->n > (2 * pli->maxW)) {
2621 nwin += (int) (sq->n - (2 * pli->maxW)) / ((2 * pli->maxW) - (pli->maxW - 1));
2622 /* (L - first window)/(number of unique residues per window) */
2623 if(((sq->n - (2 * pli->maxW)) % ((2 * pli->maxW) - (pli->maxW - 1))) > 0) {
2624 nwin++; /* if the (int) cast in previous line removed any fraction of a window, we add it back here */
2625 }
2626 }
2627 ESL_ALLOC(ws, sizeof(int64_t) * nwin);
2628 ESL_ALLOC(we, sizeof(int64_t) * nwin);
2629 for(i = 0; i < nwin; i++) {
2630 ws[i] = 1 + (i * (pli->maxW + 1));
2631 we[i] = ESL_MIN((ws[i] + (2*pli->maxW) - 1), sq->n);
2632 /*printf("window %5d/%5d %10" PRId64 "..%10" PRId64 " (L=%10" PRId64 ")\n", i+1, nwin, ws[i], we[i], sq->n);*/
2633 }
2634 }
2635 pli->acct[pli->cur_pass_idx].n_past_msv += nwin;
2636
2637 /*********************************************/
2638 /* allocate and initialize survAA, which will keep track of number of windows surviving each stage */
2639 ESL_ALLOC(survAA, sizeof(int *) * Np7_SURV);
2640 for (i = 0; i < Np7_SURV; i++) {
2641 ESL_ALLOC(survAA[i], sizeof(int) * ESL_MAX(1, nwin)); // avoid 0 mallocs
2642 esl_vec_ISet(survAA[i], nwin, FALSE);
2643 }
2644
2645 ESL_ALLOC(wp, sizeof(double) * ESL_MAX(1, nwin)); // avoid 0 mallocs
2646 ESL_ALLOC(wb, sizeof(float) * ESL_MAX(1, nwin));
2647 for (i = 0; i < nwin; i++) { wp[i] = 1.0; }
2648 for (i = 0; i < nwin; i++) { wb[i] = -999.0; }
2649
2650 for (i = 0; i < nwin; i++) {
2651 subdsq = sq->dsq + ws[i] - 1;
2652 have_filtersc = FALSE;
2653 wlen = we[i] - ws[i] + 1;
2654
2655 p7_bg_SetLength(bg, wlen);
2656 p7_bg_NullOne (bg, subdsq, wlen, &nullsc);
2657
2658 #if eslDEBUGLEVEL >= 2
2659 if(cur_do_msv) printf("#DEBUG: SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived SSV ? bits ? P\n", i, ws[i], we[i]);
2660 #endif
2661 survAA[p7_SURV_F1][i] = TRUE;
2662
2663 if (cur_do_msv && cur_do_msvbias) {
2664 /******************************************************************************/
2665 /* Filter 1B: Bias filter with p7 HMM
2666 * Have to run msv again, to get the full score for the window.
2667 * (using the standard "per-sequence" msv filter this time).
2668 */
2669 p7_oprofile_ReconfigMSVLength(om, wlen);
2670 p7_MSVFilter(subdsq, wlen, om, pli->oxf, &mfsc);
2671 p7_bg_FilterScore(bg, subdsq, wlen, &filtersc);
2672 have_filtersc = TRUE;
2673
2674 wsc = (mfsc - filtersc) / eslCONST_LOG2;
2675 P = esl_gumbel_surv(wsc, p7_evparam[CM_p7_LMMU], p7_evparam[CM_p7_LMLAMBDA]);
2676 wp[i] = P;
2677
2678 if (P > cur_F1b) continue;
2679
2680 /******************************************************************************/
2681 }
2682
2683 pli->acct[pli->cur_pass_idx].n_past_msvbias++;
2684 survAA[p7_SURV_F1b][i] = TRUE;
2685
2686 #if eslDEBUGLEVEL >= 2
2687 if(cur_do_msv && cur_do_msvbias) printf("#DEBUG: SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived MSV-Bias ? bits P ?\n", i, ws[i], we[i]);
2688 #endif
2689 if(pli->do_time_F1) return eslOK;
2690
2691 /* In scan mode, we may get to this point and not yet have read the rest of
2692 * the profile if the msvfilter is off, if so read the rest of the profile.
2693 */
2694 if (pli->mode == CM_SCAN_MODELS && (! have_rest)) {
2695 if (pli->cmfp) p7_oprofile_ReadRest(pli->cmfp->hfp, om);
2696 /* Note: we don't call cm_pli_NewModelThresholds() yet (as p7_pipeline()
2697 * does at this point), because we don't yet have the CM */
2698 have_rest = TRUE;
2699 }
2700 if(cur_do_msv && cur_do_msvbias) { /* we already called p7_oprofile_ReconfigMSVLength() above */
2701 p7_oprofile_ReconfigRestLength(om, wlen);
2702 }
2703 else { /* we did not call p7_oprofile_ReconfigMSVLength() above */
2704 p7_oprofile_ReconfigLength(om, wlen);
2705 }
2706
2707 if (cur_do_vit) {
2708 /******************************************************************************/
2709 /* Filter 2: Viterbi with p7 HMM */
2710 /* Second level filter: ViterbiFilter(), multihit with <om> */
2711 p7_ViterbiFilter(subdsq, wlen, om, pli->oxf, &vfsc);
2712 wsc = (vfsc - nullsc) / eslCONST_LOG2;
2713 P = esl_gumbel_surv(wsc, p7_evparam[CM_p7_LVMU], p7_evparam[CM_p7_LVLAMBDA]);
2714 wp[i] = P;
2715 wb[i] = wsc;
2716 if (P > cur_F2) continue;
2717 }
2718 pli->acct[pli->cur_pass_idx].n_past_vit++;
2719 survAA[p7_SURV_F2][i] = TRUE;
2720
2721 #if eslDEBUGLEVEL >= 2
2722 if (cur_do_vit) printf("#DEBUG: SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived Vit %6.2f bits P %g\n", i, ws[i], we[i], wb[i], wp[i]);
2723 #endif
2724
2725 /********************************************/
2726 if (cur_do_vit && cur_do_vitbias) {
2727 if(! have_filtersc) {
2728 p7_bg_FilterScore(bg, subdsq, wlen, &filtersc);
2729 }
2730 have_filtersc = TRUE;
2731 wsc = (vfsc - filtersc) / eslCONST_LOG2;
2732 P = esl_gumbel_surv(wsc, p7_evparam[CM_p7_LVMU], p7_evparam[CM_p7_LVLAMBDA]);
2733 wp[i] = P;
2734 wb[i] = wsc;
2735 if (P > cur_F2b) continue;
2736 /******************************************************************************/
2737 }
2738 pli->acct[pli->cur_pass_idx].n_past_vitbias++;
2739 survAA[p7_SURV_F2b][i] = TRUE;
2740
2741 #if eslDEBUGLEVEL >= 2
2742 if (cur_do_vit && cur_do_vitbias) printf("#DEBUG: SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived Vit-Bias %6.2f bits P %g\n", i, ws[i], we[i], wb[i], wp[i]);
2743 #endif
2744 if(pli->do_time_F2) continue;
2745 /********************************************/
2746
2747 if(cur_do_fwd) {
2748 /******************************************************************************/
2749 /* Filter 3: Forward with p7 HMM */
2750 /* Parse it with Forward and obtain its real Forward score. */
2751 p7_ForwardParser(subdsq, wlen, om, pli->oxf, &fwdsc);
2752 wsc = (fwdsc - nullsc) / eslCONST_LOG2;
2753 P = esl_exp_surv(wsc, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
2754 wp[i] = P;
2755 wb[i] = wsc;
2756 if (P > cur_F3) continue;
2757 }
2758 /******************************************************************************/
2759 pli->acct[pli->cur_pass_idx].n_past_fwd++;
2760 survAA[p7_SURV_F3][i] = TRUE;
2761
2762 #if eslDEBUGLEVEL >= 2
2763 if(cur_do_fwd) printf("#DEBUG SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived Fwd %6.2f bits P %g\n", i, ws[i], we[i], wb[i], wp[i]);
2764 #endif
2765
2766 if (cur_do_fwd && cur_do_fwdbias) {
2767 if (! have_filtersc) {
2768 p7_bg_FilterScore(bg, subdsq, wlen, &filtersc);
2769 }
2770 have_filtersc = TRUE;
2771 wsc = (fwdsc - filtersc) / eslCONST_LOG2;
2772 P = esl_exp_surv(wsc, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
2773 wp[i] = P;
2774 wb[i] = wsc;
2775 if (P > cur_F3b) continue;
2776 /******************************************************************************/
2777 }
2778 pli->acct[pli->cur_pass_idx].n_past_fwdbias++;
2779 nsurv_fwd++;
2780 survAA[p7_SURV_F3b][i] = TRUE;
2781
2782 #if eslDEBUGLEVEL >= 2
2783 if(cur_do_fwd && cur_do_fwdbias) printf("#DEBUG SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived Fwd-Bias %6.2f bits P %g\n", i, ws[i], we[i], wb[i], wp[i]);
2784 #endif
2785 }
2786
2787 /* Go back through all windows, and tally up total number of
2788 * residues that survived each stage, without double-counting
2789 * overlapping residues. Based on the way the windows were split, we
2790 * know that any overlapping residues must occur in adjacent windows
2791 * and we exploit that here.
2792 */
2793 for(i = 0; i < nwin; i++) {
2794 wlen = we[i] - ws[i] + 1;
2795
2796 if(survAA[p7_SURV_F1][i]) pli->acct[pli->cur_pass_idx].pos_past_msv += wlen;
2797 if(survAA[p7_SURV_F1b][i]) pli->acct[pli->cur_pass_idx].pos_past_msvbias += wlen;
2798 if(survAA[p7_SURV_F2][i]) pli->acct[pli->cur_pass_idx].pos_past_vit += wlen;
2799 if(survAA[p7_SURV_F2b][i]) pli->acct[pli->cur_pass_idx].pos_past_vitbias += wlen;
2800 if(survAA[p7_SURV_F3][i]) pli->acct[pli->cur_pass_idx].pos_past_fwd += wlen;
2801 if(survAA[p7_SURV_F3b][i]) pli->acct[pli->cur_pass_idx].pos_past_fwdbias += wlen;
2802
2803 /* now subtract residues we've double counted */
2804 if(i > 0) {
2805 overlap = we[i-1] - ws[i] + 1;
2806 if(overlap > 0) {
2807 if(survAA[p7_SURV_F1][i] && survAA[p7_SURV_F1][i-1]) pli->acct[pli->cur_pass_idx].pos_past_msv -= overlap;
2808 if(survAA[p7_SURV_F1b][i] && survAA[p7_SURV_F1b][i-1]) pli->acct[pli->cur_pass_idx].pos_past_msvbias -= overlap;
2809 if(survAA[p7_SURV_F2][i] && survAA[p7_SURV_F2][i-1]) pli->acct[pli->cur_pass_idx].pos_past_vit -= overlap;
2810 if(survAA[p7_SURV_F2b][i] && survAA[p7_SURV_F2b][i-1]) pli->acct[pli->cur_pass_idx].pos_past_vitbias -= overlap;
2811 if(survAA[p7_SURV_F3][i] && survAA[p7_SURV_F3][i-1]) pli->acct[pli->cur_pass_idx].pos_past_fwd -= overlap;
2812 if(survAA[p7_SURV_F3b][i] && survAA[p7_SURV_F3b][i-1]) pli->acct[pli->cur_pass_idx].pos_past_fwdbias -= overlap;
2813 }
2814 }
2815 }
2816
2817 /* Finally, create list of just those that survived fwd, and merge any overlapping windows together */
2818 if(nsurv_fwd > 0) {
2819 ESL_ALLOC(new_ws, sizeof(int64_t) * nsurv_fwd);
2820 ESL_ALLOC(new_we, sizeof(int64_t) * nsurv_fwd);
2821 ESL_ALLOC(new_wb, sizeof(float) * nsurv_fwd);
2822 for (i = 0, i2 = 0; i < nwin; i++) {
2823 if(survAA[p7_SURV_F3b][i]) {
2824 new_ws[i2] = ws[i];
2825 new_we[i2] = we[i];
2826 new_wb[i2] = wb[i];
2827 i2++;
2828 }
2829 }
2830 /* we could have overlapping windows, merge those that do overlap */
2831 ESL_ALLOC(useme, sizeof(int) * nsurv_fwd);
2832 esl_vec_ISet(useme, nsurv_fwd, FALSE);
2833 i2 = 0;
2834 for(i = 0, i2 = 0; i < nsurv_fwd; i++) {
2835 useme[i] = TRUE;
2836 i2 = i+1;
2837 while((i2 < nsurv_fwd) && ((new_we[i]+1) >= (new_ws[i2]))) {
2838 useme[i2] = FALSE;
2839 new_we[i] = new_we[i2]; /* merged i with i2, rewrite end for i */
2840 new_wb[i] = ESL_MAX(new_wb[i], new_wb[i2]); /* keep higher score */
2841 i2++;
2842 }
2843 i = i2-1;
2844 }
2845 i2 = 0;
2846 for(i = 0; i < nsurv_fwd; i++) {
2847 if(useme[i]) {
2848 new_ws[i2] = new_ws[i];
2849 new_we[i2] = new_we[i];
2850 new_wb[i2] = new_wb[i];
2851 i2++;
2852 }
2853 }
2854 nsurv_fwd = i2;
2855 free(useme);
2856 free(ws); ws = NULL;
2857 free(we); we = NULL;
2858 free(wp); wp = NULL;
2859 free(wb); wb = NULL;
2860 ws = new_ws;
2861 we = new_we;
2862 wb = new_wb;
2863 } /* end of 'if(nsurv_fwd > 0)' */
2864 else {
2865 if(ws != NULL) { free(ws); ws = NULL; }
2866 if(we != NULL) { free(we); we = NULL; }
2867 if(wp != NULL) { free(wp); wp = NULL; }
2868 if(wb != NULL) { free(wb); wb = NULL; }
2869 }
2870
2871 if(survAA != NULL) {
2872 for (i = 0; i < Np7_SURV; i++) free(survAA[i]);
2873 free(survAA);
2874 }
2875
2876 om->max_length = save_max_length;
2877
2878 *ret_ws = ws;
2879 *ret_we = we;
2880 *ret_wb = wb;
2881 *ret_nwin = nsurv_fwd;
2882
2883 return eslOK;
2884
2885 ERROR:
2886 ESL_EXCEPTION(eslEMEM, "Error allocating memory for hit list in pipeline\n");
2887
2888 }
2889
2890 /* Function: pli_p7_env_def()
2891 * Synopsis: Envelope definition of hits surviving Forward, prior to passing to CYK.
2892 * Incept: EPN, Wed Nov 24 13:18:54 2010
2893 *
2894 * Purpose: For each window x, from <ws[x]>..<we[x]>, determine
2895 * the envelope boundaries for any hits within it using
2896 * a p7 profile.
2897 *
2898 * In the SCAN pipeline we may enter this function with
2899 * *opt_gm == NULL because we haven't yet read it from the
2900 * HMM file. In that case, read it and return it in
2901 * <*opt_gm>. Otherwise, <*opt_gm> is valid upon entering.
2902 *
2903 * If the P-value of any detected envelopes is sufficiently
2904 * high (above pli->F5), we skip them (i.e. envelope defn
2905 * acts as a filter too). Further, in glocal mode
2906 * (<do_glocal>==TRUE) we skip any window for which the
2907 * glocal Forward P-value is too high (above pli->F4).
2908 *
2909 * In a normal pipeline run, this function call should be
2910 * just after a call to pli_p7_filter() and just
2911 * before a call to pli_cyk_env_filter().
2912 *
2913 * If pli->cur_pass_idx == PLI_PASS_HMM_ONLY_ANY,
2914 * <opt_esc> and <opt_ead> will be non-NULL and will be
2915 * filled with HMM hit scores and P7_ALIDISPLAYS
2916 * respectively, else they'll be NULL.
2917
2918 * Returns: <eslOK on success. For all <ret_nenv> envelopes not
2919 * filtered out, return their envelope boundaries in
2920 * <ret_es> and <ret_ee>.
2921 *
2922 * Throws: <eslEMEM> on allocation failure.
2923 * <eslENOTFOUND> if we need but don't have an HMM file to read
2924 * <eslESYS> on failure of system call when reading HMM
2925 */
2926 int
2927 pli_p7_env_def(CM_PIPELINE *pli, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, const ESL_SQ *sq, int64_t *ws, int64_t *we, int nwin,
2928 P7_HMM **opt_hmm, P7_PROFILE **opt_gm, P7_PROFILE **opt_Rgm, P7_PROFILE **opt_Lgm, P7_PROFILE **opt_Tgm, int64_t **ret_es, int64_t **ret_ee, float **ret_eb, int *ret_nenv)
2929 {
2930 int status;
2931 double P; /* P-value of a hit */
2932 int d, i; /* counters */
2933 void *p; /* for ESL_RALLOC */
2934 int env_len; /* envelope length */
2935 float env_sc; /* envelope bit score, and null1 score */
2936 float sc_for_pvalue; /* score for window, used for calc'ing P value */
2937 float env_edefbias; /* null2 correction for envelope */
2938 float env_sc_for_pvalue; /* corrected score for envelope, used for calc'ing P value */
2939 int64_t wlen; /* window length of current window */
2940 int64_t *es = NULL; /* [0..nenv-1] envelope start positions */
2941 int64_t *ee = NULL; /* [0..nenv-1] envelope end positions */
2942 float *eb = NULL; /* [0..nenv-1] envelope end positions */
2943 int nenv; /* number of surviving envelopes */
2944 int nenv_alloc; /* current size of es, ee */
2945 ESL_DSQ *subdsq; /* a ptr to the first position of a window */
2946 ESL_SQ *seq = NULL; /* a copy of a window */
2947 float nullsc, filtersc, fwdsc, bcksc;
2948 P7_PROFILE *gm = NULL; /* a ptr to *opt_gm, for convenience */
2949 int do_local_envdef; /* TRUE if we define envelopes with p7 in local mode, FALSE for glocal */
2950
2951 /* variables related to forcing first and/or final residue within truncated hits */
2952 P7_PROFILE *Rgm = NULL; /* a ptr to *Ropt_gm, for convenience */
2953 P7_PROFILE *Lgm = NULL; /* a ptr to *Lopt_gm, for convenience */
2954 P7_PROFILE *Tgm = NULL; /* a ptr to *Topt_gm, for convenience */
2955 float safe_lfwdsc; /* a score <= local Forward score, determined via a correction to a Forward score with Rgm or Lgm */
2956 int use_gm, use_Rgm, use_Lgm, use_Tgm; /* only one of these can be TRUE, should we use the standard profile for non
2957 * truncated hits or the specially configured T, R, or L profiles because
2958 * we're defining envelopes for hits possibly truncated 5', 3' or both 5' and 3'?
2959 */
2960 float Rgm_correction; /* nat score correction for windows and envelopes defined with Rgm */
2961 float Lgm_correction; /* nat score correction for windows and envelopes defined with Lgm */
2962
2963 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
2964 if (nwin == 0) {
2965 *ret_es = NULL;
2966 *ret_ee = NULL;
2967 *ret_eb = NULL;
2968 *ret_nenv = 0;
2969 return eslOK; /* if there's no windows to search in, return */
2970 }
2971
2972 /* Will we use local envelope definition? Only if we're in the
2973 * special pipeline pass where we allow any truncated hits (only
2974 * possibly true if pli->do_trunc_any, pli->do_trunc_int or pli->do_trunc_only is TRUE).
2975 */
2976 do_local_envdef = (pli->cur_pass_idx == PLI_PASS_5P_AND_3P_ANY) ? TRUE : FALSE;
2977
2978 nenv_alloc = nwin;
2979 ESL_ALLOC(es, sizeof(int64_t) * ESL_MAX(1, nenv_alloc)); // avoid 0 malloc
2980 ESL_ALLOC(ee, sizeof(int64_t) * ESL_MAX(1, nenv_alloc));
2981 ESL_ALLOC(eb, sizeof(float) * ESL_MAX(1, nenv_alloc));
2982 nenv = 0;
2983 seq = esl_sq_CreateDigital(sq->abc);
2984
2985 #if eslDEBUGLEVEL >= 2
2986 printf("#DEBUG:\n#DEBUG: PIPELINE p7EnvelopeDef() %s %" PRId64 " residues\n", sq->name, sq->n);
2987 #endif
2988
2989 /* determine which generic model we'll need to use based on which pass we're in */
2990 use_gm = use_Tgm = use_Rgm = use_Lgm = FALSE; /* one of these is set to TRUE below if nec */
2991 if(! do_local_envdef) {
2992 switch(pli->cur_pass_idx) {
2993 case PLI_PASS_STD_ANY: use_gm = TRUE; break;
2994 case PLI_PASS_5P_ONLY_FORCE: use_Rgm = TRUE; break;
2995 case PLI_PASS_3P_ONLY_FORCE: use_Lgm = TRUE; break;
2996 case PLI_PASS_5P_AND_3P_FORCE: use_Tgm = TRUE; break;
2997 default: ESL_FAIL(eslEINVAL, pli->errbuf, "pli_p7_env_def() invalid pass index");
2998 }
2999 }
3000
3001 /* If we're in SCAN mode and we don't yet have the generic model we
3002 * need, read the HMM and create it.
3003 */
3004 if (pli->mode == CM_SCAN_MODELS &&
3005 ((use_gm == TRUE && (*opt_gm) == NULL) ||
3006 (use_Rgm == TRUE && (*opt_Rgm) == NULL) ||
3007 (use_Lgm == TRUE && (*opt_Lgm) == NULL) ||
3008 (use_Tgm == TRUE && (*opt_Tgm) == NULL))) {
3009 if((*opt_hmm) == NULL) {
3010 /* read the HMM from the file */
3011 if (pli->cmfp == NULL) ESL_FAIL(eslENOTFOUND, pli->errbuf, "No file available to read HMM from in pli_p7_env_def()");
3012 if (pli->cmfp->hfp == NULL) ESL_FAIL(eslENOTFOUND, pli->errbuf, "No file available to read HMM from in pli_p7_env_def()");
3013 if((status = cm_p7_hmmfile_Read(pli->cmfp, pli->abc, om->offs[p7_MOFFSET], opt_hmm)) != eslOK) ESL_FAIL(status, pli->errbuf, "%s", pli->cmfp->errbuf);
3014 }
3015
3016 if((*opt_gm) == NULL) { /* we need gm to create Lgm, Rgm or Tgm */
3017 *opt_gm = p7_profile_Create((*opt_hmm)->M, pli->abc);
3018 p7_ProfileConfig(*opt_hmm, bg, *opt_gm, 100, p7_GLOCAL);
3019 }
3020 if(use_Rgm && (*opt_Rgm == NULL)) {
3021 *opt_Rgm = p7_profile_Clone(*opt_gm);
3022 p7_ProfileConfig5PrimeTrunc(*opt_Rgm, 100);
3023 }
3024 if(use_Lgm && (*opt_Lgm == NULL)) {
3025 *opt_Lgm = p7_profile_Clone(*opt_gm);
3026 p7_ProfileConfig3PrimeTrunc(*opt_hmm, *opt_Lgm, 100);
3027 }
3028 if(use_Tgm && (*opt_Tgm == NULL)) {
3029 *opt_Tgm = p7_profile_Clone(*opt_gm);
3030 p7_ProfileConfig(*opt_hmm, bg, *opt_Tgm, 100, p7_LOCAL);
3031 p7_ProfileConfig5PrimeAnd3PrimeTrunc(*opt_Tgm, 100);
3032 }
3033 }
3034 gm = *opt_gm;
3035 Rgm = *opt_Rgm;
3036 Lgm = *opt_Lgm;
3037 Tgm = *opt_Tgm;
3038
3039 for (i = 0; i < nwin; i++) {
3040 #if eslDEBUGLEVEL >= 2
3041 printf("#DEBUG: p7 envdef win: %4d of %4d [%6" PRId64 "..%6" PRId64 "] pass: %" PRId64 "\n", i, nwin, ws[i], we[i], pli->cur_pass_idx);
3042 #endif
3043 /* if we require first or final residue, and don't have it, then
3044 * this window doesn't survive.
3045 */
3046 if(cm_pli_PassEnforcesFirstRes(pli->cur_pass_idx) && ws[i] != 1) continue;
3047 if(cm_pli_PassEnforcesFinalRes(pli->cur_pass_idx) && we[i] != sq->n) continue;
3048
3049 wlen = we[i] - ws[i] + 1;
3050 subdsq = sq->dsq + ws[i] - 1;
3051
3052 /* set up seq object for domaindef function */
3053 esl_sq_GrowTo(seq, wlen);
3054 memcpy((void*)(seq->dsq), subdsq, (wlen+1) * sizeof(uint8_t));
3055 seq->dsq[0] = seq->dsq[wlen+1] = eslDSQ_SENTINEL;
3056 seq->n = wlen;
3057
3058 p7_bg_SetLength(bg, wlen);
3059 p7_bg_NullOne(bg, seq->dsq, wlen, &nullsc);
3060
3061 if(do_local_envdef) {
3062 /* Local envelope defn: we can use optimized matrices and,
3063 * consequently, p7_domaindef_ByPosteriorHeuristics().
3064 */
3065 p7_oprofile_ReconfigLength(om, wlen);
3066 p7_ForwardParser(seq->dsq, wlen, om, pli->oxf, NULL);
3067 p7_omx_GrowTo(pli->oxb, om->M, 0, wlen);
3068 p7_BackwardParser(seq->dsq, wlen, om, pli->oxf, pli->oxb, NULL);
3069 status = p7_domaindef_ByPosteriorHeuristics (seq, NULL, om, pli->oxf, pli->oxb, pli->fwd, pli->bck, pli->ddef, bg, /*long_target=*/FALSE,
3070 /*bg_tmp=*/NULL, /*scores_arr=*/NULL, /*fwd_emissions_arr=*/NULL);
3071 }
3072 else {
3073 /* We're defining envelopes in glocal mode, so we need to fill
3074 * generic fwd/bck matrices and pass them to
3075 * p7_domaindef_GlocalByPosteriorHeuristics(), but we have to do
3076 * this differently depending on which pass we're in
3077 * (i.e. which type of *gm we're using).
3078 */
3079 if(use_Tgm) {
3080 /* no length reconfiguration necessary */
3081 p7_gmx_GrowTo(pli->gxf, Tgm->M, wlen);
3082 p7_GForward (seq->dsq, wlen, Tgm, pli->gxf, &fwdsc);
3083 /*printf("Tfwdsc: %.4f\n", fwdsc);*/
3084 /* We use local Fwd statistics to determine statistical
3085 * significance of this score, it has already had basically a
3086 * 1/log(M*(M+1)) penalty for equiprobable local begins and
3087 * ends */
3088 sc_for_pvalue = (fwdsc - nullsc) / eslCONST_LOG2;
3089 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3090 }
3091 else if(use_Rgm) {
3092 p7_ReconfigLength5PrimeTrunc(Rgm, wlen);
3093 p7_gmx_GrowTo(pli->gxf, Rgm->M, wlen);
3094 p7_GForward (seq->dsq, wlen, Rgm, pli->gxf, &fwdsc);
3095 /*printf("Rfwdsc: %.4f\n", fwdsc);*/
3096 /* We use local Fwd statistics to determine significance of
3097 * the score. GForward penalized 0. for ends and log(1/Rgm->M)
3098 * for begins into any state. No further correction is
3099 * required because GForward already properly accounted for
3100 * equiprobable begins and fixed ends out of node M.
3101 */
3102 Rgm_correction = 0.;
3103 safe_lfwdsc = fwdsc + Rgm_correction;
3104 sc_for_pvalue = (safe_lfwdsc - nullsc) / eslCONST_LOG2;
3105 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3106 }
3107 else if(use_Lgm) {
3108 p7_ReconfigLength3PrimeTrunc(Lgm, wlen);
3109 p7_gmx_GrowTo(pli->gxf, Lgm->M, wlen);
3110 p7_GForward (seq->dsq, wlen, Lgm, pli->gxf, &fwdsc);
3111 /*printf("Lfwdsc: %.4f\n", fwdsc);*/
3112 /* We use local Fwd statistics to determine significance of
3113 * the score, but we need to correct for lack of equiprobable
3114 * begins and ends in fwdsc. We correct for fact that local
3115 * begins should be equiprobable.
3116 */
3117 Lgm_correction = log(1./Lgm->M);
3118 safe_lfwdsc = fwdsc + Lgm_correction;
3119 sc_for_pvalue = (safe_lfwdsc - nullsc) / eslCONST_LOG2;
3120 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3121 }
3122 else if(use_gm) { /* normal case, not looking for truncated hits */
3123 p7_ReconfigLength(gm, wlen);
3124 p7_gmx_GrowTo(pli->gxf, gm->M, wlen);
3125 p7_GForward (seq->dsq, wlen, gm, pli->gxf, &fwdsc);
3126 /*printf(" fwdsc: %.4f\n", fwdsc);*/
3127 sc_for_pvalue = (fwdsc - nullsc) / eslCONST_LOG2;
3128 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_GFMU], p7_evparam[CM_p7_GFLAMBDA]);
3129 }
3130
3131 #if eslDEBUGLEVEL >= 2
3132 if(P > pli->F4) {
3133 printf("#DEBUG: KILLED window %5d [%10" PRId64 "..%10" PRId64 "] gFwd %6.2f bits P %g\n", i, ws[i], we[i], sc_for_pvalue, P);
3134 }
3135 #endif
3136 /* Does this score exceed our glocal forward filter threshold? If not, move on to next seq */
3137 if(P > pli->F4) continue;
3138
3139 pli->acct[pli->cur_pass_idx].n_past_gfwd++;
3140 pli->acct[pli->cur_pass_idx].pos_past_gfwd += wlen;
3141
3142 #if eslDEBUGLEVEL >= 2
3143 printf("#DEBUG: SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived gFwd %6.2f bits P %g\n", i, ws[i], we[i], sc_for_pvalue, P);
3144 #endif
3145
3146 if(pli->do_gfwdbias) {
3147 /* calculate bias filter score for entire window */
3148 p7_bg_FilterScore(bg, seq->dsq, wlen, &filtersc);
3149 /* Once again, score and P-value determination depends on which
3150 * *gm we're using (see F4 code block above for comments).
3151 */
3152 if(use_Tgm) {
3153 sc_for_pvalue = (fwdsc - filtersc) / eslCONST_LOG2;
3154 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3155 }
3156 else if(use_Rgm || use_Lgm) {
3157 sc_for_pvalue = (safe_lfwdsc - nullsc) / eslCONST_LOG2;
3158 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3159 }
3160 else if(use_gm) { /* normal case */
3161 sc_for_pvalue = (fwdsc - filtersc) / eslCONST_LOG2;
3162 P = esl_exp_surv (sc_for_pvalue, p7_evparam[CM_p7_GFMU], p7_evparam[CM_p7_GFLAMBDA]);
3163 }
3164 if(P > pli->F4b) continue;
3165 #if eslDEBUGLEVEL >= 2
3166 printf("#DEBUG: SURVIVOR window %5d [%10" PRId64 "..%10" PRId64 "] survived gFwdBias %6.2f bits P %g\n", i, ws[i], we[i], sc_for_pvalue, P);
3167 #endif
3168 pli->acct[pli->cur_pass_idx].n_past_gfwdbias++;
3169 pli->acct[pli->cur_pass_idx].pos_past_gfwdbias += wlen;
3170 }
3171 if(pli->do_time_F4) continue;
3172 //if(pli->cur_pass_idx != PLI_PASS_STD_ANY) continue;
3173 //if(pli->cur_pass_idx != PLI_PASS_5P_ONLY_FORCE) continue;
3174 //if(pli->cur_pass_idx != PLI_PASS_3P_ONLY_FORCE) continue;
3175 //if(pli->cur_pass_idx != PLI_PASS_5P_AND_3P_FORCE) continue;
3176 //if(1) continue;
3177
3178 /* this block needs to match up with if..else if...else if...else block calling p7_GForward above */
3179 if(use_Tgm) {
3180 /* no length reconfiguration necessary */
3181 p7_gmx_GrowTo(pli->gxb, Tgm->M, wlen);
3182 p7_GBackward(seq->dsq, wlen, Tgm, pli->gxb, &bcksc);
3183 if((status = p7_domaindef_GlocalByPosteriorHeuristics(seq, Tgm, pli->gxf, pli->gxb, pli->gfwd, pli->gbck, pli->ddef, pli->do_null2)) != eslOK) ESL_FAIL(status, pli->errbuf, "unexpected failure during glocal envelope defn");
3184 /*printf("Tbcksc: %.4f\n", bcksc);*/
3185 }
3186 else if(use_Rgm) {
3187 p7_gmx_GrowTo(pli->gxb, Rgm->M, wlen);
3188 p7_GBackward(seq->dsq, wlen, Rgm, pli->gxb, &bcksc);
3189 if((status = p7_domaindef_GlocalByPosteriorHeuristics(seq, Rgm, pli->gxf, pli->gxb, pli->gfwd, pli->gbck, pli->ddef, pli->do_null2)) != eslOK) ESL_FAIL(status, pli->errbuf, "unexpected failure during glocal envelope defn");;
3190 /*printf("Rbcksc: %.4f\n", bcksc);*/
3191 }
3192 else if(use_Lgm) {
3193 p7_gmx_GrowTo(pli->gxb, Lgm->M, wlen);
3194 p7_GBackward(seq->dsq, wlen, Lgm, pli->gxb, &bcksc);
3195 if((status = p7_domaindef_GlocalByPosteriorHeuristics(seq, Lgm, pli->gxf, pli->gxb, pli->gfwd, pli->gbck, pli->ddef, pli->do_null2)) != eslOK) ESL_FAIL(status, pli->errbuf, "unexpected failure during glocal envelope defn");
3196 /*printf("Lbcksc: %.4f\n", bcksc);*/
3197 }
3198 else { /* normal case, not looking for truncated hits */
3199 p7_gmx_GrowTo(pli->gxb, gm->M, wlen);
3200 p7_GBackward(seq->dsq, wlen, gm, pli->gxb, &bcksc);
3201 if((status = p7_domaindef_GlocalByPosteriorHeuristics(seq, gm, pli->gxf, pli->gxb, pli->gfwd, pli->gbck, pli->ddef, pli->do_null2)) != eslOK) ESL_FAIL(status, pli->errbuf, "unexpected failure during glocal envelope defn");
3202 /*printf(" bcksc: %.4f\n", bcksc);*/
3203 }
3204 } /* end of 'else' entered if (! do_local_envdef) */
3205
3206 if (status != eslOK) ESL_FAIL(status, pli->errbuf, "envelope definition workflow failure"); /* eslERANGE can happen */
3207 if (pli->ddef->nregions == 0) continue; /* score passed threshold but there's no discrete domains here */
3208 if (pli->ddef->nenvelopes == 0) continue; /* rarer: region was found, stochastic clustered, no envelopes found */
3209
3210 /* For each domain found in the p7_domaindef_*() function, determine if it passes our criteria */
3211 for(d = 0; d < pli->ddef->ndom; d++) {
3212
3213 if(do_local_envdef) { /* we called p7_domaindef_ByPosteriorHeuristics() above, which fills pli->ddef->dcl[d].ad, but we don't need it */
3214 p7_alidisplay_Destroy(pli->ddef->dcl[d].ad);
3215 pli->ddef->dcl[d].ad = NULL;
3216 }
3217
3218 env_len = pli->ddef->dcl[d].jenv - pli->ddef->dcl[d].ienv +1;
3219 env_sc = pli->ddef->dcl[d].envsc;
3220
3221 /* Make a correction to the score
3222 * from hmmsearch's p7_pipeline():
3223 * here is the p7_pipeline code, verbatim:
3224 * Ld = hit->dcl[d].jenv - hit->dcl[d].ienv + 1;
3225 * hit->dcl[d].bitscore = hit->dcl[d].envsc + (sq->n-Ld) * log((float) sq->n / (float) (sq->n+3));
3226 * hit->dcl[d].dombias = (pli->do_null2 ? p7_FLogsum(0.0, log(bg->omega) + hit->dcl[d].domcorrection) : 0.0);
3227 * hit->dcl[d].bitscore = (hit->dcl[d].bitscore - (nullsc + hit->dcl[d].dombias)) / eslCONST_LOG2;
3228 * hit->dcl[d].pvalue = esl_exp_surv (hit->dcl[d].bitscore, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
3229 *
3230 * and here is the same code, simplified with our different names for the variables, etc
3231 * (we don't use hit->dcl the way p7_pipeline does after this): */
3232 env_sc = env_sc + (wlen - env_len) * log((float) wlen / (float) (wlen+3)); /* NATS, for the moment... */
3233 env_edefbias = pli->do_null2 ? p7_FLogsum(0.0, log(bg->omega) + pli->ddef->dcl[d].domcorrection) : 0.0; /* NATS, and will stay so */
3234 env_sc_for_pvalue = (env_sc - (nullsc + env_edefbias)) / eslCONST_LOG2; /* now BITS, as it should be */
3235
3236 if(use_Rgm) env_sc_for_pvalue += Rgm_correction / eslCONST_LOG2; /* glocal env def penalized 0. for ends and log(1/Lgm->M) for begins into any state */
3237 if(use_Lgm) env_sc_for_pvalue += Lgm_correction / eslCONST_LOG2; /* glocal env def penalized 0. for ends and 0. for begins into M1 */
3238
3239 if(do_local_envdef || use_Tgm || use_Rgm || use_Lgm) P = esl_exp_surv (env_sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3240 else P = esl_exp_surv (env_sc_for_pvalue, p7_evparam[CM_p7_GFMU], p7_evparam[CM_p7_GFLAMBDA]);
3241 /***************************************************/
3242
3243 /* check if we can skip this envelope based on its P-value */
3244 if(P > pli->F5) {
3245 if(pli->ddef->dcl[d].ad) p7_alidisplay_Destroy(pli->ddef->dcl[d].ad);
3246 continue;
3247 }
3248
3249 #if eslDEBUGLEVEL >= 2
3250 printf("#DEBUG: SURVIVOR envelope [%10" PRId64 "..%10" PRId64 "] survived F5 %6.2f bits P %g\n", pli->ddef->dcl[d].ienv + ws[i] - 1, pli->ddef->dcl[d].jenv + ws[i] - 1, env_sc_for_pvalue, P);
3251 #endif
3252 pli->acct[pli->cur_pass_idx].n_past_edef++;
3253 pli->acct[pli->cur_pass_idx].pos_past_edef += env_len;
3254
3255 /* if we're doing a bias filter on envelopes - check if we skip envelope due to that */
3256 if(pli->do_edefbias) {
3257 /* calculate bias filter score for entire window
3258 * may want to test alternative strategies in the future.
3259 */
3260 p7_bg_FilterScore(bg, seq->dsq, wlen, &filtersc);
3261 env_sc_for_pvalue = (env_sc - filtersc) / eslCONST_LOG2;
3262
3263 if(use_Rgm) env_sc_for_pvalue += Rgm_correction / eslCONST_LOG2; /* glocal env def penalized 0. for ends and log(1/Lgm->M) for begins into any state */
3264 if(use_Lgm) env_sc_for_pvalue += Lgm_correction / eslCONST_LOG2; /* glocal env def penalized 0. for ends and 0. for begins into M1 */
3265
3266 if(do_local_envdef || use_Tgm || use_Rgm || use_Lgm) P = esl_exp_surv (env_sc_for_pvalue, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
3267 else P = esl_exp_surv (env_sc_for_pvalue, p7_evparam[CM_p7_GFMU], p7_evparam[CM_p7_GFLAMBDA]);
3268 if(P > pli->F5b) {
3269 if(pli->ddef->dcl[d].ad) p7_alidisplay_Destroy(pli->ddef->dcl[d].ad);
3270 continue;
3271 }
3272 }
3273 #if eslDEBUGLEVEL >= 2
3274 printf("#DEBUG: SURVIVOR envelope [%10" PRId64 "..%10" PRId64 "] survived F5-bias %6.2f bits P %g\n", pli->ddef->dcl[d].ienv + ws[i] - 1, pli->ddef->dcl[d].jenv + ws[i] - 1, env_sc_for_pvalue, P);
3275 #endif
3276 pli->acct[pli->cur_pass_idx].n_past_edefbias++;
3277 pli->acct[pli->cur_pass_idx].pos_past_edefbias += env_len;
3278
3279 if(pli->do_time_F5) { continue; }
3280
3281 /* if we get here, the envelope has survived, add it to the growing list */
3282 if((nenv+1) == nenv_alloc) {
3283 nenv_alloc *= 2;
3284 ESL_RALLOC(es, p, sizeof(int64_t) * nenv_alloc);
3285 ESL_RALLOC(ee, p, sizeof(int64_t) * nenv_alloc);
3286 ESL_RALLOC(eb, p, sizeof(float) * nenv_alloc);
3287 }
3288 /* Define envelope to search with CM */
3289 es[nenv] = pli->ddef->dcl[d].ienv + ws[i] - 1;
3290 ee[nenv] = pli->ddef->dcl[d].jenv + ws[i] - 1;
3291 eb[nenv] = env_sc_for_pvalue;
3292 nenv++;
3293 }
3294
3295 pli->ddef->ndom = 0; /* reset for next use */
3296 }
3297
3298 /* clean up, set return variables, and return */
3299 if(seq != NULL) esl_sq_Destroy(seq);
3300
3301 *ret_es = es;
3302 *ret_ee = ee;
3303 *ret_eb = eb;
3304 *ret_nenv = nenv;
3305
3306 return eslOK;
3307
3308 ERROR:
3309 ESL_EXCEPTION(eslEMEM, "Error: out of memory");
3310 }
3311
3312 /* Function: pli_cyk_env_filter()
3313 * Synopsis: Given envelopes defined by an HMM, use CYK as a filter.
3314 * Incept: EPN, Thu Mar 1 12:03:09 2012
3315 *
3316 * Purpose: For each envelope x, from <es[x]>..<ee[x]>, run
3317 * CYK to see if any hits above threshold exist, if so
3318 * the hit will survive the filter.
3319 *
3320 * In a normal pipeline run, this function call should be
3321 * just after a call to pli_p7_env_def().
3322 *
3323 * This function is similar to pli_cyk_seq_filter(), but
3324 * differs in that it takes as input envelope boundaries
3325 * defined by an HMM as input, while cyk_seq_filter() takes
3326 * as input full length sequences that have not been
3327 * analyzed with an HMM.
3328 *
3329 * If pli->mode is CM_SCAN_MODELS, it's possible that we
3330 * haven't yet read our CM from the file. This is true when
3331 * (*opt_cm == NULL). If so, we read the CM from the file
3332 * after positioning it to position <cm_offset> and
3333 * configure the CM after setting cm->config_opts to
3334 * <pli->cm_config_opts>.
3335 *
3336 * Returns: <eslOK> on success. If a significant hit is obtained,
3337 * its information is added to the growing <hitlist>.
3338 *
3339 * <eslEINVAL> if (in a scan pipeline) we're supposed to
3340 * set GA/TC/NC bit score thresholds but the model doesn't
3341 * have any.
3342 *
3343 * Throws: <eslEMEM> on allocation failure.
3344 */
3345 int
3346 pli_cyk_env_filter(CM_PIPELINE *pli, off_t cm_offset, const ESL_SQ *sq, int64_t *p7es, int64_t *p7ee, int np7env, CM_t **opt_cm,
3347 int64_t **ret_es, int64_t **ret_ee, int *ret_nenv)
3348 {
3349 int status;
3350 float sc; /* bit score */
3351 double P; /* P-value of a hit */
3352 int i, si; /* counters */
3353 double save_tau; /* CM's tau upon entering function */
3354 int64_t cyk_envi, cyk_envj; /* cyk_envi..cyk_envj is new envelope as defined by CYK hits */
3355 float cyk_env_cutoff; /* bit score cutoff for envelope redefinition */
3356 CM_t *cm = NULL; /* ptr to *opt_cm, for convenience only */
3357 int qdbidx; /* scan matrix qdb idx, defined differently for filter and final round */
3358
3359 int *i_surv = NULL; /* [0..i..np7env-1], TRUE if hit i survived CYK filter, FALSE if not */
3360 int64_t nenv = 0; /* number of hits that survived CYK filter */
3361 int64_t *es = NULL; /* [0..si..nenv-1] start posn of surviving envelope si */
3362 int64_t *ee = NULL; /* [0..si..nenv-1] end posn of surviving envelope si */
3363 int enforce_i0; /* TRUE if first nt must be included in eventual parsetree */
3364 int enforce_j0; /* TRUE if final nt must be included in eventual parsetree */
3365
3366 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
3367 if (np7env == 0) return eslOK; /* if there's no envelopes to search in, return */
3368
3369 /* if we're in SCAN mode, and we don't yet have a CM, read it and configure it */
3370 if (pli->mode == CM_SCAN_MODELS && (*opt_cm == NULL)) {
3371 if((status = pli_scan_mode_read_cm(pli, cm_offset,
3372 NULL, 0, /* p7_evparam, p7_max_length: irrelevant because pli->do_hmmonly_cur is FALSE */
3373 opt_cm)) != eslOK) return status;
3374 }
3375 else { /* *opt_cm should be valid */
3376 if(opt_cm == NULL || *opt_cm == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "Entered pli_final_stage() with invalid CM");
3377 }
3378 cm = *opt_cm;
3379 save_tau = cm->tau;
3380
3381 enforce_i0 = cm_pli_PassEnforcesFirstRes(pli->cur_pass_idx) ? TRUE : FALSE;
3382 enforce_j0 = cm_pli_PassEnforcesFinalRes(pli->cur_pass_idx) ? TRUE : FALSE;
3383
3384 ESL_ALLOC(i_surv, sizeof(int) * np7env);
3385 esl_vec_ISet(i_surv, np7env, FALSE);
3386
3387 /* Determine bit score cutoff for CYK envelope redefinition, any
3388 * residue that exists in a CYK hit that reaches this threshold will
3389 * be included in the redefined envelope, any that doesn't will not
3390 * be.
3391 */
3392 cyk_env_cutoff = cm->expA[pli->fcyk_cm_exp_mode]->mu_extrap + (log(pli->F6env) / (-1 * cm->expA[pli->fcyk_cm_exp_mode]->lambda));
3393
3394 #if eslDEBUGLEVEL >= 2
3395 printf("#DEBUG:\n#DEBUG: PIPELINE EnvCYKFilter() %s %" PRId64 " residues\n", sq->name, sq->n);
3396 #endif
3397
3398 for (i = 0; i < np7env; i++) {
3399 #if eslDEBUGLEVEL >= 2
3400 printf("#DEBUG:\n#DEBUG: SURVIVOR Envelope %5d [%10ld..%10ld] being passed to EnvCYKFilter pass: %" PRId64 "\n", i, p7es[i], p7ee[i], pli->cur_pass_idx);
3401 #endif
3402 cm->search_opts = pli->fcyk_cm_search_opts;
3403 cm->tau = pli->fcyk_tau;
3404 qdbidx = (cm->search_opts & CM_SEARCH_NONBANDED) ? SMX_NOQDB : SMX_QDB1_TIGHT;
3405 status = pli_dispatch_cm_search(pli, cm, sq->dsq, p7es[i], p7ee[i], NULL, 0., cyk_env_cutoff, qdbidx, &sc,
3406 (pli->do_fcykenv) ? &cyk_envi : NULL,
3407 (pli->do_fcykenv) ? &cyk_envj : NULL);
3408
3409 if(status == eslERANGE) {
3410 pli->acct[pli->cur_pass_idx].n_overflow_fcyk++;
3411 continue; /* skip envelopes that would require too big of a HMM banded matrix */
3412 }
3413 else if(status != eslOK) return status;
3414
3415 P = esl_exp_surv(sc, cm->expA[pli->fcyk_cm_exp_mode]->mu_extrap, cm->expA[pli->fcyk_cm_exp_mode]->lambda);
3416
3417 if (P > pli->F6) continue;
3418
3419 i_surv[i] = TRUE;
3420 nenv++;
3421 /* update envelope boundaries, if nec */
3422 if(pli->do_fcykenv && (cyk_envi != -1 && cyk_envj != -1)) {
3423 if(! enforce_i0) { p7es[i] = cyk_envi; }
3424 if(! enforce_j0) { p7ee[i] = cyk_envj; }
3425 }
3426
3427 #if eslDEBUGLEVEL >= 2
3428 printf("#DEBUG: SURVIVOR envelope [%10" PRId64 "..%10" PRId64 "] survived EnvCYKFilter %6.2f bits P %g\n", p7es[i], p7ee[i], sc, P);
3429 #endif
3430 }
3431 /* create list of surviving envelopes */
3432 if(nenv > 0) {
3433 ESL_ALLOC(es, sizeof(int64_t) * nenv);
3434 ESL_ALLOC(ee, sizeof(int64_t) * nenv);
3435 si = 0;
3436 for(i = 0; i < np7env; i++) {
3437 if(i_surv[i]) {
3438 es[si] = p7es[i];
3439 ee[si] = p7ee[i];
3440 pli->acct[pli->cur_pass_idx].n_past_cyk++;
3441 pli->acct[pli->cur_pass_idx].pos_past_cyk += ee[si] - es[si] + 1;
3442 si++;
3443 }
3444 }
3445 }
3446 cm->tau = save_tau;
3447 if(i_surv != NULL) free(i_surv);
3448 *ret_es = es;
3449 *ret_ee = ee;
3450 *ret_nenv = nenv;
3451
3452 return eslOK;
3453
3454 ERROR:
3455 cm->tau = save_tau;
3456 if(i_surv != NULL) free(i_surv);
3457 if(es != NULL) free(es);
3458 if(ee != NULL) free(ee);
3459 *ret_es = NULL;
3460 *ret_ee = NULL;
3461 *ret_nenv = 0;
3462 ESL_FAIL(status, pli->errbuf, "out of memory");
3463 }
3464
3465
3466 /* Function: pli_cyk_seq_filter()
3467 * Synopsis: Given a sequence, use CYK as a filter and to define
3468 * surviving windows.
3469 *
3470 * Incept: EPN, Thu Mar 1 12:03:09 2012
3471 *
3472 * Purpose: Run scanning CYK to see if any hits in <dsq> above
3473 * threshold exist. Then append adjacent residues to
3474 * all such hits, merge those that overlap, and return
3475 * information on the number of resulting windows and
3476 * the locations of those windows in <ret_nwin>, <ret_ws>
3477 * and <ret_we>.
3478 *
3479 * This function is only called in a pipeline run if
3480 * HMMs were not used to define envelopes, so when used
3481 * it is the first stage of the pipeline.
3482 *
3483 * This function is similar to pli_cyk_env_filter(), but
3484 * differs in that it takes as input a single full length
3485 * sequences, while EnvCYKFilter takes as input envelopes
3486 * defined by an HMM filter.
3487 *
3488 * If pli->mode is CM_SCAN_MODELS, it's possible that we
3489 * haven't yet read our CM from the file. This is true when
3490 * (*opt_cm == NULL). If so, we read the CM from the file
3491 * after positioning it to position <cm_offset> and
3492 * configure the CM after setting cm->config_opts to
3493 * <pli->cm_config_opts>.
3494 *
3495 * Returns: <eslOK> on success. If a significant hit is obtained,
3496 * its information is added to the growing <hitlist>.
3497 *
3498 * <eslEINVAL> if (in a scan pipeline) we're supposed to
3499 * set GA/TC/NC bit score thresholds but the model doesn't
3500 * have any.
3501 *
3502 * Throws: <eslEMEM> on allocation failure.
3503 */
3504 int
3505 pli_cyk_seq_filter(CM_PIPELINE *pli, off_t cm_offset, const ESL_SQ *sq, CM_t **opt_cm, int64_t **ret_ws, int64_t **ret_we, int *ret_nwin)
3506 {
3507 int status;
3508 float sc; /* bit score */
3509 double save_tau; /* CM's tau upon entering function */
3510 float cutoff; /* CYK bit score cutoff, anything above this survives */
3511 CM_t *cm = NULL; /* ptr to *opt_cm, for convenience only */
3512 int qdbidx; /* scan matrix qdb idx, defined differently for filter and final round */
3513 CM_TOPHITS *sq_hitlist = NULL; /* hits found in sq, local to this function */
3514 int do_merge; /* TRUE to merge overlapping windows at end of function */
3515 int h; /* counter over hits */
3516
3517 int64_t nwin = 0; /* number of windows that survived CYK filter */
3518 int64_t *ws = NULL; /* [0..i..nwin-1] start posn of surviving window i */
3519 int64_t *we = NULL; /* [0..i..nwin-1] end posn of surviving window i */
3520 int alloc_size = 1000; /* chunk size for allocating ws, we */
3521 int nwin_alloc = 0; /* current size of ws, we */
3522 int64_t iwin, jwin; /* start, stop positions of a window */
3523 int64_t next_iwin; /* start position of next window */
3524
3525 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
3526
3527 if(pli->fcyk_cm_search_opts & CM_SEARCH_HBANDED) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "pli_cyk_seq_filter() trying to use HMM bands");
3528
3529 /* if we're in SCAN mode, and we don't yet have a CM, read it and configure it */
3530 if (pli->mode == CM_SCAN_MODELS && (*opt_cm == NULL)) {
3531 if((status = pli_scan_mode_read_cm(pli, cm_offset,
3532 NULL, 0, /* p7_evparam, p7_max_length: irrelevant because pli->do_hmmonly_cur is FALSE */
3533 opt_cm)) != eslOK) return status;
3534 }
3535 else { /* *opt_cm should be valid */
3536 if(opt_cm == NULL || *opt_cm == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "Entered pli_final_stage() with invalid CM");
3537 }
3538 cm = *opt_cm;
3539
3540 cm->search_opts = pli->fcyk_cm_search_opts;
3541 save_tau = cm->tau;
3542 cm->tau = pli->fcyk_tau;
3543 qdbidx = (cm->search_opts & CM_SEARCH_NONBANDED) ? SMX_NOQDB : SMX_QDB1_TIGHT;
3544 cutoff = cm->expA[pli->fcyk_cm_exp_mode]->mu_extrap + (log(pli->F6) / (-1 * cm->expA[pli->fcyk_cm_exp_mode]->lambda));
3545 sq_hitlist = cm_tophits_Create();
3546 status = pli_dispatch_cm_search(pli, cm, sq->dsq, 1, sq->n, sq_hitlist, cutoff, 0., qdbidx, &sc, NULL, NULL);
3547 if(status == eslERANGE) ESL_FAIL(status, pli->errbuf, "pli_cyk_seq_filter(), internal error, trying to use a HMM banded matrix");
3548 else if(status != eslOK) return status;
3549
3550 /* To be safe, we only trust that start..stop of our filter-passing
3551 * hit is within the real hit, so we add (W-1) to start point i and
3552 * subtract (W-1) from j, and treat this region j-(W-1)..i+(W-1) as
3553 * having survived the filter. And (unless we're using HMM bands in
3554 * the final stage) we merge overlapping hits following this
3555 * addition of residues.
3556 */
3557
3558 do_merge = (pli->final_cm_search_opts & CM_SEARCH_HBANDED) ? FALSE : TRUE;
3559 if(do_merge) {
3560 /* sort hits by position, so we can merge them after padding out */
3561 cm_tophits_SortByPosition(sq_hitlist);
3562 /* any hits in sq_hitlist will be sorted by increasing end point j */
3563 /* cm_tophits_Dump(stdout, sq_hitlist); */
3564 }
3565
3566 for(h = 0; h < sq_hitlist->N; h++) {
3567 if(sq_hitlist->hit[h]->stop < sq_hitlist->hit[h]->start) ESL_FAIL(eslEINVAL, pli->errbuf, "pli_cyk_seq_filter() internal error: hit is in revcomp");
3568
3569 iwin = ESL_MAX(1, sq_hitlist->hit[h]->stop - (cm->W-1));
3570 jwin = ESL_MIN(sq->n, sq_hitlist->hit[h]->start + (cm->W-1));
3571
3572 #if eslDEBUGLEVEL >= 2
3573 double P = esl_exp_surv(sq_hitlist->hit[h]->score, cm->expA[pli->fcyk_cm_exp_mode]->mu_extrap, cm->expA[pli->fcyk_cm_exp_mode]->lambda);
3574 printf("#DEBUG: SURVIVOR window [%10" PRId64 "..%10" PRId64 "] survived SeqCYKFilter %6.2f bits P %g\n", iwin, jwin, sq_hitlist->hit[h]->score, P);
3575 #endif
3576
3577 if(do_merge) {
3578 if((h+1) < sq_hitlist->N) {
3579 next_iwin = ((h+1) < sq_hitlist->N) ? ESL_MAX(1, sq_hitlist->hit[h+1]->stop - (cm->W-1)) : sq->n+1;
3580 while(next_iwin <= jwin) { /* merge hit h and h+1 */
3581 h++;
3582 jwin = ESL_MIN(sq->n, sq_hitlist->hit[h]->start + (cm->W-1));
3583 /* printf("\t merging with hit %" PRId64 "..%" PRId64 "\n", next_iwin, jwin); */
3584 next_iwin = ((h+1) < sq_hitlist->N) ? ESL_MAX(1, sq_hitlist->hit[h+1]->stop - (cm->W-1)) : sq->n+1;
3585 /* if (h == tophits->N-1) (last hit) next_i will be set as sq->n+1, breaking the while() */
3586 }
3587 }
3588 }
3589
3590 if(nwin == nwin_alloc) {
3591 nwin_alloc += alloc_size;
3592 ESL_REALLOC(ws, sizeof(int64_t) * nwin_alloc);
3593 ESL_REALLOC(we, sizeof(int64_t) * nwin_alloc);
3594 }
3595 ws[nwin] = iwin;
3596 we[nwin] = jwin;
3597 nwin++;
3598
3599 pli->acct[pli->cur_pass_idx].n_past_cyk++;
3600 pli->acct[pli->cur_pass_idx].pos_past_cyk += jwin-iwin+1;
3601 }
3602 cm->tau = save_tau;
3603
3604 if(sq_hitlist != NULL) cm_tophits_Destroy(sq_hitlist);
3605 *ret_ws = ws;
3606 *ret_we = we;
3607 *ret_nwin = nwin;
3608
3609 return eslOK;
3610
3611 ERROR:
3612 if(sq_hitlist != NULL) cm_tophits_Destroy(sq_hitlist);
3613 cm->tau = save_tau;
3614 *ret_ws = NULL;
3615 *ret_we = NULL;
3616 *ret_nwin = 0;
3617 ESL_FAIL(status, pli->errbuf, "out of memory");
3618 }
3619
3620 /* Function: pli_final_stage()
3621 * Synopsis: Final stage of pipeline: Inside or CYK.
3622 * Incept: EPN, Sun Nov 28 13:47:31 2010
3623 *
3624 * Purpose: For each envelope x, from <es[x]>..<ee[x]>, run
3625 * Inside or CYK for final hit definition.
3626 *
3627 * In a normal pipeline run, this function call should be
3628 * just after a call to pli_cyk_env_filter().
3629 *
3630 * If pli->mode is CM_SCAN_MODELS, it's possible that we
3631 * haven't yet read our CM from the file. This is true when
3632 * (*opt_cm == NULL). If so, we read the CM from the file
3633 * after positioning it to position <cm_offset> and
3634 * configure the CM after setting cm->config_opts to
3635 * <pli->cm_config_opts>.
3636 *
3637 * Returns: <eslOK> on success. If a significant hit is obtained,
3638 * its information is added to the growing <hitlist>.
3639 *
3640 * <eslEINVAL> if (in a scan pipeline) we're supposed to
3641 * set GA/TC/NC bit score thresholds but the model doesn't
3642 * have any.
3643 *
3644 * Throws: <eslEMEM> on allocation failure.
3645 */
3646 int
3647 pli_final_stage(CM_PIPELINE *pli, off_t cm_offset, const ESL_SQ *sq, int64_t *es, int64_t *ee, int nenv, CM_TOPHITS *hitlist, CM_t **opt_cm)
3648 {
3649 int status;
3650 CM_HIT *hit = NULL; /* ptr to the current hit output data */
3651 float sc; /* bit score */
3652 int i, h; /* counters */
3653 int nhit; /* number of hits reported */
3654 double save_tau; /* CM's tau upon entering function */
3655 CM_t *cm = NULL; /* ptr to *opt_cm, for convenience only */
3656 CP9Bands_t *scan_cp9b = NULL; /* a copy of the HMM bands derived in the final CM search stage, if its HMM banded */
3657 int qdbidx; /* scan matrix qdb idx, defined differently for filter and final round */
3658
3659 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
3660 if (nenv == 0) return eslOK; /* if there's no envelopes to search in, return */
3661
3662 /* if we're in SCAN mode, and we don't yet have a CM, read it and configure it */
3663 if (pli->mode == CM_SCAN_MODELS && (*opt_cm == NULL)) {
3664 if((status = pli_scan_mode_read_cm(pli, cm_offset,
3665 NULL, 0, /* p7_evparam, p7_max_length: irrelevant because pli->do_hmmonly_cur is FALSE */
3666 opt_cm)) != eslOK) return status;
3667 }
3668 else { /* *opt_cm should be valid */
3669 if(opt_cm == NULL || *opt_cm == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "Entered pli_final_stage() with invalid CM");
3670 }
3671 cm = *opt_cm;
3672 save_tau = cm->tau;
3673
3674 for (i = 0; i < nenv; i++) {
3675 #if eslDEBUGLEVEL >= 2
3676 printf("#DEBUG:\n#DEBUG: SURVIVOR Envelope %5d [%10ld..%10ld] being passed to Final stage pass: %" PRId64 "\n", i, es[i], ee[i], pli->cur_pass_idx);
3677 #endif
3678 nhit = hitlist->N;
3679 cm->search_opts = pli->final_cm_search_opts;
3680 cm->tau = pli->final_tau;
3681 qdbidx = (cm->search_opts & CM_SEARCH_NONBANDED) ? SMX_NOQDB : SMX_QDB2_LOOSE;
3682 status = pli_dispatch_cm_search(pli, cm, sq->dsq, es[i], ee[i], hitlist, pli->T, 0., qdbidx, &sc, NULL, NULL);
3683 if(status == eslERANGE) {
3684 pli->acct[pli->cur_pass_idx].n_overflow_final++;
3685 continue; /* skip envelopes that would require too big a HMM banded matrix */
3686 }
3687 else if(status != eslOK) return status;
3688
3689 /* if we used HMM bands during the final search stage, save a copy
3690 * of them, we'll use it to align any hits above threshold below.
3691 */
3692 if(cm->search_opts & CM_SEARCH_HBANDED) {
3693 scan_cp9b = cp9_CloneBands(cm->cp9b, pli->errbuf);
3694 if(scan_cp9b == NULL) return eslEMEM;
3695 #if eslDEBUGLEVEL >= 1
3696 if((status = cp9_ValidateBands(cm, pli->errbuf, cm->cp9b, es[i], ee[i], cm_pli_PassAllowsTruncation(pli->cur_pass_idx))) != eslOK) return status;
3697 ESL_DPRINTF1(("#DEBUG: original bands validated.\n"));
3698 if((status = cp9_ValidateBands(cm, pli->errbuf, scan_cp9b, es[i], ee[i], cm_pli_PassAllowsTruncation(pli->cur_pass_idx))) != eslOK) return status;
3699 ESL_DPRINTF1(("#DEBUG: cloned bands validated.\n"));
3700 #endif
3701 }
3702 else {
3703 scan_cp9b = NULL;
3704 }
3705
3706 /* add info to each hit DP scanning functions didn't have access to, and align the hits if nec */
3707 for (h = nhit; h < hitlist->N; h++) {
3708 hit = &(hitlist->unsrt[h]);
3709 hit->cm_idx = pli->cur_cm_idx;
3710 hit->clan_idx = pli->cur_clan_idx;
3711 hit->seq_idx = pli->cur_seq_idx;
3712 hit->pass_idx = pli->cur_pass_idx;
3713 hit->pvalue = esl_exp_surv(hit->score, cm->expA[pli->final_cm_exp_mode]->mu_extrap, cm->expA[pli->final_cm_exp_mode]->lambda);
3714 hit->srcL = sq->L; /* this may be -1, in which case it will be updated by caller (cmsearch or cmscan) when full length is known */
3715 hit->glocal = (pli->final_cm_exp_mode == EXP_CM_GI || pli->final_cm_exp_mode == EXP_CM_GC) ? TRUE : FALSE;
3716
3717 /* initialize remaining values we don't know yet */
3718 hit->evalue = 0.;
3719 hit->has_evalue = FALSE;
3720 hit->ad = NULL;
3721
3722 if (pli->mode == CM_SEARCH_SEQS) {
3723 if ( (status = esl_strdup(sq->name, -1, &(hit->name))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
3724 if (sq->acc[0] != '\0' && (status = esl_strdup(sq->acc, -1, &(hit->acc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
3725 if (sq->desc[0] != '\0' && (status = esl_strdup(sq->desc, -1, &(hit->desc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
3726 }
3727 else {
3728 if ((status = esl_strdup(cm->name, -1, &(hit->name))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
3729 if ((status = esl_strdup(cm->acc, -1, &(hit->acc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
3730 if ((status = esl_strdup(cm->desc, -1, &(hit->desc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
3731 }
3732 #if eslDEBUGLEVEL >= 2
3733 printf("#DEBUG: SURVIVOR envelope [%10ld..%10ld] survived Inside %6.2f bits P %g\n", hit->start, hit->stop, hit->score, hit->pvalue);
3734 #endif
3735 /* Get an alignment of the hit.
3736 */
3737 /* check if we need to overwrite cm->cp9b with scan_cp9b
3738 * because alignment of previous hit modified them in previous
3739 * call to pli_align_hit().
3740 */
3741 if(h > nhit && scan_cp9b != NULL) {
3742 if(cm->cp9b != NULL) FreeCP9Bands(cm->cp9b);
3743 cm->cp9b = cp9_CloneBands(scan_cp9b, pli->errbuf);
3744 if(cm->cp9b == NULL) return status;
3745 }
3746 /*cm_hit_Dump(stdout, hit);*/
3747
3748 /* Before we create the hit alignment, we do a sanity check. If
3749 * we used HMM bands in the final search stage, the hit
3750 * alignment pli_align_hit() is about to do is expected to use
3751 * those bands (they're in cm->cp9b). This should be true based
3752 * on how the cm_pipeline_Create() function created the pipeline
3753 * object. But since that's a complicated function thanks to the
3754 * litany of command-line options that affect it, we do a check
3755 * here to make sure.
3756 */
3757 if(scan_cp9b != NULL && (! (pli->cm_align_opts & CM_ALIGN_HBANDED))) {
3758 ESL_FAIL(eslEINVAL, pli->errbuf, "used HMM bands for Inside search stage, but won't for hit alignment, this shouldn't happen");
3759 }
3760 if(scan_cp9b == NULL && (pli->cm_align_opts & CM_ALIGN_HBANDED)) {
3761 ESL_FAIL(eslEINVAL, pli->errbuf, "did not use HMM bands for Inside search stage, but will for hit alignment, this shouldn't happen");
3762 }
3763 if((status = pli_align_hit(pli, cm, sq, hit)) != eslOK) return status;
3764
3765 /* Finally, if we're using model-specific bit score thresholds,
3766 * determine if the significance of the hit (is it reported
3767 * and/or included?) Adapted from Sean's comments at an
3768 * analogous point in p7_pipeline():
3769 *
3770 * If we're using model-specific bit score thresholds (GA | TC | NC)
3771 * and we're in a cmscan pipeline (mode = CM_SCAN_MODELS), then we
3772 * *must* apply those reporting or inclusion thresholds now, because
3773 * this model is about to go away; we won't have its thresholds
3774 * after all targets have been processed.
3775 *
3776 * If we're using E-value thresholds and we don't know the
3777 * search space size (Z_setby == CM_ZSETBY_NTARGETS), we
3778 * *cannot* apply those thresholds now, and we *must* wait
3779 * until all targets have been processed (see cm_tophits_Threshold()).
3780 *
3781 * For any other thresholding, it doesn't matter whether we do
3782 * it here (model-specifically) or at the end (in
3783 * cm_tophits_Threshold()).
3784 *
3785 * What we actually do, then, is to set the flags if we're using
3786 * model-specific score thresholds (regardless of whether we're
3787 * in a scan or a search pipeline); otherwise we leave it to
3788 * cm_tophits_Threshold(). cm_tophits_Threshold() is always
3789 * responsible for *counting* the reported, included sequences.
3790 *
3791 * [xref J5/92]
3792 */
3793
3794 if (pli->use_bit_cutoffs) {
3795 if (cm_pli_TargetReportable(pli, hit->score, hit->evalue)) { /* evalue is invalid, but irrelevant if pli->use_bit_cutoffs */
3796 hit->flags |= CM_HIT_IS_REPORTED;
3797 if (cm_pli_TargetIncludable(pli, hit->score, hit->evalue)) /* ditto */
3798 hit->flags |= CM_HIT_IS_INCLUDED;
3799 }
3800 }
3801 } /* end of 'for(h = nhit'... */
3802 if(scan_cp9b != NULL) {
3803 FreeCP9Bands(scan_cp9b);
3804 scan_cp9b = NULL;
3805 }
3806 } /* end of for each envelope loop */
3807 cm->tau = save_tau;
3808 /* free the scan matrices if we just allocated them */
3809 if(scan_cp9b != NULL) FreeCP9Bands(scan_cp9b);
3810
3811 return eslOK;
3812 }
3813
3814 /* Function: pli_final_stage_hmmonly()
3815 * Synopsis: Final stage of HMM only pipeline.
3816 * Incept: EPN, Sun Nov 28 13:47:31 2010
3817 *
3818 * Purpose: Each envelope x <es[x]>..<ee[x]> is a potential
3819 * HMM hit with score <esc[x]> and P7_ALIDISPLAY <ead[x]>.
3820 * Check if we should include it, and if so create a
3821 * CM_HIT for it, and convert the P7_ALIDISPLAY to
3822 * a CM_ALIDISPLAY.
3823 *
3824 * This function is only called if we're doing a special
3825 * HMM only version of the pipeline (i.e. pli->do_hmmonly
3826 * is TRUE).
3827 *
3828 * Returns: <eslOK> on success. If a significant hit is obtained,
3829 * its information is added to the growing <hitlist>.
3830 *
3831 * <eslEINVAL> if (in a scan pipeline) we're supposed to
3832 * set GA/TC/NC bit score thresholds but the model doesn't
3833 * have any.
3834 *
3835 * Throws: <eslEMEM> on allocation failure.
3836 */
3837 int
3838 pli_final_stage_hmmonly(CM_PIPELINE *pli, off_t cm_offset, P7_OPROFILE *om, P7_BG *bg, float *p7_evparam, const ESL_SQ *sq, int64_t *ws, int64_t *we, int nwin, CM_TOPHITS *hitlist, CM_t **opt_cm)
3839 {
3840 int status;
3841 int i, d; /* counter over windows, domains */
3842 CM_HIT *hit = NULL; /* ptr to the current hit output data */
3843 CM_t *cm = NULL; /* ptr to *opt_cm, for convenience only */
3844 ESL_DSQ *subdsq; /* a ptr to the first position of a window */
3845 ESL_SQ *seq = NULL; /* a copy of a window */
3846 int64_t wlen; /* window length of current window */
3847 int64_t prv_sqto; /* stop position of previous hit, within window, for checking for overlaps */
3848 int env_len; /* envelope length */
3849 float nullsc; /* null model score */
3850 float avgpp; /* average PP of emitted residues in a P7_ALIDISPLAY */
3851
3852 /* variables necessary only for bit score correction if
3853 * pli->cur_pass_idx = PLI_PASS_HMM_ONLY_ANY that is analogous to
3854 * one nhmmer performs, (search for 'nhmmer score adjustment' in
3855 * comments below). I've named this variables identically to
3856 * analogous ones in p7_pipeline.c::postMSV_LongTarget() (as of
3857 * r3976).
3858 */
3859 int ali_len;
3860 float bitscore;
3861 int loc_window_length;
3862 int window_len;
3863 float nullsc2;
3864 float dom_bias;
3865 float dom_score;
3866
3867 /* variables necessary for researching one of two overlapping windows
3868 * in case that p7_domaindef_ByPosteriorHeuristics() returns overlapping
3869 * domains
3870 */
3871 int64_t *rerun_ws = NULL; /* [0..i..rerun_nwin-1], start positions in sq for window i to rerun */
3872 int64_t *rerun_we = NULL; /* [0..i..rerun_nwin-1], end positions in sq for window i to rerun */
3873 int64_t cur_rerun_ws; /* current start position for a window to rerun */
3874 int64_t cur_rerun_we; /* current end position for a window to rerun */
3875 int rerun_nwin = 0; /* number of windows to rerun */
3876 int rerun_nalloc = 0; /* current allocated size for rerun_ws and rerun_we */
3877
3878 if (sq->n == 0) return eslOK; /* silently skip length 0 seqs; they'd cause us all sorts of weird problems */
3879 if (nwin == 0) return eslOK; /* if there's no windows, return */
3880 seq = esl_sq_CreateDigital(sq->abc);
3881
3882 /* if we're in SCAN mode, and we don't yet have a CM, read it and configure it */
3883 if (pli->mode == CM_SCAN_MODELS && (*opt_cm == NULL)) {
3884 if((status = pli_scan_mode_read_cm(pli, cm_offset, p7_evparam, om->max_length, opt_cm)) != eslOK) return status;
3885 }
3886 else { /* *opt_cm should be valid */
3887 if(opt_cm == NULL || *opt_cm == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "Entered pli_final_stage() with invalid CM");
3888 }
3889 cm = *opt_cm;
3890
3891 /* calculate nullsc2, this mimics nhmmer from
3892 * p7_pipeline:postMSV_LongTarget(), as of r3976.
3893 */
3894 loc_window_length = om->max_length;
3895 nullsc2 = (float)loc_window_length * log((float)loc_window_length/(loc_window_length+1)) + log(1./(loc_window_length+1));
3896
3897 for (i = 0; i < nwin; i++) {
3898 #if eslDEBUGLEVEL >= 2
3899 printf("#DEBUG: p7 final stage win: %4d of %4d [%6" PRId64 "..%6" PRId64 "] pass: %" PRId64 "\n", i, nwin, ws[i], we[i], pli->cur_pass_idx);
3900 #endif
3901
3902 wlen = we[i] - ws[i] + 1;
3903 subdsq = sq->dsq + ws[i] - 1;
3904 /* set up seq object for domaindef function */
3905 esl_sq_GrowTo(seq, wlen);
3906 memcpy((void*)(seq->dsq), subdsq, (wlen+1) * sizeof(uint8_t));
3907 seq->dsq[0] = seq->dsq[wlen+1] = eslDSQ_SENTINEL;
3908 seq->n = wlen;
3909
3910 p7_bg_SetLength(bg, wlen);
3911 p7_bg_NullOne(bg, seq->dsq, wlen, &nullsc);
3912
3913 /* Local envelope defn: we can use optimized matrices and,
3914 * consequently, p7_domaindef_ByPosteriorHeuristics().
3915 */
3916 p7_oprofile_ReconfigLength(om, wlen);
3917 p7_ForwardParser(seq->dsq, wlen, om, pli->oxf, NULL);
3918 p7_omx_GrowTo(pli->oxb, om->M, 0, wlen);
3919 p7_BackwardParser(seq->dsq, wlen, om, pli->oxf, pli->oxb, NULL);
3920 status = p7_domaindef_ByPosteriorHeuristics (seq, /*nt_seq=*/NULL, om, pli->oxf, pli->oxb, pli->fwd, pli->bck, pli->ddef, bg, /*long_target=*/FALSE,
3921 /*bg_tmp=*/NULL, /*scores_arr=*/NULL, /*fwd_emissions_arr=*/NULL);
3922
3923 if (status != eslOK) ESL_FAIL(status, pli->errbuf, "envelope definition workflow failure"); /* eslERANGE can happen */
3924 if (pli->ddef->nregions == 0) continue; /* score passed threshold but there's no discrete domains here */
3925 if (pli->ddef->nenvelopes == 0) continue; /* rarer: region was found, stochastic clustered, no envelopes found */
3926
3927 /* For each domain found in the p7_domaindef_*() function, determine if it passes our criteria */
3928 for(d = 0; d < pli->ddef->ndom; d++) {
3929 /* check if this domain overlaps with the previous one, if so
3930 * recursively call pli_final_stage_hmmonly with the
3931 * next window only, this prevents any overlapping
3932 * residues between HMM hits, which is important because overlap
3933 * removal code doesn't tolerate any overlapping nucleotides
3934 * (because CM pipeline implementations do not allow them)
3935 */
3936 if((d > 0) && (pli->ddef->dcl[d].ad->sqfrom <= prv_sqto)) {
3937 /* overlap of >= 1 positions */
3938 cur_rerun_ws = prv_sqto + ws[i] - 1 + 1;
3939 cur_rerun_we = pli->ddef->dcl[d].ad->sqto + ws[i] - 1;
3940 if(cur_rerun_ws <= cur_rerun_we) {
3941 /* window length to research is >= 1 */
3942 if(rerun_nwin == rerun_nalloc) {
3943 rerun_nalloc += 10;
3944 ESL_REALLOC(rerun_ws, sizeof(int64_t) * rerun_nalloc); /* yes this works on initial allocation (if rerun_ws == NULL) */
3945 ESL_REALLOC(rerun_we, sizeof(int64_t) * rerun_nalloc);
3946 }
3947 rerun_ws[rerun_nwin] = cur_rerun_ws;
3948 rerun_we[rerun_nwin] = cur_rerun_we;
3949 rerun_nwin++;
3950 }
3951 /* Free the P7_ALIDISPLAY */
3952 prv_sqto = pli->ddef->dcl[d].ad->sqto;
3953 p7_alidisplay_Destroy(pli->ddef->dcl[d].ad);
3954 pli->ddef->dcl[d].ad = NULL;
3955 }
3956 else {
3957 /* only store the hit if it didn't overlap with the previous one */
3958
3959 /* make nhmmer score adjustment from p7_pipeline.c::postMSV_LongTarget() as of SVN r3976
3960 * comments from relevant part of that function are below:
3961 *
3962 * note: the initial bitscore of a hit depends on the window_len of the
3963 * current window. Here, the score is modified (reduced) by treating
3964 * all passing windows as though they came from windows of length
3965 * om->max_length. For details, see
3966 * ~wheelert/notebook/2012/0130_bits_v_evalues/00NOTES (Feb 1)
3967 *
3968 * adjust the score of a hit to account for the full length model - the characters outside the envelope but in the window
3969 * end of p7_pipeline.c comments
3970 *
3971 * I define variables with identical names to
3972 * p7_pipeline.c::postMSV_LongTarget() to make it obvious that
3973 * I'm doing exactly the same thing.
3974 */
3975 env_len = pli->ddef->dcl[d].jenv - pli->ddef->dcl[d].ienv + 1;
3976 ali_len = pli->ddef->dcl[d].jali - pli->ddef->dcl[d].iali + 1;
3977 bitscore = pli->ddef->dcl[d].envsc;
3978 window_len = wlen;
3979 /* For these modifications, see notes, ~/notebook/2010/0716_hmmer_score_v_eval_bug/, end of Thu Jul 22 13:36:49 EDT 2010 */
3980 bitscore -= 2 * log(2. / (window_len+2)) + (env_len-ali_len) * log((float)window_len / (window_len+2));
3981 bitscore += 2 * log(2. / (loc_window_length+2));
3982 /* the ESL_MAX test handles the extremely rare case that the env_len is actually larger than om->max_length */
3983 bitscore += (ESL_MAX(loc_window_length, env_len) - ali_len) * log((float)loc_window_length / (float) (loc_window_length+2));
3984
3985 dom_bias = pli->do_null2_hmmonly ? p7_FLogsum(0.0, log(bg->omega) + pli->ddef->dcl[d].domcorrection) : 0.0;
3986 dom_score = (bitscore - (nullsc2 + dom_bias)) / eslCONST_LOG2;
3987 prv_sqto = pli->ddef->dcl[d].ad->sqto;
3988
3989 if(dom_score >= pli->T) {
3990 cm_tophits_CreateNextHit(hitlist, &hit);
3991 /* We define hit start/stop based on P7_ALIDISPLAY
3992 * sqfrom/sqto instead of envelope boundaries, to be consistent
3993 * with non-hmmonly pipeline runs, CM hit start/stop always
3994 * matches CM_ALIDISPLAY sqfrom/sqto.
3995 */
3996 pli->ddef->dcl[d].ad->sqfrom += ws[i] - 1;
3997 pli->ddef->dcl[d].ad->sqto += ws[i] - 1;
3998 hit->start = pli->ddef->dcl[d].ad->sqfrom;
3999 hit->stop = pli->ddef->dcl[d].ad->sqto;
4000 hit->root = -1; /* irrelevant in HMM only hit */
4001 hit->mode = TRMODE_J;
4002 hit->score = dom_score;
4003
4004 hit->cm_idx = pli->cur_cm_idx;
4005 hit->clan_idx = pli->cur_clan_idx;
4006 hit->seq_idx = pli->cur_seq_idx;
4007 hit->pass_idx = pli->cur_pass_idx;
4008 hit->pvalue = esl_exp_surv (hit->score, p7_evparam[CM_p7_LFTAU], p7_evparam[CM_p7_LFLAMBDA]);
4009 hit->srcL = sq->L; /* this may be -1, in which case it will be updated by caller (cmsearch or cmscan) when full length is known */
4010
4011 hit->hmmonly = TRUE;
4012 hit->glocal = FALSE; /* all HMM hits are local (currently) */
4013 hit->bias = dom_bias;
4014 hit->evalue = 0.; /* we'll redefine this later */
4015
4016 /* create a CM_ALIDISPLAY from the P7_ALIDISPLAY */
4017 avgpp = pli->ddef->dcl[d].oasc / (1.0 + fabs((float) (pli->ddef->dcl[d].jenv - pli->ddef->dcl[d].ienv)));
4018 if((status = cm_alidisplay_CreateFromP7(cm, pli->errbuf, sq, hit->start, hit->score, avgpp, pli->ddef->dcl[d].ad, &(hit->ad))) != eslOK) return status;
4019 /* Free the P7_ALIDISPLAY */
4020 p7_alidisplay_Destroy(pli->ddef->dcl[d].ad);
4021 pli->ddef->dcl[d].ad = NULL;
4022
4023 #if eslDEBUGLEVEL >= 2
4024 printf("#DEBUG: SURVIVOR envelope [%10ld..%10ld] survived Final HMM ONLY stage %6.2f bits P %g\n", hit->start, hit->stop, hit->score, hit->pvalue);
4025 #endif
4026
4027 if (pli->mode == CM_SEARCH_SEQS) {
4028 if ( (status = esl_strdup(sq->name, -1, &(hit->name))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
4029 if (sq->acc[0] != '\0' && (status = esl_strdup(sq->acc, -1, &(hit->acc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
4030 if (sq->desc[0] != '\0' && (status = esl_strdup(sq->desc, -1, &(hit->desc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
4031 }
4032 else {
4033 if ((status = esl_strdup(cm->name, -1, &(hit->name))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
4034 if ((status = esl_strdup(cm->acc, -1, &(hit->acc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
4035 if ((status = esl_strdup(cm->desc, -1, &(hit->desc))) != eslOK) ESL_FAIL(eslEMEM, pli->errbuf, "allocation failure");
4036 }
4037 /* Finally, if we're using model-specific bit score thresholds,
4038 * determine if the significance of the hit (is it reported
4039 * and/or included?) Adapted from Sean's comments at an
4040 * analogous point in p7_pipeline():
4041 *
4042 * If we're using model-specific bit score thresholds (GA | TC | NC)
4043 * and we're in a cmscan pipeline (mode = CM_SCAN_MODELS), then we
4044 * *must* apply those reporting or inclusion thresholds now, because
4045 * this model is about to go away; we won't have its thresholds
4046 * after all targets have been processed.
4047 *
4048 * If we're using E-value thresholds and we don't know the
4049 * search space size (Z_setby == CM_ZSETBY_NTARGETS), we
4050 * *cannot* apply those thresholds now, and we *must* wait
4051 * until all targets have been processed (see cm_tophits_Threshold()).
4052 *
4053 * For any other thresholding, it doesn't matter whether we do
4054 * it here (model-specifically) or at the end (in
4055 * cm_tophits_Threshold()).
4056 *
4057 * What we actually do, then, is to set the flags if we're using
4058 * model-specific score thresholds (regardless of whether we're
4059 * in a scan or a search pipeline); otherwise we leave it to
4060 * cm_tophits_Threshold(). cm_tophits_Threshold() is always
4061 * responsible for *counting* the reported, included sequences.
4062 *
4063 * [xref J5/92]
4064 */
4065 if (pli->use_bit_cutoffs) {
4066 if (cm_pli_TargetReportable(pli, hit->score, hit->evalue)) { /* evalue is invalid, but irrelevant if pli->use_bit_cutoffs */
4067 hit->flags |= CM_HIT_IS_REPORTED;
4068 if (cm_pli_TargetIncludable(pli, hit->score, hit->evalue)) /* ditto */
4069 hit->flags |= CM_HIT_IS_INCLUDED;
4070 }
4071 }
4072 } /* end of 'if(dom_score >= pli->T)' */
4073 else { /* hit wasn't above threshold, don't forget to destroy it */
4074 p7_alidisplay_Destroy(pli->ddef->dcl[d].ad);
4075 pli->ddef->dcl[d].ad = NULL;
4076 }
4077 } /* end of 'else' entered if this domain didn't overlap with previous one */
4078 } /* end of for(d = 0; d < pli->ddef->ndom; d++) */
4079 pli->ddef->ndom = 0; /* reset for next use */
4080 } /* end of 'for(i = 0; i < win'... */
4081
4082 /* if we stored any windows to rerun due to overlaps,
4083 * rerun them now, with a recursive call to this function */
4084 if(rerun_nwin > 0) {
4085 pli_final_stage_hmmonly(pli, cm_offset, om, bg, p7_evparam, sq, rerun_ws, rerun_we, rerun_nwin, hitlist, opt_cm);
4086 }
4087
4088 /* clean up, set return variables, and return */
4089 if(seq != NULL) esl_sq_Destroy(seq);
4090 if(rerun_ws != NULL) free(rerun_ws);
4091 if(rerun_we != NULL) free(rerun_we);
4092
4093 return eslOK;
4094
4095 ERROR:
4096 if(rerun_ws != NULL) free(rerun_ws);
4097 if(rerun_we != NULL) free(rerun_we);
4098 ESL_FAIL(status, pli->errbuf, "out of memory");
4099 }
4100
4101 /* Function: pli_dispatch_cm_search()
4102 * Synopsis: Search a sequence from <start> to <end> with a CM.
4103 * Incept: EPN, Thu Mar 1 10:29:53 2012
4104 *
4105 * Purpose: Use a CM scanning DP algorithm to scan <dsq> from
4106 * <start> to <stop>. The specific algorithm to use
4107 * is specified by cm->search_opts and pli->cur_pass_idx.
4108 *
4109 * Args: pli - the pipeline
4110 * cm - the CM
4111 * dsq - sequence to search
4112 * start - first position of dsq to search
4113 * stop - final position of dsq to search
4114 * hitlist - CM_TOPHITS to add to, can be NULL
4115 * cutoff - min bit score to report to hitlist,
4116 * irrelevant if hitlist is NULL
4117 * env_cutoff - min bit score for env redefn,
4118 * irrelevant if opt_envi, opt_envj are NULL
4119 * qdbidx - index
4120 * ret_sc - RETURN: score returned by scanner
4121 * opt_envi - OPT RETURN: redefined envelope start (can be NULL)
4122 * opt_envj - OPT RETURN: redefined envelope stop (can be NULL)
4123 *
4124 * Returns: eslOK on success.
4125 * eslERANGE if we wanted to do HMM banded, but couldn't.
4126 */
4127 int pli_dispatch_cm_search(CM_PIPELINE *pli, CM_t *cm, ESL_DSQ *dsq, int64_t start, int64_t stop, CM_TOPHITS *hitlist, float cutoff,
4128 float env_cutoff, int qdbidx, float *ret_sc, int64_t *opt_envi, int64_t *opt_envj)
4129 {
4130 int status;
4131 int do_trunc = cm_pli_PassAllowsTruncation(pli->cur_pass_idx);
4132 int do_inside = (cm->search_opts & CM_SEARCH_INSIDE) ? TRUE : FALSE;
4133 int do_hbanded = (cm->search_opts & CM_SEARCH_HBANDED) ? TRUE : FALSE;
4134 int do_qdb_or_nonbanded = (do_hbanded) ? FALSE : TRUE;
4135 double save_tau = cm->tau;
4136 float save_thresh1 = (cm->cp9b == NULL) ? -1. : cm->cp9b->thresh1;
4137 float save_thresh2 = (cm->cp9b == NULL) ? -1. : cm->cp9b->thresh2;
4138 float hbmx_Mb = 0.; /* approximate size in Mb for HMM banded matrix for this sequence */
4139 float sc = 0.; /* score returned from DP scanner */
4140 float mxsize_limit = (pli->mxsize_set) ? pli->mxsize_limit : pli_mxsize_limit_from_W(cm->W);
4141
4142 #if eslDEBUGLEVEL >= 1
4143 printf("in pli_dispatch_cm_search(): do_trunc: %d do_inside: %d cutoff: %.1f env_cutoff: %.1f do_hbanded: %d hitlist?: %d opt_envi/j?: %d start: %" PRId64 " stop: %" PRId64 "\n",
4144 do_trunc, do_inside, cutoff, env_cutoff, do_hbanded, (hitlist == NULL) ? 0 : 1, (opt_envi == NULL && opt_envj == NULL) ? 0 : 1, start, stop);
4145 #endif
4146
4147 if(do_hbanded) {
4148 status = cp9_IterateSeq2Bands(cm, pli->errbuf, dsq, start, stop, pli->cur_pass_idx, mxsize_limit,
4149 TRUE, FALSE, FALSE, /* yes we're doing search, no we won't sample from mx, no we don't need posteriors (yet) */
4150 (! pli->do_not_iterate), pli->maxtau, &hbmx_Mb);
4151 if(status == eslERANGE) {
4152 /* HMM banded matrix exceeded mxsize_limit with tau of
4153 * pli->maxtau. We kill this potential hit. The memory limit
4154 * is acting as a filter. This is the only filter that is
4155 * used differently for different sized models. The bigger
4156 * a model, the more likely it is to have a hit killed here.
4157 */
4158 goto ERROR; /* we'll return eslERANGE, and caller will increment pli->n_overflow_* */
4159 }
4160 else if(status != eslOK) {
4161 printf("pli_dispatch_cm_search(), error: %s\n", pli->errbuf); goto ERROR;
4162 }
4163 else if(status == eslOK) {
4164 /* bands imply a matrix or size mxsize_limit or smaller with tau == cm->tau <= pli->maxtau */
4165 if(do_trunc) { /* HMM banded, truncated */
4166 if(do_inside) {
4167 status = FTrInsideScanHB(cm, pli->errbuf, cm->trhb_mx, mxsize_limit, pli->cur_pass_idx, dsq, start, stop,
4168 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, NULL, &sc);
4169 }
4170 else {
4171 status = TrCYKScanHB(cm, pli->errbuf, cm->trhb_mx, mxsize_limit, pli->cur_pass_idx, dsq, start, stop,
4172 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, NULL, &sc);
4173 }
4174 }
4175 else { /* HMM banded, not truncated */
4176 if(do_inside) {
4177 status = FastFInsideScanHB(cm, pli->errbuf, cm->hb_mx, mxsize_limit, dsq, start, stop,
4178 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, &sc);
4179 }
4180 else {
4181 status = FastCYKScanHB(cm, pli->errbuf, cm->hb_mx, mxsize_limit, dsq, start, stop,
4182 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, &sc);
4183 }
4184 }
4185 }
4186 }
4187 else if (do_qdb_or_nonbanded) {
4188 if(do_trunc) {
4189 if(cm->trsmx == NULL) ESL_XFAIL(eslEINVAL, pli->errbuf, "pli_dispatch_cm_search(), need truncated scan mx but don't have one");
4190 if(do_inside) { /* not HMM banded, truncated */
4191 status = RefITrInsideScan(cm, pli->errbuf, cm->trsmx, qdbidx, pli->cur_pass_idx, dsq, start, stop,
4192 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, NULL, NULL, &sc);
4193 }
4194 else {
4195 status = RefTrCYKScan(cm, pli->errbuf, cm->trsmx, qdbidx, pli->cur_pass_idx, dsq, start, stop,
4196 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, NULL, NULL, &sc);
4197 }
4198 }
4199 else { /* not HMM banded, not truncated */
4200 if(cm->smx == NULL) ESL_XFAIL(eslEINVAL, pli->errbuf, "pli_dispatch_cm_search(), need standard scan mx but don't have one");
4201 if(do_inside) {
4202 status = FastIInsideScan(cm, pli->errbuf, cm->smx, qdbidx, dsq, start, stop,
4203 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, NULL, &sc);
4204 }
4205 else {
4206 status = FastCYKScan(cm, pli->errbuf, cm->smx, qdbidx, dsq, start, stop,
4207 cutoff, hitlist, pli->do_null3, env_cutoff, opt_envi, opt_envj, NULL, &sc);
4208 }
4209 }
4210 if(status != eslOK) { printf("pli_dispatch_cm_search(), error: %s\n", pli->errbuf); goto ERROR; }
4211 }
4212
4213 /* revert to original parameters */
4214 cm->tau = save_tau;
4215 if(cm->cp9b != NULL) {
4216 cm->cp9b->thresh1 = save_thresh1;
4217 cm->cp9b->thresh2 = save_thresh2;
4218 }
4219
4220 *ret_sc = sc;
4221
4222 return eslOK;
4223
4224 ERROR:
4225 cm->tau = save_tau;
4226 /* don't forget to thresh1 and thresh2 revert to their original values (not doing this was bug i42) */
4227 if(cm->cp9b != NULL) {
4228 cm->cp9b->thresh1 = save_thresh1;
4229 cm->cp9b->thresh2 = save_thresh2;
4230 }
4231 *ret_sc = IMPOSSIBLE;
4232 if(opt_envi != NULL) *opt_envi = start;
4233 if(opt_envj != NULL) *opt_envi = stop;
4234 return eslERANGE;
4235 }
4236
4237
4238 /* Function: pli_align_hit()
4239 * Synopsis: Align a hit that survives all stages of the pipeline to a CM.
4240 * Incept: EPN, Mon Aug 8 10:46:21 2011
4241 *
4242 * Purpose: For a given hit <hit> in sequence <sq> spanning
4243 * <hit->start> to <hit->stop>, align it to a CM and create a
4244 * CM_ALIDISPLAY object and store it in <hit->ad>.
4245 *
4246 * The algorithm used is dictated by pli->cm_align_opts.
4247 * There's one important special case, if we want HMM
4248 * banded optimal accuracy alignment with posteriors (which
4249 * is TRUE in the default pipeline) and it will require too
4250 * much memory, we failover to HMM banded CYK (NOT small
4251 * D&C CYK as that would take a long long time for a big
4252 * alignment). HMM banded CYK should never exceed our limit
4253 * because we must have used HMM banded Inside in the final
4254 * search stage (using the same bands we'll use here, those
4255 * in cm->cp9b).
4256 *
4257 * Returns: eslOK on success, alidisplay in hit->ad.
4258 * ! eslOK on an error, pli->errbuf is filled, hit->ad is NULL.
4259 */
4260 int
4261 pli_align_hit(CM_PIPELINE *pli, CM_t *cm, const ESL_SQ *sq, CM_HIT *hit)
4262 {
4263 int status; /* Easel status code */
4264 CM_ALNDATA *adata = NULL; /* alignment data */
4265 ESL_SQ *sq2aln = NULL; /* copy of the hit, req'd by DispatchSqAlignment() */
4266 ESL_STOPWATCH *watch = NULL; /* stopwatch for timing alignment step */
4267 float null3_correction; /* null 3 bit score penalty, for CYK score */
4268 float mxsize_limit = (pli->mxsize_set) ? pli->mxsize_limit : pli_mxsize_limit_from_W(cm->W);
4269
4270 if(cm->cmcons == NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "pli_align_hit() cm->cmcons is NULL");
4271
4272 if((watch = esl_stopwatch_Create()) == NULL) ESL_XFAIL(eslEMEM, pli->errbuf, "out of memory");
4273 esl_stopwatch_Start(watch);
4274
4275 /* make new sq object, b/c DispatchSqAlignment() requires one */
4276 if((sq2aln = esl_sq_CreateDigitalFrom(cm->abc, "seq", sq->dsq + hit->start - 1, hit->stop - hit->start + 1, NULL, NULL, NULL)) == NULL) {status = eslEMEM; goto ERROR; }
4277
4278 cm->align_opts = pli->cm_align_opts;
4279 if(pli->cur_pass_idx != PLI_PASS_STD_ANY) cm->align_opts |= CM_ALIGN_TRUNC;
4280
4281 /* do HMM banded aln, if nec */
4282 if(cm->align_opts & CM_ALIGN_HBANDED) { /* align with HMM bands */
4283 /* we have existing CP9 HMM bands from the final search stage, in
4284 * cm->cp9b (caller should have verified this). Shift them by a
4285 * fixed offset, this guarantees our alignment will be the same
4286 * hit our search found. (After this cm->cp9b bands would fail a
4287 * cp9_ValidateBands() check..., but don't worry about
4288 * that... they'll work for our purposes here.
4289 */
4290 cp9_ShiftCMBands(cm, hit->start, hit->stop, (cm->align_opts & CM_ALIGN_TRUNC) ? TRUE : FALSE);
4291
4292 /* sanity check */
4293 if(! (cm->align_opts & CM_ALIGN_POST)) ESL_XFAIL(eslEINVAL, pli->errbuf, "pli_align_hit() using HMM bands but CM_ALIGN_POST is down");
4294
4295 /* compute the HMM banded alignment */
4296 status = DispatchSqAlignment(cm, pli->errbuf, sq2aln, -1, mxsize_limit, hit->mode, pli->cur_pass_idx,
4297 TRUE, /* TRUE: cp9b bands are valid, don't recalc them */
4298 NULL, NULL, NULL, &adata);
4299 if(status != eslOK && status != eslERANGE) {
4300 goto ERROR;
4301 }
4302 else if(status == eslERANGE) {
4303 /* matrix was too big, alignment not computed, failover to HMM banded CYK with no posteriors */
4304 cm->align_opts &= ~CM_ALIGN_OPTACC;
4305 cm->align_opts &= ~CM_ALIGN_POST;
4306 cm->align_opts |= CM_ALIGN_CYK;
4307 /* Note that we double max matrix size in next call to be safe,
4308 * this should be overkill because we know that our Inside
4309 * search stage using the same bands was able to use a matrix
4310 * less than mxsize_limit. If we can't do it now in twice
4311 * that, something has gone horribly wrong. (Note that the
4312 * required matrix size will be bigger than for an Inside scan,
4313 * because of the required shadow matrix, but that's roughly 25%
4314 * the size of the DP matrix, so the total size required for
4315 * alignment should never exceed twice what the Inside scan
4316 * required.)
4317 */
4318 status = DispatchSqAlignment(cm, pli->errbuf, sq2aln, -1, 2*mxsize_limit, hit->mode, pli->cur_pass_idx,
4319 TRUE, /* TRUE: cp9b bands are valid, don't recalc them */
4320 NULL, NULL, NULL, &adata);
4321 if (status == eslERANGE) ESL_XFAIL(eslEINVAL, pli->errbuf, "pli_align_hit() alignment HB retry mx too big, this shouldn't happen");
4322 else if(status != eslOK) goto ERROR;
4323 }
4324 pli->acct[pli->cur_pass_idx].n_aln_hb++;
4325 esl_stopwatch_Stop(watch); /* we started it above before we calc'ed the CP9 bands */
4326 }
4327 else { /* do non-HMM-banded alignment (! (cm->align_opts & CM_ALIGN_HBANDED)) */
4328 esl_stopwatch_Start(watch);
4329 if((status = DispatchSqAlignment(cm, pli->errbuf, sq2aln, -1, mxsize_limit, hit->mode, pli->cur_pass_idx,
4330 FALSE, NULL, NULL, NULL, &adata)) != eslOK) goto ERROR;
4331 pli->acct[pli->cur_pass_idx].n_aln_dccyk++;
4332 esl_stopwatch_Stop(watch);
4333 }
4334 /* ParsetreeDump(stdout, tr, cm, sq2aln->dsq); */
4335
4336 /* add null3 correction to sc if nec */
4337 if(pli->do_null3) {
4338 ScoreCorrectionNull3CompUnknown(cm->abc, cm->null, sq2aln->dsq, 1, sq2aln->L, cm->null3_omega, &null3_correction);
4339 adata->sc -= null3_correction;
4340 hit->bias = null3_correction;
4341 }
4342
4343 /* create the CM_ALIDISPLAY object */
4344 if((status = cm_alidisplay_Create(cm, pli->errbuf, adata, sq, hit->start,
4345 (cm->align_opts & CM_ALIGN_HBANDED) ? cm->cp9b->tau : -1., /* what was tau, if we used HMM bands */
4346 watch->elapsed, &(hit->ad))) != eslOK) goto ERROR;
4347
4348 /* clean up and return */
4349 cm->align_opts = pli->cm_align_opts; /* restore these */
4350 if(adata != NULL) cm_alndata_Destroy(adata, FALSE); /* FALSE: don't free adata->sqp (sq2aln) */
4351 if(sq2aln != NULL) esl_sq_Destroy(sq2aln);
4352 if(watch != NULL) esl_stopwatch_Destroy(watch);
4353
4354 return eslOK;
4355
4356 ERROR:
4357 cm->align_opts = pli->cm_align_opts; /* restore these */
4358 if(adata != NULL) cm_alndata_Destroy(adata, FALSE); /* FALSE: don't free adata->sqp (sq2aln) */
4359 if(sq2aln != NULL) esl_sq_Destroy(sq2aln);
4360 if(watch != NULL) esl_stopwatch_Destroy(watch);
4361 hit->ad = NULL;
4362 return status;
4363 }
4364
4365 /* Function: pli_scan_mode_read_cm()
4366 * Synopsis: Read a CM from the CM file, mid-pipeline.
4367 * Incept: EPN, Thu Mar 1 15:00:27 2012
4368 *
4369 * Purpose: When in scan mode, we don't read the CM until we know
4370 * we're going to need it, i.e. at least one envelope has
4371 * survived all HMM filters (or HMM filters are turned
4372 * off). Here, we read the CM from the file, configure it
4373 * and return it in <ret_cm>. We also update the pipeline
4374 * regarding the CM we just read.
4375 *
4376 * <p7_evparam> will be NULL and <p7_max_length> will be 0
4377 * (bot are irrelevant) unless pli->do_hmmonly_cur is TRUE.
4378 *
4379 * Returns: <eslOK> on success. <ret_cm> contains the CM.
4380 *
4381 * Throws: <eslEMEM> on allocation failure
4382 * <eslEINCOMPAT> on contract violation.
4383 * In both case, *ret_cm set to NULL, pli->errbuf filled.
4384 */
4385 int
4386 pli_scan_mode_read_cm(CM_PIPELINE *pli, off_t cm_offset, float *p7_evparam, int p7_max_length, CM_t **ret_cm)
4387 {
4388 int status;
4389 CM_t *cm = NULL;
4390 int check_fcyk_beta; /* TRUE if we just read a CM and we need to check if its beta1 == pli->fcyk_beta */
4391 int check_final_beta; /* TRUE if we just read a CM and we need to check if its beta2 == pli->final_beta */
4392 int W_from_cmdline; /* -1 (W not set on cmdline) unless pli->use_wcx is TRUE, which means --wcx was enabled */
4393
4394 if (pli->mode != CM_SCAN_MODELS) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "pli_scan_mode_read_cm(), pipeline isn't in SCAN mode");
4395 if (*ret_cm != NULL) ESL_FAIL(eslEINCOMPAT, pli->errbuf, "pli_scan_mode_read_cm(), *ret_cm != NULL");
4396
4397 #ifdef HMMER_THREADS
4398 /* lock the mutex to prevent other threads from reading the file at the same time */
4399 if (pli->cmfp->syncRead) {
4400 if (pthread_mutex_lock (&pli->cmfp->readMutex) != 0) ESL_FAIL(eslESYS, pli->errbuf, "mutex lock failed");
4401 }
4402 #endif
4403 cm_file_Position(pli->cmfp, cm_offset);
4404 if((status = cm_file_Read(pli->cmfp, FALSE, &(pli->abc), &cm)) != eslOK) ESL_FAIL(status, pli->errbuf, "%s", pli->cmfp->errbuf);
4405 #ifdef HMMER_THREADS
4406 if (pli->cmfp->syncRead) {
4407 if (pthread_mutex_unlock (&pli->cmfp->readMutex) != 0) ESL_EXCEPTION(eslESYS, "mutex unlock failed");
4408 }
4409 #endif
4410
4411 /* from here on, this function is very similar to
4412 * cmsearch.c:configure_cm()
4413 */
4414
4415 cm->config_opts = pli->cm_config_opts;
4416 cm->align_opts = pli->cm_align_opts;
4417 /* check if we need to recalculate QDBs prior to building the scan matrix in cm_Configure()
4418 * (we couldn't do this until we read the CM file to find out what cm->qdbinfo->beta1/beta2 were
4419 */
4420 check_fcyk_beta = (pli->fcyk_cm_search_opts & CM_SEARCH_QDB) ? TRUE : FALSE;
4421 check_final_beta = (pli->final_cm_search_opts & CM_SEARCH_QDB) ? TRUE : FALSE;
4422 if((status = CheckCMQDBInfo(cm->qdbinfo, pli->fcyk_beta, check_fcyk_beta, pli->final_beta, check_final_beta)) == eslFAIL) {
4423 cm->config_opts |= CM_CONFIG_QDB;
4424 cm->qdbinfo->beta1 = pli->fcyk_beta;
4425 cm->qdbinfo->beta2 = pli->final_beta;
4426 }
4427 /* else we don't have to change cm->qdbinfo->beta1/beta2 */
4428
4429 W_from_cmdline = pli->do_wcx ? (int) (cm->clen * pli->wcx) : -1; /* -1: use W from CM file */
4430 if((status = cm_Configure(cm, pli->errbuf, W_from_cmdline)) != eslOK) goto ERROR;
4431 /* update the pipeline about the model */
4432 if((status = cm_pli_NewModel(pli, CM_NEWMODEL_CM, cm, cm->clen, cm->W, CMCountNodetype(cm, MATP_nd),
4433 NULL, NULL, p7_evparam, p7_max_length, pli->cur_cm_idx, pli->cur_clan_idx, NULL)) /* NULL: om, bg, glocal_kh */
4434 != eslOK) goto ERROR;
4435
4436 *ret_cm = cm;
4437 return eslOK;
4438
4439 ERROR:
4440 if(cm != NULL) FreeCM(cm);
4441 *ret_cm = NULL;
4442 return status;
4443 }
4444
4445 /* Function: pli_copy_subseq()
4446 * Incept: EPN, Tue Aug 9 11:13:02 2011
4447 *
4448 * Purpose: Copy a subsequence of an existing sequence <src_sq>
4449 * starting at position <i>, of length <L> to another
4450 * sequence object <dest_sq>. Copy only residues from
4451 * <i>..<i>+<L>-1. <dest_sq> must be pre-allocated.
4452 *
4453 * Returns: eslOK on success.
4454 */
4455 void
4456 pli_copy_subseq(const ESL_SQ *src_sq, ESL_SQ *dest_sq, int64_t i, int64_t L)
4457 {
4458 /*printf("entering pli_copy_subseq i: %" PRId64 " j: %" PRId64 " L: %" PRId64 " start-end: %" PRId64 "... %" PRId64 "\n",
4459 i, i+L-1, L, src_sq->start, src_sq->end);
4460 fflush(stdout);*/
4461
4462 esl_sq_Reuse(dest_sq);
4463 esl_sq_GrowTo(dest_sq, L);
4464 memcpy((void*)(dest_sq->dsq+1), src_sq->dsq+i, L * sizeof(ESL_DSQ));
4465 dest_sq->dsq[0] = dest_sq->dsq[L+1] = eslDSQ_SENTINEL;
4466 dest_sq->n = L;
4467 dest_sq->L = src_sq->L;
4468
4469 if(src_sq->start <= src_sq->end) {
4470 ESL_DASSERT1((L <= (src_sq->end - src_sq->start + 1)));
4471 /*assert(L <= (src_sq->end - src_sq->start + 1));*/
4472 dest_sq->start = src_sq->start + i - 1;
4473 dest_sq->end = dest_sq->start + L - 1;
4474 }
4475 else {
4476 ESL_DASSERT1((L <= (src_sq->start - src_sq->end + 1)));
4477 /*assert(L <= (src_sq->start - src_sq->end + 1));*/
4478 dest_sq->start = src_sq->end + L - 1;
4479 dest_sq->end = dest_sq->start - L + 1;
4480 }
4481 /*printf("leaving copy_subseq dest_sq->start..end: %" PRId64 " %" PRId64 " n: %" PRId64 "\n",
4482 dest_sq->start, dest_sq->end, dest_sq->n);*/
4483
4484 esl_sq_SetName (dest_sq, src_sq->name);
4485 esl_sq_SetAccession(dest_sq, src_sq->acc);
4486 esl_sq_SetDesc (dest_sq, src_sq->desc);
4487
4488 return;
4489 }
4490
4491 /* Function: pli_describe_pass()
4492 * Date: EPN, Tue Nov 29 04:39:38 2011
4493 *
4494 * Purpose: Translate internal flags for pipeline pass index
4495 * into human-readable strings, for clearer output.
4496 *
4497 * Args: pass_idx - a pipeline pass index
4498 * PLI_PASS_CM_SUMMED | PLI_PASS_STD_ANY | PLI_PASS_5P_ONLY_FORCE |
4499 * PLI_PASS_3P_ONLY_FORCE | PLI_PASS_5P_AND_3P_FORCE | PLI_PASS_HMM_ONLY_ANY
4500 *
4501 * Returns: the appropriate string
4502 */
4503 char *
4504 pli_describe_pass(int pass_idx)
4505 {
4506 switch (pass_idx) {
4507 case PLI_PASS_CM_SUMMED: return "(standard and truncated passes)"; break;
4508 case PLI_PASS_STD_ANY: return "(full sequences)"; break;
4509 case PLI_PASS_5P_ONLY_FORCE: return "(5' terminal sequence regions)"; break;
4510 case PLI_PASS_3P_ONLY_FORCE: return "(3' terminal sequence regions)"; break;
4511 case PLI_PASS_5P_AND_3P_FORCE: return "(full sequences short enough to contain a 5' and 3' truncated hit)"; break;
4512 case PLI_PASS_5P_AND_3P_ANY: return "(full sequences, allowing truncated hits)"; break;
4513 case PLI_PASS_HMM_ONLY_ANY: return "(HMM only mode: full sequences, allowing truncated hits)"; break;
4514 default: cm_Fail("bogus pipeline pass index %d\n", pass_idx); break;
4515 }
4516 return "";
4517 }
4518
4519 /* Function: pli_describe_hits_for_pass()
4520 * Date: EPN, Thu Apr 19 05:50:54 2012
4521 *
4522 * Purpose: Translate internal flags for pipeline pass index
4523 * into human-readable strings describe types of hits
4524 * found in that pass, for clearer output.
4525 *
4526 * Args: pass_idx - a pipeline pass index
4527 * PLI_PASS_CM_SUMMED | PLI_PASS_STD_ANY | PLI_PASS_5P_ONLY_FORCE |
4528 * PLI_PASS_3P_ONLY_FORCE | PLI_PASS_5P_AND_3P_FORCE | PLI_PASS_HMM_ONLY_ANY
4529 *
4530 * Returns: the appropriate string
4531 */
4532 char *
4533 pli_describe_hits_for_pass(int pass_idx)
4534 {
4535 switch (pass_idx) {
4536 case PLI_PASS_CM_SUMMED: return "CM hits reported:"; break;
4537 case PLI_PASS_STD_ANY: return "non-truncated CM hits reported:"; break;
4538 case PLI_PASS_5P_ONLY_FORCE: return "5' truncated CM hits reported:"; break;
4539 case PLI_PASS_3P_ONLY_FORCE: return "3' truncated CM hits reported:"; break;
4540 case PLI_PASS_5P_AND_3P_FORCE: return "5' and 3' truncated CM hits reported:"; break;
4541 case PLI_PASS_5P_AND_3P_ANY: return "CM hits reported:"; break;
4542 case PLI_PASS_HMM_ONLY_ANY: return "HMM hits reported:"; break;
4543 default: cm_Fail("bogus pipeline pass index %d\n", pass_idx); break;
4544 }
4545 return "";
4546 }
4547
4548 /* Function: pli_mxsize_limit_from_W()
4549 * Date: EPN, Fri May 30 15:28:04 2014
4550 *
4551 * Purpose: Determine the HMM banded matrix size limit dependent on
4552 * W. This is used separately for each model in the pipeline
4553 * unless the user set --mxsize in 'cmsearch' or 'cmscan'.
4554 *
4555 * Args: W: cm->W, the window length for the current CM
4556 *
4557 * Returns: the maximum matrix size for the given W.
4558 */
4559 float
4560 pli_mxsize_limit_from_W(int W)
4561 {
4562 float mxsize;
4563
4564 mxsize = (float) W - (float) DEFAULT_HB_MXSIZE_MIN_W;
4565 mxsize /= (float) DEFAULT_HB_MXSIZE_MAX_W - (float) DEFAULT_HB_MXSIZE_MIN_W;
4566 mxsize *= (float) DEFAULT_HB_MXSIZE_MAX_MB - (float) DEFAULT_HB_MXSIZE_MIN_MB;
4567 mxsize += (float) DEFAULT_HB_MXSIZE_MIN_MB;
4568
4569 /* at this point, mxsize is probably less than the minimum allowed (DEFAULT_HB_MXSIZE_MIN_MB)
4570 * (depending on what that's set to). It may even be a negative number! So it's important
4571 * to enforce the minimum and maximum here.
4572 */
4573 mxsize = ESL_MIN(mxsize, DEFAULT_HB_MXSIZE_MAX_MB);
4574 mxsize = ESL_MAX(mxsize, DEFAULT_HB_MXSIZE_MIN_MB);
4575
4576 /*printf("in mxsize_limit_from_W(): W: %d returning %.3f\n", W, mxsize);*/
4577 return mxsize;
4578 }
4579
4580 /* Function: pli_check_one_or_zero_envelopes()
4581 * Date: EPN, Tue Jun 5 11:06:15 2018
4582 *
4583 * Purpose: Given a set of envelopes for all passes, check
4584 * if all passes have 0 or 1 envelopes. Return '1'
4585 * if all passes have 0 or 1 envelopes, or 0 if
4586 * not.
4587 *
4588 * Args: nA: [0..p..NPLI_PASSES] number of envelopes
4589 *
4590 * Returns: '1' if all values in nA are 0 or 1
4591 */
4592 int
4593 pli_check_one_or_zero_envelopes(int *nA)
4594 {
4595 int p;
4596
4597 for(p = PLI_PASS_STD_ANY; p < NPLI_PASSES; p++) {
4598 if(nA[p] != 0 && nA[p] != 1) return 0;
4599 }
4600 return 1;
4601 }
4602
4603 /* Function: pli_get_pass_of_best_envelope()
4604 * Date: EPN, Tue Jun 5 11:13:17 2018
4605 *
4606 * Purpose: Given a set of envelopes for all passes, return
4607 * pass index that has envelope with the highest score.
4608 *
4609 * Args: bAA: [0..p..NPLI_PASSES][0..i..nA[p]-1] envelope bit scores
4610 * nA: [0..p..NPLI_PASSES] number of envelopes
4611 *
4612 * Returns: <best_idx> index of pass with highest scoring envelope
4613 * -1 if all passes have zero envelopes
4614 */
4615 int
4616 pli_get_pass_of_best_envelope(float **bAA, int *nA)
4617 {
4618 int p;
4619 int i;
4620 int best_idx = -1;
4621 float best_sc = 0.;
4622
4623 for(p = PLI_PASS_STD_ANY; p < NPLI_PASSES; p++) {
4624 for(i = 0; i < nA[p]; i++) {
4625 if(best_idx == -1 || bAA[p][i] > best_sc) {
4626 best_idx = p;
4627 best_sc = bAA[p][i];
4628 }
4629 }
4630 }
4631
4632 return best_idx;
4633 }
4634
4635 /* Function: pli_check_full_length_envelopes()
4636 * Date: EPN, Tue Jun 5 11:23:07 2018
4637 *
4638 * Purpose: Given a set of envelopes for all passes,
4639 * check if all envelopes include the full sequence.
4640 *
4641 * Args: sAA: [0..p..NPLI_PASSES][0..i..nA[p]-1] envelope start positions
4642 * eAA: [0..p..NPLI_PASSES][0..i..nA[p]-1] envelope end positions
4643 * nA: [0..p..NPLI_PASSES] number of envelopes
4644 * L: length of sequence
4645 *
4646 * Returns: '1' if all envelopes include the full sequence
4647 * '0' if not, or if there are zero envelopes
4648 */
4649 int
4650 pli_check_full_length_envelopes(int64_t **sAA, int64_t **eAA, int *nA, int64_t L)
4651 {
4652 int p;
4653 int i;
4654 int at_least_one = 0;
4655
4656 for(p = PLI_PASS_STD_ANY; p < NPLI_PASSES; p++) {
4657 for(i = 0; i < nA[p]; i++) {
4658 at_least_one = 1;
4659 if(sAA[p][i] != 1 || eAA[p][i] != L) {
4660 return 0;
4661 }
4662 }
4663 }
4664
4665 return at_least_one;
4666 }
4667
4668 /* Function: pli_check_overlap_envelopes()
4669 * Date: EPN, Tue Jun 5 11:32:39 2018
4670 *
4671 * Purpose: Given a set of envelopes for all passes,
4672 * check if all envelopes overlap by at least <min_fract>
4673 * with best envelope, which is specified by <best_pass_idx>
4674 * and <best_env_idx>.
4675 *
4676 * Args: sAA: [0..p..NPLI_PASSES][0..i..nA[p]-1] envelope start positions
4677 * eAA: [0..p..NPLI_PASSES][0..i..nA[p]-1] envelope end positions
4678 * nA: [0..p..NPLI_PASSES] number of envelopes
4679 * best_pass_idx: pass idx of best envelope
4680 * best_env_idx: envelope idx of best envelope
4681 * start_offset: offset to add to start and end of envelopes in PLI_PASS_3P_ONLY_FORCE pass
4682 * min_fract: minimum fraction of overlap
4683 * ret_val: RETURN: filled with '1' if all envelopes overlap sufficiently
4684 * errbuf: error message buffer
4685 *
4686 * Returns: eslOK on success, *ret_pass filled
4687 * ! eslOK on an error, errbuf filled
4688 */
4689 int
4690 pli_check_overlap_envelopes(int64_t **sAA, int64_t **eAA, int *nA, int best_pass_idx, int best_env_idx, int64_t start_offset, float min_fract, int *ret_val, char *errbuf)
4691 {
4692 int status;
4693 int p;
4694 int i;
4695 int imax;
4696 int64_t best_start;
4697 int64_t best_end;
4698 int64_t best_len;
4699 int64_t cur_start;
4700 int64_t cur_end;
4701 int64_t cur_len;
4702 int64_t cur_olap = -1;
4703 int violation = 0;
4704
4705 best_start = sAA[best_pass_idx][best_env_idx];
4706 best_end = eAA[best_pass_idx][best_env_idx];
4707 if(best_pass_idx == PLI_PASS_3P_ONLY_FORCE) {
4708 best_start += start_offset;
4709 best_end += start_offset;
4710 }
4711 best_len = best_end - best_start + 1;
4712
4713 for(p = PLI_PASS_STD_ANY; p < NPLI_PASSES; p++) {
4714 imax = nA[p]; /* need to set this before loop because our break loop convention sets p to NPLI_PASSES+1 so check of nA[p] would be out of bounds after that */
4715 for(i = 0; i < imax; i++) {
4716 if(p != best_pass_idx || i != best_env_idx) {
4717 cur_start = sAA[p][i];
4718 cur_end = eAA[p][i];
4719 if(p == PLI_PASS_3P_ONLY_FORCE) {
4720 cur_start += start_offset;
4721 cur_end += start_offset;
4722 }
4723 cur_len = cur_end - cur_start + 1;
4724 if((status = cm_tophits_OverlapNres(cur_start, cur_end, best_start, best_end, &cur_olap, errbuf)) != eslOK) return status;
4725 /*printf("returned from cm_tophits_OverlapNres, cur_start: %" PRId64 " cur_end: %" PRId64 "best_start: %" PRId64 " best_end: %" PRId64 " cur_olap: %" PRId64 "\n", cur_start, cur_end, best_start, best_end, cur_olap);*/
4726 if(cur_olap < (ESL_MIN(best_len, cur_len) * min_fract)) {
4727 /* break loop */
4728 violation = 1;
4729 i = imax+1;
4730 p = NPLI_PASSES+1;
4731 }
4732 }
4733 }
4734 }
4735
4736 if(violation) { /* at least one hit does not overlap sufficiently */
4737 *ret_val = 0;
4738 }
4739 else { /* all hits overlap sufficiently */
4740 *ret_val = 1;
4741 }
4742
4743 return eslOK;
4744 }
4745
4746 #if 0
4747 /* EPN, Fri Mar 2 13:46:18 2012
4748 * This function was developed when I was experimenting with using
4749 * multiple HMM filters, which was later scrapped. It's left here for
4750 * reference.
4751 */
4752
4753 /* Function: merge_windows_from_two_lists()
4754 * Synopsis: Merge two sorted lists of windows into one, collapsing overlapping windows together.
4755 * Incept: EPN, Mon Nov 29 14:47:40 2010
4756 *
4757 * Purpose: Given two lists of windows, merge them into one list
4758 * by collapsing any overlapping windows into a single window.
4759 *
4760 * Each window in list 1 is defined as:
4761 * <ws1[i]>..we1[i] for i=0..<nwin1-1>
4762 *
4763 * Each window in list 2 is defined as:
4764 * <ws2[i]>..we2[i] for i=0..<nwin2-1>
4765 *
4766 * The windows within each list must be sorted in
4767 * increasing order and be non-overlapping, i.e. the
4768 * following must hold:
4769 *
4770 * ws1[i] < ws1[j] for all i < j.
4771 * ws1[i] <= we1[i] for all i.
4772 *
4773 * ws2[i] < ws2[j] for all i < j.
4774 * ws2[i] <= we2[i] for all i.
4775 *
4776 * Note, we check that these conditions hold at the
4777 * beginning of the function, and return eslEINVAL if not.
4778 *
4779 * A new list is created and returned, as <mws>,
4780 * <mwe>, and <nmwin>, start, end positions and
4781 * number of windows respectively.
4782 *
4783 * The lowest P-value of any of the merged windows is also
4784 * kept, and returned in <ret_mwp>, <ret_mwp[i]> is the
4785 * lowest P-value of any of the P-values from <wp1>/<wp2>
4786 * for windows that were merged together to create
4787 * <mws>..<mwe>. If the P-value was from a window in list 1
4788 * (<wp1>), <ret_mwl[i]> is set as 1, if it is from a
4789 * window from list 2 (<wp2>), <ret_mwl[i]> is set as 2.
4790 *
4791 * Returns: New merged list in <ret_mws>, <ret_mwe>, <ret_mwp>,
4792 * <ret_mwl>, <ret_nmwin>, storing start position, end
4793 * position, P-value and list origin of each of the
4794 * <ret_nmwin> windows in the merged list.
4795 */
4796 int
4797 merge_windows_from_two_lists(int64_t *ws1, int64_t *we1, double *wp1, int *wl1, int nwin1, int64_t *ws2, int64_t *we2, double *wp2, int *wl2, int nwin2, int64_t **ret_mws, int64_t **ret_mwe, double **ret_mwp, int **ret_mwl, int *ret_nmwin)
4798 {
4799 int status;
4800 int64_t *mws = NULL;
4801 int64_t *mwe = NULL;
4802 double *mwp = NULL;
4803 int *mwl = NULL;
4804 int nmwin;
4805 int mi, i1, i2;
4806 int nalloc;
4807
4808 /* check that our required conditions hold */
4809 for(i1 = 0; i1 < nwin1-1; i1++) if(ws1[i1] >= ws1[i1+1]) return eslEINVAL;
4810 for(i2 = 0; i2 < nwin2-1; i2++) if(ws2[i2] >= ws2[i2+1]) return eslEINVAL;
4811 for(i1 = 0; i1 < nwin1; i1++) if(ws1[i1] > we1[i1]) return eslEINVAL;
4812 for(i2 = 0; i2 < nwin2; i2++) if(ws2[i2] > we2[i2]) return eslEINVAL;
4813
4814 nalloc = nwin1 + nwin2; /* we'll never exceed this */
4815 ESL_ALLOC(mws, sizeof(int64_t) * ESL_MAX(1, nalloc)); // avoid 0 malloc
4816 ESL_ALLOC(mwe, sizeof(int64_t) * ESL_MAX(1, nalloc));
4817 ESL_ALLOC(mwp, sizeof(double) * ESL_MAX(1, nalloc));
4818 ESL_ALLOC(mwl, sizeof(int) * ESL_MAX(1, nalloc));
4819
4820 i1 = 0;
4821 i2 = 0;
4822 mi = 0;
4823 while(i1 < nwin1 || i2 < nwin2) {
4824 if((i1 < nwin1) && ((i2 == nwin2) || (ws1[i1] < ws2[i2]))) {
4825 /**************************************************
4826 * case 1: our next hit, in order is from list 1 *
4827 **************************************************/
4828 /* initialize merged hit as copy of hit from list 1 */
4829 mws[mi] = ws1[i1]; /* our merged window begins at ws1[i1], this won't change */
4830 mwp[mi] = wp1[i1]; /* for now, our best P-value is wp1[i1] */
4831 mwl[mi] = wl1[i1]; /* for now, our best P-value comes from list 1 */
4832 mwe[mi] = we1[i1]; /* for now, our merged window ends at we1[i1] */
4833 /* merge with all overlapping windows in list 2 */
4834 while((i2 < nwin2) && (mwe[mi] >= ws2[i2])) { /* check all windows in list 2 that overlap with our current list 1 window */
4835 mwe[mi] = ESL_MAX(mwe[mi], we2[i2]); /* our merged window now ends at larger of mwe[me] and we2[i2] */
4836 if(wp2[i2] < mwp[mi]) {
4837 mwp[mi] = wp2[i2]; /* our best P-value now is wp2[i2] */
4838 mwl[mi] = wl2[i2]; /* our best P-value now comes from list 2 */
4839 }
4840 i2++;
4841 }
4842 i1++;
4843 /* finally, if we merged any windows from list 2, our window is possibly larger,
4844 * so we merge with all now-overlapping windows in list 1 */
4845 while((i1 < nwin1) && (mwe[mi] >= ws1[i1])) {
4846 mwe[mi] = ESL_MAX(mwe[mi], we1[i1]); /* our merged window now ends at larger of mwe[me] and we2[i2] */
4847 if(wp1[i1] < mwp[mi]) {
4848 mwp[mi] = wp1[i1]; /* our best P-value now is wp1[i1] */
4849 mwl[mi] = wl1[i1]; /* our best P-value now comes from list 1 */
4850 }
4851 i1++;
4852 }
4853 mi++;
4854 }
4855 else {
4856 /****************************************************************************************************************
4857 * case 2: our next hit, in order is from list 2, same code as case 1, with list 1 and list 2 variables inverted *
4858 ****************************************************************************************************************/
4859 /* initialize merged hit as copy of hit from list 2 */
4860 mws[mi] = ws2[i2]; /* our merged window begins at ws1[i2], this won't change */
4861 mwp[mi] = wp2[i2]; /* for now, our best P-value is wp2[i2] */
4862 mwl[mi] = wl2[i2]; /* for now, our best P-value comes from list 2 */
4863 mwe[mi] = we2[i2]; /* for now, our merged window ends at we2[i2] */
4864 /* merge with all overlapping windows in list 1 */
4865 while((i1 < nwin1) && (mwe[mi] >= ws1[i1])) { /* check all windows in list 1 that overlap with our current list 2 window */
4866 mwe[mi] = ESL_MAX(mwe[mi], we1[i1]); /* our merged window now ends at larger of mwe[me] and we1[i1] */
4867 if(wp1[i1] < mwp[mi]) {
4868 mwp[mi] = wp1[i1]; /* our best P-value now is wp1[i1] */
4869 mwl[mi] = wl1[i1]; /* our best P-value now comes from list 1 */
4870 }
4871 i1++;
4872 }
4873 i2++;
4874 /* finally, if we merged any windows from list 1, our window is possibly larger,
4875 * so we merge with all now-overlapping windows in list 2 */
4876 while((i2 < nwin2) && (mwe[mi] >= ws2[i2])) { /* check all windows in list 2 that overlap with our current list 1 window */
4877 mwe[mi] = ESL_MAX(mwe[mi], we2[i2]); /* our merged window now ends at larger of mwe[me] and we2[i2] */
4878 if(wp2[i2] < mwp[mi]) {
4879 mwp[mi] = wp2[i2]; /* our best P-value now is wp2[i2] */
4880 mwl[mi] = wl2[i2]; /* our best P-value now comes from list 2 */
4881 }
4882 i2++;
4883 }
4884 mi++;
4885 }
4886 }
4887 nmwin = mi;
4888
4889 /*
4890 for(i1 = 0; i1 < nwin1; i1++) printf("list1 win %5d %10" PRId64 "..%10" PRId64 " P: %10g\n", i1, ws1[i1], we1[i1], wp1[i1]);
4891 printf("\n");
4892 for(i2 = 0; i2 < nwin2; i2++) printf("list2 win %5d %10" PRId64 "..%10" PRId64 " P: %10g\n", i2, ws2[i2], we2[i2], wp2[i2]);
4893 printf("\n");
4894 for(mi = 0; mi < nmwin; mi++) printf("mlist win %5d %10" PRId64 "..%10" PRId64 " P: %10g %d\n", mi, mws[mi], mwe[mi], mwp[mi], mwl[mi]);
4895 printf("\n");
4896 */
4897
4898 *ret_mws = mws;
4899 *ret_mwe = mwe;
4900 *ret_mwp = mwp;
4901 *ret_mwl = mwl;
4902 *ret_nmwin = nmwin;
4903
4904 return eslOK;
4905
4906 ERROR:
4907 return status;
4908 }
4909 #endif
4910