00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "ReadMDLMol.h"
00030 #include "Molecule.h"
00031 #include "Atom.h"
00032
00033 int read_mdl_header(FILE *infile, int *natoms, int *nbonds)
00034 {
00035
00036 int i;
00037 for (int count = 0; count < 3; count++) {
00038 while (1) {
00039 i = fgetc(infile);
00040
00041 if (i == EOF) {
00042 return 0;
00043 }
00044 if (i == '\n') {
00045 break;
00046 }
00047 }
00048 }
00049
00050 i = fscanf(infile, "%d %d\n", natoms, nbonds);
00051 if (i != 2) {
00052 return 0;
00053 }
00054 return 1;
00055 }
00056
00057 int write_mdl_atom(FILE *outfile, const mdl_atom& atom)
00058 {
00059
00060 int i = fprintf(outfile,
00061 "%10.4f%10.4f%10.4f %-3s%2d%3d%3d%3d%3d%3d\n",
00062 atom.x, atom.y, atom.z,
00063 atom.symbol, atom.mass_difference,
00064 atom.charge, atom.stereo_parity,
00065 atom.hcount, atom.stereo_care_box,
00066 atom.valence);
00067 return i > 0;
00068
00069 }
00070
00071 int read_mdl_atom(FILE *infile, mdl_atom *atom)
00072 {
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 char s[201];
00093 if (!fgets(s, 200, infile)) {
00094 return 0;
00095 }
00096
00097 int i = sscanf(s, "%10f%10f%10f %3s%2d%3d%3d%3d%3d%3d",
00098 &(atom->x), &(atom->y), &(atom->z),
00099 &(atom->symbol), &(atom->mass_difference),
00100 &(atom->charge), &(atom->stereo_parity),
00101 &(atom->hcount), &(atom->stereo_care_box),
00102 &(atom->valence));
00103
00104 return i >= 8;
00105 }
00106
00107 int read_mdl_bond(FILE *infile, mdl_bond *bond)
00108 {
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 char s[201];
00124 if (!fgets(s, 200, infile)) {
00125 return 0;
00126 }
00127 int i = sscanf(s, "%3d%3d%3d%3d",
00128 &(bond->bond_from), &(bond->bond_to),
00129 &(bond->bond_type), &(bond->bond_stereo)
00130 );
00131 return i == 4;
00132 }
00133 int write_mdl_bond(FILE *infile, const mdl_bond& bond)
00134 {
00135 int i = fprintf(infile, "%3d%3d%3d%3d\n",
00136 bond.bond_from, bond.bond_to,
00137 bond.bond_type, bond.bond_stereo
00138 );
00139 return i > 0;
00140 }
00141
00142 int write_mdl_trailer(FILE *outfile)
00143 {
00144 int i = fprintf(outfile, "\n%%%%\n");
00145 return i > 0;
00146 }
00147
00148
00149 int molecule_mdl(Molecule *mol, const char *molfile) {
00150 FILE *infile = fopen(molfile, "rt");
00151 if (!infile) {
00152 return FALSE;
00153 }
00154 int num_atoms, num_bonds;
00155
00156 if (!read_mdl_header(infile, &num_atoms, &num_bonds)) {
00157 fclose(infile);
00158 return FALSE;
00159 }
00160 int i;
00161
00162 mdl_atom atom;
00163 float pos[3];
00164
00165 float newdata[ATOMEXTRA];
00166 newdata[ATOMBETA] = mol->default_beta();
00167 newdata[ATOMOCCUP] = mol->default_occup();
00168
00169 for (i=0; i<num_atoms; i++) {
00170 if (!read_mdl_atom(infile, &atom)) {
00171 fclose(infile);
00172 return FALSE;
00173 }
00174 pos[0] = atom.x;
00175 pos[1] = atom.y;
00176 pos[2] = atom.z;
00177
00178 newdata[ATOMCHARGE] = (float) 4.0 - atom.charge;
00179 newdata[ATOMMASS] = mol->default_mass(atom.symbol) + atom.mass_difference;
00180 newdata[ATOMRAD] = mol->default_radius(atom.symbol);
00181 mol->add_atom(atom.symbol, atom.symbol, "UNK", 0, " ", "UNK", pos,
00182 newdata);
00183 }
00184
00185 mdl_bond bond;
00186 for (i=0; i<num_bonds; i++) {
00187 if (!read_mdl_bond(infile, &bond)) {
00188 fclose(infile);
00189 return FALSE;
00190 }
00191 int bf = bond.bond_from - 1;
00192 int bt = bond.bond_to - 1;
00193 if (bf >= 0 && bf < num_atoms &&
00194 bt >= 0 && bt < num_atoms) {
00195 mol->add_bond(bond.bond_from, bond.bond_to);
00196 }
00197 }
00198
00199 fclose(infile);
00200 return TRUE;
00201 }
00202
00203
00204
00205