~vcs-imports/xdrawchem/1.9.9

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// moldata.h -- class MolData knows how to calculate molecular weights
// and partial charges

#ifndef MOLDATA_H
#define MOLDATA_H

#include <qstring.h>
#include <qstringlist.h>
#include <iostream>
using std::cout;
using std::endl;

class MolData {
 public:
  static double NameToMW(QString e) {
    if (e.upper() == QString("H")) return 1.00794;
    if (e.upper() == QString("HE")) return 4.0026;
    if (e.upper() == QString("LI")) return 6.941;
    if (e.upper() == QString("B")) return 10.811;
    if (e.upper() == QString("C")) return 12.011;
    if (e.upper() == QString("N")) return 14.0067;
    if (e.upper() == QString("O")) return 15.9994;
    if (e.upper() == QString("F")) return 18.9984;
    if (e.upper() == QString("NA")) return 22.98977;
    if (e.upper() == QString("MG")) return 24.305;
    if (e.upper() == QString("SI")) return 28.0855;
    if (e.upper() == QString("P")) return 30.97376;
    if (e.upper() == QString("S")) return 32.066;
    if (e.upper() == QString("CL")) return 35.4527;
    if (e.upper() == QString("K")) return 39.0983;
    if (e.upper() == QString("SE")) return 78.96;
    if (e.upper() == QString("BR")) return 79.904;
    if (e.upper() == QString("I")) return 126.9045;
    return 6.0;
  }
  static double MW(QString x) { 
    cout << "Request for MW of " << x << endl;
    // parse this string
    QString iso;  // isotope MW
    QString thiselement;  // current element
    QString repeatnum;  // number of repeats
    unsigned int ptr = 0;  // cursor position (pointer) in x
    int isoflag = false;  // isotope-override normal MW lookup if MW specified
    double this_mw; // MW of this element
    int repeat = 1; // number of this atom
    double mw_out = 0.0;  // MW returned
    QStringList tokens;
    do {
      iso.remove(0,999);
      thiselement.remove(0,999);
      repeatnum.remove(0,999);
      // Check if token starts with a number
      if (x.at(ptr).isNumber() == true) { // read isotope value
	isoflag = true;
	iso.append(x.at(ptr));
	ptr++;
	if (x.at(ptr).isNumber() == true) {
	  iso.append(x.at(ptr));
	  ptr++;
	}
	if (x.at(ptr).isNumber() == true) {
	  iso.append(x.at(ptr));
	  ptr++;
	}
      }
      // ptr now points to first letter of element
      thiselement.append(x.at(ptr));
      ptr++;
      // if next letter is lowercase, add it too
      if (x.at(ptr).category() == QChar::Letter_Lowercase) {
	thiselement.append(x.at(ptr));
	ptr++;
      }
      // if next character is number, it's the subscript
      if (x.at(ptr).isNumber() == true) {
	repeatnum.append(x.at(ptr));
	ptr++;
	if (x.at(ptr).isNumber() == true) {
	  repeatnum.append(x.at(ptr));
	  ptr++;
	}
	if (x.at(ptr).isNumber() == true) {
	  repeatnum.append(x.at(ptr));
	  ptr++;
	}
	repeat = repeatnum.toInt();
      }
      // Move to next letter/number
      if (ptr < x.length()) {
	if (x.at(ptr).isSpace() == true) ptr++;
      }
      if (isoflag)
	this_mw = iso.toDouble();
      else
	this_mw = NameToMW(thiselement);
      // add to molecular weight
      mw_out = mw_out + (repeat * this_mw);
      isoflag = false;
      repeat = 1;
    } while (ptr < x.length());
    cout << "MW = " << mw_out << endl;
    return mw_out;
  }
  // How many bonds (hydrogens) should this element or group have?
  static int Hydrogens(QString x) {
    //cout << "Request for bonds/hydrogens to |" << x << "|" << endl;
    // first check 'special' patterns
    if (x.upper().contains("TIPS") == 1) return 1;
    if (x.upper().contains("TMS") == 1) return 1;
    if (x.upper().contains("MS") == 1) return 1;
    if (x == "CO") return 2;
    if (x == "SO") return 2;
    if (x == "AcO") return 1;
    if (x == "MeO") return 1;
    if (x == "EtO") return 1;
    if (x == "TsO") return 1;
    if (x == "BnO") return 1;
    if (x == "BzO") return 1;
    if (x == "TBSO") return 1;
    if (x == "OAc") return 1;
    if (x == "OMe") return 1;
    if (x == "OEt") return 1;
    if (x == "OTs") return 1;
    if (x == "OBn") return 1;
    if (x == "OBz") return 1;
    if (x == "OTBS") return 1;
    if (x == "NO2") return 1;
    if (x == "O2N") return 1;
    if (x == "CH3") return 1;
    if (x == "OH") return 1;
    if (x == "HO") return 1;
    if (x == "CHO") return 1;
    if (x == "COOH") return 1;
    if (x == "HOOC") return 1;
    if (x == "CN") return 1;
    if (x == "NH2") return 1;
    if (x == "H2N") return 1;
    if (x == "NH") return 2;
    if (x == "HN") return 2;
    if (x == "O-") return 1;
    if (x == "-O") return 1;
    int flag = false;
    int ptr = 0;
    QString firstelement;  // element to consider
    // Find first letter
    do {
      if (x.at(ptr).isLetter() == false) 
	ptr++;
      else
	flag = true;
    } while (flag == false);
    firstelement.append(x.at(ptr));
    ptr++;
    if (x.at(ptr).category() == QChar::Letter_Lowercase) {
      firstelement.append(x.at(ptr));
      ptr++;
    }
    if (x.at(ptr).category() == QChar::Letter_Lowercase) {
      firstelement.append(x.at(ptr));
      ptr++;
    }
    if (firstelement.upper() == QString("H")) return 1;
    if (firstelement.upper() == QString("HE")) return 0;
    if (firstelement.upper() == QString("LI")) return 1;
    if (firstelement.upper() == QString("B")) return 3;
    if (firstelement.upper() == QString("C")) return 4;
    if (firstelement.upper() == QString("N")) return 3;
    if (firstelement.upper() == QString("O")) return 2;
    if (firstelement.upper() == QString("F")) return 1;
    if (firstelement.upper() == QString("NA")) return 1;
    if (firstelement.upper() == QString("MG")) return 2;
    if (firstelement.upper() == QString("SI")) return 4;
    if (firstelement.upper() == QString("CL")) return 1;
    if (firstelement.upper() == QString("K")) return 1;
    if (firstelement.upper() == QString("SE")) return 2;
    if (firstelement.upper() == QString("BR")) return 1;
    if (firstelement.upper() == QString("I")) return 1;
    // these could cause problems
    if (firstelement.upper() == QString("P")) return 3;
    if (firstelement.upper() == QString("S")) return 2;
    // if not known...
    return 1;
  }

  static double partialCharge(QString ident, int method) {
    double ret_pc;

    ret_pc = 0.0;

    return ret_pc;
  }

  static double bondLength(int el1, int el2, int order) {
    // make el1 the smaller element (by atomic number)
    int swp;
    if (el1 > el2) { swp = el1; el1 = el2; el2 = swp; }

    if (order == 1) {
      if (el1 == 1) {
	if (el2 == 6) {
	  return 1.09;
	}
	if (el2 == 7) {
	  return 1.01;
	}
	if (el2 == 8) {
	  return 0.96;
	}
      }
      if (el1 == 6) {
	if (el2 == 6) {
	  return 1.54;
	}
	if (el2 == 7) {
	  return 1.47;
	}
	if (el2 == 8) {
	  return 1.43;
	}
	if (el2 == 9) {
	  return 1.35;
	}
	if (el2 == 16) {
	  return 1.82;
	}
	if (el2 == 17) {
	  return 1.77;
	}
	if (el2 == 35) {
	  return 1.94;
	}
	if (el2 == 53) {
	  return 2.14;
	}
      }
      if (el1 == 7) {
	if (el2 == 8) {
	}
      }
    }

    if (order == 2) {
      if (el1 == 6) {
	if (el2 == 6) {
	  return 1.34;
	}
	if (el2 == 7) {
	}
	if (el2 == 8) {
	}
      }
    }

    if (order == 3) {
      if (el1 == 6) {
	if (el2 == 6) {
	  return 1.20;
	}
	if (el2 == 7) {
	}
      }
    }

    return 1.54;
  }

  static double bondAngleDegrees(int el1, int neighbors) {
    if (el1 == 6) {
      if (neighbors == 1) return 0.0;
      if (neighbors == 2) return 180.0;
      if (neighbors == 3) return 120.0;
      if (neighbors == 4) return 109.47;
    }
    if (el1 == 7) {
      if (neighbors == 1) return 180.0;
      if (neighbors == 2) return 120.0;
      if (neighbors == 3) return 109.47;      
    }
    if (el1 == 8) {
      if (neighbors == 1) return 180.0;
      if (neighbors == 2) return 120.0;
    }
    if (el1 == 14) {
      if (neighbors == 1) return 0.0;
      if (neighbors == 2) return 180.0;
      if (neighbors == 3) return 120.0;
      if (neighbors == 4) return 109.47;
    }
    if (el1 == 15) {
      if (neighbors == 1) return 180.0;
      if (neighbors == 2) return 120.0;
      if (neighbors == 3) return 109.47;      
    }
    if (el1 == 16) {
      if (neighbors == 1) return 180.0;
      if (neighbors == 2) return 120.0;
    }

    return 109.47;
  }

  // important!  maht.h expects angles in radians!
  static double bondAngle(int el1, int neighbors) {
    double retvald, retval;
    retvald = bondAngleDegrees(el1, neighbors);
    retval = retvald * 3.14159 / 180.0;
    return retval;
  }
};

#endif