1 | #include <stdio.h> |
---|
2 | #include <ctype.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | #include "clustalw.h" |
---|
6 | |
---|
7 | |
---|
8 | /* |
---|
9 | * Prototypes |
---|
10 | */ |
---|
11 | void calc_p_penalties(char **aln, sint n, sint fs, sint ls, sint *weight); |
---|
12 | void calc_h_penalties(char **aln, sint n, sint fs, sint ls, sint *weight); |
---|
13 | sint local_penalty(sint penalty, sint n, sint *pweight, sint *hweight); |
---|
14 | /* |
---|
15 | * Global variables |
---|
16 | */ |
---|
17 | |
---|
18 | extern sint gap_dist; |
---|
19 | extern sint max_aa; |
---|
20 | extern sint debug; |
---|
21 | extern Boolean dnaflag; |
---|
22 | extern Boolean use_endgaps; |
---|
23 | extern Boolean no_hyd_penalties, no_pref_penalties; |
---|
24 | extern char hyd_residues[]; |
---|
25 | extern char *amino_acid_codes; |
---|
26 | |
---|
27 | char pr[] = {'A' , 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'I', 'L', |
---|
28 | 'M' , 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'Y', 'W'}; |
---|
29 | sint pas_op[] = { 87, 87,104, 69, 80,139,100,104, 68, 79, |
---|
30 | 71,137,126, 93,128,124,111, 75,100, 77}; |
---|
31 | sint pas_op2[] ={ 88, 57,111, 98, 75,126, 95, 97, 70, 90, |
---|
32 | 60,122,110,107, 91,125,124, 81,106, 88}; |
---|
33 | sint pal_op[] = { 84, 69,128, 78, 88,176, 53, 95, 55, 49, |
---|
34 | 52,148,147,100, 91,129,105, 51,128, 88}; |
---|
35 | |
---|
36 | float reduced_gap = 0.3; |
---|
37 | Boolean nhyd_pen,npref_pen; /* local copies of ho_hyd_penalties, no_pref_penalties */ |
---|
38 | sint gdist; /* local copy of gap_dist */ |
---|
39 | |
---|
40 | void calc_gap_coeff(char **alignment, sint *gaps, sint **profile, Boolean struct_penalties, |
---|
41 | char *gap_penalty_mask, sint first_seq, sint last_seq, |
---|
42 | sint prf_length, sint gapcoef, sint lencoef) |
---|
43 | { |
---|
44 | |
---|
45 | char c; |
---|
46 | sint i, j; |
---|
47 | sint is, ie; |
---|
48 | static sint numseq,val; |
---|
49 | static sint *gap_pos; |
---|
50 | static sint *p_weight, *h_weight; |
---|
51 | static float scale; |
---|
52 | |
---|
53 | numseq = last_seq - first_seq; |
---|
54 | |
---|
55 | for (j=0; j<prf_length; j++) |
---|
56 | gaps[j] = 0; |
---|
57 | /* |
---|
58 | Check for a gap penalty mask |
---|
59 | */ |
---|
60 | if (struct_penalties != NONE) |
---|
61 | { |
---|
62 | nhyd_pen = npref_pen = TRUE; |
---|
63 | gdist = 0; |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | nhyd_pen = no_hyd_penalties; |
---|
68 | npref_pen = no_pref_penalties; |
---|
69 | gdist = gap_dist; |
---|
70 | } |
---|
71 | |
---|
72 | for (i=first_seq; i<last_seq; i++) |
---|
73 | { |
---|
74 | /* |
---|
75 | Include end gaps as gaps ? |
---|
76 | */ |
---|
77 | is = 0; |
---|
78 | ie = prf_length; |
---|
79 | if (use_endgaps == FALSE) |
---|
80 | { |
---|
81 | for (j=0; j<prf_length; j++) |
---|
82 | { |
---|
83 | c = alignment[i][j]; |
---|
84 | if ((c < 0) || (c > max_aa)) |
---|
85 | is++; |
---|
86 | else |
---|
87 | break; |
---|
88 | } |
---|
89 | for (j=prf_length-1; j>=0; j--) |
---|
90 | { |
---|
91 | c = alignment[i][j]; |
---|
92 | if ((c < 0) || (c > max_aa)) |
---|
93 | ie--; |
---|
94 | else |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | for (j=is; j<ie; j++) |
---|
100 | { |
---|
101 | if ((alignment[i][j] < 0) || (alignment[i][j] > max_aa)) |
---|
102 | gaps[j]++; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if ((!dnaflag) && (npref_pen == FALSE)) |
---|
107 | { |
---|
108 | p_weight = (sint *) ckalloc( (prf_length+2) * sizeof (sint) ); |
---|
109 | calc_p_penalties(alignment, prf_length, first_seq, last_seq, p_weight); |
---|
110 | } |
---|
111 | |
---|
112 | if ((!dnaflag) && (nhyd_pen == FALSE)) |
---|
113 | { |
---|
114 | h_weight = (sint *) ckalloc( (prf_length+2) * sizeof (sint) ); |
---|
115 | calc_h_penalties(alignment, prf_length, first_seq, last_seq, h_weight); |
---|
116 | } |
---|
117 | |
---|
118 | gap_pos = (sint *) ckalloc( (prf_length+2) * sizeof (sint) ); |
---|
119 | /* |
---|
120 | mark the residues close to an existing gap (set gaps[i] = -ve) |
---|
121 | */ |
---|
122 | if (dnaflag || (gdist <= 0)) |
---|
123 | { |
---|
124 | for (i=0;i<prf_length;i++) gap_pos[i] = gaps[i]; |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | i=0; |
---|
129 | while (i<prf_length) |
---|
130 | { |
---|
131 | if (gaps[i] <= 0) |
---|
132 | { |
---|
133 | gap_pos[i] = gaps[i]; |
---|
134 | i++; |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | for (j = -gdist+1; j<0; j++) |
---|
139 | { |
---|
140 | if ((i+j>=0) && (i+j<prf_length) && |
---|
141 | ((gaps[i+j] == 0) || (gaps[i+j] < j))) gap_pos[i+j] = j; |
---|
142 | } |
---|
143 | while (gaps[i] > 0) |
---|
144 | { |
---|
145 | if (i>=prf_length) break; |
---|
146 | gap_pos[i] = gaps[i]; |
---|
147 | i++; |
---|
148 | } |
---|
149 | for (j = 0; j<gdist; j++) |
---|
150 | { |
---|
151 | if (gaps[i+j] > 0) break; |
---|
152 | if ((i+j>=0) && (i+j<prf_length) && |
---|
153 | ((gaps[i+j] == 0) || (gaps[i+j] < -j))) gap_pos[i+j] = -j-1; |
---|
154 | } |
---|
155 | i += j; |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | if (debug>1) |
---|
161 | { |
---|
162 | fprintf(stdout,"gap open %d gap ext %d\n",(pint)gapcoef,(pint)lencoef); |
---|
163 | fprintf(stdout,"gaps:\n"); |
---|
164 | for(i=0;i<prf_length;i++) fprintf(stdout,"%d ", (pint)gaps[i]); |
---|
165 | fprintf(stdout,"\n"); |
---|
166 | fprintf(stdout,"gap_pos:\n"); |
---|
167 | for(i=0;i<prf_length;i++) fprintf(stdout,"%d ", (pint)gap_pos[i]); |
---|
168 | fprintf(stdout,"\n"); |
---|
169 | } |
---|
170 | |
---|
171 | for (j=0;j<prf_length; j++) |
---|
172 | { |
---|
173 | |
---|
174 | if (gap_pos[j] <= 0) |
---|
175 | { |
---|
176 | /* |
---|
177 | apply residue-specific and hydrophilic gap penalties. |
---|
178 | */ |
---|
179 | if (!dnaflag) { |
---|
180 | profile[j+1][GAPCOL] = local_penalty(gapcoef, j, |
---|
181 | p_weight, h_weight); |
---|
182 | profile[j+1][LENCOL] = lencoef; |
---|
183 | } |
---|
184 | else { |
---|
185 | profile[j+1][GAPCOL] = gapcoef; |
---|
186 | profile[j+1][LENCOL] = lencoef; |
---|
187 | } |
---|
188 | |
---|
189 | /* |
---|
190 | increase gap penalty near to existing gaps. |
---|
191 | */ |
---|
192 | if (gap_pos[j] < 0) |
---|
193 | { |
---|
194 | profile[j+1][GAPCOL] *= 2.0+2.0*(gdist+gap_pos[j])/gdist; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | scale = ((float)(numseq-gaps[j])/(float)numseq) * reduced_gap; |
---|
202 | profile[j+1][GAPCOL] = scale*gapcoef; |
---|
203 | profile[j+1][LENCOL] = 0.5 * lencoef; |
---|
204 | } |
---|
205 | /* |
---|
206 | apply the gap penalty mask |
---|
207 | */ |
---|
208 | if (struct_penalties != NONE) |
---|
209 | { |
---|
210 | val = gap_penalty_mask[j]-'0'; |
---|
211 | if (val > 0 && val < 10) |
---|
212 | { |
---|
213 | profile[j+1][GAPCOL] *= val; |
---|
214 | profile[j+1][LENCOL] *= val; |
---|
215 | } |
---|
216 | } |
---|
217 | /* |
---|
218 | make sure no penalty is zero - even for all-gap positions |
---|
219 | */ |
---|
220 | if (profile[j+1][GAPCOL] <= 0) profile[j+1][GAPCOL] = 1; |
---|
221 | if (profile[j+1][LENCOL] <= 0) profile[j+1][LENCOL] = 1; |
---|
222 | } |
---|
223 | |
---|
224 | profile[0][GAPCOL] = 0; |
---|
225 | profile[0][LENCOL] = 0; |
---|
226 | |
---|
227 | profile[prf_length][GAPCOL] = 0; |
---|
228 | profile[prf_length][LENCOL] = 0; |
---|
229 | |
---|
230 | if (debug>1) |
---|
231 | { |
---|
232 | fprintf(stdout,"Opening penalties:\n"); |
---|
233 | for(i=0;i<prf_length;i++) fprintf(stdout," %d:%d ",i, (pint)profile[i+1][GAPCOL]); |
---|
234 | fprintf(stdout,"\n"); |
---|
235 | } |
---|
236 | if (debug>1) |
---|
237 | { |
---|
238 | fprintf(stdout,"Extension penalties:\n"); |
---|
239 | for(i=0;i<prf_length;i++) fprintf(stdout,"%d:%d ",i, (pint)profile[i+1][LENCOL]); |
---|
240 | fprintf(stdout,"\n"); |
---|
241 | } |
---|
242 | if ((!dnaflag) && (npref_pen == FALSE)) |
---|
243 | p_weight=ckfree((void *)p_weight); |
---|
244 | |
---|
245 | if ((!dnaflag) && (nhyd_pen == FALSE)) |
---|
246 | h_weight=ckfree((void *)h_weight); |
---|
247 | |
---|
248 | |
---|
249 | gap_pos=ckfree((void *)gap_pos); |
---|
250 | } |
---|
251 | |
---|
252 | void calc_p_penalties(char **aln, sint n, sint fs, sint ls, sint *weight) |
---|
253 | { |
---|
254 | char ix; |
---|
255 | sint j,k,numseq; |
---|
256 | sint i; |
---|
257 | |
---|
258 | numseq = ls - fs; |
---|
259 | for (i=0;i<n;i++) |
---|
260 | { |
---|
261 | weight[i] = 0; |
---|
262 | for (k=fs;k<ls;k++) |
---|
263 | { |
---|
264 | for (j=0;j<22;j++) |
---|
265 | { |
---|
266 | ix = aln[k][i]; |
---|
267 | if ((ix < 0) || (ix > max_aa)) continue; |
---|
268 | if (amino_acid_codes[ix] == pr[j]) |
---|
269 | { |
---|
270 | weight[i] += (200-pas_op[j]); |
---|
271 | break; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | weight[i] /= numseq; |
---|
276 | } |
---|
277 | |
---|
278 | } |
---|
279 | |
---|
280 | void calc_h_penalties(char **aln, sint n, sint fs, sint ls, sint *weight) |
---|
281 | { |
---|
282 | |
---|
283 | /* |
---|
284 | weight[] is the length of the hydrophilic run of residues. |
---|
285 | */ |
---|
286 | char ix; |
---|
287 | sint nh,j,k; |
---|
288 | sint i,e,s; |
---|
289 | sint *hyd; |
---|
290 | float scale; |
---|
291 | |
---|
292 | hyd = (sint *)ckalloc((n+2) * sizeof(sint)); |
---|
293 | nh = (sint)strlen(hyd_residues); |
---|
294 | for (i=0;i<n;i++) |
---|
295 | weight[i] = 0; |
---|
296 | |
---|
297 | for (k=fs;k<ls;k++) |
---|
298 | { |
---|
299 | for (i=0;i<n;i++) |
---|
300 | { |
---|
301 | hyd[i] = 0; |
---|
302 | for (j=0;j<nh;j++) |
---|
303 | { |
---|
304 | ix = aln[k][i]; |
---|
305 | if ((ix < 0) || (ix > max_aa)) continue; |
---|
306 | if (amino_acid_codes[ix] == hyd_residues[j]) |
---|
307 | { |
---|
308 | hyd[i] = 1; |
---|
309 | break; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | i = 0; |
---|
314 | while (i < n) |
---|
315 | { |
---|
316 | if (hyd[i] == 0) i++; |
---|
317 | else |
---|
318 | { |
---|
319 | s = i; |
---|
320 | while ((hyd[i] != 0) && (i<n)) i++; |
---|
321 | e = i; |
---|
322 | if (e-s > 3) |
---|
323 | for (j=s; j<e; j++) weight[j] += 100; |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | scale = ls - fs; |
---|
329 | for (i=0;i<n;i++) |
---|
330 | weight[i] /= scale; |
---|
331 | |
---|
332 | hyd=ckfree((void *)hyd); |
---|
333 | |
---|
334 | if (debug>1) |
---|
335 | { |
---|
336 | for(i=0;i<n;i++) fprintf(stdout,"%d ", (pint)weight[i]); |
---|
337 | fprintf(stdout,"\n"); |
---|
338 | } |
---|
339 | |
---|
340 | } |
---|
341 | |
---|
342 | sint local_penalty(sint penalty, sint n, sint *pweight, sint *hweight) |
---|
343 | { |
---|
344 | |
---|
345 | Boolean h = FALSE; |
---|
346 | float gw; |
---|
347 | |
---|
348 | if (dnaflag) return(1); |
---|
349 | |
---|
350 | gw = 1.0; |
---|
351 | if (nhyd_pen == FALSE) |
---|
352 | { |
---|
353 | if (hweight[n] > 0) |
---|
354 | { |
---|
355 | gw *= reduced_gap; |
---|
356 | h = TRUE; |
---|
357 | } |
---|
358 | } |
---|
359 | if ((npref_pen == FALSE) && (h==FALSE)) |
---|
360 | { |
---|
361 | gw *= ((float)pweight[n]/100.0); |
---|
362 | } |
---|
363 | |
---|
364 | gw *= penalty; |
---|
365 | return((sint)gw); |
---|
366 | |
---|
367 | } |
---|
368 | |
---|