annotate PsiCLASS-1.0.2/samtools-0.1.19/kaln.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /* The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 Copyright (c) 2003-2006, 2008, 2009, by Heng Li <lh3lh3@gmail.com>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 a copy of this software and associated documentation files (the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 #include <stdint.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 #include <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 #include "kaln.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 #define FROM_M 0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 #define FROM_I 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 #define FROM_D 2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 int i, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 unsigned char ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 } path_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 int aln_sm_blosum62[] = {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 /* A R N D C Q E G H I L K M F P S T W Y V * X */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 4,-1,-2,-2, 0,-1,-1, 0,-2,-1,-1,-1,-1,-2,-1, 1, 0,-3,-2, 0,-4, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 -1, 5, 0,-2,-3, 1, 0,-2, 0,-3,-2, 2,-1,-3,-2,-1,-1,-3,-2,-3,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 -2, 0, 6, 1,-3, 0, 0, 0, 1,-3,-3, 0,-2,-3,-2, 1, 0,-4,-2,-3,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 -2,-2, 1, 6,-3, 0, 2,-1,-1,-3,-4,-1,-3,-3,-1, 0,-1,-4,-3,-3,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 0,-3,-3,-3, 9,-3,-4,-3,-3,-1,-1,-3,-1,-2,-3,-1,-1,-2,-2,-1,-4,-2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 -1, 1, 0, 0,-3, 5, 2,-2, 0,-3,-2, 1, 0,-3,-1, 0,-1,-2,-1,-2,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 -1, 0, 0, 2,-4, 2, 5,-2, 0,-3,-3, 1,-2,-3,-1, 0,-1,-3,-2,-2,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 0,-2, 0,-1,-3,-2,-2, 6,-2,-4,-4,-2,-3,-3,-2, 0,-2,-2,-3,-3,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 -2, 0, 1,-1,-3, 0, 0,-2, 8,-3,-3,-1,-2,-1,-2,-1,-2,-2, 2,-3,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 -1,-3,-3,-3,-1,-3,-3,-4,-3, 4, 2,-3, 1, 0,-3,-2,-1,-3,-1, 3,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 -1,-2,-3,-4,-1,-2,-3,-4,-3, 2, 4,-2, 2, 0,-3,-2,-1,-2,-1, 1,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 -1, 2, 0,-1,-3, 1, 1,-2,-1,-3,-2, 5,-1,-3,-1, 0,-1,-3,-2,-2,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 -1,-1,-2,-3,-1, 0,-2,-3,-2, 1, 2,-1, 5, 0,-2,-1,-1,-1,-1, 1,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 -2,-3,-3,-3,-2,-3,-3,-3,-1, 0, 0,-3, 0, 6,-4,-2,-2, 1, 3,-1,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 -1,-2,-2,-1,-3,-1,-1,-2,-2,-3,-3,-1,-2,-4, 7,-1,-1,-4,-3,-2,-4,-2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 1,-1, 1, 0,-1, 0, 0, 0,-1,-2,-2, 0,-1,-2,-1, 4, 1,-3,-2,-2,-4, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 1, 5,-2,-2, 0,-4, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 -3,-3,-4,-4,-2,-2,-3,-2,-2,-3,-2,-3,-1, 1,-4,-3,-2,11, 2,-3,-4,-2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 -2,-2,-2,-3,-2,-1,-2,-3, 2,-1,-1,-2,-1, 3,-3,-2,-2, 2, 7,-1,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 0,-3,-3,-3,-1,-2,-2,-3,-3, 3, 1,-2, 1,-1,-2,-2, 0,-3,-1, 4,-4,-1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, 1,-4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 0,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2, 0, 0,-2,-1,-1,-4,-1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 int aln_sm_blast[] = {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 1, -3, -3, -3, -2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 -3, 1, -3, -3, -2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 -3, -3, 1, -3, -2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 -3, -3, -3, 1, -2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 -2, -2, -2, -2, -2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 int aln_sm_qual[] = {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 0, -23, -23, -23, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 -23, 0, -23, -23, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 -23, -23, 0, -23, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 -23, -23, -23, 0, 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 0, 0, 0, 0, 0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 ka_param_t ka_param_blast = { 5, 2, 5, 2, aln_sm_blast, 5, 50 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 ka_param_t ka_param_aa2aa = { 10, 2, 10, 2, aln_sm_blosum62, 22, 50 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 ka_param2_t ka_param2_qual = { 37, 11, 37, 11, 37, 11, 0, 0, aln_sm_qual, 5, 50 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 static uint32_t *ka_path2cigar32(const path_t *path, int path_len, int *n_cigar)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 int i, n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 uint32_t *cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 unsigned char last_type;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 if (path_len == 0 || path == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 *n_cigar = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 last_type = path->ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 for (i = n = 1; i < path_len; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 if (last_type != path[i].ctype) ++n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 last_type = path[i].ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 *n_cigar = n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 cigar = (uint32_t*)calloc(*n_cigar, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 cigar[0] = 1u << 4 | path[path_len-1].ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 last_type = path[path_len-1].ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 for (i = path_len - 2, n = 0; i >= 0; --i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 if (path[i].ctype == last_type) cigar[n] += 1u << 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 cigar[++n] = 1u << 4 | path[i].ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 last_type = path[i].ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 return cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 /***************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 /* START OF common_align.c */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 /***************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 #define SET_INF(s) (s).M = (s).I = (s).D = MINOR_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 #define set_M(MM, cur, p, sc) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 if ((p)->M >= (p)->I) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 if ((p)->M >= (p)->D) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 (MM) = (p)->M + (sc); (cur)->Mt = FROM_M; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 (MM) = (p)->D + (sc); (cur)->Mt = FROM_D; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 if ((p)->I > (p)->D) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 (MM) = (p)->I + (sc); (cur)->Mt = FROM_I; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 (MM) = (p)->D + (sc); (cur)->Mt = FROM_D; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 #define set_I(II, cur, p) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 if ((p)->M - gap_open > (p)->I) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 (cur)->It = FROM_M; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 (II) = (p)->M - gap_open - gap_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 (cur)->It = FROM_I; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 (II) = (p)->I - gap_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 #define set_end_I(II, cur, p) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 if (gap_end_ext >= 0) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 if ((p)->M - gap_end_open > (p)->I) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 (cur)->It = FROM_M; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 (II) = (p)->M - gap_end_open - gap_end_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 (cur)->It = FROM_I; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 (II) = (p)->I - gap_end_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 } else set_I(II, cur, p); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 #define set_D(DD, cur, p) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 if ((p)->M - gap_open > (p)->D) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 (cur)->Dt = FROM_M; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 (DD) = (p)->M - gap_open - gap_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 (cur)->Dt = FROM_D; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 (DD) = (p)->D - gap_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 #define set_end_D(DD, cur, p) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 if (gap_end_ext >= 0) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if ((p)->M - gap_end_open > (p)->D) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 (cur)->Dt = FROM_M; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 (DD) = (p)->M - gap_end_open - gap_end_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 (cur)->Dt = FROM_D; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 (DD) = (p)->D - gap_end_ext; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 } else set_D(DD, cur, p); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 uint8_t Mt:3, It:2, Dt:3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 } dpcell_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 int M, I, D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 } dpscore_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 /***************************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 * banded global alignment *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 ***************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 uint32_t *ka_global_core(uint8_t *seq1, int len1, uint8_t *seq2, int len2, const ka_param_t *ap, int *_score, int *n_cigar)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 int i, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 dpcell_t **dpcell, *q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 dpscore_t *curr, *last, *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 int b1, b2, tmp_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 int *mat, end, max = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 uint8_t type, ctype;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 uint32_t *cigar = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 int gap_open, gap_ext, gap_end_open, gap_end_ext, b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 int *score_matrix, N_MATRIX_ROW;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 /* initialize some align-related parameters. just for compatibility */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 gap_open = ap->gap_open;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 gap_ext = ap->gap_ext;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 gap_end_open = ap->gap_end_open;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 gap_end_ext = ap->gap_end_ext;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 b = ap->band_width;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 score_matrix = ap->matrix;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 N_MATRIX_ROW = ap->row;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 if (n_cigar) *n_cigar = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 if (len1 == 0 || len2 == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 /* calculate b1 and b2 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 if (len1 > len2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 b1 = len1 - len2 + b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 b2 = b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 b1 = b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 b2 = len2 - len1 + b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 if (b1 > len1) b1 = len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 if (b2 > len2) b2 = len2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 --seq1; --seq2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 /* allocate memory */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 end = (b1 + b2 <= len1)? (b1 + b2 + 1) : (len1 + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 dpcell = (dpcell_t**)malloc(sizeof(dpcell_t*) * (len2 + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 for (j = 0; j <= len2; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 dpcell[j] = (dpcell_t*)malloc(sizeof(dpcell_t) * end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 for (j = b2 + 1; j <= len2; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 dpcell[j] -= j - b2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 curr = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 last = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 /* set first row */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 SET_INF(*curr); curr->M = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 for (i = 1, s = curr + 1; i < b1; ++i, ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 SET_INF(*s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 set_end_D(s->D, dpcell[0] + i, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 s = curr; curr = last; last = s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 /* core dynamic programming, part 1 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 tmp_end = (b2 < len2)? b2 : len2 - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 for (j = 1; j <= tmp_end; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 q = dpcell[j]; s = curr; SET_INF(*s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 set_end_I(s->I, q, last);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 mat = score_matrix + seq2[j] * N_MATRIX_ROW;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 ++s; ++q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 for (i = 1; i != end; ++i, ++s, ++q) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 set_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 set_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 set_M(s->M, q, last + i - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 set_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 if (j + b1 - 1 > len1) { /* bug fixed, 040227 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 set_end_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 } else s->I = MINOR_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 s = curr; curr = last; last = s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 /* last row for part 1, use set_end_D() instead of set_D() */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 if (j == len2 && b2 != len2 - 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 q = dpcell[j]; s = curr; SET_INF(*s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 set_end_I(s->I, q, last);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 mat = score_matrix + seq2[j] * N_MATRIX_ROW;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 ++s; ++q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 for (i = 1; i != end; ++i, ++s, ++q) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 set_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 set_end_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 set_M(s->M, q, last + i - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 set_end_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 if (j + b1 - 1 > len1) { /* bug fixed, 040227 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 set_end_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 } else s->I = MINOR_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 s = curr; curr = last; last = s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 ++j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 /* core dynamic programming, part 2 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 for (; j <= len2 - b2 + 1; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 SET_INF(curr[j - b2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 mat = score_matrix + seq2[j] * N_MATRIX_ROW;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 end = j + b1 - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i != end; ++i, ++s, ++q) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 set_M(s->M, q, last + i - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 set_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 set_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 set_M(s->M, q, last + i - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 set_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 s->I = MINOR_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 s = curr; curr = last; last = s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 /* core dynamic programming, part 3 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 for (; j < len2; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 SET_INF(curr[j - b2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 mat = score_matrix + seq2[j] * N_MATRIX_ROW;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 set_M(s->M, q, last + i - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 set_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 set_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 set_M(s->M, q, last + len1 - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 set_end_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 set_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 s = curr; curr = last; last = s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 /* last row */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 if (j == len2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 SET_INF(curr[j - b2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 mat = score_matrix + seq2[j] * N_MATRIX_ROW;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 set_M(s->M, q, last + i - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 set_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 set_end_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 set_M(s->M, q, last + len1 - 1, mat[seq1[i]]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 set_end_I(s->I, q, last + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 set_end_D(s->D, q, s - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 s = curr; curr = last; last = s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 *_score = last[len1].M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 if (n_cigar) { /* backtrace */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 path_t *p, *path = (path_t*)malloc(sizeof(path_t) * (len1 + len2 + 2));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 i = len1; j = len2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 q = dpcell[j] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 s = last + len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 max = s->M; type = q->Mt; ctype = FROM_M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 if (s->I > max) { max = s->I; type = q->It; ctype = FROM_I; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 if (s->D > max) { max = s->D; type = q->Dt; ctype = FROM_D; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 p = path;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 p->ctype = ctype; p->i = i; p->j = j; /* bug fixed 040408 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 do {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 switch (ctype) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 case FROM_M: --i; --j; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 case FROM_I: --j; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 case FROM_D: --i; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 q = dpcell[j] + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 ctype = type;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 switch (type) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 case FROM_M: type = q->Mt; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 case FROM_I: type = q->It; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 case FROM_D: type = q->Dt; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 p->ctype = ctype; p->i = i; p->j = j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 } while (i || j);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 cigar = ka_path2cigar32(path, p - path - 1, n_cigar);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 free(path);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 /* free memory */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 for (j = b2 + 1; j <= len2; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 dpcell[j] += j - b2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 for (j = 0; j <= len2; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 free(dpcell[j]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 free(dpcell);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 free(curr); free(last);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 return cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 int M, I, D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 } score_aux_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 #define MINUS_INF -0x40000000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 // matrix: len2 rows and len1 columns
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 int ka_global_score(const uint8_t *_seq1, int len1, const uint8_t *_seq2, int len2, const ka_param2_t *ap)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 #define __score_aux(_p, _q0, _sc, _io, _ie, _do, _de) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 int t1, t2; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 score_aux_t *_q; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 _q = _q0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 _p->M = _q->M >= _q->I? _q->M : _q->I; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 _p->M = _p->M >= _q->D? _p->M : _q->D; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 _p->M += (_sc); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 ++_q; t1 = _q->M - _io - _ie; t2 = _q->I - _ie; _p->I = t1 >= t2? t1 : t2; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 _q = _p-1; t1 = _q->M - _do - _de; t2 = _q->D - _de; _p->D = t1 >= t2? t1 : t2; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 int i, j, bw, scmat_size = ap->row, *scmat = ap->matrix, ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 const uint8_t *seq1, *seq2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 score_aux_t *curr, *last, *swap;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 bw = abs(len1 - len2) + ap->band_width;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 i = len1 > len2? len1 : len2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 if (bw > i + 1) bw = i + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 seq1 = _seq1 - 1; seq2 = _seq2 - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 curr = calloc(len1 + 2, sizeof(score_aux_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 last = calloc(len1 + 2, sizeof(score_aux_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 { // the zero-th row
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 int x, end = len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 score_aux_t *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 j = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 x = j + bw; end = len1 < x? len1 : x; // band end
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 p = curr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 p->M = 0; p->I = p->D = MINUS_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 for (i = 1, p = &curr[1]; i <= end; ++i, ++p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 p->M = p->I = MINUS_INF, p->D = -(ap->edo + ap->ede * i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 p->M = p->I = p->D = MINUS_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 swap = curr; curr = last; last = swap;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 for (j = 1; j < len2; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 int x, beg = 0, end = len1, *scrow, col_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 score_aux_t *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 x = j - bw; beg = 0 > x? 0 : x; // band start
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 x = j + bw; end = len1 < x? len1 : x; // band end
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 if (beg == 0) { // from zero-th column
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 p = curr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 p->M = p->D = MINUS_INF; p->I = -(ap->eio + ap->eie * j);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 ++beg; // then beg = 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 scrow = scmat + seq2[j] * scmat_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 if (end == len1) col_end = 1, --end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 else col_end = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 for (i = beg, p = &curr[beg]; i <= end; ++i, ++p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->iio, ap->iie, ap->ido, ap->ide);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 if (col_end) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->eio, ap->eie, ap->ido, ap->ide);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 p->M = p->I = p->D = MINUS_INF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 // for (i = 0; i <= len1; ++i) printf("(%d,%d,%d) ", curr[i].M, curr[i].I, curr[i].D); putchar('\n');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 swap = curr; curr = last; last = swap;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 { // the last row
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 int x, beg = 0, *scrow;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 score_aux_t *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 j = len2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 x = j - bw; beg = 0 > x? 0 : x; // band start
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 if (beg == 0) { // from zero-th column
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 p = curr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 p->M = p->D = MINUS_INF; p->I = -(ap->eio + ap->eie * j);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 ++beg; // then beg = 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 scrow = scmat + seq2[j] * scmat_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 for (i = beg, p = &curr[beg]; i < len1; ++i, ++p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->iio, ap->iie, ap->edo, ap->ede);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 __score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->eio, ap->eie, ap->edo, ap->ede);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 // for (i = 0; i <= len1; ++i) printf("(%d,%d,%d) ", curr[i].M, curr[i].I, curr[i].D); putchar('\n');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 ret = curr[len1].M >= curr[len1].I? curr[len1].M : curr[len1].I;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 ret = ret >= curr[len1].D? ret : curr[len1].D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 free(curr); free(last);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 #ifdef _MAIN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 int main(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 // int len1 = 35, len2 = 35;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 // uint8_t *seq1 = (uint8_t*)"\0\0\3\3\2\0\0\0\1\0\2\1\2\1\3\2\3\3\3\0\2\3\2\1\1\3\3\3\2\3\3\1\0\0\1";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 // uint8_t *seq2 = (uint8_t*)"\0\0\3\3\2\0\0\0\1\0\2\1\2\1\3\2\3\3\3\0\2\3\2\1\1\3\3\3\2\3\3\1\0\1\0";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 int len1 = 4, len2 = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 uint8_t *seq1 = (uint8_t*)"\1\0\0\1";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 uint8_t *seq2 = (uint8_t*)"\1\0\1\0";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 int sc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 // ka_global_core(seq1, 2, seq2, 1, &ka_param_qual, &sc, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 sc = ka_global_score(seq1, len1, seq2, len2, &ka_param2_qual);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 printf("%d\n", sc);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 #endif