-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResParser.cpp
More file actions
364 lines (320 loc) · 11 KB
/
ResParser.cpp
File metadata and controls
364 lines (320 loc) · 11 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: ResParser.cpp
* Author: tg
*
* Created on June 6, 2020, 9:56 PM
*/
/* list of shelxl commands according to
* http://shelx.uni-goettingen.de/shelxl_html.php
* ABIN ACTA AFIX ANIS ANSC ANSR BASF BIND BLOC BOND
* BUMP CELL CGLS CHIV CONF CONN DAMP DANG DEFS DELU
* DFIX DISP EADP END EQIV EXTI EXYZ FEND FLAT FMAP
* FRAG FREE FVAR GRID HFIX HKLF HTAB ISOR LATT LAUE
* LIST L.S. MERG MORE MOVE MPLA NCSY NEUT OMIT PART
* PLAN PRIG REM RESI RIGU RTAB SADI SAME SFAC SHEL
* SIMU SIZE SPEC STIR SUMP SWAT SYMM TEMP TITL TWIN
* TWST UNIT WGHT WIGL WPDB XNPD ZERR
*/
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <stdexcept>
#include "ResParser.h"
#include "myExceptions.h"
#include "Restraint.h"
#include "Eqiv.h"
ResParser::ResParser(const char* resfile, const short& verbosity) : resfile_(resfile), verbosity_(verbosity), eqivs_(255) {
// read res-file
readres(resfile_);
// interpret res-file, i.e. generate restraints
interpres();
if (verbosity > 0) {
std::cout << "---> Number of DFIX or DANG instructions: " << restraints_.size() << "\n";
}
if (restraints_.empty()) {
std::cout << " *** Error: no restraints found\n";
throw myExcepts::Usage("Missing restraints");
}
}
/**
* open res-file, read lines while concatenating continuation lines
* include-files '+' are added just the same
*/
void ResParser::readres(const std::string& filename) {
std::ifstream inp(filename.c_str());
if (!inp.is_open()) {
std::cout << "*** Error: cannot open res-file " << resfile_
<< std::endl;
throw myExcepts::FileMissing(resfile_.c_str());
}
while (!inp.eof()) {
std::string myline;
std::getline(inp, myline);
if (inp.eof() || inp.fail()) break;
// check for nested input
if (myline.empty()) continue;
if (myline.at(0) == '+') {
std::istringstream nested(myline);
char plus;
nested >> plus;
if (myline.at(1) == '+') {
nested >> plus;
}
std::string nestedfilename;
nested >> nestedfilename;
readres(nestedfilename);
continue;
}
while (myline.back() == '=') {
std::string contline;
std::getline(inp, contline);
if (inp.eof() || inp.fail()) break;
myline += contline;
}
// skip empty lines
if (!myline.empty()) {
myline = upcase(myline);
reslines_.push_back(myline);
}
continue;
}
inp.close();
if (verbosity_ > 0) {
std::cout << "---> Input of " << std::setw(5) << reslines_.size() << " lines from file "
<< filename << '\n';
}
}
/**
* interpret @c line as PART instruction; expects 'PART' be stripped from line
* @param line
*/
void ResParser::part(const std::string& line) {
std::istringstream conv(line);
int part;
float sof;
conv >> part;
if (conv.fail()) {
std::cout << "*** Error reading PART line\n";
throw myExcepts::ShelxFormat("Part parameter not a number");
}
shelxState_.partnum(part);
conv >> sof;
if (conv.fail()) return;
shelxState_.partsof(sof);
}
void ResParser::resi(const std::string& line) {
std::istringstream conv(line);
int num(0);
std::string first, second;
conv >> first;
if (conv.fail()) {
std::cout << "*** Error: RESI requires at least number of name of residue class.\n";
throw myExcepts::ShelxFormat("RESI missing argument");
}
try {
num = std::stoi(first);
// if this works, it is a resi number
shelxState_.resinumber(num);
} catch (std::invalid_argument& e) {
// conversion did not work, this must be residue name
shelxState_.resiclass(first);
}
// try for second argument
conv >> second;
if (conv.fail()) return; // no second argument
try {
num = std::stoi(second);
// if this works, it is residue number. Double check that previous was
// word, not number
if (shelxState_.resiclass().empty()) {
std::cout << "### Syntax error, 2 residue numbers on RESI:\n"
<< " RESI " << line << '\n';
throw myExcepts::Format("SHELX RESI command");
}
else {
shelxState_.resinumber(num);
}
} catch (std::invalid_argument& e) { // not a number, interpret is word
shelxState_.resiclass(second);
}
if (conv.fail()) {
std::cout << "*** Error: cannot interpret RESI line " << line << "\n";
throw myExcepts::ShelxFormat("RESI format");
}
conv >> num;
if (conv.fail() || conv.eof()) return;
shelxState_.resialias(num);
}
/**
* expect a string that starts with 'CELL' and read values for
* lambda and unit cell parameters
* @param line
*/
void ResParser::getcell(const std::string& line) {
std::istringstream conv(line);
conv >> lambda_ >> a_ >> b_ >> c_
>> alpha_ >> beta_ >> gamma_;
if (conv.fail()) {
std::cout << "*** Error reading wavelength and cell parameters.\n"
<< " exiting.\n";
throw myExcepts::Format(line.c_str());
}
if (verbosity_ > 0) {
std::cout << "---> Cell constants: "
<< std::fixed
<< std::setw(9) << std::setprecision(5) << a_
<< std::setw(9) << std::setprecision(5) << b_
<< std::setw(9) << std::setprecision(5) << c_
<< std::setw(9) << std::setprecision(4) << alpha_
<< std::setw(9) << std::setprecision(4) << beta_
<< std::setw(9) << std::setprecision(4) << gamma_
<< std::endl;
}
}
/**
* Check res-file line by line and interpret
*/
void ResParser::interpres() {
for (std::vector<std::string>::const_iterator it = reslines_.begin();
it != reslines_.end(); ++it) {
if (std::isspace(it->front())) continue;
try {
if (xcmd(*it)) continue;
} catch (myExcepts::ShelxEoF& e) {
break;
}
// not a shelxl command: read as atoms
xatoms_.push_back(xAtom(*it, shelxState_));
}
if (verbosity_) {
std::cout << "---> Read in " << xatoms_.size() << " atoms from ins-file\n";
}
}
/**
* interpret "DANG" or "DFIX" line. Sets default sof depending on DFIX (0.02)
* or DANG (0.04), i.e. instruction must still contain the keyword
* @param line
*/
void ResParser::addrestraint(const std::string& line) {
std::istringstream conv(line.substr(4, std::string::npos));
std::string resi(""), word;
int resinum(0);
float target, sof;
std::vector<std::string> atom_pairs;
float default_sof(0.02);
if (line.substr(0, 4) == "DANG") default_sof = 0.04;
char c = conv.peek();
if (c == '_') conv >> c >> resi;
try {
resinum = std::stoi(resi);
resi = "";
} catch (std::invalid_argument& e) {
// nothing to do
resinum = 0;
}
conv >> target >> word;
try {
sof = std::stof(word);
// if stof does not throuw an error, we can continue
conv >> word;
} catch (std::invalid_argument& e) { // sof not provided, word is firt atom
sof = default_sof;
}
while (!conv.fail()) {
atom_pairs.push_back(word);
conv >> word;
}
// double check: need even number of atom names
if (atom_pairs.size() % 2) {
std::cout << "*** Error: Extracted odd number of atoms from " << line
<< "\n";
throw myExcepts::ShelxFormat("Odd atom names in DFIX/DANG");
}
Restraint myrestraint(target, sof, resinum, resi, atom_pairs);
restraints_.push_back(myrestraint);
}
/**
* process string as shelx-command. If not a shelx command return false
*/
bool ResParser::xcmd(const std::string& instr) {
// don't read beyond HKLF or END
if (instr.substr(0, 3) == "END" || instr.substr(0, 4) == "HKLF") {
throw myExcepts::ShelxEoF("END or HKLF encountered");
}
if (instr.substr(0, 3) == "REM") {
return true;
}
// empty lines were checked earler. If shorter, cannot be a command
if (instr.size() < 4) return false;
const char* cmdlist[] = {"ABIN", "ACTA", "AFIX", "ANIS", "ANSC", "ANSR",
"BASF", "BIND", "BLOC", "BOND", "BUMP", "CELL",
"CGLS", "CHIV", "CONF", "CONN", "DAMP", "DANG",
"DEFS", "DELU", "DFIX", "DISP", "EADP", "END",
"EQIV", "EXTI", "EXYZ", "FEND", "FLAT", "FMAP",
"FRAG", "FREE", "FVAR", "GRID", "HFIX", "HKLF",
"HTAB", "ISOR", "LATT", "LAUE", "LIST", "L.S.",
"MERG", "MORE", "MOVE", "MPLA", "NCSY", "NEUT",
"OMIT", "PART", "PLAN", "PRIG", "REM ", "RESI",
"RIGU", "RTAB", "SADI", "SAME", "SFAC", "SHEL",
"SIMU", "SIZE", "SPEC", "STIR", "SUMP", "SWAT",
"SYMM", "TEMP", "TITL", "TWIN", "TWST", "UNIT",
"WGHT", "WIGL", "WPDB", "XNPD", "ZERR"};
if (instr.substr(0, 4) == "EQIV") {
Eqiv eqiv(instr.substr(4, std::string::npos));
eqivs_[eqiv.n()] = eqiv.symop();
return true;
}
if (instr.substr(0, 4) == "CELL") {
getcell(instr.substr(4, std::string::npos));
return true;
}
if (instr.substr(0, 4) == "DFIX" || instr.substr(0, 4) == "DANG") {
addrestraint(instr);
return true;
}
if (instr.substr(0, 4) == "RESI") {
resi(instr.substr(4, std::string::npos));
return true;
}
if (instr.substr(0, 4) == "PART") {
part(instr.substr(4, std::string::npos));
return true;
}
// check for the remaining commands, not processed
for (int i = 0; i < sizeof (cmdlist) / sizeof (cmdlist[0]); ++i) {
if (instr.substr(0, 4) == cmdlist[i]) {
return true;
};
}
return false;
}
/**
* Create list of numeric restraints
* @return
*/
std::vector<Restraint::Numeric> ResParser::restraints() const {
std::vector<Restraint>::const_iterator it;
std::vector<Restraint::Numeric> numrestr(0);
for (it = restraints_.begin(); it != restraints_.end(); ++it) {
std::vector<Restraint::Numeric> localrestr = it->make(xatoms_, eqivs_);
numrestr.insert(numrestr.end(), localrestr.begin(), localrestr.end());
}
if (verbosity_ > 0) {
std::cout << "---> Total number of restraints to be applied: " << numrestr.size() << '\n';
}
return numrestr;
}
std::string ResParser::upcase(const std::string& word) {
std::string up (word);
for (std::string::iterator it = up.begin(); it != up.end(); ++it) {
*it = std::toupper(*it);
}
return up;
}