Debian Codegraph @

hmmer/src/p7_alidisplay.c

raw symbols SCIP
1 /* Formatting, transmitting, and printing single alignments to a
2 * profile.
3 *
4 * Contents:
5 * 1. The P7_ALIDISPLAY object.
6 * 2. The P7_ALIDISPLAY API.
7 * 3. Debugging/dev code.
8 * 4. Benchmark driver.
9 * 5. Unit tests.
10 * 6. Test driver.
11 * 7. Example.
12 */
13 #include <p7_config.h>
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <string.h>
20
21 #include "easel.h"
22 #include "hmmer.h"
23
24 // Define bit-vector constants used in the _Serialize() and _Deserialize routines
25 #define RFLINE_PRESENT (1 << 0)
26 #define MMLINE_PRESENT (1 << 1)
27 #define CSLINE_PRESENT (1 << 2)
28 #define PPLINE_PRESENT (1 << 3)
29 #define ASEQ_PRESENT (1 << 4)
30 #define NTSEQ_PRESENT (1 << 5)
31
32 /*****************************************************************
33 * 1. The P7_ALIDISPLAY object
34 *****************************************************************/
35
36 /* A P7_ALIDISPLAY structure originally had two possible states:
37 * "serialized", in which all of the variable-length fields of the structure
38 * were pointers into one block of allocated memory, which was pointed to by the
39 * structure's "mem" field, and
40 * "deserialized", in which each of the variable-length fields of the structure
41 * were separate regions of allocated memory. The intent of this was that
42 * "serialized" structures would be faster to create, as they only require one
43 * memory allocation, and easier to send over sockets, but that "deserialized"
44 * structures would be easier to modify and/or reuse. However, HMMER currently
45 * does not ever modify or reuse P7_ALIDISPLAY structures.
46 *
47 * P7_ALIDISPLAY structures are created in "serialized" mode. The only time they
48 * were ever converted into "deserialized" mode was in hmmpgmd2msa, and this was
49 * somewhat unnecessary.
50 *
51 * Now, we have added _Serialize() and _Deserialize() functions that have the more
52 * traditional behavior of converting P7_ALIDISPLAY structures to and from
53 * contiguous blocks of bytes for transmission over sockets. With this change,
54 * P7_ALIDISPLAY structures will always be in "serialized" mode, and will never be
55 * converted to their "deserialized" mode. (Yes, this terminology is confusing.)
56 *
57 * For H3, we will leave the existing code to handle the "serialized" and "deserialized"
58 * modes in place. HMMER's output code is being heavily revised for H4, and we will
59 * revisit how these modes are handled in H4 once that portion of the code stabilizes.
60 */
61
62 /* Function: p7_alidisplay_Create()
63 * Synopsis: Create an alignment display, from trace and oprofile.
64 *
65 * Purpose: Creates and returns an alignment display for domain number
66 * <which> in traceback <tr>, where the traceback
67 * corresponds to an alignment of optimized profile <om> to digital sequence
68 * <dsq>, and the unique name of that target
69 * sequence <dsq> is <sqname>. The <which> index starts at 0.
70 *
71 * It will be a little faster if the trace is indexed with
72 * <p7_trace_Index()> first. The number of domains is then
73 * in <tr->ndom>. If the caller wants to create alidisplays
74 * for all of these, it would loop <which> from
75 * <0..tr->ndom-1>.
76 *
77 * However, even without an index, the routine will work fine.
78 *
79 * Args: tr - traceback
80 * which - domain number, 0..tr->ndom-1
81 * om - optimized profile (query)
82 * sq - digital sequence (target)
83 * ntsq - text sequence (original nucleotide target in the case of translated search)
84 * ddef_app - optional posterior prob alignment line; only nhmmer sends a not-NULL value
85 *
86 * Returns: <eslOK> on success.
87 *
88 * Throws: <NULL> on allocation failure, or if something's internally corrupt
89 * in the data.
90 */
91 P7_ALIDISPLAY *
92 p7_alidisplay_Create(const P7_TRACE *tr, int which, const P7_OPROFILE *om, const ESL_SQ *sq, const ESL_SQ *ntsq)
93 {
94 P7_ALIDISPLAY *ad = NULL;
95 char *Alphabet = om->abc->sym;
96 int n, pos, z;
97 int z1,z2;
98 int k,x,i,s;
99 int hmm_namelen, hmm_acclen, hmm_desclen;
100 int sq_namelen, sq_acclen, sq_desclen;
101 int status;
102 ESL_SQ *ntorfseqtxt = NULL;
103
104 /* First figure out which piece of the trace (from first match to last match)
105 * we're going to represent, and how big it is.
106 */
107 if (tr->ndom > 0) { /* if we have an index, this is a little faster: */
108 for (z1 = tr->tfrom[which]; z1 < tr->N; z1++) if (tr->st[z1] == p7T_M) break; /* find next M state */
109 if (z1 == tr->N) return NULL; /* no M? corrupt trace */
110 for (z2 = tr->tto[which]; z2 >= 0 ; z2--) if (tr->st[z2] == p7T_M) break; /* find prev M state */
111 if (z2 == -1) return NULL; /* no M? corrupt trace */
112 } else { /* without an index, we can still do it fine: */
113 for (z1 = 0; which >= 0 && z1 < tr->N; z1++) if (tr->st[z1] == p7T_B) which--; /* find the right B state */
114 if (z1 == tr->N) return NULL; /* no such domain <which> */
115 for (; z1 < tr->N; z1++) if (tr->st[z1] == p7T_M) break; /* find next M state */
116 if (z1 == tr->N) return NULL; /* no M? corrupt trace */
117 for (z2 = z1; z2 < tr->N; z2++) if (tr->st[z2] == p7T_E) break; /* find the next E state */
118 for (; z2 >= 0; z2--) if (tr->st[z2] == p7T_M) break; /* find prev M state */
119 if (z2 == -1) return NULL; /* no M? corrupt trace */
120 }
121
122 /* Now we know that z1..z2 in the trace will be represented in the
123 * alidisplay; that's z2-z1+1 positions. We need a \0 trailer on all
124 * our display lines, so allocate z2-z1+2. We know each position is
125 * M, D, or I, so there's a 1:1 correspondence of trace positions
126 * with alignment display positions. We also know the display
127 * starts and ends with M states.
128 *
129 * So now let's allocate. The alidisplay is packed into a single
130 * memory space, so this appears to be intricate, but it's just
131 * bookkeeping.
132 */
133 n = (z2-z1+2) * 3; /* model, mline, aseq mandatory */
134
135 if (om->rf[0] != 0) n += z2-z1+2; /* optional reference line */
136 if (om->mm[0] != 0) n += z2-z1+2; /* optional reference line */
137 if (om->cs[0] != 0) n += z2-z1+2; /* optional structure line */
138 if (tr->pp != NULL) n += z2-z1+2; /* optional posterior prob line */
139 hmm_namelen = strlen(om->name); n += hmm_namelen + 1;
140 hmm_acclen = (om->acc != NULL ? strlen(om->acc) : 0); n += hmm_acclen + 1;
141 hmm_desclen = (om->desc != NULL ? strlen(om->desc) : 0); n += hmm_desclen + 1;
142
143 sq_namelen = strlen(sq->name); n += sq_namelen + 1;
144 sq_acclen = strlen(sq->acc); n += sq_acclen + 1; /* sq->acc is "\0" when unset */
145 sq_desclen = strlen(sq->desc); n += sq_desclen + 1; /* same for desc */
146
147 ESL_ALLOC(ad, sizeof(P7_ALIDISPLAY));
148 ad->mem = NULL;
149
150 pos = 0;
151 ad->memsize = sizeof(char) * n;
152 ESL_ALLOC(ad->mem, ad->memsize);
153 if (om->rf[0] != 0) { ad->rfline = ad->mem + pos; pos += z2-z1+2; } else { ad->rfline = NULL; }
154 //if (om->mm[0] != 0) { ad->mmline = ad->mem + pos; pos += z2-z1+2; } else { ad->mmline = NULL; }
155 ad->mmline = NULL;
156 if (om->cs[0] != 0) { ad->csline = ad->mem + pos; pos += z2-z1+2; } else { ad->csline = NULL; }
157 ad->model = ad->mem + pos; pos += z2-z1+2;
158 ad->mline = ad->mem + pos; pos += z2-z1+2;
159 ad->aseq = ad->mem + pos; pos += z2-z1+2;
160
161 if (tr->pp != NULL) { ad->ppline = ad->mem + pos; pos += z2-z1+2;} else { ad->ppline = NULL; }
162 ad->hmmname = ad->mem + pos; pos += hmm_namelen +1;
163 ad->hmmacc = ad->mem + pos; pos += hmm_acclen +1;
164 ad->hmmdesc = ad->mem + pos; pos += hmm_desclen +1;
165 ad->sqname = ad->mem + pos; pos += sq_namelen +1;
166 ad->sqacc = ad->mem + pos; pos += sq_acclen +1;
167 ad->sqdesc = ad->mem + pos; pos += sq_desclen +1;
168
169 strcpy(ad->hmmname, om->name);
170 if (om->acc != NULL) strcpy(ad->hmmacc, om->acc); else ad->hmmacc[0] = 0;
171 if (om->desc != NULL) strcpy(ad->hmmdesc, om->desc); else ad->hmmdesc[0] = 0;
172
173 strcpy(ad->sqname, sq->name);
174 strcpy(ad->sqacc, sq->acc);
175 strcpy(ad->sqdesc, sq->desc);
176
177 /* Determine hit coords */
178 ad->hmmfrom = tr->k[z1];
179 ad->hmmto = tr->k[z2];
180 ad->M = om->M;
181
182 ad->sqfrom = tr->i[z1];
183 ad->sqto = tr->i[z2];
184 ad->L = sq->n;
185
186 /* optional rf line */
187 if (ad->rfline != NULL) {
188 for (z = z1; z <= z2; z++) ad->rfline[z-z1] = ((tr->st[z] == p7T_I) ? '.' : om->rf[tr->k[z]]);
189 ad->rfline[z-z1] = '\0';
190 }
191
192 /* optional mm line */
193 if (ad->mmline != NULL) {
194 for (z = z1; z <= z2; z++) ad->mmline[z-z1] = ((tr->st[z] == p7T_I) ? '.' : om->mm[tr->k[z]]);
195 ad->mmline[z-z1] = '\0';
196 }
197
198 /* optional cs line */
199 if (ad->csline != NULL) {
200 for (z = z1; z <= z2; z++) ad->csline[z-z1] = ((tr->st[z] == p7T_I) ? '.' : om->cs[tr->k[z]]);
201 ad->csline[z-z1] = '\0';
202 }
203
204 /* optional pp line */
205 if (ad->ppline != NULL) {
206 for (z = z1; z <= z2; z++) ad->ppline[z-z1] = ( (tr->st[z] == p7T_D) ? '.' : p7_alidisplay_EncodePostProb(tr->pp[z]));
207 ad->ppline[z-z1] = '\0';
208 }
209
210
211 /* mandatory three alignment display lines: model, mline, aseq */
212 for (z = z1; z <= z2; z++)
213 {
214 k = tr->k[z];
215 i = tr->i[z];
216 x = sq->dsq[i];
217 s = tr->st[z];
218
219 switch (s) {
220 case p7T_M:
221 ad->model[z-z1] = om->consensus[k];
222 if (x == esl_abc_DigitizeSymbol(om->abc, om->consensus[k])) ad->mline[z-z1] = ad->model[z-z1];
223 else if (p7_oprofile_FGetEmission(om, k, x) > 1.0) ad->mline[z-z1] = '+'; /* >1 not >0; om has odds ratios, not scores */
224 else ad->mline[z-z1] = ' ';
225 ad->aseq [z-z1] = toupper(Alphabet[x]);
226 break;
227
228 case p7T_I:
229 ad->model [z-z1] = '.';
230 ad->mline [z-z1] = ' ';
231 ad->aseq [z-z1] = tolower(Alphabet[x]);
232 break;
233
234 case p7T_D:
235 ad->model [z-z1] = om->consensus[k];
236 ad->mline [z-z1] = ' ';
237 ad->aseq [z-z1] = '-';
238
239 break;
240
241 default: ESL_XEXCEPTION(eslEINVAL, "invalid state in trace: not M,D,I");
242 }
243 }
244 ad->model [z2-z1+1] = '\0';
245 ad->mline [z2-z1+1] = '\0';
246 ad->aseq [z2-z1+1] = '\0';
247 ad->N = z2-z1+1;
248 ad->ntseq = NULL; // Mark this NULL so it doesn't cause problems with serialization.
249 // nhmmer can reset it if it wants later
250 esl_sq_Destroy(ntorfseqtxt);
251
252
253 return ad;
254
255 ERROR:
256 p7_alidisplay_Destroy(ad);
257 return NULL;
258 }
259
260 /* Function: p7_alidisplay_Create_empty()
261 * Synopsis: Creates an empty P7_ALIDISPLAY object
262 *
263 * Purpose: Creates an empty P7_ALIDISPLAY object, one that does not contain an alignment to
264 * display but has been initialized to reasonable values (NULL for all strings, 0 for the amount
265 * of memory allocated in its buffer, etc.). This is mainly intended to be used to create a structure
266 * that a serialized P7_ALIDISPLAY object can be deserialized into.
267 *
268 * Returns: Pointer to the new <P7_ALIDISPLAY>
269 *
270 * Throws: Returns NULL if unable to allocate memory
271 */
272 extern P7_ALIDISPLAY *p7_alidisplay_Create_empty()
273 {
274 P7_ALIDISPLAY *new_obj;
275 int status; // standard ESL_ALLOC error condition
276
277 ESL_ALLOC(new_obj, sizeof(P7_ALIDISPLAY));
278
279 // Init fields to sensible "empty" values
280 new_obj->rfline = NULL;
281 new_obj->mmline = NULL;
282 new_obj->csline = NULL;
283 new_obj->model = NULL;
284 new_obj->mline = NULL;
285 new_obj->aseq = NULL;
286 new_obj->ntseq = NULL;
287 new_obj->ppline = NULL;
288 new_obj->N = 0;
289
290 new_obj->hmmname = NULL;
291 new_obj->hmmacc = NULL;
292 new_obj->hmmdesc = NULL;
293 new_obj->hmmfrom = 0;
294 new_obj->hmmto = 0;
295 new_obj->M = 0;
296
297 new_obj->sqname = NULL;
298 new_obj->sqacc = NULL;
299 new_obj->sqdesc = NULL;
300 new_obj->sqfrom = 0;
301 new_obj->sqto = 0;
302 new_obj->L = 0;
303
304 new_obj->memsize = 0;
305 new_obj->mem = NULL;
306
307 return new_obj;
308
309 ERROR: // only get here if the ESL_ALLOC fails
310 return NULL;
311 }
312 /* Function: p7_alidisplay_Clone()
313 * Synopsis: Make a duplicate of an ALIDISPLAY.
314 *
315 * Purpose: Create a duplicate of alignment display <ad>.
316 * Return a pointer to the duplicate. Caller
317 * is responsible for freeing the new object.
318 *
319 * Returns: pointer to new <P7_ALIDISPLAY>
320 *
321 * Throws: <NULL> on allocation failure.
322 */
323 P7_ALIDISPLAY *
324 p7_alidisplay_Clone(const P7_ALIDISPLAY *ad)
325 {
326 P7_ALIDISPLAY *ad2 = NULL;
327 int status;
328
329 ESL_ALLOC(ad2, sizeof(P7_ALIDISPLAY));
330 ad2->rfline = ad2->mmline = ad2->csline = ad2->model = ad2->mline = ad2->aseq = ad2->ntseq = ad2->ppline = NULL;
331 ad2->hmmname = ad2->hmmacc = ad2->hmmdesc = NULL;
332 ad2->sqname = ad2->sqacc = ad2->sqdesc = NULL;
333 ad2->mem = NULL;
334 ad2->memsize = 0;
335
336 if (ad->memsize) /* serialized */
337 {
338 ESL_ALLOC(ad2->mem, sizeof(char) * ad->memsize);
339 ad2->memsize = ad->memsize;
340 memcpy(ad2->mem, ad->mem, ad->memsize);
341
342 ad2->rfline = (ad->rfline ? ad2->mem + (ad->rfline - ad->mem) : NULL );
343 ad2->mmline = (ad->mmline ? ad2->mem + (ad->mmline - ad->mem) : NULL );
344 ad2->csline = (ad->csline ? ad2->mem + (ad->csline - ad->mem) : NULL );
345 ad2->model = ad2->mem + (ad->model - ad->mem);
346 ad2->mline = ad2->mem + (ad->mline - ad->mem);
347 ad2->aseq = ad2->mem + (ad->aseq - ad->mem);
348 ad2->ntseq = (ad->ntseq ? ad2->mem + (ad->ntseq - ad->mem) : NULL );
349 ad2->ppline = (ad->ppline ? ad2->mem + (ad->ppline - ad->mem) : NULL );
350 ad2->N = ad->N;
351
352 ad2->hmmname = ad2->mem + (ad->hmmname - ad->mem);
353 ad2->hmmacc = ad2->mem + (ad->hmmacc - ad->mem);
354 ad2->hmmdesc = ad2->mem + (ad->hmmdesc - ad->mem);
355 ad2->hmmfrom = ad->hmmfrom;
356 ad2->hmmto = ad->hmmto;
357 ad2->M = ad->M;
358
359 ad2->sqname = ad2->mem + (ad->sqname - ad->mem);
360 ad2->sqacc = ad2->mem + (ad->sqacc - ad->mem);
361 ad2->sqdesc = ad2->mem + (ad->sqdesc - ad->mem);
362 ad2->sqfrom = ad->sqfrom;
363 ad2->sqto = ad->sqto;
364 ad2->L = ad->L;
365 }
366 else /* deserialized */
367 {
368 if ( esl_strdup(ad->rfline, -1, &(ad2->rfline)) != eslOK) goto ERROR;
369 if ( esl_strdup(ad->mmline, -1, &(ad2->mmline)) != eslOK) goto ERROR;
370 if ( esl_strdup(ad->csline, -1, &(ad2->csline)) != eslOK) goto ERROR;
371 if ( esl_strdup(ad->model, -1, &(ad2->model)) != eslOK) goto ERROR;
372 if ( esl_strdup(ad->mline, -1, &(ad2->mline)) != eslOK) goto ERROR;
373 if ( esl_strdup(ad->aseq, -1, &(ad2->aseq)) != eslOK) goto ERROR;
374 if ( esl_strdup(ad->ntseq, -1, &(ad2->ntseq)) != eslOK) goto ERROR;
375 if ( esl_strdup(ad->ppline, -1, &(ad2->ppline)) != eslOK) goto ERROR;
376 ad2->N = ad->N;
377
378 if ( esl_strdup(ad->hmmname, -1, &(ad2->hmmname)) != eslOK) goto ERROR;
379 if ( esl_strdup(ad->hmmacc, -1, &(ad2->hmmacc)) != eslOK) goto ERROR;
380 if ( esl_strdup(ad->hmmdesc, -1, &(ad2->hmmdesc)) != eslOK) goto ERROR;
381 ad2->hmmfrom = ad->hmmfrom;
382 ad2->hmmto = ad->hmmto;
383 ad2->M = ad->M;
384
385 if ( esl_strdup(ad->sqname, -1, &(ad2->sqname)) != eslOK) goto ERROR;
386 if ( esl_strdup(ad->sqacc, -1, &(ad2->sqacc)) != eslOK) goto ERROR;
387 if ( esl_strdup(ad->sqdesc, -1, &(ad2->sqdesc)) != eslOK) goto ERROR;
388 ad2->sqfrom = ad->sqfrom;
389 ad2->sqto = ad->sqto;
390 ad2->L = ad->L;
391 }
392
393 return ad2;
394
395 ERROR:
396 if (ad2) p7_alidisplay_Destroy(ad2);
397 return NULL;
398 }
399
400
401 /* Function: p7_alidisplay_Sizeof()
402 * Synopsis: Returns the total size of a P7_ALIDISPLAY, in bytes.
403 *
404 * Purpose: Return the total size of <P7_ALIDISPLAY> <ad>, in bytes.
405 *
406 * Note that <ad->memsize = p7_alidisplay_Sizeof(ad) - sizeof(P7_ALIDISPLAY)>,
407 * for a serialized object, because <ad->memsize> only refers to the sum
408 * of the variable-length allocated fields.
409 *
410 * Args: ad - P7_ALIDISPLAY to get the size of
411 *
412 * Returns: size of <ad> in bytes
413 */
414 size_t
415 p7_alidisplay_Sizeof(const P7_ALIDISPLAY *ad)
416 {
417 size_t n = sizeof(P7_ALIDISPLAY);
418
419 if (ad->rfline) n += ad->N+1; /* +1 for \0 */
420 if (ad->mmline) n += ad->N+1;
421 if (ad->csline) n += ad->N+1;
422 if (ad->ppline) n += ad->N+1;
423 n += 3 * (ad->N+1); /* model, mline, aseq */
424 if (ad->ntseq) n += (3 * ad->N) + 1; /* ntseq */
425 n += 1 + strlen(ad->hmmname);
426 n += 1 + strlen(ad->hmmacc); /* optional acc, desc fields: when not present, just "" ("\0") */
427 n += 1 + strlen(ad->hmmdesc);
428 n += 1 + strlen(ad->sqname);
429 n += 1 + strlen(ad->sqacc);
430 n += 1 + strlen(ad->sqdesc);
431
432 return n;
433 }
434
435
436 #define SER_BASE_SIZE ((5 * sizeof(int)) + (3 * sizeof(int64_t)) +1) // Total size of the fixed-length fields in a
437 // serialized P7_ALIDISPLAY
438
439 /* Function: p7_alidisplay_Serialize
440 * Synopsis: Serializes a HMMD_SEARCH_STATS object into a stream of bytes
441 *. that can be reliably transmitted over internet sockets
442 *
443 * Purpose: Converts an architecture-dependent P7_SEARCH_STATS object into a contiguous stream
444 * of bytes with each field of the data structure in network byte order for transmission
445 * over sockets. The serialized byte stream may be part of a larger allocated buffer.
446 * If the provided buffer is NULL, allocates a new buffer large enough for the serialized object
447 * If the provided buffer is not large enough to hold the serialized object and its existing data, re-allocates
448 * a larger buffer
449 *
450 * Inputs: obj: A pointer to the HMMD_SEARCH_STATS object to be serialized
451 * buf: Handle to the buffer that the object should be serialized into. If *buf is NULL,
452 * a new buffer will be allocated. buf == NULL is not allowed.
453 * n: Offset (in bytes) from the start of the buffer to where the serialized object should start.
454 * nalloc: size (in bytes) of the buffer passed in buf
455 *
456 *Returns: On success: returns eslOK, sets *buf to the base of the buffer containing the object
457 * if allocation or re-allocation was requried, sets *n to the offset from the start of the buffer
458 * to the first position after the serialized object and sets *nalloc to the new size of the buffer
459 * if allocation or re-allocation was required.
460 *
461 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if obj == NULL, n == NULL, or buf == NULL
462 * Returns eslFAIL if one of the internal calculations fails a consistency check.
463 */
464 int p7_alidisplay_Serialize(const P7_ALIDISPLAY *obj, uint8_t **buf, uint32_t *n, uint32_t *nalloc){
465
466 int status; // error variable used by ESL_ALLOC
467 uint32_t ser_size; // size of the structure when serialized
468 uint8_t *ptr; // current position within the buffer
469 uint32_t network_32bit; // hold 32-bit fields after conversion to network order
470 uint64_t network_64bit; // hold 64-bit fields after conversion to network order
471 uint8_t presence_flags = 0; // Bit-vector that records presence or absence of optional strings
472 uint32_t hmmname_length, hmmacc_length, hmmdesc_length, sqname_length, sqacc_length, sqdesc_length;
473
474 // check to make sure we were passed a valid pointer
475 if(obj == NULL || buf == NULL || n == NULL){ // no object to serialize or nowhere to put a buffer pointer
476 return(eslEINVAL);
477 }
478
479 // Pass 1: Compute size of the serialized data structure
480 /* 11 ints: 4 int fields in P7_ALIDISPLAY + lengths of 6 variable-length strings + total length of serialized structure
481 3 int64_t fields in P7_ALIDISPLAY
482 1 byte for presence/absence bit vector for rfline, mmline, csline, ppline, aseq, ntseq
483 */
484 ser_size = SER_BASE_SIZE;
485
486 // This method will work regardless of whether the object is in "serialized" or "deserialized" format
487 // it's also blatantly stolen from the _Sizeof() routine.
488 // Note that we can't just call _Sizeof() to figure out how big the serialized data structure will be because
489 // the serialized structure has some fields that differ from the base P7_ALIDISPLAY structure
490
491 if (obj->rfline){
492 presence_flags += RFLINE_PRESENT;
493 ser_size += obj->N+1; /* +1 for \0 */
494 }
495
496 if (obj->mmline) {
497 presence_flags += MMLINE_PRESENT;
498 ser_size += obj->N+1;
499 }
500
501 if (obj->csline){
502 presence_flags += CSLINE_PRESENT;
503 ser_size += obj->N+1;
504 }
505
506 ser_size += 2 * (obj->N+1); /* model, mline */
507
508 if (obj->aseq){
509 presence_flags += ASEQ_PRESENT;
510 ser_size += obj->N+1;
511 }
512
513 if (obj->ntseq){
514 presence_flags += NTSEQ_PRESENT;
515 ser_size += (3 * obj->N) + 1; /* ntseq */
516 }
517
518 if (obj->ppline){
519 presence_flags += PPLINE_PRESENT;
520 ser_size += obj->N+1;
521 }
522
523 hmmname_length = strlen(obj->hmmname);
524 ser_size += 1 + hmmname_length;
525
526 hmmacc_length = strlen(obj->hmmacc);
527 ser_size += 1 + hmmacc_length; /* optional acc, desc fields: when not present, just "" ("\0") */
528
529 hmmdesc_length = strlen(obj->hmmdesc);
530 ser_size += 1 + hmmdesc_length;
531
532 sqname_length = strlen(obj->sqname);
533 ser_size += 1 + sqname_length;
534
535 sqacc_length = strlen(obj->sqacc);
536 ser_size += 1 + sqacc_length;
537
538 sqdesc_length = strlen(obj->sqdesc);
539 ser_size += 1 + sqdesc_length;
540
541 // Now that we know how big the serialized data structure will be, determine if we have enough buffer space to hold it
542 if(*buf == NULL){ // have no buffer, so allocate one
543 ESL_ALLOC(*buf, ser_size);
544 *nalloc = ser_size;
545 }
546
547 if((*n + ser_size) > *nalloc){ //have a buffer, but it's not big enough
548 ESL_REALLOC(*buf, (*n + ser_size));
549 *nalloc = *n + ser_size;
550 }
551
552 // Pass 2: serialize the structure
553
554 //First, the fixed-length field
555 ptr = *buf + *n; // point to the start of empty space in the buffer
556
557 network_32bit = esl_hton32(ser_size); // first field in the serialized object is its size
558 memcpy(ptr, &network_32bit, sizeof(uint32_t)); // Write size of the serialized object into the buffer
559 ptr += sizeof(uint32_t);
560
561 // Field 2: N
562 network_32bit = esl_hton32(obj->N);
563 memcpy(ptr, &network_32bit, sizeof(uint32_t));
564 ptr += sizeof(uint32_t);
565
566 // Field 3: Hmmfrom field
567 network_32bit = esl_hton32(obj->hmmfrom);
568 memcpy(ptr, &network_32bit, sizeof(uint32_t));
569 ptr += sizeof(uint32_t);
570
571 // Field 4: Hmmto field
572 network_32bit = esl_hton32(obj->hmmto);
573 memcpy(ptr, &network_32bit, sizeof(uint32_t));
574 ptr += sizeof(uint32_t);
575
576 // Field 5: M field
577 network_32bit = esl_hton32(obj->M);
578 memcpy(ptr, &network_32bit, sizeof(uint32_t));
579 ptr += sizeof(uint32_t);
580
581 // Field 6: Sqfrom
582 network_64bit = esl_hton64(obj->sqfrom);
583 memcpy(ptr, &network_64bit, sizeof(int64_t));
584 ptr += sizeof(int64_t);
585
586 // Field 7: Sqto
587 network_64bit = esl_hton64(obj->sqto);
588 memcpy(ptr, &network_64bit, sizeof(int64_t));
589 ptr += sizeof(int64_t);
590
591 // Field 8: L
592 network_64bit = esl_hton64(obj->L);
593 memcpy(ptr, &network_64bit, sizeof(int64_t));
594 ptr += sizeof(int64_t);
595
596 // Field 9: presence_flags
597 memcpy(ptr, &presence_flags, sizeof(uint8_t));
598 ptr += sizeof(uint8_t);
599
600 //Now, the strings, some of which are optional
601 // Note that many of these strings are fixed-length if they are present
602
603 // Field 10: Rfline
604 if(presence_flags & RFLINE_PRESENT){
605 strcpy((char *) ptr, obj->rfline);
606 ptr += obj->N + 1;
607 }
608
609 // Field 11: mmline
610 if(presence_flags & MMLINE_PRESENT){
611 strcpy((char *) ptr, obj->mmline);
612 ptr += obj->N + 1;
613 }
614
615 // Field 12: csline
616 if(presence_flags & CSLINE_PRESENT){
617 strcpy((char *) ptr, obj->csline);
618 ptr += obj->N + 1;
619 }
620
621 // Field 13: Model
622 strcpy((char *) ptr, obj->model);
623 ptr += obj->N + 1;
624
625 // Field 14: Mline
626 strcpy((char *) ptr, obj->mline);
627 ptr += obj->N + 1;
628
629 // Field 15: Aseq
630 if(presence_flags & ASEQ_PRESENT){
631 strcpy((char *) ptr, obj->aseq);
632 ptr += obj->N + 1;
633 }
634
635 // Field 16: ntseq
636 if(presence_flags & NTSEQ_PRESENT){
637 strcpy((char *) ptr, obj->ntseq);
638 ptr += (3 * obj->N) + 1;
639 }
640
641 // Field 17: PPline
642 if(presence_flags & PPLINE_PRESENT){
643 strcpy((char *) ptr, obj->ppline);
644 ptr += obj->N + 1;
645 }
646
647 // Field 18: Hmmname
648 strcpy((char *) ptr, obj->hmmname);
649 ptr += hmmname_length + 1;
650
651 // Field 19: Hmmacc
652 strcpy((char *) ptr, obj->hmmacc);
653 ptr += hmmacc_length + 1;
654
655 // Field 20: Hmmdesc
656 strcpy((char *) ptr, obj->hmmdesc);
657 ptr += hmmdesc_length + 1;
658
659 // Field 21: Sqname
660 strcpy((char *) ptr, obj->sqname);
661 ptr += sqname_length +1;
662
663 // Field 22: Sqacc
664 strcpy((char *) ptr, obj->sqacc);
665 ptr += sqacc_length +1 ;
666
667 // Field 23: Sqdesc
668 strcpy((char *) ptr, obj->sqdesc);
669 ptr += sqdesc_length +1 ;
670
671 // sanity-check that we computed the length correctly
672 if(ptr != *buf + *n + ser_size){
673 ESL_EXCEPTION(eslFAIL, "Serialized object length did not match computed length in p7_alidisplay_Serialize");
674 }
675
676 *n = ptr - *buf; // update n to be just past the serialized object
677 return eslOK; // If we make it here, everything succeeded, so return a pass
678
679 ERROR:
680 return eslEMEM;
681 }
682
683 /* Function: p7_alidisplay_Deserialize
684 * Synopsis: Derializes a P7_ALIDISPLAY object from a stream of bytes in network order into
685 * a valid data structure
686 *
687 * Purpose: Deserializes a serialized P7_ALIDISPLAY object from
688 *. buf starting at position position *pos.
689 *
690 * Inputs: buf: the buffer that the object should be de-serialized from
691 * pos: a pointer to the offset from the start of buf to the beginning of the object
692 * ret_obj: a P7_ALIDISPLAY structure to deserialize the object into. May not be NULL. May either be an
693 * "empty" object created with p7_alidisplay_Create_empty, or a P7_ALIDISPLAY object containing valid data
694 *
695 * Returns: On success: returns eslOK, deserializes the P7_ALIDISPLAY object into ret_object, and updates
696 *. pos to point to the position after the end of the P7_ALIDISPLAY object.
697 *
698 * Throws: Returns eslEINVAL if ret_obj == NULL, buf == NULL, or N == NULL. Returns eslEMEM if unable to increase
699 * the buffer in ret_obj to match the size of the deserialized object. Returns eslFAIL if one of the
700 * internal calculations fails a consistency check.
701 */
702 extern int p7_alidisplay_Deserialize(const uint8_t *buf, uint32_t *n, P7_ALIDISPLAY *ret_obj){
703 int status; // Standard Easel error code variable
704
705 uint8_t *ptr;
706 char *mem_ptr;
707 uint64_t network_64bit; // holds 64-bit values in network order
708 uint32_t network_32bit; // holds 64-bit values in network order
709 uint32_t obj_size; // How much space does the variable-length portion of the serialized object take up?
710 uint8_t presence_flags; // bit-vector that tells us which strings are present in the object
711 int string_length; // used to hold the length of strings copied out of serialized object
712
713 if ((buf == NULL) || (ret_obj == NULL) || (n == NULL)){ // check to make sure we've been passed valid objects
714 return(eslEINVAL);
715 }
716
717 mem_ptr = NULL; // set this for safety
718 ptr = (uint8_t *) buf + *n; // Get pointer to start of the object
719 //First field: Size of the serialized object. Copy out of buffer into scalar variable to deal with memory alignment, convert to
720 // host machine order
721 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
722 obj_size = esl_ntoh32(network_32bit);
723 ptr += sizeof(uint32_t);
724
725 if(ret_obj->memsize < (obj_size - SER_BASE_SIZE)){ // ret_obj doesn't have enough space for this P7_ALIDISPLAY
726 if(ret_obj->mem != NULL){
727 ESL_REALLOC(ret_obj->mem, (obj_size - SER_BASE_SIZE));
728 }
729 else{
730 ESL_ALLOC(ret_obj->mem, (obj_size - SER_BASE_SIZE));
731 }
732 ret_obj->memsize = obj_size - SER_BASE_SIZE;
733 }
734
735 // Second field: N
736 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
737 ret_obj->N = esl_ntoh32(network_32bit);
738 ptr += sizeof(uint32_t);
739
740 // Third field: Hmmfrom
741 memcpy(&network_32bit, ptr, sizeof(uint32_t));
742 ret_obj->hmmfrom = esl_ntoh32(network_32bit);
743 ptr += sizeof(uint32_t);
744
745 // Fourth field: Hmmto
746 memcpy(&network_32bit, ptr, sizeof(uint32_t));
747 ret_obj->hmmto = esl_ntoh32(network_32bit);
748 ptr += sizeof(uint32_t);
749
750 // Fifth field: M
751 memcpy(&network_32bit, ptr, sizeof(uint32_t));
752 ret_obj->M = esl_ntoh32(network_32bit);
753 ptr += sizeof(uint32_t);
754
755 // Sixth field: sqfrom
756 memcpy(&network_64bit, ptr, sizeof(uint64_t));
757 ret_obj->sqfrom = esl_ntoh64(network_64bit);
758 ptr += sizeof(uint64_t);
759
760 // Seventh field: sqto
761 memcpy(&network_64bit, ptr, sizeof(uint64_t));
762 ret_obj->sqto = esl_ntoh64(network_64bit);
763 ptr += sizeof(uint64_t);
764
765 // Eighth field: L
766 memcpy(&network_64bit, ptr, sizeof(uint64_t));
767 ret_obj->L = esl_ntoh64(network_64bit);
768 ptr += sizeof(uint64_t);
769
770 // Ninth field: presence flags
771 presence_flags = *ptr; // no need for memcpy with one-byte field
772 ptr += sizeof(uint8_t);
773
774 // Bulk copy the strings into the alidisplay's mem field
775 memcpy(ret_obj->mem, ptr, (obj_size - SER_BASE_SIZE));
776 mem_ptr = ret_obj->mem;
777 ptr += (obj_size - SER_BASE_SIZE);
778 // Tenth field: rfline, if present
779
780 if(ptr != buf + *n + obj_size){
781 ESL_EXCEPTION(eslFAIL, "In p7_alidisplay_Deserialize, found object (ptr) to be of size %ld, expected %u.\n", (long int) (ptr - (buf + *n)), obj_size);
782 }
783
784 if(presence_flags & RFLINE_PRESENT){
785 ret_obj->rfline = mem_ptr;
786 string_length = strlen(ret_obj->rfline);
787 mem_ptr+= string_length +1; // + 1 to account for end-of-string character
788 }
789 else{ // not present
790 ret_obj->rfline = NULL;
791 }
792
793 // Eleventh field: mmline, if present
794 if(presence_flags & MMLINE_PRESENT){
795 ret_obj->mmline = mem_ptr;
796 string_length = strlen(ret_obj->mmline);
797 mem_ptr+= string_length + 1;
798 }
799 else{ // not present
800 ret_obj->mmline = NULL;
801 }
802
803 // Twelfth field: csline, if present
804 if(presence_flags & CSLINE_PRESENT){
805 ret_obj->csline = mem_ptr;
806 string_length = strlen(ret_obj->csline);
807 mem_ptr+= string_length + 1;
808 }
809 else{ // not present
810 ret_obj->csline = NULL;
811 }
812
813
814 // Thirteenth field: model
815 ret_obj->model = mem_ptr;
816 string_length = strlen(ret_obj->model);
817 mem_ptr+= string_length + 1;
818
819 // Thirteenth field: mline
820 ret_obj->mline = mem_ptr;
821 string_length = strlen(ret_obj->mline);
822 mem_ptr+= string_length + 1;
823
824 // Fourteenth field: aseq, if present
825 if(presence_flags & ASEQ_PRESENT){
826 ret_obj->aseq = mem_ptr;
827 string_length = strlen(ret_obj->aseq);
828 mem_ptr+= string_length + 1;
829 }
830 else{ // not present
831 ret_obj->aseq = NULL;
832 }
833
834 // Fifteenth field: ntseq, if present
835 if(presence_flags & NTSEQ_PRESENT){
836 ret_obj->ntseq = mem_ptr;
837 string_length = strlen(ret_obj->ntseq);
838 mem_ptr+= string_length + 1;
839 }
840 else{ // not present
841 ret_obj->ntseq = NULL;
842 }
843
844 // Sixteenth field: ppline, if present
845 if(presence_flags & PPLINE_PRESENT){
846 ret_obj->ppline = mem_ptr;
847 string_length = strlen(ret_obj->ppline);
848 mem_ptr+= string_length + 1;
849 }
850 else{ // not present
851 ret_obj->ppline = NULL;
852 }
853
854 // Seventeenth field: hmmname
855 ret_obj->hmmname = mem_ptr;
856 string_length = strlen(ret_obj->hmmname);
857 mem_ptr+= string_length + 1;
858
859 // Eighteenth field: hmmacc
860 ret_obj->hmmacc = mem_ptr;
861 string_length = strlen(ret_obj->hmmacc);
862 mem_ptr+= string_length + 1;
863
864 // Nineteenth field: hmmdesc
865 ret_obj->hmmdesc = mem_ptr;
866 string_length = strlen(ret_obj->hmmdesc);
867 mem_ptr+= string_length + 1;
868
869 // Twentyith field: sqname
870 ret_obj->sqname = mem_ptr;
871 string_length = strlen(ret_obj->sqname);
872 mem_ptr+= string_length + 1;
873
874 // Twentyfirst field: sqacc
875 ret_obj->sqacc = mem_ptr;
876 string_length = strlen(ret_obj->sqacc);
877 mem_ptr+= string_length + 1;
878
879 // Twentysecond field: sqdesc
880 ret_obj->sqdesc = mem_ptr;
881 string_length = strlen(ret_obj->sqdesc);
882 mem_ptr+= string_length +1;
883
884 // Sanity-check that we got the length right
885 if(mem_ptr - ret_obj->mem != (obj_size - SER_BASE_SIZE)){
886 ESL_EXCEPTION(eslFAIL, "At end of p7_alidisplay_Deserialize, found strings to be of size %ld, expected %ld.\n", (long)(mem_ptr - ret_obj->mem), (long)(obj_size - SER_BASE_SIZE));
887 }
888 *n += obj_size;
889 return eslOK; // as usual, if we get to the end of the routine without failing, return success
890
891 ERROR: // only get here if we can't allocate memory
892 return eslEMEM;
893 }
894
895 /* Function: p7_alidisplay_Serialize()
896 * Synopsis: Serialize a P7_ALIDISPLAY, using internal memory.
897 *
898 * Purpose: Serialize the <P7_ALIDISPLAY> <ad>, internally converting
899 * all its variable-length allocations to a single
900 * contiguous memory allocation. Serialization aids
901 * interprocess communication.
902 *
903 * If <ad> is already serialized, do nothing.
904 *
905 * Args: ad - alidisplay to serialize
906 *
907 * Returns: <eslOK> on success.
908 *
909 * Throws: <eslEMEM> on allocation failure, and <ad> is restored to
910 * its original (deserialized) state.
911 */
912 int
913 p7_alidisplay_Serialize_old(P7_ALIDISPLAY *ad)
914 {
915 int pos;
916 int n;
917 int status;
918
919 if (ad->mem) return eslOK; /* already serialized, so no-op */
920 ad->memsize = p7_alidisplay_Sizeof(ad) - sizeof(P7_ALIDISPLAY);
921 ESL_ALLOC(ad->mem, ad->memsize);
922
923 /* allow no exceptions past this point, because API guarantees restore of original state upon error */
924
925 pos = 0;
926 if (ad->rfline) { memcpy(ad->mem+pos, ad->rfline, ad->N+1); free(ad->rfline); ad->rfline = ad->mem+pos; pos += ad->N+1; }
927 if (ad->mmline) { memcpy(ad->mem+pos, ad->mmline, ad->N+1); free(ad->mmline); ad->mmline = ad->mem+pos; pos += ad->N+1; }
928 if (ad->csline) { memcpy(ad->mem+pos, ad->csline, ad->N+1); free(ad->csline); ad->csline = ad->mem+pos; pos += ad->N+1; }
929 memcpy(ad->mem+pos, ad->model, ad->N+1); free(ad->model); ad->model = ad->mem+pos; pos += ad->N+1;
930 memcpy(ad->mem+pos, ad->mline, ad->N+1); free(ad->mline); ad->mline = ad->mem+pos; pos += ad->N+1;
931 memcpy(ad->mem+pos, ad->aseq, ad->N+1); free(ad->aseq); ad->aseq = ad->mem+pos; pos += ad->N+1;
932 if (ad->ntseq) { memcpy(ad->mem+pos, ad->ntseq, (3*ad->N)+1); free(ad->ntseq); ad->ntseq = ad->mem+pos; pos += (3*ad->N)+1; }
933 if (ad->ppline) { memcpy(ad->mem+pos, ad->ppline, ad->N+1); free(ad->ppline); ad->ppline = ad->mem+pos; pos += ad->N+1; }
934 n = 1 + strlen(ad->hmmname); memcpy(ad->mem + pos, ad->hmmname, n); free(ad->hmmname); ad->hmmname = ad->mem+pos; pos += n;
935 n = 1 + strlen(ad->hmmacc); memcpy(ad->mem + pos, ad->hmmacc, n); free(ad->hmmacc); ad->hmmacc = ad->mem+pos; pos += n;
936 n = 1 + strlen(ad->hmmdesc); memcpy(ad->mem + pos, ad->hmmdesc, n); free(ad->hmmdesc); ad->hmmdesc = ad->mem+pos; pos += n;
937 n = 1 + strlen(ad->sqname); memcpy(ad->mem + pos, ad->sqname, n); free(ad->sqname); ad->sqname = ad->mem+pos; pos += n;
938 n = 1 + strlen(ad->sqacc); memcpy(ad->mem + pos, ad->sqacc, n); free(ad->sqacc); ad->sqacc = ad->mem+pos; pos += n;
939 n = 1 + strlen(ad->sqdesc); memcpy(ad->mem + pos, ad->sqdesc, n); free(ad->sqdesc); ad->sqdesc = ad->mem+pos; pos += n;
940
941 return eslOK;
942
943 ERROR:
944 if (ad->mem) { free(ad->mem); ad->mem = NULL; }
945 return status;
946 }
947
948 /* Function: p7_alidisplay_Deserialize()
949 * Synopsis: Deserialize a P7_ALIDISPLAY, using internal memory.
950 *
951 * Purpose: Deserialize the <P7_ALIDISPLAY> <ad>, converting its internal
952 * allocations from a single contiguous memory chunk to individual
953 * variable-length allocations. Deserialization facilitates
954 * reallocation/editing of individual elements of the display.
955 *
956 * If <ad> is already deserialized, do nothing.
957 *
958 * Args: ad - alidisplay to serialize
959 *
960 * Returns: <eslOK> on success
961 *
962 * Throws: <eslEMEM> on allocation failure, and <ad> is restored to
963 * its original (serialized) state.
964 */
965 int
966 p7_alidisplay_Deserialize_old(P7_ALIDISPLAY *ad)
967 {
968 int pos;
969 int n;
970 int status;
971
972 if (ad->mem == NULL) return eslOK; /* already deserialized, so no-op */
973
974 pos = 0;
975 if (ad->rfline) { ESL_ALLOC(ad->rfline, sizeof(char) * ad->N+1); memcpy(ad->rfline, ad->mem+pos, ad->N+1); pos += ad->N+1; }
976 if (ad->mmline) { ESL_ALLOC(ad->mmline, sizeof(char) * ad->N+1); memcpy(ad->mmline, ad->mem+pos, ad->N+1); pos += ad->N+1; }
977 if (ad->csline) { ESL_ALLOC(ad->csline, sizeof(char) * ad->N+1); memcpy(ad->csline, ad->mem+pos, ad->N+1); pos += ad->N+1; }
978 ESL_ALLOC(ad->model, sizeof(char) * ad->N+1); memcpy(ad->model, ad->mem+pos, ad->N+1); pos += ad->N+1;
979 ESL_ALLOC(ad->mline, sizeof(char) * ad->N+1); memcpy(ad->mline, ad->mem+pos, ad->N+1); pos += ad->N+1;
980 ESL_ALLOC(ad->aseq, sizeof(char) * ad->N+1); memcpy(ad->aseq, ad->mem+pos, ad->N+1); pos += ad->N+1;
981 if (ad->ntseq) { ESL_ALLOC(ad->ntseq, sizeof(char) * (3*ad->N)+1); memcpy(ad->ntseq, ad->mem+pos, (3*ad->N)+1); pos += (3*ad->N)+1; }
982 if (ad->ppline) { ESL_ALLOC(ad->ppline, sizeof(char) * ad->N+1); memcpy(ad->ppline, ad->mem+pos, ad->N+1); pos += ad->N+1; }
983 n = 1 + strlen(ad->mem+pos); ESL_ALLOC(ad->hmmname, sizeof(char) * n); memcpy(ad->hmmname, ad->mem+pos, n); pos += n;
984 n = 1 + strlen(ad->mem+pos); ESL_ALLOC(ad->hmmacc, sizeof(char) * n); memcpy(ad->hmmacc, ad->mem+pos, n); pos += n;
985 n = 1 + strlen(ad->mem+pos); ESL_ALLOC(ad->hmmdesc, sizeof(char) * n); memcpy(ad->hmmdesc, ad->mem+pos, n); pos += n;
986 n = 1 + strlen(ad->mem+pos); ESL_ALLOC(ad->sqname, sizeof(char) * n); memcpy(ad->sqname, ad->mem+pos, n); pos += n;
987 n = 1 + strlen(ad->mem+pos); ESL_ALLOC(ad->sqacc, sizeof(char) * n); memcpy(ad->sqacc, ad->mem+pos, n); pos += n;
988 n = 1 + strlen(ad->mem+pos); ESL_ALLOC(ad->sqdesc, sizeof(char) * n); memcpy(ad->sqdesc, ad->mem+pos, n); pos += n;
989
990 free(ad->mem);
991 ad->mem = NULL;
992 ad->memsize = 0;
993 return eslOK;
994
995 ERROR:
996 /* restore serialized state, if an alloc fails. tedious, if not nontrivial. */
997 /* the pointers are non-NULL whether we just allocated them or if they're pointing into mem, so we have to check against mem+pos */
998 pos = 0;
999 if (ad->rfline) { if (ad->rfline != ad->mem+pos) { free(ad->rfline); ad->rfline = ad->mem+pos; } pos += ad->N+1; }
1000 if (ad->mmline) { if (ad->mmline != ad->mem+pos) { free(ad->mmline); ad->mmline = ad->mem+pos; } pos += ad->N+1; }
1001 if (ad->csline) { if (ad->csline != ad->mem+pos) { free(ad->csline); ad->csline = ad->mem+pos; } pos += ad->N+1; }
1002 if (ad->model != ad->mem+pos) { free(ad->model); ad->model = ad->mem+pos; } pos += ad->N+1;
1003 if (ad->mline != ad->mem+pos) { free(ad->mline); ad->mline = ad->mem+pos; } pos += ad->N+1;
1004 if (ad->aseq != ad->mem+pos) { free(ad->aseq); ad->aseq = ad->mem+pos; } pos += ad->N+1;
1005 if (ad->ntseq) { if (ad->ntseq != ad->mem+pos) { free(ad->ntseq); ad->ntseq = ad->mem+pos; } pos += (3*ad->N)+1; }
1006 if (ad->ppline) { if (ad->ppline != ad->mem+pos) { free(ad->ppline); ad->ppline = ad->mem+pos; } pos += ad->N+1; }
1007
1008 n = 1 + strlen(ad->hmmname); if (ad->hmmname != ad->mem+pos) { free(ad->hmmname); ad->hmmname = ad->mem+pos; } pos += n;
1009 n = 1 + strlen(ad->hmmacc); if (ad->hmmacc != ad->mem+pos) { free(ad->hmmacc); ad->hmmacc = ad->mem+pos; } pos += n;
1010 n = 1 + strlen(ad->hmmname); if (ad->hmmdesc != ad->mem+pos) { free(ad->hmmdesc); ad->hmmdesc = ad->mem+pos; } pos += n;
1011 n = 1 + strlen(ad->sqname); if (ad->sqname != ad->mem+pos) { free(ad->sqname); ad->sqname = ad->mem+pos; } pos += n;
1012 n = 1 + strlen(ad->sqacc); if (ad->sqacc != ad->mem+pos) { free(ad->sqacc); ad->sqacc = ad->mem+pos; } pos += n;
1013 n = 1 + strlen(ad->sqname); if (ad->sqdesc != ad->mem+pos) { free(ad->sqdesc); ad->sqdesc = ad->mem+pos; } pos += n;
1014 return status;
1015 }
1016
1017
1018 /* Function: p7_alidisplay_Destroy()
1019 * Synopsis: Frees a <P7_ALIDISPLAY>
1020 */
1021 void
1022 p7_alidisplay_Destroy(P7_ALIDISPLAY *ad)
1023 {
1024 if (ad == NULL) return;
1025 if (ad->mem)
1026 { /* serialized form */
1027 free(ad->mem);
1028 }
1029 else
1030 { /* deserialized form */
1031 if (ad->rfline) free(ad->rfline);
1032 if (ad->mmline) free(ad->mmline);
1033 if (ad->csline) free(ad->csline);
1034 if (ad->model) free(ad->model);
1035 if (ad->mline) free(ad->mline);
1036 if (ad->aseq) free(ad->aseq);
1037 if (ad->ntseq) free(ad->ntseq);
1038 if (ad->ppline) free(ad->ppline);
1039 if (ad->hmmname) free(ad->hmmname);
1040 if (ad->hmmacc) free(ad->hmmacc);
1041 if (ad->hmmdesc) free(ad->hmmdesc);
1042 if (ad->sqname) free(ad->sqname);
1043 if (ad->sqacc) free(ad->sqacc);
1044 if (ad->sqdesc) free(ad->sqdesc);
1045 }
1046 free(ad);
1047 }
1048 /*---------------- end, alidisplay object -----------------------*/
1049
1050
1051
1052 /*****************************************************************
1053 * 2. The P7_ALIDISPLAY API
1054 *****************************************************************/
1055
1056 static int
1057 integer_textwidth(long n)
1058 {
1059 int w = (n < 0)? 1 : 0;
1060 while (n != 0) { n /= 10; w++; }
1061 return w;
1062 }
1063
1064
1065 /* Function: p7_alidisplay_EncodePostProb()
1066 * Synopsis: Convert a posterior probability to a char code.
1067 *
1068 * Purpose: Convert the posterior probability <p> to
1069 * a character code suitable for Stockholm format
1070 * <#=GC PP_cons> and <#=GR seqname PP> annotation
1071 * lines. HMMER uses the same codes in alignment
1072 * output.
1073 *
1074 * Characters <0-9*> are used; $0.0 \leq p < 0.05$
1075 * is coded as 0, $0.05 \leq p < 0.15$ is coded as
1076 * 1, ... and so on ..., $0.85 \leq p < 0.95$ is
1077 * coded as 9, and $0.95 \leq p \leq 1.0$ is coded
1078 * as '*'.
1079 *
1080 * Returns: the encoded character.
1081 */
1082 char
1083 p7_alidisplay_EncodePostProb(float p)
1084 {
1085 return (p + 0.05 >= 1.0) ? '*' : (char) ((p + 0.05) * 10.0) + '0';
1086 }
1087
1088 /* Function: p7_alidisplay_DecodePostProb()
1089 * Synopsis: Convert a char code post prob to an approx float.
1090 *
1091 * Purpose: Convert posterior probability code <pc>, which
1092 * is [0-9*], to an approximate floating point probability.
1093 *
1094 * The result is crude, because <pc> has already discretized
1095 * with loss of precision. We require that
1096 * <p7_alidisplay_EncodePostProb(p7_alidisplay_DecodePostProb(pc)) == pc>,
1097 * and that <pc=='0'> decodes to a nonzero probability just to
1098 * avoid any possible absorbing-zero artifacts.
1099 *
1100 * Returns: the decoded real-valued approximate probability.
1101 */
1102 float
1103 p7_alidisplay_DecodePostProb(char pc)
1104 {
1105 if (pc == '0') return 0.01;
1106 else if (pc == '*') return 1.0;
1107 else if (pc == '.') return 0.0;
1108 else return ((float) (pc - '0') / 10.);
1109 }
1110
1111
1112 /* Function: p7_alidisplay_Print()
1113 * Synopsis: Human readable output of <P7_ALIDISPLAY>
1114 *
1115 * Purpose: Prints alignment <ad> to stream <fp>.
1116 *
1117 * Put at least <min_aliwidth> alignment characters per
1118 * line; try to make lines no longer than <linewidth>
1119 * characters, including name, coords, and spacing. The
1120 * width of lines may exceed <linewidth>, if that's what it
1121 * takes to put a name, coords, and <min_aliwidth>
1122 * characters of alignment on a line.
1123 *
1124 * As a special case, if <linewidth> is negative or 0, then
1125 * alignments are formatted in a single block of unlimited
1126 * line length.
1127 *
1128 * Returns: <eslOK> on success.
1129 *
1130 * Throws: <eslEWRITE> on write error, such as filling the disk.
1131 */
1132 int
1133 p7_alidisplay_Print(FILE *fp, P7_ALIDISPLAY *ad, int min_aliwidth, int linewidth, P7_PIPELINE *pli)
1134 {
1135 int status;
1136 if ((status = p7_nontranslated_alidisplay_Print(fp, ad, min_aliwidth, linewidth, pli->show_accessions)) != eslOK) return status;
1137
1138 return status;
1139 }
1140
1141 /* Function: p7_nontranslated_alidisplay_Print()
1142 * Synopsis: Human readable output of <P7_ALIDISPLAY>
1143 *
1144 * Purpose: Prints alignment <ad> to stream <fp>.
1145 *
1146 * Put at least <min_aliwidth> alignment characters per
1147 * line; try to make lines no longer than <linewidth>
1148 * characters, including name, coords, and spacing. The
1149 * width of lines may exceed <linewidth>, if that's what it
1150 * takes to put a name, coords, and <min_aliwidth>
1151 * characters of alignment on a line.
1152 *
1153 * As a special case, if <linewidth> is negative or 0, then
1154 * alignments are formatted in a single block of unlimited
1155 * line length.
1156 *
1157 * Returns: <eslOK> on success.
1158 *
1159 * Throws: <eslEWRITE> on write error, such as filling the disk.
1160 */
1161 int
1162 p7_nontranslated_alidisplay_Print(FILE *fp, P7_ALIDISPLAY *ad, int min_aliwidth, int linewidth, int show_accessions)
1163 {
1164 char *buf = NULL;
1165 char *show_hmmname = NULL;
1166 char *show_seqname = NULL;
1167 int namewidth, coordwidth, aliwidth;
1168 int pos;
1169 int status;
1170 int ni, nk;
1171 int z;
1172 long i1,i2;
1173 int k1,k2;
1174
1175 /* implement the --acc option for preferring accessions over names in output */
1176 show_hmmname = (show_accessions && ad->hmmacc[0] != '\0') ? ad->hmmacc : ad->hmmname;
1177 show_seqname = (show_accessions && ad->sqacc[0] != '\0') ? ad->sqacc : ad->sqname;
1178
1179 /* dynamically size the output lines */
1180 namewidth = ESL_MAX(strlen(show_hmmname), strlen(show_seqname));
1181 coordwidth = ESL_MAX(ESL_MAX(integer_textwidth(ad->hmmfrom),
1182 integer_textwidth(ad->hmmto)),
1183 ESL_MAX(integer_textwidth(ad->sqfrom),
1184 integer_textwidth(ad->sqto)));
1185
1186 aliwidth = (linewidth > 0) ? linewidth - namewidth - 2*coordwidth - 5 : ad->N;
1187 if (aliwidth < ad->N && aliwidth < min_aliwidth) aliwidth = min_aliwidth; /* at least, regardless of some silly linewidth setting */
1188 ESL_ALLOC(buf, sizeof(char) * (aliwidth+1));
1189 buf[aliwidth] = 0;
1190
1191 /* Break the alignment into multiple blocks of width aliwidth for printing */
1192 i1 = ad->sqfrom;
1193 k1 = ad->hmmfrom;
1194 for (pos = 0; pos < ad->N; pos += aliwidth)
1195 {
1196 if (pos > 0) { if (fprintf(fp, "\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); } /* blank line betweeen blocks */
1197
1198 ni = nk = 0;
1199 for (z = pos; z < pos + aliwidth && z < ad->N; z++) {
1200 if (ad->model[z] != '.') nk++; /* k advances except on insert states */
1201 if (ad->aseq[z] != '-') ni++; /* i advances except on delete states */
1202 }
1203
1204 k2 = k1+nk-1;
1205 if (ad->sqfrom < ad->sqto) i2 = i1+ni-1;
1206 else i2 = i1-ni+1; // revcomp hit for DNA
1207
1208 if (ad->csline != NULL) { strncpy(buf, ad->csline+pos, aliwidth); if (fprintf(fp, " %*s %s CS\n", namewidth+coordwidth+1, "", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); }
1209 if (ad->rfline != NULL) { strncpy(buf, ad->rfline+pos, aliwidth); if (fprintf(fp, " %*s %s RF\n", namewidth+coordwidth+1, "", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); }
1210 if (ad->mmline != NULL) { strncpy(buf, ad->mmline+pos, aliwidth); if (fprintf(fp, " %*s %s MM\n", namewidth+coordwidth+1, "", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); }
1211
1212 strncpy(buf, ad->model+pos, aliwidth); if (fprintf(fp, " %*s %*d %s %-*d\n", namewidth, show_hmmname, coordwidth, k1, buf, coordwidth, k2) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed");
1213 strncpy(buf, ad->mline+pos, aliwidth); if (fprintf(fp, " %*s %s\n", namewidth+coordwidth+1, " ", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed");
1214
1215 if (ni > 0) { strncpy(buf, ad->aseq+pos, aliwidth); if (fprintf(fp, " %*s %*ld %s %-*ld\n", namewidth, show_seqname, coordwidth, i1, buf, coordwidth, i2) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); }
1216 else { strncpy(buf, ad->aseq+pos, aliwidth); if (fprintf(fp, " %*s %*s %s %*s\n", namewidth, show_seqname, coordwidth, "-", buf, coordwidth, "-") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); }
1217
1218 if (ad->ppline != NULL) { strncpy(buf, ad->ppline+pos, aliwidth); if (fprintf(fp, " %*s %s PP\n", namewidth+coordwidth+1, "", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "alignment display write failed"); }
1219
1220 k1 += nk;
1221 if (ad->sqfrom < ad->sqto) i1 += ni;
1222 else i1 -= ni; // revcomp hit for DNA
1223 }
1224 fflush(fp);
1225 free(buf);
1226 return eslOK;
1227
1228 ERROR:
1229 if (buf) free(buf);
1230 return status;
1231 }
1232
1233 /* Function: p7_alidisplay_Backconvert()
1234 * Synopsis: Convert an alidisplay to a faux trace and subsequence.
1235 *
1236 * Purpose: Convert alignment display object <ad> to a faux subsequence
1237 * and faux subsequence trace, returning them in <ret_sq> and
1238 * <ret_tr>.
1239 *
1240 * The subsequence <*ret_sq> is digital; ascii residues in
1241 * <ad> are digitized using digital alphabet <abc>.
1242 *
1243 * The subsequence and trace are suitable for passing as
1244 * array elements to <p7_tracealign_Seqs>. This is the
1245 * main purpose of backconversion. Results of a profile
1246 * search are stored in a hit list as a processed
1247 * <P7_ALIDISPLAY>, not as a <P7_TRACE> and <ESL_SQ>, to
1248 * reduce space and to reduce communication overhead in
1249 * parallelized search implementations. After reduction
1250 * to a final hit list, a master may want to construct a
1251 * multiple alignment of all the significant hits.
1252 *
1253 * Returns: <eslOK> on success.
1254 *
1255 * Throws: <eslEMEM> on allocation failures. <eslECORRUPT> on unexpected internal
1256 * data corruption. On any exception, <*ret_sq> and <*ret_tr> are
1257 * <NULL>.
1258 *
1259 * Xref: SRE:J4/29.
1260 */
1261 int
1262 p7_alidisplay_Backconvert(const P7_ALIDISPLAY *ad, const ESL_ALPHABET *abc, ESL_SQ **ret_sq, P7_TRACE **ret_tr)
1263 {
1264 ESL_SQ *sq = NULL; /* RETURN: faux subsequence */
1265 P7_TRACE *tr = NULL; /* RETURN: faux trace */
1266 int subL = 0; /* subsequence length in the <ad> */
1267 int a, i, k; /* coords for <ad>, <sq->dsq>, model */
1268 char s; /* current state type: MDI */
1269 int status;
1270
1271 /* Make a first pass over <ad> just to calculate subseq length */
1272 for (a = 0; a < ad->N; a++)
1273 if (! esl_abc_CIsGap(abc, ad->aseq[a])) subL++;
1274
1275 /* Allocations */
1276 if ((sq = esl_sq_CreateDigital(abc)) == NULL) { status = eslEMEM; goto ERROR; }
1277 if ((status = esl_sq_GrowTo(sq, subL)) != eslOK) goto ERROR;
1278
1279 if ((tr = (ad->ppline == NULL) ? p7_trace_Create() : p7_trace_CreateWithPP()) == NULL) { status = eslEMEM; goto ERROR; }
1280 if ((status = p7_trace_GrowTo(tr, subL+6)) != eslOK) goto ERROR; /* +6 is for SNB/ECT */
1281
1282 /* Construction of dsq, trace */
1283 sq->dsq[0] = eslDSQ_SENTINEL;
1284 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, p7T_S, 0, 0) : p7_trace_AppendWithPP(tr, p7T_S, 0, 0, 0.0))) != eslOK) goto ERROR;
1285 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, p7T_N, 0, 0) : p7_trace_AppendWithPP(tr, p7T_N, 0, 0, 0.0))) != eslOK) goto ERROR;
1286 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, p7T_B, 0, 0) : p7_trace_AppendWithPP(tr, p7T_B, 0, 0, 0.0))) != eslOK) goto ERROR;
1287 k = ad->hmmfrom - 1; // -1 so the first M causes k to advance to <hmmfrom>.
1288 i = 1; // ... which assumes <ad> always starts with M; currently true, all alis are local alis.
1289 for (a = 0; a < ad->N; a++)
1290 {
1291 /* here, anything that could appear in the original input
1292 * sequence, as opposed to an alignment gap, needs to be
1293 * reconstructed. So, do not test for IsResidue(), because that
1294 * will fail to reconstruct * chars. use ! IsGap() instead.
1295 * [xref iss#135]
1296 */
1297 if (! esl_abc_CIsGap(abc, ad->model[a])) { k++; s = (! esl_abc_CIsGap(abc, ad->aseq[a]) ? p7T_M : p7T_D); }
1298 else s = p7T_I;
1299
1300 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, s, k, i) : p7_trace_AppendWithPP(tr, s, k, i, p7_alidisplay_DecodePostProb(ad->ppline[a])))) != eslOK) goto ERROR;
1301
1302 switch (s) {
1303 case p7T_M: sq->dsq[i] = esl_abc_DigitizeSymbol(abc, ad->aseq[a]); i++; break;
1304 case p7T_I: sq->dsq[i] = esl_abc_DigitizeSymbol(abc, ad->aseq[a]); i++; break;
1305 case p7T_D: break;
1306 }
1307 }
1308 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, p7T_E, 0, 0) : p7_trace_AppendWithPP(tr, p7T_E, 0, 0, 0.0))) != eslOK) goto ERROR;
1309 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, p7T_C, 0, 0) : p7_trace_AppendWithPP(tr, p7T_C, 0, 0, 0.0))) != eslOK) goto ERROR;
1310 if ((status = ((ad->ppline == NULL) ? p7_trace_Append(tr, p7T_T, 0, 0) : p7_trace_AppendWithPP(tr, p7T_T, 0, 0, 0.0))) != eslOK) goto ERROR;
1311 sq->dsq[i] = eslDSQ_SENTINEL;
1312
1313 /* some sanity checks */
1314 if (tr->N != ad->N + 6) ESL_XEXCEPTION(eslECORRUPT, "backconverted trace ended up with unexpected size (%s/%s)", ad->sqname, ad->hmmname);
1315 if (k != ad->hmmto) ESL_XEXCEPTION(eslECORRUPT, "backconverted trace didn't end at expected place on model (%s/%s)", ad->sqname, ad->hmmname);
1316 if (i != subL+1) ESL_XEXCEPTION(eslECORRUPT, "backconverted subseq didn't end at expected length (%s/%s)", ad->sqname, ad->hmmname);
1317
1318 /* Set up <sq> annotation as a subseq of a source sequence */
1319 if ((status = esl_sq_FormatName(sq, "%s/%" PRId64 "-%" PRId64 "", ad->sqname, ad->sqfrom, ad->sqto)) != eslOK) goto ERROR;
1320 if ((status = esl_sq_FormatDesc(sq, "[subseq from] %s", ad->sqdesc[0] != '\0' ? ad->sqdesc : ad->sqname)) != eslOK) goto ERROR;
1321 if ((status = esl_sq_SetSource (sq, ad->sqname)) != eslOK) goto ERROR;
1322 if (ad->sqacc[0] != '\0') { if ((status = esl_sq_SetAccession (sq, ad->sqacc)) != eslOK) goto ERROR; }
1323 sq->n = subL;
1324 sq->start = ad->sqfrom;
1325 sq->end = ad->sqto;
1326 sq->C = 0;
1327 sq->W = subL;
1328 sq->L = ad->L;
1329
1330 tr->M = ad->M;
1331 tr->L = ad->L;
1332
1333 *ret_sq = sq;
1334 *ret_tr = tr;
1335 return eslOK;
1336
1337 ERROR:
1338 if (sq != NULL) esl_sq_Destroy(sq);
1339 if (tr != NULL) p7_trace_Destroy(tr);
1340 *ret_sq = NULL;
1341 *ret_tr = NULL;
1342 return status;
1343 }
1344 /*------------------- end, alidisplay API -----------------------*/
1345
1346
1347 /*****************************************************************
1348 * 3. Debugging/dev code
1349 *****************************************************************/
1350
1351
1352 /* Function: p7_alidisplay_Sample()
1353 * Synopsis: Sample a random, ugly <P7_ALIDISPLAY> for test purposes
1354 * Incept: SRE, Wed Feb 28 14:22:12 2018 [Caravan Palace, Dragons]
1355 *
1356 * Purpose: Sample a random, dirty <P7_ALIDISPLAY> of length <N> for
1357 * testing purposes, using random number generator <rng>.
1358 * Return it through <ret_ad>. Caller frees.
1359 *
1360 * P7_ALIDISPLAY is assumed to be a _local_ alignment.
1361 * Must start with M, and end with M|D.
1362 *
1363 * Args: rng - random number generator
1364 * N - length of alignment
1365 * ret_ad - RETURN: random sampled <P7_ALIDISPLAY>
1366 *
1367 * Returns: <eslOK> on success, and <ret_ad> points to the new
1368 * <P7_ALIDISPLAY>
1369 *
1370 * Throws: <eslEMEM> on allocation error, and <ret_ad> is NULL.
1371 */
1372 int
1373 p7_alidisplay_Sample(ESL_RANDOMNESS *rng, int N, P7_ALIDISPLAY **ret_ad)
1374 {
1375 P7_ALIDISPLAY *ad = NULL;
1376 char *guidestring = NULL; /* string [0..N-1] composed of MDI */
1377 int nM = 0;
1378 int nD = 0;
1379 int nI = 0;
1380 enum p7t_statetype_e last_st;
1381 int pos;
1382 int status;
1383
1384 ESL_ALLOC(guidestring, sizeof(char) * (N+1));
1385
1386 guidestring[0] = 'M'; nM++; last_st = p7T_M; /* local alignments must start with M */
1387 for (pos = 1; pos < N-1; pos++)
1388 {
1389 switch (last_st)
1390 {
1391 case p7T_M:
1392 switch (esl_rnd_Roll(rng, 3))
1393 {
1394 case 0: guidestring[pos] = 'M'; nM++; last_st = p7T_M; break;
1395 case 1: guidestring[pos] = 'D'; nD++; last_st = p7T_D; break;
1396 case 2: guidestring[pos] = 'I'; nI++; last_st = p7T_I; break;
1397 }
1398 break;
1399
1400 case p7T_I:
1401 switch (esl_rnd_Roll(rng, 2))
1402 {
1403 case 0: guidestring[pos] = 'M'; nM++; last_st = p7T_M; break;
1404 case 1: guidestring[pos] = 'I'; nI++; last_st = p7T_I; break;
1405 }
1406 break;
1407
1408 case p7T_D:
1409 switch (esl_rnd_Roll(rng, 2))
1410 {
1411 case 0: guidestring[pos] = 'M'; nM++; last_st = p7T_M; break;
1412 case 1: guidestring[pos] = 'D'; nD++; last_st = p7T_D; break;
1413 }
1414 break;
1415
1416 default:
1417 break;
1418 }
1419 }
1420 /* local alignments can end on M or D. (optimal local alignments can only end on M) */
1421 switch (last_st) {
1422 case p7T_I:
1423 guidestring[N-1] = 'M'; nM++; break;
1424 default:
1425 switch (esl_rnd_Roll(rng, 2)) {
1426 case 0: guidestring[N-1] = 'M'; nM++; break;
1427 case 1: guidestring[N-1] = 'D'; nD++; break;
1428 }
1429 break;
1430 }
1431 guidestring[N] = '\0';
1432
1433 ESL_ALLOC(ad, sizeof(P7_ALIDISPLAY));
1434 ad->rfline = ad->mmline = ad->csline = ad->model = ad->mline = ad->aseq = ad->ntseq = ad->ppline = NULL;
1435 ad->hmmname = ad->hmmacc = ad->hmmdesc = NULL;
1436 ad->sqname = ad->sqacc = ad->sqdesc = NULL;
1437 ad->mem = NULL;
1438 ad->memsize = 0;
1439
1440 /* Optional lines are added w/ 50% chance */
1441 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->rfline, sizeof(char) * (N+1));
1442 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->mmline, sizeof(char) * (N+1));
1443 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->csline, sizeof(char) * (N+1));
1444 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->ppline, sizeof(char) * (N+1));
1445 ESL_ALLOC(ad->model, sizeof(char) * (N+1));
1446 ESL_ALLOC(ad->mline, sizeof(char) * (N+1));
1447 ESL_ALLOC(ad->aseq, sizeof(char) * (N+1));
1448 ad->N = N;
1449
1450 esl_strdup("my_hmm", -1, &(ad->hmmname));
1451 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("PF000007", -1, &(ad->hmmacc)); else esl_strdup("", -1, &(ad->hmmacc));
1452 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("(hmm description)", -1, &(ad->hmmdesc)); else esl_strdup("", -1, &(ad->hmmdesc));
1453
1454 esl_strdup("my_seq", -1, &(ad->sqname));
1455 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("ABC000001.42", -1, &(ad->sqacc)); else esl_strdup("", -1, &(ad->sqacc));
1456 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("(sequence description)", -1, &(ad->sqdesc)); else esl_strdup("", -1, &(ad->sqdesc));
1457
1458 /* model, seq coords must look valid. */
1459 ad->hmmfrom = 100;
1460 ad->hmmto = ad->hmmfrom + nM + nD - 1;
1461 ad->M = ad->hmmto + esl_rnd_Roll(rng, 2);
1462
1463 ad->sqfrom = 1000;
1464 ad->sqto = ad->sqfrom + nM + nI - 1;
1465 ad->L = ad->sqto + esl_rnd_Roll(rng, 2);
1466
1467 /* rfline is free-char "reference annotation" on consensus; H3 puts '.' for inserts. */
1468 if (ad->rfline) {
1469 for (pos = 0; pos < N; pos++)
1470 ad->rfline[pos] = (guidestring[pos] == 'I' ? '.' : 'x');
1471 ad->rfline[pos] = '\0';
1472 }
1473
1474 /* mmline indicates which columns should be masked (assigned background distribution), '.' indicates no mask; H3 puts '.' for inserts. */
1475 if (ad->mmline) {
1476 for (pos = 0; pos < N; pos++)
1477 ad->mmline[pos] = (guidestring[pos] == 'I' ? '.' : '.');
1478 ad->mmline[pos] = '\0';
1479 }
1480
1481 /* csline is optional. It has free-char "consensus structure annotation" on consensus positions. H3 puts '.' on inserts. */
1482 if (ad->csline) {
1483 for (pos = 0; pos < N; pos++)
1484 ad->csline[pos] = (guidestring[pos] == 'I' ? '.' : 'X');
1485 ad->csline[pos] = '\0';
1486 }
1487
1488 /* the mandatory three-line alignment display:
1489 *
1490 * guidestring: MMMDI
1491 * model: XXXX.
1492 * mline: A+
1493 * aseq: AAA-a
1494 */
1495 for (pos = 0; pos < N; pos++)
1496 {
1497 switch (guidestring[pos]) {
1498 case 'M':
1499 ad->model[pos] = 'X';
1500 switch (esl_rnd_Roll(rng, 3)) {
1501 case 0: ad->mline[pos] = 'A';
1502 case 1: ad->mline[pos] = '+';
1503 case 2: ad->mline[pos] = ' ';
1504 }
1505 if (ad->mline[pos] == ' ' && esl_rnd_Roll(rng, 50) == 0) ad->aseq[pos] = '*'; // dirty aligned sequence up with nasty * stop codons, about 1/(3*50) of the time.
1506 else ad->aseq[pos] = 'A'; // ... they would only be aligned to ' ' on an mline.
1507 break; // ... they might appear aligned to a match or insert state (see iss#135)
1508
1509 case 'D':
1510 ad->model[pos] = 'X';
1511 ad->mline[pos] = ' ';
1512 ad->aseq[pos] = '-';
1513 break;
1514
1515 case 'I':
1516 ad->model[pos] = '.';
1517 ad->mline[pos] = ' ';
1518 ad->aseq[pos] = 'a';
1519 break;
1520 }
1521 }
1522 ad->model[pos] = '\0';
1523 ad->mline[pos] = '\0';
1524 ad->aseq[pos] = '\0';
1525
1526 /* ppline is optional */
1527 if (ad->ppline) {
1528 for (pos = 0; pos < N; pos++)
1529 ad->ppline[pos] = (guidestring[pos] == 'D' ? '.' : p7_alidisplay_EncodePostProb(esl_random(rng)));
1530 ad->ppline[pos] = '\0';
1531 }
1532
1533 free(guidestring);
1534 *ret_ad = ad;
1535 return eslOK;
1536
1537 ERROR:
1538 if (guidestring) free(guidestring);
1539 if (ad) p7_alidisplay_Destroy(ad);
1540 *ret_ad = NULL;
1541 return status;
1542 }
1543
1544
1545
1546 /* Function: p7_alidisplay_Dump()
1547 * Synopsis: Print contents of P7_ALIDISPLAY for inspection.
1548 *
1549 * Purpose: Print contents of the <P7_ALIDISPLAY> <ad> to
1550 * stream <fp> for inspection. Includes all elements
1551 * of the structure, whether the object is allocated
1552 * in serialized or deserialized form, and the total
1553 * size of the object in bytes.
1554 *
1555 * Returns: <eslOK>
1556 */
1557 int
1558 p7_alidisplay_Dump(FILE *fp, const P7_ALIDISPLAY *ad)
1559 {
1560 fprintf(fp, "P7_ALIDISPLAY dump\n");
1561 fprintf(fp, "------------------\n");
1562
1563 fprintf(fp, "rfline = %s\n", ad->rfline ? ad->rfline : "[none]");
1564 fprintf(fp, "mmline = %s\n", ad->mmline ? ad->mmline : "[none]");
1565 fprintf(fp, "csline = %s\n", ad->csline ? ad->csline : "[none]");
1566 fprintf(fp, "model = %s\n", ad->model);
1567 fprintf(fp, "mline = %s\n", ad->mline);
1568 fprintf(fp, "aseq = %s\n", ad->aseq);
1569 fprintf(fp, "N = %d\n", ad->N);
1570 fprintf(fp, "\n");
1571
1572 fprintf(fp, "hmmname = %s\n", ad->hmmname);
1573 fprintf(fp, "hmmacc = %s\n", ad->hmmacc[0] == '\0' ? "[none]" : ad->hmmacc);
1574 fprintf(fp, "hmmdesc = %s\n", ad->hmmdesc[0] == '\0' ? "[none]" : ad->hmmdesc);
1575 fprintf(fp, "hmmfrom = %d\n", ad->hmmfrom);
1576 fprintf(fp, "hmmto = %d\n", ad->hmmto);
1577 fprintf(fp, "M = %d\n", ad->M);
1578 fprintf(fp, "\n");
1579
1580 fprintf(fp, "sqname = %s\n", ad->sqname);
1581 fprintf(fp, "sqacc = %s\n", ad->sqacc[0] == '\0' ? "[none]" : ad->sqacc);
1582 fprintf(fp, "sqdesc = %s\n", ad->sqdesc[0] == '\0' ? "[none]" : ad->sqdesc);
1583 fprintf(fp, "sqfrom = %" PRId64 "\n", ad->sqfrom);
1584 fprintf(fp, "sqto = %" PRId64 "\n", ad->sqto);
1585 fprintf(fp, "L = %" PRId64 "\n", ad->L);
1586 fprintf(fp, "\n");
1587
1588 fprintf(fp, "size = %d bytes\n", (int) p7_alidisplay_Sizeof(ad));
1589 fprintf(fp, "%s\n", ad->mem ? "serialized" : "not serialized");
1590 return eslOK;
1591 }
1592
1593 /* Function: p7_alidisplay_Compare()
1594 * Synopsis: Compare two <P7_ALIDISPLAY> objects for equality
1595 *
1596 * Purpose: Compare alignment displays <ad1> and <ad2> for
1597 * equality. Return <eslOK> if they have identical
1598 * contents; <eslFAIL> if not.
1599 *
1600 * Only contents matter, not serialization status;
1601 * a serialized and deserialized version of the same
1602 * alidisplay will compare identical.
1603 */
1604 int
1605 p7_alidisplay_Compare(const P7_ALIDISPLAY *ad1, const P7_ALIDISPLAY *ad2)
1606 {
1607 if (ad1->mem && ad2->mem) /* both objects serialized */
1608 {
1609 if (ad1->memsize != ad2->memsize) return eslFAIL;
1610 if (memcmp(ad1->mem, ad2->mem, ad1->memsize) != 0) return eslFAIL;
1611 }
1612
1613 if (esl_strcmp(ad1->rfline, ad2->rfline) != eslOK) return eslFAIL;
1614 if (esl_strcmp(ad1->mmline, ad2->mmline) != eslOK) return eslFAIL;
1615 if (esl_strcmp(ad1->csline, ad2->csline) != eslOK) return eslFAIL;
1616 if (esl_strcmp(ad1->model, ad2->model) != eslOK) return eslFAIL;
1617 if (esl_strcmp(ad1->mline, ad2->mline) != eslOK) return eslFAIL;
1618 if (esl_strcmp(ad1->aseq, ad2->aseq) != eslOK) return eslFAIL;
1619 if (esl_strcmp(ad1->ntseq, ad2->ntseq) != eslOK) return eslFAIL;
1620 if (esl_strcmp(ad1->ppline, ad2->ppline) != eslOK) return eslFAIL;
1621 if (ad1->N != ad2->N) return eslFAIL;
1622
1623 if (esl_strcmp(ad1->hmmname, ad2->hmmname) != eslOK) return eslFAIL;
1624 if (esl_strcmp(ad1->hmmacc, ad2->hmmacc) != eslOK) return eslFAIL;
1625 if (esl_strcmp(ad1->hmmdesc, ad2->hmmdesc) != eslOK) return eslFAIL;
1626 if (ad1->hmmfrom != ad2->hmmfrom) return eslFAIL;
1627 if (ad1->hmmto != ad2->hmmto) return eslFAIL;
1628 if (ad1->M != ad2->M) return eslFAIL;
1629
1630 if (esl_strcmp(ad1->sqname, ad2->sqname) != eslOK) return eslFAIL;
1631 if (esl_strcmp(ad1->sqacc, ad2->sqacc) != eslOK) return eslFAIL;
1632 if (esl_strcmp(ad1->sqdesc, ad2->sqdesc) != eslOK) return eslFAIL;
1633 if (ad1->sqfrom != ad2->sqfrom) return eslFAIL;
1634 if (ad1->sqto != ad2->sqto) return eslFAIL;
1635 if (ad1->M != ad2->M) return eslFAIL;
1636
1637 return eslOK;
1638 }
1639
1640
1641 /*-------------- end, debugging/dev code ------------------------*/
1642
1643
1644
1645 /*****************************************************************
1646 * 4. Benchmark driver.
1647 *****************************************************************/
1648 #ifdef p7ALIDISPLAY_BENCHMARK
1649 /*
1650 gcc -o benchmark-alidisplay -std=gnu99 -g -Wall -O2 -I. -L. -I../easel -L../easel -Dp7ALIDISPLAY_BENCHMARK p7_alidisplay.c -lhmmer -leasel -lm
1651
1652 ./benchmark-alidisplay <hmmfile> runs benchmark
1653 ./benchmark-alidisplay -b <hmmfile> gets baseline time to subtract: just random trace generation
1654 */
1655 #include <p7_config.h>
1656
1657 #include "easel.h"
1658 #include "esl_alphabet.h"
1659 #include "esl_getopts.h"
1660 #include "esl_random.h"
1661 #include "esl_stopwatch.h"
1662
1663 #include "hmmer.h"
1664
1665 static ESL_OPTIONS options[] = {
1666 /* name type default env range toggles reqs incomp help docgroup*/
1667 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1668 { "-b", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "baseline timing", 0 },
1669 { "-p", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "include fake PP line, just to see how it looks", 0 },
1670 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
1671 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of traces to generate", 0 },
1672 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1673 };
1674 static char usage[] = "[-options] <hmmfile>";
1675 static char banner[] = "benchmark driver for P7_ALIDISPLAY";
1676
1677 int
1678 main(int argc, char **argv)
1679 {
1680 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
1681 char *hmmfile = esl_opt_GetArg(go, 1);
1682 int N = esl_opt_GetInteger(go, "-N");
1683 ESL_STOPWATCH *w = esl_stopwatch_Create();
1684 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
1685 ESL_ALPHABET *abc = NULL;
1686 P7_HMMFILE *hfp = NULL;
1687 P7_HMM *hmm = NULL;
1688 P7_BG *bg = NULL;
1689 P7_PROFILE *gm = NULL;
1690 P7_OPROFILE *om = NULL;
1691 P7_TRACE *tr = NULL;
1692 ESL_SQ *sq = NULL;
1693 P7_ALIDISPLAY *ad = NULL;
1694 int i,z;
1695
1696 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
1697 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
1698 p7_hmmfile_Close(hfp);
1699
1700
1701 bg = p7_bg_Create(abc);
1702 p7_bg_SetLength(bg, 0);
1703 gm = p7_profile_Create(hmm->M, abc);
1704 p7_ProfileConfig(hmm, bg, gm, 0, p7_UNIGLOCAL); /* that sets N,C,J to generate nothing */
1705 om = p7_oprofile_Create(gm->M, abc);
1706 p7_oprofile_Convert(gm, om);
1707
1708 if (esl_opt_GetBoolean(go, "-p")) tr = p7_trace_CreateWithPP();
1709 else tr = p7_trace_Create();
1710
1711 sq = esl_sq_CreateDigital(abc);
1712
1713 esl_stopwatch_Start(w);
1714 for (i = 0; i < N; i++)
1715 {
1716 p7_ProfileEmit(r, hmm, gm, bg, sq, tr);
1717 esl_sq_SetName(sq, "random");
1718
1719 if (! esl_opt_GetBoolean(go, "-b"))
1720 {
1721 if (esl_opt_GetBoolean(go, "-p"))
1722 for (z = 0; z < tr->N; z++)
1723 if (tr->i[z] > 0) tr->pp[z] = esl_random(r);
1724
1725 ad = p7_alidisplay_Create(tr, 0, om, sq, NULL);
1726 p7_alidisplay_Print(stdout, ad, 40, 80, FALSE);
1727 p7_alidisplay_Destroy(ad);
1728 }
1729 p7_trace_Reuse(tr);
1730 esl_sq_Reuse(sq);
1731 }
1732 esl_stopwatch_Stop(w);
1733 esl_stopwatch_Display(stdout, w, "# CPU time: ");
1734
1735 esl_sq_Destroy(sq);
1736 p7_trace_Destroy(tr);
1737 p7_oprofile_Destroy(om);
1738 p7_profile_Destroy(gm);
1739 p7_bg_Destroy(bg);
1740 p7_hmm_Destroy(hmm);
1741 esl_alphabet_Destroy(abc);
1742 esl_randomness_Destroy(r);
1743 esl_stopwatch_Destroy(w);
1744 esl_getopts_Destroy(go);
1745 return 0;
1746 }
1747 #endif /*p7ALIDISPLAY_BENCHMARK*/
1748 /*--------------------- end, benchmark driver -------------------*/
1749
1750 /****************************************************************
1751 * 5. Unit tests.
1752 ****************************************************************/
1753 #ifdef p7ALIDISPLAY_TESTDRIVE
1754
1755 /*Testing function that generates a P7_ALIDISPLAY containing a nucleotide sequence string rather than an amino
1756 *sequence string. This function should only be used for testing the serialization/deserializaton code. No attempt
1757 *is made to make the nucleotide string be reasonable or even valid -- it's just a valid C string of the correct length
1758 * Like p7_alidisplay_Sample, which it is based on, it randomly selects which of the optional fields should be present in the
1759 * alidisplay */
1760 static int
1761 alidisplay_SampleFake_ntseq(ESL_RANDOMNESS *rng, int N, P7_ALIDISPLAY **ret_ad)
1762 {
1763 P7_ALIDISPLAY *ad = NULL;
1764 char *guidestring = NULL; /* string [0..N-1] composed of MDI */
1765 int nM = 0;
1766 int nD = 0;
1767 int nI = 0;
1768 enum p7t_statetype_e last_st;
1769 int pos;
1770 int status;
1771
1772 ESL_ALLOC(guidestring, sizeof(char) * (N+1));
1773
1774 guidestring[0] = 'M'; nM++; last_st = p7T_M; /* local alignments must start with M */
1775 for (pos = 1; pos < N-1; pos++)
1776 {
1777 switch (last_st)
1778 {
1779 case p7T_M:
1780 switch (esl_rnd_Roll(rng, 3))
1781 {
1782 case 0: guidestring[pos] = 'M'; nM++; last_st = p7T_M; break;
1783 case 1: guidestring[pos] = 'D'; nD++; last_st = p7T_D; break;
1784 case 2: guidestring[pos] = 'I'; nI++; last_st = p7T_I; break;
1785 }
1786 break;
1787
1788 case p7T_I:
1789 switch (esl_rnd_Roll(rng, 2))
1790 {
1791 case 0: guidestring[pos] = 'M'; nM++; last_st = p7T_M; break;
1792 case 1: guidestring[pos] = 'I'; nI++; last_st = p7T_I; break;
1793 }
1794 break;
1795
1796 case p7T_D:
1797 switch (esl_rnd_Roll(rng, 2))
1798 {
1799 case 0: guidestring[pos] = 'M'; nM++; last_st = p7T_M; break;
1800 case 1: guidestring[pos] = 'D'; nD++; last_st = p7T_D; break;
1801 }
1802 break;
1803
1804 default:
1805 break;
1806 }
1807 }
1808 /* local alignments can end on M or D. (optimal local alignments can only end on M) */
1809 switch (last_st) {
1810 case p7T_I:
1811 guidestring[N-1] = 'M'; nM++; break;
1812 default:
1813 switch (esl_rnd_Roll(rng, 2)) {
1814 case 0: guidestring[N-1] = 'M'; nM++; break;
1815 case 1: guidestring[N-1] = 'D'; nD++; break;
1816 }
1817 break;
1818 }
1819 guidestring[N] = '\0';
1820
1821 ESL_ALLOC(ad, sizeof(P7_ALIDISPLAY));
1822 ad->rfline = ad->mmline = ad->csline = ad->model = ad->mline = ad->aseq = ad->ntseq = ad->ppline = NULL;
1823 ad->hmmname = ad->hmmacc = ad->hmmdesc = NULL;
1824 ad->sqname = ad->sqacc = ad->sqdesc = NULL;
1825 ad->mem = NULL;
1826 ad->memsize = 0;
1827
1828 /* Optional lines are added w/ 50% chance */
1829 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->rfline, sizeof(char) * (N+1));
1830 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->mmline, sizeof(char) * (N+1));
1831 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->csline, sizeof(char) * (N+1));
1832 if (esl_rnd_Roll(rng, 2) == 0) ESL_ALLOC(ad->ppline, sizeof(char) * (N+1));
1833 ESL_ALLOC(ad->model, sizeof(char) * (N+1));
1834 ESL_ALLOC(ad->mline, sizeof(char) * (N+1));
1835 ESL_ALLOC(ad->ntseq, sizeof(char) * ((3 *N)+1));
1836 ad->N = N;
1837
1838 esl_strdup("my_hmm", -1, &(ad->hmmname));
1839 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("PF000007", -1, &(ad->hmmacc)); else esl_strdup("", -1, &(ad->hmmacc));
1840 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("(hmm description)", -1, &(ad->hmmdesc)); else esl_strdup("", -1, &(ad->hmmdesc));
1841
1842 esl_strdup("my_seq", -1, &(ad->sqname));
1843 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("ABC000001.42", -1, &(ad->sqacc)); else esl_strdup("", -1, &(ad->sqacc));
1844 if (esl_rnd_Roll(rng, 2) == 0) esl_strdup("(sequence description)", -1, &(ad->sqdesc)); else esl_strdup("", -1, &(ad->sqdesc));
1845
1846 /* model, seq coords must look valid. */
1847 ad->hmmfrom = 100;
1848 ad->hmmto = ad->hmmfrom + nM + nD - 1;
1849 ad->M = ad->hmmto + esl_rnd_Roll(rng, 2);
1850
1851 ad->sqfrom = 1000;
1852 ad->sqto = ad->sqfrom + nM + nI - 1;
1853 ad->L = ad->sqto + esl_rnd_Roll(rng, 2);
1854
1855 /* rfline is free-char "reference annotation" on consensus; H3 puts '.' for inserts. */
1856 if (ad->rfline) {
1857 for (pos = 0; pos < N; pos++)
1858 ad->rfline[pos] = (guidestring[pos] == 'I' ? '.' : 'x');
1859 ad->rfline[pos] = '\0';
1860 }
1861
1862 /* mmline indicates which columns should be masked (assigned background distribution), '.' indicates no mask; H3 puts '.' for inserts. */
1863 if (ad->mmline) {
1864 for (pos = 0; pos < N; pos++)
1865 ad->mmline[pos] = (guidestring[pos] == 'I' ? '.' : '.');
1866 ad->mmline[pos] = '\0';
1867 }
1868
1869 /* csline is optional. It has free-char "consensus structure annotation" on consensus positions. H3 puts '.' on inserts. */
1870 if (ad->csline) {
1871 for (pos = 0; pos < N; pos++)
1872 ad->csline[pos] = (guidestring[pos] == 'I' ? '.' : 'X');
1873 ad->csline[pos] = '\0';
1874 }
1875
1876 /* the mandatory three-line alignment display:
1877 *
1878 * guidestring: MMMDI
1879 * model: XXXX.
1880 * mline: A+
1881 * ntseq: AAA-a
1882 */
1883 for (pos = 0; pos < N; pos++)
1884 {
1885 switch (guidestring[pos]) {
1886 case 'M':
1887 ad->model[pos] = 'X';
1888 switch (esl_rnd_Roll(rng, 3)) {
1889 case 0: ad->mline[pos] = 'A';
1890 case 1: ad->mline[pos] = '+';
1891 case 2: ad->mline[pos] = ' ';
1892 }
1893 if (ad->mline[pos] == ' ' && esl_rnd_Roll(rng, 50) == 0) ad->ntseq[pos] = '*'; // dirty aligned sequence up with nasty * stop codons, about 1/(3*50) of the time.
1894 else ad->ntseq[pos] = 'A'; // ... they would only be aligned to ' ' on an mline.
1895 break; // ... they might appear aligned to a match or insert state (see iss#135)
1896
1897 case 'D':
1898 ad->model[pos] = 'X';
1899 ad->mline[pos] = ' ';
1900 ad->ntseq[pos] = '-';
1901 break;
1902
1903 case 'I':
1904 ad->model[pos] = '.';
1905 ad->mline[pos] = ' ';
1906 ad->ntseq[pos] = 'a';
1907 break;
1908 }
1909 }
1910 ad->model[pos] = '\0';
1911 ad->mline[pos] = '\0';
1912
1913 // pad out ntseq to the 3N length it needs
1914 for (pos = N; pos < (3 * N); pos++){
1915 if(esl_rnd_Roll(rng, 50) == 0) ad->ntseq[pos] = '*'; // dirty aligned sequence up with nasty * stop codons, about 1/(3*50) of the time.
1916 else ad->ntseq[pos] = 'A'; // ... they would only be aligned to ' ' on an mline.
1917 }
1918 ad->ntseq[pos] = '\0';
1919
1920 /* ppline is optional */
1921 if (ad->ppline) {
1922 for (pos = 0; pos < N; pos++)
1923 ad->ppline[pos] = (guidestring[pos] == 'D' ? '.' : p7_alidisplay_EncodePostProb(esl_random(rng)));
1924 ad->ppline[pos] = '\0';
1925 }
1926
1927 free(guidestring);
1928 *ret_ad = ad;
1929 return eslOK;
1930
1931 ERROR:
1932 if (guidestring) free(guidestring);
1933 if (ad) p7_alidisplay_Destroy(ad);
1934 *ret_ad = NULL;
1935 return status;
1936 }
1937
1938
1939
1940
1941 static void
1942 utest_Serialize(ESL_RANDOMNESS *rng, int ntrials)
1943 {
1944 char msg[] = "utest_Serialize failed";
1945 P7_ALIDISPLAY **serial = malloc(ntrials * sizeof(P7_ALIDISPLAY *));
1946 P7_ALIDISPLAY **deserial = malloc(ntrials * sizeof(P7_ALIDISPLAY *));
1947 uint8_t **buf = malloc(sizeof(uint8_t *));
1948 uint32_t n = 0;
1949 uint32_t nalloc = 0;
1950 int alignment_length;
1951 int i;
1952
1953 *buf = NULL;
1954 for (i = 0; i < ntrials; i++)
1955 {
1956 // Create random alignment to serialize
1957 alignment_length = (esl_random_uint32(rng) %300) + 50;
1958 if (esl_rnd_Roll(rng, 2) == 0)
1959 { // 50% chance of alidisplay with an amino sequence, 50% chance of alidisplay with nucleotide seq.
1960 if (p7_alidisplay_Sample(rng, alignment_length, &(serial[i])) != eslOK) esl_fatal(msg);
1961 }
1962 else
1963 {
1964 if (alidisplay_SampleFake_ntseq(rng, alignment_length, &(serial[i])) != eslOK) esl_fatal(msg);
1965 }
1966 if (p7_alidisplay_Serialize(serial[i], buf, &n, &nalloc) != eslOK) esl_fatal(msg);
1967 }
1968
1969 n = 0; // reset to start of buffer
1970 for (i = 0; i < ntrials; i++)
1971 {
1972 if ((deserial[i] = p7_alidisplay_Create_empty()) == NULL) esl_fatal(msg);
1973 if( p7_alidisplay_Deserialize(*buf, &n, deserial[i]) != eslOK) esl_fatal(msg);
1974 }
1975
1976 // free the buffer here to make sure we've actually copied all the data out of it into the new structures
1977 free(*buf);
1978 free(buf);
1979 for (i = 0; i < ntrials; i++)
1980 if (p7_alidisplay_Compare(serial[i], deserial[i]) != eslOK) esl_fatal(msg); // deserialized structure didn't match serialized
1981 // haven't failed yet, so we've succeeded. Clean up and exit
1982
1983 for (i = 0; i < ntrials; i++) {
1984 p7_alidisplay_Destroy(serial[i]);
1985 p7_alidisplay_Destroy(deserial[i]);
1986 }
1987 free(serial);
1988 free(deserial);
1989 return;
1990 }
1991
1992 // Test that the _Serialize() function generates the correct errors when passed invalid arguments
1993 static void
1994 utest_serialize_error_conditions(ESL_RANDOMNESS *rng)
1995 {
1996 char msg[] = "utest_serialize_error_conditions failed";
1997 P7_ALIDISPLAY *foo = NULL;
1998 uint8_t **buf = malloc(sizeof(uint8_t *));
1999 uint32_t n = 0;
2000 uint32_t nalloc = 0;
2001
2002 // Create an alisplay to work with. Don't really care about its contents -- other tests will verify
2003 // correct serialization and deserialization
2004 *buf = NULL; // set buf to valid value
2005 if ( p7_alidisplay_Sample(rng, 100, &foo) != eslOK) esl_fatal(msg);
2006 if ( p7_alidisplay_Serialize(foo, NULL, &n, &nalloc) != eslEINVAL) esl_fatal(msg); // Test 1: _Serialize returns error if passed NULL buffer
2007 if ( p7_alidisplay_Serialize(foo, buf, NULL, &nalloc) != eslEINVAL) esl_fatal(msg); // Test 2: error on NULL n ptr
2008 if ( p7_alidisplay_Serialize(NULL, buf, &n, &nalloc) != eslEINVAL) esl_fatal(msg); // Test 3: error on NULL object ptr
2009
2010 if (buf) { free(*buf); free(buf); }
2011 p7_alidisplay_Destroy(foo);
2012 return;
2013 }
2014
2015 static void
2016 utest_deserialize_error_conditions(ESL_RANDOMNESS *rng)
2017 {
2018 char msg[] = "utest_deserialize_error_conditions failed";
2019 P7_ALIDISPLAY *sampled = NULL; // sampled alidisplay that we'll serialze
2020 P7_ALIDISPLAY *deserial = NULL; // alidisplay to hold the deserialized object
2021 uint8_t *buf = NULL;
2022 uint32_t n = 0, nalloc = 0;
2023
2024 if ((deserial = p7_alidisplay_Create_empty()) == NULL) esl_fatal(msg);
2025 if ( p7_alidisplay_Sample(rng, 100, &sampled) != eslOK) esl_fatal(msg);
2026 if ( p7_alidisplay_Serialize(sampled, &buf, &n, &nalloc) != eslOK) esl_fatal(msg);
2027
2028
2029 if ( p7_alidisplay_Deserialize(NULL, &n, deserial) != eslEINVAL) esl_fatal(msg); // Test 1: error on buf == NULL;
2030 if ( p7_alidisplay_Deserialize(buf, NULL, deserial) != eslEINVAL) esl_fatal(msg); // Test 2: error on n == NULL
2031 if ( p7_alidisplay_Deserialize(buf, &n, NULL) != eslEINVAL) esl_fatal(msg); // Test 3: error on serialized object == NULL
2032
2033 free(buf);
2034 p7_alidisplay_Destroy(deserial);
2035 p7_alidisplay_Destroy(sampled);
2036 return;
2037 }
2038
2039
2040 static void
2041 utest_Backconvert(int be_verbose, ESL_RANDOMNESS *rng, ESL_ALPHABET *abc, int ntrials, int N)
2042 {
2043 char msg[] = "utest_Backconvert failed";
2044 P7_ALIDISPLAY *ad = NULL;
2045 ESL_SQ *sq = NULL;
2046 P7_TRACE *tr = NULL;
2047 int trial;
2048
2049 for (trial = 0; trial < ntrials; trial++)
2050 {
2051 if ( p7_alidisplay_Sample(rng, N, &ad) != eslOK) esl_fatal(msg);
2052 if ( p7_alidisplay_Serialize_old(ad) != eslOK) esl_fatal(msg);
2053 if (be_verbose && p7_alidisplay_Dump(stdout, ad) != eslOK) esl_fatal(msg);
2054 if ( p7_alidisplay_Backconvert(ad, abc, &sq, &tr) != eslOK) esl_fatal(msg);
2055 if (be_verbose && p7_trace_Dump(stdout, tr, NULL, sq->dsq) != eslOK) esl_fatal(msg);
2056 if ( p7_trace_Validate(tr, abc, sq->dsq, NULL) != eslOK) esl_fatal(msg);
2057
2058 p7_alidisplay_Destroy(ad);
2059 esl_sq_Destroy(sq);
2060 p7_trace_Destroy(tr);
2061 }
2062 return;
2063 }
2064
2065
2066 #endif /*p7ALIDISPLAY_TESTDRIVE*/
2067 /*------------------- end, unit tests ---------------------------*/
2068
2069 /*****************************************************************
2070 * 6. Test driver.
2071 *****************************************************************/
2072 #ifdef p7ALIDISPLAY_TESTDRIVE
2073
2074 #include <stdlib.h>
2075 #include <string.h>
2076
2077 #include "easel.h"
2078 #include "esl_getopts.h"
2079 #include "esl_random.h"
2080
2081 #include "hmmer.h"
2082
2083 static ESL_OPTIONS options[] = {
2084 /* name type default env range togs reqs incomp help docgrp */
2085 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
2086 {"-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of random-sampled alidisplays to test", 0},
2087 {"-L", eslARG_INT, "20", NULL, NULL, NULL, NULL, NULL, "length of random-sampled alidisplays to test", 0},
2088 {"-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0},
2089 {"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show verbose commentary/output", 0},
2090 { 0,0,0,0,0,0,0,0,0,0},
2091 };
2092 static char usage[] = "[-options]";
2093 static char banner[] = "test driver for p7_alidisplay.c";
2094
2095 int
2096 main(int argc, char **argv)
2097 {
2098 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
2099 ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
2100 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
2101 int N = esl_opt_GetInteger(go, "-N");
2102 int L = esl_opt_GetInteger(go, "-L");
2103 int be_verbose = esl_opt_GetBoolean(go, "-v");
2104
2105 //utest_Serialize_old ( rng, N, L);
2106 utest_Serialize(rng, 100);
2107 utest_Backconvert(be_verbose, rng, abc, N, L);
2108 utest_serialize_error_conditions(rng);
2109 utest_deserialize_error_conditions(rng);
2110
2111 esl_alphabet_Destroy(abc);
2112 esl_randomness_Destroy(rng);
2113 esl_getopts_Destroy(go);
2114 return 0;
2115 }
2116 #endif /*p7ALIDISPLAY_TESTDRIVE*/
2117 /*------------------- end, test driver --------------------------*/
2118
2119
2120 /*****************************************************************
2121 * 7. Example.
2122 *****************************************************************/
2123 /*
2124 gcc -o p7_alidisplay_example -std=gnu99 -g -Wall -I. -L. -I../easel -L../easel -Dp7ALIDISPLAY_EXAMPLE p7_alidisplay.c -lhmmer -leasel -lm
2125 */
2126 #ifdef p7ALIDISPLAY_EXAMPLE
2127 #include <p7_config.h>
2128
2129 #include "easel.h"
2130 #include "esl_alphabet.h"
2131 #include "esl_getopts.h"
2132 #include "esl_sq.h"
2133 #include "esl_sqio.h"
2134
2135 #include "hmmer.h"
2136
2137 static ESL_OPTIONS options[] = {
2138 /* name type default env range toggles reqs incomp help docgroup*/
2139 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2140 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2141 };
2142 static char usage[] = "[-options] <hmmfile> <seqfile>";
2143 static char banner[] = "example driver for P7_ALIDISPLAY";
2144
2145 int
2146 main(int argc, char **argv)
2147 {
2148 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
2149 char *hmmfile = esl_opt_GetArg(go, 1);
2150 char *seqfile = esl_opt_GetArg(go, 2);
2151 ESL_ALPHABET *abc = NULL;
2152 P7_HMMFILE *hfp = NULL;
2153 P7_HMM *hmm = NULL;
2154 P7_BG *bg = NULL;
2155 P7_PROFILE *gm = NULL;
2156 P7_OPROFILE *om = NULL;
2157 P7_TRACE *tr1 = NULL;
2158 P7_TRACE *tr2 = NULL;
2159 ESL_SQFILE *sqfp = NULL;
2160 ESL_SQ *sq = NULL;
2161 ESL_SQ *sq2 = NULL;
2162 P7_ALIDISPLAY *ad = NULL;
2163 P7_PIPELINE *pli = NULL;
2164 P7_TOPHITS *hitlist = NULL;
2165
2166 p7_FLogsumInit();
2167
2168 /* Read a single HMM from a file */
2169 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
2170 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
2171 p7_hmmfile_Close(hfp);
2172
2173 /* Read a single sequence from a file */
2174 if (esl_sqfile_Open(seqfile, eslSQFILE_UNKNOWN, NULL, &sqfp) != eslOK) p7_Fail("Failed to open sequence file %s", seqfile);
2175 sq = esl_sq_CreateDigital(abc);
2176 if (esl_sqio_Read(sqfp, sq) != eslOK) p7_Fail("Failed to read sequence");
2177
2178 /* Configure a profile from the HMM */
2179 bg = p7_bg_Create(abc);
2180 p7_bg_SetLength(bg, 0);
2181 gm = p7_profile_Create(hmm->M, abc);
2182 p7_ProfileConfig(hmm, bg, gm, 0, p7_UNILOCAL);
2183 om = p7_oprofile_Create(gm->M, abc);
2184 p7_oprofile_Convert(gm, om);
2185
2186 /* Create a pipeline and a top hits list */
2187 pli = p7_pipeline_Create(NULL/*=default*/, hmm->M, 400, FALSE, p7_SEARCH_SEQS);
2188 hitlist = p7_tophits_Create();
2189
2190 /* Run the pipeline */
2191 p7_pli_NewSeq(pli, sq);
2192 p7_bg_SetLength(bg, sq->n);
2193 p7_oprofile_ReconfigLength(om, sq->n);
2194 p7_Pipeline(pli, om, bg, sq, NULL, hitlist);
2195
2196 if (hitlist->N == 0) { p7_Fail("target sequence doesn't hit"); }
2197
2198 if (p7_alidisplay_Backconvert(hitlist->hit[0]->dcl[0].ad, abc, &sq2, &tr2) != eslOK) p7_Fail("backconvert failed");
2199
2200 p7_trace_Dump(stdout, tr2, gm, sq2->dsq);
2201
2202 p7_tophits_Destroy(hitlist);
2203 p7_alidisplay_Destroy(ad);
2204 esl_sq_Destroy(sq);
2205 esl_sq_Destroy(sq2);
2206 p7_trace_Destroy(tr2);
2207 p7_trace_Destroy(tr1);
2208 p7_oprofile_Destroy(om);
2209 p7_profile_Destroy(gm);
2210 p7_bg_Destroy(bg);
2211 p7_hmm_Destroy(hmm);
2212 esl_alphabet_Destroy(abc);
2213 esl_getopts_Destroy(go);
2214 return 0;
2215 }
2216 #endif /*p7ALIDISPLAY_EXAMPLE*/
2217
2218