-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit.c
71 lines (62 loc) · 1.8 KB
/
split.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
#include "cfile.h"
static int usage() {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: yame split [options] <in.cx> out_prefix\n");
fprintf(stderr, "\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -v verbose\n");
fprintf(stderr, " -s sample name list\n");
fprintf(stderr, " -h This help\n");
fprintf(stderr, "\n");
return 1;
}
int main_split(int argc, char *argv[]) {
int c, verbose = 0; char *fname_snames = NULL;
while ((c = getopt(argc, argv, "s:vh"))>=0) {
switch (c) {
case 'v': verbose = 1; break;
case 's': fname_snames = strdup(optarg); break;
case 'h': return usage(); break;
default: usage(); wzfatal("Unrecognized option: %c.\n", c);
}
}
if (optind + 2 > argc) {
usage();
wzfatal("Please supply input file.\n");
}
cfile_t cf = open_cfile(argv[optind++]);
char *prefix = argv[optind];
char **snames = NULL; int snames_n = 0;
if (fname_snames) {
gzFile fh = wzopen(fname_snames, 1);
char *line = NULL;
char **fields; int nfields;
while (gzFile_read_line(fh, &line)>0) {
line_get_fields(line, "\t", &fields, &nfields);
snames = realloc(snames, (snames_n+1)*sizeof(char*));
snames[snames_n] = strdup(fields[0]);
++snames_n;
free_fields(fields, nfields);
}
free(line);
wzclose(fh);
free(fname_snames);
}
int i = 0;
for (i=0; ; ++i) {
cdata_t c = read_cdata1(&cf);
if (c.n == 0) break;
char *tmp = NULL;
if (snames_n) {
tmp = malloc(strlen(prefix)+strlen(snames[i])+1000);
sprintf(tmp, "%s%s.cx", prefix, snames[i]);
} else {
tmp = malloc(strlen(prefix) + 1000);
sprintf(tmp, "%s_split_%i.cx", prefix, i+1);
}
cdata_write(tmp, &c, "wb", verbose);
free(tmp);
free(c.s);
}
return 0;
}