-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroup_by.c
executable file
·179 lines (136 loc) · 4.85 KB
/
group_by.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "utils.h"
#include "kvec.h"
#include <math.h>
static kvec_t( const char* ) vs;
typedef struct {
int n;
int m;
double *fields;
} shape_t ;
KHASH_MAP_INIT_STR(shape, shape_t)
void kh_shape_destroy(khash_t(shape) *h){
khint_t k;
if (h == 0) return;
for (k = 0; k < kh_end(h); ++k) {
if (kh_exist(h, k)) free((char*)kh_key(h, k));
}
kh_destroy(shape, h);
}
int group_by_main(int argc, char *argv[]){
int R = 0, opt = 1;
int c;
while ((c = getopt(argc, argv, "rt:")) >= 0) {
if (c == 't') opt = atoi(optarg);
else if (c == 'r') R = 1;
}
if ( optind == argc || argc != optind + 2) {
fprintf(stderr, "\nUsage: atlas-utils group_by <tsv> <map>\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -t STR operattion. 1:sum, 2: average. 3:cov\n");
fprintf(stderr, " default: [1];\n");
fprintf(stderr, " -r round value to nearest intege;\n\n");
return 1;
}
khash_t(reg) *h;
h = kh_init(reg);
khash_t(shape) *melt;
melt = kh_init(shape);
khint_t k;
int i, *fields, n, ret;
kstream_t *ks;
kstring_t kt = {0, 0, 0};
gzFile fp;
fp = strcmp(argv[ optind + 1 ], "-")? gzopen(argv[ optind + 1 ], "r") : gzdopen(fileno(stdin), "r");
if (fp) {
ks = ks_init(fp);
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if(kt.s[0] == '#') continue;
fields = ksplit(&kt, '\t', &n);
k = kh_put(reg, h, kt.s, &ret);
kh_key(h, k) = strdup(kt.s);
kh_val(h, k) = strdup(kt.s + fields[1]);
k = kh_put(shape, melt, kt.s + fields[1], &ret);
if(ret == 1){
kh_key(melt, k) = strdup(kt.s + fields[1]);
kv_push( const char *, vs, kh_key(melt, k) );
shape_t shape = {0, 0, 0};
kh_val(melt, k) = shape;
}
++kh_val(melt, k).n;
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[ optind + 1 ]);
exit(1);
}
for (i = 0; i < kv_size(vs); ++i){
k = kh_get(shape, melt, kv_A(vs, i));
if(k != kh_end( melt )) kh_val(melt, k).fields = (double*)calloc(kh_val(melt, k).n, sizeof(double));
}
fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
if (fp) {
khint_t t;
ks = ks_init(fp);
if( ks_getuntil(ks, '\n', &kt, 0) > 0 ){
if(kt.s[0] == '#'){
fields = ksplit(&kt, '\t', &n);
for (i = 0; i < n; ++i){
k = kh_get(reg, h, kt.s + fields[i]);
if( k != kh_end(h) ){
t = kh_get(shape, melt, kh_val(h, k));
kh_val(melt, t).fields[kh_val(melt, t).m] = i;
++kh_val(melt, t).m;
}
}
printf("%s", kt.s);
for (i = 0; i < kv_size(vs); ++i){
k = kh_get(shape, melt, kv_A(vs, i));
if(kh_val(melt, k).m) printf("\t%s", kv_A(vs, i));
}
printf("\n");
}else{
fprintf(stderr, "[ERR]: no headline ?\n");
return -1;
}
}else{
fprintf(stderr, "[ERR]: file empty? \n");
return -1;
}
double val = 0.0;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
fields = ksplit(&kt, '\t', &n);
printf("%s", kt.s);
for (i = 0; i < kv_size(vs); ++i){
k = kh_get(shape, melt, kv_A(vs, i));
int z = 0;
if(k != kh_end( melt )){
val = 0.0;
int j;
for (j = 0; j < kh_val(melt, k).m; ++j){
int q = kh_val(melt, k).fields[j];
double v = atof( kt.s + fields[q] ) ;
val += v;
if( v != 0. ) ++z;
}
if(opt == 2) val = val/kh_val(melt, k).m;
else if(opt == 3) val = 1.0 * z/kh_val(melt, k).m;
}
if(kh_val(melt, k).m){
(R) ? printf("\t%d", (int)round(val)) : printf("\t%.4g", val);
}
}
printf("\n");
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[optind]);
exit(1);
}
free(kt.s);
kh_reg_destroy(h);
kh_shape_destroy(melt);
kv_destroy(vs);
return 0;
}