Debian Codegraph @

hmmer/easel/esl_msafile_phylip.c

raw symbols SCIP
1 /* I/O of multiple sequence alignment files in PHYLIP format(s)
2 *
3 * Contents:
4 * 1. API for reading/writing PHYLIP format alignment files
5 * 2. I/O of the interleaved variant of the format
6 * 3. I/O of the sequential variant of the format
7 * 4. Autodetection of format and its variants
8 * 5. Rectifying valid input/output symbols in names, seqs
9 * 6. Unit tests
10 * 7. Test driver
11 * 8. Example
12 *
13 * See: http://evolution.genetics.washington.edu/phylip/doc/sequence.html
14 */
15 #include <esl_config.h>
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21
22 #include "easel.h"
23 #include "esl_alphabet.h"
24 #include "esl_mem.h"
25 #include "esl_msa.h"
26 #include "esl_msafile.h"
27
28 #include "esl_msafile_phylip.h"
29
30 #define eslMSAFILE_PHYLIP_LEGALSYMS "-ABCDEFGHIJKLMNOPQRSTUVWZYX*?."
31
32 static int phylip_interleaved_Read(ESL_MSAFILE *afp, ESL_MSA *msa, int nseq, int32_t alen_stated);
33 static int phylip_interleaved_Write(FILE *fp, const ESL_MSA *msa, ESL_MSAFILE_FMTDATA *opt_fmtd);
34
35 static int phylip_sequential_Read(ESL_MSAFILE *afp, ESL_MSA *msa, int nseq, int32_t alen_stated);
36 static int phylip_sequential_Write(FILE *fp, const ESL_MSA *msa, ESL_MSAFILE_FMTDATA *opt_fmtd);
37
38 static int phylip_check_interleaved (ESL_BUFFER *bf, int *ret_nblocks, int *ret_namewidth);
39 static int phylip_check_sequential_known (ESL_BUFFER *bf, int namewidth);
40 static int phylip_check_sequential_unknown(ESL_BUFFER *bf, int *ret_namewidth);
41 static int phylip_parse_header(ESL_BUFFER *bf, int32_t *ret_nseq, int32_t *ret_alen, char **ret_p, esl_pos_t *ret_n);
42 static int phylip_collate_colcodes(char *p, esl_pos_t n, char *colcodes, int ncols);
43 static int phylip_deduce_namewidth(char *colcodes0, int ncols0, int alen, int nres2, int *ret_namewidth);
44
45 static int phylip_rectify_input_name(char *namebuf, char *p, int n);
46 static int phylip_rectify_output_seq_digital(char *buf);
47 static int phylip_rectify_output_seq_text(char *buf);
48
49 /*****************************************************************
50 * 1. API for reading/writing PHYLIP format alignment files
51 *****************************************************************/
52
53 /* Function: esl_msafile_phylip_SetInmap()
54 * Synopsis: Configure input map for PHYLIP formats.
55 *
56 * Purpose: Set the <afp->inmap> for PHYLIP formats.
57 *
58 * Phylip documentation states that DNA programs accept
59 * 'ABCDGHKMNORSTUVWXY?-', that 'a period was previously
60 * allowed' and that O means a deletion. Protein programs
61 * accept 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*?-', and while JOU
62 * are accepted, they are unused.
63 *
64 * So: in text mode, we accept any alphabetic character
65 * plus '-*?.', verbatim. '~_', which Easel would normally
66 * accept, are illegal. Whitespace and numbers are ignored.
67 *
68 * In digital mode, we modify the digital alphabet by
69 * demapping '~_' and making them illegal; '?' is mapped to
70 * missing data; whitespace and numbers are ignored;
71 * and ONLY in <eslDNA> or <eslRNA> alphabets, 'O' is
72 * mapped to a gap.
73 *
74 * The inconsistent mapping of 'O' poses potential
75 * problems. In text mode (where we don't know the
76 * alphabet, and thus don't know what to do with O), we
77 * input the O verbatim. In digital mode, in a DNA or RNA
78 * alphabet, we map O to a gap; in other digital alphabets,
79 * we use the default digital alphabet mapping of O.
80 *
81 * Xref: http://evolution.genetics.washington.edu/phylip/doc/sequence.html
82 */
83 int
84 esl_msafile_phylip_SetInmap(ESL_MSAFILE *afp)
85 {
86 int sym;
87
88 if (afp->abc)
89 {
90 for (sym = 1; sym < 128; sym++) afp->inmap[sym] = afp->abc->inmap[sym];
91 for (sym = '0'; sym < '9'; sym++) afp->inmap[sym] = eslDSQ_IGNORED;
92 afp->inmap['?'] = esl_abc_XGetMissing(afp->abc);
93 afp->inmap['~'] = eslDSQ_ILLEGAL;
94 afp->inmap['_'] = eslDSQ_ILLEGAL;
95 afp->inmap[' '] = eslDSQ_IGNORED;
96 afp->inmap['\t'] = eslDSQ_IGNORED;
97 afp->inmap[0] = esl_abc_XGetUnknown(afp->abc);
98
99 if (afp->abc->type == eslDNA || afp->abc->type == eslRNA)
100 afp->inmap['O'] = esl_abc_XGetGap(afp->abc);
101 }
102
103 if (! afp->abc)
104 {
105 for (sym = 1; sym < 128; sym++) afp->inmap[sym] = eslDSQ_ILLEGAL;
106 for (sym = 'a'; sym <= 'z'; sym++) afp->inmap[sym] = sym;
107 for (sym = 'A'; sym <= 'Z'; sym++) afp->inmap[sym] = sym;
108 for (sym = '0'; sym <= '9'; sym++) afp->inmap[sym] = eslDSQ_IGNORED;
109 afp->inmap['-'] = '-';
110 afp->inmap['*'] = '*';
111 afp->inmap['?'] = '?';
112 afp->inmap['.'] = '.';
113 afp->inmap[' '] = eslDSQ_IGNORED;
114 afp->inmap['\t'] = eslDSQ_IGNORED;
115 afp->inmap[0] = '?';
116 }
117 return eslOK;
118 }
119
120
121 /* Function: esl_msafile_phylip_GuessAlphabet()
122 * Synopsis: Guess the alphabet of an open PHYLIP MSA input.
123 *
124 * Purpose: Guess the alphabet of the sequences in open
125 * PHYLIP format MSA file <afp>.
126 *
127 * On normal return, <*ret_type> is set to <eslDNA>,
128 * <eslRNA>, or <eslAMINO>, and <afp> is reset to its
129 * original point.
130 *
131 * Args: afp - open PHYLIP format MSA file
132 * *ret_type - RETURN: <eslDNA>, <eslRNA>, <eslAMINO>; or <eslUNKNOWN>.
133 *
134 * Returns: <eslOK> on success.
135 * <eslENOALPHABET> if autodetection fails.
136 *
137 * Throws: <eslEMEM> on allocation error.
138 * <eslESYS> on failures of fread() or other system calls
139 */
140 int
141 esl_msafile_phylip_GuessAlphabet(ESL_MSAFILE *afp, int *ret_type)
142 {
143 int namewidth = (afp->fmtd.namewidth ? afp->fmtd.namewidth : 10); /* default: strict PHYLIP, namewidth 10 */
144 int alphatype = eslUNKNOWN;
145 int threshold[3] = { 500, 5000, 50000 }; /* we check after 500, 5000, 50000 residues; else we go to EOF */
146 int nsteps = 3; /* ...there's 3 threshold[]'s we check */
147 int step = 0; /* ...starting on threshold[0] */
148 int64_t nres = 0;
149 char *p;
150 esl_pos_t n, pos;
151 int x;
152 esl_pos_t anchor;
153 int64_t ct[26];
154 int status;
155
156 for (x = 0; x < 26; x++) ct[x] = 0;
157
158 anchor = esl_buffer_GetOffset(afp->bf);
159 if ((status = esl_buffer_SetAnchor(afp->bf, anchor)) != eslOK) { status = eslEINCONCEIVABLE; goto ERROR; } /* [eslINVAL] can't happen here */
160
161 /* Find the first nonblank line, which says " <nseq> <alen>" and may also have options. we ignore this header */
162 while ( (status = esl_buffer_GetLine(afp->bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
163 if (status == eslEOF) ESL_XFAIL(eslENOALPHABET, afp->errmsg, "can't determine alphabet: no alignment data found");
164 else if (status != eslOK) goto ERROR;
165
166 /* Read line by line, just looking for residues, not worrying about nseq/alen or sequential/interleaved
167 * Always skip the name field, even in continuation lines/blocks
168 * This may miss some residues, but it means we work on both sequential and interleaved formats
169 */
170 while ( (status = esl_buffer_GetLine(afp->bf, &p, &n)) == eslOK)
171 {
172 if (esl_memspn(p, n, " \t") == n) continue;
173 if (n < namewidth) continue;
174
175 p += namewidth;
176 n -= namewidth;
177
178 /* count characters into ct[] array */
179 for (pos = 0; pos < n; pos++)
180 if (isalpha(p[pos])) {
181 x = toupper(p[pos]) - 'A';
182 ct[x]++;
183 nres++;
184 }
185
186 /* try to stop early, checking after 500, 5000, and 50000 residues: */
187 if (step < nsteps && nres > threshold[step]) {
188 if ((status = esl_abc_GuessAlphabet(ct, &alphatype)) == eslOK) goto DONE; /* (eslENOALPHABET) */
189 step++;
190 }
191 }
192 if (status != eslEOF) goto ERROR; /* [eslEMEM,eslESYS,eslEINCONCEIVABLE] */
193 status = esl_abc_GuessAlphabet(ct, &alphatype); /* (eslENOALPHABET) */
194
195 DONE:
196 esl_buffer_SetOffset(afp->bf, anchor); /* Rewind to where we were. */
197 esl_buffer_RaiseAnchor(afp->bf, anchor);
198 *ret_type = alphatype;
199 return status;
200
201 ERROR:
202 if (anchor != -1) {
203 esl_buffer_SetOffset(afp->bf, anchor);
204 esl_buffer_RaiseAnchor(afp->bf, anchor);
205 }
206 *ret_type = eslUNKNOWN;
207 return status;
208 }
209
210 /* Function: esl_msafile_phylip_Read()
211 * Synopsis: Read in a PHYLIP format alignment.
212 *
213 * Purpose: Read an MSA from an open <ESL_MSAFILE> <afp>, parsing for
214 * Phylip format, starting from the current point. The
215 * format may be either the interleaved or sequential
216 * variant, according to the format set in <afp->format>:
217 * <eslMSAFILE_PHYLIP> means interleaved, and
218 * <eslMSAFILE_PHYLIPS> means sequential. Create a new
219 * multiple alignment, and return a ptr to that alignment
220 * in <*ret_msa>. Caller is responsible for free'ing this
221 * <ESL_MSA>.
222 *
223 * Args: afp - open <ESL_MSAFILE>
224 * ret_msa - RETURN: newly parsed <ESL_MSA>
225 *
226 * Returns: <eslOK> on success.
227 *
228 * <eslEOF> if no (more) alignment data were found in
229 * <afp>, and <afp> is returned at EOF.
230 *
231 * <eslEFORMAT> on a parse error. <*ret_msa> is set to
232 * <NULL>. <afp> contains information sufficient for
233 * constructing useful diagnostic output:
234 * | <afp->errmsg> | user-directed error message |
235 * | <afp->linenumber> | line # where error was detected |
236 * | <afp->line> | offending line (not NUL-term) |
237 * | <afp->n> | length of offending line |
238 * | <afp->bf->filename> | name of the file |
239 * and <afp> is poised at the start of the following line,
240 * so (in principle) the caller could try to resume
241 * parsing.
242 *
243 * Throws: <eslEMEM> - an allocation failed.
244 * <eslESYS> - a system call such as fread() failed
245 * <eslEINCONCEIVABLE> - "impossible" corruption
246 */
247 int
248 esl_msafile_phylip_Read(ESL_MSAFILE *afp, ESL_MSA **ret_msa)
249 {
250 ESL_MSA *msa = NULL;
251 int32_t alen_stated; /* int32_t because we're using strtoi32() to parse it from the file */
252 int nseq;
253 char *p, *tok;
254 esl_pos_t n, toklen;
255 int status;
256
257 ESL_DASSERT1( (afp->format == eslMSAFILE_PHYLIP || afp->format == eslMSAFILE_PHYLIPS) );
258
259 afp->errmsg[0] = '\0';
260
261 /* skip leading blank lines (though there shouldn't be any) */
262 while ( (status = esl_msafile_GetLine(afp, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
263 if (status != eslOK) goto ERROR; /* includes normal EOF */
264
265 /* the first line: <nseq> <alen> */
266 esl_memtok(&p, &n, " \t", &tok, &toklen);
267 if (esl_mem_strtoi32(tok, toklen, 0, NULL, &nseq) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "first PHYLIP line should be <nseq> <alen>: first field isn't an integer");
268 if (esl_memtok(&p, &n, " \t", &tok, &toklen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "first PHYLIP line should be <nseq> <alen>: only one field found");
269 if (esl_mem_strtoi32(tok, toklen, 0, NULL, &alen_stated) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "first PHYLIP line should be <nseq> <alen>: second field isn't an integer");
270
271 /* believe <nseq> and allocate accordingly */
272 /* don't believe <alen_stated>; use it for validation, after we've parsed everything. */
273 if (afp->abc && (msa = esl_msa_CreateDigital(afp->abc, nseq, -1)) == NULL) { status = eslEMEM; goto ERROR; }
274 if (! afp->abc && (msa = esl_msa_Create( nseq, -1)) == NULL) { status = eslEMEM; goto ERROR; }
275
276 /* load next line, skipping any blank ones (though there shouldn't be any) */
277 do {
278 status = esl_msafile_GetLine(afp, &p, &n);
279 if (status == eslEOF) ESL_XFAIL(eslEFORMAT, afp->errmsg, "no alignment data following PHYLIP header");
280 else if (status != eslOK) goto ERROR;
281 } while (esl_memspn(p, n, " \t") == n); /* idiom for "blank line" */
282
283 /* hand off to interleaved vs. sequential parser for the rest */
284 if (afp->format == eslMSAFILE_PHYLIP) status = phylip_interleaved_Read(afp, msa, nseq, alen_stated);
285 else if (afp->format == eslMSAFILE_PHYLIPS) status = phylip_sequential_Read (afp, msa, nseq, alen_stated);
286 if (status != eslOK) goto ERROR;
287
288 if (( status = esl_msa_SetDefaultWeights(msa)) != eslOK) goto ERROR;
289 *ret_msa = msa;
290 return eslOK;
291
292 ERROR:
293 if (msa) esl_msa_Destroy(msa);
294 *ret_msa = NULL;
295 return status;
296 }
297
298
299 /* Function: esl_msafile_phylip_Write()
300 * Synopsis: Write an MSA to a stream in PHYLIP format.
301 *
302 * Purpose: Write alignment <msa> in PHYLIP format to stream <fp>.
303 * If <format> is <eslMSAFILE_PHYLIP>, write interleaved format;
304 * if <format> is <eslMSAFILE_PHYLIPS>, write sequential format.
305 *
306 * Optionally, caller may pass additional formatting
307 * information by passing a ptr to a valid <opt_fmtd>
308 * structure. <opt_fmtd->namewidth> sets the width of the
309 * name field. For strict PHYLIP format, this must be 10.
310 * <opt_fmtd->rpl> sets the number of residues per line.
311 * The default is 60. For either value, if it is unset or
312 * if <opt_fmtd> is <NULL>, the default is used.
313 *
314 * Args: fp - open output stream
315 * msa - alignment to write
316 * format - <eslMSAFILE_PHYLIP> for interleaved; <eslMSAFILE_PHYLIPS> for sequential.
317 * fmtd - optional: <NULL>, or additional format information
318 *
319 * Returns: <eslOK> on success.
320 *
321 * Throws: <eslEINVAL> if <format> isn't <eslMSAFILE_PHYLIP> or <eslMSAFILE_PHYLIPS>.
322 * <eslEMEM> on allocation failure.
323 * <eslEWRITE> on any system write error, such as a filled disk.
324 */
325 int
326 esl_msafile_phylip_Write(FILE *fp, const ESL_MSA *msa, int format, ESL_MSAFILE_FMTDATA *opt_fmtd)
327 {
328 if (format == eslMSAFILE_PHYLIP) return phylip_interleaved_Write(fp, msa, opt_fmtd);
329 else if (format == eslMSAFILE_PHYLIPS) return phylip_sequential_Write (fp, msa, opt_fmtd);
330 else ESL_EXCEPTION(eslEINVAL, "format %s is not a PHYLIP format", esl_msafile_DecodeFormat(format));
331 }
332
333 /* Function: esl_msafile_phylip_CheckFileFormat()
334 * Synopsis: Check whether an input seems to be in PHYLIP format.
335 *
336 * Purpose: Check whether input buffer <bf> appears to be
337 * in a PHYLIP format, starting from the current point.
338 * Return <eslOK> if it is, and <eslFAIL> if it isn't.
339 *
340 * There are two main variants of the format, interleaved
341 * and sequential. Upon successful return, <*ret_format> is
342 * set to <eslMSAFILE_PHYLIP> for interleaved, or
343 * <eslMSAFILE_PHYLIPS> for sequential.
344 *
345 * Strict PHYLIP format has a name/identifier field width
346 * of exactly 10 characters, but variants of the format are
347 * in common use with different name widths. The
348 * name width is determined and returned in <*ret_namewidth>.
349 *
350 * If the input doesn't appear to be in PHYLIP format,
351 * return <eslFAIL>, with <*ret_format> as
352 * <eslMSAFILE_UNKNOWN> and <*ret_namewidth> as 0.
353 *
354 * The PHYLIP format definition is ambiguous; it is
355 * possible to construct pathological inputs that could be
356 * validly parsed to yield different data when read as
357 * interleaved versus sequential. (This requires names that
358 * use a character set that looks like sequence, among
359 * other unusual edge conditions.) If the format variant
360 * cannot be unambiguously determined, we do not attempt to
361 * make a guess that might result in corrupted input;
362 * rather, return <eslEAMBIGUOUS>.
363 *
364 * Args: bf - input buffer
365 * *ret_format - RETURN: format variant, <eslMSAFILE_PHYLIP>, <_PHYLIPS>, <_UNKNOWN>
366 * *ret_namewidth - RETURN: width of name field, in characters
367 *
368 * Returns: <eslOK> if input is in PHYLIP format; <*ret_format> is set to
369 * <eslMSAFILE_PHYLIP> or <eslMSAFILE_PHYLIPS>; <*ret_namewidth>
370 * is the name width, either the usual 10, or something nonstandard.
371 *
372 * <eslEAMBIGUOUS> if the input appears to be in a PHYLIP format but
373 * we can't tell which one. <*ret_format> is <eslMSAFILE_UNKNOWN>,
374 * <*ret_namewidth> is 0.
375 *
376 * <eslFAIL> if the input is not in either PHYLIP format;
377 * <*ret_format> is <eslMSAFILE_UNKNOWN>, <*ret_namewidth> is 0.
378 *
379 * Throws: (no abnormal error conditions)
380 */
381 int
382 esl_msafile_phylip_CheckFileFormat(ESL_BUFFER *bf, int *ret_format, int *ret_namewidth)
383 {
384 int maybe_interleaved = FALSE; // Must worry about pathological files consistent with both formats.
385 int maybe_sequential = FALSE;
386 int nblocks; // number of interleaved blocks. if only 1, interleaved == sequential
387 int w1, w2; // name widths if interleaved, sequential
388
389 if ( phylip_check_interleaved(bf, &nblocks, &w1) == eslOK) { maybe_interleaved = TRUE; }
390
391 if (maybe_interleaved && nblocks == 1) { *ret_namewidth = w1; *ret_format = eslMSAFILE_PHYLIP; return eslOK; }
392
393 if ( phylip_check_sequential_known (bf, 10) == eslOK) { maybe_sequential = TRUE; w2 = 10; }
394 else if ( phylip_check_sequential_unknown(bf, &w2) == eslOK) { maybe_sequential = TRUE; }
395
396 if (maybe_interleaved && maybe_sequential) { *ret_namewidth = 0; *ret_format = eslMSAFILE_UNKNOWN; return eslEAMBIGUOUS; }
397 else if (maybe_interleaved) { *ret_namewidth = w1; *ret_format = eslMSAFILE_PHYLIP; return eslOK; }
398 else if (maybe_sequential) { *ret_namewidth = w2; *ret_format = eslMSAFILE_PHYLIPS; return eslOK; }
399 else { *ret_namewidth = 0; *ret_format = eslMSAFILE_UNKNOWN; return eslFAIL; }
400 }
401 /*-------------- end, API for PHYLIP format i/o -----------------*/
402
403
404 /*****************************************************************
405 * 2. i/o of the interleaved variant of the format
406 *****************************************************************/
407
408 /* Read the interleaved variant.
409 * header told us to expect <nseq>, <alen_stated>.
410 * <msa> is already allocated <nseq>, <-1>.
411 * In <afp>, we've loaded the first line of the alignment data.
412 */
413 static int
414 phylip_interleaved_Read(ESL_MSAFILE *afp, ESL_MSA *msa, int nseq, int32_t alen_stated)
415 {
416 int namewidth = (afp->fmtd.namewidth ? afp->fmtd.namewidth : 10); /* default: strict PHYLIP, namewidth 10 */
417 char *namebuf = NULL;
418 int nblocks = 0;
419 int64_t alen = 0; /* alignment length observed so far */
420 char *p = afp->line;
421 esl_pos_t n = afp->n;
422 int64_t block_alen; /* # of residues being added by current block */
423 int64_t cur_alen;
424 int idx;
425 int status;
426
427 ESL_ALLOC(namebuf, sizeof(char) * (namewidth+1));
428
429 /* p, n is now the first line of a block */
430 /* read the alignment data */
431 do {
432 idx = 0;
433 do { /* First block? store the sequence names */
434 if (nblocks == 0)
435 {
436 if (n < namewidth) ESL_XFAIL(eslEFORMAT, afp->errmsg, "PHYLIP line too short to find sequence name");
437 if (phylip_rectify_input_name(namebuf, p, namewidth) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "invalid character(s) in sequence name");
438 if ( (status = esl_msa_SetSeqName(msa, idx, namebuf, -1)) != eslOK) goto ERROR;
439 p += namewidth;
440 n -= namewidth;
441 }
442
443 /* Append the sequence. */
444 cur_alen = alen;
445 if (msa->abc) { status = esl_abc_dsqcat(afp->inmap, &(msa->ax[idx]), &(cur_alen), p, n); }
446 if (! msa->abc) { status = esl_strmapcat (afp->inmap, &(msa->aseq[idx]), &(cur_alen), p, n); }
447 if (status == eslEINVAL) ESL_XFAIL(eslEFORMAT, afp->errmsg, "one or more invalid sequence characters");
448 else if (status != eslOK) goto ERROR;
449
450 /* validate # of residues added */
451 if (idx == 0) block_alen = cur_alen - alen;
452 else if (cur_alen - alen != block_alen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "number of residues on line differs from previous seqs in alignment block");
453
454 /* get next line. */
455 idx++;
456 status = esl_msafile_GetLine(afp, &p, &n);
457 } while (status == eslOK && idx < nseq && esl_memspn(p, n, " \t") < n); /* stop block on: read error, EOF; idx == nseq; or blank line */
458
459 if (idx != nseq) ESL_XFAIL(eslEFORMAT, afp->errmsg, "unexpected number of sequences in block (saw %d, expected %d)", idx, nseq);
460 nblocks += 1;
461 alen += block_alen;
462
463 /* tolerate blank lines only at the end of a block */
464 while (status == eslOK && esl_memspn(p, n, " \t") == n)
465 status = esl_msafile_GetLine(afp, &p, &n); /* [eslEMEM,eslESYS] */
466 /* now status can be: read error, EOF, or OK. */
467 } while (status == eslOK && alen < alen_stated);
468
469 /* End of all blocks. We swallowed blank lines following last block,
470 * so we should be EOF; unless we're in a seqboot file, in which
471 * case we just read the first line of the next MSA.
472 */
473 if (status == eslOK) esl_msafile_PutLine(afp); /* put <nseq> <alen> line back in the stream, so we will read it properly w/ next msa read */
474 else if (status != eslEOF) goto ERROR;
475 else if (alen != alen_stated) ESL_XFAIL(eslEFORMAT, afp->errmsg, "alignment length disagrees with header: header said %d, parsed %" PRId64, alen_stated, alen);
476
477 msa->nseq = nseq;
478 msa->alen = alen;
479 free(namebuf);
480 return eslOK;
481
482 ERROR:
483 msa->nseq = nseq; /* we're allocated and initialized for <nseq>: this makes sure we free everything we need to in <msa> */
484 if (namebuf) free(namebuf);
485 return status;
486 }
487
488 /* Write an interleaved PHYLIP file.
489 * Returns <eslOK> on success.
490 * Throws <eslEWRITE> on any system write error.
491 */
492 static int
493 phylip_interleaved_Write(FILE *fp, const ESL_MSA *msa, ESL_MSAFILE_FMTDATA *opt_fmtd)
494 {
495 int rpl = ( (opt_fmtd && opt_fmtd->rpl) ? opt_fmtd->rpl : 60);
496 int namewidth = ( (opt_fmtd && opt_fmtd->namewidth) ? opt_fmtd->namewidth : 10);
497 char *buf = NULL;
498 int idx;
499 int64_t apos;
500 int status;
501
502 ESL_ALLOC(buf, sizeof(char) * (rpl+1));
503 buf[rpl] = '\0';
504
505 if (fprintf(fp, " %d %" PRId64, msa->nseq, msa->alen) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "interleaved phylip write failed");
506
507 for (apos = 0; apos < msa->alen; apos += rpl)
508 {
509 if (fprintf(fp, "\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "interleaved phylip write failed");
510 for (idx = 0; idx < msa->nseq; idx++)
511 {
512 if (msa->abc)
513 {
514 esl_abc_TextizeN(msa->abc, msa->ax[idx]+apos+1, rpl, buf);
515 phylip_rectify_output_seq_digital(buf);
516 }
517 if (! msa->abc)
518 {
519 strncpy(buf, msa->aseq[idx]+apos, rpl);
520 phylip_rectify_output_seq_text(buf);
521 }
522
523 if (apos == 0) { if (fprintf(fp, "%-*.*s %s\n", namewidth, namewidth, msa->sqname[idx], buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "interleaved phylip write failed"); }
524 else { if (fprintf(fp, "%s\n", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "interleaved phylip write failed"); }
525 }
526 }
527 free(buf);
528 return eslOK;
529
530 ERROR:
531 if (buf) free(buf);
532 return status;
533 }
534 /*----------------- end, interleaved variant -------------------*/
535
536
537 /*****************************************************************
538 * 3. i/o of sequential variant of the format
539 *****************************************************************/
540
541 /* Read the sequential variant.
542 * header told us to expect <nseq>, <alen_stated>.
543 * <msa> is already allocated <nseq>, <-1>.
544 * In <afp>, we've loaded the first line of the alignment data.
545 */
546 static int
547 phylip_sequential_Read(ESL_MSAFILE *afp, ESL_MSA *msa, int nseq, int32_t alen_stated)
548 {
549 int namewidth = (afp->fmtd.namewidth ? afp->fmtd.namewidth : 10); /* default: strict PHYLIP, namewidth 10 */
550 char *namebuf = NULL;
551 char *p = afp->line;
552 esl_pos_t n = afp->n;
553 int idx;
554 int64_t alen = 0;
555 int status = eslOK;
556
557 ESL_ALLOC(namebuf, sizeof(char) * (namewidth+1));
558
559 for (idx = 0; idx < nseq; idx++)
560 {
561 alen = 0;
562 status = eslOK;
563 while (status == eslOK && alen < alen_stated)
564 {
565 if (alen == 0) /* First line? Store the sequence name */
566 {
567 if (n < namewidth) ESL_XFAIL(eslEFORMAT, afp->errmsg, "PHYLIP line too short to find sequence name");
568 if (phylip_rectify_input_name(namebuf, p, namewidth) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "invalid character(s) in sequence name");
569 if ( (status = esl_msa_SetSeqName(msa, idx, namebuf, -1)) != eslOK) goto ERROR;
570 p += namewidth;
571 n -= namewidth;
572 }
573
574 if (msa->abc) { status = esl_abc_dsqcat(afp->inmap, &(msa->ax[idx]), &alen, p, n); }
575 if (! msa->abc) { status = esl_strmapcat (afp->inmap, &(msa->aseq[idx]), &alen, p, n); }
576 if (status == eslEINVAL) ESL_XFAIL(eslEFORMAT, afp->errmsg, "one or more invalid sequence characters");
577 else if (status != eslOK) goto ERROR;
578
579 /* get next line */
580 status = esl_msafile_GetLine(afp, &p, &n);
581 } /* end looping over a seq */
582
583 /* tolerate blank lines after sequences. */
584 while (status == eslOK && esl_memspn(p, n, " \t") == n)
585 status = esl_msafile_GetLine(afp, &p, &n);
586
587 if (status == eslEOF) { if (idx < nseq-1) ESL_XFAIL(eslEFORMAT, afp->errmsg, "premature end of file: header said to expect %d sequences", nseq); }
588 else if (status != eslOK) goto ERROR;
589 else if (alen != alen_stated) ESL_XFAIL(eslEFORMAT, afp->errmsg, "aligned length of sequence disagrees with header: header says %d, parsed %" PRId64, alen_stated, alen);
590 } /* end looping over all seqs */
591
592 /* we should be EOF; we've swallowed all trailing blank lines.
593 * Exception: if we're in a seqboot file, we just read the <nseq> <alen> line of the next msa record; push it back on stream
594 */
595 if (status == eslOK) esl_msafile_PutLine(afp);
596
597 msa->nseq = nseq;
598 msa->alen = alen;
599 free(namebuf);
600 return eslOK;
601
602 ERROR:
603 msa->nseq = nseq; /* we're allocated and initialized for <nseq>: this makes sure we free everything we need to in <msa> */
604 if (namebuf) free(namebuf);
605 return status;
606 }
607
608 static int
609 phylip_sequential_Write(FILE *fp, const ESL_MSA *msa, ESL_MSAFILE_FMTDATA *opt_fmtd)
610 {
611 int rpl = ( (opt_fmtd && opt_fmtd->rpl) ? opt_fmtd->rpl : 60);
612 int namewidth = ( (opt_fmtd && opt_fmtd->namewidth) ? opt_fmtd->namewidth : 10);
613 char *buf = NULL;
614 int idx;
615 int64_t apos;
616 int status;
617
618 ESL_ALLOC(buf, sizeof(char) * (rpl+1));
619 buf[rpl] = '\0';
620
621 fprintf(fp, " %d %" PRId64 "\n", msa->nseq, msa->alen);
622
623 for (idx = 0; idx < msa->nseq; idx++)
624 {
625 for (apos = 0; apos < msa->alen; apos += rpl)
626 {
627 if (msa->abc)
628 {
629 esl_abc_TextizeN(msa->abc, msa->ax[idx]+apos+1, rpl, buf);
630 phylip_rectify_output_seq_digital(buf);
631 }
632 if (! msa->abc)
633 {
634 strncpy(buf, msa->aseq[idx]+apos, rpl);
635 phylip_rectify_output_seq_text(buf);
636 }
637
638 if (apos == 0) fprintf(fp, "%-*.*s %s\n", namewidth, namewidth, msa->sqname[idx], buf);
639 else fprintf(fp, "%s\n", buf);
640 }
641 }
642 free(buf);
643 return eslOK;
644
645 ERROR:
646 if (buf) free(buf);
647 return status;
648 }
649 /*------------------ end, sequential variant --------------------*/
650
651
652 /*****************************************************************
653 * 4. Autodetection of the format and its variants
654 *****************************************************************/
655
656 /* return <eslOK> if input is consistent with interleaved format;
657 * set <*ret_namewidth> to the apparent name width;
658 * set <*ret_nblocks> to the number of interleaved blocks seen.
659 *
660 * If <nblocks=1>, it doesn't matter whether we read the file as
661 * interleaved or sequential; it will also appear to be consistent
662 * with sequential format, but we will parse it as interleaved.
663 *
664 * <namewidth> can be uniquely determined because we know <nseq> and
665 * <alen> (from the Phylip header), so we know how many characters
666 * should be accounted for by the alignment; the rest have to be
667 * the names.
668 *
669 * If the input is not consistent with interleaved Phylip format,
670 * return <eslFAIL>, and <*ret_namewidth> and <*ret_nblocks> are 0.
671 *
672 * upon return, restore the <bf> to its original position.
673 */
674 static int
675 phylip_check_interleaved(ESL_BUFFER *bf, int *ret_nblocks, int *ret_namewidth)
676 {
677 esl_pos_t anchor = -1;
678 char *p;
679 esl_pos_t n;
680 int32_t nseq, alen;
681 char *colcodes = NULL;
682 char *colcodes0 = NULL;
683 int ncols, ncols0;
684 int c, idx;
685 int nblocks = 0;
686 int nres2 = 0;
687 int nres1;
688 int status;
689
690 anchor = esl_buffer_GetOffset(bf);
691 if (esl_buffer_SetAnchor(bf, anchor) != eslOK) { status = eslFAIL; goto ERROR; }
692 if ((status = phylip_parse_header(bf, &nseq, &alen, &p, &n)) != eslOK) goto ERROR;
693
694 /* read the whole file, one block at a time */
695 while (status == eslOK)
696 {
697 ncols = n;
698 ESL_REALLOC(colcodes, sizeof(char) * ncols);
699 for (c = 0; c < ncols; c++) colcodes[c] = '?';
700
701 /* read a block: for each line, update the colcodes[] array. */
702 for (idx = 0; idx < nseq; idx++)
703 {
704 if (status == eslEOF) goto ERROR;
705 if ((status = phylip_collate_colcodes(p, n, colcodes, ncols)) != eslOK) goto ERROR;
706 status = esl_buffer_GetLine(bf, &p, &n);
707 if (status != eslOK && status != eslEOF) goto ERROR; /* EOF is ok on last seq of single-block data */
708 }
709
710 /* finished a block. status may be EOF or OK.
711 * save the first block's colcodes[] to defer its analysis
712 * for all other blocks: count x columns, reject n columns, ignore . and o
713 */
714 if (nblocks == 0)
715 {
716 colcodes0 = colcodes; ncols0 = ncols;
717 colcodes = NULL;
718 /* let's speculate that it's strictly conforming PHYLIP with a namewidth of 10
719 * in that case, we'll be able to stop parsing blocks when we reach the full
720 * alignment length
721 */
722 for (nres1 = 0, c = 10; c < ncols0; c++)
723 if (colcodes0[c] != '.') nres1++; /* for this test, consider even invalid symbols to be "residues". */
724 }
725 else
726 {
727 for (c = 0; c < ncols; c++)
728 {
729 if (colcodes[c] == 'x') nres2++;
730 if (colcodes[c] == 'n') { status = eslFAIL; goto ERROR; } /* subsequent blocks can't contain name-like columns */
731 }
732 }
733 nblocks++;
734
735 /* if it's strictly conforming w/ namewidth=10, we know when we're done,
736 * and in that case we can tolerate trailing data (tree, whatever) in the
737 * input. status will be eslOK if we break this way.
738 */
739 if (nres1+nres2 == alen) break;
740
741 /* skip blank lines until we load start of next block in <p>, <n> */
742 while (status == eslOK && esl_memspn(p, n, "\t") == n)
743 status = esl_buffer_GetLine(bf, &p, &n);
744 }
745
746 if (nres1+nres2 == alen) *ret_namewidth = 10;
747 else if ((status = phylip_deduce_namewidth(colcodes0, ncols0, alen, nres2, ret_namewidth)) != eslOK) goto ERROR;
748
749 free(colcodes);
750 free(colcodes0);
751 esl_buffer_SetOffset(bf, anchor);
752 esl_buffer_RaiseAnchor(bf, anchor);
753 *ret_nblocks = nblocks;
754 return eslOK;
755
756 ERROR:
757 if (anchor != -1) {
758 esl_buffer_SetOffset(bf, anchor);
759 esl_buffer_RaiseAnchor(bf, anchor);
760 }
761 if (colcodes) free(colcodes);
762 if (colcodes0) free(colcodes0);
763 *ret_namewidth = 0;
764 *ret_nblocks = 0;
765 return status;
766 }
767
768
769 /* check for a sequential format file, given a known namewidth (10, for strict PHYLIP) */
770 static int
771 phylip_check_sequential_known(ESL_BUFFER *bf, int namewidth)
772 {
773 esl_pos_t anchor = -1;
774 char *p;
775 esl_pos_t n;
776 int32_t nseq, alen;
777 int idx, nres, line, c;
778 int status;
779
780 anchor = esl_buffer_GetOffset(bf);
781 if (esl_buffer_SetAnchor(bf, anchor) != eslOK) { status = eslFAIL; goto ERROR; }
782 if ((status = phylip_parse_header(bf, &nseq, &alen, &p, &n)) != eslOK) goto ERROR;
783
784 for (idx = 0; idx < nseq; idx++)
785 {
786 nres = 0;
787 line = 0;
788 while (nres < alen)
789 {
790 if (status == eslEOF) goto ERROR;
791
792 c = (line == 0 ? namewidth : 0);
793 for ( ; c < n; c++)
794 if (strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[c]) != NULL)
795 nres++;
796
797 status = esl_buffer_GetLine(bf, &p, &n);
798 if (status != eslOK && status != eslEOF) goto ERROR;
799 }
800 if (nres != alen) { status = eslFAIL; goto ERROR; }
801
802 /* tolerate blank spaces after individual sequences */
803 while (status == eslOK && esl_memspn(p, n, "\t") == n)
804 status = esl_buffer_GetLine(bf, &p, &n);
805 if (status != eslOK && status != eslEOF) goto ERROR;
806 }
807
808 esl_buffer_SetOffset(bf, anchor);
809 esl_buffer_RaiseAnchor(bf, anchor);
810 return eslOK;
811
812 ERROR:
813 if (anchor != -1) {
814 esl_buffer_SetOffset(bf, anchor);
815 esl_buffer_RaiseAnchor(bf, anchor);
816 }
817 return status;
818 }
819
820
821 /* phylip_check_sequential_unknown()
822 * Check for sequential format when namewidth may deviate from standard 10.
823 *
824 * Returns: <eslOK> if file is consistent with sequential Phylip format,
825 * and <*ret_namewidth> is the name width.
826 *
827 * <eslFAIL> if file is inconsistent with sequential format.
828 *
829 * Throws: <eslEMEM> on allocation failure
830 */
831 static int
832 phylip_check_sequential_unknown(ESL_BUFFER *bf, int *ret_namewidth)
833 {
834 esl_pos_t anchor = -1;
835 int nlines = 0;
836 int nblocks = 0;
837 int32_t nseq, alen;
838 int *rth = NULL; // rth[i=0..L1-1] = # of legal chars, inclusive, in i..L1-1, or 0
839 int r; // total # of legal seq chars on line 1 of a seq
840 int L1; // total length of line 1, including all chars & whitespace
841 int b; // # of legal seq chars on remainder of lines of a seq
842 int a; // # of seq chars we need on line 1: alen - b
843 char *p; // pointer to current line in input buffer
844 esl_pos_t n; // length of current line in input buffer
845 int i, j, k;
846 int w; // name width we determine
847 int len2; // alen of subsequent seqs in file
848 int status;
849
850 /* Set an anchor where we are in the input, so we can rewind */
851 anchor = esl_buffer_GetOffset(bf);
852 if ( esl_buffer_SetStableAnchor(bf, anchor) != eslOK) { status = eslFAIL; goto ERROR; }
853
854 /* Pass 1: number of data lines in file / nseq = integral # of lines per sequence */
855 while ( (status = esl_buffer_GetLine(bf, &p, &n)) == eslOK)
856 if (esl_memspn(p, n, " \t") != n) nlines++;
857 if (status != eslEOF) { status = eslFAIL; goto ERROR; }
858 nlines--; /* not counting the <nseq> <alen> header */
859 esl_buffer_SetOffset(bf, anchor); /* rewind */
860
861 /* pass 2: first, parse the <nseq> <alen> line */
862 if ((status = phylip_parse_header(bf, &nseq, &alen, &p, &n)) != eslOK) { status = eslFAIL; goto ERROR; }
863 if (nlines % nseq != 0) { status = eslFAIL; goto ERROR; }
864 nblocks = nlines / nseq;
865
866 /* ... evaluate the first line, already at point in p,n */
867 ESL_ALLOC(rth, sizeof(int) * n);
868 for (i = n-1, r = 0; i >= 0; i--)
869 rth[i] = ( strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[i]) != NULL ? ++r : 0 );
870 L1 = n;
871
872 /* rth[i] = 0 if line1[i] is not a legal seq char
873 * else # of legal chars in line1[i..L1-1]
874 * r = total # of legal chars on line 1
875 * L1 = total length of line 1, all chars (including whitespace)
876 * Once we know how many seq residues we need to get on the first line,
877 * we can use rth[] to find the position of the first one.
878 */
879
880 /* Now count b: # of legal chars on subsequent lines for 1st seq */
881 for (k = 1, b = 0; k < nblocks; k++)
882 {
883 while ( (status = esl_buffer_GetLine(bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
884 if (status != eslOK) { status = eslFAIL; goto ERROR; }
885 for (i = 0; i < n; i++)
886 if (strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[i]) != NULL) b++;
887 }
888
889 /* We need to find (alen - b) = a residues on the first line. */
890 a = alen - b;
891 if (a > r) { status = eslFAIL; goto ERROR; }
892 for (w = 0; w < L1; w++) // find the leftmost seq residue
893 if (rth[w] == a) break;
894 if (w == L1) { status = eslFAIL; goto ERROR; }
895 for (i = 0; i < w; i++) // make sure there's a name of some sort
896 if (! isspace(p[i])) break;
897 if ( i == w) { status = eslFAIL; goto ERROR; }
898 /* Now we "know" the name width is w. */
899
900 /* Check that the rest of the sequences (up to 100 of them) are consistent w/ that. */
901 for (j = 1; j < nseq && j < 100; j++)
902 {
903 while ( (status = esl_buffer_GetLine(bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
904 if (status != eslOK) { status = eslFAIL; goto ERROR; }
905 if ( strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[w]) == NULL) { status = eslFAIL; goto ERROR; }
906 for (i = 0; i < w; i++)
907 if (! isspace(p[i])) break;
908 if ( i == w) { status = eslFAIL; goto ERROR; }
909
910 for (i = w, len2 = 0; i < n; i++)
911 if ( strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[i]) != NULL) len2++;
912
913 for (k = 1; k < nblocks; k++)
914 {
915 while ( (status = esl_buffer_GetLine(bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
916 if (status != eslOK) { status = eslFAIL; goto ERROR; }
917 for (i = 0; i < n; i++)
918 if ( strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[i]) != NULL) len2++;
919 }
920 if (len2 != alen) { status = eslFAIL; goto ERROR; }
921 }
922
923 esl_buffer_SetOffset(bf, anchor);
924 esl_buffer_RaiseAnchor(bf, anchor);
925 free(rth);
926 *ret_namewidth = w;
927 return eslOK;
928
929 ERROR:
930 if (anchor != -1) {
931 esl_buffer_SetOffset(bf, anchor);
932 esl_buffer_RaiseAnchor(bf, anchor);
933 }
934 free(rth);
935 *ret_namewidth = 0;
936 return status;
937 }
938
939
940 /* parse the header from a buffer (note, NOT an open MSAFILE).
941 * used by the format checkers (before an MSAFILE is open)
942 * return <eslOK> on success, <eslFAIL> on parse failure.
943 *
944 * Upon return <p>, <n> contains the first alignment data line,
945 * and the <bf>'s point is on the line following that.
946 *
947 */
948 static int
949 phylip_parse_header(ESL_BUFFER *bf, int32_t *ret_nseq, int32_t *ret_alen, char **ret_p, esl_pos_t *ret_n)
950 {
951 char *p, *tok;
952 esl_pos_t n, toklen;
953 int32_t nseq, alen;
954 int status;
955
956 /* Find the first nonblank line, which says " <nseq> <alen>" and may also have options (which we'll ignore) */
957 while ( (status = esl_buffer_GetLine(bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
958 if (status == eslEOF) status = eslFAIL;
959 if (status != eslOK) goto ERROR;
960
961 esl_memtok(&p, &n, " \t", &tok, &toklen);
962 if (esl_mem_strtoi32(tok, toklen, 0, NULL, &nseq) != eslOK) { status = eslFAIL; goto ERROR; }
963 if (esl_memtok(&p, &n, " \t", &tok, &toklen) != eslOK) { status = eslFAIL; goto ERROR; }
964 if (esl_mem_strtoi32(tok, toklen, 0, NULL, &alen) != eslOK) { status = eslFAIL; goto ERROR; }
965
966 /* skip any blank lines, load p,n as first line of putative block */
967 while ( (status = esl_buffer_GetLine(bf, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ;
968 if (status == eslEOF) status = eslFAIL;
969 if (status != eslOK) goto ERROR;
970
971 *ret_nseq = nseq;
972 *ret_alen = alen;
973 *ret_p = p;
974 *ret_n = n;
975 return eslOK;
976
977 ERROR:
978 *ret_nseq = 0;
979 *ret_alen = 0;
980 *ret_p = NULL;
981 *ret_n = 0;
982 return status;
983 }
984
985
986 /*
987 * '?' : unset
988 * 'x' : column consists only of valid data symbols
989 * '.' : column consists only of spaces
990 * 'o' : column consists only of spaces or other (invalid) symbols such as numbers
991 * 'n' : column mixes x and (. or o): must be a name column
992 *
993 * A name can contain spaces, valid symbols, other symbols ('x', 'n', '.', 'o')
994 * Any whitespace between name, data must contain only '.'
995 * Spacer columns elsewhere can contain spaces or other symbols (such as numbers): 'o'
996 * Alignment columns must contain only valid data symbols: 'x'.
997 */
998 static int
999 phylip_collate_colcodes(char *p, esl_pos_t n, char *colcodes, int ncols)
1000 {
1001 int c;
1002
1003 for (c = 0; c < ncols && c < n; c++)
1004 {
1005 if (strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[c]) != NULL)
1006 {
1007 switch (colcodes[c]) {
1008 case '?': colcodes[c] = 'x'; break;
1009 case '.': colcodes[c] = 'n'; break;
1010 }
1011 }
1012 else if (p[c] == ' ')
1013 {
1014 switch (colcodes[c]) {
1015 case '?': colcodes[c] = '.'; break;
1016 case 'x': colcodes[c] = 'n'; break;
1017 }
1018 }
1019 else if (isgraph(p[c]))
1020 {
1021 switch (colcodes[c]) {
1022 case '?': colcodes[c] = 'o'; break;
1023 case 'x': colcodes[c] = 'n'; break;
1024 case '.': colcodes[c] = 'o'; break;
1025 }
1026 }
1027 else return eslFAIL;
1028 }
1029 for ( ; c < n; c++)
1030 if (strchr(eslMSAFILE_PHYLIP_LEGALSYMS, p[c]) != NULL) return eslFAIL;
1031
1032 return eslOK;
1033 }
1034
1035 static int
1036 phylip_deduce_namewidth(char *colcodes0, int ncols0, int alen, int nres2, int *ret_namewidth)
1037 {
1038 int nres1;
1039 int c;
1040 int nwA, nwB, namewidth;
1041
1042 /* nres1 = alen - nres2 : # of columns that must be provided in first block */
1043 nres1 = alen - nres2;
1044 if (nres1 <= 0) return eslFAIL;
1045
1046 /* search leftward in first block until we've accounted for enough cols */
1047 /* c becomes the position of the rightmost col before ali data, and hence = maximal namewidth-1 */
1048 for (c = ncols0-1; nres1 && c >= 0; c--)
1049 if (colcodes0[c] == 'x') nres1--;
1050 if (nres1 > 0) return eslFAIL;
1051 nwB = c+1;
1052
1053 /* search leftward past whitespace columns that might be trailing the name */
1054 /* c becomes the position of the rightmost non-whitespace column, and hence = minimal namewidth-1 */
1055 for ( ; c >= 0; c--)
1056 if (colcodes0[c] != '.') break;
1057 nwA = c+1;
1058
1059 /* if nwA <= 10 <= nwB, the namewidth is consistent with strict PHYLIP namewidth of 10 */
1060 if (nwA <= 10 && nwB >= 10) namewidth = 10;
1061 else namewidth = nwA;
1062
1063 *ret_namewidth = namewidth;
1064 return eslOK;
1065 }
1066 /*-------------- end, format autodetection ----------------------*/
1067
1068
1069 /*****************************************************************
1070 * 5. Rectifying valid input/output symbols in names and seqs
1071 *****************************************************************/
1072
1073 /* We allow any isprint() char in a name, except tabs.
1074 * Phylip allows spaces in names, but Easel doesn't.
1075 * Internal spaces are converted to underscore _
1076 * Leading and trailing spaces are ignored
1077 * namebuf must be allocated at least n+1 chars
1078 * returns eslEINVAL if it sees an invalid character
1079 */
1080 static int
1081 phylip_rectify_input_name(char *namebuf, char *p, int n)
1082 {
1083 int pos, endpos;
1084 int npos = 0;
1085
1086 for (endpos = n-1; endpos > 0; endpos--) if (p[endpos] != ' ') break;
1087 for (pos = 0; pos <= endpos; pos++) if (p[pos] != ' ') break;
1088 for ( ; pos <= endpos; pos++)
1089 {
1090 if (! isgraph(p[pos]) && p[pos] != ' ') return eslEINVAL;
1091 namebuf[npos++] = (p[pos] == ' ' ? '_' : p[pos]);
1092 }
1093 namebuf[npos] = '\0';
1094 return eslOK;
1095 }
1096
1097 /* Easel's internal alphabet (and output syms) are compatible with PHYLIP
1098 * (upper case, '-' for gaps, '*' for stop codon; except that PHYLIP uses
1099 * '?' for missing data
1100 */
1101 static int
1102 phylip_rectify_output_seq_digital(char *buf)
1103 {
1104 int i;
1105 for (i = 0; buf[i]; i++)
1106 {
1107 if (buf[i] == '~') buf[i] = '?';
1108 }
1109 return eslOK;
1110 }
1111
1112 static int
1113 phylip_rectify_output_seq_text(char *buf)
1114 {
1115 int i;
1116 for (i = 0; buf[i]; i++)
1117 {
1118 if (islower(buf[i])) buf[i] = toupper(buf[i]);
1119 if (strchr("._ ", buf[i]) != NULL) buf[i] = '-';
1120 if (buf[i] == '~') buf[i] = '?';
1121 }
1122 return eslOK;
1123 }
1124 /*---------- end, rectification of name, seq symbols ------------*/
1125
1126
1127
1128
1129 /*****************************************************************
1130 * 6. Unit tests
1131 *****************************************************************/
1132 #ifdef eslMSAFILE_PHYLIP_TESTDRIVE
1133 static void
1134 utest_write_good1(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1135 {
1136 fputs(" 5 42\n", ofp);
1137 fputs("Turkey AAGCTNGGGC ATTTCAGGGT\n", ofp);
1138 fputs("Salmo gairAAGCCTTGGC AGTGCAGGGT\n", ofp);
1139 fputs("H. SapiensACCGGTTGGC CGTTCAGGGT\n", ofp);
1140 fputs("Chimp AAACCCTTGC CGTTACGCTT\n", ofp);
1141 fputs("Gorilla AAACCCTTGC CGGTACGCTT\n", ofp);
1142 fputs("\n", ofp);
1143 fputs("GAGCCCGGGC AATACAGGGT AT\n", ofp);
1144 fputs("GAGCCGTGGC CGGGCACGGT AT\n", ofp);
1145 fputs("ACAGGTTGGC CGTTCAGGGT AA\n", ofp);
1146 fputs("AAACCGAGGC CGGGACACTC AT\n", ofp);
1147 fputs("AAACCATTGC CGGTACGCTT AA\n", ofp);
1148
1149 *ret_format = eslMSAFILE_PHYLIP;
1150 *ret_alphatype = eslDNA;
1151 *ret_nseq = 5;
1152 *ret_alen = 42;
1153 }
1154
1155 static void
1156 utest_write_good2(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1157 {
1158 fputs("7 50 \n", ofp);
1159 fputs("thermotogaATGGCGAAGGAAAAATTTGTGAGAACAAAACCGCATGTTAACGTTGGAAC\n", ofp);
1160 fputs("TthermophiATGGCGAAGGGCGAGTTTGTTCGGACGAAGCCTCACGTGAACGTGGGGAC \n", ofp);
1161 fputs("TaquaticusATGGCGAAGGGCGAGTTTATCCGGACGAAGCCCCACGTGAACGTGGGGAC \n", ofp);
1162 fputs("deinonema-ATGGCTAAGGGAACGTTTGAACGCACCAAACCCCACGTGAACGTGGGCAC \n", ofp);
1163 fputs("ChlamydiaBATGTCAAAAGAAACTTTTCAACGTAATAAGCCTCATATCAACATAGGGGC \n", ofp);
1164 fputs("flexistipsATGTCCAAGCAAAAGTACGAAAGGAAGAAACCTCACGTAAACGTAGGCAC \n", ofp);
1165 fputs("borrelia-bATGGCAAAAGAAGTTTTTCAAAGAACAAAGCCGCACATGAATGTTGGAAC \n", ofp);
1166
1167 *ret_format = eslMSAFILE_PHYLIP;
1168 *ret_alphatype = eslDNA;
1169 *ret_nseq = 7;
1170 *ret_alen = 50;
1171 }
1172
1173 static void
1174 utest_write_good3(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1175 {
1176 fputs(" 3 384\n", ofp);
1177 fputs("CYS1_DICDI -----MKVIL LFVLAVFTVF VSS------- --------RG IPPEEQ---- --------SQ \n", ofp);
1178 fputs("ALEU_HORVU MAHARVLLLA LAVLATAAVA VASSSSFADS NPIRPVTDRA ASTLESAVLG ALGRTRHALR \n", ofp);
1179 fputs("CATH_HUMAN ------MWAT LPLLCAGAWL LGV------- -PVCGAAELS VNSLEK---- --------FH \n", ofp);
1180 fputs("\n", ofp);
1181 fputs(" FLEFQDKFNK KY-SHEEYLE RFEIFKSNLG KIEELNLIAI NHKADTKFGV NKFADLSSDE \n", ofp);
1182 fputs(" FARFAVRYGK SYESAAEVRR RFRIFSESLE EVRSTN---- RKGLPYRLGI NRFSDMSWEE \n", ofp);
1183 fputs(" FKSWMSKHRK TY-STEEYHH RLQTFASNWR KINAHN---- NGNHTFKMAL NQFSDMSFAE \n", ofp);
1184 fputs("\n", ofp);
1185 fputs(" FKNYYLNNKE AIFTDDLPVA DYLDDEFINS IPTAFDWRTR G-AVTPVKNQ GQCGSCWSFS \n", ofp);
1186 fputs(" FQATRL-GAA QTCSATLAGN HLMRDA--AA LPETKDWRED G-IVSPVKNQ AHCGSCWTFS \n", ofp);
1187 fputs(" IKHKYLWSEP QNCSAT--KS NYLRGT--GP YPPSVDWRKK GNFVSPVKNQ GACGSCWTFS \n", ofp);
1188 fputs("\n", ofp);
1189 fputs(" TTGNVEGQHF ISQNKLVSLS EQNLVDCDHE CMEYEGEEAC DEGCNGGLQP NAYNYIIKNG \n", ofp);
1190 fputs(" TTGALEAAYT QATGKNISLS EQQLVDCAGG FNNF------ --GCNGGLPS QAFEYIKYNG \n", ofp);
1191 fputs(" TTGALESAIA IATGKMLSLA EQQLVDCAQD FNNY------ --GCQGGLPS QAFEYILYNK \n", ofp);
1192 fputs("\n", ofp);
1193 fputs(" GIQTESSYPY TAETGTQCNF NSANIGAKIS NFTMIP-KNE TVMAGYIVST GPLAIAADAV \n", ofp);
1194 fputs(" GIDTEESYPY KGVNGV-CHY KAENAAVQVL DSVNITLNAE DELKNAVGLV RPVSVAFQVI \n", ofp);
1195 fputs(" GIMGEDTYPY QGKDGY-CKF QPGKAIGFVK DVANITIYDE EAMVEAVALY NPVSFAFEVT \n", ofp);
1196 fputs("\n", ofp);
1197 fputs(" E-WQFYIGGV F-DIPCN--P NSLDHGILIV GYSAKNTIFR KNMPYWIVKN SWGADWGEQG \n", ofp);
1198 fputs(" DGFRQYKSGV YTSDHCGTTP DDVNHAVLAV GYGVENGV-- ---PYWLIKN SWGADWGDNG \n", ofp);
1199 fputs(" QDFMMYRTGI YSSTSCHKTP DKVNHAVLAV GYGEKNGI-- ---PYWIVKN SWGPQWGMNG \n", ofp);
1200 fputs("\n", ofp);
1201 fputs(" YIYLRRGKNT CGVSNFVSTS II-- \n", ofp);
1202 fputs(" YFKMEMGKNM CAIATCASYP VVAA \n", ofp);
1203 fputs(" YFLIERGKNM CGLAACASYP IPLV\n", ofp);
1204
1205 *ret_format = eslMSAFILE_PHYLIP;
1206 *ret_alphatype = eslAMINO;
1207 *ret_nseq = 3;
1208 *ret_alen = 384;
1209 }
1210
1211 static void
1212 utest_write_good4(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1213 {
1214 fputs(" 5 42\n", ofp);
1215 fputs("Turkey AAGCTNGGGC ATTTCAGGGT\n", ofp);
1216 fputs("GAGCCCGGGC AATACAGGGT AT\n", ofp);
1217 fputs("Salmo gairAAGCCTTGGC AGTGCAGGGT\n", ofp);
1218 fputs("GAGCCGTGGC CGGGCACGGT AT\n", ofp);
1219 fputs("H. SapiensACCGGTTGGC CGTTCAGGGT\n", ofp);
1220 fputs("ACAGGTTGGC CGTTCAGGGT AA\n", ofp);
1221 fputs("Chimp AAACCCTTGC CGTTACGCTT\n", ofp);
1222 fputs("AAACCGAGGC CGGGACACTC AT\n", ofp);
1223 fputs("Gorilla AAACCCTTGC CGGTACGCTT\n", ofp);
1224 fputs("AAACCATTGC CGGTACGCTT AA\n", ofp);
1225
1226 *ret_format = eslMSAFILE_PHYLIPS;
1227 *ret_alphatype = eslDNA;
1228 *ret_nseq = 5;
1229 *ret_alen = 42;
1230 }
1231
1232 static void
1233 utest_write_good5(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1234 {
1235 fputs(" 3 384\n", ofp);
1236 fputs("CYS1_DICDI-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ------------SQFLEFQDKFNKKY-SHEEY\n", ofp);
1237 fputs("LERFEIFKSNLGKIEELNLIAINHKADTKFGVNKFADLSSDEFKNYYLNNKEAIFTDDLPVADYLDDEFINSIPTAFDWRTRG-AVTP\n", ofp);
1238 fputs("VKNQGQCGSCWSFSTTGNVEGQHFISQNKLVSLSEQNLVDCDHECMEYEGEEACDEGCNGGLQPNAYNYIIKNGGIQTESSYPYTAET\n", ofp);
1239 fputs("GTQCNFNSANIGAKISNFTMIP-KNETVMAGYIVSTGPLAIAADAVE-WQFYIGGVF-DIPCN--PNSLDHGILIVGYSAKNTIFRKN\n", ofp);
1240 fputs("MPYWIVKNSWGADWGEQGYIYLRRGKNTCGVSNFVSTSII--\n", ofp);
1241 fputs("\n", ofp);
1242 fputs("ALEU_HORVUMAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTLESAVLGALGRTRHALRFARFAVRYGKSYESAAEVR\n", ofp);
1243 fputs("RRFRIFSESLEEVRSTN----RKGLPYRLGINRFSDMSWEEFQATRL-GAAQTCSATLAGNHLMRDA--AALPETKDWREDG-IVSPVK\n", ofp);
1244 fputs("NQAHCGSCWTFSTTGALEAAYTQATGKNISLSEQQLVDCAGGFNNF--------GCNGGLPSQAFEYIKYNGGIDTEESYPYKGVNGV-\n", ofp);
1245 fputs("CHYKAENAAVQVLDSVNITLNAEDELKNAVGLVRPVSVAFQVIDGFRQYKSGVYTSDHCGTTPDDVNHAVLAVGYGVENGV-----PYW\n", ofp);
1246 fputs("LIKNSWGADWGDNGYFKMEMGKNMCAIATCASYPVVAA\n", ofp);
1247 fputs("\n", ofp);
1248 fputs("CATH_HUMAN------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSLEK------------FHFKSWMSKHRKTY-STEEYH\n", ofp);
1249 fputs("HRLQTFASNWRKINAHN----NGNHTFKMALNQFSDMSFAEIKHKYLWSEPQNCSAT--KSNYLRGT--GPYPPSVDWRKKGNFVSPVK\n", ofp);
1250 fputs("NQGACGSCWTFSTTGALESAIAIATGKMLSLAEQQLVDCAQDFNNY--------GCQGGLPSQAFEYILYNKGIMGEDTYPYQGKDGY-\n", ofp);
1251 fputs("CKFQPGKAIGFVKDVANITIYDEEAMVEAVALYNPVSFAFEVTQDFMMYRTGIYSSTSCHKTPDKVNHAVLAVGYGEKNGI-----PYW\n", ofp);
1252 fputs("IVKNSWGPQWGMNGYFLIERGKNMCGLAACASYPIPLV\n", ofp);
1253 fputs("\n", ofp);
1254
1255 *ret_format = eslMSAFILE_PHYLIPS;
1256 *ret_alphatype = eslAMINO;
1257 *ret_nseq = 3;
1258 *ret_alen = 384;
1259 }
1260
1261 /* a tricky one, with a nonstandard name width of 12 (all names end in xx) */
1262 static void
1263 utest_write_good6(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1264 {
1265 fputs(" 5 42\n", ofp);
1266 fputs("Turkey xxAAGCTNGGGC ATTTCAGGGT\n", ofp);
1267 fputs("Salmo gairxxAAGCCTTGGC AGTGCAGGGT\n", ofp);
1268 fputs("H. SapiensxxACCGGTTGGC CGTTCAGGGT\n", ofp);
1269 fputs("Chimp xxAAACCCTTGC CGTTACGCTT\n", ofp);
1270 fputs("Gorilla xxAAACCCTTGC CGGTACGCTT\n", ofp);
1271 fputs("\n", ofp);
1272 fputs("GAGCCCGGGC AATACAGGGT AT\n", ofp);
1273 fputs("GAGCCGTGGC CGGGCACGGT AT\n", ofp);
1274 fputs("ACAGGTTGGC CGTTCAGGGT AA\n", ofp);
1275 fputs("AAACCGAGGC CGGGACACTC AT\n", ofp);
1276 fputs("AAACCATTGC CGGTACGCTT AA\n", ofp);
1277
1278 *ret_format = eslMSAFILE_PHYLIP;
1279 *ret_alphatype = eslDNA;
1280 *ret_nseq = 5;
1281 *ret_alen = 42;
1282 }
1283
1284 /* nonstandard name field width in a sequential file. */
1285 static void
1286 utest_write_good7(FILE *ofp, int *ret_format, int *ret_alphatype, int *ret_nseq, int *ret_alen)
1287 {
1288 fputs(" 5 42\n", ofp);
1289 fputs("Turkey xxAAGCTNGGGC ATTTCAGGGT\n", ofp);
1290 fputs("GAGCCCGGGC AATACAGGGT AT\n", ofp);
1291 fputs("\n", ofp);
1292 fputs("Salmo gairxxAAGCCTTGGC AGTGCAGGGT\n", ofp);
1293 fputs("GAGCCGTGGC CGGGCACGGT AT\n", ofp);
1294 fputs("\n", ofp);
1295 fputs("H. SapiensxxACCGGTTGGC CGTTCAGGGT\n", ofp);
1296 fputs("ACAGGTTGGC CGTTCAGGGT AA\n", ofp);
1297 fputs("\n", ofp);
1298 fputs("Chimp xxAAACCCTTGC CGTTACGCTT\n", ofp);
1299 fputs("AAACCGAGGC CGGGACACTC AT\n", ofp);
1300 fputs("\n", ofp);
1301 fputs("Gorilla xxAAACCCTTGC CGGTACGCTT\n", ofp);
1302 fputs("AAACCATTGC CGGTACGCTT AA\n", ofp);
1303
1304 *ret_format = eslMSAFILE_PHYLIPS;
1305 *ret_alphatype = eslDNA;
1306 *ret_nseq = 5;
1307 *ret_alen = 42;
1308 }
1309
1310 /* if using strict PHYLIP, 10 char namewidth results in reading a M=21 alignment,
1311 * which disagrees with alen=20
1312 */
1313 static void
1314 utest_write_bad1(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1315 {
1316 fputs(" 2 20\n", ofp);
1317 fputs("Turkey xAAGCTNGGGC ATTTCAGGGT\n", ofp);
1318 fputs("Salmo gairxAAGCCTTGGC AGTGCAGGGT\n", ofp);
1319
1320 *ret_alphatype = eslDNA;
1321 *ret_errstatus = eslEFORMAT;
1322 *ret_linenumber = 3;
1323 strcpy(errmsg, "alignment length disagrees");
1324 }
1325
1326 static void
1327 utest_write_bad2(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1328 {
1329 fputs(" x 2 20\n", ofp);
1330 fputs("Turkey AAGCTNGGGC ATTTCAGGGT\n", ofp);
1331 fputs("Salmo gairAAGCCTTGGC AGTGCAGGGT\n", ofp);
1332
1333 *ret_alphatype = eslDNA;
1334 *ret_errstatus = eslEFORMAT;
1335 *ret_linenumber = 1;
1336 strcpy(errmsg, "first field isn't an integer");
1337 }
1338
1339 static void
1340 utest_write_bad3(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1341 {
1342 fputs(" 2 x\n", ofp);
1343 fputs("Turkey AAGCTNGGGC ATTTCAGGGT\n", ofp);
1344 fputs("Salmo gairAAGCCTTGGC AGTGCAGGGT\n", ofp);
1345
1346 *ret_alphatype = eslDNA;
1347 *ret_errstatus = eslEFORMAT;
1348 *ret_linenumber = 1;
1349 strcpy(errmsg, "second field isn't an integer");
1350 }
1351
1352 static void
1353 utest_write_bad4(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1354 {
1355 fputs(" 2\n", ofp);
1356 fputs("Turkey AAGCTNGGGC ATTTCAGGGT\n", ofp);
1357 fputs("Salmo gairAAGCCTTGGC AGTGCAGGGT\n", ofp);
1358
1359 *ret_alphatype = eslDNA;
1360 *ret_errstatus = eslEFORMAT;
1361 *ret_linenumber = 1;
1362 strcpy(errmsg, "only one field found");
1363 }
1364
1365 static void
1366 utest_write_bad5(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1367 {
1368 fputs(" 2 20\n", ofp);
1369 fputs("\n", ofp);
1370
1371 *ret_alphatype = eslDNA;
1372 *ret_errstatus = eslEFORMAT;
1373 *ret_linenumber = 2;
1374 strcpy(errmsg, "no alignment data");
1375 }
1376
1377 static void
1378 utest_write_bad6(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1379 {
1380 fputs(" 2 20\n", ofp);
1381 fputs("seq1 \n", ofp);
1382 fputs("seq2 ACDEFGHIKLMNPQRSTVWY\n", ofp);
1383
1384 *ret_alphatype = eslAMINO;
1385 *ret_errstatus = eslEFORMAT;
1386 *ret_linenumber = 2;
1387 strcpy(errmsg, "line too short");
1388 }
1389
1390 static void
1391 utest_write_bad7(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1392 {
1393 fputs(" 2 20\n", ofp);
1394 fputs("\tseq1_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1395 fputs("seq2_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1396
1397 *ret_alphatype = eslAMINO;
1398 *ret_errstatus = eslEFORMAT;
1399 *ret_linenumber = 2;
1400 strcpy(errmsg, "invalid character(s) in sequence name");
1401 }
1402
1403 static void
1404 utest_write_bad8(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1405 {
1406 fputs(" 2 20\n", ofp);
1407 fputs("seq1_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1408 fputs("seq2_name ACDEFGHI~LMNPQRSTVWY\n", ofp);
1409
1410 *ret_alphatype = eslAMINO;
1411 *ret_errstatus = eslEFORMAT;
1412 *ret_linenumber = 3;
1413 strcpy(errmsg, "one or more invalid sequence characters");
1414 }
1415
1416 static void
1417 utest_write_bad9(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1418 {
1419 fputs(" 2 20\n", ofp);
1420 fputs("seq1_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1421 fputs("seq2_name ACDEFGHIKLMNPQRSTVW\n", ofp);
1422
1423 *ret_alphatype = eslAMINO;
1424 *ret_errstatus = eslEFORMAT;
1425 *ret_linenumber = 3;
1426 strcpy(errmsg, "number of residues on line differs from previous seqs");
1427 }
1428
1429 static void
1430 utest_write_bad10(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1431 {
1432 fputs(" 3 40\n", ofp);
1433 fputs("seq1_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1434 fputs("seq2_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1435 fputs("\n", ofp);
1436 fputs("ACDEFGHIKLMNPQRSTVWY\n", ofp);
1437 fputs("ACDEFGHIKLMNPQRSTVWY\n", ofp);
1438
1439 *ret_alphatype = eslAMINO;
1440 *ret_errstatus = eslEFORMAT;
1441 *ret_linenumber = 4;
1442 strcpy(errmsg, "unexpected number of sequences in block");
1443 }
1444
1445 static void
1446 utest_write_bad11(FILE *ofp, int *ret_alphatype, int *ret_errstatus, int *ret_linenumber, char *errmsg)
1447 {
1448 fputs(" 2 30\n", ofp);
1449 fputs("seq1_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1450 fputs("seq2_name ACDEFGHIKLMNPQRSTVWY\n", ofp);
1451 fputs("\n", ofp);
1452 fputs(" ACDEFGHIKLMNPQRSTVWY\n", ofp);
1453 fputs(" ACDEFGHIKLMNPQRSTVWY\n", ofp);
1454 fputs("\n", ofp);
1455
1456 *ret_alphatype = eslAMINO;
1457 *ret_errstatus = eslEFORMAT;
1458 *ret_linenumber = 7;
1459 strcpy(errmsg, "alignment length disagrees with header");
1460 }
1461
1462 /* example of a pathological file where interleaved, sequential
1463 * can't be distinguished. If we read this as interleaved:
1464 * seq1 AAAAAAAAAA CCCCCCCCCC YYYYYYYYY FFFFFFFFFF GGGGGGGGGG
1465 * YYYYYYYYY DDDDDDDDDD EEEEEEEEEE HHHHHHHHH IIIIIIIIII KKKKKKKKKK
1466 * whereas if we read it as sequential:
1467 * seq1 AAAAAAAAAA CCCCCCCCCC YYYYYYYYY DDDDDDDDDD EEEEEEEEEE
1468 * YYYYYYYYY FFFFFFFFFF GGGGGGGGGG HHHHHHHHH IIIIIIIIII KKKKKKKKKK
1469 *
1470 * To fall into this, sequence names have to look like a chunk of
1471 * aligned sequence, with exactly the right length to give a correct
1472 * <alen> whichever way we read the file.
1473 */
1474 static void
1475 utest_write_ambig1(FILE *ofp)
1476 {
1477 fputs(" 2 49\n", ofp);
1478 fputs("seq1 AAAAAAAAAA CCCCCCCCCC\n", ofp);
1479 fputs("YYYYYYYYY DDDDDDDDDD EEEEEEEEEE\n", ofp);
1480 fputs("YYYYYYYYY FFFFFFFFFF GGGGGGGGGG\n", ofp);
1481 fputs("HHHHHHHHH IIIIIIIIII KKKKKKKKKK\n", ofp);
1482 }
1483
1484
1485
1486
1487
1488 static void
1489 utest_goodfile(char *filename, int testnumber, int expected_format, int expected_alphatype, int expected_nseq, int expected_alen)
1490 {
1491 ESL_ALPHABET *abc = NULL;
1492 ESL_MSAFILE *afp = NULL;
1493 ESL_MSAFILE_FMTDATA fmtd; /* for writing formats with nonstandard name field widths */
1494 ESL_MSA *msa1 = NULL;
1495 ESL_MSA *msa2 = NULL;
1496 char tmpfile1[32] = "esltmpXXXXXX";
1497 char tmpfile2[32] = "esltmpXXXXXX";
1498 FILE *ofp = NULL;
1499 int status;
1500
1501 esl_msafile_fmtdata_Init(&fmtd);
1502
1503 /* guessing both the format and the alphabet should work: this is a digital open */
1504 if ( (status = esl_msafile_Open(&abc, filename, NULL, eslMSAFILE_UNKNOWN, NULL, &afp)) != eslOK) esl_fatal("phylip good file unit test %d failed: digital open", testnumber);
1505 if (afp->format != expected_format) esl_fatal("phylip good file unit test %d failed: format autodetection", testnumber);
1506 if (abc->type != expected_alphatype) esl_fatal("phylip good file unit test %d failed: alphabet autodetection", testnumber);
1507
1508 /* This is a digital read, using <abc>. */
1509 if ( (status = esl_msafile_phylip_Read(afp, &msa1)) != eslOK) esl_fatal("phylip good file unit test %d failed: msa read, digital", testnumber);
1510 if (msa1->nseq != expected_nseq || msa1->alen != expected_alen) esl_fatal("phylip good file unit test %d failed: nseq/alen", testnumber);
1511 if (esl_msa_Validate(msa1, NULL) != eslOK) esl_fatal("phylip good file test %d failed: msa1 invalid", testnumber);
1512 fmtd.namewidth = afp->fmtd.namewidth;
1513 esl_msafile_Close(afp);
1514
1515 /* write it back out to a new tmpfile (digital write) */
1516 if ( (status = esl_tmpfile_named(tmpfile1, &ofp)) != eslOK) esl_fatal("phylip good file unit test %d failed: tmpfile creation", testnumber);
1517 if ( (status = esl_msafile_phylip_Write(ofp, msa1, expected_format, &fmtd)) != eslOK) esl_fatal("phylip good file unit test %d failed: msa write, digital", testnumber);
1518 fclose(ofp);
1519
1520 /* 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) */
1521 if ( (status = esl_msafile_Open(NULL, tmpfile1, NULL, expected_format, &fmtd, &afp)) != eslOK) esl_fatal("phylip good file unit test %d failed: text mode open", testnumber);
1522 if ( (status = esl_msafile_phylip_Read(afp, &msa2)) != eslOK) esl_fatal("phylip good file unit test %d failed: msa read, text", testnumber);
1523 if (esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal("phylip good file test %d failed: msa2 invalid", testnumber);
1524 if (msa2->nseq != expected_nseq || msa2->alen != expected_alen) esl_fatal("phylip good file unit test %d failed: nseq/alen", testnumber);
1525 fmtd.namewidth = afp->fmtd.namewidth;
1526 esl_msafile_Close(afp);
1527
1528 /* write it back out to a new tmpfile (text write) */
1529 if ( (status = esl_tmpfile_named(tmpfile2, &ofp)) != eslOK) esl_fatal("phylip good file unit test %d failed: tmpfile creation", testnumber);
1530 if ( (status = esl_msafile_phylip_Write(ofp, msa2, expected_format, &fmtd)) != eslOK) esl_fatal("phylip good file unit test %d failed: msa write, text", testnumber);
1531 fclose(ofp);
1532 esl_msa_Destroy(msa2);
1533
1534 /* open and read it in digital mode */
1535 if ( (status = esl_msafile_Open(&abc, tmpfile1, NULL, expected_format, &fmtd, &afp)) != eslOK) esl_fatal("phylip good file unit test %d failed: 2nd digital mode open", testnumber);
1536 if ( (status = esl_msafile_phylip_Read(afp, &msa2)) != eslOK) esl_fatal("phylip good file unit test %d failed: 2nd digital msa read", testnumber);
1537 if (esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal("phylip good file test %d failed: msa2 invalid", testnumber);
1538 esl_msafile_Close(afp);
1539
1540 /* this msa <msa2> should be identical to <msa1> */
1541 if (esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal("phylip good file unit test %d failed: msa compare", testnumber);
1542
1543 remove(tmpfile1);
1544 remove(tmpfile2);
1545 esl_msa_Destroy(msa1);
1546 esl_msa_Destroy(msa2);
1547 esl_alphabet_Destroy(abc);
1548 }
1549
1550 /* utest_badfile:
1551 * Test the strict PHYLIP parser's ability to detect bad input;
1552 * regression test its user-directed error messages.
1553 *
1554 * note: this test uses strict phylip, with 10 char name width.
1555 * it's not using the format guesser, which could determine a nonstandard name width
1556 * some "bad" utests can be parsed differently by esl_msafile_phylip_example,
1557 * which does use the format guesser
1558 *
1559 * TODO: we should also have "bad" tests for sequential PHYLIP, and for
1560 * nonstandard name widths.
1561 *
1562 */
1563 static void
1564 utest_badfile(char *filename, int testnumber, int expected_alphatype, int expected_status, int expected_linenumber, char *expected_errmsg)
1565 {
1566 ESL_ALPHABET *abc = esl_alphabet_Create(expected_alphatype);
1567 ESL_MSAFILE *afp = NULL;
1568 ESL_MSA *msa = NULL;
1569 int status;
1570
1571 if ( (status = esl_msafile_Open(&abc, filename, NULL, eslMSAFILE_PHYLIP, NULL, &afp)) != eslOK) esl_fatal("phylip bad file unit test %d failed: unexpected open failure", testnumber);
1572 if ( (status = esl_msafile_phylip_Read(afp, &msa)) != expected_status) esl_fatal("phylip bad file unit test %d failed: unexpected error code", testnumber);
1573 if (strstr(afp->errmsg, expected_errmsg) == NULL) esl_fatal("phylip bad file unit test %d failed: unexpected errmsg", testnumber);
1574 if (afp->linenumber != expected_linenumber) esl_fatal("phylip bad file unit test %d failed: unexpected linenumber", testnumber);
1575 esl_msafile_Close(afp);
1576 esl_alphabet_Destroy(abc);
1577 esl_msa_Destroy(msa);
1578 }
1579
1580 static void
1581 utest_ambigfile(char *filename, int testnumber)
1582 {
1583 ESL_BUFFER *bf = NULL;
1584 int fmt;
1585 int namewidth;
1586
1587 if ( esl_buffer_Open(filename, NULL, &bf) != eslOK) esl_fatal("phylip ambig file unit test %d failed: buffer open", testnumber);
1588 if ( esl_msafile_phylip_CheckFileFormat(bf, &fmt, &namewidth) != eslEAMBIGUOUS) esl_fatal("phylip ambig file unit test %d failed: ambiguity detection", testnumber);
1589 if ( fmt != eslMSAFILE_UNKNOWN ) esl_fatal("phylip ambig file unit test %d failed: format code", testnumber);
1590 if ( namewidth != 0 ) esl_fatal("phylip ambig file unit test %d failed: namewidth not 0", testnumber);
1591 esl_buffer_Close(bf);
1592 }
1593
1594
1595 /* PHYLIP's seqboot program can output many MSAs to the same phylip file.
1596 * For this reason (only), we allow PHYLIP format to have multiple MSAs per file.
1597 * PHYLIP format 'officially' does not document this!
1598 */
1599 static void
1600 utest_seqboot(void)
1601 {
1602 char msg[] = "seqboot unit test failed";
1603 char tmpfile[32];
1604 FILE *ofp;
1605 int expected_fmt;
1606 int expected_alphatype;
1607 int expected_nseq;
1608 int expected_alen;
1609 int expected_nali;
1610 int i;
1611 ESL_ALPHABET *abc = NULL;
1612 ESL_MSAFILE *afp = NULL;
1613 ESL_MSA *msa = NULL;
1614
1615 /* Write a tmp testfile with good1 concatenated 3 times. */
1616 expected_nali = 3;
1617 strcpy(tmpfile, "esltmpXXXXXX");
1618 if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
1619 for (i = 0; i < expected_nali; i++)
1620 utest_write_good1(ofp, &expected_fmt, &expected_alphatype, &expected_nseq, &expected_alen);
1621 fclose(ofp);
1622
1623 /* open it and loop over it, reading MSAs. there should be 3, of the expected size. */
1624 if (esl_msafile_Open(&abc, tmpfile, /*env=*/NULL, eslMSAFILE_UNKNOWN, /*fmtd=*/NULL, &afp) != eslOK) esl_fatal(msg);
1625 if (abc->type != expected_alphatype) esl_fatal(msg);
1626 if (afp->format != expected_fmt) esl_fatal(msg);
1627 i = 0;
1628 while ( esl_msafile_Read(afp, &msa) == eslOK)
1629 {
1630 i++;
1631 if (msa->nseq != expected_nseq) esl_fatal(msg);
1632 if (msa->alen != expected_alen) esl_fatal(msg);
1633 esl_msa_Destroy(msa);
1634 }
1635 if (i != expected_nali) esl_fatal(msg);
1636
1637 remove(tmpfile);
1638 esl_msafile_Close(afp);
1639 esl_alphabet_Destroy(abc);
1640 }
1641
1642 #endif /*eslMSAFILE_PHYLIP_TESTDRIVE*/
1643 /*---------------------- end, unit tests ------------------------*/
1644
1645
1646
1647 /*****************************************************************
1648 * 7. Test driver
1649 *****************************************************************/
1650
1651 #ifdef eslMSAFILE_PHYLIP_TESTDRIVE
1652 /* compile: gcc -g -Wall -I. -L. -o esl_msafile_phylip_utest -DeslMSAFILE_PHYLIP_TESTDRIVE esl_msafile_phylip.c -leasel -lm
1653 * (gcov): gcc -g -Wall -fprofile-arcs -ftest-coverage -I. -L. -o esl_msafile_phylip_utest -DeslMSAFILE_PHYLIP_TESTDRIVE esl_msafile_phylip.c -leasel -lm
1654 * run: ./esl_msafile_phylip_utest
1655 */
1656 #include <esl_config.h>
1657
1658 #include <stdio.h>
1659
1660 #include "easel.h"
1661 #include "esl_getopts.h"
1662 #include "esl_msafile.h"
1663 #include "esl_msafile_phylip.h"
1664
1665 static ESL_OPTIONS options[] = {
1666 /* name type default env range togs reqs incomp help docgrp */
1667 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
1668 { 0,0,0,0,0,0,0,0,0,0},
1669 };
1670 static char usage[] = "[-options]";
1671 static char banner[] = "test driver for PHYLIP MSA format module";
1672
1673 int
1674 main(int argc, char **argv)
1675 {
1676 char msg[] = "PHYLIP MSA i/o module test driver failed";
1677 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
1678 FILE *ofp = NULL;
1679 int ngoodtests = 7;
1680 int nbadtests = 11;
1681 int nambigtests = 1;
1682 int testnumber = 0;
1683 char tmpfile[32];
1684 int expected_format;
1685 int expected_alphatype;
1686 int expected_errstatus;
1687 int expected_linenumber;
1688 int expected_nseq;
1689 int expected_alen;
1690 char expected_errmsg[eslERRBUFSIZE];
1691
1692 /* Test various correct versions of the format */
1693 for (testnumber = 1; testnumber <= ngoodtests; testnumber++)
1694 {
1695 strcpy(tmpfile, "esltmpXXXXXX");
1696 if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
1697 switch (testnumber) {
1698 case 1: utest_write_good1 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1699 case 2: utest_write_good2 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1700 case 3: utest_write_good3 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1701 case 4: utest_write_good4 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1702 case 5: utest_write_good5 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1703 case 6: utest_write_good6 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1704 case 7: utest_write_good7 (ofp, &expected_format, &expected_alphatype, &expected_nseq, &expected_alen); break;
1705 }
1706 fclose(ofp);
1707 utest_goodfile(tmpfile, testnumber, expected_format, expected_alphatype, expected_nseq, expected_alen);
1708 remove(tmpfile);
1709 }
1710
1711 /* Test for all the possible EFORMAT errors (using strict Phylip interleaved parser) */
1712 for (testnumber = 1; testnumber <= nbadtests; testnumber++)
1713 {
1714 strcpy(tmpfile, "esltmpXXXXXX");
1715 if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
1716
1717 switch (testnumber) {
1718 case 1: utest_write_bad1 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1719 case 2: utest_write_bad2 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1720 case 3: utest_write_bad3 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1721 case 4: utest_write_bad4 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1722 case 5: utest_write_bad5 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1723 case 6: utest_write_bad6 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1724 case 7: utest_write_bad7 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1725 case 8: utest_write_bad8 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1726 case 9: utest_write_bad9 (ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1727 case 10: utest_write_bad10(ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1728 case 11: utest_write_bad11(ofp, &expected_alphatype, &expected_errstatus, &expected_linenumber, expected_errmsg); break;
1729 }
1730 fclose(ofp);
1731
1732 utest_badfile(tmpfile, testnumber, expected_alphatype, expected_errstatus, expected_linenumber, expected_errmsg);
1733 remove(tmpfile);
1734 }
1735
1736 /* Test that we correctly detect pathological files that look both interleaved and sequential */
1737 for (testnumber = 1; testnumber <= nambigtests; testnumber++)
1738 {
1739 strcpy(tmpfile, "esltmpXXXXXX");
1740 if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
1741
1742 switch (testnumber) {
1743 case 1: utest_write_ambig1 (ofp);
1744 }
1745 fclose(ofp);
1746
1747 utest_ambigfile(tmpfile, testnumber);
1748 remove(tmpfile);
1749 }
1750
1751
1752 /* Other tests */
1753 utest_seqboot();
1754
1755 esl_getopts_Destroy(go);
1756 return 0;
1757 }
1758 #endif /*eslMSAFILE_PHYLIP_TESTDRIVE*/
1759 /*--------------------- end, test driver ------------------------*/
1760
1761
1762
1763 /*****************************************************************
1764 * 8. Example.
1765 *****************************************************************/
1766
1767 #ifdef eslMSAFILE_PHYLIP_EXAMPLE
1768 /* A full-featured example of reading/writing an MSA in Phylip format(s).
1769 gcc -g -Wall -o esl_msafile_phylip_example -I. -L. -DeslMSAFILE_PHYLIP_EXAMPLE esl_msafile_phylip.c -leasel -lm
1770 ./esl_msafile_phylip_example <msafile>
1771 */
1772 /*::cexcerpt::msafile_phylip_example::begin::*/
1773 #include <stdio.h>
1774
1775 #include "easel.h"
1776 #include "esl_alphabet.h"
1777 #include "esl_getopts.h"
1778 #include "esl_msa.h"
1779 #include "esl_msafile.h"
1780 #include "esl_msafile_phylip.h"
1781
1782 static ESL_OPTIONS options[] = {
1783 /* name type default env range toggles reqs incomp help docgroup*/
1784 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1785 { "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-1", "no autodetection; use interleaved PHYLIP", 0 },
1786 { "-2", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-2", "no autodetection; use sequential PHYLIPS", 0 },
1787 { "-q", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "quieter: don't write msa back, just summary", 0 },
1788 { "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use text mode: no digital alphabet", 0 },
1789 { "-w", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "specify that format's name width is <n>", 0 },
1790 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is DNA", 0 },
1791 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is RNA", 0 },
1792 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is protein", 0 },
1793 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1794 };
1795 static char usage[] = "[-options] <msafile>";
1796 static char banner[] = "example of guessing, reading, writing PHYLIP formats";
1797
1798 int
1799 main(int argc, char **argv)
1800 {
1801 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage);
1802 char *filename = esl_opt_GetArg(go, 1);
1803 int infmt = eslMSAFILE_UNKNOWN;
1804 ESL_ALPHABET *abc = NULL;
1805 ESL_MSAFILE *afp = NULL;
1806 ESL_MSA *msa = NULL;
1807 ESL_MSAFILE_FMTDATA fmtd;
1808 int status;
1809
1810 if (esl_opt_GetBoolean(go, "-1")) infmt = eslMSAFILE_PHYLIP; /* interleaved format */
1811 else if (esl_opt_GetBoolean(go, "-2")) infmt = eslMSAFILE_PHYLIPS; /* sequential format */
1812
1813 if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
1814 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
1815 else if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
1816
1817 /* Variant PHYLIP formats allow nonstandard name field width.
1818 * Usually we can successfully guess this, when guessing format.
1819 * But if PHYLIP or PHYLIPS format is set (no guessing), caller may also
1820 * want to allow a nonstandard name field width to be set.
1821 */
1822 esl_msafile_fmtdata_Init(&fmtd);
1823 fmtd.namewidth = esl_opt_GetInteger(go, "-w");
1824
1825 /* Text mode: pass NULL for alphabet.
1826 * Digital mode: pass ptr to expected ESL_ALPHABET; and if abc=NULL, alphabet is guessed
1827 */
1828 if (esl_opt_GetBoolean(go, "-t")) status = esl_msafile_Open(NULL, filename, NULL, infmt, &fmtd, &afp);
1829 else status = esl_msafile_Open(&abc, filename, NULL, infmt, &fmtd, &afp);
1830 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
1831
1832 if ( (status = esl_msafile_phylip_Read(afp, &msa)) != eslOK)
1833 esl_msafile_ReadFailure(afp, status);
1834
1835 printf("format variant: %s\n", esl_msafile_DecodeFormat(afp->format));
1836 printf("name width: %d\n", afp->fmtd.namewidth);
1837 printf("alphabet: %s\n", (abc ? esl_abc_DecodeType(abc->type) : "none (text mode)"));
1838 printf("# of seqs: %d\n", msa->nseq);
1839 printf("# of cols: %d\n", (int) msa->alen);
1840 printf("\n");
1841
1842 if (afp->fmtd.namewidth != 10) fmtd.namewidth = afp->fmtd.namewidth;
1843
1844 if (! esl_opt_GetBoolean(go, "-q"))
1845 esl_msafile_phylip_Write(stdout, msa, eslMSAFILE_PHYLIP, &fmtd);
1846
1847 esl_msa_Destroy(msa);
1848 esl_msafile_Close(afp);
1849 if (abc) esl_alphabet_Destroy(abc);
1850 esl_getopts_Destroy(go);
1851 exit(0);
1852 }
1853 /*::cexcerpt::msafile_phylip_example::end::*/
1854 #endif /*eslMSAFILE_PHYLIP_EXAMPLE*/
1855
1856
1857
1858 #ifdef eslMSAFILE_PHYLIP_EXAMPLE2
1859 /* A minimal example. Reading a strict interleaved PHYLIP MSA in text mode.
1860 gcc -g -Wall -o esl_msafile_phylip_example2 -I. -L. -DeslMSAFILE_PHYLIP_EXAMPLE2 esl_msafile_phylip.c -leasel -lm
1861 ./esl_msafile_phylip_example2 <msafile>
1862 */
1863 /*::cexcerpt::msafile_phylip_example2::begin::*/
1864 #include <stdio.h>
1865
1866 #include "easel.h"
1867 #include "esl_msa.h"
1868 #include "esl_msafile.h"
1869 #include "esl_msafile_phylip.h"
1870
1871 int
1872 main(int argc, char **argv)
1873 {
1874 char *filename = argv[1];
1875 int infmt = eslMSAFILE_PHYLIP; /* or eslMSAFILE_PHYLIPS, for sequential format */
1876 ESL_MSAFILE *afp = NULL;
1877 ESL_MSA *msa = NULL;
1878 int status;
1879
1880 if ( (status = esl_msafile_Open(NULL, filename, NULL, infmt, NULL, &afp)) != eslOK) esl_msafile_OpenFailure(afp, status);
1881 if ( (status = esl_msafile_phylip_Read(afp, &msa)) != eslOK) esl_msafile_ReadFailure(afp, status);
1882
1883 printf("# of seqs: %d\n", msa->nseq);
1884 printf("# of cols: %d\n", (int) msa->alen);
1885 printf("\n");
1886
1887 esl_msafile_phylip_Write(stdout, msa, eslMSAFILE_PHYLIP, NULL);
1888
1889 esl_msa_Destroy(msa);
1890 esl_msafile_Close(afp);
1891 exit(0);
1892 }
1893 /*::cexcerpt::msafile_phylip_example::end::*/
1894 #endif /*eslMSAFILE_PHYLIP_EXAMPLE*/
1895 /*--------------------- end of examples -------------------------*/