-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGDBshow.c
396 lines (327 loc) · 12.3 KB
/
GDBshow.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************************
*
* Display a specified set of reads of a database in fasta format.
*
* Author: Gene Myers
* Origin: Reworking of DAZZ_DB program DBshow.c from the FASTGA module specialized to "gdb" ("dam")
* Date : Jan 2024
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include "GDB.h"
#include "hash.h"
#include "select.h"
#define DEBUG_RANGE
static char *Usage =
"[-hU] [-w<int(80)>] <source:path>[.1gdb] [ <selection>|<FILE> ]";
#define MAX_BUFFER 10001
static int NSCAFF; // # of scaffolds
static char *HEADERS; // All headers
static GDB_CONTIG *CONTIGS; // contig vector of the database
static GDB_SCAFFOLD *SCAFFS; // scaffold vector of the database
static Hash_Table *HASH; // Hash table of all scaffold names
#define SOEL '<' // Start/End of element (scaffold or contig)
#define EOEL '>'
#define SPOS '[' // Start/ending pposition (but not of element)
#define EPOS ']'
static int Comp_Table[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 'T', 0, 'G', 0, 0, 0, 'C',
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 'A', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 't', 0, 'g', 0, 0, 0, 'c',
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 'a', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
static void complement(char *seq, int len)
{ int i, j, x;
for (i = 0, j = len-1; i < j; i++, j--)
{ x = seq[i];
seq[i] = Comp_Table[(int) seq[j]];
seq[j] = Comp_Table[x];
}
if (i <= j)
seq[i] = Comp_Table[(int) seq[i]];
}
int main(int argc, char *argv[])
{ GDB _gdb, *gdb = &_gdb;
Selection *select;
int llen;
int UPPER;
int DOSEQ;
int WIDTH;
// Process arguments
{ int i, j, k;
int flags[128];
char *eptr;
ARG_INIT("GDBshow")
WIDTH = 80;
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("hU")
break;
case 'w':
ARG_NON_NEGATIVE(WIDTH,"Line width")
break;
}
else
argv[j++] = argv[i];
argc = j;
if (flags['U'])
UPPER = UPPER_CASE;
else
UPPER = LOWER_CASE;
DOSEQ = 1-flags['h'];
if (argc <= 1)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
fprintf(stderr,"\n");
fprintf(stderr," <selection> = <range>[+-] [ , <range>[+-] ]*\n");
fprintf(stderr,"\n");
fprintf(stderr," <range> = <object/position> [ - <object/position> ] | @ | .\n");
fprintf(stderr,"\n");
fprintf(stderr," <object/position> = @ <scaffold> [ . <contig>] [ : <position> ]\n");
fprintf(stderr," | . <contig> [ : <position> ]\n");
fprintf(stderr," | <position>\n");
fprintf(stderr,"\n");
fprintf(stderr," <scaffold> = # | <int> | <identifier>\n");
fprintf(stderr," <contig> = # | <int>\n");
fprintf(stderr," <position> = # | <int> [ . <int> ] [kMG]\n");
fprintf(stderr,"\n");
fprintf(stderr," -h: Show only the header lines.\n");
fprintf(stderr," -w: Print -w bp per line (default is 80).\n");
fprintf(stderr," -U: Use upper case for DNA (default is lower case).\n");
exit (1);
}
}
// Read GDB and establish sorted order of headers
{ int s, c;
char *sptr, *eptr;
Read_GDB(gdb,argv[1]);
NSCAFF = gdb->nscaff;
CONTIGS = gdb->contigs;
SCAFFS = gdb->scaffolds;
HEADERS = gdb->headers;
HASH = New_Hash_Table(NSCAFF,1);
for (s = 0; s < NSCAFF; s++)
{ sptr = HEADERS + SCAFFS[s].hoff;
for (eptr = sptr; *eptr != '\0'; eptr++)
if (isspace(*eptr))
break;
c = *eptr;
*eptr = '\0';
if (Hash_Lookup(HASH,sptr) < 0)
Hash_Add(HASH,sptr);
else
{ fprintf(stderr,"%s: Duplicate scaffold name: %s\n",Prog_Name,sptr);
exit (1);
}
*eptr = c;
}
}
// Get the selection list (every contig by default)
if (argc == 3)
select = get_selection_list(argv[2],gdb,HASH,&llen);
else
select = get_selection_list(NULL,gdb,HASH,&llen);
// Display each contig or scaffold in the GDB according to the
// range list of the selection, observing the display option
{ GDB_CONTIG *r;
GDB_SCAFFOLD *s;
char *contig;
char *nstring;
int c, k, x;
int fst, lst, ori;
// Setup 'n' string for printing scaffold gaps and buffer for contigs
nstring = Malloc(WIDTH+1,"Allocating write buffer\n");
if (UPPER == 2)
for (c = 0; c < WIDTH; c++)
nstring[c] = 'N';
else
for (c = 0; c < WIDTH; c++)
nstring[c] = 'n';
nstring[WIDTH] = '\0';
contig = New_Contig_Buffer(gdb);
for (c = 0; c < llen; c++)
{ ori = select[c].orient;
if (select[c].type == SCAFF_SELECTION)
{ for (k = select[c].s1; k <= select[c].s2; k++)
if (ori < 0)
{ int cbeg, cend, wpos;
int j, u, w, f, l;
if (k == select[c].s1)
fst = CONTIGS[select[c].c1].sbeg + select[c].p1;
else
fst = 0;
if (k == select[c].s2)
lst = CONTIGS[select[c].c2].sbeg + select[c].p2;
else
lst = SCAFFS[k].slen;
r = CONTIGS + SCAFFS[k].ectg - 1;
printf(">%s ",HEADERS+SCAFFS[k].hoff);
printf("%c%lld,%lld%c\n",fst==0?SOEL:SPOS,SCAFFS[k].slen-fst,
SCAFFS[k].slen-lst,lst==SCAFFS[k].slen?EOEL:EPOS);
#define ROLLING_PRINT(f,target) \
for (j = f; j+(w = WIDTH-wpos) <= l; j += w) \
{ printf("%.*s\n",w,target); \
wpos = 0; \
} \
if (j < l) \
{ printf("%.*s",l-j,target); \
wpos += l-j; \
}
if (DOSEQ)
{ cbeg = SCAFFS[k].slen;
wpos = 0;
for (u = SCAFFS[k].ectg-1; u >= SCAFFS[k].fctg; u--, r--)
{ cend = r->sbeg + r->clen;
if (cbeg > lst)
cbeg = lst;
if (cend < lst && cbeg > fst)
{ if (cend >= fst)
l = cbeg-cend;
else
l = cbeg-fst;
ROLLING_PRINT(0,nstring)
}
cbeg = r->sbeg;
if (cbeg < lst && cend > fst)
{ Get_Contig(gdb,u,UPPER,contig);
complement(contig,r->clen);
f = fst-cbeg;
l = lst-cbeg;
if (f < 0) f = 0;
if (l > r->clen) l = r->clen;
x = r->clen-f;
f = r->clen-l;
l = x;
ROLLING_PRINT(f,contig+j)
}
}
cend = 0;
if (cbeg > lst)
cbeg = lst;
if (cend < lst && cbeg > fst)
{ if (cend >= fst)
l = cbeg-cend;
else
l = cbeg-fst;
ROLLING_PRINT(0,nstring)
}
printf("\n");
}
}
else
{ int cbeg, cend, wpos;
int j, u, w, f, l;
if (k == select[c].s1)
fst = CONTIGS[select[c].c1].sbeg + select[c].p1;
else
fst = 0;
if (k == select[c].s2)
lst = CONTIGS[select[c].c2].sbeg + select[c].p2;
else
lst = SCAFFS[k].slen;
r = CONTIGS + SCAFFS[k].fctg;
printf(">%s",HEADERS+SCAFFS[k].hoff);
printf("%c%d,%d%c\n",fst==0?SOEL:SPOS,fst,lst,lst==SCAFFS[k].slen?EOEL:EPOS);
if (DOSEQ)
{ cend = 0;
wpos = 0;
for (u = SCAFFS[k].fctg; u < SCAFFS[k].ectg; u++, r++)
{ cbeg = r->sbeg;
if (cend < fst)
cend = fst;
if (cend < lst && cbeg > fst)
{ if (cbeg <= lst)
l = cbeg-cend;
else
l = lst-cend;
ROLLING_PRINT(0,nstring)
}
cend = cbeg + r->clen;
if (cbeg < lst && cend > fst)
{ Get_Contig(gdb,u,UPPER,contig);
f = fst-cbeg;
l = lst-cbeg;
if (f < 0) f = 0;
if (l > r->clen) l = r->clen;
ROLLING_PRINT(f,contig+j)
}
}
cbeg = SCAFFS[k].slen;
if (cend < fst)
cend = fst;
if (cend < lst && cbeg > fst)
{ if (cbeg <= lst)
l = cbeg-cend;
else
l = lst-cend;
ROLLING_PRINT(0,nstring)
}
printf("\n");
}
}
}
else // select[c].type == CONTG_SELECTION
for (k = select[c].c1; k <= select[c].c2; k++)
{ r = CONTIGS + k;
s = SCAFFS + r->scaf;
if (k == select[c].c1)
fst = select[c].p1;
else
fst = 0;
if (k == select[c].c2)
lst = select[c].p2;
else
lst = r->clen;
if (ori < 0)
printf(">%s %c%lld,%lld%c :: Contig %d %c%d,%d%c\n",
HEADERS+s->hoff,r->sbeg+lst==s->slen?SOEL:SPOS,r->sbeg+lst,
r->sbeg+fst,r->sbeg+fst==0?EOEL:EPOS,
k-(s->fctg)+1,lst==r->clen?SOEL:SPOS,lst,fst,fst==0?EOEL:EPOS);
else
printf(">%s %c%lld,%lld%c :: Contig %d %c%d,%d%c\n",
HEADERS+s->hoff,r->sbeg+fst==0?SOEL:SPOS,r->sbeg+fst,
r->sbeg+lst,r->sbeg+lst==s->slen?EOEL:EPOS,
k-(s->fctg)+1,fst==0?SOEL:SPOS,fst,lst,lst==r->clen?EOEL:EPOS);
if (DOSEQ)
{ int j;
Get_Contig(gdb,k,UPPER,contig);
if (ori < 0)
{ x = r->clen - fst;
fst = r->clen - lst;
lst = x;
complement(contig,r->clen);
}
for (j = fst; j+WIDTH < lst; j += WIDTH)
printf("%.*s\n",WIDTH,contig+j);
if (j < lst)
printf("%.*s\n",lst-j,contig+j);
}
fflush(stdout);
}
}
}
Close_GDB(gdb);
exit (0);
}