-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrib.c
executable file
·137 lines (102 loc) · 3.5 KB
/
contrib.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
#include "utils.h"
#include "kvec.h"
int contrib_main(int argc, char *argv[]){
int f = 1;
int c;
while ((c = getopt(argc, argv, "k:")) >= 0) {
if (c == 'k') f = atoi(optarg);
}
if ( optind == argc || argc != optind + 2) {
fprintf(stderr, "\nUsage: atlas-utils contrib [option] <mapping> <abundance>\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -k INT column as key. default: [1];\n\n");
fprintf(stderr, "Note:\n");
fprintf(stderr, "Need three columns, ZOTU|KO|copy\n\n");
return 1;
}
int *fields, i, absent, n;
kstring_t kt = {0, 0, 0};
kstring_t kv = {0, 0, 0};
khash_t(reg) *h;
h = kh_init(reg);
khint_t k;
gzFile fp;
fp = strcmp(argv[ optind + 1 ], "-")? gzopen(argv[optind + 1], "r") : gzdopen(fileno(stdin), "r");
if( fp ){
kstream_t *ks;
ks = ks_init(fp);
char *p;
int l;
if( ks_getuntil(ks, '\n', &kt, 0) >= 0 ){
if(kt.s[0] == '#'){
ks_tokaux_t aux;
p = kstrtok(kt.s, "\t", &aux);
l = aux.p - p;
kt.s[l] = '\0';
kputs(aux.p + 1, &kv);
}else{
fprintf(stderr, "[ERR]: first line not start with '#' \n %s\n", kt.s);
exit(-1);
}
}
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if( kt.l == 0 ) continue;
ks_tokaux_t aux;
p = kstrtok(kt.s, "\t", &aux);
l = aux.p - p;
kt.s[l] = '\0';
k = kh_put(reg, h, kt.s, &absent);
if(absent == 1){
kh_key(h, k) = strdup(kt.s);
kh_val(h, k) = strdup(aux.p + 1);
}
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[optind + 1]);
exit(1);
}
fp = strcmp(argv[ optind ], "-")? gzopen(argv[ optind ], "r") : gzdopen(fileno(stdin), "r");
if( fp ){
kstream_t *ks;
ks = ks_init(fp);
double val = 0.;
if( ks_getuntil(ks, '\n', &kt, 0) >= 0 ){
if(kt.s[0] == '#'){
fields = ksplit(&kt, '\t', &n);
if(n != 3){
fprintf(stderr, "[ERR]: need tree columns data frame; \n");
exit(-1);
}
printf("%s\t%s\t%s\n", kt.s, kt.s + fields[1], kv.s);
}else{
fprintf(stderr, "[ERR]: first line not start with '#' \n %s\n", kt.s);
exit(-1);
}
}
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
if( kt.l == 0 ) continue;
fields = ksplit(&kt, '\t', &n);
val = atof(kt.s + fields[2]);
k = kh_get(reg, h, kt.s + fields[ f - 1 ]);
if(k != kh_end(h) ){
printf("%s\t%s", kt.s, kt.s + fields[1]);
kv.l = 0;
kputs(kh_val(h, k), &kv);
fields = ksplit(&kv, '\t', &n);
for (i = 0; i < n; ++i) printf("\t%lf", val * atof(kv.s + fields[i]));
printf("\n");
}
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[optind + 1]);
exit(1);
}
free(kt.s);
free(kv.s);
kh_reg_destroy(h);
return 0;
}