1 | #include "phylip.h" |
---|
2 | #include "seq.h" |
---|
3 | |
---|
4 | /* version 3.6. (c) Copyright 1993-2002 by the University of Washington. |
---|
5 | Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, and Andrew Keeffe. |
---|
6 | Permission is granted to copy and use this program provided no fee is |
---|
7 | charged for it and provided that this copyright notice is not removed. */ |
---|
8 | |
---|
9 | typedef enum { |
---|
10 | seqs, morphology, restsites, genefreqs |
---|
11 | } datatype; |
---|
12 | |
---|
13 | typedef enum { |
---|
14 | dna, rna, protein |
---|
15 | } seqtype; |
---|
16 | |
---|
17 | |
---|
18 | #ifndef OLDC |
---|
19 | /* function prototypes */ |
---|
20 | void getoptions(void); |
---|
21 | void seqboot_inputnumbers(void); |
---|
22 | void seqboot_inputfactors(void); |
---|
23 | void inputoptions(void); |
---|
24 | void seqboot_inputdata(void); |
---|
25 | void allocrest(void); |
---|
26 | void doinput(int argc, Char *argv[]); |
---|
27 | void bootweights(void); |
---|
28 | void sppermute(long); |
---|
29 | void writedata(void); |
---|
30 | void writeweights(void); |
---|
31 | void writecategories(void); |
---|
32 | void bootwrite(void); |
---|
33 | /* function prototypes */ |
---|
34 | #endif |
---|
35 | |
---|
36 | |
---|
37 | FILE *outcatfile, *outweightfile; |
---|
38 | Char infilename[FNMLNGTH], outfilename[FNMLNGTH], catfilename[FNMLNGTH], outcatfilename[FNMLNGTH], |
---|
39 | weightfilename[FNMLNGTH], outweightfilename[FNMLNGTH]; |
---|
40 | long sites, loci, maxalleles, groups, newsites, newersites, |
---|
41 | newgroups, newergroups, nenzymes, reps, ws, blocksize, categs; |
---|
42 | boolean bootstrap, permute, jackknife, xml, weights, categories, factors, |
---|
43 | enzymes, all, justwts, progress; |
---|
44 | datatype data; |
---|
45 | seqtype seq; |
---|
46 | steptr oldweight, where, how_many, newwhere, newhowmany, |
---|
47 | newerwhere, newerhowmany, factorr, newerfactor; |
---|
48 | Char *factor; |
---|
49 | long *alleles; |
---|
50 | Char **nodep; |
---|
51 | double **nodef; |
---|
52 | long **sppord; |
---|
53 | longer seed; |
---|
54 | |
---|
55 | |
---|
56 | void getoptions() |
---|
57 | { |
---|
58 | /* interactively set options */ |
---|
59 | long reps0; |
---|
60 | long inseed, inseed0, loopcount, loopcount2; |
---|
61 | Char ch; |
---|
62 | boolean done1; |
---|
63 | |
---|
64 | data = seqs; |
---|
65 | seq = dna; |
---|
66 | bootstrap = true; |
---|
67 | jackknife = false; |
---|
68 | permute = false; |
---|
69 | blocksize = 1; |
---|
70 | all = false; |
---|
71 | reps = 100; |
---|
72 | weights = false; |
---|
73 | categories = false; |
---|
74 | justwts = false; |
---|
75 | printdata = false; |
---|
76 | dotdiff = true; |
---|
77 | progress = true; |
---|
78 | interleaved = true; |
---|
79 | xml = false; |
---|
80 | loopcount = 0; |
---|
81 | for (;;) { |
---|
82 | cleerhome(); |
---|
83 | printf("\nBootstrapping algorithm, version %s\n\n",VERSION); |
---|
84 | printf("Settings for this run:\n"); |
---|
85 | printf(" D Sequence, Morph, Rest., Gene Freqs? %s\n", |
---|
86 | (data == seqs ) ? "Molecular sequences" : |
---|
87 | (data == morphology ) ? "Discrete Morphology" : |
---|
88 | (data == restsites) ? "Restriction Sites" : |
---|
89 | (data == genefreqs) ? "Gene Frequencies" : ""); |
---|
90 | if (data == restsites) |
---|
91 | printf(" E Number of enzymes? %s\n", |
---|
92 | enzymes ? "Present in input file" : |
---|
93 | "Not present in input file"); |
---|
94 | if (data == genefreqs) |
---|
95 | printf(" A All alleles present at each locus? %s\n", |
---|
96 | all ? "Yes" : "No, one absent at each locus"); |
---|
97 | |
---|
98 | printf(" J Bootstrap, Jackknife, Permute, Rewrite? %s\n", |
---|
99 | jackknife ? "Delete-half jackknife" : |
---|
100 | permute ? "Permute species for each character" : |
---|
101 | bootstrap ? "Bootstrap" : "Rewrite data"); |
---|
102 | if (!(jackknife || permute || bootstrap)) { |
---|
103 | printf(" P PHYLIP or XML output format? %s\n", |
---|
104 | xml ? "XML" : "PHYLIP"); |
---|
105 | if (xml) { |
---|
106 | printf(" S Type of molecular sequences? "); |
---|
107 | switch (seq) { |
---|
108 | case (dna) : printf("DNA\n"); break; |
---|
109 | case (rna) : printf("RNA\n"); break; |
---|
110 | case (protein) : printf("Protein\n"); break; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | if (bootstrap) { |
---|
115 | if (blocksize > 1) |
---|
116 | printf(" B Block size for block-bootstrapping? %ld\n", blocksize); |
---|
117 | else |
---|
118 | printf(" B Block size for block-bootstrapping? %ld (regular bootstrap)\n", blocksize); |
---|
119 | } |
---|
120 | if (bootstrap || jackknife || permute) |
---|
121 | printf(" R How many replicates? %ld\n", reps); |
---|
122 | if (jackknife || bootstrap || permute) { |
---|
123 | printf(" W Read weights of characters? %s\n", |
---|
124 | (weights ? "Yes" : "No")); |
---|
125 | if (data == seqs) |
---|
126 | printf(" C Read categories of sites? %s\n", |
---|
127 | (categories ? "Yes" : "No")); |
---|
128 | if ((!permute)) { |
---|
129 | printf(" F Write out data sets or just weights? %s\n", |
---|
130 | (justwts ? "Just weights" : "Data sets")); |
---|
131 | } |
---|
132 | } |
---|
133 | if (data == seqs || data == restsites) |
---|
134 | printf(" I Input sequences interleaved? %s\n", |
---|
135 | interleaved ? "Yes" : "No, sequential"); |
---|
136 | printf(" 0 Terminal type (IBM PC, ANSI, none)? %s\n", |
---|
137 | ibmpc ? "IBM PC" : ansi ? "ANSI" : "(none)"); |
---|
138 | printf(" 1 Print out the data at start of run %s\n", |
---|
139 | printdata ? "Yes" : "No"); |
---|
140 | if (printdata) |
---|
141 | printf(" . Use dot-differencing to display them %s\n", |
---|
142 | dotdiff ? "Yes" : "No"); |
---|
143 | printf(" 2 Print indications of progress of run %s\n", |
---|
144 | progress ? "Yes" : "No"); |
---|
145 | printf("\n Y to accept these or type the letter for one to change\n"); |
---|
146 | #ifdef WIN32 |
---|
147 | phyFillScreenColor(); |
---|
148 | #endif |
---|
149 | scanf("%c%*[^\n]", &ch); |
---|
150 | getchar(); |
---|
151 | uppercase(&ch); |
---|
152 | if (ch == 'Y') |
---|
153 | break; |
---|
154 | if ((bootstrap && (strchr("ABCDEFJPRWI1.20",ch) != NULL)) || |
---|
155 | ((jackknife || permute) && (strchr("ACDEFJPRWI1.20",ch) != NULL)) || |
---|
156 | (((!bootstrap) && (!jackknife) && (!permute)) && |
---|
157 | ((!xml && (strchr("ADEJPI1.20",ch) != NULL)) || |
---|
158 | (xml && (strchr("ADEJPSI1.20",ch) != NULL))))) { |
---|
159 | switch (ch) { |
---|
160 | |
---|
161 | case 'D': |
---|
162 | if (data == genefreqs) |
---|
163 | data = seqs; |
---|
164 | else |
---|
165 | data = (datatype)((long)data + 1); |
---|
166 | break; |
---|
167 | |
---|
168 | case 'A': |
---|
169 | all = !all; |
---|
170 | break; |
---|
171 | |
---|
172 | case 'E': |
---|
173 | enzymes = !enzymes; |
---|
174 | break; |
---|
175 | |
---|
176 | case 'J': |
---|
177 | if (permute) |
---|
178 | permute = false; |
---|
179 | else if (jackknife) { |
---|
180 | jackknife = false; |
---|
181 | permute = true; |
---|
182 | } else if (bootstrap) { |
---|
183 | bootstrap = false; |
---|
184 | jackknife = true; |
---|
185 | } else |
---|
186 | bootstrap = true; |
---|
187 | break; |
---|
188 | |
---|
189 | case 'P': |
---|
190 | xml = !xml; |
---|
191 | break; |
---|
192 | |
---|
193 | case 'S': |
---|
194 | switch (seq) { |
---|
195 | case (dna): seq = rna; break; |
---|
196 | case (rna): seq = protein; break; |
---|
197 | case (protein): seq = dna; break; |
---|
198 | } |
---|
199 | break; |
---|
200 | |
---|
201 | case 'B': |
---|
202 | loopcount2 = 0; |
---|
203 | do { |
---|
204 | printf("Block size?\n"); |
---|
205 | #ifdef WIN32 |
---|
206 | phyFillScreenColor(); |
---|
207 | #endif |
---|
208 | scanf("%ld%*[^\n]", &blocksize); |
---|
209 | getchar(); |
---|
210 | done1 = (blocksize > 0); |
---|
211 | if (!done1) { |
---|
212 | printf("BAD NUMBER: must be positive\n"); |
---|
213 | } |
---|
214 | countup(&loopcount2, 10); |
---|
215 | } while (done1 != true); |
---|
216 | break; |
---|
217 | |
---|
218 | case 'R': |
---|
219 | done1 = true; |
---|
220 | reps0 = reps; |
---|
221 | loopcount2 = 0; |
---|
222 | do { |
---|
223 | printf("Number of replicates?\n"); |
---|
224 | #ifdef WIN32 |
---|
225 | phyFillScreenColor(); |
---|
226 | #endif |
---|
227 | scanf("%ld%*[^\n]", &reps); |
---|
228 | getchar(); |
---|
229 | done1 = (reps > 0); |
---|
230 | if (!done1) { |
---|
231 | printf("BAD NUMBER: must be positive\n"); |
---|
232 | reps = reps0; |
---|
233 | } |
---|
234 | countup(&loopcount2, 10); |
---|
235 | } while (done1 != true); |
---|
236 | break; |
---|
237 | |
---|
238 | case 'W': |
---|
239 | weights = !weights; |
---|
240 | break; |
---|
241 | |
---|
242 | case 'C': |
---|
243 | categories = !categories; |
---|
244 | break; |
---|
245 | |
---|
246 | case 'F': |
---|
247 | justwts = !justwts; |
---|
248 | break; |
---|
249 | |
---|
250 | case 'I': |
---|
251 | interleaved = !interleaved; |
---|
252 | break; |
---|
253 | |
---|
254 | case '0': |
---|
255 | initterminal(&ibmpc, &ansi); |
---|
256 | break; |
---|
257 | |
---|
258 | case '1': |
---|
259 | printdata = !printdata; |
---|
260 | break; |
---|
261 | |
---|
262 | case '.': |
---|
263 | dotdiff = !dotdiff; |
---|
264 | break; |
---|
265 | |
---|
266 | case '2': |
---|
267 | progress = !progress; |
---|
268 | break; |
---|
269 | } |
---|
270 | } else |
---|
271 | printf("Not a possible option!\n"); |
---|
272 | countup(&loopcount, 100); |
---|
273 | } |
---|
274 | if (bootstrap || jackknife || permute) |
---|
275 | initseed(&inseed, &inseed0, seed); |
---|
276 | xml = xml && (data == seqs); |
---|
277 | } /* getoptions */ |
---|
278 | |
---|
279 | |
---|
280 | void seqboot_inputnumbers() |
---|
281 | { |
---|
282 | /* read numbers of species and of sites */ |
---|
283 | long i; |
---|
284 | |
---|
285 | fscanf(infile, "%ld%ld", &spp, &sites); |
---|
286 | loci = sites; |
---|
287 | maxalleles = 1; |
---|
288 | if (data == restsites && enzymes) |
---|
289 | fscanf(infile, "%ld", &nenzymes); |
---|
290 | if (data == genefreqs) { |
---|
291 | alleles = (long *)Malloc(sites*sizeof(long)); |
---|
292 | scan_eoln(infile); |
---|
293 | sites = 0; |
---|
294 | for (i = 0; i < (loci); i++) { |
---|
295 | if (eoln(infile)) |
---|
296 | scan_eoln(infile); |
---|
297 | fscanf(infile, "%ld", &alleles[i]); |
---|
298 | if (alleles[i] > maxalleles) |
---|
299 | maxalleles = alleles[i]; |
---|
300 | if (all) |
---|
301 | sites += alleles[i]; |
---|
302 | else |
---|
303 | sites += alleles[i] - 1; |
---|
304 | } |
---|
305 | if (!all) |
---|
306 | maxalleles--; |
---|
307 | } |
---|
308 | } /* seqboot_inputnumbers */ |
---|
309 | |
---|
310 | |
---|
311 | void seqboot_inputfactors() |
---|
312 | { |
---|
313 | long i, j; |
---|
314 | Char ch, prevch; |
---|
315 | |
---|
316 | for (i = 2; i <= nmlngth; i++) |
---|
317 | ch = gettc(infile); |
---|
318 | prevch = ' '; |
---|
319 | j = 0; |
---|
320 | for (i = 0; i < (sites); i++) { |
---|
321 | do { |
---|
322 | if (eoln(infile)) |
---|
323 | scan_eoln(infile); |
---|
324 | ch = gettc(infile); |
---|
325 | } while (ch == ' '); |
---|
326 | if (ch != prevch) |
---|
327 | j++; |
---|
328 | prevch = ch; |
---|
329 | factorr[i] = j; |
---|
330 | } |
---|
331 | scan_eoln(infile); |
---|
332 | factors = true; |
---|
333 | } /* seqboot_inputfactors */ |
---|
334 | |
---|
335 | |
---|
336 | void inputoptions() |
---|
337 | { |
---|
338 | /* input the information on the options */ |
---|
339 | Char ch; |
---|
340 | long extranum, weightsum, i, j, k, l, m; |
---|
341 | |
---|
342 | factors = false; |
---|
343 | if (data == genefreqs) { |
---|
344 | k = 0; |
---|
345 | l = 0; |
---|
346 | for (i = 0; i < (loci); i++) { |
---|
347 | if (all) |
---|
348 | m = alleles[i]; |
---|
349 | else |
---|
350 | m = alleles[i] - 1; |
---|
351 | k++; |
---|
352 | for (j = 1; j <= m; j++) { |
---|
353 | l++; |
---|
354 | factorr[l - 1] = k; |
---|
355 | } |
---|
356 | } |
---|
357 | } else { |
---|
358 | for (i = 1; i <= (sites); i++) |
---|
359 | factorr[i - 1] = i; |
---|
360 | } |
---|
361 | for (i = 0; i < (sites); i++) |
---|
362 | oldweight[i] = 1; |
---|
363 | if (weights) |
---|
364 | inputweights2(1, sites, &weightsum, oldweight, &weights, "seqboot"); |
---|
365 | extranum = 0; |
---|
366 | readoptions(&extranum, "F"); |
---|
367 | for (i = 1; i <= extranum; i++) { |
---|
368 | matchoptions(&ch, "F"); |
---|
369 | if (ch == 'F') |
---|
370 | seqboot_inputfactors(); |
---|
371 | } |
---|
372 | if (factors && printdata) { |
---|
373 | for(i = 0; i < sites; i++) |
---|
374 | factor[i] = (char)('0' + (factorr[i]%10)); |
---|
375 | printfactors(outfile, sites, factor, " (least significant digit)"); |
---|
376 | } |
---|
377 | if (weights && printdata) |
---|
378 | printweights(outfile, 0, sites, oldweight, "Sites"); |
---|
379 | for (i = 0; i < (loci); i++) |
---|
380 | how_many[i] = 0; |
---|
381 | for (i = 0; i < (loci); i++) |
---|
382 | where[i] = 0; |
---|
383 | for (i = 1; i <= (sites); i++) { |
---|
384 | how_many[factorr[i - 1] - 1]++; |
---|
385 | if (where[factorr[i - 1] - 1] == 0) |
---|
386 | where[factorr[i - 1] - 1] = i; |
---|
387 | } |
---|
388 | groups = factorr[sites - 1]; |
---|
389 | newgroups = 0; |
---|
390 | newsites = 0; |
---|
391 | for (i = 0; i < (groups); i++) { |
---|
392 | if (oldweight[where[i] - 1] > 0) { |
---|
393 | newgroups++; |
---|
394 | newsites += how_many[i]; |
---|
395 | newwhere[newgroups - 1] = where[i]; |
---|
396 | newhowmany[newgroups - 1] = how_many[i]; |
---|
397 | } |
---|
398 | } |
---|
399 | } /* inputoptions */ |
---|
400 | |
---|
401 | |
---|
402 | void seqboot_inputdata() |
---|
403 | { |
---|
404 | /* input the names and sequences for each species */ |
---|
405 | long i, j, k, l, m, n, basesread, basesnew=0; |
---|
406 | double x; |
---|
407 | Char charstate; |
---|
408 | boolean allread, done; |
---|
409 | |
---|
410 | if (data == genefreqs) { |
---|
411 | nodef = (double **)Malloc(spp*sizeof(double *)); |
---|
412 | for (i = 0; i < (spp); i++) |
---|
413 | nodef[i] = (double *)Malloc(sites*sizeof(double)); |
---|
414 | } else { |
---|
415 | nodep = (Char **)Malloc(spp*sizeof(Char *)); |
---|
416 | for (i = 0; i < (spp); i++) |
---|
417 | nodep[i] = (Char *)Malloc(sites*sizeof(Char)); |
---|
418 | } |
---|
419 | j = nmlngth + (sites + (sites - 1) / 10) / 2 - 5; |
---|
420 | if (j < nmlngth - 1) |
---|
421 | j = nmlngth - 1; |
---|
422 | if (j > 37) |
---|
423 | j = 37; |
---|
424 | if (printdata) { |
---|
425 | fprintf(outfile, "\nBootstrapping algorithm, version %s\n\n\n",VERSION); |
---|
426 | if (data == genefreqs) |
---|
427 | fprintf(outfile, "%3ld species, %3ld loci\n\n", spp, loci); |
---|
428 | else |
---|
429 | fprintf(outfile, "%3ld species, %3ld sites\n\n", spp, sites); |
---|
430 | fprintf(outfile, "Name"); |
---|
431 | for (i = 1; i <= j; i++) |
---|
432 | putc(' ', outfile); |
---|
433 | fprintf(outfile, "Data\n"); |
---|
434 | fprintf(outfile, "----"); |
---|
435 | for (i = 1; i <= j; i++) |
---|
436 | putc(' ', outfile); |
---|
437 | fprintf(outfile, "----\n\n"); |
---|
438 | } |
---|
439 | interleaved = (interleaved && ((data == seqs) || (data == restsites))); |
---|
440 | if (data == genefreqs) { |
---|
441 | for (i = 1; i <= (spp); i++) { |
---|
442 | initname(i - 1); |
---|
443 | j = 1; |
---|
444 | while (j <= sites && !eoff(infile)) { |
---|
445 | if (eoln(infile)) |
---|
446 | scan_eoln(infile); |
---|
447 | fscanf(infile, "%lf", &x); |
---|
448 | if ((unsigned)x > 1.0) { |
---|
449 | printf("GENE FREQ OUTSIDE [0,1], species%3ld\n", i); |
---|
450 | exxit(-1); |
---|
451 | } else { |
---|
452 | nodef[i - 1][j - 1] = x; |
---|
453 | j++; |
---|
454 | } |
---|
455 | } |
---|
456 | scan_eoln(infile); |
---|
457 | } |
---|
458 | return; |
---|
459 | } |
---|
460 | basesread = 0; |
---|
461 | allread = false; |
---|
462 | while (!allread) { |
---|
463 | allread = true; |
---|
464 | if (eoln(infile)) |
---|
465 | scan_eoln(infile); |
---|
466 | i = 1; |
---|
467 | while (i <= spp) { |
---|
468 | if ((interleaved && basesread == 0) || !interleaved) |
---|
469 | initname(i-1); |
---|
470 | j = interleaved ? basesread : 0; |
---|
471 | done = false; |
---|
472 | while (!done && !eoff(infile)) { |
---|
473 | if (interleaved) |
---|
474 | done = true; |
---|
475 | while (j < sites && !(eoln(infile) ||eoff(infile))) { |
---|
476 | charstate = gettc(infile); |
---|
477 | if (charstate == ' ' || |
---|
478 | (data == seqs && charstate >= '0' && charstate <= '9')) |
---|
479 | continue; |
---|
480 | uppercase(&charstate); |
---|
481 | j++; |
---|
482 | if (charstate == '.') |
---|
483 | charstate = nodep[0][j-1]; |
---|
484 | nodep[i-1][j-1] = charstate; |
---|
485 | } |
---|
486 | if (interleaved) |
---|
487 | continue; |
---|
488 | if (j < sites) |
---|
489 | scan_eoln(infile); |
---|
490 | else if (j == sites) |
---|
491 | done = true; |
---|
492 | } |
---|
493 | if (interleaved && i == 1) |
---|
494 | basesnew = j; |
---|
495 | scan_eoln(infile); |
---|
496 | if ((interleaved && j != basesnew) || ((!interleaved) && j != sites)){ |
---|
497 | printf("\n\nERROR: sequences out of alignment at site %ld", j+1); |
---|
498 | printf(" of species %ld\n\n", i); |
---|
499 | exxit(-1);} |
---|
500 | i++; |
---|
501 | } |
---|
502 | if (interleaved) { |
---|
503 | basesread = basesnew; |
---|
504 | allread = (basesread == sites); |
---|
505 | } else |
---|
506 | allread = (i > spp); |
---|
507 | } |
---|
508 | if (!printdata) |
---|
509 | return; |
---|
510 | if (data == genefreqs) |
---|
511 | m = (sites - 1) / 8 + 1; |
---|
512 | else |
---|
513 | m = (sites - 1) / 60 + 1; |
---|
514 | for (i = 1; i <= m; i++) { |
---|
515 | for (j = 0; j < (spp); j++) { |
---|
516 | for (k = 0; k < nmlngth; k++) |
---|
517 | putc(nayme[j][k], outfile); |
---|
518 | fprintf(outfile, " "); |
---|
519 | if (data == genefreqs) |
---|
520 | l = i * 8; |
---|
521 | else |
---|
522 | l = i * 60; |
---|
523 | if (l > sites) |
---|
524 | l = sites; |
---|
525 | if (data == genefreqs) |
---|
526 | n = (i - 1) * 8; |
---|
527 | else |
---|
528 | n = (i - 1) * 60; |
---|
529 | for (k = n; k < l; k++) { |
---|
530 | if (data == genefreqs) |
---|
531 | fprintf(outfile, "%8.5f", nodef[j][k]); |
---|
532 | else { |
---|
533 | if (j + 1 > 1 && nodep[j][k] == nodep[0][k]) |
---|
534 | charstate = '.'; |
---|
535 | else |
---|
536 | charstate = nodep[j][k]; |
---|
537 | putc(charstate, outfile); |
---|
538 | if ((k + 1) % 10 == 0 && (k + 1) % 60 != 0) |
---|
539 | putc(' ', outfile); |
---|
540 | |
---|
541 | } |
---|
542 | } |
---|
543 | putc('\n', outfile); |
---|
544 | } |
---|
545 | putc('\n', outfile); |
---|
546 | } |
---|
547 | putc('\n', outfile); |
---|
548 | } /* seqboot_inputdata */ |
---|
549 | |
---|
550 | |
---|
551 | void allocrest() |
---|
552 | { |
---|
553 | oldweight = (steptr)Malloc(sites*sizeof(long)); |
---|
554 | weight = (steptr)Malloc(sites*sizeof(long)); |
---|
555 | if (categories) |
---|
556 | category = (steptr)Malloc(sites*sizeof(long)); |
---|
557 | where = (steptr)Malloc(loci*sizeof(long)); |
---|
558 | how_many = (steptr)Malloc(loci*sizeof(long)); |
---|
559 | factor = (Char *)Malloc(sites*sizeof(Char)); |
---|
560 | factorr = (steptr)Malloc(sites*sizeof(long)); |
---|
561 | newwhere = (steptr)Malloc(loci*sizeof(long)); |
---|
562 | newhowmany = (steptr)Malloc(loci*sizeof(long)); |
---|
563 | newerwhere = (steptr)Malloc(loci*sizeof(long)); |
---|
564 | newerhowmany = (steptr)Malloc(loci*sizeof(long)); |
---|
565 | newerfactor = (steptr)Malloc(loci*maxalleles*sizeof(long)); |
---|
566 | nayme = (naym *)Malloc(spp*sizeof(naym)); |
---|
567 | } /* allocrest */ |
---|
568 | |
---|
569 | |
---|
570 | void doinput(int UNUSED_argc, Char *argv[]) |
---|
571 | { |
---|
572 | (void)UNUSED_argc; |
---|
573 | |
---|
574 | /* reads the input data */ |
---|
575 | getoptions(); |
---|
576 | seqboot_inputnumbers(); |
---|
577 | allocrest(); |
---|
578 | if (weights) |
---|
579 | openfile(&weightfile,WEIGHTFILE,"input weight file", |
---|
580 | "r",argv[0],weightfilename); |
---|
581 | if (categories) { |
---|
582 | openfile(&catfile,CATFILE,"input category file","r",argv[0],catfilename); |
---|
583 | openfile(&outcatfile,"outcategories","output category file","w",argv[0], |
---|
584 | outcatfilename); |
---|
585 | inputcategs(0, sites, category, 9, "SeqBoot"); |
---|
586 | } |
---|
587 | if (justwts && !permute) |
---|
588 | openfile(&outweightfile,"outweights","output weight file", |
---|
589 | "w",argv[0],outweightfilename); |
---|
590 | else { |
---|
591 | if (weights) |
---|
592 | openfile(&outweightfile,"outweights","output weight file", |
---|
593 | "w",argv[0],outweightfilename); |
---|
594 | openfile(&outfile,OUTFILE,"output data file","w",argv[0],outfilename); |
---|
595 | } |
---|
596 | inputoptions(); |
---|
597 | seqboot_inputdata(); |
---|
598 | } /* doinput */ |
---|
599 | |
---|
600 | |
---|
601 | void bootweights() |
---|
602 | { |
---|
603 | /* sets up weights by resampling data */ |
---|
604 | long i, j, k, blocks; |
---|
605 | double p, q, r; |
---|
606 | |
---|
607 | ws = newgroups; |
---|
608 | for (i = 0; i < (ws); i++) |
---|
609 | weight[i] = 0; |
---|
610 | if (jackknife) { |
---|
611 | if (newgroups & 1) { |
---|
612 | if (randum(seed) < 0.5) |
---|
613 | q = (newgroups - 1.0) / 2; |
---|
614 | else |
---|
615 | q = (newgroups + 1.0) / 2; |
---|
616 | } else |
---|
617 | q = newgroups / 2.0; |
---|
618 | r = newgroups; |
---|
619 | p = q / r; |
---|
620 | ws = 0; |
---|
621 | for (i = 0; i < (newgroups); i++) { |
---|
622 | if (randum(seed) < p) { |
---|
623 | weight[i]++; |
---|
624 | ws++; |
---|
625 | q--; |
---|
626 | } |
---|
627 | r--; |
---|
628 | if (i + 1 < newgroups) |
---|
629 | p = q / r; |
---|
630 | } |
---|
631 | } else if (permute) { |
---|
632 | for (i = 0; i < (newgroups); i++) |
---|
633 | weight[i] = 1; |
---|
634 | } else if (bootstrap) { |
---|
635 | blocks = newgroups / blocksize; |
---|
636 | for (i = 1; i <= (blocks); i++) { |
---|
637 | j = (long)(newgroups * randum(seed)) + 1; |
---|
638 | for (k = 0; k < blocksize; k++) { |
---|
639 | weight[j - 1]++; |
---|
640 | j++; |
---|
641 | if (j > newgroups) |
---|
642 | j = 1; |
---|
643 | } |
---|
644 | } |
---|
645 | } else /* case of rewriting data */ |
---|
646 | for (i = 0; i < (newgroups); i++) |
---|
647 | weight[i] = 1; |
---|
648 | for (i = 0; i < (newgroups); i++) |
---|
649 | newerwhere[i] = 0; |
---|
650 | for (i = 0; i < (newgroups); i++) |
---|
651 | newerhowmany[i] = 0; |
---|
652 | newergroups = 0; |
---|
653 | newersites = 0; |
---|
654 | for (i = 0; i < (newgroups); i++) { |
---|
655 | for (j = 1; j <= (weight[i]); j++) { |
---|
656 | newergroups++; |
---|
657 | for (k = 1; k <= (newhowmany[i]); k++) { |
---|
658 | newersites++; |
---|
659 | newerfactor[newersites - 1] = newergroups; |
---|
660 | } |
---|
661 | newerwhere[newergroups - 1] = newwhere[i]; |
---|
662 | newerhowmany[newergroups - 1] = newhowmany[i]; |
---|
663 | } |
---|
664 | } |
---|
665 | } /* bootweights */ |
---|
666 | |
---|
667 | |
---|
668 | void sppermute(long n) |
---|
669 | { long i, j, k; |
---|
670 | for (i = 1; i <= (spp - 1); i++) { |
---|
671 | k = (long)((i+1) * randum(seed)); |
---|
672 | j = sppord[n - 1][i]; |
---|
673 | sppord[n - 1][i] = sppord[n - 1][k]; |
---|
674 | sppord[n - 1][k] = j; |
---|
675 | } |
---|
676 | } /* sppermute */ |
---|
677 | |
---|
678 | |
---|
679 | void writedata() |
---|
680 | { |
---|
681 | /* write out one set of bootstrapped sequences */ |
---|
682 | long i, j, k, l, m, n, n2; |
---|
683 | double x; |
---|
684 | Char charstate; |
---|
685 | |
---|
686 | sppord = (long **)Malloc(newergroups*sizeof(long *)); |
---|
687 | for (i = 0; i < (newergroups); i++) |
---|
688 | sppord[i] = (long *)Malloc(spp*sizeof(long)); |
---|
689 | for (j = 1; j <= spp; j++) |
---|
690 | sppord[0][j - 1] = j; |
---|
691 | for (i = 1; i < newergroups; i++) { |
---|
692 | for (j = 1; j <= (spp); j++) |
---|
693 | sppord[i][j - 1] = sppord[i - 1][j - 1]; |
---|
694 | } |
---|
695 | if (!justwts || permute) { |
---|
696 | if (data == restsites && enzymes) |
---|
697 | fprintf(outfile, "%5ld %5ld% 4ld\n", spp, newergroups, nenzymes); |
---|
698 | else if (data == genefreqs) |
---|
699 | fprintf(outfile, "%5ld %5ld\n", spp, newergroups); |
---|
700 | else { |
---|
701 | if ((data == seqs) && !(bootstrap || jackknife || permute) && xml) |
---|
702 | fprintf(outfile, "<alignment>\n"); |
---|
703 | else |
---|
704 | fprintf(outfile, "%5ld %5ld\n", spp, newersites); |
---|
705 | } |
---|
706 | if (data == genefreqs) { |
---|
707 | for (i = 0; i < (newergroups); i++) |
---|
708 | fprintf(outfile, " %3ld", alleles[factorr[newerwhere[i] - 1] - 1]); |
---|
709 | putc('\n', outfile); |
---|
710 | } |
---|
711 | } |
---|
712 | l = 1; |
---|
713 | if ((!(bootstrap || jackknife || permute)) |
---|
714 | && ((data == seqs) || (data == restsites))) { |
---|
715 | interleaved = !interleaved; |
---|
716 | if (!(bootstrap || jackknife || permute) && xml) |
---|
717 | interleaved = false; |
---|
718 | } |
---|
719 | if (interleaved) |
---|
720 | m = 60; |
---|
721 | else |
---|
722 | m = newergroups; |
---|
723 | do { |
---|
724 | if (m > newergroups) |
---|
725 | m = newergroups; |
---|
726 | for (j = 0; j < spp; j++) { |
---|
727 | n = 0; |
---|
728 | if (l == 1) { |
---|
729 | if (!(bootstrap || jackknife || permute) && xml) { |
---|
730 | fprintf(outfile, " <sequence"); |
---|
731 | switch (seq) { |
---|
732 | case (dna): fprintf(outfile, " type=dna"); break; |
---|
733 | case (rna): fprintf(outfile, " type=rna"); break; |
---|
734 | case (protein): fprintf(outfile, " type=protein"); break; |
---|
735 | } |
---|
736 | fprintf(outfile, ">\n"); |
---|
737 | fprintf(outfile, " <name>"); |
---|
738 | } |
---|
739 | n2 = nmlngth-1; |
---|
740 | if (!(bootstrap || jackknife || permute) && xml) { |
---|
741 | while (nayme[sppord[0][j] - 1][n2] == ' ') |
---|
742 | n2--; |
---|
743 | } |
---|
744 | for (k = 0; k <= n2; k++) |
---|
745 | putc(nayme[sppord[0][j] - 1][k], outfile); |
---|
746 | if (!(bootstrap || jackknife || permute) && xml) |
---|
747 | fprintf(outfile, "</name>\n <data>"); |
---|
748 | } else { |
---|
749 | if (!(bootstrap || jackknife || permute) && xml) { |
---|
750 | fprintf(outfile, " "); |
---|
751 | } |
---|
752 | else { |
---|
753 | for (k = 1; k <= nmlngth; k++) |
---|
754 | putc(' ', outfile); |
---|
755 | } |
---|
756 | } |
---|
757 | for (k = l - 1; k < m; k++) { |
---|
758 | if (permute && j + 1 == 1) |
---|
759 | sppermute(newerfactor[n]); |
---|
760 | for (n2 = -1; n2 <= (newerhowmany[k] - 2); n2++) { |
---|
761 | n++; |
---|
762 | if (data == genefreqs) { |
---|
763 | if (n > 1 && (n & 7) == 1) |
---|
764 | fprintf(outfile, "\n "); |
---|
765 | x = nodef[sppord[newerfactor[n - 1] - 1][j] - 1][newerwhere[k] + n2]; |
---|
766 | fprintf(outfile, "%8.5f", x); |
---|
767 | } else { |
---|
768 | if (!(bootstrap || jackknife || permute) && xml |
---|
769 | && (n > 1) && (n % 60 == 1)) |
---|
770 | fprintf(outfile, "\n "); |
---|
771 | else if (!interleaved && (n > 1) && (n % 60 == 1)) |
---|
772 | fprintf(outfile, "\n "); |
---|
773 | charstate = |
---|
774 | nodep[sppord[newerfactor[n - 1] - 1][j] - 1][newerwhere[k] + n2]; |
---|
775 | putc(charstate, outfile); |
---|
776 | if (n % 10 == 0 && n % 60 != 0) |
---|
777 | putc(' ', outfile); |
---|
778 | } |
---|
779 | } |
---|
780 | } |
---|
781 | if (!(bootstrap || jackknife || permute) && xml) { |
---|
782 | fprintf(outfile, "</data>\n </sequence>\n"); |
---|
783 | } |
---|
784 | putc('\n', outfile); |
---|
785 | } |
---|
786 | if (interleaved) { |
---|
787 | if ((m <= newersites) && (newersites > 60)) |
---|
788 | putc('\n', outfile); |
---|
789 | l += 60; |
---|
790 | m += 60; |
---|
791 | } |
---|
792 | } while (interleaved && l <= newersites); |
---|
793 | if ((data == seqs) && (!(bootstrap || jackknife || permute) && xml)) |
---|
794 | fprintf(outfile, "</alignment>\n"); |
---|
795 | for (i = 0; i < (newergroups); i++) |
---|
796 | free(sppord[i]); |
---|
797 | free(sppord); |
---|
798 | } /* writedata */ |
---|
799 | |
---|
800 | |
---|
801 | void writeweights() |
---|
802 | { |
---|
803 | /* write out one set of post-bootstrapping weights */ |
---|
804 | long k, l, m, n; |
---|
805 | |
---|
806 | l = 1; |
---|
807 | if (interleaved) |
---|
808 | m = 60; |
---|
809 | else |
---|
810 | m = newergroups; |
---|
811 | do { |
---|
812 | if (m > newergroups) |
---|
813 | m = newergroups; |
---|
814 | n = 0; |
---|
815 | for (k = l - 1; k < m; k++) { |
---|
816 | if (weight[k] < 10) |
---|
817 | fprintf(outweightfile, "%c", (char)('0'+weight[k])); |
---|
818 | else |
---|
819 | fprintf(outweightfile, "%c", (char)('A'+weight[k]-10)); |
---|
820 | n++; |
---|
821 | if (!interleaved && n > 1 && n % 60 == 1) { |
---|
822 | fprintf(outweightfile, "\n"); |
---|
823 | if (n % 10 == 0 && n % 60 != 0) |
---|
824 | putc(' ', outweightfile); |
---|
825 | } |
---|
826 | } |
---|
827 | putc('\n', outweightfile); |
---|
828 | if (interleaved) { |
---|
829 | l += 60; |
---|
830 | m += 60; |
---|
831 | } |
---|
832 | } while (interleaved && l <= newersites); |
---|
833 | } /* writeweights */ |
---|
834 | |
---|
835 | |
---|
836 | void writecategories() |
---|
837 | { |
---|
838 | /* write out categories for the bootstrapped sequences */ |
---|
839 | long k, l, m, n, n2; |
---|
840 | Char charstate; |
---|
841 | |
---|
842 | l = 1; |
---|
843 | if (interleaved) |
---|
844 | m = 60; |
---|
845 | else |
---|
846 | m = newergroups; |
---|
847 | do { |
---|
848 | if (m > newergroups) |
---|
849 | m = newergroups; |
---|
850 | n = 0; |
---|
851 | for (k = l - 1; k < m; k++) { |
---|
852 | for (n2 = -1; n2 <= (newerhowmany[k] - 2); n2++) { |
---|
853 | n++; |
---|
854 | if (!interleaved && n > 1 && n % 60 == 1) |
---|
855 | fprintf(outcatfile, "\n "); |
---|
856 | charstate = '0' + category[newerwhere[k] + n2]; |
---|
857 | putc(charstate, outcatfile); |
---|
858 | if (n % 10 == 0 && n % 60 != 0) |
---|
859 | putc(' ', outcatfile); |
---|
860 | } |
---|
861 | } |
---|
862 | if (interleaved) { |
---|
863 | l += 60; |
---|
864 | m += 60; |
---|
865 | } |
---|
866 | } while (interleaved && l <= newersites); |
---|
867 | fprintf(outcatfile, "\n "); |
---|
868 | } /* writecategories */ |
---|
869 | |
---|
870 | |
---|
871 | void bootwrite() |
---|
872 | { |
---|
873 | /* does bootstrapping and writes out data sets */ |
---|
874 | long rr, repdiv10; |
---|
875 | |
---|
876 | if (!(bootstrap || jackknife || permute)) |
---|
877 | reps = 1; |
---|
878 | repdiv10 = reps / 10; |
---|
879 | if (repdiv10 < 1) |
---|
880 | repdiv10 = 1; |
---|
881 | if (progress) |
---|
882 | putchar('\n'); |
---|
883 | for (rr = 1; rr <= (reps); rr++) { |
---|
884 | bootweights(); |
---|
885 | if (!justwts || permute) |
---|
886 | writedata(); |
---|
887 | if (justwts && !permute) |
---|
888 | writeweights(); |
---|
889 | if (categories) |
---|
890 | writecategories(); |
---|
891 | if (progress && (bootstrap || jackknife || permute) |
---|
892 | && rr % repdiv10 == 0) { |
---|
893 | printf("completed replicate number %4ld\n", rr); |
---|
894 | #ifdef WIN32 |
---|
895 | phyFillScreenColor(); |
---|
896 | #endif |
---|
897 | } |
---|
898 | } |
---|
899 | if (progress) { |
---|
900 | if (justwts) |
---|
901 | printf("\nOutput weights written to file \"%s\"\n\n", outweightfilename); |
---|
902 | else |
---|
903 | printf("\nOutput written to file \"%s\"\n\n", outfilename); |
---|
904 | } |
---|
905 | } /* bootwrite */ |
---|
906 | |
---|
907 | |
---|
908 | int main(int argc, Char *argv[]) |
---|
909 | { /* Read in sequences or frequencies and bootstrap or jackknife them */ |
---|
910 | #ifdef MAC |
---|
911 | argc = 1; /* macsetup("SeqBoot",""); */ |
---|
912 | argv[0] = "SeqBoot"; |
---|
913 | #endif |
---|
914 | init(argc,argv); |
---|
915 | openfile(&infile,INFILE,"input file","r",argv[0],infilename); |
---|
916 | ibmpc = IBMCRT; |
---|
917 | ansi = ANSICRT; |
---|
918 | doinput(argc, argv); |
---|
919 | bootwrite(); |
---|
920 | FClose(infile); |
---|
921 | if (weights) |
---|
922 | FClose(weightfile); |
---|
923 | if (categories) { |
---|
924 | FClose(catfile); |
---|
925 | FClose(outcatfile); |
---|
926 | } |
---|
927 | if (justwts && !permute) { |
---|
928 | FClose(outweightfile); |
---|
929 | } |
---|
930 | else |
---|
931 | FClose(outfile); |
---|
932 | #ifdef MAC |
---|
933 | fixmacfile(outfilename); |
---|
934 | if (justwts && !permute) |
---|
935 | fixmacfile(outweightfilename); |
---|
936 | if (categories) |
---|
937 | fixmacfile(outcatfilename); |
---|
938 | #endif |
---|
939 | printf("Done.\n\n"); |
---|
940 | #ifdef WIN32 |
---|
941 | phyRestoreConsoleAttributes(); |
---|
942 | #endif |
---|
943 | return 0; |
---|
944 | } |
---|