| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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); |
| 144 |
int alphatype = eslUNKNOWN; |
| 145 |
int threshold[3] = { 500, 5000, 50000 }; |
| 146 |
int nsteps = 3; |
| 147 |
int step = 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; } |
| 160 |
|
| 161 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 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 |
|
| 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 |
|
| 187 |
if (step < nsteps && nres > threshold[step]) { |
| 188 |
if ((status = esl_abc_GuessAlphabet(ct, &alphatype)) == eslOK) goto DONE; |
| 189 |
step++; |
| 190 |
} |
| 191 |
} |
| 192 |
if (status != eslEOF) goto ERROR; |
| 193 |
status = esl_abc_GuessAlphabet(ct, &alphatype); |
| 194 |
|
| 195 |
DONE: |
| 196 |
esl_buffer_SetOffset(afp->bf, anchor); |
| 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 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 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; |
| 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 |
|
| 262 |
while ( (status = esl_msafile_GetLine(afp, &p, &n)) == eslOK && esl_memspn(p, n, " \t") == n) ; |
| 263 |
if (status != eslOK) goto ERROR; |
| 264 |
|
| 265 |
|
| 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 |
|
| 272 |
|
| 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 |
|
| 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); |
| 282 |
|
| 283 |
|
| 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 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 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 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
int |
| 382 |
esl_msafile_phylip_CheckFileFormat(ESL_BUFFER *bf, int *ret_format, int *ret_namewidth) |
| 383 |
{ |
| 384 |
int maybe_interleaved = FALSE; |
| 385 |
int maybe_sequential = FALSE; |
| 386 |
int nblocks; |
| 387 |
int w1, w2; |
| 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 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 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); |
| 417 |
char *namebuf = NULL; |
| 418 |
int nblocks = 0; |
| 419 |
int64_t alen = 0; |
| 420 |
char *p = afp->line; |
| 421 |
esl_pos_t n = afp->n; |
| 422 |
int64_t block_alen; |
| 423 |
int64_t cur_alen; |
| 424 |
int idx; |
| 425 |
int status; |
| 426 |
|
| 427 |
ESL_ALLOC(namebuf, sizeof(char) * (namewidth+1)); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
do { |
| 432 |
idx = 0; |
| 433 |
do { |
| 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 |
|
| 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 |
|
| 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 |
|
| 455 |
idx++; |
| 456 |
status = esl_msafile_GetLine(afp, &p, &n); |
| 457 |
} while (status == eslOK && idx < nseq && esl_memspn(p, n, " \t") < n); |
| 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 |
|
| 464 |
while (status == eslOK && esl_memspn(p, n, " \t") == n) |
| 465 |
status = esl_msafile_GetLine(afp, &p, &n); |
| 466 |
|
| 467 |
} while (status == eslOK && alen < alen_stated); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
if (status == eslOK) esl_msafile_PutLine(afp); |
| 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; |
| 484 |
if (namebuf) free(namebuf); |
| 485 |
return status; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 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 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 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); |
| 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) |
| 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 |
|
| 580 |
status = esl_msafile_GetLine(afp, &p, &n); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 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 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 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; |
| 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 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 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 |
|
| 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 |
|
| 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; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
if (nblocks == 0) |
| 715 |
{ |
| 716 |
colcodes0 = colcodes; ncols0 = ncols; |
| 717 |
colcodes = NULL; |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
for (nres1 = 0, c = 10; c < ncols0; c++) |
| 723 |
if (colcodes0[c] != '.') nres1++; |
| 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; } |
| 731 |
} |
| 732 |
} |
| 733 |
nblocks++; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
if (nres1+nres2 == alen) break; |
| 740 |
|
| 741 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 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; |
| 839 |
int r; |
| 840 |
int L1; |
| 841 |
int b; |
| 842 |
int a; |
| 843 |
char *p; |
| 844 |
esl_pos_t n; |
| 845 |
int i, j, k; |
| 846 |
int w; |
| 847 |
int len2; |
| 848 |
int status; |
| 849 |
|
| 850 |
|
| 851 |
anchor = esl_buffer_GetOffset(bf); |
| 852 |
if ( esl_buffer_SetStableAnchor(bf, anchor) != eslOK) { status = eslFAIL; goto ERROR; } |
| 853 |
|
| 854 |
|
| 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--; |
| 859 |
esl_buffer_SetOffset(bf, anchor); |
| 860 |
|
| 861 |
|
| 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 |
|
| 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 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 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 |
|
| 890 |
a = alen - b; |
| 891 |
if (a > r) { status = eslFAIL; goto ERROR; } |
| 892 |
for (w = 0; w < L1; w++) |
| 893 |
if (rth[w] == a) break; |
| 894 |
if (w == L1) { status = eslFAIL; goto ERROR; } |
| 895 |
for (i = 0; i < w; i++) |
| 896 |
if (! isspace(p[i])) break; |
| 897 |
if ( i == w) { status = eslFAIL; goto ERROR; } |
| 898 |
|
| 899 |
|
| 900 |
|
| 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 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 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 |
|
| 1043 |
nres1 = alen - nres2; |
| 1044 |
if (nres1 <= 0) return eslFAIL; |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 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 |
|
| 1054 |
|
| 1055 |
for ( ; c >= 0; c--) |
| 1056 |
if (colcodes0[c] != '.') break; |
| 1057 |
nwA = c+1; |
| 1058 |
|
| 1059 |
|
| 1060 |
if (nwA <= 10 && nwB >= 10) namewidth = 10; |
| 1061 |
else namewidth = nwA; |
| 1062 |
|
| 1063 |
*ret_namewidth = namewidth; |
| 1064 |
return eslOK; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 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 |
|
| 1098 |
|
| 1099 |
|
| 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 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1311 |
|
| 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 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 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 |
|
| 1596 |
|
| 1597 |
|
| 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 |
|
| 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 |
|
| 1624 |
if (esl_msafile_Open(&abc, tmpfile, NULL, eslMSAFILE_UNKNOWN, 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 |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
#ifdef eslMSAFILE_PHYLIP_TESTDRIVE |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1753 |
utest_seqboot(); |
| 1754 |
|
| 1755 |
esl_getopts_Destroy(go); |
| 1756 |
return 0; |
| 1757 |
} |
| 1758 |
#endif |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
#ifdef eslMSAFILE_PHYLIP_EXAMPLE |
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 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 |
|
| 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; |
| 1811 |
else if (esl_opt_GetBoolean(go, "-2")) infmt = eslMSAFILE_PHYLIPS; |
| 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 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
esl_msafile_fmtdata_Init(&fmtd); |
| 1823 |
fmtd.namewidth = esl_opt_GetInteger(go, "-w"); |
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 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 |
|
| 1854 |
#endif |
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
#ifdef eslMSAFILE_PHYLIP_EXAMPLE2 |
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 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; |
| 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 |
|
| 1894 |
#endif |
| 1895 |
|