home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / TIERUN.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  17KB  |  579 lines

  1. /* tierun.c  27-3-92  makes run_info file for tierra.run, with genotypes */
  2. /*** Beagle Explorer: Version 3.1  Copyright (c) 1990, 1991, 1992  Tom Ray ***/
  3.  
  4. #include "beagle.h"
  5. #include "externb.h"
  6.  
  7. extern int  genotypes, update;
  8. extern char  directory[];
  9. char  infile[13];
  10. FILE  *inf, *souf, *gouf;
  11. HWND  witi;
  12.  
  13. struct pop_dat  totals;
  14. struct last_out  rlo;
  15. Ulong  sctime, stime, tctime, ttime;
  16.  
  17. int Lbl2Int(char  s[])
  18. {   if(s[0] == '-') return 0;
  19.     return 1 + (s[2]- 'a') + (26 * (s[1] - 'a')) + (676 * (s[0] - 'a'));
  20. }
  21.  
  22. void Int2Lbl(int  i, char  s[])
  23. {   if(!i)
  24.     {   strcpy(s,"---");
  25.         return;
  26.     }
  27.     i--;
  28.     s[0] = 'a' + i / 676;
  29.     i %= 676;
  30.     s[1] = 'a' + i / 26;
  31.     i %= 26;
  32.     s[2] = 'a' + i;
  33.     s[3] = 0;
  34. }
  35.  
  36. int gtierunc(void)
  37. {   int  i;
  38.     static int  first = 1;
  39.     HWND         whgt, whins;
  40.     TRANSACTION  *ptgt;
  41.  
  42.     if(first)
  43.     {   first = 0;
  44.         genotypes = 0;
  45.         update = 100;
  46.         sprintf(infile,"break.1");
  47.         sprintf(directory,"c:\\tierra\\td");
  48.         wind_setup();
  49.     }
  50.     while(1) {
  51.     whgt = wigen(2,4,3,55,FRDOUBLE,"tierun");
  52.     ptgt = vpreload(3,0,NULLF,NULLF,NULLF,NULLF);
  53.     for(i = 0; i <= 2; i++) (*(ptgt->def))[i].use_ext = TRUE;
  54.     vdeffld(ptgt,0,STRING,0,1,"directory  =",-1,40,NULL,directory,NO);
  55.     vdeffld(ptgt,1,STRING,1,1,"input file =",-1,12,NULL,infile,NO);
  56.     vdeffld(ptgt,2,INTEGER,2,1,"update frequency   =",-1,5,NULL,&update,NO);
  57.     ptgt->fld_valfn = temp_info_chk_g;
  58.     whins = wiinv(23,13,
  59.         "\x1e\x1f, or Enter to select; Esc to exit; Ctrl-Enter to run");
  60.     visible(whgt,YES,YES);
  61.     if(DWSUCCESS == vread(ptgt,whgt,YES))
  62.     {   vdelete(whgt,NONE); vdelete(whins,NONE);
  63.         gtierun();
  64.     }
  65.     else
  66.     {   vdelete(whgt,NONE); vdelete(whins,NONE);
  67.         return 1;
  68.     }
  69.     }
  70.     vdelete(whins,NONE);
  71.     return 1;
  72. }
  73.  
  74. int temp_info_chk_g(TRANSACTION  *tp)
  75. {   char  path[80];
  76.     int   success;
  77.  
  78.     if(tp->cur_fld == 0)
  79.     {   getcwd(path,79);
  80.         success = chdir(directory);
  81.         chdir(path);
  82.         if(success)
  83.         {   wierror(2,45,"directory not found");
  84.             return FALSE;
  85.         }
  86.         return TRUE;
  87.     }
  88.     if(tp->cur_fld == 1)
  89.     {   sprintf(path,"%s\\%s", directory, infile);
  90.         inf = fopen(path,"r");
  91.         if(inf == NULL)
  92.         {   wierror(2,45,"file not found");
  93.             return FALSE;
  94.         }
  95.         fclose(inf);
  96.         return TRUE;
  97.     }
  98.     return TRUE;
  99. }
  100.  
  101. void gtierun(void)
  102. {   int  first = 1, upcount = update;
  103.     struct tnode  *uroot = NULL, *troot = NULL;
  104.     Ulong c = 0;
  105.     char  data[81];
  106.     char   bifile[13];
  107.     int    binum = 1;
  108.  
  109.     sctime = stime = tctime = ttime = 0;
  110.     rlo.time = rlo.ctime = rlo.size = 0;
  111.     witi = wigen(7,30,2,19,FRDOUBLE,"time");
  112.     sscanf(infile,"%[^.]", bifile);
  113.     sprintf(data,"%s\\%s", directory, infile);
  114.     inf = fopen(data,"r");
  115.     if(inf == NULL)
  116.     {   wierror(2,45,"input file not found");
  117.         return ;
  118.     }
  119.     sprintf(data,"%s\\run_info", directory);
  120.     souf = fopen(data,"w");
  121.     sprintf(data,"%s\\run_info.g", directory);
  122.     gouf = fopen(data,"w");
  123.     for(;;)
  124.     {   if(fgets(data,80,inf) == NULL)
  125.         {   fclose(inf);
  126.             binum++;
  127.             sprintf(data,"%s/%s.%d", directory, bifile, binum);
  128.             inf = fopen(data,"r");
  129.             if(inf == NULL)
  130.                 break ;
  131.             if(fgets(data,80,inf) == NULL)
  132.                 break ;
  133.         }
  134.         if(!t_read(data, &rlo, &first, &genotypes))
  135.         {   StartFragment(data,&rlo,&uroot,&troot,c);
  136.             sctime = rlo.ctime; stime = rlo.time;
  137.             if(!c)
  138.             {   tctime = sctime; ttime = stime; }
  139.             while(rlo.ctime > upcount)
  140.                 upcount += update;
  141.             continue;
  142.         }
  143.         if(!(c%100LU))
  144.             vatputf(witi,0,1,"%6lu,%06lu", rlo.ctime, rlo.time);
  145.         c++;
  146.         if(update && rlo.ctime >= upcount)
  147.         {   upcount += update;
  148.             uroot = DoUpdate(uroot,update);
  149.             sctime = rlo.ctime; stime = rlo.time;
  150.         }
  151.         if(update) uroot = AddTree(uroot,&rlo);
  152.         troot = AddTree(troot,&rlo);
  153.     }
  154.     vdelete(witi,NONE);
  155.     fclose(inf);
  156.     if(update) uroot = DoUpdate(uroot,update);
  157.     FreeTTree(uroot);
  158.     sctime = tctime; stime = ttime;
  159.     troot = DoUpdate(troot,0);
  160.     FreeTTree(troot);
  161.     fclose(souf); fclose(gouf);
  162.     if(!genotypes)
  163.     {   sprintf(data,"del %s\\run_info.g", directory);
  164.         system(data);
  165.     }
  166. }
  167.  
  168. struct tnode * AddTree(struct tnode  *p, struct last_out  *lo)
  169. {   int  i, j, osize;
  170.     struct pop_dat  *pd;
  171.  
  172.     if(p == NULL)
  173.     {   if(lo->bd == 'd')
  174.         {   wierror(2,45,"new node is a death, exiting");
  175.             vexit(0);
  176.         }
  177.         p = (struct tnode  *) calloc(1,sizeof(struct tnode));
  178.         if(p == NULL)
  179.         {   wierror(2,45,"calloc failure, exiting");
  180.             vexit(0);
  181.         }
  182.         else
  183.             vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  184.         p->size = lo->size;
  185.         p->sd.max = p->sd.end = 1;
  186.         p->sd.min = p->sd.beg = 0;
  187.         if(genotypes)
  188.         {   i = Lbl2Int(lo->label);
  189.             p->gsize = i + 1;
  190.             p->g = (struct pop_dat  *)
  191.                 calloc(p->gsize,sizeof(struct pop_dat));
  192.             if(p->g == NULL)
  193.             {   wierror(2,45,"calloc failure, exiting");
  194.                 vexit(0);
  195.             }
  196.             else
  197.                 vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  198.             (p->g + i)->end = (p->g + i)->max = 1;
  199.         }
  200.         p->l = p->r = NULL;
  201.     }
  202.     else if(lo->size < p->size)
  203.         p->l = AddTree(p->l,lo);
  204.     else if(lo->size > p->size)
  205.         p->r = AddTree(p->r,lo);
  206.     else
  207.     {   if(genotypes)
  208.         {   i = Lbl2Int(lo->label);
  209.             if(i >= p->gsize)
  210.             {   osize = p->gsize;
  211.                 p->gsize = i + 5;
  212.                 if(p->g == NULL)
  213.                     p->g = (struct pop_dat  *)
  214.                         calloc(p->gsize, sizeof(struct pop_dat));
  215.                 else
  216.                     p->g = (struct pop_dat  *)
  217.                         realloc(p->g, p->gsize * sizeof(struct pop_dat));
  218.                 if(p->g == NULL)
  219.                 {   wierror(2,45,"realloc failure, exiting");
  220.                     vexit(0);
  221.                 }
  222.                 else
  223.                     vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  224.                 for(j = osize; j < p->gsize; j++)
  225.                 {   pd = p->g + j;
  226.                     pd->max = pd->min = pd->beg = pd->end = 0;
  227.                 }
  228.             }
  229.         }
  230.         if(lo->bd == 'b')
  231.         {   p->sd.end++;
  232.             if(p->sd.end > p->sd.max)
  233.                 p->sd.max = p->sd.end;
  234.             if(genotypes)
  235.             {   pd = p->g + i;
  236.                 pd->end++;
  237.                 if(pd->end > pd->max)
  238.                     pd->max = pd->end;
  239.             }
  240.         }
  241.         else
  242.         {   p->sd.end--;
  243.             if(p->sd.end < p->sd.min)
  244.                 p->sd.min = p->sd.end;
  245.             if(genotypes)
  246.             {   pd = p->g + i;
  247.                 pd->end--;
  248.                 if(pd->end < pd->min)
  249.                     pd->min = pd->end;
  250.             }
  251.         }
  252.     }
  253.     return p;
  254. }
  255.  
  256. struct snode * Tree2Tree(struct tnode  *t, struct snode  *s, int  index)
  257. {   int  i;
  258.  
  259.     if(t != NULL)          /* sort tnode tree into snode tree */
  260.     {   s = Tree2Tree(t->l,s,index);
  261.         if(index) for(i = 0; i < t->gsize; i++)
  262.         {  if((t->g + i)->max > 0)
  263.                s = AddMaxTree(t,i,s);
  264.         }
  265.         else s = AddMaxTree(t,-1,s);
  266.         s = Tree2Tree(t->r,s,index);
  267.     }
  268.     return s;
  269. }
  270.  
  271. struct snode * AddMaxTree(struct tnode  *t, int  index, struct snode  *s)
  272. {   int  i;
  273.     long  osiz;
  274.  
  275.     if(s == NULL)
  276.     {   s = (struct snode  *) calloc(1,sizeof(struct snode));
  277.         if(s == NULL)
  278.         {   wierror(2,45,"calloc failure, exiting");
  279.             vexit(0);
  280.         }
  281.         else
  282.             vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  283.         if(index == -1) s->max = t->sd.max;
  284.         else s->max = (t->g + index)->max;
  285.         s->num = 1;
  286.         s->gsiz = 1;
  287.         s->g = (struct gene_dat  *) calloc(s->gsiz,sizeof(struct gene_dat));
  288.         if(s->g == NULL)
  289.         {   wierror(2,45,"calloc failure, exiting");
  290.             vexit(0);
  291.         }
  292.         else
  293.             vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  294.         s->g->t = t;
  295.         s->g->index = index;
  296.         s->l = s->r = NULL;
  297.     }
  298.     else if((index == -1 && (t->sd.max < s->max)) ||
  299.         (index != -1 && ((t->g + index)->max < s->max)))
  300.         s->l = AddMaxTree(t,index,s->l);
  301.     else if((index == -1 && (t->sd.max > s->max)) ||
  302.         (index != -1 && ((t->g + index)->max > s->max)))
  303.         s->r = AddMaxTree(t,index,s->r);
  304.     else
  305.     {   s->num++;
  306.         if(s->num > s->gsiz)
  307.         {   osiz = s->gsiz;
  308.             s->gsiz *= 1.1;
  309.             if(s->gsiz == osiz) s->gsiz++;
  310.             s->g = (struct gene_dat  *)
  311.                 realloc(s->g, s->gsiz * sizeof(struct gene_dat));
  312.             if(s->g == NULL)
  313.             {   wierror(2,45,"realloc failure, exiting");
  314.                 vexit(0);
  315.             }
  316.             else
  317.                 vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  318.             for(i = osiz; i < s->gsiz; i++)
  319.             {   (s->g + i)->t = NULL;
  320.                 (s->g + i)->index = -1;
  321.             }
  322.         }
  323.         i = s->num - 1;
  324.         (s->g + i)->t = t;
  325.         (s->g + i)->index = index;
  326.     }
  327.     return s;
  328. }
  329.  
  330. void OutSearchTree(struct snode  *s, FILE  *ouf, int  *out_num, int  update)
  331. {   int   i, in;
  332.     char  lbl[4];
  333.     struct pop_dat  *p;
  334.  
  335.     if(update && *out_num <= 0) return;
  336.     if(s != NULL)
  337.     {   OutSearchTree(s->r, ouf, out_num, update);
  338.         if(s->max) for(i = 0; i < s->num; i++)
  339.         {   if(update && *out_num <= 0) return;
  340.             in = (s->g + i)->index;
  341.             if(in == -1)
  342.             {   strcpy(lbl,"---");
  343.                 p = &((s->g + i)->t->sd);
  344.             }
  345.             else
  346.             {   Int2Lbl(in,lbl);
  347.                 p = (s->g + i)->t->g + in;
  348.             }
  349.             fprintf(ouf,"%04ld%s    %ld    %ld    %ld    %ld\n",
  350.                 (s->g + i)->t->size, lbl, p->min, p->max, p->end, p->beg);
  351.             if(update) (*out_num)--;
  352.         }
  353.         OutSearchTree(s->l, ouf, out_num, update);
  354.     }
  355. }
  356.  
  357. void CountTree(struct snode  *s, int  *num)
  358. {   if(s != NULL)
  359.     {   if(s->max) *num += s->num;
  360.         CountTree(s->l,num);
  361.         CountTree(s->r,num);
  362.     }
  363. }
  364.  
  365. void OutUpdate(struct snode  *s, FILE  *ouf, int  update)
  366. {   int  num_sp = 0, out_num = 55;
  367.  
  368.     CountTree(s, &num_sp);
  369.     if(update && num_sp > 55) num_sp = 55;
  370.     out_num = num_sp;
  371.     fprintf(ouf,"num_sp = %d  sctime = %ld  stime = %ld  update = %d\n",
  372.         num_sp, sctime, stime, update);
  373.     fprintf(ouf,"classes    min    max    end    beg\n");
  374.     OutSearchTree(s, ouf, &out_num, update);
  375. }
  376.  
  377. void FreeSTree(struct snode  *p)
  378. {   if(p != NULL)
  379.     {   FreeSTree(p->l);
  380.         FreeSTree(p->r);
  381.         free(p->g);
  382.         free(p);
  383.     }
  384. }
  385.  
  386. void FreeTTree(struct tnode  *p)
  387. {   if(p != NULL)
  388.     {   FreeTTree(p->l);
  389.         FreeTTree(p->r);
  390.         free(p->g);
  391.         free(p);
  392.     }
  393. }
  394.  
  395. struct tnode * oldPruneTree(struct tnode  *p, struct tnode  *root)
  396. {   if(p != NULL)
  397.     {   if(p->sd.end > 0)
  398.             root = AddPrunedTree(root,p);
  399.         oldPruneTree(p->l,root);
  400.         oldPruneTree(p->r,root);
  401.     }
  402.     return root;
  403. }
  404.  
  405. void PruneTree(struct tnode  *p)
  406. {   int  i;
  407.     struct pop_dat  *pd;
  408.  
  409.     if(p != NULL)
  410.     {   if(p->sd.end <= 0)
  411.         {   p->sd.max = p->sd.min = p->sd.beg = p->sd.end = 0;
  412.             if(genotypes)
  413.             {   free(p->g);
  414.                 p->g = NULL;
  415.                 p->gsize = 0;
  416.             }
  417.         }
  418.         else
  419.             p->sd.max = p->sd.beg = p->sd.end; p->sd.min = 0;
  420.         if(genotypes) for(i = 0; i < p->gsize; i++)
  421.         {   pd = p->g + i;
  422.             if(pd->end <= 0)
  423.                 pd->max = pd->min = pd->beg = pd->end = 0;
  424.             else
  425.                 pd->max = pd->beg = pd->end; pd->min = 0;
  426.         }
  427.         PruneTree(p->l);
  428.         PruneTree(p->r);
  429.     }
  430. }
  431.  
  432. struct tnode * AddPrunedTree(struct tnode  *r, struct tnode *p)
  433. {   int  i;
  434.     struct pop_dat  *d;
  435.  
  436.     if(r == NULL)
  437.     {   r = (struct tnode  *) calloc(1,sizeof(struct tnode));
  438.         if(r == NULL)
  439.         {   wierror(2,45,"calloc failure, exiting");
  440.             vexit(0);
  441.         }
  442.         else
  443.             vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  444.         *r = *p;
  445.         r->sd.beg = r->sd.max = r->sd.end; r->sd.min = 0;
  446.         if(genotypes) for(i = 0; i < r->gsize; i++)
  447.         {   d = r->g + i;
  448.             if(d->end <= 0)
  449.                 d->beg = d->max = d->min = d->end = 0;
  450.             else
  451.             {   d->beg = d->max = d->end; d->min = 0;
  452.             }
  453.         }
  454.         r->l = r->r = NULL;
  455.     }
  456.     else if(p->size < r->size)
  457.         r->l = AddPrunedTree(r->l,p);
  458.     else if(p->size > r->size)
  459.         r->r = AddPrunedTree(r->r,p);
  460.     else if(p->size == r->size)
  461.         wierror(5,45,"size repeated in pruned tree");
  462.     return r;
  463. }
  464.  
  465. struct tnode * DoUpdate(struct tnode  *p, int  update)
  466. {   struct snode  *sroot = NULL;
  467.  
  468.     sroot = Tree2Tree(p,sroot,0);
  469.     OutUpdate(sroot, souf, update);
  470.     FreeSTree(sroot);
  471.     vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  472.     sroot = NULL;
  473.     sroot = Tree2Tree(p,sroot,1);
  474.     OutUpdate(sroot, gouf, update);
  475.     FreeSTree(sroot);
  476.     vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  477.     if(!update) return p;
  478.     PruneTree(p);
  479.     return p;
  480. }
  481.  
  482. struct tnode * AddFragTree(struct tnode *p, long size, char lbl[], long end)
  483. {   int  i, j;
  484.     long  osize;
  485.  
  486.     if(p == NULL)
  487.     {   p = (struct tnode  *) calloc(1,sizeof(struct tnode));
  488.         if(p == NULL)
  489.         {   wierror(2,45,"calloc failure, exiting");
  490.             vexit(0);
  491.         }
  492.         else
  493.             vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  494.         p->size = size;
  495.         p->sd.beg = p->sd.end = p->sd.max = end;
  496.         if(genotypes)
  497.         {   i = Lbl2Int(lbl);
  498.             p->gsize = i + 1;
  499.             p->g = (struct pop_dat  *) calloc(p->gsize,sizeof(struct pop_dat));
  500.             if(p->g == NULL)
  501.             {   wierror(2,45,"calloc failure, exiting");
  502.                 vexit(0);
  503.             }
  504.             else
  505.                 vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  506.             (p->g + i)->beg = (p->g + i)->end = (p->g + i)->max = end;
  507.         }
  508.         p->l = p->r = NULL;
  509.     }
  510.     else if(size < p->size)
  511.         p->l = AddFragTree(p->l,size,lbl,end);
  512.     else if(size > p->size)
  513.         p->r = AddFragTree(p->r,size,lbl,end);
  514.     else
  515.     {   if(genotypes)
  516.         {   i = Lbl2Int(lbl);
  517.             if(i >= p->gsize)
  518.             {   osize = p->gsize;
  519.                 p->gsize = i + 5;
  520.                 p->g = (struct pop_dat  *)
  521.                     realloc(p->g, p->gsize * sizeof(struct pop_dat));
  522.                 if(p->g == NULL)
  523.                 {   wierror(2,45,"realloc failure, exiting");
  524.                     vexit(0);
  525.                 }
  526.                 else
  527.                     vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  528.                 for(j = osize; j < p->gsize; j++)
  529.                     (p->g + j)->max = (p->g + j)->min =
  530.                     (p->g + j)->beg = (p->g + j)->end = 0;
  531.             }
  532.             (p->g + i)->max = (p->g + i)->beg = (p->g + i)->end = end;
  533.         }
  534.         p->sd.max += end; p->sd.end += end; p->sd.beg += end;
  535.     }
  536.     return p;
  537. }
  538.  
  539. void ZeroTree(struct tnode  *p)
  540. {   int  i;
  541.  
  542.     if(p != NULL)
  543.     {   ZeroTree(p->l);
  544.         ZeroTree(p->r);
  545.         p->sd.end = 0;
  546.         if(genotypes)
  547.             for(i = 0; i < p->gsize; i++)
  548.                 (p->g + i)->end = 0;
  549.     }
  550. }
  551.  
  552. void StartFragment(char  data[], struct last_out  *lo,
  553.     struct tnode  **uroot, struct tnode  **troot, int  c)
  554. {   int  i, lnum_sp;
  555.     long  size, end;
  556.     char  ldata[81], lbl[4];
  557.  
  558.     if(update && c) *uroot = DoUpdate(*uroot, update);
  559.     if(c)
  560.     {   FreeTTree(*uroot);
  561.         vatputf(witi,1,1,"coreleft = %6lu", coreleft());
  562.         *uroot = NULL;
  563.         ZeroTree(*troot);
  564.     }
  565.     sscanf(data,"%*s%*s%d%*s%*s%lu%*s%*s%lu", &lnum_sp, &lo->ctime, &lo->time);
  566.     for(i = 0; i < lnum_sp; i++)
  567.     {   fgets(ldata,80,inf);
  568.         sscanf(ldata,"%ld%s%ld", &size, lbl, &end);
  569.         if(!i)
  570.     {   if(!strcmp(lbl,"---"))
  571.                 genotypes = 0;
  572.             else
  573.                 genotypes = 1;
  574.         }
  575.         *uroot = AddFragTree(*uroot,size,lbl,end);
  576.         *troot = AddFragTree(*troot,size,lbl,end);
  577.     }
  578. }
  579.