1 /*****************************************************************
2 * HMMER - Biological sequence analysis with profile HMMs
3 * Copyright (C) 1992-1999 Washington University School of Medicine
6 * This source code is distributed under the terms of the
7 * GNU General Public License. See the files COPYING and LICENSE
9 *****************************************************************/
13 * reading/writing A2M (aligned FASTA) files.
15 * RCS $Id: a2m.c,v 1.1.1.1 2005/03/22 08:34:17 cmzmasek Exp $
24 /* Function: ReadA2M()
25 * Date: SRE, Sun Jun 6 17:11:29 1999 [bus from Madison 1999 worm mtg]
27 * Purpose: Parse an alignment read from an open A2M format
28 * alignment file. A2M is a single alignment format.
29 * Return the alignment, or NULL if we've already
32 * Args: afp - open alignment file
34 * Returns: MSA * - an alignment object.
35 * Caller responsible for an MSAFree()
48 if (feof(afp->f)) return NULL;
51 msa = MSAAlloc(10, 0);
53 while ((buf = MSAFileGetLine(afp)) != NULL)
57 buf++; /* skip the '>' */
58 if ((name = sre_strtok(&buf, WHITESPACE, &len1)) == NULL)
59 Die("Blank name in A2M file %s (line %d)\n", afp->fname, afp->linenumber);
60 desc = sre_strtok(&buf, "\n", &len2);
62 idx = GKIStoreKey(msa->index, name);
63 if (idx >= msa->nseqalloc) MSAExpand(msa);
65 msa->sqname[idx] = sre_strdup(name, len1);
66 if (desc != NULL) MSASetSeqDescription(msa, idx, desc);
69 else if (name != NULL)
71 if ((seq = sre_strtok(&buf, WHITESPACE, &len1)) == NULL) continue;
72 msa->sqlen[idx] = sre_strcat(&(msa->aseq[idx]), msa->sqlen[idx], seq, len1);
75 if (name == NULL) { MSAFree(msa); return NULL; }
82 /* Function: WriteA2M()
83 * Date: SRE, Sun Jun 6 17:40:35 1999 [bus from Madison, 1999 worm mtg]
85 * Purpose: Write an "aligned FASTA" (aka a2m, to UCSC) formatted
88 * Args: fp - open FILE to write to.
89 * msa - alignment to write
94 WriteA2M(FILE *fp, MSA *msa)
96 int idx; /* sequence index */
97 int pos; /* position in sequence */
98 char buf[64]; /* buffer for individual lines */
99 int cpl = 60; /* char per line; must be < 64 unless buf is bigger */
102 for (idx = 0; idx < msa->nseq; idx++)
104 fprintf(fp, ">%s %s\n",
106 (msa->sqdesc != NULL && msa->sqdesc[idx] != NULL) ? msa->sqdesc[idx] : "");
107 for (pos = 0; pos < msa->alen; pos+=cpl)
109 strncpy(buf, &(msa->aseq[idx][pos]), cpl);
110 fprintf(fp, "%s\n", buf);