-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkann.c
executable file
·175 lines (127 loc) · 4.21 KB
/
kann.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
#include "utils.h"
#include <math.h>
static khash_t(kreg) *ko;
static khash_t(kreg) *brite;
#include "kvec.h"
static int n_sample;
static kstring_t lt = {0, 0, 0};
void kann_titles(kstring_t *kt);
void kann_update_abundance(khint_t k, double abundance[n_sample]);
int kann_main(int argc, char *argv[]){
int R = 0;
int c;
while ((c = getopt(argc, argv, "rv")) >= 0) {
if (c == 'r') R = 1;
}
if ( optind == argc || argc != optind + 2) {
fprintf(stderr, "\nUsage: atlas-utils kann [options] <feature-map|KO\\tModule> <KO matrix>\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -r round value to nearest intege;\n\n");
return 1;
}
ko = kh_init(kreg);
khint_t k;
kstream_t *ks;
kstring_t kt = {0, 0, 0};
kstring_t samples = {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);
char *p;
int ret;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if( kt.s[0] == '#'){
if( kt.s[1] == ' ') continue;
kputs(kt.s, &samples);
continue;
}
ks_tokaux_t aux;
p = kstrtok(kt.s, "\t", &aux);
kt.s[aux.p - p] = '\0';
k = kh_put(kreg, ko, p, &ret);
kh_key(ko, k) = strdup(p);
kstring_t ktmp = {0, 0, 0};
kputs(aux.p + 1, &ktmp);
kh_val(ko, k) = ktmp;
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[optind + 1]);
exit(1);
}
brite = kh_init(kreg);
fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
if(fp){
ks = ks_init(fp);
char *p;
int ret;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
ks_tokaux_t aux;
p = kstrtok(kt.s, "\t", &aux);
kt.s[aux.p - p] = '\0';
k = kh_get(kreg, ko, p);
if(k == kh_end(ko)) continue;
k = kh_put(kreg, brite, aux.p + 1, &ret);
if( ret == 1 ){
kh_key(brite, k) = strdup(aux.p + 1);
kstring_t ktmp = {0, 0, 0};
kputs(p, &ktmp);
kh_val(brite, k) = ktmp;
}else{
kputc(',', &kh_val(brite, k));
kputs(p, &kh_val(brite, k));
}
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[optind]);
exit(1);
}
kann_titles(&samples);
int n_sample;
ksplit(&samples, '\t', &n_sample);
n_sample -= 1;
double abundance[n_sample];
kstring_t kv = {0, 0, 0};
int i;
for (k = 0; k < kh_end(brite); ++k){
if(!kh_exist(brite, k)) continue;
memset(abundance, 0, sizeof(abundance[0]) * n_sample );
kann_update_abundance(k, abundance);
kv.l = 0;
kputs(kh_key(brite, k), &kv);
for (i = 0; i < n_sample; ++i){
kputc('\t', &kv);
(R) ? ksprintf(&kv, "%d", (int)round( abundance[i]) ) :
ksprintf(&kv, "%g", abundance[i]);
}
puts(kv.s);
}
kh_kreg_destroy(ko);
kh_kreg_destroy(brite);
free(kt.s);
return 0;
}
void kann_titles(kstring_t *kt) {
ks_tokaux_t aux;
kstrtok(kt->s, "\t", &aux);
printf("#catalog\t%s\n", aux.p + 1);
}
void kann_update_abundance(khint_t k, double abundance[n_sample]){
int *fileds, n, i, j, *samples, n_sample;
fileds = ksplit(&kh_val(brite, k), ',', &n);
for (i = 0; i < n; ++i){
khint_t t = kh_get(kreg, ko, kh_val(brite, k).s+fileds[i]);
if(t == kh_end(ko)){
fprintf(stderr, "[ERR]: %s no abundance values!\n", kh_val(brite, k).s + fileds[i]);
}else{
lt.l = 0;
kputs(kh_val(ko, t).s, <);
samples = ksplit(<, '\t', &n_sample);
for (j = 0; j < n_sample; ++j) abundance[j] += atof(lt.s + samples[j]);
}
}
}