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

  1. /* geniob.c   27-3-92 genebank input/output routines for Beagle */
  2. /*** Beagle Explorer: Version 3.1  Copyright (c) 1990, 1991, 1992  Tom Ray ***/
  3.  
  4. #define INSTD aid
  5.  
  6. #include "beagle.h"
  7. #include "externb.h"
  8.  
  9. #define WritEcoS(bits)        WritEcoB(bits, mes[9])
  10.  
  11. /*
  12.  * read_head - read header from a genebank archive
  13.  */
  14.  
  15. head_t read_head(fp)
  16.     FILE *fp;
  17. {
  18.     head_t t;
  19.  
  20.     if ((fp != NULL) && !fseek(fp, 0, 0)) fread(&t, sizeof(head_t), 1, fp);
  21.     else {
  22.     fprintf(stderr, "read_head file access failed");
  23.     exit(errno);
  24.        }
  25.    if (GFormat < 0) 
  26.       GFormat = t.magic[3] -'0';    /* autoselect */
  27.    return t;
  28. }
  29.  
  30. /*
  31.  * read_indx - read the index from a genebank archive
  32.  */
  33.  
  34. indx_t *read_indx(fp, head)
  35.     FILE *fp;
  36.     head_t *head;
  37. {
  38.     indx_t *t = 0;
  39.  
  40.     if (!fseek(fp, sizeof(head_t), 0)) {
  41.     t = (indx_t *) farcalloc(head->n_alloc, sizeof(indx_t));
  42.     fread(t, sizeof(indx_t), head->n, fp);
  43.     }
  44.     else {
  45.     fprintf(stderr, "read_index file access failed");
  46.     exit(errno);
  47.          }
  48.     return t;
  49. }
  50.  
  51. /*
  52.  * find_gen - find the index of a genome in an archive by its 3 letter name
  53.  *
  54.  * will return n (number of genomes) if not found, otherwise the position
  55.  * (0 - n-1) in archive
  56.  */
  57.  
  58. I32s find_gen(indx, gen, n)
  59.     indx_t indx[];
  60.     I8s *gen;
  61.     I32s n;
  62. {
  63.     I32s i;
  64.  
  65.     for (i = 0; i < n; i++) if (!strncmp(indx[i].gen, gen, 3)) break;
  66.     return i;
  67. }
  68.  
  69. /*
  70.  * get_gen - read a genome from genebank archive and return a pointer
  71.  *     to a struct g_list containing all saved info.
  72.  *
  73.  *     fp - pointer to open archive file
  74.  *     head - archive header
  75.  *     indxn - index entry of desired genome
  76.  *     n - position of desired genome in archive
  77.  *
  78.  * reads the genome and reformats its other args into the form used
  79.  * internally by tierra. the genotype must be in archive. n can be
  80.  * determined by find_gen(). currently no error checking
  81.  */
  82.  
  83. Pgl get_gen(fp, head, indxn, n)
  84.     FILE *fp;
  85.     indx_t *indxn;
  86.     head_t *head;
  87.     I32s n;
  88. {
  89.     Pgl t = (Pgl) calloc(1, sizeof(GList));
  90.  
  91.     fseek(fp, head->g_off +
  92.       (n * head->size * (sizeof(Instruction) + sizeof(GenBits))), 0);
  93.     t->genome = (FpInst) calloc(head->size, sizeof(Instruction));
  94.     t->gbits = (FpGenB) calloc(head->size, sizeof(GenBits));
  95.     fread(t->genome, head->size * sizeof(Instruction), 1, fp);
  96.     fread(t->gbits, head->size * sizeof(GenBits), 1, fp);
  97.     t->gen.size = head->size;
  98.     strncpy(t->gen.label, indxn->gen, 3);
  99.     t->parent.size = indxn->psize;
  100.     strncpy(t->parent.label, indxn->pgen, 3);
  101.     t->bits = indxn->bits;
  102.     t->d1 = indxn->d1;
  103.     t->d2 = indxn->d2;
  104.     t->originI = indxn->originI;
  105.     t->originC = indxn->originC;
  106.     t->MaxPropPop = (float) indxn->mpp / 10000.;
  107.     t->MaxPropInst = (float) indxn->mpi / 10000.;
  108.     t->mpp_time = indxn->mppT;
  109.     t->ploidy = (indxn->pt & 0360) >> 4;
  110.     t->track = indxn->pt & 017;
  111.     return t;
  112. }
  113.  
  114. void WritEcoB(bits, buf)
  115.     I32u bits;
  116.     I8s *buf;    /* changed by DAN */
  117. {
  118.     I32u i, j;
  119.     
  120.     if(!buf) return;
  121.     sprintf(buf,"EX      TC      TP      MF      MT      MB      ");
  122.     for (i = 0, j = 0; i < 6; i++, j = 0) {
  123.     if (IsBit(bits, (I32u) (5 * i + 2)))
  124.         buf[2+(i*8)+j++] = 's';
  125.     if (IsBit(bits, (I32u) (5 * i + 3)))
  126.         buf[2+(i*8)+j++] = 'd';
  127.     if (IsBit(bits, (I32u) (5 * i + 4)))
  128.         buf[2+(i*8)+j++] = 'o';
  129.     if (IsBit(bits, (I32u) (5 * i + 5)))
  130.         buf[2+(i*8)+j++] = 'f';
  131.     if (IsBit(bits, (I32u) (5 * i + 6)))
  132.         buf[2+(i*8)+j++] = 'h';
  133.     }
  134. }
  135.  
  136. void SetBit(seed, bit, value)
  137.     I32u *seed, bit, value;
  138. {
  139.     if (value)
  140.     (*seed) |= (1 << bit);
  141.     else
  142.     (*seed) &= (~(1 << bit));
  143. }
  144.  
  145. I16s id_compare(i,j)
  146. ArgInstDef   *i, *j;
  147. {   return(i->op - j->op);
  148. }
  149.  
  150. void GetAMap(file)
  151.     I8s file[85];
  152. {   FILE  *afp;
  153.     char  data[85];
  154.     I32s  i;
  155.  
  156.     if((afp = fopen(file,"r")) == NULL)
  157.     {   fprintf(stderr,"unable to open IMapFile  - %s", file);
  158.         exit(-666);
  159.     }
  160.     fgets(data, 84, afp);
  161.     i = 0;
  162.     while (strlen(data) > 3)
  163.     {   if ((( sscanf(data,"%*[^x]x%lx%*[^\"]\"%[^\"]", &aid[i].op,
  164.             aid[i].mn)) >= 2) && ((aid[i].op >= 0) &&
  165.             (aid[i].op < INSTNUM )))
  166.         {   i++;
  167.             if (!strcmp(aid[i].mn, "nop0"))
  168.             {   Nop0 = aid[i].op;
  169.                 NopS = Nop0 + Nop1;
  170.             }
  171.             if (!strcmp(aid[i].mn, "nop1"))
  172.             {   Nop1 = aid[i].op;
  173.                 NopS = Nop0 + Nop1;
  174.             }
  175.         }
  176.         if (fgets(data, 84, afp) == NULL)
  177.             break ;
  178.     }
  179.     fclose(afp);
  180.     qsort(aid,INSTNUM, sizeof(ArgInstDef), id_compare);
  181. }
  182.