io_mol2.c

来自「最经典的分子对结软件」· C语言 代码 · 共 992 行 · 第 1/2 页

C
992
字号
          "WARNING read_mol2: Incomplete SET record for %s in %s\n",          molecule->info.name, molecule_file_name);        return_value = NULL;        goto terminate;      }      if (token = strtok (white_line (line), " "))        vstrcpy (&molecule->set[i].name, token);      else        token_error = TRUE;      if (!token_error && (token = strtok (NULL, " ")))        vstrcpy (&molecule->set[i].type, token);      else        token_error = TRUE;      if (!token_error && (token = strtok (NULL, " ")))        vstrcpy (&molecule->set[i].obj_type, token);      else        token_error = TRUE;      if (!token_error && (token = strtok (NULL, " ")))      {        vstrcpy (&molecule->set[i].sub_type, token);        if (token = strtok (NULL, " "))        {          vstrcpy (&molecule->set[i].status, token);          while (token = strtok (NULL, " "))          {            if (molecule->set[i].comment)              vstrcat (&molecule->set[i].comment, " ");            vstrcat (&molecule->set[i].comment, token);          }        }      }      if (token_error)      {        fprintf (global.outfile,          "WARNING read_mol2: Error in SET record for %s in %s\n",          molecule->info.name, molecule_file_name);        return_value = NULL;        goto terminate;      }      if (vfgets (&line, molecule_file) == NULL)      {        fprintf (global.outfile,          "WARNING read_mol2: Incomplete SET record for %s in %s\n",          molecule->info.name, molecule_file_name);        return_value = NULL;        goto terminate;      }      if (token = strtok (white_line (line), " "))        molecule->set[i].member_total = atoi (token);      else        token_error = TRUE;      emalloc      (        (void **) &molecule->set[i].member,        molecule->set[i].member_total * sizeof (int),        "molecule set members",        global.outfile      );      for (j = 0; !token_error && (j < molecule->set[i].member_total); j++)      {        if (token = strtok (NULL, " "))        {          if (strcmp (token, "\\"))            molecule->set[i].member[j] = atoi (token) - 1;          else if ((vfgets (&line, molecule_file) == NULL) ||            ((token = strtok (white_line (line), " ")) == NULL))            token_error = TRUE;        }        else          token_error = TRUE;      }      if (token_error)      {        fprintf (global.outfile,          "WARNING read_mol2: Error in SET record for %s in %s\n",          molecule->info.name, molecule_file_name);        return_value = NULL;        goto terminate;      }    }  }/** Upon termination, free up space* 11/96 te*/  terminate:  efree ((void **) &line);  return return_value;}/* ========================================================== */int write_mol2(  MOLECULE *molecule,  FILE *molecule_file){  int i, j, k;			/* Iteration variables */  STRING20 subst_name;		/* String to construct substructure name */  int segment;			/* Segment id */  int *atom_key = NULL;		/* Key for old to new atom ids */  int *bond_key = NULL;		/* Key for old to new bond ids */  int *subst_key = NULL;	/* Key for old to new subst ids */  int atom_total = 0;		/* Total atoms in new set */  int bond_total = 0; 		/* Total bonds in new set */  int subst_total = 0; 		/* Total substs in new set */  long header_position;		/* File position of header information *//** Check if partial structure* 2/98 te  partial_flag = FALSE;  if (molecule_in->total.layers > 0)    for (i = 0; i < molecule_in->total.atoms; i++)      if (((segment = molecule->atom[atom].segment_id) != NEITHER) &&        (molecule->segment[segment].active_flag == FALSE))      {        partial_flag == TRUE;        break;      }  if (partial_flag == TRUE)  {    ecalloc    (      (void **) &molecule,      1,      sizeof (MOLECULE),      "write_mol2 temporary molecule",      global.outfile    );    molecule->total = molecule_in->total;    reallocate_molecule (molecule);    reset_molecule (molecule);  }  else    molecule = molecule_in;*/  fprintf (molecule_file, "\n");  fprintf (molecule_file, "@<TRIPOS>MOLECULE\n");  fprintf (molecule_file, "%s\n",    molecule->info.name ? molecule->info.name : "****");  header_position = ftell (molecule_file);  fprintf (molecule_file, "%-5d %-5d %-5d %-5d %-5d\n",    molecule->total.atoms,    molecule->total.bonds,    molecule->total.substs, 0,    molecule->total.sets);  fprintf (molecule_file, "%s\n",    molecule->info.molecule_type ? molecule->info.molecule_type : "****");  fprintf (molecule_file, "%s\n",    molecule->info.charge_type ? molecule->info.charge_type : "****");  if (molecule->info.status_bits)  {    fprintf (molecule_file, "%s\n", molecule->info.status_bits);    fprintf (molecule_file, "%s\n",      molecule->info.comment ? molecule->info.comment : "****");  }/** Print out atoms* 6/95 te*/  fprintf (molecule_file, "@<TRIPOS>ATOM\n");  for (i = 0; i < molecule->total.atoms; i++)  {/**   Check if only a sub-structure will be written out*   2/98 te*/    if    (      (molecule->total.layers > 0) &&      (        ((segment = molecule->atom[i].segment_id) != NEITHER) &&        (molecule->segment[segment].active_flag == FALSE)      )    )    {      if (atom_key == NULL)      {        ecalloc        (          (void **) &atom_key,          molecule->total.atoms,          sizeof (int),          "write_mol2 atom_key array",          global.outfile        );        ecalloc        (          (void **) &bond_key,          molecule->total.bonds,          sizeof (int),          "write_mol2 bond_key array",          global.outfile        );        ecalloc        (          (void **) &subst_key,          molecule->total.substs,          sizeof (int),          "write_mol2 subst_key array",          global.outfile        );        for (j = 0; j < i; j++)          atom_key[j] = j;        atom_total = i;      }      atom_key[i] = NEITHER;      continue;    }    if (atom_key != NULL)      atom_key[i] = atom_total++;    if ((molecule->total.substs > 0) &&      (molecule->atom[i].subst_id < molecule->total.substs) &&      molecule->subst[molecule->atom[i].subst_id].name &&      strcmp (molecule->subst[molecule->atom[i].subst_id].name, "****"))      sprintf        (subst_name, "%s", molecule->subst[molecule->atom[i].subst_id].name);    else      sprintf (subst_name, "<%d>", molecule->atom[i].subst_id + 1);    fprintf (molecule_file, "%-5d %-5s %9.4f %9.4f %9.4f %-5s %3d %4s %8.4f\n",      atom_key == NULL ? i + 1 : atom_key[i] + 1,      molecule->atom[i].name ? molecule->atom[i].name : "****",      molecule->coord[i][0],      molecule->coord[i][1],      molecule->coord[i][2],      molecule->atom[i].type ? molecule->atom[i].type : "****",      molecule->atom[i].subst_id + 1, subst_name,      molecule->atom[i].charge);    fflush (molecule_file);  }/** Print out bonds* 6/95 te*/  fprintf (molecule_file, "@<TRIPOS>BOND\n");  if (atom_key == NULL)    for (i = 0; i < molecule->total.bonds; i++)    {      fprintf (molecule_file, "%-5d %-5d %-5d %s\n",        i + 1, molecule->bond[i].origin + 1, molecule->bond[i].target + 1,        molecule->bond[i].type ? molecule->bond[i].type : "****");    }  else  {    for (i = 0; i < molecule->total.bonds; i++)    {      if ((atom_key[molecule->bond[i].origin] != NEITHER) &&        (atom_key[molecule->bond[i].target] != NEITHER))      {        bond_key[i] = bond_total++;        fprintf (molecule_file, "%-5d %-5d %-5d %s\n",          bond_total,          atom_key[molecule->bond[i].origin] + 1,          atom_key[molecule->bond[i].target] + 1,          molecule->bond[i].type ? molecule->bond[i].type : "****");      }      else        bond_key[i] = NEITHER;    }  }/** Print out substructures* 6/95 te*/  if (molecule->total.substs > 0)  {    fprintf (molecule_file, "@<TRIPOS>SUBSTRUCTURE\n");    for (i = 0; i < molecule->total.substs; i++)    {      if (atom_key == NULL)        fprintf (molecule_file, "%-3d %-6s %-5d",          i + 1, molecule->subst[i].name, molecule->subst[i].root_atom + 1);      else      {        if (atom_key[molecule->subst[i].root_atom] != NEITHER)          fprintf (molecule_file, "%-3d %-6s %-5d",            i + 1, molecule->subst[i].name,            atom_key[molecule->subst[i].root_atom] + 1);        else        {          for (j = 0; j < molecule->total.atoms; j++)            if ((atom_key[j] != NEITHER) && (molecule->atom[j].subst_id == i))            {              fprintf (molecule_file, "%-3d %-6s %-5d",                i + 1, molecule->subst[i].name,                atom_key[j] + 1);              break;            }/**         Check if no root atoms written for substructure*         2/98 te*/          if (j >= molecule->total.atoms)          {            subst_key[i] = NEITHER;            continue;          }        }        subst_key[i] = subst_total++;      }      if (molecule->subst[i].type)      {        fprintf (molecule_file, " %-10s", molecule->subst[i].type);        fprintf (molecule_file, " %-3d", molecule->subst[i].dict_type + 1);        if (molecule->subst[i].chain)        {          fprintf (molecule_file, " %-5s", molecule->subst[i].chain);          if (molecule->subst[i].sub_type)          {            fprintf (molecule_file, " %-5s", molecule->subst[i].sub_type);            if (molecule->subst[i].inter_bonds)            {              fprintf (molecule_file, " %1d", molecule->subst[i].inter_bonds);              if (molecule->subst[i].status)                fprintf (molecule_file, " %s", molecule->subst[i].status);            }          }        }      }      fprintf (molecule_file, "\n");    }  }/** Print out sets* 10/96 te*/  if (molecule->total.sets > 0)  {    fprintf (molecule_file, "@<TRIPOS>SET\n");    for (i = 0; i < molecule->total.sets; i++)    {      fprintf (molecule_file, "%s %s %s",        molecule->set[i].name,        molecule->set[i].type,        molecule->set[i].obj_type);      if (molecule->set[i].sub_type)      {        fprintf (molecule_file, " %s", molecule->set[i].sub_type);        if (molecule->set[i].status)        {          fprintf (molecule_file, " %s", molecule->set[i].status);          if (molecule->set[i].comment)            fprintf (molecule_file, " %s", molecule->set[i].comment);        }      }      if (atom_key == NULL)      {        fprintf (molecule_file, "\n%d", molecule->set[i].member_total);        for (j = 0; j < molecule->set[i].member_total; j++)          fprintf (molecule_file, " %d", molecule->set[i].member[j] + 1);      }/**     Edit set for non-printed members*     2/98 te*/      else      {        if (!strcmp (molecule->set[i].type, "ATOMS"))        {          for (j = k = 0; j < molecule->set[i].member_total; j++)            if ((molecule->set[i].member[j] < molecule->total.atoms) &&              (atom_key[molecule->set[i].member[j]] != NEITHER))              k++;          fprintf (molecule_file, "\n%d", k);          for (j = 0; j < molecule->set[i].member_total; j++)            if ((molecule->set[i].member[j] < molecule->total.atoms) &&              (atom_key[molecule->set[i].member[j]] != NEITHER))              fprintf (molecule_file, " %d",                atom_key[molecule->set[i].member[j]] + 1);        }        if (!strcmp (molecule->set[i].type, "BONDS"))        {          for (j = k = 0; j < molecule->set[i].member_total; j++)            if ((molecule->set[i].member[j] < molecule->total.bonds) &&              (bond_key[molecule->set[i].member[j]] != NEITHER))              k++;          fprintf (molecule_file, "\n%d", k);          for (j = 0; j < molecule->set[i].member_total; j++)            if ((molecule->set[i].member[j] < molecule->total.bonds) &&              (bond_key[molecule->set[i].member[j]] != NEITHER))              fprintf (molecule_file, " %d",                bond_key[molecule->set[i].member[j]] + 1);        }        if (!strcmp (molecule->set[i].type, "SUBSTS"))        {          for (j = k = 0; j < molecule->set[i].member_total; j++)            if ((molecule->set[i].member[j] < molecule->total.substs) &&              (subst_key[molecule->set[i].member[j]] != NEITHER))              k++;          fprintf (molecule_file, "\n%d", k);          for (j = 0; j < molecule->set[i].member_total; j++)            if ((molecule->set[i].member[j] < molecule->total.substs) &&              (subst_key[molecule->set[i].member[j]] != NEITHER))              fprintf (molecule_file, " %d",                subst_key[molecule->set[i].member[j]] + 1);        }      }      fprintf (molecule_file, "\n");    }  }  fprintf (molecule_file, "\n\n");  if (atom_key != NULL)  {    fseek (molecule_file, header_position, SEEK_SET);    fprintf (molecule_file, "%-5d %-5d %-5d %-5d %-5d\n",      atom_total,      bond_total,      subst_total, 0,      molecule->total.sets);    fseek (molecule_file, 0, SEEK_END);    efree ((void **) &atom_key);    efree ((void **) &bond_key);    efree ((void **) &subst_key);  }  return TRUE;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?