-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqio.c
1619 lines (1404 loc) · 41.6 KB
/
sqio.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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.
*
*/
/* File: sqio.c
* From: ureadseq.c in Don Gilbert's sequence i/o package
*
* Reads and writes nucleic/protein sequence in various
* formats. Data files may have multiple sequences.
*
* Heavily modified from READSEQ package
* Copyright (C) 1990 by D.G. Gilbert
* Biology Dept., Indiana University, Bloomington, IN 47405
* email: [email protected]
* Thanks Don!
*
* SRE: Modifications as noted. Fri Jul 3 09:44:54 1992
* Packaged for squid, Thu Oct 1 10:07:11 1992
* ANSI conversion in full swing, Mon Jul 12 12:22:21 1993
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef SEEK_SET
#include <unistd.h> /* may Sun Microsystems rot in hell */
#endif
#include "squid.h"
#ifdef MEMDEBUG
#include "dbmalloc.h"
#endif
/* Our hack for "sequential" access of sequences in
* interleaved-format alignment files (MSF, SELEX):
* static pointers at data for the entire alignment.
*/
static char **ali_aseqs;
static char **ali_rseqs;
static struct aliinfo_s ali_ainfo;
static int ali_num;
static int curridx;
#define kStartLength 500
static char *aminos = "ABCDEFGHIKLMNPQRSTVWXYZ*";
static char *primenuc = "ACGTUN";
static char *protonly = "EFIPQZ";
/* static char stdsymbols[6] = "_.-*?"; */
static char allsymbols[32] = "_.-*?<>{}[]()!@#$%^&=+;:'|`~\"\\";
static char *seqsymbols = allsymbols;
/*
use general form of isseqchar -- all chars + symbols.
no formats except nbrf (?) use symbols in data area as
anything other than sequence chars.
(wrong. PIR-CODATA does. Remove /)
*/
void
FreeSequence(char *seq, SQINFO *sqinfo)
{
if (seq != NULL) free(seq);
if (sqinfo->flags & SQINFO_SS) free(sqinfo->ss);
if (sqinfo->flags & SQINFO_FREE) free(sqinfo->free);
if (sqinfo->flags & SQINFO_SA) free(sqinfo->sa);
}
int
SetSeqinfoString(SQINFO *sqinfo, char *sptr, int flag)
{
int len;
if (flag != SQINFO_FREE)
while (*sptr == ' ') sptr++; /* ignore leading whitespace */
switch (flag) {
case SQINFO_NAME:
if (*sptr != '-')
{
strncpy(sqinfo->name, sptr, 31);
sqinfo->name[31] = '\0';
sqinfo->flags |= SQINFO_NAME;
}
break;
case SQINFO_ID:
if (*sptr != '-')
{
strncpy(sqinfo->id, sptr, 31);
sqinfo->id[31] = '\0';
sqinfo->flags |= SQINFO_ID;
}
break;
case SQINFO_ACC:
if (*sptr != '-')
{
strncpy(sqinfo->acc, sptr, 31);
sqinfo->acc[31] = '\0';
sqinfo->flags |= SQINFO_ACC;
}
break;
case SQINFO_DESC:
if (*sptr != '-')
{
if (sqinfo->flags & SQINFO_DESC) /* append? */
{
len = strlen(sqinfo->desc);
if (len < 126) /* is there room? */
{
strncat(sqinfo->desc, " ", 127-len); len++;
strncat(sqinfo->desc, sptr, 127-len);
}
}
else /* else copy */
strncpy(sqinfo->desc, sptr, 127);
sqinfo->desc[127] = '\0';
sqinfo->flags |= SQINFO_DESC;
}
break;
case SQINFO_START:
if (!IsInt(sptr)) { squid_errno = SQERR_FORMAT; return 0; }
sqinfo->start = atoi(sptr);
if (sqinfo->start != 0) sqinfo->flags |= SQINFO_START;
break;
case SQINFO_STOP:
if (!IsInt(sptr)) { squid_errno = SQERR_FORMAT; return 0; }
sqinfo->stop = atoi(sptr);
if (sqinfo->stop != 0) sqinfo->flags |= SQINFO_STOP;
break;
case SQINFO_OLEN:
if (!IsInt(sptr)) { squid_errno = SQERR_FORMAT; return 0; }
sqinfo->olen = atoi(sptr);
if (sqinfo->olen != 0) sqinfo->flags |= SQINFO_OLEN;
break;
case SQINFO_WGT:
if (*sptr != '-')
{
if (!IsReal(sptr)) { squid_errno = SQERR_FORMAT; return 0; }
sqinfo->weight = (float) atof(sptr);
sqinfo->flags |= SQINFO_WGT;
}
break;
case SQINFO_FREE:
if (sqinfo->flags & SQINFO_FREE) /* append? */
{
len = strlen(sqinfo->free) + strlen(sptr);
sqinfo->free = (char *) ReallocOrDie (sqinfo->free,
(len + 2) * sizeof(char));
strcat(sqinfo->free, sptr);
strcat(sqinfo->free, "\n");
}
else /* else copy */
{
len = strlen(sptr);
sqinfo->free = (char *) MallocOrDie ((len +2) * sizeof(char));
strcpy(sqinfo->free, sptr);
strcat(sqinfo->free, "\n");
sqinfo->flags |= SQINFO_FREE;
}
break;
default:
Die("Invalid flag %d to SetSeqinfoString()");
}
return 1;
}
void
SeqinfoCopy(SQINFO *sq1, SQINFO *sq2)
{
sq1->flags = sq2->flags;
if (sq2->flags & SQINFO_NAME) strcpy(sq1->name, sq2->name);
if (sq2->flags & SQINFO_ID) strcpy(sq1->id, sq2->id);
if (sq2->flags & SQINFO_ACC) strcpy(sq1->acc, sq2->acc);
if (sq2->flags & SQINFO_DESC) strcpy(sq1->desc, sq2->desc);
if (sq2->flags & SQINFO_LEN) sq1->len = sq2->len;
if (sq2->flags & SQINFO_START) sq1->start = sq2->start;
if (sq2->flags & SQINFO_STOP) sq1->stop = sq2->stop;
if (sq2->flags & SQINFO_OLEN) sq1->olen = sq2->olen;
if (sq2->flags & SQINFO_TYPE) sq1->type = sq2->type;
if (sq2->flags & SQINFO_WGT) sq1->weight = sq2->weight;
if (sq2->flags & SQINFO_SS) sq1->ss = strdup(sq2->ss);
if (sq2->flags & SQINFO_FREE) sq1->free = strdup(sq2->free);
if (sq2->flags & SQINFO_SA) sq1->sa = strdup(sq2->sa);
}
/* Function: ToDNA()
*
* Purpose: Convert a sequence to DNA.
* U --> T
*/
void
ToDNA(char *seq)
{
for (; *seq != '\0'; seq++)
{
if (*seq == 'U') *seq = 'T';
else if (*seq == 'u') *seq = 't';
}
}
/* Function: ToRNA()
*
* Purpose: Convert a sequence to RNA.
* T --> U
*/
void
ToRNA(char *seq)
{
for (; *seq != '\0'; seq++)
{
if (*seq == 'T') *seq = 'U';
else if (*seq == 't') *seq = 'u';
}
}
static int
isSeqChar(int c)
{
if (c > 127) return 0; /* IRIX 4.0 bug! isascii(255) returns TRUE */
return (isalpha(c) || strchr(seqsymbols,c));
}
static void
readline(FILE *f, char *s)
{
char *cp;
if (NULL == fgets(s, LINEBUFLEN, f))
*s = 0;
else {
cp = strchr(s, '\n');
if (cp != NULL) *cp = 0;
}
}
static void
getlines(struct ReadSeqVars *V)
{
readline(V->f, V->sbuffer);
}
/* Function: addseq()
*
* Purpose: Add a line of sequence to the growing string in V.
*/
static void
addseq(char *s, struct ReadSeqVars *V)
{
char *ptr;
while (*s != 0) {
if (isSeqChar((int) *s)) {
if (*s == '-' && V->dash_equals_n) *s = 'N';
if (V->seqlen >= V->maxseq) {
V->maxseq += kStartLength;
ptr = (char*) realloc(V->seq, V->maxseq+1);
if (ptr==NULL) {
squid_errno = SQERR_MEM;
return;
}
else V->seq = ptr;
}
V->seq[(V->seqlen)++] = *s;
}
s++;
}
}
static void
addstruc(char *s, struct ReadSeqVars *V)
{
char *sptr;
if (! (V->sqinfo->flags & SQINFO_SS))
{
if ((V->sqinfo->ss = (char *) malloc ((V->maxseq+1) * sizeof(char))) == NULL)
{ squid_errno = SQERR_MEM; return; }
V->sqinfo->flags |= SQINFO_SS;
sptr = V->sqinfo->ss;
}
else
{
if ((V->sqinfo->ss = (char *) realloc(V->sqinfo->ss, V->maxseq+1)) == NULL)
{ squid_errno = SQERR_MEM; return; }
sptr = V->sqinfo->ss;
while (*sptr != '\0') sptr++;
}
while (*s != 0)
{
if (isSeqChar((int)*s)) { *sptr = *s; sptr++; }
s++;
}
*sptr = '\0';
}
static void
readLoop(int addfirst, int (*endTest)(char *,int *), struct ReadSeqVars *V)
{
int addend = 0;
int done = 0;
V->seqlen = 0;
if (addfirst) addseq(V->sbuffer, V);
do {
getlines(V);
done = feof(V->f);
done |= (*endTest)(V->sbuffer, &addend);
if (addend || !done)
addseq(V->sbuffer, V);
} while (!done);
}
static int
endPIR(char *s, int *addend)
{
*addend = 0;
if ((strncmp(s, "///", 3) == 0) ||
(strncmp(s, "ENTRY", 5) == 0))
return 1;
else
return 0;
}
static void
readPIR(struct ReadSeqVars *V)
{
char *sptr;
/* load first line of entry */
while (!feof(V->f) && strncmp(V->sbuffer, "ENTRY", 5) != 0)
getlines(V);
if (feof(V->f)) return;
if ((sptr = strtok(V->sbuffer + 15, "\n\t ")) != NULL)
{
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ID);
}
do {
getlines(V);
if (!feof(V->f) && strncmp(V->sbuffer, "TITLE", 5) == 0)
SetSeqinfoString(V->sqinfo, V->sbuffer+15, SQINFO_DESC);
else if (!feof(V->f) && strncmp(V->sbuffer, "ACCESSION", 9) == 0)
{
if ((sptr = strtok(V->sbuffer+15, " \t\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ACC);
}
} while (! feof(V->f) && (strncmp(V->sbuffer,"SEQUENCE", 8) != 0));
getlines(V); /* skip next line, coords */
readLoop(0, endPIR, V);
/* reading a real PIR-CODATA database file, we keep the source coords
*/
V->sqinfo->start = 1;
V->sqinfo->stop = V->seqlen;
V->sqinfo->olen = V->seqlen;
V->sqinfo->flags |= SQINFO_START | SQINFO_STOP | SQINFO_OLEN;
/* get next line
*/
while (!feof(V->f) && strncmp(V->sbuffer, "ENTRY", 5) != 0)
getlines(V);
}
static int
endIG(char *s, int *addend)
{
*addend = 1; /* 1 or 2 occur in line w/ bases */
return((strchr(s,'1')!=NULL) || (strchr(s,'2')!=NULL));
}
static void
readIG(struct ReadSeqVars *V)
{
char *nm;
/* position past ';' comments */
do {
getlines(V);
} while (! (feof(V->f) || ((*V->sbuffer != 0) && (*V->sbuffer != ';')) ));
if (!feof(V->f))
{
if ((nm = strtok(V->sbuffer, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, nm, SQINFO_NAME);
readLoop(0, endIG, V);
}
while (!(feof(V->f) || ((*V->sbuffer != '\0') && (*V->sbuffer == ';'))))
getlines(V);
}
static int
endStrider(char *s, int *addend)
{
*addend = 0;
return (strstr( s, "//") != NULL);
}
static void
readStrider(struct ReadSeqVars *V)
{
char *nm;
while ((!feof(V->f)) && (*V->sbuffer == ';'))
{
if (strncmp(V->sbuffer,"; DNA sequence", 14) == 0)
{
if ((nm = strtok(V->sbuffer+16, ",\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, nm, SQINFO_NAME);
}
getlines(V);
}
if (! feof(V->f))
readLoop(1, endStrider, V);
/* load next line
*/
while ((!feof(V->f)) && (*V->sbuffer != ';'))
getlines(V);
}
static int
endGB(char *s, int *addend)
{
*addend = 0;
return ((strstr(s,"//") != NULL) || (strstr(s,"LOCUS") == s));
}
static void
readGenBank(struct ReadSeqVars *V)
{
char *sptr;
int in_definition;
while (strncmp(V->sbuffer, "LOCUS", 5) != 0)
getlines(V);
if ((sptr = strtok(V->sbuffer+12, "\n\t ")) != NULL)
{
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ID);
}
in_definition = FALSE;
while (! feof(V->f))
{
getlines(V);
if (! feof(V->f) && strstr(V->sbuffer, "DEFINITION") == V->sbuffer)
{
if ((sptr = strtok(V->sbuffer+12, "\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_DESC);
in_definition = TRUE;
}
else if (! feof(V->f) && strstr(V->sbuffer, "ACCESSION") == V->sbuffer)
{
if ((sptr = strtok(V->sbuffer+12, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ACC);
in_definition = FALSE;
}
else if (strncmp(V->sbuffer,"ORIGIN", 6) != 0)
{
if (in_definition)
SetSeqinfoString(V->sqinfo, V->sbuffer, SQINFO_DESC);
else
SetSeqinfoString(V->sqinfo, V->sbuffer, SQINFO_FREE);
}
else
break;
}
readLoop(0, endGB, V);
/* reading a real GenBank database file, we keep the source coords
*/
V->sqinfo->start = 1;
V->sqinfo->stop = V->seqlen;
V->sqinfo->olen = V->seqlen;
V->sqinfo->flags |= SQINFO_START | SQINFO_STOP | SQINFO_OLEN;
while (!(feof(V->f) || ((*V->sbuffer!=0) && (strstr(V->sbuffer,"LOCUS") == V->sbuffer))))
getlines(V);
/* SRE: V->s now holds "//", so sequential
reads are wedged: fixed Tue Jul 13 1993 */
while (!feof(V->f) && strstr(V->sbuffer, "LOCUS ") != V->sbuffer)
getlines(V);
}
static int
endNBRF(char *s, int *addend)
{
char *a;
if ((a = strchr(s, '*')) != NULL) { /* end of 1st seq */
/* "*" can be valid base symbol, drop it here */
*a = 0;
*addend = 1;
return(1);
}
else if (*s == '>') { /* start of next seq */
*addend = 0;
return(1);
}
else
return(0);
}
static void
readNBRF(struct ReadSeqVars *V)
{
char *sptr;
if ((sptr = strtok(V->sbuffer+4, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
getlines(V); /*skip title-junk line*/
readLoop(0, endNBRF, V);
while (!(feof(V->f) || (*V->sbuffer != 0 && *V->sbuffer == '>')))
getlines(V);
}
static int
endPearson(char *s, int *addend)
{
*addend = 0;
return(*s == '>');
}
static void
readPearson(struct ReadSeqVars *V)
{
char *sptr;
/* check for my special FASTA format */
if (strstr(V->sbuffer, "..") != NULL && strstr(V->sbuffer, "::") != NULL)
{
if ((sptr = strtok(V->sbuffer+1, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
if ((sptr = strtok(NULL, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_WGT);
if ((sptr = strtok(NULL, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ID);
if ((sptr = strtok(NULL, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ACC);
if ((sptr = strtok(NULL, ".")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_START);
if ((sptr = strtok(NULL, ".:")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_STOP);
if ((sptr = strtok(NULL, ":\t\n ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_OLEN);
if ((sptr = strtok(NULL, "\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_DESC);
}
else /* else we're normal FASTA format */
{
if ((sptr = strtok(V->sbuffer+1, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
if ((sptr = strtok(NULL, "\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_DESC);
}
readLoop(0, endPearson, V);
while (!(feof(V->f) || ((*V->sbuffer != 0) && (*V->sbuffer == '>'))))
getlines(V);
}
static int
endEMBL(char *s, int *addend)
{
*addend = 0;
return ((strstr(s,"//") != NULL) || (strstr(s,"ID ") == s));
}
static void
readEMBL(struct ReadSeqVars *V)
{
char *sptr;
/* make sure we have first line */
while (!feof(V->f) && strncmp(V->sbuffer, "ID ", 4) != 0)
getlines(V);
if ((sptr = strtok(V->sbuffer+5, "\n\t ")) != NULL)
{
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ID);
}
do {
getlines(V);
if (!feof(V->f) && strstr(V->sbuffer, "AC ") == V->sbuffer)
{
if ((sptr = strtok(V->sbuffer+5, "; \t\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ACC);
}
else if (!feof(V->f) && strstr(V->sbuffer, "DE ") == V->sbuffer)
{
if ((sptr = strtok(V->sbuffer+5, "\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_DESC);
}
} while (! feof(V->f) && strncmp(V->sbuffer,"SQ",2) != 0);
readLoop(0, endEMBL, V);
/* reading a real EMBL database file, we keep the source coords
*/
V->sqinfo->start = 1;
V->sqinfo->stop = V->seqlen;
V->sqinfo->olen = V->seqlen;
V->sqinfo->flags |= SQINFO_START | SQINFO_STOP | SQINFO_OLEN;
/* load next record's ID line */
while (!feof(V->f) && strncmp(V->sbuffer, "ID ", 4) != 0)
getlines(V);
}
static int
endZuker(char *s, int *addend)
{
*addend = 0;
return( *s == '(' );
}
static void
readZuker(struct ReadSeqVars *V)
{
char *sptr;
getlines(V); /*s == "seqLen seqid string..."*/
if ((sptr = strtok(V->sbuffer+6, " \t\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
if ((sptr = strtok(NULL, "\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_DESC);
readLoop(0, endZuker, V);
while (!(feof(V->f) | ((*V->sbuffer != '\0') & (*V->sbuffer == '('))))
getlines(V);
}
static void
readUWGCG(struct ReadSeqVars *V)
{
char *si;
char *sptr;
int done;
V->seqlen = 0;
/*writeseq: " %s Length: %d (today) Check: %d ..\n" */
/*drop above or ".." from id*/
if ((si = strstr(V->sbuffer," Length: ")) != NULL) *si = 0;
else if ((si = strstr(V->sbuffer,"..")) != NULL) *si = 0;
if ((sptr = strtok(V->sbuffer, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
do {
done = feof(V->f);
getlines(V);
if (! done) addseq(V->sbuffer, V);
} while (!done);
}
static void
readSquid(struct ReadSeqVars *V)
{
char *sptr;
int dostruc = FALSE;
while (strncmp(V->sbuffer, "NAM ", 4) != 0) getlines(V);
if ((sptr = strtok(V->sbuffer+4, "\n\t ")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_NAME);
while (1)
{
getlines(V);
if (feof(V->f)) {squid_errno = SQERR_FORMAT; return; }
if (strncmp(V->sbuffer, "SRC ", 4) == 0)
{
if ((sptr = strtok(V->sbuffer+4, " \t\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ID);
if ((sptr = strtok(NULL, " \t\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_ACC);
if ((sptr = strtok(NULL, ".")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_START);
if ((sptr = strtok(NULL, ".:")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_STOP);
if ((sptr = strtok(NULL, ": \t\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_OLEN);
}
else if (strncmp(V->sbuffer, "DES ", 4) == 0)
{
if ((sptr = strtok(V->sbuffer+4, "\n")) != NULL)
SetSeqinfoString(V->sqinfo, sptr, SQINFO_DESC);
}
else if (strncmp(V->sbuffer,"SEQ", 3) == 0)
break;
}
if (strstr(V->sbuffer, "+SS") != NULL) dostruc = TRUE;
V->seqlen = 0;
while (1)
{
/* sequence line */
getlines(V);
if (feof(V->f) || strncmp(V->sbuffer, "++", 2) == 0)
break;
addseq(V->sbuffer, V);
/* structure line */
if (dostruc)
{
getlines(V);
if (feof(V->f)) { squid_errno = SQERR_FORMAT; return; }
addstruc(V->sbuffer, V);
}
}
while (!feof(V->f) && strncmp(V->sbuffer, "NAM ", 4) != 0)
getlines(V);
}
/* Function: SeqfileOpen()
*
* Purpose : Open a sequence database file and prepare for reading
* sequentially.
*
* Args: filename - name of file to open
* format - format of file
* env - environment variable for path (e.g. BLASTDB)
*
* Returns opened SQFILE ptr, or NULL on failure.
*/
SQFILE *
SeqfileOpen(char *filename, int format, char *env)
{
SQFILE *dbfp;
if ((dbfp = (SQFILE *) malloc (sizeof(SQFILE))) == NULL)
{ squid_errno = SQERR_MEM; return NULL; }
if ((dbfp->f = fopen(filename, "r")) == NULL &&
(dbfp->f = EnvFileOpen(filename, env)) == NULL)
{ squid_errno = SQERR_NOFILE; return NULL; }
/* If it's a SELEX- or MSF-formatted alignment file, we
* hack a way to fake sequential access: read the whole
* alignment into a static at once. Relies on the prediction
* that these files will always be relatively small, unlike
* GenBank or EMBL databases.
*/
/* Note (Tue Aug 30 11:57:19 1994): This cause a bug:
* ReadSeq() is not reentrant for alignment files.
*/
if (format == kSelex || format == kMSF)
{
if (! ReadAlignment(filename, format, &ali_aseqs, &ali_num, &ali_ainfo))
return NULL;
if (! DealignAseqs(ali_aseqs, ali_num, &ali_rseqs))
return NULL;
curridx = 0;
}
/* Load the first line.
*/
getlines(dbfp);
return dbfp;
}
/* Function: SeqfilePosition()
*
* Purpose: Move to a particular offset in a seqfile.
* Will not work on interleaved files (SELEX, MSF).
*/
void
SeqfilePosition(SQFILE *sqfp, long offset)
{
fseek(sqfp->f, offset, SEEK_SET);
getlines(sqfp);
}
void
SeqfileClose(SQFILE *sqfp)
{
fclose(sqfp->f);
free(sqfp);
}
/* Function: ReadSeq()
*
* Purpose: Read next sequence from an open database file.
* Return the sequence and associated info.
*
* Args: fp - open sequence database file pointer
* format - format of the file (previously determined
* by call to SeqfileFormat())
* ret_seq - RETURN: sequence
* sqinfo - RETURN: filled in w/ other information
*
* Return: 1 on success, 0 on failure.
* ret_seq and some field of sqinfo are allocated here,
* The preferred call mechanism to properly free the memory is:
*
* SQINFO sqinfo;
* char *seq;
*
* ReadSeq(fp, format, &seq, &sqinfo);
* ... do something...
* FreeSequence(seq, &sqinfo);
*/
int
ReadSeq(SQFILE *V, int format, char **ret_seq, SQINFO *sqinfo)
{
int gotuw;
squid_errno = SQERR_OK;
if (format < kMinFormat || format > kMaxFormat)
{
squid_errno = SQERR_FORMAT;
*ret_seq = NULL;
return 0;
}
/* Here's the hack for accessing sequences from
* the multiple sequence alignment formats
*/
if (format == kMSF || format == kSelex)
{
if (curridx >= ali_num)
{ /* none left; free static ptrs */
FreeAlignment(ali_aseqs, ali_num, &ali_ainfo);
free(ali_rseqs); /* only free rseqs pointer array */
return 0;
}
SeqinfoCopy(sqinfo, &(ali_ainfo.sqinfo[curridx]));
*ret_seq = ali_rseqs[curridx];
curridx++;
return 1;
}
else
{
if (feof(V->f)) return 0;
V->seq = (char*) calloc (kStartLength+1, sizeof(char));
V->maxseq = kStartLength;
V->seqlen = 0;
V->sqinfo = sqinfo;
V->sqinfo->flags = 0;
V->dash_equals_n = (format == kEMBL) ? TRUE : FALSE;
switch (format) {
case kIG : readIG(V); break;
case kStrider: readStrider(V); break;
case kGenBank: readGenBank(V); break;
case kNBRF : readNBRF(V); break;
case kPearson: readPearson(V); break;
case kEMBL : readEMBL(V); break;
case kZuker : readZuker(V); break;
case kPIR : readPIR(V); break;
case kSquid : readSquid(V); break;
case kGCG:
do { /* skip leading comments on GCG file */
gotuw = (strstr(V->sbuffer,"..") != NULL);
if (gotuw) readUWGCG(V);
getlines(V);
} while (! feof(V->f));
break;
case kIdraw: /* SRE: no attempt to read idraw postscript */
default:
squid_errno = SQERR_FORMAT;
free(V->seq);
return 0;
}
V->seq[V->seqlen] = 0; /* stick a string terminator on it */
}
/* Cleanup
*/
sqinfo->len = V->seqlen;
sqinfo->flags |= SQINFO_LEN;
*ret_seq = V->seq;
if (squid_errno == SQERR_OK) return 1; else return 0;
}
/* Function: SeqfileFormat()
*
* Purpose: Determine format of seqfile, and return it
* through ret_format. From Gilbert's seqFileFormat().
*
* Return: 1 on success, 0 on failure.
*/
int
SeqfileFormat(char *filename, int *ret_format, char *env)
{
int foundIG = 0;
int foundStrider = 0;
int foundGB = 0;
int foundEMBL = 0;
int foundNBRF = 0;
int foundPearson = 0;
int foundZuker = 0;
int gotPIR = 0;
int gotSquid = 0;
int gotuw = 0;
int done = 0;
int gotMSF = 0;
int format = kUnknown;
int nlines= 0, dnalines= 0;
int splen = 0;
char sp[LINEBUFLEN];
FILE *fseq;
#define ReadOneLine(sp) \
{ done |= (feof(fseq)); \
readline( fseq, sp); \
if (!done) { splen = (int) strlen(sp); ++nlines; } }
if ((fseq = fopen(filename, "r")) == NULL &&
(fseq = EnvFileOpen(filename, env)) == NULL)
{ squid_errno = SQERR_NOFILE; return 0; }
/* Look at a line at a time
*/
while ( !done ) {
ReadOneLine(sp);
if (sp==NULL || *sp==0)
; /* nada */
/* high probability identities: */
else if (strstr(sp, "MSF:") != NULL &&
strstr(sp, "Type:") != NULL &&
strstr(sp, "Check:") != NULL)
gotMSF = 1;
else if (strstr(sp,"Check:") != NULL)
gotuw= 1;
else if ((strncmp(sp, "///", 3) == 0) ||
(strncmp(sp, "ENTRY ", 6) == 0))
gotPIR = 1;
else if (strncmp(sp, "++", 2) == 0 ||
strncmp(sp, "NAM ", 4) == 0)
gotSquid = 1;
/* uncertain identities: */
else if (*sp ==';') {
if (strstr(sp,"Strider") !=NULL) foundStrider= 1;
else foundIG= 1;