File: createSpectrumProcessor.C

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

#include <BALL/NMR/createSpectrumProcessor.h>
#include <BALL/NMR/shiftModule.h>
#include <BALL/DATATYPE/regularData1D.h>
#include <BALL/SYSTEM/path.h>
#include <BALL/KERNEL/PTE.h>
#include <BALL/KERNEL/atom.h>

using namespace std;

namespace BALL
{

	const String CreateSpectrumProcessor::IGNORE_SECTION_NAME =  "IgnoreAtoms";
	const String CreateSpectrumProcessor::AVERAGE_SECTION_NAME =  "AverageAtoms";

	CreateSpectrumProcessor::CreateSpectrumProcessor()
		:	width_(1.0),
			use_averaging_(true),
			use_ignore_table_(true),
			expression_("element(H)")
	{
		// call init to read the default config file
		init();
	}

	CreateSpectrumProcessor::~CreateSpectrumProcessor ()
		
	{
	}

	bool CreateSpectrumProcessor::start()
		
	{
		// clear the contents of the old peak list
		peaklist_.clear();
		
		return valid_;
	}

	void CreateSpectrumProcessor::init()
	{
		ShiftModule::init();
		init("NMR/StandardSpectrum.ini");
	}

	void CreateSpectrumProcessor::init(const String& filename)
	{
		valid_ = false;

		Path path;
		String expanded_filename = path.find(filename);
		if (expanded_filename.empty())
		{
			throw Exception::FileNotFound(__FILE__, __LINE__, filename);
		}

		INIFile infile(expanded_filename);
		infile.read();

		if (!infile.isValid())
		{
			throw Exception::ParseError(__FILE__, __LINE__, "Error while reading INIFile ", expanded_filename);
		}

		
		String name;
		

		if (!infile.hasSection(IGNORE_SECTION_NAME))
		{
			throw Exception::ParseError(__FILE__, __LINE__, "Section not found in INIFile:", String("section ") + IGNORE_SECTION_NAME);
		}
		// Read the contents of the ignore set.
		// This hash set contains all quickly exchanging protons
		// -- those protons are not seen in the spectrum and do
		// thus not produce a peak in the peak list.
		ignore_atoms_.clear();
		INIFile::LineIterator line = infile.getSectionFirstLine(IGNORE_SECTION_NAME);
		INIFile::LineIterator last_line = infile.getSectionLastLine(IGNORE_SECTION_NAME);
		for (; line != last_line; ++line)
		{
			ignore_atoms_.insert(*line);
		}

		// read the list of equivalent protons
		// clear old contents
		equivalency_residues_.clear();
		equivalency_atoms_.clear();
		line = infile.getSectionFirstLine(AVERAGE_SECTION_NAME);
		last_line = infile.getSectionLastLine(AVERAGE_SECTION_NAME);
		for (; +line; ++line)
		{
			String atoms = (*line).after(" ");
			String residue = (*line).before(" ");
			if ((residue != "") && (atoms != ""))
			{
				vector<String> equivalencies;
				atoms.split(equivalencies, ",");
				equivalency_residues_.push_back(residue);
				equivalency_atoms_.push_back(equivalencies);
			}
		}

		// initialization successful
		valid_ = true;

		return;
	}

	Processor::Result CreateSpectrumProcessor::operator () (Composite& composite)
		
	{		
		// Collect all atoms with assigned chemical shifts
		Atom* atom = dynamic_cast<Atom*>(&composite);
		if (atom != 0)
		{
			if (atom->hasProperty(ShiftModule::PROPERTY__SHIFT))
			{
				float shift = atom->getProperty(ShiftModule::PROPERTY__SHIFT).getFloat();
				// if the atom is in the ignore table, skip it
				if ((!(ignore_atoms_.has(atom->getFullName())
							 || ignore_atoms_.has("*:" + atom->getName()))
						 || !use_ignore_table_)
						&& expression_(*atom)
						&& (shift != 0.0))
				{
					Peak1D peak;
					peak.setAtom(atom);
					peak.setPosition(shift);
					peak.setWidth(width_);
					peak.setIntensity(1.0);
					peaklist_.push_back(peak);	
				}
			}	
		}
		

		if (use_averaging_)
		{
			// average the shifts of chemically equivalent nuclei
			// in a residue
			Residue* residue = dynamic_cast<Residue*>(&composite);
			if (residue != 0)
			{
				String residue_name = residue->getName();

				for (Position i = 0; i < equivalency_residues_.size(); i++)
				{
					if (residue_name == equivalency_residues_[i])
					{
						float shift = 0.0;
						list<Atom*> atoms;
						for (AtomIterator it = residue->beginAtom(); +it; ++it)
						{
							for (Position j = 0; j < equivalency_atoms_[i].size(); j++)
							{
								if (equivalency_atoms_[i][j] == it->getName())
								{
									atoms.push_back(&*it);
									shift += it->getProperty(ShiftModule::PROPERTY__SHIFT).getFloat();
									break;
								}
							}
						}
						if (!atoms.empty())
						{
							shift /= (float)atoms.size();
							list<Atom*>::const_iterator list_it = atoms.begin();
							for (; list_it != atoms.end(); ++list_it)
							{
								(*list_it)->setProperty(ShiftModule::PROPERTY__SHIFT, shift);
							}
						}
					}
				}
			}
		}

		return Processor::CONTINUE;
	}

	const PeakList1D& CreateSpectrumProcessor::getPeakList() const
	{
		return peaklist_;
	}

	void CreateSpectrumProcessor::setWidth(float width)
		
	{
		width_ = width;
	}
	
	float CreateSpectrumProcessor::getWidth() const
		
	{
		return width_;
	}

	void CreateSpectrumProcessor::setAtomAveraging(bool flag)
		
	{
		use_averaging_ = flag;
	}

	bool CreateSpectrumProcessor::getAtomAveraging() const
		
	{
		return use_averaging_;
	}

	void CreateSpectrumProcessor::setAtomIgnoring(bool flag)
		
	{
		use_ignore_table_ = flag;
	}
	
	bool CreateSpectrumProcessor::getAtomIgnoring() const
		
	{
		return use_ignore_table_;
	}

	void CreateSpectrumProcessor::setExpression(const String& expression)
		
	{
		expression_.setExpression(expression);
	}

	const String& CreateSpectrumProcessor::getExpression() const
		
	{
		return expression_.getExpressionString();
	}

	const RegularData1D& operator << (RegularData1D& data, const PeakList1D& /* peak_list */)
		
	{
		// ????? implementation missing!

		return data;
	}
}	// namespace BALL