File: templates.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 239,924 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (334 lines) | stat: -rw-r--r-- 8,020 bytes parent folder | download | duplicates (7)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/MOLMEC/PARAMETER/templates.h>
#include <BALL/KERNEL/residue.h>

using namespace std;

namespace BALL 
{

	Templates::Templates()
		: ParameterSection(),
			unassigned_atoms_(),
			max_number_unassigned_atoms_(std::numeric_limits<Size>::max())
	{
	}


	Templates::Templates(const Templates& templates, bool /* deep */)
		: ParameterSection(templates),
			unassigned_atoms_(),
			max_number_unassigned_atoms_(templates.max_number_unassigned_atoms_)
	{
		charges_ = templates.charges_;
		type_names_ = templates.type_names_;
	}


	void Templates::clear() 
	{
		charges_.clear();
		type_names_.clear();

		ParameterSection::clear();
		unassigned_atoms_.clear();
		max_number_unassigned_atoms_ = std::numeric_limits<Size>::max();
	}


	Templates::~Templates() 
	{
		clear();
	}

	Templates& Templates::operator = (const Templates& templates)
	{
		charges_.set(templates.charges_);
		type_names_.set(templates.type_names_);
		max_number_unassigned_atoms_ = templates.max_number_unassigned_atoms_;

		return *this;
	}

	bool Templates::extractSection
		(Parameters& parameters, const String& section_name)
	{
		// clean up first
		type_names_.clear();
		charges_.clear();

		// extract the basis information
		if (!ParameterSection::extractSection(parameters, section_name))
		{
			Log.error() << "Didn't find section for " << section_name << endl;
			return false;
		}

		// check for the correct entries:
		if (!hasVariable("q") || !hasVariable("type"))
		{
			Log.error() << "residue template section [" << section_name << "] requires variables q and type." << endl;
			return false;
		}

		// check for the correct unit
		if (!options.has("unit_q") || (options["unit_q"] != "e0"))
		{
			Log.error() << "unknown unit in section [" << section_name 
				<< "]. Please specify charges in multiples of the elementary charge (e0)" << endl;
			return false;
		}

		// iterate over all entries and store charges and types in the
		// corresponding hash maps
		StringHashMap<Index>::Iterator	it;
		for (it = section_entries_.begin(); it != section_entries_.end(); ++it)
		{
			type_names_[it->first] = getValue(it->first, "type");
			charges_[it->first] = getValue(it->first, "q").toFloat();
		}
		
		return true;
	}

	bool Templates::has(const String& name) const 
	{
		return type_names_.has(name);
	}

	float Templates::getCharge(const String& name) const 
	{
		if (charges_.has(name)) 
		{
			return (*charges_.find(name)).second;
		} 
		else 
		{
			return 0.0;
		}
	}

	String Templates::getTypeName(const String& name) const 
	{
		if (type_names_.has(name)) 
		{
			return (*type_names_.find(name)).second;
		} 
		else 
		{
			return "";
		}
	}
	
	void Templates::assign(System& system, bool overwrite_existing_type_names, bool overwrite_non_zero_charges) const
	{
 		(const_cast<Templates*>(this))->unassigned_atoms_.clear();

		// iterate over all atoms
		AtomIterator it = system.beginAtom();
		for (; +it; ++it)
		{
			String name(it->getFullName());
			if (overwrite_non_zero_charges || (it->getCharge() == 0.0))
			{
				if (charges_.has(name))
				{
					it->setCharge(charges_[name]);
				} 
				else 
				{
					// If we could not find the name with the variant extension 
					// (e.g. ALA-C), try the short name (ALA) instead.
					name = it->getFullName(Atom::NO_VARIANT_EXTENSIONS);
					if (charges_.has(name))
					{
						it->setCharge(charges_[name]);
					} 
					else 
					{					
						// try a final wildcard match
						name = "*:" + it->getName();
						name.trim();
						if (charges_.has(name))
						{
							// assign the charge
							it->setCharge(charges_[name]);
						} 
						else
						{
							Log.warn() << "Templates::assign: cannot assign charge for atom " << name << endl;
							(const_cast<Templates*>(this))-> getUnassignedAtoms().insert(&*it);
  						if (getNumberOfUnassignedAtoms() > getMaximumUnassignedAtoms())
							{
								return;
							}
						}
					}
					// Reset the name to avoid influence on the type assignment.
					name = it->getFullName();
				}
			}
			if (overwrite_existing_type_names || (it->getTypeName() == BALL_ATOM_DEFAULT_TYPE_NAME))
			{
				if (type_names_.has(name))
				{
					it->setTypeName(type_names_[name]);
				} 
				else
				{
					// If we could not find the name with the variant extension 
					// (e.g. ALA-C), try the short name (ALA) instead.
					name = it->getFullName(Atom::NO_VARIANT_EXTENSIONS);
					if (type_names_.has(name))
					{
						it->setTypeName(type_names_[name]);
					} 
					else 
					{					
						// try a final wildcard match
						name = "*:" + it->getName();
						name.trim();
						if (type_names_.has(name))
						{
							// assign the charge
							it->setTypeName(type_names_[name]);
						} 
						else
						{
							Log.warn() << "Templates::assign: cannot assign type name for atom " << name << endl;
						}
					}
				}
			}
		}	
	}


	void Templates::assignTypeNames(System& system, bool overwrite_existing_type_names) const
	{
 		(const_cast<Templates*>(this))->unassigned_atoms_.clear();

		// iterate over all atoms
		AtomIterator it = system.beginAtom();
		for (; +it; ++it)
		{
			if ((overwrite_existing_type_names || (it->getTypeName() == BALL_ATOM_DEFAULT_TYPE_NAME)))
			{
				// retrieve the atom's full name
				String name(it->getFullName());
				if (type_names_.has(name))
				{	
					// assign the type name
					it->setTypeName(type_names_[name]);
				} 
				else 
				{
					// try the residue name without variant extension
					name = it->getFullName(Atom::NO_VARIANT_EXTENSIONS);
					if (type_names_.has(name))
					{	
						// assign the type name
						it->setTypeName(type_names_[name]);
					} 
					else 
					{
						// try a final wildcard match
						name = "*:" + it->getName();
						name.trim();
						if (type_names_.has(name))
						{
							// assign the type name
							it->setTypeName(type_names_[name]);
						} 
						else 
						{
							Log.warn() << "Templates::assignTypeNames: cannot assign type name for atom " << it->getFullName() << endl;
							(const_cast<Templates*>(this))-> getUnassignedAtoms().insert(&*it);
  						if (getNumberOfUnassignedAtoms() > getMaximumUnassignedAtoms())
							{
								return;
							}
						}
					}
				}
			}
		}	
	}


	void Templates::assignCharges(System& system, bool overwrite_non_zero_charges) const
	{
 		(const_cast<Templates*>(this))->unassigned_atoms_.clear();

		// iterate over all atoms
		AtomIterator it = system.beginAtom();
		for (; +it; ++it)
		{
			if ((overwrite_non_zero_charges || (it->getTypeName() == BALL_ATOM_DEFAULT_TYPE_NAME)))
			{
				// retrieve the atom's full name
				String name(it->getFullName());
				if (charges_.has(name))
				{	
					// assign the type name
					it->setCharge(charges_[name]);
				} 
				else 
				{
					// try the residue name without variant extension
					name = it->getFullName(Atom::NO_VARIANT_EXTENSIONS);
					if (charges_.has(name))
					{	
						// assign the type name
						it->setCharge(charges_[name]);
					} 
					else 
					{
						// try a final wildcard match
						name = "*:" + it->getName();
						name.trim();
						if (charges_.has(name))
						{
							// assign it
							it->setCharge(charges_[name]);
						} 
						else 
						{
							Log.warn() << "Templates::assignCharges: cannot assign charge for atom " << it->getFullName() << endl;
							(const_cast<Templates*>(this))-> getUnassignedAtoms().insert(&*it);
  						if (getNumberOfUnassignedAtoms() > getMaximumUnassignedAtoms())
							{
								return;
							}
						}
					}
				}
			}
		}	
	}


	void Templates::setMaximumUnassignedAtoms(Size nr)
	{
		max_number_unassigned_atoms_ = nr;
	}
	
	Size Templates::getMaximumUnassignedAtoms() const
	{
		return max_number_unassigned_atoms_;
	}

	Size Templates::getNumberOfUnassignedAtoms() const
	{
		return unassigned_atoms_.size();
	}

	HashSet<const Atom*>& Templates::getUnassignedAtoms()
	{
		return unassigned_atoms_;
	}

} // namespace BALL