File: MolV3000.cpp

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (263 lines) | stat: -rw-r--r-- 6,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * MOL/SDF V3000 input support for PyMOL.
 *
 * According to:
 * http://c4.cabrillo.edu/404/ctfile.pdf
 * http://infochim.u-strasbg.fr/recherche/Download/Fragmentor/MDL_SDF.pdf
 *
 * (c) Schrodinger, Inc.
 */

#include <string>

#include "os_std.h"

#include "MolV3000.h"

#include "MemoryDebug.h"
#include "Feedback.h"
#include "Parse.h"
#include "Vector.h"
#include "Rep.h"
#include "Lex.h"
#include "strcasecmp.h"
#include "PyMOLGlobals.h"
#include "AtomInfo.h"

/**
 * Read a MOL/SDF V3000 line into the `out` buffer, stripping the leading
 * "M  V30 " and taking line continuation (trailing hyphen) into account.
 *
 * Moves the input pointer `p` to the next line.
 *
 * Return false if line doesn't start with "M  V30 ".
 */
static
bool MOLV3000ReadLine(const char * &p, std::string &out) {
  out.clear();

  for (bool continued = true; continued;) {
    if (strncmp(p, "M  V30 ", 7))
      return false;

    // find beginning of next line
    p += 7;
    auto next = ParseNextLine(p);

    // find end of line
    auto last = next;
    if (last > p && last[-1] == '\n') --last;
    if (last > p && last[-1] == '\r') --last;

    // check for line continuation (hyphen)
    if ((continued = last > p && last[-1] == '-')) {
      --last;
    }

    // append to out buffer
    out.append(p, last);

    p = next;
  }

  return true;
}

/**
 * Parse the next keyword=value pair from `p` and advance `p`.
 */
static
bool MOLV3000ReadKeyValue(const char *& p,
    std::string &key,
    std::string &value)
{
  // skip whitespace
  while (*p && (*p == ' ' || *p == '\t')) ++p;

  auto begin = p;
  auto termchars = " \t";

  // parse key
  for (;; ++p) {
    if (!*p)
      return false;

    if (*p == '=') {
      key.assign(begin, p);
      begin = ++p;
      if (*begin == '(') {
        termchars = ")";
      }
      break;
    }
  }

  // parse value
  while (!strchr(termchars, *p)) ++p;
  if (*begin == '(' && *p == ')') ++p;
  value.assign(begin, p);

  return true;
}

/**
 * Parse the "Extended Connection Table" (V3000) from a MOL/SDF file.
 *
 * Returns a pointer to the end of the parsed content on success, or nullptr on
 * failure.
 *
 * buffer: input buffer, should begin with "M  V30 "
 * atInfo: atom VLA
 * bond:   bond VLA
 * coord:  coordinates VLA
 * nAtom:  output variable for number of atoms
 * nBond:  output variable for number of bonds
 */
const char * MOLV3000Parse(PyMOLGlobals * G,
    const char * buffer,
    AtomInfoType *& atInfo,
    BondType     *& bond,
    float        *& coord,
    int & nAtom,
    int & nBond)
{
  char cc[16]; // buffer for short words like "BEGIN" or "COUNTS"
  bool inside_atom = false;
  bool inside_bond = false;
  bool inside_any  = false;
  int auto_show = RepGetAutoShowMask(G);
  const char * error = nullptr;
  AtomInfoType * ai = nullptr;
  std::string line;
  std::string key, value;

  while (MOLV3000ReadLine(buffer, line)) {
    auto p = line.c_str();

    // save position after "V30"
    auto p_data = p;

    p = ParseWordCopy(cc, p, sizeof(cc));
    bool is_end = (strcasecmp(cc, "END") == 0);

    if (inside_any) {
      // skip
      if (is_end) {
        inside_any = false;
      }
    } else if (inside_atom) {
      if (is_end) {
        inside_atom = false;
        continue;
      }

      int index, p_offset;
      char type[4];
      float xyz[3];
      auto n = sscanf(p_data, "%d %3s %f %f %f%n %*d%n", &index, type,
            xyz, xyz + 1, xyz + 2, &p_offset, &p_offset);

      if (n != 5) {
        error = "failed to parse atom line";
        break;
      }

      p = p_data + p_offset;

      if (index < 1 || index > nAtom) {
        error = "atom index out of range";
        break;
      }

      if (atInfo) {
        ai = atInfo + index - 1;
        ai->name = LexIdx(G, type);
        ai->visRep = auto_show;
        ai->hetatm = true;
        ai->id = index;
        ai->rank = index - 1;

        copy3(xyz, coord + 3 * (index - 1));

        AtomInfoAssignParameters(G, ai);
        AtomInfoAssignColors(G, ai);

        while (MOLV3000ReadKeyValue(p, key, value)) {
          if (key == "CHG") {
            ai->formalCharge = atoi(value.c_str());
          } else if (key == "CFG") {
            ai->stereo = atoi(value.c_str());
          }
        }
      }
    } else if (inside_bond) {
      if (is_end) {
        inside_bond = false;
        continue;
      }

      int index, type, atom1, atom2, p_offset;
      auto n = sscanf(p_data, "%d %d %d %d%n", &index, &type,
          &atom1, &atom2, &p_offset);

      if (n != 4) {
        error = "failed to parse bond line";
        break;
      }

      if (bond) {
        if (index < 1 || index > nBond) {
          error = "bond index out of range";
          break;
        }

        if (type == 7) {
          type = 2; // double or aromatic
        } else if (type > 4) {
          type = 1; // 5: single or double, 6: single or aromatic, 8: any
        }

        BondTypeInit2(bond + index - 1, atom1 - 1, atom2 - 1, type);
      }

      p = p_data + p_offset;
    } else if (strcasecmp(cc, "BEGIN") == 0) {
      p = ParseWordCopy(cc, p, sizeof(cc));
      if (strcasecmp(cc, "CTAB") == 0) {
        // ignore
      } else if (strcasecmp(cc, "ATOM") == 0) {
        inside_atom = true;
      } else if (strcasecmp(cc, "BOND") == 0) {
        inside_bond = true;
      } else {
        inside_any = true;
      }
    } else if (strcasecmp(cc, "COUNTS") == 0) {
      if (sscanf(p, "%d %d", &nAtom, &nBond) != 2) {
        error = "COUNTS parsing failed";
        break;
      } else {
        if(atInfo)
          VLACheck(atInfo, AtomInfoType, nAtom);
        if(coord)
          VLACheck(coord, float, 3 * nAtom);
        if(bond)
          VLACheck(bond, BondType, nBond);
      }
    } else {
      // ignore
    }
  }

  if (!error && (inside_atom || inside_bond)) {
    error = "expected 'M  V30'";
  }

  if (error) {
    PRINTFB(G, FB_ObjectMolecule, FB_Errors)
      " MOL-V3000-Error: %s.\n", error ENDFB(G);
    return nullptr;
  }

  return buffer;
}