-
Notifications
You must be signed in to change notification settings - Fork 37
/
split_fa_by_id.c
55 lines (44 loc) · 981 Bytes
/
split_fa_by_id.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
#include "./kseq.h"
#include <zlib.h>
#include <string.h>
#include <stdio.h>
KSEQ_INIT(gzFile, gzread)
const char *get_filename_ext(const char *filename){
const char *dot = strrchr(filename, '.');
if ( !dot || dot == filename ) return "";
return dot + 1;
}
void split_file(const char *file, const char* ext){
gzFile fi;
FILE *fo;
kseq_t *seq;
fi = gzopen(file, "r");
seq = kseq_init(fi);
int l;
char *name;
char *token;
while ( ( l = kseq_read(seq)) >= 0) {
name = strdup(seq->name.s);
token = strtok(name, ":");
strcat(token, ".");
strcat(token, ext);
fo = fopen(token, "a+");
fprintf(fo, ">%s\n%s\n", seq->name.s, seq->seq.s);
fclose(fo);
free(name);
}
kseq_destroy(seq);
gzclose(fi);
return ;
}
int main(int argc, char *argv[])
{
if (argc == 1){
fprintf(stderr, "Usage: %s <in.fasta>\n", argv[0]);
return 1;
}
char *file_name = argv[1];
const char* ext = get_filename_ext(file_name);
split_file(file_name, ext);
return 0;
}