- // set S/W parameters
- par = ka_param_blast;
- par.gap_open = 4; par.gap_ext = 1; par.gap_end_open = par.gap_end_ext = 0;
- if (c->n_cigar > 1) { // set band width
- int sumi, sumd;
- sumi = sumd = 0;
- for (k = 0; k < c->n_cigar; ++k)
- if ((cigar[k]&0xf) == BAM_CINS) sumi += cigar[k]>>4;
- else if ((cigar[k]&0xf) == BAM_CDEL) sumd += cigar[k]>>4;
- par.band_width = (sumi > sumd? sumi : sumd) + MIN_BAND_WIDTH;
- } else par.band_width = MIN_BAND_WIDTH;
- // calculate the length of the reference in the alignment
- for (k = l_ref = 0; k < c->n_cigar; ++k) {
- if ((cigar[k]&0xf) == BAM_CREF_SKIP) break; // do not do realignment if there is an `N' operation
- if ((cigar[k]&0xf) == BAM_CMATCH || (cigar[k]&0xf) == BAM_CDEL)
- l_ref += cigar[k]>>4;
+ q[0] = q[1] = r[0] = r[1] = kk[0] = kk[1] = kl[0] = kl[1] = -1;
+ // find the right boundary
+ for (k = 0, score = max = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
+ int op = cigar[k]&0xf;
+ int ol = cigar[k]>>4;
+ if (op == BAM_CMATCH) {
+ for (j = 0; j < ol; ++j) {
+ int c1, c2, z = y + j;
+ c1 = bam_nt16_nt4_table[bam1_seqi(seq, z)];
+ if (ref[x+j] == 0) return -1;
+ c2 = bam_nt16_nt4_table[(int)bam_nt16_table[(int)ref[x+j]]];
+ if (c1 < 3 && c2 < 3)
+ score += c1 == c2? 5 : -4;
+ if (score < 0) score = 0;
+ if (score > max) max = score, q[1] = z, r[1] = x+j, kk[1] = k, kl[1] = j + 1;
+ }
+ x += ol; y += ol;
+ } else if (op == BAM_CINS) {
+ score -= 4 - ol * 3;
+ y += ol;
+ if (score < 0) score = 0;
+ } else if (op == BAM_CDEL) {
+ score -= 4 - ol * 3;
+ x += ol;
+ if (score < 0) score = 0;
+ } else if (op == BAM_CSOFT_CLIP) y += ol;
+ else if (op == BAM_CREF_SKIP) x += ol;
+ }
+ if (score < 0) return -1; // no high scoring segments
+ endx = x - 1;
+ // find the left boundary
+ for (k = c->n_cigar - 1, score = max = 0, x = x-1, y = y-1; k >= 0; --k) {
+ int op = cigar[k]&0xf;
+ int ol = cigar[k]>>4;
+ if (op == BAM_CMATCH) {
+ for (j = 0; j < ol; ++j) {
+ int c1, c2, z = y - j;
+ c1 = bam_nt16_nt4_table[bam1_seqi(seq, z)];
+ if (ref[x+j] == 0) return -1;
+ c2 = bam_nt16_nt4_table[(int)bam_nt16_table[(int)ref[x-j]]];
+ if (c1 < 3 && c2 < 3)
+ score += c1 == c2? 5 : -4;
+ if (score < 0) score = 0;
+ if (score > max) max = score, q[0] = z, r[0] = x-j, kk[0] = k, kl[0] = j + 1;
+ }
+ x -= ol; y -= ol;
+ } else if (op == BAM_CINS) {
+ score -= 4 - ol * 3;
+ y -= ol;
+ if (score < 0) score = 0;
+ } else if (op == BAM_CDEL) {
+ score -= 4 - ol * 3;
+ x -= ol;
+ if (score < 0) score = 0;
+ } else if (op == BAM_CSOFT_CLIP) y -= ol;
+ else if (op == BAM_CREF_SKIP) x -= ol;