Debian Codegraph @

src/cm_alidisplay.c

raw symbols SCIP
1 /* Formatting, transmitting, and printing single alignments to a
2 * profile.
3 *
4 * Contents:
5 * 1. The CM_ALIDISPLAY object.
6 * 2. The CM_ALIDISPLAY API.
7 * 3. Debugging/dev code.
8 */
9 #include <esl_config.h>
10 #include <p7_config.h>
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 #include "easel.h"
19 #include "esl_stack.h"
20 #include "esl_vectorops.h"
21
22 #include "hmmer.h"
23
24 #include "infernal.h"
25
26 static int bp_is_canonical(char lseq, char rseq);
27 static float post_code_to_avg_pp(char postcode);
28
29 /*****************************************************************
30 * 1. The CM_ALIDISPLAY object
31 *****************************************************************/
32
33 /* Function: cm_alidisplay_Create()
34 * Synopsis: Create an alignment display, from parsetree and model.
35 * Incept: EPN, Wed May 25 05:38:12 2011
36 * SRE, Thu May 23 13:46:09 2002 [St. Louis] (display.c:CreateFancyAli())
37 *
38 * Purpose: Given a parsetree (and the model and sequence it corresponds
39 * to), create a pairwise alignment for display; return in a CM_ALIDISPLAY
40 * structure.
41 *
42 * Args: cm - model
43 * errbuf - for error messages
44 * aln_data - CM_ALNDATA, includes parsetree, score etc.
45 * sq - the sequence, parsetree corresponds to subseq beginning at seqoffset
46 * seqoffset - position in sq which corresponds to first position in tr
47 * tau - tau used to calc HMM bands, -1.0 if bands not used
48 * elapsed_secs - time (seconds) required for alignment
49 * ret_ad - RETURN: CM_ALIDISPLAY, allocated and filled here.
50 *
51 * Returns: eslOK on success.
52 * eslFAIL on error, errbuf is filled.
53 *
54 * Xref: STL6 p.58
55 */
56 int
57 cm_alidisplay_Create(CM_t *cm, char *errbuf, CM_ALNDATA *adata, const ESL_SQ *sq, int64_t seqoffset,
58 double tau, double elapsed_secs, CM_ALIDISPLAY **ret_ad)
59 {
60 int status;
61 CM_ALIDISPLAY *ad = NULL; /* alidisplay structure we're building */
62 ESL_STACK *pda = NULL; /* pushdown automaton used to traverse trace */
63 float *act = NULL; /* [0..cm->abc->K-1], count of residue in hit */
64 int type; /* type of pda move: PDA_RESIDUE, PDA_STATE */
65 int v; /* state index */
66 int nd; /* node index */
67 int ti; /* position in trace */
68 int x; /* position in sequence */
69 int qinset, tinset; /* # consensus nt skipped by an EL, or in an EL */
70 int ninset; /* max # nt in an EL */
71 int pos; /* position in growing ali */
72 int lc, rc; /* indices for left, right pos in consensus */
73 int symi, symj;
74 char mode;
75 int lrf, rrf; /* chars in annotation line; left, right */
76 int lstr, rstr; /* chars in structure line; left, right */
77 int lcons, rcons; /* chars in consensus line; left, right */
78 int lmid, rmid; /* chars in ali quality line; left, right */
79 int lnnc, rnnc; /* chars in negative scoring noncanonical line; left right */
80 int lseq, rseq; /* chars in aligned target line; left, right */
81 int lpost, rpost; /* chars in aligned posteriors, left, right */
82 int do_left, do_right; /* flags to generate left, right */
83 float tmpsc; /* a temporary score */
84 float ppavg; /* average PP for string of contiguous EL emissions */
85 int cm_namelen, cm_acclen, cm_desclen;
86 int sq_namelen, sq_acclen, sq_desclen;
87 int len, n;
88 int len_el; /* lengths for ad->aseq_el, ad->rfline_el and ad->ppline_el vars */
89 int nw; /* number of chars written by snprintf, checked to avoid compiler warnings */
90
91 /* variables for constructing a single sequence MSA from passed-in
92 * <tr> so we can copy it's aseq[0] to ad->aseq_el (we only need
93 * this in case we output an alignment of all hits later with
94 * cm_tophits_Alignment()).
95 */
96 ESL_SQ **tmpsqA = NULL;
97 Parsetree_t **tmptrA = NULL;
98 char **tmpppA = NULL;
99 ESL_MSA *tmpmsa = NULL;
100
101 /* convenience ptrs */
102 Parsetree_t *tr = adata->tr;
103 char *ppstr = adata->ppstr;
104
105 /* Variables for possibly dealing with truncated alignments */
106 int cfrom_span; /* first model position spanned by any state in parsetree (regardless of truncation mode) */
107 int cto_span; /* final model position spanned by any state in parsetree (regardless of truncation mode) */
108 int cfrom_emit; /* first model position spanned by any state in parsetree in relevant mode
109 * (J or L for MATP&MATL, J or R for MATP&MATR) */
110 int cto_emit; /* final model position spanned by any state in parsetree in relevant mode
111 * (J or L for MATP&MATL, J or R for MATP&MATR) */
112 int have_i0; /* TRUE if first residue of source sequence is in the parsetree */
113 int have_j0; /* TRUE if final residue of source sequence is in the parsetree */
114
115 /* if alignment is in J mode (not L, R, or T) then
116 * cfrom_span == cfrom_emit and cto_span == cto_emit
117 */
118 int ntrunc_R = 0; /* num positions truncated at 5' end of alignment */
119 int wtrunc_R = 0; /* num chars for displaying 5' truncated begin */
120 int ntrunc_L = 0; /* num positions truncated at 3' end of alignment */
121 int wtrunc_L = 0; /* num chars for displaying 3' truncated begin */
122 int numwidth; /* number of chars to leave for displaying width numbers */
123 int is_left, is_right;
124
125 /* Contract check. We allow the caller to specify the alphabet they want the
126 * resulting MSA in, but it has to make sense (see next few lines). */
127 if(cm->cmcons == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_alidisplay_Create(): cm->cmcons is NULL");
128
129 /* Useful for debugging:
130 * DumpEmitMap(stdout, cm->emap, cm);
131 * ParsetreeDump(stdout, tr, cm, sq->dsq+seqoffset-1);
132 */
133
134 /* Calculate length of the alignment display (len):
135 * : J L R (alignment mode)
136 * MATP node : +2 +1 +1
137 * MATL node : +1 +1 +0
138 * MATR node : +1 +0 +1
139 * IL state : +1 +1 +0
140 * IR state : +1 +0 +1
141 * EL: : +4+w N/A N/A (w=width of length display : "*[nn]*")
142 * anything else : +0 +0 +0
143 *
144 * And if marginal mode of alignment is:
145 * L or T: + 4 + wtrunc_L where wtrunc_L is number of digits in number of 5' truncated cpos
146 * R or T: + 4 + wtrunc_R where wtrunc_R is number of digits in number of 3' truncated cpos
147 */
148 len = 0;
149 for (ti = 0; ti < tr->n; ti++) {
150 v = tr->state[ti];
151 mode = tr->mode[ti];
152 if (v == cm->M) { /* special case: local exit into EL */
153 nd = 1 + cm->ndidx[tr->state[ti-1]]; /* calculate node that EL replaced */
154 qinset = cm->cmcons->rpos[nd] - cm->cmcons->lpos[nd] + 1;
155 tinset = tr->emitr[ti] - tr->emitl[ti] + 1;
156 ninset = ESL_MAX(qinset,tinset);
157 len += 4;
158 do { len++; ninset/=10; } while (ninset); /* poor man's (int)log_10(ninset)+1 */
159 continue;
160 }
161 else {
162 nd = cm->ndidx[v];
163
164 if (cm->sttype[v] == IL_st) { is_left = TRUE; is_right = FALSE; }
165 else if(cm->sttype[v] == IR_st) { is_left = FALSE; is_right = TRUE; }
166 else if(cm->ndtype[nd] == MATP_nd) { is_left = TRUE; is_right = TRUE; }
167 else if(cm->ndtype[nd] == MATL_nd) { is_left = TRUE; is_right = FALSE; }
168 else if(cm->ndtype[nd] == MATR_nd) { is_left = FALSE; is_right = TRUE; }
169 else { is_left = FALSE; is_right = FALSE; }
170
171 if((is_left) && (mode == TRMODE_J || mode == TRMODE_L)) len++;
172 if((is_right) && (mode == TRMODE_J || mode == TRMODE_R)) len++;
173 }
174 /* ignore marginal-type local ends, (different from v1.0->v1.0.2)*/
175 }
176 /* One more step for calculating len, catch 5' and 3' truncations
177 * and treat them similar to EL for output. First determine
178 * how many positions were truncated 5' and 3'.
179 *
180 * Of course, we don't actually know how many positions were
181 * truncated. Here we guess that it is the maximum possible given
182 * the parsetree and the tr->pass_idx (pipeline pass we found the
183 * hit in). Specifically in the parsetree, the relevant data is
184 * the consensus positions spanned (lpos..rpos) by the internal
185 * truncated entry state. The guess is made in ParsetreeToCMBounds()
186 * see that function for details.
187 */
188 have_i0 = (seqoffset == 1) ? TRUE : FALSE;
189 have_j0 = ((seqoffset + tr->emitr[0] - 1) == sq->n) ? TRUE : FALSE;
190 if((status = ParsetreeToCMBounds(cm, tr, have_i0, have_j0, errbuf, &cfrom_span, &cto_span, &cfrom_emit, &cto_emit, NULL, NULL)) != eslOK) return status;
191
192 /* now determine display length required to show truncations */
193 ntrunc_R = wtrunc_R = 0;
194 ntrunc_L = wtrunc_L = 0;
195 if (cfrom_span != cfrom_emit) {
196 /* We'll put a truncated begin at the beginning of the alignment. */
197 ntrunc_R = cfrom_emit - cfrom_span;
198 ninset = ntrunc_R;
199 wtrunc_R = 0; do { wtrunc_R++; ninset/=10; } while (ninset); /* poor man's (int)log_10(ninset)+1 */
200 wtrunc_R += 4; /* space for '<[]*' */
201 len += wtrunc_R;
202 }
203 if (cto_span != cto_emit) {
204 /* We'll put a truncated begin at end of the alignment. */
205 ntrunc_L = cto_span - cto_emit;
206 ninset = ntrunc_L;
207 wtrunc_L = 0; do { wtrunc_L++; ninset/=10; } while (ninset); /* poor man's (int)log_10(ninset)+1 */
208 wtrunc_L += 4; /* space for '*[]>' */
209 len += wtrunc_L;
210 }
211 #if eslDEBUGLEVEL >= 1
212 printf("#DEBUG: cfrom_span: %4d\n", cfrom_span);
213 printf("#DEBUG: cfrom_emit: %4d\n", cfrom_emit);
214 printf("#DEBUG: cto_emit: %4d\n", cto_emit);
215 printf("#DEBUG: cto_span: %4d\n", cto_span);
216 #endif
217
218 /* Create strings of the full model and sequence used in an output
219 * alignment. We use Parsetrees2Alignment() for this to get a 1 seq MSA.
220 * This seems like it may be expensive, but it's trival compared to
221 * time required for other steps of pipeline (i.e. CM DP functions).
222 */
223 ESL_ALLOC(tmpsqA, sizeof(ESL_SQ *) * 1);
224 if((tmpsqA[0] = esl_sq_CreateDigitalFrom(cm->abc, "i", sq->dsq + seqoffset - 1, tr->emitr[0] - tr->emitl[0] + 1, NULL, NULL, NULL)) == NULL) {
225 goto ERROR;
226 }
227 ESL_ALLOC(tmptrA, sizeof(Parsetree_t *) * 1);
228 tmptrA[0] = tr;
229 if(adata->ppstr) {
230 ESL_ALLOC(tmpppA, sizeof(char *) * 1);
231 tmpppA[0] = adata->ppstr;
232 }
233 if((status = Parsetrees2Alignment(cm, errbuf, cm->abc, tmpsqA, NULL, tmptrA, tmpppA, 1, NULL, NULL, TRUE, FALSE, /*allow_trunc=*/FALSE, &tmpmsa)) != eslOK) goto ERROR;
234 esl_sq_Destroy(tmpsqA[0]);
235 free(tmpsqA); tmpsqA = NULL;
236 free(tmptrA); tmptrA = NULL; /* don't free tmptrA[0], it was just used as a pointer */
237 if(adata->ppstr) { free(tmpppA); tmpppA = NULL; } /* don't free tmpppA[0], it was just meant as a pointer */
238 len_el = tmpmsa->alen;
239
240 /* Now we know the length of all arrays (len), determine total amount of memory required, and allocate it */
241 /* Allocate the char arrays */
242
243 n = (len+1) * 5; /* model, csline, mline, aseq, ncline */
244 n += 2 * (len_el+1); /* aseq_el, rfline_el (includes EL emits) */
245 if(adata->ppstr != NULL) n += len+1 + len_el+1; /* ppline and ppline_el */
246 if(cm->rf != NULL) n += len+1;
247 cm_namelen = strlen(cm->name); n += cm_namelen + 1;
248 cm_acclen = (cm->acc != NULL ? strlen(cm->acc) : 0); n += cm_acclen + 1;
249 cm_desclen = (cm->desc != NULL ? strlen(cm->desc) : 0); n += cm_desclen + 1;
250 sq_namelen = strlen(sq->name); n += sq_namelen + 1;
251 sq_acclen = strlen(sq->acc); n += sq_acclen + 1; /* sq->acc is "\0" when unset */
252 sq_desclen = strlen(sq->desc); n += sq_desclen + 1; /* sq->desc is "\0" when unset */
253
254 ESL_ALLOC(ad, sizeof(CM_ALIDISPLAY));
255 ad->mem = NULL;
256 ad->memsize = sizeof(char) * n;
257 ad->sc = adata->sc;
258 ad->avgpp = adata->pp;
259 ad->tau = tau;
260 ad->matrix_Mb = adata->mb_tot;
261 ad->elapsed_secs = elapsed_secs;
262 ad->hmmonly = FALSE;
263 ad->N = len;
264 ad->N_el = len_el;
265
266 /* and finally, space for the unaligned sequence (only nec b/c the
267 * aseq will not include the residues from EL emissions).
268 */
269
270 pos = 0;
271 ESL_ALLOC(ad->mem, ad->memsize);
272 if (cm->rf != NULL) { ad->rfline = ad->mem + pos; pos += len+1;} else { ad->rfline = NULL; }
273 ad->ncline = ad->mem + pos; pos += len+1;
274 ad->csline = ad->mem + pos; pos += len+1;
275 ad->model = ad->mem + pos; pos += len+1;
276 ad->mline = ad->mem + pos; pos += len+1;
277 ad->aseq = ad->mem + pos; pos += len+1;
278 if (adata->ppstr != NULL) { ad->ppline = ad->mem + pos; pos += len+1;} else { ad->ppline = NULL; }
279 ad->aseq_el = ad->mem + pos; pos += len_el+1;
280 ad->rfline_el = ad->mem + pos; pos += len_el+1;
281 if (adata->ppstr != NULL) { ad->ppline_el = ad->mem + pos; pos += len_el+1;} else { ad->ppline_el = NULL; }
282 ad->cmname = ad->mem + pos; pos += cm_namelen +1;
283 ad->cmacc = ad->mem + pos; pos += cm_acclen +1;
284 ad->cmdesc = ad->mem + pos; pos += cm_desclen +1;
285 ad->sqname = ad->mem + pos; pos += sq_namelen +1;
286 ad->sqacc = ad->mem + pos; pos += sq_acclen +1;
287 ad->sqdesc = ad->mem + pos; pos += sq_desclen +1;
288
289 /* Set name, acc, desc char arrays */
290 strcpy(ad->cmname, cm->name);
291 if (cm->acc != NULL) strcpy(ad->cmacc, cm->acc); else ad->cmacc[0] = 0;
292 if (cm->desc != NULL) strcpy(ad->cmdesc, cm->desc); else ad->cmdesc[0] = 0;
293 strcpy(ad->sqname, sq->name);
294 strcpy(ad->sqacc, sq->acc);
295 strcpy(ad->sqdesc, sq->desc);
296
297 /* Set aseq_el and possibly ppline_el */
298 strcpy(ad->aseq_el, tmpmsa->aseq[0]);
299 strcpy(ad->rfline_el, tmpmsa->rf);
300 if(adata->ppstr) strcpy(ad->ppline_el, tmpmsa->pp[0]);
301
302 /* Set clen */
303 ad->clen = cm->clen;
304
305 /* Allocate and initialize.
306 * Blank the annotation lines (memset calls) - only needed
307 * because of the way we deal w/ EL.
308 */
309 if (cm->rf != NULL) memset(ad->rfline, ' ', ad->N);
310 memset(ad->ncline, ' ', ad->N);
311 memset(ad->csline, ' ', ad->N);
312 memset(ad->model, ' ', ad->N);
313 memset(ad->mline, ' ', ad->N);
314 memset(ad->aseq, ' ', ad->N);
315 if(adata->ppstr != NULL) memset(ad->ppline, ' ', ad->N);
316
317 /* Fill in the lines: traverse the traceback.
318 */
319 pos = 0;
320
321 /* Before we start on the stack, add truncated begin info at the
322 * beginning of the alignment display, if nec (if tr->mode[0] is R
323 * or T).
324 */
325 if(ntrunc_R > 0) {
326 /* wtrunc_R and ntrunc_R were calc'ed above */
327 memset(ad->csline+pos, '~', wtrunc_R);
328 if((nw = snprintf(ad->model+pos, wtrunc_R+1, "<[%*d]*", wtrunc_R-4, ntrunc_R)) != wtrunc_R) goto ERROR; /* +1 in size for null terminating char */
329 if((nw = snprintf(ad->aseq+pos, wtrunc_R+1, "<[%*s]*", wtrunc_R-4, "0")) != wtrunc_R) goto ERROR; /* +1 in size for null terminating char */
330 if(adata->ppstr != NULL) {
331 for(x = 0; x < wtrunc_R; x++) ad->ppline[pos+x] = '.';
332 }
333 pos += wtrunc_R;
334 }
335
336 if((pda = esl_stack_ICreate()) == NULL) goto ERROR;
337 if((status = esl_stack_IPush(pda, 0)) != eslOK) goto ERROR;
338 if((status = esl_stack_IPush(pda, PDA_STATE)) != eslOK) goto ERROR;
339 while (esl_stack_IPop(pda, &type) != eslEOD)
340 {
341 if (type == PDA_RESIDUE) {
342 if (cm->rf != NULL) {
343 esl_stack_IPop(pda, &rrf);
344 ad->rfline[pos] = rrf;
345 }
346 esl_stack_IPop(pda, &rnnc); ad->ncline[pos] = rnnc;
347 esl_stack_IPop(pda, &rstr); ad->csline[pos] = rstr;
348 esl_stack_IPop(pda, &rcons); ad->model[pos] = rcons;
349 esl_stack_IPop(pda, &rmid); ad->mline[pos] = rmid;
350 esl_stack_IPop(pda, &rseq); ad->aseq[pos] = rseq;
351 if(ppstr != NULL) {
352 esl_stack_IPop(pda, &rpost); ad->ppline[pos] = rpost;
353 }
354 pos++;
355 continue;
356 }
357
358 /* Else, we're PDA_STATE - e.g. dealing with a trace node.
359 */
360 esl_stack_IPop(pda, &ti);
361 v = tr->state[ti];
362
363 /* Deal with EL (local ends, state M) as a special case.
364 */
365 if (v == cm->M) {
366 nd = 1 + cm->ndidx[tr->state[ti-1]]; /* calculate node that EL replaced */
367 qinset = cm->cmcons->rpos[nd] - cm->cmcons->lpos[nd] + 1;
368 tinset = tr->emitr[ti] - tr->emitl[ti] + 1;
369 ninset = ESL_MAX(qinset,tinset);
370 numwidth = 0; do { numwidth++; ninset/=10; } while (ninset); /* poor man's (int)log_10(ninset)+1 */
371 memset(ad->csline+pos, '~', numwidth+4);
372 if((nw = snprintf(ad->model+pos, numwidth+5, "*[%*d]*", numwidth, qinset)) != (numwidth+4)) goto ERROR; /* +5 is length of "*[" + "]*" + 1 for null terminating char */
373 if((nw = snprintf(ad->aseq+pos, numwidth+5, "*[%*d]*", numwidth, tinset)) != (numwidth+4)) goto ERROR; /* +5 is length of "*[" + "]*" + 1 for null terminating char */
374 if(cm->rf != NULL) memset(ad->rfline+pos, '~', numwidth+4);
375 if(adata->ppstr != NULL) {
376 /* calculate the single character PP code for the average posterior
377 * by averaging the average for the PP codes in ppstr (this gives
378 * the correct PP code (I didn't think it was guaranteed to at first
379 * but a simulation I performed couldn't produce a counterexample!
380 * xref: ~nawrockie/notebook/12_0423_inf_final_stress_tests/00LOG; May 22, 2012
381 */
382 ppavg = 0.;
383 for(x = tr->emitl[ti]; x <= tr->emitr[ti]; x++) {
384 ppavg += post_code_to_avg_pp(adata->ppstr[x-1]);
385 }
386 ppavg /= (float) (tr->emitr[ti] - tr->emitl[ti] + 1);
387 ad->ppline[pos] = '.';
388 ad->ppline[pos+1] = '.';
389 for(x = 0; x < (numwidth-1); x++) ad->ppline[pos+2+x] = '.';
390 if(tr->emitl[ti] <= tr->emitr[ti]) {
391 ad->ppline[pos+numwidth+1] = (ppavg + 0.05 >= 1.0) ? '*' : (char) ((ppavg + 0.05) * 10.0) + '0';
392 }
393 else { /* no residues emitted in EL, PP annotation is a gap ('.') */
394 ad->ppline[pos+numwidth+1] = '.';
395 }
396 ad->ppline[pos+numwidth+2] = '.';
397 ad->ppline[pos+numwidth+3] = '.';
398 }
399 pos += 4 + numwidth;
400 continue;
401 }
402
403 /* Fetch some info into tmp variables, for "clarity"
404 */
405 nd = cm->ndidx[v]; /* what CM node we're in */
406 lc = cm->cmcons->lpos[nd]; /* where CM node aligns to in consensus (left) */
407 rc = cm->cmcons->rpos[nd]; /* where CM node aligns to in consensus (right) */
408 symi = sq->dsq[tr->emitl[ti] + (seqoffset-1)]; /* residue indices that node is aligned to (left) */
409 symj = sq->dsq[tr->emitr[ti] + (seqoffset-1)]; /* residue indices that node is aligned to (right) */
410 if(ppstr != NULL) { /* posterior codes are indexed 0..alen-1, off-by-one w.r.t dsq */
411 lpost = '.'; /* init to gap, if it corresponds to a residue, we'll reset it below */
412 rpost = '.'; /* init to gap, if it corresponds to a residue, we'll reset it below */
413 }
414 mode = tr->mode[ti];
415
416 /* Calculate four of the six lines: rfline, csline, model, and aseq.
417 */
418 do_left = do_right = FALSE;
419 if (cm->sttype[v] == IL_st) {
420 if(mode == TRMODE_J || mode == TRMODE_L) {
421 /* careful, its impt the above 2 'if's are separated, we don't want to
422 * enter the nearest 'else' below if we're an IL (specifically a MATP_IL
423 * with mode==TRMODE_R).
424 */
425 do_left = TRUE;
426 if (cm->rf != NULL) lrf = '.';
427 lstr = '.';
428 lcons = '.';
429 lseq = tolower((int) cm->abc->sym[symi]);
430 if(ppstr != NULL) lpost = ppstr[tr->emitl[ti]-1]; /* watch off-by-one b/t ppstr and dsq */
431 }
432 }
433 else if (cm->sttype[v] == IR_st) {
434 if (mode == TRMODE_J || mode == TRMODE_R) {
435 /* careful, its impt the above 2 'if's are separated, we don't want to
436 * enter the nearest 'else' below if we're an IR (specifically a MATP_IR
437 * with mode==TRMODE_L).
438 */
439 do_right = TRUE;
440 if (cm->rf != NULL) rrf = '.';
441 rstr = '.';
442 rcons = '.';
443 rseq = tolower((int) cm->abc->sym[symj]);
444 if(ppstr != NULL) rpost = ppstr[tr->emitr[ti]-1]; /* watch off-by-one b/t ppstr and dsq */
445 }
446 }
447 else {
448 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd) && (mode == TRMODE_J || mode == TRMODE_L)) {
449 do_left = TRUE;
450 if (cm->rf != NULL) lrf = cm->rf[lc+1];
451 lstr = cm->cmcons->cstr[lc];
452 lcons = (cm->flags & CMH_CONS) ? cm->consensus[(lc+1)] : cm->cmcons->cseq[lc];
453 if (cm->sttype[v] == MP_st || cm->sttype[v] == ML_st) {
454 lseq = cm->abc->sym[symi];
455 if(ppstr != NULL) lpost = ppstr[tr->emitl[ti]-1]; /* watch off-by-one b/t ppstr and dsq */
456 }
457 else {
458 lseq = '-';
459 if(ppstr != NULL) lpost = '.';
460 }
461 }
462 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) && (mode == TRMODE_J || mode == TRMODE_R)) {
463 do_right = TRUE;
464 if (cm->rf != NULL) rrf = cm->rf[rc+1];
465 rstr = cm->cmcons->cstr[rc];
466 rcons = (cm->flags & CMH_CONS) ? cm->consensus[(rc+1)] : cm->cmcons->cseq[rc];
467 if (cm->sttype[v] == MP_st || cm->sttype[v] == MR_st) {
468 rseq = cm->abc->sym[symj];
469 if(ppstr != NULL) rpost = ppstr[tr->emitr[ti]-1]; /* watch off-by-one b/t ppstr and dsq */
470 }
471 else {
472 rseq = '-';
473 if(ppstr != NULL) rpost = '.';
474 }
475 }
476 }
477
478 /* Use emission p and score to set lmid, rmid line for emitting states.
479 */
480 lmid = rmid = ' ';
481 lnnc = rnnc = ' ';
482 if (cm->sttype[v] == MP_st) {
483 if (mode == TRMODE_L) {
484 if(lseq == toupper(lcons)) lmid = lseq;
485 lnnc = '?';
486 }
487 else if (mode == TRMODE_R) {
488 if(rseq == toupper(rcons)) rmid = rseq;
489 rnnc = '?';
490 }
491 else if (mode == TRMODE_J) {
492 tmpsc = DegeneratePairScore(cm->abc, cm->esc[v], symi, symj);
493 if (lseq == toupper(lcons) && rseq == toupper(rcons)) {
494 lmid = lseq;
495 rmid = rseq;
496 }
497 else if (tmpsc >= 0) {
498 lmid = rmid = ':';
499 }
500 /* determine lnnc, rnnc for optional negative scoring
501 * non-canonical annotation, they are 'v' if lseq and rseq
502 * are a negative scoring non-canonical (not a
503 * AU,UA,GC,CG,GU,UG) pair.
504 */
505 if (tmpsc < 0 && (! bp_is_canonical(lseq, rseq))) {
506 lnnc = rnnc = 'v';
507 }
508 }
509 }
510 else if ((cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) &&
511 (mode == TRMODE_J || mode == TRMODE_L)) {
512 if (lseq == toupper(lcons)) {
513 lmid = lseq;
514 }
515 else if(esl_abc_FAvgScore(cm->abc, symi, cm->esc[v]) > 0) {
516 lmid = '+';
517 }
518 }
519 else if ((cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) &&
520 (mode == TRMODE_J || mode == TRMODE_R)) {
521 if (rseq == toupper(rcons)) {
522 rmid = rseq;
523 }
524 else if(esl_abc_FAvgScore(cm->abc, symj, cm->esc[v]) > 0) {
525 rmid = '+';
526 }
527 }
528 if((cm->stid[v] == MATP_ML || cm->stid[v] == MATP_MR) && mode == TRMODE_J) {
529 lnnc = rnnc = 'v'; /* mark non-truncated half base-pairs (MATP_ML or MATP_MR) with 'v' */
530 }
531 if(cm->stid[v] == MATP_ML && mode == TRMODE_L) {
532 lnnc = '?'; /* right half is truncated away, we're not sure if its canonical or not */
533 }
534 if(cm->stid[v] == MATP_MR && mode == TRMODE_R) {
535 rnnc = '?'; /* left half is truncated away, we're not sure if its canonical or not */
536 }
537 /* If we're storing a residue leftwise - just do it.
538 * If rightwise - push it onto stack.
539 */
540 if (do_left) {
541 if (cm->rf != NULL) ad->rfline[pos] = lrf;
542 ad->ncline[pos] = lnnc;
543 ad->csline[pos] = lstr;
544 ad->model[pos] = lcons;
545 ad->mline[pos] = lmid;
546 ad->aseq[pos] = lseq;
547 if(ppstr != NULL) ad->ppline[pos] = lpost;
548 pos++;
549 }
550 if (do_right) {
551 if(ppstr != NULL) {
552 if ((status = esl_stack_IPush(pda, (int) rpost)) != eslOK) goto ERROR;
553 }
554 if ((status = esl_stack_IPush(pda, (int) rseq)) != eslOK) goto ERROR;
555 if ((status = esl_stack_IPush(pda, (int) rmid)) != eslOK) goto ERROR;
556 if ((status = esl_stack_IPush(pda, (int) rcons)) != eslOK) goto ERROR;
557 if ((status = esl_stack_IPush(pda, (int) rstr)) != eslOK) goto ERROR;
558 if ((status = esl_stack_IPush(pda, (int) rnnc)) != eslOK) goto ERROR;
559 if (cm->rf != NULL) if ((status = esl_stack_IPush(pda, (int) rrf)) != eslOK) goto ERROR;
560 if ((status = esl_stack_IPush(pda, PDA_RESIDUE)) != eslOK) goto ERROR;
561 }
562
563 /* Push the child trace nodes onto the PDA;
564 * right first, so it pops last.
565 */
566 if (tr->nxtr[ti] != -1) {
567 if ((status = esl_stack_IPush(pda, tr->nxtr[ti])) != eslOK) goto ERROR;
568 if ((status = esl_stack_IPush(pda, PDA_STATE)) != eslOK) goto ERROR;
569 }
570 if (tr->nxtl[ti] != -1) {
571 if ((status = esl_stack_IPush(pda, tr->nxtl[ti])) != eslOK) goto ERROR;
572 if ((status = esl_stack_IPush(pda, PDA_STATE)) != eslOK) goto ERROR;
573 }
574 else if (cm->sttype[v] != E_st) {
575 ; /* marginal type local end, do nothing */
576 }
577 } /* end loop over the PDA; PDA now empty */
578
579 /* Final step, add truncated begin info at the end of the alignment
580 * display, if nec (if tr->mode[0] is L or T).
581 */
582 if(ntrunc_L > 0) {
583 /* wtrunc_L and ntrunc_L were calc'ed above */
584 memset(ad->csline+pos, '~', wtrunc_L);
585 if((nw = snprintf(ad->model+pos, wtrunc_L+1, "*[%*d]>", wtrunc_L-4, ntrunc_L)) != wtrunc_L) goto ERROR; /* +1 in size for null terminating char */
586 if((nw = snprintf(ad->aseq+pos, wtrunc_L+1, "*[%*s]>", wtrunc_L-4, "0")) != wtrunc_L) goto ERROR; /* +1 in size for null terminating char */
587 if(adata->ppstr != NULL) {
588 for(x = 0; x < wtrunc_L; x++) ad->ppline[pos+x] = '.';
589 }
590 pos += wtrunc_L;
591 }
592
593 /* Truly final step, calculate GC frequency, we could do this
594 * inside the parstree traversal, but this is easier and can't
595 * be much slower given that we'd need to check if we were an
596 * emitter before calling, or count gaps as well.
597 */
598 ESL_ALLOC(act, sizeof(float) * cm->abc->K);
599 esl_vec_FSet(act, cm->abc->K, 0.);
600 for(x = tr->emitl[0] + seqoffset - 1; x <= tr->emitr[0] + seqoffset - 1; x++) {
601 esl_abc_FCount(cm->abc, act, sq->dsq[x], 1.0);
602 }
603
604 if(cm->rf != NULL) ad->rfline[ad->N] = '\0';
605 ad->ncline[ad->N] = '\0';
606 ad->csline[ad->N] = '\0';
607 ad->model[ad->N] = '\0';
608 ad->mline[ad->N] = '\0';
609 ad->aseq[ad->N] = '\0';
610 if(ppstr != NULL) ad->ppline[ad->N] = '\0';
611 ad->sqfrom = tr->emitl[0] + seqoffset-1;
612 ad->sqto = tr->emitr[0] + seqoffset-1;
613 ad->cfrom_emit = cfrom_emit;
614 ad->cto_emit = cto_emit;
615 ad->cfrom_span = cfrom_span;
616 ad->cto_span = cto_span;
617 ad->gc = (act[1] + act[2]) / (float) (tr->emitr[0]- tr->emitl[0] + 1);
618
619 esl_stack_Destroy(pda);
620 if(tmpmsa != NULL) esl_msa_Destroy(tmpmsa);
621 if(act != NULL) free(act);
622 if(ret_ad != NULL) *ret_ad = ad;
623 return eslOK;
624
625 ERROR:
626 if(pda != NULL) esl_stack_Destroy(pda);
627 if(ad != NULL) cm_alidisplay_Destroy(ad);
628 if(act != NULL) free(act);
629 if(ret_ad != NULL) *ret_ad = NULL;
630 if(status == eslEMEM) ESL_FAIL(status, errbuf, "cm_alidisplay_Create() out of memory");
631 return status; /* errbuf filled some other way */
632 }
633
634 /* Function: cm_alidisplay_CreateFromP7()
635 * Synopsis: Create a CM_ALIDISPLAY from a P7_ALIDISPLAY.
636 * Incept: EPN, Mon Apr 9 12:46:07 2012
637 * SRE, Thu May 23 13:46:09 2002 [St. Louis] (display.c:CreateFancyAli())
638 *
639 * Purpose: Given a P7_ALIDISPLAY, create a CM_ALIDISPLAY from it.
640 * A copy of all relevant strings is made. This function
641 * was written for special pipeline runs which use an
642 * HMM only.
643 *
644 * Args: cm - model
645 * errbuf - for error messages
646 * sq - the sequence, parsetree corresponds to subseq beginning at seqoffset
647 * seqoffset - position in sq which corresponds to first position in tr
648 * p7sc - score of envelope p7ad was derived from
649 * p7pp - avg pp of all aligned residues in envelope P7_ALIDISPLAY was derived from
650 * p7ad - P7_ALIDISPLAY to convert
651 * ret_ad - RETURN: CM_ALIDISPLAY, allocated and filled here.
652 *
653 * Returns: eslOK on success.
654 * eslFAIL on error, errbuf is filled.
655 *
656 * Xref: STL6 p.58
657 */
658 int
659 cm_alidisplay_CreateFromP7(CM_t *cm, char *errbuf, const ESL_SQ *sq, int64_t seqoffset, float p7sc, float p7pp, P7_ALIDISPLAY *p7ad, CM_ALIDISPLAY **ret_ad)
660 {
661 int status;
662 CM_ALIDISPLAY *ad = NULL; /* alidisplay structure we're building */
663 int n;
664 int cm_namelen, cm_acclen, cm_desclen;
665 int sq_namelen, sq_acclen, sq_desclen;
666 int len;
667 int len_el;
668 int pos; /* position in ad->mem */
669 int x; /* residue position */
670 float *act = NULL; /* [0..cm->abc->K-1], count of residue in hit */
671 int n5p_skipped = 0; /* number of 5' match positions skipped (p7ad->hmmfrom-1) */
672 int n3p_skipped = 0; /* number of 3' match positions skipped (p7ad->M - p7ad->hmmto) */
673
674 len = p7ad->N;
675 n5p_skipped = p7ad->hmmfrom -1;
676 n3p_skipped = p7ad->M - p7ad->hmmto;
677 len_el = len + n5p_skipped + n3p_skipped;
678
679 /* Allocate the char arrays, we copy aseq into aseq_el, rf into
680 * rfline_el and ppline into ppline_el for consistency with
681 * cm_alidisplay_Create()
682 */
683 n = (len+1) * 4; /* model, csline, mline, aseq (ncline will remain NULL) */
684 n += 2 * (len_el+1); /* aseq_el and rfline_el, mandatory */
685 if(p7ad->ppline != NULL) n += len+1 + len_el+1; /* ppline and ppline_el */
686 if(p7ad->rfline != NULL) n += len+1;
687 cm_namelen = strlen(cm->name); n += cm_namelen + 1;
688 cm_acclen = (cm->acc != NULL ? strlen(cm->acc) : 0); n += cm_acclen + 1;
689 cm_desclen = (cm->desc != NULL ? strlen(cm->desc) : 0); n += cm_desclen + 1;
690 sq_namelen = strlen(sq->name); n += sq_namelen + 1;
691 sq_acclen = strlen(sq->acc); n += sq_acclen + 1; /* sq->acc is "\0" when unset */
692 sq_desclen = strlen(sq->desc); n += sq_desclen + 1; /* sq->desc is "\0" when unset */
693
694 ESL_ALLOC(ad, sizeof(CM_ALIDISPLAY));
695 ad->mem = NULL;
696 ad->memsize = sizeof(char) * n;
697 ad->sc = p7sc;
698 ad->avgpp = p7pp;
699 ad->tau = -1.0;
700 ad->matrix_Mb = 0.0; /* unknown */
701 ad->elapsed_secs = 0.0; /* unknown */
702 ad->hmmonly = TRUE;
703 ad->N = len;
704 ad->N_el = len + n5p_skipped + n3p_skipped;
705
706 ad->clen = cm->clen;
707
708 ad->sqfrom = p7ad->sqfrom;
709 ad->sqto = p7ad->sqto;
710 ad->cfrom_emit = p7ad->hmmfrom;
711 ad->cto_emit = p7ad->hmmto;
712 ad->cfrom_span = p7ad->hmmfrom;
713 ad->cto_span = p7ad->hmmto;
714
715 /* calculate GC frequency */
716 ESL_ALLOC(act, sizeof(float) * cm->abc->K);
717 esl_vec_FSet(act, cm->abc->K, 0.);
718 for(x = p7ad->sqfrom; x <= p7ad->sqto; x++) esl_abc_FCount(cm->abc, act, sq->dsq[x], 1.0);
719 ad->gc = (act[1] + act[2]) / (float) (p7ad->sqto - p7ad->sqfrom + 1);
720
721 pos = 0;
722 ESL_ALLOC(ad->mem, ad->memsize);
723 if (p7ad->rfline != NULL) { ad->rfline = ad->mem + pos; pos += len+1;} else { ad->rfline = NULL; }
724 ad->ncline = NULL; /* not printed in HMM hits */
725 ad->csline = ad->mem + pos; pos += len+1;
726 ad->model = ad->mem + pos; pos += len+1;
727 ad->mline = ad->mem + pos; pos += len+1;
728 ad->aseq = ad->mem + pos; pos += len+1;
729 if (p7ad->ppline != NULL) { ad->ppline = ad->mem + pos; pos += len+1;} else { ad->ppline = NULL; }
730 ad->aseq_el = ad->mem + pos; pos += len_el+1;
731 ad->rfline_el = ad->mem + pos; pos += len_el+1;
732 if (p7ad->ppline != NULL) { ad->ppline_el = ad->mem + pos; pos += len_el+1;} else { ad->ppline_el = NULL; }
733 ad->cmname = ad->mem + pos; pos += cm_namelen +1;
734 ad->cmacc = ad->mem + pos; pos += cm_acclen +1;
735 ad->cmdesc = ad->mem + pos; pos += cm_desclen +1;
736 ad->sqname = ad->mem + pos; pos += sq_namelen +1;
737 ad->sqacc = ad->mem + pos; pos += sq_acclen +1;
738 ad->sqdesc = ad->mem + pos; pos += sq_desclen +1;
739
740 /* Set name, acc, desc char arrays */
741 strcpy(ad->cmname, cm->name);
742 if (cm->acc != NULL) strcpy(ad->cmacc, cm->acc); else ad->cmacc[0] = 0;
743 if (cm->desc != NULL) strcpy(ad->cmdesc, cm->desc); else ad->cmdesc[0] = 0;
744 strcpy(ad->sqname, sq->name);
745 strcpy(ad->sqacc, sq->acc);
746 strcpy(ad->sqdesc, sq->desc);
747
748 /* Copy strings from p7ad */
749 if(p7ad->rfline) strcpy(ad->rfline, p7ad->rfline);
750 strcpy(ad->csline, p7ad->csline);
751 strcpy(ad->model, p7ad->model);
752 strcpy(ad->mline, p7ad->mline);
753 strcpy(ad->aseq, p7ad->aseq);
754 if(p7ad->ppline) strcpy(ad->ppline, p7ad->ppline);
755
756 /* Create aseq_el, rfline_el, and ppline_el. aseq_el and ppline_el
757 * are copies of p7ad->aseq p7ad->ppline with n5p_skipped '-'
758 * characters prepended and n3p_skipped '-' characters appended.
759 * rfline_el is a copy of p7ad->rfline but with 'x' instead
760 * '-' (to indicate the skipped positions were match positions).
761 */
762 for(x = 0; x < n5p_skipped; x++) ad->aseq_el[x] = '-';
763 memcpy(ad->aseq_el + n5p_skipped, p7ad->aseq, ad->N);
764 for(x = ad->N + n5p_skipped; x < ad->N_el; x++) ad->aseq_el[x] = '-';
765 ad->aseq_el[ad->N_el] = '\0';
766
767 for(x = 0; x < n5p_skipped; x++) ad->rfline_el[x] = 'x';
768 /* copy p7ad->model NOT p7ad->rfline into p7ad->rfline_el (p7ad->rfline is not mandatory) */
769 memcpy(ad->rfline_el + n5p_skipped, p7ad->model, ad->N);
770 for(x = ad->N + n5p_skipped; x < ad->N_el; x++) ad->rfline_el[x] = 'x';
771 ad->rfline_el[ad->N_el] = '\0';
772
773 if(p7ad->ppline) {
774 for(x = 0; x < n5p_skipped; x++) ad->ppline_el[x] = '-';
775 memcpy(ad->ppline_el + n5p_skipped, p7ad->ppline, ad->N);
776 for(x = ad->N + n5p_skipped; x < ad->N_el; x++) ad->ppline_el[x] = '-';
777 ad->ppline_el[ad->N_el] = '\0';
778 }
779 else {
780 ad->ppline_el[0] = '\0';
781 }
782
783 if(act != NULL) free(act);
784 if(ret_ad != NULL) *ret_ad = ad;
785 return eslOK;
786
787 ERROR:
788 if(act != NULL) free(act);
789 if(ret_ad != NULL) *ret_ad = NULL;
790 if(status == eslEMEM) ESL_FAIL(status, errbuf, "cm_alidisplay_CreateFromP7() out of memory");
791 return status; /* errbuf filled some other way */
792 }
793
794 /* Function: cm_alidisplay_Clone()
795 * Synopsis: Make a duplicate of an ALIDISPLAY.
796 *
797 * Purpose: Create a duplicate of alignment display <ad>.
798 * Return a pointer to the duplicate. Caller
799 * is responsible for freeing the new object.
800 *
801 * Returns: pointer to new <CM_ALIDISPLAY>
802 *
803 * Throws: <NULL> on allocation failure.
804 */
805 CM_ALIDISPLAY *
806 cm_alidisplay_Clone(const CM_ALIDISPLAY *ad)
807 {
808 CM_ALIDISPLAY *ad2 = NULL;
809 int status;
810
811 ESL_ALLOC(ad2, sizeof(CM_ALIDISPLAY));
812 ad2->rfline = ad2->ncline = ad2->csline = ad2->model = ad2->mline = ad2->aseq = ad2->ppline = NULL;
813 ad2->cmname = ad2->cmacc = ad2->cmdesc = NULL;
814 ad2->sqname = ad2->sqacc = ad2->sqdesc = NULL;
815 ad2->aseq_el = ad2->rfline_el = ad2->ppline_el = NULL;
816 ad2->mem = NULL;
817 ad2->memsize = 0;
818
819 if (ad->memsize) /* serialized */
820 {
821 ESL_ALLOC(ad2->mem, sizeof(char) * ad->memsize);
822 ad2->memsize = ad->memsize;
823 memcpy(ad2->mem, ad->mem, ad->memsize);
824
825 ad2->rfline = (ad->rfline ? ad2->mem + (ad->rfline - ad->mem) : NULL );
826 ad2->ncline = (ad->ncline ? ad2->mem + (ad->ncline - ad->mem) : NULL );
827 ad2->csline = ad2->mem + (ad->csline - ad->mem);
828 ad2->model = ad2->mem + (ad->model - ad->mem);
829 ad2->mline = ad2->mem + (ad->mline - ad->mem);
830 ad2->aseq = ad2->mem + (ad->aseq - ad->mem);
831 ad2->ppline = (ad->ppline ? ad2->mem + (ad->ppline - ad->mem) : NULL );
832 ad2->aseq_el = ad2->mem + (ad->aseq_el - ad->mem);
833 ad2->rfline_el = ad2->mem + (ad->rfline_el - ad->mem);
834 ad2->ppline_el = (ad->ppline_el ? ad2->mem + (ad->ppline_el - ad->mem) : NULL );
835 ad2->N = ad->N;
836 ad2->N_el = ad->N_el;
837
838 ad2->cmname = ad2->mem + (ad->cmname - ad->mem);
839 ad2->cmacc = ad2->mem + (ad->cmacc - ad->mem);
840 ad2->cmdesc = ad2->mem + (ad->cmdesc - ad->mem);
841 ad2->cfrom_emit = ad->cfrom_emit;
842 ad2->cto_emit = ad->cto_emit;
843 ad2->cfrom_span = ad->cfrom_span;
844 ad2->cto_span = ad->cto_span;
845 ad2->clen = ad->clen;
846
847 ad2->sqname = ad2->mem + (ad->sqname - ad->mem);
848 ad2->sqacc = ad2->mem + (ad->sqacc - ad->mem);
849 ad2->sqdesc = ad2->mem + (ad->sqdesc - ad->mem);
850 ad2->sqfrom = ad->sqfrom;
851 ad2->sqto = ad->sqto;
852 }
853 else /* deserialized */
854 {
855 if ( esl_strdup(ad->rfline, -1, &(ad2->rfline)) != eslOK) goto ERROR;
856 if ( esl_strdup(ad->ncline, -1, &(ad2->ncline)) != eslOK) goto ERROR;
857 if ( esl_strdup(ad->csline, -1, &(ad2->csline)) != eslOK) goto ERROR;
858 if ( esl_strdup(ad->model, -1, &(ad2->model)) != eslOK) goto ERROR;
859 if ( esl_strdup(ad->mline, -1, &(ad2->mline)) != eslOK) goto ERROR;
860 if ( esl_strdup(ad->aseq, -1, &(ad2->aseq)) != eslOK) goto ERROR;
861 if ( esl_strdup(ad->ppline, -1, &(ad2->ppline)) != eslOK) goto ERROR;
862 if ( esl_strdup(ad->aseq_el, -1, &(ad2->aseq_el)) != eslOK) goto ERROR;
863 if ( esl_strdup(ad->rfline_el, -1, &(ad2->rfline_el)) != eslOK) goto ERROR;
864 if ( esl_strdup(ad->ppline_el, -1, &(ad2->ppline_el)) != eslOK) goto ERROR;
865 ad2->N = ad->N;
866 ad2->N_el = ad->N_el;
867
868 if ( esl_strdup(ad->cmname, -1, &(ad2->cmname)) != eslOK) goto ERROR;
869 if ( esl_strdup(ad->cmacc, -1, &(ad2->cmacc)) != eslOK) goto ERROR;
870 if ( esl_strdup(ad->cmdesc, -1, &(ad2->cmdesc)) != eslOK) goto ERROR;
871 ad2->cfrom_emit = ad->cfrom_emit;
872 ad2->cto_emit = ad->cto_emit;
873 ad2->cfrom_span = ad->cfrom_span;
874 ad2->cto_span = ad->cto_span;
875 ad2->clen = ad->clen;
876
877 if ( esl_strdup(ad->sqname, -1, &(ad2->sqname)) != eslOK) goto ERROR;
878 if ( esl_strdup(ad->sqacc, -1, &(ad2->sqacc)) != eslOK) goto ERROR;
879 if ( esl_strdup(ad->sqdesc, -1, &(ad2->sqdesc)) != eslOK) goto ERROR;
880 ad2->sqfrom = ad->sqfrom;
881 ad2->sqto = ad->sqto;
882 }
883
884 /* other data */
885 ad2->sc = ad->sc;
886 ad2->avgpp = ad->avgpp;
887 ad2->gc = ad->gc;
888 ad2->tau = ad->tau;
889 ad2->matrix_Mb = ad->matrix_Mb;
890 ad2->elapsed_secs = ad->elapsed_secs;
891 ad2->hmmonly = ad->hmmonly;
892
893 return ad2;
894
895 ERROR:
896 if (ad2) cm_alidisplay_Destroy(ad2);
897 return NULL;
898 }
899
900
901 /* Function: cm_alidisplay_Sizeof()
902 * Synopsis: Returns the total size of a CM_ALIDISPLAY, in bytes.
903 *
904 * Purpose: Return the total size of <CM_ALIDISPLAY> <ad>, in bytes.
905 *
906 * Note that <ad->memsize = cm_alidisplay_Sizeof(ad) - sizeof(CM_ALIDISPLAY)>,
907 * for a serialized object, because <ad->memsize> only refers to the sum
908 * of the variable-length allocated fields.
909 *
910 * Args: ad - CM_ALIDISPLAY to get the size of
911 *
912 * Returns: size of <ad> in bytes
913 */
914 size_t
915 cm_alidisplay_Sizeof(const CM_ALIDISPLAY *ad)
916 {
917 size_t n = sizeof(CM_ALIDISPLAY);
918
919 if (ad->rfline) n += ad->N+1; /* +1 for \0 */
920 n += 4 * (ad->N+1); /* csline, model, mline, aseq */
921 if (ad->ppline) n += ad->N+1;
922 if (ad->ncline) n += ad->N+1;
923 n += ad->N_el+1; /* aseq_el */
924 n += ad->N_el+1; /* rfline_el */
925 if (ad->ppline_el) n += ad->N_el+1;
926 n += 1 + strlen(ad->cmname);
927 n += 1 + strlen(ad->cmacc); /* optional acc, desc fields: when not present, just "" ("\0") */
928 n += 1 + strlen(ad->cmdesc);
929 n += 1 + strlen(ad->sqname);
930 n += 1 + strlen(ad->sqacc);
931 n += 1 + strlen(ad->sqdesc);
932
933 return n;
934 }
935
936 /* Function: cm_alidisplay_Destroy()
937 * Synopsis: Frees a <CM_ALIDISPLAY>
938 */
939 void
940 cm_alidisplay_Destroy(CM_ALIDISPLAY *ad)
941 {
942 if (ad == NULL) return;
943 if (ad->mem)
944 { /* serialized form */
945 free(ad->mem);
946 }
947 else
948 { /* deserialized form */
949 if (ad->rfline) free(ad->rfline);
950 if (ad->ncline) free(ad->ncline);
951 if (ad->csline) free(ad->csline);
952 if (ad->model) free(ad->model);
953 if (ad->mline) free(ad->mline);
954 if (ad->aseq) free(ad->aseq);
955 if (ad->ppline) free(ad->ppline);
956 if (ad->aseq_el) free(ad->aseq_el);
957 if (ad->rfline_el) free(ad->rfline_el);
958 if (ad->ppline_el) free(ad->ppline_el);
959 if (ad->cmname) free(ad->cmname);
960 if (ad->cmacc) free(ad->cmacc);
961 if (ad->cmdesc) free(ad->cmdesc);
962 if (ad->sqname) free(ad->sqname);
963 if (ad->sqacc) free(ad->sqacc);
964 if (ad->sqdesc) free(ad->sqdesc);
965 }
966 free(ad);
967 }
968
969 /* Function: post_code_to_avg_pp()
970 * Date: EPN, Tue May 22 20:43:46 2012
971 *
972 * Purpose: Return the average posterior probability
973 * for the given posterior single character
974 * code <postcode>.
975 *
976 *
977 */
978 float
979 post_code_to_avg_pp(char postcode)
980 {
981 switch (postcode) {
982 case '*': return 0.975; break;
983 case '9': return 0.9; break;
984 case '8': return 0.8; break;
985 case '7': return 0.7; break;
986 case '6': return 0.6; break;
987 case '5': return 0.5; break;
988 case '4': return 0.4; break;
989 case '3': return 0.3; break;
990 case '2': return 0.2; break;
991 case '1': return 0.1; break;
992 case '0': return 0.025; break;
993 default: return 0.0;
994 }
995 return 0.; /* NOT REACHED */
996 }
997
998 /* Function: bp_is_canonical
999 * Date: EPN, Wed Oct 14 06:17:27 2009
1000 *
1001 * Purpose: Determine if two residues form a canonical base pair or not.
1002 * Works for RNA or DNA (because for some reason cmsearch allows
1003 * the user to format output as DNA (with --dna)).
1004 *
1005 * Returns: TRUE if:
1006 * lseq rseq
1007 * ---- ----
1008 * A U
1009 * U A
1010 * C G
1011 * G C
1012 * G U
1013 * U G
1014 * A T
1015 * T A
1016 * G T
1017 * T G
1018 * Else, return FALSE.
1019 */
1020 int
1021 bp_is_canonical(char lseq, char rseq)
1022 {
1023 switch (toupper(lseq)) {
1024 case 'A':
1025 switch (toupper(rseq)) {
1026 case 'U': return TRUE; break;
1027 case 'T': return TRUE; break;
1028 default: break;
1029 }
1030 break;
1031 case 'C':
1032 switch (toupper(rseq)) {
1033 case 'G': return TRUE; break;
1034 default: break;
1035 }
1036 break;
1037 case 'G':
1038 switch (toupper(rseq)) {
1039 case 'C': return TRUE; break;
1040 case 'U': return TRUE; break;
1041 case 'T': return TRUE; break;
1042 default: break;
1043 }
1044 break;
1045 case 'U':
1046 switch (toupper(rseq)) {
1047 case 'A': return TRUE; break;
1048 case 'G': return TRUE; break;
1049 default: break;
1050 }
1051 break;
1052 case 'T':
1053 switch (toupper(rseq)) {
1054 case 'A': return TRUE; break;
1055 case 'G': return TRUE; break;
1056 default: break;
1057 }
1058 break;
1059 default: break;
1060 }
1061
1062 return FALSE;
1063 }
1064
1065 /*---------------- end, alidisplay object -----------------------*/
1066
1067
1068
1069 /*****************************************************************
1070 * 2. The CM_ALIDISPLAY API
1071 *****************************************************************/
1072
1073 static int
1074 integer_textwidth(long n)
1075 {
1076 int w = (n < 0)? 1 : 0;
1077 while (n != 0) { n /= 10; w++; }
1078 return w;
1079 }
1080
1081 /* Function: cm_alidisplay_EncodePostProb()
1082 * Synopsis: Convert a posterior probability to a char code.
1083 *
1084 * Purpose: Convert the posterior probability <p> to
1085 * a character code suitable for Stockholm format
1086 * <#=GC PP_cons> and <#=GR seqname PP> annotation
1087 * lines. HMMER uses the same codes in alignment
1088 * output.
1089 *
1090 * Characters <0-9*> are used; $0.0 \leq p < 0.05$
1091 * is coded as 0, $0.05 \leq p < 0.15$ is coded as
1092 * 1, ... and so on ..., $0.85 \leq p < 0.95$ is
1093 * coded as 9, and $0.95 \leq p \leq 1.0$ is coded
1094 * as '*'.
1095 *
1096 * Returns: the encoded character.
1097 */
1098 char
1099 cm_alidisplay_EncodePostProb(float p)
1100 {
1101 return (p + 0.05 >= 1.0) ? '*' : (char) ((p + 0.05) * 10.0) + '0';
1102 }
1103
1104
1105 /* Function: cm_alidisplay_DecodePostProb()
1106 * Synopsis: Convert a char code post prob to an approx float.
1107 *
1108 * Purpose: Convert posterior probability code <pc>, which
1109 * is [0-9*], to an approximate floating point probability.
1110 *
1111 * The result is crude, because <pc> has already discretized
1112 * with loss of precision. We require that
1113 * <cm_alidisplay_EncodePostProb(cm_alidisplay_DecodePostProb(pc)) == pc>,
1114 * and that <pc=='0'> decodes to a nonzero probability just to
1115 * avoid any possible absorbing-zero artifacts.
1116 *
1117 * Returns: the decoded real-valued approximate probability.
1118 */
1119 float
1120 cm_alidisplay_DecodePostProb(char pc)
1121 {
1122 if (pc == '0') return 0.01;
1123 else if (pc == '*') return 1.0;
1124 else if (pc == '.') return 0.0;
1125 else return ((float) (pc - '0') / 10.);
1126 }
1127
1128 /* Function: cm_alidisplay_Print()
1129 * Synopsis: Human readable output of <CM_ALIDISPLAY>
1130 *
1131 * Purpose: Prints alignment <ad> to stream <fp>.
1132 *
1133 * Put at least <min_aliwidth> alignment characters per
1134 * line; try to make lines no longer than <linewidth>
1135 * characters, including name, coords, and spacing. The
1136 * width of lines may exceed <linewidth>, if that's what it
1137 * takes to put a name, coords, and <min_aliwidth>
1138 * characters of alignment on a line.
1139 *
1140 * As a special case, if <linewidth> is negative or 0, then
1141 * alignments are formatted in a single block of unlimited
1142 * line length.
1143 *
1144 * Returns: eslOK on success
1145 * eslEINVAL if ad->aseq or ad->model are invalid,
1146 * specifically if local end formatting is invalid.
1147 * All local ends should begin with '*[' and end with
1148 * ']*' with the intervening characters being 0 or
1149 * more whitespace characters followed by an integer.
1150 */
1151 int
1152 cm_alidisplay_Print(FILE *fp, CM_ALIDISPLAY *ad, int min_aliwidth, int linewidth, int show_accessions)
1153 {
1154 char *buf = NULL;
1155 char *show_cmname = NULL;
1156 char *show_seqname = NULL;
1157 int namewidth, coordwidth, aliwidth, cur_aliwidth;
1158 int pos;
1159 int status;
1160 int ni, nk;
1161 int z, zp;
1162 long i1,i2;
1163 int k1,k2;
1164 int ni_toadd, nk_toadd;
1165 int trunc_at_start; /* special case, ali begins with a truncated begin */
1166
1167 /* implement the --acc option for preferring accessions over names in output */
1168 show_cmname = (show_accessions && ad->cmacc[0] != '\0') ? ad->cmacc : ad->cmname;
1169 show_seqname = (show_accessions && ad->sqacc[0] != '\0') ? ad->sqacc : ad->sqname;
1170
1171 /* dynamically size the output lines */
1172 namewidth = ESL_MAX(strlen(show_cmname), strlen(show_seqname));
1173 coordwidth = ESL_MAX(ESL_MAX(integer_textwidth(ad->cfrom_span),
1174 integer_textwidth(ad->cto_span)),
1175 ESL_MAX(integer_textwidth(ad->sqfrom),
1176 integer_textwidth(ad->sqto)));
1177 aliwidth = (linewidth > 0) ? linewidth - namewidth - 2*coordwidth - 5 : ad->N;
1178 if (aliwidth < ad->N && aliwidth < min_aliwidth) aliwidth = min_aliwidth; /* at least, regardless of some silly linewidth setting */
1179 ESL_ALLOC(buf, sizeof(char) * (aliwidth+1));
1180 buf[aliwidth] = '\0';
1181
1182 /* Break the alignment into multiple blocks of width aliwidth for printing */
1183 i1 = ad->sqfrom;
1184 k1 = ad->cfrom_emit;
1185 cur_aliwidth = aliwidth;
1186
1187 for (pos = 0; pos < ad->N; pos += cur_aliwidth)
1188 {
1189 if (pos > 0) fprintf(fp, "\n"); /* blank line betweeen blocks */
1190
1191 ni = nk = 0;
1192 cur_aliwidth = aliwidth; /* this will change if a aliwidth-wide block will end in the middle of a local end display */
1193
1194 for (z = pos; z < pos + cur_aliwidth && z < ad->N; z++) {
1195 if ((ad->aseq[z] == '*' && ad->model[z] == '*') ||
1196 (ad->aseq[z] == '<' && ad->model[z] == '<')) {
1197 /* we're at the beginning of a local end or truncated begin display, process it:
1198 * Examples:
1199 * "*[ 7]*" (local begin)
1200 * "<[ 7]*" (trunc begin, at aln start) (processed differently, initial k1 will be '1', not ad->cfrom_emit)
1201 * "*[ 7]>" (trunc begin, at aln end) (we process this just like a local begin here)
1202 */
1203 trunc_at_start = (ad->aseq[z] == '<' && ad->model[z] == '<') ? TRUE : FALSE;
1204 nk_toadd = ni_toadd = 0;
1205 if(ad->aseq[z+1] != '[' || ad->model[z+1] != '[') { status = eslEINVAL; goto ERROR; }
1206
1207 zp = z+2;
1208 while(ad->model[zp] == ' ') { zp++; } /* chew up any whitespace */
1209 while(ad->model[zp] != ']') { nk_toadd *= 10; nk_toadd += ad->model[zp] - '0'; zp++; } /* determine size of local end in model */
1210
1211 zp = z+2;
1212 while(ad->aseq[zp] == ' ') { zp++; } /* chew up any whitespace */
1213 while(ad->aseq[zp] != ']') { ni_toadd *= 10; ni_toadd += ad->aseq[zp] - '0'; zp++; } /* determine size of local end in aseq */
1214
1215 if((zp+1) >= (pos + aliwidth)) { /* the local end display will not fit completely on this block, save it for next block */
1216 cur_aliwidth = z - pos;
1217 }
1218 else {
1219 nk += nk_toadd;
1220 ni += ni_toadd;
1221 if(trunc_at_start) k1 -= nk_toadd;
1222 z = zp+1; /* position z at end of local end display (one char past the ']', on the '*'), the 'z++' at end of for loop will increase it 1 more */
1223 /* printf("z: %4d nk_toadd: %d nk: %4d\n", z, nk_toadd, nk); */
1224 }
1225 }
1226 else { /* normal case, we're not at the beginning of a local end */
1227 if (ad->model[z] != '.') nk++; /* k advances except on insert states */
1228 if (ad->aseq[z] != '-') ni++; /* i advances except on delete states */
1229 }
1230 }
1231
1232 if(aliwidth != cur_aliwidth) buf[cur_aliwidth] = '\0';
1233
1234 k2 = k1+nk-1;
1235 if (ad->sqfrom < ad->sqto) { i2 = i1+ni-1; }
1236 else { i2 = i1-ni+1; }
1237
1238 if (ad->ncline != NULL) { strncpy(buf, ad->ncline+pos, cur_aliwidth); fprintf(fp, " %*s %s %*sNC\n", namewidth+coordwidth+1, "", buf, aliwidth-cur_aliwidth, ""); }
1239 strncpy(buf, ad->csline+pos, cur_aliwidth); fprintf(fp, " %*s %s %*sCS\n", namewidth+coordwidth+1, "", buf, aliwidth-cur_aliwidth, "");
1240 strncpy(buf, ad->model+pos, cur_aliwidth); fprintf(fp, " %*s %*d %s %*s%-*d\n", namewidth, show_cmname, coordwidth, k1, buf, aliwidth-cur_aliwidth, "", coordwidth, k2);
1241 strncpy(buf, ad->mline+pos, cur_aliwidth); fprintf(fp, " %*s %s\n", namewidth+coordwidth+1, " ", buf);
1242 if (ni > 0) { strncpy(buf, ad->aseq+pos, cur_aliwidth); fprintf(fp, " %*s %*ld %s %*s%-*ld\n", namewidth, show_seqname, coordwidth, i1, buf, aliwidth-cur_aliwidth, "", coordwidth, i2); }
1243 else { strncpy(buf, ad->aseq+pos, cur_aliwidth); fprintf(fp, " %*s %*s %s %*s%*s\n", namewidth, show_seqname, coordwidth, "-", buf, aliwidth-cur_aliwidth, "", coordwidth, "-"); }
1244 if (ad->ppline != NULL) { strncpy(buf, ad->ppline+pos, cur_aliwidth); fprintf(fp, " %*s %s %*sPP\n", namewidth+coordwidth+1, "", buf, aliwidth-cur_aliwidth, ""); }
1245 if (ad->rfline != NULL) { strncpy(buf, ad->rfline+pos, cur_aliwidth); fprintf(fp, " %*s %s %*sRF\n", namewidth+coordwidth+1, "", buf, aliwidth-cur_aliwidth, ""); }
1246
1247 k1 += nk;
1248 if (ad->sqfrom < ad->sqto) { i1 += ni; }
1249 else { i1 -= ni; } /* revcomp hit for DNA */
1250 }
1251 fflush(fp);
1252 free(buf);
1253 return eslOK;
1254
1255 ERROR:
1256 if (buf != NULL) free(buf);
1257 return status;
1258 }
1259
1260 /* Functions: cm_alidisplay_Is5PTrunc()
1261 * cm_alidisplay_Is3PTrunc()
1262 * cm_alidisplay_Is5PAnd3PTrunc()
1263 * cm_alidisplay_Is5PTruncOnly()
1264 * cm_alidisplay_Is3PTruncOnly()
1265 *
1266 * Synopsis: Return TRUE if an alignment is truncated in a specific way.
1267 * These are convenience functions that use a simple tests
1268 * of equality between ad->cfrom_span and ad->cfrom_emit and
1269 * between ad->cto_span and ad->cto_emit. Those four values
1270 * were calculated in ParsetreeToCMBounds() (which is called
1271 * by cm_alidisplay_Create()) based on the parsetree of the
1272 * hit, the pipeline pass the hit was found in and whether
1273 * the parsetree contained the first and/or final residue of
1274 * the source sequence of the hit. See ParsetreeToCMBounds()
1275 * for details.
1276 *
1277 * Returns: TRUE or FALSE;
1278 */
1279 int
1280 cm_alidisplay_Is5PTrunc(const CM_ALIDISPLAY *ad)
1281 {
1282 return (ad->cfrom_emit != ad->cfrom_span) ? TRUE : FALSE;
1283 }
1284
1285 int
1286 cm_alidisplay_Is3PTrunc(const CM_ALIDISPLAY *ad)
1287 {
1288 return (ad->cto_emit != ad->cto_span) ? TRUE : FALSE;
1289 }
1290
1291 int
1292 cm_alidisplay_Is5PAnd3PTrunc(const CM_ALIDISPLAY *ad)
1293 {
1294 return (ad->cfrom_emit != ad->cfrom_span && ad->cto_emit != ad->cto_span) ? TRUE : FALSE;
1295 }
1296
1297 int
1298 cm_alidisplay_Is5PTruncOnly(const CM_ALIDISPLAY *ad)
1299 {
1300 return (ad->cfrom_emit != ad->cfrom_span && ad->cto_emit == ad->cto_span) ? TRUE : FALSE;
1301 }
1302
1303 int
1304 cm_alidisplay_Is3PTruncOnly(const CM_ALIDISPLAY *ad)
1305 {
1306 return (ad->cfrom_emit == ad->cfrom_span && ad->cto_emit != ad->cto_span) ? TRUE : FALSE;
1307 }
1308
1309 /* Function: cm_alidisplay_TruncString()
1310 * Synopsis: Determine if an alignment is truncated 5', 3' or both
1311 * and return a string summarizing the truncation: "5'&3'",
1312 * "5'", "3'", or "no". As a special case, if hit was
1313 * found using a HMM only pipeline pass, we return "-".
1314 *
1315 * Returns: informative string
1316 */
1317 char *
1318 cm_alidisplay_TruncString(const CM_ALIDISPLAY *ad)
1319 {
1320 if (ad->hmmonly) return "-";
1321 else if(cm_alidisplay_Is5PAnd3PTrunc(ad)) return "5'&3'";
1322 else if(cm_alidisplay_Is5PTruncOnly(ad)) return "5'";
1323 else if(cm_alidisplay_Is3PTruncOnly(ad)) return "3'";
1324 else return "no";
1325 }
1326
1327 /* Function: cm_alidisplay_Backconvert()
1328 * Synopsis: Convert an alidisplay to a parsetree and subsequence.
1329 *
1330 * Purpose: Convert alignment display object <ad> to a faux subsequence
1331 * and faux subsequence parsetree, returning them in <ret_sq> and
1332 * <ret_tr>.
1333 *
1334 * The subsequence <*ret_sq> is digital; ascii residues in
1335 * <ad> are digitized using digital alphabet <abc>.
1336 *
1337 * The subsequence and trace are suitable for passing as
1338 * array elements to <p7_tracealign_Seqs>. This is the
1339 * main purpose of backconversion. Results of a profile
1340 * search are stored in a hit list as a processed
1341 * <P7_ALIDISPLAY>, not as a <P7_TRACE> and <ESL_SQ>, to
1342 * reduce space and to reduce communication overhead in
1343 * parallelized search implementations. After reduction
1344 * to a final hit list, a master may want to construct a
1345 * multiple alignment of all the significant hits.
1346 *
1347 * Returns: <eslOK> on success.
1348 *
1349 * Throws: <eslEMEM> on allocation failures. <eslECORRUPT> on unexpected internal
1350 * data corruption. On any exception, <*ret_sq>, <*ret_tr> and
1351 * <*ret_pp> are <NULL>.
1352 *
1353 * Xref: SRE:J4/29.
1354 */
1355 int
1356 cm_alidisplay_Backconvert(CM_t *cm, const CM_ALIDISPLAY *ad, char *errbuf, ESL_SQ **ret_sq, Parsetree_t **ret_tr, char **ret_pp)
1357 {
1358 int status;
1359 ESL_SQ *sq = NULL; /* RETURN: faux subsequence */
1360 Parsetree_t *tr = NULL; /* RETURN: faux parsetree */
1361 char *pp = NULL; /* RETURN: post prob annotation */
1362 Parsetree_t *mtr = NULL; /* guide tree for the CM, unfortunately we need to create this */
1363 ESL_MSA *msa = NULL; /* we'll build an MSA from the single seq ad pertains to */
1364 char *aseq = NULL; /* an aligned string, a text sequence */
1365 int apos, upos; /* counter over aligned, unaligned positions */
1366 int *a2u_map = NULL; /* map of aligned to unaligned positions for updating tr->emitl, tr->emitr */
1367 int *used_el = NULL; /* [1..msa->alen] used_el[apos] = TRUE if apos is modeled by EL state, else FALSE */
1368 int x, ulen;
1369
1370 if(cm->cmcons == NULL) ESL_FAIL(eslEINVAL, errbuf, "cm_alidisplay_Backconvert(): cm->cmcons is NULL");
1371
1372 msa = esl_msa_Create(1, ad->N_el);
1373 memcpy(msa->aseq[0], ad->aseq_el, ad->N_el);
1374 if((status = esl_strdup(ad->rfline_el, msa->alen, &(msa->rf))) != eslOK) ESL_XFAIL(status, errbuf, "cm_alidisplay_BackConvert() out of memory");
1375 /*if((status = esl_strdup(ad->aseq_el, msa->alen, &(msa->aseq[0]))) != eslOK) ESL_XFAIL(status, errbuf, "cm_alidisplay_Backconvert() out of memory");*/
1376 if(ad->ppline_el) {
1377 ESL_ALLOC(msa->pp, sizeof(char *) * 1);
1378 if((status = esl_strdup(ad->ppline_el, msa->alen, &(msa->pp[0]))) != eslOK) ESL_XFAIL(status, errbuf, "cm_alidisplay_Backconvert() out of memory");
1379 }
1380 ESL_ALLOC(msa->ss_cons, sizeof(char) * (msa->alen+1));
1381
1382 /*cm_alidisplay_Dump(stdout, ad);*/
1383
1384 upos = 0;
1385 for(apos = 0; apos < msa->alen; apos++) {
1386 msa->ss_cons[apos] = (isupper(msa->aseq[0][apos]) || msa->aseq[0][apos] == '-') ? cm->cmcons->cstr[upos++] : '.';
1387 }
1388 msa->ss_cons[msa->alen] = '\0';
1389 if(upos != cm->clen) ESL_XFAIL(eslERANGE, errbuf, "cm_alidisplay_Backconvert() failed to create temporary msa");
1390
1391 esl_msa_FormatSeqName(msa, 0, "%s/%ld-%ld", ad->sqname, ad->sqfrom, ad->sqto);
1392 /*esl_msa_SetSeqName(msa, 0, ad->sqname, -1);*/
1393 if(ad->sqacc) esl_msa_SetSeqAccession (msa, 0, ad->sqacc, -1);
1394 if(ad->sqdesc) esl_msa_SetSeqDescription(msa, 0, ad->sqdesc, -1);
1395
1396 if((status = esl_msa_Digitize(cm->abc, msa, errbuf)) != eslOK) goto ERROR;
1397
1398 /* get a guidetree for the CM */
1399 if((status = cm_Guidetree(cm, errbuf, msa, &mtr)) != eslOK) goto ERROR;
1400
1401 ESL_ALLOC(used_el, (msa->alen+1) * sizeof(int));
1402 /* change any EL emissions in aseq to '~' so Transmogrify deals with them appropriately */
1403 used_el[0] = FALSE;
1404 for(apos = 0; apos < msa->alen; apos++) {
1405 used_el[apos+1] = (msa->rf[apos] == '~') ? TRUE : FALSE;
1406 }
1407
1408 /* determine if we're truncated and if so add missing (~) chars to ax */
1409 if(cm_alidisplay_Is5PTrunc(ad)) {
1410 for (apos = 1; apos <= msa->alen; apos++) {
1411 if (esl_abc_XIsResidue(cm->abc, msa->ax[0][apos])) break;
1412 msa->ax[0][apos] = esl_abc_XGetMissing(cm->abc);
1413 }
1414 }
1415 if(cm_alidisplay_Is3PTrunc(ad)) {
1416 for (apos = msa->alen; apos >= 1; apos--) {
1417 if (esl_abc_XIsResidue(cm->abc, msa->ax[0][apos])) break;
1418 msa->ax[0][apos] = esl_abc_XGetMissing(cm->abc);
1419 }
1420 }
1421
1422 if((status = Transmogrify(cm, errbuf, mtr, msa->ax[0], used_el, msa->alen, &tr)) != eslOK) goto ERROR;
1423 /* tr is in alignment coords, convert it to unaligned coords.
1424 * First we construct a map of aligned to unaligned coords, then
1425 * we use it to convert.
1426 */
1427 ESL_ALLOC(a2u_map, sizeof(int) * (msa->alen+1));
1428 a2u_map[0] = -1; /* invalid */
1429 upos = 1;
1430 for(apos = 1; apos <= msa->alen; apos++) {
1431 a2u_map[apos] = ((esl_abc_XIsGap(msa->abc, msa->ax[0][apos])) ||
1432 (esl_abc_XIsMissing(msa->abc, msa->ax[0][apos])))
1433 ? -1 : upos++;
1434 }
1435 ulen = upos;
1436 for(x = 0; x < tr->n; x++) {
1437 if(tr->emitl[x] != -1) tr->emitl[x] = a2u_map[tr->emitl[x]];
1438 if(tr->emitr[x] != -1) tr->emitr[x] = a2u_map[tr->emitr[x]];
1439 }
1440 if((status = esl_sq_FetchFromMSA(msa, 0, &sq)) != eslOK) ESL_XFAIL(status, errbuf, "cm_alidisplay_Backconvert() unable to fetch seq from msa");
1441 if(msa->pp) {
1442 ESL_ALLOC(pp, sizeof(char) * (ulen+1));
1443 upos = 0;
1444 for(apos = 0; apos < msa->alen; apos++) {
1445 if(a2u_map[apos+1] != -1) pp[upos++] = msa->pp[0][apos];
1446 }
1447 if(upos+1 != ulen) ESL_XFAIL(eslERANGE, errbuf, "cm_alidisplay_Backconvert() failed to create temporary msa");
1448 }
1449
1450 esl_msa_Destroy(msa);
1451 free(used_el);
1452 free(a2u_map);
1453 FreeParsetree(mtr);
1454 free(aseq);
1455
1456 *ret_sq = sq;
1457 *ret_tr = tr;
1458 *ret_pp = pp;
1459 return eslOK;
1460
1461 ERROR:
1462 if (msa != NULL) esl_msa_Destroy(msa);
1463 if (mtr != NULL) FreeParsetree(mtr);
1464 if (aseq != NULL) free(aseq);
1465 if (a2u_map != NULL) free(a2u_map);
1466 if (sq != NULL) esl_sq_Destroy(sq);
1467 if (tr != NULL) FreeParsetree(tr);
1468 if (pp != NULL) free(pp);
1469 *ret_sq = NULL;
1470 *ret_tr = NULL;
1471 return status;
1472 }
1473
1474 /*------------------- end, alidisplay API -----------------------*/
1475
1476
1477 /*****************************************************************
1478 * 3. Debugging/dev code
1479 *****************************************************************/
1480
1481 /* Function: cm_alidisplay_Dump()
1482 * Synopsis: Print contents of CM_ALIDISPLAY for inspection.
1483 *
1484 * Purpose: Print contents of the <CM_ALIDISPLAY> <ad> to
1485 * stream <fp> for inspection. Includes all elements
1486 * of the structure, whether the object is allocated
1487 * in serialized or deserialized form, and the total
1488 * size of the object in bytes.
1489 *
1490 * Returns: <eslOK>
1491 */
1492 int
1493 cm_alidisplay_Dump(FILE *fp, const CM_ALIDISPLAY *ad)
1494 {
1495 fprintf(fp, "CM_ALIDISPLAY dump\n");
1496 fprintf(fp, "------------------\n");
1497
1498 fprintf(fp, "rfline = %s\n", ad->rfline ? ad->rfline : "[none]");
1499 fprintf(fp, "ncline = %s\n", ad->ncline ? ad->ncline : "[none]");
1500 fprintf(fp, "csline = %s\n", ad->csline ? ad->csline : "[none]");
1501 fprintf(fp, "model = %s\n", ad->model);
1502 fprintf(fp, "mline = %s\n", ad->mline);
1503 fprintf(fp, "ppline = %s\n", ad->ppline ? ad->ppline : "[none]");
1504 fprintf(fp, "aseq = %s\n", ad->aseq);
1505 fprintf(fp, "N = %d\n", ad->N);
1506 fprintf(fp, "\n");
1507
1508 fprintf(fp, "aseq_el = %s\n", ad->aseq_el);
1509 fprintf(fp, "ppline_el = %s\n", ad->ppline_el ? ad->ppline_el : "[none]");
1510 fprintf(fp, "N_el = %d\n", ad->N_el);
1511 fprintf(fp, "\n");
1512
1513 fprintf(fp, "cmname = %s\n", ad->cmname);
1514 fprintf(fp, "cmacc = %s\n", ad->cmacc[0] == '\0' ? "[none]" : ad->cmacc);
1515 fprintf(fp, "cmdesc = %s\n", ad->cmdesc[0] == '\0' ? "[none]" : ad->cmdesc);
1516 fprintf(fp, "cfrom_span = %d\n", ad->cfrom_span);
1517 fprintf(fp, "cfrom_emit = %d\n", ad->cfrom_emit);
1518 fprintf(fp, "cto_emit = %d\n", ad->cto_emit);
1519 fprintf(fp, "cto_span = %d\n", ad->cto_span);
1520 fprintf(fp, "clen = %d\n", ad->clen);
1521 fprintf(fp, "\n");
1522
1523 fprintf(fp, "sqname = %s\n", ad->sqname);
1524 fprintf(fp, "sqacc = %s\n", ad->sqacc[0] == '\0' ? "[none]" : ad->sqacc);
1525 fprintf(fp, "sqdesc = %s\n", ad->sqdesc[0] == '\0' ? "[none]" : ad->sqdesc);
1526 fprintf(fp, "sqfrom = %ld\n", ad->sqfrom);
1527 fprintf(fp, "sqto = %ld\n", ad->sqto);
1528 fprintf(fp, "\n");
1529
1530 fprintf(fp, "sc = %.2f\n",ad->sc);
1531 fprintf(fp, "avgpp = %.2f\n",ad->avgpp);
1532 fprintf(fp, "gc = %.2f\n",ad->gc);
1533 fprintf(fp, "tau = %g\n", ad->tau);
1534 fprintf(fp, "mx Mb = %.6f\n",ad->matrix_Mb);
1535 fprintf(fp, "seconds = %.6f\n",ad->elapsed_secs);
1536 fprintf(fp, "hmmonly = %s\n", ad->hmmonly ? "TRUE" : "FALSE");
1537
1538 fprintf(fp, "\n");
1539
1540 fprintf(fp, "size = %d bytes\n", (int) cm_alidisplay_Sizeof(ad));
1541 fprintf(fp, "%s\n", ad->mem ? "serialized" : "not serialized");
1542 return eslOK;
1543 }
1544
1545 /* Function: cm_alidisplay_Compare()
1546 * Synopsis: Compare two <CM_ALIDISPLAY> objects for equality
1547 *
1548 * Purpose: Compare alignment displays <ad1> and <ad2> for
1549 * equality. Return <eslOK> if they have identical
1550 * contents; <eslFAIL> if not.
1551 *
1552 * Only contents matter, not serialization status;
1553 * a serialized and deserialized version of the same
1554 * alidisplay will compare identical.
1555 */
1556 int
1557 cm_alidisplay_Compare(const CM_ALIDISPLAY *ad1, const CM_ALIDISPLAY *ad2)
1558 {
1559 if (ad1->mem && ad2->mem) /* both objects serialized */
1560 {
1561 if (ad1->memsize != ad2->memsize) return eslFAIL;
1562 if (memcmp(ad1->mem, ad2->mem, ad1->memsize) != 0) return eslFAIL;
1563 }
1564
1565 if (esl_strcmp(ad1->rfline, ad2->rfline) != eslOK) return eslFAIL;
1566 if (esl_strcmp(ad1->csline, ad2->csline) != eslOK) return eslFAIL;
1567 if (esl_strcmp(ad1->model, ad2->model) != eslOK) return eslFAIL;
1568 if (esl_strcmp(ad1->mline, ad2->mline) != eslOK) return eslFAIL;
1569 if (esl_strcmp(ad1->aseq, ad2->aseq) != eslOK) return eslFAIL;
1570 if (esl_strcmp(ad1->ppline, ad2->ppline) != eslOK) return eslFAIL;
1571 if (ad1->N != ad2->N) return eslFAIL;
1572
1573 if (esl_strcmp(ad1->cmname, ad2->cmname) != eslOK) return eslFAIL;
1574 if (esl_strcmp(ad1->cmacc, ad2->cmacc) != eslOK) return eslFAIL;
1575 if (esl_strcmp(ad1->cmdesc, ad2->cmdesc) != eslOK) return eslFAIL;
1576 if (ad1->cfrom_emit != ad2->cfrom_emit) return eslFAIL;
1577 if (ad1->cto_emit != ad2->cto_emit) return eslFAIL;
1578 if (ad1->cfrom_span != ad2->cfrom_span) return eslFAIL;
1579 if (ad1->cto_span != ad2->cto_span) return eslFAIL;
1580
1581 if (esl_strcmp(ad1->sqname, ad2->sqname) != eslOK) return eslFAIL;
1582 if (esl_strcmp(ad1->sqacc, ad2->sqacc) != eslOK) return eslFAIL;
1583 if (esl_strcmp(ad1->sqdesc, ad2->sqdesc) != eslOK) return eslFAIL;
1584 if (ad1->sqfrom != ad2->sqfrom) return eslFAIL;
1585 if (ad1->sqto != ad2->sqto) return eslFAIL;
1586 if (ad1->clen != ad2->clen) return eslFAIL;
1587
1588 return eslOK;
1589 }
1590
1591 /*-------------- end, debugging/dev code ------------------------*/
1592
1593
1594