-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmean_size.c
executable file
·62 lines (43 loc) · 1.31 KB
/
mean_size.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
#include "utils.h"
#include "kvec.h"
static kvec_t(int) sum;
#define khint_lt(a, b) (a < b)
KSORT_INIT(mean, int, khint_lt)
int mean_size_main (int argc, char *argv[]){
if ( argc != 2) {
fprintf(stderr, "\nUsage: atlas-utils mean_size <otutab>\n\n");
return 1;
}
kstring_t kt = {0, 0, 0};
int *fields, n, i;
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] == '#'){
fields = ksplit(&kt,'\t', &n);
for (i = 1; i < n; ++i) kv_push(int, sum, 0 );
continue;
}
fields = ksplit(&kt,'\t', &n);
for (i = 1; i < n; ++i) kv_a(int, sum, i - 1 ) += atof(kt.s + fields[i]);
}
free(kt.s);
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[1]);
exit(1);
}
ks_mergesort(mean, sum.n, sum.a, 0);
if(kv_size(sum) % 2 == 0) {
printf("%d\n", (sum.a[sum.n/2] + sum.a[sum.n/2 - 1]) / 2 );
} else {
printf("%d\n", sum.a[sum.n/2]);
}
kv_destroy(sum);
return 0;
}