forked from thegenemyers/FASTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabex.c
229 lines (196 loc) · 6.25 KB
/
Tabex.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*********************************************************************************************\
*
* Example code for reading, listing, and searching a kmer-count table produced by FastK.
*
* Author: Gene Myers
* Date : October, 2020
*
*********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <math.h>
#include "libfastk.h"
static char *Usage = "[-t<int>] <source_root>[.ktab] (LIST|CHECK|(k-mer:string>) ...";
static int Check_Kmer_Table(Kmer_Table *T)
{ char *curs, *last, *flip;
int64 i;
curs = Fetch_Kmer(T,0,NULL);
last = Fetch_Kmer(T,0,NULL);
for (i = 1; i < T->nels; i++)
{ if (strcmp(last,Fetch_Kmer(T,i,curs)) >= 0)
{ fprintf(stderr,"\nOut of Order\n");
fprintf(stderr," %9lld: %s = %5d\n",i-1,last,Fetch_Count(T,i-1));
fprintf(stderr," %9lld: %s = %5d\n",i,curs,Fetch_Count(T,i));
fflush(stderr);
break;
}
flip = curs;
curs = last;
last = flip;
}
free(curs);
free(last);
return (i >= T->nels);
}
static void List_Kmer_Table(Kmer_Table *T, FILE *out)
{ char *seq;
int64 i;
seq = Fetch_Kmer(T,0,NULL);
for (i = 0; i < T->nels; i++)
fprintf(out," %9lld: %s = %5d\n",i,Fetch_Kmer(T,i,seq),Fetch_Count(T,i));
free(seq);
}
static int Check_Kmer_Stream(Kmer_Stream *S)
{ int hbyte = S->hbyte;
int lpre, u;
uint8 *lsuf;
char *seq;
lsuf = (uint8 *) (seq = Current_Kmer(S,NULL));
First_Kmer_Entry(S);
if (S->csuf == NULL)
return (1);
lpre = S->cpre;
memcpy(lsuf,S->csuf,hbyte);
for (Next_Kmer_Entry(S); S->csuf != NULL; Next_Kmer_Entry(S))
{ if (S->cpre < lpre || (S->cpre == lpre && memcmp(S->csuf,lsuf,hbyte) < 0))
{ fprintf(stderr,"\nOut of Order %02x %02x\n",S->cpre,lpre);
for (u = 0; u < S->hbyte; u++)
printf(" %02x %02x\n",S->csuf[u],lsuf[u]);
fprintf(stderr," %9lld: %s = %5d\n",S->cidx,Current_Kmer(S,seq),Current_Count(S));
GoTo_Kmer_Index(S,S->cidx-1);
fprintf(stderr," %9lld: %s = %5d\n",S->cidx,Current_Kmer(S,seq),Current_Count(S));
Next_Kmer_Entry(S);
fprintf(stderr," %9lld: %s = %5d\n",S->cidx,Current_Kmer(S,seq),Current_Count(S));
fflush(stderr);
break;
}
lpre = S->cpre;
memcpy(lsuf,S->csuf,hbyte);
}
free(seq);
return (S->csuf == NULL);
}
static void List_Kmer_Stream(Kmer_Stream *S, int cut, FILE *out)
{ char *seq;
seq = Current_Kmer(S,NULL);
for (First_Kmer_Entry(S); S->csuf != NULL; Next_Kmer_Entry(S))
if (Current_Count(S) >= cut)
{ fprintf(out," %9lld: %s = %5d\n",S->cidx,Current_Kmer(S,seq),Current_Count(S)); fflush(stdout); }
free(seq);
}
int main(int argc, char *argv[])
{ Kmer_Table *T;
Kmer_Stream *S;
int CUT;
int STREAM;
{ int i, j, k;
int flags[128];
char *eptr;
(void) flags;
ARG_INIT("Tabex");
CUT = 0;
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("T")
break;
case 't':
ARG_POSITIVE(CUT,"Cutoff for k-mer table")
break;
}
else
argv[j++] = argv[i];
argc = j;
STREAM = ! flags['T']; // This is undocumented and only for developer use.
if (argc < 3)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
fprintf(stderr,"\n");
fprintf(stderr," -t: Trim all k-mers with counts less than threshold\n");
exit (1);
}
}
// Default is to use a Kmer_Stream to realize operations
if (STREAM)
{ S = Open_Kmer_Stream(argv[1]);
if (S == NULL)
{ fprintf(stderr,"%s: Cannot open %s\n",Prog_Name,argv[1]);
exit (1);
}
printf("Opening %d-mer table with ",S->kmer);
Print_Number(S->nels,0,stdout);
printf(" entries");
if (S->minval > 1)
printf(" occuring %d-or-more times",S->minval);
printf("\n");
fflush(stdout);
{ int c;
char *seq;
seq = NULL;
for (c = 2; c < argc; c++)
if (strcmp(argv[c],"LIST") == 0)
List_Kmer_Stream(S,CUT,stdout);
else if (strcmp(argv[c],"CHECK") == 0)
{ if (Check_Kmer_Stream(S))
printf("The table is OK\n");
}
else
{ if ((int) strlen(argv[c]) != S->kmer)
printf("%*s: Not a %d-mer\n",S->kmer,argv[c],S->kmer);
else
{ if (GoTo_Kmer_String(S,argv[c]))
printf("%*s: %5d @ idx = %lld\n",S->kmer,argv[c],Current_Count(S),S->cidx);
else
printf("%*s: Not found\n",S->kmer,argv[c]);
}
}
free(seq);
}
Free_Kmer_Stream(S);
}
// But for developers and illustrative purposes we also give a Kmer_Table implementation
else
{ T = Load_Kmer_Table(argv[1],CUT);
if (T == NULL)
{ fprintf(stderr,"%s: Cannot open %s\n",Prog_Name,argv[1]);
exit (1);
}
printf("Loaded %d-mer table with ",T->kmer);
Print_Number(T->nels,0,stdout);
printf(" entries");
if (T->minval > 1)
printf(" occuring %d-or-more times",T->minval);
printf("\n");
fflush(stdout);
{ int c;
int64 loc;
for (c = 2; c < argc; c++)
if (strcmp(argv[c],"LIST") == 0)
List_Kmer_Table(T,stdout);
else if (strcmp(argv[c],"CHECK") == 0)
{ if (Check_Kmer_Table(T))
printf("The table is OK\n");
}
else
{ if ((int) strlen(argv[c]) != T->kmer)
printf("%*s: Not a %d-mer\n",T->kmer,argv[c],T->kmer);
else
{ loc = Find_Kmer(T,argv[c]);
if (loc < 0)
printf("%*s: Not found\n",T->kmer,argv[c]);
else
printf("%*s: %5d @ idx = %lld\n",T->kmer,argv[c],Fetch_Count(T,loc),loc);
}
}
}
Free_Kmer_Table(T);
}
Catenate(NULL,NULL,NULL,NULL);
Numbered_Suffix(NULL,0,NULL);
free(Prog_Name);
exit (0);
}