File: smilesParser.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 (236 lines) | stat: -rw-r--r-- 4,997 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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/STRUCTURE/smilesParser.h>
#include <BALL/KERNEL/PTE.h>

#include <algorithm>

// defined in the lexer (smilesParserLexer.l)
extern void SmilesParser_initBuffer(const char* buf);
extern void SmilesParser_delBuffer();
extern void SmilesParser_destroy();
extern int SmilesParserparse();

namespace BALL
{
	SmilesParser::SPAtom::SPAtom(const String& symbol, bool in_brackets)
		:	Atom(), 
			isotope_(0),
			formal_charge_(0),
			chirality_(SmilesParser::NONCHIRAL, 0),
			is_aromatic_(false),
			in_brackets_(in_brackets)
	{
		setAromatic(islower(symbol[0]) != 0);
		String s(symbol);
		s.toUpper(0, 1);
		setElement(PTE[s]);
	}
	
	SmilesParser::SPAtom::~SPAtom()
	{
	}

	Size SmilesParser::SPAtom::getDefaultValence() const
	{
		switch (getElement().getAtomicNumber())
		{
			case  1: return 1; // hydrogen
			case  5: return 3; // boron
			case  6: return 4; // carbon
			case  7: return 3; // nitrogen
			case  8: return 2; // oxygen
			case 15: return 3; // phosphorus
			case 16: return 2; // sulfur
			case  9:
			case 17:
			case 35:
			case 53: return 1; // halogens
			default:
				break;
		};
		return 0;
	}

	Size SmilesParser::SPAtom::countRealValences() const
	{
		Size count_non_aromatic = 0;
		Size count_aromatic = 0;
		for (Position i = 0; i < countBonds(); ++i)
		{
			Size order = getBond(i)->getOrder();

			if (order == Bond::ORDER__AROMATIC)
				count_aromatic += 3;
			else 
				count_non_aromatic += order;
		}
		
		// correct for the missing "half-valence" for each aromatic bond
		Size count = count_non_aromatic + count_aromatic/2;
		
		return count;
	}

	SmilesParser::SmilesParser()
		:	system_(),
			connections_(),
			all_atoms_()
	{
	}

	SmilesParser::SmilesParser(const SmilesParser& parser)
		:	system_(parser.system_),
			connections_(),
			all_atoms_()
	{
	}

	SmilesParser::~SmilesParser()
	{
		for (Position i = 0; i < all_atoms_.size(); i++)
		{
			all_atoms_[i]->destroy();
			delete all_atoms_[i];
			all_atoms_[i] = 0;
		}
		all_atoms_.clear();

		system_.destroy();

		SmilesParser_destroy();
	}

	const System& SmilesParser::getSystem() const
	{
		return system_;
	}

	void SmilesParser::parse(const String& s)
	{
		// clear out previous atoms
		for (Position i = 0; i < all_atoms_.size(); i++)
		{
			all_atoms_[i]->destroy();
			delete all_atoms_[i];
		}
		all_atoms_.clear();
		system_.destroy();

		// setup all connections
		connections_.resize(MAX_CONNECTIONS);
		std::fill(connections_.begin(), connections_.end(), (SPAtom*)0);

		// make the internals of this parser available for all
		state.current_parser = this;
		state.buffer = s.c_str();
		state.char_count = 0;
		
		try
		{
			SmilesParser_initBuffer(state.buffer);
			SmilesParserparse();
			SmilesParser_delBuffer();	
		}
		catch (Exception::ParseError& e)
		{
			// Clean up the parser buffer.
			SmilesParser_delBuffer();

			// Clean up allocated memory (atoms).
			for (Position i = 0; i < all_atoms_.size(); i++)
			{
				all_atoms_[i]->destroy();
				delete all_atoms_[i];
			}
			all_atoms_.clear();

			// Propagate the parse error upwards.
			throw e;
		}		

		// fill up empty valences with hydrogens
		addMissingHydrogens();

		// Transfer all atoms into a new molecule.
		Molecule* molecule = new Molecule;
		system_.insert(*molecule);
		for (Position i = 0; i < all_atoms_.size(); i++)
		{
			molecule->insert(*all_atoms_[i]);
		}
		
		// Clean up the pointers to these atoms.
		all_atoms_.clear();
	}
	
	void SmilesParser::addMissingHydrogens()
	{
		for (Position i = 0; i < all_atoms_.size(); i++)
		{
			SPAtom& atom(*(all_atoms_[i]));	
			while (!atom.isInBrackets() && (atom.countRealValences() < (atom.getDefaultValence() + atom.getFormalCharge())))
			{
				new SPBond(&atom, createAtom("H"));
			}
		}
	}

	SmilesParser::SPAtom* SmilesParser::createAtom(const String& symbol, bool in_bracket)
	{
		SPAtom* atom = new SPAtom(symbol, in_bracket);
		all_atoms_.push_back(atom);

		return atom;
	}

	SmilesParser::SPBond::SPBond
		(SmilesParser::SPAtom* left, SmilesParser::SPAtom* right, Index order)
		:	Bond(), 
			ze_type_(SmilesParser::NONE)
	{
		left->createBond(*this, *right);
		if (left->isAromatic() && right->isAromatic())
			setOrder(Bond::ORDER__AROMATIC);
		else
			setOrder(order);
	}

	SmilesParser::SPBond::~SPBond()
	{
		destroy();
	}

	SmilesParser::ZEIsomerType SmilesParser::SPBond::getZEType() const
	{
		return ze_type_;
	}

	void SmilesParser::SPBond::setZEType(SmilesParser::ZEIsomerType type)
	{
		ze_type_ = type;
	}

	struct SmilesParser::State SmilesParser::state;
	
	void SmilesParser::createBonds
		(SmilesParser::SPAtom* atom, const SmilesParser::ConnectionList* conns)
	{
		SmilesParser::ConnectionList::const_iterator it = conns->begin();
		for (; it != conns->end(); ++it)
		{
			if (connections_[*it] == 0)
			{
				connections_[*it] = atom;
			}
			else
			{
				new SPBond(atom, connections_[*it], 1);
				connections_[*it] = 0;
			}
		}
	}

} // namespace BALL