forked from thegenemyers/DALIGNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daligner.c
307 lines (260 loc) · 8.58 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
/*********************************************************************************************\
*
* 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 "DB.h"
#include "filter.h"
static char *Usage[] =
{ "[-vbd] [-k<int(14)>] [-w<int(6)>] [-h<int(35)>] [-t<int>]",
" [-e<double(.70)] [-m<double(.55)>] [-l<int(1000)>] [-s<int(100)>]",
" <subject:file> <target:file> ...",
};
int VERBOSE; // Globally visible to filter.c
int BIASED;
int MINOVER;
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 = 3-*t;
*t = 3-c;
s += 1;
t -= 1;
}
if (s == t)
*s = 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;
double MIN_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;
AVE_ERROR = .70;
MIN_ERROR = .55;
SPACING = 100;
MINOVER = 1000; // Globally visible to filter.c
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 '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 'm':
ARG_REAL(MIN_ERROR)
if (MIN_ERROR < .55 || MIN_ERROR >= 1.)
{ fprintf(stderr,"%s: Minimum correlation must be in [.55,1.) (%g)\n",
Prog_Name,MIN_ERROR);
exit (1);
}
break;
case 's':
ARG_POSITIVE(SPACING,"Trace spacing")
break;
case 'l':
ARG_POSITIVE(MINOVER,"Minimum alignment length")
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);
asettings = New_Align_Spec( AVE_ERROR, MIN_ERROR, SPACING, ablock.freq);
/* Build indices for A and A complement */
Build_Table(&ablock);
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);
}
}
}
exit (0);
}