Debian Codegraph @

src/cm_p7_domaindef.c

raw symbols SCIP
1 /* Domain definition using glocal unihit configured profiles.
2 * Only generic, not optimized matrices are used b/c the optimized
3 * functions make assumptions that are violated by a glocal model.
4 *
5 * Exegesis:
6 *
7 * The following is copied from p7_domaindef.c. The only difference
8 * between this and that file (and between
9 * p7_domaindef_ByPosteriorHeuristics() and
10 * p7_domaindef_GlocalByPosteriorHeuristics() is that
11 * the latter uses a glocal unihit configured model and thus requires
12 * generic matrices instead of optimized ones:
13 *
14 * From p7_domaindef.c:
15 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 * The key function here is <p7_domaindef_GlocalByPosteriorHeuristics()>.
17 * Everything else is support structure.
18 *
19 * When you call <p7_domaindef_GlocalByPosteriorHeuristics()>, you have a
20 * per-sequence hit that's judged significant, and you've calculated
21 * Forward/Backward score matrices for the complete sequence. Thus,
22 * the input data are the model <gm>, the sequence <sq>, and filled-in
23 * forward and backward matrices <fwd>, <bck>.
24 *
25 * The function then chews over this data, using posterior
26 * probabilities and heuristics to define, score, and obtain
27 * display-ready alignments for individual domains. When it's done,
28 * your <fwd> and <bck> matrices have been effectively destroyed (they
29 * get reused for individual domain alignment calculations), and
30 * <ddef> contains all the per-domain results you need. It returns to
31 * you the number of domains it's defined (in <ret_ndom>), and the
32 * total per-sequence score derived by a sum of individual domain
33 * scores (in <ret_sc>).
34 *
35 * The <P7_DOMAINDEF> structure is a reusable container that manages
36 * all the necessary working memory and heuristic thresholds.
37 *
38 * SRE, Thu Jan 24 09:28:01 2008 [Janelia]
39 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 */
41 #include <esl_config.h>
42 #include <p7_config.h>
43 #include "config.h"
44
45 #include <math.h>
46 #include <string.h>
47
48 #include "easel.h"
49 #include "esl_random.h"
50 #include "esl_sq.h"
51 #include "esl_vectorops.h"
52
53 #include "hmmer.h"
54
55 #include "infernal.h"
56
57 static int is_multidomain_region (P7_DOMAINDEF *ddef, int i, int j);
58 /* Note: is_multidomain_region is *identical* to the function of the same name in p7_domaindef.c*/
59 static int glocal_region_trace_ensemble (P7_DOMAINDEF *ddef, const P7_PROFILE *gm, const ESL_DSQ *dsq, int ireg, int jreg, const P7_GMX *fwd, P7_GMX *wrk, int do_null2, int *ret_nc);
60 static int glocal_rescore_isolated_domain(P7_DOMAINDEF *ddef, const P7_PROFILE *gm, const ESL_SQ *sq, P7_GMX *gx1, P7_GMX *gx2,
61 int i, int j, int null2_is_done, int do_null2, int do_aln);
62
63 /* Function: p7_domaindef_GlocalByPosteriorHeuristics()
64 * Synopsis: Define glocal domains in a sequence using posterior probs.
65 * Incept: EPN, Tue Oct 5 10:02:34 2010
66 * SRE, Sat Feb 23 08:17:44 2008 [Janelia] (p7_domaindef_ByPosteriorHeuristics())
67 *
68 * Purpose: Given a sequence <sq> and model <gm> for which we have
69 * already calculated a Forward and Backward parsing
70 * matrices <gxf> and <gxb>; use posterior probability
71 * heuristics to determine an annotated domain structure;
72 * and for each domain found, score it (with null2
73 * calculations) and obtain an optimal accuracy alignment,
74 * using <fwd> and <bck> matrices as workspace for the
75 * necessary full-matrix DP calculations. Caller provides a
76 * new or reused <ddef> object to hold these results.
77 *
78 * As a special case, if the profile is in unihit mode
79 * upon entering, we don't ever modify its configuration.
80 * This is especially important if this function is
81 * being used within a search/scan pipeline with a
82 * specially configured p7 profile in which N->N and/or
83 * C->C transitions have been set to IMPOSSIBLE. (If
84 * we were to call ReconfigLength() on such a profile
85 * we would make those transitions possible.)
86 *
87 * One case in which profile reconfiguration is necessary
88 * is when multiple domains are suspected. However, we
89 * guard against this if the profile enters in unihit mode
90 * by no allowing multiple domains (in fact, it should
91 * never happen because J states are unreachable in unihit
92 * profiles). If multiple domains are suspected in this case,
93 * we return eslEINCONCEIVABLE.
94 *
95 * Upon return, <ddef> contains the definitions of all the
96 * domains: their bounds, their null-corrected Forward
97 * scores, and their optimal posterior accuracy alignments.
98 *
99 * <do_null2> is TRUE if we'll eventually apply a null2
100 * penalty FALSE if not. If FALSE, we can save time by
101 * skipping Backward calls at some stages.
102 *
103 * Returns: <eslOK> on success.
104 *
105 * <eslERANGE> on numeric overflow in posterior
106 * decoding. This should not be possible for multihit
107 * models.
108 *
109 * <eslEINCONCEIVABLE> if profile enters as unihit but
110 * multiple domains are suspected.
111 */
112 int
113 p7_domaindef_GlocalByPosteriorHeuristics(const ESL_SQ *sq, P7_PROFILE *gm,
114 P7_GMX *gxf, P7_GMX *gxb, P7_GMX *fwd, P7_GMX *bck,
115 P7_DOMAINDEF *ddef, int do_null2)
116 {
117 int i, j;
118 int triggered;
119 int d;
120 int i2,j2;
121 int last_j2;
122 int nc;
123 int saveL = gm->L; /* Save the length config of <om>; will restore upon return */
124 int save_mode = gm->mode; /* Likewise for the mode. */
125 int status;
126 int save_mode_is_unihit;
127
128 save_mode_is_unihit = (p7_IsMulti(save_mode)) ? FALSE : TRUE; /* if save_mode_is_unihit is TRUE, we never modify profile's configuration (length nor mode) */
129
130 if ((status = p7_domaindef_GrowTo(ddef, sq->n)) != eslOK) return status; /* ddef's btot,etot,mocc now ready for seq of length n */
131 /*printf("GDD P7 mode: %d\n", gm->mode);*/
132 if ((status = p7_GDomainDecoding(gm, gxf, gxb, ddef)) != eslOK) return status; /* ddef->{btot,etot,mocc} now made. */
133
134 /*printf("In p7_domaindef_GlocalByPosteriorHeuristics(): mode: %d rt1: %g rt2: %g rt3: %g nsamples: %d reseed: %d\n", save_mode, ddef->rt1, ddef->rt2, ddef->rt3, ddef->nsamples, ddef->do_reseeding);*/
135
136 esl_vec_FSet(ddef->n2sc, sq->n+1, 0.0); /* ddef->n2sc null2 scores are initialized */
137 ddef->nexpected = ddef->btot[sq->n]; /* posterior expectation for # of domains (same as etot[sq->n]) */
138
139 if(! save_mode_is_unihit) p7_ReconfigUnihit(gm, saveL); /* process each domain in unihit mode, regardless of gm->mode */
140 i = -1;
141 triggered = FALSE;
142 for (j = 1; j <= sq->n; j++)
143 {
144 /*printf("GDD j: %5d m: %.5f b: %8.3f e: %8.3f bhere: %8.3f ehere: %8.3f\n",
145 j,
146 ddef->mocc[j],
147 ddef->btot[j],
148 ddef->etot[j],
149 ddef->btot[j] - ddef->btot[j-1],
150 ddef->etot[j] - ddef->etot[j-1]);
151 */
152 if (! triggered)
153 { /* xref J2/101 for what the logic below is: */
154 if (ddef->mocc[j] - (ddef->btot[j] - ddef->btot[j-1]) < ddef->rt2) i = j;
155 else if (i == -1) i = j;
156 if (ddef->mocc[j] >= ddef->rt1) triggered = TRUE;
157 }
158 else if (ddef->mocc[j] - (ddef->etot[j] - ddef->etot[j-1]) < ddef->rt2)
159 {
160 /* We have a region i..j to evaluate. */
161 p7_gmx_GrowTo(fwd, gm->M, j-i+1);
162 p7_gmx_GrowTo(bck, gm->M, j-i+1);
163 ddef->nregions++;
164 if (is_multidomain_region(ddef, i, j))
165 {
166 if(save_mode_is_unihit) return eslEINCONCEIVABLE;
167
168 /* This region appears to contain more than one domain, so we have to
169 * resolve it by cluster analysis of posterior trace samples, to define
170 * one or more domain envelopes.
171 */
172 ddef->nclustered++;
173
174 /* Resolve the region into domains by stochastic trace
175 * clustering; assign position-specific null2 model by
176 * stochastic trace clustering; there is redundancy
177 * here; we will consolidate later if null2 strategy
178 * works
179 */
180 p7_ReconfigMultihit(gm, saveL);
181 p7_GForward(sq->dsq+i-1, j-i+1, gm, fwd, NULL);
182 glocal_region_trace_ensemble(ddef, gm, sq->dsq, i, j, fwd, bck, do_null2, &nc);
183 p7_ReconfigUnihit(gm, saveL);
184 /* ddef->n2sc is now set on i..j by the traceback-dependent method */
185
186 last_j2 = 0;
187 for (d = 0; d < nc; d++) {
188 p7_spensemble_GetClusterCoords(ddef->sp, d, &i2, &j2, NULL, NULL, NULL);
189 if (i2 <= last_j2) ddef->noverlaps++;
190
191 /* Note that k..m coords on model are available, but
192 * we're currently ignoring them. This leads to a
193 * rare clustering bug that we eventually need to fix
194 * properly [xref J3/32]: two different regions in one
195 * profile HMM might have hit same seq domain, and
196 * when we now go to calculate an OA trace, nothing
197 * constrains us to find the two different alignments
198 * to the HMM; in fact, because OA is optimal, we'll
199 * find one and the *same* alignment, leading to an
200 * apparent duplicate alignment in the output.
201 *
202 * Registered as #h74, Dec 2009, after EBI finds and
203 * reports it. #h74 is worked around in p7_tophits.c
204 * by hiding all but one envelope with an identical
205 * alignment, in the rare event that this
206 * happens. [xref J5/130].
207 */
208 ddef->nenvelopes++;
209 if (glocal_rescore_isolated_domain(ddef, gm, sq, fwd, bck, i2, j2, TRUE, do_null2, FALSE) == eslOK)
210 last_j2 = j2;
211 }
212 p7_spensemble_Reuse(ddef->sp);
213 p7_trace_Reuse(ddef->tr);
214 }
215 else
216 {
217 /* The region looks simple, single domain; convert the region to an envelope. */
218 ddef->nenvelopes++;
219 glocal_rescore_isolated_domain(ddef, gm, sq, fwd, bck, i, j, FALSE, do_null2, FALSE);
220 }
221 i = -1;
222 triggered = FALSE;
223 }
224 }
225
226 /* If profile was unihit upon entrance, we didn't modify its configuration (length nor mode),
227 * else restore it to its original multihit mode, and to its original length model */
228 if (! save_mode_is_unihit) {
229 p7_ReconfigMultihit(gm, saveL);
230 }
231
232 return eslOK;
233 }
234
235
236
237 /*****************************************************************
238 * 3. Internal routines
239 *****************************************************************/
240
241
242 /* is_multidomain_region()
243 * SRE, Fri Feb 8 11:35:04 2008 [Janelia]
244 *
245 * This defines the trigger for when we need to hand a "region" off to
246 * a deeper analysis (using stochastic tracebacks and clustering)
247 * because there's reason to suspect it may encompass two or more
248 * domains.
249 *
250 * The criterion is to find the split point z at which the expected
251 * number of E occurrences preceding B occurrences is maximized, and
252 * if that number is greater than the heuristic threshold <ddef->rt3>,
253 * then return TRUE. In other words, we're checking to see if there's
254 * any point in the region at which it looks like an E was followed by
255 * a B, as expected for a multidomain interpretation of the region.
256 *
257 * More precisely: return TRUE if \max_z [ \min (B(z), E(z)) ] >= rt3
258 * where
259 * E(z) = expected number of E states occurring in region before z is emitted
260 * = \sum_{y=i}^{z} eocc[i] = etot[z] - etot[i-1]
261 * B(z) = expected number of B states occurring in region after z is emitted
262 * = \sum_{y=z}^{j} bocc[i] = btot[j] - btot[z-1]
263 *
264 *
265 * Because this relies on the <ddef->etot> and <ddef->btot> arrays,
266 * <calculate_domain_posteriors()> needs to have been called first.
267 *
268 * Xref: J2/101.
269 */
270 static int
271 is_multidomain_region(P7_DOMAINDEF *ddef, int i, int j)
272 {
273 int z;
274 float max;
275 float expected_n;
276
277 max = -1.0;
278 for (z = i; z <= j; z++)
279 {
280 expected_n = ESL_MIN( (ddef->etot[z] - ddef->etot[i-1]), (ddef->btot[j] - ddef->btot[z-1]));
281 max = ESL_MAX(max, expected_n);
282 }
283 return ( (max >= ddef->rt3) ? TRUE : FALSE);
284 }
285
286
287 /* glocal_region_trace_ensemble()
288 * EPN, Tue Oct 5 10:13:25 2010
289 *
290 * Based on p7_domaindef.c's region_trace_ensemble(). Modified so that
291 * generic matrices (which can be used for glocally configured models)
292 * can be used. An additional parameter <do_null2> has been added,
293 * so that null2-related calculations are only done if necessary.
294 * That is, they're skipped if null2 has been turned off in the pipeline.
295 *
296 * Notes from p7_domaindef.c::region_trace_ensemble():
297 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298 * SRE, Fri Feb 8 11:49:44 2008 [Janelia]
299 *
300 * Here, we've decided that region <ireg>..<jreg> in sequence <dsq> might be
301 * composed of more than one domain, and we're going to use clustering
302 * of a posterior ensemble of stochastic tracebacks to sort it out.
303 *
304 * Caller provides a filled Forward matrix in <fwd> for the sequence
305 * region <dsq+ireg-1>, length <jreg-ireg+1>, for the model <om>
306 * configured in multihit mode with its target length distribution
307 * set to the total length of <dsq>: i.e., the same model
308 * configuration used to score the complete sequence (if it weren't
309 * multihit, we wouldn't be worried about multiple domains).
310 *
311 * Caller also provides a DP matrix in <wrk> containing at least one
312 * row, for use as temporary workspace. (This will typically be the
313 * caller's Backwards matrix, which we haven't yet used at this point
314 * in the processing pipeline.)
315 *
316 * Caller provides <ddef>, which defines heuristic parameters that
317 * control the clustering, and provides working space for the
318 * calculation and the answers. The <ddef->sp> object must have been
319 * reused (i.e., it needs to be fresh; we're going to use it here);
320 * the caller needs to Reuse() it specifically, because it can't just
321 * Reuse() the whole <ddef>, when it's in the process of analyzing
322 * regions.
323 *
324 * Upon return, <*ret_nc> contains the number of clusters that were
325 * defined.
326 *
327 * The caller can retrieve info on each cluster by calling
328 * <p7_spensemble_GetClusterCoords(ddef->sp...)> on the
329 * <P7_SPENSEMBLE> object in <ddef>.
330 *
331 * Other information on what's happened in working memory:
332 *
333 * <ddef->n2sc[ireg..jreg]> now contains log f'(x_i) / f(x_i) null2 scores
334 * for each residue.
335 *
336 * <ddef->sp> gets filled in, and upon return, it's holding the answers
337 * (the cluster definitions). When the caller is done retrieving those
338 * answers, it needs to <esl_spensemble_Reuse()> it before calling
339 * <region_trace_ensemble()> again.
340 *
341 * <ddef->tr> is used as working memory for sampled traces.
342 *
343 * <wrk> has had its zero row clobbered as working space for a null2 calculation.
344 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
345 */
346 static int
347 glocal_region_trace_ensemble(P7_DOMAINDEF *ddef, const P7_PROFILE *gm, const ESL_DSQ *dsq, int ireg, int jreg,
348 const P7_GMX *fwd, P7_GMX *wrk, int do_null2, int *ret_nc)
349 {
350 int Lr = jreg-ireg+1;
351 int t, d, d2;
352 int nov, n;
353 int nc;
354 int pos;
355 float null2[p7_MAXCODE];
356
357 esl_vec_FSet(ddef->n2sc+ireg, Lr, 0.0); /* zero the null2 scores in region */
358
359 /* By default, we make results reproducible by forcing a reset of
360 * the RNG to its originally seeded state.
361 */
362 if (ddef->do_reseeding)
363 esl_randomness_Init(ddef->r, esl_randomness_GetSeed(ddef->r));
364
365 /* Collect an ensemble of sampled traces; calculate null2 odds ratios from these if nec */
366 for (t = 0; t < ddef->nsamples; t++)
367 {
368 p7_GStochasticTrace(ddef->r, dsq+ireg-1, Lr, gm, fwd, ddef->tr);
369 p7_trace_Index(ddef->tr);
370
371 pos = 1;
372 for (d = 0; d < ddef->tr->ndom; d++)
373 {
374 p7_spensemble_Add(ddef->sp, t, ddef->tr->sqfrom[d]+ireg-1, ddef->tr->sqto[d]+ireg-1, ddef->tr->hmmfrom[d], ddef->tr->hmmto[d]);
375
376 if(do_null2) {
377 p7_GNull2_ByTrace(gm, ddef->tr, ddef->tr->tfrom[d], ddef->tr->tto[d], wrk, null2);
378
379 /* residues outside domains get bumped +1: because f'(x) = f(x), so f'(x)/f(x) = 1 in these segments */
380 for (; pos <= ddef->tr->sqfrom[d]; pos++) ddef->n2sc[ireg+pos-1] += 1.0;
381
382 /* Residues inside domains get bumped by their null2 ratio */
383 for (; pos <= ddef->tr->sqto[d]; pos++) ddef->n2sc[ireg+pos-1] += null2[dsq[ireg+pos-1]];
384 }
385 }
386 if(do_null2) {
387 /* the remaining residues in the region outside any domains get +1 */
388 for (; pos <= Lr; pos++) ddef->n2sc[ireg+pos-1] += 1.0;
389 }
390 p7_trace_Reuse(ddef->tr);
391 }
392
393 /* Convert the accumulated n2sc[] ratios in this region to log odds null2 scores on each residue. */
394 if(do_null2) {
395 for (pos = ireg; pos <= jreg; pos++)
396 ddef->n2sc[pos] = logf(ddef->n2sc[pos] / (float) ddef->nsamples);
397 }
398
399 /* Cluster the ensemble of traces to break region into envelopes. */
400 p7_spensemble_Cluster(ddef->sp, ddef->min_overlap, ddef->of_smaller, ddef->max_diagdiff, ddef->min_posterior, ddef->min_endpointp, &nc);
401
402 /* A little hacky now. Remove "dominated" domains relative to seq coords. */
403 for (d = 0; d < nc; d++)
404 ddef->sp->assignment[d] = 0; /* overload <assignment> to flag that a domain is dominated */
405
406 /* who dominates who? (by post prob) */
407 for (d = 0; d < nc; d++)
408 {
409 for (d2 = d+1; d2 < nc; d2++)
410 {
411 nov = ESL_MIN(ddef->sp->sigc[d].j, ddef->sp->sigc[d2].j) - ESL_MAX(ddef->sp->sigc[d].i, ddef->sp->sigc[d2].i) + 1;
412 if (nov == 0) break;
413 n = ESL_MIN(ddef->sp->sigc[d].j - ddef->sp->sigc[d].i + 1, ddef->sp->sigc[d2].j - ddef->sp->sigc[d2].i + 1);
414 if ((float) nov / (float) n >= 0.8) /* overlap */
415 {
416 if (ddef->sp->sigc[d].prob > ddef->sp->sigc[d2].prob) ddef->sp->assignment[d2] = 1;
417 else ddef->sp->assignment[d] = 1;
418 }
419 }
420 }
421
422 /* shrink the sigc list, removing dominated domains */
423 d = 0;
424 for (d2 = 0; d2 < nc; d2++)
425 {
426 if (ddef->sp->assignment[d2]) continue; /* skip domain d2, it's dominated. */
427 if (d != d2) memcpy(ddef->sp->sigc + d, ddef->sp->sigc + d2, sizeof(struct p7_spcoord_s));
428 d++;
429 }
430 ddef->sp->nc = d;
431 *ret_nc = d;
432 return eslOK;
433 }
434
435 /* glocal_rescore_isolated_domain()
436 * EPN, Tue Oct 5 10:16:12 2010
437 *
438 * Based on p7_domaindef.c's rescore_isolated_domain(). Modified
439 * so that generic matrices (which can be used for glocally configured
440 * models) can be used. This function finds a single glocal domain, not a
441 * single local one.
442 *
443 * Also modified to optionally remove the Backward and OA alignment.
444 * The decision to do these is determined by three input parameters:
445 * <null2_is_done>: TRUE if we've already computed the null2 scores for
446 * this region (see Sean's notes below).
447 * <do_null2>: TRUE if we will apply a null2 penalty eventually
448 * to this domain
449 * <do_aln>: TRUE if we need the OA alignment
450 *
451 * Notes (verbatim) from p7_domaindef.c::rescore_isolated_domain():
452 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
453 * SRE, Fri Feb 8 09:18:33 2008 [Janelia]
454 *
455 * We have isolated a single domain's envelope from <i>..<j> in
456 * sequence <sq>, and now we want to score it in isolation and obtain
457 * an alignment display for it.
458 *
459 * (Later, we can add up all the individual domain scores from this
460 * seq into a new per-seq score, to compare to the original per-seq
461 * score).
462 *
463 * The caller provides model <om> configured in unilocal mode; by
464 * using unilocal (as opposed to multilocal), we're going to force the
465 * identification of a single domain in this envelope now.
466 *
467 * The alignment is an optimal accuracy alignment (sensu IH Holmes),
468 * also obtained in unilocal mode.
469 *
470 * The caller provides DP matrices <ox1> and <ox2> with sufficient
471 * space to hold Forward and Backward calculations for this domain
472 * against the model. (The caller will typically already have matrices
473 * sufficient for the complete sequence lying around, and can just use
474 * those.) The caller also provides a <P7_DOMAINDEF> object which is
475 * (efficiently, we trust) managing any necessary temporary working
476 * space and heuristic thresholds.
477 *
478 * Returns <eslOK> if a domain was successfully identified, scored,
479 * and aligned in the envelope; if so, the per-domain information is
480 * registered in <ddef>, in <ddef->dcl>.
481 *
482 * And here's what's happened to our working memory:
483 *
484 * <ddef>: <ddef->tr> has been used, and possibly reallocated, for
485 * the OA trace of the domain. Before exit, we called
486 * <Reuse()> on it.
487 *
488 * <ox1> : happens to be holding OA score matrix for the domain
489 * upon return, but that's not part of the spec; officially
490 * its contents are "undefined".
491 *
492 * <ox2> : happens to be holding a posterior probability matrix
493 * for the domain upon return, but we're not making that
494 * part of the spec, so caller shouldn't rely on this;
495 * spec just makes its contents "undefined".
496 */
497 static int
498 glocal_rescore_isolated_domain(P7_DOMAINDEF *ddef, const P7_PROFILE *gm, const ESL_SQ *sq,
499 P7_GMX *gx1, P7_GMX *gx2, int i, int j, int null2_is_done,
500 int do_null2, int do_aln)
501 {
502 P7_DOMAIN *dom = NULL;
503 int Ld = j-i+1;
504 float domcorrection = 0.0;
505 float envsc, oasc;
506 int z;
507 int pos;
508 float null2[p7_MAXCODE];
509 int status;
510
511 p7_GForward (sq->dsq + i-1, Ld, gm, gx1, &envsc);
512
513 oasc = 0.;
514 if(do_null2 || do_aln) {
515 p7_GBackward(sq->dsq + i-1, Ld, gm, gx2, NULL);
516
517 status = p7_GDecoding(gm, gx1, gx2, gx2); /* <ox2> is now overwritten with post probabilities */
518 if (status == eslERANGE) return eslFAIL; /* rare: numeric overflow; domain is assumed to be repetitive garbage [J3/119-212] */
519
520 /* Is null2 set already for this i..j? (It is, if we're in a domain that
521 * was defined by stochastic traceback clustering in a multidomain region;
522 * it isn't yet, if we're in a simple one-domain region). If it isn't,
523 * do it now, by the expectation (posterior decoding) method.
524 */
525 if ((! null2_is_done) && do_null2) {
526 p7_GNull2_ByExpectation(gm, gx2, null2);
527 for (pos = i; pos <= j; pos++)
528 ddef->n2sc[pos] = logf(null2[sq->dsq[pos]]);
529 }
530 if(do_null2) {
531 for (pos = i; pos <= j; pos++)
532 domcorrection += ddef->n2sc[pos]; /* domcorrection is in units of NATS */
533 }
534
535 if(do_aln) {
536 /* Find an optimal accuracy alignment */
537 p7_GOptimalAccuracy(gm, gx2, gx1, &oasc); /* <ox1> is now overwritten with OA scores */
538 p7_GOATrace (gm, gx2, gx1, ddef->tr); /* <tr>'s seq coords are offset by i-1, rel to orig dsq */
539
540 /* hack the trace's sq coords to be correct w.r.t. original dsq */
541 for (z = 0; z < ddef->tr->N; z++)
542 if (ddef->tr->i[z] > 0) ddef->tr->i[z] += i-1;
543 }
544 /* get ptr to next empty domain structure in domaindef's results */
545 }
546 if (ddef->ndom == ddef->nalloc) {
547 void *p;
548 ESL_RALLOC(ddef->dcl, p, sizeof(P7_DOMAIN) * (ddef->nalloc*2));
549 ddef->nalloc *= 2;
550 }
551 dom = &(ddef->dcl[ddef->ndom]);
552
553 /* store the results in it */
554 dom->ienv = i;
555 dom->jenv = j;
556 dom->envsc = envsc; /* in units of NATS */
557 dom->domcorrection = domcorrection; /* in units of NATS, will be 0. if do_null2 == FALSE */
558 dom->oasc = oasc; /* in units of expected # of correctly aligned residues, will be 0. if do_aln == FALSE */
559 dom->dombias = 0.0; /* gets set later, using bg->omega and dombias */
560 dom->bitscore = 0.0; /* gets set later by caller, using envsc, null score, and dombias */
561 dom->lnP = 1.0; /* gets set later by caller, using bitscore */
562 dom->is_reported = FALSE; /* gets set later by caller */
563 dom->is_included = FALSE; /* gets set later by caller */
564 dom->ad = NULL;
565 dom->iali = i;
566 dom->jali = j;
567
568 ddef->ndom++;
569
570 if(do_aln) {
571 p7_trace_Reuse(ddef->tr);
572 }
573 return eslOK;
574
575 ERROR:
576 if(do_aln) {
577 p7_trace_Reuse(ddef->tr);
578 }
579 return status;
580 }
581