Exemple #1
0
/* Construct the molecule data from either: the builtins; or from
   the (one) .pdb file specified with -molecule.
 */
static void
load_molecules (ModeInfo *mi)
{
  molecule_configuration *mc = &mcs[MI_SCREEN(mi)];
  int wire = cur_wire;

  if (!molecule_str || !*molecule_str ||
      !strcmp(molecule_str, "(default)"))	/* do the builtins */
    {
      int i;
      mc->nmolecules = countof(builtin_pdb_data);
      if (!mols) {
        mols = (molecule *) calloc (sizeof (molecule), mc->nmolecules);
        for (i = 0; i < mc->nmolecules; i++)
          {
            char name[100];
            (void) sprintf (name, "<builtin-%d>", i);
            parse_pdb_data (&mols[i], builtin_pdb_data[i], name, 1);
            generate_molecule_formula (&mols[i]);
            insert_vertical_whitespace ((char *) mols[i].label);
          }
      }
      mc->molecules = mols;
    }
  else						/* Load a file */
    {
      int i = 0;
      mc->nmolecules = 1;
      if (!mols) {
        mols = (molecule *) calloc (sizeof (molecule), mc->nmolecules);
        parse_pdb_file (&mols[i], molecule_str);
        generate_molecule_formula (&mols[i]);
        insert_vertical_whitespace ((char *) mols[i].label);

        if ((wire || !do_atoms) &&
            !do_labels &&
            mols[i].nbonds == 0)
          {
            /* If we're not drawing atoms (e.g., wireframe mode), and
               there is no bond info, then make sure labels are turned on,
               or we'll be looking at a black screen... */
            (void) fprintf (stderr, "molecule: no bonds: turning -label on.\n");
            do_labels = 1;
          }
       }
       mc->molecules = mols;
    }
}
/* Construct the molecule data from either: the builtins; or from
   the (one) .pdb file specified with -molecule.
 */
static void
load_molecules (ModeInfo *mi)
{
  molecule_configuration *mc = &mcs[MI_SCREEN(mi)];
  int wire = MI_IS_WIREFRAME(mi);
  int i;

  mc->nmolecules = 0;
  if (molecule_str && *molecule_str && 
      strcmp(molecule_str, "(default)"))	/* try external PDB files */
    {
      /* The -molecule option can point to a .pdb file, or to
         a directory of them.
      */
      struct stat st;
      int nfiles = 0;
      int list_size = 0;
      char **files = 0;
      int molecule_ctr;

      if (!stat (molecule_str, &st) &&
          S_ISDIR (st.st_mode))
        {
          char buf [255];
          DIR *pdb_dir;
          struct dirent *dentry;

          pdb_dir = opendir (molecule_str);
          if (! pdb_dir)
            {
              sprintf (buf, "%.100s: %.100s", progname, molecule_str);
              perror (buf);
              exit (1);
            }

          if (verbose_p)
            fprintf (stderr, "%s: directory %s\n", progname, molecule_str);

          nfiles = 0;
          list_size = 100;
          files = (char **) calloc (sizeof(*files), list_size);

          while ((dentry = readdir (pdb_dir)))
            {
              int L = strlen (dentry->d_name);
              if (L > 4 && !strcasecmp (dentry->d_name + L - 4, ".pdb"))
                {
                  char *fn;
                  if (nfiles >= list_size-1)
                    {
                      list_size = (list_size + 10) * 1.2;
                      files = (char **)
                        realloc (files, list_size * sizeof(*files));
                      if (!files)
                        {
                        OOM:
                          fprintf (stderr, "%s: out of memory (%d files)\n",
                                   progname, nfiles);
                          exit (1);
                        }
                    }

                  fn = (char *) malloc (strlen (molecule_str) + L + 10);
                  if (!fn) goto OOM;
                  strcpy (fn, molecule_str);
                  if (fn[strlen(fn)-1] != '/') strcat (fn, "/");
                  strcat (fn, dentry->d_name);
                  files[nfiles++] = fn;
                  if (verbose_p)
                    fprintf (stderr, "%s: file %s\n", progname, fn);
                }
            }
          closedir (pdb_dir);

          if (nfiles == 0)
            fprintf (stderr, "%s: no .pdb files in directory %s\n",
                     progname, molecule_str);
        }
      else
        {
          files = (char **) malloc (sizeof (*files));
          nfiles = 1;
          files[0] = strdup (molecule_str);
          if (verbose_p)
            fprintf (stderr, "%s: file %s\n", progname, molecule_str);
        }

      mc->nmolecules = nfiles;
      mc->molecules = (molecule *) calloc (sizeof (molecule), mc->nmolecules);
      molecule_ctr = 0;
      for (i = 0; i < mc->nmolecules; i++)
        {
          if (verbose_p)
            fprintf (stderr, "%s: reading %s\n", progname, files[i]);
          if (!parse_pdb_file (&mc->molecules[molecule_ctr], files[i]))
            {
              if ((wire || !do_atoms) &&
                  !do_labels &&
                  mc->molecules[molecule_ctr].nbonds == 0)
                {
                  /* If we're not drawing atoms (e.g., wireframe mode), and
                     there is no bond info, then make sure labels are turned
                     on, or we'll be looking at a black screen... */
                  fprintf (stderr, "%s: %s: no bonds: turning -label on.\n",
                           progname, files[i]);
                  do_labels = 1;
                }
              free (files[i]);
	      files[i] = 0;
              molecule_ctr++;
	    }
        }

      free (files);
      files = 0;
      mc->nmolecules = molecule_ctr;
    }

  if (mc->nmolecules == 0)	/* do the builtins if no files */
    {
      mc->nmolecules = countof(builtin_pdb_data);
      mc->molecules = (molecule *) calloc (sizeof (molecule), mc->nmolecules);
      for (i = 0; i < mc->nmolecules; i++)
        {
          char name[100];
          sprintf (name, "<builtin-%d>", i);
          if (verbose_p) fprintf (stderr, "%s: reading %s\n", progname, name);
          parse_pdb_data (&mc->molecules[i], builtin_pdb_data[i], name, 1);
        }
    }

  for (i = 0; i < mc->nmolecules; i++)
    {
      generate_molecule_formula (&mc->molecules[i]);
      insert_vertical_whitespace ((char *) mc->molecules[i].label);
    }
}