-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathread_fasta.c
78 lines (73 loc) · 1.78 KB
/
read_fasta.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "gfa.h"
/**********************************************************
*
* Procedure to read the dna file and put into array
*
*
**********************************************************/
/*
* Defined in main module file to avoid stack overflow problem
* can easily be 300Mb
*/
extern char dna[];
int read_fasta(FILE *dna_file, char fasta_title[]) {
register int i;
int base, fasta_len;
char line[MAX_LINE + 1];
BOOLEAN start;
/* Declare Procedure */
//void nulls(char line[], int n);
fasta_len = MAX_FASTA_SIZE;
start = TRUE;
i = 0;
if ((base = getc(dna_file)) != EOF) {
if (base != '>') {
fprintf(stderr, " base read =");
putc(base, stderr);
fprintf(stderr, ".\n");
fprintf(stderr, " FATAL ERROR: Sequence file NOT in FASTA format\n");
exit(88);
}
else
ungetc(base, dna_file);
}
else {
fprintf(stderr, " End of Sequence file!\n");
fclose(dna_file);
return (0);
}
while ((base = getc(dna_file)) != EOF) {
switch (base) {
case '>': /* if (start) get header information
else break and return */
if (start) {
if (fgets(line, MAX_LINE, dna_file) != NULL) {
/* crack some part of header and make title */
memset((char *) fasta_title, '\0', fasta_len);
//nulls(fasta_title, fasta_len);
if (strlen(line) < fasta_len) fasta_len = strlen(line) - 1;
strncpy(fasta_title, &line[0], fasta_len);
}
start = FALSE;
}
else {
ungetc(base, dna_file);
fprintf(stderr, " INFO: Program read %d bases \n", i);
return (i);
}
break;
default: /* look for DNA sequence */
if (isalpha(base)) {
dna[i] = tolower(base);
i++;
}
break;
}
}
fprintf(stderr, " INFO: Program read %d bases \n", i);
return (i);
}