File: aln_cigar.h

package info (click to toggle)
bio-rainbow 2.0.4+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 560 kB
  • sloc: ansic: 7,475; perl: 172; makefile: 129; sh: 49
file content (440 lines) | stat: -rw-r--r-- 10,743 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
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
/*
 * 
 * Copyright (c) 2011, Jue Ruan <ruanjue@gmail.com>
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#ifndef __ALN_CIGAR_RJ_H
#define __ALN_CIGAR_RJ_H

#include <stdlib.h>
#include <stdio.h>

#define ALN_CIGAR_MAX_LEN	8191

#define ALN_CIGAR_TYPE_NULL	0
#define ALN_CIGAR_TYPE_MAT	3
#define ALN_CIGAR_TYPE_INS	1
#define ALN_CIGAR_TYPE_DEL	2
#define ALN_CIGAR_TYPE_SKIP	7
#define ALN_CIGAR_TYPE_CLIP1	5
#define ALN_CIGAR_TYPE_CLIP2	6

static const char aln_cigar_string[8] = "?IDM?SHN";

typedef struct {
	uint16_t len:13, type:3;
} AlnCigar;

static inline void cigars_lengths(AlnCigar *cigars, int n_cigar, int *aln_size, int *seq1_size, int *seq2_size){
	int i;
	if(aln_size) *aln_size = 0;
	if(seq1_size) *seq1_size = 0;
	if(seq2_size) *seq2_size = 0;
	for(i=0;i<n_cigar;i++){
		if(seq1_size &&  (cigars[i].type & 0x01)) *seq1_size += cigars[i].len;
		if(seq2_size &&  (cigars[i].type & 0x02)) *seq2_size += cigars[i].len;
		if(aln_size  && !(cigars[i].type & 0x04)) *aln_size += cigars[i].len;
	}
}

static inline int _aln_cigar_h_num_str_len(int n){
	int i;
	i = 0;
	while(n){
		i ++;
		n /= 10;
	}
	return i;
}

static inline int _aln_cigar_add_cigar(AlnCigar *cs, int n_cigar, int len, int type){
	while(len){
		if(len > ALN_CIGAR_MAX_LEN){
			cs[n_cigar].len = ALN_CIGAR_MAX_LEN;
			len -= ALN_CIGAR_MAX_LEN;
		} else {
			cs[n_cigar].len = len;
			len = 0;
		}
		cs[n_cigar++].type = type;
	}
	return n_cigar;
}

static inline char* cigars2string(AlnCigar *cigars, int n_cigar, char *str){
	int i, j, n, str_len, type;
	char *p;
	if(str == NULL){
		str_len = 0;
		for(i=0;i<n_cigar;i++) str_len += _aln_cigar_h_num_str_len(cigars[i].len) + 1;
		str = malloc(str_len + 1);
	}
	p = str;
	if(n_cigar){
		n = cigars[0].len;
		type = cigars[0].type;
		for(i=1;i<=n_cigar;i++){
			if(i == n_cigar || (type != cigars[i].type && n)){
				str_len = _aln_cigar_h_num_str_len(n) - 1;
				j = 0;
				while(n){
					p[str_len - j] = '0' + (n % 10);
					n /= 10;
					j ++;
				}
				p[str_len + 1] = aln_cigar_string[type];
				p = p + str_len + 1 + 1;
			}
			if(i == n_cigar) break;
			n += cigars[i].len;
			type = cigars[i].type;
		}
	}
	p[0] = 0;
	return str;
}

static inline int string2cigars(AlnCigar *cigars, char *str, int len){
	int i, n, x;
	n = 0;
	x = 0;
	for(i=0;i<len;i++){
		switch(str[i]){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9': x = x * 10 + (str[i] - '0'); break;
			case 'M':
			case 'm': n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_MAT); x = 0; break;
			case 'I':
			case 'i': n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_INS); x = 0; break;
			case 'D':
			case 'd': n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_DEL); x = 0; break;
			case 'S':
			case 's': n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_CLIP1); x = 0; break;
			case 'H':
			case 'h': n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_CLIP2); x = 0; break;
			case 'N':
			case 'n': n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_SKIP); x = 0; break;
			default : n = _aln_cigar_add_cigar(cigars, n, x, ALN_CIGAR_TYPE_NULL); x = 0; break;
		}
	}
	return n;
}

static inline int rank_cigars_seqlen(AlnCigar *cigars, int n_cigar, int len, int seq_idx){
	int i, ret;
	if(len < 0) len = 0x7FFFFFFF;
	for(i=0,ret=0;i<n_cigar&&len>=0;i++){
		if((cigars[i].type >> seq_idx) & 0x01){
			if(len > (int)cigars[i].len){
				ret += cigars[i].len;
			} else {
				ret += len;
			}
		}
		len -= cigars[i].len;
	}
	return ret;
}

static inline int rev_rank_cigars_seqlen(AlnCigar *cigars, int n_cigar, int len, int seq_idx){
	int i, ret;
	if(len < 0) len = 0x7FFFFFFF;
	for(i=n_cigar-1,ret=0;i>=0&&len>=0;i--){
		if((cigars[i].type >> seq_idx) & 0x01){
			if(len > (int)cigars[i].len){
				ret += cigars[i].len;
			} else {
				ret += len;
			}
		}
		len -= cigars[i].len;
	}
	return ret;
}

static inline int select_cigars_seqlen(AlnCigar *cigars, int n_cigar, int len, int seq_idx){
	int i, ret;
	for(i=0,ret=0;i<n_cigar;i++){
		if((cigars[i].type >> seq_idx) & 0x01){
			if(len > (int)cigars[i].len){
				ret += cigars[i].len;
			} else {
				ret += len;
				break;
			}
			len -= cigars[i].len;
		} else {
			ret += cigars[i].len;
		}
	}
	return ret;
}

static inline int rev_select_cigars_seqlen(AlnCigar *cigars, int n_cigar, int len, int seq_idx){
	int i, ret;
	for(i=n_cigar-1,ret=0;i>=0;i--){
		if((cigars[i].type >> seq_idx) & 0x01){
			if(len > (int)cigars[i].len){
				ret += cigars[i].len;
			} else {
				ret += len;
				break;
			}
			len -= cigars[i].len;
		} else {
			ret += cigars[i].len;
		}
	}
	return ret;
}

static inline void flip_cigars(AlnCigar *cigars, int n_cigar){
	int i;
	for(i=0;i<n_cigar;i++){
		cigars[i].type = (cigars[i].type & 0x04) | ((cigars[i].type & 0x01) << 1) | ((cigars[i].type & 0x02) >> 1);
	}
}

static inline int sub_cigars(AlnCigar *dst, AlnCigar *cigars, int n_cigar, int off, int len){
	int i, x, y, n_sub;
	n_sub = 0;
	if(len < 0) len = 0x3FFFFFFF;
	else if(len == 0) return 0;
	for(i=0,x=0;i<n_cigar;i++){
		y = x + cigars[i].len;
		if(x < off){
			if(y < off){
			} else if(y < off + len){
				n_sub = _aln_cigar_add_cigar(dst, n_sub, y - off, cigars[i].type);
			} else {
				n_sub = _aln_cigar_add_cigar(dst, n_sub, off + len - x, cigars[i].type);
				break;
			}
		} else if(x >= off + len){
			break;
		} else {
			if(y < off + len){
				n_sub = _aln_cigar_add_cigar(dst, n_sub, cigars[i].len, cigars[i].type);
			} else {
				n_sub = _aln_cigar_add_cigar(dst, n_sub, off + len - x, cigars[i].type);
				break;
			}
		}
		x = y;
	}
	return n_sub;
}

static inline int sub_seq_cigars(AlnCigar *dst, AlnCigar *c, int n, int seq_idx, int off, int len){
	int d, i, x, y;
	if(len < 0) len = 0x7FFFFFFF;
	d = 0;
	x = y = 0;
	for(i=0;i<n;i++){
		if(x > off + len) break;
		if((c[i].type >> seq_idx) & 0x01){
			y = x + c[i].len;
		} else {
			y = x;
		}
		if(y < off) continue;
		if(x < off){
			if(y < off + len){
				d = _aln_cigar_add_cigar(dst, d, y - off, c[i].type);
			} else {
				d = _aln_cigar_add_cigar(dst, d, off + len - x, c[i].type);
				break;
			}
		} else if(y < off + len){
			d = _aln_cigar_add_cigar(dst, d, c[i].len, c[i].type);
		} else {
			d = _aln_cigar_add_cigar(dst, d, off + len - x, c[i].type);
			break;
		}
		x = y;
	}
	return d;
}

static inline int cat_cigars(AlnCigar *cigars1, int n_cigar1, AlnCigar *cigars2, int n_cigar2){
	int i;
	if(n_cigar2 == 0) return n_cigar1;
	if(n_cigar1){
		if(cigars1[n_cigar1-1].type == cigars2[0].type){
			n_cigar1 = _aln_cigar_add_cigar(cigars1, n_cigar1 - 1, cigars1[n_cigar1 - 1].len + cigars2[0].len, cigars2[0].type);
			i = 1;
		} else i = 0;
	} else i = 0;
	while(i < n_cigar2){
		cigars1[n_cigar1].type = cigars2[i].type;
		cigars1[n_cigar1].len = cigars2[i].len;
		n_cigar1 ++;
		i ++;
	}
	return n_cigar1;
}

static inline int append_cigars(AlnCigar *cs, int n, int type, int len){
	if(n && cs[n-1].type == type){
		return _aln_cigar_add_cigar(cs, n-1, cs[n-1].len + len, type);
	} else {
		return _aln_cigar_add_cigar(cs, n, len, type);
	}
}

static inline void reverse_cigars(AlnCigar *cs, int n){
	int i, j;
	AlnCigar c;
	i = 0;
	j = n - 1;
	while(i < j){
		c = cs[i]; cs[i] = cs[j]; cs[j] = c;
		i ++; j --;
	}
}

// seq1_size of c1 must be no less than c2
// seq1_size of dst will equal length of c1
static inline int compile_cigars(AlnCigar *dst, AlnCigar *c1, int n1, AlnCigar *c2, int n2, int seq_idx){
	int i, j, n3, x1, x2, x3, f1, f2, f3;
	n3 = 0;
	x2 = 0;
	x3 = 0;
	f2 = ALN_CIGAR_TYPE_MAT;
	f3 = ALN_CIGAR_TYPE_INS;
	for(i=j=0;i<n1;i++){
		x1 = c1[i].len;
		f1 = (c1[i].type >> seq_idx) & 0x01;
		while(x1){
			if(x2 == 0){
				if(j < n2){
					x2 = c2[j].len;
					f2 = (c2[j].type >> seq_idx) & 0x01;
					j ++;
				} else {
					x2 = x1;
					f2 = f1;
				}
			}
			if(f1){
				if(f2){
					if(x3 && f3 != ALN_CIGAR_TYPE_INS){ n3 = _aln_cigar_add_cigar(dst, n3, x3, f3); x3 = 0; }
					if(x1 < x2){ x2 -= x1; x3 += x1; x1 = 0; }
					else { x1 -= x2; x3 += x2; x2 = 0; }
					f3 = ALN_CIGAR_TYPE_INS;
				} else {
					if(x3 && f3 != ALN_CIGAR_TYPE_DEL){ n3 = _aln_cigar_add_cigar(dst, n3, x3, f3); x3 = 0; }
					f3 = ALN_CIGAR_TYPE_DEL;
					x3 += x2; x2 = 0;
				}
			} else {
				if(f2){
					if(x3 && f3 != ALN_CIGAR_TYPE_INS){ n3 = _aln_cigar_add_cigar(dst, n3, x3, f3); x3 = 0; }
					f3 = ALN_CIGAR_TYPE_INS;
					x3 += x1; x1 = 0;
				} else {
					if(x3 && f3 != ALN_CIGAR_TYPE_INS){ n3 = _aln_cigar_add_cigar(dst, n3, x3, f3); x3 = 0; }
					f3 = ALN_CIGAR_TYPE_INS;
					if(x1 < x2){ x2 -= x1; x3 += x1; x1 = 0; }
					else { x1 -= x2; x3 += x2; x2 = 0; }
				}
			}
		}
	}
	if(x3){ n3 = _aln_cigar_add_cigar(dst, n3, x3, f3); }
	return n3;
}

// c2 is the dst in align_cigars
static inline int apply_cigars(AlnCigar *dst, AlnCigar *c1, int n1, AlnCigar *c2, int n2){
	int i, j, n3, x1, x2, x3, f1, f2;
	n3 = 0;
	x2 = 0;
	f2 = ALN_CIGAR_TYPE_MAT;
	for(i=j=0;i<n1;i++){
		x1 = c1[i].len;
		f1 = c1[i].type;
		while(x1){
			if(x2 == 0){
				if(j < n2){
					x2 = c2[j].len;
					f2 = c2[j].type;
					j ++;
				} else {
					x2 = x1;
					f2 = ALN_CIGAR_TYPE_MAT;
				}
			}
			if(f2 == ALN_CIGAR_TYPE_DEL){
				n3 = _aln_cigar_add_cigar(dst, n3, x2, ALN_CIGAR_TYPE_DEL);
				x2 = 0;
			} else {
				x3 = (x1 < x2)? x1 : x2;
				n3 = _aln_cigar_add_cigar(dst, n3, x3, f1);
				x1 -= x3;
				x2 -= x3;
			}
		}
	}
	return n3;
}

static inline int refine_cigars(AlnCigar *c, int n){
	int i, j, x, f;
	x = 0;
	f = ALN_CIGAR_TYPE_NULL;
	for(i=j=0;i<n;i++){
		if(c[i].type == f){
			x += c[i].len;
		} else {
			if(x){
				j = _aln_cigar_add_cigar(c, j, x, f);
			}
			x = c[i].len;
			f = c[i].type;
		}
	}
	if(x){ j = _aln_cigar_add_cigar(c, j, x, f); }
	return j;
}

static inline int cigars_seq2aln(char *dst, AlnCigar *c, int n, int seq_idx, char *seq){
	int i, j, k, m;
	for(i=m=k=0;i<n;i++){
		if((c[i].type >> seq_idx) & 0x01){
			if(c[i].type & 0x04){
				k += c[i].len;
			} else {
				for(j=0;j<c[i].len;j++) dst[m++] = seq[k++];
			}
		} else if(!(c[i].type & 0x04)){
			for(j=0;j<c[i].len;j++) dst[m++] = '-';
		}
	}
	dst[m] = '\0';
	return m;
}

#endif