source: tags/arb-6.0/GDE/PHYLIP/seqboot.c

Last change on this file was 2175, checked in by westram, 20 years ago

upgrade to PHYLIP 3.6

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.0 KB
Line 
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
9typedef enum {
10  seqs, morphology, restsites, genefreqs
11} datatype;
12
13typedef enum {
14  dna, rna, protein
15} seqtype;
16
17
18#ifndef OLDC
19/* function prototypes */
20void   getoptions(void);
21void   seqboot_inputnumbers(void);
22void   seqboot_inputfactors(void);
23void   inputoptions(void);
24void   seqboot_inputdata(void);
25void   allocrest(void);
26void   doinput(int argc, Char *argv[]);
27void   bootweights(void);
28void   sppermute(long);
29void   writedata(void);
30void   writeweights(void);
31void   writecategories(void);
32void   bootwrite(void);
33/* function prototypes */
34#endif
35
36
37FILE *outcatfile, *outweightfile;
38Char infilename[FNMLNGTH], outfilename[FNMLNGTH], catfilename[FNMLNGTH], outcatfilename[FNMLNGTH],
39   weightfilename[FNMLNGTH], outweightfilename[FNMLNGTH];
40long sites, loci, maxalleles, groups, newsites, newersites,
41             newgroups, newergroups, nenzymes, reps, ws, blocksize, categs;
42boolean bootstrap, permute, jackknife, xml, weights, categories, factors,
43          enzymes, all, justwts, progress;
44datatype data;
45seqtype seq;
46steptr oldweight, where, how_many, newwhere, newhowmany,
47             newerwhere, newerhowmany, factorr, newerfactor;
48Char *factor;
49long *alleles;
50Char **nodep;
51double **nodef;
52long **sppord;
53longer seed;
54
55
56void 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
280void 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
311void 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
336void 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
402void 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
551void 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
570void doinput(int argc, Char *argv[])
571{
572  /* reads the input data */
573  getoptions();
574  seqboot_inputnumbers();
575  allocrest();
576  if (weights)
577    openfile(&weightfile,WEIGHTFILE,"input weight file",
578               "r",argv[0],weightfilename);
579  if (categories) {
580    openfile(&catfile,CATFILE,"input category file","r",argv[0],catfilename);
581    openfile(&outcatfile,"outcategories","output category file","w",argv[0],
582               outcatfilename);
583    inputcategs(0, sites, category, 9, "SeqBoot");
584  }
585  if (justwts && !permute)
586    openfile(&outweightfile,"outweights","output weight file",
587               "w",argv[0],outweightfilename);
588  else {
589    if (weights)
590      openfile(&outweightfile,"outweights","output weight file",
591               "w",argv[0],outweightfilename);
592    openfile(&outfile,OUTFILE,"output data file","w",argv[0],outfilename);
593  }
594  inputoptions();
595  seqboot_inputdata();
596}  /* doinput */
597
598
599void bootweights()
600{
601  /* sets up weights by resampling data */
602  long i, j, k, blocks;
603  double p, q, r;
604
605  ws = newgroups;
606  for (i = 0; i < (ws); i++)
607    weight[i] = 0;
608  if (jackknife) {
609    if (newgroups & 1) {
610      if (randum(seed) < 0.5)
611        q = (newgroups - 1.0) / 2;
612      else
613        q = (newgroups + 1.0) / 2;
614    } else
615      q = newgroups / 2.0;
616    r = newgroups;
617    p = q / r;
618    ws = 0;
619    for (i = 0; i < (newgroups); i++) {
620      if (randum(seed) < p) {
621        weight[i]++;
622        ws++;
623        q--;
624      }
625      r--;
626      if (i + 1 < newgroups)
627        p = q / r;
628    }
629  } else if (permute) {
630    for (i = 0; i < (newgroups); i++)
631      weight[i] = 1;
632  } else if (bootstrap) {
633    blocks = newgroups / blocksize;
634    for (i = 1; i <= (blocks); i++) {
635      j = (long)(newgroups * randum(seed)) + 1;
636      for (k = 0; k < blocksize; k++) {
637        weight[j - 1]++;
638        j++;
639        if (j > newgroups)
640          j = 1;
641      }
642    }
643  } else             /* case of rewriting data */
644    for (i = 0; i < (newgroups); i++)
645      weight[i] = 1;
646  for (i = 0; i < (newgroups); i++)
647    newerwhere[i] = 0;
648  for (i = 0; i < (newgroups); i++)
649    newerhowmany[i] = 0;
650  newergroups = 0;
651  newersites = 0;
652  for (i = 0; i < (newgroups); i++) {
653    for (j = 1; j <= (weight[i]); j++) {
654      newergroups++;
655      for (k = 1; k <= (newhowmany[i]); k++) {
656        newersites++;
657        newerfactor[newersites - 1] = newergroups;
658      }
659      newerwhere[newergroups - 1] = newwhere[i];
660      newerhowmany[newergroups - 1] = newhowmany[i];
661    }
662  }
663}  /* bootweights */
664
665
666void sppermute(long n)
667{ long i, j, k;
668  for (i = 1; i <= (spp - 1); i++) {
669    k = (long)((i+1) * randum(seed));
670    j = sppord[n - 1][i];
671    sppord[n - 1][i] = sppord[n - 1][k];
672    sppord[n - 1][k] = j;
673  }
674}  /* sppermute */
675
676
677void writedata()
678{
679  /* write out one set of bootstrapped sequences */
680  long i, j, k, l, m, n, n2;
681  double x;
682  Char charstate;
683
684  sppord = (long **)Malloc(newergroups*sizeof(long *));
685  for (i = 0; i < (newergroups); i++)
686    sppord[i] = (long *)Malloc(spp*sizeof(long));
687  for (j = 1; j <= spp; j++)
688    sppord[0][j - 1] = j;
689  for (i = 1; i < newergroups; i++) {
690    for (j = 1; j <= (spp); j++)
691      sppord[i][j - 1] = sppord[i - 1][j - 1];
692  }
693  if (!justwts || permute) {
694    if (data == restsites && enzymes)
695      fprintf(outfile, "%5ld %5ld% 4ld\n", spp, newergroups, nenzymes);
696    else if (data == genefreqs)
697      fprintf(outfile, "%5ld %5ld\n", spp, newergroups);
698    else {
699      if ((data == seqs) && !(bootstrap || jackknife || permute) && xml)
700        fprintf(outfile, "<alignment>\n");
701      else
702      fprintf(outfile, "%5ld %5ld\n", spp, newersites);
703    }
704    if (data == genefreqs) {
705      for (i = 0; i < (newergroups); i++)
706        fprintf(outfile, " %3ld", alleles[factorr[newerwhere[i] - 1] - 1]);
707      putc('\n', outfile);
708    }
709  }
710  l = 1;
711  if ((!(bootstrap || jackknife || permute))
712       && ((data == seqs) || (data == restsites))) {
713    interleaved = !interleaved;
714    if (!(bootstrap || jackknife || permute) && xml)
715      interleaved = false;
716  }
717  if (interleaved)
718    m = 60;
719  else
720    m = newergroups;
721  do {
722    if (m > newergroups)
723      m = newergroups;
724    for (j = 0; j < spp; j++) {
725      n = 0;
726      if (l == 1) {
727        if (!(bootstrap || jackknife || permute) && xml) {
728          fprintf(outfile, "   <sequence");
729          switch (seq) {
730            case (dna): fprintf(outfile, " type=dna"); break;
731            case (rna): fprintf(outfile, " type=rna"); break;
732            case (protein): fprintf(outfile, " type=protein"); break;
733          }
734          fprintf(outfile, ">\n");
735          fprintf(outfile, "      <name>");
736        }
737        n2 = nmlngth-1;
738        if (!(bootstrap || jackknife || permute) && xml) {
739          while (nayme[sppord[0][j] - 1][n2] == ' ')
740            n2--;
741        }
742        for (k = 0; k <= n2; k++)
743          putc(nayme[sppord[0][j] - 1][k], outfile);
744        if (!(bootstrap || jackknife || permute) && xml)
745          fprintf(outfile, "</name>\n      <data>");
746      } else {
747        if (!(bootstrap || jackknife || permute) && xml) {
748          fprintf(outfile, "      ");
749        }
750        else {
751          for (k = 1; k <= nmlngth; k++)
752            putc(' ', outfile);
753        }
754      }
755      for (k = l - 1; k < m; k++) {
756        if (permute && j + 1 == 1)
757          sppermute(newerfactor[n]);
758        for (n2 = -1; n2 <= (newerhowmany[k] - 2); n2++) {
759          n++;
760          if (data == genefreqs) {
761            if (n > 1 && (n & 7) == 1)
762              fprintf(outfile, "\n              ");
763          x = nodef[sppord[newerfactor[n - 1] - 1][j] - 1][newerwhere[k] + n2];
764            fprintf(outfile, "%8.5f", x);
765          } else {
766            if (!(bootstrap || jackknife || permute) && xml
767                && (n > 1) && (n % 60 == 1))
768              fprintf(outfile, "\n            ");
769            else if (!interleaved && (n > 1) && (n % 60 == 1))
770                fprintf(outfile, "\n           ");
771            charstate =
772              nodep[sppord[newerfactor[n - 1] - 1][j] - 1][newerwhere[k] + n2];
773            putc(charstate, outfile);
774            if (n % 10 == 0 && n % 60 != 0)
775              putc(' ', outfile);
776          }
777        }
778      }
779      if (!(bootstrap || jackknife || permute) && xml) {
780        fprintf(outfile, "</data>\n   </sequence>\n");
781      }
782      putc('\n', outfile);
783    }
784    if (interleaved) {
785      if ((m <= newersites) && (newersites > 60))
786        putc('\n', outfile);
787      l += 60;
788      m += 60;
789    }
790  } while (interleaved && l <= newersites);
791  if ((data == seqs) && (!(bootstrap || jackknife || permute) && xml))
792    fprintf(outfile, "</alignment>\n");
793  for (i = 0; i < (newergroups); i++)
794    free(sppord[i]);
795  free(sppord);
796}  /* writedata */
797
798
799void writeweights()
800{
801  /* write out one set of post-bootstrapping weights */
802  long k, l, m, n;
803
804  l = 1;
805  if (interleaved)
806    m = 60;
807  else
808    m = newergroups;
809  do {
810    if (m > newergroups)
811      m = newergroups;
812    n = 0;
813    for (k = l - 1; k < m; k++) {
814      if (weight[k] < 10)
815        fprintf(outweightfile, "%c", (char)('0'+weight[k]));
816      else
817        fprintf(outweightfile, "%c", (char)('A'+weight[k]-10));
818      n++;
819      if (!interleaved && n > 1 && n % 60 == 1) {
820        fprintf(outweightfile, "\n");
821        if (n % 10 == 0 && n % 60 != 0)
822          putc(' ', outweightfile);
823      }
824    }
825    putc('\n', outweightfile);
826    if (interleaved) {
827      l += 60;
828      m += 60;
829    }
830  } while (interleaved && l <= newersites);
831}  /* writeweights */
832
833
834void writecategories()
835{
836  /* write out categories for the bootstrapped sequences */
837  long k, l, m, n, n2;
838  Char charstate;
839
840  l = 1;
841  if (interleaved)
842    m = 60;
843  else
844    m = newergroups;
845  do {
846    if (m > newergroups)
847      m = newergroups;
848    n = 0;
849    for (k = l - 1; k < m; k++) {
850      for (n2 = -1; n2 <= (newerhowmany[k] - 2); n2++) {
851        n++;
852        if (!interleaved && n > 1 && n % 60 == 1)
853          fprintf(outcatfile, "\n ");
854        charstate = '0' + category[newerwhere[k] + n2];
855        putc(charstate, outcatfile);
856        if (n % 10 == 0 && n % 60 != 0)
857          putc(' ', outcatfile);
858      }
859    }
860    if (interleaved) {
861      l += 60;
862      m += 60;
863    }
864  } while (interleaved && l <= newersites);
865  fprintf(outcatfile, "\n ");
866}  /* writecategories */
867
868
869void bootwrite()
870{
871  /* does bootstrapping and writes out data sets */
872  long rr, repdiv10;
873
874  if (!(bootstrap || jackknife || permute))
875    reps = 1;
876  repdiv10 = reps / 10;
877  if (repdiv10 < 1)
878    repdiv10 = 1;
879  if (progress)
880    putchar('\n');
881  for (rr = 1; rr <= (reps); rr++) {
882    bootweights();
883    if (!justwts || permute)
884      writedata();
885    if (justwts && !permute)
886      writeweights();
887    if (categories)
888      writecategories();
889    if (progress && (bootstrap || jackknife || permute)
890          && rr % repdiv10 == 0) {
891      printf("completed replicate number %4ld\n", rr);
892#ifdef WIN32
893      phyFillScreenColor();
894#endif
895    }
896  }
897  if (progress) {
898    if (justwts)
899      printf("\nOutput weights written to file \"%s\"\n\n", outweightfilename);
900    else
901      printf("\nOutput written to file \"%s\"\n\n", outfilename);
902  }
903}  /* bootwrite */
904
905
906int main(int argc, Char *argv[])
907{  /* Read in sequences or frequencies and bootstrap or jackknife them */
908#ifdef MAC
909  argc = 1;                /* macsetup("SeqBoot","");                */
910  argv[0] = "SeqBoot";
911#endif
912  init(argc,argv);
913  openfile(&infile,INFILE,"input file","r",argv[0],infilename);
914  ibmpc = IBMCRT;
915  ansi = ANSICRT;
916  doinput(argc, argv);
917  bootwrite();
918  FClose(infile);
919  if (weights)
920    FClose(weightfile);
921  if (categories) {
922    FClose(catfile);
923    FClose(outcatfile);
924  }
925  if (justwts && !permute) {
926    FClose(outweightfile);
927  }
928  else
929    FClose(outfile);
930#ifdef MAC
931  fixmacfile(outfilename);
932  if (justwts && !permute)
933    fixmacfile(outweightfilename);
934  if (categories)
935    fixmacfile(outcatfilename);
936#endif
937  printf("Done.\n\n");
938#ifdef WIN32
939  phyRestoreConsoleAttributes();
940#endif
941  return 0;
942}
Note: See TracBrowser for help on using the repository browser.