Debian Codegraph @

atac-driver/clumpMaker/clumpMaker.C

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "util.h"
5 #include "atac.H"
6
7 // Aaron Halpern's clumpMaker algorithm.
8 //
9 // To reproduce the original clumpMaker exactly, assuming that your
10 // atac mapping is for a QUERYvsREFERENCE:
11 //
12 // in=VISD6vsB35LC/VISD6vsB35LC
13 // cut -d' ' -f 1-12 < $in.atac.ckpLast | grep "^M u" | sort -k5,5 -k6n > tmp.a.clumpMaker
14 // $clumpMaker -S -c 50000 -2 -f tmp.a.clumpMaker > $in.50000.clumps
15 //
16 // That is, use only the first 12 columns of info, only ungapped
17 // matches (the original also allows gapped matches, but we don't
18 // have any of those), then sort by the QUERY iid and position. Yes,
19 // the sort is supposed to be alphanumeric.
20 //
21 // Then, run clumpMaker DISABLING it's sort (which sorts iids
22 // numerically), using the second sequence as the reference.
23 //
24
25 class tClumpHit {
26 public:
27
28 void set(atacMatch *m, bool seq1IsRef) {
29
30 match = *m;
31 matchIID = m->matchiid;
32
33 if (seq1IsRef) {
34 refIID = m->iid1;
35 refBeg = m->pos1;
36 refEnd = m->pos1 + m->len1;
37 qryIID = m->iid2;
38 qryBeg = m->pos2;
39 qryEnd = m->pos2 + m->len2;
40 } else {
41 refIID = m->iid2;
42 refBeg = m->pos2;
43 refEnd = m->pos2 + m->len2;
44 qryIID = m->iid1;
45 qryBeg = m->pos1;
46 qryEnd = m->pos1 + m->len1;
47 }
48
49 ori = m->fwd2 ? 1 : -1;
50
51 bestStart = -1;
52 bestExtend = -1;
53 scoreStart = 0;
54 scoreExtend = 0;
55 clump = -1;
56 };
57
58 int64 get_bestScore() const {
59 return(max(scoreStart, scoreExtend));
60 };
61
62 atacMatch match;
63
64 uint32 matchIID;
65
66 uint32 refIID;
67 int32 refBeg;
68 int32 refEnd;
69
70 uint32 qryIID;
71 int32 qryBeg;
72 int32 qryEnd;
73
74 int32 ori;
75
76 int32 scoreStart;
77 int32 bestStart;
78 int32 scoreExtend;
79 int32 bestExtend;
80 int32 clump;
81 };
82
83
84
85 int
86 clumpHitCompareQry(const void *A, const void *B) {
87 const tClumpHit *a = (const tClumpHit *)A;
88 const tClumpHit *b = (const tClumpHit *)B;
89
90 if (a->qryIID > b->qryIID) return(1);
91 if (a->qryIID < b->qryIID) return(-1);
92 if (a->qryBeg > b->qryBeg) return(1);
93 if (a->qryBeg < b->qryBeg) return(-1);
94 if (a->qryEnd > b->qryEnd) return(1);
95 if (a->qryEnd < b->qryEnd) return(-1);
96
97 if (a->refIID > b->refIID) return(1);
98 if (a->refIID < b->refIID) return(-1);
99 if (a->refBeg > b->refBeg) return(1);
100 if (a->refBeg < b->refBeg) return(-1);
101 if (a->refEnd > b->refEnd) return(1);
102 if (a->refEnd < b->refEnd) return(-1);
103
104 return(0);
105 }
106
107 int
108 clumpHitCompareIID(const void *A, const void *B) {
109 const tClumpHit *a = (const tClumpHit *)A;
110 const tClumpHit *b = (const tClumpHit *)B;
111
112 if (a->matchIID > b->matchIID) return(1);
113 if (a->matchIID < b->matchIID) return(-1);
114 return(0);
115 }
116
117
118 bool
119 chainable(tClumpHit *a, tClumpHit *b, int32 maxjump) {
120
121 // return false if
122 // hits are to different chromosomes
123 // hits are not similarly oriented
124 // hits are too far apart on query axis
125 // hits are too far apart on reference axis
126 // hits are "out of order" (we're sorted by the qry)
127 //
128 return(!((a->refIID != b->refIID) || (a->qryIID != b->qryIID) ||
129 (a->ori != b->ori) ||
130 (b->qryBeg - a->qryEnd > maxjump) ||
131 (a->ori * (b->refBeg - a->refEnd) > maxjump) ||
132 (a->ori * (b->refBeg - a->refBeg) < 0)));
133 }
134
135
136 int32
137 score_all_hits(tClumpHit *hits,
138 int32 clumpcost,
139 int32 maxjump,
140 uint32 num_hits){
141
142 // location of best score so far (to which we point whenever starting a new clump)
143 int32 bestEnd = -1;
144
145
146 // best scores so far internal to a reference unit (scaffold, chromosome, etc)
147 int32 bestEndThis = -1;
148 int32 bestScoreThis = -clumpcost;
149
150 // furthest back still accessible ...
151 uint32 furthest_back=0;
152
153 for(uint32 i=0; i<num_hits; i++) {
154
155 if ((i==0) || (hits[i].qryIID != hits[i-1].qryIID)) {
156 bestEnd = bestEndThis; // best of previous query unit
157 bestEndThis = i-1;
158 bestScoreThis = -clumpcost;
159 }
160
161 // find best way of using this as start of a new clump
162 if ((bestEndThis >= 0) &&
163 (bestScoreThis >= 0)) {
164 // start new clump that is not the first for this reference unit
165 hits[i].scoreStart = hits[i].qryEnd - hits[i].qryBeg + bestScoreThis - clumpcost;
166 hits[i].bestStart = bestEndThis;
167 } else {
168 // clump would be first (to be used) for this reference unit
169 hits[i].scoreStart = hits[i].qryEnd - hits[i].qryBeg - clumpcost;
170 hits[i].bestStart = bestEnd;
171 }
172
173 // find best way of extending a clump, if any
174 if (furthest_back < i) {
175 int32 cutoff = hits[i].qryBeg - maxjump;
176
177 while ((hits[furthest_back].qryIID != hits[i].qryIID) ||
178 (hits[furthest_back].qryEnd < cutoff))
179 furthest_back++;
180 }
181
182 int32 extendScore = -clumpcost;
183 int32 extendprev = -1;
184 for (uint32 j=furthest_back; j<i; j++) {
185 if (chainable(hits+j, hits+i, maxjump)) {
186 int32 tmpscore = hits[j].get_bestScore() + hits[i].qryEnd - hits[i].qryBeg;
187 if(extendScore < tmpscore){
188 extendScore=tmpscore;
189 extendprev=j;
190 }
191 }
192 }
193 hits[i].scoreExtend = extendScore;
194 hits[i].bestExtend = extendprev;
195
196 //figure out whether this is a new best ...
197 int32 tmpscore = hits[i].get_bestScore();
198 if (tmpscore > bestScoreThis) {
199 bestScoreThis = tmpscore;
200 bestEndThis = i;
201 }
202 }
203
204 return(bestEndThis);
205 }
206
207
208
209
210
211
212
213
214 int
215 main(int argc, char **argv) {
216 int32 clumpcost = 50000;
217 int32 maxjump = 200000;
218 bool seq1IsRef = false;
219 char *filename = 0L;
220 bool isSorted = false;
221
222 int arg=1;
223 while (arg < argc) {
224 if (strcmp(argv[arg], "-c") == 0) {
225 clumpcost = atoi(argv[++arg]);
226 } else if (strcmp(argv[arg], "-j") == 0) {
227 maxjump = atoi(argv[++arg]);
228 } else if (strcmp(argv[arg], "-1") == 0) {
229 seq1IsRef = true;
230 } else if (strcmp(argv[arg], "-2") == 0) {
231 seq1IsRef = false;
232 } else if (strcmp(argv[arg], "-f") == 0) {
233 filename = argv[++arg];
234 } else if (strcmp(argv[arg], "-S") == 0) {
235 isSorted = true;
236 } else {
237 fprintf(stderr, "Unknown argument '%s'\n", argv[arg]);
238 }
239 arg++;
240 }
241
242 if (filename == 0L) {
243 fprintf(stderr, "usage: %s [] -f filename\n", argv[0]);
244 fprintf(stderr, " -c x penalty for clump start, default 50000\n");
245 fprintf(stderr, " -j x max jump between consistent hits in a clump, default 200000\n");
246 fprintf(stderr, " -1 the reference assembly is the first one.\n");
247 fprintf(stderr, " -2 the reference assembly is the second one (default).\n");
248 fprintf(stderr, " -S assume the input is already sorted by the query IID, position.\n");
249 fprintf(stderr, " this will also make the output sorted by queryIID, queryPosition\n");
250 exit(1);
251 }
252
253
254
255
256 fprintf(stderr, "1 load the matches\n");
257
258 atacFile *AF = new atacFile(filename);
259 uint32 hitsLen = AF->matches()->numberOfMatches();
260 tClumpHit *hits = new tClumpHit [hitsLen];
261
262 for (uint32 i=0; i<hitsLen; i++)
263 hits[i].set(AF->matches()->getMatch(i), seq1IsRef);
264
265
266
267 fprintf(stderr, "2 sort the matches\n");
268 qsort(hits, hitsLen, sizeof(tClumpHit), clumpHitCompareQry);
269
270
271
272 fprintf(stderr, "3 score the matches\n");
273 int32 bestEnd = score_all_hits(hits,
274 clumpcost,
275 maxjump,
276 hitsLen);
277
278 // Mark the clumps
279 //
280 fprintf(stderr, "4 mark clumps\n");
281 uint32 clump = 0;
282
283 while(bestEnd >= 0) {
284 hits[bestEnd].clump = clump;
285
286 if (hits[bestEnd].scoreExtend > hits[bestEnd].scoreStart) {
287 bestEnd = hits[bestEnd].bestExtend;
288 } else {
289 bestEnd = hits[bestEnd].bestStart;
290 clump++;
291 }
292 }
293
294 // Sort the hits by iid, then merge into the output
295 //
296 fprintf(stderr, "5 sort the matches\n");
297 qsort(hits, hitsLen, sizeof(tClumpHit), clumpHitCompareIID);
298
299
300 // For each clump, find the min/max extent in both sequences. We
301 // use this to output the clump match record.
302 //
303 int32 *clumpLoRef = new int32 [clump];
304 int32 *clumpHiRef = new int32 [clump];
305 int32 *clumpLoQry = new int32 [clump];
306 int32 *clumpHiQry = new int32 [clump];
307 bool *clumpOut = new bool [clump];
308
309 for (uint32 xx=0; xx<clump; xx++) {
310 clumpLoRef[xx] = 1000000000;
311 clumpHiRef[xx] = 0;
312 clumpLoQry[xx] = 1000000000;
313 clumpHiQry[xx] = 0;
314 clumpOut[xx] = false;
315 }
316
317 for (uint32 xx=0; xx<hitsLen; xx++) {
318 int32 cc = hits[xx].clump;
319 if (cc >= 0) {
320 if (hits[xx].refBeg < clumpLoRef[cc]) clumpLoRef[cc] = hits[xx].refBeg;
321 if (hits[xx].refEnd > clumpHiRef[cc]) clumpHiRef[cc] = hits[xx].refEnd;
322
323 if (hits[xx].qryBeg < clumpLoQry[cc]) clumpLoQry[cc] = hits[xx].qryBeg;
324 if (hits[xx].qryEnd > clumpHiQry[cc]) clumpHiQry[cc] = hits[xx].qryEnd;
325 }
326 }
327
328 // Dump the clumps
329 //
330
331 fprintf(stderr, "6 output matches with clumps\n");
332
333 AF->writeHeader(stdout);
334
335 for (uint32 mm=0; mm<hitsLen; mm++) {
336 int32 cc = hits[mm].clump;
337
338 if ((cc >= 0) &&
339 (clumpOut[cc] == false)) {
340 atacMatch C;
341 sprintf(C.matchuid, "clump"int32FMTW(06), cc);
342 sprintf(C.parentuid, ".");
343 C.matchiid = 0;
344 C.type[0] = 'c';
345 C.type[1] = 0;
346
347 C.iid1 = hits[mm].match.iid1;
348 C.iid2 = hits[mm].match.iid2;
349
350 // Set the position and length based on the correct reference
351 // -- in particular, since we get the IID and orientation from
352 // the copy of the match we don't need to listen to the
353 // seq1IsRef flag for those.
354
355 if (seq1IsRef) {
356 C.pos1 = clumpLoRef[cc];
357 C.len1 = clumpHiRef[cc] - clumpLoRef[cc];
358 C.pos2 = clumpLoQry[cc];
359 C.len2 = clumpHiQry[cc] - clumpLoQry[cc];
360 } else {
361 C.pos1 = clumpLoQry[cc];
362 C.len1 = clumpHiQry[cc] - clumpLoQry[cc];
363 C.pos2 = clumpLoRef[cc];
364 C.len2 = clumpHiRef[cc] - clumpLoRef[cc];
365 }
366
367 C.fwd1 = hits[mm].match.fwd1;
368 C.fwd2 = hits[mm].match.fwd2;
369
370 C.print(stdout, AF->labelA(), AF->labelB());
371
372 clumpOut[cc] = true;
373 }
374
375
376 if (cc >= 0)
377 sprintf(hits[mm].match.parentuid, "clump"int32FMTW(06), cc);
378 else
379 sprintf(hits[mm].match.parentuid, ".");
380
381 hits[mm].match.print(stdout, AF->labelA(), AF->labelB());
382 }
383
384 return(0);
385 }