-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasta.c
executable file
·123 lines (108 loc) · 2.94 KB
/
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
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
#include "fasta.h"
size_t *countInfo(char *path, size_t *num)
{
FILE *fp = NULL;
fp = fopen(path, "r");
if (fp == NULL) exit(EXIT_FAILURE);
int state = 0;
char *line = NULL;
ssize_t read;
size_t n = 0, length = 0, tempL = 1;
size_t *lens = (size_t *)malloc(sizeof(size_t) * tempL * 100);
if (lens == NULL) exit(EXIT_FAILURE);
*num = 0;
while ((read = getline(&line, &n, fp)) != -1)
{
if (line[0] == '>')
{
if (state == 1)
{
if ((*num) >= (tempL * 100))
{
lens = realloc(lens, sizeof(size_t) * (++tempL) * 100);
if (lens == NULL) exit(EXIT_FAILURE);
}
lens[(*num)++] = length;
length = 0;
}
state = 1;
}
else
length += (read - 1);
}
fclose(fp);
if (line)
free(line);
lens = realloc(lens, sizeof(size_t) * ((*num) + 1));
if (lens == NULL) exit(EXIT_FAILURE);
lens[(*num)++] = length;
return lens;
}
void readFasta(char *path, char ***strs, char ***labels, size_t *num)
{
FILE *fp;
char *line = NULL;
size_t n = 0, idx = 0;
ssize_t read;
*num = 0;
// get the number of and length of seq
size_t *lens = (size_t *)countInfo(path, num);
*strs = (char **)malloc(sizeof(char *) * (*num));
*labels = (char **)malloc(sizeof(char *) * (*num));
fp = fopen(path, "r");
if (fp == NULL)
{
printf("Couldn't open the file %s\n", path);
exit(EXIT_FAILURE);
}
int state = 0;
while ((read = getline(&line, &n, fp)) != -1) {
if (line[read - 1] == '\n')
line[read - 1] = 0;
if (line[0] == '>')
{
if (state == 1)
idx++;
state = 1;
(*strs)[idx] = (char *)malloc(sizeof(char) * (lens[idx] + 1));
(*strs)[idx][0] = 0;
(*labels)[idx] = (char *)malloc(sizeof(char) * read);
strcpy((*labels)[idx], line+1);
}
else
strcat((*strs)[idx], line);
}
free(lens);
fclose(fp);
if (line)
free(line);
}
void writeFasta(char *path, char **strs, char **lables, size_t num)
{
FILE *fp = fopen(path, "w");
if (fp == NULL)
exit(EXIT_FAILURE);
char *p = (char *)malloc(sizeof(char) * (LEN + 1));
p[LEN] = 0;
for (int i = 0; i < num; i++)
{
fputc('>', fp);
fputs(lables[i], fp);
fputc('\n', fp);
int idx = 0, length = strlen(strs[i]);
while (idx + LEN <= length)
{
strncpy(p, strs[i]+idx, LEN);
fputs(p, fp);
idx += LEN;
fputc('\n', fp);
}
if (idx < length)
{
strcpy(p, strs[i]+idx);
fputs(p, fp);
}
fputc('\n', fp);
}
free(p);
}