File: LAsort.c

package info (click to toggle)
daligner 1.0%2Bgit20200727.ed40ce5-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 744 kB
  • sloc: ansic: 15,253; makefile: 202; sh: 20
file content (413 lines) | stat: -rw-r--r-- 9,881 bytes parent folder | download | duplicates (2)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
/*******************************************************************************************
 *
 *  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);

  pl = ol->path.aepos;
  pr = or->path.aepos;
  if (pl != pr)
    return (pl-pr);

  pl = ol->path.bbpos;
  pr = or->path.bbpos;
  if (pl != pr)
    return (pl-pr);

  pl = ol->path.bepos;
  pr = or->path.bepos;
  if (pl != pr)
    return (pl-pr);

  pl = ol->path.diffs;
  pr = or->path.diffs;
  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      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);

  pl = ol->path.abpos;
  pr = or->path.abpos;
  if (pl != pr)
    return (pl-pr);

  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.aepos;
  pr = or->path.aepos;
  if (pl != pr)
    return (pl-pr);

  pl = ol->path.bbpos;
  pr = or->path.bbpos;
  if (pl != pr)
    return (pl-pr);

  pl = ol->path.bepos;
  pr = or->path.bepos;
  if (pl != pr)
    return (pl-pr);

  pl = ol->path.diffs;
  pr = or->path.diffs;
  if (pl != pr)
    return (pl-pr);

  if (ol < or)
    return (-1);
  else if (ol > or)
    return (1);
  else
    return (0);
}

static int EQUAL(Overlap *ol, Overlap *or)
{ int      al, ar;
  int      bl, br;
  int      cl, cr;
  int      pl, pr;

  al = ol->aread;
  ar = or->aread;
  if (al != ar)
    return (0);

  bl = ol->bread;
  br = or->bread;
  if (bl != br)
    return (0);

  cl = COMP(ol->flags);
  cr = COMP(or->flags);
  if (cl != cr)
    return (0);

  pl = ol->path.abpos;
  pr = or->path.abpos;
  if (pl != pr)
    return (0);

  pl = ol->path.aepos;
  pr = or->path.aepos;
  if (pl != pr)
    return (0);

  pl = ol->path.bbpos;
  pr = or->path.bbpos;
  if (pl != pr)
    return (0);

  pl = ol->path.bepos;
  pr = or->path.bepos;
  if (pl != pr)
    return (0);

  return (1);
}

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_LAS_Arg(argv[i]);

      while ((input = Next_Block_Arg(parse)) != NULL)
        {
          //  Read in the entire file and output header

          { int64  size;
            struct stat info;
            char  *root, *path;

            path = Block_Arg_Path(parse);
            root = Block_Arg_Root(parse);

            stat(Catenate(path,"/",root,".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: ",root);
                Print_Number(novl,0,stdout);
                printf(" records ");
                Print_Number(size-novl*ovlsize,0,stdout);
                printf(" trace bytes\n");
                fflush(stdout);
              }

            foutput = Fopen(Catenate(path,"/",root,".S.las"),"w");
            if (foutput == NULL)
              exit (1);

            if (fwrite(&novl,sizeof(int64),1,foutput) != 1)
              SYSTEM_WRITE_ERROR
            if (fwrite(&tspace,sizeof(int),1,foutput) != 1)
              SYSTEM_WRITE_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);

            free(root);
            free(path);
          }

          if (novl == 0)
            { fclose(foutput);
              continue;
            }
    
          //  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, equal;
            Overlap *w, *x, y;
            int64    tsize, span;
            char    *fptr, *ftop, *wo;
    
            y.aread = ((Overlap *) (iblock+perm[0]))->aread+1;
            x = &y;

            fptr = fblock;
            ftop = fblock + osize;
            for (j = 0; j < sov; j++)
              { w = (Overlap *) (wo = iblock+perm[j]);
                do
                  { equal = EQUAL(w,x); 
                    tsize = w->path.tlen*tbytes;
                    span  = ovlsize + tsize;
                    if (fptr + span > ftop)
                      { if (fwrite(fblock,1,fptr-fblock,foutput) != (size_t) (fptr-fblock))
                          SYSTEM_WRITE_ERROR
                        fptr = fblock;
                      }
                    if (equal)
                      { fptr += (ovlsize + tsize);
                        novl -= 1;
                      }
                    else
                      { memmove(fptr,((char *) w)+ptrsize,ovlsize);
                        fptr += ovlsize;
                        memmove(fptr,(char *) (w+1),tsize);
                        fptr += tsize;
                      }
                    x = w;
                    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_WRITE_ERROR
              }
          }

          rewind(foutput);
          if (fwrite(&novl,sizeof(int64),1,foutput) != 1)
            SYSTEM_WRITE_ERROR

          free(perm);
          fclose(foutput);
        }
      Free_Block_Arg(parse);
    }
    
  if (iblock != NULL)
    free(iblock - ptrsize);
  free(fblock);

  exit (0);
}