-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunique_table.c
executable file
·166 lines (124 loc) · 4.06 KB
/
unique_table.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
#include "utils.h"
static khash_t(reg32) *samples_h;
static khash_t(set) *uniques_h;
#include "kvec.h"
static kvec_t(const char*) samples_t;
static kvec_t(const char*) uniques_t;
KHASH_MAP_INIT_STR(uniques_map, int *)
static khash_t(uniques_map) *map_h;
static int n_sample = 0;
void kh_uniques_map_destroy(khash_t(uniques_map) *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));
free((int*)kh_val(h, k));
}
}
kh_destroy(uniques_map, h);
}
void unique_samples (khash_t(reg32) *samples_h, const char *optarg){
khint_t k;
gzFile fp;
fp = strcmp(optarg, "-")? gzopen(optarg, "r") : gzdopen(fileno(stdin), "r");
if( fp ){
kstream_t *ks;
ks = ks_init(fp);
kstring_t kt = {0, 0, 0};
int idx = 0;
int ret;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if(kt.s[0] == '#') continue;
ks_tokaux_t aux;
kstrtok(kt.s, "\t", &aux);
kt.s[aux.p - kt.s] = '\0';
n_sample++;
k = kh_put(reg32, samples_h, kt.s, &ret);
if(ret){
char *p = strdup(kt.s);
kh_key(samples_h, k) = p;
kh_val(samples_h, k) = idx++;
kv_push(const char *, samples_t, p);
}
}
free(kt.s);
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", optarg);
exit(1);
}
}
void unique_map (khash_t(uniques_map) *h, khash_t(reg32) *samples_h, khash_t(set) *uniques_h, const char *optarg){
khint_t k, v;
gzFile fp;
fp = strcmp(optarg, "-")? gzopen(optarg, "r") : gzdopen(fileno(stdin), "r");
if( fp ){
kstream_t *ks;
ks = ks_init(fp);
kstring_t kt = {0, 0, 0};
int *fields, n, ret;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
fields = ksplit(&kt, '\t', &n);
ks_tokaux_t aux;
kstrtok(kt.s, ";", &aux);
kt.s[fields[1] - 2] = '\0';
k = kh_put(uniques_map, h, kt.s + fields[1], &ret);
if(ret){
char *p = strdup(kt.s + fields[1]);
kh_key(h, k) = p;
kv_push(const char *, uniques_t, p);
int *counts = (int *)calloc(sizeof(int), n_sample);
kh_val(h, k) = counts;
}
v = kh_get(reg32, samples_h, aux.p + 8);
if(v != kh_end(samples_h))
kh_val(h, k)[ kh_val(samples_h, v) ]++;
else{
fprintf(stderr, "[ERR]: can't map sample: %s\n", aux.p + 8);
exit(-1);
}
}
free(kt.s);
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", optarg);
exit(1);
}
}
void unique_table (khash_t(uniques_map) *h){
fputs("#OTU table", stdout);
int i;
for (i = 0; i < n_sample; ++i)printf("\t%s", kv_A(samples_t, i));
fputc('\n', stdout);
khint_t k;
for (i = 0; i < kv_size(uniques_t); ++i){
k = kh_get(uniques_map, h, kv_A(uniques_t, i));
if(k != kh_end(h)){
fputs(kh_key(h, k), stdout);
int j;
for (j = 0; j < n_sample; ++j)printf("\t%d", kh_val(h, k)[j]);
fputc('\n', stdout);
}
}
}
int unique_table_main(int argc, char *argv[]){
if ( optind == argc || argc != optind + 2) {
fprintf(stderr, "\nUsage: atlas-utils uniques_table [options] <mapping_file> <maps>\n\n");
return 1;
}
map_h = kh_init(uniques_map);
samples_h = kh_init(reg32);
uniques_h = kh_init(set);
unique_samples(samples_h, argv[optind]);
unique_map(map_h, samples_h, uniques_h, argv[optind + 1]);
unique_table(map_h);
kh_uniques_map_destroy(map_h);
kh_set_destroy(uniques_h);
kv_destroy(uniques_t);
kh_reg32_destroy(samples_h);
kv_destroy(samples_t);
return 0;
}