1 /*****************************************************************
2 * SQUID - a library of functions for biological sequence analysis
3 * Copyright (C) 1992-2002 Washington University School of Medicine
5 * This source code is freely distributed under the terms of the
6 * GNU General Public License. See the files COPYRIGHT and LICENSE
8 *****************************************************************/
11 * SRE, Mon Jun 14 14:08:33 1999 [St. Louis]
13 * Import/export of PHYLIP interleaved multiple sequence alignment
16 * RCS $Id: phylip.c 217 2011-03-19 10:27:10Z andreas $ (Original squid RCS Id: phylip.c,v 1.1 1999/07/15 22:29:20 eddy Exp)
26 #ifdef TESTDRIVE_PHYLIP
27 /*****************************************************************
28 * phylip.c test driver:
32 main(int argc, char **argv)
40 if ((afp = MSAFileOpen(file, MSAFILE_UNKNOWN, NULL)) == NULL)
41 Die("Couldn't open %s\n", file);
43 printf("format %d\n", afp->format);
45 while ((msa = ReadPhylip(afp)) != NULL)
47 WritePhylip(stdout, msa);
54 /******************************************************************/
55 #endif /* testdrive_phylip */
59 /* Function: ReadPhylip()
60 * Date: SRE, Fri Jun 18 12:59:37 1999 [Sanger Centre]
62 * Purpose: Parse an alignment from an open Phylip format
63 * alignment file. Phylip is a single-alignment format.
64 * Return the alignment, or NULL if we have no data.
66 * Args: afp - open alignment file
68 * Returns: MSA * - an alignment object
69 * Caller responsible for an MSAFree()
70 * NULL if no more alignments
73 ReadPhylip(MSAFILE *afp)
77 char name[11]; /* seq name max len = 10 char */
79 int idx; /* index of current sequence */
83 if (feof(afp->f)) return NULL;
85 /* Skip until we see a nonblank line; it's the header,
86 * containing nseq/alen
89 while ((s = MSAFileGetLine(afp)) != NULL)
91 if ((s1 = sre_strtok(&s, WHITESPACE, NULL)) == NULL) continue;
92 if ((s2 = sre_strtok(&s, WHITESPACE, NULL)) == NULL)
93 Die("Failed to parse nseq/alen from first line of PHYLIP file %s\n", afp->fname);
94 if (! IsInt(s1) || ! IsInt(s2))
95 Die("nseq and/or alen not an integer in first line of PHYLIP file %s\n", afp->fname);
101 msa = MSAAlloc(nseq, 0);
104 while ((s = MSAFileGetLine(afp)) != NULL)
106 /* ignore blank lines. nonblank lines start w/ nonblank char */
107 if (isspace(*s)) continue;
108 /* First block has seq names */
110 strncpy(name, s, 10);
112 GKIStoreKey(msa->index, name);
113 msa->sqname[idx] = sre_strdup(name, -1);
116 /* be careful of trailing whitespace on lines */
117 if ((s1 = sre_strtok(&s, WHITESPACE, &slen)) == NULL)
118 Die("Failed to parse sequence at line %d of PHYLIP file %s\n",
119 afp->linenumber, afp->fname);
120 msa->sqlen[idx] = sre_strcat(&(msa->aseq[idx]), msa->sqlen[idx], s1, slen);
123 if (idx == nseq) { idx = 0; nblock++; }
126 MSAVerifyParse(msa); /* verifies; sets alen, wgt; frees sqlen[] */
132 /* Function: WritePhylip()
133 * Date: SRE, Fri Jun 18 12:07:41 1999 [Sanger Centre]
135 * Purpose: Write an alignment in Phylip format to an open file.
137 * Args: fp - file that's open for writing.
138 * msa - alignment to write.
143 WritePhylip(FILE *fp, MSA *msa)
145 int idx; /* counter for sequences */
146 int cpl = 50; /* 50 seq char per line */
147 char buf[51]; /* buffer for writing seq */
150 /* First line has nseq, alen
152 fprintf(fp, " %d %d\n", msa->nseq, msa->alen);
154 /* Alignment section.
155 * PHYLIP is a multiblock format, blocks (optionally) separated
156 * by blanks; names only attached to first block. Names are
157 * restricted to ten char; we achieve this by simple truncation (!).
158 * (Do we need to convert gap characters from our ./- convention?)
160 for (pos = 0; pos < msa->alen; pos += cpl)
162 if (pos > 0) fprintf(fp, "\n");
164 for (idx = 0; idx < msa->nseq; idx++)
166 strncpy(buf, msa->aseq[idx] + pos, cpl);
168 if (pos > 0) fprintf(fp, "%s\n", buf);
169 else fprintf(fp, "%-10.10s%s\n", msa->sqname[idx], buf);