forked from thegenemyers/DALIGNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAshow.c
400 lines (354 loc) · 12.5 KB
/
LAshow.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
397
398
399
400
/************************************************************************************\
* *
* Copyright (c) 2014, Dr. Eugene W. Myers (EWM). All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, *
* are permitted provided that the following conditions are met: *
* *
* · Redistributions of source code must retain the above copyright notice, this *
* list of conditions and the following disclaimer. *
* *
* · Redistributions in binary form must reproduce the above copyright notice, this *
* list of conditions and the following disclaimer in the documentation and/or *
* other materials provided with the distribution. *
* *
* · The name of EWM may not be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY EWM ”AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EWM BE LIABLE *
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN *
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* For any issues regarding this software and its use, contact EWM at: *
* *
* Eugene W. Myers Jr. *
* Bautzner Str. 122e *
* 01099 Dresden *
* GERMANY *
* Email: gene.myers@gmail.com *
* *
\************************************************************************************/
/*******************************************************************************************
*
* Utility for displaying the overlaps in a .las file in a variety of ways including
* a minimal listing of intervals, a cartoon, and a full out alignment. One can
* also list only those overlaps whose A read is in a set of supplied read ranges.
*
* Author: Gene Myers
* Date : July 2013
*
*******************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "DB.h"
#include "align.h"
static int ORDER(const void *l, const void *r)
{ int x = *((int32 *) l);
int y = *((int32 *) r);
return (x-y);
}
static char *Usage[] =
{ "[-coU] [-(a|r):<db>] [-i<int(4)>] [-w<int(100)>] [-b<int(10)>] ",
" <align:las> [ <reads:range> ... ]"
};
int main(int argc, char *argv[])
{ HITS_DB _db, *db = &_db;
Overlap _ovl, *ovl = &_ovl;
Alignment _aln, *aln = &_aln;
FILE *input;
int64 novl;
int tspace, tbytes, small;
int reps, *pts;
int ALIGN, CARTOON, OVERLAP, REFERENCE;
int INDENT, WIDTH, BORDER, UPPERCASE;
// Process options
{ int i, j, k;
int flags[128];
char *eptr;
ARG_INIT("LAshow")
ALIGN = 0;
REFERENCE = 0;
INDENT = 4;
WIDTH = 100;
BORDER = 10;
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("coU")
break;
case 'i':
ARG_NON_NEGATIVE(INDENT,"Indent")
break;
case 'w':
ARG_POSITIVE(WIDTH,"Alignment width")
break;
case 'b':
ARG_NON_NEGATIVE(BORDER,"Alignment border")
break;
case 'r':
REFERENCE = 1;
case 'a':
ALIGN = 1;
if (argv[i][2] != ':')
{ fprintf(stderr,"%s: Unrecognizable option %s\n",Prog_Name,argv[i]);
exit (1);
}
if (Open_DB(argv[i]+3,db))
exit (1);
Trim_DB(db);
break;
}
else
argv[j++] = argv[i];
argc = j;
CARTOON = flags['c'];
OVERLAP = flags['o'];
UPPERCASE = flags['U'];
if (argc <= 1)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage[0]);
fprintf(stderr," %*s %s\n",(int) strlen(Prog_Name),"",Usage[1]);
exit (1);
}
}
// Process read index arguments into a sorted list of read ranges
pts = (int *) Malloc(sizeof(int)*2*argc,"Allocating read parameters");
if (pts == NULL)
exit (1);
reps = 0;
if (argc > 2)
{ int c, b, e;
char *eptr, *fptr;
for (c = 2; c < argc; c++)
{ if (argv[c][0] == '#')
{ fprintf(stderr,"%s: # is not allowed as range start, '%s'\n",
Prog_Name,argv[c]);
exit (1);
}
else
{ b = strtol(argv[c],&eptr,10);
if (b < 1)
{ fprintf(stderr,"%s: Non-positive index?, '%d'\n",Prog_Name,b);
exit (1);
}
}
if (eptr > argv[c])
{ if (*eptr == '\0')
{ pts[reps++] = b;
pts[reps++] = b;
continue;
}
else if (*eptr == '-')
{ if (eptr[1] == '#')
{ e = INT32_MAX;
fptr = eptr+2;
}
else
e = strtol(eptr+1,&fptr,10);
if (fptr > eptr+1 && *fptr == 0 && eptr[1] != '-')
{ pts[reps++] = b;
pts[reps++] = e;
if (b > e)
{ fprintf(stderr,"%s: Empty range '%s'\n",Prog_Name,argv[c]);
exit (1);
}
continue;
}
}
}
fprintf(stderr,"%s: argument '%s' is not an integer range\n",Prog_Name,argv[c]);
exit (1);
}
qsort(pts,reps/2,sizeof(int64),ORDER);
b = 0;
for (c = 0; c < reps; c += 2)
if (b > 0 && pts[b-1] >= pts[c]-1)
{ if (pts[c+1] > pts[b-1])
pts[b-1] = pts[c+1];
}
else
{ pts[b++] = pts[c];
pts[b++] = pts[c+1];
}
pts[b++] = INT32_MAX;
reps = b;
}
else
{ pts[reps++] = 1;
pts[reps++] = INT32_MAX;
}
// Initiate file reading and read (novl, tspace) header
{ char *over, *pwd, *root;
pwd = PathTo(argv[1]);
root = Root(argv[1],".las");
over = Catenate(pwd,"/",root,".las");
input = Fopen(over,"r");
if (input == NULL)
exit (1);
if (fread(&novl,sizeof(int64),1,input) != 1)
SYSTEM_ERROR
if (fread(&tspace,sizeof(int),1,input) != 1)
SYSTEM_ERROR
if (tspace <= TRACE_XOVR)
{ small = 1;
tbytes = sizeof(uint8);
}
else
{ small = 0;
tbytes = sizeof(uint16);
}
printf("\n%s: ",root);
Print_Number(novl,0,stdout);
printf(" records\n");
free(pwd);
free(root);
}
// Read the file and display selected records
{ int j;
uint16 *trace;
Work_Data *work;
int tmax;
int in, npt, idx, ar;
int64 tps;
if (ALIGN)
{ work = New_Work_Data();
aln->path = &(ovl->path);
aln->aseq = New_Read_Buffer(db);
aln->bseq = New_Read_Buffer(db);
}
else
work = NULL;
tmax = 1000;
trace = (uint16 *) Malloc(sizeof(uint16)*tmax,"Allocating trace vector");
if (trace == NULL)
exit (1);
in = 0;
npt = pts[0];
idx = 1;
// For each record do
for (j = 0; j < novl; j++)
// Read it in
{ Read_Overlap(input,ovl);
if (ovl->path.tlen > tmax)
{ tmax = ((int) 1.2*ovl->path.tlen) + 100;
trace = (uint16 *) Realloc(trace,sizeof(uint16)*tmax,"Allocating trace vector");
if (trace == NULL)
exit (1);
}
ovl->path.trace = (void *) trace;
Read_Trace(input,ovl,tbytes);
// Determine if it should be displayed
ar = ovl->aread+1;
if (in)
{ while (ar > npt)
{ npt = pts[idx++];
if (ar < npt)
{ in = 0;
break;
}
npt = pts[idx++];
}
}
else
{ while (ar >= npt)
{ npt = pts[idx++];
if (ar <= npt)
{ in = 1;
break;
}
npt = pts[idx++];
}
}
if (!in)
continue;
if (OVERLAP)
{ if (ovl->path.abpos != 0 && ovl->path.bbpos != 0)
continue;
if (ovl->path.aepos != ovl->alen && ovl->path.bepos != ovl->blen)
continue;
}
// Display it
if (CARTOON || ALIGN)
printf("\n");
Print_Number((int64) ovl->aread+1,10,stdout);
printf(" ");
Print_Number((int64) ovl->bread+1,9,stdout);
if (COMP(ovl->flags))
printf(" c");
else
printf(" n");
printf(" [");
Print_Number((int64) ovl->path.abpos,6,stdout);
printf("..");
Print_Number((int64) ovl->path.aepos,6,stdout);
printf("] x [");
Print_Number((int64) ovl->path.bbpos,6,stdout);
printf("..");
Print_Number((int64) ovl->path.bepos,6,stdout);
printf("]");
tps = ((ovl->path.aepos-1)/tspace - ovl->path.abpos/tspace);
if (ALIGN)
{ if (small)
Decompress_TraceTo16(ovl);
aln->alen = ovl->alen;
aln->blen = ovl->blen;
aln->flags = ovl->flags;
Load_Read(db,ovl->aread,aln->aseq,0);
Load_Read(db,ovl->bread,aln->bseq,0);
if (COMP(aln->flags))
Complement_Seq(aln->bseq);
Compute_Trace_PTS(aln,work,tspace);
if (CARTOON)
{ printf(" (");
Print_Number(tps,3,stdout);
printf(" trace pts)\n\n");
Print_ACartoon(stdout,aln,INDENT);
}
else
{ printf(" : = ");
Print_Number((int64) ovl->path.diffs,6,stdout);
printf(" diffs (");
Print_Number(tps,3,stdout);
printf(" trace pts)\n");
}
if (REFERENCE)
Print_Reference(stdout,aln,work,INDENT,WIDTH,BORDER,UPPERCASE,5);
else
Print_Alignment(stdout,aln,work,INDENT,WIDTH,BORDER,UPPERCASE,5);
}
else if (CARTOON)
{ printf(" (");
Print_Number(tps,3,stdout);
printf(" trace pts)\n\n");
Print_OCartoon(stdout,ovl,INDENT);
}
else
{ printf(" : < ");
Print_Number((int64) ovl->path.diffs,6,stdout);
printf(" diffs (");
Print_Number(tps,3,stdout);
printf(" trace pts)\n");
}
}
free(trace);
if (ALIGN)
{ free(aln->bseq-1);
free(aln->aseq-1);
Free_Work_Data(work);
}
}
exit (0);
}