forked from PacificBiosciences/DALIGNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HPCmapper.c
621 lines (559 loc) · 19.4 KB
/
HPCmapper.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/************************************************************************************\
* *
* 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: [email protected] *
* *
\************************************************************************************/
/*********************************************************************************************\
*
* Produce a script to compute overlaps for all block pairs of a DB, and then sort and merge
* them into as many .las files as their are blocks.
*
* Author: Gene Myers
* Date : December 31, 2014
*
*********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "DB.h"
#include "filter.h"
#undef LSF // define if want a directly executable LSF script
static char *Usage[] =
{ "[-vb] [-k<int(20)>] [-w<int(6)>] [-h<int(50)>] [-t<int>] [-M<int>]",
" [-e<double(.85)] [-l<int(1000)>] [-s<int(100)] [-H<int>]",
" [-m<track>]+ [-dal<int(4)>] [-deg<int(25)>]",
" <ref:db|dam> <reads:db|dam> [<first:int>[-<last:int>]]"
};
static int power(int base, int exp)
{ int i, pow;
pow = 1;
for (i = 0; i < exp; i++)
pow *= base;
return (pow);
}
#define LSF_ALIGN "bsub -q medium -n 4 -o ALIGN.out -e ALIGN.err -R span[hosts=1] -J align#%d"
#define LSF_MERGE "bsub -q short -n 12 -o MERGE.out -e MERGE.err -R span[hosts=1] -J merge#%d"
int main(int argc, char *argv[])
{ int nblocks1, nblocks2;
int useblock1, useblock2;
int fblock, lblock;
#ifdef LSF
int jobid;
#endif
char *pwd1, *root1;
char *pwd2, *root2;
int MUNIT, DUNIT;
int VON, BON;
int WINT, TINT, HGAP, HINT, KINT, SINT, LINT, MINT;
double EREL;
int MMAX, MTOP;
char **MASK;
{ int i, j, k; // Process options
int flags[128];
char *eptr;
ARG_INIT("HPCmapper")
DUNIT = 4;
MUNIT = 25;
KINT = 20;
WINT = 6;
HINT = 50;
TINT = 0;
HGAP = 0;
EREL = 0.;
LINT = 1000;
SINT = 100;
MINT = -1;
MTOP = 0;
MMAX = 10;
MASK = (char **) Malloc(MMAX*sizeof(char *),"Allocating mask track array");
if (MASK == NULL)
exit (1);
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("vbd");
break;
case 'k':
ARG_POSITIVE(KINT,"K-mer length")
break;
case 'w':
ARG_POSITIVE(WINT,"Log of bin width")
break;
case 'h':
ARG_POSITIVE(HINT,"Hit threshold (in bp.s)")
break;
case 't':
ARG_POSITIVE(TINT,"Tuple suppression frequency")
break;
case 'H':
ARG_POSITIVE(HGAP,"HGAP threshold (in bp.s)")
break;
case 'e':
ARG_REAL(EREL)
if (EREL < .7 || EREL >= 1.)
{ fprintf(stderr,"%s: Average correlation must be in [.7,1.) (%g)\n",Prog_Name,EREL);
exit (1);
}
break;
case 'l':
ARG_POSITIVE(LINT,"Minimum ovlerap length")
break;
case 's':
ARG_POSITIVE(SINT,"Trace spacing")
break;
case 'M':
ARG_NON_NEGATIVE(MINT,"Memory allocation (in Gb)")
break;
case 'm':
if (MTOP >= MMAX)
{ MMAX = 1.2*MTOP + 10;
MASK = (char **) Realloc(MASK,MMAX*sizeof(char *),"Reallocating mask track array");
if (MASK == NULL)
exit (1);
}
MASK[MTOP++] = argv[i]+2;
break;
case 'd':
if (argv[i][2] == 'e' && argv[i][3] == 'g')
{ MUNIT = strtol(argv[i]+4,&eptr,10);
if (*eptr != '\0' || argv[i][4] == '\0')
{ fprintf(stderr,"%s: -mrg argument is not an integer\n",Prog_Name);
exit (1);
}
if (MUNIT <= 0)
{ fprintf(stderr,"%s: Files per merge must be positive (%d)\n",
Prog_Name,MUNIT);
exit (1);
}
if (MUNIT < 3)
{ fprintf(stderr,"%s: Files per merge must be at least 3 (%d)\n",
Prog_Name,MUNIT);
exit (1);
}
}
else if (argv[i][2] == 'a' && argv[i][3] == 'l')
{ DUNIT = strtol(argv[i]+4,&eptr,10);
if (*eptr != '\0' || argv[i][4] == '\0')
{ fprintf(stderr,"%s: -dal argument is not an integer\n",Prog_Name);
exit (1);
}
if (DUNIT <= 0)
{ fprintf(stderr,"%s: Blocks per daligner call must be positive (%d)\n",
Prog_Name,DUNIT);
exit (1);
}
}
else
{ fprintf(stderr,"%s: -%.3s is an illegal option\n",Prog_Name,argv[i]+1);
exit (1);
}
break;
}
else
argv[j++] = argv[i];
argc = j;
VON = flags['v'];
BON = flags['b'];
if (argc < 3 || argc > 4)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage[0]);
fprintf(stderr," %*s %s\n",(int) strlen(Prog_Name),"",Usage[1]);
fprintf(stderr," %*s %s\n",(int) strlen(Prog_Name),"",Usage[2]);
fprintf(stderr," %*s %s\n",(int) strlen(Prog_Name),"",Usage[3]);
exit (1);
}
}
// Make sure DAM and DB exist and the DB is partitioned, get number of blocks in partition
pwd1 = PathTo(argv[1]);
if (strcmp(argv[1]+(strlen(argv[1])-4),".dam") == 0)
root1 = Root(argv[1],".dam");
else
root1 = Root(argv[1],".db");
{ int i, nfiles;
FILE *dbvis;
dbvis = fopen(Catenate(pwd1,"/",root1,".dam",NULL),"r");
if (dbvis == NULL)
{ dbvis = Fopen(Catenate(pwd1,"/",root1,".db",NULL),"r");
if (dbvis == NULL)
exit (1);
}
if (fscanf(dbvis,"files = %d\n",&nfiles) != 1)
SYSTEM_ERROR
for (i = 0; i < nfiles; i++)
{ char buffer[30001];
if (fgets(buffer,30000,dbvis) == NULL)
SYSTEM_ERROR
}
useblock1 = 1;
if (fscanf(dbvis,"blocks = %d\n",&nblocks1) != 1)
{ useblock1 = 0;
nblocks1 = 1;
}
fclose(dbvis);
}
pwd2 = PathTo(argv[2]);
if (strcmp(argv[2]+(strlen(argv[2])-4),".dam") == 0)
root2 = Root(argv[2],".dam");
else
root2 = Root(argv[2],".db");
if (strcmp(root2,root1) == 0 && strcmp(pwd1,pwd2) == 0)
{ fprintf(stderr,"%s: Comparing the same data base %s/%s against itself, use HPCdaligner\n",
Prog_Name,pwd1,root1);
exit (1);
}
{ int i, nfiles;
FILE *dbvis;
dbvis = fopen(Catenate(pwd2,"/",root2,".dam",NULL),"r");
if (dbvis == NULL)
{ dbvis = Fopen(Catenate(pwd2,"/",root2,".db",NULL),"r");
if (dbvis == NULL)
exit (1);
}
if (fscanf(dbvis,"files = %d\n",&nfiles) != 1)
SYSTEM_ERROR
for (i = 0; i < nfiles; i++)
{ char buffer[30001];
if (fgets(buffer,30000,dbvis) == NULL)
SYSTEM_ERROR
}
useblock2 = 1;
if (fscanf(dbvis,"blocks = %d\n",&nblocks2) != 1)
{ useblock2 = 0;
nblocks2 = 1;
}
fclose(dbvis);
}
// Set range fblock-lblock checking that DB.<fblock-1>.las exists & DB.<fblock>.las does not
{ char *eptr, *fptr;
FILE *file;
if (argc == 4)
{ fblock = strtol(argv[3],&eptr,10);
if (*eptr != '\0' && *eptr != '-')
{ fprintf(stderr,"%s: final argument '%s' does not start with an integer\n",
Prog_Name,argv[3]);
exit (1);
}
if (*eptr == '-')
{ lblock = strtol(eptr+1,&fptr,10);
if (*fptr != '\0')
{ fprintf(stderr,"%s: second part of range '%s' is not an integer\n",
Prog_Name,eptr+1);
exit (1);
}
}
else
lblock = fblock;
if (fblock < 1 || lblock > nblocks2 || fblock > lblock)
{ fprintf(stderr,"%s: range %d-%d is empty or out of bounds\n",Prog_Name,fblock,lblock);
exit (1);
}
}
else
{ fblock = 1;
lblock = nblocks2;
}
if (fblock > 1)
{ file = fopen(Catenate(root1,".",root2,".",Int_To_Str(fblock-1),".las",NULL),"r");
if (file == NULL)
{ fprintf(stderr,"%s: File %s.%s.%d.las should already be present!\n",
Prog_Name,root1,root2,fblock-1);
exit (1);
}
else
fclose(file);
}
if (useblock2)
{ file = fopen(Catenate(root1,".",root2,".",Int_To_Str(fblock),".las",NULL),"r");
if (file != NULL)
{ fprintf(stderr,"%s: File %s.%s.%d.las should not yet exist!\n",
Prog_Name,root1,root2,fblock);
exit (1);
}
}
else
{ file = fopen(Catenate(root1,".",root2,".las",NULL),"r");
if (file != NULL)
{ fprintf(stderr,"%s: File %s.%s.las should not yet exist!\n",
Prog_Name,root1,root2);
exit (1);
}
}
}
{ int level, njobs;
int i, j, k;
int usepath1, usepath2;
// Produce all necessary daligner jobs ...
usepath1 = (strcmp(pwd1,".") != 0);
usepath2 = (strcmp(pwd2,".") != 0);
njobs = nblocks1 * ( (lblock-fblock)/DUNIT + 1);
printf("# Daligner jobs (%d)\n",njobs);
#ifdef LSF
jobid = 1;
#endif
for (i = 1; i <= nblocks1; i++)
{ int bits;
int low, hgh;
bits = (lblock-fblock)/DUNIT+1;
low = fblock;
for (j = 1; j <= bits; j++)
{
#ifdef LSF
printf(LSF_ALIGN,jobid++);
printf(" \"");
#endif
printf("daligner -A");
if (VON)
printf(" -v");
if (BON)
printf(" -b");
printf(" -k%d",KINT);
if (WINT != 6)
printf(" -w%d",WINT);
printf(" -h%d",HINT);
if (TINT > 0)
printf(" -t%d",TINT);
if (HGAP > 0)
printf(" -H%d",HGAP);
if (EREL > .1)
printf(" -e%g",EREL);
else
printf(" -e.85");
if (LINT != 1000)
printf(" -l%d",LINT);
if (SINT != 100)
printf(" -s%d",SINT);
if (MINT >= 0)
printf(" -M%d",MINT);
for (k = 0; k < MTOP; k++)
printf(" -m%s",MASK[k]);
if (useblock1)
if (usepath1)
printf(" %s/%s.%d",pwd1,root1,i);
else
printf(" %s.%d",root1,i);
else
if (usepath1)
printf(" %s/%s",pwd1,root1);
else
printf(" %s",root1);
hgh = fblock + (((lblock-fblock)+1)*j)/bits;
for (k = low; k < hgh; k++)
if (useblock2)
if (usepath2)
printf(" %s/%s.%d",pwd2,root2,k);
else
printf(" %s.%d",root2,k);
else
if (usepath2)
printf(" %s/%s",pwd2,root2);
else
printf(" %s",root2);
#ifdef LSF
printf("\"");
#endif
printf("\n");
low = hgh;
}
}
// ... and then all the initial sort & merge jobs for each block pair
printf("# Initial sort jobs (%d)\n", nblocks1*((lblock-fblock)+1));
#ifdef LSF
jobid = 1;
#endif
for (i = 1; i <= nblocks1; i++)
for (j = fblock; j <= lblock; j++)
{
#ifdef LSF
printf(LSF_MERGE,jobid++);
printf(" \"");
#endif
printf("LAsort");
if (VON)
printf(" -v");
for (k = 0; k < NTHREADS; k++)
{ if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.C%d",root2,j,k);
else
printf(".%s.C%d",root2,k);
if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.N%d",root2,j,k);
else
printf(".%s.N%d",root2,k);
}
printf(" && LAmerge");
if (VON)
printf(" -v");
if (nblocks1 == 1)
if (useblock2)
printf(" %s.%s.%d",root1,root2,j);
else
printf(" %s.%s",root1,root2);
else
printf(" L1.%d.%d",i,j);
for (k = 0; k < NTHREADS; k++)
{ if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.C%d.S",root2,j,k);
else
printf(".%s.C%d.S",root2,k);
if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.N%d.S",root2,j,k);
else
printf(".%s.N%d.S",root2,k);
}
printf(" && rm");
for (k = 0; k < NTHREADS; k++)
{ if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.C%d.S.las",root2,j,k);
else
printf(".%s.C%d.S.las",root2,k);
if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.N%d.S.las",root2,j,k);
else
printf(".%s.N%d.S.las",root2,k);
if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.C%d.las",root2,j,k);
else
printf(".%s.C%d.las",root2,k);
if (useblock1)
printf(" %s.%d",root1,i);
else
printf(" %s",root1);
if (useblock2)
printf(".%s.%d.N%d.las",root2,j,k);
else
printf(".%s.N%d.las",root2,k);
}
#ifdef LSF
printf("\"");
#endif
printf("\n");
}
// Higher level merges (if lblock > 1)
if (nblocks1 > 1)
{ int pow, mway;
// Determine most balance mway for merging in ceil(log_mrg lblock) levels
pow = 1;
for (level = 0; pow < nblocks1; level++)
pow *= MUNIT;
for (mway = MUNIT; mway >= 3; mway--)
if (power(mway,level) < nblocks1)
break;
mway += 1;
// Issue the commands for each merge level
{ int p, cnt;
cnt = nblocks1;
for (i = 1; i <= level; i++)
{ int bits;
int low, hgh;
bits = (cnt-1)/mway+1;
printf("# Level %d jobs (%d)\n",i,bits*((lblock-fblock)+1));
// Block merges
#ifdef LSF
jobid = 1;
#endif
for (j = fblock; j <= lblock; j++)
{ low = 1;
for (p = 1; p <= bits; p++)
{ hgh = (cnt*p)/bits;
#ifdef LSF
printf(LSF_MERGE,jobid++);
printf(" \"");
#endif
printf("LAmerge");
if (VON)
printf(" -v");
if (i == level)
if (useblock2)
printf(" %s.%s.%d",root1,root2,j);
else
printf(" %s.%s",root1,root2);
else
printf(" L%d.%d.%d",i+1,j,p);
for (k = low; k <= hgh; k++)
printf(" L%d.%d.%d",i,j,k);
printf(" && rm");
for (k = low; k <= hgh; k++)
printf(" L%d.%d.%d.las",i,j,k);
#ifdef LSF
printf("\"");
#endif
printf("\n");
low = hgh+1;
}
}
cnt = bits;
}
}
}
}
free(root2);
free(pwd2);
free(root1);
free(pwd1);
exit (0);
}