1 | /* |
---|
2 | * seqproc.c Adachi, J. 1994.01.21 |
---|
3 | * Copyright (C) 1992, 1993 J. Adachi & M. Hasegawa, All rights reserved. |
---|
4 | */ |
---|
5 | |
---|
6 | #include "protml.h" |
---|
7 | |
---|
8 | |
---|
9 | void |
---|
10 | convseq(seqconint, maxspc, numptrn) |
---|
11 | imatrix seqconint; |
---|
12 | int maxspc, numptrn; |
---|
13 | { |
---|
14 | int i, j; |
---|
15 | |
---|
16 | for (i = 0; i < maxspc; i++) { |
---|
17 | for (j = 0; j < numptrn; j++) { |
---|
18 | if (seqconint[i][j] == Tpmradix) { |
---|
19 | seqconint[i][j] = -1; |
---|
20 | } |
---|
21 | } |
---|
22 | } |
---|
23 | #ifdef SEQ_DEBUG |
---|
24 | if (Debug) { |
---|
25 | for (i = 0; i < maxspc; i++) { |
---|
26 | for (j = 0; j < numptrn; j++) { |
---|
27 | if ((j % 30) == 0) putchar('\n'); |
---|
28 | printf("%2d",seqconint[i][j]); |
---|
29 | } |
---|
30 | } putchar('\n'); |
---|
31 | } |
---|
32 | #endif |
---|
33 | } /*_ convseq */ |
---|
34 | |
---|
35 | |
---|
36 | void |
---|
37 | getfreqepm(seqchar, freqemp, maxspc, maxsite) |
---|
38 | cmatrix seqchar; |
---|
39 | double *freqemp; |
---|
40 | int maxspc, maxsite; |
---|
41 | { |
---|
42 | int i, j, all; |
---|
43 | ivector gene; |
---|
44 | |
---|
45 | gene = new_ivector(Tpmradix + 1); |
---|
46 | for (i = 0; i < Tpmradix + 1; i++) |
---|
47 | gene[i] = 0; |
---|
48 | for (i = 0; i < maxspc; i++) { |
---|
49 | for (j = 0; j < maxsite; j++) { |
---|
50 | gene[(int)seqchar[i][j]]++; |
---|
51 | } |
---|
52 | } |
---|
53 | all = maxspc * maxsite - gene[Tpmradix]; |
---|
54 | for (i = 0; i < Tpmradix; i++) { |
---|
55 | freqemp[i] = (double)gene[i] / (double)all; |
---|
56 | if (Debug) printf("%4d%8d%10.5f\n", i, gene[i], freqemp[i]); |
---|
57 | } |
---|
58 | if (Debug) printf("all:%8d other:%5d\n", all, gene[Tpmradix]); |
---|
59 | free_ivector(gene); |
---|
60 | |
---|
61 | } /*_ getfreqepm */ |
---|
62 | |
---|
63 | |
---|
64 | void |
---|
65 | convfreq(freqemp) |
---|
66 | double *freqemp; |
---|
67 | { |
---|
68 | int i, maxi; |
---|
69 | double freq, maxfreq, sum; |
---|
70 | |
---|
71 | sum = 0.0; |
---|
72 | maxfreq = 0.0; |
---|
73 | for (i = 0; i < Tpmradix; i++) { |
---|
74 | freq = freqemp[i]; |
---|
75 | if (freq < MINFREQ) |
---|
76 | freqemp[i] = MINFREQ; |
---|
77 | if (freq > maxfreq) { |
---|
78 | maxfreq = freq; |
---|
79 | maxi = i; |
---|
80 | } |
---|
81 | sum += freqemp[i]; |
---|
82 | } |
---|
83 | freqemp[maxi] += 1.0 - sum; |
---|
84 | |
---|
85 | if (Debug) { |
---|
86 | putchar('\n'); |
---|
87 | for (i = 0, sum = 0.0; i < Tpmradix; i++) { |
---|
88 | printf(" %2d %12.8f\n", i, freqemp[i]); |
---|
89 | sum += freqemp[i]; |
---|
90 | } |
---|
91 | printf("sum %12.8f\n", sum); |
---|
92 | } |
---|
93 | } /* convfreq */ |
---|
94 | |
---|
95 | |
---|
96 | void |
---|
97 | radixsort(seqchar, alias, maxspc, maxsite, numptrn) |
---|
98 | cmatrix seqchar; |
---|
99 | ivector alias; |
---|
100 | int maxspc, maxsite; |
---|
101 | int *numptrn; |
---|
102 | { |
---|
103 | int i, j, k, l, n, pass; |
---|
104 | int *awork; |
---|
105 | int *count; |
---|
106 | |
---|
107 | awork = new_ivector(maxsite); |
---|
108 | count = new_ivector(Tpmradix+1); |
---|
109 | for (i = 0; i < maxsite; i++) |
---|
110 | alias[i] = i; |
---|
111 | for (pass = maxspc - 1; pass >= 0; pass--) { |
---|
112 | for (j = 0; j < Tpmradix+1; j++) |
---|
113 | count[j] = 0; |
---|
114 | for (i = 0; i < maxsite; i++) |
---|
115 | count[seqchar[pass][alias[i]]]++; |
---|
116 | for (j = 1; j < Tpmradix+1; j++) |
---|
117 | count[j] += count[j-1]; |
---|
118 | for (i = maxsite-1; i >= 0; i--) |
---|
119 | awork[ --count[seqchar[pass][alias[i]]] ] = alias[i]; |
---|
120 | for (i = 0; i < maxsite; i++) |
---|
121 | alias[i] = awork[i]; |
---|
122 | } |
---|
123 | free_ivector(awork); |
---|
124 | free_ivector(count); |
---|
125 | n = 1; |
---|
126 | for (j = 1; j < maxsite; j++) { |
---|
127 | k = alias[j]; |
---|
128 | l = alias[j-1]; |
---|
129 | for (i = 0; i < maxspc; i++) { |
---|
130 | if (seqchar[i][l] != seqchar[i][k]) { |
---|
131 | n++; |
---|
132 | break; |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | *numptrn = n; |
---|
137 | #ifdef SEQ_DEBUG |
---|
138 | if (Debug_optn) { |
---|
139 | printf("numpatrn =%4d", *numptrn); |
---|
140 | for (k = 0; k < maxsite; k += 30) { |
---|
141 | putchar('\n'); |
---|
142 | for (i = 0; i < maxspc; i++) { |
---|
143 | for (j = k; j < maxsite && j < k+30; j++) |
---|
144 | printf("%2d", seqchar[i][alias[j]]); |
---|
145 | putchar('\n'); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | #endif |
---|
150 | } /*_ radixsort */ |
---|
151 | |
---|
152 | |
---|
153 | void |
---|
154 | condenceseq(seqchar, alias, seqconint, weight, maxspc, maxsite, numptrn) |
---|
155 | cmatrix seqchar; |
---|
156 | ivector alias; |
---|
157 | imatrix seqconint; |
---|
158 | ivector weight; |
---|
159 | int maxspc, maxsite, numptrn; |
---|
160 | { |
---|
161 | int i, j, k, n; |
---|
162 | int agree_flag; /* boolean */ |
---|
163 | |
---|
164 | n = 0; |
---|
165 | k = alias[n]; |
---|
166 | for (i = 0; i < maxspc; i++) { |
---|
167 | seqconint[i][n] = seqchar[i][k]; |
---|
168 | } |
---|
169 | weight[n] = 1; |
---|
170 | for (j = 1; j < maxsite; j++) { |
---|
171 | k = alias[j]; |
---|
172 | agree_flag = TRUE; |
---|
173 | for (i = 0; i < maxspc; i++) { |
---|
174 | if (seqconint[i][n] != seqchar[i][k]) { |
---|
175 | agree_flag = FALSE; |
---|
176 | break; |
---|
177 | } |
---|
178 | } |
---|
179 | if (agree_flag == FALSE) { |
---|
180 | n++; |
---|
181 | for (i = 0; i < maxspc; i++) { |
---|
182 | seqconint[i][n] = seqchar[i][k]; |
---|
183 | } |
---|
184 | weight[n] = 1; |
---|
185 | } else { |
---|
186 | weight[n]++; |
---|
187 | } |
---|
188 | } |
---|
189 | n++; |
---|
190 | if (numptrn != n) { |
---|
191 | printf("ERROR in condenceseq. numptrn != number of array of seqconint"); |
---|
192 | exit(1); |
---|
193 | } |
---|
194 | } /*_ condenceseq */ |
---|
195 | |
---|
196 | |
---|
197 | void |
---|
198 | getnumsites(seqconint, numsites, weight, numspc, numptrn) |
---|
199 | imatrix seqconint; |
---|
200 | ivector numsites; |
---|
201 | ivector weight; |
---|
202 | int numspc; |
---|
203 | int numptrn; |
---|
204 | { |
---|
205 | int i, j, n; |
---|
206 | ivector seq; |
---|
207 | |
---|
208 | for (i = 0; i < numspc; i++) { |
---|
209 | seq = seqconint[i]; |
---|
210 | for (j = 0, n = 0; j < numptrn; j++) { |
---|
211 | if (seq[j] >= 0) n += weight[j]; |
---|
212 | } |
---|
213 | numsites[i] = n; |
---|
214 | /* printf("numsites %3d %5d\n", i ,n); */ |
---|
215 | } |
---|
216 | } /* getnumsites */ |
---|
217 | |
---|
218 | |
---|
219 | void |
---|
220 | prcondenceseq(identif, seqconint, weight, numspc, numsite, numptrn) |
---|
221 | char **identif; |
---|
222 | imatrix seqconint; |
---|
223 | ivector weight; |
---|
224 | int numspc; |
---|
225 | int numsite; |
---|
226 | int numptrn; |
---|
227 | { |
---|
228 | int i, j, k, l, m, n, num, maxj, deci10; |
---|
229 | |
---|
230 | num = 0; |
---|
231 | while ((numsite /= 10) > 0) num++; |
---|
232 | for (k = 0; k < numptrn; k += LINESITES) { |
---|
233 | maxj = ((numptrn < k+LINESITES) ? numptrn : (k+LINESITES)); |
---|
234 | putchar('\n'); |
---|
235 | for (i = 0; i < numspc; i++) { |
---|
236 | fputid(stdout, identif[i], 10); |
---|
237 | putchar(' '); |
---|
238 | for (j = k; j < maxj; j++) { |
---|
239 | if (i == 0 || seqconint[i][j] != seqconint[0][j]) |
---|
240 | printf("%c", int2acid(seqconint[i][j])); |
---|
241 | else |
---|
242 | putchar('.'); |
---|
243 | } |
---|
244 | putchar('\n'); |
---|
245 | } |
---|
246 | n = num; |
---|
247 | do { |
---|
248 | fputs(" ", stdout); |
---|
249 | for (l = 0, deci10 = 1; l < n; l++) deci10 *= 10; |
---|
250 | for (j = k; j < maxj; j++) { |
---|
251 | if ((m = weight[j] / deci10) > 0) { |
---|
252 | if ((m %= 10) != 0) |
---|
253 | printf("%1d", m); |
---|
254 | else |
---|
255 | putchar('0'); |
---|
256 | } else { |
---|
257 | putchar(' '); |
---|
258 | } |
---|
259 | } |
---|
260 | putchar('\n'); |
---|
261 | } while (n-- > 0); |
---|
262 | } |
---|
263 | } /*_ prcondenceseq */ |
---|