-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsummary.c
executable file
·71 lines (51 loc) · 1.54 KB
/
summary.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 "utils.h"
#include "kvec.h"
static kvec_t(int) otu;
static kvec_t(int) sum;
int summary_main(int argc, char *argv[]){
if ( argc != 2) {
fprintf(stderr, "\nUsage: atlas-utils summary <otutab>\n\n");
return 1;
}
kstring_t kt = {0, 0, 0};
int *fields, n, i;
kv_init(otu);
kv_init(sum);
gzFile fp;
fp = strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
if (fp) {
kstream_t *ks;
ks = ks_init(fp);
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if(kt.s[0] == '#'){
puts(kt.s);
fields = ksplit(&kt,'\t', &n);
for (i = 1; i < n; ++i){
kv_push(int, otu, 0 );
kv_push(int, sum, 0 );
}
continue;
}
fields = ksplit(&kt,'\t', &n);
for (i = 1; i < n; ++i){
if( atoi(kt.s + fields[i]) > 0 ) kv_a(int, otu, i - 1 ) += 1;
kv_a(int, sum, i - 1 ) += atof(kt.s + fields[i]);
}
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[1]);
exit(1);
}
printf("tag_num");
for (i = 0; i < kv_size(sum); ++i) printf("\t%d", kv_A(sum, i));
printf("\n");
printf("otu_num");
for (i = 0; i < kv_size(otu); ++i) printf("\t%d", kv_A(otu, i));
printf("\n");
kv_destroy(otu);
kv_destroy(sum);
free(kt.s);
return 0;
}