forked from thegenemyers/DALIGNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAsort.c
291 lines (238 loc) · 7.5 KB
/
LAsort.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
/*******************************************************************************************
*
* Load a file U.las of overlaps into memory, sort them all by A,B index,
* and then output the result to U.S.las
*
* Author: Gene Myers
* Date : July 2013
*
*******************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "DB.h"
#include "align.h"
static char *Usage = "[-va] <align:las> ...";
#define MEMORY 1000 // How many megabytes for output buffer
static char *IBLOCK;
static int SORT_OVL(const void *x, const void *y)
{ int64 l = *((int64 *) x);
int64 r = *((int64 *) y);
Overlap *ol, *or;
int al, ar;
int bl, br;
int cl, cr;
int pl, pr;
ol = (Overlap *) (IBLOCK+l);
or = (Overlap *) (IBLOCK+r);
al = ol->aread;
ar = or->aread;
if (al != ar)
return (al-ar);
bl = ol->bread;
br = or->bread;
if (bl != br)
return (bl-br);
cl = COMP(ol->flags);
cr = COMP(or->flags);
if (cl != cr)
return (cl-cr);
pl = ol->path.abpos;
pr = or->path.abpos;
if (pl != pr)
return (pl-pr);
if (ol < or)
return (-1);
else if (ol > or)
return (1);
else
return (0);
}
static int SORT_MAP(const void *x, const void *y)
{ int64 l = *((int64 *) x);
int64 r = *((int64 *) y);
Overlap *ol, *or;
int al, ar;
int pl, pr;
ol = (Overlap *) (IBLOCK+l);
or = (Overlap *) (IBLOCK+r);
al = ol->aread;
ar = or->aread;
if (al != ar)
return (al-ar);
pl = ol->path.abpos;
pr = or->path.abpos;
if (pl != pr)
return (pl-pr);
if (ol < or)
return (-1);
else if (ol > or)
return (1);
else
return (0);
}
int main(int argc, char *argv[])
{ char *iblock, *fblock, *iend;
int64 isize, osize;
int64 ovlsize, ptrsize;
int tspace, tbytes;
int i;
int VERBOSE;
int MAP_ORDER;
// Process options
{ int j, k;
int flags[128];
ARG_INIT("LAsort")
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
{ ARG_FLAGS("va") }
else
argv[j++] = argv[i];
argc = j;
VERBOSE = flags['v'];
MAP_ORDER = flags['a'];
if (argc <= 1)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
fprintf(stderr,"\n");
fprintf(stderr," -v: Verbose mode, output statistics as proceed.\n");
fprintf(stderr," -a: sort .las by A-read,A-position pairs for map usecase\n");
fprintf(stderr," off => sort .las by A,B-read pairs for overlap piles\n");
exit (1);
}
}
// For each file do
ptrsize = sizeof(void *);
ovlsize = sizeof(Overlap) - ptrsize;
isize = 0;
iblock = NULL;
osize = MEMORY * 1000000ll;
fblock = Malloc(osize,"Allocating LAsort output block");
for (i = 1; i < argc; i++)
{ int64 *perm;
FILE *input, *foutput;
int64 novl, sov;
Block_Looper *parse;
parse = Parse_Block_Arg(argv[i]);
while ((input = Next_Block_Arg(parse)) != NULL)
{
// Read in the entire file and output header
{ int64 size;
struct stat info;
stat(Catenate(Block_Arg_Path(parse),"/",Block_Arg_Root(parse),".las"),&info);
size = info.st_size;
if (fread(&novl,sizeof(int64),1,input) != 1)
SYSTEM_READ_ERROR
if (fread(&tspace,sizeof(int),1,input) != 1)
SYSTEM_READ_ERROR
if (tspace <= TRACE_XOVR && tspace != 0)
tbytes = sizeof(uint8);
else
tbytes = sizeof(uint16);
if (VERBOSE)
{ printf(" %s: ",Block_Arg_Root(parse));
Print_Number(novl,0,stdout);
printf(" records ");
Print_Number(size-novl*ovlsize,0,stdout);
printf(" trace bytes\n");
fflush(stdout);
}
foutput = Fopen(Catenate(Block_Arg_Path(parse),"/",Block_Arg_Root(parse),".S.las"),"w");
if (foutput == NULL)
exit (1);
if (fwrite(&novl,sizeof(int64),1,foutput) != 1)
SYSTEM_READ_ERROR
if (fwrite(&tspace,sizeof(int),1,foutput) != 1)
SYSTEM_READ_ERROR
if (size > isize)
{ if (iblock == NULL)
iblock = Malloc(size+ptrsize,"Allocating LAsort input block");
else
iblock = Realloc(iblock-ptrsize,size+ptrsize,"Allocating LAsort input block");
if (iblock == NULL)
exit (1);
iblock += ptrsize;
isize = size;
}
size -= (sizeof(int64) + sizeof(int));
if (size > 0)
{ if (fread(iblock,size,1,input) != 1)
SYSTEM_READ_ERROR
}
fclose(input);
iend = iblock + (size - ptrsize);
}
// Set up unsorted permutation array
perm = (int64 *) Malloc(sizeof(int64)*novl,"Allocating LAsort permutation vector");
if (perm == NULL)
exit (1);
{ int64 off;
int j;
if (CHAIN_START(((Overlap *) (iblock-ptrsize))->flags))
{ sov = 0;
off = -ptrsize;
for (j = 0; j < novl; j++)
{ if (CHAIN_START(((Overlap *) (iblock+off))->flags))
perm[sov++] = off;
off += ovlsize + ((Overlap *) (iblock+off))->path.tlen*tbytes;
}
}
else
{ off = -ptrsize;
for (j = 0; j < novl; j++)
{ perm[j] = off;
off += ovlsize + ((Overlap *) (iblock+off))->path.tlen*tbytes;
}
sov = novl;
}
}
// Sort permutation array of ptrs to records
IBLOCK = iblock;
if (MAP_ORDER)
qsort(perm,sov,sizeof(int64),SORT_MAP);
else
qsort(perm,sov,sizeof(int64),SORT_OVL);
// Output the records in sorted order
{ int j;
Overlap *w;
int64 tsize, span;
char *fptr, *ftop, *wo;
fptr = fblock;
ftop = fblock + osize;
for (j = 0; j < sov; j++)
{ w = (Overlap *) (wo = iblock+perm[j]);
do
{ tsize = w->path.tlen*tbytes;
span = ovlsize + tsize;
if (fptr + span > ftop)
{ if (fwrite(fblock,1,fptr-fblock,foutput) != (size_t) (fptr-fblock))
SYSTEM_READ_ERROR
fptr = fblock;
}
memmove(fptr,((char *) w)+ptrsize,ovlsize);
fptr += ovlsize;
memmove(fptr,(char *) (w+1),tsize);
fptr += tsize;
w = (Overlap *) (wo += span);
}
while (wo < iend && CHAIN_NEXT(w->flags));
}
if (fptr > fblock)
{ if (fwrite(fblock,1,fptr-fblock,foutput) != (size_t) (fptr-fblock))
SYSTEM_READ_ERROR
}
}
}
free(perm);
fclose(foutput);
Free_Block_Arg(parse);
}
if (iblock != NULL)
free(iblock - ptrsize);
free(fblock);
exit (0);
}