-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnormalization.c
executable file
·104 lines (82 loc) · 2.49 KB
/
normalization.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
#include "utils.h"
KHASH_MAP_INIT_STR(normalization, int)
void kh_normalization_destroy(khash_t(normalization) *h);
int normalization_main(int argc, char *argv[]){
if ( argc != 3) {
fprintf(stderr, "\nUsage: atlas-utils normalization <copy_number> <otutab>\n\n");
return 1;
}
khash_t(normalization) *h;
h = kh_init(normalization);
khint_t k;
kstream_t *ks;
kstring_t kt = {0, 0, 0};
gzFile fp;
fp = strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
if(fp){
ks = ks_init(fp);
int ret;
char *p;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if( kt.l == 0 ) continue;
ks_tokaux_t aux;
p = kstrtok(kt.s, "\t", &aux);
kt.s[aux.p - p] = '\0';
k = kh_put(normalization, h, p, &ret);
if(ret){
kh_key(h, k) = strdup(p);
kh_val(h, k) = atoi(aux.p + 1);
}
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[1]);
exit(1);
}
fp = strcmp(argv[2], "-")? gzopen(argv[2], "r") : gzdopen(fileno(stdin), "r");
if(fp){
ks = ks_init(fp);
kstring_t kv = {0, 0, 0};
int i, *fields, n;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if(kt.l == 0) continue;
if(kt.s[0] == '#') {
puts(kt.s);
continue;
}
kv.l = 0;
kputs(kt.s, &kv);
fields = ksplit(&kt, '\t', &n);
k = kh_get(normalization, h, kt.s);
if(k == kh_end(h)){
puts(kv.s);
continue;
}
kv.l = 0;
kputs(kt.s , &kv);
int cnt = kh_val(h, k);
for (i = 1; i < n; ++i) ksprintf(&kv, "\t%g", atof(kt.s + fields[i])/cnt );
puts(kv.s);
}
free(kv.s);
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[2]);
exit(1);
}
free(kt.s);
kh_normalization_destroy(h);
return 0;
}
void kh_normalization_destroy(khash_t(normalization) *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(normalization, h);
}