-
Notifications
You must be signed in to change notification settings - Fork 0
/
msf.c
428 lines (381 loc) · 12.8 KB
/
msf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
326
327
328
329
330
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* SQUID - A C function library for biological sequence analysis
* Copyright (C) 1992-1995 Sean R. Eddy
*
* This source code is distributed under terms of the
* GNU General Public License. See the files COPYING
* and GNULICENSE for further details.
*
*/
/* msf.c
* SRE, Sun Jul 11 16:17:32 1993
*
* Import/export of GCG MSF multiple sequence alignment
* formatted files.
*
*************************************************************
* Specification of an MSF-formatted file:
* (non-official. Empirically derived by examination!)
*
* - The header apparently can consist of arbitrary information,
* specific to the program that generated the .msf file.
*
* - After the header, a line appears like this:
*
* picorna.msf MSF: 100 Type: P January 17, 1991 17:53 Check: 541
* ..
* or MSF: 171 Type: P Check: 4694 ..
* or pileup.msf MSF: 617 Type: P October 7, 1992 12:14 Check: 1 ..
*
* To check for MSF formatted files, we check for "MSF:" && "Type:"
* && "Check:".
*
* To determine end of header, we check for "..".
*
* - Then follows a list of N lines, containing name, length,
* checksum, and weight information; i.e.:
*
* Name: GLB2_MORMR oo Len: 171 Check: 6522 Weight: 2.7687
*
* - Then a separator:
* //
*
* - Then the sequences, in blocks like SELEX format, 50 symbols per line
* in groups of 10; there may or may not be coordinates written above each line.
* "."'s are gaps. We assume the sequences come in the same order their
* names did.
*
* 1 50
* Cb3 ...gpvedai .......t.. aaigr..vad tvgtgptnse aipaltaaet
* E gvenae.kgv tentna.tad fvaqpvylpe .nqt...... kv.affynrs
*******************************************************************
*
* Functions provided:
*
* int
* ReadMSF(char *filename, char ***ret_aseqs, char ***ret_names,
* int *num, struct aliinfo_s *ainfo)
*
* int
* WriteMSF(FILE *fp, char **aseqs, char **names, int *weights, int num)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "squid.h"
#ifdef MEMDEBUG
#include "dbmalloc.h"
#endif
/* Function: ReadMSF()
*
* Purpose: Read multiple aligned sequences from the file seqfile.
* Returns aseqs (aligned sequences), names, weights,
* and num (number of sequences).
*
* Memory is allocated for aseqs, names, and weights;
* must be free'd by caller.
*
* Return: 1 on success, 0 on failure.
*/
int
ReadMSF(char *seqfile, /* file to read seqs from */
char ***ret_aseqs, /* RETURN: aligned seqs */
int *ret_num, /* RETURN: number of seqs */
struct aliinfo_s *ainfo)
{
FILE *fp; /* ptr to opened seqfile */
char **aseqs; /* aligned seqs */
int num; /* number of seqs read */
char buffer[LINEBUFLEN]; /* input buffer for lines */
int blocknum; /* number of blocks in file */
char *sptr; /* ptr into sequence on line */
int currblock; /* index for blocks */
int i; /* loop counter */
int idx; /* counter for seqs */
int grp; /* counter for 5 seq groups per line */
int pos; /* counter for position in a seq */
int count;
int inblock;
/***************************************************
* First pass across file.
* Verify that it looks like an MSF file.
* Count names (Name: Len: Check: Weight:)
* Count blocks (50 * blocks is the maximum seqlen we'll have to deal with.)
***************************************************/
/* open the file for reading */
fp = fopen(seqfile, "r");
if (fp == NULL) { squid_errno = SQERR_NOFILE; return 0; }
/* look for MSF: header line */
do
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
} while ((strstr(buffer, "MSF:") == NULL) &&
(strstr(buffer, "Type:") == NULL) &&
(strstr(buffer, "Check:") == NULL));
/* count the names */
num = 0;
do
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
if (strstr(buffer, "Name:") != NULL &&
strstr(buffer, "Len:") != NULL &&
strstr(buffer, "Check:") != NULL &&
strstr(buffer, "Weight:") != NULL)
num++;
} while ((strstr(buffer, "//") == NULL));
/* count blocks of sequence */
blocknum = 0;
inblock = FALSE;
while (fgets(buffer, LINEBUFLEN, fp) != NULL)
if (strtok(buffer, WHITESPACE) == NULL)
inblock = FALSE;
else if (! inblock)
{ inblock = TRUE; blocknum++; }
#ifdef SRE_REMOVED
/* if seqlines isn't evenly divisible
by sequences, we have a problem */
if (seqlines % num && seqlines % (num+1))
{ squid_errno = SQERR_FORMAT; return 0; }
blocknum = seqlines / num;
#endif
/***************************************************
* Rewind file for second pass; skip header; position on first Name line
***************************************************/
rewind(fp);
do
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
} while ((strstr(buffer, "MSF:") == NULL) ||
(strstr(buffer, "Type:") == NULL) ||
(strstr(buffer, "Check:") == NULL));
do
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
} while (strstr(buffer, "Name:") == NULL ||
strstr(buffer, "Len:") == NULL ||
strstr(buffer, "Check:") == NULL ||
strstr(buffer, "Weight:") == NULL);
/***************************************************
* Parse the name lines, to get name and weight
***************************************************/
/* allocations for num sequences */
if ((aseqs = (char **) malloc
(num * sizeof(char *))) == NULL ||
(ainfo->sqinfo = (struct seqinfo_s *) malloc
(num * sizeof(struct seqinfo_s))) == NULL)
{ squid_errno = SQERR_MEM; return 0; }
ainfo->flags = 0;
idx = 0;
while (strstr(buffer, "Name:") != NULL)
{
ainfo->sqinfo[idx].flags = 0;
/* Name: foo */
sptr = strtok(buffer, WHITESPACE);
sptr = strtok(NULL, WHITESPACE);
SetSeqinfoString(&(ainfo->sqinfo[idx]), sptr, SQINFO_NAME);
/* Len: 000 */
/* Check: 0000 */
/* Weight: 1.00 */
while (strcmp(sptr, "Weight:") != 0)
sptr = strtok(NULL, WHITESPACE);
sptr = strtok(NULL, WHITESPACE);
SetSeqinfoString(&(ainfo->sqinfo[idx]), sptr, SQINFO_WGT);
idx++;
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
}
/* skip to separator */
do
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
} while ((strstr(buffer, "//") == NULL));
/***************************************************
* Read the sequences.
***************************************************/
/* allocations for sequences */
for (idx = 0; idx < num; idx++)
if ((aseqs[idx] = (char *) malloc ((blocknum * 50 + 1) * sizeof(char))) == NULL)
{ squid_errno = SQERR_MEM; return 0; }
/* for each block of sequences: */
for (currblock = 0; currblock < blocknum; currblock++)
{
/* skip blank lines, until name field
matches names[0] */
do
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
} while ((sptr = strtok(buffer, WHITESPACE)) == NULL ||
strcmp(ainfo->sqinfo[0].name, sptr) != 0);
/* for each sequence */
for (idx = 0; idx < num; idx++)
{
/* for each of 5 groups of 10 */
for (grp = 0; grp < 5; grp++)
{
if ((sptr = strtok(NULL, WHITESPACE)) == NULL)
break;
pos = (currblock * 50) + (grp * 10);
for (i = 0; i < 10; i++)
{
if (strchr(WHITESPACE, sptr[i]) != NULL)
{ aseqs[idx][pos + i] = '\0'; break; }
if (isgap((int) sptr[i])) aseqs[idx][pos+i] = '.';
else aseqs[idx][pos+i] = sptr[i];
}
}
/* get next line, if we expect one */
if (idx < num-1)
{
if (fgets(buffer, LINEBUFLEN, fp) == NULL)
{ squid_errno = SQERR_NODATA; return 0; }
if ((sptr = strtok(buffer, WHITESPACE)) == NULL)
{squid_errno = SQERR_NODATA; return 0; }
}
}
}
/* guarantee NULL termination all sequences */
for (idx = 0; idx < num; idx++)
aseqs[idx][blocknum * 50] = '\0';
/* make sure alignment is flushed right */
FlushAlignment(aseqs, num, &(ainfo->alen));
ainfo->flags |= AINFO_ALEN;
/* find raw sequence lengths for sqinfo */
for (idx = 0; idx < num; idx++)
{
count = 0;
for (sptr = aseqs[idx]; *sptr != '\0'; sptr++)
if (!isgap(*sptr)) count++;
ainfo->sqinfo[idx].len = count;
ainfo->sqinfo[idx].flags |= SQINFO_LEN;
}
fclose(fp);
*ret_num = num;
*ret_aseqs = aseqs;
return 1;
}
/* Function: WriteMSF()
*
* Purpose: Write aseqs, names, weights to an open fp,
* in GCG MSF format. The alignment must
* be flushed (all aseqs the same length, padded
* with gaps)
*
* Returns 1 on success. Returns 0 on failure, and sets
* squid_errno to indicate the cause.
*/
int
WriteMSF(FILE *fp, /* open fp for writing */
char **aseqs, /* aligned sequences */
int num,
struct aliinfo_s *ainfo)
{
int still_going; /* True if writing another block */
int idx; /* counter for sequences */
int pos; /* position counter */
int namelen; /* maximum name length used */
int len; /* tmp variable for name lengths */
char buffer[51]; /* buffer for writing seq */
char **sqptr; /* ptrs into each sequence */
int charcount; /* num. symbols we're writing */
double weight;
/* allocate seq pointers that we'll
move across each sequence */
if ((sqptr = (char **) malloc (num * sizeof(char *))) == NULL)
{ squid_errno = SQERR_MEM; return 0; }
/* set sqptrs to start of each seq */
for (idx = 0; idx < num; idx++)
sqptr[idx] = aseqs[idx];
/* calculate max namelen used */
namelen = 0;
for (idx = 0; idx < num; idx++)
if ((len = strlen(ainfo->sqinfo[idx].name)) > namelen)
namelen = len;
/*****************************************************
* Write the title line
*****************************************************/
fprintf(fp, "\n");
/* ack! we're writing bullshit here */
fprintf(fp, " MSF: 000 Type: X Check: 0000 ..\n");
fprintf(fp, "\n");
/*****************************************************
* Write the names
*****************************************************/
for (idx = 0; idx < num; idx++)
{
weight = 1.0;
if (ainfo->sqinfo[idx].flags & SQINFO_WGT)
weight = ainfo->sqinfo[idx].weight;
fprintf(fp, " Name: %-*.*s Len: %5d Check: %5d Weight: %.4f\n",
namelen, namelen,
ainfo->sqinfo[idx].name,
ainfo->alen,
GCGchecksum(aseqs[idx], ainfo->alen),
weight);
}
fprintf(fp, "\n");
fprintf(fp, "//\n");
fprintf(fp, "\n");
/*****************************************************
* Write the sequences
*****************************************************/
still_going = 1;
while (still_going)
{
still_going = 0;
for (idx = 0; idx < num; idx++)
{
fprintf(fp, "%-*.*s ", namelen, namelen,
ainfo->sqinfo[idx].name);
/* get next line's worth of 50 from seq */
strncpy(buffer, sqptr[idx], 50);
buffer[50] = '\0';
charcount = strlen(buffer);
/* is there still more to go? */
if (charcount == 50 && sqptr[idx][50] != '\0')
still_going = 1;
/* shift the seq ptr by a line */
sqptr[idx] += charcount;
/* draw the sequence line */
pos = 0;
while (pos < charcount)
{
if (isgap(buffer[pos])) fputc('.', fp);
else fputc(buffer[pos], fp);
pos++;
if (!(pos % 10)) fputc(' ', fp);
}
fputc('\n', fp);
}
/* put blank line between blocks */
fputc('\n', fp);
}
free(sqptr);
return 1;
}
void
FlushAlignment(char **aseqs, int num, int *ret_alen)
{
int len, alen;
int idx;
int apos;
alen = strlen(aseqs[0]);
for (idx = 1; idx < num; idx++)
if ((len = strlen(aseqs[idx])) > alen)
alen = len;
for (idx = 0; idx < num; idx++)
{
if ((aseqs[idx] = (char *) realloc (aseqs[idx], sizeof(char) * (alen+1))) == NULL)
Die("realloc failed");
for (apos = strlen(aseqs[idx]); apos < alen; apos++)
aseqs[idx][apos] = '.';
aseqs[idx][apos] = '\0';
}
*ret_alen = alen;
}