forked from thegenemyers/DALIGNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daligner.c
443 lines (369 loc) · 13.9 KB
/
daligner.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
/************************************************************************************\
* *
* 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 *
* *
\************************************************************************************/
/*********************************************************************************************\
*
* Find all local alignment between long, noisy DNA reads:
* Compare sequences in 'subject' database against those in the list of 'target' databases
* searching for local alignments of 1000bp or more (defined constant MIN_OVERLAP in
* filter.c). Subject is compared in both orientations againt each target. An output
* stream of 'Overlap' records (see align.h) is written in binary to the standard output,
* each encoding a given found local alignment between two of the sequences. The -v
* option turns on a verbose reporting mode that gives statistics on each major stage.
*
* There cannot be more than 65,535 reads in a given db, and each read must be less than
* 66,535 characters long.
*
* The filter operates by looking for a pair of diagonal bands of width 2^'s' that contain
* a collection of exact matching 'k'-mers between the two sequences, such that the total
* number of bases covered by 'k'-mer hits is 'h'. k cannot be larger than 15 in the
* current implementation.
*
* Some k-mers are significantly over-represented (e.g. homopolymer runs). These are
* suppressed as seed hits, with the parameter 'm' -- any k-mer that occurs more than
* 'm' times in either the subject or target is not counted as a seed hit. If the -m
* option is absent then no k-mer is suppressed.
*
* For each subject, target pair, say XXX and YYY, the program outputs a file containing
* overlaps of the form XXX.YYY.[C|N]#.las where C implies that the reads in XXX were
* complemented and N implies they were not (both comparisons are performed), and # is
* the thread that detected and wrote out the collection of overlaps. For example, if
* NTHREAD in the program is 4, then 8 files are output for each subject, target pair.
*
* Author: Gene Myers
* Date : June 1, 2014
*
*********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif
#include "DB.h"
#include "filter.h"
static char *Usage[] =
{ "[-vbd] [-k<int(14)>] [-w<int(6)>] [-h<int(35)>] [-t<int>] [-H<int>]",
" [-e<double(.70)] [-l<int(1000)>] [-s<int(100)>] [-M<int>]",
" <subject:file> <target:file> ...",
};
int VERBOSE; // Globally visible to filter.c
int BIASED;
int MINOVER;
int HGAP_MIN;
uint64 MEM_LIMIT;
uint64 MEM_PHYSICAL;
/* Adapted from code by David Robert Nadeau (http://NadeauSoftware.com) licensed under
* "Creative Commons Attribution 3.0 Unported License"
* (http://creativecommons.org/licenses/by/3.0/deed.en_US)
*
* I removed Windows options, reformated, and return int64 instead of size_t
*/
static int64 getMemorySize( )
{
#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
// OSX, NetBSD, OpenBSD
int mib[2];
size_t size = 0;
size_t len = sizeof( size );
mib[0] = CTL_HW;
#if defined(HW_MEMSIZE)
mib[1] = HW_MEMSIZE; // OSX
#elif defined(HW_PHYSMEM64)
mib[1] = HW_PHYSMEM64; // NetBSD, OpenBSD
#endif
if (sysctl(mib,2,&size,&len,NULL,0) == 0)
return ((size_t) size);
return (0);
#elif defined(_SC_AIX_REALMEM)
// AIX
return ((size_t) sysconf( _SC_AIX_REALMEM ) * ((size_t) 1024L));
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
// FreeBSD, Linux, OpenBSD, & Solaris
size_t size = 0;
size = (size_t) sysconf(_SC_PHYS_PAGES);
return (size * ((size_t) sysconf(_SC_PAGESIZE)));
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
// ? Legacy ?
size_t size = 0;
size = (size_t) sysconf(_SC_PHYS_PAGES);
return (size * ((size_t) sysconf(_SC_PAGE_SIZE)));
#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
// DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX
int mib[2];
unsigned int size = 0;
size_t len = sizeof( size );
mib[0] = CTL_HW;
#if defined(HW_REALMEM)
mib[1] = HW_REALMEM; // FreeBSD
#elif defined(HW_PYSMEM)
mib[1] = HW_PHYSMEM; // Others
#endif
if (sysctl(mib,2,&size,&len,NULL,0) == 0)
return (size_t)size;
return (0);
#else
return (0);
#endif
}
static HITS_DB *read_DB(char *name, int dust)
{ static HITS_DB block;
HITS_TRACK *dtrack;
if (Open_DB(name,&block))
exit (1);
if (dust)
dtrack = Load_Track(&block,"dust");
else
dtrack = NULL;
Trim_DB(&block);
if (block.totlen > 0x7fffffffll)
{ fprintf(stderr,"File (%s) is too large\n",name);
exit (1);
}
if (block.nreads > 0xffff)
{ fprintf(stderr,"There are more than %d reads in file (%s)\n",0xffff,name);
exit (1);
}
if (block.maxlen > 0xffff)
{ fprintf(stderr,"Reads are over %d bases long in file (%s)\n",0xffff,name);
exit (1);
}
Read_All_Sequences(&block,0);
if (dtrack != NULL)
{ int *anno = (int *) (dtrack->anno);
int i;
for (i = 0; i <= block.nreads; i++)
anno[i] /= sizeof(int);
}
return (&block);
}
static void complement(char *s, int len)
{ char *t;
int c;
t = s + (len-1);
while (s < t)
{ c = *s;
*s = (char) (3-*t);
*t = (char) (3-c);
s += 1;
t -= 1;
}
if (s == t)
*s = (char) (3-*s);
}
static HITS_DB *complement_DB(HITS_DB *block)
{ static HITS_DB cblock;
int i, nreads;
HITS_READ *reads;
char *seq;
float x;
nreads = block->nreads;
reads = block->reads;
seq = (char *) Malloc(block->reads[nreads].boff+1,"Allocating dazzler sequence block");
if (seq == NULL)
exit (1);
*seq++ = 4;
memcpy(seq,block->bases,block->reads[nreads].boff);
for (i = 0; i < nreads; i++)
complement(seq+reads[i].boff,reads[i].end-reads[i].beg);
cblock = *block;
cblock.bases = (void *) seq;
x = cblock.freq[0];
cblock.freq[0] = cblock.freq[3];
cblock.freq[3] = x;
x = cblock.freq[1];
cblock.freq[1] = cblock.freq[2];
cblock.freq[2] = x;
{ HITS_TRACK *t, *dust;
int *data, *tano, *tata;
int j, p, rlen;
for (t = block->tracks; t != NULL; t++)
if (strcmp(t->name,"dust") == 0)
break;
if (t != NULL)
{ tano = (int *) t->anno;
tata = (int *) t->data;
data = (int *) Malloc(sizeof(int)*tano[nreads],"Allocating dazzler .dust index");
dust = (HITS_TRACK *) Malloc(sizeof(HITS_TRACK),"Allocating dazzler .dust track");
if (data == NULL || dust == NULL)
exit (1);
dust->next = NULL;
dust->name = t->name;
dust->size = 4;
dust->anno = (void *) tano;
dust->data = (void *) data;
cblock.tracks = dust;
p = 0;
for (i = 0; i < nreads; i++)
{ rlen = (reads[i].end-reads[i].beg)-1;
for (j = tano[i+1]-1; j >= tano[i]; j--)
data[p++] = rlen - tata[j];
}
}
}
return (&cblock);
}
int main(int argc, char *argv[])
{ HITS_DB ablock, bblock, cblock;
char *afile, *bfile;
char *aroot, *broot;
Align_Spec *asettings;
int DUSTED;
int KMER_LEN;
int BIN_SHIFT;
int MAX_REPS;
int HIT_MIN;
double AVE_ERROR;
int SPACING;
{ int i, j, k;
int flags[128];
char *eptr;
ARG_INIT("daligner")
KMER_LEN = 14;
HIT_MIN = 35;
BIN_SHIFT = 6;
MAX_REPS = 0;
HGAP_MIN = 0;
AVE_ERROR = .70;
SPACING = 100;
MINOVER = 1000; // Globally visible to filter.c
MEM_PHYSICAL = getMemorySize() / sizeof(uint64);
MEM_LIMIT = MEM_PHYSICAL;
if (MEM_PHYSICAL == 0)
{ fprintf(stderr,"\nWarning: Could not get physical memory size\n");
fflush(stderr);
}
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(KMER_LEN,"K-mer length")
break;
case 'w':
ARG_POSITIVE(BIN_SHIFT,"Log of bin width")
break;
case 'h':
ARG_POSITIVE(HIT_MIN,"Hit threshold (in bp.s)")
break;
case 't':
ARG_POSITIVE(MAX_REPS,"Tuple supression frequency")
break;
case 'H':
ARG_POSITIVE(HGAP_MIN,"HGAP threshold (in bp.s)")
break;
case 'e':
ARG_REAL(AVE_ERROR)
if (AVE_ERROR < .7 || AVE_ERROR >= 1.)
{ fprintf(stderr,"%s: Average correlation must be in [.7,1.) (%g)\n",
Prog_Name,AVE_ERROR);
exit (1);
}
break;
case 'l':
ARG_POSITIVE(MINOVER,"Minimum alignment length")
break;
case 's':
ARG_POSITIVE(SPACING,"Trace spacing")
break;
case 'M':
{ int limit;
ARG_NON_NEGATIVE(limit,"Memory allocation (in Gb)")
MEM_LIMIT = limit * 134217728;
break;
}
}
else
argv[j++] = argv[i];
argc = j;
VERBOSE = flags['v']; // Globally declared in filter.h
BIASED = flags['b']; // Globally declared in filter.h
DUSTED = flags['d'];
if (argc <= 2)
{ 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]);
exit (1);
}
}
MINOVER *= 2;
if (Set_Filter_Params(KMER_LEN,BIN_SHIFT,MAX_REPS,HIT_MIN))
{ fprintf(stderr,"Illegal combination of filter parameters\n");
exit (1);
}
/* Read in the reads in A */
afile = argv[1];
aroot = Root(afile,".db");
ablock = *read_DB(afile,DUSTED);
cblock = *complement_DB(&ablock);
if (ablock.cutoff >= HGAP_MIN)
HGAP_MIN = ablock.cutoff;
asettings = New_Align_Spec( AVE_ERROR, SPACING, ablock.freq);
/* Build indices for A and A complement */
if (VERBOSE)
printf("\nBuilding index for %s\n",aroot);
Build_Table(&ablock);
if (VERBOSE)
printf("\nBuilding index for c(%s)\n",aroot);
Build_Table(&cblock);
/* Compare against reads in B in both orientations */
{ int i;
for (i = 2; i < argc; i++)
{ bfile = argv[i];
broot = Root(bfile,".db");
if (strcmp(afile,bfile) == 0)
{ Match_Filter(aroot,&ablock,broot,&ablock,1,0,asettings);
Match_Filter(aroot,&cblock,broot,&ablock,1,1,asettings);
}
else
{ bblock = *read_DB(bfile,DUSTED);
Match_Filter(aroot,&ablock,broot,&bblock,0,0,asettings);
Match_Filter(aroot,&cblock,broot,&bblock,0,1,asettings);
Close_DB(&bblock);
}
free(broot);
}
}
exit (0);
}