Debian Codegraph @

easel/esl_sq.c

raw symbols SCIP
1 /* esl_sq : a single biological sequence.
2 *
3 * Contents:
4 * 1. Text version of the ESL_SQ object.
5 * 2. Digitized version of the ESL_SQ object.
6 * 3. Other functions that operate on sequences.
7 * 4. Getting single sequences from MSAs.
8 * 5. Debugging, development tools
9 * 6. Internal functions.
10 * 7. Unit tests.
11 * 8. Test driver.
12 * 9. Examples.
13 */
14 #include <esl_config.h>
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 #include "easel.h"
22 #include "esl_alphabet.h"
23 #include "esl_msa.h"
24 #include "esl_random.h"
25 #include "esl_randomseq.h"
26 #include "esl_sq.h"
27 #include "esl_vectorops.h"
28
29
30 /* Shared parts of text/digital creation functions (defined in "internal functions" section) */
31 static ESL_SQ *sq_create(int do_digital);
32 static ESL_SQ *sq_create_from(const char *name, const char *desc, const char *acc);
33
34 static ESL_SQ_BLOCK *sq_createblock(int count, int do_digital);
35
36 static int sq_init(ESL_SQ *sq, int do_digital);
37
38
39 /*****************************************************************
40 *# 1. Text version of the <ESL_SQ> object.
41 *****************************************************************/
42
43 /* Function: esl_sq_Create()
44 * Synopsis: Create a new, empty <ESL_SQ>.
45 * Incept: SRE, Thu Dec 23 11:57:00 2004 [Zaragoza]
46 *
47 * Purpose: Creates an empty <ESL_SQ> sequence object, in text mode, with
48 * internal fields allocated to reasonable initial sizes.
49 *
50 * Args: (void)
51 *
52 * Returns: a pointer to the new <ESL_SQ>. Caller frees this with
53 * <esl_sq_Destroy()>.
54 *
55 * Throws: <NULL> if allocation fails.
56 */
57 ESL_SQ *
58 esl_sq_Create(void)
59 {
60 return sq_create(FALSE);
61 }
62
63 /* Function: esl_sq_CreateFrom()
64 * Synopsis: Create a new <ESL_SQ> from text information.
65 * Incept: SRE, Wed Mar 22 09:17:04 2006 [St. Louis]
66 *
67 * Purpose: Create a new <ESL_SQ> object in text mode from elemental data.
68 * This provides an interface between non-Easel code
69 * and Easel's object.
70 *
71 * Makes copies of all data. Caller is still
72 * responsible for memory of name, seq, etc.
73 *
74 * <desc>, <acc>, and <ss> are optional. They can be passed
75 * as <NULL> to leave them blank.
76 *
77 * <ss> is an optional alphabetic secondary structure
78 * annotation string. If it is provided, its length must
79 * match the length of <seq>.
80 *
81 * Args: name - name of the sequence (NUL-terminated)
82 * seq - the sequence (alphabetic; NUL-terminated)
83 * desc - optional: description line (or NULL)
84 * acc - optional: accession (or NULL)
85 * ss - optional: secondary structure annotation (or NULL)
86 *
87 * Returns: a pointer to the new object. Free with
88 * <esl_sq_Destroy()>.
89 *
90 * Throws: <NULL> on allocation failure.
91 */
92 ESL_SQ *
93 esl_sq_CreateFrom(const char *name, const char *seq, const char *desc, const char *acc, const char *ss)
94 {
95 ESL_SQ *sq = NULL;
96 int64_t n = strlen(seq);
97 int status;
98
99 if ((sq = sq_create_from(name, desc, acc)) == NULL) goto ERROR;
100 if ((status = esl_strdup(seq, n, &(sq->seq))) != eslOK) goto ERROR;
101
102 if (ss != NULL)
103 {
104 if (strlen(ss) != n) ESL_XEXCEPTION(eslEINVAL, "ss, seq lengths mismatch");
105 if ((status = esl_strdup(ss, n, &(sq->ss))) != eslOK) goto ERROR;
106 }
107 else sq->ss = NULL;
108
109 sq->n = n;
110 sq->salloc = n+1;
111
112 /* We assume we've created a complete sequence; set the coord bookkeeping accordingly. */
113 sq->start = 1;
114 sq->end = n;
115 sq->C = 0;
116 sq->W = n;
117 sq->L = n;
118
119 /* optional for extra residue markups */
120 sq->nxr = 0;
121 sq->xr_tag = NULL;
122 sq->xr = NULL;
123
124 return sq;
125
126 ERROR:
127 esl_sq_Destroy(sq);
128 return NULL;
129 }
130
131 /* Function: esl_sq_Grow()
132 * Synopsis: Assure that a <ESL_SQ> has space to add more residues.
133 * Incept: SRE, Wed Jan 10 08:26:23 2007 [Janelia]
134 *
135 * Purpose: Assure that the sequence <sq> can hold at least
136 * one more residue, whether in digital or text mode.
137 * Reallocate if necessary. Optionally returns the number
138 * of residues that can be added before the next call
139 * to <esl_sq_Grow()> in <opt_nsafe>.
140 *
141 * The terminal <NUL> or sentinel count as a residue for
142 * allocation purposes: that is, you may need to call
143 * <esl_sq_Grow()> before terminating a new sequence.
144 *
145 * Returns: <eslOK> on success.
146 *
147 * Throws: <eslEMEM> on reallocation failure. In this case, the
148 * original <sq> is untouched, and <*opt_nsafe> is returned
149 * as 0.
150 *
151 * Xref: STL11/125.
152 */
153 int
154 esl_sq_Grow(ESL_SQ *sq, int64_t *opt_nsafe)
155 {
156 void *tmp;
157 int64_t new;
158 int64_t nsafe;
159 int x; /* index for optional extra residue markups */
160 int status;
161
162 if (sq->seq != NULL) nsafe = sq->salloc - sq->n; /* text */
163 else nsafe = (sq->salloc-1) - sq->n; /* digital: -1 because 0 is a sentinel */
164
165 if (nsafe < 1)
166 { /* reallocate by doubling (shouldn't need more, but if we do, keep doubling) */
167 new = sq->salloc;
168 do { nsafe += new; new*=2; } while (nsafe < 1);
169
170 if (sq->seq != NULL) ESL_RALLOC(sq->seq, tmp, new * sizeof(char)); /* text */
171 else ESL_RALLOC(sq->dsq, tmp, new * sizeof(ESL_DSQ)); /* digital */
172 if (sq->ss != NULL) ESL_RALLOC(sq->ss, tmp, new * sizeof(char));
173
174 for (x = 0; x < sq->nxr; x++)
175 if (sq->xr[x] != NULL) ESL_RALLOC(sq->xr[x], tmp, new * sizeof(char));
176
177 sq->salloc = new;
178 }
179 if (opt_nsafe != NULL) *opt_nsafe = nsafe;
180 return eslOK;
181
182 ERROR:
183 if (opt_nsafe != NULL) *opt_nsafe = 0;
184 return status;
185 }
186
187 /* Function: esl_sq_GrowTo()
188 * Synopsis: Grows an <ESL_SQ> to hold a seq of at least <n> residues.
189 * Incept: SRE, Fri Jan 18 11:06:50 2008 [UA5233 Westchester-Dulles]
190 *
191 * Purpose: Assure that the appropriate (text or digital) sequence
192 * field in <sq> can hold up to a total of <n> residues,
193 * reallocating as needed.
194 *
195 * If reallocated, the allocation will be $\geq (n+1)$ for
196 * text mode (the +1 is for the terminal NUL byte), $\geq
197 * (n+2)$ for digital mode (+2 for sentinel bytes at each
198 * end). That is, you don't need to take these extra bytes into
199 * account in your <n>; <n> is the number of residues, not
200 * bytes.
201 *
202 * Returns: <eslOK> on success.
203 *
204 * Throws: <eslEMEM> on allocation failure.
205 *
206 * Note that n=0 is fine here, because we'll allocate either n+1 or n+2.
207 */
208 int
209 esl_sq_GrowTo(ESL_SQ *sq, int64_t n)
210 {
211 int x; /* index for optional extra residue markups */
212 int status;
213
214 if (sq->seq != NULL) /* text mode */
215 {
216 if (n+1 > sq->salloc) {
217 ESL_REALLOC(sq->seq, (n+1) * sizeof(char));
218 if (sq->ss != NULL) ESL_REALLOC(sq->ss, (n+1) * sizeof(char));
219 for (x = 0; x < sq->nxr; x++) /* optional extra residue markups */
220 if (sq->xr[x] != NULL) ESL_REALLOC(sq->xr[x], (n+1) * sizeof(char));
221 sq->salloc = n+1;
222 }
223 }
224 else /* digital mode */
225 {
226 if (n+2 > sq->salloc) {
227 ESL_REALLOC(sq->dsq, (n+2) * sizeof(ESL_DSQ));
228 if (sq->ss != NULL) ESL_REALLOC(sq->ss, (n+2) * sizeof(char));
229 for (x = 0; x < sq->nxr; x++) /* optional extra residue markups */
230 if (sq->xr[x] != NULL) ESL_REALLOC(sq->xr[x], (n+2) * sizeof(char));
231
232 sq->salloc = n+2;
233 }
234 }
235 return eslOK;
236
237 ERROR:
238 return status;
239 }
240
241
242 /* Function: esl_sq_Copy()
243 * Synopsis: Make a copy of an <ESL_SQ>.
244 * Incept: SRE, Sun Feb 24 17:59:24 2008 [UA5315 to St. Louis]
245 *
246 * Purpose: Copies a source sequence object <src> into
247 * destination sequence object <dst>.
248 *
249 * The two objects don't have to be matched as far as
250 * text/digital mode go; if mismatched, appropriate
251 * text/digital conversion will be done.
252 *
253 * The destination sequence <dst> is reallocated internally
254 * as necessary to hold a copy of <src>.
255 *
256 * Returns: <eslOK> on success.
257 *
258 * Throws: <eslEMEM> on allocation failure.
259 *
260 * Note: Note the shenanigans involved in copying ss; you have
261 * to pay attention to the ss being a 0..n-1 string in text
262 * mode versus a 1..n string in digital mode.
263 */
264 int
265 esl_sq_Copy(const ESL_SQ *src, ESL_SQ *dst)
266 {
267 int x; /* index for optional extra residue markups */
268 int status;
269
270 /* If <src> has structure annotation and <dst> does not, initialize an allocation in <dst> */
271 if (src->ss != NULL && dst->ss == NULL) ESL_ALLOC(dst->ss, sizeof(char) * dst->salloc);
272
273 /* similarly for optional extra residue markups */
274 if (src->nxr > 0) {
275 if (dst->nxr > 0) {
276 for (x = 0; x < dst->nxr; x++) {
277 if (dst->xr[x] != NULL) { free(dst->xr[x]); dst->xr[x] = NULL; }
278 if (dst->xr_tag[x] != NULL) { free(dst->xr_tag[x]); dst->xr_tag[x] = NULL; }
279 }
280 if (dst->xr != NULL) { free(dst->xr); dst->xr = NULL; }
281 if (dst->xr_tag != NULL) { free(dst->xr_tag); dst->xr_tag = NULL; }
282 }
283
284 dst->nxr = src->nxr;
285 ESL_ALLOC(dst->xr_tag, sizeof(char *) * dst->nxr);
286 ESL_ALLOC(dst->xr, sizeof(char *) * dst->nxr);
287
288 for (x = 0; x < dst->nxr; x++) {
289 ESL_ALLOC(dst->xr_tag[x], sizeof(char) * src->nalloc);
290 ESL_ALLOC(dst->xr[x], sizeof(char) * src->salloc);
291 }
292 }
293
294 if ((status = esl_sq_SetName (dst, src->name)) != eslOK) goto ERROR;
295 if ((status = esl_sq_SetSource (dst, src->source)) != eslOK) goto ERROR;
296 if ((status = esl_sq_SetAccession(dst, src->acc)) != eslOK) goto ERROR;
297 if ((status = esl_sq_SetDesc (dst, src->desc)) != eslOK) goto ERROR;
298 if ((status = esl_sq_GrowTo (dst, src->n)) != eslOK) goto ERROR;
299
300 if (src->seq != NULL && dst->seq != NULL) /* text to text */
301 {
302 strcpy(dst->seq, src->seq);
303 if (src->ss != NULL) strcpy(dst->ss, src->ss);
304 for (x = 0; x < src->nxr; x++)
305 if (src->xr[x] != NULL) strcpy(dst->xr[x], src->xr[x]);
306 }
307 else if (src->seq != NULL && dst->dsq != NULL) /* text to digital */
308 {
309 if ((status = esl_abc_Digitize(dst->abc, src->seq, dst->dsq)) != eslOK) goto ERROR;
310 if (src->ss != NULL) {
311 strcpy(dst->ss+1, src->ss);
312 dst->ss[0] = '\0';
313 for (x = 0; x < src->nxr; x++)
314 if (src->xr[x] != NULL) { strcpy(dst->xr[x]+1, src->xr[x]); dst->xr[x][0] = '\0'; }
315 }
316 }
317 else if (src->dsq != NULL && dst->seq != NULL) /* digital to text */
318 {
319 if ((status = esl_abc_Textize(src->abc, src->dsq, src->n, dst->seq)) != eslOK) goto ERROR;
320 if (src->ss != NULL) strcpy(dst->ss, src->ss+1);
321 for (x = 0; x < src->nxr; x++)
322 if (src->xr[x] != NULL) strcpy(dst->xr[x], src->xr[x]+1);
323 }
324 else /* digital to digital */
325 {
326 if (src->abc->type != dst->abc->type)
327 ESL_XEXCEPTION(eslEINCOMPAT, "seq objects involved in Copy differ in digital alphabet");
328 if ((status = esl_abc_dsqcpy(src->dsq, src->n, dst->dsq)) != eslOK) goto ERROR;
329 if (src->ss != NULL) {
330 strcpy(dst->ss+1, src->ss+1);
331 dst->ss[0] = '\0';
332 }
333 for (x = 0; x < src->nxr; x++)
334 if (src->xr[x] != NULL) { strcpy(dst->xr[x]+1, src->xr[x]+1); dst->xr[x][0] = '\0'; }
335 }
336
337
338 for (x = 0; x < src->nxr; x++)
339 if (src->xr_tag[x] != NULL) strcpy(dst->xr_tag[x], src->xr_tag[x]);
340
341 dst->n = src->n;
342 dst->start = src->start;
343 dst->end = src->end;
344 dst->C = src->C;
345 dst->W = src->W;
346 dst->L = src->L;
347 /* don't copy allocations (nalloc, etc); dst knows its own memory */
348 dst->roff = src->roff;
349 dst->doff = src->doff;
350 dst->hoff = src->hoff;
351 dst->eoff = src->eoff;
352 return eslOK;
353
354 ERROR:
355 esl_sq_Reuse(dst);
356 return status;
357 }
358
359 /* Function: esl_sq_Compare()
360 * Synopsis: Compare two sequence objects for equality.
361 * Incept: SRE, Tue May 13 09:00:41 2008 [Janelia]
362 *
363 * Purpose: Compare the contents of two sequence objects <sq1>
364 * and <sq2> for equality.
365 *
366 * Disk offsets are only compared if they are set in both
367 * <sq1> and <sq2>. Allocation sizes are not compared at
368 * all.
369 *
370 * Returns: <eslOK> if identical, <eslFAIL> if not.
371 */
372 int
373 esl_sq_Compare(ESL_SQ *sq1, ESL_SQ *sq2)
374 {
375 int x; /* index for optional extra residue markups */
376
377 /* Annotation comparison */
378 if (strcmp(sq1->name, sq2->name) != 0) return eslFAIL;
379 if (strcmp(sq1->acc, sq2->acc) != 0) return eslFAIL;
380 if (strcmp(sq1->desc, sq2->desc) != 0) return eslFAIL;
381 if (strcmp(sq1->source, sq2->source) != 0) return eslFAIL;
382 if (sq1->ss != NULL && sq2->ss != NULL) {
383 if (strcmp(sq1->ss, sq2->ss) != 0) return eslFAIL;
384 } else
385 if (sq1->ss != NULL || sq2->ss != NULL) return eslFAIL;
386 if (sq1->n != sq2->n) return eslFAIL;
387
388 /* Sequence comparison */
389 if (sq1->seq != NULL && sq2->seq != NULL) {
390 if (strcmp(sq1->seq, sq2->seq) != 0) return eslFAIL;
391 }
392 else if (sq1->dsq != NULL && sq2->dsq != NULL) {
393 if (memcmp(sq1->dsq, sq2->dsq, sizeof(ESL_DSQ) * (sq1->n+2)) != 0) return eslFAIL;
394 }
395 else return eslFAIL;
396
397 /* Coordinate comparison */
398 if (sq1->start != sq2->start) return eslFAIL;
399 if (sq1->end != sq2->end) return eslFAIL;
400 if (sq1->C != sq2->C) return eslFAIL;
401 if (sq1->W != sq2->W) return eslFAIL;
402 if (sq1->L != sq2->L) return eslFAIL;
403
404 /* Disk offset comparison */
405 if (sq1->roff != -1 && sq2->roff != -1 && sq1->roff != sq2->roff) return eslFAIL;
406 if (sq1->doff != -1 && sq2->doff != -1 && sq1->doff != sq2->doff) return eslFAIL;
407 if (sq1->hoff != -1 && sq2->hoff != -1 && sq1->hoff != sq2->hoff) return eslFAIL;
408 if (sq1->eoff != -1 && sq2->eoff != -1 && sq1->eoff != sq2->eoff) return eslFAIL;
409
410 /* optional extra residue markup comparison */
411 if (sq1->nxr != sq2->nxr) return eslFAIL;
412 for (x = 0; x < sq1->nxr; x++) {
413 if (sq1->xr_tag[x] != NULL && sq2->xr_tag[x] != NULL) {
414 if (strcmp(sq1->xr_tag[x], sq2->xr_tag[x]) != 0) return eslFAIL;
415 } else
416 if (sq1->xr_tag[x] != NULL || sq2->xr_tag[x] != NULL) return eslFAIL;
417
418 if (sq1->xr[x] != NULL && sq2->xr[x] != NULL) {
419 if (strcmp(sq1->xr[x], sq2->xr[x]) != 0) return eslFAIL;
420 } else
421 if (sq1->xr[x] != NULL || sq2->xr[x] != NULL) return eslFAIL;
422 }
423
424 /* alphabet comparison */
425 if (sq1->abc != NULL && (sq1->abc->type != sq2->abc->type)) return eslFAIL;
426 return eslOK;
427 }
428
429
430
431 /* Function: esl_sq_Reuse()
432 * Synopsis: Reinitialize an <ESL_SQ> for re-use.
433 * Incept: SRE, Thu Dec 23 12:23:51 2004 [Zaragoza]
434 *
435 * Purpose: Given a sequence object <sq> already in use;
436 * reinitialize all its data, so a new seq
437 * may be read into it. This allows sequential sequence
438 * input without a lot of wasted allocation/free cycling.
439 *
440 * Returns: <eslOK> on success.
441 */
442 int
443 esl_sq_Reuse(ESL_SQ *sq)
444 {
445 int x; /* index for optional extra residue markups */
446
447 sq->name[0] = '\0';
448 sq->acc[0] = '\0';
449 sq->desc[0] = '\0';
450 sq->tax_id = -1;
451 sq->source[0] = '\0';
452 if (sq->seq != NULL) sq->seq[0] = '\0';
453 if (sq->dsq != NULL) sq->dsq[0] = sq->dsq[1] = eslDSQ_SENTINEL;
454 if (sq->ss != NULL) {
455 if (sq->seq != NULL) sq->ss[0] = '\0';
456 else sq->ss[0] = sq->ss[1] = '\0'; /* in digital mode, ss string is 1..n; 0 is a dummy \0 byte*/
457 }
458
459 /* optional extra residue markup */
460 if (sq->nxr > 0) {
461 for (x = 0; x < sq->nxr; x++) {
462 if (sq->xr[x] != NULL) { free(sq->xr[x]); sq->xr[x] = NULL; }
463 if (sq->xr_tag[x] != NULL) { free(sq->xr_tag[x]); sq->xr_tag[x] = NULL; }
464 }
465 if (sq->xr != NULL) { free(sq->xr); sq->xr = NULL; }
466 if (sq->xr_tag != NULL) { free(sq->xr_tag); sq->xr_tag = NULL; }
467 sq->nxr = 0;
468 }
469
470 sq->n = 0;
471 sq->start = 0;
472 sq->end = 0;
473 sq->C = 0;
474 sq->W = 0;
475 sq->L = -1;
476 sq->idx = -1;
477 sq->doff = -1;
478 sq->hoff = -1;
479 sq->roff = -1;
480 sq->eoff = -1;
481 return eslOK;
482 }
483
484 /* Function: esl_sq_IsDigital()
485 * Synopsis: Return <TRUE> if <sq> is digital.
486 * Incept: SRE, Mon Mar 2 18:05:34 2009 [Casa de Gatos]
487 *
488 * Purpose: Return <TRUE> if <sq> is in digital mode,
489 * and <FALSE> if not.
490 */
491 int
492 esl_sq_IsDigital(const ESL_SQ *sq)
493 {
494 return ((sq->dsq != NULL) ? TRUE : FALSE);
495 }
496
497
498 /* Function: esl_sq_IsText()
499 * Synopsis: Return <TRUE> if <sq> is text mode.
500 * Incept: SRE, Mon Mar 2 18:06:22 2009 [Casa de Gatos]
501 *
502 * Purpose: Return <TRUE> if <sq> is in text mode,
503 * and <FALSE> if not.
504 */
505 int
506 esl_sq_IsText(const ESL_SQ *sq)
507 {
508 return ((sq->seq != NULL) ? TRUE : FALSE);
509 }
510
511
512 /* sq_free_internals()
513 * Free the insides of an <ESL_SQ> but not the shell.
514 * We need this version in a ESL_SQ_BLOCK, which allocates
515 * an array of ESL_SQ structures, not one at a time.
516 */
517 static void
518 sq_free_internals(ESL_SQ *sq)
519 {
520 if (sq)
521 {
522 int x; /* index for optional extra residue markups */
523 free(sq->name);
524 free(sq->acc);
525 free(sq->desc);
526 free(sq->seq);
527 free(sq->dsq);
528 free(sq->ss);
529 free(sq->source);
530 if (sq->nxr) {
531 for (x = 0; x < sq->nxr; x++) {
532 free(sq->xr[x]);
533 free(sq->xr_tag[x]);
534 }
535 }
536 free(sq->xr);
537 free(sq->xr_tag);
538 }
539 return;
540 }
541
542
543 /* Function: esl_sq_Destroy()
544 * Synopsis: Frees an <ESL_SQ>.
545 * Incept: SRE, Thu Dec 23 12:28:07 2004 [Zaragoza]
546 *
547 * Purpose: Free a Create()'d <sq>.
548 */
549 void
550 esl_sq_Destroy(ESL_SQ *sq)
551 {
552 if (sq)
553 {
554 sq_free_internals(sq);
555 free(sq);
556 }
557 return;
558 }
559
560
561 /* Function: esl_sq_CreateBlock()
562 * Synopsis: Create a new block of empty <ESL_SQ>.
563 *
564 * Purpose: Creates a block of empty <ESL_SQ> sequence objects.
565 *
566 * Returns: a pointer to the new <ESL_SQ_BLOCK>. Caller frees this
567 * with <esl_sq_DestroyBlock()>.
568 *
569 * Throws: <NULL> if allocation fails.
570 */
571 ESL_SQ_BLOCK *
572 esl_sq_CreateBlock(int count)
573 {
574 return sq_createblock(count, FALSE);
575 }
576
577 /* Function: esl_sq_DestroyBlock()
578 * Synopsis: Frees an <ESL_SQ_BLOCK>.
579 *
580 * Purpose: Free a Create()'d block of <sq>.
581 */
582 void
583 esl_sq_DestroyBlock(ESL_SQ_BLOCK *block)
584 {
585 int i;
586
587 if (block == NULL) return;
588
589 for (i = 0; i < block->listSize; ++i)
590 sq_free_internals(block->list + i);
591
592 free(block->list);
593 free(block);
594 return;
595 }
596
597 /* Function: esl_sq_BlockReallocSequences
598 * Synopsis: Re-allocates the internal data structures of a block's sequences back to their defaults
599 *
600 * Purpose: In some uses of ESL_SQ_BLOCK, fetching long sequences into the block causes the block to grow
601 * to unacceptable sizes. This function reallocates the internal data structures of each of a
602 * block's sequences back to their default values to shrink the block down to a reasonable size.
603 * This destroy's the sequences, so only call this function when the block's contents aren't needed any more.
604 *
605 * Returns: <eslOK> on success
606 *
607 * Throws: <eslEMEM> on allocation failure
608 */
609 int esl_sq_BlockReallocSequences(ESL_SQ_BLOCK *block){
610 int status;
611 int i;
612 for (i = 0; i < block->listSize; i++)
613 {
614 (block->list+i)->nalloc = eslSQ_NAMECHUNK;
615 (block->list+i)->aalloc = eslSQ_ACCCHUNK;
616 (block->list+i)->dalloc = eslSQ_DESCCHUNK;
617 (block->list+i)->salloc = eslSQ_SEQCHUNK;
618 (block->list+i)->srcalloc = eslSQ_NAMECHUNK;
619 ESL_REALLOC((block->list+i)->name, sizeof(char) * (block->list+i)->nalloc);
620 ESL_REALLOC((block->list+i)->acc, sizeof(char) * (block->list+i)->aalloc);
621 ESL_REALLOC((block->list+i)->desc, sizeof(char) * (block->list+i)->dalloc);
622 ESL_REALLOC((block->list+i)->source, sizeof(char) * (block->list+i)->srcalloc);
623 if ((block->list+i)->dsq!=NULL) ESL_REALLOC((block->list+i)->dsq, sizeof(ESL_DSQ) * (block->list+i)->salloc);
624 else ESL_REALLOC((block->list+i)->seq, sizeof(char) * (block->list+i)->salloc);
625 if ((block->list+i)->ss != NULL){
626 ESL_REALLOC((block->list+i)->ss, sizeof(char) * (block->list+i)->salloc);
627 }
628 }
629 return(eslOK);
630 ERROR:
631 return(status);
632 }
633
634 /* Function: esl_sq_BlockGrowTo()
635 * Synopsis: Grows a sequence block to hold at least <n> <ESL_SQ>.
636 *
637 * Purpose: Assure that the list of sequences can hold up to a total
638 * of <n> sequences, reallocating as needed.
639 *
640 * Returns: <eslOK> on success.
641 *
642 * Throws: <eslEMEM> on allocation failure.
643 *
644 */
645 int
646 esl_sq_BlockGrowTo(ESL_SQ_BLOCK *sqblock, int newsize, int do_digital, const ESL_ALPHABET *abc)
647 {
648 int status = eslOK;
649 int i;
650 if(sqblock->listSize < newsize)
651 {
652 ESL_REALLOC(sqblock->list, sizeof(ESL_SQ) * newsize);
653 sqblock->listSize = newsize;
654
655 for (i = sqblock->count; i < sqblock->listSize; ++i)
656 {
657 sqblock->list[i].abc = abc;
658 if ((status = sq_init(sqblock->list + i, do_digital)) != eslOK)
659 goto ERROR;
660 }
661 }
662 return eslOK;
663
664 ERROR:
665 return status;
666 }
667
668
669
670 /* Function: esl_sq_CreateDigitalBlock()
671 * Synopsis: Create a new block of empty <ESL_SQ> in digital mode.
672 * Incept:
673 *
674 * Purpose: Same as <esl_sq_CreateBlock()>, except the returned <sq>
675 * is configured for a digital sequence using internal
676 * alphabet <abc>, rather than a text sequence. Creates an
677 * empty digital <ESL_SQ> sequence object, with internal
678 * fields allocated to reasonable initial sizes.
679 *
680 * Returns: a pointer to the new <ESL_SQ_BLOCK>. Caller frees this with
681 * <esl_sq_DestroyBlock()>.
682 *
683 * Throws: <NULL> if an allocation fails.
684 *
685 * Xref:
686 */
687 ESL_SQ_BLOCK *
688 esl_sq_CreateDigitalBlock(int count, const ESL_ALPHABET *abc)
689 {
690 int i;
691 ESL_SQ_BLOCK *block;
692 if ((block = sq_createblock(count, TRUE)) == NULL) return NULL;
693
694 for (i = 0; i < count; ++i)
695 {
696 block->list[i].abc = abc;
697 }
698
699 return block;
700 }
701 /*--------------- end of ESL_SQ object functions ----------------*/
702
703
704
705
706 /*****************************************************************
707 *# 2. Digitized version of the <ESL_SQ> object. (Requires <alphabet>)
708 *****************************************************************/
709
710 /* Function: esl_sq_CreateDigital()
711 * Synopsis: Create a new, empty <ESL_SQ> in digital mode.
712 * Incept: SRE, Tue Jan 9 16:42:35 2007 [Janelia]
713 *
714 * Purpose: Same as <esl_sq_Create()>, except the returned <sq> is
715 * configured for a digital sequence using internal
716 * alphabet <abc>, rather than a text sequence. Creates an
717 * empty digital <ESL_SQ> sequence object, with internal
718 * fields allocated to reasonable initial sizes.
719 *
720 * Args: abc - pointer to internal alphabet
721 *
722 * Returns: a pointer to the new <ESL_SQ>. Caller frees this with
723 * <esl_sq_Destroy()>.
724 *
725 * Throws: <NULL> if an allocation fails.
726 *
727 * Xref: STL11/124
728 */
729 ESL_SQ *
730 esl_sq_CreateDigital(const ESL_ALPHABET *abc)
731 {
732 ESL_SQ *s;
733 if ((s = sq_create(TRUE)) == NULL) return NULL;
734 s->abc = abc;
735 return s;
736 }
737
738 /* Function: esl_sq_CreateDigitalFrom()
739 * Synopsis: Create a new digital <ESL_SQ> from text info.
740 * Incept: EPN, Fri Aug 24 13:38:56 2007
741 *
742 * Purpose: Create a new <ESL_SQ> object from elemental data.
743 * Same as <esl_sq_CreateFrom> except takes a digital <ESL_DSQ *dsq>
744 * instead of a text <char *seq> as the sequence to copy.
745 *
746 * Makes copies of all data. Caller is still
747 * responsible for memory of name, seq, etc.
748 *
749 * <ss> is an optional alphabetic secondary structure
750 * annotation string, <0..n-1>. If provided, its length
751 * must match the length of <seq>. (Note that although the
752 * argument <ss> is provided as a standard <0..n-1> C
753 * string, <ss> is stored internally as a <1..n> string in
754 * a digital sequence object, so that both the digital
755 * sequence and its alphabetic secondary structure
756 * annotation are indexed the same.)
757 *
758 * The object is growable; you can use <esl_sq_Reuse()>
759 * on it.
760 *
761 * Args: abc - the digital alphabet
762 * name - name of the sequence
763 * dsq - digital sequence <1..L>
764 * n - length of digitized sequence in residues (or -1 if unknown)
765 * desc - optional: description line (or NULL)
766 * acc - optional: accession (or NULL)
767 * ss - optional: secondary structure annotation (or NULL)
768 *
769 * Returns: a pointer to the new object. Free with
770 * <esl_sq_Destroy()>.
771 *
772 * Throws: <NULL> on allocation failure.
773 */
774 ESL_SQ *
775 esl_sq_CreateDigitalFrom(const ESL_ALPHABET *abc, const char *name, const ESL_DSQ *dsq, int64_t n,
776 const char *desc, const char *acc, const char *ss)
777 {
778 ESL_SQ *sq = NULL;
779 int status;
780
781 if((sq = sq_create_from(name, desc, acc)) == NULL) goto ERROR;
782 sq->n = (n == -1) ? esl_abc_dsqlen(dsq) : n;
783 if ((status = esl_abc_dsqdup(dsq, sq->n, &(sq->dsq))) != eslOK) goto ERROR;
784
785 if (ss != NULL)
786 {
787 if (strlen(ss) != sq->n) ESL_XEXCEPTION(eslEINVAL, "ss, seq lengths mismatch");
788 ESL_ALLOC(sq->ss, sizeof(char) * (sq->n+2));
789 sq->ss[0] = '\0';
790 strcpy(sq->ss+1, ss);
791 }
792
793 /* We assume we've created a complete sequence; set the coord bookkeeping accordingly. */
794 sq->start = 1;
795 sq->end = n;
796 sq->C = 0;
797 sq->W = n;
798 sq->L = n;
799
800 sq->salloc = sq->n+2;
801 sq->abc = abc;
802 return sq;
803
804 ERROR:
805 esl_sq_Destroy(sq);
806 return NULL;
807 }
808
809
810 /* Function: esl_sq_Digitize()
811 * Synopsis: Convert an <ESL_SQ> to digital mode.
812 * Incept: EPN, Mon Feb 12 11:09:06 2007
813 *
814 * Purpose: Given a sequence <sq> in text mode, convert it to
815 * digital mode, using alphabet <abc>.
816 *
817 * Internally, the <dsq> digital sequence field is filled,
818 * the <seq> text alignment field is destroyed and free'd,
819 * a copy of the alphabet pointer is kept in the sq's
820 * <abc> reference.
821 *
822 * Args: abc - digital alphabet
823 * sq - sequence to digitize
824 *
825 * Returns: <eslOK> on success.
826 * Returns <eslEINVAL> if the sequence contains invalid characters
827 * that can't be digitized. If this happens, the <sq> is returned
828 * unaltered - left in text mode, with <seq> as it was. (This is
829 * a normal error, because <sq->seq> may be user input that we
830 * haven't validated yet.)
831 *
832 * Throws: <eslEMEM> on allocation failure; in this case, state of <sq> may be
833 * wedged, and it should only be destroyed, not used.
834 */
835 int
836 esl_sq_Digitize(const ESL_ALPHABET *abc, ESL_SQ *sq)
837 {
838 int x; /* index for optional extra residue markups */
839 int status;
840
841 /* Contract checks */
842 if (sq->dsq != NULL) return eslOK;
843 if (sq->seq == NULL) ESL_EXCEPTION(eslEINVAL, "sq has no text sequence");
844
845 /* Validate before we convert, so we leave <seq> untouched if it's bad. */
846 if (esl_abc_ValidateSeq(abc, sq->seq, sq->n, NULL) != eslOK) return eslEINVAL;
847
848 /* Allocate dsq, ss properly; these are our last failure points. */
849 /* You can't just call Grow() here, because it would grow for old text mode, not new digital */
850 if (sq->salloc < sq->n+2) { /* it's possible (though unlikely) for salloc to be 1 residue too small */
851 sq->salloc = sq->n+2;
852 if (sq->ss != NULL) {
853 void *tmp;
854 ESL_RALLOC(sq->ss, tmp, sizeof(char) * sq->salloc);
855 }
856 /* optional extra residue markups follow same convenctions as ss */
857 for (x = 0; x < sq->nxr; x++)
858 if (sq->xr[x] != NULL) {
859 void *tmp;
860 ESL_RALLOC(sq->xr[x], tmp, sizeof(char) * sq->salloc);
861 }
862 }
863 ESL_ALLOC(sq->dsq, (sq->salloc) * sizeof(ESL_DSQ));
864
865 /* Now convert. */
866 if ((status = esl_abc_Digitize(abc, sq->seq, sq->dsq)) != eslOK) goto ERROR;
867 if (sq->ss != NULL) {
868 memmove(sq->ss+1, sq->ss, sq->n+1);
869 sq->ss[0] = '\0';
870 }
871 for (x = 0; x < sq->nxr; x++)
872 if (sq->xr[x] != NULL) {
873 memmove(sq->xr[x]+1, sq->xr[x], sq->n+1);
874 sq->xr[x][0] = '\0';
875 }
876
877 free(sq->seq);
878 sq->seq = NULL;
879 sq->abc = abc;
880 return eslOK;
881
882 ERROR:
883 if (sq->dsq != NULL) free(sq->dsq);
884 return status;
885 }
886
887 /* Function: esl_sq_Textize()
888 * Synopsis: Convert an <ESL_SQ> to text mode.
889 * Incept: EPN, Mon Feb 12 11:15:06 2007
890 *
891 * Purpose: Given a sequence <sq> in digital mode, convert it
892 * to text mode.
893 *
894 * Internally, the <seq> text alignment field is filled, the
895 * <dsq> digital alignment field is destroyed and free'd, the
896 * sq's <abc> digital alphabet reference is nullified.
897 *
898 * Args: sq - sequence object to convert to text
899 *
900 * Returns: <eslOK> on success.
901 *
902 * Throws: <eslEMEM> on allocation failure.
903 * Throws <eslECORRUPT> if the digital sequence contains
904 * invalid codes.
905 */
906 int
907 esl_sq_Textize(ESL_SQ *sq)
908 {
909 int x; /* index for optional extra residue markups */
910 int status;
911
912 /* Contract checks */
913 if (sq->seq != NULL) return eslOK;
914 if (sq->dsq == NULL) ESL_EXCEPTION(eslEINVAL, "sq has no digital sequence");
915 if (sq->abc == NULL) ESL_EXCEPTION(eslEINVAL, "sq has no digital alphabet");
916
917 /* Allocate. salloc is guaranteed big enough, if it was big enough for digital. */
918 ESL_ALLOC(sq->seq, sq->salloc * sizeof(char));
919
920 /* Convert. */
921 if ((status = esl_abc_Textize(sq->abc, sq->dsq, sq->n, sq->seq)) != eslOK) goto ERROR;
922 if (sq->ss != NULL)
923 memmove(sq->ss, sq->ss+1, sq->n+1); /* slide back to 0..n-1; +1 includes terminal \0 */
924 for (x = 0; x < sq->nxr; x++)
925 if (sq->xr[x] != NULL)
926 memmove(sq->xr[x], sq->xr[x]+1, sq->n+1); /* slide back to 0..n-1; +1 includes terminal \0 */
927
928 free(sq->dsq);
929 sq->dsq = NULL;
930 sq->abc = NULL; /* nullify reference (caller still owns real abc) */
931 return eslOK;
932
933 ERROR:
934 if (sq->seq != NULL) free(sq->seq);
935 return status;
936 }
937
938 /* Function: esl_sq_GuessAlphabet()
939 * Synopsis: Guess alphabet type of a single sequence.
940 *
941 * Purpose: Guess the alphabet type of biosequence <sq>, and store the
942 * guess in <*ret_type>.
943 *
944 * All 26 letters are valid in the amino alphabet (even <O>
945 * and <J> now), so the DNA alphabet is necessarily a subset.
946 * Therefore most protein sequences can be identified
947 * unambiguously (because they use letters that only occur
948 * in amino acid sequence), but DNA sequences cannot be.
949 *
950 * The sequence must contain more than 10 residues, or it
951 * is called <eslUNKNOWN>.
952 *
953 * For details on the rules used to classify a residue
954 * composition, see <esl_abc_GuessAlphabet()>. The rules
955 * are good but not perfect. We err on the conservative
956 * side, calling <eslUNKNOWN> rather than making
957 * classification errors. However, errors are possible; an
958 * example is a protein sequence <ACGTACGTACGT...>
959 * ("ala-cys-gly-thr..."), which will be called <eslDNA>,
960 * because it contains all and only DNA residues.
961 *
962 * The routine is tested on large sequence databases to
963 * make sure there are zero false positive classifications
964 * on known sequences. See <esl_abc_GuessAlphabet()> for
965 * details of these tests, and crossreferences.
966 *
967 * Returns: <eslOK> on success, and <*ret_type> is set to
968 * <eslAMINO>, <eslRNA>, or <eslDNA>.
969 *
970 * Returns <eslENOALPHABET> if unable to determine the
971 * alphabet type; in this case, <*ret_type> is set to
972 * <eslUNKNOWN>.
973 *
974 * Xref: See notes on esl_alphabet.c::esl_abc_GuessAlphabet()
975 */
976 int
977 esl_sq_GuessAlphabet(ESL_SQ *sq, int *ret_type)
978 {
979 int64_t ct[26];
980 int x;
981 int64_t i;
982 int64_t n = 0;
983
984 for (x = 0; x < 26; x++) ct[x] = 0;
985 for (i = 0; i < sq->n; i++) {
986 x = toupper(sq->seq[i]) - 'A';
987 if (x < 0 || x > 26) continue;
988 ct[x]++;
989 n++;
990 if (n > 10000) break; /* we oughta know by now! */
991 }
992 return esl_abc_GuessAlphabet(ct, ret_type);
993 }
994
995
996 /* Function: esl_sq_ConvertDegen2X()
997 * Synopsis: Convert all degenerate residues to X/N
998 * Incept: SRE, Tue Apr 20 08:52:54 2010 [Janelia]
999 *
1000 * Purpose: Convert all the degenerate residue codes in digital
1001 * sequence <sq> to the code for "unknown residue" (maximum
1002 * degeneracy); for example, X for protein, N for nucleic
1003 * acid.
1004 *
1005 * This is handy when you need to be compatible with
1006 * software that can't deal with unusual residue codes.
1007 * For example, WU-BLAST can't deal with O (pyrrolysine)
1008 * codes.
1009 *
1010 * Returns: <eslOK> on success.
1011 *
1012 * Throws: <eslEINVAL> if <sq> isn't in digital mode.
1013 * (We only know how to interpret the alphabet
1014 * in digital mode. In text mode, letters are
1015 * just letters.)
1016 */
1017 int
1018 esl_sq_ConvertDegen2X(ESL_SQ *sq)
1019 {
1020 if (! esl_sq_IsDigital(sq)) ESL_EXCEPTION(eslEINVAL, "esl_sq_ConvertDegen2X() only works on digital sequences");
1021 return esl_abc_ConvertDegen2X(sq->abc, sq->dsq);
1022 }
1023
1024 /*---------- end of digitized ESL_SQ object functions -----------*/
1025
1026
1027
1028 /*****************************************************************
1029 *# 3. Other functions that operate on sequences.
1030 *****************************************************************/
1031
1032 /* Function: esl_sq_SetName()
1033 * Synopsis: Set the name of a sequence.
1034 * Incept: SRE, Thu Jan 11 08:42:53 2007 [Janelia]
1035 *
1036 * Purpose: Set the name of the sequence <sq> to <name>, reallocating
1037 * as needed. For example, <esl_sq_SetName(sq, "random")>.
1038 *
1039 * A copy of <name> is made, so if caller had <name> allocated,
1040 * it is still responsible for freeing it.
1041 *
1042 * Returns: <eslOK> on success.
1043 *
1044 * Throws: <eslEMEM> on allocation error.
1045 *
1046 * Xref: STL11/125
1047 */
1048 int
1049 esl_sq_SetName(ESL_SQ *sq, const char *name)
1050 {
1051 int n;
1052 void *tmp;
1053 int status;
1054
1055 if (name == NULL) { sq->name[0] = '\0'; return eslOK; }
1056
1057 n = strlen(name);
1058 if (n >= sq->nalloc)
1059 {
1060 ESL_RALLOC(sq->name, tmp, sizeof(char) * (n+1));
1061 sq->nalloc = n+1;
1062 }
1063 strcpy(sq->name, name);
1064 return eslOK;
1065
1066 ERROR:
1067 return status;
1068 }
1069
1070 /* Function: esl_sq_SetAccession()
1071 * Synopsis: Set the accession field in a sequence.
1072 * Incept: SRE, Fri Jan 18 09:48:54 2008 [Westchester airport]
1073 *
1074 * Purpose: Set the accession of the sequence <sq> to <acc>, reallocating
1075 * as needed. For example, <esl_sq_SetAccession(sq, "ACC12356")>.
1076 *
1077 * A copy of <acc> is made, so if caller had <acc> allocated,
1078 * it is still responsible for freeing it.
1079 *
1080 * Returns: <eslOK> on success.
1081 *
1082 * Throws: <eslEMEM> on allocation error.
1083 *
1084 * Xref: STL11/125
1085 */
1086 int
1087 esl_sq_SetAccession(ESL_SQ *sq, const char *acc)
1088 {
1089 int n;
1090 void *tmp;
1091 int status;
1092
1093 if (acc == NULL) { sq->acc[0] = '\0'; return eslOK; }
1094
1095 n = strlen(acc);
1096 if (n >= sq->aalloc)
1097 {
1098 ESL_RALLOC(sq->acc, tmp, sizeof(char) * (n+1));
1099 sq->aalloc = n+1;
1100 }
1101 strcpy(sq->acc, acc);
1102 return eslOK;
1103
1104 ERROR:
1105 return status;
1106 }
1107
1108
1109 /* Function: esl_sq_SetDesc()
1110 * Synopsis: Set the description field in a sequence.
1111 * Incept: SRE, Fri Jan 18 09:46:14 2008 [Westchester airport]
1112 *
1113 * Purpose: Set the description of the sequence <sq> to <desc>, reallocating
1114 * as needed.
1115 * For example, <esl_sq_SetDesc(sq, "this is a random sequence")>.
1116 *
1117 * A copy of <desc> is made, so if caller had <desc> allocated,
1118 * it is still responsible for freeing it.
1119 *
1120 * Returns: <eslOK> on success.
1121 *
1122 * Throws: <eslEMEM> on allocation error.
1123 *
1124 * Xref: STL11/125
1125 */
1126 int
1127 esl_sq_SetDesc(ESL_SQ *sq, const char *desc)
1128 {
1129 int n;
1130 void *tmp;
1131 int status;
1132
1133 if (desc == NULL) { sq->desc[0] = '\0'; return eslOK; }
1134
1135 n = strlen(desc);
1136 if (n >= sq->dalloc)
1137 {
1138 ESL_RALLOC(sq->desc, tmp, sizeof(char) * (n+1));
1139 sq->dalloc = n+1;
1140 }
1141 strcpy(sq->desc, desc);
1142 return eslOK;
1143
1144 ERROR:
1145 return status;
1146 }
1147
1148 /* Function: esl_sq_SetSource()
1149 * Synopsis: Set the source name field in a sequence.
1150 * Incept: SRE, Wed May 7 16:17:56 2008 [Janelia]
1151 *
1152 * Purpose: Set the source of the sequence <sq> to <source>, reallocating
1153 * as needed. For example, <esl_sq_SetSource(sq, "X123456")>.
1154 *
1155 * A copy of <source> is made, so if caller had <source> allocated,
1156 * it is still responsible for freeing it.
1157 *
1158 * Returns: <eslOK> on success.
1159 *
1160 * Throws: <eslEMEM> on allocation error.
1161 *
1162 * Xref: STL11/125
1163 */
1164 int
1165 esl_sq_SetSource(ESL_SQ *sq, const char *source)
1166 {
1167 int n;
1168 void *tmp;
1169 int status;
1170
1171 if (source == NULL) { sq->source[0] = '\0'; return eslOK; }
1172
1173 n = strlen(source);
1174 if (n >= sq->srcalloc)
1175 {
1176 ESL_RALLOC(sq->source, tmp, sizeof(char) * (n+1));
1177 sq->srcalloc = n+1;
1178 }
1179 strcpy(sq->source, source);
1180 return eslOK;
1181
1182 ERROR:
1183 return status;
1184 }
1185
1186
1187 /* Function: esl_sq_FormatName()
1188 * Synopsis: Format a name of a sequence, printf()-style.
1189 * Incept: SRE, Fri Sep 11 10:59:01 2009 [Janelia]
1190 *
1191 * Purpose: Format the name of the sequence <sq> using
1192 * <printf()>-style format string <name> and corresponding
1193 * <printf()>-style arguments, reallocating as
1194 * needed.
1195 * For example, <esl_sq_FormatName(sq, "random%d", i)>.
1196 *
1197 * A copy of <name> is made, so if caller had <name> allocated,
1198 * it is still responsible for freeing it.
1199 *
1200 * Returns: <eslOK> on success.
1201 *
1202 * Throws: <eslEMEM> on allocation error.
1203 */
1204 int
1205 esl_sq_FormatName(ESL_SQ *sq, const char *name, ...)
1206 {
1207 va_list argp;
1208 va_list argp2;
1209 int n;
1210 void *tmp;
1211 int status;
1212
1213 if (name == NULL) { sq->name[0] = '\0'; return eslOK; }
1214
1215 va_start(argp, name);
1216 va_copy(argp2, argp);
1217 if ((n = vsnprintf(sq->name, sq->nalloc, name, argp)) >= sq->nalloc)
1218 {
1219 ESL_RALLOC(sq->name, tmp, sizeof(char) * (n+1));
1220 sq->nalloc = n+1;
1221 vsnprintf(sq->name, sq->nalloc, name, argp2);
1222 }
1223 va_end(argp);
1224 va_end(argp2);
1225 return eslOK;
1226
1227 ERROR:
1228 return status;
1229 }
1230
1231 /* Function: esl_sq_FormatAccession()
1232 * Synopsis: Format the accession field in a sequence, printf()-style.
1233 * Incept: SRE, Fri Sep 11 11:00:37 2009 [Janelia]
1234 *
1235 * Purpose: Format the accession of the sequence <sq> using <printf()>-style
1236 * format string <acc> and corresponding <printf()>-style arguments,
1237 * reallocating as needed.
1238 * For example, <esl_sq_FormatAccession(sq, "ACC%06d", i)>.
1239 *
1240 * A copy of <acc> is made, so if caller had <acc> allocated,
1241 * it is still responsible for freeing it.
1242 *
1243 * Returns: <eslOK> on success.
1244 *
1245 * Throws: <eslEMEM> on allocation error.
1246 */
1247 int
1248 esl_sq_FormatAccession(ESL_SQ *sq, const char *acc, ...)
1249 {
1250 va_list argp, argp2;
1251 int n;
1252 void *tmp;
1253 int status;
1254
1255 if (acc == NULL) { sq->acc[0] = '\0'; return eslOK; }
1256
1257 va_start(argp, acc);
1258 va_copy(argp2, argp);
1259 if ((n = vsnprintf(sq->acc, sq->aalloc, acc, argp)) >= sq->aalloc)
1260 {
1261 ESL_RALLOC(sq->acc, tmp, sizeof(char) * (n+1));
1262 sq->aalloc = n+1;
1263 vsnprintf(sq->acc, sq->aalloc, acc, argp2);
1264 }
1265 va_end(argp);
1266 va_end(argp2);
1267 return eslOK;
1268
1269 ERROR:
1270 return status;
1271 }
1272
1273
1274 /* Function: esl_sq_FormatDesc()
1275 * Synopsis: Format the description field in a sequence, printf()-style.
1276 * Incept: SRE, Fri Sep 11 11:02:11 2009 [Janelia]
1277 *
1278 * Purpose: Format the description of the sequence <sq> using <printf()>-style
1279 * format string <desc> and corresponding <printf()>-style arguments,
1280 * reallocating as needed.
1281 * For example, <esl_sq_FormatDesc(sq, "random sequence %d", i)>.
1282 *
1283 * A copy of <desc> is made, so if caller had <desc> allocated,
1284 * it is still responsible for freeing it.
1285 *
1286 * Returns: <eslOK> on success.
1287 *
1288 * Throws: <eslEMEM> on allocation error.
1289 */
1290 int
1291 esl_sq_FormatDesc(ESL_SQ *sq, const char *desc, ...)
1292 {
1293 va_list argp, argp2;
1294 int n;
1295 void *tmp;
1296 int status;
1297
1298 if (desc == NULL) { sq->desc[0] = '\0'; return eslOK; }
1299
1300 va_start(argp, desc);
1301 va_copy(argp2, argp);
1302
1303 if ((n = vsnprintf(sq->desc, sq->dalloc, desc, argp)) >= sq->dalloc)
1304 {
1305 ESL_RALLOC(sq->desc, tmp, sizeof(char) * (n+1));
1306 sq->dalloc = n+1;
1307 vsnprintf(sq->desc, sq->dalloc, desc, argp2);
1308 }
1309 va_end(argp);
1310 va_end(argp2);
1311 return eslOK;
1312
1313 ERROR:
1314 return status;
1315 }
1316
1317
1318 /* Function: esl_sq_FormatSource()
1319 * Synopsis: Format the source name field in a sequence, printf()-style.
1320 * Incept: SRE, Fri Sep 11 10:55:10 2009 [Janelia]
1321 *
1322 * Purpose: Format the source of the sequence <sq> using <printf()>-style
1323 * format string <source> and corresponding <printf()>-style arguments,
1324 * reallocating as needed.
1325 * For example, <esl_sq_FormatSource(sq, "source %d", i)>.
1326 *
1327 * A copy of <source> is made, so if caller had <source> allocated,
1328 * it is still responsible for freeing it.
1329 *
1330 * Returns: <eslOK> on success.
1331 *
1332 * Throws: <eslEMEM> on allocation error.
1333 */
1334 int
1335 esl_sq_FormatSource(ESL_SQ *sq, const char *source, ...)
1336 {
1337 va_list argp, argp2;
1338 int n;
1339 void *tmp;
1340 int status;
1341
1342 if (source == NULL) { sq->source[0] = '\0'; return eslOK; }
1343
1344 va_start(argp, source);
1345 va_copy(argp2, argp);
1346 if ((n = vsnprintf(sq->source, sq->srcalloc, source, argp)) >= sq->srcalloc)
1347 {
1348 ESL_RALLOC(sq->source, tmp, sizeof(char) * (n+1));
1349 sq->srcalloc = n+1;
1350 vsnprintf(sq->source, sq->srcalloc, source, argp2);
1351 }
1352 va_end(argp);
1353 va_end(argp2);
1354 return eslOK;
1355
1356 ERROR:
1357 return status;
1358 }
1359
1360
1361 /* Function: esl_sq_AppendDesc()
1362 * Synopsis: Append a new line to a growing multiline description.
1363 * Incept: SRE, Thu May 22 15:33:43 2008 [Janelia]
1364 *
1365 * Purpose: Append line <desc> to the description annotation line
1366 * in <sq>.
1367 *
1368 * The annotation line <sq->desc> is a single line; it may
1369 * not contain \verb+\n+ newlines. Caller is responsible
1370 * for making sure <desc> does not terminate in \verb+\n+.
1371 * If <sq->desc> already contains a description
1372 * line (presumably because we're reading from a file format
1373 * that's split the description across multiple lines),
1374 * append a space before adding this next line <desc>.
1375 *
1376 * Returns: <eslOK> on success.
1377 *
1378 * Throws: <eslEMEM> on allocation failure.
1379 */
1380 int
1381 esl_sq_AppendDesc(ESL_SQ *sq, const char *desc)
1382 {
1383 int dlen = (sq->desc == NULL ? 0 : strlen(sq->desc));
1384 int newlen = (desc == NULL ? 0 : strlen(desc));
1385 int status;
1386
1387 if (desc == NULL) return eslOK;
1388
1389 // sq->desc == NULL check below is logically unnecessary but it silences zealous static analyzers
1390 if (sq->desc == NULL || dlen + newlen + 1 >= sq->dalloc) { // +1 for appended space.
1391 ESL_REALLOC(sq->desc, sizeof(char) * (newlen+dlen+eslSQ_DESCCHUNK));
1392 sq->dalloc = newlen+dlen+eslSQ_DESCCHUNK;
1393 }
1394
1395 if (dlen > 0) { sq->desc[dlen] = ' '; dlen++; }
1396 strcpy(sq->desc + dlen, desc);
1397 return eslOK;
1398
1399 ERROR:
1400 return status;
1401 }
1402
1403
1404
1405 /* Function: esl_sq_SetCoordComplete()
1406 * Synopsis: Sets coords in a complete sequence.
1407 * Incept: SRE, Tue May 13 09:25:33 2008 [Janelia]
1408 *
1409 * Purpose: Declare that <sq> contains a complete sequence of length
1410 * <L>; set the coordinate and length information in <sq>
1411 * accordingly. This is used in building new sequence
1412 * objects.
1413 *
1414 * <sq->seq> or <sq->dsq> must contain a sequence of length
1415 * <L>.
1416 *
1417 * Returns: <eslOK> on success.
1418 */
1419 int
1420 esl_sq_SetCoordComplete(ESL_SQ *sq, int64_t L)
1421 {
1422 sq->start = 1;
1423 sq->end = L;
1424 sq->C = 0;
1425 sq->W = L;
1426 sq->L = L;
1427 sq->n = L;
1428 return eslOK;
1429 }
1430
1431
1432
1433 /* Function: esl_sq_CAddResidue()
1434 * Synopsis: Add one residue (or terminal NUL) to a text seq.
1435 * Incept: SRE, Wed Jan 10 07:58:20 2007 [Janelia]
1436 *
1437 * Purpose: Add one residue <c> onto a growing text mode sequence <sq>,
1438 * and deal with any necessary reallocation.
1439 *
1440 * The sequence in <sq> is not <NUL>-terminated. To
1441 * finish and NUL-terminate <sq>, call
1442 * <esl_sq_CAddResidue(sq, 0)>.
1443 *
1444 * Note: Not the most efficient routine, but convenient in some
1445 * routines. Parsers (where speed is at a premium) typically
1446 * use an addseq() kind of function instead.
1447 *
1448 * Returns: <eslOK> on success.
1449 *
1450 * Throws: <eslEMEM> on re-allocation failure.
1451 *
1452 * Xref: STL11/125.
1453 */
1454 int
1455 esl_sq_CAddResidue(ESL_SQ *sq, char c)
1456 {
1457 if (esl_sq_Grow(sq, NULL) != eslOK) return eslEMEM;
1458 sq->seq[sq->n] = c;
1459 if (c != '\0') sq->n++;
1460 return eslOK;
1461 }
1462
1463 /* Function: esl_sq_XAddResidue()
1464 * Synopsis: Add one residue (or terminal sentinel) to digital seq.
1465 * Incept: SRE, Wed Jan 10 08:23:23 2007 [Janelia]
1466 *
1467 * Purpose: Like <esl_sq_CAddResidue()>, but for digital mode
1468 * sequence: add a digital residue <x> onto a growing
1469 * digital sequence <sq>.
1470 *
1471 * The digital sequence in <sq> must be explicitly
1472 * terminated when you're done adding to it; call
1473 * <esl_sq_XAddResidue(sq, eslDSQ_SENTINEL)>.
1474 *
1475 * Returns: <eslOK> on success.
1476 *
1477 * Throws: <eslEMEM> on re-allocation failure.
1478 *
1479 * Xref: STL11/125.
1480 */
1481 int
1482 esl_sq_XAddResidue(ESL_SQ *sq, ESL_DSQ x)
1483 {
1484 if (esl_sq_Grow(sq, NULL) != eslOK) return eslEMEM;
1485 sq->dsq[sq->n+1] = x;
1486 if (x != eslDSQ_SENTINEL) sq->n++;
1487 return eslOK;
1488 }
1489
1490
1491 /* Function: esl_sq_ReverseComplement()
1492 * Synopsis: Reverse complement a sequence.
1493 * Incept: SRE, Thu May 15 20:52:13 2008 [Casa de Gatos]
1494 *
1495 * Purpose: Reverse complement the sequence <sq>, in place.
1496 *
1497 * If <sq> is in text mode, upper/lower case is preserved,
1498 * and the DNA alphabet is used (<Aa> is complemented to
1499 * <Tt>, not <Uu>). If a non-nucleic character is seen, it
1500 * is reverse complemented to an N, and the return status
1501 * is <eslEINVAL> (but the whole sequence is still reverse
1502 * complemented).
1503 *
1504 * If <sq> is in digital mode, the appropriate alphabet
1505 * (DNA vs. RNA) is used. If the alphabet has no defined
1506 * complement (such as amino acids), an <eslEINCOMPAT>
1507 * error is thrown, and the sequence isn't changed at all.
1508 *
1509 * Gap, nonresidues, and missing data characters, if any,
1510 * are preserved (in text mode, <._-> are treated as gaps,
1511 * <*> are nonresidues, and <~> as missing
1512 * data). Degenerate nucleic acid IUPAC characters are
1513 * complemented appropriately.
1514 *
1515 * The <start/end> coords in <sq> are swapped in the
1516 * source-tracking coordinate info. If <sq> is length 1,
1517 * we're unable to unambiguously differentiate forward
1518 * vs. reverse strand in Easel's
1519 * 1-offset/closed-interval/no-flag coord system (see
1520 * comments in esl_sq.h), so caller needs to hack some way
1521 * of remembering for itself that the sequence is reverse
1522 * complemented.
1523 *
1524 * Returns: <eslOK> on success.
1525 *
1526 * Returns <eslEINVAL> if the <sq> is in text mode, and we
1527 * see a character that doesn't belong to the IUPAC DNA/RNA
1528 * alphabet; in this case, the <sq> is still reverse
1529 * complemented using the DNA alphabet, with <N> for any
1530 * non-nucleic residues.
1531 *
1532 * Throws: <eslEINCOMPAT> if the <sq> is in digital mode, but the
1533 * digital alphabet has no defined complement.
1534 */
1535 int
1536 esl_sq_ReverseComplement(ESL_SQ *sq)
1537 {
1538 int64_t i;
1539 int x; /* index for optional extra residue markups */
1540 int status = eslOK;
1541
1542 if (sq->seq != NULL)
1543 {
1544 /* first, complement the sequence */
1545 for (i = 0; i < sq->n; i++)
1546 switch (sq->seq[i]) {
1547 case 'A': sq->seq[i] = 'T'; break;
1548 case 'C': sq->seq[i] = 'G'; break;
1549 case 'G': sq->seq[i] = 'C'; break;
1550 case 'T': sq->seq[i] = 'A'; break;
1551 case 'U': sq->seq[i] = 'A'; break;
1552 case 'R': sq->seq[i] = 'Y'; break;
1553 case 'Y': sq->seq[i] = 'R'; break;
1554 case 'M': sq->seq[i] = 'K'; break;
1555 case 'K': sq->seq[i] = 'M'; break;
1556 case 'S': sq->seq[i] = 'S'; break;
1557 case 'W': sq->seq[i] = 'W'; break;
1558 case 'H': sq->seq[i] = 'D'; break;
1559 case 'B': sq->seq[i] = 'V'; break;
1560 case 'V': sq->seq[i] = 'B'; break;
1561 case 'D': sq->seq[i] = 'H'; break;
1562 case 'N': sq->seq[i] = 'N'; break;
1563 case 'X': sq->seq[i] = 'X'; break;
1564 case 'a': sq->seq[i] = 't'; break;
1565 case 'c': sq->seq[i] = 'g'; break;
1566 case 'g': sq->seq[i] = 'c'; break;
1567 case 't': sq->seq[i] = 'a'; break;
1568 case 'u': sq->seq[i] = 'a'; break;
1569 case 'r': sq->seq[i] = 'y'; break;
1570 case 'y': sq->seq[i] = 'r'; break;
1571 case 'm': sq->seq[i] = 'k'; break;
1572 case 'k': sq->seq[i] = 'm'; break;
1573 case 's': sq->seq[i] = 's'; break;
1574 case 'w': sq->seq[i] = 'w'; break;
1575 case 'h': sq->seq[i] = 'd'; break;
1576 case 'b': sq->seq[i] = 'v'; break;
1577 case 'v': sq->seq[i] = 'b'; break;
1578 case 'd': sq->seq[i] = 'h'; break;
1579 case 'n': sq->seq[i] = 'n'; break;
1580 case 'x': sq->seq[i] = 'x'; break;
1581 case '.': sq->seq[i] = '.'; break;
1582 case '_': sq->seq[i] = '_'; break;
1583 case '-': sq->seq[i] = '-'; break;
1584 case '~': sq->seq[i] = '~'; break;
1585 case '*': sq->seq[i] = '*'; break;
1586 default: sq->seq[i] = 'N'; status = eslEINVAL; break;
1587 }
1588
1589 /* then, reverse it in place */
1590 for (i = 0; i < sq->n / 2; i++)
1591 ESL_SWAP(sq->seq[i], sq->seq[sq->n-i-1], char);
1592 }
1593 else
1594 {
1595 if ((status = esl_abc_revcomp(sq->abc, sq->dsq, sq->n)) != eslOK) goto ERROR;
1596 }
1597
1598 ESL_SWAP(sq->start, sq->end, int64_t);
1599
1600 /* revcomp invalidates any secondary structure annotation */
1601 if (sq->ss != NULL) { free(sq->ss); sq->ss = NULL; }
1602 /* revcomp invalidates any extra residue markup */
1603 if (sq->nxr > 0) {
1604 for (x = 0; x < sq->nxr; x++)
1605 if (sq->xr[x] != NULL) { free(sq->xr_tag[x]); free(sq->xr[x]); sq->xr_tag[x] = NULL; sq->xr[x] = NULL; }
1606 free(sq->xr_tag); sq->xr_tag = NULL;
1607 free(sq->xr); sq->xr = NULL;
1608 }
1609 return status;
1610
1611 ERROR:
1612 return status;
1613 }
1614
1615 /* Function: esl_sq_Checksum()
1616 * Synopsis: Calculate a 32-bit checksum for a sequence.
1617 * Incept: SRE, Tue Aug 25 14:32:17 2009 [Janelia]
1618 *
1619 * Purpose: Calculate a 32-bit checksum for <sq>.
1620 *
1621 * Only the sequence data are considered, not name or other
1622 * annotation. For text mode sequences, the checksum is
1623 * case sensitive. The checksum is also sensitive to
1624 * whether the sequence is text or digital mode; the same
1625 * sequence in will yield different checksums in digital
1626 * vs. text mode.
1627 *
1628 * Returns: <eslOK> on success; the checksum is in <*ret_checksum>.
1629 */
1630 int
1631 esl_sq_Checksum(const ESL_SQ *sq, uint32_t *ret_checksum)
1632 {
1633 uint32_t val = 0;
1634 uint64_t pos;
1635
1636 if (sq->seq != NULL)
1637 {
1638 for (pos = 0; pos < sq->n; pos++)
1639 {
1640 val += sq->seq[pos];
1641 val += (val << 10);
1642 val ^= (val >> 6);
1643 }
1644 }
1645 else
1646 {
1647 for (pos = 1; pos <= sq->n; pos++)
1648 {
1649 val += sq->dsq[pos];
1650 val += (val << 10);
1651 val ^= (val >> 6);
1652 }
1653 }
1654
1655 val += (val << 3);
1656 val ^= (val >> 11);
1657 val += (val << 15);
1658
1659 *ret_checksum = val;
1660 return eslOK;
1661 }
1662
1663
1664
1665 /* Function: esl_sq_CountResidues()
1666 * Synopsis: compute character counts
1667 *
1668 * Purpose: Given an ESL\_SQ <sq>, compute counts of all observed
1669 * residues in the range between <start> and <start>+<L>-1. Note
1670 * that a text-mode sequence starts at 0, while a digital-mode
1671 * sequence starts at 1. Will count degeneracies as partial
1672 * observations of the K canonical residues. Gaps, missing data,
1673 * and not-a-residue characters will be ignored (so $\sum_x f[x]$ is
1674 * not necessarily == L!). The array <*f> needs to be allocated for
1675 * sq->abc->K values.
1676 *
1677 * The vector is not zeroed out, allowing counts to be gathered from
1678 * a collection of ESL\_SQs.
1679 *
1680 * Returns: <eslOK> on success, <eslERANGE> when start or L are
1681 * outside the range of the sequence.
1682 */
1683 int
1684 esl_sq_CountResidues(const ESL_SQ *sq, int start, int L, float *f)
1685 {
1686 int i;
1687
1688 if (sq->seq != NULL) { /* text */
1689 if (start<0 || start+L>sq->n)
1690 return eslERANGE; //range out of sequence bounds
1691
1692 for (i=start ; i < start+L; i++) {
1693 if(! esl_abc_CIsGap(sq->abc, sq->seq[i])) // ignore gap characters
1694 esl_abc_FCount(sq->abc, f, sq->abc->inmap[(int) sq->seq[i]], 1.);
1695 }
1696 } else { /* digital sequence; 0 is a sentinel */
1697 if (start<1 || start+L>sq->n+1)
1698 return eslERANGE; //range out of sequence bounds
1699
1700 for (i=start ; i < start+L; i++) {
1701 if(! esl_abc_XIsGap(sq->abc, sq->dsq[i])) // ignore gap characters
1702 esl_abc_FCount(sq->abc, f, sq->dsq[i], 1.);
1703 }
1704 }
1705
1706 return eslOK;
1707 }
1708
1709
1710
1711 /*---------------------- end, other functions -------------------*/
1712
1713
1714
1715 /*****************************************************************
1716 *# 4. Getting single sequences from MSAs (requires <msa>)
1717 *****************************************************************/
1718
1719 /* Function: esl_sq_GetFromMSA()
1720 * Synopsis: Get a single sequence from an MSA.
1721 * Incept: SRE, Tue Apr 1 11:13:28 2008 [Janelia]
1722 *
1723 * Purpose: Retrieve sequence number <which> (<0..msa->nseq-1>) from
1724 * <msa> and store it in the <sq> that the caller allocated
1725 * and provided. This version (as opposed to
1726 * <esl_sq_FetchFromMSA()>, below) allows caller to reuse
1727 * the same <sq> container for retrieving sequences one at
1728 * a time from an MSA.
1729 *
1730 * The retrieved sequence <sq> must be in the same mode as
1731 * the source <msa>, text versus digital.
1732 *
1733 * The retrieved sequence is dealigned. For a text mode
1734 * sequence, gap characters to be removed are assumed to be
1735 * <-_.>. For a digital mode sequence, gap characters are
1736 * defined by the digital alphabet.
1737 *
1738 * The <sq->source> field is set to the name of the MSA, if
1739 * an MSA name is present.
1740 *
1741 * Returns: <eslOK> on success, and the retrieved sequence is in <sq>.
1742 * Some of the internals of <sq> may have been reallocated if
1743 * necessary.
1744 *
1745 * Returns <eslEOD> if there is no sequence number <which>.
1746 *
1747 * Throws: <eslEMEM> on allocation error;
1748 * <eslEINVAL> if <sq> is in a different text/digital mode than
1749 * <msa>.
1750 */
1751 int
1752 esl_sq_GetFromMSA(const ESL_MSA *msa, int which, ESL_SQ *sq)
1753 {
1754 char *gapchars = "-_.~"; /* hardcoded for now */
1755 char *acc = NULL;
1756 char *desc = NULL;
1757 char *ss = NULL;
1758 char **xr_tag = NULL; /* extra residue markup tags */
1759 char **xr = NULL; /* extra residue markup */
1760 int x; /* index for optional extra residue markups */
1761 int status;
1762
1763 if (which >= msa->nseq || which < 0) return eslEOD;
1764 if ( (msa->flags & eslMSA_DIGITAL) && sq->dsq == NULL) ESL_XEXCEPTION(eslEINVAL, "msa is digital, sq is not");
1765 if (!(msa->flags & eslMSA_DIGITAL) && sq->seq == NULL) ESL_XEXCEPTION(eslEINVAL, "msa is text, sq is not");
1766
1767 /* watch out for completely missing optional msa annotations;
1768 * msa->sqacc[which] could segfault if msa->sqacc itself is NULL
1769 */
1770 if (msa->sqacc != NULL) acc = msa->sqacc[which];
1771 if (msa->sqdesc != NULL) desc = msa->sqdesc[which];
1772 if (msa->ss != NULL) ss = msa->ss[which];
1773
1774 /* a markup for unparsed #=GR lines is converted to a sequence extra residue markup */
1775 if (msa->ngr)
1776 {
1777 ESL_ALLOC(xr_tag, sizeof(char *) * msa->ngr); for (x = 0; x < msa->ngr; x++) xr_tag[x] = NULL;
1778 ESL_ALLOC(xr, sizeof(char *) * msa->ngr); for (x = 0; x < msa->ngr; x++) xr[x] = NULL;
1779 }
1780
1781 sq->nxr = 0;
1782 for (x = 0; x < msa->ngr; x ++) {
1783 if (msa->gr[x][which] != NULL) {
1784 xr[sq->nxr] = msa->gr[x][which];
1785 if (msa->gr_tag[x] != NULL) xr_tag[sq->nxr] = msa->gr_tag[x]; else { status = eslEINVAL; goto ERROR; }
1786 sq->nxr ++;
1787 }
1788 }
1789 if (sq->nxr > 0) {
1790 ESL_ALLOC(sq->xr_tag, sizeof(char *) * sq->nxr); for (x = 0; x < sq->nxr; x ++) sq->xr_tag[x] = NULL;
1791 ESL_ALLOC(sq->xr, sizeof(char *) * sq->nxr); for (x = 0; x < sq->nxr; x ++) sq->xr[x] = NULL;
1792 }
1793
1794 if ((status = esl_sq_SetName (sq, msa->sqname[which])) != eslOK) goto ERROR;
1795 if ((status = esl_sq_SetAccession(sq, acc)) != eslOK) goto ERROR;
1796 if ((status = esl_sq_SetDesc (sq, desc)) != eslOK) goto ERROR;
1797 if ((status = esl_sq_SetSource (sq, msa->name)) != eslOK) goto ERROR;
1798 if ((status = esl_sq_GrowTo (sq, msa->alen)) != eslOK) goto ERROR; /* can't be more than alen residues */
1799
1800 if (! (msa->flags & eslMSA_DIGITAL)) /* text mode to text mode */
1801 {
1802 strcpy(sq->seq, msa->aseq[which]);
1803 if (ss != NULL) {
1804 if (sq->ss == NULL) esl_strdup(ss, -1, &(sq->ss));
1805 else strcpy(sq->ss, ss);
1806 esl_strdealign(sq->ss, sq->seq, gapchars, NULL);
1807 }
1808 for (x = 0; x < sq->nxr; x++) {
1809 esl_strdup(xr[x], -1, &(sq->xr[x]));
1810 esl_strdup(xr_tag[x], -1, &(sq->xr_tag[x]));
1811 esl_strdealign(sq->xr[x], sq->seq, gapchars, NULL);
1812 }
1813 esl_strdealign(sq->seq, sq->seq, gapchars, &(sq->n)); /* sq->n gets set as side effect */
1814 }
1815 else
1816 {
1817 esl_abc_dsqcpy(msa->ax[which], msa->alen, sq->dsq);
1818 if (ss != NULL) {
1819 if (sq->ss == NULL) { /* even in digital mode, msa->ss is [0.alen-1] */
1820 ESL_ALLOC(sq->ss, sizeof(char) * (strlen(ss)+2));
1821 sq->ss[0] = '\0';
1822 strcpy(sq->ss+1, ss);
1823 }
1824 else { strcpy(sq->ss+1, ss); sq->ss[0] = '\0'; }
1825 esl_abc_CDealign(sq->abc, sq->ss+1, sq->dsq, NULL);
1826 }
1827 for (x = 0; x < sq->nxr; x ++) { /* even in digital mode, msa->gr are [0.alen-1] */
1828 ESL_ALLOC(sq->xr[x], sizeof(char) * (strlen(xr[x])+2));
1829 sq->xr[x][0] = '\0';
1830 strcpy(sq->xr[x]+1, xr[x]);
1831 esl_abc_CDealign(sq->abc, sq->xr[x]+1, sq->dsq, NULL);
1832 esl_strdup(xr_tag[x], -1, &(sq->xr_tag[x]));
1833 }
1834 esl_abc_XDealign(sq->abc, sq->dsq, sq->dsq, &(sq->n)); /* sq->n gets set as side effect */
1835 }
1836
1837 /* This is a complete sequence; set bookkeeping accordingly */
1838 sq->start = 1;
1839 sq->end = sq->n;
1840 sq->C = 0;
1841 sq->W = sq->n;
1842 sq->L = sq->n;
1843
1844 sq->roff = -1;
1845 sq->doff = -1;
1846 sq->hoff = -1;
1847 sq->eoff = -1;
1848
1849 if (xr_tag) free(xr_tag);
1850 if (xr) free(xr);
1851 return eslOK;
1852
1853 ERROR:
1854 if (xr_tag) free(xr_tag);
1855 if (xr) free(xr);
1856 return status;
1857 }
1858
1859
1860 /* Function: esl_sq_FetchFromMSA()
1861 * Synopsis: Fetch a single sequence from an MSA.
1862 * Incept: SRE, Sun Mar 30 13:39:06 2008 [Janelia]
1863 *
1864 * Purpose: Retrieve sequence number <which> (<0..msa->nseq-1>) from <msa>, in a newly
1865 * allocated sequence object; return a pointer to this object
1866 * in <ret_sq>.
1867 *
1868 * The retrieved sequence is in the same mode as the source
1869 * <msa>, text versus digital.
1870 *
1871 * The retrieved sequence is dealigned. For a text mode
1872 * sequence, gap characters to be removed are assumed to be
1873 * <-_.~>. For a digital mode sequence, gap characters are
1874 * defined by the digital alphabet.
1875 *
1876 * Returns: <eslOK> on success, and a pointer to the newly fetched
1877 * sequence is in <*ret_sq>, which caller is responsible for freeing.
1878 *
1879 * Returns <eslEOD> if there is no sequence number <which>.
1880 *
1881 * Throws: <eslEMEM> on allocation error.
1882 */
1883 int
1884 esl_sq_FetchFromMSA(const ESL_MSA *msa, int which, ESL_SQ **ret_sq)
1885 {
1886 ESL_SQ *sq = NULL;
1887 char *acc = NULL;
1888 char *desc = NULL;
1889 char *ss = NULL;
1890 char *gapchars = "-_.~"; /* hardcoded for now; only affects text mode, not digital */
1891 char **xr_tag = NULL; /* extra residue markup tags */
1892 char **xr = NULL; /* extra residue markup */
1893 int nxr = 0; /* number of extra residue markups */
1894 int x; /* index for optional extra residue markups */
1895 int status;
1896
1897 if (which >= msa->nseq || which < 0) return eslEOD;
1898
1899 /* watch out for optional msa annotations being totally NULL */
1900 if (msa->sqacc != NULL) acc = msa->sqacc[which];
1901 if (msa->sqdesc != NULL) desc = msa->sqdesc[which];
1902 if (msa->ss != NULL) ss = msa->ss[which];
1903
1904 /* a markup for unparsed #=GR lines is converted to a sequence extra residue markup */
1905 if (msa->ngr > 0) {
1906 ESL_ALLOC(xr_tag, sizeof(char *) * msa->ngr);
1907 ESL_ALLOC(xr, sizeof(char *) * msa->ngr);
1908 for (x = 0; x < msa->ngr; x ++) {
1909 xr_tag[x] = NULL;
1910 xr[x] = NULL;
1911 if (msa->gr[x][which] != NULL) {
1912 xr[nxr] = msa->gr[x][which];
1913 if (msa->gr_tag[x] != NULL) xr_tag[nxr] = msa->gr_tag[x]; else goto ERROR;
1914 nxr ++;
1915 }
1916 }
1917 }
1918
1919 if (! (msa->flags & eslMSA_DIGITAL)) /* text mode MSA to text mode sequence */
1920 {
1921 if ((sq = esl_sq_CreateFrom(msa->sqname[which], msa->aseq[which], desc, acc, ss)) == NULL) goto ERROR;
1922 if (sq->ss != NULL) esl_strdealign(sq->ss, sq->seq, gapchars, NULL);
1923
1924 if (nxr > 0) {
1925 sq->nxr = nxr;
1926 ESL_ALLOC(sq->xr_tag, sizeof(char *) * sq->nxr); for (x = 0; x < sq->nxr; x ++) sq->xr_tag[x] = NULL;
1927 ESL_ALLOC(sq->xr, sizeof(char *) * sq->nxr); for (x = 0; x < sq->nxr; x ++) sq->xr[x] = NULL;
1928 for (x = 0; x < sq->nxr; x ++) {
1929 if (xr[x] != NULL) {
1930 if (sq->xr[x] == NULL) esl_strdup(xr[x], sq->n, &(sq->xr[x]));
1931 else strcpy(sq->xr[x], xr[x]);
1932 esl_strdealign(sq->xr[x], sq->seq, gapchars, NULL);
1933 }
1934 if (xr_tag[x] != NULL) {
1935 if (sq->xr_tag[x] == NULL) esl_strdup(xr_tag[x], -1, &(sq->xr_tag[x]));
1936 else strcpy(sq->xr_tag[x], xr_tag[x]);
1937 }
1938 }
1939 }
1940 esl_strdealign(sq->seq, sq->seq, gapchars, &(sq->n));
1941 }
1942 else /* digital mode MSA to digital mode sequence */
1943 {
1944 if ((sq = esl_sq_CreateDigitalFrom(msa->abc, msa->sqname[which], msa->ax[which], msa->alen, desc, acc, ss)) == NULL) goto ERROR;
1945 if (sq->ss != NULL) esl_abc_CDealign(sq->abc, sq->ss+1, sq->dsq, NULL);
1946 if (nxr > 0) {
1947 sq->nxr = nxr;
1948 ESL_ALLOC(sq->xr_tag, sizeof(char *) * sq->nxr); for (x = 0; x < sq->nxr; x ++) sq->xr_tag[x] = NULL;
1949 ESL_ALLOC(sq->xr, sizeof(char *) * sq->nxr); for (x = 0; x < sq->nxr; x ++) sq->xr[x] = NULL;
1950 for (x = 0; x < sq->nxr; x ++) {
1951 if (xr[x] != NULL) {
1952 if (sq->xr[x] == NULL)
1953 {
1954 ESL_ALLOC(sq->xr[x], sizeof(char) * (sq->n+2));
1955 sq->xr[x][0] = '\0';
1956 strcpy(sq->xr[x]+1, xr[x]);
1957 }
1958 else
1959 {
1960 strcpy(sq->xr[x]+1, xr[x]);
1961 sq->xr[x][0] = '\0';
1962 }
1963 esl_abc_CDealign(sq->abc, sq->xr[x]+1, sq->dsq, NULL);
1964 }
1965 if (xr_tag[x] != NULL) {
1966 if (sq->xr_tag[x] == NULL) esl_strdup(xr_tag[x], -1, &(sq->xr_tag[x]));
1967 else strcpy(sq->xr_tag[x], xr_tag[x]);
1968 }
1969 }
1970 }
1971 esl_abc_XDealign(sq->abc, sq->dsq, sq->dsq, &(sq->n));
1972 }
1973
1974 if ((status = esl_sq_SetSource(sq, msa->name)) != eslOK) goto ERROR;
1975
1976 sq->start = 1;
1977 sq->end = sq->n;
1978 sq->L = sq->n;
1979 sq->C = 0;
1980 sq->W = sq->n;
1981 *ret_sq = sq;
1982
1983 if (msa->ngr > 0) {
1984 free(xr_tag); free(xr);
1985 }
1986 return eslOK;
1987
1988 ERROR:
1989 if (msa->ngr > 0) {
1990 if (xr_tag) free(xr_tag);
1991 if (xr) free(xr);
1992 }
1993 esl_sq_Destroy(sq);
1994 *ret_sq = NULL;
1995 return eslEMEM;
1996 }
1997 /*---------------- end, sequences from MSAs --------------------*/
1998
1999
2000
2001 /*****************************************************************
2002 *# 5. Debugging/development tools
2003 *****************************************************************/
2004
2005 /* Function: esl_sq_Validate()
2006 * Synopsis: Validate an ESL_SQ object
2007 * Incept: SRE, Tue 12 Jan 2021 [H9/136]
2008 */
2009 int
2010 esl_sq_Validate(ESL_SQ *sq, char *errmsg)
2011 {
2012 if (sq->name == NULL) ESL_FAIL(eslFAIL, errmsg, "seq name can't be NULL");
2013 if (sq->acc == NULL) ESL_FAIL(eslFAIL, errmsg, "optional accession must be '\0' empty string if missing, not NULL");
2014 if (sq->desc == NULL) ESL_FAIL(eslFAIL, errmsg, "optional desc line must be '\0' empty string if missing, not NULL");
2015 if (sq->tax_id < -1) ESL_FAIL(eslFAIL, errmsg, "optional tax_id must be -1 or an NCBI taxid");
2016
2017 if (sq->dsq != NULL)
2018 { // digital seq
2019 if (sq->seq != NULL) ESL_FAIL(eslFAIL, errmsg, "seq must be digital or text, not both");
2020 if (esl_abc_dsqlen(sq->dsq) != sq->n) ESL_FAIL(eslFAIL, errmsg, "digital seq length doesn't agree with sq->n");
2021 if (sq->ss ) {
2022 if (sq->ss[0] != '\0') ESL_FAIL(eslFAIL, errmsg, "ss annotation for a digital seq is 1..n with \0 at 0,n+1");
2023 if (strlen(sq->ss+1) != sq->n) ESL_FAIL(eslFAIL, errmsg, "ss annotation length (for digital seq) doesn't agree with sq->n");
2024 }
2025 if (!sq->abc) ESL_FAIL(eslFAIL, errmsg, "digital seq needs a non-NULL alphabet");
2026 }
2027 else
2028 { // text seq
2029 if (sq->dsq != NULL) ESL_FAIL(eslFAIL, errmsg, "seq must be digital or text, not both");
2030 if (strlen(sq->seq) != sq->n) ESL_FAIL(eslFAIL, errmsg, "text seq length doesn't agree with sq->n");
2031 if (sq->ss && strlen(sq->ss) != sq->n) ESL_FAIL(eslFAIL, errmsg, "ss annotation length (for text seq) doesn't agree with sq->n");
2032 if (sq->abc) ESL_FAIL(eslFAIL, errmsg, "text seq mustn't have a digital alphabet");
2033 }
2034
2035 // TK TK TK
2036 // ... should check source-tracking info
2037 // ... and memory allocation
2038 // ... and disk offset bookkeeping
2039 // ... and optional residue markup
2040 return eslOK;
2041 }
2042
2043
2044
2045 /* Function: esl_sq_Sample()
2046 * Synopsis: Sample a random, ugly <ESL_SQ> for test purposes.
2047 * Incept: SRE, Tue Feb 23 08:32:54 2016 [H1/83]
2048 *
2049 * Purpose: Sample a random sequence with random annotation, with a
2050 * sequence length of <0..maxL> (note 0 is included!),
2051 * using the random number generator <rng>. Return the
2052 * newly allocated sequence in <*ret_sq>. Caller is
2053 * responsible for free'ing it with <esl_sq_Destroy()>.
2054 *
2055 * If <abc> is <NULL>, a text mode sequence is sampled. If
2056 * a digital alphabet <abc> is provided, a digital mode
2057 * sequence is sampled.
2058 *
2059 * This routine is intended for producing test cases. The
2060 * sequence object contains randomized contents. If you
2061 * want to synthesize random sequences (as opposed to
2062 * sampling an entirely synthetic <ESL_SQ> object), see
2063 * the <randomseq> module.
2064 *
2065 * Args: rng : random number generator
2066 * abc : digital alphabet; or <NULL> for text mode
2067 * maxL : sequence length sampled is 0..maxL
2068 * ret_sq : RESULT: new <ESL_SQ>
2069 *
2070 * Returns: <eslOK> on success
2071 *
2072 * Throws: <eslEMEM> on allocation failure.
2073 */
2074 int
2075 esl_sq_Sample(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc, int maxL, ESL_SQ **ret_sq)
2076 {
2077 ESL_SQ *sq = *ret_sq; // caller may or may not provide an existing <sq>
2078 int maxn = eslSQ_NAMECHUNK *2; // by being bigger than the initial alloc size, we will exercise realloc
2079 int maxa = eslSQ_ACCCHUNK *2;
2080 int maxd = eslSQ_DESCCHUNK *2;
2081 char *buf = NULL;
2082 int n;
2083 int status;
2084
2085 n = ESL_MAX(maxn, ESL_MAX(maxa, maxd));
2086 ESL_ALLOC(buf, sizeof(char) * (n+1));
2087
2088 if (! sq)
2089 {
2090 if (abc == NULL) { if (( sq = esl_sq_Create()) == NULL) { status = eslEMEM; goto ERROR; } }
2091 else { if (( sq = esl_sq_CreateDigital(abc)) == NULL) { status = eslEMEM; goto ERROR; } }
2092 }
2093
2094 /* Name */
2095 do {
2096 n = 1 + esl_rnd_Roll(rng, maxn); // 1..maxn
2097 esl_rsq_Sample(rng, eslRSQ_SAMPLE_GRAPH, n, &buf); // one word: no space
2098 } while (ispunct(buf[0])); // #, // are bad things for names to start with in Stockholm
2099 esl_sq_SetName(sq, buf);
2100
2101 /* Optional accession */
2102 if (esl_rnd_Roll(rng, 2)) // 50% chance of an accession
2103 {
2104 n = 1 + esl_rnd_Roll(rng, maxa); // 1..maxa
2105 esl_rsq_Sample(rng, eslRSQ_SAMPLE_GRAPH, n, &buf); // one word: no space
2106 esl_sq_SetAccession(sq, buf);
2107 }
2108
2109 /* Optional description */
2110 if (esl_rnd_Roll(rng, 2)) // 50% chance of a description
2111 {
2112 do {
2113 n = 1 + esl_rnd_Roll(rng, maxd); // 1..maxa
2114 esl_rsq_Sample(rng, eslRSQ_SAMPLE_PRINT, n, &buf); // include spaces in descriptions...
2115 } while (isspace(buf[0])); // ... just not as the first char.
2116 esl_sq_SetDesc(sq, buf);
2117 }
2118
2119 /* Optional taxid. */
2120 if (esl_rnd_Roll(rng, 2)) // 50% chance of taxid
2121 {
2122 sq->tax_id = 1 + esl_rnd_Roll(rng, 2147483647); // 1..2^31-1
2123 }
2124
2125 /* Sequence, in text or digital mode */
2126 n = esl_rnd_Roll(rng, maxL+1); //0..maxL; 0 len seqs happen
2127 esl_sq_GrowTo(sq, n);
2128 if (abc == NULL) esl_rsq_Sample(rng, eslRSQ_SAMPLE_ALPHA, n, &(sq->seq));
2129 else esl_rsq_SampleDirty(rng, abc, NULL, n, sq->dsq); // "dirty" = with ambig residues
2130 esl_sq_SetCoordComplete(sq, n);
2131
2132 free(buf);
2133 *ret_sq = sq;
2134 return eslOK;
2135
2136 ERROR:
2137 if (buf) free(buf);
2138 if (!(*ret_sq) && sq) esl_sq_Destroy(sq);
2139 return status;
2140 }
2141
2142
2143 /*****************************************************************
2144 * 6. Internal functions
2145 *****************************************************************/
2146
2147 /* Create and CreateDigital() are almost identical, so
2148 * their shared guts are here:
2149 */
2150 static ESL_SQ *
2151 sq_create(int do_digital)
2152 {
2153 ESL_SQ *sq = NULL;
2154 int status;
2155
2156 ESL_ALLOC(sq, sizeof(ESL_SQ));
2157
2158 if (sq_init(sq, do_digital) != eslOK) goto ERROR;
2159
2160 return sq;
2161
2162 ERROR:
2163 esl_sq_Destroy(sq);
2164 return NULL;
2165 }
2166
2167 /* Create an <ESL_SQ_BLOCK> object and its list of <ESL_SQ> objects */
2168 static ESL_SQ_BLOCK *
2169 sq_createblock(int count, int do_digital)
2170 {
2171 int i = 0;
2172
2173 ESL_SQ_BLOCK *block = NULL;
2174 int status = eslOK;
2175
2176 ESL_ALLOC(block, sizeof(ESL_SQ_BLOCK));
2177
2178 block->count = 0;
2179 block->first_seqidx = -1;
2180 block->list = NULL;
2181 block->complete = TRUE;
2182
2183 ESL_ALLOC(block->list, sizeof(ESL_SQ) * count);
2184 block->listSize = count;
2185
2186 for (i = 0; i < count; ++i)
2187 {
2188 if (sq_init(block->list + i, do_digital) != eslOK) goto ERROR;
2189 }
2190
2191 return block;
2192
2193 ERROR:
2194 esl_sq_DestroyBlock(block);
2195 return NULL;
2196 }
2197
2198 /* Initialize <ESL_SQ> object */
2199 static int
2200 sq_init(ESL_SQ *sq, int do_digital)
2201 {
2202 int status;
2203
2204 sq->name = NULL;
2205 sq->acc = NULL;
2206 sq->desc = NULL;
2207 sq->source = NULL;
2208 sq->tax_id = -1;
2209 sq->seq = NULL;
2210 sq->dsq = NULL;
2211 sq->ss = NULL; /* Note that ss is optional - it will only be allocated if needed */
2212 /* n, coord bookkeeping, and strings are all set below by a call to Reuse() */
2213
2214 sq->nalloc = eslSQ_NAMECHUNK;
2215 sq->aalloc = eslSQ_ACCCHUNK;
2216 sq->dalloc = eslSQ_DESCCHUNK;
2217 sq->salloc = eslSQ_SEQCHUNK;
2218 sq->srcalloc = eslSQ_NAMECHUNK;
2219
2220 ESL_DASSERT1(( sq->salloc >= 4 )); // dsqdata makes this assumption when it packs in place.
2221
2222 /* optional for extra residue markups */
2223 sq->nxr = 0;
2224 sq->xr_tag = NULL;
2225 sq->xr = NULL;
2226
2227 ESL_ALLOC(sq->name, sizeof(char) * sq->nalloc);
2228 ESL_ALLOC(sq->acc, sizeof(char) * sq->aalloc);
2229 ESL_ALLOC(sq->desc, sizeof(char) * sq->dalloc);
2230 ESL_ALLOC(sq->source, sizeof(char) * sq->srcalloc);
2231 if (do_digital)
2232 {
2233 ESL_ALLOC(sq->dsq, sizeof(ESL_DSQ) * sq->salloc);
2234 }
2235 else
2236 {
2237 ESL_ALLOC(sq->seq, sizeof(char) * sq->salloc);
2238 sq->abc = NULL;
2239 }
2240
2241 esl_sq_Reuse(sq); /* initialization of sq->n, offsets, and strings */
2242 return eslOK;
2243
2244 ERROR:
2245 return eslEMEM;
2246 }
2247
2248 /* CreateFrom and CreateDigitalFrom() are almost identical, so
2249 * their shared guts are here:
2250 */
2251 static ESL_SQ *
2252 sq_create_from(const char *name, const char *desc, const char *acc)
2253 {
2254 ESL_SQ *sq = NULL;
2255 int64_t n;
2256 int status;
2257
2258 ESL_ALLOC(sq, sizeof(ESL_SQ));
2259 sq->name = NULL;
2260 sq->acc = NULL;
2261 sq->desc = NULL;
2262 sq->seq = NULL;
2263 sq->dsq = NULL;
2264 sq->ss = NULL;
2265 sq->source = NULL;
2266
2267 /* optional for extra residue markups */
2268 sq->nxr = 0;
2269 sq->xr_tag = NULL;
2270 sq->xr = NULL;
2271
2272 /* coord bookkeeping has to be set by the parent caller,
2273 * because that's where we know the seq length <n>. We don't
2274 * know it here.
2275 */
2276 sq->doff = -1;
2277 sq->hoff = -1;
2278 sq->roff = -1;
2279 sq->eoff = -1;
2280
2281 if (name != NULL)
2282 {
2283 n = strlen(name)+1;
2284 ESL_ALLOC(sq->name, sizeof(char) * n);
2285 strcpy(sq->name, name);
2286 sq->nalloc = n;
2287 }
2288 else
2289 {
2290 sq->nalloc = eslSQ_NAMECHUNK;
2291 ESL_ALLOC(sq->name, sizeof(char) * sq->nalloc);
2292 sq->name[0] = '\0';
2293 }
2294
2295 if (desc != NULL)
2296 {
2297 n = strlen(desc)+1;
2298 ESL_ALLOC(sq->desc, sizeof(char) * n);
2299 strcpy(sq->desc, desc);
2300 sq->dalloc = n;
2301 }
2302 else
2303 {
2304 sq->dalloc = eslSQ_DESCCHUNK;
2305 ESL_ALLOC(sq->desc, sizeof(char) * sq->dalloc);
2306 sq->desc[0] = '\0';
2307 }
2308
2309 if (acc != NULL)
2310 {
2311 n = strlen(acc)+1;
2312 ESL_ALLOC(sq->acc, sizeof(char) * n);
2313 strcpy(sq->acc, acc);
2314 sq->aalloc = n;
2315 }
2316 else
2317 {
2318 sq->aalloc = eslSQ_ACCCHUNK;
2319 ESL_ALLOC(sq->acc, sizeof(char) * sq->aalloc);
2320 sq->acc[0] = '\0';
2321 }
2322
2323 /* no source name */
2324 sq->srcalloc = eslSQ_NAMECHUNK;
2325 ESL_ALLOC(sq->source, sizeof(char) * sq->srcalloc);
2326 sq->source[0] = '\0';
2327
2328
2329 return sq;
2330
2331 ERROR:
2332 esl_sq_Destroy(sq);
2333 return NULL;
2334 }
2335
2336 /*----------------- end, internal functions ---------------------*/
2337
2338
2339 /*****************************************************************
2340 * 7. Unit tests.
2341 *****************************************************************/
2342 #ifdef eslSQ_TESTDRIVE
2343 #include "esl_random.h"
2344
2345 static void
2346 utest_Create()
2347 {
2348 char *msg = "failure in utest_Create()";
2349 char *name = "seqname";
2350 char *acc = "XX00001";
2351 char *desc = "test sequence description";
2352 char *seq = "ACDEFGHIKLMNPQRSTVWY";
2353 char *ss = "xxxx....xxxx....xxxx";
2354 int64_t n = strlen(seq);
2355 ESL_SQ *sq1 = esl_sq_CreateFrom(name, seq, desc, acc, ss);
2356 ESL_SQ *sq2 = esl_sq_Create();
2357
2358 if (sq1 == NULL || sq2 == NULL) esl_fatal(msg);
2359
2360 if (esl_sq_GrowTo(sq2, n) != eslOK) esl_fatal(msg);
2361 if (esl_sq_FormatName (sq2, "%s%s", "seq", "name") != eslOK) esl_fatal(msg);
2362 if (esl_sq_FormatAccession(sq2, "%s%05d", "XX", 1) != eslOK) esl_fatal(msg);
2363 if (esl_sq_FormatDesc (sq2, "%s %s %s", "test","sequence","description") != eslOK) esl_fatal(msg);
2364 if (esl_sq_FormatSource (sq2, "%s", "source-unknown") != eslOK) esl_fatal(msg);
2365 if (esl_strdup(ss, -1, &(sq2->ss)) != eslOK) esl_fatal(msg);
2366 strcpy(sq2->seq, seq);
2367 sq2->n = n;
2368
2369 if (strcmp(sq1->name, sq2->name) != 0) esl_fatal(msg);
2370 if (strcmp(sq1->acc, sq2->acc) != 0) esl_fatal(msg);
2371 if (strcmp(sq1->desc, sq2->desc) != 0) esl_fatal(msg);
2372 if (strcmp(sq1->seq, sq2->seq) != 0) esl_fatal(msg);
2373 if (strcmp(sq1->ss, sq2->ss) != 0) esl_fatal(msg);
2374 if (sq1->n != sq2->n) esl_fatal(msg);
2375
2376 esl_sq_Destroy(sq1);
2377 esl_sq_Destroy(sq2);
2378 }
2379
2380 /* This tests the Set() functions. */
2381 static void
2382 utest_Set(ESL_RANDOMNESS *r)
2383 {
2384 char *msg = "sqio Set unit test failure";
2385 ESL_SQ *sq = esl_sq_Create();
2386 int ntrials = 8;
2387 int maxn = eslSQ_NAMECHUNK*2;
2388 int maxa = eslSQ_ACCCHUNK*2;
2389 int maxd = eslSQ_DESCCHUNK*2;
2390 int n = ESL_MAX( maxn, ESL_MAX(maxa, maxd));
2391 char *buf = malloc(sizeof(char) * (n+1));
2392 int64_t L;
2393 int i;
2394
2395 for (i = 0; i < ntrials; i++)
2396 {
2397 L = esl_rnd_Roll(r, maxn) + 1;
2398 memset(buf, 'x', L);
2399 buf[L] = '\0';
2400 if (esl_sq_SetName(sq, buf) != eslOK) esl_fatal(msg);
2401 }
2402 for (i = 0; i < ntrials; i++)
2403 {
2404 L = esl_rnd_Roll(r, maxa) + 1;
2405 memset(buf, 'x', L);
2406 buf[L] = '\0';
2407 if (esl_sq_SetAccession(sq, buf) != eslOK) esl_fatal(msg);
2408 }
2409 for (i = 0; i < ntrials; i++)
2410 {
2411 L = esl_rnd_Roll(r, maxd) + 1;
2412 memset(buf, 'x', L);
2413 buf[L] = '\0';
2414 if (esl_sq_SetDesc(sq, buf) != eslOK) esl_fatal(msg);
2415 }
2416 free(buf);
2417 esl_sq_Destroy(sq);
2418 }
2419
2420
2421 /* This tests the Format() functions -
2422 * in particular, the way they use vsnprintf().
2423 */
2424 static void
2425 utest_Format(ESL_RANDOMNESS *r)
2426 {
2427 char *msg = "esl_sq_Format*() unit test failure";
2428 ESL_SQ *sq = esl_sq_Create();
2429 int ntrials = 128;
2430 int maxn = eslSQ_NAMECHUNK*2;
2431 int maxa = eslSQ_ACCCHUNK*2;
2432 int maxd = eslSQ_DESCCHUNK*2;
2433 int n = ESL_MAX( maxn, ESL_MAX(maxa, maxd));
2434 char *buf = malloc(sizeof(char) * (n+1));
2435 int64_t L;
2436 int i;
2437
2438 for (i = 0; i < ntrials; i++)
2439 {
2440 L = esl_rnd_Roll(r, maxn) + 1;
2441 memset(buf, 'x', L);
2442 buf[L] = '\0';
2443 if (esl_sq_FormatName(sq, "%s%d", buf, i) != eslOK) esl_fatal(msg);
2444 }
2445 for (i = 0; i < ntrials; i++)
2446 {
2447 L = esl_rnd_Roll(r, maxa) + 1;
2448 memset(buf, 'x', L);
2449 buf[L] = '\0';
2450 if (esl_sq_FormatAccession(sq, "%s%d", buf, i) != eslOK) esl_fatal(msg);
2451 }
2452 for (i = 0; i < ntrials; i++)
2453 {
2454 L = esl_rnd_Roll(r, maxd) + 1;
2455 memset(buf, 'x', L);
2456 buf[L] = '\0';
2457 if (esl_sq_FormatDesc(sq, "%s%d", buf, i) != eslOK) esl_fatal(msg);
2458 }
2459 free(buf);
2460 esl_sq_Destroy(sq);
2461 }
2462
2463
2464 static void
2465 utest_CreateDigital()
2466 {
2467 char *msg = "failure in utest_CreateDigital()";
2468 ESL_ALPHABET *abc = esl_alphabet_Create(eslRNA);
2469 char *name = "seqname";
2470 char *acc = "XX00001";
2471 char *desc = "test sequence description";
2472 char *seq = "GGGAAATTTCCC";
2473 char *ss = "<<<......>>>";
2474 ESL_DSQ *dsq = NULL;
2475 int64_t n = strlen(seq);
2476 ESL_SQ *sq1 = NULL;
2477 ESL_SQ *sq2 = NULL;
2478 ESL_SQ *sq3 = NULL;
2479
2480 if (esl_abc_CreateDsq(abc, seq, &dsq) != eslOK) esl_fatal(msg);
2481 if ((sq1 = esl_sq_CreateDigitalFrom(abc, name, dsq, n, desc, acc, ss)) == NULL) esl_fatal(msg);
2482
2483 if ((sq2 = esl_sq_CreateDigital(abc)) == NULL) esl_fatal(msg);
2484 if (esl_sq_GrowTo(sq2, n) != eslOK) esl_fatal(msg);
2485 if (esl_sq_FormatName (sq2, "%s%s", "seq", "name") != eslOK) esl_fatal(msg);
2486 if (esl_sq_FormatAccession(sq2, "%s%05d", "XX", 1) != eslOK) esl_fatal(msg);
2487 if (esl_sq_FormatDesc (sq2, "%s %s %s", "test","sequence","description") != eslOK) esl_fatal(msg);
2488 if (esl_sq_FormatSource (sq2, "%s", "source-unknown") != eslOK) esl_fatal(msg);
2489 if ((sq2->ss = malloc(sizeof(char) * (n+2))) == NULL) esl_fatal(msg);
2490 strcpy(sq2->ss+1, ss); sq2->ss[0] = '\0';
2491 if (esl_abc_Digitize(abc, seq, sq2->dsq) != eslOK) esl_fatal(msg);
2492 sq2->n = n;
2493
2494 if ((sq3 = esl_sq_Create()) == NULL) esl_fatal(msg);
2495 if (esl_sq_Copy(sq1, sq3) != eslOK) esl_fatal(msg); /* sq3 is now text mode */
2496 if (esl_sq_Textize(sq2) != eslOK) esl_fatal(msg); /* sq2 is now text mode */
2497
2498 if (strcmp(sq3->name, sq2->name) != 0) esl_fatal(msg); /* sq2,sq3 should be identical text mode */
2499 if (strcmp(sq3->acc, sq2->acc) != 0) esl_fatal(msg);
2500 if (strcmp(sq3->desc, sq2->desc) != 0) esl_fatal(msg);
2501 if (strcmp(sq3->seq, sq2->seq) != 0) esl_fatal(msg);
2502 if (strcmp(sq3->ss, sq2->ss) != 0) esl_fatal(msg);
2503 if (sq3->n != sq2->n) esl_fatal(msg);
2504
2505 /* sq3 back to digital; should = sq1 again */
2506 if (esl_sq_Digitize(abc, sq3) != eslOK) esl_fatal(msg);
2507 if (memcmp(sq3->dsq, sq1->dsq, sizeof(ESL_DSQ) * (sq3->n)) != 0) esl_fatal(msg);
2508 if (sq3->n != sq1->n) esl_fatal(msg);
2509
2510 free(dsq);
2511 esl_alphabet_Destroy(abc);
2512 esl_sq_Destroy(sq1);
2513 esl_sq_Destroy(sq2);
2514 esl_sq_Destroy(sq3);
2515 }
2516
2517 /* write_msa_with_seqmarkups()
2518 * Write a good MSA with sequence markups to a tmpfile in Stockholm format.
2519 */
2520 static void
2521 write_msa_with_seqmarkups(FILE *ofp)
2522 {
2523 fprintf(ofp, "# STOCKHOLM 1.0\n");
2524 fprintf(ofp, "seq1 ACDE.FGHKLMNPQRSTVWY\n");
2525 fprintf(ofp, "#=GR seq1 tWS ..<..<........>...>.\n");
2526 fprintf(ofp, "seq2 ACDEGFGHKLMNPQRSTVWY\n");
2527 fprintf(ofp, "seq3 ACDEGFGHKLMNPQRSTVWY\n");
2528 fprintf(ofp, "#=GR seq3 SS ...<<..>>...........\n");
2529 fprintf(ofp, "seq4 ACDE.FGHKLMNPQRSTVWY\n");
2530 fprintf(ofp, "seq5 ACDEGFGHKLMNPQRSTVWY\n");
2531 fprintf(ofp, "seq6 ACDE.FGHKLMNPQRSTVWY\n");
2532 fprintf(ofp, "#=GR seq6 SS ........<<<..>>>....\n");
2533 fprintf(ofp, "#=GR seq6 tWH .<...A...>....a.....\n");
2534 fprintf(ofp, "#=GR seq6 csS .<.................>\n");
2535 fprintf(ofp, "//\n");
2536 return;
2537 }
2538
2539 /* test optional extra residue markups in a sq */
2540 #include "esl_msafile.h"
2541 #include "esl_msafile_stockholm.h"
2542 static void
2543 utest_ExtraResMarkups()
2544 {
2545 char msg[] = "sq extra residue markups test driver failed";
2546 char tmpfile[32];
2547 FILE *ofp = NULL;
2548 ESL_ALPHABET *abc = NULL;
2549 ESL_MSAFILE *afp1 = NULL;
2550 ESL_MSAFILE *afp2 = NULL;
2551 ESL_MSA *msa1 = NULL;
2552 ESL_MSA *msa2 = NULL;
2553 ESL_SQ *sq = NULL;
2554 ESL_SQ *sq1 = NULL;
2555 ESL_SQ *sq2 = NULL;
2556
2557 strcpy(tmpfile, "esltmpXXXXXX");
2558 if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
2559 write_msa_with_seqmarkups(ofp);
2560 fclose(ofp);
2561
2562 /* Digital msa to digital sq */
2563 esl_msafile_Open(&abc, tmpfile, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp1);
2564 esl_msafile_stockholm_Read(afp1, &msa1);
2565
2566 sq = esl_sq_CreateDigital(abc); if (esl_sq_GetFromMSA(msa1, 0, sq) != eslOK) esl_fatal(msg);
2567 esl_sq_Reuse(sq); if (esl_sq_GetFromMSA(msa1, 1, sq) != eslOK) esl_fatal(msg);
2568 esl_sq_Reuse(sq); if (esl_sq_GetFromMSA(msa1, 2, sq) != eslOK) esl_fatal(msg);
2569 esl_sq_Reuse(sq); if (esl_sq_GetFromMSA(msa1, 5, sq) != eslOK) esl_fatal(msg);
2570
2571 /* test of sq_Copy */
2572 sq1 = esl_sq_Create();
2573 sq2 = esl_sq_CreateDigital(abc);
2574 esl_sq_Copy(sq, sq1);
2575 esl_sq_Copy(sq, sq2);
2576 esl_sq_Destroy(sq1);
2577 esl_sq_Destroy(sq2);
2578
2579 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa1, 0, &sq) != eslOK) esl_fatal(msg);
2580 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa1, 1, &sq) != eslOK) esl_fatal(msg);
2581 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa1, 2, &sq) != eslOK) esl_fatal(msg);
2582 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa1, 5, &sq) != eslOK) esl_fatal(msg);
2583 esl_sq_Destroy(sq);
2584
2585 /* Text msa to text sq */
2586 esl_msafile_Open(NULL, tmpfile, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2);
2587 esl_msafile_stockholm_Read(afp2, &msa2);
2588
2589 sq = esl_sq_Create(); if (esl_sq_GetFromMSA(msa2, 0, sq) != eslOK) esl_fatal(msg);
2590 esl_sq_Reuse(sq); if (esl_sq_GetFromMSA(msa2, 1, sq) != eslOK) esl_fatal(msg);
2591 esl_sq_Reuse(sq); if (esl_sq_GetFromMSA(msa2, 2, sq) != eslOK) esl_fatal(msg);
2592 esl_sq_Reuse(sq); if (esl_sq_GetFromMSA(msa2, 5, sq) != eslOK) esl_fatal(msg);
2593
2594 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa2, 0, &sq) != eslOK) esl_fatal(msg);
2595 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa2, 1, &sq) != eslOK) esl_fatal(msg);
2596 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa2, 2, &sq) != eslOK) esl_fatal(msg);
2597 esl_sq_Destroy(sq); if (esl_sq_FetchFromMSA(msa2, 5, &sq) != eslOK) esl_fatal(msg);
2598
2599 /* test of sq_Copy */
2600 sq1 = esl_sq_Create();
2601 sq2 = esl_sq_CreateDigital(abc);
2602 esl_sq_Copy(sq, sq1);
2603 esl_sq_Copy(sq, sq2);
2604 esl_sq_Destroy(sq1);
2605 esl_sq_Destroy(sq2);
2606 esl_sq_Destroy(sq);
2607
2608 /* clean up */
2609 remove(tmpfile);
2610 esl_msafile_Close(afp1);
2611 esl_msafile_Close(afp2);
2612 esl_msa_Destroy(msa1);
2613 esl_msa_Destroy(msa2);
2614 esl_alphabet_Destroy(abc);
2615 }
2616
2617 /* test counting residues in a sq */
2618 static void
2619 utest_CountResidues()
2620 {
2621 char *msg = "failure in utest_CountResidues()";
2622 char *name = "seqname";
2623 char *acc = "XX00001";
2624 char *desc = "test sequence description";
2625 char *seq = "GGGAATTCCC";
2626 char *ss = "xxxx...xxx";
2627 ESL_SQ *sq = NULL;
2628 float *cnts = NULL;
2629 int status;
2630 ESL_ALPHABET *abc = esl_alphabet_Create(eslDNA);
2631
2632 ESL_ALLOC(cnts, abc->Kp * sizeof(float));
2633
2634
2635 if ((sq = esl_sq_CreateFrom(name, seq, desc, acc, ss)) == NULL) esl_fatal(msg);
2636 sq->abc = abc;
2637 esl_vec_FSet (cnts, abc->K, 0);
2638 esl_sq_CountResidues(sq, 0, sq->n, cnts);
2639 if (cnts[0] != 2) esl_fatal(msg);
2640 if (cnts[1] != 3) esl_fatal(msg);
2641 if (cnts[2] != 3) esl_fatal(msg);
2642 if (cnts[3] != 2) esl_fatal(msg);
2643
2644
2645 esl_sq_Digitize(abc, sq);
2646 esl_vec_FSet (cnts, abc->K, 0);
2647 esl_sq_CountResidues(sq, 1, sq->n, cnts);
2648 if (cnts[0] != 2) esl_fatal(msg);
2649 if (cnts[1] != 3) esl_fatal(msg);
2650 if (cnts[2] != 3) esl_fatal(msg);
2651 if (cnts[3] != 2) esl_fatal(msg);
2652
2653 free(cnts);
2654 esl_sq_Destroy(sq);
2655 esl_alphabet_Destroy(abc);
2656 return;
2657
2658
2659 ERROR:
2660 if (cnts != NULL) free(cnts);
2661 if (sq != NULL) esl_sq_Destroy(sq);
2662 if (abc != NULL) esl_alphabet_Destroy(abc);
2663
2664 esl_fatal(msg);
2665 return;
2666 }
2667
2668
2669 #endif /* eslSQ_TESTDRIVE*/
2670 /*--------------------- end, unit tests -------------------------*/
2671
2672 /*****************************************************************
2673 * 8. Test driver.
2674 *****************************************************************/
2675 #ifdef eslSQ_TESTDRIVE
2676 /* gcc -g -Wall -o esl_sq_utest -I. -L. -DeslSQ_TESTDRIVE esl_sq.c -leasel -lm
2677 * ./esl_sq_utest
2678 */
2679 #include <esl_config.h>
2680
2681 #include <stdio.h>
2682 #include <math.h>
2683
2684 #include "easel.h"
2685 #include "esl_alphabet.h"
2686 #include "esl_getopts.h"
2687 #include "esl_msa.h"
2688 #include "esl_msafile.h"
2689 #include "esl_msafile_stockholm.h"
2690 #include "esl_random.h"
2691 #include "esl_sq.h"
2692
2693 static ESL_OPTIONS options[] = {
2694 /* name type default env range toggles reqs incomp help docgroup*/
2695 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2696 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
2697 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2698 };
2699 static char usage[] = "[-options]";
2700 static char banner[] = "test driver for sq module";
2701
2702 int
2703 main(int argc, char **argv)
2704 {
2705 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
2706 ESL_RANDOMNESS *r = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
2707
2708 utest_Create();
2709 utest_Set(r);
2710 utest_Format(r);
2711 utest_CountResidues();
2712
2713 utest_CreateDigital();
2714
2715 utest_ExtraResMarkups();
2716
2717 esl_randomness_Destroy(r);
2718 esl_getopts_Destroy(go);
2719 return 0;
2720 }
2721 #endif /* eslSQ_TESTDRIVE*/
2722 /*-------------------- end, test driver -------------------------*/
2723
2724
2725
2726
2727
2728 /*****************************************************************
2729 * 9. Examples.
2730 *****************************************************************/
2731
2732 #ifdef eslSQ_EXAMPLE
2733 /*::cexcerpt::sq_example::begin::*/
2734 /* compile: gcc -g -Wall -I. -o example -DeslSQ_EXAMPLE esl_sq.c easel.c
2735 * run: ./example
2736 */
2737 #include <stdio.h>
2738 #include <string.h>
2739 #include "easel.h"
2740 #include "esl_sq.h"
2741
2742 int main(void)
2743 {
2744 ESL_SQ *sq1, *sq2;
2745 char *name = "seq1";
2746 char *acc = "XX00001";
2747 char *desc = "This is a test.";
2748 char *testseq = "GGGAAATTTCCC";
2749 char *ss = "<<<......>>>";
2750 int n = strlen(testseq);
2751
2752 /* Creating an ESL_SQ from text info: */
2753 sq1 = esl_sq_CreateFrom(name, testseq, desc, acc, ss); /* desc, acc, or ss may be NULL */
2754
2755 /* Building up a ESL_SQ yourself: */
2756 sq2 = esl_sq_Create();
2757 esl_sq_FormatName (sq2, "seq%d", 1);
2758 esl_sq_FormatAccession(sq2, "XX%05d", 1);
2759 esl_sq_FormatDesc (sq2, "This %s a test", "is");
2760 esl_sq_GrowTo (sq2, n);
2761 strcpy(sq2->seq, testseq);
2762 esl_strdup(ss, -1, &(sq2->ss));
2763 sq2->n = n;
2764
2765 /* Accessing the information */
2766 printf("Name: %s\n", sq2->name);
2767 printf("Accession: %s\n", sq2->acc);
2768 printf("Description: %s\n", sq2->desc);
2769 printf("Sequence: %s\n", sq2->seq);
2770 printf("Structure: %s\n", sq2->ss);
2771 printf("Residue 3: %c\n", sq2->seq[2]); /* note 0..n-1 coords */
2772 printf("Structure 3: %c\n", sq2->ss[2]); /* same for ss */
2773
2774 /* Freeing the structures */
2775 esl_sq_Destroy(sq1);
2776 esl_sq_Destroy(sq2);
2777 return 0;
2778 }
2779 /*::cexcerpt::sq_example::end::*/
2780 #endif /*eslSQ_EXAMPLE*/
2781
2782
2783 #ifdef eslSQ_EXAMPLE2
2784 /*::cexcerpt::sq_example2::begin::*/
2785 #include <stdio.h>
2786 #include <string.h>
2787 #include "easel.h"
2788 #include "esl_alphabet.h"
2789 #include "esl_sq.h"
2790
2791 int main(void)
2792 {
2793 ESL_ALPHABET *abc;
2794 ESL_SQ *sq1, *sq2;
2795 char *name = "seq1";
2796 char *acc = "XX00001";
2797 char *desc = "This is a test.";
2798 char *testseq = "GGGAAATTTCCC";
2799 ESL_DSQ *dsq = NULL;
2800 char *ss = "<<<......>>>";
2801 int n = strlen(testseq);
2802 int i;
2803
2804 /* Creating a digital alphabet: */
2805 abc = esl_alphabet_Create(eslRNA);
2806
2807 /* Creating a digital ESL_SQ from text info: */
2808 esl_abc_CreateDsq(abc, testseq, &dsq);
2809 sq1 = esl_sq_CreateDigitalFrom(abc, name, dsq, n, desc, acc, ss);
2810 free(dsq);
2811
2812 /* Building up a digital ESL_SQ yourself: */
2813 sq2 = esl_sq_CreateDigital(abc);
2814 esl_sq_FormatName (sq2, "seq%d", 1);
2815 esl_sq_FormatAccession(sq2, "XX%05d", 1);
2816 esl_sq_FormatDesc (sq2, "This %s a test", "is");
2817 esl_sq_GrowTo (sq2, n);
2818 esl_abc_Digitize(abc, testseq, sq2->dsq);
2819 sq2->n = n;
2820
2821 /* a "digital" ss isn't so pretty, but just so you know: */
2822 sq2->ss = malloc(sizeof(char) * (n+2));
2823 sq2->ss[0] = '\0';
2824 strcpy(sq2->ss+1, ss);
2825
2826 /* Accessing the information */
2827 printf("Name: %s\n", sq2->name);
2828 printf("Accession: %s\n", sq2->acc);
2829 printf("Description: %s\n", sq2->desc);
2830 printf("Sequence: ");
2831 for (i = 1; i <= n; i++)
2832 putchar(abc->sym[sq2->dsq[i]]);
2833 putchar('\n');
2834 printf("Structure: %s\n", sq2->ss+1); /* +1, ss is 1..n like dsq */
2835 printf("Residue 3: %c\n", abc->sym[sq2->dsq[3]]);
2836 printf("Structure 3: %c\n", sq2->ss[3]); /* note 1..n coord system */
2837
2838 /* Freeing the structures */
2839 esl_sq_Destroy(sq1);
2840 esl_sq_Destroy(sq2);
2841 return 0;
2842 }
2843 /*::cexcerpt::sq_example2::end::*/
2844 #endif /*eslSQ_EXAMPLE2*/
2845 /*------------------ end, example drivers ------------------------*/
2846