| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include <esl_config.h> |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include "easel.h" |
| 19 |
#include "esl_alphabet.h" |
| 20 |
#include "esl_mem.h" |
| 21 |
#include "esl_msa.h" |
| 22 |
#include "esl_msafile.h" |
| 23 |
|
| 24 |
#include "esl_msafile_a2m.h" |
| 25 |
|
| 26 |
static int a2m_padding_digital(ESL_MSA *msa, char **csflag, int *nins, int ncons); |
| 27 |
static int a2m_padding_text (ESL_MSA *msa, char **csflag, int *nins, int ncons); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int |
| 61 |
esl_msafile_a2m_SetInmap(ESL_MSAFILE *afp) |
| 62 |
{ |
| 63 |
int sym; |
| 64 |
|
| 65 |
if (afp->abc) |
| 66 |
{ |
| 67 |
for (sym = 0; sym < 128; sym++) |
| 68 |
afp->inmap[sym] = afp->abc->inmap[sym]; |
| 69 |
afp->inmap[0] = esl_abc_XGetUnknown(afp->abc); |
| 70 |
afp->inmap['_'] = eslDSQ_ILLEGAL; |
| 71 |
afp->inmap['*'] = eslDSQ_ILLEGAL; |
| 72 |
afp->inmap['~'] = eslDSQ_ILLEGAL; |
| 73 |
} |
| 74 |
|
| 75 |
if (! afp->abc) |
| 76 |
{ |
| 77 |
for (sym = 1; sym < 128; sym++) |
| 78 |
afp->inmap[sym] = (isalpha(sym) ? sym : eslDSQ_ILLEGAL); |
| 79 |
afp->inmap[0] = '?'; |
| 80 |
afp->inmap['-'] = '-'; |
| 81 |
} |
| 82 |
|
| 83 |
afp->inmap[' '] = eslDSQ_IGNORED; |
| 84 |
afp->inmap['\t'] = eslDSQ_IGNORED; |
| 85 |
afp->inmap['.'] = eslDSQ_IGNORED; |
| 86 |
afp->inmap['O'] = eslDSQ_IGNORED; |
| 87 |
afp->inmap['o'] = eslDSQ_IGNORED; |
| 88 |
return eslOK; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
int |
| 113 |
esl_msafile_a2m_GuessAlphabet(ESL_MSAFILE *afp, int *ret_type) |
| 114 |
{ |
| 115 |
int alphatype = eslUNKNOWN; |
| 116 |
esl_pos_t anchor = -1; |
| 117 |
int threshold[3] = { 500, 5000, 50000 }; |
| 118 |
int nsteps = 3; |
| 119 |
int step = 0; |
| 120 |
int nres = 0; |
| 121 |
int x; |
| 122 |
int64_t ct[26]; |
| 123 |
char *p; |
| 124 |
esl_pos_t n, pos; |
| 125 |
int status; |
| 126 |
|
| 127 |
for (x = 0; x < 26; x++) ct[x] = 0; |
| 128 |
|
| 129 |
anchor = esl_buffer_GetOffset(afp->bf); |
| 130 |
if ((status = esl_buffer_SetAnchor(afp->bf, anchor)) != eslOK) { status = eslEINCONCEIVABLE; goto ERROR; } |
| 131 |
|
| 132 |
while ( (status = esl_buffer_GetLine(afp->bf, &p, &n)) == eslOK) |
| 133 |
{ |
| 134 |
while (n && isspace(*p)) { p++; n--; } |
| 135 |
if (!n || *p == '>') continue; |
| 136 |
|
| 137 |
for (pos = 0; pos < n; pos++) |
| 138 |
if (isalpha(p[pos])) { |
| 139 |
x = toupper(p[pos]) - 'A'; |
| 140 |
ct[x]++; |
| 141 |
nres++; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if (step < nsteps && nres > threshold[step]) { |
| 146 |
if ((status = esl_abc_GuessAlphabet(ct, &alphatype)) == eslOK) goto DONE; |
| 147 |
step++; |
| 148 |
} |
| 149 |
} |
| 150 |
if (status != eslEOF) goto ERROR; |
| 151 |
status = esl_abc_GuessAlphabet(ct, &alphatype); |
| 152 |
|
| 153 |
|
| 154 |
DONE: |
| 155 |
esl_buffer_SetOffset(afp->bf, anchor); |
| 156 |
esl_buffer_RaiseAnchor(afp->bf, anchor); |
| 157 |
*ret_type = alphatype; |
| 158 |
return status; |
| 159 |
|
| 160 |
ERROR: |
| 161 |
if (anchor != -1) { |
| 162 |
esl_buffer_SetOffset(afp->bf, anchor); |
| 163 |
esl_buffer_RaiseAnchor(afp->bf, anchor); |
| 164 |
} |
| 165 |
*ret_type = eslUNKNOWN; |
| 166 |
return status; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
int |
| 215 |
esl_msafile_a2m_Read(ESL_MSAFILE *afp, ESL_MSA **ret_msa) |
| 216 |
{ |
| 217 |
ESL_MSA *msa = NULL; |
| 218 |
char **csflag = NULL; |
| 219 |
int *nins = NULL; |
| 220 |
int *this_nins = NULL; |
| 221 |
int nseq = 0; |
| 222 |
int ncons = 0; |
| 223 |
int idx; |
| 224 |
int64_t thislen; |
| 225 |
int64_t spos; |
| 226 |
int this_ncons; |
| 227 |
int cpos, bpos; |
| 228 |
char *p, *tok; |
| 229 |
esl_pos_t n, toklen; |
| 230 |
int status; |
| 231 |
|
| 232 |
ESL_DASSERT1( (afp->format == eslMSAFILE_A2M) ); |
| 233 |
|
| 234 |
afp->errmsg[0] = '\0'; |
| 235 |
|
| 236 |
if (afp->abc && (msa = esl_msa_CreateDigital(afp->abc, 16, -1)) == NULL) { status = eslEMEM; goto ERROR; } |
| 237 |
if (! afp->abc && (msa = esl_msa_Create( 16, -1)) == NULL) { status = eslEMEM; goto ERROR; } |
| 238 |
ESL_ALLOC(csflag, sizeof(char *) * msa->sqalloc); |
| 239 |
for (idx = 0; idx < msa->sqalloc; idx++) csflag[idx] = NULL; |
| 240 |
|
| 241 |
|
| 242 |
while ( (status = esl_msafile_GetLine(afp, &p, &n)) == eslOK && esl_memspn(afp->line, afp->n, " \t") == afp->n) ; |
| 243 |
if (status != eslOK) goto ERROR; |
| 244 |
|
| 245 |
|
| 246 |
while (n && isspace(*p)) { p++; n--; } |
| 247 |
if (*p != '>') ESL_XFAIL(eslEFORMAT, afp->errmsg, "expected A2M name/desc line starting with >"); |
| 248 |
|
| 249 |
do { |
| 250 |
p++; n--; |
| 251 |
|
| 252 |
if ( (status = esl_memtok(&p, &n, " \t", &tok, &toklen)) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "no name found for A2M record"); |
| 253 |
if (nseq >= msa->sqalloc) { |
| 254 |
int old_sqalloc = msa->sqalloc; |
| 255 |
if ( (status = esl_msa_Expand(msa)) != eslOK) goto ERROR; |
| 256 |
ESL_REALLOC(csflag, sizeof(char *) * msa->sqalloc); |
| 257 |
for (idx = old_sqalloc; idx < msa->sqalloc; idx++) csflag[idx] = NULL; |
| 258 |
} |
| 259 |
|
| 260 |
if ( (status = esl_msa_SetSeqName (msa, nseq, tok, toklen)) != eslOK) goto ERROR; |
| 261 |
if (n && (status = esl_msa_SetSeqDescription(msa, nseq, p, n)) != eslOK) goto ERROR; |
| 262 |
|
| 263 |
|
| 264 |
thislen = 0; |
| 265 |
this_ncons = 0; |
| 266 |
if (! this_nins) |
| 267 |
ESL_ALLOC(this_nins, sizeof(int) * 1); |
| 268 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 269 |
this_nins[cpos] = 0; |
| 270 |
|
| 271 |
while ( (status = esl_msafile_GetLine(afp, &p, &n)) == eslOK) |
| 272 |
{ |
| 273 |
while (n && isspace(*p)) { p++; n--; } |
| 274 |
if (n == 0) continue; |
| 275 |
if (*p == '>') break; |
| 276 |
|
| 277 |
ESL_REALLOC(csflag[nseq], sizeof(char) * (thislen + n + 1)); |
| 278 |
if (nseq == 0) { |
| 279 |
ESL_REALLOC(this_nins, sizeof(int) * (this_ncons + n + 1)); |
| 280 |
for (cpos = this_ncons+1; cpos <= this_ncons+n; cpos++) |
| 281 |
this_nins[cpos] = 0; |
| 282 |
} |
| 283 |
|
| 284 |
for (spos = thislen, bpos = 0; bpos < n; bpos++) |
| 285 |
{ |
| 286 |
if (p[bpos] == 'O') continue; |
| 287 |
else if (isupper(p[bpos])) { csflag[nseq][spos++] = TRUE; this_ncons++; } |
| 288 |
else if (islower(p[bpos])) { csflag[nseq][spos++] = FALSE; this_nins[this_ncons]++; } |
| 289 |
else if (p[bpos] == '-') { csflag[nseq][spos++] = TRUE; this_ncons++; } |
| 290 |
if (ncons && this_ncons > ncons) ESL_XFAIL(eslEFORMAT, afp->errmsg, "unexpected # of consensus residues, didn't match previous seq(s)"); |
| 291 |
} |
| 292 |
csflag[nseq][spos] = TRUE; |
| 293 |
|
| 294 |
if (msa->abc) { status = esl_abc_dsqcat(afp->inmap, &(msa->ax[nseq]), &thislen, p, n); } |
| 295 |
if (! msa->abc) { status = esl_strmapcat (afp->inmap, &(msa->aseq[nseq]), &thislen, p, n); } |
| 296 |
if (status == eslEINVAL) ESL_XFAIL(eslEFORMAT, afp->errmsg, "one or more invalid sequence characters"); |
| 297 |
else if (status != eslOK) goto ERROR; |
| 298 |
ESL_DASSERT1( (spos == thislen) ); |
| 299 |
} |
| 300 |
if (status != eslOK && status != eslEOF) goto ERROR; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if (thislen == 0) |
| 308 |
{ |
| 309 |
ESL_ALLOC(csflag[nseq], sizeof(char) * 1); |
| 310 |
csflag[nseq][0] = TRUE; |
| 311 |
ESL_ALLOC(msa->ax[nseq], sizeof(ESL_DSQ) * 2); |
| 312 |
msa->ax[nseq][0] = msa->ax[nseq][1] = eslDSQ_SENTINEL; |
| 313 |
} |
| 314 |
|
| 315 |
if (nseq == 0) |
| 316 |
{ |
| 317 |
ncons = this_ncons; |
| 318 |
ESL_ALLOC(nins, sizeof(int) * (ncons+1)); |
| 319 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 320 |
nins[cpos] = this_nins[cpos]; |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
if (this_ncons != ncons) ESL_XFAIL(eslEFORMAT, afp->errmsg, "unexpected # of consensus residues, didn't match previous seq(s). (Do you have an O residue?)"); |
| 325 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 326 |
nins[cpos] = ESL_MAX(nins[cpos], this_nins[cpos]); |
| 327 |
} |
| 328 |
nseq++; |
| 329 |
} while (status == eslOK); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
msa->nseq = nseq; |
| 337 |
if (msa->abc) { if ((status = a2m_padding_digital(msa, csflag, nins, ncons)) != eslOK) goto ERROR; } |
| 338 |
if (!msa->abc) { if ((status = a2m_padding_text (msa, csflag, nins, ncons)) != eslOK) goto ERROR; } |
| 339 |
|
| 340 |
if (( status = esl_msa_SetDefaultWeights(msa)) != eslOK) goto ERROR; |
| 341 |
|
| 342 |
*ret_msa = msa; |
| 343 |
free(nins); |
| 344 |
free(this_nins); |
| 345 |
for (idx = 0; idx < msa->nseq; idx++) free(csflag[idx]); |
| 346 |
free(csflag); |
| 347 |
return eslOK; |
| 348 |
|
| 349 |
ERROR: |
| 350 |
if (nins) free(nins); |
| 351 |
if (this_nins) free(this_nins); |
| 352 |
if (csflag) { |
| 353 |
for (idx = 0; idx < msa->nseq; idx++) |
| 354 |
if (csflag[idx]) free(csflag[idx]); |
| 355 |
free(csflag); |
| 356 |
} |
| 357 |
if (msa) esl_msa_Destroy(msa); |
| 358 |
return status; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
int |
| 400 |
esl_msafile_a2m_Write(FILE *fp, const ESL_MSA *msa) |
| 401 |
{ |
| 402 |
char *buf = NULL; |
| 403 |
int cpl = 60; |
| 404 |
int bpos; |
| 405 |
int pos; |
| 406 |
int is_consensus; |
| 407 |
int is_residue; |
| 408 |
int do_dotless = TRUE; |
| 409 |
int i; |
| 410 |
int sym; |
| 411 |
int status; |
| 412 |
|
| 413 |
ESL_ALLOC(buf, sizeof(char) * (cpl+1)); |
| 414 |
|
| 415 |
for (i = 0; i < msa->nseq; i++) |
| 416 |
{ |
| 417 |
|
| 418 |
if (fprintf(fp, ">%s", msa->sqname[i]) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "a2m msa file write failed"); |
| 419 |
if (msa->sqacc != NULL && msa->sqacc[i] != NULL) { if (fprintf(fp, " %s", msa->sqacc[i]) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "a2m msa file write failed"); } |
| 420 |
if (msa->sqdesc != NULL && msa->sqdesc[i] != NULL) { if (fprintf(fp, " %s", msa->sqdesc[i]) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "a2m msa file write failed"); } |
| 421 |
if (fputc('\n', fp) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "a2m msa file write failed"); |
| 422 |
|
| 423 |
if (msa->abc) |
| 424 |
{ |
| 425 |
pos = 0; |
| 426 |
while (pos < msa->alen) |
| 427 |
{ |
| 428 |
for (bpos = 0; pos < msa->alen && bpos < cpl; pos++) |
| 429 |
{ |
| 430 |
sym = msa->abc->sym[msa->ax[i][pos+1]]; |
| 431 |
is_residue = esl_abc_XIsResidue(msa->abc, msa->ax[i][pos+1]); |
| 432 |
if (msa->rf) is_consensus = (isalnum(msa->rf[pos]) ? TRUE : FALSE); |
| 433 |
else is_consensus = (esl_abc_XIsResidue(msa->abc, msa->ax[0][pos+1]) ? TRUE : FALSE); |
| 434 |
|
| 435 |
if (sym == 'O') sym = esl_abc_CGetUnknown(msa->abc); |
| 436 |
|
| 437 |
if (is_consensus) { buf[bpos++] = (is_residue ? toupper(sym) : '-'); } |
| 438 |
else if (is_residue) { buf[bpos++] = tolower(sym); } |
| 439 |
else if (! do_dotless) { buf[bpos++] = '.'; } |
| 440 |
} |
| 441 |
buf[bpos] = '\0'; |
| 442 |
if (bpos) { if (fprintf(fp, "%s\n", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "a2m msa file write failed");} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if (! msa->abc) |
| 447 |
{ |
| 448 |
pos = 0; |
| 449 |
while (pos < msa->alen) |
| 450 |
{ |
| 451 |
for (bpos = 0; pos < msa->alen && bpos < cpl; pos++) |
| 452 |
{ |
| 453 |
sym = msa->aseq[i][pos]; |
| 454 |
is_residue = isalpha(msa->aseq[i][pos]); |
| 455 |
if (msa->rf) is_consensus = (isalnum(msa->rf[pos]) ? TRUE : FALSE); |
| 456 |
else is_consensus = (isalnum(msa->aseq[0][pos]) ? TRUE : FALSE); |
| 457 |
|
| 458 |
if (sym == 'O') sym = 'X'; |
| 459 |
|
| 460 |
if (is_consensus) { buf[bpos++] = ( is_residue ? toupper(sym) : '-'); } |
| 461 |
else if (is_residue) { buf[bpos++] = tolower(sym); } |
| 462 |
else if (! do_dotless) { buf[bpos++] = '.'; } |
| 463 |
|
| 464 |
} |
| 465 |
buf[bpos] = '\0'; |
| 466 |
if (bpos) { if (fprintf(fp, "%s\n", buf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "a2m msa file write failed"); } |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
free(buf); |
| 472 |
return eslOK; |
| 473 |
|
| 474 |
ERROR: |
| 475 |
if (buf) free(buf); |
| 476 |
return status; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
static int |
| 508 |
a2m_padding_digital(ESL_MSA *msa, char **csflag, int *nins, int ncons) |
| 509 |
{ |
| 510 |
ESL_DSQ *ax = NULL; |
| 511 |
ESL_DSQ gapsym = esl_abc_XGetGap(msa->abc); |
| 512 |
int apos, cpos, spos; |
| 513 |
int alen; |
| 514 |
int icount; |
| 515 |
int idx; |
| 516 |
int status; |
| 517 |
|
| 518 |
alen = ncons; |
| 519 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 520 |
alen += nins[cpos]; |
| 521 |
|
| 522 |
ESL_ALLOC(msa->rf, sizeof(char) * (alen+1)); |
| 523 |
for (apos = 0, cpos = 0; cpos <= ncons; cpos++) |
| 524 |
{ |
| 525 |
for (icount = 0; icount < nins[cpos]; icount++) msa->rf[apos++] = '.'; |
| 526 |
if (cpos < ncons) msa->rf[apos++] = 'x'; |
| 527 |
} |
| 528 |
msa->rf[apos] = '\0'; |
| 529 |
|
| 530 |
for (idx = 0; idx < msa->nseq; idx++) |
| 531 |
{ |
| 532 |
ESL_ALLOC(ax, sizeof(ESL_DSQ) * (alen + 2)); |
| 533 |
ax[0] = eslDSQ_SENTINEL; |
| 534 |
apos = spos = 0; |
| 535 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 536 |
{ |
| 537 |
icount = 0; |
| 538 |
while (csflag[idx][spos] == FALSE) { ax[apos+1] = msa->ax[idx][spos+1]; apos++; spos++; icount++; } |
| 539 |
while (icount < nins[cpos]) { ax[apos+1] = gapsym; apos++; icount++; } |
| 540 |
if (cpos < ncons) { ax[apos+1] = msa->ax[idx][spos+1]; apos++; spos++; } |
| 541 |
} |
| 542 |
ESL_DASSERT1( (msa->ax[idx][spos+1] == eslDSQ_SENTINEL) ); |
| 543 |
ESL_DASSERT1( (apos == alen) ); |
| 544 |
ax[alen+1] = eslDSQ_SENTINEL; |
| 545 |
free(msa->ax[idx]); |
| 546 |
msa->ax[idx] = ax; |
| 547 |
ax = NULL; |
| 548 |
} |
| 549 |
msa->alen = alen; |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
return eslOK; |
| 554 |
|
| 555 |
ERROR: |
| 556 |
if (ax) free(ax); |
| 557 |
return status; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
static int |
| 562 |
a2m_padding_text(ESL_MSA *msa, char **csflag, int *nins, int ncons) |
| 563 |
{ |
| 564 |
char *aseq = NULL; |
| 565 |
int apos, cpos, spos; |
| 566 |
int alen; |
| 567 |
int icount; |
| 568 |
int idx; |
| 569 |
int status; |
| 570 |
|
| 571 |
alen = ncons; |
| 572 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 573 |
alen += nins[cpos]; |
| 574 |
|
| 575 |
ESL_ALLOC(msa->rf, sizeof(char) * (alen+1)); |
| 576 |
for (apos = 0, cpos = 0; cpos <= ncons; cpos++) |
| 577 |
{ |
| 578 |
for (icount = 0; icount < nins[cpos]; icount++) msa->rf[apos++] = '.'; |
| 579 |
if (cpos < ncons) msa->rf[apos++] = 'x'; |
| 580 |
} |
| 581 |
msa->rf[apos] = '\0'; |
| 582 |
|
| 583 |
for (idx = 0; idx < msa->nseq; idx++) |
| 584 |
{ |
| 585 |
ESL_ALLOC(aseq, sizeof(char) * (alen + 1)); |
| 586 |
apos = spos = 0; |
| 587 |
for (cpos = 0; cpos <= ncons; cpos++) |
| 588 |
{ |
| 589 |
icount = 0; |
| 590 |
while (csflag[idx][spos] == FALSE) { aseq[apos] = msa->aseq[idx][spos]; apos++; spos++; icount++; } |
| 591 |
while (icount < nins[cpos]) { aseq[apos] = '.'; apos++; icount++; } |
| 592 |
if (cpos < ncons) { aseq[apos] = msa->aseq[idx][spos]; apos++; spos++; } |
| 593 |
} |
| 594 |
ESL_DASSERT1( (msa->aseq[idx][spos] == '\0') ); |
| 595 |
ESL_DASSERT1( (apos == alen) ); |
| 596 |
aseq[alen] = '\0'; |
| 597 |
free(msa->aseq[idx]); |
| 598 |
msa->aseq[idx] = aseq; |
| 599 |
aseq = NULL; |
| 600 |
} |
| 601 |
msa->alen = alen; |
| 602 |
return eslOK; |
| 603 |
|
| 604 |
ERROR: |
| 605 |
if (aseq) free(aseq); |
| 606 |
return status; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
#ifdef eslMSAFILE_A2M_TESTDRIVE |
| 615 |
|
| 616 |
#include "esl_random.h" |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static void |
| 623 |
a2m_no_O(ESL_MSA *msa) |
| 624 |
{ |
| 625 |
int idx; |
| 626 |
int apos; |
| 627 |
int symO; |
| 628 |
|
| 629 |
ESL_DASSERT1(( msa->flags & eslMSA_DIGITAL )); |
| 630 |
ESL_DASSERT1(( msa->abc != NULL )); |
| 631 |
|
| 632 |
symO = esl_abc_DigitizeSymbol(msa->abc, 'O'); |
| 633 |
|
| 634 |
for (idx = 0; idx < msa->nseq; idx++) |
| 635 |
for (apos = 1; apos <= msa->alen; apos++) |
| 636 |
if (msa->ax[idx][apos] == symO) msa->ax[idx][apos] = esl_abc_XGetUnknown(msa->abc); |
| 637 |
} |
| 638 |
|
| 639 |
static void |
| 640 |
utest_write_good1(FILE *ofp, int *ret_alphatype, int *ret_nseq, int *ret_alen) |
| 641 |
{ |
| 642 |
fputs(">MYG_PHYCA\n", ofp); |
| 643 |
fputs("VLSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSHPETLEKFDRFKHLKTEAEMKASED\n", ofp); |
| 644 |
fputs("LKKHGVTVLTALGAILKKKGHHEAELKPLAQSHATKHKIPIKYLEFISEAIIHVLHSRHP\n", ofp); |
| 645 |
fputs("GDFGADAQGAMNKALELFRKDIAAKYKELGYQG\n", ofp); |
| 646 |
fputs(">GLB5_PETMA\n", ofp); |
| 647 |
fputs("pivdtgsvApLSAAEKTKIRSAWAPVYSTYETSGVDILVKFFTSTPAAQEFFPKFKGLTT\n", ofp); |
| 648 |
fputs("ADQLKKSADVRWHAERIINAVNDAVASMDDtekMSMKLRDLSGKHAKSFQVDPQYFKVLA\n", ofp); |
| 649 |
fputs("AVI---------ADTVAAGDAGFEKLMSMICILLRSAY-------\n", ofp); |
| 650 |
fputs(">HBB_HUMAN\n", ofp); |
| 651 |
fputs("VhLTPEEKSAVTALWGKV--NVDEVGGEALGRLLVVYPWTQRFFESFGDLSTPDAVMGNP\n", ofp); |
| 652 |
fputs("KVKAHGKKVLGAFSDGLAHLDNLKGTFATLSELHCDKLHVDPENFRLLGNVLVCVLAHHF\n", ofp); |
| 653 |
fputs("GKEFTPPVQAAYQKVVAGVANALAHKYH------\n", ofp); |
| 654 |
fputs(">HBA_HUMAN\n", ofp); |
| 655 |
fputs("VLSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHF------DLSHGSAQ\n", ofp); |
| 656 |
fputs("VKGHGKKVADALTNAVAHVDDMPNALSALSDLHAHKLRVDPVNFKLLSHCLLVTLAAHLP\n", ofp); |
| 657 |
fputs("AEFTPAVHASLDKFLASVSTVLTSKYR------\n", ofp); |
| 658 |
|
| 659 |
*ret_alphatype = eslAMINO; |
| 660 |
*ret_nseq = 4; |
| 661 |
*ret_alen = 165; |
| 662 |
} |
| 663 |
|
| 664 |
static void |
| 665 |
utest_write_good2(FILE *ofp, int *ret_alphatype, int *ret_nseq, int *ret_alen) |
| 666 |
{ |
| 667 |
fputs(">tRNA2\n", ofp); |
| 668 |
fputs("UCCGAUAUAGUGUAACGGCUAUCACAUCACGCUUUCACCGUGGAGACCGGGGUUCGACUC\n", ofp); |
| 669 |
fputs("CCCGUAUCGGAG\n", ofp); |
| 670 |
fputs(">tRNA3\n", ofp); |
| 671 |
fputs("UCCGUGAUAGUUUAAUGGUCAGAAUGG-GCGCUUGUCGCGUGCcAGAUCGGGGUUCAAUU\n", ofp); |
| 672 |
fputs("CCCCGUCGCGGAG\n", ofp); |
| 673 |
fputs(">tRNA5\n", ofp); |
| 674 |
fputs("GGGCACAUGGCGCAGUUGGUAGCGCGCUUCCCUUGCAAGGAAGaGGUCAUCGGUUCGAUU\n", ofp); |
| 675 |
fputs("CCGGUUGCGUCCA\n", ofp); |
| 676 |
fputs(">tRNA1\n", ofp); |
| 677 |
fputs("GCGGAUUUAGCUCAGUUGGGAGAGCGCCAGACUGAAGAUCUGGaGGUCCUGUGUUCGAUC\n", ofp); |
| 678 |
fputs("CACAGAAUUCGCA\n", ofp); |
| 679 |
fputs(">tRNA4\n", ofp); |
| 680 |
fputs("GCUCGUAUGGCGCAGUGG-UAGCGCAGCAGAUUGCAAAUCUGUuGGUCCUUAGUUCGAUC\n", ofp); |
| 681 |
fputs("CUGAGUGCGAGCU\n", ofp); |
| 682 |
|
| 683 |
*ret_alphatype = eslRNA; |
| 684 |
*ret_nseq = 5; |
| 685 |
*ret_alen = 73; |
| 686 |
} |
| 687 |
|
| 688 |
static void |
| 689 |
utest_goodfile(char *filename, int testnumber, int expected_alphatype, int expected_nseq, int expected_alen) |
| 690 |
{ |
| 691 |
ESL_ALPHABET *abc = NULL; |
| 692 |
ESL_MSAFILE *afp = NULL; |
| 693 |
ESL_MSA *msa1 = NULL; |
| 694 |
ESL_MSA *msa2 = NULL; |
| 695 |
char tmpfile1[32] = "esltmpXXXXXX"; |
| 696 |
char tmpfile2[32] = "esltmpXXXXXX"; |
| 697 |
FILE *ofp = NULL; |
| 698 |
int status; |
| 699 |
|
| 700 |
|
| 701 |
if ( (status = esl_msafile_Open(&abc, filename, NULL, eslMSAFILE_A2M, NULL, &afp)) != eslOK) esl_fatal("a2m good file test %d failed: digital open", testnumber); |
| 702 |
if (afp->format != eslMSAFILE_A2M) esl_fatal("a2m good file test %d failed: bad format", testnumber); |
| 703 |
if (abc->type != expected_alphatype) esl_fatal("a2m good file test %d failed: alphabet autodetection", testnumber); |
| 704 |
|
| 705 |
|
| 706 |
if ( (status = esl_msafile_a2m_Read(afp, &msa1)) != eslOK) esl_fatal("a2m good file test %d failed: msa read, digital", testnumber); |
| 707 |
if (msa1->nseq != expected_nseq || msa1->alen != expected_alen) esl_fatal("a2m good file test %d failed: nseq/alen", testnumber); |
| 708 |
if (esl_msa_Validate(msa1, NULL) != eslOK) esl_fatal("a2m good file test %d failed: msa invalid", testnumber); |
| 709 |
esl_msafile_Close(afp); |
| 710 |
|
| 711 |
|
| 712 |
if ( (status = esl_tmpfile_named(tmpfile1, &ofp)) != eslOK) esl_fatal("a2m good file test %d failed: tmpfile creation", testnumber); |
| 713 |
if ( (status = esl_msafile_a2m_Write(ofp, msa1)) != eslOK) esl_fatal("a2m good file test %d failed: msa write, digital", testnumber); |
| 714 |
fclose(ofp); |
| 715 |
|
| 716 |
|
| 717 |
if ( (status = esl_msafile_Open(NULL, tmpfile1, NULL, eslMSAFILE_A2M, NULL, &afp)) != eslOK) esl_fatal("a2m good file test %d failed: text mode open", testnumber); |
| 718 |
if ( (status = esl_msafile_a2m_Read(afp, &msa2)) != eslOK) esl_fatal("a2m good file test %d failed: msa read, text", testnumber); |
| 719 |
if (msa2->nseq != expected_nseq || msa2->alen != expected_alen) esl_fatal("a2m good file test %d failed: nseq/alen", testnumber); |
| 720 |
if (esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal("a2m good file test %d failed: msa invalid", testnumber); |
| 721 |
esl_msafile_Close(afp); |
| 722 |
|
| 723 |
|
| 724 |
if ( (status = esl_tmpfile_named(tmpfile2, &ofp)) != eslOK) esl_fatal("a2m good file test %d failed: tmpfile creation", testnumber); |
| 725 |
if ( (status = esl_msafile_a2m_Write(ofp, msa2)) != eslOK) esl_fatal("a2m good file test %d failed: msa write, text", testnumber); |
| 726 |
fclose(ofp); |
| 727 |
esl_msa_Destroy(msa2); |
| 728 |
|
| 729 |
|
| 730 |
if ( (status = esl_msafile_Open(&abc, tmpfile1, NULL, eslMSAFILE_A2M, NULL, &afp)) != eslOK) esl_fatal("a2m good file test %d failed: 2nd digital mode open", testnumber); |
| 731 |
if ( (status = esl_msafile_a2m_Read(afp, &msa2)) != eslOK) esl_fatal("a2m good file test %d failed: 2nd digital msa read", testnumber); |
| 732 |
if (esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal("a2m good file test %d failed: msa invalid", testnumber); |
| 733 |
esl_msafile_Close(afp); |
| 734 |
|
| 735 |
|
| 736 |
if (esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal("a2m good file test %d failed: msa compare", testnumber); |
| 737 |
|
| 738 |
remove(tmpfile1); |
| 739 |
remove(tmpfile2); |
| 740 |
esl_msa_Destroy(msa1); |
| 741 |
esl_msa_Destroy(msa2); |
| 742 |
esl_alphabet_Destroy(abc); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
static void |
| 747 |
write_test_msas(FILE *ofp1, FILE *ofp2) |
| 748 |
{ |
| 749 |
fprintf(ofp1, ">seq1 description line for seq1\n"); |
| 750 |
fprintf(ofp1, "ACDEFGHIKLMNPQRSTVWY\n"); |
| 751 |
fprintf(ofp1, "ACDEFGHIKLMNPQRSTVWY\n"); |
| 752 |
fprintf(ofp1, ">seq2 description line for seq2\n"); |
| 753 |
fprintf(ofp1, "ACDEFGHIKLMNPQRSTV--\n"); |
| 754 |
fprintf(ofp1, "ACDEFGHIKLMNPQRSTVWY\n"); |
| 755 |
fprintf(ofp1, "yy\n"); |
| 756 |
fprintf(ofp1, ">seq3\n"); |
| 757 |
fprintf(ofp1, "aaACDEFGHIKLMNPQRSTV\n"); |
| 758 |
fprintf(ofp1, "--ACDEFGHIKLMNPQRSTVWY\n"); |
| 759 |
fprintf(ofp1, ">seq4 \n"); |
| 760 |
fprintf(ofp1, "ACDEFGHIKLMNPQR\n"); |
| 761 |
fprintf(ofp1, "STVWYACDEFGHIKL\n"); |
| 762 |
fprintf(ofp1, "MNPQRSTVWY\n"); |
| 763 |
|
| 764 |
fprintf(ofp2, "# STOCKHOLM 1.0\n"); |
| 765 |
fprintf(ofp2, "\n"); |
| 766 |
fprintf(ofp2, "#=GS seq1 DE description line for seq1\n"); |
| 767 |
fprintf(ofp2, "#=GS seq2 DE description line for seq2\n"); |
| 768 |
fprintf(ofp2, "\n"); |
| 769 |
fprintf(ofp2, "#=GC RF ..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..\n"); |
| 770 |
fprintf(ofp2, "seq1 ..ACDEFGHIKLMNPQRSTVWYACDEFGHIKLMNPQRSTVWY..\n"); |
| 771 |
fprintf(ofp2, "seq2 ..ACDEFGHIKLMNPQRSTV--ACDEFGHIKLMNPQRSTVWYyy\n"); |
| 772 |
fprintf(ofp2, "seq3 aaACDEFGHIKLMNPQRSTV--ACDEFGHIKLMNPQRSTVWY..\n"); |
| 773 |
fprintf(ofp2, "seq4 ..ACDEFGHIKLMNPQRSTVWYACDEFGHIKLMNPQRSTVWY..\n"); |
| 774 |
fprintf(ofp2, "//\n"); |
| 775 |
} |
| 776 |
|
| 777 |
static void |
| 778 |
read_test_msas_digital(char *a2mfile, char *stkfile) |
| 779 |
{ |
| 780 |
char msg[] = "A2M msa digital read unit test failed"; |
| 781 |
ESL_ALPHABET *abc = NULL; |
| 782 |
ESL_MSAFILE *afp1 = NULL; |
| 783 |
ESL_MSAFILE *afp2 = NULL; |
| 784 |
ESL_MSA *msa1, *msa2, *msa3, *msa4; |
| 785 |
FILE *a2mfp, *stkfp; |
| 786 |
char a2mfile2[32] = "esltmpa2m2XXXXXX"; |
| 787 |
char stkfile2[32] = "esltmpstk2XXXXXX"; |
| 788 |
|
| 789 |
if ( esl_msafile_Open(&abc, a2mfile, NULL, eslMSAFILE_A2M, NULL, &afp1) != eslOK) esl_fatal(msg); |
| 790 |
if ( !abc || abc->type != eslAMINO) esl_fatal(msg); |
| 791 |
if ( esl_msafile_Open(&abc, stkfile, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg); |
| 792 |
if ( esl_msafile_a2m_Read (afp1, &msa1) != eslOK) esl_fatal(msg); |
| 793 |
if ( esl_msafile_stockholm_Read(afp2, &msa2) != eslOK) esl_fatal(msg); |
| 794 |
if ( esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal(msg); |
| 795 |
|
| 796 |
if ( esl_msafile_a2m_Read (afp1, &msa3) != eslEOF) esl_fatal(msg); |
| 797 |
if ( esl_msafile_stockholm_Read(afp2, &msa3) != eslEOF) esl_fatal(msg); |
| 798 |
|
| 799 |
esl_msafile_Close(afp2); |
| 800 |
esl_msafile_Close(afp1); |
| 801 |
|
| 802 |
|
| 803 |
if ( esl_tmpfile_named(a2mfile2, &a2mfp) != eslOK) esl_fatal(msg); |
| 804 |
if ( esl_tmpfile_named(stkfile2, &stkfp) != eslOK) esl_fatal(msg); |
| 805 |
if ( esl_msafile_a2m_Write (a2mfp, msa2) != eslOK) esl_fatal(msg); |
| 806 |
if ( esl_msafile_stockholm_Write(stkfp, msa1, eslMSAFILE_PFAM) != eslOK) esl_fatal(msg); |
| 807 |
fclose(a2mfp); |
| 808 |
fclose(stkfp); |
| 809 |
if ( esl_msafile_Open(&abc, a2mfile2, NULL, eslMSAFILE_A2M, NULL, &afp1) != eslOK) esl_fatal(msg); |
| 810 |
if ( esl_msafile_Open(&abc, stkfile2, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg); |
| 811 |
if ( esl_msafile_a2m_Read (afp1, &msa3) != eslOK) esl_fatal(msg); |
| 812 |
if ( esl_msafile_stockholm_Read(afp2, &msa4) != eslOK) esl_fatal(msg); |
| 813 |
if ( esl_msa_Compare(msa3, msa4) != eslOK) esl_fatal(msg); |
| 814 |
|
| 815 |
remove(a2mfile2); |
| 816 |
remove(stkfile2); |
| 817 |
esl_msafile_Close(afp2); |
| 818 |
esl_msafile_Close(afp1); |
| 819 |
|
| 820 |
esl_msa_Destroy(msa1); |
| 821 |
esl_msa_Destroy(msa2); |
| 822 |
esl_msa_Destroy(msa3); |
| 823 |
esl_msa_Destroy(msa4); |
| 824 |
esl_alphabet_Destroy(abc); |
| 825 |
} |
| 826 |
|
| 827 |
static void |
| 828 |
read_test_msas_text(char *a2mfile, char *stkfile) |
| 829 |
{ |
| 830 |
char msg[] = "A2M msa text-mode read unit test failed"; |
| 831 |
ESL_MSAFILE *afp1 = NULL; |
| 832 |
ESL_MSAFILE *afp2 = NULL; |
| 833 |
ESL_MSA *msa1, *msa2, *msa3, *msa4; |
| 834 |
FILE *a2mfp, *stkfp; |
| 835 |
char a2mfile2[32] = "esltmpa2m2XXXXXX"; |
| 836 |
char stkfile2[32] = "esltmpstk2XXXXXX"; |
| 837 |
|
| 838 |
|
| 839 |
if ( esl_msafile_Open(NULL, a2mfile, NULL, eslMSAFILE_A2M, NULL, &afp1) != eslOK) esl_fatal(msg); |
| 840 |
if ( esl_msafile_Open(NULL, stkfile, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg); |
| 841 |
if ( esl_msafile_a2m_Read (afp1, &msa1) != eslOK) esl_fatal(msg); |
| 842 |
if ( esl_msafile_stockholm_Read(afp2, &msa2) != eslOK) esl_fatal(msg); |
| 843 |
if ( esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal(msg); |
| 844 |
if ( esl_msafile_a2m_Read (afp1, &msa3) != eslEOF) esl_fatal(msg); |
| 845 |
if ( esl_msafile_stockholm_Read(afp2, &msa3) != eslEOF) esl_fatal(msg); |
| 846 |
esl_msafile_Close(afp2); |
| 847 |
esl_msafile_Close(afp1); |
| 848 |
|
| 849 |
if ( esl_tmpfile_named(a2mfile2, &a2mfp) != eslOK) esl_fatal(msg); |
| 850 |
if ( esl_tmpfile_named(stkfile2, &stkfp) != eslOK) esl_fatal(msg); |
| 851 |
if ( esl_msafile_a2m_Write (a2mfp, msa2) != eslOK) esl_fatal(msg); |
| 852 |
if ( esl_msafile_stockholm_Write(stkfp, msa1, eslMSAFILE_PFAM) != eslOK) esl_fatal(msg); |
| 853 |
fclose(a2mfp); |
| 854 |
fclose(stkfp); |
| 855 |
if ( esl_msafile_Open(NULL, a2mfile2, NULL, eslMSAFILE_A2M, NULL, &afp1) != eslOK) esl_fatal(msg); |
| 856 |
if ( esl_msafile_Open(NULL, stkfile2, NULL, eslMSAFILE_STOCKHOLM, NULL, &afp2) != eslOK) esl_fatal(msg); |
| 857 |
if ( esl_msafile_a2m_Read (afp1, &msa3) != eslOK) esl_fatal(msg); |
| 858 |
if ( esl_msafile_stockholm_Read(afp2, &msa4) != eslOK) esl_fatal(msg); |
| 859 |
if ( esl_msa_Compare(msa3, msa4) != eslOK) esl_fatal(msg); |
| 860 |
|
| 861 |
remove(a2mfile2); |
| 862 |
remove(stkfile2); |
| 863 |
esl_msafile_Close(afp2); |
| 864 |
esl_msafile_Close(afp1); |
| 865 |
|
| 866 |
esl_msa_Destroy(msa1); |
| 867 |
esl_msa_Destroy(msa2); |
| 868 |
esl_msa_Destroy(msa3); |
| 869 |
esl_msa_Destroy(msa4); |
| 870 |
} |
| 871 |
|
| 872 |
static void |
| 873 |
utest_gibberish(ESL_RANDOMNESS *rng) |
| 874 |
{ |
| 875 |
char msg[] = "esl_msafile_a2m gibberish test failed"; |
| 876 |
char a2mfile[32] = "esltmpXXXXXX"; |
| 877 |
FILE *fp = NULL; |
| 878 |
ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO); |
| 879 |
ESL_MSA *msa = NULL; |
| 880 |
ESL_MSAFILE *msafp = NULL; |
| 881 |
ESL_MSA *msa2 = NULL; |
| 882 |
|
| 883 |
if ( esl_msa_Sample(rng, abc, 100, 100, &msa) != eslOK) esl_fatal(msg); |
| 884 |
if ( esl_msa_Validate(msa, NULL) != eslOK) esl_fatal(msg); |
| 885 |
if ( esl_msa_FlushLeftInserts(msa) != eslOK) esl_fatal(msg); |
| 886 |
if ( esl_msa_MinimGaps(msa, NULL, NULL, TRUE) != eslOK) esl_fatal(msg); |
| 887 |
a2m_no_O(msa); |
| 888 |
ESL_DASSERT1(( !(msa->flags & eslMSA_HASWGTS) )); |
| 889 |
ESL_DASSERT1(( msa->rf != NULL )); |
| 890 |
|
| 891 |
if ( esl_tmpfile_named(a2mfile, &fp) != eslOK) esl_fatal(msg); |
| 892 |
if ( esl_msafile_a2m_Write(fp, msa) != eslOK) esl_fatal(msg); |
| 893 |
fclose(fp); |
| 894 |
|
| 895 |
if ( esl_msafile_Open(&abc, a2mfile, NULL, eslMSAFILE_A2M, NULL, &msafp) != eslOK) esl_fatal(msg); |
| 896 |
if ( esl_msafile_a2m_Read(msafp, &msa2) != eslOK) esl_fatal(msg); |
| 897 |
if ( esl_msa_Validate(msa2, NULL) != eslOK) esl_fatal(msg); |
| 898 |
if ( esl_msa_Compare(msa, msa2) != eslOK) esl_fatal(msg); |
| 899 |
esl_msafile_Close(msafp); |
| 900 |
|
| 901 |
remove(a2mfile); |
| 902 |
esl_msa_Destroy(msa); |
| 903 |
esl_msa_Destroy(msa2); |
| 904 |
esl_alphabet_Destroy(abc); |
| 905 |
} |
| 906 |
#endif |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
#ifdef eslMSAFILE_A2M_TESTDRIVE |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
#include <esl_config.h> |
| 920 |
|
| 921 |
#include <stdio.h> |
| 922 |
|
| 923 |
#include "easel.h" |
| 924 |
#include "esl_getopts.h" |
| 925 |
#include "esl_random.h" |
| 926 |
#include "esl_msafile.h" |
| 927 |
#include "esl_msafile_a2m.h" |
| 928 |
|
| 929 |
static ESL_OPTIONS options[] = { |
| 930 |
|
| 931 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 932 |
{"-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 933 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 934 |
}; |
| 935 |
static char usage[] = "[-options]"; |
| 936 |
static char banner[] = "test driver for A2M MSA format module"; |
| 937 |
|
| 938 |
int |
| 939 |
main(int argc, char **argv) |
| 940 |
{ |
| 941 |
char msg[] = "a2m MSA i/o module test driver failed"; |
| 942 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 943 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 944 |
char a2mfile[32] = "esltmpa2mXXXXXX"; |
| 945 |
char stkfile[32] = "esltmpstkXXXXXX"; |
| 946 |
FILE *a2mfp, *stkfp; |
| 947 |
int testnumber; |
| 948 |
int ngoodtests = 2; |
| 949 |
char tmpfile[32]; |
| 950 |
FILE *ofp; |
| 951 |
int expected_alphatype; |
| 952 |
int expected_nseq; |
| 953 |
int expected_alen; |
| 954 |
|
| 955 |
fprintf(stderr, "## %s\n", argv[0]); |
| 956 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 957 |
|
| 958 |
if ( esl_tmpfile_named(a2mfile, &a2mfp) != eslOK) esl_fatal(msg); |
| 959 |
if ( esl_tmpfile_named(stkfile, &stkfp) != eslOK) esl_fatal(msg); |
| 960 |
write_test_msas(a2mfp, stkfp); |
| 961 |
fclose(a2mfp); |
| 962 |
fclose(stkfp); |
| 963 |
|
| 964 |
read_test_msas_digital(a2mfile, stkfile); |
| 965 |
read_test_msas_text (a2mfile, stkfile); |
| 966 |
|
| 967 |
|
| 968 |
for (testnumber = 1; testnumber <= ngoodtests; testnumber++) |
| 969 |
{ |
| 970 |
strcpy(tmpfile, "esltmpXXXXXX"); |
| 971 |
if (esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg); |
| 972 |
switch (testnumber) { |
| 973 |
case 1: utest_write_good1 (ofp, &expected_alphatype, &expected_nseq, &expected_alen); break; |
| 974 |
case 2: utest_write_good2 (ofp, &expected_alphatype, &expected_nseq, &expected_alen); break; |
| 975 |
} |
| 976 |
fclose(ofp); |
| 977 |
utest_goodfile(tmpfile, testnumber, expected_alphatype, expected_nseq, expected_alen); |
| 978 |
remove(tmpfile); |
| 979 |
} |
| 980 |
|
| 981 |
utest_gibberish(rng); |
| 982 |
|
| 983 |
fprintf(stderr, "# status = ok\n"); |
| 984 |
|
| 985 |
remove(a2mfile); |
| 986 |
remove(stkfile); |
| 987 |
esl_randomness_Destroy(rng); |
| 988 |
esl_getopts_Destroy(go); |
| 989 |
return 0; |
| 990 |
} |
| 991 |
#endif |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
#ifdef eslMSAFILE_A2M_EXAMPLE |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
#include <stdio.h> |
| 1007 |
|
| 1008 |
#include "easel.h" |
| 1009 |
#include "esl_alphabet.h" |
| 1010 |
#include "esl_getopts.h" |
| 1011 |
#include "esl_msa.h" |
| 1012 |
#include "esl_msafile.h" |
| 1013 |
#include "esl_msafile_a2m.h" |
| 1014 |
|
| 1015 |
static ESL_OPTIONS options[] = { |
| 1016 |
|
| 1017 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1018 |
{ "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "override autodetection; force A2M format", 0 }, |
| 1019 |
{ "-q", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "quieter: don't write msa back, just summary", 0 }, |
| 1020 |
{ "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use text mode: no digital alphabet", 0 }, |
| 1021 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is DNA", 0 }, |
| 1022 |
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is RNA", 0 }, |
| 1023 |
{ "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "specify that alphabet is protein", 0 }, |
| 1024 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1025 |
}; |
| 1026 |
static char usage[] = "[-options] <msafile>"; |
| 1027 |
static char banner[] = "example of guessing, reading, writing A2M format"; |
| 1028 |
|
| 1029 |
int |
| 1030 |
main(int argc, char **argv) |
| 1031 |
{ |
| 1032 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 1033 |
char *filename = esl_opt_GetArg(go, 1); |
| 1034 |
int infmt = eslMSAFILE_UNKNOWN; |
| 1035 |
ESL_ALPHABET *abc = NULL; |
| 1036 |
ESL_MSAFILE *afp = NULL; |
| 1037 |
ESL_MSA *msa = NULL; |
| 1038 |
int status; |
| 1039 |
|
| 1040 |
if (esl_opt_GetBoolean(go, "-1")) infmt = eslMSAFILE_A2M; |
| 1041 |
|
| 1042 |
if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA); |
| 1043 |
else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA); |
| 1044 |
else if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO); |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
if (esl_opt_GetBoolean(go, "-t")) status = esl_msafile_Open(NULL, filename, NULL, infmt, NULL, &afp); |
| 1050 |
else status = esl_msafile_Open(&abc, filename, NULL, infmt, NULL, &afp); |
| 1051 |
if (status != eslOK) esl_msafile_OpenFailure(afp, status); |
| 1052 |
|
| 1053 |
if ((status = esl_msafile_a2m_Read(afp, &msa)) != eslOK) |
| 1054 |
esl_msafile_ReadFailure(afp, status); |
| 1055 |
|
| 1056 |
printf("alphabet: %s\n", (abc ? esl_abc_DecodeType(abc->type) : "none (text mode)")); |
| 1057 |
printf("# of seqs: %d\n", msa->nseq); |
| 1058 |
printf("# of cols: %d\n", (int) msa->alen); |
| 1059 |
printf("\n"); |
| 1060 |
|
| 1061 |
if (! esl_opt_GetBoolean(go, "-q")) |
| 1062 |
esl_msafile_a2m_Write(stdout, msa); |
| 1063 |
|
| 1064 |
esl_msa_Destroy(msa); |
| 1065 |
esl_msafile_Close(afp); |
| 1066 |
if (abc) esl_alphabet_Destroy(abc); |
| 1067 |
esl_getopts_Destroy(go); |
| 1068 |
exit(0); |
| 1069 |
} |
| 1070 |
|
| 1071 |
#endif |
| 1072 |
|
| 1073 |
|
| 1074 |
#ifdef eslMSAFILE_A2M_EXAMPLE2 |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
#include <stdio.h> |
| 1081 |
|
| 1082 |
#include "easel.h" |
| 1083 |
#include "esl_msa.h" |
| 1084 |
#include "esl_msafile.h" |
| 1085 |
#include "esl_msafile_a2m.h" |
| 1086 |
|
| 1087 |
int |
| 1088 |
main(int argc, char **argv) |
| 1089 |
{ |
| 1090 |
char *filename = argv[1]; |
| 1091 |
int fmt = eslMSAFILE_A2M; |
| 1092 |
ESL_MSAFILE *afp = NULL; |
| 1093 |
ESL_MSA *msa = NULL; |
| 1094 |
int status; |
| 1095 |
|
| 1096 |
if ( (status = esl_msafile_Open(NULL, filename, NULL, fmt, NULL, &afp)) != eslOK) esl_msafile_OpenFailure(afp, status); |
| 1097 |
if ( (status = esl_msafile_a2m_Read(afp, &msa)) != eslOK) esl_msafile_ReadFailure(afp, status); |
| 1098 |
|
| 1099 |
printf("%6d seqs, %5d columns\n", msa->nseq, (int) msa->alen); |
| 1100 |
|
| 1101 |
esl_msafile_a2m_Write(stdout, msa); |
| 1102 |
esl_msa_Destroy(msa); |
| 1103 |
esl_msafile_Close(afp); |
| 1104 |
exit(0); |
| 1105 |
} |
| 1106 |
|
| 1107 |
#endif |
| 1108 |
|
| 1109 |
|