Debian Codegraph @

easel/esl_msafile_clustal.c

raw symbols SCIP
1 /* i/o of multiple sequence alignment files in Clustal-like formats
2 *
3 * Contents:
4 * 1. API for reading/writing Clustal and Clustal-like formats
5 * 2. Internal routines for Clustal formats.
6 * 3. Unit tests.
7 * 4. Test driver.
8 * 5. Example.
9 *
10 * This module is responsible for i/o of both eslMSAFILE_CLUSTAL and
11 * eslMSAFILE_CLUSTALLIKE alignment formats.
12 */
13 #include <esl_config.h>
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19
20 #include "easel.h"
21 #include "esl_alphabet.h"
22 #include "esl_mem.h"
23 #include "esl_msa.h"
24 #include "esl_msafile.h"
25
26 #include "esl_msafile_clustal.h"
27
28 static int make_text_consensus_line(const ESL_MSA *msa, char **ret_consline);
29 static int make_digital_consensus_line(const ESL_MSA *msa, char **ret_consline);
30
31 /*****************************************************************
32 *# 1. API for reading/writing Clustal and Clustal-like formats
33 *****************************************************************/
34
35 /* Function: esl_msafile_clustal_SetInmap()
36 * Synopsis: Configure input map for CLUSTAL, CLUSTALLIKE formats.
37 *
38 * Purpose: Set the <afp->inmap> for Clustal-like formats.
39 *
40 * Text mode accepts any <isgraph()> character.
41 * Digital mode enforces the usual Easel alphabets.
42 */
43 int
44 esl_msafile_clustal_SetInmap(ESL_MSAFILE *afp)
45 {
46 int sym;
47
48 if (afp->abc)
49 {
50 for (sym = 0; sym < 128; sym++)
51 afp->inmap[sym] = afp->abc->inmap[sym];
52 afp->inmap[0] = esl_abc_XGetUnknown(afp->abc);
53 }
54
55 if (! afp->abc)
56 {
57 for (sym = 1; sym < 128; sym++)
58 afp->inmap[sym] = (isgraph(sym) ? sym : eslDSQ_ILLEGAL);
59 afp->inmap[0] = '?';
60 }
61 return eslOK;
62 }
63
64
65 /* Function: esl_msafile_clustal_GuessAlphabet()
66 * Synopsis: Guess the alphabet of an open Clustal MSA input.
67 *
68 * Purpose: Guess the alpbabet of the sequences in open
69 * Clustal format MSA file <afp>.
70 *
71 * On a normal return, <*ret_type> is set to <eslDNA>,
72 * <eslRNA>, or <eslAMINO>, and <afp> is reset to its
73 * original position.
74 *
75 * Args: afp - open Clustal format MSA file
76 * ret_type - RETURN: <eslDNA>, <eslRNA>, or <eslAMINO>
77 *
78 * Returns: <eslOK> on success.
79 * <eslENOALPHABET> if alphabet type can't be determined.
80 * In either case, <afp> is rewound to the position it
81 * started at.
82 *
83 * Throws: <eslEMEM> on allocation error.
84 * <eslESYS> on failures of fread() or other system calls
85 */
86 int
87 esl_msafile_clustal_GuessAlphabet(ESL_MSAFILE *afp, int *ret_type)
88 {
89 int alphatype = eslUNKNOWN;
90 esl_pos_t anchor = -1;
91 int threshold[3] = { 500, 5000, 50000 }; /* we check after 500, 5000, 50000 residues; else we go to EOF */
92 int nsteps = 3;
93 int step = 0;
94 int nres = 0;
95 int x;
96 int64_t ct[26];
97 char *p, *tok;
98 esl_pos_t n, toklen, pos;
99 int status;
100
101 for (x = 0; x < 26; x++) ct[x] = 0;
102
103 anchor = esl_buffer_GetOffset(afp->bf);
104 if ((status = esl_buffer_SetAnchor(afp->bf, anchor)) != eslOK) { status = eslEINCONCEIVABLE; goto ERROR; } /* [eslINVAL] can't happen here */
105
106 /* Ignore the first nonblank line, which says "CLUSTAL W (1.83) multiple sequence alignment" or some such */
107 while ( (status = esl_buffer_GetLine(afp->bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
108 if (status == eslEOF) ESL_XFAIL(eslENOALPHABET, afp->errmsg, "can't determine alphabet: no alignment data found");
109 else if (status != eslOK) goto ERROR;
110
111 while ( (status = esl_buffer_GetLine(afp->bf, &p, &n)) == eslOK)
112 {
113 if ((status = esl_memtok(&p, &n, " \t", &tok, &toklen)) != eslOK) continue; /* ignore blank lines */
114 /* p now points to the rest of the sequence line, after a name */
115
116 /* count characters into ct[] array */
117 for (pos = 0; pos < n; pos++)
118 if (isalpha(p[pos])) {
119 x = toupper(p[pos]) - 'A';
120 ct[x]++;
121 nres++;
122 }
123 /* note that GuessAlphabet() is robust against the optional coord lines
124 * and the annotation lines -- it only counts ascii characters.
125 */
126
127 /* try to stop early, checking after 500, 5000, and 50000 residues: */
128 if (step < nsteps && nres > threshold[step]) {
129 if ((status = esl_abc_GuessAlphabet(ct, &alphatype)) == eslOK) goto DONE; /* (eslENOALPHABET) */
130 step++;
131 }
132 }
133 if (status != eslEOF) goto ERROR; /* [eslEMEM,eslESYS,eslEINCONCEIVABLE] */
134 status = esl_abc_GuessAlphabet(ct, &alphatype); /* (eslENOALPHABET) */
135
136 DONE:
137 esl_buffer_SetOffset(afp->bf, anchor); /* Rewind to where we were. */
138 esl_buffer_RaiseAnchor(afp->bf, anchor);
139 *ret_type = alphatype;
140 return status;
141
142 ERROR:
143 if (anchor != -1) {
144 esl_buffer_SetOffset(afp->bf, anchor);
145 esl_buffer_RaiseAnchor(afp->bf, anchor);
146 }
147 *ret_type = eslUNKNOWN;
148 return status;
149 }
150
151
152 /* Function: esl_msafile_clustal_Read()
153 * Synopsis: Read in a CLUSTAL or CLUSTALLIKE alignment.
154 *
155 * Purpose: Read an MSA from an open <ESL_MSAFILE> <afp>, parsing
156 * for Clustal or Clustal-like format, starting from the
157 * current point. (<afp->format> is expected to be
158 * <eslMSAFILE_CLUSTAL> or <eslMSAFILE_CLUSTALLIKE>.) Create a
159 * new multiple alignment, and return a ptr to that
160 * alignment in <*ret_msa>. Caller is responsible for
161 * free'ing this <ESL_MSA>.
162 *
163 * Args: afp - open <ESL_MSAFILE>
164 * ret_msa - RETURN: newly parsed <ESL_MSA>
165 *
166 * Returns: <eslOK> on success.
167 *
168 * <eslEOF> if no (more) alignment data are found in
169 * <afp>, and <afp> is returned at EOF.
170 *
171 * <eslEFORMAT> on a parse error. <*ret_msa> is set to
172 * <NULL>. <afp> contains information sufficient for
173 * constructing useful diagnostic output:
174 * | <afp->errmsg> | user-directed error message |
175 * | <afp->linenumber> | line # where error was detected |
176 * | <afp->line> | offending line (not NUL-term) |
177 * | <afp->n> | length of offending line |
178 * | <afp->bf->filename> | name of the file |
179 * and <afp> is poised at the start of the following line,
180 * so (in principle) the caller could try to resume
181 * parsing.
182 *
183 * Throws: <eslEMEM> - an allocation failed.
184 * <eslESYS> - a system call such as fread() failed
185 * <eslEINCONCEIVABLE> - "impossible" corruption
186 */
187 int
188 esl_msafile_clustal_Read(ESL_MSAFILE *afp, ESL_MSA **ret_msa)
189 {
190 ESL_MSA *msa = NULL;
191 char *p = NULL;
192 esl_pos_t n = 0;
193 char *tok = NULL;
194 esl_pos_t ntok = 0;
195 int nblocks = 0;
196 int idx = 0;
197 int nseq = 0;
198 int64_t alen = 0;
199 int64_t cur_alen;
200 esl_pos_t pos;
201 esl_pos_t name_start, name_len;
202 esl_pos_t seq_start, seq_len;
203 esl_pos_t block_seq_start, block_seq_len;
204 int status;
205
206 ESL_DASSERT1( (afp->format == eslMSAFILE_CLUSTAL || afp->format == eslMSAFILE_CLUSTALLIKE) );
207
208 afp->errmsg[0] = '\0';
209
210 if (afp->abc && (msa = esl_msa_CreateDigital(afp->abc, 16, -1)) == NULL) { status = eslEMEM; goto ERROR; }
211 if (! afp->abc && (msa = esl_msa_Create( 16, -1)) == NULL) { status = eslEMEM; goto ERROR; }
212
213 /* skip leading blank lines in file */
214 while ( (status = esl_msafile_GetLine(afp, &p, &n)) == eslOK && esl_memspn(afp->line, afp->n, " \t") == afp->n) ;
215 if (status != eslOK) goto ERROR; /* includes normal EOF */
216
217 /* That first line says something like:
218 * "CLUSTAL W (1.83) multiple sequence alignment"
219 * "CLUSTAL format alignment by MAFFT FFT-NS-i (v7.309)"
220 * "MUSCLE (3.7) multiple sequence alignment"
221 */
222 if (esl_memtok(&p, &n, " \t", &tok, &ntok) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "missing CLUSTAL header");
223 if (afp->format == eslMSAFILE_CLUSTAL && ! esl_memstrpfx(tok, ntok, "CLUSTAL")) ESL_XFAIL(eslEFORMAT, afp->errmsg, "missing CLUSTAL header");
224 if (! esl_memstrcontains(p, n, "alignment")) ESL_XFAIL(eslEFORMAT, afp->errmsg, "missing CLUSTAL header");
225
226 /* skip blank lines again */
227 do {
228 status = esl_msafile_GetLine(afp, &p, &n);
229 if (status == eslEOF) ESL_XFAIL(eslEFORMAT, afp->errmsg, "no alignment data following header");
230 else if (status != eslOK) goto ERROR;
231 } while (esl_memspn(afp->line, afp->n, " \t") == afp->n); /* idiom for "blank line" */
232
233 /* Read the file a line at a time. */
234 do { /* afp->line, afp->n is now the first line of a block... */
235 idx = 0;
236 do {
237 for (pos = 0; pos < n; pos++) if (! isspace(p[pos])) break;
238 name_start = pos;
239 for (pos = pos+1; pos < n; pos++) if ( isspace(p[pos])) break;
240 name_len = pos - name_start;
241 for (pos = pos+1; pos < n; pos++) if (! isspace(p[pos])) break;
242 seq_start = pos;
243 if (pos >= n) ESL_XFAIL(eslEFORMAT, afp->errmsg, "invalid alignment line");
244 for (pos = pos+1; pos < n; pos++) if ( isspace(p[pos])) break;
245 seq_len = pos - seq_start; /* expect one block; ignore trailing stuff, inc. optional coords */
246
247 if (idx == 0) {
248 block_seq_start = seq_start;
249 block_seq_len = seq_len;
250 } else {
251 if (seq_start != block_seq_start) ESL_XFAIL(eslEFORMAT, afp->errmsg, "sequence start is misaligned");
252 if (seq_len != block_seq_len) ESL_XFAIL(eslEFORMAT, afp->errmsg, "sequence end is misaligned");
253 }
254
255 /* Store the sequence name. */
256 if (nblocks == 0) {
257 /* make sure we have room for another sequence */
258 if (idx >= msa->sqalloc && (status = esl_msa_Expand(msa)) != eslOK) goto ERROR;
259 if ( (status = esl_msa_SetSeqName(msa, idx, p+name_start, name_len)) != eslOK) goto ERROR;
260 nseq++;
261 } else {
262 if (! esl_memstrcmp(p+name_start, name_len, msa->sqname[idx]))
263 ESL_XFAIL(eslEFORMAT, afp->errmsg, "expected sequence %s on this line, but saw %.*s", msa->sqname[idx], (int) name_len, p+name_start);
264 }
265
266 /* Append the sequence. */
267 cur_alen = alen;
268 if (msa->abc) { status = esl_abc_dsqcat(afp->inmap, &(msa->ax[idx]), &(cur_alen), p+seq_start, seq_len); }
269 if (! msa->abc) { status = esl_strmapcat (afp->inmap, &(msa->aseq[idx]), &(cur_alen), p+seq_start, seq_len); }
270 if (status == eslEINVAL) ESL_XFAIL(eslEFORMAT, afp->errmsg, "one or more invalid sequence characters");
271 else if (status != eslOK) goto ERROR;
272 if (cur_alen - alen != seq_len) ESL_XFAIL(eslEFORMAT, afp->errmsg, "unexpected number of seq characters");
273
274 /* get next line. if it's a consensus line, we're done with the block */
275 status = esl_msafile_GetLine(afp, &p, &n);
276 if (status == eslEOF) ESL_XFAIL(eslEFORMAT, afp->errmsg, "alignment block did not end with consensus line");
277 else if (status != eslOK) goto ERROR;
278
279 idx++;
280 } while (esl_memspn(afp->line, afp->n, " .:*") < afp->n); /* end loop over a block */
281
282 if (idx != nseq) ESL_XFAIL(eslEFORMAT, afp->errmsg, "last block didn't contain same # of seqs as earlier blocks");
283
284 /* skip blank lines until we find start of next block, or EOF */
285 do {
286 status = esl_msafile_GetLine(afp, &p, &n);
287 if (status == eslEOF) break;
288 else if (status != eslOK) goto ERROR;
289 } while (esl_memspn(p, n, " \t") == n);
290
291 alen += block_seq_len;
292 nblocks++;
293 } while (status == eslOK); /* normal end has status == EOF after last block. */
294
295 msa->nseq = nseq;
296 msa->alen = alen;
297 if (( status = esl_msa_SetDefaultWeights(msa)) != eslOK) goto ERROR;
298 *ret_msa = msa;
299 return eslOK;
300
301 ERROR:
302 if (msa) esl_msa_Destroy(msa);
303 *ret_msa = NULL;
304 return status;
305 }
306
307
308 /* Function: esl_msafile_clustal_Write()
309 * Synopsis: Write a CLUSTAL format alignment file to a stream.
310 *
311 * Purpose: Write alignment <msa> to output stream <fp>, in
312 * format <fmt>. If <fmt> is <eslMSAFILE_CLUSTAL>,
313 * write strict CLUSTAL 2.1 format. If <fmt>
314 * is <eslMSAFILE_CLUSTALLIKE>, put "EASEL (VERSION)"
315 * in the header.
316 *
317 * The alignment is written in blocks of 60 aligned
318 * residues at a time.
319 *
320 * Constructing the CLUSTAL consensus line properly
321 * requires knowing the alphabet. If the <msa> is in text
322 * mode, we don't know the alphabet, so then we use a
323 * simplified consensus line, with '*' marking completely
324 * conserved columns, ' ' on everything else. If the <msa>
325 * is in digital mode and of type <eslAMINO>, then we also
326 * use Clustal's "strong" and "weak" residue group
327 * annotations, ':' and '.'. Strong groups are STA, NEQK,
328 * NHQK, NDEQ, QHRK, MILV, MILF, HY, and FYW. Weak groups
329 * are CSA, ATV, SAG, STNK, STPA, SGND, SNDEQK, NDEQHK,
330 * NEQHRK, FVLIM, and HFY.
331 *
332 * Args: fp - open output stream, writable
333 * msa - alignment to write
334 * fmt - eslMSAFILE_CLUSTAL or eslMSAFILE_CLUSTALLIKE
335 *
336 * Returns: <eslOK> on success.
337 *
338 * Throws: <eslEMEM> on allocation error.
339 * <eslEWRITE> on any system write error, such as filled disk.
340 */
341 int
342 esl_msafile_clustal_Write(FILE *fp, const ESL_MSA *msa, int fmt)
343 {
344 int cpl = 60;
345 int maxnamelen = 0;
346 int namelen;
347 char *consline = NULL;
348 char *buf = NULL;
349 int64_t apos;
350 int i;
351 int status;
352
353 ESL_ALLOC(buf, sizeof(char) * (cpl+1));
354 buf[cpl] = '\0';
355 for (i = 0; i < msa->nseq; i++)
356 {
357 namelen = strlen(msa->sqname[i]);
358 maxnamelen = ESL_MAX(namelen, maxnamelen);
359 }
360
361 /* Make a CLUSTAL-like consensus line */
362 if ( msa->abc && (status = make_digital_consensus_line(msa, &consline)) != eslOK) goto ERROR;
363 if (! msa->abc && (status = make_text_consensus_line (msa, &consline)) != eslOK) goto ERROR;
364
365 /* The magic header */
366 if (fmt == eslMSAFILE_CLUSTAL) { if (fprintf(fp, "CLUSTAL 2.1 multiple sequence alignment\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "clustal msa write failed"); }
367 else if (fmt == eslMSAFILE_CLUSTALLIKE) { if (fprintf(fp, "EASEL (%s) multiple sequence alignment\n", EASEL_VERSION) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "clustal msa write failed"); }
368
369 /* The alignment */
370 for (apos = 0; apos < msa->alen; apos += cpl)
371 {
372 if (fprintf(fp, "\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "clustal msa write failed");
373 for (i = 0; i < msa->nseq; i++)
374 {
375 if (msa->abc) esl_abc_TextizeN(msa->abc, msa->ax[i]+apos+1, cpl, buf);
376 if (! msa->abc) strncpy(buf, msa->aseq[i]+apos, cpl);
377 if (fprintf(fp, "%-*s %s\n", maxnamelen, msa->sqname[i], buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "clustal msa write failed");
378 }
379 strncpy(buf, consline+apos, cpl);
380 if (fprintf(fp, "%-*s %s\n", maxnamelen, "", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "clustal msa write failed");
381 }
382
383 free(buf);
384 free(consline);
385 return eslOK;
386
387 ERROR:
388 if (buf) free(buf);
389 if (consline) free(consline);
390 return status;
391 }
392 /*---------------- end, Clustal API -----------------------------*/
393
394
395
396 /*****************************************************************
397 * 2. Internal routines for Clustal formats
398 *****************************************************************/
399
400 /* Clustal consensus lines.
401 * '*' : 100% conserved positions
402 * ':' : all residues in column belong to a "strong group"
403 * '.' : all residues in column belong to a "weak group"
404 * ' ' : otherwise
405 *
406 * Gap characters count, and ambiguity codes are interpreted verbatim,
407 * so even a single gap or ambiguity code makes the column a ' '.
408 *
409 * From examining the source code for ClustalW (as it writes its
410 * "self explanatory format", ahem!):
411 * strong groups = STA, NEQK, NHQK, NDEQ, QHRK, MILV, MILF,
412 * HY, FYW
413 * weak groups = CSA, ATV, SAG, STNK, STPA, SGND, SNDEQK,
414 * NDEQHK, NEQHRK, FVLIM, HFY
415 *
416 * These groups only apply to protein data, and therefore only to
417 * digital alignments using an <eslAMINO> alphabet.
418
419 * Calculating the consensus line can be compute-intensive, for large
420 * alignments. A naive implementation (for each column, collect
421 * residue counts, compare to each conservation group) was judged too
422 * slow: 16.2s to write the Pkinase full alignment, compared to 1.5s
423 * to write Stockholm format [SRE:J8/22]. Here we use a slightly less
424 * naive implementation, which collects a bit vector (one bit per
425 * residue) for each column, and traverses the alignment in stride
426 * (sequences, then columns). Writing Clustal format Pkinase now takes
427 * 2.3s, and most of the difference w.r.t. Stockholm is now assignable
428 * to the smaller width (thus greater number of blocks) written for
429 * Clustal (60 cpl vs 200) rather than to consensus construction.
430 *
431 * An oversophisticated approach could use a finite
432 * automaton to store all groups in one machine, then to use the FA to
433 * process each residue seen in a column; for most columns, we would
434 * quickly reach a rejection state (most columns don't belong to
435 * a conservation group, especially in large alignments). For a sketch
436 * of how to construct and use such an automaton, xref SRE:J8/22.
437 * I decided this was probably overkill, and didn't implement it.
438 */
439
440
441 /* make_text_consensus_line()
442 *
443 * Given a text mode <msa>, allocate and create a CLUSTAL-style
444 * consensus line; return it in <*ret_consline>. Caller is responsible
445 * for free'ing this string.
446 *
447 * In text mode, we don't know the alphabet; in particular, we can't
448 * know if the data are amino acids, so we don't know if it's
449 * appropriate to use the amino acid group codes. So we don't;
450 * in text mode, only '*' and ' ' appear in consensus lines.
451 *
452 * The consensus line is numbered 0..alen-1, and is NUL-terminated.
453 *
454 * Returns <eslOK> on success.
455 * No normal failure codes.
456 * Throws <eslEMEM> on allocation error.
457 */
458 static int
459 make_text_consensus_line(const ESL_MSA *msa, char **ret_consline)
460 {
461 char *consline = NULL;
462 uint32_t *v = NULL;
463 uint32_t tmpv, maxv;
464 int n;
465 int idx, apos, x;
466 int status;
467
468 ESL_ALLOC(consline, sizeof(char) * (msa->alen+1));
469 ESL_ALLOC(v, sizeof(uint32_t) * (msa->alen));
470 for (apos = 0; apos < msa->alen; apos++)
471 v[apos] = 0;
472
473 for (idx = 0; idx < msa->nseq; idx++)
474 for (apos = 0; apos < msa->alen; apos++)
475 {
476 x = toupper(msa->aseq[idx][apos]) - 'A';
477 if (x >= 0 && x < 26) v[apos] |= (1 << x);
478 else v[apos] |= (1 << 26);
479 }
480 maxv = (1 << 26) - 1;
481
482 for (apos = 0; apos < msa->alen; apos++)
483 {
484 for (n = 0, tmpv = v[apos]; tmpv; n++) tmpv &= tmpv-1; /* Kernighan magic: count # of bits set in tmpv */
485 consline[apos] = ((n == 1 && v[apos] < maxv) ? '*' : ' ');
486 }
487 consline[msa->alen] = '\0';
488
489 *ret_consline = consline;
490 free(v);
491 return eslOK;
492
493 ERROR:
494 if (v) free(v);
495 if (consline) free(consline);
496 *ret_consline = NULL;
497 return status;
498 }
499
500
501 /* make_digital_consensus_line()
502 *
503 * Exactly the same as make_text_consensus_line(), except for
504 * digital mode <msa>.
505 */
506 static int
507 matches_group_digital(ESL_ALPHABET *abc, uint32_t v, char *group)
508 {
509 uint32_t gv = 0;
510 ESL_DSQ sym;
511 char *c;
512
513 for (c = group; *c; c++) {
514 sym = esl_abc_DigitizeSymbol(abc, *c);
515 gv |= (1 << sym);
516 }
517 return ( ((v & gv) == v) ? TRUE : FALSE);
518 }
519
520 static int
521 make_digital_consensus_line(const ESL_MSA *msa, char **ret_consline)
522 {
523 char *consline = NULL;
524 uint32_t *v = NULL;
525 uint32_t tmpv, maxv;
526 int n;
527 int idx, apos;
528 int status;
529
530 /* if this ever becomes a problem, easy enough to make v a uint64_t to get up to Kp<=64 */
531 if (msa->abc->Kp > 32) ESL_EXCEPTION(eslEINVAL, "Clustal format writer cannot handle digital alphabets of Kp>32 residues");
532
533 ESL_ALLOC(v, sizeof(uint32_t) * (msa->alen+1));
534 ESL_ALLOC(consline, sizeof(char) * (msa->alen+1));
535 for (apos = 0; apos <= msa->alen; apos++)
536 v[apos] = 0;
537
538 for (idx = 0; idx < msa->nseq; idx++)
539 for (apos = 1; apos <= msa->alen; apos++)
540 v[apos] |= (1 << msa->ax[idx][apos]);
541
542 maxv = (1 << msa->abc->K) - 1; /* maxv: has all canonical residue bits set */
543
544 for (apos = 1; apos <= msa->alen; apos++)
545 {
546 consline[apos-1] = ' ';
547
548 for (n = 0, tmpv = v[apos]; tmpv; n++) tmpv &= tmpv-1; /* Kernighan magic: count # of bits set in tmpv */
549
550 if (n == 0 || n > 6) continue; /* n==0 shouldn't happen; n > 6 means too many different residues seen */
551 else if (v[apos] > maxv) continue; /* gap or ambiguity chars seen; column must be left unannotated */
552 else if (n == 1) consline[apos-1] = '*'; /* complete conservation of a canonical residue */
553 else if (msa->abc->type == eslAMINO)
554 {
555 if (matches_group_digital(msa->abc, v[apos], "STA")) consline[apos-1] = ':';
556 else if (matches_group_digital(msa->abc, v[apos], "NEQK")) consline[apos-1] = ':';
557 else if (matches_group_digital(msa->abc, v[apos], "NHQK")) consline[apos-1] = ':';
558 else if (matches_group_digital(msa->abc, v[apos], "NDEQ")) consline[apos-1] = ':';
559 else if (matches_group_digital(msa->abc, v[apos], "QHRK")) consline[apos-1] = ':';
560 else if (matches_group_digital(msa->abc, v[apos], "MILV")) consline[apos-1] = ':';
561 else if (matches_group_digital(msa->abc, v[apos], "MILF")) consline[apos-1] = ':';
562 else if (matches_group_digital(msa->abc, v[apos], "HY")) consline[apos-1] = ':';
563 else if (matches_group_digital(msa->abc, v[apos], "FYW")) consline[apos-1] = ':';
564
565 else if (matches_group_digital(msa->abc, v[apos], "CSA")) consline[apos-1] = '.';
566 else if (matches_group_digital(msa->abc, v[apos], "ATV")) consline[apos-1] = '.';
567 else if (matches_group_digital(msa->abc, v[apos], "SAG")) consline[apos-1] = '.';
568 else if (matches_group_digital(msa->abc, v[apos], "STNK")) consline[apos-1] = '.';
569 else if (matches_group_digital(msa->abc, v[apos], "STPA")) consline[apos-1] = '.';
570 else if (matches_group_digital(msa->abc, v[apos], "SGND")) consline[apos-1] = '.';
571 else if (matches_group_digital(msa->abc, v[apos], "SNDEQK")) consline[apos-1] = '.';
572 else if (matches_group_digital(msa->abc, v[apos], "NDEQHK")) consline[apos-1] = '.';
573 else if (matches_group_digital(msa->abc, v[apos], "NEQHRK")) consline[apos-1] = '.';
574 else if (matches_group_digital(msa->abc, v[apos], "FVLIM")) consline[apos-1] = '.';
575 else if (matches_group_digital(msa->abc, v[apos], "HFY")) consline[apos-1] = '.';
576 }
577 }
578 consline[apos-1] = '\0';
579
580 *ret_consline = consline;
581 free(v);
582 return eslOK;
583
584 ERROR:
585 if (v) free(v);
586 if (consline) free(consline);
587 *ret_consline = NULL;
588 return eslOK;
589 }
590 /*-------------- end, internal clustal routines -----------------*/
591
592
593 /*****************************************************************
594 * 3. Unit tests.
595 *****************************************************************/
596 #ifdef eslMSAFILE_CLUSTAL_TESTDRIVE
597
598 static void
599 utest_write_good1(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
600 {
601 fputs("MUSCLE (3.7) multiple sequence alignment\n", ofp);
602 fputs("\n", ofp);
603 fputs("\n", ofp);
604 fputs("MYG_PHYCA --------V-LSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSHPETLEKFDRFKHLKT\n", ofp);
605 fputs("GLB5_PETMA PIVDTGSVAPLSAAEKTKIRSAWAPVYSTYETSGVDILVKFFTSTPAAQEFFPKFKGLTT\n", ofp);
606 fputs("HBB_HUMAN --------VHLTPEEKSAVTALWGKV--NVDEVGGEALGRLLVVYPWTQRFFESFGDLST\n", ofp);
607 fputs("HBA_HUMAN --------V-LSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHF-----\n", ofp);
608 fputs(" . *: :. : *. * * : * .:: * : * * \n", ofp);
609 fputs("\n", ofp);
610 fputs("MYG_PHYCA EAEMKASEDLKKHGVTVLTALGAILKKKGH---HEAELKPLAQSHATKHKIPIKYLEFIS\n", ofp);
611 fputs("GLB5_PETMA ADQLKKSADVRWHAERIINAVNDAVASMDDTEKMSMKLRDLSGKHAKSFQVDPQYFKVLA\n", ofp);
612 fputs("HBB_HUMAN PDAVMGNPKVKAHGKKVLGAFSDGLAHLDN---LKGTFATLSELHCDKLHVDPENFRLLG\n", ofp);
613 fputs("HBA_HUMAN -DLSHGSAQVKGHGKKVADALTNAVAHVDD---MPNALSALSDLHAHKLRVDPVNFKLLS\n", ofp);
614 fputs(" \n", ofp); /* deliberately made blank */
615 fputs("\n", ofp);
616 fputs("MYG_PHYCA EAIIHVLHSRHPGDFGADAQGAMNKALELFRKDIAAKYKELGYQG\n", ofp);
617 fputs("GLB5_PETMA AVI---------ADTVAAGDAGFEKLMSMICILLRSAY-------\n", ofp);
618 fputs("HBB_HUMAN NVLVCVLAHHFGKEFTPPVQAAYQKVVAGVANALAHKYH------\n", ofp);
619 fputs("HBA_HUMAN HCLLVTLAAHLPAEFTPAVHASLDKFLASVSTVLTSKYR------\n", ofp);
620 fputs(" : : . .. :* : . : * \n", ofp);
621 fputs("\n", ofp);
622
623 *ret_format = eslMSAFILE_CLUSTALLIKE;
624 *ret_alphatype = eslAMINO;
625 *ret_nseq = 4;
626 *ret_alen = 165;
627 }
628
629 static void
630 utest_write_good2(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
631 {
632 fputs("CLUSTAL W (1.81) multiple sequence alignment\n", ofp);
633 fputs("\n", ofp);
634 fputs("tRNA2 UCCGAUAUAGUGUAACGGCUAUCACAUCACGCUUUCACCGUGG-AGACCGGGGUUCGACU\n", ofp);
635 fputs("tRNA3 UCCGUGAUAGUUUAAUGGUCAGAAUGG-GCGCUUGUCGCGUGCCAGAUCGGGGUUCAAUU\n", ofp);
636 fputs("tRNA5 GGGCACAUGGCGCAGUUGGUAGCGCGCUUCCCUUGCAAGGAAGAGGUCAUCGGUUCGAUU\n", ofp);
637 fputs("tRNA1 GCGGAUUUAGCUCAGUUGGGAGAGCGCCAGACUGAAGAUCUGGAGGUCCUGUGUUCGAUC\n", ofp);
638 fputs("tRNA4 GCUCGUAUGGCGCAGUGG-UAGCGCAGCAGAUUGCAAAUCUGUUGGUCCUUAGUUCGAUC\n", ofp);
639 fputs(" * * * * * * * **** * \n", ofp);
640 fputs("\n", ofp);
641 fputs("tRNA2 CCCCGUAUCGGAG\n", ofp);
642 fputs("tRNA3 CCCCGUCGCGGAG\n", ofp);
643 fputs("tRNA5 CCGGUUGCGUCCA\n", ofp);
644 fputs("tRNA1 CACAGAAUUCGCA\n", ofp);
645 fputs("tRNA4 CUGAGUGCGAGCU\n", ofp);
646 fputs(" * \n", ofp);
647
648 *ret_format = eslMSAFILE_CLUSTAL;
649 *ret_alphatype = eslRNA;
650 *ret_nseq = 5;
651 *ret_alen = 73;
652 }
653
654 /* An example of clustal format with optional sequence coords;
655 * a quickly-taken subset of a larger alignment reported as a bug.
656 */
657 static void
658 utest_write_good3(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
659 {
660 fputs("CLUSTAL 2.1 multiple sequence alignment\n", ofp);
661 fputs("\n", ofp);
662 fputs("gi|85091828|ref|XP_959093.1| MSDFTSKVKVLRDGQKPEFPSN----ANTLEYAQSLDAQDELRHFRNEFI 46\n", ofp);
663 fputs("gi|70993990|ref|XP_751842.1| ----MSTNGTLS---KPEFPAN----AASKEYAASLDAADPFAGFREKFI 39\n", ofp);
664 fputs("gi|71001376|ref|XP_755369.1| ---MGSRLHVQVIHGGPPLPYKDDIRAFGKEYAEQLDAQDPLRRFRDEFI 47\n", ofp);
665 fputs("gi|71744026|ref|XP_803513.1| -----------------------------------MDRNDPLQVHRDAFN 15\n", ofp);
666 fputs(" : : \n", ofp);
667 fputs("\n", ofp);
668 fputs("gi|85091828|ref|XP_959093.1| IPTRASLKKKALDGI--------------IPGTQANGTTTSTDADTPCIY 82\n", ofp);
669 fputs("gi|70993990|ref|XP_751842.1| IPSKANIASTKLA----------------KPGLSSE----------PCIY 63\n", ofp);
670 fputs("gi|71001376|ref|XP_755369.1| IPSKKDLKRKTLFPNDGMYSCGHPICFANTSCACVHAAETEETSDEKCIY 97\n", ofp);
671 fputs("gi|71744026|ref|XP_803513.1| IPKRRDGS--------------------------------------DHVY 27\n", ofp);
672 fputs(" *\n", ofp);
673
674 *ret_format = eslMSAFILE_CLUSTAL;
675 *ret_alphatype = eslAMINO;
676 *ret_nseq = 4;
677 *ret_alen = 100;
678 }
679
680
681 static void
682 utest_goodfile(char *filename, int testnumber, int expected_format, int expected_alphatype, int expected_nseq, int expected_alen)
683 {
684 ESL_ALPHABET *abc = NULL;
685 ESL_MSAFILE *afp = NULL;
686 ESL_MSA *msa1 = NULL;
687 ESL_MSA *msa2 = NULL;
688 char tmpfile1[32] = "esltmpXXXXXX";
689 char tmpfile2[32] = "esltmpXXXXXX";
690 FILE *ofp = NULL;
691 int status;
692
693 /* guessing both the format and the alphabet should work: this is a digital open */
694 if ( (status = esl_msafile_Open(&abc, filename, NULL, eslMSAFILE_UNKNOWN, NULL, &afp)) != eslOK) esl_fatal("clustal good file test %d failed: digital open", testnumber);
695 if (afp->format != expected_format) esl_fatal("clustal good file test %d failed: format autodetection", testnumber);
696 if (abc->type != expected_alphatype) esl_fatal("clustal good file test %d failed: alphabet autodetection", testnumber);
697
698 /* This is a digital read, using <abc>. */
699 if ( (status = esl_msafile_clustal_Read(afp, &msa1)) != eslOK) esl_fatal("clustal good file test %d failed: msa read, digital", testnumber);
700 if (msa1->nseq != expected_nseq || msa1->alen != expected_alen) esl_fatal("clustal good file test %d failed: nseq/alen", testnumber);
701 if (esl_msa_Validate(msa1, NULL) != eslOK) esl_fatal("clustal good file test %d failed: msa1 invalid", testnumber);
702 esl_msafile_Close(afp);
703
704 /* write it back out to a new tmpfile (digital write) */
705 if ( (status = esl_tmpfile_named(tmpfile1, &ofp)) != eslOK) esl_fatal("clustal good file test %d failed: tmpfile creation", testnumber);
706 if ( (status = esl_msafile_clustal_Write(ofp, msa1, expected_format)) != eslOK) esl_fatal("clustal good file test %d failed: msa write, digital", testnumber);
707 fclose(ofp);
708
709 /* now open and read it as text mode, in known format. (We have to pass fmtd now, to deal with the possibility of a nonstandard name width) */
710 if ( (status = esl_msafile_Open(NULL, tmpfile1, NULL, expected_format, NULL, &afp)) != eslOK) esl_fatal("clustal good file test %d failed: text mode open", testnumber);
711 if ( (status = esl_msafile_clustal_Read(afp, &msa2)) != eslOK) esl_fatal("clustal good file test %d failed: msa read, text", testnumber);
712 if (msa2->nseq != expected_nseq || msa2->alen != expected_alen) esl_fatal("clustal good file test %d failed: nseq/alen", testnumber);
713 if (esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal("clustal good file test %d failed: msa2 invalid", testnumber);
714 esl_msafile_Close(afp);
715
716 /* write it back out to a new tmpfile (text write) */
717 if ( (status = esl_tmpfile_named(tmpfile2, &ofp)) != eslOK) esl_fatal("clustal good file test %d failed: tmpfile creation", testnumber);
718 if ( (status = esl_msafile_clustal_Write(ofp, msa2, expected_format)) != eslOK) esl_fatal("clustal good file test %d failed: msa write, text", testnumber);
719 fclose(ofp);
720 esl_msa_Destroy(msa2);
721
722 /* open and read it in digital mode */
723 if ( (status = esl_msafile_Open(&abc, tmpfile1, NULL, expected_format, NULL, &afp)) != eslOK) esl_fatal("clustal good file test %d failed: 2nd digital mode open", testnumber);
724 if ( (status = esl_msafile_clustal_Read(afp, &msa2)) != eslOK) esl_fatal("clustal good file test %d failed: 2nd digital msa read", testnumber);
725 if (esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal("clustal good file test %d failed: msa2 invalid", testnumber);
726 esl_msafile_Close(afp);
727
728 /* this msa <msa2> should be identical to <msa1> */
729 if (esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal("clustal good file test %d failed: msa compare", testnumber);
730
731 remove(tmpfile1);
732 remove(tmpfile2);
733 esl_msa_Destroy(msa1);
734 esl_msa_Destroy(msa2);
735 esl_alphabet_Destroy(abc);
736 }
737
738 static void
739 write_test_msas(FILE *ofp1, FILE *ofp2)
740 {
741 fprintf(ofp1, "EASEL (X.x) multiple sequence alignment\n");
742 fprintf(ofp1, "\n");
743 fprintf(ofp1, "seq1 ..acdefghiklmnpqrstvwy\n");
744 fprintf(ofp1, "seq2 ..acdefghiklmnpqrstv--\n");
745 fprintf(ofp1, "seq3 aaacdefghiklmnpqrstv--\n");
746 fprintf(ofp1, "seq4 ..acdefghiklmnpqrstvwy\n");
747 fprintf(ofp1, " ****************** \n");
748 fprintf(ofp1, "\n");
749 fprintf(ofp1, "seq1 ACDEFGHIKLMNPQRSTVWY\n");
750 fprintf(ofp1, "seq2 ACDEFGHIKLMNPQRSTVWY\n");
751 fprintf(ofp1, "seq3 ACDEFGHIKLMNPQRSTVWY\n");
752 fprintf(ofp1, "seq4 ACDEFGHIKLMNPQRSTVWY\n");
753 fprintf(ofp1, " ********************\n");
754 fprintf(ofp1, "\n");
755 fprintf(ofp1, "seq1 ..\n");
756 fprintf(ofp1, "seq2 YY\n");
757 fprintf(ofp1, "seq3 ..\n");
758 fprintf(ofp1, "seq4 ..\n");
759 fprintf(ofp1, "\n");
760
761 fprintf(ofp2, "# STOCKHOLM 1.0\n");
762 fprintf(ofp2, "\n");
763 fprintf(ofp2, "seq1 ..acdefghiklmnpqrstvwyACDEFGHIKLMNPQRSTVWY..\n");
764 fprintf(ofp2, "seq2 ..acdefghiklmnpqrstv--ACDEFGHIKLMNPQRSTVWYYY\n");
765 fprintf(ofp2, "seq3 aaacdefghiklmnpqrstv--ACDEFGHIKLMNPQRSTVWY..\n");
766 fprintf(ofp2, "seq4 ..acdefghiklmnpqrstvwyACDEFGHIKLMNPQRSTVWY..\n");
767 fprintf(ofp2, "//\n");
768 }
769
770 static void
771 read_test_msas_digital(char *alnfile, char *stkfile)
772 {
773 char msg[] = "CLUSTAL msa digital read unit test failed";
774 ESL_ALPHABET *abc = NULL;
775 ESL_MSAFILE *afp1 = NULL;
776 ESL_MSAFILE *afp2 = NULL;
777 ESL_MSA *msa1, *msa2, *msa3, *msa4;
778 FILE *alnfp, *stkfp;
779 char alnfile2[32] = "esltmpaln2XXXXXX";
780 char stkfile2[32] = "esltmpstk2XXXXXX";
781
782 if ( esl_msafile_Open(&abc, alnfile, NULL, eslMSAFILE_CLUSTALLIKE, NULL, &afp1) != eslOK) esl_fatal(msg);
783 if ( !abc || abc->type != eslAMINO) esl_fatal(msg);
784 if ( esl_msafile_Open(&abc, stkfile, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg);
785 if ( esl_msafile_clustal_Read (afp1, &msa1) != eslOK) esl_fatal(msg);
786 if ( esl_msafile_stockholm_Read(afp2, &msa2) != eslOK) esl_fatal(msg);
787 if ( esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal(msg);
788
789 if ( esl_msafile_clustal_Read (afp1, &msa3) != eslEOF) esl_fatal(msg);
790 if ( esl_msafile_stockholm_Read(afp2, &msa3) != eslEOF) esl_fatal(msg);
791
792 esl_msafile_Close(afp2);
793 esl_msafile_Close(afp1);
794
795 /* Now write stk to clustal file, and vice versa; then retest */
796 if ( esl_tmpfile_named(alnfile2, &alnfp) != eslOK) esl_fatal(msg);
797 if ( esl_tmpfile_named(stkfile2, &stkfp) != eslOK) esl_fatal(msg);
798 if ( esl_msafile_clustal_Write (alnfp, msa2, eslMSAFILE_CLUSTAL) != eslOK) esl_fatal(msg);
799 if ( esl_msafile_stockholm_Write(stkfp, msa1, eslMSAFILE_STOCKHOLM) != eslOK) esl_fatal(msg);
800 fclose(alnfp);
801 fclose(stkfp);
802 if ( esl_msafile_Open(&abc, alnfile2, NULL, eslMSAFILE_CLUSTAL, NULL, &afp1) != eslOK) esl_fatal(msg);
803 if ( esl_msafile_Open(&abc, stkfile2, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg);
804 if ( esl_msafile_clustal_Read (afp1, &msa3) != eslOK) esl_fatal(msg);
805 if ( esl_msafile_stockholm_Read(afp2, &msa4) != eslOK) esl_fatal(msg);
806 if ( esl_msa_Compare(msa3, msa4) != eslOK) esl_fatal(msg);
807
808 remove(alnfile2);
809 remove(stkfile2);
810 esl_msafile_Close(afp2);
811 esl_msafile_Close(afp1);
812
813 esl_msa_Destroy(msa1);
814 esl_msa_Destroy(msa2);
815 esl_msa_Destroy(msa3);
816 esl_msa_Destroy(msa4);
817 esl_alphabet_Destroy(abc);
818 }
819
820 static void
821 read_test_msas_text(char *alnfile, char *stkfile)
822 {
823 char msg[] = "CLUSTAL msa text-mode read unit test failed";
824 ESL_MSAFILE *afp1 = NULL;
825 ESL_MSAFILE *afp2 = NULL;
826 ESL_MSA *msa1, *msa2, *msa3, *msa4;
827 FILE *alnfp, *stkfp;
828 char alnfile2[32] = "esltmpaln2XXXXXX";
829 char stkfile2[32] = "esltmpstk2XXXXXX";
830
831 /* vvvv-- everything's the same as the digital utest except these NULLs */
832 if ( esl_msafile_Open(NULL, alnfile, NULL, eslMSAFILE_CLUSTALLIKE, NULL, &afp1) != eslOK) esl_fatal(msg);
833 if ( esl_msafile_Open(NULL, stkfile, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg);
834 if ( esl_msafile_clustal_Read (afp1, &msa1) != eslOK) esl_fatal(msg);
835 if ( esl_msafile_stockholm_Read(afp2, &msa2) != eslOK) esl_fatal(msg);
836 if ( esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal(msg);
837 if ( esl_msafile_clustal_Read (afp1, &msa3) != eslEOF) esl_fatal(msg);
838 if ( esl_msafile_stockholm_Read(afp2, &msa3) != eslEOF) esl_fatal(msg);
839 esl_msafile_Close(afp2);
840 esl_msafile_Close(afp1);
841
842 if ( esl_tmpfile_named(alnfile2, &alnfp) != eslOK) esl_fatal(msg);
843 if ( esl_tmpfile_named(stkfile2, &stkfp) != eslOK) esl_fatal(msg);
844 if ( esl_msafile_clustal_Write (alnfp, msa2, eslMSAFILE_CLUSTAL) != eslOK) esl_fatal(msg);
845 if ( esl_msafile_stockholm_Write(stkfp, msa1, eslMSAFILE_STOCKHOLM) != eslOK) esl_fatal(msg);
846 fclose(alnfp);
847 fclose(stkfp);
848 if ( esl_msafile_Open(NULL, alnfile2, NULL, eslMSAFILE_CLUSTAL, NULL, &afp1) != eslOK) esl_fatal(msg);
849 if ( esl_msafile_Open(NULL, stkfile2, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg);
850 if ( esl_msafile_clustal_Read (afp1, &msa3) != eslOK) esl_fatal(msg);
851 if ( esl_msafile_stockholm_Read(afp2, &msa4) != eslOK) esl_fatal(msg);
852 if ( esl_msa_Compare(msa3, msa4) != eslOK) esl_fatal(msg);
853
854 remove(alnfile2);
855 remove(stkfile2);
856 esl_msafile_Close(afp2);
857 esl_msafile_Close(afp1);
858
859 esl_msa_Destroy(msa1);
860 esl_msa_Destroy(msa2);
861 esl_msa_Destroy(msa3);
862 esl_msa_Destroy(msa4);
863 }
864 #endif /*eslMSAFILE_CLUSTAL_TESTDRIVE*/
865 /*---------------------- end, unit tests ------------------------*/
866
867
868 /*****************************************************************
869 * 4. Test driver.
870 *****************************************************************/
871 #ifdef eslMSAFILE_CLUSTAL_TESTDRIVE
872 /* compile: gcc -g -Wall -I. -L. -o esl_msafile_clustal_utest -DeslMSAFILE_CLUSTAL_TESTDRIVE esl_msafile_clustal.c -leasel -lm
873 * (gcov): gcc -g -Wall -fprofile-arcs -ftest-coverage -I. -L. -o esl_msafile_clustal_utest -DeslMSAFILE_CLUSTAL_TESTDRIVE esl_msafile_clustal.c -leasel -lm
874 * run: ./esl_msafile_clustal_utest
875 */
876 #include <esl_config.h>
877
878 #include <stdio.h>
879
880 #include "easel.h"
881 #include "esl_getopts.h"
882 #include "esl_random.h"
883 #include "esl_msafile.h"
884 #include "esl_msafile_clustal.h"
885
886 static ESL_OPTIONS options[] = {
887 /* name type default env range togs reqs incomp help docgrp */
888 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
889 { 0,0,0,0,0,0,0,0,0,0},
890 };
891 static char usage[] = "[-options]";
892 static char banner[] = "test driver for CLUSTAL MSA format module";
893
894 int
895 main(int argc, char **argv)
896 {
897 char msg[] = "CLUSTAL MSA i/o module test driver failed";
898 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
899 char alnfile[32] = "esltmpalnXXXXXX";
900 char stkfile[32] = "esltmpstkXXXXXX";
901 FILE *alnfp, *stkfp;
902 int testnumber;
903 int ngoodtests = 3;
904 char tmpfile[32];
905 FILE *ofp;
906 int expected_format;
907 int expected_alphatype;
908 int expected_nseq;
909 int expected_alen;
910
911 if ( esl_tmpfile_named(alnfile, &alnfp) != eslOK) esl_fatal(msg);
912 if ( esl_tmpfile_named(stkfile, &stkfp) != eslOK) esl_fatal(msg);
913 write_test_msas(alnfp, stkfp);
914 fclose(alnfp);
915 fclose(stkfp);
916
917 read_test_msas_digital(alnfile, stkfile);
918 read_test_msas_text (alnfile, stkfile);
919
920 /* Various "good" files that should be parsed correctly */
921 for (testnumber = 1; testnumber <= ngoodtests; testnumber++)
922 {
923 strcpy(tmpfile, "esltmpXXXXXX");
924 if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
925 switch (testnumber) {
926 case 1: utest_write_good1 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
927 case 2: utest_write_good2 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
928 case 3: utest_write_good3 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
929 }
930 fclose(ofp);
931 utest_goodfile(tmpfile, testnumber, expected_format, expected_alphatype, expected_nseq, expected_alen);
932 remove(tmpfile);
933 }
934
935 remove(alnfile);
936 remove(stkfile);
937 esl_getopts_Destroy(go);
938 return 0;
939 }
940 #endif /*eslMSAFILE_CLUSTAL_TESTDRIVE*/
941 /*--------------------- end, test driver ------------------------*/
942
943
944 /*****************************************************************
945 * 5. Examples.
946 *****************************************************************/
947
948
949 #ifdef eslMSAFILE_CLUSTAL_EXAMPLE
950 /* A full-featured example of reading/writing an MSA in Clustal format(s).
951 gcc -g -Wall -o esl_msafile_clustal_example -I. -L. -DeslMSAFILE_CLUSTAL_EXAMPLE esl_msafile_clustal.c -leasel -lm
952 ./esl_msafile_clustal_example <msafile>
953 */
954 /*::cexcerpt::msafile_clustal_example::begin::*/
955 #include <stdio.h>
956
957 #include "easel.h"
958 #include "esl_alphabet.h"
959 #include "esl_getopts.h"
960 #include "esl_msa.h"
961 #include "esl_msafile.h"
962 #include "esl_msafile_clustal.h"
963
964 static ESL_OPTIONS options[] = {
965 /* name type default env range toggles reqs incomp help docgroup*/
966 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
967 { "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "no autodetection; use CLUSTAL format", 0 },
968 { "-2", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "no autodetection; use CLUSTALLIKE format", 0 },
969 { "-q", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "quieter: don't write msa back, just summary", 0 },
970 { "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use text mode: no digital alphabet", 0 },
971 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is DNA", 0 },
972 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is RNA", 0 },
973 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is protein", 0 },
974 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
975 };
976 static char usage[] = "[-options] <msafile>";
977 static char banner[] = "example of guessing, reading, writing Clustal formats";
978
979 int
980 main(int argc, char **argv)
981 {
982 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage);
983 char *filename = esl_opt_GetArg(go, 1);
984 int infmt = eslMSAFILE_UNKNOWN;
985 ESL_ALPHABET *abc = NULL;
986 ESL_MSAFILE *afp = NULL;
987 ESL_MSA *msa = NULL;
988 int status;
989
990 if (esl_opt_GetBoolean(go, "-1")) infmt = eslMSAFILE_CLUSTAL;
991 else if (esl_opt_GetBoolean(go, "-2")) infmt = eslMSAFILE_CLUSTALLIKE;
992
993 if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
994 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
995 else if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
996
997 /* Text mode: pass NULL for alphabet.
998 * Digital mode: pass ptr to expected ESL_ALPHABET; and if abc=NULL, alphabet is guessed
999 */
1000 if (esl_opt_GetBoolean(go, "-t")) status = esl_msafile_Open(NULL, filename, NULL, infmt, NULL, &afp);
1001 else status = esl_msafile_Open(&abc, filename, NULL, infmt, NULL, &afp);
1002 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
1003
1004 if ( (status = esl_msafile_clustal_Read(afp, &msa)) != eslOK)
1005 esl_msafile_ReadFailure(afp, status);
1006
1007 printf("format variant: %s\n", esl_msafile_DecodeFormat(afp->format));
1008 printf("alphabet: %s\n", (abc ? esl_abc_DecodeType(abc->type) : "none (text mode)"));
1009 printf("# of seqs: %d\n", msa->nseq);
1010 printf("# of cols: %d\n", (int) msa->alen);
1011 printf("\n");
1012
1013 if (! esl_opt_GetBoolean(go, "-q"))
1014 esl_msafile_clustal_Write(stdout, msa, eslMSAFILE_CLUSTAL);
1015
1016 esl_msa_Destroy(msa);
1017 esl_msafile_Close(afp);
1018 if (abc) esl_alphabet_Destroy(abc);
1019 esl_getopts_Destroy(go);
1020 exit(0);
1021 }
1022 /*::cexcerpt::msafile_clustal_example::end::*/
1023 #endif /*eslMSAFILE_CLUSTAL_EXAMPLE*/
1024
1025 #ifdef eslMSAFILE_CLUSTAL_EXAMPLE2
1026 /* A minimal example. Read Clustal MSA, in text mode.
1027 gcc -g -Wall -o esl_msafile_clustal_example2 -I. -L. -DeslMSAFILE_CLUSTAL_EXAMPLE2 esl_msafile_clustal.c -leasel -lm
1028 ./esl_msafile_clustal_example2 <msafile>
1029 */
1030
1031 /*::cexcerpt::msafile_clustal_example2::begin::*/
1032 #include <stdio.h>
1033
1034 #include "easel.h"
1035 #include "esl_msa.h"
1036 #include "esl_msafile.h"
1037 #include "esl_msafile_clustal.h"
1038
1039 int
1040 main(int argc, char **argv)
1041 {
1042 char *filename = argv[1];
1043 int fmt = eslMSAFILE_CLUSTAL; /* or eslMSAFILE_CLUSTALLIKE */
1044 ESL_MSAFILE *afp = NULL;
1045 ESL_MSA *msa = NULL;
1046 int status;
1047
1048 if ( (status = esl_msafile_Open(NULL, filename, NULL, fmt, NULL, &afp)) != eslOK) esl_msafile_OpenFailure(afp, status);
1049 if ( (status = esl_msafile_clustal_Read(afp, &msa)) != eslOK) esl_msafile_ReadFailure(afp, status);
1050
1051 printf("%6d seqs, %5d columns\n", msa->nseq, (int) msa->alen);
1052
1053 esl_msafile_clustal_Write(stdout, msa, eslMSAFILE_CLUSTAL);
1054
1055 esl_msa_Destroy(msa);
1056 esl_msafile_Close(afp);
1057 exit(0);
1058 }
1059 /*::cexcerpt::msafile_clustal_example2::end::*/
1060 #endif /*eslMSAFILE_CLUSTAL_EXAMPLE2*/
1061 /*--------------------- end of example --------------------------*/
1062