A possibel wrong coding in Parameters::read_charmm_parameter_file(char *)

From: Qikai Li (qkli_at_iccas.ac.cn)
Date: Wed Apr 05 2006 - 10:25:56 CDT

Hi, all

When I browse the NAMD source code, I've noticed that when the option
for reading CHARMM-style parameter file in on, you will likely to get an
empty tree rather than the expected.
The following is the snapshot of the code segment. Here, we have two
variables to play around, it's the variable skipline that results in
unexpected behavior.
For example, if we have a valid key, for example, "bond", then we set
both variables:
par_type=1;
skipline=1;

However, in following line parsing part, we use condition:
if ( (par_type != 0) && (!skipline) )

Thus, a valid "bond" item is simply skipped. Similarly for other types
of items, in the end we will have an empty tree!

The same is true for NAMD version 2.6b1.

I hope I'm wrong.

Qikai LI
MSE of GA Tech.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Parameters.C, @ line 432
void Parameters::read_charmm_parameter_file(char *fname)

{
  int par_type=0; // What type of parameter are we currently
                           // dealing with? (vide infra)
  int skipline; // skip this line?
  char buffer[512]; // Buffer to store each line of the file
  char first_word[512]; // First word of the current line
  FILE *pfile; // File descriptor for the parameter file

  /* Check to make sure that we haven't previously been told */
  /* that all the files were read */
  if (AllFilesRead)
  {
    NAMD_die("Tried to read another parameter file after being told that
all files were read!");
  }

  /* Try and open the file */
  if ( (pfile = fopen(fname, "r")) == NULL)
  {
    char err_msg[256];

    sprintf(err_msg, "UNABLE TO OPEN CHARMM PARAMETER FILE %s\n", fname);
    NAMD_die(err_msg);
  }

  /* Keep reading in lines until we hit the EOF */
  while (NAMD_read_line(pfile, buffer) != -1)
  {
    /* Get the first word of the line */
    NAMD_find_first_word(buffer, first_word);
    skipline=0;

    /* First, screen out things that we ignore. */
    /* blank lines, lines that start with '!' or '*', lines that */
    /* start with "END". */
    if (!NAMD_blank_string(buffer) &&
        (strncmp(first_word, "!", 1) != 0) &&
         (strncmp(first_word, "*", 1) != 0) &&
        (strncasecmp(first_word, "END", 3) != 0))
    {
      /* Now, determine the apropriate parameter type. */
      if (strncasecmp(first_word, "bond", 4)==0)
      {
        par_type=1; skipline=1;
      }
      else if (strncasecmp(first_word, "angl", 4)==0)
      {
        par_type=2; skipline=1;
      }
      else if (strncasecmp(first_word, "thet", 4)==0)
      {
        par_type=2; skipline=1;
      }
      else if (strncasecmp(first_word, "dihe", 4)==0)
      {
        par_type=3; skipline=1;
      }
      else if (strncasecmp(first_word, "phi", 3)==0)
      {
        par_type=3; skipline=1;
      }
      else if (strncasecmp(first_word, "impr", 4)==0)
      {
        par_type=4; skipline=1;
      }
      else if (strncasecmp(first_word, "imph", 4)==0)
      {
        par_type=4; skipline=1;
      }
      else if (strncasecmp(first_word, "nbon", 4)==0)
      {
        par_type=5; skipline=1;
      }
      else if (strncasecmp(first_word, "nonb", 4)==0)
      {
        par_type=5; skipline=1;
      }
      else if (strncasecmp(first_word, "nbfi", 4)==0)
      {
        par_type=6; skipline=1;
      }
      else if (strncasecmp(first_word, "hbon", 4)==0)
      {
        par_type=7; skipline=1;
      }
      else if ((strncasecmp(first_word, "nbxm", 4) == 0) ||
               (strncasecmp(first_word, "grou", 4) == 0) ||
               (strncasecmp(first_word, "cdie", 4) == 0) ||
               (strncasecmp(first_word, "shif", 4) == 0) ||
               (strncasecmp(first_word, "vgro", 4) == 0) ||
               (strncasecmp(first_word, "vdis", 4) == 0) ||
               (strncasecmp(first_word, "vswi", 4) == 0) ||
               (strncasecmp(first_word, "cutn", 4) == 0) ||
               (strncasecmp(first_word, "ctof", 4) == 0) ||
               (strncasecmp(first_word, "cton", 4) == 0) ||
               (strncasecmp(first_word, "eps", 3) == 0) ||
               (strncasecmp(first_word, "e14f", 4) == 0) ||
               (strncasecmp(first_word, "wmin", 4) == 0) ||
               (strncasecmp(first_word, "aexp", 4) == 0) ||
               (strncasecmp(first_word, "rexp", 4) == 0) ||
               (strncasecmp(first_word, "haex", 4) == 0) ||
               (strncasecmp(first_word, "aaex", 4) == 0) ||
               (strncasecmp(first_word, "noac", 4) == 0) ||
               (strncasecmp(first_word, "hbno", 4) == 0) ||
               (strncasecmp(first_word, "cuth", 4) == 0) ||
               (strncasecmp(first_word, "ctof", 4) == 0) ||
               (strncasecmp(first_word, "cton", 4) == 0) ||
               (strncasecmp(first_word, "cuth", 4) == 0) ||
               (strncasecmp(first_word, "ctof", 4) == 0) ||
               (strncasecmp(first_word, "cton", 4) == 0) )
      {
        if ((par_type != 5) && (par_type != 6) && (par_type != 7))
        {
          char err_msg[512];

          sprintf(err_msg, "ERROR IN CHARMM PARAMETER FILE
%s\nLINE=*%s*",fname, buffer);
          NAMD_die(err_msg);
        }
        else
        {
          skipline = 1;
        }
      }
      else if (par_type == 0)
      {
        /* This is an unknown paramter. */
        /* This is BAD */
        char err_msg[512];

        sprintf(err_msg, "UNKNOWN PARAMETER IN CHARMM PARAMETER FILE
%s\nLINE=*%s*",fname, buffer);
        NAMD_die(err_msg);
      }
    }
    else
    {
      skipline=1;
    }

    if ( (par_type != 0) && (!skipline) )
    {
      /* Now, call the appropriate function based */
      /* on the type of parameter we have */
      /* I know, this should really be a switch ... */
      if (par_type == 1)
      {
        add_bond_param(buffer);
        NumBondParams++;
      }
      else if (par_type == 2)
      {
        add_angle_param(buffer);
        NumAngleParams++;
      }
      else if (par_type == 3)
      {
        add_dihedral_param(buffer, pfile);
        NumDihedralParams++;
      }
      else if (par_type == 4)
      {
        add_improper_param(buffer, pfile);
        NumImproperParams++;
      }
      else if (par_type == 5)
      {
        add_vdw_param(buffer);
        NumVdwParams++;
      }
      else if (par_type == 6)
      {
        add_vdw_pair_param(buffer);
        NumVdwPairParams++;
      }
      else if (par_type == 7)
      {
        add_hb_pair_param(buffer);
      }
      else
      {
        /* This really should not occour! */
        /* This is an internal error. */
        /* This is VERY BAD */
        char err_msg[512];

        sprintf(err_msg, "INTERNAL ERROR IN CHARMM PARAMETER FILE
%s\nLINE=*%s*",fname, buffer);
        NAMD_die(err_msg);
      }
    }
  }

  /* Close the file */
  fclose(pfile);

  return;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

This archive was generated by hypermail 2.1.6 : Wed Feb 29 2012 - 15:43:28 CST