1 | #include <stdio.h> |
---|
2 | #include <math.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | #include <ctype.h> |
---|
6 | #include "clustalw.h" |
---|
7 | #include "matrices.h" |
---|
8 | |
---|
9 | |
---|
10 | /* |
---|
11 | * Prototypes |
---|
12 | */ |
---|
13 | |
---|
14 | |
---|
15 | /* |
---|
16 | * Global variables |
---|
17 | */ |
---|
18 | |
---|
19 | extern char *amino_acid_codes; |
---|
20 | extern sint gap_pos1, gap_pos2; |
---|
21 | extern sint max_aa; |
---|
22 | extern short def_dna_xref[],def_aa_xref[]; |
---|
23 | extern sint mat_avscore; |
---|
24 | extern sint debug; |
---|
25 | extern Boolean dnaflag; |
---|
26 | |
---|
27 | void init_matrix(void) |
---|
28 | { |
---|
29 | |
---|
30 | char c1,c2; |
---|
31 | short i, j, maxres; |
---|
32 | |
---|
33 | max_aa = strlen(amino_acid_codes)-2; |
---|
34 | gap_pos1 = NUMRES-2; /* code for gaps inserted by clustalw */ |
---|
35 | gap_pos2 = NUMRES-1; /* code for gaps already in alignment */ |
---|
36 | |
---|
37 | /* |
---|
38 | set up cross-reference for default matrices hard-coded in matrices.h |
---|
39 | */ |
---|
40 | for (i=0;i<NUMRES;i++) def_aa_xref[i] = -1; |
---|
41 | for (i=0;i<NUMRES;i++) def_dna_xref[i] = -1; |
---|
42 | |
---|
43 | maxres = 0; |
---|
44 | for (i=0;(c1=amino_acid_order[i]);i++) |
---|
45 | { |
---|
46 | for (j=0;(c2=amino_acid_codes[j]);j++) |
---|
47 | { |
---|
48 | if (c1 == c2) |
---|
49 | { |
---|
50 | def_aa_xref[i] = j; |
---|
51 | maxres++; |
---|
52 | break; |
---|
53 | } |
---|
54 | } |
---|
55 | if ((def_aa_xref[i] == -1) && (amino_acid_order[i] != '*')) |
---|
56 | { |
---|
57 | error("residue %c in matrices.h is not recognised", |
---|
58 | amino_acid_order[i]); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | maxres = 0; |
---|
63 | for (i=0;(c1=nucleic_acid_order[i]);i++) |
---|
64 | { |
---|
65 | for (j=0;(c2=amino_acid_codes[j]);j++) |
---|
66 | { |
---|
67 | if (c1 == c2) |
---|
68 | { |
---|
69 | def_dna_xref[i] = j; |
---|
70 | maxres++; |
---|
71 | break; |
---|
72 | } |
---|
73 | } |
---|
74 | if ((def_dna_xref[i] == -1) && (nucleic_acid_order[i] != '*')) |
---|
75 | { |
---|
76 | error("nucleic acid %c in matrices.h is not recognised", |
---|
77 | nucleic_acid_order[i]); |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | sint get_matrix(short *matptr, short *xref, sint matrix[NUMRES][NUMRES], Boolean neg_flag, sint scale) |
---|
83 | { |
---|
84 | sint gg_score = 1; |
---|
85 | sint gr_score = 0; |
---|
86 | sint i, j, k, ix = 0; |
---|
87 | sint ti, tj; |
---|
88 | sint maxres; |
---|
89 | sint av1,av2,av3,min, max; |
---|
90 | /* |
---|
91 | default - set all scores to 0 |
---|
92 | */ |
---|
93 | for (i=0;i<=max_aa;i++) |
---|
94 | for (j=0;j<=max_aa;j++) |
---|
95 | matrix[i][j] = 0; |
---|
96 | |
---|
97 | ix = 0; |
---|
98 | maxres = 0; |
---|
99 | for (i=0;i<=max_aa;i++) |
---|
100 | { |
---|
101 | ti = xref[i]; |
---|
102 | for (j=0;j<=i;j++) |
---|
103 | { |
---|
104 | tj = xref[j]; |
---|
105 | if ((ti != -1) && (tj != -1)) |
---|
106 | { |
---|
107 | k = matptr[ix]; |
---|
108 | if (ti==tj) |
---|
109 | { |
---|
110 | matrix[ti][ti] = k * scale; |
---|
111 | maxres++; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | matrix[ti][tj] = k * scale; |
---|
116 | matrix[tj][ti] = k * scale; |
---|
117 | } |
---|
118 | ix++; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | av1 = av2 = av3 = 0; |
---|
124 | for (i=0;i<=max_aa;i++) |
---|
125 | { |
---|
126 | for (j=0;j<=i;j++) |
---|
127 | { |
---|
128 | av1 += matrix[i][j]; |
---|
129 | if (i==j) |
---|
130 | { |
---|
131 | av2 += matrix[i][j]; |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | av3 += matrix[i][j]; |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | --maxres; |
---|
141 | av1 /= (maxres*maxres)/2; |
---|
142 | av2 /= maxres; |
---|
143 | av3 /= ((float)(maxres*maxres-maxres))/2; |
---|
144 | if (debug>1) fprintf(stdout,"average mismatch score %d\n",(pint)av3); |
---|
145 | if (debug>1) fprintf(stdout,"average match score %d\n",(pint)av2); |
---|
146 | if (debug>1) fprintf(stdout,"average score %d\n",(pint)av1); |
---|
147 | |
---|
148 | min = max = matrix[0][0]; |
---|
149 | for (i=0;i<=max_aa;i++) |
---|
150 | for (j=1;j<=i;j++) |
---|
151 | { |
---|
152 | if (matrix[i][j] < min) min = matrix[i][j]; |
---|
153 | if (matrix[i][j] > max) max = matrix[i][j]; |
---|
154 | } |
---|
155 | /* |
---|
156 | if requested, make a positive matrix - add -(lowest score) to every entry |
---|
157 | */ |
---|
158 | if (neg_flag == FALSE) |
---|
159 | { |
---|
160 | |
---|
161 | if (debug>1) fprintf(stdout,"min %d max %d\n",(pint)min,(pint)max); |
---|
162 | if (min < 0) |
---|
163 | { |
---|
164 | for (i=0;i<=max_aa;i++) |
---|
165 | { |
---|
166 | ti = xref[i]; |
---|
167 | if (ti != -1) |
---|
168 | { |
---|
169 | for (j=0;j<=max_aa;j++) |
---|
170 | { |
---|
171 | tj = xref[j]; |
---|
172 | /* |
---|
173 | if (tj != -1) matrix[ti][tj] -= (2*av3); |
---|
174 | */ |
---|
175 | if (tj != -1) matrix[ti][tj] -= min; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | /* |
---|
181 | gr_score = av3; |
---|
182 | gg_score = -av3; |
---|
183 | */ |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | mat_avscore = -av3; |
---|
188 | |
---|
189 | |
---|
190 | for (i=0;i<gap_pos1;i++) |
---|
191 | { |
---|
192 | matrix[i][gap_pos1] = gr_score; |
---|
193 | matrix[gap_pos1][i] = gr_score; |
---|
194 | matrix[i][gap_pos2] = gr_score; |
---|
195 | matrix[gap_pos2][i] = gr_score; |
---|
196 | } |
---|
197 | matrix[gap_pos1][gap_pos1] = gg_score; |
---|
198 | matrix[gap_pos2][gap_pos2] = gg_score; |
---|
199 | matrix[gap_pos2][gap_pos1] = gg_score; |
---|
200 | matrix[gap_pos2][gap_pos1] = gg_score; |
---|
201 | |
---|
202 | maxres += 2; |
---|
203 | |
---|
204 | return(maxres); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | sint read_user_matrix(char *filename, short *usermat, short *xref) |
---|
209 | { |
---|
210 | double f; |
---|
211 | sint numargs,farg; |
---|
212 | sint i, j, k = 0; |
---|
213 | FILE *fd = NULL; |
---|
214 | char codes[NUMRES]; |
---|
215 | char inline1[1024]; |
---|
216 | char *args[NUMRES+4]; |
---|
217 | char c1,c2; |
---|
218 | sint ix1, ix = 0; |
---|
219 | sint maxres = 0; |
---|
220 | |
---|
221 | if (filename[0] == '\0') |
---|
222 | { |
---|
223 | error("comparison matrix not specified"); |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | if ((fd=fopen(filename,"r"))==NULL) |
---|
228 | { |
---|
229 | error("cannot open %s", filename); |
---|
230 | return((sint)0); |
---|
231 | } |
---|
232 | |
---|
233 | maxres = 0; |
---|
234 | while (fgets(inline1,1024,fd) != NULL) |
---|
235 | { |
---|
236 | if ((inline1[0] == '\0') || (inline1[0] == '#')) continue; |
---|
237 | /* |
---|
238 | read residue characters. |
---|
239 | */ |
---|
240 | k = 0; |
---|
241 | for (j=0;j<strlen(inline1);j++) |
---|
242 | { |
---|
243 | if (isalpha((int)inline1[j])) codes[k++] = inline1[j]; |
---|
244 | if (k>NUMRES) |
---|
245 | { |
---|
246 | error("too many entries in %s",filename); |
---|
247 | return((sint)0); |
---|
248 | } |
---|
249 | } |
---|
250 | codes[k] = '\0'; |
---|
251 | break; |
---|
252 | } |
---|
253 | |
---|
254 | if (k == 0) |
---|
255 | { |
---|
256 | error("wrong format in %s",filename); |
---|
257 | return((sint)0); |
---|
258 | } |
---|
259 | |
---|
260 | /* |
---|
261 | cross-reference the residues |
---|
262 | */ |
---|
263 | for (i=0;i<NUMRES;i++) xref[i] = -1; |
---|
264 | |
---|
265 | maxres = 0; |
---|
266 | for (i=0;(c1=codes[i]);i++) |
---|
267 | { |
---|
268 | for (j=0;(c2=amino_acid_codes[j]);j++) |
---|
269 | if (c1 == c2) |
---|
270 | { |
---|
271 | xref[i] = j; |
---|
272 | maxres++; |
---|
273 | break; |
---|
274 | } |
---|
275 | if ((xref[i] == -1) && (codes[i] != '*')) |
---|
276 | { |
---|
277 | warning("residue %c in %s not recognised", |
---|
278 | codes[i],filename); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | /* |
---|
284 | get the weights |
---|
285 | */ |
---|
286 | |
---|
287 | ix = ix1 = 0; |
---|
288 | while (fgets(inline1,1024,fd) != NULL) |
---|
289 | { |
---|
290 | if (inline1[0] == '\n') continue; |
---|
291 | numargs = getargs(inline1, args, (int)(k+1)); |
---|
292 | if (numargs == 0) |
---|
293 | { |
---|
294 | error("wrong format in %s", filename); |
---|
295 | return((sint)0); |
---|
296 | } |
---|
297 | if (isalpha(args[0][0])) farg=1; |
---|
298 | else farg=0; |
---|
299 | for (i=0;i<=ix;i++) |
---|
300 | { |
---|
301 | if (xref[i] != -1) |
---|
302 | { |
---|
303 | f = atof(args[i+farg]); |
---|
304 | usermat[ix1++] = (short)(f*10.0); |
---|
305 | } |
---|
306 | } |
---|
307 | ix++; |
---|
308 | } |
---|
309 | if (ix != k+1) |
---|
310 | { |
---|
311 | error("wrong format in %s", filename); |
---|
312 | return((sint)0); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | fclose(fd); |
---|
317 | |
---|
318 | maxres += 2; |
---|
319 | |
---|
320 | return(maxres); |
---|
321 | } |
---|
322 | |
---|
323 | int getargs(char *inline1,char *args[],int max) |
---|
324 | { |
---|
325 | |
---|
326 | char *inptr; |
---|
327 | /* |
---|
328 | #ifndef MAC |
---|
329 | char *strtok(char *s1, const char *s2); |
---|
330 | #endif |
---|
331 | */ |
---|
332 | int i; |
---|
333 | |
---|
334 | inptr=inline1; |
---|
335 | for (i=0;i<=max;i++) |
---|
336 | { |
---|
337 | if ((args[i]=strtok(inptr," \t\n"))==NULL) |
---|
338 | break; |
---|
339 | inptr=NULL; |
---|
340 | } |
---|
341 | |
---|
342 | return(i); |
---|
343 | } |
---|
344 | |
---|
345 | |
---|