Esempio n. 1
0
void read_checkpoint ( char *filename, int *gen, multipop **mpop )
{
     FILE *f;
     char *buffer;
     ephem_const **eind;
     int random_state_bytes;
     int i, j;
     char *rand_state;

     /* miscellaneous buffer for reading. */
     buffer = (char *)MALLOC ( MAXCHECKLINELENGTH );

     /* open the file. */
     f = fopen ( filename, "rb" );
     if ( f == NULL )
     {
          error ( E_FATAL_ERROR, "couldn't read checkpoint \"%s\".",
                 filename );
     }

     oprintf ( OUT_SYS, 30, "reading from checkpoint \"%s\".\n",
              filename );
     
     /** confirm the magic word that starts every checkpoint file. **/
     fgets ( buffer, MAXCHECKLINELENGTH, f );
     if ( strcmp ( buffer, CK_MAGIC ) )
          error ( E_FATAL_ERROR,
                 "\"%s\" is not a lil-gp v1.0 checkpoint file.", filename );

     /* skip the human-readable id line. */
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "id line: %s", buffer );
#endif

     /** read and print the timestamp. **/
     fscanf ( f, "%*s " );
     fgets ( buffer, MAXCHECKLINELENGTH, f );
     /* chop the newline. */
     buffer[strlen(buffer)-1] = 0;
     oprintf ( OUT_SYS, 30, "    checkpoint timestamp: [%s].\n", buffer );

     /* skip the "section: global" line. */
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "should be global section: %s", buffer );
#endif

     /* read the generation number. */
     fscanf ( f, "%*s %d\n", gen );

     /** read the random number state encoded as a string of hex chars. **/

     /* first read the length. */
     fscanf ( f, "%*s %d ", &random_state_bytes );
#ifdef DEBUG
     fprintf ( stderr, "%d random state bytes.\n", random_state_bytes );
#endif
     /* allocate the buffer. */
     rand_state = (char *)MALLOC ( random_state_bytes+1 );
     /* read the hex data into the buffer. */
     read_hex_block ( rand_state, random_state_bytes, f );
     /* set the state. */
     random_set_state ( rand_state );
     /* free the buffer. */
     FREE ( rand_state );
     /* slurp the newline character following the hex data. */
     fgetc ( f );

     /** skip the "section: parameter" line. **/
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "should be parameter section: %s", buffer );
#endif
     /* read the parameter database. */
     read_parameter_database ( f );

     /* make internal copies of function set(s). */
     if ( app_build_function_sets() ) 
          error ( E_FATAL_ERROR, "app_build_function_sets() failure." );

     /** skip the "section: erc" line. **/
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "should be erc section: %s", buffer );
#endif
     
     /* read the list of ephemeral constants, and index them */
     eind = read_ephem_list ( f );

     /** skip the "section: erc" line. **/
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "should be population section: %s", buffer );
#endif

     /** read the population **/

     /* allocate memory. */
     *mpop = (multipop *)MALLOC ( sizeof ( multipop ) );
     /* read number of subpops. */
     fscanf ( f, "%*s %d\n", &((**mpop).size) );
     /* allocate subpop list. */
     (**mpop).pop = (population **)MALLOC ( (**mpop).size *
                                           sizeof ( population * ) );
     for ( i = 0; i < (**mpop).size; ++i )
     {
	  /** skip each "subpop: #" line. **/
	  fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
	  printf ( "should be subpop %d: %s", i, buffer );
#endif
	  /* read the population. */
          (**mpop).pop[i] = read_population ( eind, f );
     }

     /** skip the "section: application" line. **/
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "should be application section: %s", buffer );
#endif
     /* read application-specific stuff. */
     app_read_checkpoint ( f );

     /** skip the "section: statistics" line. **/
     fgets ( buffer, MAXCHECKLINELENGTH, f );
#ifdef DEBUG
     printf ( "should be statistics section: %s", buffer );
#endif
     /* read the statistics. */
     read_stats_checkpoint ( *mpop, eind, f );

     /* close'n'free. */
     FREE ( eind );
     FREE ( buffer );
     fclose ( f );
     
     oprintf ( OUT_SYS, 30, "population read from checkpoint \"%s\".\n",
              filename );

}
Esempio n. 2
0
int main(int argc, char **argv)
{
  extern int   optind, opterr;
  extern char *optarg;
  int          optchar;
  char *treefile_name = NULL, *genomefile_name = NULL, *outfile_basename = NULL, g_outfile_name[FILENAME_MAX];
  FILE *treefile, *genomefile, *outfile;
  long generation, g, num_trees, i;
  int ret_code;

  genomefile_name = NULL;
  treefile_name = NULL;
  outfile_basename = NULL;
  generation = -1;
  phyl_init_tree(&phyltree);
  while ((optchar = getopt(argc, argv, "t:g:o:h")) != -1)
  {
    switch (optchar)
    {
    case 'g':
      genomefile_name = optarg;
      break;
    case 't':
      treefile_name = optarg;
      break;
    case 'o':
      outfile_basename = optarg;
      break;
    case 'h':
      printf("distcorr -- a hack to visualize correlation between\n");
      printf("    phylogenetic and edit distances\n");
      printf("Usage of command line options:\n");
      printf("-g <filename>: Specify genome file\n");
      printf("-t <filename>: Specify tree file\n");
      printf("-o <outfile>: specify output file basename:");
      printf("-h: Print this info and exit\n");
      exit (EXIT_SUCCESS);
    }
  }
  if (treefile_name == NULL)
  {
    fprintf(stderr, "no tree file specified -- exit\n");
    exit (EXIT_FAILURE);
  }
  if ((treefile = fopen(treefile_name, "r")) == NULL)
  {
    fprintf(stderr, "failed to open tree file \"%s\" -- exit\n", treefile_name);
    exit (EXIT_FAILURE);
  }
  if (genomefile_name == NULL)
  {
    fprintf(stderr, "no tree file specified -- exit\n");
    exit (EXIT_FAILURE);
  }
  if ((genomefile = fopen(genomefile_name, "r")) == NULL)
  {
    fprintf(stderr, "failed to open genome file \"%s\" -- exit\n", genomefile_name);
    exit (EXIT_FAILURE);
  }
  while (!feof(treefile) && !ferror(treefile) && !feof(genomefile) && !ferror(genomefile))
  {
    get_line(buf, 256, treefile);
    if (feof(treefile) || ferror(treefile))
      break;
    if (buf[0] != 'g')
    {
      fprintf(stderr, "error in header of treefile after generation %ld -- exit\n", generation);
      exit (EXIT_FAILURE);
    }
    generation = strtol(buf + 1, (char **) NULL, 10);
    get_line(buf, 256, genomefile);
    if (feof(genomefile) || ferror(genomefile))
      break;
    if (buf[0] != 'g')
    {
      fprintf(stderr, "error in header of genomefile after generation %ld -- exit\n", g);
      exit (EXIT_FAILURE);
    }
    g = strtol(buf + 1, (char **) NULL, 10);
    if (g != generation)
    {
      fprintf(stderr, "treefile: g=%ld, genomefile: g=%ld -- out of sync\n", generation, g);
      break;
    }
    get_line(buf, 256, treefile);
    num_trees = strtol(buf, NULL, 10);
    if (num_trees < 1)
    {
      fprintf(stderr, "%ld trees in treefile at generation %ld\n", num_trees, generation);
      break;
    }
    if (num_trees > 1)
    {
      fprintf(stderr, "distcorr: multiple trees in treefile\n");
      for (i = 0; i < num_trees; i++)
      {
	if ((ret_code = phyl_read_tree(treefile, &phyltree)) < 0)
	  fprintf(stderr, "error #%d while reading tree #%ld of generation %ld\n", ret_code, i, generation);
	phyl_free_tree(&phyltree);
      }
      read_population(genomefile, g);
      free_population();
      continue;
    }
    if ((ret_code = phyl_read_tree(treefile, &phyltree)) < 0)
    {
      fprintf(stderr, "error #%d while reading tree #%ld of generation %ld\n", ret_code, i, generation);
      break;
    }
    if ((ret_code = read_population(genomefile, g)) < 0)
    {
      fprintf(stderr, "error %d reading genomes of generation %ld\n", ret_code, g);
      phyl_free_tree(&phyltree);
      break;
    }
    if (outfile_basename)
    {
      sprintf(g_outfile_name, "%s-%06ld-dc.gpd", outfile_basename, generation);
      if ((outfile = fopen(g_outfile_name, "w")) == NULL)
      {
        fprintf(stderr, "failed to open \"%s\" for output -- exit\n", g_outfile_name);
        exit (EXIT_FAILURE);
      }
    }
    else
      outfile = stdout;
    ret_code = distcorr(outfile, generation);
    if (ret_code < 0)
      fprintf(stderr, "error %d in distcorr\n", ret_code);
    if (outfile_basename)
      fclose(outfile);
    free_population();
    phyl_free_tree(&phyltree);
  }
  fclose(treefile);
  fclose(genomefile);
  return (EXIT_SUCCESS);
}